|
| 1 | +/** |
| 2 | + * Patches TracingChannel.prototype to handle APIPromise and other Promise subclasses |
| 3 | + * that change the constructor signature (violating the species contract). |
| 4 | + * |
| 5 | + * node:diagnostics_channel's tracePromise wraps the result with Promise.resolve(), |
| 6 | + * which calls the subclass constructor with the wrong signature for classes like |
| 7 | + * Anthropic's APIPromise. This patch uses duck-typing (.then check) instead. |
| 8 | + * |
| 9 | + * This is applied both in the loader hook (hook.mts) for the --import path, |
| 10 | + * and in configureNode/configureBrowser for the bundler plugin path. |
| 11 | + */ |
| 12 | + |
| 13 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 14 | +export function patchTracingChannel( |
| 15 | + tracingChannelFn: (name: string) => any, |
| 16 | +): void { |
| 17 | + const dummyChannel = tracingChannelFn("__braintrust_probe__"); |
| 18 | + const TracingChannel = dummyChannel?.constructor; |
| 19 | + |
| 20 | + if (!TracingChannel?.prototype) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + if ( |
| 25 | + !Object.getOwnPropertyDescriptor(TracingChannel.prototype, "hasSubscribers") |
| 26 | + ) { |
| 27 | + Object.defineProperty(TracingChannel.prototype, "hasSubscribers", { |
| 28 | + configurable: true, |
| 29 | + enumerable: false, |
| 30 | + get(this: { |
| 31 | + start?: { hasSubscribers?: boolean }; |
| 32 | + end?: { hasSubscribers?: boolean }; |
| 33 | + asyncStart?: { hasSubscribers?: boolean }; |
| 34 | + asyncEnd?: { hasSubscribers?: boolean }; |
| 35 | + error?: { hasSubscribers?: boolean }; |
| 36 | + }) { |
| 37 | + return Boolean( |
| 38 | + this.start?.hasSubscribers || |
| 39 | + this.end?.hasSubscribers || |
| 40 | + this.asyncStart?.hasSubscribers || |
| 41 | + this.asyncEnd?.hasSubscribers || |
| 42 | + this.error?.hasSubscribers, |
| 43 | + ); |
| 44 | + }, |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + if (TracingChannel.prototype.tracePromise) { |
| 49 | + TracingChannel.prototype.tracePromise = function ( |
| 50 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 51 | + fn: any, |
| 52 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 53 | + context: any = {}, |
| 54 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 55 | + thisArg: any, |
| 56 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 57 | + ...args: any[] |
| 58 | + ) { |
| 59 | + const { start, end, asyncStart, asyncEnd, error } = this; |
| 60 | + |
| 61 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 62 | + function reject(err: any) { |
| 63 | + context.error = err; |
| 64 | + error?.publish(context); |
| 65 | + asyncStart?.publish(context); |
| 66 | + asyncEnd?.publish(context); |
| 67 | + return Promise.reject(err); |
| 68 | + } |
| 69 | + |
| 70 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 71 | + function resolve(result: any) { |
| 72 | + context.result = result; |
| 73 | + asyncStart?.publish(context); |
| 74 | + asyncEnd?.publish(context); |
| 75 | + return result; |
| 76 | + } |
| 77 | + |
| 78 | + start?.publish(context); |
| 79 | + |
| 80 | + try { |
| 81 | + // PATCHED: duck-type thenable instead of instanceof Promise + Promise.resolve(). |
| 82 | + // This allows APIPromise and other Promise subclasses to work correctly — |
| 83 | + // Promise.resolve() on a subclass with a non-standard constructor signature |
| 84 | + // triggers the species protocol and calls the constructor, which throws. |
| 85 | + const result = Reflect.apply(fn, thisArg, args); |
| 86 | + |
| 87 | + if ( |
| 88 | + result && |
| 89 | + (typeof result === "object" || typeof result === "function") && |
| 90 | + typeof result.then === "function" |
| 91 | + ) { |
| 92 | + return result.then(resolve, reject); |
| 93 | + } |
| 94 | + |
| 95 | + context.result = result; |
| 96 | + asyncStart?.publish(context); |
| 97 | + asyncEnd?.publish(context); |
| 98 | + return result; |
| 99 | + } catch (err) { |
| 100 | + context.error = err; |
| 101 | + error?.publish(context); |
| 102 | + throw err; |
| 103 | + } finally { |
| 104 | + end?.publish(context); |
| 105 | + } |
| 106 | + }; |
| 107 | + } |
| 108 | +} |
0 commit comments