2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2021-01-28 15:37:21 -05:00
|
|
|
// @ts-check
|
|
|
|
/// <reference path="../../core/lib.deno_core.d.ts" />
|
|
|
|
/// <reference path="../web/internal.d.ts" />
|
2021-04-14 16:49:16 -04:00
|
|
|
/// <reference path="../url/internal.d.ts" />
|
2021-01-28 15:37:21 -05:00
|
|
|
/// <reference path="../web/lib.deno_web.d.ts" />
|
2021-06-14 07:51:02 -04:00
|
|
|
/// <reference path="../web/06_streams_types.d.ts" />
|
2021-01-28 15:37:21 -05:00
|
|
|
/// <reference path="./internal.d.ts" />
|
|
|
|
/// <reference path="./lib.deno_fetch.d.ts" />
|
|
|
|
/// <reference lib="esnext" />
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2024-01-26 17:46:46 -05:00
|
|
|
import {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_fetch,
|
2024-01-29 16:02:26 -05:00
|
|
|
op_fetch_send,
|
2024-01-10 17:37:25 -05:00
|
|
|
op_wasm_streaming_feed,
|
|
|
|
op_wasm_streaming_set_url,
|
2024-01-26 17:46:46 -05:00
|
|
|
} from "ext:core/ops";
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeSplice,
|
|
|
|
ArrayPrototypeFilter,
|
|
|
|
ArrayPrototypeIncludes,
|
|
|
|
Error,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
Promise,
|
|
|
|
PromisePrototypeThen,
|
|
|
|
PromisePrototypeCatch,
|
|
|
|
SafeArrayIterator,
|
|
|
|
String,
|
|
|
|
StringPrototypeStartsWith,
|
|
|
|
StringPrototypeToLowerCase,
|
|
|
|
TypeError,
|
|
|
|
TypedArrayPrototypeGetSymbolToStringTag,
|
|
|
|
} = primordials;
|
2023-12-26 20:30:26 -05:00
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
|
|
|
import { byteLowerCase } from "ext:deno_web/00_infra.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
errorReadableStream,
|
2023-12-01 10:56:10 -05:00
|
|
|
getReadableStreamResourceBacking,
|
2023-02-07 14:22:46 -05:00
|
|
|
readableStreamForRid,
|
|
|
|
ReadableStreamPrototype,
|
2023-12-01 10:56:10 -05:00
|
|
|
resourceForReadableStream,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_web/06_streams.js";
|
|
|
|
import { extractBody, InnerBody } from "ext:deno_fetch/22_body.js";
|
|
|
|
import { processUrlList, toInnerRequest } from "ext:deno_fetch/23_request.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
abortedNetworkError,
|
|
|
|
fromInnerResponse,
|
|
|
|
networkError,
|
|
|
|
nullBodyStatus,
|
|
|
|
redirectStatus,
|
|
|
|
toInnerResponse,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_fetch/23_response.js";
|
|
|
|
import * as abortSignal from "ext:deno_web/03_abort_signal.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
const REQUEST_BODY_HEADER_NAMES = [
|
|
|
|
"content-encoding",
|
|
|
|
"content-language",
|
|
|
|
"content-location",
|
|
|
|
"content-type",
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} rid
|
2023-12-01 10:56:10 -05:00
|
|
|
* @returns {Promise<{ status: number, statusText: string, headers: [string, string][], url: string, responseRid: number, error: string? }>}
|
2023-02-07 14:22:46 -05:00
|
|
|
*/
|
|
|
|
function opFetchSend(rid) {
|
2023-12-26 20:30:26 -05:00
|
|
|
return op_fetch_send(rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} responseBodyRid
|
|
|
|
* @param {AbortSignal} [terminator]
|
|
|
|
* @returns {ReadableStream<Uint8Array>}
|
|
|
|
*/
|
|
|
|
function createResponseBodyStream(responseBodyRid, terminator) {
|
|
|
|
const readable = readableStreamForRid(responseBodyRid);
|
|
|
|
|
|
|
|
function onAbort() {
|
|
|
|
errorReadableStream(readable, terminator.reason);
|
|
|
|
core.tryClose(responseBodyRid);
|
2021-01-10 14:54:29 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// TODO(lucacasonato): clean up registration
|
|
|
|
terminator[abortSignal.add](onAbort);
|
|
|
|
|
|
|
|
return readable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {InnerRequest} req
|
|
|
|
* @param {boolean} recursive
|
|
|
|
* @param {AbortSignal} terminator
|
|
|
|
* @returns {Promise<InnerResponse>}
|
|
|
|
*/
|
|
|
|
async function mainFetch(req, recursive, terminator) {
|
|
|
|
if (req.blobUrlEntry !== null) {
|
|
|
|
if (req.method !== "GET") {
|
|
|
|
throw new TypeError("Blob URL fetch only supports GET method.");
|
2021-06-06 09:37:17 -04:00
|
|
|
}
|
2022-10-04 09:48:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const body = new InnerBody(req.blobUrlEntry.stream());
|
|
|
|
terminator[abortSignal.add](() => body.error(terminator.reason));
|
|
|
|
processUrlList(req.urlList, req.urlListProcessed);
|
2022-10-04 09:48:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return {
|
|
|
|
headerList: [
|
|
|
|
["content-length", String(req.blobUrlEntry.size)],
|
|
|
|
["content-type", req.blobUrlEntry.type],
|
|
|
|
],
|
|
|
|
status: 200,
|
|
|
|
statusMessage: "OK",
|
|
|
|
body,
|
|
|
|
type: "basic",
|
|
|
|
url() {
|
|
|
|
if (this.urlList.length == 0) return null;
|
|
|
|
return this.urlList[this.urlList.length - 1];
|
|
|
|
},
|
|
|
|
urlList: recursive
|
|
|
|
? []
|
|
|
|
: [...new SafeArrayIterator(req.urlListProcessed)],
|
|
|
|
};
|
2021-04-12 20:45:57 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/** @type {ReadableStream<Uint8Array> | Uint8Array | null} */
|
|
|
|
let reqBody = null;
|
2023-12-01 10:56:10 -05:00
|
|
|
let reqRid = null;
|
|
|
|
|
|
|
|
if (req.body) {
|
|
|
|
const stream = req.body.streamOrStatic;
|
|
|
|
const body = stream.body;
|
|
|
|
|
2024-01-03 23:12:38 -05:00
|
|
|
if (TypedArrayPrototypeGetSymbolToStringTag(body) === "Uint8Array") {
|
2023-12-01 10:56:10 -05:00
|
|
|
reqBody = body;
|
|
|
|
} else if (typeof body === "string") {
|
|
|
|
reqBody = core.encode(body);
|
|
|
|
} else if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, stream)) {
|
|
|
|
const resourceBacking = getReadableStreamResourceBacking(stream);
|
|
|
|
if (resourceBacking) {
|
|
|
|
reqRid = resourceBacking.rid;
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2023-12-01 10:56:10 -05:00
|
|
|
reqRid = resourceForReadableStream(stream, req.body.length);
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2023-12-01 10:56:10 -05:00
|
|
|
throw TypeError("invalid body");
|
2021-04-20 08:47:22 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-04-20 08:47:22 -04:00
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
const { requestRid, cancelHandleRid } = op_fetch(
|
2023-02-07 14:22:46 -05:00
|
|
|
req.method,
|
|
|
|
req.currentUrl(),
|
|
|
|
req.headerList,
|
|
|
|
req.clientRid,
|
2023-12-01 10:56:10 -05:00
|
|
|
reqBody !== null || reqRid !== null,
|
|
|
|
reqBody,
|
|
|
|
reqRid,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
function onAbort() {
|
|
|
|
if (cancelHandleRid !== null) {
|
|
|
|
core.tryClose(cancelHandleRid);
|
2021-06-06 09:37:17 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
terminator[abortSignal.add](onAbort);
|
|
|
|
let resp;
|
|
|
|
try {
|
|
|
|
resp = await opFetchSend(requestRid);
|
|
|
|
} catch (err) {
|
2024-03-24 13:49:01 -04:00
|
|
|
if (terminator.aborted) return abortedNetworkError();
|
2023-02-07 14:22:46 -05:00
|
|
|
throw err;
|
|
|
|
} finally {
|
|
|
|
if (cancelHandleRid !== null) {
|
|
|
|
core.tryClose(cancelHandleRid);
|
|
|
|
}
|
|
|
|
}
|
2023-12-01 10:56:10 -05:00
|
|
|
// Re-throw any body errors
|
|
|
|
if (resp.error) {
|
|
|
|
throw new TypeError("body failed", { cause: new Error(resp.error) });
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (terminator.aborted) return abortedNetworkError();
|
|
|
|
|
|
|
|
processUrlList(req.urlList, req.urlListProcessed);
|
|
|
|
|
|
|
|
/** @type {InnerResponse} */
|
|
|
|
const response = {
|
|
|
|
headerList: resp.headers,
|
|
|
|
status: resp.status,
|
|
|
|
body: null,
|
|
|
|
statusMessage: resp.statusText,
|
|
|
|
type: "basic",
|
|
|
|
url() {
|
|
|
|
if (this.urlList.length == 0) return null;
|
|
|
|
return this.urlList[this.urlList.length - 1];
|
|
|
|
},
|
|
|
|
urlList: req.urlListProcessed,
|
|
|
|
};
|
|
|
|
if (redirectStatus(resp.status)) {
|
|
|
|
switch (req.redirectMode) {
|
|
|
|
case "error":
|
|
|
|
core.close(resp.responseRid);
|
|
|
|
return networkError(
|
|
|
|
"Encountered redirect while redirect mode is set to 'error'",
|
|
|
|
);
|
|
|
|
case "follow":
|
|
|
|
core.close(resp.responseRid);
|
|
|
|
return httpRedirectFetch(req, response, terminator);
|
|
|
|
case "manual":
|
|
|
|
break;
|
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
|
|
|
if (nullBodyStatus(response.status)) {
|
|
|
|
core.close(resp.responseRid);
|
|
|
|
} else {
|
|
|
|
if (req.method === "HEAD" || req.method === "CONNECT") {
|
|
|
|
response.body = null;
|
2021-04-20 08:47:22 -04:00
|
|
|
core.close(resp.responseRid);
|
|
|
|
} else {
|
2023-02-07 14:22:46 -05:00
|
|
|
response.body = new InnerBody(
|
|
|
|
createResponseBodyStream(resp.responseRid, terminator),
|
|
|
|
);
|
2020-09-18 09:20:55 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (recursive) return response;
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (response.urlList.length === 0) {
|
|
|
|
processUrlList(req.urlList, req.urlListProcessed);
|
|
|
|
response.urlList = [...new SafeArrayIterator(req.urlListProcessed)];
|
|
|
|
}
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {InnerRequest} request
|
|
|
|
* @param {InnerResponse} response
|
|
|
|
* @param {AbortSignal} terminator
|
|
|
|
* @returns {Promise<InnerResponse>}
|
|
|
|
*/
|
|
|
|
function httpRedirectFetch(request, response, terminator) {
|
|
|
|
const locationHeaders = ArrayPrototypeFilter(
|
|
|
|
response.headerList,
|
|
|
|
(entry) => byteLowerCase(entry[0]) === "location",
|
|
|
|
);
|
|
|
|
if (locationHeaders.length === 0) {
|
2021-04-20 08:47:22 -04:00
|
|
|
return response;
|
2021-01-07 13:06:08 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const locationURL = new URL(
|
|
|
|
locationHeaders[0][1],
|
|
|
|
response.url() ?? undefined,
|
|
|
|
);
|
|
|
|
if (locationURL.hash === "") {
|
|
|
|
locationURL.hash = request.currentUrl().hash;
|
|
|
|
}
|
|
|
|
if (locationURL.protocol !== "https:" && locationURL.protocol !== "http:") {
|
|
|
|
return networkError("Can not redirect to a non HTTP(s) url");
|
|
|
|
}
|
|
|
|
if (request.redirectCount === 20) {
|
|
|
|
return networkError("Maximum number of redirects (20) reached");
|
|
|
|
}
|
|
|
|
request.redirectCount++;
|
|
|
|
if (
|
|
|
|
response.status !== 303 &&
|
|
|
|
request.body !== null &&
|
|
|
|
request.body.source === null
|
|
|
|
) {
|
|
|
|
return networkError(
|
|
|
|
"Can not redeliver a streaming request body after a redirect",
|
2021-01-10 14:54:29 -05:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
((response.status === 301 || response.status === 302) &&
|
|
|
|
request.method === "POST") ||
|
|
|
|
(response.status === 303 &&
|
|
|
|
request.method !== "GET" &&
|
|
|
|
request.method !== "HEAD")
|
|
|
|
) {
|
|
|
|
request.method = "GET";
|
|
|
|
request.body = null;
|
|
|
|
for (let i = 0; i < request.headerList.length; i++) {
|
|
|
|
if (
|
|
|
|
ArrayPrototypeIncludes(
|
|
|
|
REQUEST_BODY_HEADER_NAMES,
|
|
|
|
byteLowerCase(request.headerList[i][0]),
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
ArrayPrototypeSplice(request.headerList, i, 1);
|
|
|
|
i--;
|
2021-01-28 15:37:21 -05:00
|
|
|
}
|
2021-01-10 14:54:29 -05:00
|
|
|
}
|
2020-09-18 09:20:55 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (request.body !== null) {
|
|
|
|
const res = extractBody(request.body.source);
|
|
|
|
request.body = res.body;
|
|
|
|
}
|
|
|
|
ArrayPrototypePush(request.urlList, () => locationURL.href);
|
|
|
|
return mainFetch(request, true, terminator);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {RequestInfo} input
|
|
|
|
* @param {RequestInit} init
|
|
|
|
*/
|
|
|
|
function fetch(input, init = {}) {
|
|
|
|
// There is an async dispatch later that causes a stack trace disconnect.
|
|
|
|
// We reconnect it by assigning the result of that dispatch to `opPromise`,
|
|
|
|
// awaiting `opPromise` in an inner function also named `fetch()` and
|
|
|
|
// returning the result from that.
|
|
|
|
let opPromise = undefined;
|
|
|
|
// 1.
|
|
|
|
const result = new Promise((resolve, reject) => {
|
2024-03-09 21:23:14 -05:00
|
|
|
const prefix = "Failed to execute 'fetch'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.
|
|
|
|
const requestObject = new Request(input, init);
|
|
|
|
// 3.
|
|
|
|
const request = toInnerRequest(requestObject);
|
|
|
|
// 4.
|
|
|
|
if (requestObject.signal.aborted) {
|
|
|
|
reject(abortFetch(request, null, requestObject.signal.reason));
|
|
|
|
return;
|
|
|
|
}
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 7.
|
|
|
|
let responseObject = null;
|
|
|
|
// 9.
|
|
|
|
let locallyAborted = false;
|
|
|
|
// 10.
|
|
|
|
function onabort() {
|
|
|
|
locallyAborted = true;
|
|
|
|
reject(
|
|
|
|
abortFetch(request, responseObject, requestObject.signal.reason),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
requestObject.signal[abortSignal.add](onabort);
|
2021-06-06 09:37:17 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!requestObject.headers.has("Accept")) {
|
|
|
|
ArrayPrototypePush(request.headerList, ["Accept", "*/*"]);
|
|
|
|
}
|
2021-06-06 09:37:17 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!requestObject.headers.has("Accept-Language")) {
|
|
|
|
ArrayPrototypePush(request.headerList, ["Accept-Language", "*"]);
|
|
|
|
}
|
2022-06-19 17:33:24 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 12.
|
|
|
|
opPromise = PromisePrototypeCatch(
|
|
|
|
PromisePrototypeThen(
|
|
|
|
mainFetch(request, false, requestObject.signal),
|
|
|
|
(response) => {
|
|
|
|
// 12.1.
|
|
|
|
if (locallyAborted) return;
|
|
|
|
// 12.2.
|
|
|
|
if (response.aborted) {
|
|
|
|
reject(
|
|
|
|
abortFetch(
|
|
|
|
request,
|
|
|
|
responseObject,
|
|
|
|
requestObject.signal.reason,
|
|
|
|
),
|
|
|
|
);
|
2021-07-06 05:32:59 -04:00
|
|
|
requestObject.signal[abortSignal.remove](onabort);
|
2023-02-07 14:22:46 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 12.3.
|
|
|
|
if (response.type === "error") {
|
|
|
|
const err = new TypeError(
|
|
|
|
"Fetch failed: " + (response.error ?? "unknown error"),
|
|
|
|
);
|
|
|
|
reject(err);
|
|
|
|
requestObject.signal[abortSignal.remove](onabort);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
responseObject = fromInnerResponse(response, "immutable");
|
|
|
|
resolve(responseObject);
|
2021-06-06 09:37:17 -04:00
|
|
|
requestObject.signal[abortSignal.remove](onabort);
|
2021-07-06 05:32:59 -04:00
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
),
|
|
|
|
(err) => {
|
|
|
|
reject(err);
|
|
|
|
requestObject.signal[abortSignal.remove](onabort);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
if (opPromise) {
|
|
|
|
PromisePrototypeCatch(result, () => {});
|
|
|
|
return (async function fetch() {
|
|
|
|
await opPromise;
|
|
|
|
return result;
|
|
|
|
})();
|
2021-06-06 09:37:17 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return result;
|
|
|
|
}
|
2021-06-06 09:37:17 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function abortFetch(request, responseObject, error) {
|
|
|
|
if (request.body !== null) {
|
2023-12-01 10:56:10 -05:00
|
|
|
// Cancel the body if we haven't taken it as a resource yet
|
|
|
|
if (!request.body.streamOrStatic.locked) {
|
2023-02-07 14:22:46 -05:00
|
|
|
request.body.cancel(error);
|
2021-06-06 09:37:17 -04:00
|
|
|
}
|
2020-09-18 09:20:55 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (responseObject !== null) {
|
|
|
|
const response = toInnerResponse(responseObject);
|
|
|
|
if (response.body !== null) response.body.error(error);
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the Response argument to the WebAssembly streaming APIs, after
|
|
|
|
* resolving if it was passed as a promise. This function should be registered
|
|
|
|
* through `Deno.core.setWasmStreamingCallback`.
|
|
|
|
*
|
|
|
|
* @param {any} source The source parameter that the WebAssembly streaming API
|
|
|
|
* was called with. If it was called with a Promise, `source` is the resolved
|
|
|
|
* value of that promise.
|
|
|
|
* @param {number} rid An rid that represents the wasm streaming resource.
|
|
|
|
*/
|
|
|
|
function handleWasmStreaming(source, rid) {
|
|
|
|
// This implements part of
|
|
|
|
// https://webassembly.github.io/spec/web-api/#compile-a-potential-webassembly-response
|
|
|
|
try {
|
2023-05-01 06:47:13 -04:00
|
|
|
const res = webidl.converters["Response"](
|
|
|
|
source,
|
2024-03-09 21:23:14 -05:00
|
|
|
"Failed to execute 'WebAssembly.compileStreaming'",
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 1",
|
|
|
|
);
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.3.
|
|
|
|
// The spec is ambiguous here, see
|
|
|
|
// https://github.com/WebAssembly/spec/issues/1138. The WPT tests expect
|
|
|
|
// the raw value of the Content-Type attribute lowercased. We ignore this
|
|
|
|
// for file:// because file fetches don't have a Content-Type.
|
|
|
|
if (!StringPrototypeStartsWith(res.url, "file://")) {
|
|
|
|
const contentType = res.headers.get("Content-Type");
|
|
|
|
if (
|
|
|
|
typeof contentType !== "string" ||
|
|
|
|
StringPrototypeToLowerCase(contentType) !== "application/wasm"
|
|
|
|
) {
|
|
|
|
throw new TypeError("Invalid WebAssembly content type.");
|
2022-03-29 08:44:33 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-07-03 17:33:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// 2.5.
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new TypeError(`HTTP status code ${res.status}`);
|
|
|
|
}
|
2022-03-29 08:44:33 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// Pass the resolved URL to v8.
|
2024-01-10 17:37:25 -05:00
|
|
|
op_wasm_streaming_set_url(rid, res.url);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
if (res.body !== null) {
|
|
|
|
// 2.6.
|
|
|
|
// Rather than consuming the body as an ArrayBuffer, this passes each
|
|
|
|
// chunk to the feed as soon as it's available.
|
|
|
|
PromisePrototypeThen(
|
|
|
|
(async () => {
|
|
|
|
const reader = res.body.getReader();
|
|
|
|
while (true) {
|
|
|
|
const { value: chunk, done } = await reader.read();
|
|
|
|
if (done) break;
|
2024-01-10 17:37:25 -05:00
|
|
|
op_wasm_streaming_feed(rid, chunk);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
})(),
|
2022-03-29 08:44:33 -04:00
|
|
|
// 2.7
|
2023-02-07 14:22:46 -05:00
|
|
|
() => core.close(rid),
|
|
|
|
// 2.8
|
|
|
|
(err) => core.abortWasmStreaming(rid, err),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// 2.7
|
|
|
|
core.close(rid);
|
2022-03-29 08:44:33 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} catch (err) {
|
|
|
|
// 2.8
|
|
|
|
core.abortWasmStreaming(rid, err);
|
2021-07-03 17:33:36 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-07-03 17:33:36 -04:00
|
|
|
|
2023-10-31 13:16:27 -04:00
|
|
|
export { fetch, handleWasmStreaming, mainFetch };
|