Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const {
} = require('internal/streams/utils');

// Lazy load
let AsyncLocalStorage;
let AsyncResource;
let addAbortListener;

function isRequest(stream) {
Expand All @@ -54,6 +54,14 @@ function isRequest(stream) {

const nop = () => {};

function bindAsyncResource(fn, type) {
AsyncResource ??= require('async_hooks').AsyncResource;
const resource = new AsyncResource(type);
return function(...args) {
return resource.runInAsyncScope(fn, this, ...args);
};
}

function eos(stream, options, callback) {
if (arguments.length === 2) {
callback = options;
Expand All @@ -66,8 +74,9 @@ function eos(stream, options, callback) {
validateFunction(callback, 'callback');
validateAbortSignal(options.signal, 'options.signal');

AsyncLocalStorage ??= require('async_hooks').AsyncLocalStorage;
callback = once(AsyncLocalStorage.bind(callback));
// Avoid AsyncResource.bind() because it calls ObjectDefineProperties which
// is a bottleneck here.
callback = once(bindAsyncResource(callback, 'STREAM_END_OF_STREAM'));

if (isReadableStream(stream) || isWritableStream(stream)) {
return eosWeb(stream, options, callback);
Expand Down
Loading