@nyariv/sandboxjs 0.8.23 → 0.8.25

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.
Files changed (54) hide show
  1. package/.eslintignore +6 -0
  2. package/.eslintrc.js +22 -0
  3. package/.prettierrc +4 -0
  4. package/.vscode/settings.json +4 -0
  5. package/README.md +1 -1
  6. package/build/Sandbox.d.ts +11 -79
  7. package/build/Sandbox.js +21 -216
  8. package/build/SandboxExec.d.ts +25 -0
  9. package/build/SandboxExec.js +169 -0
  10. package/build/eval.d.ts +18 -0
  11. package/build/eval.js +43 -0
  12. package/build/executor.d.ts +29 -90
  13. package/build/executor.js +249 -328
  14. package/build/parser.d.ts +8 -239
  15. package/build/parser.js +345 -444
  16. package/build/unraw.js +13 -16
  17. package/build/utils.d.ts +242 -0
  18. package/build/utils.js +276 -0
  19. package/dist/Sandbox.d.ts +11 -79
  20. package/dist/Sandbox.js +106 -1
  21. package/dist/Sandbox.js.map +1 -1
  22. package/dist/Sandbox.min.js +2 -0
  23. package/dist/Sandbox.min.js.map +1 -0
  24. package/dist/SandboxExec.d.ts +25 -0
  25. package/dist/SandboxExec.js +173 -0
  26. package/dist/SandboxExec.js.map +1 -0
  27. package/dist/SandboxExec.min.js +2 -0
  28. package/dist/SandboxExec.min.js.map +1 -0
  29. package/dist/eval.d.ts +18 -0
  30. package/dist/executor.d.ts +29 -90
  31. package/dist/executor.js +1270 -0
  32. package/dist/executor.js.map +1 -0
  33. package/dist/node/Sandbox.d.ts +11 -79
  34. package/dist/node/Sandbox.js +36 -3150
  35. package/dist/node/SandboxExec.d.ts +25 -0
  36. package/dist/node/SandboxExec.js +176 -0
  37. package/dist/node/eval.d.ts +18 -0
  38. package/dist/node/executor.d.ts +29 -90
  39. package/dist/node/executor.js +1289 -0
  40. package/dist/node/parser.d.ts +8 -239
  41. package/dist/node/parser.js +1527 -0
  42. package/dist/node/utils.d.ts +242 -0
  43. package/dist/node/utils.js +290 -0
  44. package/dist/parser.d.ts +8 -239
  45. package/dist/parser.js +1513 -0
  46. package/dist/parser.js.map +1 -0
  47. package/dist/utils.d.ts +242 -0
  48. package/dist/utils.js +279 -0
  49. package/dist/utils.js.map +1 -0
  50. package/jest.config.js +200 -0
  51. package/package.json +16 -5
  52. package/tsconfig.jest.json +16 -0
  53. package/.github/workflows/npm-publish.yml +0 -34
  54. package/rollup.config.mjs +0 -33
package/build/eval.js ADDED
@@ -0,0 +1,43 @@
1
+ import { createFunction, currentTicks } from './executor.js';
2
+ import parse, { lispifyFunction } from './parser.js';
3
+ export function createEvalContext() {
4
+ return {
5
+ sandboxFunction,
6
+ sandboxedEval,
7
+ sandboxedSetTimeout,
8
+ sandboxedSetInterval,
9
+ lispifyFunction,
10
+ };
11
+ }
12
+ export function sandboxFunction(context, ticks) {
13
+ return SandboxFunction;
14
+ function SandboxFunction(...params) {
15
+ const code = params.pop() || '';
16
+ const parsed = parse(code);
17
+ return createFunction(params, parsed.tree, ticks || currentTicks.current, {
18
+ ...context,
19
+ constants: parsed.constants,
20
+ tree: parsed.tree,
21
+ }, undefined, 'anonymous');
22
+ }
23
+ }
24
+ export function sandboxedEval(func) {
25
+ return sandboxEval;
26
+ function sandboxEval(code) {
27
+ return func(code)();
28
+ }
29
+ }
30
+ export function sandboxedSetTimeout(func) {
31
+ return function sandboxSetTimeout(handler, ...args) {
32
+ if (typeof handler !== 'string')
33
+ return setTimeout(handler, ...args);
34
+ return setTimeout(func(handler), ...args);
35
+ };
36
+ }
37
+ export function sandboxedSetInterval(func) {
38
+ return function sandboxSetInterval(handler, ...args) {
39
+ if (typeof handler !== 'string')
40
+ return setInterval(handler, ...args);
41
+ return setInterval(func(handler), ...args);
42
+ };
43
+ }
@@ -1,134 +1,70 @@
1
- import { LispItem, Lisp, LispType, LispFamily, ExtractLispOp } from "./parser.js";
2
- import { IExecContext, Ticks } from "./Sandbox.js";
3
- export type SandboxFunction = (code: string, ...args: unknown[]) => () => unknown;
4
- export type sandboxedEval = (code: string) => unknown;
5
- export type sandboxSetTimeout = (handler: TimerHandler, timeout?: number, ...args: unknown[]) => any;
6
- export type sandboxSetInterval = (handler: TimerHandler, timeout?: number, ...args: unknown[]) => any;
7
- export type Done = (err?: unknown, res?: unknown) => void;
1
+ import { LispItem, Lisp, LispFamily, ExtractLispOp } 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;
8
4
  export declare class ExecReturn<T> {
9
- auditReport: IAuditReport;
5
+ auditReport: IAuditReport | undefined;
10
6
  result: T;
11
7
  returned: boolean;
12
8
  breakLoop: boolean;
13
9
  continueLoop: boolean;
14
- constructor(auditReport: IAuditReport, result: T, returned: boolean, breakLoop?: boolean, continueLoop?: boolean);
15
- }
16
- export interface IAuditReport {
17
- globalsAccess: Set<unknown>;
18
- prototypeAccess: {
19
- [name: string]: Set<string>;
20
- };
21
- }
22
- export interface IGlobals {
23
- [key: string]: unknown;
10
+ constructor(auditReport: IAuditReport | undefined, result: T, returned: boolean, breakLoop?: boolean, continueLoop?: boolean);
24
11
  }
12
+ export type Unknown = undefined | null | Record<string | number, unknown>;
25
13
  export interface IChange {
26
14
  type: string;
27
15
  }
28
16
  export interface ICreate extends IChange {
29
- type: "create";
17
+ type: 'create';
30
18
  prop: number | string;
31
19
  }
32
20
  export interface IReplace extends IChange {
33
- type: "replace";
21
+ type: 'replace';
34
22
  }
35
23
  export interface IDelete extends IChange {
36
- type: "delete";
24
+ type: 'delete';
37
25
  prop: number | string;
38
26
  }
39
27
  export interface IReverse extends IChange {
40
- type: "reverse";
28
+ type: 'reverse';
41
29
  }
42
30
  export interface ISort extends IChange {
43
- type: "sort";
31
+ type: 'sort';
44
32
  }
45
33
  export interface IPush extends IChange {
46
- type: "push";
34
+ type: 'push';
47
35
  added: unknown[];
48
36
  }
49
37
  export interface IPop extends IChange {
50
- type: "pop";
38
+ type: 'pop';
51
39
  removed: unknown[];
52
40
  }
53
41
  export interface IShift extends IChange {
54
- type: "shift";
42
+ type: 'shift';
55
43
  removed: unknown[];
56
44
  }
57
45
  export interface IUnShift extends IChange {
58
- type: "unshift";
46
+ type: 'unshift';
59
47
  added: unknown[];
60
48
  }
61
49
  export interface ISplice extends IChange {
62
- type: "splice";
50
+ type: 'splice';
63
51
  startIndex: number;
64
52
  deleteCount: number;
65
53
  added: unknown[];
66
54
  removed: unknown[];
67
55
  }
68
56
  export interface ICopyWithin extends IChange {
69
- type: "copyWithin";
57
+ type: 'copyWithin';
70
58
  startIndex: number;
71
59
  endIndex: number;
72
60
  added: unknown[];
73
61
  removed: unknown[];
74
62
  }
75
63
  export type Change = ICreate | IReplace | IDelete | IReverse | ISort | IPush | IPop | IUnShift | IShift | ISplice | ICopyWithin;
76
- export declare class Prop {
77
- context: {
78
- [key: string]: any;
79
- };
80
- prop: string;
81
- isConst: boolean;
82
- isGlobal: boolean;
83
- isVariable: boolean;
84
- constructor(context: {
85
- [key: string]: any;
86
- }, prop: string, isConst?: boolean, isGlobal?: boolean, isVariable?: boolean);
87
- get<T = unknown>(context: IExecContext): T;
88
- }
89
- declare enum VarType {
90
- let = "let",
91
- const = "const",
92
- var = "var"
93
- }
94
- export declare class Scope {
95
- parent: Scope;
96
- const: {
97
- [key: string]: true;
98
- };
99
- let: {
100
- [key: string]: true;
101
- };
102
- var: {
103
- [key: string]: true;
104
- };
105
- globals: {
106
- [key: string]: true;
107
- };
108
- allVars: {
109
- [key: string]: unknown;
110
- } & Object;
111
- functionThis?: unknown;
112
- constructor(parent: Scope, vars?: {}, functionThis?: unknown);
113
- get(key: string, functionScope?: boolean): Prop;
114
- set(key: string, val: unknown): Prop;
115
- declare(key: string, type?: VarType, value?: unknown, isGlobal?: boolean): Prop;
116
- }
117
- export interface IScope {
118
- [key: string]: any;
119
- }
120
- export declare class FunctionScope implements IScope {
121
- }
122
- export declare class LocalScope implements IScope {
123
- }
124
- export declare class SandboxError extends Error {
125
- }
126
- export declare function sandboxFunction(context: IExecContext, ticks?: Ticks): SandboxFunction;
127
- export declare function createFunction(argNames: string[], parsed: Lisp[], ticks: Ticks, context: IExecContext, scope?: Scope, name?: string): any;
128
- export declare function createFunctionAsync(argNames: string[], parsed: Lisp[], ticks: Ticks, context: IExecContext, scope?: Scope, name?: string): any;
129
- export declare function sandboxedEval(func: SandboxFunction): sandboxedEval;
130
- export declare function sandboxedSetTimeout(func: SandboxFunction): sandboxSetTimeout;
131
- export declare function sandboxedSetInterval(func: SandboxFunction): sandboxSetInterval;
64
+ declare const optional: {};
65
+ export declare const sandboxedFunctions: WeakSet<object>;
66
+ export declare function createFunction(argNames: string[], parsed: Lisp[], ticks: Ticks, context: IExecContext, scope?: Scope, name?: string): (...args: unknown[]) => unknown;
67
+ export declare function createFunctionAsync(argNames: string[], parsed: Lisp[], ticks: Ticks, context: IExecContext, scope?: Scope, name?: string): (...args: unknown[]) => Promise<unknown>;
132
68
  export declare function assignCheck(obj: Prop, context: IExecContext, op?: string): void;
133
69
  export declare class KeyVal {
134
70
  key: string | SpreadObject;
@@ -156,7 +92,7 @@ type OpCallback = (exec: Execution, done: Done, ticks: Ticks, a: any, b: any, ob
156
92
  export declare const ops: Map<LispType, OpCallback>;
157
93
  export declare function addOps<Type extends LispFamily>(type: ExtractLispOp<Type>, cb: OpCallback): void;
158
94
  export declare function execMany(ticks: Ticks, exec: Execution, tree: Lisp[], done: Done, scope: Scope, context: IExecContext, inLoopOrSwitch?: string): void;
159
- type Execution = (ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, done: Done, inLoopOrSwitch?: string) => void;
95
+ type Execution = <T = any>(ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, done: Done<T>, inLoopOrSwitch?: string) => void;
160
96
  export interface AsyncDoneRet {
161
97
  isInstant: boolean;
162
98
  instant: any;
@@ -168,8 +104,11 @@ export declare function asyncDone(callback: (done: Done) => void): AsyncDoneRet;
168
104
  export declare function syncDone(callback: (done: Done) => void): {
169
105
  result: any;
170
106
  };
171
- export declare function execAsync(ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, doneOriginal: Done, inLoopOrSwitch?: string): Promise<void>;
172
- export declare function execSync(ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, done: Done, inLoopOrSwitch?: string): void;
173
- export declare function executeTree<T>(ticks: Ticks, context: IExecContext, executionTree: Lisp[], scopes?: (IScope)[], inLoopOrSwitch?: string): ExecReturn<T>;
174
- export declare function executeTreeAsync<T>(ticks: Ticks, context: IExecContext, executionTree: Lisp[], scopes?: (IScope)[], inLoopOrSwitch?: string): Promise<ExecReturn<T>>;
107
+ export declare function execAsync<T = any>(ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, doneOriginal: Done<T>, inLoopOrSwitch?: string): Promise<void>;
108
+ export declare function execSync<T = any>(ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, done: Done<T>, inLoopOrSwitch?: string): void;
109
+ export declare const currentTicks: {
110
+ current: Ticks;
111
+ };
112
+ export declare function executeTree<T>(ticks: Ticks, context: IExecContext, executionTree: Lisp[], scopes?: IScope[], inLoopOrSwitch?: string): ExecReturn<T>;
113
+ export declare function executeTreeAsync<T>(ticks: Ticks, context: IExecContext, executionTree: Lisp[], scopes?: IScope[], inLoopOrSwitch?: string): Promise<ExecReturn<T>>;
175
114
  export {};