-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (69 loc) · 2.05 KB
/
index.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const ivm = require('isolated-vm');
exports.handler = async function (event) {
try {
const reqBody = JSON.parse(event.body);
const binaryData = new Uint8Array(reqBody.code);
const decoder = new TextDecoder('utf-8');
const code = decoder.decode(binaryData);
const capturedLogs = [];
const isolate = new ivm.Isolate(
{ memoryLimit: 128 * 1024 * 1024 },
{ cpuTimeLimit: 10000 },
);
const context = await isolate.createContext();
context.evalClosureSync(
`
globalThis.console = {
log: $0
}
`,
[
(...args) =>
capturedLogs.push(...args.map((arg) => JSON.stringify(arg))),
],
);
const script = isolate.compileScriptSync(code);
const returned = script.runSync(context);
return {
statusCode: 200,
body: JSON.stringify({ returned: returned, logged: capturedLogs }),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ message: error.message }),
event,
};
}
};
const event =
'{"code":{"0":99,"1":111,"2":110,"3":115,"4":111,"5":108,"6":101,"7":46,"8":108,"9":111,"10":103,"11":40,"12":39,"13":104,"14":101,"15":108,"16":108,"17":111,"18":32,"19":119,"20":111,"21":114,"22":108,"23":100,"24":39,"25":41,"26":59}}';
const foo = async () => {
const reqBody = JSON.parse(event);
const binaryData = new Uint8Array(reqBody.code);
const decoder = new TextDecoder('utf-8');
const code = decoder.decode(binaryData);
console.log(code, reqBody);
const capturedLogs = [];
const isolate = new ivm.Isolate(
{ memoryLimit: 128 * 1024 * 1024 },
{ cpuTimeLimit: 10000 },
);
const context = await isolate.createContext();
context.evalClosureSync(
`
globalThis.console = {
log: $0
}
`,
[(...args) => capturedLogs.push(...args.map((arg) => JSON.stringify(arg)))],
);
const script = isolate.compileScriptSync(code);
const returned = script.runSync(context);
console.log(returned, capturedLogs);
return {
statusCode: 200,
body: JSON.stringify({ returned: returned, logged: capturedLogs }),
};
};
foo();