mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 18:25:13 +08:00
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: https://github.com/nodejs/node/issues/63277 Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: openai:gpt-5.5 PR-URL: https://github.com/nodejs/node/pull/63278 Fixes: https://github.com/nodejs/node/issues/63277 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
46 lines
1020 B
JavaScript
46 lines
1020 B
JavaScript
// Flags: --experimental-stream-iter
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const { Writable } = require('stream');
|
|
const { fromWritable } = require('stream/iter');
|
|
|
|
{
|
|
const writable = new Writable({ write() {} });
|
|
|
|
fromWritable(writable);
|
|
|
|
assert.throws(
|
|
() => fromWritable(writable, { backpressure: 'invalid' }),
|
|
{ code: 'ERR_INVALID_ARG_VALUE' },
|
|
);
|
|
|
|
writable.destroy();
|
|
}
|
|
|
|
async function testCachedWritableUsesLaterBackpressureOptions() {
|
|
const chunks = [];
|
|
const writable = new Writable({
|
|
highWaterMark: 1,
|
|
write(chunk, encoding, callback) {
|
|
chunks.push(Buffer.from(chunk));
|
|
},
|
|
});
|
|
|
|
fromWritable(writable);
|
|
const writer = fromWritable(writable, { backpressure: 'drop-newest' });
|
|
|
|
await writer.write('a');
|
|
await writer.write('b');
|
|
|
|
assert.deepStrictEqual(
|
|
chunks.map((chunk) => chunk.toString()),
|
|
['a'],
|
|
);
|
|
|
|
writable.destroy();
|
|
}
|
|
|
|
testCachedWritableUsesLaterBackpressureOptions().then(common.mustCall());
|