Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-by: Opencode:Opus 4.6 PR-URL: https://github.com/nodejs/node/pull/62876 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Robert Nagy <ronagy@icloud.com>
65 lines
2.2 KiB
JavaScript
65 lines
2.2 KiB
JavaScript
// Flags: --experimental-quic --experimental-stream-iter --no-warnings
|
|
|
|
// Test: basic unidirectional stream data transfer.
|
|
// The client creates a unidirectional stream with a body. The server reads
|
|
// the data and verifies integrity. The unidirectional stream is write-only
|
|
// on the client side and read-only on the server side.
|
|
|
|
import { hasQuic, skip, mustCall } from '../common/index.mjs';
|
|
import * as assert from 'node:assert';
|
|
|
|
const { deepStrictEqual, strictEqual } = assert;
|
|
|
|
if (!hasQuic) {
|
|
skip('QUIC is not enabled');
|
|
}
|
|
|
|
const { listen, connect } = await import('../common/quic.mjs');
|
|
const { bytes } = await import('stream/iter');
|
|
|
|
const message = 'unidirectional payload';
|
|
const encoder = new TextEncoder();
|
|
const decoder = new TextDecoder();
|
|
const body = encoder.encode(message);
|
|
const expected = encoder.encode(message);
|
|
|
|
const done = Promise.withResolvers();
|
|
|
|
const serverEndpoint = await listen(mustCall((serverSession) => {
|
|
serverSession.onstream = mustCall(async (stream) => {
|
|
strictEqual(stream.direction, 'uni');
|
|
|
|
const received = await bytes(stream);
|
|
deepStrictEqual(received, expected);
|
|
strictEqual(decoder.decode(received), message);
|
|
|
|
// The server side of a remote unidirectional stream is not writable.
|
|
// The writer should be pre-closed (desiredSize returns null).
|
|
const w = stream.writer;
|
|
strictEqual(w.desiredSize, null);
|
|
strictEqual(w.endSync(), 0);
|
|
|
|
await stream.closed;
|
|
serverSession.close();
|
|
done.resolve();
|
|
});
|
|
}));
|
|
|
|
const clientSession = await connect(serverEndpoint.address);
|
|
await clientSession.opened;
|
|
|
|
const stream = await clientSession.createUnidirectionalStream({ body });
|
|
strictEqual(stream.direction, 'uni');
|
|
|
|
// The client-side uni stream is write-only — async iteration yields nothing.
|
|
const iter = stream[Symbol.asyncIterator]();
|
|
const { done: iterDone } = await iter.next();
|
|
strictEqual(iterDone, true);
|
|
|
|
await done.promise;
|
|
// The server closed its session, delivering CONNECTION_CLOSE to the client.
|
|
// The client session enters the draining period, after which all streams
|
|
// and the session itself close cleanly.
|
|
await stream.closed;
|
|
await clientSession.close();
|
|
await serverEndpoint.close();
|