Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmark/quic/h3-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function main({ mode, n }) {
alpn: 'h3',
...resume,
});
const stream = await session.createBidirectionalStream({
const stream = session.createBidirectionalStream({
headers: request,
onheaders,
});
Expand Down Expand Up @@ -121,7 +121,7 @@ async function main({ mode, n }) {
alpn: 'h3',
...resume,
});
const stream = await session.createBidirectionalStream({
const stream = session.createBidirectionalStream({
headers: request,
onheaders,
});
Expand Down
58 changes: 42 additions & 16 deletions doc/api/quic.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ import { Buffer } from 'node:buffer';
const enc = new TextEncoder();
const alpn = 'foo';
const client = await connect('123.123.123.123:8888', { alpn });
await client.createUnidirectionalStream({
client.createUnidirectionalStream({
body: enc.encode('hello world'),
});
```
Expand Down Expand Up @@ -1365,11 +1365,11 @@ added: v23.8.0
will buffer before `writeSync()` returns `false`. When the buffered
data exceeds this limit, the caller should wait for drain before
writing more. **Default:** `65536` (64 KB).
* `waitUntilAvailable` {boolean} When true the promise will wait until flow
control will allow to open the stream. If set to false, the function
will return a rejected promise, if flow control will not allow to
open the stream immediately.
**Default:** `true`
* `waitUntilAvailable` {boolean} When `true`, a stream that cannot be
opened immediately (because the handshake has not yet completed or the
peer's stream limit has been reached) is returned as a pending stream,
and is opened once that becomes possible. When `false`, an error is
thrown instead of creating a pending stream. **Default:** `true`.
* `onheaders` {Function} Callback for received initial response headers.
Called with `(headers)`.
* `ontrailers` {Function} Callback for received trailing headers.
Expand All @@ -1379,7 +1379,7 @@ added: v23.8.0
* `onwanttrailers` {Function} Callback when trailers should be sent.
Called with no arguments; use [`stream.sendTrailers()`][] within the
callback.
* Returns: {Promise} for a {quic.QuicStream}
* Returns: {quic.QuicStream}

Open a new bidirectional stream. If the `body` option is not specified,
the stream's outgoing side remains writable and no FIN is sent
Expand All @@ -1388,6 +1388,10 @@ options are only used when the session supports priority (e.g. HTTP/3).
The `headers`, `onheaders`, `ontrailers`, `oninfo`, and `onwanttrailers`
options are only used when the session supports headers (e.g. HTTP/3).

The stream is returned synchronously. If it cannot be opened immediately, it
is returned as a pending stream, with further operations queued until the
stream opens. See [`stream.pending`][].

### `session.createUnidirectionalStream([options])`

<!-- YAML
Expand All @@ -1411,25 +1415,28 @@ added: v23.8.0
will buffer before `writeSync()` returns `false`. When the buffered
data exceeds this limit, the caller should wait for drain before
writing more. **Default:** `65536` (64 KB).
* `waitUntilAvailable` {boolean} When true the promise will wait until flow
control will allow to open the stream. If set to false, the function
will return a rejected promise, if flow control will not allow
to open the stream immediately.
**Default:** `true`
* `waitUntilAvailable` {boolean} When `true`, a stream that cannot be
opened immediately (because the handshake has not yet completed or the
peer's stream limit has been reached) is returned as a pending stream,
and is opened once that becomes possible. When `false`, an error is
thrown instead of creating a pending stream. **Default:** `true`.
* `onheaders` {Function} Callback for received initial response headers.
Called with `(headers)`.
* `ontrailers` {Function} Callback for received trailing headers.
Called with `(trailers)`.
* `oninfo` {Function} Callback for received informational (1xx) headers.
Called with `(headers)`.
* `onwanttrailers` {Function} Callback when trailers should be sent.
* Returns: {Promise} for a {quic.QuicStream}
* Returns: {quic.QuicStream}

Open a new unidirectional stream. If the `body` option is not specified,
the stream's outgoing side remains writable and no FIN is sent
immediately. The `priority` and `incremental`
options are only used when the session supports priority (e.g. HTTP/3).

As with [`session.createBidirectionalStream()`][], the stream is returned
synchronously and may be pending.

### `session.path`

<!-- YAML
Expand Down Expand Up @@ -1956,9 +1963,25 @@ added: v26.10.0
* Type: {Promise}

A promise that is immediately fulfilled, if the stream fits within
flow control limits or fulfilled when the pending stream is created.
flow control limits or fulfilled when the pending stream is opened.
It rejects, if a pending stream is closed with an error before being
created.
opened.

### `stream.pending`

<!-- YAML
added: v23.8.0
-->

* Type: {boolean}

`true` if the stream has been created locally but not yet opened, either
because the handshake has not yet completed or because the peer's stream limit
has been reached. Read only.

While a stream is pending, [`stream.id`][] is `null`. Once it is opened,
[`stream.opened`][] is fulfilled. A pending stream can be used like any other
stream; its body and headers are queued until it is opened.

### `stream.closed`

Expand Down Expand Up @@ -4213,7 +4236,7 @@ const session = await connect('example.com:443', {
});
await session.opened;

const stream = await session.createBidirectionalStream({
const stream = session.createBidirectionalStream({
headers: {
':method': 'GET',
':path': '/',
Expand Down Expand Up @@ -4909,9 +4932,12 @@ throughput issues caused by flow control.
[`sessionOptions.truncatedReads`]: #sessionoptionstruncatedreads
[`stream.destroy()`]: #streamdestroyerror-options
[`stream.headers`]: #streamheaders
[`stream.id`]: #streamid
[`stream.onerror`]: #streamonerror
[`stream.onwanttrailers`]: #streamonwanttrailers
[`stream.opened`]: #streamopened
[`stream.pendingTrailers`]: #streampendingtrailers
[`stream.pending`]: #streampending
[`stream.priority`]: #streampriority
[`stream.resetStream()`]: #streamresetstreamcode
[`stream.sendHeaders()`]: #streamsendheadersheaders-options
Expand Down
25 changes: 16 additions & 9 deletions lib/internal/quic/quic.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ const endpointRegistry = new SafeSet();
* @property {boolean} [incremental] Whether to interleave data with same-priority streams.
* @property {number} [budget] The byte budget for write backpressure.
* **Default:** `65536`.
* @property {boolean} [waitUntilAvailable] When false, throw rather than
* creating a pending stream if the stream cannot be opened immediately.
* **Default:** `true`.
* @property {OnHeadersCallback} [onheaders] Callback for incoming initial headers
* @property {OnTrailersCallback} [ontrailers] Callback for incoming trailing headers
* @property {OnInfoCallback} [oninfo] Callback for informational (1xx) headers
Expand Down Expand Up @@ -3209,16 +3212,16 @@ class QuicSession {
* @param {OpenStreamOptions} options
* @returns {QuicStream}
*/
async #createStream(direction, options = kEmptyObject) {
#createStream(direction, options = kEmptyObject) {
const inner = this.#inner;
if (this.#isClosedOrClosing) {
throw new ERR_INVALID_STATE('Session is closed. New streams cannot be opened.');
}
const dir = direction === kStreamDirectionBidirectional ? 'bidi' : 'uni';
if (inner.state.isStreamOpenAllowed) {
debug(`opening new pending ${dir} stream`);
} else {
debug(`opening new ${dir} stream`);
} else {
debug(`opening new pending ${dir} stream`);
}

validateObject(options, 'options');
Expand Down Expand Up @@ -3291,23 +3294,27 @@ class QuicSession {
/**
* Creates a new bidirectional stream on this session. If the session
* does not allow new streams to be opened, an error will be thrown.
* If the stream cannot be opened on the wire yet, it is returned in a
* pending state.
* @param {OpenStreamOptions} [options]
* @returns {Promise<QuicStream>}
* @returns {QuicStream}
*/
async createBidirectionalStream(options = kEmptyObject) {
createBidirectionalStream(options = kEmptyObject) {
assertIsQuicSession(this);
return await this.#createStream(kStreamDirectionBidirectional, options);
return this.#createStream(kStreamDirectionBidirectional, options);
}

/**
* Creates a new unidirectional stream on this session. If the session
* does not allow new streams to be opened, an error will be thrown.
* If the stream cannot be opened on the wire yet, it is returned in a
* pending state.
* @param {OpenStreamOptions} [options]
* @returns {Promise<QuicStream>}
* @returns {QuicStream}
*/
async createUnidirectionalStream(options = kEmptyObject) {
createUnidirectionalStream(options = kEmptyObject) {
assertIsQuicSession(this);
return await this.#createStream(kStreamDirectionUnidirectional, options);
return this.#createStream(kStreamDirectionUnidirectional, options);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/common/quic.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ async function readStream(serverBody, options = {}) {
await session.opened;
session.closed.catch(() => {});

const stream = await session.createBidirectionalStream();
const stream = session.createBidirectionalStream();
await stream.writer.write(new Uint8Array([1]));

let closedError;
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-quic-callback-error-onblocked.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const clientSession = await connect(serverEndpoint.address, {
});
await clientSession.opened;

const stream = await clientSession.createBidirectionalStream();
const stream = clientSession.createBidirectionalStream();

stream.onblocked = mustCall(() => {
throw testError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const clientSession = await connect(serverEndpoint.address);
await clientSession.opened;

// Client-side stream onerror validation.
const stream = await clientSession.createBidirectionalStream({
const stream = clientSession.createBidirectionalStream({
body: new TextEncoder().encode('x'),
});

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-quic-callback-error-onreset.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const serverEndpoint = await listen(mustCall((serverSession) => {
const clientSession = await connect(serverEndpoint.address);
await clientSession.opened;

const stream = await clientSession.createBidirectionalStream({
const stream = clientSession.createBidirectionalStream({
body: encoder.encode('trigger onstream'),
});

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-quic-callback-error-onstream-async.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const clientSession = await connect(serverEndpoint.address, {
});
await clientSession.opened;

const stream = await clientSession.createBidirectionalStream({
const stream = clientSession.createBidirectionalStream({
body: new TextEncoder().encode('trigger onstream'),
});

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-quic-callback-error-onstream.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const clientSession = await connect(serverEndpoint.address, {
await clientSession.opened;

// Send data to trigger onstream on the server.
const stream = await clientSession.createBidirectionalStream({
const stream = clientSession.createBidirectionalStream({
body: encoder.encode('trigger onstream'),
});

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-quic-callback-error-stream-onerror.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const clientSession = await connect(serverEndpoint.address);
await clientSession.opened;

{
const stream = await clientSession.createBidirectionalStream({
const stream = clientSession.createBidirectionalStream({
body: encoder.encode('will error'),
});

Expand All @@ -65,7 +65,7 @@ await clientSession.opened;
// Create a stream with no body — use the writer API so the server sees
// it and can close cleanly.
{
const stream = await clientSession.createBidirectionalStream();
const stream = clientSession.createBidirectionalStream();
const w = stream.writer;

stream.onerror = mustNotCall('stream.onerror should not be called');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-quic-cc-algorithm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ for (const cc of ['reno', 'cubic', 'bbr']) {
const clientSession = await connect(serverEndpoint.address, { cc });
await clientSession.opened;

const stream = await clientSession.createBidirectionalStream({
const stream = clientSession.createBidirectionalStream({
body: encoder.encode('congestion control test'),
});

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-quic-connection-concurrent.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ for (let i = 0; i < numClients; i++) {
const cs = await connect(serverEndpoint.address, { reuseEndpoint: false });
await cs.opened;
const message = `client ${i}`;
const stream = await cs.createBidirectionalStream({
const stream = cs.createBidirectionalStream({
body: encoder.encode(message),
});
const received = await bytes(stream);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-quic-datagram-multiple.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ await clientSession.opened;
// Open a stream FIRST to establish bidirectional traffic.
// This ensures ACKs flow back from the server, keeping the
// congestion window open for subsequent datagram sends.
const stream = await clientSession.createBidirectionalStream({
const stream = clientSession.createBidirectionalStream({
body: new TextEncoder().encode('hello'),
});

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-quic-default-stream-limits.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ assert.ok(info, 'handshake info should be available');
// Check that the session has reasonable default stream limits by
// verifying we can create at least one bidirectional and one
// unidirectional stream.
const bidiStream = await clientSession.createBidirectionalStream();
const bidiStream = clientSession.createBidirectionalStream();
assert.ok(bidiStream, 'should be able to create a bidi stream');
bidiStream.destroy();

const uniStream = await clientSession.createUnidirectionalStream();
const uniStream = clientSession.createUnidirectionalStream();
assert.ok(uniStream, 'should be able to create a uni stream');
uniStream.destroy();

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-quic-diagnostics-channel-stream.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const serverEndpoint = await listen(mustCall((serverSession) => {
const clientSession = await connect(serverEndpoint.address);
await clientSession.opened;

const stream = await clientSession.createBidirectionalStream({
const stream = clientSession.createBidirectionalStream({
body: encoder.encode('diagnostics test'),
});

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-quic-early-selection-order.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const decoder = new TextDecoder();
});
await cs1.opened;
await Promise.all([gotTicket.promise, gotToken.promise]);
const s1 = await cs1.createBidirectionalStream({
const s1 = cs1.createBidirectionalStream({
headers: request,
onheaders: mustCall(),
});
Expand All @@ -76,7 +76,7 @@ const decoder = new TextDecoder();
});

// Send a 0RTT request immediately, before the handshake completes:
const s2 = await cs2.createBidirectionalStream({
const s2 = cs2.createBidirectionalStream({
headers: request,
onheaders: mustCall(),
});
Expand Down
10 changes: 5 additions & 5 deletions test/parallel/test-quic-edge-closing-ops.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ await clientSession.opened;
// Initiate graceful close.
clientSession.close();

// Creating a stream on a closing session rejects.
await assert.rejects(
clientSession.createBidirectionalStream(),
// Creating a stream on a closing session throws.
assert.throws(
() => clientSession.createBidirectionalStream(),
{ code: 'ERR_INVALID_STATE' },
);

await assert.rejects(
clientSession.createUnidirectionalStream(),
assert.throws(
() => clientSession.createUnidirectionalStream(),
{ code: 'ERR_INVALID_STATE' },
);

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-quic-edge-concurrent-close.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const clientSession = await connect(serverEndpoint.address);
await clientSession.opened;

// Open a stream so the server session has work, then close from both sides.
const stream = await clientSession.createBidirectionalStream();
const stream = clientSession.createBidirectionalStream();
stream.writer.endSync();
for await (const _ of stream) { /* drain */ } // eslint-disable-line no-unused-vars
await stream.closed;
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-quic-edge-destroyed-ops.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const serverEndpoint = await listen(mustCall(async (serverSession) => {
const clientSession = await connect(serverEndpoint.address);
await clientSession.opened;

const stream = await clientSession.createBidirectionalStream();
const stream = clientSession.createBidirectionalStream();

// Destroy the stream, then try operations on it.
stream.destroy();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-quic-edge-endpoint-destroy-active.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const clientSession = await connect(serverEndpoint.address);
await clientSession.opened;

// Create a stream so there's active work.
const stream = await clientSession.createBidirectionalStream({
const stream = clientSession.createBidirectionalStream({
body: encoder.encode('hello'),
});
for await (const _ of stream) { /* drain */ } // eslint-disable-line no-unused-vars
Expand Down
Loading
Loading