diff options
-rw-r--r-- | cli/tests/unit/fetch_test.ts | 16 | ||||
-rw-r--r-- | ext/fetch/22_body.js | 42 |
2 files changed, 28 insertions, 30 deletions
diff --git a/cli/tests/unit/fetch_test.ts b/cli/tests/unit/fetch_test.ts index bafb23c2a..a92a7a051 100644 --- a/cli/tests/unit/fetch_test.ts +++ b/cli/tests/unit/fetch_test.ts @@ -1893,3 +1893,19 @@ Deno.test( await server; }, ); + +Deno.test("Request with subarray TypedArray body", async () => { + const body = new Uint8Array([1, 2, 3, 4, 5]).subarray(1); + const req = new Request("https://example.com", { method: "POST", body }); + const actual = new Uint8Array(await req.arrayBuffer()); + const expected = new Uint8Array([2, 3, 4, 5]); + assertEquals(actual, expected); +}); + +Deno.test("Response with subarray TypedArray body", async () => { + const body = new Uint8Array([1, 2, 3, 4, 5]).subarray(1); + const req = new Response(body); + const actual = new Uint8Array(await req.arrayBuffer()); + const expected = new Uint8Array([2, 3, 4, 5]); + assertEquals(actual, expected); +}); diff --git a/ext/fetch/22_body.js b/ext/fetch/22_body.js index 9dbd58fa4..875ec0620 100644 --- a/ext/fetch/22_body.js +++ b/ext/fetch/22_body.js @@ -38,7 +38,6 @@ import { const primordials = globalThis.__bootstrap.primordials; const { ArrayBufferPrototype, - ArrayBufferPrototypeGetByteLength, ArrayBufferIsView, ArrayPrototypeMap, DataViewPrototypeGetBuffer, @@ -394,44 +393,27 @@ function extractBody(object) { } } else if (ArrayBufferIsView(object)) { const tag = TypedArrayPrototypeGetSymbolToStringTag(object); - if (tag === "Uint8Array") { - // Fast(er) path for common case of Uint8Array - const copy = TypedArrayPrototypeSlice( - object, - TypedArrayPrototypeGetByteOffset(/** @type {Uint8Array} */ (object)), - TypedArrayPrototypeGetByteLength(/** @type {Uint8Array} */ (object)), - ); - source = copy; - } else if (tag !== undefined) { + if (tag !== undefined) { // TypedArray - const copy = TypedArrayPrototypeSlice( - new Uint8Array( + if (tag !== "Uint8Array") { + // TypedArray, unless it's Uint8Array + object = new Uint8Array( TypedArrayPrototypeGetBuffer(/** @type {Uint8Array} */ (object)), TypedArrayPrototypeGetByteOffset(/** @type {Uint8Array} */ (object)), TypedArrayPrototypeGetByteLength(/** @type {Uint8Array} */ (object)), - ), - ); - source = copy; + ); + } } else { // DataView - const copy = TypedArrayPrototypeSlice( - new Uint8Array( - DataViewPrototypeGetBuffer(/** @type {DataView} */ (object)), - DataViewPrototypeGetByteOffset(/** @type {DataView} */ (object)), - DataViewPrototypeGetByteLength(/** @type {DataView} */ (object)), - ), + object = new Uint8Array( + DataViewPrototypeGetBuffer(/** @type {DataView} */ (object)), + DataViewPrototypeGetByteOffset(/** @type {DataView} */ (object)), + DataViewPrototypeGetByteLength(/** @type {DataView} */ (object)), ); - source = copy; } + source = TypedArrayPrototypeSlice(object); } else if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, object)) { - const copy = TypedArrayPrototypeSlice( - new Uint8Array( - object, - 0, - ArrayBufferPrototypeGetByteLength(object), - ), - ); - source = copy; + source = TypedArrayPrototypeSlice(new Uint8Array(object)); } else if (ObjectPrototypeIsPrototypeOf(FormDataPrototype, object)) { const res = formDataToBlob(object); stream = res.stream(); |