mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
The iterator of a push() readable resolved return(value) with an undefined value, unlike async generators and the other stream/iter iterators. Since push() readables are no longer wrapped by from(), this also applied to from() and pipeTo(). Resolve it with `value`. Assisted-by: OpenCode Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: https://github.com/nodejs/node/pull/66504 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
207 lines
5.1 KiB
JavaScript
207 lines
5.1 KiB
JavaScript
// Flags: --experimental-stream-iter
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const { from, push, text } = require('stream/iter');
|
|
|
|
async function testBasicWriteRead() {
|
|
const { writer, readable } = push();
|
|
|
|
writer.write('hello');
|
|
writer.end();
|
|
|
|
const data = await text(readable);
|
|
assert.strictEqual(data, 'hello');
|
|
}
|
|
|
|
async function testMultipleWrites() {
|
|
const { writer, readable } = push({ budget: 16384 });
|
|
|
|
writer.write('a');
|
|
writer.write('b');
|
|
writer.write('c');
|
|
writer.end();
|
|
|
|
const data = await text(readable);
|
|
assert.strictEqual(data, 'abc');
|
|
}
|
|
|
|
async function testCanWrite() {
|
|
const kHalf = new Uint8Array(8192);
|
|
const { writer } = push({ budget: 16384 });
|
|
|
|
assert.strictEqual(writer.canWrite, true);
|
|
writer.writeSync(kHalf);
|
|
assert.strictEqual(writer.canWrite, true); // 8192 < 16384
|
|
writer.writeSync(kHalf);
|
|
assert.strictEqual(writer.canWrite, false); // 16384 >= 16384
|
|
|
|
writer.end();
|
|
assert.strictEqual(writer.canWrite, null);
|
|
}
|
|
|
|
async function testWriterEnd() {
|
|
const { writer, readable } = push();
|
|
|
|
const totalBytes = writer.endSync();
|
|
assert.strictEqual(totalBytes, 0);
|
|
|
|
// Calling endSync again returns byte count (idempotent when closed)
|
|
assert.strictEqual(writer.endSync(), 0);
|
|
|
|
const batches = [];
|
|
for await (const batch of readable) {
|
|
batches.push(batch);
|
|
}
|
|
assert.strictEqual(batches.length, 0);
|
|
}
|
|
|
|
async function testWriterFail() {
|
|
const { writer, readable } = push();
|
|
|
|
writer.fail(new Error('test fail'));
|
|
|
|
await assert.rejects(
|
|
async () => {
|
|
// eslint-disable-next-line no-unused-vars
|
|
for await (const _ of readable) {
|
|
assert.fail('Should not reach here');
|
|
}
|
|
},
|
|
{ message: 'test fail' },
|
|
);
|
|
}
|
|
|
|
async function testConsumerBreak() {
|
|
const { writer, readable } = push({ budget: 16384 });
|
|
|
|
writer.writeSync('a');
|
|
writer.writeSync('b');
|
|
writer.writeSync('c');
|
|
|
|
// Break after first batch
|
|
// eslint-disable-next-line no-unused-vars
|
|
for await (const _ of readable) {
|
|
break;
|
|
}
|
|
|
|
// Writer should now see null canWrite
|
|
assert.strictEqual(writer.canWrite, null);
|
|
}
|
|
|
|
async function testAbortSignal() {
|
|
const ac = new AbortController();
|
|
const { readable } = push({ signal: ac.signal });
|
|
|
|
ac.abort();
|
|
|
|
await assert.rejects(
|
|
async () => {
|
|
// eslint-disable-next-line no-unused-vars
|
|
for await (const _ of readable) {
|
|
assert.fail('Should not reach here');
|
|
}
|
|
},
|
|
{ name: 'AbortError' },
|
|
);
|
|
}
|
|
|
|
async function testAbortSignalReason() {
|
|
const reason = 'test reason';
|
|
const ac = new AbortController();
|
|
const { writer } = push({ signal: ac.signal });
|
|
|
|
ac.abort(reason);
|
|
|
|
await assert.rejects(writer.write('data'), (err) => err === reason);
|
|
}
|
|
|
|
async function testPreAbortedSignal() {
|
|
const { readable } = push({ signal: AbortSignal.abort() });
|
|
await assert.rejects(async () => {
|
|
// eslint-disable-next-line no-unused-vars
|
|
for await (const _ of readable) {
|
|
assert.fail('Should not reach here');
|
|
}
|
|
}, { name: 'AbortError' });
|
|
}
|
|
|
|
async function testConsumerBreakWriteSyncReturnsFalse() {
|
|
const { writer, readable } = push({ budget: 16384 });
|
|
writer.writeSync('a');
|
|
|
|
// Break after first batch
|
|
// eslint-disable-next-line no-unused-vars
|
|
for await (const _ of readable) {
|
|
break;
|
|
}
|
|
|
|
// After consumer break, writeSync should return false
|
|
assert.strictEqual(writer.writeSync('b'), false);
|
|
assert.strictEqual(writer.canWrite, null);
|
|
}
|
|
|
|
async function testPushWithTransforms() {
|
|
const upper = (chunks) => {
|
|
if (chunks === null) return null;
|
|
return chunks.map((c) => {
|
|
const str = new TextDecoder().decode(c);
|
|
return new TextEncoder().encode(str.toUpperCase());
|
|
});
|
|
};
|
|
|
|
const { writer, readable } = push(upper);
|
|
|
|
writer.write('hello');
|
|
writer.end();
|
|
|
|
const data = await text(readable);
|
|
assert.strictEqual(data, 'HELLO');
|
|
}
|
|
|
|
async function testInvalidBackpressure() {
|
|
assert.throws(() => push({ backpressure: 'banana' }), {
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
|
});
|
|
assert.throws(() => push({ backpressure: '' }), {
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
|
});
|
|
|
|
// Valid values should not throw
|
|
for (const bp of ['strict', 'unbounded', 'drop-oldest', 'drop-newest']) {
|
|
push({ backpressure: bp });
|
|
}
|
|
}
|
|
|
|
async function testReturnValue() {
|
|
// return(value) resolves with `value`, read directly or through from().
|
|
for (const getIterator of [
|
|
(readable) => readable[Symbol.asyncIterator](),
|
|
(readable) => from(readable)[Symbol.asyncIterator](),
|
|
]) {
|
|
const { writer, readable } = push();
|
|
const iterator = getIterator(readable);
|
|
const result = await iterator.return('value');
|
|
assert.strictEqual(result.done, true);
|
|
assert.strictEqual(result.value, 'value');
|
|
assert.strictEqual(writer.canWrite, null);
|
|
}
|
|
}
|
|
|
|
Promise.all([
|
|
testBasicWriteRead(),
|
|
testMultipleWrites(),
|
|
testCanWrite(),
|
|
testWriterEnd(),
|
|
testWriterFail(),
|
|
testConsumerBreak(),
|
|
testAbortSignal(),
|
|
testAbortSignalReason(),
|
|
testPreAbortedSignal(),
|
|
testConsumerBreakWriteSyncReturnsFalse(),
|
|
testPushWithTransforms(),
|
|
testInvalidBackpressure(),
|
|
testReturnValue(),
|
|
]).then(common.mustCall());
|