@nyariv/sandboxjs 0.8.22 → 0.8.24

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 (50) 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/build/Sandbox.d.ts +11 -78
  6. package/build/Sandbox.js +21 -216
  7. package/build/SandboxExec.d.ts +25 -0
  8. package/build/SandboxExec.js +169 -0
  9. package/build/eval.d.ts +18 -0
  10. package/build/eval.js +43 -0
  11. package/build/executor.d.ts +56 -95
  12. package/build/executor.js +739 -815
  13. package/build/parser.d.ts +112 -74
  14. package/build/parser.js +504 -542
  15. package/build/unraw.js +13 -16
  16. package/build/utils.d.ts +242 -0
  17. package/build/utils.js +276 -0
  18. package/dist/Sandbox.d.ts +11 -78
  19. package/dist/Sandbox.js +106 -1
  20. package/dist/Sandbox.js.map +1 -1
  21. package/dist/Sandbox.min.js +1 -1
  22. package/dist/Sandbox.min.js.map +1 -1
  23. package/dist/SandboxExec.d.ts +25 -0
  24. package/dist/SandboxExec.js +173 -0
  25. package/dist/SandboxExec.js.map +1 -0
  26. package/dist/SandboxExec.min.js +2 -0
  27. package/dist/SandboxExec.min.js.map +1 -0
  28. package/dist/eval.d.ts +18 -0
  29. package/dist/executor.d.ts +56 -95
  30. package/dist/executor.js +1270 -0
  31. package/dist/executor.js.map +1 -0
  32. package/dist/node/Sandbox.d.ts +11 -78
  33. package/dist/node/Sandbox.js +37 -3091
  34. package/dist/node/SandboxExec.d.ts +25 -0
  35. package/dist/node/SandboxExec.js +176 -0
  36. package/dist/node/eval.d.ts +18 -0
  37. package/dist/node/executor.d.ts +56 -95
  38. package/dist/node/executor.js +1289 -0
  39. package/dist/node/parser.d.ts +112 -74
  40. package/dist/node/parser.js +1528 -0
  41. package/dist/node/utils.d.ts +242 -0
  42. package/dist/node/utils.js +290 -0
  43. package/dist/parser.d.ts +112 -74
  44. package/dist/parser.js +1514 -0
  45. package/dist/parser.js.map +1 -0
  46. package/dist/utils.d.ts +242 -0
  47. package/dist/utils.js +279 -0
  48. package/dist/utils.js.map +1 -0
  49. package/package.json +22 -14
  50. package/.github/workflows/npm-publish.yml +0 -34
@@ -0,0 +1,25 @@
1
+ import { IEvalContext } from './eval.js';
2
+ import { Change, ExecReturn } from './executor.js';
3
+ import { IContext, IExecContext, IGlobals, IOptionParams, IScope, SubscriptionSubject } from './utils.js';
4
+ export default class SandboxExec {
5
+ evalContext?: IEvalContext | undefined;
6
+ context: IContext;
7
+ setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
8
+ changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
9
+ sandboxFunctions: WeakMap<(...args: any[]) => any, IExecContext>;
10
+ constructor(options?: IOptionParams, evalContext?: IEvalContext | undefined);
11
+ static get SAFE_GLOBALS(): IGlobals;
12
+ static get SAFE_PROTOTYPES(): Map<any, Set<string>>;
13
+ subscribeGet(callback: (obj: SubscriptionSubject, name: string) => void, context: IExecContext): {
14
+ unsubscribe: () => void;
15
+ };
16
+ subscribeSet(obj: object, name: string, callback: (modification: Change) => void, context: SandboxExec | IExecContext): {
17
+ unsubscribe: () => void;
18
+ };
19
+ subscribeSetGlobal(obj: SubscriptionSubject, name: string, callback: (modification: Change) => void): {
20
+ unsubscribe: () => void;
21
+ };
22
+ getContext(fn: (...args: any[]) => any): IExecContext | undefined;
23
+ executeTree<T>(context: IExecContext, scopes?: IScope[]): ExecReturn<T>;
24
+ executeTreeAsync<T>(context: IExecContext, scopes?: IScope[]): Promise<ExecReturn<T>>;
25
+ }
@@ -0,0 +1,176 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var executor = require('./executor.js');
6
+ var utils = require('./utils.js');
7
+
8
+ function subscribeSet(obj, name, callback, context) {
9
+ if (!(obj instanceof Object))
10
+ throw new Error('Invalid subscription object, got ' + (typeof obj === 'object' ? 'null' : typeof obj));
11
+ const names = context.setSubscriptions.get(obj) || new Map();
12
+ context.setSubscriptions.set(obj, names);
13
+ const callbacks = names.get(name) || new Set();
14
+ names.set(name, callbacks);
15
+ callbacks.add(callback);
16
+ let changeCbs;
17
+ const val = obj[name];
18
+ if (val instanceof Object) {
19
+ changeCbs = context.changeSubscriptions.get(val) || new Set();
20
+ changeCbs.add(callback);
21
+ context.changeSubscriptions.set(val, changeCbs);
22
+ }
23
+ return {
24
+ unsubscribe: () => {
25
+ callbacks.delete(callback);
26
+ changeCbs?.delete(callback);
27
+ },
28
+ };
29
+ }
30
+ class SandboxExec {
31
+ constructor(options, evalContext) {
32
+ this.evalContext = evalContext;
33
+ this.setSubscriptions = new WeakMap();
34
+ this.changeSubscriptions = new WeakMap();
35
+ this.sandboxFunctions = new WeakMap();
36
+ const opt = Object.assign({
37
+ audit: false,
38
+ forbidFunctionCalls: false,
39
+ forbidFunctionCreation: false,
40
+ globals: SandboxExec.SAFE_GLOBALS,
41
+ prototypeWhitelist: SandboxExec.SAFE_PROTOTYPES,
42
+ prototypeReplacements: new Map(),
43
+ }, options || {});
44
+ this.context = utils.createContext(this, opt);
45
+ }
46
+ static get SAFE_GLOBALS() {
47
+ return {
48
+ Function,
49
+ console: {
50
+ debug: console.debug,
51
+ error: console.error,
52
+ info: console.info,
53
+ log: console.log,
54
+ table: console.table,
55
+ warn: console.warn,
56
+ },
57
+ isFinite,
58
+ isNaN,
59
+ parseFloat,
60
+ parseInt,
61
+ decodeURI,
62
+ decodeURIComponent,
63
+ encodeURI,
64
+ encodeURIComponent,
65
+ escape,
66
+ unescape,
67
+ Boolean,
68
+ Number,
69
+ BigInt,
70
+ String,
71
+ Object,
72
+ Array,
73
+ Symbol,
74
+ Error,
75
+ EvalError,
76
+ RangeError,
77
+ ReferenceError,
78
+ SyntaxError,
79
+ TypeError,
80
+ URIError,
81
+ Int8Array,
82
+ Uint8Array,
83
+ Uint8ClampedArray,
84
+ Int16Array,
85
+ Uint16Array,
86
+ Int32Array,
87
+ Uint32Array,
88
+ Float32Array,
89
+ Float64Array,
90
+ Map,
91
+ Set,
92
+ WeakMap,
93
+ WeakSet,
94
+ Promise,
95
+ Intl,
96
+ JSON,
97
+ Math,
98
+ Date,
99
+ RegExp,
100
+ };
101
+ }
102
+ static get SAFE_PROTOTYPES() {
103
+ const protos = [
104
+ utils.SandboxGlobal,
105
+ Function,
106
+ Boolean,
107
+ Number,
108
+ BigInt,
109
+ String,
110
+ Date,
111
+ Error,
112
+ Array,
113
+ Int8Array,
114
+ Uint8Array,
115
+ Uint8ClampedArray,
116
+ Int16Array,
117
+ Uint16Array,
118
+ Int32Array,
119
+ Uint32Array,
120
+ Float32Array,
121
+ Float64Array,
122
+ Map,
123
+ Set,
124
+ WeakMap,
125
+ WeakSet,
126
+ Promise,
127
+ Symbol,
128
+ Date,
129
+ RegExp,
130
+ ];
131
+ const map = new Map();
132
+ protos.forEach((proto) => {
133
+ map.set(proto, new Set());
134
+ });
135
+ map.set(Object, new Set([
136
+ 'entries',
137
+ 'fromEntries',
138
+ 'getOwnPropertyNames',
139
+ 'is',
140
+ 'keys',
141
+ 'hasOwnProperty',
142
+ 'isPrototypeOf',
143
+ 'propertyIsEnumerable',
144
+ 'toLocaleString',
145
+ 'toString',
146
+ 'valueOf',
147
+ 'values',
148
+ ]));
149
+ return map;
150
+ }
151
+ subscribeGet(callback, context) {
152
+ context.getSubscriptions.add(callback);
153
+ return { unsubscribe: () => context.getSubscriptions.delete(callback) };
154
+ }
155
+ subscribeSet(obj, name, callback, context) {
156
+ return subscribeSet(obj, name, callback, context);
157
+ }
158
+ subscribeSetGlobal(obj, name, callback) {
159
+ return subscribeSet(obj, name, callback, this);
160
+ }
161
+ getContext(fn) {
162
+ return this.sandboxFunctions.get(fn);
163
+ }
164
+ executeTree(context, scopes = []) {
165
+ return executor.executeTree({
166
+ ticks: BigInt(0),
167
+ }, context, context.tree, scopes);
168
+ }
169
+ executeTreeAsync(context, scopes = []) {
170
+ return executor.executeTreeAsync({
171
+ ticks: BigInt(0),
172
+ }, context, context.tree, scopes);
173
+ }
174
+ }
175
+
176
+ exports.default = SandboxExec;
@@ -0,0 +1,18 @@
1
+ import { lispifyFunction } from './parser.js';
2
+ import { IExecContext, Ticks } from './utils.js';
3
+ export interface IEvalContext {
4
+ sandboxFunction: typeof sandboxFunction;
5
+ sandboxedEval: typeof sandboxedEval;
6
+ sandboxedSetTimeout: typeof sandboxedSetTimeout;
7
+ sandboxedSetInterval: typeof sandboxedSetInterval;
8
+ lispifyFunction: typeof lispifyFunction;
9
+ }
10
+ export type SandboxFunction = (code: string, ...args: string[]) => () => unknown;
11
+ export type SandboxEval = (code: string) => unknown;
12
+ export type SandboxSetTimeout = (handler: TimerHandler, timeout?: number, ...args: unknown[]) => any;
13
+ export type SandboxSetInterval = (handler: TimerHandler, timeout?: number, ...args: unknown[]) => any;
14
+ export declare function createEvalContext(): IEvalContext;
15
+ export declare function sandboxFunction(context: IExecContext, ticks?: Ticks): SandboxFunction;
16
+ export declare function sandboxedEval(func: SandboxFunction): SandboxEval;
17
+ export declare function sandboxedSetTimeout(func: SandboxFunction): SandboxSetTimeout;
18
+ export declare function sandboxedSetInterval(func: SandboxFunction): SandboxSetInterval;
@@ -1,140 +1,98 @@
1
- import { LispItem, Lisp, LispArray } from "./parser.js";
2
- import { IExecContext, Ticks } from "./Sandbox.js";
3
- export declare type SandboxFunction = (code: string, ...args: any[]) => () => any;
4
- export declare type sandboxedEval = (code: string) => any;
5
- export declare type sandboxSetTimeout = (handler: TimerHandler, timeout?: any, ...args: any[]) => any;
6
- export declare type sandboxSetInterval = (handler: TimerHandler, timeout?: any, ...args: any[]) => any;
7
- export declare type Done = (err?: any, res?: any) => 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<any>;
18
- prototypeAccess: {
19
- [name: string]: Set<string>;
20
- };
21
- }
22
- export interface IGlobals {
23
- [key: string]: any;
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
- export declare type Change = ICreate | IReplace | IDelete | IReverse | ISort | IPush | IPop | IUnShift | IShift | ISplice | ICopyWithin;
76
- export declare type replacementCallback = (obj: any, isStaticAccess: boolean) => any;
77
- export declare class Prop {
78
- context: {
79
- [key: string]: any;
80
- };
81
- prop: string;
82
- isConst: boolean;
83
- isGlobal: boolean;
84
- isVariable: boolean;
85
- constructor(context: {
86
- [key: string]: any;
87
- }, prop: string, isConst?: boolean, isGlobal?: boolean, isVariable?: boolean);
88
- get(context: IExecContext): any;
89
- }
90
- declare enum VarType {
91
- let = "let",
92
- const = "const",
93
- var = "var"
94
- }
95
- export declare class Scope {
96
- parent: Scope;
97
- const: {
98
- [key: string]: true;
99
- };
100
- let: {
101
- [key: string]: true;
102
- };
103
- var: {
104
- [key: string]: true;
105
- };
106
- globals: {
107
- [key: string]: true;
108
- };
109
- allVars: {
110
- [key: string]: any;
111
- } & Object;
112
- functionThis?: any;
113
- constructor(parent: Scope, vars?: {}, functionThis?: any);
114
- get(key: string, functionScope?: boolean): Prop;
115
- set(key: string, val: any): Prop;
116
- declare(key: string, type?: VarType, value?: any, isGlobal?: boolean): any;
117
- }
118
- export interface IScope {
119
- [key: string]: any;
120
- }
121
- export declare class FunctionScope implements IScope {
122
- }
123
- export declare class LocalScope implements IScope {
124
- }
125
- export declare class SandboxError extends Error {
126
- }
127
- export declare function sandboxFunction(context: IExecContext, ticks?: Ticks): SandboxFunction;
128
- export declare function createFunction(argNames: string[], parsed: LispItem, ticks: Ticks, context: IExecContext, scope?: Scope, name?: string): any;
129
- export declare function createFunctionAsync(argNames: string[], parsed: LispItem, ticks: Ticks, context: IExecContext, scope?: Scope, name?: string): any;
130
- export declare function sandboxedEval(func: SandboxFunction): sandboxedEval;
131
- export declare function sandboxedSetTimeout(func: SandboxFunction): sandboxSetTimeout;
132
- export declare function sandboxedSetInterval(func: SandboxFunction): sandboxSetInterval;
63
+ export type Change = ICreate | IReplace | IDelete | IReverse | ISort | IPush | IPop | IUnShift | IShift | ISplice | ICopyWithin;
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>;
133
68
  export declare function assignCheck(obj: Prop, context: IExecContext, op?: string): void;
134
- declare type OpCallback = (exec: Execution, done: Done, ticks: Ticks, a: LispItem | string[], b: LispItem | Lisp[], obj: Prop | any | undefined, context: IExecContext, scope: Scope, bobj?: Prop | any | undefined, inLoopOrSwitch?: string) => void;
135
- export declare let ops: Map<string, OpCallback>;
136
- export declare function execMany(ticks: Ticks, exec: Execution, tree: LispArray, done: Done, scope: Scope, context: IExecContext, inLoopOrSwitch?: string): void;
137
- declare type Execution = (ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, done: Done, inLoopOrSwitch?: string) => void;
69
+ export declare class KeyVal {
70
+ key: string | SpreadObject;
71
+ val: unknown;
72
+ constructor(key: string | SpreadObject, val: unknown);
73
+ }
74
+ export declare class SpreadObject {
75
+ item: {
76
+ [key: string]: unknown;
77
+ };
78
+ constructor(item: {
79
+ [key: string]: unknown;
80
+ });
81
+ }
82
+ export declare class SpreadArray {
83
+ item: unknown[];
84
+ constructor(item: unknown[]);
85
+ }
86
+ export declare class If {
87
+ t: Lisp;
88
+ f: Lisp;
89
+ constructor(t: Lisp, f: Lisp);
90
+ }
91
+ type OpCallback = (exec: Execution, done: Done, ticks: Ticks, a: any, b: any, obj: any, context: IExecContext, scope: Scope, bobj?: any, inLoopOrSwitch?: string) => void;
92
+ export declare const ops: Map<LispType, OpCallback>;
93
+ export declare function addOps<Type extends LispFamily>(type: ExtractLispOp<Type>, cb: OpCallback): void;
94
+ export declare function execMany(ticks: Ticks, exec: Execution, tree: Lisp[], done: Done, scope: Scope, context: IExecContext, inLoopOrSwitch?: string): void;
95
+ type Execution = <T = any>(ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, done: Done<T>, inLoopOrSwitch?: string) => void;
138
96
  export interface AsyncDoneRet {
139
97
  isInstant: boolean;
140
98
  instant: any;
@@ -146,8 +104,11 @@ export declare function asyncDone(callback: (done: Done) => void): AsyncDoneRet;
146
104
  export declare function syncDone(callback: (done: Done) => void): {
147
105
  result: any;
148
106
  };
149
- export declare function execAsync(ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, doneOriginal: Done, inLoopOrSwitch?: string): Promise<void>;
150
- export declare function execSync(ticks: Ticks, tree: LispItem, scope: Scope, context: IExecContext, done: Done, inLoopOrSwitch?: string): void;
151
- export declare function executeTree<T>(ticks: Ticks, context: IExecContext, executionTree: LispItem, scopes?: (IScope)[], inLoopOrSwitch?: string): ExecReturn<T>;
152
- export declare function executeTreeAsync<T>(ticks: Ticks, context: IExecContext, executionTree: LispItem, 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>>;
153
114
  export {};