|
| 1 | +/** |
| 2 | + * A browser stand-in for `node:diagnostics_channel`. |
| 3 | + * |
| 4 | + * Orchestrion's transform injects `import dc from '<dcModule>'` into every instrumented module and |
| 5 | + * drives it through Node's `tracingChannel` API. The browser has no `node:diagnostics_channel`, so |
| 6 | + * without this shim the transform cannot run on browser code. |
| 7 | + * |
| 8 | + * This implements exactly what the injected code calls, which is not the `traceSync` helpers but the |
| 9 | + * per-event channels under them: |
| 10 | + * |
| 11 | + * ```js |
| 12 | + * // The gate is true when ANY of the five event channels has a subscriber, so subscribing to `end` |
| 13 | + * // alone is enough to get the call wrapped. |
| 14 | + * if (!(ch.start.hasSubscribers || ch.end.hasSubscribers || ch.asyncStart.hasSubscribers |
| 15 | + * || ch.asyncEnd.hasSubscribers || ch.error.hasSubscribers)) return traced(); |
| 16 | + * return ch.start.runStores(ctx, () => { |
| 17 | + * try { ctx.result = traced(); } |
| 18 | + * catch (err) { ctx.error = err; ch.error.publish(ctx); throw err; } |
| 19 | + * finally { ch.end.publish(ctx); } |
| 20 | + * }); |
| 21 | + * ``` |
| 22 | + * |
| 23 | + * The browser has no `AsyncLocalStorage`, so `runStores` publishes and then calls the callback directly |
| 24 | + * instead of entering a store. The injected code does not need a store: it passes state along on the |
| 25 | + * `context` object. |
| 26 | + */ |
| 27 | + |
| 28 | +type Handler = (context: unknown) => void; |
| 29 | + |
| 30 | +interface Channel { |
| 31 | + readonly hasSubscribers: boolean; |
| 32 | + publish(context: unknown): void; |
| 33 | + runStores<T>(context: unknown, fn: (...args: unknown[]) => T, thisArg?: unknown, ...args: unknown[]): T; |
| 34 | + subscribe(handler: Handler): void; |
| 35 | + unsubscribe(handler: Handler): void; |
| 36 | +} |
| 37 | + |
| 38 | +const EVENTS = ['start', 'end', 'asyncStart', 'asyncEnd', 'error'] as const; |
| 39 | +type EventName = (typeof EVENTS)[number]; |
| 40 | + |
| 41 | +export type TracingChannelSubscribers = Partial<Record<EventName, Handler>>; |
| 42 | + |
| 43 | +export interface BrowserTracingChannel extends Record<EventName, Channel> { |
| 44 | + subscribe(subscribers: TracingChannelSubscribers): void; |
| 45 | + unsubscribe(subscribers: TracingChannelSubscribers): void; |
| 46 | +} |
| 47 | + |
| 48 | +function createChannel(): Channel { |
| 49 | + const handlers = new Set<Handler>(); |
| 50 | + |
| 51 | + return { |
| 52 | + get hasSubscribers(): boolean { |
| 53 | + return handlers.size > 0; |
| 54 | + }, |
| 55 | + publish(context) { |
| 56 | + // Snapshot: a subscriber may unsubscribe itself while being notified. |
| 57 | + for (const handler of Array.from(handlers)) { |
| 58 | + try { |
| 59 | + handler(context); |
| 60 | + } catch { |
| 61 | + // A throwing subscriber must never break the instrumented library. |
| 62 | + } |
| 63 | + } |
| 64 | + }, |
| 65 | + runStores(context, fn, thisArg, ...args) { |
| 66 | + this.publish(context); |
| 67 | + return fn.apply(thisArg, args); |
| 68 | + }, |
| 69 | + subscribe(handler) { |
| 70 | + handlers.add(handler); |
| 71 | + }, |
| 72 | + unsubscribe(handler) { |
| 73 | + handlers.delete(handler); |
| 74 | + }, |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +const registry = new Map<string, BrowserTracingChannel>(); |
| 79 | + |
| 80 | +/** Create (or look up) a tracing channel by name. */ |
| 81 | +export function tracingChannel(name: string): BrowserTracingChannel { |
| 82 | + const existing = registry.get(name); |
| 83 | + if (existing) { |
| 84 | + return existing; |
| 85 | + } |
| 86 | + |
| 87 | + const channels = Object.fromEntries(EVENTS.map(event => [event, createChannel()])) as Record<EventName, Channel>; |
| 88 | + |
| 89 | + const channel: BrowserTracingChannel = { |
| 90 | + ...channels, |
| 91 | + subscribe(subscribers) { |
| 92 | + for (const event of EVENTS) { |
| 93 | + const handler = subscribers[event]; |
| 94 | + if (handler) { |
| 95 | + channels[event].subscribe(handler); |
| 96 | + } |
| 97 | + } |
| 98 | + }, |
| 99 | + unsubscribe(subscribers) { |
| 100 | + for (const event of EVENTS) { |
| 101 | + const handler = subscribers[event]; |
| 102 | + if (handler) { |
| 103 | + channels[event].unsubscribe(handler); |
| 104 | + } |
| 105 | + } |
| 106 | + }, |
| 107 | + }; |
| 108 | + |
| 109 | + registry.set(name, channel); |
| 110 | + return channel; |
| 111 | +} |
| 112 | + |
| 113 | +export default { tracingChannel }; |
0 commit comments