stream: validate fromWritable() options before cache · nodejs/node@ae79f2b · GitHub
Skip to content

Commit ae79f2b

Browse files
trivikraduh95
authored andcommitted
stream: validate fromWritable() options before cache
Validate options before returning a cached fromWritable() adapter so invalid later options still throw. Cache adapters by backpressure policy as well as Writable instance, since the policy changes write behavior. Fixes: #63277 Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: openai:gpt-5.5 PR-URL: #63278 Backport-PR-URL: #64675 Fixes: #63277 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
1 parent c98cc67 commit ae79f2b

3 files changed

Lines changed: 61 additions & 7 deletions

File tree

doc/api/stream_iter.md

Lines changed: 3 additions & 2 deletions

lib/internal/streams/iter/classic.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const {
2121
PromiseReject,
2222
PromiseResolve,
2323
PromiseWithResolvers,
24+
SafeMap,
2425
SafeWeakMap,
2526
SymbolAsyncDispose,
2627
SymbolAsyncIterator,
@@ -427,10 +428,6 @@ function fromWritable(writable, options = kNullPrototype) {
427428
throw new ERR_INVALID_ARG_TYPE('writable', 'Writable', writable);
428429
}
429430

430-
// Return cached adapter if available.
431-
const cached = fromWritableCache.get(writable);
432-
if (cached !== undefined) return cached;
433-
434431
validateObject(options, 'options');
435432
const {
436433
backpressure = 'strict',
@@ -459,6 +456,17 @@ function fromWritable(writable, options = kNullPrototype) {
459456
'drop-oldest is not supported for classic stream.Writable');
460457
}
461458

459+
// Return cached adapter if available. Backpressure policy changes writer
460+
// behavior, so cache one adapter per policy.
461+
let cachedByBackpressure = fromWritableCache.get(writable);
462+
if (cachedByBackpressure !== undefined) {
463+
const cached = cachedByBackpressure.get(backpressure);
464+
if (cached !== undefined) return cached;
465+
} else {
466+
cachedByBackpressure = new SafeMap();
467+
fromWritableCache.set(writable, cachedByBackpressure);
468+
}
469+
462470
// Fall back to sensible defaults for duck-typed streams that may not
463471
// expose the full stream.Writable property set.
464472
const hwm = writable.writableHighWaterMark ?? 16384;
@@ -710,7 +718,7 @@ function fromWritable(writable, options = kNullPrototype) {
710718
return promise;
711719
};
712720

713-
fromWritableCache.set(writable, writer);
721+
cachedByBackpressure.set(backpressure, writer);
714722
return writer;
715723
}
716724

Lines changed: 45 additions & 0 deletions

0 commit comments

Comments
 (0)