2023-05-23 18:53:53 +00:00
|
|
|
import type {
|
|
|
|
ProxyFetchRequest,
|
|
|
|
ProxyFetchResponse,
|
|
|
|
} from "../../common/proxy_fetch.ts";
|
2023-01-08 11:24:12 +00:00
|
|
|
import { base64Decode } from "../../plugos/asset_bundle/base64.ts";
|
|
|
|
|
|
|
|
export function sandboxFetch(
|
|
|
|
url: string,
|
2023-05-23 18:53:53 +00:00
|
|
|
options?: ProxyFetchRequest,
|
|
|
|
): Promise<ProxyFetchResponse> {
|
2023-01-08 11:24:12 +00:00
|
|
|
// @ts-ignore: monkey patching fetch
|
|
|
|
return syscall("sandboxFetch.fetch", url, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function monkeyPatchFetch() {
|
2023-07-02 09:25:32 +00:00
|
|
|
// @ts-ignore: monkey patching fetch
|
|
|
|
globalThis.nativeFetch = globalThis.fetch;
|
2023-01-08 11:24:12 +00:00
|
|
|
// @ts-ignore: monkey patching fetch
|
|
|
|
globalThis.fetch = async function (
|
|
|
|
url: string,
|
|
|
|
init?: RequestInit,
|
|
|
|
): Promise<Response> {
|
|
|
|
const r = await sandboxFetch(
|
|
|
|
url,
|
|
|
|
init && {
|
|
|
|
method: init.method,
|
|
|
|
headers: init.headers as Record<string, string>,
|
2023-01-08 20:01:48 +00:00
|
|
|
body: init.body as string,
|
2023-01-08 11:24:12 +00:00
|
|
|
},
|
|
|
|
);
|
2023-01-08 21:38:54 +00:00
|
|
|
return new Response(r.base64Body ? base64Decode(r.base64Body) : null, {
|
2023-01-08 11:24:12 +00:00
|
|
|
status: r.status,
|
|
|
|
headers: r.headers,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|