1
0
silverbullet/web/syscalls/fetch.ts

38 lines
1.0 KiB
TypeScript
Raw Normal View History

import type { SysCallMapping } from "../../plugos/system.ts";
import {
performLocalFetch,
ProxyFetchRequest,
ProxyFetchResponse,
} from "../../common/proxy_fetch.ts";
2023-07-24 07:36:33 +00:00
import type { Client } from "../client.ts";
export function sandboxFetchSyscalls(
2023-07-24 07:36:33 +00:00
client: Client,
): SysCallMapping {
return {
"sandboxFetch.fetch": async (
_ctx,
url: string,
options: ProxyFetchRequest,
): Promise<ProxyFetchResponse> => {
// console.log("Got sandbox fetch ", url);
2023-07-24 07:36:33 +00:00
if (!client.remoteSpacePrimitives) {
// No SB server to proxy the fetch available so let's execute the request directly
return performLocalFetch(url, options);
}
2023-07-24 07:36:33 +00:00
const resp = client.remoteSpacePrimitives.authenticatedFetch(
`${client.remoteSpacePrimitives.url}/.rpc`,
{
method: "POST",
body: JSON.stringify({
operation: "fetch",
url,
options,
}),
},
);
return (await resp).json();
},
};
}