node:diagnostics_channel
Node-compatible diagnostics channels for publishing and subscribing to named
diagnostic message streams (node:diagnostics_channel).
Available in both vjs and vjs-lite runtimes. Enabled by default unless
--no-node-compat is passed.
import ... from "node:diagnostics_channel"
node-compat (enabled by default; disable with --no-node-compat)Example
Section titled “Example”import { channel, subscribe } from "node:diagnostics_channel";
subscribe("app:events", (message, name) => { console.log(name, message);});channel("app:events").publish({ ok: true });channelv0.0.0-alpha.2Channel registered under name.hasSubscribersv0.0.0-alpha.2true if the named channel currently has any subscribers.subscribev0.0.0-alpha.2onMessage to the channel named name.unsubscribev0.0.0-alpha.2onMessage from the channel named name.tracingChannelv0.0.0-alpha.2TracingChannel for nameOrChannels.ChannelStorev0.0.0-alpha.2AsyncLocalStorage.TracingChannelCollectionv0.0.0-alpha.2TracingChannel.TracingChannelSubscribersv0.0.0-alpha.2TracingChannel.subscribe() and TracingChannel.unsubscribe().Functions
Section titled “Functions”channel
Section titled “channel”export function channel(name: string | symbol): Channel;
Look up or create the shared Channel registered under name.
| Name | Type | Description |
|---|---|---|
name | string | symbol | Channel name or symbol. |
hasSubscribers
Section titled “hasSubscribers”export function hasSubscribers(name: string | symbol): boolean;
Return true if the named channel currently has any subscribers.
| Name | Type | Description |
|---|---|---|
name | string | symbol | Channel name or symbol. |
boolean — true if subscribers exist, otherwise false.subscribe
Section titled “subscribe”export function subscribe(name: string | symbol, onMessage: ChannelListener): void;
Subscribe onMessage to the channel named name.
| Name | Type | Description |
|---|---|---|
name | string | symbol | Channel name or symbol. |
onMessage | ChannelListener | Subscriber callback. |
voidunsubscribe
Section titled “unsubscribe”export function unsubscribe(name: string | symbol, onMessage: ChannelListener): boolean;
Unsubscribe onMessage from the channel named name.
| Name | Type | Description |
|---|---|---|
name | string | symbol | Channel name or symbol. |
onMessage | ChannelListener | Subscriber callback to remove. |
boolean — true if the listener was registered and removed, otherwise false.tracingChannel
Section titled “tracingChannel”export function tracingChannel<ContextType = unknown>(
nameOrChannels: string | TracingChannelCollection,
): TracingChannel<ContextType>;
Create or retrieve a TracingChannel for nameOrChannels.
| Name | Type | Description |
|---|---|---|
nameOrChannels | string | TracingChannelCollection | Base channel name or existing TracingChannelCollection. |
TracingChannel<ContextType> — A TracingChannel instance.Classes
Section titled “Classes”Channel
Section titled “Channel”export class Channel {
readonly name: string | symbol;
readonly hasSubscribers: boolean;
constructor(name: string | symbol);
publish(message: unknown): void;
subscribe(onMessage: ChannelListener): void;
unsubscribe(onMessage: ChannelListener): boolean;
bindStore<T = unknown>(store: ChannelStore<T>, transform?: (context: unknown) => T): void;
unbindStore(store: ChannelStore<any>): boolean;
runStores<R, TArgs extends any[] = any[]>(
context: unknown,
fn: (...args: TArgs) => R,
thisArg?: unknown,
...args: TArgs
): R;
}
Named diagnostic channel supporting zero-overhead subscriber checks and synchronous message publishing.
constructor(name: string | symbol);
Construct a standalone Channel instance.
| Name | Type | Description |
|---|---|---|
name | string | symbol | Channel name or symbol. |
| Property | Type | Description | Since |
|---|---|---|---|
name readonly | string | symbol | Canonical name or symbol identifying this channel. | v0.0.0-alpha.2 |
hasSubscribers readonly | boolean | true when one or more subscribers are attached to this channel. | v0.0.0-alpha.2 |
Channel.publish
Section titled “Channel.publish”publish(message: unknown): void;
Publish message synchronously to every current subscriber.
| Name | Type | Description |
|---|---|---|
message | unknown | Payload delivered to subscribers. |
voidChannel.subscribe
Section titled “Channel.subscribe”subscribe(onMessage: ChannelListener): void;
Attach a listener function to this channel.
| Name | Type | Description |
|---|---|---|
onMessage | ChannelListener | Subscriber callback. |
voidChannel.unsubscribe
Section titled “Channel.unsubscribe”unsubscribe(onMessage: ChannelListener): boolean;
Detach a previously subscribed listener function.
| Name | Type | Description |
|---|---|---|
onMessage | ChannelListener | Subscriber callback to remove. |
boolean — true if onMessage was registered and removed, otherwise false.Channel.bindStore
Section titled “Channel.bindStore”bindStore<T = unknown>(store: ChannelStore<T>, transform?: (context: unknown) => T): void;
Bind an AsyncLocalStorage-compatible store to run during runStores().
| Name | Type | Description |
|---|---|---|
store | ChannelStore<T> | Store instance to bind. |
transform optional | (context: unknown) => T | Optional context mapper (context) => storeValue. |
voidChannel.unbindStore
Section titled “Channel.unbindStore”unbindStore(store: ChannelStore<any>): boolean;
Unbind a previously bound store from this channel.
| Name | Type | Description |
|---|---|---|
store | ChannelStore<any> | Store instance to remove. |
boolean — true if store was bound and removed, otherwise false.Channel.runStores
Section titled “Channel.runStores”runStores<R, TArgs extends any[] = any[]>(
context: unknown,
fn: (...args: TArgs) => R,
thisArg?: unknown,
...args: TArgs
): R;
Publish context and invoke fn inside all bound stores.
| Name | Type | Description |
|---|---|---|
context | unknown | Context payload published to subscribers and passed to bound stores. |
fn | (...args: TArgs) => R | Function executed within bound stores. |
thisArg optional | unknown | Optional this binding for fn. |
...args | TArgs | Positional arguments forwarded to fn. |
R — Return value of fn.TracingChannel
Section titled “TracingChannel”export class TracingChannel<ContextType = unknown> {
readonly start: Channel;
readonly end: Channel;
readonly asyncStart: Channel;
readonly asyncEnd: Channel;
readonly error: Channel;
readonly hasSubscribers: boolean;
constructor(nameOrChannels: string | TracingChannelCollection);
subscribe(subscribers: TracingChannelSubscribers<ContextType>): void;
unsubscribe(subscribers: TracingChannelSubscribers<ContextType>): boolean;
traceSync<T, TArgs extends any[] = any[], TThis = unknown>(
fn: (this: TThis, ...args: TArgs) => T,
context?: ContextType,
thisArg?: TThis,
...args: TArgs
): T;
tracePromise<T, TArgs extends any[] = any[], TThis = unknown>(
fn: (this: TThis, ...args: TArgs) => Promise<T>,
context?: ContextType,
thisArg?: TThis,
...args: TArgs
): Promise<T>;
traceCallback<TArgs extends any[] = any[], TThis = unknown>(
fn: (this: TThis, ...args: TArgs) => unknown,
position?: number,
context?: ContextType,
thisArg?: TThis,
...args: TArgs
): unknown;
}
Coordinated set of five diagnostic channels (start, end, asyncStart,
asyncEnd, error) for tracing synchronous, promise, and callback calls.
constructor(nameOrChannels: string | TracingChannelCollection);
Construct a TracingChannel from a base channel name or channel map.
| Name | Type | Description |
|---|---|---|
nameOrChannels | string | TracingChannelCollection | Base channel name or existing TracingChannelCollection. |
| Property | Type | Description | Since |
|---|---|---|---|
start readonly | Channel | Channel published when a traced call starts. | v0.0.0-alpha.2 |
end readonly | Channel | Channel published when a traced call's synchronous frame ends. | v0.0.0-alpha.2 |
asyncStart readonly | Channel | Channel published when a traced async continuation begins. | v0.0.0-alpha.2 |
asyncEnd readonly | Channel | Channel published when a traced async continuation ends. | v0.0.0-alpha.2 |
error readonly | Channel | Channel published when a traced call throws or rejects. | v0.0.0-alpha.2 |
hasSubscribers readonly | boolean | true if any of the five constituent channels has subscribers. | v0.0.0-alpha.2 |
TracingChannel.subscribe
Section titled “TracingChannel.subscribe”subscribe(subscribers: TracingChannelSubscribers<ContextType>): void;
Subscribe handlers to the matching channels of this TracingChannel.
| Name | Type | Description |
|---|---|---|
subscribers | TracingChannelSubscribers<ContextType> | Map of channel callbacks to register. |
voidTracingChannel.unsubscribe
Section titled “TracingChannel.unsubscribe”unsubscribe(subscribers: TracingChannelSubscribers<ContextType>): boolean;
Unsubscribe previously registered handlers from this TracingChannel.
| Name | Type | Description |
|---|---|---|
subscribers | TracingChannelSubscribers<ContextType> | Map of channel callbacks to remove. |
boolean — true if all specified handlers were removed, otherwise false.TracingChannel.traceSync
Section titled “TracingChannel.traceSync”traceSync<T, TArgs extends any[] = any[], TThis = unknown>(
fn: (this: TThis, ...args: TArgs) => T,
context?: ContextType,
thisArg?: TThis,
...args: TArgs
): T;
Trace a synchronous function invocation across start, end, and error.
| Name | Type | Description |
|---|---|---|
fn | (this: TThis, ...args: TArgs) => T | Synchronous function to execute. |
context optional | ContextType | Context object passed to channel subscribers. |
thisArg optional | TThis | Optional this binding for fn. |
...args | TArgs | Positional arguments forwarded to fn. |
T — Return value of fn.TracingChannel.tracePromise
Section titled “TracingChannel.tracePromise”tracePromise<T, TArgs extends any[] = any[], TThis = unknown>(
fn: (this: TThis, ...args: TArgs) => Promise<T>,
context?: ContextType,
thisArg?: TThis,
...args: TArgs
): Promise<T>;
Trace a promise-returning function across start, end, asyncStart,
asyncEnd, and error.
| Name | Type | Description |
|---|---|---|
fn | (this: TThis, ...args: TArgs) => Promise<T> | Async or promise-returning function to execute. |
context optional | ContextType | Context object passed to channel subscribers. |
thisArg optional | TThis | Optional this binding for fn. |
...args | TArgs | Positional arguments forwarded to fn. |
Promise<T> — Promise returned by fn.TracingChannel.traceCallback
Section titled “TracingChannel.traceCallback”traceCallback<TArgs extends any[] = any[], TThis = unknown>(
fn: (this: TThis, ...args: TArgs) => unknown,
position?: number,
context?: ContextType,
thisArg?: TThis,
...args: TArgs
): unknown;
Trace a callback-style function across start, end, asyncStart,
asyncEnd, and error.
| Name | Type | Description |
|---|---|---|
fn | (this: TThis, ...args: TArgs) => unknown | Callback-accepting function to execute. |
position optional | number | Zero-based or negative index of the callback parameter (default -1). |
context optional | ContextType | Context object passed to channel subscribers. |
thisArg optional | TThis | Optional this binding for fn. |
...args | TArgs | Positional arguments forwarded to fn. |
unknown — Return value of fn.Interfaces
Section titled “Interfaces”ChannelStore
Section titled “ChannelStore”export interface ChannelStore<T = unknown> {
run<R, TArgs extends any[] = any[]>(context: T, fn: (...args: TArgs) => R, ...args: TArgs): R;
}
Store-binding interface compatible with AsyncLocalStorage.
ChannelStore.run
Section titled “ChannelStore.run”run<R, TArgs extends any[] = any[]>(context: T, fn: (...args: TArgs) => R, ...args: TArgs): R;
Run fn synchronously within the given store context.
| Name | Type | Description |
|---|---|---|
context | T | Context value to bind. |
fn | (...args: TArgs) => R | Callback to invoke. |
...args | TArgs | Arguments forwarded to fn. |
R — Return value of fn.TracingChannelCollection
Section titled “TracingChannelCollection”export interface TracingChannelCollection {
start: Channel;
end: Channel;
asyncStart: Channel;
asyncEnd: Channel;
error: Channel;
}
Collection of five lifecycle channels backing a TracingChannel.
| Property | Type | Description | Since |
|---|---|---|---|
start | Channel | Channel published when a traced operation begins. | v0.0.0-alpha.2 |
end | Channel | Channel published when the synchronous part of a traced operation ends. | v0.0.0-alpha.2 |
asyncStart | Channel | Channel published when the async continuation of a traced operation starts. | v0.0.0-alpha.2 |
asyncEnd | Channel | Channel published when the async continuation of a traced operation finishes. | v0.0.0-alpha.2 |
error | Channel | Channel published when a traced operation throws or rejects. | v0.0.0-alpha.2 |
TracingChannelSubscribers
Section titled “TracingChannelSubscribers”export interface TracingChannelSubscribers<ContextType = unknown> {
start?: (message: ContextType) => void;
end?: (message: ContextType) => void;
asyncStart?: (message: ContextType) => void;
asyncEnd?: (message: ContextType) => void;
error?: (message: ContextType) => void;
}
Subscriber callbacks accepted by TracingChannel.subscribe() and
TracingChannel.unsubscribe().
| Property | Type | Description | Since |
|---|---|---|---|
start optional | (message: ContextType) => void | Invoked on the start channel. | v0.0.0-alpha.2 |
end optional | (message: ContextType) => void | Invoked on the end channel. | v0.0.0-alpha.2 |
asyncStart optional | (message: ContextType) => void | Invoked on the asyncStart channel. | v0.0.0-alpha.2 |
asyncEnd optional | (message: ContextType) => void | Invoked on the asyncEnd channel. | v0.0.0-alpha.2 |
error optional | (message: ContextType) => void | Invoked on the error channel. | v0.0.0-alpha.2 |
Type Aliases
Section titled “Type Aliases”ChannelListener
Section titled “ChannelListener”export type ChannelListener = (message: unknown, name: string | symbol) => void;
Callback invoked whenever a message is published to a Channel.
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: {
/** `Channel` constructor. @since 0.0.0-alpha.2 */
Channel: typeof Channel;
/** `TracingChannel` constructor. @since 0.0.0-alpha.2 */
TracingChannel: typeof TracingChannel;
/** Look up or create a shared channel. @since 0.0.0-alpha.2 */
channel: typeof channel;
/** Check whether a channel has subscribers. @since 0.0.0-alpha.2 */
hasSubscribers: typeof hasSubscribers;
/** Subscribe a listener to a named channel. @since 0.0.0-alpha.2 */
subscribe: typeof subscribe;
/** Unsubscribe a listener from a named channel. @since 0.0.0-alpha.2 */
unsubscribe: typeof unsubscribe;
/** Create a `TracingChannel`. @since 0.0.0-alpha.2 */
tracingChannel: typeof tracingChannel;
};
export default _default;
Default export object providing the node:diagnostics_channel API.
