@cuxt/sandboxjs 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +186 -0
- package/build/Sandbox.d.ts +25 -0
- package/build/Sandbox.js +62 -0
- package/build/SandboxExec.d.ts +66 -0
- package/build/SandboxExec.js +214 -0
- package/build/eval.d.ts +27 -0
- package/build/eval.js +205 -0
- package/build/executor.d.ts +124 -0
- package/build/executor.js +1546 -0
- package/build/parser.d.ts +154 -0
- package/build/parser.js +1527 -0
- package/build/unraw.d.ts +11 -0
- package/build/unraw.js +168 -0
- package/build/utils.d.ts +264 -0
- package/build/utils.js +362 -0
- package/dist/Sandbox.d.ts +25 -0
- package/dist/Sandbox.js +270 -0
- package/dist/Sandbox.js.map +1 -0
- package/dist/Sandbox.min.js +2 -0
- package/dist/Sandbox.min.js.map +1 -0
- package/dist/SandboxExec.d.ts +66 -0
- package/dist/SandboxExec.js +218 -0
- package/dist/SandboxExec.js.map +1 -0
- package/dist/SandboxExec.min.js +2 -0
- package/dist/SandboxExec.min.js.map +1 -0
- package/dist/eval.d.ts +27 -0
- package/dist/executor.d.ts +124 -0
- package/dist/executor.js +1550 -0
- package/dist/executor.js.map +1 -0
- package/dist/node/Sandbox.d.ts +25 -0
- package/dist/node/Sandbox.js +277 -0
- package/dist/node/SandboxExec.d.ts +66 -0
- package/dist/node/SandboxExec.js +225 -0
- package/dist/node/eval.d.ts +27 -0
- package/dist/node/executor.d.ts +124 -0
- package/dist/node/executor.js +1567 -0
- package/dist/node/parser.d.ts +154 -0
- package/dist/node/parser.js +1704 -0
- package/dist/node/unraw.d.ts +11 -0
- package/dist/node/utils.d.ts +264 -0
- package/dist/node/utils.js +385 -0
- package/dist/parser.d.ts +154 -0
- package/dist/parser.js +1690 -0
- package/dist/parser.js.map +1 -0
- package/dist/unraw.d.ts +11 -0
- package/dist/utils.d.ts +264 -0
- package/dist/utils.js +365 -0
- package/dist/utils.js.map +1 -0
- package/package.json +68 -0
package/build/eval.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { createFunction, createFunctionAsync } from './executor.js';
|
|
2
|
+
import parse, { lispifyFunction } from './parser.js';
|
|
3
|
+
export function createEvalContext() {
|
|
4
|
+
return {
|
|
5
|
+
sandboxFunction,
|
|
6
|
+
sandboxAsyncFunction,
|
|
7
|
+
sandboxedEval,
|
|
8
|
+
sandboxedSetTimeout,
|
|
9
|
+
sandboxedSetInterval,
|
|
10
|
+
sandboxedClearTimeout,
|
|
11
|
+
sandboxedClearInterval,
|
|
12
|
+
lispifyFunction,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function SB() { }
|
|
16
|
+
export function sandboxFunction(context) {
|
|
17
|
+
SandboxFunction.prototype = SB.prototype;
|
|
18
|
+
return SandboxFunction;
|
|
19
|
+
function SandboxFunction(...params) {
|
|
20
|
+
const code = params.pop() || '';
|
|
21
|
+
const parsed = parse(code);
|
|
22
|
+
return createFunction(params, parsed.tree, context.ctx.ticks, {
|
|
23
|
+
...context,
|
|
24
|
+
constants: parsed.constants,
|
|
25
|
+
tree: parsed.tree,
|
|
26
|
+
}, undefined, 'anonymous');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function SAF() { }
|
|
30
|
+
export function sandboxAsyncFunction(context) {
|
|
31
|
+
SandboxAsyncFunction.prototype = SAF.prototype;
|
|
32
|
+
return SandboxAsyncFunction;
|
|
33
|
+
function SandboxAsyncFunction(...params) {
|
|
34
|
+
const code = params.pop() || '';
|
|
35
|
+
const parsed = parse(code);
|
|
36
|
+
return createFunctionAsync(params, parsed.tree, context.ctx.ticks, {
|
|
37
|
+
...context,
|
|
38
|
+
constants: parsed.constants,
|
|
39
|
+
tree: parsed.tree,
|
|
40
|
+
}, undefined, 'anonymous');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function SE() { }
|
|
44
|
+
export function sandboxedEval(func, context) {
|
|
45
|
+
sandboxEval.prototype = SE.prototype;
|
|
46
|
+
return sandboxEval;
|
|
47
|
+
function sandboxEval(code) {
|
|
48
|
+
// Parse the code and wrap last statement in return for completion value
|
|
49
|
+
const parsed = parse(code);
|
|
50
|
+
const tree = wrapLastStatementInReturn(parsed.tree);
|
|
51
|
+
// Create and execute function with modified tree
|
|
52
|
+
return createFunction([], tree, context.ctx.ticks, {
|
|
53
|
+
...context,
|
|
54
|
+
constants: parsed.constants,
|
|
55
|
+
tree,
|
|
56
|
+
}, undefined, 'anonymous')();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function wrapLastStatementInReturn(tree) {
|
|
60
|
+
if (tree.length === 0)
|
|
61
|
+
return tree;
|
|
62
|
+
const newTree = [...tree];
|
|
63
|
+
const lastIndex = newTree.length - 1;
|
|
64
|
+
const lastStmt = newTree[lastIndex];
|
|
65
|
+
// Only wrap if it's not already a return or throw
|
|
66
|
+
if (Array.isArray(lastStmt) && lastStmt.length >= 1) {
|
|
67
|
+
const op = lastStmt[0];
|
|
68
|
+
// Don't wrap Return (8) or Throw (47) - they already control flow
|
|
69
|
+
if (op === 8 /* LispType.Return */ || op === 46 /* LispType.Throw */) {
|
|
70
|
+
return newTree;
|
|
71
|
+
}
|
|
72
|
+
// List of statement types that should have undefined completion value
|
|
73
|
+
// These match JavaScript semantics where declarations and control structures
|
|
74
|
+
// don't produce a completion value
|
|
75
|
+
const statementTypes = [
|
|
76
|
+
3 /* LispType.Let */, // 3
|
|
77
|
+
4 /* LispType.Const */, // 4
|
|
78
|
+
34 /* LispType.Var */, // 35
|
|
79
|
+
37 /* LispType.Function */, // 38
|
|
80
|
+
13 /* LispType.If */, // 14
|
|
81
|
+
38 /* LispType.Loop */, // 39
|
|
82
|
+
39 /* LispType.Try */, // 40
|
|
83
|
+
40 /* LispType.Switch */, // 41
|
|
84
|
+
42 /* LispType.Block */, // 43
|
|
85
|
+
43 /* LispType.Expression */, // 44
|
|
86
|
+
];
|
|
87
|
+
// If the last statement is a declaration or control structure,
|
|
88
|
+
// don't wrap it (it will naturally return undefined)
|
|
89
|
+
if (statementTypes.includes(op)) {
|
|
90
|
+
return newTree;
|
|
91
|
+
}
|
|
92
|
+
// For all other types (expressions, operators, etc.),
|
|
93
|
+
// wrap in return to capture the completion value
|
|
94
|
+
newTree[lastIndex] = [8 /* LispType.Return */, 0 /* LispType.None */, lastStmt];
|
|
95
|
+
}
|
|
96
|
+
return newTree;
|
|
97
|
+
}
|
|
98
|
+
function sST() { }
|
|
99
|
+
export function sandboxedSetTimeout(func, context) {
|
|
100
|
+
sandboxSetTimeout.prototype = sST.prototype;
|
|
101
|
+
return sandboxSetTimeout;
|
|
102
|
+
function sandboxSetTimeout(handler, timeout, ...args) {
|
|
103
|
+
const sandbox = context.ctx.sandbox;
|
|
104
|
+
const exec = (...a) => {
|
|
105
|
+
const h = typeof handler === 'string' ? func(handler) : handler;
|
|
106
|
+
haltsub.unsubscribe();
|
|
107
|
+
contsub.unsubscribe();
|
|
108
|
+
sandbox.setTimeoutHandles.delete(sandBoxhandle);
|
|
109
|
+
return h(...a);
|
|
110
|
+
};
|
|
111
|
+
const sandBoxhandle = ++sandbox.timeoutHandleCounter;
|
|
112
|
+
let start = Date.now();
|
|
113
|
+
let handle = setTimeout(exec, timeout, ...args);
|
|
114
|
+
let elapsed = 0;
|
|
115
|
+
const haltsub = sandbox.subscribeHalt(() => {
|
|
116
|
+
elapsed = Date.now() - start + elapsed;
|
|
117
|
+
clearTimeout(handle);
|
|
118
|
+
});
|
|
119
|
+
const contsub = sandbox.subscribeResume(() => {
|
|
120
|
+
start = Date.now();
|
|
121
|
+
const remaining = Math.floor((timeout || 0) - elapsed);
|
|
122
|
+
handle = setTimeout(exec, remaining, ...args);
|
|
123
|
+
sandbox.setTimeoutHandles.set(sandBoxhandle, {
|
|
124
|
+
handle,
|
|
125
|
+
haltsub,
|
|
126
|
+
contsub,
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
sandbox.setTimeoutHandles.set(sandBoxhandle, {
|
|
130
|
+
handle,
|
|
131
|
+
haltsub,
|
|
132
|
+
contsub,
|
|
133
|
+
});
|
|
134
|
+
return sandBoxhandle;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function sCT() { }
|
|
138
|
+
export function sandboxedClearTimeout(context) {
|
|
139
|
+
sandboxClearTimeout.prototype = sCT.prototype;
|
|
140
|
+
return sandboxClearTimeout;
|
|
141
|
+
function sandboxClearTimeout(handle) {
|
|
142
|
+
const sandbox = context.ctx.sandbox;
|
|
143
|
+
const timeoutHandle = sandbox.setTimeoutHandles.get(handle);
|
|
144
|
+
if (timeoutHandle) {
|
|
145
|
+
clearTimeout(timeoutHandle.handle);
|
|
146
|
+
timeoutHandle.haltsub.unsubscribe();
|
|
147
|
+
timeoutHandle.contsub.unsubscribe();
|
|
148
|
+
sandbox.setTimeoutHandles.delete(handle);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function sCI() { }
|
|
153
|
+
export function sandboxedClearInterval(context) {
|
|
154
|
+
sandboxClearInterval.prototype = sCI.prototype;
|
|
155
|
+
return sandboxClearInterval;
|
|
156
|
+
function sandboxClearInterval(handle) {
|
|
157
|
+
const sandbox = context.ctx.sandbox;
|
|
158
|
+
const intervalHandle = sandbox.setIntervalHandles.get(handle);
|
|
159
|
+
if (intervalHandle) {
|
|
160
|
+
clearInterval(intervalHandle.handle);
|
|
161
|
+
intervalHandle.haltsub.unsubscribe();
|
|
162
|
+
intervalHandle.contsub.unsubscribe();
|
|
163
|
+
sandbox.setIntervalHandles.delete(handle);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
function sSI() { }
|
|
168
|
+
export function sandboxedSetInterval(func, context) {
|
|
169
|
+
sandboxSetInterval.prototype = sSI.prototype;
|
|
170
|
+
return sandboxSetInterval;
|
|
171
|
+
function sandboxSetInterval(handler, timeout, ...args) {
|
|
172
|
+
const sandbox = context.ctx.sandbox;
|
|
173
|
+
const h = typeof handler === 'string' ? func(handler) : handler;
|
|
174
|
+
const exec = (...a) => {
|
|
175
|
+
start = Date.now();
|
|
176
|
+
elapsed = 0;
|
|
177
|
+
return h(...a);
|
|
178
|
+
};
|
|
179
|
+
const sandBoxhandle = ++sandbox.timeoutHandleCounter;
|
|
180
|
+
let start = Date.now();
|
|
181
|
+
let handle = setInterval(exec, timeout, ...args);
|
|
182
|
+
let elapsed = 0;
|
|
183
|
+
const haltsub = sandbox.subscribeHalt(() => {
|
|
184
|
+
elapsed = Date.now() - start + elapsed;
|
|
185
|
+
clearInterval(handle);
|
|
186
|
+
});
|
|
187
|
+
const contsub = sandbox.subscribeResume(() => {
|
|
188
|
+
start = Date.now();
|
|
189
|
+
handle = setTimeout(() => {
|
|
190
|
+
start = Date.now();
|
|
191
|
+
elapsed = 0;
|
|
192
|
+
handle = setInterval(exec, timeout, ...args);
|
|
193
|
+
exec(...args);
|
|
194
|
+
}, Math.floor((timeout || 0) - elapsed), ...args);
|
|
195
|
+
handlObj.handle = handle;
|
|
196
|
+
});
|
|
197
|
+
const handlObj = {
|
|
198
|
+
handle,
|
|
199
|
+
haltsub,
|
|
200
|
+
contsub,
|
|
201
|
+
};
|
|
202
|
+
sandbox.setIntervalHandles.set(sandBoxhandle, handlObj);
|
|
203
|
+
return sandBoxhandle;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { LispItem, Lisp } from './parser.js';
|
|
2
|
+
import { IAuditReport, IExecContext, IScope, LispType, Prop, Scope, Ticks } from './utils.js';
|
|
3
|
+
export type Done<T = any> = (err?: any, res?: T | typeof optional) => void;
|
|
4
|
+
export declare class ExecReturn<T> {
|
|
5
|
+
auditReport: IAuditReport | undefined;
|
|
6
|
+
result: T;
|
|
7
|
+
returned: boolean;
|
|
8
|
+
breakLoop: boolean;
|
|
9
|
+
continueLoop: boolean;
|
|
10
|
+
constructor(auditReport: IAuditReport | undefined, result: T, returned: boolean, breakLoop?: boolean, continueLoop?: boolean);
|
|
11
|
+
}
|
|
12
|
+
export type Unknown = undefined | null | Record<string | number, unknown>;
|
|
13
|
+
export interface IChange {
|
|
14
|
+
type: string;
|
|
15
|
+
}
|
|
16
|
+
export interface ICreate extends IChange {
|
|
17
|
+
type: 'create';
|
|
18
|
+
prop: number | string;
|
|
19
|
+
}
|
|
20
|
+
export interface IReplace extends IChange {
|
|
21
|
+
type: 'replace';
|
|
22
|
+
}
|
|
23
|
+
export interface IDelete extends IChange {
|
|
24
|
+
type: 'delete';
|
|
25
|
+
prop: number | string;
|
|
26
|
+
}
|
|
27
|
+
export interface IReverse extends IChange {
|
|
28
|
+
type: 'reverse';
|
|
29
|
+
}
|
|
30
|
+
export interface ISort extends IChange {
|
|
31
|
+
type: 'sort';
|
|
32
|
+
}
|
|
33
|
+
export interface IPush extends IChange {
|
|
34
|
+
type: 'push';
|
|
35
|
+
added: unknown[];
|
|
36
|
+
}
|
|
37
|
+
export interface IPop extends IChange {
|
|
38
|
+
type: 'pop';
|
|
39
|
+
removed: unknown[];
|
|
40
|
+
}
|
|
41
|
+
export interface IShift extends IChange {
|
|
42
|
+
type: 'shift';
|
|
43
|
+
removed: unknown[];
|
|
44
|
+
}
|
|
45
|
+
export interface IUnShift extends IChange {
|
|
46
|
+
type: 'unshift';
|
|
47
|
+
added: unknown[];
|
|
48
|
+
}
|
|
49
|
+
export interface ISplice extends IChange {
|
|
50
|
+
type: 'splice';
|
|
51
|
+
startIndex: number;
|
|
52
|
+
deleteCount: number;
|
|
53
|
+
added: unknown[];
|
|
54
|
+
removed: unknown[];
|
|
55
|
+
}
|
|
56
|
+
export interface ICopyWithin extends IChange {
|
|
57
|
+
type: 'copyWithin';
|
|
58
|
+
startIndex: number;
|
|
59
|
+
endIndex: number;
|
|
60
|
+
added: unknown[];
|
|
61
|
+
removed: unknown[];
|
|
62
|
+
}
|
|
63
|
+
export type Change = ICreate | IReplace | IDelete | IReverse | ISort | IPush | IPop | IUnShift | IShift | ISplice | ICopyWithin;
|
|
64
|
+
declare const optional: {};
|
|
65
|
+
export declare function createFunction(argNames: string[], parsed: Lisp[], ticks: Ticks, context: IExecContext, scope?: Scope, name?: string): (...args: unknown[]) => unknown;
|
|
66
|
+
export declare function createFunctionAsync(argNames: string[], parsed: Lisp[], ticks: Ticks, context: IExecContext, scope?: Scope, name?: string): (...args: unknown[]) => Promise<unknown>;
|
|
67
|
+
export declare function assignCheck(obj: Prop, context: IExecContext, op?: string): void;
|
|
68
|
+
export declare class KeyVal {
|
|
69
|
+
key: string | SpreadObject;
|
|
70
|
+
val: unknown;
|
|
71
|
+
constructor(key: string | SpreadObject, val: unknown);
|
|
72
|
+
}
|
|
73
|
+
export declare class SpreadObject {
|
|
74
|
+
item: {
|
|
75
|
+
[key: string]: unknown;
|
|
76
|
+
};
|
|
77
|
+
constructor(item: {
|
|
78
|
+
[key: string]: unknown;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
export declare class SpreadArray {
|
|
82
|
+
item: unknown[];
|
|
83
|
+
constructor(item: unknown[]);
|
|
84
|
+
}
|
|
85
|
+
export declare class If {
|
|
86
|
+
t: Lisp;
|
|
87
|
+
f: Lisp;
|
|
88
|
+
constructor(t: Lisp, f: Lisp);
|
|
89
|
+
}
|
|
90
|
+
type OpCallback<a, b, obj, bobj> = (params: OpsCallbackParams<a, b, obj, bobj>) => void;
|
|
91
|
+
export declare const ops: Map<LispType, OpCallback<any, any, any, any>>;
|
|
92
|
+
export declare function addOps<a = unknown, b = unknown, obj = unknown, bobj = unknown>(type: LispType, cb: OpCallback<a, b, obj, bobj>): void;
|
|
93
|
+
export declare function execMany(ticks: Ticks, exec: Execution, tree: Lisp[], done: Done, scope: Scope, context: IExecContext, inLoopOrSwitch?: string): void;
|
|
94
|
+
type Execution = <T = any>(ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, done: Done<T>, inLoopOrSwitch?: string) => void;
|
|
95
|
+
export interface AsyncDoneRet {
|
|
96
|
+
isInstant: boolean;
|
|
97
|
+
instant: any;
|
|
98
|
+
p: Promise<{
|
|
99
|
+
result: any;
|
|
100
|
+
}>;
|
|
101
|
+
}
|
|
102
|
+
export declare function asyncDone(callback: (done: Done) => void): AsyncDoneRet;
|
|
103
|
+
export declare function syncDone(callback: (done: Done) => void): {
|
|
104
|
+
result: any;
|
|
105
|
+
};
|
|
106
|
+
export declare function execAsync<T = any>(ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, doneOriginal: Done<T>, inLoopOrSwitch?: string): Promise<void>;
|
|
107
|
+
export declare function execSync<T = any>(ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, done: Done<T>, inLoopOrSwitch?: string): void;
|
|
108
|
+
type OpsCallbackParams<a, b, obj, bobj> = {
|
|
109
|
+
op: LispType;
|
|
110
|
+
exec: Execution;
|
|
111
|
+
a: a;
|
|
112
|
+
b: b;
|
|
113
|
+
obj: obj;
|
|
114
|
+
bobj: bobj;
|
|
115
|
+
ticks: Ticks;
|
|
116
|
+
tree: LispItem;
|
|
117
|
+
scope: Scope;
|
|
118
|
+
context: IExecContext;
|
|
119
|
+
done: Done;
|
|
120
|
+
inLoopOrSwitch?: string;
|
|
121
|
+
};
|
|
122
|
+
export declare function executeTree<T>(ticks: Ticks, context: IExecContext, executionTree: Lisp[], scopes?: IScope[], inLoopOrSwitch?: string): ExecReturn<T>;
|
|
123
|
+
export declare function executeTreeAsync<T>(ticks: Ticks, context: IExecContext, executionTree: Lisp[], scopes?: IScope[], inLoopOrSwitch?: string): Promise<ExecReturn<T>>;
|
|
124
|
+
export {};
|