Skip to content

Commit 04148ab

Browse files
committed
Don't throw EndOfStreamError on 0 bytes read, with mayBeLess flag set
1 parent 6eeebb6 commit 04148ab

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

lib/stream/AbstractStreamReader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export abstract class AbstractStreamReader implements IStreamReader {
5555
if (!this.endOfStream) {
5656
bytesRead += await this.readRemainderFromStream(buffer.subarray(bytesRead), mayBeLess);
5757
}
58-
if (bytesRead === 0) {
58+
if (bytesRead === 0 && !mayBeLess) {
5959
throw new EndOfStreamError();
6060
}
6161
return bytesRead;

lib/stream/WebStreamDefaultReader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class WebStreamDefaultReader extends AbstractStreamReader {
5555
}
5656
}
5757

58-
if (totalBytesRead === 0 && this.endOfStream) {
58+
if (!mayBeLess && totalBytesRead === 0 && this.endOfStream) {
5959
throw new EndOfStreamError();
6060
}
6161

test/test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,10 +1110,17 @@ describe('fromStream with mayBeLess flag', () => {
11101110
const streamReader = factory.fromString('abcdefg');
11111111

11121112
const buf = new Uint8Array(0);
1113-
const bytesRead = await streamReader.read(buf.subarray(0, 0));
1113+
const bytesRead = await streamReader.read(buf);
11141114
assert.strictEqual(bytesRead, 0, 'Should return');
11151115
});
11161116

1117+
it('should not throw an end-of-stream-error when mayBeLess=true', async () => {
1118+
const streamReader = factory.fromString('');
1119+
const buf = new Uint8Array(1);
1120+
const bytesRead = await streamReader.read(buf, true);
1121+
assert.strictEqual(bytesRead, 0, 'Expect 0 byte read');
1122+
});
1123+
11171124
it('read from a streamed data chunk', async () => {
11181125
const streamReader = factory.fromString('\x05peter');
11191126

0 commit comments

Comments
 (0)