-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support setting network permissions via allowNet option (#386)
* feat: support setting network permissions via allowNet option * stamp: format * chore: add integration tests for `allowNet` option * chore: update `Cargo.lock` --------- Co-authored-by: Nyannyacha <[email protected]>
- Loading branch information
1 parent
4ff108f
commit 4a99525
Showing
11 changed files
with
347 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Deno.serve(async (req: Request) => { | ||
try { | ||
const payload = await req.json(); | ||
const url = new URL(payload["url"]); | ||
const resp = await fetch(url); | ||
const body = await resp.text(); | ||
|
||
return Response.json({ | ||
status: resp.status, | ||
body | ||
}); | ||
} catch (e) { | ||
return new Response(e.toString(), { status: 500 }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
console.log('main function started'); | ||
|
||
Deno.serve({ | ||
handler: async (req: Request) => { | ||
console.log(req.url); | ||
const url = new URL(req.url); | ||
const { pathname } = url; | ||
const path_parts = pathname.split("/"); | ||
const service_name = path_parts[1]; | ||
|
||
if (!service_name || service_name === "") { | ||
const error = { msg: "missing function name in request" } | ||
return new Response( | ||
JSON.stringify(error), | ||
{ status: 400, headers: { "Content-Type": "application/json" } }, | ||
) | ||
} | ||
|
||
const servicePath = `./test_cases/${service_name}`; | ||
|
||
let allowNet: string[] | null | undefined; | ||
|
||
try { | ||
const payload = await req.clone().json(); | ||
allowNet = payload.allowNet; | ||
} catch { } | ||
|
||
console.error(`serving the request with ${servicePath}`); | ||
|
||
const createWorker = async () => { | ||
const memoryLimitMb = 150; | ||
const workerTimeoutMs = 10 * 60 * 1000; | ||
const cpuTimeSoftLimitMs = 10 * 60 * 1000; | ||
const cpuTimeHardLimitMs = 10 * 60 * 1000; | ||
const noModuleCache = false; | ||
const importMapPath = null; | ||
const envVarsObj = Deno.env.toObject(); | ||
const envVars = Object.keys(envVarsObj).map(k => [k, envVarsObj[k]]); | ||
|
||
return await EdgeRuntime.userWorkers.create({ | ||
servicePath, | ||
memoryLimitMb, | ||
workerTimeoutMs, | ||
cpuTimeSoftLimitMs, | ||
cpuTimeHardLimitMs, | ||
noModuleCache, | ||
importMapPath, | ||
envVars, | ||
allowNet, | ||
}); | ||
} | ||
|
||
const callWorker = async () => { | ||
try { | ||
const worker = await createWorker(); | ||
return await worker.fetch(req); | ||
} catch (e) { | ||
console.error(e); | ||
|
||
// if (e instanceof Deno.errors.WorkerRequestCancelled) { | ||
// return await callWorker(); | ||
// } | ||
|
||
const error = { msg: e.toString() } | ||
return new Response( | ||
JSON.stringify(error), | ||
{ status: 500, headers: { "Content-Type": "application/json" } }, | ||
); | ||
} | ||
} | ||
|
||
return callWorker(); | ||
}, | ||
|
||
onError: e => new Response(e.toString(), { status: 500 }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.