From bcd557baf79db080d5349e23c75c4239189e4380 Mon Sep 17 00:00:00 2001 From: Brian Botha Date: Wed, 31 Jan 2024 18:35:24 +1100 Subject: [PATCH] fix: using `reasonToCode` instead of using hard coded shutdown codes for streams * Related: #77 [ci skip] --- src/QUICConnection.ts | 36 ++++++++++++++++++++++++++++++------ src/errors.ts | 5 +++++ tests/QUICStream.test.ts | 4 ++-- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/QUICConnection.ts b/src/QUICConnection.ts index 6f0a0ee2..9db62530 100644 --- a/src/QUICConnection.ts +++ b/src/QUICConnection.ts @@ -542,10 +542,18 @@ class QUICConnection { // No `QUICStream` objects could have been created, however quiche stream // state should be cleaned up, and this can be done synchronously for (const streamId of this.conn.readable() as Iterable) { - this.conn.streamShutdown(streamId, quiche.Shutdown.Read, 0); + this.conn.streamShutdown( + streamId, + quiche.Shutdown.Read, + this.reasonToCode('read', e), + ); } for (const streamId of this.conn.writable() as Iterable) { - this.conn.streamShutdown(streamId, quiche.Shutdown.Write, 0); + this.conn.streamShutdown( + streamId, + quiche.Shutdown.Write, + this.reasonToCode('write', e), + ); } // According to RFC9000, closing while in the middle of a handshake // should use a transport error code `APPLICATION_ERROR`. @@ -954,8 +962,16 @@ class QUICConnection { if (quicStream == null) { if (this[running] === false || this[status] === 'stopping') { // We should reject new connections when stopping - this.conn.streamShutdown(streamId, Shutdown.Write, 1); - this.conn.streamShutdown(streamId, Shutdown.Read, 1); + this.conn.streamShutdown( + streamId, + Shutdown.Write, + this.reasonToCode('write', errors.ErrorQUICConnectionStopping), + ); + this.conn.streamShutdown( + streamId, + Shutdown.Read, + this.reasonToCode('read', errors.ErrorQUICConnectionStopping), + ); continue; } @@ -990,8 +1006,16 @@ class QUICConnection { if (quicStream == null) { if (this[running] === false || this[status] === 'stopping') { // We should reject new connections when stopping - this.conn.streamShutdown(streamId, Shutdown.Write, 1); - this.conn.streamShutdown(streamId, Shutdown.Read, 1); + this.conn.streamShutdown( + streamId, + Shutdown.Write, + this.reasonToCode('write', errors.ErrorQUICConnectionStopping), + ); + this.conn.streamShutdown( + streamId, + Shutdown.Read, + this.reasonToCode('read', errors.ErrorQUICConnectionStopping), + ); continue; } diff --git a/src/errors.ts b/src/errors.ts index 0b2d066a..229b8707 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -92,6 +92,10 @@ class ErrorQUICConnection extends ErrorQUIC { static description = 'QUIC Connection error'; } +class ErrorQUICConnectionStopping extends ErrorQUICConnection { + static description = 'QUIC Connection is stopping'; +} + class ErrorQUICConnectionNotRunning extends ErrorQUICConnection { static description = 'QUIC Connection is not running'; } @@ -297,6 +301,7 @@ export { ErrorQUICServerNewConnection, ErrorQUICServerInternal, ErrorQUICConnection, + ErrorQUICConnectionStopping, ErrorQUICConnectionNotRunning, ErrorQUICConnectionClosed, ErrorQUICConnectionStartData, diff --git a/tests/QUICStream.test.ts b/tests/QUICStream.test.ts index 557c871b..d2d1342e 100644 --- a/tests/QUICStream.test.ts +++ b/tests/QUICStream.test.ts @@ -1487,12 +1487,12 @@ describe(QUICStream.name, () => { // Creating a stream on the server side should throw const newStream = conn.newStream(); await newStream.writable.close(); - const asd = (async () => { + const code = (async () => { for await (const _ of newStream.readable) { // Do nothing } })(); - await expect(asd).rejects.toThrow('read 1'); + await expect(code).rejects.toThrow('read 0'); waitResolveP(); await Promise.all(activeServerStreams);