-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcontext.ts
More file actions
66 lines (60 loc) · 1.45 KB
/
context.ts
File metadata and controls
66 lines (60 loc) · 1.45 KB
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
import vm from 'node:vm';
import type { State } from '../types';
import type { Options } from '../runtime';
const freezeAll = (
obj: Record<string, any>,
exclude: Record<string, true> = {}
) => {
const copy: typeof obj = {};
for (const key in obj) {
copy[key] = exclude[key] ? obj[key] : Object.freeze(obj[key]);
}
return copy;
};
// Build a safe and helpful execution context
// This will be shared by all jobs
export default (state: State, options: Pick<Options, 'jobLogger' | 'globals'>) => {
const logger = options.jobLogger ?? console;
const globals = options.globals || {};
const context = vm.createContext(
freezeAll(
{
...globals,
// Note that these globals will be overridden
console: logger,
clearInterval,
clearTimeout,
parseFloat,
parseInt,
setInterval,
setTimeout,
state, // TODO will be dropped as a global one day, see https://github.com/OpenFn/kit/issues/17
},
{ state: true }
),
{
codeGeneration: {
strings: false,
wasm: false,
},
}
);
return context;
};
// Special, highly restricted cotext for a plan condition
// Ie, a javascript expression
export const conditionContext = () => {
const context = vm.createContext(
{
console,
},
{
codeGeneration: {
strings: false,
wasm: false,
},
}
);
return context;
};
export type Context = vm.Context;