Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const {
willEmitClose: _willEmitClose,
kIsClosedPromise,
} = require('internal/streams/utils');

// Lazy load
let AsyncLocalStorage;
let addAbortListener;

function isRequest(stream) {
Expand All @@ -63,7 +66,8 @@ function eos(stream, options, callback) {
validateFunction(callback, 'callback');
validateAbortSignal(options.signal, 'options.signal');

callback = once(callback);
AsyncLocalStorage ??= require('async_hooks').AsyncLocalStorage;
callback = once(AsyncLocalStorage.bind(callback));

if (isReadableStream(stream) || isWritableStream(stream)) {
return eosWeb(stream, options, callback);
Expand Down
20 changes: 20 additions & 0 deletions test/async-hooks/test-async-local-storage-stream-finished.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const common = require('../common');
const { Readable, finished } = require('stream');
const { AsyncLocalStorage } = require('async_hooks');
const { strictEqual } = require('assert');

// This test verifies that AsyncLocalStorage context is maintained
// when using stream.finished()

const readable = new Readable();
const als = new AsyncLocalStorage();

als.run(321, () => {
finished(readable, common.mustCall(() => {
strictEqual(als.getStore(), 321);
}));
});

readable.destroy();
Loading