@@ -13,7 +13,6 @@ const { |
13 | 13 | ErrorCaptureStackTrace, |
14 | 14 | FunctionPrototypeBind, |
15 | 15 | FunctionPrototypeCall, |
16 | | - Number, |
17 | 16 | ObjectDefineProperties, |
18 | 17 | ObjectKeys, |
19 | 18 | PromisePrototypeThen, |
@@ -117,7 +116,6 @@ const { |
117 | 116 | ERR_QUIC_ENDPOINT_CLOSED, |
118 | 117 | ERR_QUIC_OPEN_STREAM_FAILED, |
119 | 118 | ERR_QUIC_STREAM_ABORTED, |
120 | | - ERR_QUIC_STREAM_RESET, |
121 | 119 | ERR_QUIC_VERSION_NEGOTIATION_ERROR, |
122 | 120 | }, |
123 | 121 | } = require('internal/errors'); |
@@ -401,6 +399,7 @@ const endpointRegistry = new SafeSet(); |
401 | 399 | * @property {number} [minVersion] The minimum acceptable QUIC version |
402 | 400 | * @property {'use'|'ignore'|'default'} [preferredAddressPolicy] The preferred address policy |
403 | 401 | * @property {'strict'|'auto'|'manual'} [verifyPeer='auto'] Peer certificate verification policy (client only) |
| 402 | + * @property {'error'|'ignore'} [truncatedReads] Truncated read policy |
404 | 403 | * @property {ApplicationOptions} [application] The application options |
405 | 404 | * @property {TransportParams} [transportParams] The transport parameters |
406 | 405 | * @property {string} [servername] The server name identifier (client only) |
@@ -1600,6 +1599,9 @@ class QuicStream { |
1600 | 1599 | state: undefined, |
1601 | 1600 | stats: undefined, |
1602 | 1601 | pendingClose: undefined, |
| 1602 | + destroyError: undefined, |
| 1603 | + stopSendingCode: undefined, |
| 1604 | + truncatedReads: undefined, |
1603 | 1605 | reader: undefined, |
1604 | 1606 | destroying: false, |
1605 | 1607 | iteratorLocked: false, |
@@ -1648,9 +1650,10 @@ class QuicStream { |
1648 | 1650 | * @param {object} handle |
1649 | 1651 | * @param {QuicSession} session |
1650 | 1652 | * @param {number} direction |
1651 | | - * @param {boolean} [isLocal] |
| 1653 | + * @param {boolean} isLocal |
| 1654 | + * @param {'error'|'ignore'} truncatedReads |
1652 | 1655 | */ |
1653 | | - constructor(privateSymbol, handle, session, direction, isLocal) { |
| 1656 | + constructor(privateSymbol, handle, session, direction, isLocal, truncatedReads) { |
1654 | 1657 | assertPrivateSymbol(privateSymbol); |
1655 | 1658 |
|
1656 | 1659 | this.#handle = handle; |
@@ -1659,6 +1662,7 @@ class QuicStream { |
1659 | 1662 | inner.session = session; |
1660 | 1663 | inner.direction = direction; |
1661 | 1664 | inner.isLocal = isLocal; |
| 1665 | + inner.truncatedReads = truncatedReads; |
1662 | 1666 | inner.state = new QuicStreamState( |
1663 | 1667 | kPrivateConstructor, handle.state, handle.stateByteOffset); |
1664 | 1668 |
|
@@ -1690,34 +1694,49 @@ class QuicStream { |
1690 | 1694 | inner.iteratorLocked = true; |
1691 | 1695 |
|
1692 | 1696 | inner.reader ??= this.#handle?.getReader(); |
1693 | | - // Non-readable stream (outbound-only unidirectional, or closed) |
1694 | | - if (!inner.reader) return; |
1695 | | - |
1696 | | - yield* createBlobReaderIterable(inner.reader, { |
1697 | | - getReadError: () => { |
1698 | | - // The read side ends for one of three reasons: |
1699 | | - // * Clean FIN received from the peer (state.finReceived |
1700 | | - // === true). The iterator stops without calling this; |
1701 | | - // fall through to the generic state error if it does. |
1702 | | - // * Peer sent us a RESET_STREAM. The C++ side records the |
1703 | | - // code in state.resetCode regardless of whether the JS |
1704 | | - // onreset handler was attached. state.finReceived stays |
1705 | | - // false because no FIN was seen. |
1706 | | - // * We aborted locally via stream.resetStream() or |
1707 | | - // stream.stopSending(). Both paths run EndReadable in |
1708 | | - // C++, setting state.readEnded without setting |
1709 | | - // state.finReceived. There is no peer code to surface. |
1710 | | - if (inner.state.readEnded && !inner.state.finReceived) { |
1711 | | - const peerResetCode = inner.state.resetCode; |
1712 | | - if (peerResetCode !== undefined && peerResetCode > 0n) { |
1713 | | - return new ERR_QUIC_STREAM_RESET(Number(peerResetCode)); |
1714 | | - } |
1715 | | - return new ERR_QUIC_STREAM_ABORTED( |
1716 | | - 'Stream aborted before FIN was received'); |
1717 | | - } |
1718 | | - return new ERR_INVALID_STATE('The stream is not readable'); |
1719 | | - }, |
1720 | | - }); |
| 1697 | + // No reader means either an outbound-only unidirectional stream, or a |
| 1698 | + // stream already destroyed (data gone, but truncation must still be |
| 1699 | + // checked below). |
| 1700 | + if (inner.reader) { |
| 1701 | + yield* createBlobReaderIterable(inner.reader); |
| 1702 | + } |
| 1703 | + |
| 1704 | + if (inner.state.readEnded && !inner.state.finReceived) { |
| 1705 | + // The readable has been truncated - ended with no clean FIN. We expose |
| 1706 | + // this in different ways depending on the truncatedReads option. |
| 1707 | + |
| 1708 | + // If we cancelled ourselves, check our own stop-sending code (not the |
| 1709 | + // result mirrored by the remote peer) |
| 1710 | + if (inner.stopSendingCode > 0n) { |
| 1711 | + throw new QuicError('Stream aborted before FIN was received', |
| 1712 | + { __proto__: null, |
| 1713 | + errorCode: inner.stopSendingCode }); |
| 1714 | + } |
| 1715 | + |
| 1716 | + // Non-zero reset is always an error: |
| 1717 | + const peerResetCode = inner.state.resetCode; |
| 1718 | + if (peerResetCode > 0n) { |
| 1719 | + throw new QuicError( |
| 1720 | + `The QUIC stream was reset by the peer with error code ${peerResetCode}`, |
| 1721 | + { __proto__: null, |
| 1722 | + code: 'ERR_QUIC_STREAM_RESET', |
| 1723 | + errorCode: peerResetCode }); |
| 1724 | + } |
| 1725 | + |
| 1726 | + // If stream teardown has started (stats.destroyedAt is set) then a |
| 1727 | + // close event confirming a final error/clean close will settle |
| 1728 | + // imminently (might be settled already). We await here to rethrow |
| 1729 | + // any errors if the connection has failed. |
| 1730 | + if (this.destroyed || this.stats.destroyedAt !== 0n) { |
| 1731 | + await this.closed; |
| 1732 | + } |
| 1733 | + |
| 1734 | + // Clean abort is truncation, but not necessarily an error: |
| 1735 | + if (inner.truncatedReads === 'error') { |
| 1736 | + throw new QuicError('Stream aborted before FIN was received', |
| 1737 | + { __proto__: null, errorCode: peerResetCode ?? 0n }); |
| 1738 | + } |
| 1739 | + } |
1721 | 1740 | } |
1722 | 1741 |
|
1723 | 1742 | /** |
@@ -2099,9 +2118,11 @@ class QuicStream { |
2099 | 2118 | if (error !== undefined && typeof inner.onerror === 'function') { |
2100 | 2119 | invokeOnerror(inner.onerror, error); |
2101 | 2120 | } |
2102 | | - const handle = this.#handle; |
2103 | | - this[kFinishClose](error); |
2104 | | - handle.destroy(); |
| 2121 | + // handle.destroy() kicks off all the cleanup internals, eventually |
| 2122 | + // including [kFinishClose] which needs this original destroy error: |
| 2123 | + inner.destroyError = error; |
| 2124 | + this.#handle.destroy(); |
| 2125 | + this[kFinishClose](error); // A no-op, unless destroy() failed |
2105 | 2126 | } |
2106 | 2127 |
|
2107 | 2128 | /** |
@@ -2519,7 +2540,9 @@ class QuicStream { |
2519 | 2540 | stopSending(code = 0n) { |
2520 | 2541 | assertIsQuicStream(this); |
2521 | 2542 | if (this.destroyed) return; |
2522 | | - this.#handle.stopSending(BigInt(code)); |
| 2543 | + const abortCode = BigInt(code); |
| 2544 | + this.#inner.stopSendingCode = abortCode; |
| 2545 | + this.#handle.stopSending(abortCode); |
2523 | 2546 | } |
2524 | 2547 |
|
2525 | 2548 | /** |
@@ -2612,6 +2635,9 @@ class QuicStream { |
2612 | 2635 | if (this.destroyed) { |
2613 | 2636 | return inner.pendingClose.promise; |
2614 | 2637 | } |
| 2638 | + // Prefer an error staged by destroy() (the original object the caller |
| 2639 | + // passed) over the error delivered by the native close callback. |
| 2640 | + error = inner.destroyError ?? error; |
2615 | 2641 | if (error !== undefined) { |
2616 | 2642 | inner.pendingClose.reject(error); |
2617 | 2643 | } else { |
@@ -2649,6 +2675,7 @@ class QuicStream { |
2649 | 2675 | inner.session = undefined; |
2650 | 2676 | inner.pendingClose.reject = undefined; |
2651 | 2677 | inner.pendingClose.resolve = undefined; |
| 2678 | + inner.destroyError = undefined; |
2652 | 2679 | inner.onblocked = undefined; |
2653 | 2680 | inner.onreset = undefined; |
2654 | 2681 | inner.onstopsending = undefined; |
@@ -2859,6 +2886,7 @@ class QuicSession { |
2859 | 2886 | // because server-side cert validation is handled by rejectUnauthorized |
2860 | 2887 | // at the C++ level. |
2861 | 2888 | verifyPeer: 'manual', |
| 2889 | + truncatedReads: 'error', |
2862 | 2890 | handshakeInfo: undefined, |
2863 | 2891 | /** @type {QuicSessionPath|undefined} */ |
2864 | 2892 | path: undefined, |
@@ -2892,8 +2920,9 @@ class QuicSession { |
2892 | 2920 | * @param {symbol} privateSymbol |
2893 | 2921 | * @param {object} handle |
2894 | 2922 | * @param {QuicEndpoint} endpoint |
| 2923 | + * @param {{ truncatedReads?: 'error'|'ignore' }} [options] |
2895 | 2924 | */ |
2896 | | - constructor(privateSymbol, handle, endpoint) { |
| 2925 | + constructor(privateSymbol, handle, endpoint, options = kEmptyObject) { |
2897 | 2926 | // Instances of QuicSession can only be created internally. |
2898 | 2927 | assertPrivateSymbol(privateSymbol); |
2899 | 2928 |
|
@@ -2902,6 +2931,8 @@ class QuicSession { |
2902 | 2931 |
|
2903 | 2932 | const inner = this.#inner; |
2904 | 2933 | inner.endpoint = endpoint; |
| 2934 | + const { truncatedReads } = options; |
| 2935 | + if (truncatedReads !== undefined) inner.truncatedReads = truncatedReads; |
2905 | 2936 | // Move any qlog entries that arrived before the wrapper existed. |
2906 | 2937 | if (handle._pendingQlog !== undefined) { |
2907 | 2938 | inner.pendingQlog = handle._pendingQlog; |
@@ -3447,7 +3478,8 @@ class QuicSession { |
3447 | 3478 | } |
3448 | 3479 |
|
3449 | 3480 | const stream = new QuicStream( |
3450 | | - kPrivateConstructor, handle, this, direction, true /* isLocal */); |
| 3481 | + kPrivateConstructor, handle, this, direction, true /* isLocal */, |
| 3482 | + inner.truncatedReads); |
3451 | 3483 | inner.streams.add(stream); |
3452 | 3484 | if (typeof this.#inner.onerror === 'function') { |
3453 | 3485 | markPromiseAsHandled(stream.closed); |
@@ -4232,7 +4264,7 @@ class QuicSession { |
4232 | 4264 | [kNewStream](handle, direction) { |
4233 | 4265 | const inner = this.#inner; |
4234 | 4266 | const stream = new QuicStream(kPrivateConstructor, handle, this, direction, |
4235 | | - false /* isLocal */); |
| 4267 | + false /* isLocal */, inner.truncatedReads); |
4236 | 4268 |
|
4237 | 4269 | // Set the default byte budget for received streams. |
4238 | 4270 | stream.budget = kDefaultBudget; |
@@ -4349,6 +4381,7 @@ class QuicEndpoint { |
4349 | 4381 | sessions: new SafeSet(), |
4350 | 4382 | stat: undefined, |
4351 | 4383 | stats: undefined, |
| 4384 | + truncatedReads: undefined, |
4352 | 4385 | onsession: undefined, |
4353 | 4386 | sessionCallbacks: undefined, |
4354 | 4387 | }; |
@@ -4481,8 +4514,8 @@ class QuicEndpoint { |
4481 | 4514 | }; |
4482 | 4515 | } |
4483 | 4516 |
|
4484 | | - #newSession(handle) { |
4485 | | - const session = new QuicSession(kPrivateConstructor, handle, this); |
| 4517 | + #newSession(handle, options) { |
| 4518 | + const session = new QuicSession(kPrivateConstructor, handle, this, options); |
4486 | 4519 | this.#inner.sessions.add(session); |
4487 | 4520 | // Set default pending datagram queue size. |
4488 | 4521 | session.maxPendingDatagrams = kDefaultMaxPendingDatagrams; |
@@ -4656,9 +4689,13 @@ class QuicEndpoint { |
4656 | 4689 | ontrailers, |
4657 | 4690 | oninfo, |
4658 | 4691 | onwanttrailers, |
| 4692 | + // Stored on the endpoint and applied to each incoming session. |
| 4693 | + truncatedReads, |
4659 | 4694 | ...rest |
4660 | 4695 | } = options; |
4661 | 4696 |
|
| 4697 | + inner.truncatedReads = truncatedReads; |
| 4698 | + |
4662 | 4699 | // Store session and stream callbacks to apply to each new incoming session. |
4663 | 4700 | inner.sessionCallbacks = { |
4664 | 4701 | __proto__: null, |
@@ -4700,6 +4737,7 @@ class QuicEndpoint { |
4700 | 4737 | validateObject(options, 'options'); |
4701 | 4738 | const { |
4702 | 4739 | sessionTicket, |
| 4740 | + truncatedReads, |
4703 | 4741 | ...rest |
4704 | 4742 | } = options; |
4705 | 4743 |
|
@@ -4708,7 +4746,7 @@ class QuicEndpoint { |
4708 | 4746 | if (handle === undefined) { |
4709 | 4747 | throw new ERR_QUIC_CONNECTION_FAILED(); |
4710 | 4748 | } |
4711 | | - const session = this.#newSession(handle); |
| 4749 | + const session = this.#newSession(handle, { __proto__: null, truncatedReads }); |
4712 | 4750 | // Set callbacks before any async work to avoid missing events |
4713 | 4751 | // that fire during or immediately after the handshake. |
4714 | 4752 | applyCallbacks(session, options); |
@@ -4942,7 +4980,8 @@ class QuicEndpoint { |
4942 | 4980 | const inner = this.#inner; |
4943 | 4981 | assert(typeof inner.onsession === 'function', |
4944 | 4982 | 'onsession callback not specified'); |
4945 | | - const session = this.#newSession(handle); |
| 4983 | + const session = this.#newSession(handle, |
| 4984 | + { __proto__: null, truncatedReads: inner.truncatedReads }); |
4946 | 4985 | // Apply session callbacks stored at listen time before notifying |
4947 | 4986 | // the onsession callback, to avoid missing events that fire |
4948 | 4987 | // during or immediately after the handshake. |
@@ -5434,6 +5473,7 @@ function processSessionOptions(options, config = kEmptyObject) { |
5434 | 5473 | maxDatagramSendAttempts = 5, |
5435 | 5474 | streamIdleTimeout, |
5436 | 5475 | verifyPeer = 'auto', |
| 5476 | + truncatedReads = 'error', |
5437 | 5477 | // HTTP/3 application-specific options. Nested under `application` |
5438 | 5478 | // to separate protocol-specific settings from transport-level ones. |
5439 | 5479 | application = kEmptyObject, |
@@ -5485,6 +5525,9 @@ function processSessionOptions(options, config = kEmptyObject) { |
5485 | 5525 | validateOneOf(verifyPeer, 'options.verifyPeer', |
5486 | 5526 | ['strict', 'auto', 'manual']); |
5487 | 5527 |
|
| 5528 | + validateOneOf(truncatedReads, 'options.truncatedReads', |
| 5529 | + ['error', 'ignore']); |
| 5530 | + |
5488 | 5531 | validateInteger(drainingPeriodMultiplier, 'options.drainingPeriodMultiplier', |
5489 | 5532 | 3, 255); |
5490 | 5533 |
|
@@ -5547,6 +5590,7 @@ function processSessionOptions(options, config = kEmptyObject) { |
5547 | 5590 | verifyHostname: verifyPeer !== 'manual', |
5548 | 5591 | }, |
5549 | 5592 | verifyPeer, |
| 5593 | + truncatedReads, |
5550 | 5594 | qlog, |
5551 | 5595 | maxPayloadSize, |
5552 | 5596 | unacknowledgedPacketThreshold, |
|