-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsandbox.ts
More file actions
34 lines (30 loc) · 934 Bytes
/
sandbox.ts
File metadata and controls
34 lines (30 loc) · 934 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const scriptId = Deno.args[0] /* `scriptId=b` */.split("=")[1];
const scriptPath = Deno.args[1] /* `scriptPath=/a/b.ts` */.split("=")[1];
const scriptProxy = "http://localhost:3001/proxy";
const scriptAuthHeaders = {
"x-script-id": scriptId,
};
const realFetch = fetch;
// User fetch (all requests pass through, and are limited by, the proxy)
// n.b. there's no way around this – all other hosts are locked down via `--allow-net`
// e.g. `realFetch("https://bing.com")` will cause the sandbox to crash with `PermissionDenied`
globalThis.fetch = (
input: string | Request | URL,
init?: RequestInit | undefined,
) => {
if (init === undefined) {
init = {};
}
init.headers = {
...init.headers,
...scriptAuthHeaders,
"x-script-fetch": input.toString(),
};
return realFetch(scriptProxy, init);
};
try {
// Here's where we run user code
await import(scriptPath);
} catch (e) {
console.error(e);
}