2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-04-20 08:47:22 -04:00
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
/// <reference path="../webidl/internal.d.ts" />
|
|
|
|
/// <reference path="../url/internal.d.ts" />
|
|
|
|
/// <reference path="../url/lib.deno_url.d.ts" />
|
|
|
|
/// <reference path="../web/internal.d.ts" />
|
2021-06-10 09:26:10 -04:00
|
|
|
/// <reference path="../web/lib.deno_web.d.ts" />
|
2021-04-20 08:47:22 -04:00
|
|
|
/// <reference path="./internal.d.ts" />
|
2021-06-14 07:51:02 -04:00
|
|
|
/// <reference path="../web/06_streams_types.d.ts" />
|
2021-04-20 08:47:22 -04:00
|
|
|
/// <reference path="./lib.deno_fetch.d.ts" />
|
|
|
|
/// <reference lib="esnext" />
|
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
isAnyArrayBuffer,
|
|
|
|
isArrayBuffer,
|
|
|
|
} = core;
|
|
|
|
const {
|
|
|
|
ArrayBufferIsView,
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
DataViewPrototypeGetBuffer,
|
|
|
|
DataViewPrototypeGetByteLength,
|
|
|
|
DataViewPrototypeGetByteOffset,
|
|
|
|
JSONParse,
|
|
|
|
ObjectDefineProperties,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
TypedArrayPrototypeGetBuffer,
|
|
|
|
TypedArrayPrototypeGetByteLength,
|
|
|
|
TypedArrayPrototypeGetByteOffset,
|
|
|
|
TypedArrayPrototypeGetSymbolToStringTag,
|
|
|
|
TypedArrayPrototypeSlice,
|
|
|
|
TypeError,
|
|
|
|
Uint8Array,
|
|
|
|
} = primordials;
|
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
parseUrlEncoded,
|
|
|
|
URLSearchParamsPrototype,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_url/00_url.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
formDataFromEntries,
|
|
|
|
FormDataPrototype,
|
|
|
|
formDataToBlob,
|
|
|
|
parseFormData,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_fetch/21_formdata.js";
|
|
|
|
import * as mimesniff from "ext:deno_web/01_mimesniff.js";
|
|
|
|
import { BlobPrototype } from "ext:deno_web/09_file.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
createProxy,
|
|
|
|
errorReadableStream,
|
|
|
|
isReadableStreamDisturbed,
|
|
|
|
readableStreamClose,
|
|
|
|
readableStreamCollectIntoUint8Array,
|
|
|
|
readableStreamDisturb,
|
|
|
|
ReadableStreamPrototype,
|
2023-08-15 03:21:02 -04:00
|
|
|
readableStreamTee,
|
2023-02-07 14:22:46 -05:00
|
|
|
readableStreamThrowIfErrored,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_web/06_streams.js";
|
2021-04-20 08:47:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {Uint8Array | string} chunk
|
|
|
|
* @returns {Uint8Array}
|
|
|
|
*/
|
|
|
|
function chunkToU8(chunk) {
|
|
|
|
return typeof chunk === "string" ? core.encode(chunk) : chunk;
|
|
|
|
}
|
2021-10-26 16:00:01 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {Uint8Array | string} chunk
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function chunkToString(chunk) {
|
|
|
|
return typeof chunk === "string" ? chunk : core.decode(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
class InnerBody {
|
2021-10-26 16:00:01 -04:00
|
|
|
/**
|
2023-02-07 14:22:46 -05:00
|
|
|
* @param {ReadableStream<Uint8Array> | { body: Uint8Array | string, consumed: boolean }} stream
|
2021-10-26 16:00:01 -04:00
|
|
|
*/
|
2023-02-07 14:22:46 -05:00
|
|
|
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;
|
2021-10-26 16:00:01 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
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();
|
|
|
|
},
|
|
|
|
});
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return this.streamOrStatic;
|
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* https://fetch.spec.whatwg.org/#body-unusable
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
unusable() {
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
ReadableStreamPrototype,
|
|
|
|
this.streamOrStatic,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return this.streamOrStatic.locked ||
|
|
|
|
isReadableStreamDisturbed(this.streamOrStatic);
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return this.streamOrStatic.consumed;
|
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
consumed() {
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
ReadableStreamPrototype,
|
|
|
|
this.streamOrStatic,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return isReadableStreamDisturbed(this.streamOrStatic);
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return this.streamOrStatic.consumed;
|
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* 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;
|
2021-06-06 09:37:17 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-06-06 09:37:17 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
cancel(error) {
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
ReadableStreamPrototype,
|
|
|
|
this.streamOrStatic,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
this.streamOrStatic.cancel(error);
|
|
|
|
} else {
|
|
|
|
this.streamOrStatic.consumed = true;
|
2021-06-06 09:37:17 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-06-06 09:37:17 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
error(error) {
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
ReadableStreamPrototype,
|
|
|
|
this.streamOrStatic,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
errorReadableStream(this.streamOrStatic, error);
|
|
|
|
} else {
|
|
|
|
this.streamOrStatic.consumed = true;
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-06-23 10:00:23 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @returns {InnerBody}
|
|
|
|
*/
|
|
|
|
clone() {
|
2024-07-31 13:57:47 -04:00
|
|
|
let second;
|
|
|
|
if (
|
|
|
|
!ObjectPrototypeIsPrototypeOf(
|
|
|
|
ReadableStreamPrototype,
|
|
|
|
this.streamOrStatic,
|
|
|
|
) && !this.streamOrStatic.consumed
|
|
|
|
) {
|
|
|
|
second = new InnerBody({
|
|
|
|
body: this.streamOrStatic.body,
|
|
|
|
consumed: false,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const { 0: out1, 1: out2 } = readableStreamTee(this.stream, true);
|
|
|
|
this.streamOrStatic = out1;
|
|
|
|
second = new InnerBody(out2);
|
|
|
|
}
|
|
|
|
second.source = this.source;
|
2023-02-07 14:22:46 -05:00
|
|
|
second.length = this.length;
|
|
|
|
return second;
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-07 14:22:46 -05:00
|
|
|
* @returns {InnerBody}
|
2021-04-20 08:47:22 -04:00
|
|
|
*/
|
2023-02-07 14:22:46 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2022-10-08 16:08:47 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @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);
|
2022-10-08 16:08:47 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const body = object[bodySymbol] !== null
|
|
|
|
? await object[bodySymbol].consume()
|
|
|
|
: new Uint8Array();
|
2021-04-20 08:47:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
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;
|
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
},
|
|
|
|
bodyUsed: {
|
|
|
|
/**
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
get() {
|
|
|
|
webidl.assertBranded(this, prototype);
|
|
|
|
if (this[bodySymbol] !== null) {
|
|
|
|
return this[bodySymbol].consumed();
|
|
|
|
}
|
|
|
|
return false;
|
2021-04-20 08:47:22 -04:00
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
},
|
|
|
|
arrayBuffer: {
|
|
|
|
/** @returns {Promise<ArrayBuffer>} */
|
|
|
|
value: function arrayBuffer() {
|
|
|
|
return consumeBody(this, "ArrayBuffer");
|
2021-04-20 08:47:22 -04:00
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
writable: true,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
},
|
|
|
|
blob: {
|
|
|
|
/** @returns {Promise<Blob>} */
|
|
|
|
value: function blob() {
|
|
|
|
return consumeBody(this, "Blob");
|
2021-04-20 08:47:22 -04:00
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
writable: true,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
},
|
2024-05-22 20:27:58 -04:00
|
|
|
bytes: {
|
|
|
|
/** @returns {Promise<Uint8Array>} */
|
|
|
|
value: function bytes() {
|
|
|
|
return consumeBody(this, "bytes");
|
|
|
|
},
|
|
|
|
writable: true,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
formData: {
|
|
|
|
/** @returns {Promise<FormData>} */
|
|
|
|
value: function formData() {
|
|
|
|
return consumeBody(this, "FormData");
|
2021-04-20 08:47:22 -04:00
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
writable: true,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
},
|
|
|
|
json: {
|
|
|
|
/** @returns {Promise<any>} */
|
|
|
|
value: function json() {
|
|
|
|
return consumeBody(this, "JSON");
|
2021-04-20 08:47:22 -04:00
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
writable: true,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
/** @returns {Promise<string>} */
|
|
|
|
value: function text() {
|
|
|
|
return consumeBody(this, "text");
|
2021-04-20 08:47:22 -04:00
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
writable: true,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return ObjectDefineProperties(prototype, mixin);
|
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* https://fetch.spec.whatwg.org/#concept-body-package-data
|
|
|
|
* @param {Uint8Array | string} bytes
|
2024-05-22 20:27:58 -04:00
|
|
|
* @param {"ArrayBuffer" | "Blob" | "FormData" | "JSON" | "text" | "bytes"} type
|
2023-02-07 14:22:46 -05:00
|
|
|
* @param {MimeType | null} [mimeType]
|
|
|
|
*/
|
|
|
|
function packageData(bytes, type, mimeType) {
|
|
|
|
switch (type) {
|
|
|
|
case "ArrayBuffer":
|
2023-04-02 13:41:41 -04:00
|
|
|
return TypedArrayPrototypeGetBuffer(chunkToU8(bytes));
|
2023-02-07 14:22:46 -05:00
|
|
|
case "Blob":
|
|
|
|
return new Blob([bytes], {
|
|
|
|
type: mimeType !== null ? mimesniff.serializeMimeType(mimeType) : "",
|
|
|
|
});
|
2024-05-22 20:27:58 -04:00
|
|
|
case "bytes":
|
|
|
|
return chunkToU8(bytes);
|
2023-02-07 14:22:46 -05:00
|
|
|
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.",
|
2021-07-19 18:11:50 -04:00
|
|
|
);
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
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] }),
|
|
|
|
),
|
|
|
|
);
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new TypeError("Body can not be decoded as form data");
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new TypeError("Missing content type");
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "JSON":
|
|
|
|
return JSONParse(chunkToString(bytes));
|
|
|
|
case "text":
|
|
|
|
return chunkToString(bytes);
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @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;
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-04-02 13:41:41 -04:00
|
|
|
} else if (ArrayBufferIsView(object)) {
|
|
|
|
const tag = TypedArrayPrototypeGetSymbolToStringTag(object);
|
2023-04-28 08:26:21 -04:00
|
|
|
if (tag !== undefined) {
|
2023-04-02 13:41:41 -04:00
|
|
|
// TypedArray
|
2023-04-28 08:26:21 -04:00
|
|
|
if (tag !== "Uint8Array") {
|
|
|
|
// TypedArray, unless it's Uint8Array
|
|
|
|
object = new Uint8Array(
|
2023-04-02 13:41:41 -04:00
|
|
|
TypedArrayPrototypeGetBuffer(/** @type {Uint8Array} */ (object)),
|
|
|
|
TypedArrayPrototypeGetByteOffset(/** @type {Uint8Array} */ (object)),
|
|
|
|
TypedArrayPrototypeGetByteLength(/** @type {Uint8Array} */ (object)),
|
2023-04-28 08:26:21 -04:00
|
|
|
);
|
|
|
|
}
|
2023-04-02 13:41:41 -04:00
|
|
|
} else {
|
|
|
|
// DataView
|
2023-04-28 08:26:21 -04:00
|
|
|
object = new Uint8Array(
|
|
|
|
DataViewPrototypeGetBuffer(/** @type {DataView} */ (object)),
|
|
|
|
DataViewPrototypeGetByteOffset(/** @type {DataView} */ (object)),
|
|
|
|
DataViewPrototypeGetByteLength(/** @type {DataView} */ (object)),
|
2023-04-02 13:41:41 -04:00
|
|
|
);
|
|
|
|
}
|
2023-04-28 08:26:21 -04:00
|
|
|
source = TypedArrayPrototypeSlice(object);
|
2024-01-03 23:12:38 -05:00
|
|
|
} else if (isArrayBuffer(object)) {
|
2023-04-28 08:26:21 -04:00
|
|
|
source = TypedArrayPrototypeSlice(new Uint8Array(object));
|
2023-02-07 14:22:46 -05:00
|
|
|
} 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.
|
2023-06-05 15:57:01 -04:00
|
|
|
// deno-lint-ignore prefer-primordials
|
2023-02-07 14:22:46 -05:00
|
|
|
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");
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
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
|
2023-04-02 13:41:41 -04:00
|
|
|
} else if (TypedArrayPrototypeGetSymbolToStringTag(source) === "Uint8Array") {
|
2023-02-07 14:22:46 -05:00
|
|
|
stream = { body: source, consumed: false };
|
2023-04-02 13:41:41 -04:00
|
|
|
length = TypedArrayPrototypeGetByteLength(source);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
const body = new InnerBody(stream);
|
|
|
|
body.source = source;
|
|
|
|
body.length = length;
|
|
|
|
return { body, contentType };
|
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
|
2023-05-01 06:47:13 -04:00
|
|
|
webidl.converters["BodyInit_DOMString"] = (V, prefix, context, opts) => {
|
2023-02-07 14:22:46 -05:00
|
|
|
// Union for (ReadableStream or Blob or ArrayBufferView or ArrayBuffer or FormData or URLSearchParams or USVString)
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, V)) {
|
2023-05-01 06:47:13 -04:00
|
|
|
return webidl.converters["ReadableStream"](V, prefix, context, opts);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (ObjectPrototypeIsPrototypeOf(BlobPrototype, V)) {
|
2023-05-01 06:47:13 -04:00
|
|
|
return webidl.converters["Blob"](V, prefix, context, opts);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (ObjectPrototypeIsPrototypeOf(FormDataPrototype, V)) {
|
2023-05-01 06:47:13 -04:00
|
|
|
return webidl.converters["FormData"](V, prefix, context, opts);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (ObjectPrototypeIsPrototypeOf(URLSearchParamsPrototype, V)) {
|
2023-05-01 06:47:13 -04:00
|
|
|
return webidl.converters["URLSearchParams"](V, prefix, context, opts);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
if (typeof V === "object") {
|
2024-01-03 23:12:38 -05:00
|
|
|
if (isAnyArrayBuffer(V)) {
|
2023-05-01 06:47:13 -04:00
|
|
|
return webidl.converters["ArrayBuffer"](V, prefix, context, opts);
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (ArrayBufferIsView(V)) {
|
2023-05-01 06:47:13 -04:00
|
|
|
return webidl.converters["ArrayBufferView"](V, prefix, context, opts);
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
// 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.
|
2023-05-01 06:47:13 -04:00
|
|
|
return webidl.converters["DOMString"](V, prefix, context, opts);
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|
|
|
|
webidl.converters["BodyInit_DOMString?"] = webidl.createNullableConverter(
|
|
|
|
webidl.converters["BodyInit_DOMString"],
|
|
|
|
);
|
2021-04-20 08:47:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { extractBody, InnerBody, mixinBody };
|