1
0
silverbullet/web/syscalls/fetch.ts
Zef Hemel fb75ea1a65
No More Collab. Fixes #449
* Fully removes real-time collaboration
* URL scheme rewrite
2023-07-06 16:47:50 +02:00

38 lines
1.0 KiB
TypeScript

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