Skip to content

Commit e7a1039

Browse files
committed
fix: removed unnecessary looping in stream pulling
1 parent a99c0e5 commit e7a1039

File tree

1 file changed

+35
-41
lines changed

1 file changed

+35
-41
lines changed

src/QUICStream.ts

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -150,50 +150,44 @@ class QUICStream
150150
const buf = Buffer.alloc(1024);
151151
let recvLength: number, fin: boolean;
152152
// Read messages until buffer is empty
153-
while (true) {
154-
try {
155-
[recvLength, fin] = this.conn.streamRecv(this.streamId, buf);
156-
} catch (e) {
157-
this.logger.debug(`Stream recv reported: error ${e.message}`);
158-
// Done means there is no more data to read
159-
if (!this._recvClosed && e.message !== 'Done') {
160-
const reason =
161-
(await this.processSendStreamError(e, 'recv')) ?? e;
162-
// If it is `StreamReset(u64)` error, then the peer has closed
163-
// the stream, and we are receiving the error code
164-
// If it is not a `StreamReset(u64)`, then something else broke,
165-
// and we need to propagate the error up and down the stream
166-
controller.error(reason);
167-
await this.closeRecv(true, reason);
168-
// It is possible the stream was cancelled, let's check the writable state;
169-
try {
170-
this.conn.streamWritable(this.streamId, 0);
171-
} catch (e) {
172-
const match = e.message.match(/InvalidStreamState\((.+)\)/);
173-
if (match == null) {
174-
return never(
175-
'Errors besides [InvalidStreamState(StreamId)] are not expected here',
176-
);
177-
}
178-
this.writableController.error(reason);
153+
try {
154+
[recvLength, fin] = this.conn.streamRecv(this.streamId, buf);
155+
} catch (e) {
156+
this.logger.debug(`Stream recv reported: error ${e.message}`);
157+
// Done means there is no more data to read
158+
if (!this._recvClosed && e.message !== 'Done') {
159+
const reason =
160+
(await this.processSendStreamError(e, 'recv')) ?? e;
161+
// If it is `StreamReset(u64)` error, then the peer has closed
162+
// the stream, and we are receiving the error code
163+
// If it is not a `StreamReset(u64)`, then something else broke,
164+
// and we need to propagate the error up and down the stream
165+
controller.error(reason);
166+
await this.closeRecv(true, reason);
167+
// It is possible the stream was cancelled, let's check the writable state;
168+
try {
169+
this.conn.streamWritable(this.streamId, 0);
170+
} catch (e) {
171+
const match = e.message.match(/InvalidStreamState\((.+)\)/);
172+
if (match == null) {
173+
return never(
174+
'Errors besides [InvalidStreamState(StreamId)] are not expected here',
175+
);
179176
}
177+
this.writableController.error(reason);
180178
}
181-
break;
182-
}
183-
this.logger.debug(
184-
`stream read ${recvLength} bytes with fin(${fin})`,
185-
);
186-
// Check and drop if we're already closed or message is 0-length message
187-
if (!this._recvClosed && recvLength > 0) {
188-
this.readableController.enqueue(buf.subarray(0, recvLength));
189-
}
190-
// If fin is true, then that means, the stream is CLOSED
191-
if (fin) {
192-
await this.closeRecv();
193-
controller.close();
194-
// Return out of the loop
195-
break;
196179
}
180+
return;
181+
}
182+
this.logger.debug(`stream read ${recvLength} bytes with fin(${fin})`);
183+
// Check and drop if we're already closed or message is 0-length message
184+
if (!this._recvClosed && recvLength > 0) {
185+
controller.enqueue(buf.subarray(0, recvLength));
186+
}
187+
// If fin is true, then that means, the stream is CLOSED
188+
if (fin) {
189+
await this.closeRecv();
190+
controller.close();
197191
}
198192
},
199193
cancel: async (reason) => {

0 commit comments

Comments
 (0)