Skip to content

Commit af851d9

Browse files
committed
stream: update writable-to-web context handling
Remove references to the retired legacy AsyncLocalStorage path and AsyncContextFrame.enabled. Preserve AsyncResource callbacks when async hooks are enabled and use frame swapping otherwise. Remove obsolete --no-async-context-frame subprocess runs from the context regression tests. Signed-off-by: seungwoo <zoozoo1302@gmail.com>
1 parent b94440b commit af851d9

4 files changed

Lines changed: 13 additions & 52 deletions

File tree

‎lib/internal/webstreams/adapters.js‎

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ const {
1616
Int16Array,
1717
Int32Array,
1818
Int8Array,
19-
ObjectAssign,
2019
ObjectKeys,
2120
PromisePrototypeThen,
2221
PromiseResolve,
2322
PromiseWithResolvers,
24-
ReflectOwnKeys,
2523
SafePromiseAllReturnVoid,
2624
SafeSet,
2725
StringPrototypeStartsWith,
@@ -41,11 +39,7 @@ const {
4139

4240
const { TextEncoder } = require('internal/encoding');
4341
const AsyncContextFrame = require('internal/async_context_frame');
44-
const {
45-
enabledHooksExist,
46-
executionAsyncResource,
47-
symbols: { async_local_storage_context_symbol },
48-
} = require('internal/async_hooks');
42+
const { enabledHooksExist } = require('internal/async_hooks');
4943

5044
let addAbortListener;
5145
let AsyncResource;
@@ -430,32 +424,14 @@ function newWritableStreamFromStreamWritable(
430424
function createBufferedWriteCallback() {
431425
const onwrite = streamWritable._writableState.onwrite;
432426
const contextFrame = AsyncContextFrame.current();
433-
if (!AsyncContextFrame.enabled || enabledHooksExist()) {
427+
if (enabledHooksExist()) {
434428
AsyncResource ??= require('async_hooks').AsyncResource;
435-
const currentResource = AsyncContextFrame.enabled ? undefined : executionAsyncResource();
436-
const resourceContext = currentResource?.[async_local_storage_context_symbol];
437-
const contextSnapshot = currentResource === undefined ? undefined :
438-
ObjectAssign({ __proto__: null }, resourceContext);
439429
let resource;
440430
try {
441431
resource = new AsyncResource('WEBSTREAM_WRITABLE_WRITE');
442432
} finally {
443433
// An init hook can change the current frame while creating a resource.
444434
AsyncContextFrame.set(contextFrame);
445-
if (currentResource !== undefined) {
446-
// Legacy ALS.run() retains this object's identity for its own
447-
// restoration. Restore its entries without replacing the object.
448-
if (resourceContext === undefined) {
449-
delete currentResource[async_local_storage_context_symbol];
450-
} else {
451-
for (const key of ReflectOwnKeys(resourceContext)) delete resourceContext[key];
452-
ObjectAssign(resourceContext, contextSnapshot);
453-
}
454-
}
455-
}
456-
if (currentResource !== undefined) {
457-
// A user init hook may run before the legacy ALS propagation hook.
458-
resource[async_local_storage_context_symbol] = contextSnapshot;
459435
}
460436
return (error) => resource.runInAsyncScope(onwrite, undefined, error);
461437
}
@@ -538,8 +514,8 @@ function newWritableStreamFromStreamWritable(
538514
if (waitForWriteCallback && streamWritable._writableState?.writing === true) {
539515
observedAsyncWrite = true;
540516
}
541-
// Keep promise resources for async hooks and legacy context propagation.
542-
const canPark = AsyncContextFrame.enabled && !enabledHooksExist();
517+
// Keep promise resources when async hooks need to observe them.
518+
const canPark = !enabledHooksExist();
543519
const state = {
544520
deferred: canPark ? undefined : PromiseWithResolvers(),
545521
contextFrame: canPark ? AsyncContextFrame.current() : undefined,

‎test/parallel/test-webstreams-adapters-writable-async-context.js‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,9 @@ async function main() {
160160
hook?.disable();
161161

162162
if (mode === undefined) {
163-
for (const args of [
164-
[__filename, 'hooks'],
165-
['--no-async-context-frame', __filename, 'legacy'],
166-
]) {
167-
const { code, signal, stderr } = await common.spawnPromisified(process.execPath, args);
168-
assert.strictEqual(code, 0, stderr);
169-
assert.strictEqual(signal, null);
170-
}
163+
const { code, signal, stderr } = await common.spawnPromisified(process.execPath, [__filename, 'hooks']);
164+
assert.strictEqual(code, 0, stderr);
165+
assert.strictEqual(signal, null);
171166
}
172167
}
173168

‎test/parallel/test-webstreams-adapters-writable-batching-context.js‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,9 @@ async function main() {
137137
hook?.disable();
138138

139139
if (mode === undefined) {
140-
for (const args of [
141-
[__filename, 'hooks'],
142-
['--no-async-context-frame', __filename, 'legacy'],
143-
]) {
144-
const { code, signal, stderr } = await common.spawnPromisified(process.execPath, args);
145-
assert.strictEqual(code, 0, stderr);
146-
assert.strictEqual(signal, null);
147-
}
140+
const { code, signal, stderr } = await common.spawnPromisified(process.execPath, [__filename, 'hooks']);
141+
assert.strictEqual(code, 0, stderr);
142+
assert.strictEqual(signal, null);
148143
}
149144
}
150145

‎test/parallel/test-webstreams-adapters-writable-lifecycle.js‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,9 @@ async function main() {
182182
hook?.disable();
183183

184184
if (mode === undefined) {
185-
for (const args of [
186-
[__filename, 'hooks'],
187-
['--no-async-context-frame', __filename, 'legacy'],
188-
]) {
189-
const { code, signal, stderr } = await common.spawnPromisified(process.execPath, args);
190-
assert.strictEqual(code, 0, stderr);
191-
assert.strictEqual(signal, null);
192-
}
185+
const { code, signal, stderr } = await common.spawnPromisified(process.execPath, [__filename, 'hooks']);
186+
assert.strictEqual(code, 0, stderr);
187+
assert.strictEqual(signal, null);
193188
}
194189
}
195190

0 commit comments

Comments
 (0)