- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
Node.js v16.20.2 文档
- Node.js v16.20.2
- 目录
-
导航
- assert 断言
- async_hooks 异步钩子
- async_hooks/context 异步上下文
- buffer 缓冲区
- C++插件
- C/C++插件(使用 Node-API)
- C++嵌入器
- child_process 子进程
- cluster 集群
- CLI 命令行
- console 控制台
- Corepack 核心包
- crypto 加密
- crypto/webcrypto 网络加密
- debugger 调试器
- deprecation 弃用
- dgram 数据报
- diagnostics_channel 诊断通道
- dns 域名服务器
- domain 域
- Error 错误
- events 事件触发器
- fs 文件系统
- global 全局变量
- http 超文本传输协议
- http2 超文本传输协议 2.0
- https 安全超文本传输协议
- inspector 检查器
- Intl 国际化
- module 模块
- module/cjs CommonJS 模块
- module/esm ECMAScript 模块
- module/package 包模块
- net 网络
- os 操作系统
- path 路径
- perf_hooks 性能钩子
- permission 权限
- process 进程
- punycode 域名代码
- querystring 查询字符串
- readline 逐行读取
- repl 交互式解释器
- report 诊断报告
- stream 流
- stream/web 网络流
- string_decoder 字符串解码器
- test 测试
- timers 定时器
- tls 安全传输层
- trace_events 跟踪事件
- tty 终端
- url 网址
- util 实用工具
- v8 引擎
- vm 虚拟机
- wasi 网络汇编系统接口
- worker_threads 工作线程
- zlib 压缩
- 其他版本
诊断通道#>
【Diagnostics Channel】
源代码: lib/diagnostics_channel.js
node:diagnostics_channel 模块提供了一个 API,用于创建命名通道,以便为诊断目的报告任意消息数据。
【The node: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 'node:diagnostics_channel';const diagnostics_channel = require('node: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】
以下是公共 API 的简单概述。
【Following is a simple overview of the public API.】
import diagnostics_channel from 'node:diagnostics_channel';
// Get a reusable channel object
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
// Subscribe to the channel
diagnostics_channel.subscribe('my-channel', onMessage);
// Check if the channel has an active subscriber
if (channel.hasSubscribers) {
// Publish data to the channel
channel.publish({
some: 'data'
});
}
// Unsubscribe from the channel
diagnostics_channel.unsubscribe('my-channel', onMessage);const diagnostics_channel = require('node:diagnostics_channel');
// Get a reusable channel object
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
// Subscribe to the channel
diagnostics_channel.subscribe('my-channel', onMessage);
// Check if the channel has an active subscriber
if (channel.hasSubscribers) {
// Publish data to the channel
channel.publish({
some: 'data'
});
}
// Unsubscribe from the channel
diagnostics_channel.unsubscribe('my-channel', onMessage);
diagnostics_channel.hasSubscribers(name)#>
检查指定通道是否有活跃订阅者。如果你要发送的消息准备起来可能很昂贵,这会很有用。
【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.】
此 API 是可选的,但在从对性能极其敏感的代码发布消息时非常有用。
【This API is optional but helpful when trying to publish messages from very performance-sensitive code.】
import diagnostics_channel from 'node:diagnostics_channel';
if (diagnostics_channel.hasSubscribers('my-channel')) {
// There are subscribers, prepare and publish message
}const diagnostics_channel = require('node:diagnostics_channel');
if (diagnostics_channel.hasSubscribers('my-channel')) {
// There are subscribers, prepare and publish message
}
diagnostics_channel.channel(name)#>
这是任何想要发布到指定通道的人的主要入口点。它生成一个通道对象,该对象经过优化,以尽可能减少发布时的开销。
【This is the primary entry-point for anyone wanting to publish to 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 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
diagnostics_channel.subscribe(name, onMessage)#>
name<string> | <symbol> 通道名称onMessage<Function> 接收通道消息的处理程序
注册一个消息处理程序以订阅此通道。每当有消息发布到该通道时,这个消息处理程序将同步执行。消息处理程序中抛出的任何错误都会触发 'uncaughtException'。
【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';
diagnostics_channel.subscribe('my-channel', (message, name) => {
// Received data
});const diagnostics_channel = require('diagnostics_channel');
diagnostics_channel.subscribe('my-channel', (message, name) => {
// Received data
});
diagnostics_channel.unsubscribe(name, onMessage)#>
name<string> | <symbol> 通道名称onMessage<Function> 要移除的先前订阅的处理程序- 返回:<boolean> 如果找到处理程序,则为
true,否则为false。
移除之前使用 diagnostics_channel.subscribe(name, onMessage) 在此通道注册的消息处理程序。
【Remove a message handler previously registered to this channel with
diagnostics_channel.subscribe(name, onMessage).】
import diagnostics_channel from 'diagnostics_channel';
function onMessage(message, name) {
// Received data
}
diagnostics_channel.subscribe('my-channel', onMessage);
diagnostics_channel.unsubscribe('my-channel', onMessage);const diagnostics_channel = require('diagnostics_channel');
function onMessage(message, name) {
// Received data
}
diagnostics_channel.subscribe('my-channel', onMessage);
diagnostics_channel.unsubscribe('my-channel', onMessage);
类:Channel#>
【Class: Channel】
类 Channel 表示数据管道中的一个名为通道的个体。它用于跟踪订阅者,并在有订阅者存在时发布消息。它作为一个独立的对象存在,以避免在发布时进行通道查找,从而实现非常快速的发布速度,并在高频使用时产生极低的成本。通道通过 diagnostics_channel.channel(name) 创建,不支持直接使用 new Channel(name) 构造通道。
【The class Channel represents an individual named channel within the data
pipeline. It is used 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#>
- 返回:<boolean> 如果有活跃的订阅者
检查此通道是否有活跃的订阅者。如果你要发送的消息准备起来可能很昂贵,这会很有用。
【Check if there are active subscribers to this channel. This is helpful if the message you want to send might be expensive to prepare.】
此 API 是可选的,但在从对性能极其敏感的代码发布消息时非常有用。
【This API is optional but helpful when trying to publish messages from very performance-sensitive code.】
import diagnostics_channel from 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
if (channel.hasSubscribers) {
// There are subscribers, prepare and publish message
}const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
if (channel.hasSubscribers) {
// There are subscribers, prepare and publish message
}
channel.publish(message)#>
message<any> 要发送给通道订阅者的消息
向通道的任何订阅者发布消息。这将同步触发消息处理程序,使它们在相同的上下文中执行。
【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 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
channel.publish({
some: 'message'
});const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
channel.publish({
some: 'message'
});
channel.subscribe(onMessage)#>
onMessage<Function> 接收通道消息的处理程序
注册一个消息处理程序以订阅此通道。每当有消息发布到该通道时,这个消息处理程序将同步执行。消息处理程序中抛出的任何错误都会触发 'uncaughtException'。
【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 'node:diagnostics_channel';
const channel = diagnostics_channel.channel('my-channel');
channel.subscribe((message, name) => {
// Received data
});const diagnostics_channel = require('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
channel.subscribe((message, name) => {
// Received data
});
channel.unsubscribe(onMessage)#>
onMessage<Function> 要移除的先前订阅的处理程序- 返回:<boolean> 如果找到处理程序,则为
true,否则为false。
移除之前使用 channel.subscribe(onMessage) 在此通道注册的消息处理程序。
【Remove a message handler previously registered to this channel with
channel.subscribe(onMessage).】
import diagnostics_channel from 'node: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('node:diagnostics_channel');
const channel = diagnostics_channel.channel('my-channel');
function onMessage(message, name) {
// Received data
}
channel.subscribe(onMessage);
channel.unsubscribe(onMessage);
内置通道#>
【Built-in Channels】
超文本传输协议#>
【HTTP】
http.client.request.start
request<http.ClientRequest>
当客户端开始请求时触发。
【Emitted when client starts a request.】
http.client.response.finish
request<http.ClientRequest>response<http.IncomingMessage>
当客户端收到响应时触发。
【Emitted when client receives a response.】
http.server.request.start
request<http.IncomingMessage>response<http.ServerResponse>socket<net.Socket>server<http.Server>
当服务器收到请求时触发。
【Emitted when server receives a request.】
http.server.response.finish
request<http.IncomingMessage>response<http.ServerResponse>socket<net.Socket>server<http.Server>
服务器发送响应时触发。
【Emitted when server sends a response.】
净值#>
【NET】
net.client.socket
socket<net.Socket>
创建新的 TCP 或管道客户端套接字时触发。
【Emitted when a new TCP or pipe client socket is created.】
net.server.socket
socket<net.Socket>
当接收到新的 TCP 或管道连接时触发。
【Emitted when a new TCP or pipe connection is received.】
用户数据报协议#>
【UDP】
udp.socket
socket<dgram.Socket>
创建新的 UDP 套接字时触发。
【Emitted when a new UDP socket is created.】
进程#>
【Process】
child_process
process<ChildProcess>
创建新进程时触发。
【Emitted when a new process is created.】
工作线程#>
【Worker Thread】
worker_threads
workerWorker
创建新线程时触发。
【Emitted when a new thread is created.】