7c825348b2
* Query regen * Fix anchor completion * Dependency fixes * Changelog update
37 lines
972 B
TypeScript
37 lines
972 B
TypeScript
import type {
|
|
ProxyFetchRequest,
|
|
ProxyFetchResponse,
|
|
} from "../../common/proxy_fetch.ts";
|
|
import { base64Decode } from "../../plugos/asset_bundle/base64.ts";
|
|
|
|
export function sandboxFetch(
|
|
url: string,
|
|
options?: ProxyFetchRequest,
|
|
): Promise<ProxyFetchResponse> {
|
|
// @ts-ignore: monkey patching fetch
|
|
return syscall("sandboxFetch.fetch", url, options);
|
|
}
|
|
|
|
export function monkeyPatchFetch() {
|
|
// @ts-ignore: monkey patching fetch
|
|
globalThis.nativeFetch = globalThis.fetch;
|
|
// @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>,
|
|
body: init.body as string,
|
|
},
|
|
);
|
|
return new Response(r.base64Body ? base64Decode(r.base64Body) : null, {
|
|
status: r.status,
|
|
headers: r.headers,
|
|
});
|
|
};
|
|
}
|