診斷通道#

穩定性:2 - 穩定

原始碼: lib/diagnostics_channel.js

node:diagnostics_channel 模組提供了一個 API,用於建立命名通道,以報告用於診斷目的的任意訊息資料。

它可以透過以下方式訪問

import diagnostics_channel from 'node:diagnostics_channel';const diagnostics_channel = require('node:diagnostics_channel');

其意圖是讓希望報告診斷訊息的模組編寫者建立一個或多個頂級通道來報告訊息。通道也可以在執行時獲取,但不鼓勵這樣做,因為會產生額外的開銷。為方便起見,可以匯出通道,但只要名稱已知,就可以在任何地方獲取它。

如果你希望你的模組為他人消費而生成診斷資料,建議你在文件中說明使用了哪些命名通道以及訊息資料的結構。通道名稱通常應包含模組名稱,以避免與其他模組的資料發生衝突。

公共 API#

概述#

以下是公共 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)#

檢查命名通道是否有活躍的訂閱者。如果你要傳送的訊息準備起來可能成本很高,這會很有用。

此 API 是可選的,但在嘗試從對效能非常敏感的程式碼釋出訊息時很有幫助。

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)#

這是任何想要釋出到命名通道的人的主要入口點。它會生成一個通道物件,該物件經過最佳化,以儘可能減少釋出時的開銷。

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)#

註冊一個訊息處理程式以訂閱此通道。每當有訊息釋出到該通道時,此訊息處理程式將同步執行。訊息處理程式中丟擲的任何錯誤都將觸發一個 'uncaughtException'

import diagnostics_channel from 'node:diagnostics_channel';

diagnostics_channel.subscribe('my-channel', (message, name) => {
  // Received data
});const diagnostics_channel = require('node: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) 註冊到此通道的訊息處理程式。

import diagnostics_channel from 'node:diagnostics_channel';

function onMessage(message, name) {
  // Received data
}

diagnostics_channel.subscribe('my-channel', onMessage);

diagnostics_channel.unsubscribe('my-channel', onMessage);const diagnostics_channel = require('node:diagnostics_channel');

function onMessage(message, name) {
  // Received data
}

diagnostics_channel.subscribe('my-channel', onMessage);

diagnostics_channel.unsubscribe('my-channel', onMessage);
diagnostics_channel.tracingChannel(nameOrChannels)#

穩定性:1 - 實驗性

為給定的 TracingChannel 通道 建立一個 TracingChannel 包裝器。如果給定一個名稱,將以 tracing:${name}:${eventType} 的形式建立相應的跟蹤通道,其中 eventType 對應於 TracingChannel 通道 的型別。

import diagnostics_channel from 'node:diagnostics_channel';

const channelsByName = diagnostics_channel.tracingChannel('my-channel');

// or...

const channelsByCollection = diagnostics_channel.tracingChannel({
  start: diagnostics_channel.channel('tracing:my-channel:start'),
  end: diagnostics_channel.channel('tracing:my-channel:end'),
  asyncStart: diagnostics_channel.channel('tracing:my-channel:asyncStart'),
  asyncEnd: diagnostics_channel.channel('tracing:my-channel:asyncEnd'),
  error: diagnostics_channel.channel('tracing:my-channel:error'),
});const diagnostics_channel = require('node:diagnostics_channel');

const channelsByName = diagnostics_channel.tracingChannel('my-channel');

// or...

const channelsByCollection = diagnostics_channel.tracingChannel({
  start: diagnostics_channel.channel('tracing:my-channel:start'),
  end: diagnostics_channel.channel('tracing:my-channel:end'),
  asyncStart: diagnostics_channel.channel('tracing:my-channel:asyncStart'),
  asyncEnd: diagnostics_channel.channel('tracing:my-channel:asyncEnd'),
  error: diagnostics_channel.channel('tracing:my-channel:error'),
});

類: Channel#

Channel 代表資料管道中的一個獨立的命名通道。它用於跟蹤訂閱者,並在有訂閱者存在時釋出訊息。它作為一個獨立的物件存在,以避免在釋出時進行通道查詢,從而實現非常快的釋出速度,並允許大量使用,同時產生的成本非常小。通道是透過 diagnostics_channel.channel(name) 建立的,不支援直接使用 new Channel(name) 構造通道。

channel.hasSubscribers#
  • 返回: <boolean> 如果有活躍的訂閱者

檢查此通道是否有活躍的訂閱者。如果你要傳送的訊息準備起來可能成本很高,這會很有用。

此 API 是可選的,但在嘗試從對效能非常敏感的程式碼釋出訊息時很有幫助。

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> 要傳送給通道訂閱者的訊息

向通道的任何訂閱者釋出一條訊息。這將同步觸發訊息處理程式,因此它們將在相同的上下文中執行。

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)#

註冊一個訊息處理程式以訂閱此通道。每當有訊息釋出到該通道時,此訊息處理程式將同步執行。訊息處理程式中丟擲的任何錯誤都將觸發一個 '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) 註冊到此通道的訊息處理程式。

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);
channel.bindStore(store[, transform])#

穩定性:1 - 實驗性

當呼叫 channel.runStores(context, ...) 時,給定的上下文資料將應用於繫結到該通道的任何儲存區。如果該儲存區已經被繫結,則先前的 transform 函式將被新的函式替換。可以省略 transform 函式,直接將給定的上下文資料設定為上下文。

import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';

const store = new AsyncLocalStorage();

const channel = diagnostics_channel.channel('my-channel');

channel.bindStore(store, (data) => {
  return { data };
});const diagnostics_channel = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('node:async_hooks');

const store = new AsyncLocalStorage();

const channel = diagnostics_channel.channel('my-channel');

channel.bindStore(store, (data) => {
  return { data };
});
channel.unbindStore(store)#

穩定性:1 - 實驗性

移除先前透過 channel.bindStore(store) 註冊到此通道的訊息處理程式。

import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';

const store = new AsyncLocalStorage();

const channel = diagnostics_channel.channel('my-channel');

channel.bindStore(store);
channel.unbindStore(store);const diagnostics_channel = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('node:async_hooks');

const store = new AsyncLocalStorage();

const channel = diagnostics_channel.channel('my-channel');

channel.bindStore(store);
channel.unbindStore(store);
channel.runStores(context, fn[, thisArg[, ...args]])#

穩定性:1 - 實驗性

  • context <any> 要傳送給訂閱者並繫結到儲存區的訊息
  • fn <Function> 在進入的儲存上下文中執行的處理程式
  • thisArg <any> 用於函式呼叫的接收者。
  • ...args <any> 傳遞給函式的可選引數。

在給定函式的持續時間內,將給定的資料應用於繫結到通道的任何 AsyncLocalStorage 例項,然後在將該資料應用於儲存區的範圍內釋出到通道。

如果向 channel.bindStore(store) 提供了轉換函式,它將被應用於轉換訊息資料,然後該資料才會成為儲存區的上下文值。在需要上下文連結的情況下,可以從轉換函式內部訪問先前的儲存上下文。

應用於儲存區的上下文應該在任何從給定函式期間開始的執行中繼續的非同步程式碼中都可訪問,但在某些情況下可能會發生上下文丟失

import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';

const store = new AsyncLocalStorage();

const channel = diagnostics_channel.channel('my-channel');

channel.bindStore(store, (message) => {
  const parent = store.getStore();
  return new Span(message, parent);
});
channel.runStores({ some: 'message' }, () => {
  store.getStore(); // Span({ some: 'message' })
});const diagnostics_channel = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('node:async_hooks');

const store = new AsyncLocalStorage();

const channel = diagnostics_channel.channel('my-channel');

channel.bindStore(store, (message) => {
  const parent = store.getStore();
  return new Span(message, parent);
});
channel.runStores({ some: 'message' }, () => {
  store.getStore(); // Span({ some: 'message' })
});

類: TracingChannel#

穩定性:1 - 實驗性

TracingChannelTracingChannel 通道 的集合,它們共同表達了一個可跟蹤的單一操作。它用於規範化和簡化為跟蹤應用程式流程而生成事件的過程。使用 diagnostics_channel.tracingChannel() 來構造一個 TracingChannel。與 Channel 一樣,建議在檔案的頂層建立並重用單個 TracingChannel,而不是動態建立它們。

tracingChannel.subscribe(subscribers)#

用於將一組函式訂閱到相應通道的輔助方法。這與在每個通道上單獨呼叫 channel.subscribe(onMessage) 相同。

import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.subscribe({
  start(message) {
    // Handle start message
  },
  end(message) {
    // Handle end message
  },
  asyncStart(message) {
    // Handle asyncStart message
  },
  asyncEnd(message) {
    // Handle asyncEnd message
  },
  error(message) {
    // Handle error message
  },
});const diagnostics_channel = require('node:diagnostics_channel');

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.subscribe({
  start(message) {
    // Handle start message
  },
  end(message) {
    // Handle end message
  },
  asyncStart(message) {
    // Handle asyncStart message
  },
  asyncEnd(message) {
    // Handle asyncEnd message
  },
  error(message) {
    // Handle error message
  },
});
tracingChannel.unsubscribe(subscribers)#

用於從相應通道取消訂閱一組函式的輔助方法。這與在每個通道上單獨呼叫 channel.unsubscribe(onMessage) 相同。

import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.unsubscribe({
  start(message) {
    // Handle start message
  },
  end(message) {
    // Handle end message
  },
  asyncStart(message) {
    // Handle asyncStart message
  },
  asyncEnd(message) {
    // Handle asyncEnd message
  },
  error(message) {
    // Handle error message
  },
});const diagnostics_channel = require('node:diagnostics_channel');

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.unsubscribe({
  start(message) {
    // Handle start message
  },
  end(message) {
    // Handle end message
  },
  asyncStart(message) {
    // Handle asyncStart message
  },
  asyncEnd(message) {
    // Handle asyncEnd message
  },
  error(message) {
    // Handle error message
  },
});
tracingChannel.traceSync(fn[, context[, thisArg[, ...args]]])#
  • fn <Function> 要進行跟蹤包裝的函式
  • context <Object> 用於關聯事件的共享物件
  • thisArg <any> 用於函式呼叫的接收者
  • ...args <any> 傳遞給函式的可選引數
  • 返回: <any> 給定函式的返回值

跟蹤同步函式呼叫。這總是會在執行前後產生一個 start 事件 和一個 end 事件,如果給定函式丟擲錯誤,則可能產生一個 error 事件。這將使用 channel.runStores(context, ...)start 通道上執行給定函式,這確保所有事件都應將任何繫結的儲存區設定為與此跟蹤上下文匹配。

為確保只形成正確的跟蹤圖,只有在開始跟蹤之前存在訂閱者時才會釋出事件。在跟蹤開始後新增的訂閱將不會收到該跟蹤的未來事件,只有未來的跟蹤才會被看到。

import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.traceSync(() => {
  // Do something
}, {
  some: 'thing',
});const diagnostics_channel = require('node:diagnostics_channel');

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.traceSync(() => {
  // Do something
}, {
  some: 'thing',
});
tracingChannel.tracePromise(fn[, context[, thisArg[, ...args]]])#
  • fn <Function> 返回 Promise 的函式,用於進行跟蹤包裝
  • context <Object> 用於關聯跟蹤事件的共享物件
  • thisArg <any> 用於函式呼叫的接收者
  • ...args <any> 傳遞給函式的可選引數
  • 返回: <Promise> 從給定函式返回的 promise 連結而來

跟蹤一個返回 Promise 的函式呼叫。這總是會在函式執行的同步部分前後產生一個 start 事件和一個 end 事件,當達到 Promise 的延續時,會產生一個 asyncStart 事件和一個 asyncEnd 事件。如果給定函式丟擲錯誤或返回的 Promise 被拒絕,它也可能產生一個 error 事件。這將使用 channel.runStores(context, ...)start 通道上執行給定函式,這確保所有事件都應將任何繫結的儲存區設定為與此跟蹤上下文匹配。

為確保只形成正確的跟蹤圖,只有在開始跟蹤之前存在訂閱者時才會釋出事件。在跟蹤開始後新增的訂閱將不會收到該跟蹤的未來事件,只有未來的跟蹤才會被看到。

import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.tracePromise(async () => {
  // Do something
}, {
  some: 'thing',
});const diagnostics_channel = require('node:diagnostics_channel');

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.tracePromise(async () => {
  // Do something
}, {
  some: 'thing',
});
tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]])#
  • fn <Function> 使用回撥的函式,用於進行跟蹤包裝
  • position <number> 預期的回撥函式的零索引引數位置(如果傳遞 undefined,則預設為最後一個引數)
  • context <Object> 用於關聯跟蹤事件的共享物件(如果傳遞 undefined,則預設為 {}
  • thisArg <any> 用於函式呼叫的接收者
  • ...args <any> 傳遞給函式的引數(必須包含回撥)
  • 返回: <any> 給定函式的返回值

跟蹤一個接收回調的函式呼叫。回撥應遵循通常使用的“錯誤作為第一個引數”的約定。這總是會在函式執行的同步部分前後產生一個 start 事件和一個 end 事件,並會在回撥執行前後產生一個 asyncStart 事件和一個 asyncEnd 事件。如果給定函式丟擲錯誤,或者傳遞給回撥的第一個引數被設定,它也可能產生一個 error 事件。這將使用 channel.runStores(context, ...)start 通道上執行給定函式,這確保所有事件都應將任何繫結的儲存區設定為與此跟蹤上下文匹配。

為確保只形成正確的跟蹤圖,只有在開始跟蹤之前存在訂閱者時才會釋出事件。在跟蹤開始後新增的訂閱將不會收到該跟蹤的未來事件,只有未來的跟蹤才會被看到。

import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.traceCallback((arg1, callback) => {
  // Do something
  callback(null, 'result');
}, 1, {
  some: 'thing',
}, thisArg, arg1, callback);const diagnostics_channel = require('node:diagnostics_channel');

const channels = diagnostics_channel.tracingChannel('my-channel');

channels.traceCallback((arg1, callback) => {
  // Do something
  callback(null, 'result');
}, 1, {
  some: 'thing',
}, thisArg, arg1, callback);

回撥也將使用 channel.runStores(context, ...) 執行,這在某些情況下可以恢復上下文丟失。

import diagnostics_channel from 'node:diagnostics_channel';
import { AsyncLocalStorage } from 'node:async_hooks';

const channels = diagnostics_channel.tracingChannel('my-channel');
const myStore = new AsyncLocalStorage();

// The start channel sets the initial store data to something
// and stores that store data value on the trace context object
channels.start.bindStore(myStore, (data) => {
  const span = new Span(data);
  data.span = span;
  return span;
});

// Then asyncStart can restore from that data it stored previously
channels.asyncStart.bindStore(myStore, (data) => {
  return data.span;
});const diagnostics_channel = require('node:diagnostics_channel');
const { AsyncLocalStorage } = require('node:async_hooks');

const channels = diagnostics_channel.tracingChannel('my-channel');
const myStore = new AsyncLocalStorage();

// The start channel sets the initial store data to something
// and stores that store data value on the trace context object
channels.start.bindStore(myStore, (data) => {
  const span = new Span(data);
  data.span = span;
  return span;
});

// Then asyncStart can restore from that data it stored previously
channels.asyncStart.bindStore(myStore, (data) => {
  return data.span;
});
tracingChannel.hasSubscribers#
  • 返回: <boolean> 如果任何一個單獨的通道有訂閱者,則為 true,否則為 false

這是一個在 TracingChannel 例項上可用的輔助方法,用於檢查 TracingChannel 通道 中是否有任何一個有訂閱者。如果其中任何一個至少有一個訂閱者,則返回 true,否則返回 false

import diagnostics_channel from 'node:diagnostics_channel';

const channels = diagnostics_channel.tracingChannel('my-channel');

if (channels.hasSubscribers) {
  // Do something
}const diagnostics_channel = require('node:diagnostics_channel');

const channels = diagnostics_channel.tracingChannel('my-channel');

if (channels.hasSubscribers) {
  // Do something
}

TracingChannel 通道#

一個 TracingChannel 是幾個 diagnostics_channels 的集合,代表了單個可跟蹤操作執行生命週期中的特定點。該行為被分為五個 diagnostics_channels,包括 startendasyncStartasyncEnderror。單個可跟蹤操作將在所有事件之間共享同一個事件物件,這對於透過弱對映(weakmap)管理關聯很有幫助。

當任務“完成”時,這些事件物件將用 resulterror 值進行擴充套件。對於同步任務,result 將是函式的返回值,而 error 將是函式丟擲的任何東西。對於基於回撥的非同步函式,result 將是回撥的第二個引數,而 error 將是在 end 事件中可見的丟擲錯誤,或在 asyncStartasyncEnd 事件中作為第一個回撥引數。

為確保只形成正確的跟蹤圖,只有在開始跟蹤之前存在訂閱者時才應釋出事件。在跟蹤開始後新增的訂閱不應收到該跟蹤的未來事件,只有未來的跟蹤才會被看到。

跟蹤通道應遵循以下命名模式

  • tracing:module.class.method:starttracing:module.function:start
  • tracing:module.class.method:endtracing:module.function:end
  • tracing:module.class.method:asyncStarttracing:module.function:asyncStart
  • tracing:module.class.method:asyncEndtracing:module.function:asyncEnd
  • tracing:module.class.method:errortracing:module.function:error
start(event)#
  • 名稱: tracing:${name}:start

start 事件代表函式被呼叫的那個點。此時,事件資料可能包含函式引數或在函式執行開始時可用的任何其他資訊。

end(event)#
  • 名稱: tracing:${name}:end

end 事件代表函式呼叫返回一個值的那個點。對於非同步函式,這是指返回 promise 的時候,而不是函式本身內部執行 return 語句的時候。此時,如果被跟蹤的函式是同步的,result 欄位將被設定為函式的返回值。或者,可能會出現 error 欄位來表示任何丟擲的錯誤。

建議專門監聽 error 事件來跟蹤錯誤,因為一個可跟蹤的操作可能會產生多個錯誤。例如,一個失敗的非同步任務可能在任務的同步部分丟擲錯誤之前在內部啟動。

asyncStart(event)#
  • 名稱: tracing:${name}:asyncStart

asyncStart 事件代表到達可跟蹤函式的回撥或延續。此時,像回撥引數這樣的東西可能是可用的,或者任何其他表達操作“結果”的東西。

對於基於回撥的函式,回撥的第一個引數將被分配給 error 欄位(如果不是 undefinednull),第二個引數將被分配給 result 欄位。

對於 promise,resolve 路徑的引數將被分配給 result,或者 reject 路徑的引數將被分配給 error

建議專門監聽 error 事件來跟蹤錯誤,因為一個可跟蹤的操作可能會產生多個錯誤。例如,一個失敗的非同步任務可能在任務的同步部分丟擲錯誤之前在內部啟動。

asyncEnd(event)#
  • 名稱: tracing:${name}:asyncEnd

asyncEnd 事件代表非同步函式的回撥返回。在 asyncStart 事件之後,事件資料不太可能改變,但看到回撥完成的點可能很有用。

error(event)#
  • 名稱: tracing:${name}:error

error 事件表示可跟蹤函式同步或非同步產生的任何錯誤。如果在被跟蹤函式的同步部分丟擲錯誤,該錯誤將被分配給事件的 error 欄位,並觸發 error 事件。如果透過回撥或 promise 拒絕非同步接收到錯誤,它也將被分配給事件的 error 欄位並觸發 error 事件。

一個可跟蹤的函式呼叫可能會多次產生錯誤,因此在消費此事件時應考慮到這一點。例如,如果內部觸發的另一個非同步任務失敗,然後函式的同步部分丟擲錯誤,將發出兩個 error 事件,一個用於同步錯誤,一個用於非同步錯誤。

內建通道#

控制檯#

穩定性:1 - 實驗性

事件: 'console.log'#

當呼叫 console.log() 時觸發。接收一個傳遞給 console.log() 的引數陣列。

事件: 'console.info'#

當呼叫 console.info() 時觸發。接收一個傳遞給 console.info() 的引數陣列。

事件: 'console.debug'#

當呼叫 console.debug() 時觸發。接收一個傳遞給 console.debug() 的引數陣列。

事件: 'console.warn'#

當呼叫 console.warn() 時觸發。接收一個傳遞給 console.warn() 的引數陣列。

事件: 'console.error'#

當呼叫 console.error() 時觸發。接收一個傳遞給 console.error() 的引數陣列。

HTTP#

穩定性:1 - 實驗性

事件: 'http.client.request.created'#

當客戶端建立請求物件時觸發。與 http.client.request.start 不同,此事件在請求傳送之前觸發。

事件: 'http.client.request.start'#

當客戶端開始一個請求時觸發。

事件: 'http.client.request.error'#

當客戶端請求期間發生錯誤時觸發。

事件: 'http.client.response.finish'#

當客戶端收到響應時觸發。

事件: 'http.server.request.start'#

當伺服器收到一個請求時觸發。

事件: 'http.server.response.created'#

當伺服器建立響應時觸發。該事件在響應傳送前觸發。

事件: 'http.server.response.finish'#

當伺服器傳送響應時觸發。

HTTP/2#

穩定性:1 - 實驗性

事件: 'http2.client.stream.created'#

當在客戶端上建立流時觸發。

事件: 'http2.client.stream.start'#

當在客戶端上啟動流時觸發。

事件: 'http2.client.stream.error'#

當在客戶端處理流期間發生錯誤時觸發。

事件: 'http2.client.stream.finish'#

當在客戶端上收到流時觸發。

事件: 'http2.client.stream.close'#

當在客戶端上關閉流時觸發。關閉流時使用的 HTTP/2 錯誤程式碼可以透過 stream.rstCode 屬性獲取。

事件: 'http2.server.stream.created'#

當在伺服器上建立流時觸發。

事件: 'http2.server.stream.start'#

當在伺服器上啟動流時觸發。

事件: 'http2.server.stream.error'#

當在伺服器上處理流期間發生錯誤時觸發。

事件: 'http2.server.stream.finish'#

當在伺服器上傳送流時觸發。

事件: 'http2.server.stream.close'#

當在伺服器上關閉流時觸發。關閉流時使用的 HTTP/2 錯誤程式碼可以透過 stream.rstCode 屬性獲取。

模組#

穩定性:1 - 實驗性

事件: 'module.require.start'#
  • event <Object> 包含以下屬性
    • id 傳遞給 require() 的引數。模組名稱。
    • parentFilename 嘗試 require(id) 的模組名稱。

當執行 require() 時觸發。請參閱 start 事件

事件: 'module.require.end'#
  • event <Object> 包含以下屬性
    • id 傳遞給 require() 的引數。模組名稱。
    • parentFilename 嘗試 require(id) 的模組名稱。

當一個 require() 呼叫返回時觸發。請參閱 end 事件

事件: 'module.require.error'#
  • event <Object> 包含以下屬性
    • id 傳遞給 require() 的引數。模組名稱。
    • parentFilename 嘗試 require(id) 的模組名稱。
  • error <Error>

當一個 require() 丟擲錯誤時觸發。請參閱 error 事件

事件: 'module.import.asyncStart'#
  • event <Object> 包含以下屬性
    • id 傳遞給 import() 的引數。模組名稱。
    • parentURL 嘗試 import(id) 的模組的 URL 物件。

當呼叫 import() 時觸發。請參閱 asyncStart 事件

事件: 'module.import.asyncEnd'#
  • event <Object> 包含以下屬性
    • id 傳遞給 import() 的引數。模組名稱。
    • parentURL 嘗試 import(id) 的模組的 URL 物件。

import() 完成時觸發。請參閱 asyncEnd 事件

事件: 'module.import.error'#
  • event <Object> 包含以下屬性
    • id 傳遞給 import() 的引數。模組名稱。
    • parentURL 嘗試 import(id) 的模組的 URL 物件。
  • error <Error>

當一個 import() 丟擲錯誤時觸發。請參閱 error 事件

NET#

穩定性:1 - 實驗性

事件: 'net.client.socket'#

當建立新的 TCP 或管道客戶端套接字連線時觸發。

事件: 'net.server.socket'#

當接收到新的 TCP 或管道連線時觸發。

事件: 'tracing:net.server.listen:asyncStart'#

當呼叫 net.Server.listen() 時觸發,在實際設定埠或管道之前。

事件: 'tracing:net.server.listen:asyncEnd'#

net.Server.listen() 完成並且伺服器準備好接受連線時觸發。

事件: 'tracing:net.server.listen:error'#

net.Server.listen() 返回錯誤時觸發。

UDP#

穩定性:1 - 實驗性

事件: 'udp.socket'#

當建立新的 UDP 套接字時觸發。

程序#

穩定性:1 - 實驗性

事件: 'child_process'#

當建立新程序時觸發。

事件: 'execve'#

當呼叫 process.execve() 時觸發。

工作執行緒#

穩定性:1 - 實驗性

事件: 'worker_threads'#

當建立新執行緒時觸發。