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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a bind variant (or add an option) which doesn't copy the length property to AsyncLocalStore and AsyncResource?
There are cases like express handlers where this is needed but at a lot places noone cars about lenght.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might just document how to do it "fast"? It's literally 4 lines of code.

I'm ok in adding another method as well.

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