-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathdiagnosticsTestUtils.ts
More file actions
54 lines (50 loc) · 1.73 KB
/
diagnosticsTestUtils.ts
File metadata and controls
54 lines (50 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* eslint-disable n/no-unsupported-features/node-builtins, import/no-nodejs-modules */
import dc from 'node:diagnostics_channel';
import type { MinimalTracingChannel } from '../diagnostics.js';
export interface CollectedEvent {
kind: 'start' | 'end' | 'asyncStart' | 'asyncEnd' | 'error';
ctx: { [key: string]: unknown };
}
/**
* Subscribe to every lifecycle sub-channel on a TracingChannel and collect
* events in order. Returns the event buffer plus an unsubscribe hook.
*/
export function collectEvents(channel: MinimalTracingChannel): {
events: Array<CollectedEvent>;
unsubscribe: () => void;
} {
const events: Array<CollectedEvent> = [];
const handler = {
start: (ctx: unknown) =>
events.push({ kind: 'start', ctx: ctx as { [key: string]: unknown } }),
end: (ctx: unknown) =>
events.push({ kind: 'end', ctx: ctx as { [key: string]: unknown } }),
asyncStart: (ctx: unknown) =>
events.push({
kind: 'asyncStart',
ctx: ctx as { [key: string]: unknown },
}),
asyncEnd: (ctx: unknown) =>
events.push({
kind: 'asyncEnd',
ctx: ctx as { [key: string]: unknown },
}),
error: (ctx: unknown) =>
events.push({ kind: 'error', ctx: ctx as { [key: string]: unknown } }),
};
(channel as unknown as dc.TracingChannel).subscribe(handler);
return {
events,
unsubscribe() {
(channel as unknown as dc.TracingChannel).unsubscribe(handler);
},
};
}
/**
* Resolve a graphql tracing channel by name on the real
* `node:diagnostics_channel`. graphql-js publishes on the same channels at
* module load.
*/
export function getTracingChannel(name: string): MinimalTracingChannel {
return dc.tracingChannel(name) as unknown as MinimalTracingChannel;
}