mirror of
https://github.com/nodejs/node.git
synced 2025-04-28 13:40:37 +00:00
stream: preserve AsyncLocalStorage context in finished()
PR-URL: https://github.com/nodejs/node/pull/57865 Reviewed-By: Raz Luvaton <rluvaton@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
91d8a524ad
commit
4b2b3c01b4
@ -43,6 +43,9 @@ const {
|
|||||||
willEmitClose: _willEmitClose,
|
willEmitClose: _willEmitClose,
|
||||||
kIsClosedPromise,
|
kIsClosedPromise,
|
||||||
} = require('internal/streams/utils');
|
} = require('internal/streams/utils');
|
||||||
|
|
||||||
|
// Lazy load
|
||||||
|
let AsyncLocalStorage;
|
||||||
let addAbortListener;
|
let addAbortListener;
|
||||||
|
|
||||||
function isRequest(stream) {
|
function isRequest(stream) {
|
||||||
@ -63,7 +66,8 @@ function eos(stream, options, callback) {
|
|||||||
validateFunction(callback, 'callback');
|
validateFunction(callback, 'callback');
|
||||||
validateAbortSignal(options.signal, 'options.signal');
|
validateAbortSignal(options.signal, 'options.signal');
|
||||||
|
|
||||||
callback = once(callback);
|
AsyncLocalStorage ??= require('async_hooks').AsyncLocalStorage;
|
||||||
|
callback = once(AsyncLocalStorage.bind(callback));
|
||||||
|
|
||||||
if (isReadableStream(stream) || isWritableStream(stream)) {
|
if (isReadableStream(stream) || isWritableStream(stream)) {
|
||||||
return eosWeb(stream, options, callback);
|
return eosWeb(stream, options, callback);
|
||||||
|
20
test/async-hooks/test-async-local-storage-stream-finished.js
Normal file
20
test/async-hooks/test-async-local-storage-stream-finished.js
Normal file
@ -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…
Reference in New Issue
Block a user