From 8a636d0600dc7000d1e12b2fc4f4f46ecd70164a Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Thu, 23 May 2024 10:27:58 +1000 Subject: feat(ext/fetch): `Request.bytes()` and `Response.bytes()` (#23823) Closes #23790 --- ext/fetch/22_body.js | 13 ++++++++++++- ext/fetch/lib.deno_fetch.d.ts | 4 ++++ 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'ext/fetch') diff --git a/ext/fetch/22_body.js b/ext/fetch/22_body.js index e16fd4c54..e9d493658 100644 --- a/ext/fetch/22_body.js +++ b/ext/fetch/22_body.js @@ -296,6 +296,15 @@ function mixinBody(prototype, bodySymbol, mimeTypeSymbol) { configurable: true, enumerable: true, }, + bytes: { + /** @returns {Promise} */ + value: function bytes() { + return consumeBody(this, "bytes"); + }, + writable: true, + configurable: true, + enumerable: true, + }, formData: { /** @returns {Promise} */ value: function formData() { @@ -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) { @@ -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); diff --git a/ext/fetch/lib.deno_fetch.d.ts b/ext/fetch/lib.deno_fetch.d.ts index 4eb303e68..c27313903 100644 --- a/ext/fetch/lib.deno_fetch.d.ts +++ b/ext/fetch/lib.deno_fetch.d.ts @@ -58,6 +58,10 @@ declare interface Body { * that resolves with a `Blob`. */ blob(): Promise; + /** Takes a `Response` stream and reads it to completion. It returns a promise + * that resolves with a `Uint8Array`. + */ + bytes(): Promise; /** Takes a `Response` stream and reads it to completion. It returns a promise * that resolves with a `FormData` object. */ -- cgit v1.2.3