summaryrefslogtreecommitdiff
path: root/ext/fetch/22_body.js
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2023-02-07 20:22:46 +0100
committerGitHub <noreply@github.com>2023-02-07 20:22:46 +0100
commitb4aa1530970f7b9cc4e6f2f27e077852c4e178d3 (patch)
tree3d008912affe8550692183bd2697a386db5e3c79 /ext/fetch/22_body.js
parent65500f36e870b4ada3996b06aa287e30177d21a3 (diff)
refactor: Use ES modules for internal runtime code (#17648)
This PR refactors all internal js files (except core) to be written as ES modules. `__bootstrap`has been mostly replaced with static imports in form in `internal:[path to file from repo root]`. To specify if files are ESM, an `esm` method has been added to `Extension`, similar to the `js` method. A new ModuleLoader called `InternalModuleLoader` has been added to enable the loading of internal specifiers, which is used in all situations except when a snapshot is only loaded, and not a new one is created from it. --------- Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'ext/fetch/22_body.js')
-rw-r--r--ext/fetch/22_body.js820
1 files changed, 410 insertions, 410 deletions
diff --git a/ext/fetch/22_body.js b/ext/fetch/22_body.js
index bea1abce2..48819650a 100644
--- a/ext/fetch/22_body.js
+++ b/ext/fetch/22_body.js
@@ -10,462 +10,462 @@
/// <reference path="../web/06_streams_types.d.ts" />
/// <reference path="./lib.deno_fetch.d.ts" />
/// <reference lib="esnext" />
-"use strict";
-((window) => {
- const core = window.Deno.core;
- const webidl = globalThis.__bootstrap.webidl;
- const { parseUrlEncoded } = globalThis.__bootstrap.url;
- const { URLSearchParamsPrototype } = globalThis.__bootstrap.url;
- const {
- parseFormData,
- formDataFromEntries,
- formDataToBlob,
- FormDataPrototype,
- } = globalThis.__bootstrap.formData;
- const mimesniff = globalThis.__bootstrap.mimesniff;
- const { BlobPrototype } = globalThis.__bootstrap.file;
- const {
- isReadableStreamDisturbed,
- errorReadableStream,
- readableStreamClose,
- readableStreamDisturb,
- readableStreamCollectIntoUint8Array,
- readableStreamThrowIfErrored,
- createProxy,
- ReadableStreamPrototype,
- } = globalThis.__bootstrap.streams;
- const {
- ArrayBufferPrototype,
- ArrayBufferIsView,
- ArrayPrototypeMap,
- JSONParse,
- ObjectDefineProperties,
- ObjectPrototypeIsPrototypeOf,
- // TODO(lucacasonato): add SharedArrayBuffer to primordials
- // SharedArrayBufferPrototype
- TypedArrayPrototypeSlice,
- TypeError,
- Uint8Array,
- Uint8ArrayPrototype,
- } = window.__bootstrap.primordials;
+const core = globalThis.Deno.core;
+import * as webidl from "internal:ext/webidl/00_webidl.js";
+import {
+ parseUrlEncoded,
+ URLSearchParamsPrototype,
+} from "internal:ext/url/00_url.js";
+import {
+ formDataFromEntries,
+ FormDataPrototype,
+ formDataToBlob,
+ parseFormData,
+} from "internal:ext/fetch/21_formdata.js";
+import * as mimesniff from "internal:ext/web/01_mimesniff.js";
+import { BlobPrototype } from "internal:ext/web/09_file.js";
+import {
+ createProxy,
+ errorReadableStream,
+ isReadableStreamDisturbed,
+ readableStreamClose,
+ readableStreamCollectIntoUint8Array,
+ readableStreamDisturb,
+ ReadableStreamPrototype,
+ readableStreamThrowIfErrored,
+} from "internal:ext/web/06_streams.js";
+const primordials = globalThis.__bootstrap.primordials;
+const {
+ ArrayBufferPrototype,
+ ArrayBufferIsView,
+ ArrayPrototypeMap,
+ JSONParse,
+ ObjectDefineProperties,
+ ObjectPrototypeIsPrototypeOf,
+ // TODO(lucacasonato): add SharedArrayBuffer to primordials
+ // SharedArrayBufferPrototype
+ TypedArrayPrototypeSlice,
+ TypeError,
+ Uint8Array,
+ Uint8ArrayPrototype,
+} = primordials;
- /**
- * @param {Uint8Array | string} chunk
- * @returns {Uint8Array}
- */
- function chunkToU8(chunk) {
- return typeof chunk === "string" ? core.encode(chunk) : chunk;
- }
+/**
+ * @param {Uint8Array | string} chunk
+ * @returns {Uint8Array}
+ */
+function chunkToU8(chunk) {
+ return typeof chunk === "string" ? core.encode(chunk) : chunk;
+}
+/**
+ * @param {Uint8Array | string} chunk
+ * @returns {string}
+ */
+function chunkToString(chunk) {
+ return typeof chunk === "string" ? chunk : core.decode(chunk);
+}
+
+class InnerBody {
/**
- * @param {Uint8Array | string} chunk
- * @returns {string}
+ * @param {ReadableStream<Uint8Array> | { body: Uint8Array | string, consumed: boolean }} stream
*/
- function chunkToString(chunk) {
- return typeof chunk === "string" ? chunk : core.decode(chunk);
+ constructor(stream) {
+ /** @type {ReadableStream<Uint8Array> | { body: Uint8Array | string, consumed: boolean }} */
+ this.streamOrStatic = stream ??
+ { body: new Uint8Array(), consumed: false };
+ /** @type {null | Uint8Array | string | Blob | FormData} */
+ this.source = null;
+ /** @type {null | number} */
+ this.length = null;
}
- class InnerBody {
- /**
- * @param {ReadableStream<Uint8Array> | { body: Uint8Array | string, consumed: boolean }} stream
- */
- constructor(stream) {
- /** @type {ReadableStream<Uint8Array> | { body: Uint8Array | string, consumed: boolean }} */
- this.streamOrStatic = stream ??
- { body: new Uint8Array(), consumed: false };
- /** @type {null | Uint8Array | string | Blob | FormData} */
- this.source = null;
- /** @type {null | number} */
- this.length = null;
- }
-
- get stream() {
- if (
- !ObjectPrototypeIsPrototypeOf(
- ReadableStreamPrototype,
- this.streamOrStatic,
- )
- ) {
- const { body, consumed } = this.streamOrStatic;
- if (consumed) {
- this.streamOrStatic = new ReadableStream();
- this.streamOrStatic.getReader();
- readableStreamDisturb(this.streamOrStatic);
- readableStreamClose(this.streamOrStatic);
- } else {
- this.streamOrStatic = new ReadableStream({
- start(controller) {
- controller.enqueue(chunkToU8(body));
- controller.close();
- },
- });
- }
- }
- return this.streamOrStatic;
- }
-
- /**
- * https://fetch.spec.whatwg.org/#body-unusable
- * @returns {boolean}
- */
- unusable() {
- if (
- ObjectPrototypeIsPrototypeOf(
- ReadableStreamPrototype,
- this.streamOrStatic,
- )
- ) {
- return this.streamOrStatic.locked ||
- isReadableStreamDisturbed(this.streamOrStatic);
+ get stream() {
+ if (
+ !ObjectPrototypeIsPrototypeOf(
+ ReadableStreamPrototype,
+ this.streamOrStatic,
+ )
+ ) {
+ const { body, consumed } = this.streamOrStatic;
+ if (consumed) {
+ this.streamOrStatic = new ReadableStream();
+ this.streamOrStatic.getReader();
+ readableStreamDisturb(this.streamOrStatic);
+ readableStreamClose(this.streamOrStatic);
+ } else {
+ this.streamOrStatic = new ReadableStream({
+ start(controller) {
+ controller.enqueue(chunkToU8(body));
+ controller.close();
+ },
+ });
}
- return this.streamOrStatic.consumed;
}
+ return this.streamOrStatic;
+ }
- /**
- * @returns {boolean}
- */
- consumed() {
- if (
- ObjectPrototypeIsPrototypeOf(
- ReadableStreamPrototype,
- this.streamOrStatic,
- )
- ) {
- return isReadableStreamDisturbed(this.streamOrStatic);
- }
- return this.streamOrStatic.consumed;
+ /**
+ * https://fetch.spec.whatwg.org/#body-unusable
+ * @returns {boolean}
+ */
+ unusable() {
+ if (
+ ObjectPrototypeIsPrototypeOf(
+ ReadableStreamPrototype,
+ this.streamOrStatic,
+ )
+ ) {
+ return this.streamOrStatic.locked ||
+ isReadableStreamDisturbed(this.streamOrStatic);
}
+ return this.streamOrStatic.consumed;
+ }
- /**
- * https://fetch.spec.whatwg.org/#concept-body-consume-body
- * @returns {Promise<Uint8Array>}
- */
- consume() {
- if (this.unusable()) throw new TypeError("Body already consumed.");
- if (
- ObjectPrototypeIsPrototypeOf(
- ReadableStreamPrototype,
- this.streamOrStatic,
- )
- ) {
- readableStreamThrowIfErrored(this.stream);
- return readableStreamCollectIntoUint8Array(this.stream);
- } else {
- this.streamOrStatic.consumed = true;
- return this.streamOrStatic.body;
- }
+ /**
+ * @returns {boolean}
+ */
+ consumed() {
+ if (
+ ObjectPrototypeIsPrototypeOf(
+ ReadableStreamPrototype,
+ this.streamOrStatic,
+ )
+ ) {
+ return isReadableStreamDisturbed(this.streamOrStatic);
}
+ return this.streamOrStatic.consumed;
+ }
- cancel(error) {
- if (
- ObjectPrototypeIsPrototypeOf(
- ReadableStreamPrototype,
- this.streamOrStatic,
- )
- ) {
- this.streamOrStatic.cancel(error);
- } else {
- this.streamOrStatic.consumed = true;
- }
+ /**
+ * https://fetch.spec.whatwg.org/#concept-body-consume-body
+ * @returns {Promise<Uint8Array>}
+ */
+ consume() {
+ if (this.unusable()) throw new TypeError("Body already consumed.");
+ if (
+ ObjectPrototypeIsPrototypeOf(
+ ReadableStreamPrototype,
+ this.streamOrStatic,
+ )
+ ) {
+ readableStreamThrowIfErrored(this.stream);
+ return readableStreamCollectIntoUint8Array(this.stream);
+ } else {
+ this.streamOrStatic.consumed = true;
+ return this.streamOrStatic.body;
}
+ }
- error(error) {
- if (
- ObjectPrototypeIsPrototypeOf(
- ReadableStreamPrototype,
- this.streamOrStatic,
- )
- ) {
- errorReadableStream(this.streamOrStatic, error);
- } else {
- this.streamOrStatic.consumed = true;
- }
+ cancel(error) {
+ if (
+ ObjectPrototypeIsPrototypeOf(
+ ReadableStreamPrototype,
+ this.streamOrStatic,
+ )
+ ) {
+ this.streamOrStatic.cancel(error);
+ } else {
+ this.streamOrStatic.consumed = true;
}
+ }
- /**
- * @returns {InnerBody}
- */
- clone() {
- const { 0: out1, 1: out2 } = this.stream.tee();
- this.streamOrStatic = out1;
- const second = new InnerBody(out2);
- second.source = core.deserialize(core.serialize(this.source));
- second.length = this.length;
- return second;
+ error(error) {
+ if (
+ ObjectPrototypeIsPrototypeOf(
+ ReadableStreamPrototype,
+ this.streamOrStatic,
+ )
+ ) {
+ errorReadableStream(this.streamOrStatic, error);
+ } else {
+ this.streamOrStatic.consumed = true;
}
+ }
- /**
- * @returns {InnerBody}
- */
- createProxy() {
- let proxyStreamOrStatic;
- if (
- ObjectPrototypeIsPrototypeOf(
- ReadableStreamPrototype,
- this.streamOrStatic,
- )
- ) {
- proxyStreamOrStatic = createProxy(this.streamOrStatic);
- } else {
- proxyStreamOrStatic = { ...this.streamOrStatic };
- this.streamOrStatic.consumed = true;
- }
- const proxy = new InnerBody(proxyStreamOrStatic);
- proxy.source = this.source;
- proxy.length = this.length;
- return proxy;
- }
+ /**
+ * @returns {InnerBody}
+ */
+ clone() {
+ const { 0: out1, 1: out2 } = this.stream.tee();
+ this.streamOrStatic = out1;
+ const second = new InnerBody(out2);
+ second.source = core.deserialize(core.serialize(this.source));
+ second.length = this.length;
+ return second;
}
/**
- * @param {any} prototype
- * @param {symbol} bodySymbol
- * @param {symbol} mimeTypeSymbol
- * @returns {void}
+ * @returns {InnerBody}
*/
- function mixinBody(prototype, bodySymbol, mimeTypeSymbol) {
- async function consumeBody(object, type) {
- webidl.assertBranded(object, prototype);
+ createProxy() {
+ let proxyStreamOrStatic;
+ if (
+ ObjectPrototypeIsPrototypeOf(
+ ReadableStreamPrototype,
+ this.streamOrStatic,
+ )
+ ) {
+ proxyStreamOrStatic = createProxy(this.streamOrStatic);
+ } else {
+ proxyStreamOrStatic = { ...this.streamOrStatic };
+ this.streamOrStatic.consumed = true;
+ }
+ const proxy = new InnerBody(proxyStreamOrStatic);
+ proxy.source = this.source;
+ proxy.length = this.length;
+ return proxy;
+ }
+}
- const body = object[bodySymbol] !== null
- ? await object[bodySymbol].consume()
- : new Uint8Array();
+/**
+ * @param {any} prototype
+ * @param {symbol} bodySymbol
+ * @param {symbol} mimeTypeSymbol
+ * @returns {void}
+ */
+function mixinBody(prototype, bodySymbol, mimeTypeSymbol) {
+ async function consumeBody(object, type) {
+ webidl.assertBranded(object, prototype);
- const mimeType = type === "Blob" || type === "FormData"
- ? object[mimeTypeSymbol]
- : null;
- return packageData(body, type, mimeType);
- }
+ const body = object[bodySymbol] !== null
+ ? await object[bodySymbol].consume()
+ : new Uint8Array();
- /** @type {PropertyDescriptorMap} */
- const mixin = {
- body: {
- /**
- * @returns {ReadableStream<Uint8Array> | null}
- */
- get() {
- webidl.assertBranded(this, prototype);
- if (this[bodySymbol] === null) {
- return null;
- } else {
- return this[bodySymbol].stream;
- }
- },
- configurable: true,
- enumerable: true,
+ const mimeType = type === "Blob" || type === "FormData"
+ ? object[mimeTypeSymbol]
+ : null;
+ return packageData(body, type, mimeType);
+ }
+
+ /** @type {PropertyDescriptorMap} */
+ const mixin = {
+ body: {
+ /**
+ * @returns {ReadableStream<Uint8Array> | null}
+ */
+ get() {
+ webidl.assertBranded(this, prototype);
+ if (this[bodySymbol] === null) {
+ return null;
+ } else {
+ return this[bodySymbol].stream;
+ }
},
- bodyUsed: {
- /**
- * @returns {boolean}
- */
- get() {
- webidl.assertBranded(this, prototype);
- if (this[bodySymbol] !== null) {
- return this[bodySymbol].consumed();
- }
- return false;
- },
- configurable: true,
- enumerable: true,
+ configurable: true,
+ enumerable: true,
+ },
+ bodyUsed: {
+ /**
+ * @returns {boolean}
+ */
+ get() {
+ webidl.assertBranded(this, prototype);
+ if (this[bodySymbol] !== null) {
+ return this[bodySymbol].consumed();
+ }
+ return false;
},
- arrayBuffer: {
- /** @returns {Promise<ArrayBuffer>} */
- value: function arrayBuffer() {
- return consumeBody(this, "ArrayBuffer");
- },
- writable: true,
- configurable: true,
- enumerable: true,
+ configurable: true,
+ enumerable: true,
+ },
+ arrayBuffer: {
+ /** @returns {Promise<ArrayBuffer>} */
+ value: function arrayBuffer() {
+ return consumeBody(this, "ArrayBuffer");
},
- blob: {
- /** @returns {Promise<Blob>} */
- value: function blob() {
- return consumeBody(this, "Blob");
- },
- writable: true,
- configurable: true,
- enumerable: true,
+ writable: true,
+ configurable: true,
+ enumerable: true,
+ },
+ blob: {
+ /** @returns {Promise<Blob>} */
+ value: function blob() {
+ return consumeBody(this, "Blob");
},
- formData: {
- /** @returns {Promise<FormData>} */
- value: function formData() {
- return consumeBody(this, "FormData");
- },
- writable: true,
- configurable: true,
- enumerable: true,
+ writable: true,
+ configurable: true,
+ enumerable: true,
+ },
+ formData: {
+ /** @returns {Promise<FormData>} */
+ value: function formData() {
+ return consumeBody(this, "FormData");
},
- json: {
- /** @returns {Promise<any>} */
- value: function json() {
- return consumeBody(this, "JSON");
- },
- writable: true,
- configurable: true,
- enumerable: true,
+ writable: true,
+ configurable: true,
+ enumerable: true,
+ },
+ json: {
+ /** @returns {Promise<any>} */
+ value: function json() {
+ return consumeBody(this, "JSON");
},
- text: {
- /** @returns {Promise<string>} */
- value: function text() {
- return consumeBody(this, "text");
- },
- writable: true,
- configurable: true,
- enumerable: true,
+ writable: true,
+ configurable: true,
+ enumerable: true,
+ },
+ text: {
+ /** @returns {Promise<string>} */
+ value: function text() {
+ return consumeBody(this, "text");
},
- };
- return ObjectDefineProperties(prototype, mixin);
- }
+ writable: true,
+ configurable: true,
+ enumerable: true,
+ },
+ };
+ return ObjectDefineProperties(prototype, mixin);
+}
- /**
- * https://fetch.spec.whatwg.org/#concept-body-package-data
- * @param {Uint8Array | string} bytes
- * @param {"ArrayBuffer" | "Blob" | "FormData" | "JSON" | "text"} type
- * @param {MimeType | null} [mimeType]
- */
- function packageData(bytes, type, mimeType) {
- switch (type) {
- case "ArrayBuffer":
- return chunkToU8(bytes).buffer;
- case "Blob":
- return new Blob([bytes], {
- type: mimeType !== null ? mimesniff.serializeMimeType(mimeType) : "",
- });
- case "FormData": {
- if (mimeType !== null) {
- const essence = mimesniff.essence(mimeType);
- if (essence === "multipart/form-data") {
- const boundary = mimeType.parameters.get("boundary");
- if (boundary === null) {
- throw new TypeError(
- "Missing boundary parameter in mime type of multipart formdata.",
- );
- }
- return parseFormData(chunkToU8(bytes), boundary);
- } else if (essence === "application/x-www-form-urlencoded") {
- // TODO(@AaronO): pass as-is with StringOrBuffer in op-layer
- const entries = parseUrlEncoded(chunkToU8(bytes));
- return formDataFromEntries(
- ArrayPrototypeMap(
- entries,
- (x) => ({ name: x[0], value: x[1] }),
- ),
+/**
+ * https://fetch.spec.whatwg.org/#concept-body-package-data
+ * @param {Uint8Array | string} bytes
+ * @param {"ArrayBuffer" | "Blob" | "FormData" | "JSON" | "text"} type
+ * @param {MimeType | null} [mimeType]
+ */
+function packageData(bytes, type, mimeType) {
+ switch (type) {
+ case "ArrayBuffer":
+ return chunkToU8(bytes).buffer;
+ case "Blob":
+ return new Blob([bytes], {
+ type: mimeType !== null ? mimesniff.serializeMimeType(mimeType) : "",
+ });
+ case "FormData": {
+ if (mimeType !== null) {
+ const essence = mimesniff.essence(mimeType);
+ if (essence === "multipart/form-data") {
+ const boundary = mimeType.parameters.get("boundary");
+ if (boundary === null) {
+ throw new TypeError(
+ "Missing boundary parameter in mime type of multipart formdata.",
);
}
- throw new TypeError("Body can not be decoded as form data");
+ return parseFormData(chunkToU8(bytes), boundary);
+ } else if (essence === "application/x-www-form-urlencoded") {
+ // TODO(@AaronO): pass as-is with StringOrBuffer in op-layer
+ const entries = parseUrlEncoded(chunkToU8(bytes));
+ return formDataFromEntries(
+ ArrayPrototypeMap(
+ entries,
+ (x) => ({ name: x[0], value: x[1] }),
+ ),
+ );
}
- throw new TypeError("Missing content type");
+ throw new TypeError("Body can not be decoded as form data");
}
- case "JSON":
- return JSONParse(chunkToString(bytes));
- case "text":
- return chunkToString(bytes);
+ throw new TypeError("Missing content type");
}
+ case "JSON":
+ return JSONParse(chunkToString(bytes));
+ case "text":
+ return chunkToString(bytes);
}
+}
- /**
- * @param {BodyInit} object
- * @returns {{body: InnerBody, contentType: string | null}}
- */
- function extractBody(object) {
- /** @type {ReadableStream<Uint8Array> | { body: Uint8Array | string, consumed: boolean }} */
- let stream;
- let source = null;
- let length = null;
- let contentType = null;
- if (typeof object === "string") {
- source = object;
- contentType = "text/plain;charset=UTF-8";
- } else if (ObjectPrototypeIsPrototypeOf(BlobPrototype, object)) {
- stream = object.stream();
- source = object;
- length = object.size;
- if (object.type.length !== 0) {
- contentType = object.type;
- }
- } else if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, object)) {
- // Fast(er) path for common case of Uint8Array
- const copy = TypedArrayPrototypeSlice(object, 0, object.byteLength);
- source = copy;
- } else if (
- ArrayBufferIsView(object) ||
- ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, object)
- ) {
- const u8 = ArrayBufferIsView(object)
- ? new Uint8Array(
- object.buffer,
- object.byteOffset,
- object.byteLength,
- )
- : new Uint8Array(object);
- const copy = TypedArrayPrototypeSlice(u8, 0, u8.byteLength);
- source = copy;
- } else if (ObjectPrototypeIsPrototypeOf(FormDataPrototype, object)) {
- const res = formDataToBlob(object);
- stream = res.stream();
- source = res;
- length = res.size;
- contentType = res.type;
- } else if (
- ObjectPrototypeIsPrototypeOf(URLSearchParamsPrototype, object)
- ) {
- // TODO(@satyarohith): not sure what primordial here.
- source = object.toString();
- contentType = "application/x-www-form-urlencoded;charset=UTF-8";
- } else if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, object)) {
- stream = object;
- if (object.locked || isReadableStreamDisturbed(object)) {
- throw new TypeError("ReadableStream is locked or disturbed");
- }
+/**
+ * @param {BodyInit} object
+ * @returns {{body: InnerBody, contentType: string | null}}
+ */
+function extractBody(object) {
+ /** @type {ReadableStream<Uint8Array> | { body: Uint8Array | string, consumed: boolean }} */
+ let stream;
+ let source = null;
+ let length = null;
+ let contentType = null;
+ if (typeof object === "string") {
+ source = object;
+ contentType = "text/plain;charset=UTF-8";
+ } else if (ObjectPrototypeIsPrototypeOf(BlobPrototype, object)) {
+ stream = object.stream();
+ source = object;
+ length = object.size;
+ if (object.type.length !== 0) {
+ contentType = object.type;
}
- if (typeof source === "string") {
- // WARNING: this deviates from spec (expects length to be set)
- // https://fetch.spec.whatwg.org/#bodyinit > 7.
- // no observable side-effect for users so far, but could change
- stream = { body: source, consumed: false };
- length = null; // NOTE: string length != byte length
- } else if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, source)) {
- stream = { body: source, consumed: false };
- length = source.byteLength;
+ } else if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, object)) {
+ // Fast(er) path for common case of Uint8Array
+ const copy = TypedArrayPrototypeSlice(object, 0, object.byteLength);
+ source = copy;
+ } else if (
+ ArrayBufferIsView(object) ||
+ ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, object)
+ ) {
+ const u8 = ArrayBufferIsView(object)
+ ? new Uint8Array(
+ object.buffer,
+ object.byteOffset,
+ object.byteLength,
+ )
+ : new Uint8Array(object);
+ const copy = TypedArrayPrototypeSlice(u8, 0, u8.byteLength);
+ source = copy;
+ } else if (ObjectPrototypeIsPrototypeOf(FormDataPrototype, object)) {
+ const res = formDataToBlob(object);
+ stream = res.stream();
+ source = res;
+ length = res.size;
+ contentType = res.type;
+ } else if (
+ ObjectPrototypeIsPrototypeOf(URLSearchParamsPrototype, object)
+ ) {
+ // TODO(@satyarohith): not sure what primordial here.
+ source = object.toString();
+ contentType = "application/x-www-form-urlencoded;charset=UTF-8";
+ } else if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, object)) {
+ stream = object;
+ if (object.locked || isReadableStreamDisturbed(object)) {
+ throw new TypeError("ReadableStream is locked or disturbed");
}
- const body = new InnerBody(stream);
- body.source = source;
- body.length = length;
- return { body, contentType };
}
+ if (typeof source === "string") {
+ // WARNING: this deviates from spec (expects length to be set)
+ // https://fetch.spec.whatwg.org/#bodyinit > 7.
+ // no observable side-effect for users so far, but could change
+ stream = { body: source, consumed: false };
+ length = null; // NOTE: string length != byte length
+ } else if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, source)) {
+ stream = { body: source, consumed: false };
+ length = source.byteLength;
+ }
+ const body = new InnerBody(stream);
+ body.source = source;
+ body.length = length;
+ return { body, contentType };
+}
- webidl.converters["BodyInit_DOMString"] = (V, opts) => {
- // Union for (ReadableStream or Blob or ArrayBufferView or ArrayBuffer or FormData or URLSearchParams or USVString)
- if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, V)) {
- return webidl.converters["ReadableStream"](V, opts);
- } else if (ObjectPrototypeIsPrototypeOf(BlobPrototype, V)) {
- return webidl.converters["Blob"](V, opts);
- } else if (ObjectPrototypeIsPrototypeOf(FormDataPrototype, V)) {
- return webidl.converters["FormData"](V, opts);
- } else if (ObjectPrototypeIsPrototypeOf(URLSearchParamsPrototype, V)) {
- return webidl.converters["URLSearchParams"](V, opts);
+webidl.converters["BodyInit_DOMString"] = (V, opts) => {
+ // Union for (ReadableStream or Blob or ArrayBufferView or ArrayBuffer or FormData or URLSearchParams or USVString)
+ if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, V)) {
+ return webidl.converters["ReadableStream"](V, opts);
+ } else if (ObjectPrototypeIsPrototypeOf(BlobPrototype, V)) {
+ return webidl.converters["Blob"](V, opts);
+ } else if (ObjectPrototypeIsPrototypeOf(FormDataPrototype, V)) {
+ return webidl.converters["FormData"](V, opts);
+ } else if (ObjectPrototypeIsPrototypeOf(URLSearchParamsPrototype, V)) {
+ return webidl.converters["URLSearchParams"](V, opts);
+ }
+ if (typeof V === "object") {
+ if (
+ ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, V) ||
+ // deno-lint-ignore prefer-primordials
+ ObjectPrototypeIsPrototypeOf(SharedArrayBuffer.prototype, V)
+ ) {
+ return webidl.converters["ArrayBuffer"](V, opts);
}
- if (typeof V === "object") {
- if (
- ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, V) ||
- // deno-lint-ignore prefer-primordials
- ObjectPrototypeIsPrototypeOf(SharedArrayBuffer.prototype, V)
- ) {
- return webidl.converters["ArrayBuffer"](V, opts);
- }
- if (ArrayBufferIsView(V)) {
- return webidl.converters["ArrayBufferView"](V, opts);
- }
+ if (ArrayBufferIsView(V)) {
+ return webidl.converters["ArrayBufferView"](V, opts);
}
- // BodyInit conversion is passed to extractBody(), which calls core.encode().
- // core.encode() will UTF-8 encode strings with replacement, being equivalent to the USV normalization.
- // Therefore we can convert to DOMString instead of USVString and avoid a costly redundant conversion.
- return webidl.converters["DOMString"](V, opts);
- };
- webidl.converters["BodyInit_DOMString?"] = webidl.createNullableConverter(
- webidl.converters["BodyInit_DOMString"],
- );
+ }
+ // BodyInit conversion is passed to extractBody(), which calls core.encode().
+ // core.encode() will UTF-8 encode strings with replacement, being equivalent to the USV normalization.
+ // Therefore we can convert to DOMString instead of USVString and avoid a costly redundant conversion.
+ return webidl.converters["DOMString"](V, opts);
+};
+webidl.converters["BodyInit_DOMString?"] = webidl.createNullableConverter(
+ webidl.converters["BodyInit_DOMString"],
+);
- window.__bootstrap.fetchBody = { mixinBody, InnerBody, extractBody };
-})(globalThis);
+export { extractBody, InnerBody, mixinBody };