|
| 1 | +import type { |
| 2 | + MinimalChannel, |
| 3 | + MinimalDiagnosticsChannel, |
| 4 | + MinimalTracingChannel, |
| 5 | +} from '../diagnostics.js'; |
| 6 | + |
| 7 | +export type Listener = (message: unknown) => void; |
| 8 | + |
| 9 | +/** |
| 10 | + * In-memory `MinimalChannel` implementation used by the unit tests. Tracks |
| 11 | + * subscribers and replays Node's `runStores` semantics by simply invoking |
| 12 | + * `fn`. |
| 13 | + */ |
| 14 | +export class FakeChannel implements MinimalChannel { |
| 15 | + listeners: Array<Listener> = []; |
| 16 | + |
| 17 | + get [Symbol.toStringTag]() { |
| 18 | + return 'FakeChannel'; |
| 19 | + } |
| 20 | + |
| 21 | + get hasSubscribers(): boolean { |
| 22 | + return this.listeners.length > 0; |
| 23 | + } |
| 24 | + |
| 25 | + publish(message: unknown): void { |
| 26 | + for (const l of this.listeners) { |
| 27 | + l(message); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + runStores<T, ContextType extends object>( |
| 32 | + _ctx: ContextType, |
| 33 | + fn: (this: ContextType, ...args: Array<unknown>) => T, |
| 34 | + thisArg?: unknown, |
| 35 | + ...args: Array<unknown> |
| 36 | + ): T { |
| 37 | + return fn.apply(thisArg as ContextType, args); |
| 38 | + } |
| 39 | + |
| 40 | + subscribe(listener: Listener): void { |
| 41 | + this.listeners.push(listener); |
| 42 | + } |
| 43 | + |
| 44 | + unsubscribe(listener: Listener): void { |
| 45 | + const idx = this.listeners.indexOf(listener); |
| 46 | + if (idx >= 0) { |
| 47 | + this.listeners.splice(idx, 1); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Structurally-faithful `MinimalTracingChannel` implementation mirroring |
| 54 | + * Node's `TracingChannel.traceSync` / `tracePromise` lifecycle (start, |
| 55 | + * runStores, error, asyncStart, asyncEnd, end). |
| 56 | + */ |
| 57 | +export class FakeTracingChannel implements MinimalTracingChannel { |
| 58 | + start: FakeChannel = new FakeChannel(); |
| 59 | + end: FakeChannel = new FakeChannel(); |
| 60 | + asyncStart: FakeChannel = new FakeChannel(); |
| 61 | + asyncEnd: FakeChannel = new FakeChannel(); |
| 62 | + error: FakeChannel = new FakeChannel(); |
| 63 | + |
| 64 | + get [Symbol.toStringTag]() { |
| 65 | + return 'FakeTracingChannel'; |
| 66 | + } |
| 67 | + |
| 68 | + get hasSubscribers(): boolean { |
| 69 | + return ( |
| 70 | + this.start.hasSubscribers || |
| 71 | + this.end.hasSubscribers || |
| 72 | + this.asyncStart.hasSubscribers || |
| 73 | + this.asyncEnd.hasSubscribers || |
| 74 | + this.error.hasSubscribers |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + traceSync<T>( |
| 79 | + fn: (...args: Array<unknown>) => T, |
| 80 | + ctx: object, |
| 81 | + thisArg?: unknown, |
| 82 | + ...args: Array<unknown> |
| 83 | + ): T { |
| 84 | + this.start.publish(ctx); |
| 85 | + try { |
| 86 | + return this.end.runStores(ctx, fn, thisArg, ...args); |
| 87 | + } catch (err) { |
| 88 | + (ctx as { error: unknown }).error = err; |
| 89 | + this.error.publish(ctx); |
| 90 | + throw err; |
| 91 | + } finally { |
| 92 | + this.end.publish(ctx); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + tracePromise<T>( |
| 97 | + fn: (...args: Array<unknown>) => Promise<T>, |
| 98 | + ctx: object, |
| 99 | + thisArg?: unknown, |
| 100 | + ...args: Array<unknown> |
| 101 | + ): Promise<T> { |
| 102 | + this.start.publish(ctx); |
| 103 | + let promise: Promise<T>; |
| 104 | + try { |
| 105 | + promise = this.end.runStores(ctx, fn, thisArg, ...args); |
| 106 | + } catch (err) { |
| 107 | + (ctx as { error: unknown }).error = err; |
| 108 | + this.error.publish(ctx); |
| 109 | + this.end.publish(ctx); |
| 110 | + throw err; |
| 111 | + } |
| 112 | + this.end.publish(ctx); |
| 113 | + return promise.then( |
| 114 | + (result) => { |
| 115 | + (ctx as { result: unknown }).result = result; |
| 116 | + this.asyncStart.publish(ctx); |
| 117 | + this.asyncEnd.publish(ctx); |
| 118 | + return result; |
| 119 | + }, |
| 120 | + (err: unknown) => { |
| 121 | + (ctx as { error: unknown }).error = err; |
| 122 | + this.error.publish(ctx); |
| 123 | + this.asyncStart.publish(ctx); |
| 124 | + this.asyncEnd.publish(ctx); |
| 125 | + throw err; |
| 126 | + }, |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +export class FakeDc implements MinimalDiagnosticsChannel { |
| 132 | + private cache = new Map<string, FakeTracingChannel>(); |
| 133 | + |
| 134 | + get [Symbol.toStringTag]() { |
| 135 | + return 'FakeDc'; |
| 136 | + } |
| 137 | + |
| 138 | + tracingChannel(name: string): FakeTracingChannel { |
| 139 | + let existing = this.cache.get(name); |
| 140 | + if (existing === undefined) { |
| 141 | + existing = new FakeTracingChannel(); |
| 142 | + this.cache.set(name, existing); |
| 143 | + } |
| 144 | + return existing; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +export interface CollectedEvent { |
| 149 | + kind: 'start' | 'end' | 'asyncStart' | 'asyncEnd' | 'error'; |
| 150 | + ctx: { [key: string]: unknown }; |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * Attach listeners to every sub-channel on a FakeTracingChannel and return |
| 155 | + * the captured event buffer plus an unsubscribe hook. |
| 156 | + */ |
| 157 | +export function collectEvents(channel: FakeTracingChannel): { |
| 158 | + events: Array<CollectedEvent>; |
| 159 | + unsubscribe: () => void; |
| 160 | +} { |
| 161 | + const events: Array<CollectedEvent> = []; |
| 162 | + const make = |
| 163 | + (kind: CollectedEvent['kind']): Listener => |
| 164 | + (m) => |
| 165 | + events.push({ kind, ctx: m as { [key: string]: unknown } }); |
| 166 | + const startL = make('start'); |
| 167 | + const endL = make('end'); |
| 168 | + const asyncStartL = make('asyncStart'); |
| 169 | + const asyncEndL = make('asyncEnd'); |
| 170 | + const errorL = make('error'); |
| 171 | + channel.start.subscribe(startL); |
| 172 | + channel.end.subscribe(endL); |
| 173 | + channel.asyncStart.subscribe(asyncStartL); |
| 174 | + channel.asyncEnd.subscribe(asyncEndL); |
| 175 | + channel.error.subscribe(errorL); |
| 176 | + return { |
| 177 | + events, |
| 178 | + unsubscribe() { |
| 179 | + channel.start.unsubscribe(startL); |
| 180 | + channel.end.unsubscribe(endL); |
| 181 | + channel.asyncStart.unsubscribe(asyncStartL); |
| 182 | + channel.asyncEnd.unsubscribe(asyncEndL); |
| 183 | + channel.error.unsubscribe(errorL); |
| 184 | + }, |
| 185 | + }; |
| 186 | +} |
0 commit comments