Skip to content

Commit

Permalink
feat(ext/fetch): Request.bytes() and Response.bytes() (#23823)
Browse files Browse the repository at this point in the history
Closes #23790
  • Loading branch information
iuioiua authored May 23, 2024
1 parent f5d0c4b commit 8a636d0
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 48 deletions.
2 changes: 2 additions & 0 deletions cli/tsc/dts/lib.dom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3156,6 +3156,8 @@ interface Body {
arrayBuffer(): Promise<ArrayBuffer>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/blob) */
blob(): Promise<Blob>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/bytes) */
bytes(): Promise<Uint8Array>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/formData) */
formData(): Promise<FormData>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/json) */
Expand Down
2 changes: 2 additions & 0 deletions cli/tsc/dts/lib.webworker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,8 @@ interface Body {
arrayBuffer(): Promise<ArrayBuffer>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/blob) */
blob(): Promise<Blob>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/bytes) */
bytes(): Promise<Uint8Array>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/formData) */
formData(): Promise<FormData>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/json) */
Expand Down
13 changes: 12 additions & 1 deletion ext/fetch/22_body.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,15 @@ function mixinBody(prototype, bodySymbol, mimeTypeSymbol) {
configurable: true,
enumerable: true,
},
bytes: {
/** @returns {Promise<Uint8Array>} */
value: function bytes() {
return consumeBody(this, "bytes");
},
writable: true,
configurable: true,
enumerable: true,
},
formData: {
/** @returns {Promise<FormData>} */
value: function formData() {
Expand Down Expand Up @@ -330,7 +339,7 @@ function mixinBody(prototype, bodySymbol, mimeTypeSymbol) {
/**
* https://fetch.spec.whatwg.org/#concept-body-package-data
* @param {Uint8Array | string} bytes
* @param {"ArrayBuffer" | "Blob" | "FormData" | "JSON" | "text"} type
* @param {"ArrayBuffer" | "Blob" | "FormData" | "JSON" | "text" | "bytes"} type
* @param {MimeType | null} [mimeType]
*/
function packageData(bytes, type, mimeType) {
Expand All @@ -341,6 +350,8 @@ function packageData(bytes, type, mimeType) {
return new Blob([bytes], {
type: mimeType !== null ? mimesniff.serializeMimeType(mimeType) : "",
});
case "bytes":
return chunkToU8(bytes);
case "FormData": {
if (mimeType !== null) {
const essence = mimesniff.essence(mimeType);
Expand Down
4 changes: 4 additions & 0 deletions ext/fetch/lib.deno_fetch.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ declare interface Body {
* that resolves with a `Blob`.
*/
blob(): Promise<Blob>;
/** Takes a `Response` stream and reads it to completion. It returns a promise
* that resolves with a `Uint8Array`.
*/
bytes(): Promise<Uint8Array>;
/** Takes a `Response` stream and reads it to completion. It returns a promise
* that resolves with a `FormData` object.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/run/045_proxy/proxy_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function handler(req: Request): Promise<Response> {
method: req.method,
headers: headers,
});
return new Response(new Uint8Array(await resp.arrayBuffer()), {
return new Response(await resp.bytes(), {
status: resp.status,
headers: resp.headers,
});
Expand Down
12 changes: 4 additions & 8 deletions tests/testdata/fmt/with_config/subdir/a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ Deno.test(
.statusText,
)
const u8a =
new Uint8Array(
await response
.arrayBuffer(),
)
await response
.bytes()
const u8a1 =
new Uint8Array(
await response1
.arrayBuffer(),
)
await response1
.bytes()
for (
let i = 0;
i <
Expand Down
33 changes: 16 additions & 17 deletions tests/unit/fetch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ Deno.test({ permissions: { net: true } }, async function responseClone() {
assert(response !== response1);
assertEquals(response.status, response1.status);
assertEquals(response.statusText, response1.statusText);
const u8a = new Uint8Array(await response.arrayBuffer());
const u8a1 = new Uint8Array(await response1.arrayBuffer());
const u8a = await response.bytes();
const u8a1 = await response1.bytes();
for (let i = 0; i < u8a.byteLength; i++) {
assertEquals(u8a[i], u8a1[i]);
}
Expand Down Expand Up @@ -675,7 +675,7 @@ Deno.test(
["Foo", "Bar"],
],
});
await response.arrayBuffer();
await response.body?.cancel();
assertEquals(response.status, 404);
assertEquals(response.headers.get("Content-Length"), "2");

Expand Down Expand Up @@ -709,7 +709,7 @@ Deno.test(
["Accept-Language", "en-US"],
],
});
await response.arrayBuffer();
await response.body?.cancel();
assertEquals(response.status, 404);
assertEquals(response.headers.get("Content-Length"), "2");

Expand Down Expand Up @@ -743,7 +743,7 @@ Deno.test(
],
body,
});
await response.arrayBuffer();
await response.body?.cancel();
assertEquals(response.status, 404);
assertEquals(response.headers.get("Content-Length"), "2");

Expand Down Expand Up @@ -782,7 +782,7 @@ Deno.test(
],
body,
});
await response.arrayBuffer();
await response.body?.cancel();
assertEquals(response.status, 404);
assertEquals(response.headers.get("Content-Length"), "2");

Expand Down Expand Up @@ -816,7 +816,7 @@ Deno.test(
["Content-Length", "10"],
],
});
await response.arrayBuffer();
await response.body?.cancel();
assertEquals(response.status, 404);
assertEquals(response.headers.get("Content-Length"), "2");

Expand Down Expand Up @@ -847,7 +847,7 @@ Deno.test(
["Transfer-Encoding", "chunked"],
],
});
await response.arrayBuffer();
await response.body?.cancel();
assertEquals(response.status, 404);
assertEquals(response.headers.get("Content-Length"), "2");

Expand Down Expand Up @@ -933,7 +933,7 @@ Deno.test(function responseRedirectTakeURLObjectAsParameter() {

Deno.test(async function responseWithoutBody() {
const response = new Response();
assertEquals(await response.arrayBuffer(), new ArrayBuffer(0));
assertEquals(await response.bytes(), new Uint8Array(0));
const blob = await response.blob();
assertEquals(blob.size, 0);
assertEquals(await blob.arrayBuffer(), new ArrayBuffer(0));
Expand Down Expand Up @@ -1214,7 +1214,7 @@ Deno.test(
],
body: stream.readable,
});
await response.arrayBuffer();
await response.body?.cancel();
assertEquals(response.status, 404);
assertEquals(response.headers.get("Content-Length"), "2");

Expand Down Expand Up @@ -1793,10 +1793,9 @@ Deno.test(
const listener = invalidServer(addr, body);
const response = await fetch(`https://${addr}/`);

const res = await response.arrayBuffer();
const res = await response.bytes();
const buf = new TextEncoder().encode(data);
assertEquals(res.byteLength, buf.byteLength);
assertEquals(new Uint8Array(res), buf);
assertEquals(res, buf);

listener.close();
},
Expand Down Expand Up @@ -1850,10 +1849,10 @@ Deno.test(

// If content-length < totalLength, a maximum of content-length bytes
// should be returned.
const res = await response.arrayBuffer();
const res = await response.bytes();
const buf = new TextEncoder().encode(data);
assertEquals(res.byteLength, contentLength);
assertEquals(new Uint8Array(res), buf.subarray(contentLength));
assertEquals(res, buf.subarray(contentLength));

listener.close();
},
Expand Down Expand Up @@ -2029,15 +2028,15 @@ Deno.test(
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 actual = await req.bytes();
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 actual = await req.bytes();
const expected = new Uint8Array([2, 3, 4, 5]);
assertEquals(actual, expected);
});
8 changes: 4 additions & 4 deletions tests/unit/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ Deno.test(
headers: { "connection": "close" },
});

await resp.arrayBuffer();
await resp.body?.cancel();
await promise;
},
);
Expand Down Expand Up @@ -963,7 +963,7 @@ Deno.test(
await respondWith(new Response(f.readable, { status: 200 }));
})();
const resp = await fetch(`https://127.0.0.1:${listenPort}/`);
const body = await resp.arrayBuffer();
const body = await resp.bytes();
assertEquals(body.byteLength, 70 * 1024);
await promise;
httpConn!.close();
Expand Down Expand Up @@ -1293,8 +1293,8 @@ Deno.test(

const resp = await fetch(`https://localhost:${listenPort}/`);
assertEquals(resp.status, 200);
const body = await resp.arrayBuffer();
assertEquals(new Uint8Array(body), new Uint8Array([128]));
const body = await resp.bytes();
assertEquals(body, new Uint8Array([128]));

await promise;
httpConn!.close();
Expand Down
17 changes: 8 additions & 9 deletions tests/unit/serve_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ Deno.test(
});

const resp = await fetch(`https://localhost:${servePort}`);
dataPromise = resp.arrayBuffer();
dataPromise = resp.bytes();
}

assertEquals((await dataPromise).byteLength, 1048576);
Expand All @@ -358,7 +358,7 @@ Deno.test(

const [_, data] = await Promise.all([
server.shutdown(),
resp.arrayBuffer(),
resp.bytes(),
]);

assertEquals(data.byteLength, 1048576);
Expand Down Expand Up @@ -1861,13 +1861,12 @@ Deno.test(
signal: ac.signal,
});
const response = await fetch(`https://localhost:${servePort}/`);
const body = await response.arrayBuffer();
const body = await response.bytes();
assertEquals(1024 * 1024, body.byteLength);
const buffer = new Uint8Array(body);
for (let i = 0; i < 256; i++) {
assertEquals(
i,
buffer[i * 4096],
body[i * 4096],
`sentinel mismatch at index ${i * 4096}`,
);
}
Expand Down Expand Up @@ -2078,8 +2077,8 @@ Deno.test(
await deferred.promise;

assertEquals(resp.status, 200);
const body = await resp.arrayBuffer();
assertEquals(new Uint8Array(body), new Uint8Array([128]));
const body = await resp.bytes();
assertEquals(body, new Uint8Array([128]));

ac.abort();
await server.finished;
Expand Down Expand Up @@ -2694,7 +2693,7 @@ for (const testCase of compressionTestCases) {
headers: testCase.in as HeadersInit,
});
await deferred.promise;
const body = await resp.arrayBuffer();
const body = await resp.bytes();
if (testCase.expect == null) {
assertEquals(body.byteLength, testCase.length);
assertEquals(
Expand Down Expand Up @@ -2731,7 +2730,7 @@ Deno.test(
const server = Deno.serve({
handler: async (request) => {
assertEquals(
new Uint8Array(await request.arrayBuffer()),
await request.bytes(),
makeTempData(70 * 1024),
);
deferred.resolve();
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/worker_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ Deno.test({

assert(worker);
const response = await fetch("https://localhost:4506");
assert(await response.arrayBuffer());
assert(await response.bytes());
worker.terminate();
},
});
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_node/http_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Deno.test("[node/http] server can respond with 101, 204, 205, 304 status", async
// deno-lint-ignore no-explicit-any
`https://127.0.0.1:${(server.address() as any).port}/`,
);
await res.arrayBuffer();
await res.body?.cancel();
assertEquals(res.status, status);
server.close(() => resolve());
});
Expand Down
24 changes: 19 additions & 5 deletions tests/wpt/runner/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -7153,6 +7153,12 @@
"response": {
"json.any.html": true,
"json.any.worker.html": true,
"response-blob-realm.any.html": [
"realm of the Uint8Array from Response bytes()"
],
"response-blob-realm.any.worker.html": [
"realm of the Uint8Array from Response bytes()"
],
"response-init-001.any.html": true,
"response-init-001.any.worker.html": true,
"response-init-002.any.html": true,
Expand Down Expand Up @@ -8148,7 +8154,11 @@
"HTTP/1.1 200 ",
"HTTP/1.1 999 DOES IT MATTER "
],
"resources-with-0x00-in-header.window.html": false
"resources-with-0x00-in-header.window.html": [
"Expect network error for script with 0x00 in a header",
"Expect network error for frame navigation to resource with 0x00 in a header",
"Expect network error for image with 0x00 in a header"
]
},
"range": {
"general.any.html": [
Expand Down Expand Up @@ -12825,8 +12835,6 @@
"eventsource-onopen.any.worker.html": true,
"eventsource-prototype.any.html": true,
"eventsource-prototype.any.worker.html": true,
"eventsource-request-cancellation.window.any.html": false,
"eventsource-request-cancellation.window.any.worker.html": false,
"eventsource-url.any.html": true,
"eventsource-url.any.worker.html": true,
"format-bom-2.any.html": true,
Expand Down Expand Up @@ -12883,6 +12891,12 @@
"eventsource-constructor-stringify.window.html": false,
"eventsource-cross-origin.window.html": false,
"eventsource-reconnect.window.html": false,
"request-status-error.window.html": false
"request-status-error.window.html": false,
"eventsource-constructor-empty-url.any.serviceworker.html": false,
"eventsource-constructor-empty-url.any.sharedworker.html": false,
"eventsource-constructor-url-bogus.any.serviceworker.html": false,
"eventsource-constructor-url-bogus.any.sharedworker.html": false,
"request-credentials.window.html": false,
"request-redirect.window.html": false
}
}
}
2 changes: 1 addition & 1 deletion tests/wpt/suite
Submodule suite updated 462 files

0 comments on commit 8a636d0

Please sign in to comment.