Skip to content
VaelJS Blog

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"
Since v0.0.0-alpha.2 vjs (V8) vjs-lite (QuickJS)
Cargo feature: node-compat (enabled by default; disable with --no-node-compat)
import { channel, subscribe } from "node:diagnostics_channel";
subscribe("app:events", (message, name) => {
console.log(name, message);
});
channel("app:events").publish({ ok: true });
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function channel(name: string | symbol): Channel;

Look up or create the shared Channel registered under name.

Parameters
NameTypeDescription
namestring | symbolChannel name or symbol.
Returns
Channel — The shared Channel instance for name.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function hasSubscribers(name: string | symbol): boolean;

Return true if the named channel currently has any subscribers.

Parameters
NameTypeDescription
namestring | symbolChannel name or symbol.
Returns
boolean — true if subscribers exist, otherwise false.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function subscribe(name: string | symbol, onMessage: ChannelListener): void;

Subscribe onMessage to the channel named name.

Parameters
NameTypeDescription
namestring | symbolChannel name or symbol.
onMessageChannelListenerSubscriber callback.
Returns
void
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function unsubscribe(name: string | symbol, onMessage: ChannelListener): boolean;

Unsubscribe onMessage from the channel named name.

Parameters
NameTypeDescription
namestring | symbolChannel name or symbol.
onMessageChannelListenerSubscriber callback to remove.
Returns
boolean — true if the listener was registered and removed, otherwise false.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function tracingChannel<ContextType = unknown>(
  nameOrChannels: string | TracingChannelCollection,
): TracingChannel<ContextType>;

Create or retrieve a TracingChannel for nameOrChannels.

Parameters
NameTypeDescription
nameOrChannelsstring | TracingChannelCollectionBase channel name or existing TracingChannelCollection.
Returns
TracingChannel<ContextType> — A TracingChannel instance.
classSince v0.0.0-alpha.2vjs & vjs-lite
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
constructor(name: string | symbol);

Construct a standalone Channel instance.

Parameters
NameTypeDescription
namestring | symbolChannel name or symbol.
Properties
PropertyTypeDescriptionSince
name readonlystring | symbolCanonical name or symbol identifying this channel.v0.0.0-alpha.2
hasSubscribers readonlybooleantrue when one or more subscribers are attached to this channel.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
publish(message: unknown): void;

Publish message synchronously to every current subscriber.

Parameters
NameTypeDescription
messageunknownPayload delivered to subscribers.
Returns
void
methodSince v0.0.0-alpha.2
subscribe(onMessage: ChannelListener): void;

Attach a listener function to this channel.

Parameters
NameTypeDescription
onMessageChannelListenerSubscriber callback.
Returns
void
methodSince v0.0.0-alpha.2
unsubscribe(onMessage: ChannelListener): boolean;

Detach a previously subscribed listener function.

Parameters
NameTypeDescription
onMessageChannelListenerSubscriber callback to remove.
Returns
boolean — true if onMessage was registered and removed, otherwise false.
methodSince v0.0.0-alpha.2
bindStore<T = unknown>(store: ChannelStore<T>, transform?: (context: unknown) => T): void;

Bind an AsyncLocalStorage-compatible store to run during runStores().

Parameters
NameTypeDescription
storeChannelStore<T>Store instance to bind.
transform optional(context: unknown) => TOptional context mapper (context) => storeValue.
Returns
void
methodSince v0.0.0-alpha.2
unbindStore(store: ChannelStore<any>): boolean;

Unbind a previously bound store from this channel.

Parameters
NameTypeDescription
storeChannelStore<any>Store instance to remove.
Returns
boolean — true if store was bound and removed, otherwise false.
methodSince v0.0.0-alpha.2
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.

Parameters
NameTypeDescription
contextunknownContext payload published to subscribers and passed to bound stores.
fn(...args: TArgs) => RFunction executed within bound stores.
thisArg optionalunknownOptional this binding for fn.
...argsTArgsPositional arguments forwarded to fn.
Returns
R — Return value of fn.
classSince v0.0.0-alpha.2vjs & vjs-lite
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
constructor(nameOrChannels: string | TracingChannelCollection);

Construct a TracingChannel from a base channel name or channel map.

Parameters
NameTypeDescription
nameOrChannelsstring | TracingChannelCollectionBase channel name or existing TracingChannelCollection.
Properties
PropertyTypeDescriptionSince
start readonlyChannelChannel published when a traced call starts.v0.0.0-alpha.2
end readonlyChannelChannel published when a traced call's synchronous frame ends.v0.0.0-alpha.2
asyncStart readonlyChannelChannel published when a traced async continuation begins.v0.0.0-alpha.2
asyncEnd readonlyChannelChannel published when a traced async continuation ends.v0.0.0-alpha.2
error readonlyChannelChannel published when a traced call throws or rejects.v0.0.0-alpha.2
hasSubscribers readonlybooleantrue if any of the five constituent channels has subscribers.v0.0.0-alpha.2
Methods
methodSince v0.0.0-alpha.2
subscribe(subscribers: TracingChannelSubscribers<ContextType>): void;

Subscribe handlers to the matching channels of this TracingChannel.

Parameters
NameTypeDescription
subscribersTracingChannelSubscribers<ContextType>Map of channel callbacks to register.
Returns
void
methodSince v0.0.0-alpha.2
unsubscribe(subscribers: TracingChannelSubscribers<ContextType>): boolean;

Unsubscribe previously registered handlers from this TracingChannel.

Parameters
NameTypeDescription
subscribersTracingChannelSubscribers<ContextType>Map of channel callbacks to remove.
Returns
boolean — true if all specified handlers were removed, otherwise false.
methodSince v0.0.0-alpha.2
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.

Parameters
NameTypeDescription
fn(this: TThis, ...args: TArgs) => TSynchronous function to execute.
context optionalContextTypeContext object passed to channel subscribers.
thisArg optionalTThisOptional this binding for fn.
...argsTArgsPositional arguments forwarded to fn.
Returns
T — Return value of fn.
methodSince v0.0.0-alpha.2
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.

Parameters
NameTypeDescription
fn(this: TThis, ...args: TArgs) => Promise<T>Async or promise-returning function to execute.
context optionalContextTypeContext object passed to channel subscribers.
thisArg optionalTThisOptional this binding for fn.
...argsTArgsPositional arguments forwarded to fn.
Returns
Promise<T> — Promise returned by fn.
methodSince v0.0.0-alpha.2
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.

Parameters
NameTypeDescription
fn(this: TThis, ...args: TArgs) => unknownCallback-accepting function to execute.
position optionalnumberZero-based or negative index of the callback parameter (default -1).
context optionalContextTypeContext object passed to channel subscribers.
thisArg optionalTThisOptional this binding for fn.
...argsTArgsPositional arguments forwarded to fn.
Returns
unknown — Return value of fn.
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
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.

Methods
methodSince v0.0.0-alpha.2
run<R, TArgs extends any[] = any[]>(context: T, fn: (...args: TArgs) => R, ...args: TArgs): R;

Run fn synchronously within the given store context.

Parameters
NameTypeDescription
contextTContext value to bind.
fn(...args: TArgs) => RCallback to invoke.
...argsTArgsArguments forwarded to fn.
Returns
R — Return value of fn.
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface TracingChannelCollection {
  start: Channel;
  end: Channel;
  asyncStart: Channel;
  asyncEnd: Channel;
  error: Channel;
}

Collection of five lifecycle channels backing a TracingChannel.

Properties
PropertyTypeDescriptionSince
start ChannelChannel published when a traced operation begins.v0.0.0-alpha.2
end ChannelChannel published when the synchronous part of a traced operation ends.v0.0.0-alpha.2
asyncStart ChannelChannel published when the async continuation of a traced operation starts.v0.0.0-alpha.2
asyncEnd ChannelChannel published when the async continuation of a traced operation finishes.v0.0.0-alpha.2
error ChannelChannel published when a traced operation throws or rejects.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
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().

Properties
PropertyTypeDescriptionSince
start optional(message: ContextType) => voidInvoked on the start channel.v0.0.0-alpha.2
end optional(message: ContextType) => voidInvoked on the end channel.v0.0.0-alpha.2
asyncStart optional(message: ContextType) => voidInvoked on the asyncStart channel.v0.0.0-alpha.2
asyncEnd optional(message: ContextType) => voidInvoked on the asyncEnd channel.v0.0.0-alpha.2
error optional(message: ContextType) => voidInvoked on the error channel.v0.0.0-alpha.2
typeSince v0.0.0-alpha.2vjs & vjs-lite
export type ChannelListener = (message: unknown, name: string | symbol) => void;

Callback invoked whenever a message is published to a Channel.

defaultSince v0.0.0-alpha.2vjs & vjs-lite
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.