Skip to content

Commit

Permalink
fix: RangeError handling
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a committed Oct 23, 2023
1 parent b44a3a2 commit 320e1a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/buffered_readable_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class BufferedReadableStream {

async readFull(buffer: Uint8Array): Promise<void> {
if (buffer.length <= this.#buffer.length) {
buffer.set(this.#buffer);
buffer.set(this.#buffer.subarray(0, buffer.length));
this.#buffer = this.#buffer.subarray(buffer.length);
return;
}
Expand Down
32 changes: 32 additions & 0 deletions internal/buffered_readable_stream_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { encoder } from "../protocol/_util.ts";
import {
assertEquals,
assertFalse,
Expand Down Expand Up @@ -62,6 +63,37 @@ Deno.test({

assertFalse(readable.locked);
});

await t.step(
"`readFull` should not throw `RangeError: offset is out of bounds` error",
async () => {
const readable = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(encoder.encode("foobar"));
controller.close();
},
});
const buffered = new BufferedReadableStream(readable);
{
const buf = new Uint8Array(3);
await buffered.readFull(buf);
assertEquals(decoder.decode(buf), "foo");
}

{
const buf = new Uint8Array(1);
await buffered.readFull(buf);
assertEquals(decoder.decode(buf), "b");
}

await buffered.readFull(new Uint8Array(0));
{
const buf = new Uint8Array(2);
await buffered.readFull(buf);
assertEquals(decoder.decode(buf), "ar");
}
},
);
},
});

Expand Down

0 comments on commit 320e1a7

Please sign in to comment.