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
15 changes: 11 additions & 4 deletions lib/internal/quic/quic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1375,14 +1375,21 @@ function configureOutbound(handle, stream, body) {
// Handle Promise - await and recurse. Native promises auto-flatten,
// so the resolved value will never itself be a promise.
if (isPromise(body)) {
const onError = (err) => {
if (!stream.destroyed) {
stream.destroy(err);
}
};
PromisePrototypeThen(
body,
(resolved) => configureOutbound(handle, stream, resolved),
(err) => {
if (!stream.destroyed) {
stream.destroy(err);
(resolved) => {
try {
configureOutbound(handle, stream, resolved);
} catch (err) {
onError(err);
}
},
onError,
);
return;
}
Expand Down
39 changes: 39 additions & 0 deletions test/parallel/test-quic-stream-body-promise-invalid.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Flags: --experimental-quic --no-warnings

// Test: body: Promise resolving to an invalid body destroys the stream.
// When the body is a Promise that resolves to an unsupported type, the
// stream should be destroyed with the same ERR_INVALID_ARG_TYPE that
// setBody() throws synchronously for that type.

import { hasQuic, skip, mustCall } from '../common/index.mjs';
import assert from 'node:assert';

if (!hasQuic) {
skip('QUIC is not enabled');
}

const { listen, connect } = await import('../common/quic.mjs');

const serverEndpoint = await listen(mustCall(async (serverSession) => {
await serverSession.closed;
}), { transportParams: { maxIdleTimeout: 5 } });

const clientSession = await connect(serverEndpoint.address, {
transportParams: { maxIdleTimeout: 5 },
});
await clientSession.opened;

const stream = await clientSession.createBidirectionalStream();

assert.throws(() => stream.setBody(42), { code: 'ERR_INVALID_ARG_TYPE' });

const stream2 = await clientSession.createBidirectionalStream();

const closedPromise = assert.rejects(stream2.closed, {
code: 'ERR_INVALID_ARG_TYPE',
});

stream2.setBody(Promise.resolve(42));

await Promise.all([closedPromise, clientSession.closed]);
await serverEndpoint.close();
Loading