Notable changes:
Importing JSON modules now requires experimental import assertions
syntax
This release adds experimental support for the import assertions stage 3
proposal.
To keep Node.js ESM implementation as compatible as possible with the
HTML spec, import assertions are now required to import JSON modules
(still behind the `--experimental-json-modules` CLI flag):
```mjs
import info from './package.json' assert { type: 'json' };
```
Or use dynamic import:
```mjs
const info = await import('./package.json', {
assert: { type: 'json' }
});
```
Contributed by Antoine du Hamel and Geoffrey Booth https://github.com/nodejs/node/pull/40250
Other notable changes:
* async_hooks:
* (SEMVER-MINOR) expose async_wrap providers (Rafael Gonzaga) https://github.com/nodejs/node/pull/40760
* child_process:
* (SEMVER-MINOR) add support for URL to `cp.fork` (Antoine du Hamel) https://github.com/nodejs/node/pull/41225
* doc:
* add @Mesteery to collaborators (Mestery) https://github.com/nodejs/node/pull/41543
* add @bnb as a collaborator (Tierney Cyren) https://github.com/nodejs/node/pull/41100
* esm:
* (SEMVER-MINOR) graduate capturerejections to supported (James M Snell) https://github.com/nodejs/node/pull/41267
* (SEMVER-MINOR) add EventEmitterAsyncResource to core (James M Snell) https://github.com/nodejs/node/pull/41246
* events:
* (SEMVER-MINOR) propagate weak option for kNewListener (James M Snell) https://github.com/nodejs/node/pull/40899
* fs:
* (SEMVER-MINOR) accept URL as argument for `fs.rm` and `fs.rmSync` (Antoine du Hamel) https://github.com/nodejs/node/pull/41132
* lib:
* (SEMVER-MINOR) make AbortSignal cloneable/transferable (James M Snell) https://github.com/nodejs/node/pull/41050
* (SEMVER-MINOR) add AbortSignal.timeout (James M Snell) https://github.com/nodejs/node/pull/40899
* (SEMVER-MINOR) add reason to AbortSignal (James M Snell) https://github.com/nodejs/node/pull/40807
* (SEMVER-MINOR) add unsubscribe method to non-active DC channels (simon-id) https://github.com/nodejs/node/pull/40433
* (SEMVER-MINOR) add return value for DC channel.unsubscribe (simon-id) https://github.com/nodejs/node/pull/40433
* loader:
* (SEMVER-MINOR) return package format from defaultResolve if known (Gabriel Bota) https://github.com/nodejs/node/pull/40980
* perf_hooks:
* (SEMVER-MINOR) multiple fixes for Histogram (James M Snell) https://github.com/nodejs/node/pull/41153
* process:
* (SEMVER-MINOR) add `getActiveResourcesInfo()` (Darshan Sen) https://github.com/nodejs/node/pull/40813
* src:
* (SEMVER-MINOR) add x509.fingerprint512 to crypto module (3nprob) https://github.com/nodejs/node/pull/39809
* (SEMVER-MINOR) add flags for controlling process behavior (Cheng Zhao) https://github.com/nodejs/node/pull/40339
* stream:
* (SEMVER-MINOR) add filter method to readable (Benjamin Gruenbaum) https://github.com/nodejs/node/pull/41354
* (SEMVER-MINOR) add isReadable helper (Robert Nagy) https://github.com/nodejs/node/pull/41199
* (SEMVER-MINOR) add map method to Readable (Benjamin Gruenbaum) https://github.com/nodejs/node/pull/40815
* deprecate thenable support (Antoine du Hamel) https://github.com/nodejs/node/pull/40860
* util:
* (SEMVER-MINOR) pass through the inspect function to custom inspect functions (Ruben Bridgewater) https://github.com/nodejs/node/pull/41019
* (SEMVER-MINOR) add numericSeparator to util.inspect (Ruben Bridgewater) https://github.com/nodejs/node/pull/41003
* (SEMVER-MINOR) always visualize cause property in errors during inspection (Ruben Bridgewater) https://github.com/nodejs/node/pull/41002
* timers:
* (SEMVER-MINOR) add experimental scheduler api (James M Snell) https://github.com/nodejs/node/pull/40909
* v8:
* (SEMVER-MINOR) multi-tenant promise hook api (Stephen Belanger) https://github.com/nodejs/node/pull/39283
PR-URL: https://github.com/nodejs/node/pull/41804
7.3 KiB
Diagnostics Channel
Stability: 1 - Experimental
The diagnostics_channel module provides an API to create named channels
to report arbitrary message data for diagnostics purposes.
It can be accessed using:
import diagnostics_channel from 'diagnostics_channel';
const diagnostics_channel = require('diagnostics_channel');
It is intended that a module writer wanting to report diagnostics messages will create one or many top-level channels to report messages through. Channels may also be acquired at runtime but it is not encouraged due to the additional overhead of doing so. Channels may be exported for convenience, but as long as the name is known it can be acquired anywhere.
If you intend for your module to produce diagnostics data for others to consume it is recommended that you include documentation of what named channels are used along with the shape of the message data. Channel names should generally include the module name to avoid collisions with data from other modules.
Public API
Overview
Following is a simple overview of the public API.
import diagnostics_channel from 'diagnostics_channel';
// Get a reusable channel object
const channel = diagnostics_channel.channel('my-channel');
// Subscribe to the channel
channel.subscribe((message, name) => {
// Received data
});
// Check if the channel has an active subscriber
if (channel.hasSubscribers) {
// Publish data to the channel
channel.publish({
some: 'data'
});
}
const diagnostics_channel = require('diagnostics_channel');
// Get a reusable channel object
const channel = diagnostics_channel.channel('my-channel');
// Subscribe to the channel
channel.subscribe((message, name) => {
// Received data
});
// Check if the channel has an active subscriber
if (channel.hasSubscribers) {
// Publish data to the channel
channel.publish({
some: 'data'
});
}
diagnostics_channel.hasSubscribers(name)
name{string|symbol} The channel name- Returns: {boolean} If there are active subscribers
Check if there are active subscribers to the named channel. This is helpful if the message you want to send might be expensive to prepare.
This API is optional but helpful when trying to publish messages from very performance-sensitive code.
import diagnostics_channel from 'diagnostics_channel';
if (diagnostics_channel.hasSubscribers('my-channel')) {
// There are subscribers, prepare and publish message
}
const diagnostics_channel = require('diagnostics_channel');
if (diagnostics_channel.hasSubscribers('my-channel')) {
// There are subscribers, prepare and publish message
}
diagnostics_channel.channel(name)
name{string|symbol} The channel name- Returns: {Channel} The named channel object
This is the primary entry-point for anyone wanting to interact with a named channel. It produces a channel object which is optimized to reduce overhead at publish time as much as possible.
import diagnostics_channel from 'diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
const diagnostics_channel = require('diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
Class: Channel
The class Channel represents an individual named channel within the data
pipeline. It is use to track subscribers and to publish messages when there
are subscribers present. It exists as a separate object to avoid channel
lookups at publish time, enabling very fast publish speeds and allowing
for heavy use while incurring very minimal cost. Channels are created with
diagnostics_channel.channel(name), constructing a channel directly
with new Channel(name) is not supported.
channel.hasSubscribers
- Returns: {boolean} If there are active subscribers
Check if there are active subscribers to this channel. This is helpful if the message you want to send might be expensive to prepare.
This API is optional but helpful when trying to publish messages from very performance-sensitive code.
import diagnostics_channel from 'diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
if (channel.hasSubscribers) {
// There are subscribers, prepare and publish message
}
const diagnostics_channel = require('diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
if (channel.hasSubscribers) {
// There are subscribers, prepare and publish message
}
channel.publish(message)
message{any} The message to send to the channel subscribers
Publish a message to any subscribers to the channel. This will trigger message handlers synchronously so they will execute within the same context.
import diagnostics_channel from 'diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
channel.publish({
some: 'message'
});
const diagnostics_channel = require('diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
channel.publish({
some: 'message'
});
channel.subscribe(onMessage)
onMessage{Function} The handler to receive channel messagesmessage{any} The message dataname{string|symbol} The name of the channel
Register a message handler to subscribe to this channel. This message handler
will be run synchronously whenever a message is published to the channel. Any
errors thrown in the message handler will trigger an 'uncaughtException'.
import diagnostics_channel from 'diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
channel.subscribe((message, name) => {
// Received data
});
const diagnostics_channel = require('diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
channel.subscribe((message, name) => {
// Received data
});
channel.unsubscribe(onMessage)
onMessage{Function} The previous subscribed handler to remove- Returns: {boolean}
trueif the handler was found,falseotherwise.
Remove a message handler previously registered to this channel with
channel.subscribe(onMessage).
import diagnostics_channel from 'diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
channel.subscribe(onMessage);
channel.unsubscribe(onMessage);
const diagnostics_channel = require('diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
channel.subscribe(onMessage);
channel.unsubscribe(onMessage);