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

const { AsyncLocalStorage } = require('async_hooks');

// Lazy load
let addAbortListener;

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

callback = AsyncLocalStorage.bind(callback);

callback = once(callback);

if (isReadableStream(stream) || isWritableStream(stream)) {
Expand Down Expand Up @@ -149,13 +155,11 @@ function eos(stream, options, callback) {

if (readable && !readableFinished && isReadableNodeStream(stream, true)) {
if (!isReadableFinished(stream, false))
return callback.call(stream,
new ERR_STREAM_PREMATURE_CLOSE());
return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE());
}
if (writable && !writableFinished) {
if (!isWritableFinished(stream, false))
return callback.call(stream,
new ERR_STREAM_PREMATURE_CLOSE());
return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE());
Comment thread
gurgunday marked this conversation as resolved.
Resolved
}

callback.call(stream);
Expand Down Expand Up @@ -310,14 +314,16 @@ function finished(stream, opts) {
autoCleanup = opts.cleanup;
}
return new Promise((resolve, reject) => {
const boundResolve = AsyncLocalStorage.bind(resolve);
const boundReject = AsyncLocalStorage.bind(reject);
Comment thread
gurgunday marked this conversation as resolved.
Resolved
const cleanup = eos(stream, opts, (err) => {
if (autoCleanup) {
cleanup();
}
if (err) {
reject(err);
boundReject(err);
} else {
resolve();
boundResolve();
}
});
});
Expand Down
36 changes: 36 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,36 @@
'use strict';

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

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

const als = new AsyncLocalStorage();
const store = { foo: 'bar' };

{
Comment thread
gurgunday marked this conversation as resolved.
Resolved
const server = http.createServer(common.mustCall((req, res) => {
als.run(store, () => {
finished(res, common.mustCall(() => {
assert.strictEqual(als.getStore()?.foo, 'bar');
}));
});

setTimeout(() => res.end(), 0);
}));

server.listen(0, common.mustCall(() => {
const port = server.address().port;

http.get(`http://localhost:${port}`, common.mustCall((res) => {
res.resume();
res.on('end', common.mustCall(() => {
server.close();
}));
}));
}));
}