2023-01-08 11:24:12 +00:00
|
|
|
import type { SysCallMapping } from "../../plugos/system.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
import {
|
|
|
|
performLocalFetch,
|
|
|
|
ProxyFetchRequest,
|
|
|
|
ProxyFetchResponse,
|
|
|
|
} from "../../common/proxy_fetch.ts";
|
2023-07-24 07:36:33 +00:00
|
|
|
import type { Client } from "../client.ts";
|
2023-01-08 11:24:12 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
export function sandboxFetchSyscalls(
|
2023-07-24 07:36:33 +00:00
|
|
|
client: Client,
|
2023-05-23 18:53:53 +00:00
|
|
|
): 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) {
|
2023-05-23 18:53:53 +00:00
|
|
|
// 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`,
|
2023-05-23 18:53:53 +00:00
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({
|
|
|
|
operation: "fetch",
|
|
|
|
url,
|
|
|
|
options,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return (await resp).json();
|
|
|
|
},
|
|
|
|
};
|
2023-01-08 11:24:12 +00:00
|
|
|
}
|