@cuxt/sandboxjs 0.1.1 → 0.1.4

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 (226) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +198 -185
  3. package/{build → dist/cjs}/Sandbox.d.ts +15 -6
  4. package/dist/cjs/Sandbox.js +126 -0
  5. package/dist/{SandboxExec.d.ts → cjs/SandboxExec.d.ts} +8 -17
  6. package/dist/cjs/SandboxExec.js +227 -0
  7. package/{build/eval.d.ts → dist/cjs/eval/index.d.ts} +10 -2
  8. package/dist/cjs/eval/index.js +233 -0
  9. package/dist/cjs/executor/executorUtils.d.ts +161 -0
  10. package/dist/cjs/executor/executorUtils.js +930 -0
  11. package/dist/cjs/executor/index.d.ts +1 -0
  12. package/dist/cjs/executor/index.js +2 -0
  13. package/dist/cjs/executor/ops/assignment.d.ts +1 -0
  14. package/dist/cjs/executor/ops/assignment.js +88 -0
  15. package/dist/cjs/executor/ops/call.d.ts +1 -0
  16. package/dist/cjs/executor/ops/call.js +160 -0
  17. package/dist/cjs/executor/ops/comparison.d.ts +1 -0
  18. package/dist/cjs/executor/ops/comparison.js +36 -0
  19. package/dist/cjs/executor/ops/control.d.ts +1 -0
  20. package/dist/cjs/executor/ops/control.js +203 -0
  21. package/dist/cjs/executor/ops/functions.d.ts +1 -0
  22. package/dist/cjs/executor/ops/functions.js +55 -0
  23. package/dist/cjs/executor/ops/index.d.ts +0 -0
  24. package/dist/cjs/executor/ops/index.js +11 -0
  25. package/dist/cjs/executor/ops/literals.d.ts +1 -0
  26. package/dist/cjs/executor/ops/literals.js +45 -0
  27. package/dist/cjs/executor/ops/misc.d.ts +1 -0
  28. package/dist/cjs/executor/ops/misc.js +25 -0
  29. package/dist/cjs/executor/ops/object.d.ts +1 -0
  30. package/dist/cjs/executor/ops/object.js +51 -0
  31. package/dist/cjs/executor/ops/prop.d.ts +1 -0
  32. package/dist/cjs/executor/ops/prop.js +70 -0
  33. package/dist/cjs/executor/ops/unary.d.ts +1 -0
  34. package/dist/cjs/executor/ops/unary.js +48 -0
  35. package/dist/cjs/executor/ops/variables.d.ts +1 -0
  36. package/dist/cjs/executor/ops/variables.js +16 -0
  37. package/dist/cjs/executor/opsRegistry.d.ts +33 -0
  38. package/dist/cjs/executor/opsRegistry.js +8 -0
  39. package/dist/cjs/package.json +3 -0
  40. package/dist/cjs/parser/index.d.ts +3 -0
  41. package/dist/cjs/parser/index.js +1 -0
  42. package/dist/{parser.d.ts → cjs/parser/lisp.d.ts} +29 -42
  43. package/dist/cjs/parser/lispTypes/conditionals.d.ts +2 -0
  44. package/dist/cjs/parser/lispTypes/conditionals.js +135 -0
  45. package/dist/cjs/parser/lispTypes/control.d.ts +2 -0
  46. package/dist/cjs/parser/lispTypes/control.js +204 -0
  47. package/dist/cjs/parser/lispTypes/declarations.d.ts +2 -0
  48. package/dist/cjs/parser/lispTypes/declarations.js +99 -0
  49. package/dist/cjs/parser/lispTypes/index.d.ts +3 -0
  50. package/dist/cjs/parser/lispTypes/index.js +17 -0
  51. package/dist/cjs/parser/lispTypes/operators.d.ts +2 -0
  52. package/dist/cjs/parser/lispTypes/operators.js +252 -0
  53. package/dist/cjs/parser/lispTypes/shared.d.ts +38 -0
  54. package/dist/cjs/parser/lispTypes/structures.d.ts +2 -0
  55. package/dist/cjs/parser/lispTypes/structures.js +188 -0
  56. package/dist/cjs/parser/lispTypes/values.d.ts +2 -0
  57. package/dist/cjs/parser/lispTypes/values.js +89 -0
  58. package/dist/cjs/parser/parserUtils.d.ts +34 -0
  59. package/dist/cjs/parser/parserUtils.js +968 -0
  60. package/dist/cjs/utils/CodeString.d.ts +16 -0
  61. package/dist/cjs/utils/CodeString.js +64 -0
  62. package/dist/cjs/utils/ExecContext.d.ts +34 -0
  63. package/dist/cjs/utils/ExecContext.js +171 -0
  64. package/dist/cjs/utils/Prop.d.ts +16 -0
  65. package/dist/cjs/utils/Prop.js +81 -0
  66. package/dist/cjs/utils/Scope.d.ts +47 -0
  67. package/dist/cjs/utils/Scope.js +127 -0
  68. package/dist/cjs/utils/errors.d.ts +10 -0
  69. package/dist/cjs/utils/errors.js +12 -0
  70. package/dist/cjs/utils/functionReplacements.d.ts +11 -0
  71. package/dist/cjs/utils/functionReplacements.js +362 -0
  72. package/dist/cjs/utils/index.d.ts +7 -0
  73. package/dist/cjs/utils/index.js +7 -0
  74. package/dist/cjs/utils/types.d.ts +221 -0
  75. package/dist/cjs/utils/types.js +164 -0
  76. package/dist/cjs/utils/unraw.js +145 -0
  77. package/dist/{node → esm}/Sandbox.d.ts +15 -6
  78. package/dist/esm/Sandbox.js +115 -0
  79. package/dist/esm/Sandbox.js.map +1 -0
  80. package/{build → dist/esm}/SandboxExec.d.ts +8 -17
  81. package/dist/esm/SandboxExec.js +224 -0
  82. package/dist/esm/SandboxExec.js.map +1 -0
  83. package/dist/{eval.d.ts → esm/eval/index.d.ts} +10 -2
  84. package/dist/esm/eval/index.js +235 -0
  85. package/dist/esm/eval/index.js.map +1 -0
  86. package/dist/esm/executor/executorUtils.d.ts +161 -0
  87. package/dist/esm/executor/executorUtils.js +898 -0
  88. package/dist/esm/executor/executorUtils.js.map +1 -0
  89. package/dist/esm/executor/index.d.ts +1 -0
  90. package/dist/esm/executor/index.js +2 -0
  91. package/dist/esm/executor/ops/assignment.d.ts +1 -0
  92. package/dist/esm/executor/ops/assignment.js +90 -0
  93. package/dist/esm/executor/ops/assignment.js.map +1 -0
  94. package/dist/esm/executor/ops/call.d.ts +1 -0
  95. package/dist/esm/executor/ops/call.js +162 -0
  96. package/dist/esm/executor/ops/call.js.map +1 -0
  97. package/dist/esm/executor/ops/comparison.d.ts +1 -0
  98. package/dist/esm/executor/ops/comparison.js +38 -0
  99. package/dist/esm/executor/ops/comparison.js.map +1 -0
  100. package/dist/esm/executor/ops/control.d.ts +1 -0
  101. package/dist/esm/executor/ops/control.js +205 -0
  102. package/dist/esm/executor/ops/control.js.map +1 -0
  103. package/dist/esm/executor/ops/functions.d.ts +1 -0
  104. package/dist/esm/executor/ops/functions.js +57 -0
  105. package/dist/esm/executor/ops/functions.js.map +1 -0
  106. package/dist/esm/executor/ops/index.d.ts +0 -0
  107. package/dist/esm/executor/ops/index.js +11 -0
  108. package/dist/esm/executor/ops/literals.d.ts +1 -0
  109. package/dist/esm/executor/ops/literals.js +47 -0
  110. package/dist/esm/executor/ops/literals.js.map +1 -0
  111. package/dist/esm/executor/ops/misc.d.ts +1 -0
  112. package/dist/esm/executor/ops/misc.js +27 -0
  113. package/dist/esm/executor/ops/misc.js.map +1 -0
  114. package/dist/esm/executor/ops/object.d.ts +1 -0
  115. package/dist/esm/executor/ops/object.js +53 -0
  116. package/dist/esm/executor/ops/object.js.map +1 -0
  117. package/dist/esm/executor/ops/prop.d.ts +1 -0
  118. package/dist/esm/executor/ops/prop.js +72 -0
  119. package/dist/esm/executor/ops/prop.js.map +1 -0
  120. package/dist/esm/executor/ops/unary.d.ts +1 -0
  121. package/dist/esm/executor/ops/unary.js +50 -0
  122. package/dist/esm/executor/ops/unary.js.map +1 -0
  123. package/dist/esm/executor/ops/variables.d.ts +1 -0
  124. package/dist/esm/executor/ops/variables.js +18 -0
  125. package/dist/esm/executor/ops/variables.js.map +1 -0
  126. package/dist/esm/executor/opsRegistry.d.ts +33 -0
  127. package/dist/esm/executor/opsRegistry.js +9 -0
  128. package/dist/esm/executor/opsRegistry.js.map +1 -0
  129. package/dist/esm/package.json +3 -0
  130. package/dist/esm/parser/index.d.ts +3 -0
  131. package/dist/esm/parser/index.js +1 -0
  132. package/{build/parser.d.ts → dist/esm/parser/lisp.d.ts} +29 -42
  133. package/dist/esm/parser/lispTypes/conditionals.d.ts +2 -0
  134. package/dist/esm/parser/lispTypes/conditionals.js +137 -0
  135. package/dist/esm/parser/lispTypes/conditionals.js.map +1 -0
  136. package/dist/esm/parser/lispTypes/control.d.ts +2 -0
  137. package/dist/esm/parser/lispTypes/control.js +206 -0
  138. package/dist/esm/parser/lispTypes/control.js.map +1 -0
  139. package/dist/esm/parser/lispTypes/declarations.d.ts +2 -0
  140. package/dist/esm/parser/lispTypes/declarations.js +101 -0
  141. package/dist/esm/parser/lispTypes/declarations.js.map +1 -0
  142. package/dist/esm/parser/lispTypes/index.d.ts +3 -0
  143. package/dist/esm/parser/lispTypes/index.js +19 -0
  144. package/dist/esm/parser/lispTypes/index.js.map +1 -0
  145. package/dist/esm/parser/lispTypes/operators.d.ts +2 -0
  146. package/dist/esm/parser/lispTypes/operators.js +254 -0
  147. package/dist/esm/parser/lispTypes/operators.js.map +1 -0
  148. package/dist/esm/parser/lispTypes/shared.d.ts +38 -0
  149. package/dist/esm/parser/lispTypes/structures.d.ts +2 -0
  150. package/dist/esm/parser/lispTypes/structures.js +190 -0
  151. package/dist/esm/parser/lispTypes/structures.js.map +1 -0
  152. package/dist/esm/parser/lispTypes/values.d.ts +2 -0
  153. package/dist/esm/parser/lispTypes/values.js +91 -0
  154. package/dist/esm/parser/lispTypes/values.js.map +1 -0
  155. package/dist/esm/parser/parserUtils.d.ts +34 -0
  156. package/dist/esm/parser/parserUtils.js +959 -0
  157. package/dist/esm/parser/parserUtils.js.map +1 -0
  158. package/dist/esm/utils/CodeString.d.ts +16 -0
  159. package/dist/esm/utils/CodeString.js +66 -0
  160. package/dist/esm/utils/CodeString.js.map +1 -0
  161. package/dist/esm/utils/ExecContext.d.ts +34 -0
  162. package/dist/esm/utils/ExecContext.js +168 -0
  163. package/dist/esm/utils/ExecContext.js.map +1 -0
  164. package/dist/esm/utils/Prop.d.ts +16 -0
  165. package/dist/esm/utils/Prop.js +80 -0
  166. package/dist/esm/utils/Prop.js.map +1 -0
  167. package/dist/esm/utils/Scope.d.ts +47 -0
  168. package/dist/esm/utils/Scope.js +122 -0
  169. package/dist/esm/utils/Scope.js.map +1 -0
  170. package/dist/esm/utils/errors.d.ts +10 -0
  171. package/dist/esm/utils/errors.js +10 -0
  172. package/dist/esm/utils/errors.js.map +1 -0
  173. package/dist/esm/utils/functionReplacements.d.ts +11 -0
  174. package/dist/esm/utils/functionReplacements.js +361 -0
  175. package/dist/esm/utils/functionReplacements.js.map +1 -0
  176. package/dist/esm/utils/index.d.ts +7 -0
  177. package/dist/esm/utils/index.js +7 -0
  178. package/dist/esm/utils/types.d.ts +221 -0
  179. package/dist/esm/utils/types.js +160 -0
  180. package/dist/esm/utils/types.js.map +1 -0
  181. package/dist/esm/utils/unraw.js +147 -0
  182. package/dist/esm/utils/unraw.js.map +1 -0
  183. package/dist/umd/Sandbox.min.js +2 -0
  184. package/dist/umd/Sandbox.min.js.map +1 -0
  185. package/dist/umd/SandboxExec.min.js +2 -0
  186. package/dist/umd/SandboxExec.min.js.map +1 -0
  187. package/package.json +17 -16
  188. package/build/Sandbox.js +0 -62
  189. package/build/SandboxExec.js +0 -214
  190. package/build/eval.js +0 -205
  191. package/build/executor.d.ts +0 -124
  192. package/build/executor.js +0 -1554
  193. package/build/parser.js +0 -1527
  194. package/build/unraw.js +0 -168
  195. package/build/utils.d.ts +0 -264
  196. package/build/utils.js +0 -362
  197. package/dist/Sandbox.d.ts +0 -25
  198. package/dist/Sandbox.js +0 -270
  199. package/dist/Sandbox.js.map +0 -1
  200. package/dist/Sandbox.min.js +0 -2
  201. package/dist/Sandbox.min.js.map +0 -1
  202. package/dist/SandboxExec.js +0 -218
  203. package/dist/SandboxExec.js.map +0 -1
  204. package/dist/SandboxExec.min.js +0 -2
  205. package/dist/SandboxExec.min.js.map +0 -1
  206. package/dist/executor.d.ts +0 -124
  207. package/dist/executor.js +0 -1558
  208. package/dist/executor.js.map +0 -1
  209. package/dist/node/Sandbox.js +0 -277
  210. package/dist/node/SandboxExec.d.ts +0 -66
  211. package/dist/node/SandboxExec.js +0 -225
  212. package/dist/node/eval.d.ts +0 -27
  213. package/dist/node/executor.d.ts +0 -124
  214. package/dist/node/executor.js +0 -1575
  215. package/dist/node/parser.d.ts +0 -154
  216. package/dist/node/parser.js +0 -1704
  217. package/dist/node/utils.d.ts +0 -264
  218. package/dist/node/utils.js +0 -385
  219. package/dist/parser.js +0 -1690
  220. package/dist/parser.js.map +0 -1
  221. package/dist/unraw.d.ts +0 -11
  222. package/dist/utils.d.ts +0 -264
  223. package/dist/utils.js +0 -365
  224. package/dist/utils.js.map +0 -1
  225. /package/{build → dist/cjs/utils}/unraw.d.ts +0 -0
  226. /package/dist/{node → esm/utils}/unraw.d.ts +0 -0
@@ -0,0 +1,16 @@
1
+ export declare class CodeString {
2
+ start: number;
3
+ end: number;
4
+ ref: {
5
+ str: string;
6
+ };
7
+ constructor(str: string | CodeString);
8
+ substring(start: number, end?: number): CodeString;
9
+ get length(): number;
10
+ char(i: number): string | undefined;
11
+ toString(): string;
12
+ trimStart(): CodeString;
13
+ slice(start: number, end?: number): CodeString;
14
+ trim(): CodeString;
15
+ valueOf(): string;
16
+ }
@@ -0,0 +1,64 @@
1
+ //#region src/utils/CodeString.ts
2
+ var CodeString = class CodeString {
3
+ constructor(str) {
4
+ this.ref = { str: "" };
5
+ if (str instanceof CodeString) {
6
+ this.ref = str.ref;
7
+ this.start = str.start;
8
+ this.end = str.end;
9
+ } else {
10
+ this.ref.str = str;
11
+ this.start = 0;
12
+ this.end = str.length;
13
+ }
14
+ }
15
+ substring(start, end) {
16
+ if (!this.length) return this;
17
+ start = this.start + start;
18
+ if (start < 0) start = 0;
19
+ if (start > this.end) start = this.end;
20
+ end = end === void 0 ? this.end : this.start + end;
21
+ if (end < 0) end = 0;
22
+ if (end > this.end) end = this.end;
23
+ const code = new CodeString(this);
24
+ code.start = start;
25
+ code.end = end;
26
+ return code;
27
+ }
28
+ get length() {
29
+ const len = this.end - this.start;
30
+ return len < 0 ? 0 : len;
31
+ }
32
+ char(i) {
33
+ if (this.start === this.end) return void 0;
34
+ return this.ref.str[this.start + i];
35
+ }
36
+ toString() {
37
+ return this.ref.str.substring(this.start, this.end);
38
+ }
39
+ trimStart() {
40
+ const found = /^\s+/.exec(this.toString());
41
+ const code = new CodeString(this);
42
+ if (found) code.start += found[0].length;
43
+ return code;
44
+ }
45
+ slice(start, end) {
46
+ if (start < 0) start = this.end - this.start + start;
47
+ if (start < 0) start = 0;
48
+ if (end === void 0) end = this.end - this.start;
49
+ if (end < 0) end = this.end - this.start + end;
50
+ if (end < 0) end = 0;
51
+ return this.substring(start, end);
52
+ }
53
+ trim() {
54
+ const code = this.trimStart();
55
+ const found = /\s+$/.exec(code.toString());
56
+ if (found) code.end -= found[0].length;
57
+ return code;
58
+ }
59
+ valueOf() {
60
+ return this.toString();
61
+ }
62
+ };
63
+ //#endregion
64
+ exports.CodeString = CodeString;
@@ -0,0 +1,34 @@
1
+ import { IEvalContext } from '../eval';
2
+ import { Change } from '../executor';
3
+ import { IConstants, IExecutionTree, Lisp, LispItem } from '../parser';
4
+ import { default as SandboxExec } from '../SandboxExec';
5
+ import { IContext, IExecContext, IOptions, ISandboxGlobal, SandboxSymbolContext, SubscriptionSubject } from './types';
6
+ export declare class ExecContext implements IExecContext {
7
+ ctx: IContext;
8
+ constants: IConstants;
9
+ tree: Lisp[];
10
+ getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>;
11
+ setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
12
+ changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
13
+ setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
14
+ changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
15
+ evals: Map<any, any>;
16
+ registerSandboxFunction: (fn: (...args: any[]) => any) => void;
17
+ allowJit: boolean;
18
+ evalContext?: IEvalContext | undefined;
19
+ constructor(ctx: IContext, constants: IConstants, tree: Lisp[], getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>, setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>, changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>, setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>, changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>, evals: Map<any, any>, registerSandboxFunction: (fn: (...args: any[]) => any) => void, allowJit: boolean, evalContext?: IEvalContext | undefined);
20
+ }
21
+ export declare function getSandboxSymbolCtor(symbols: SandboxSymbolContext): Function;
22
+ interface SandboxGlobalConstructor {
23
+ new (): ISandboxGlobal;
24
+ }
25
+ export declare function sandboxedGlobal(globals: ISandboxGlobal): SandboxGlobalConstructor;
26
+ export declare function createContext(sandbox: SandboxExec, options: IOptions): IContext;
27
+ export declare function createExecContext(sandbox: {
28
+ readonly setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
29
+ readonly changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
30
+ readonly sandboxFunctions: WeakMap<(...args: any[]) => any, IExecContext>;
31
+ readonly context: IContext;
32
+ }, executionTree: IExecutionTree, evalContext?: IEvalContext): IExecContext;
33
+ export declare function isLisp<Type extends Lisp = Lisp>(item: LispItem | LispItem): item is Type;
34
+ export {};
@@ -0,0 +1,171 @@
1
+ const require_types = require("./types.js");
2
+ const require_functionReplacements = require("./functionReplacements.js");
3
+ const require_Prop = require("./Prop.js");
4
+ const require_Scope = require("./Scope.js");
5
+ //#region src/utils/ExecContext.ts
6
+ var ExecContext = class {
7
+ constructor(ctx, constants, tree, getSubscriptions, setSubscriptions, changeSubscriptions, setSubscriptionsGlobal, changeSubscriptionsGlobal, evals, registerSandboxFunction, allowJit, evalContext) {
8
+ this.ctx = ctx;
9
+ this.constants = constants;
10
+ this.tree = tree;
11
+ this.getSubscriptions = getSubscriptions;
12
+ this.setSubscriptions = setSubscriptions;
13
+ this.changeSubscriptions = changeSubscriptions;
14
+ this.setSubscriptionsGlobal = setSubscriptionsGlobal;
15
+ this.changeSubscriptionsGlobal = changeSubscriptionsGlobal;
16
+ this.evals = evals;
17
+ this.registerSandboxFunction = registerSandboxFunction;
18
+ this.allowJit = allowJit;
19
+ this.evalContext = evalContext;
20
+ }
21
+ };
22
+ function createSandboxSymbolContext(symbolWhitelist) {
23
+ return {
24
+ registry: /* @__PURE__ */ new Map(),
25
+ reverseRegistry: /* @__PURE__ */ new Map(),
26
+ whitelist: { ...symbolWhitelist }
27
+ };
28
+ }
29
+ var RESERVED_SYMBOL_PROPERTIES = new Set([
30
+ "length",
31
+ "name",
32
+ "prototype",
33
+ "for",
34
+ "keyFor"
35
+ ]);
36
+ function copyWhitelistedSymbols(target, symbolWhitelist) {
37
+ for (const [key, value] of Object.entries(symbolWhitelist)) {
38
+ if (RESERVED_SYMBOL_PROPERTIES.has(key)) continue;
39
+ const descriptor = Object.getOwnPropertyDescriptor(Symbol, key);
40
+ if (descriptor) Object.defineProperty(target, key, descriptor);
41
+ }
42
+ }
43
+ function getSandboxSymbolCtor(symbols) {
44
+ if (symbols.ctor) return symbols.ctor;
45
+ function SandboxSymbol(description) {
46
+ if (new.target) throw new TypeError("Symbol is not a constructor");
47
+ return Symbol(description === void 0 ? void 0 : String(description));
48
+ }
49
+ copyWhitelistedSymbols(SandboxSymbol, symbols.whitelist);
50
+ Object.defineProperties(SandboxSymbol, {
51
+ prototype: {
52
+ value: Symbol.prototype,
53
+ enumerable: false,
54
+ configurable: false,
55
+ writable: false
56
+ },
57
+ for: {
58
+ value(key) {
59
+ const stringKey = String(key);
60
+ let symbol = symbols.registry.get(stringKey);
61
+ if (!symbol) {
62
+ symbol = Symbol(stringKey);
63
+ symbols.registry.set(stringKey, symbol);
64
+ symbols.reverseRegistry.set(symbol, stringKey);
65
+ }
66
+ return symbol;
67
+ },
68
+ enumerable: false,
69
+ configurable: true,
70
+ writable: true
71
+ },
72
+ keyFor: {
73
+ value(symbol) {
74
+ return typeof symbol === "symbol" ? symbols.reverseRegistry.get(symbol) : void 0;
75
+ },
76
+ enumerable: false,
77
+ configurable: true,
78
+ writable: true
79
+ }
80
+ });
81
+ symbols.ctor = SandboxSymbol;
82
+ return SandboxSymbol;
83
+ }
84
+ function SandboxGlobal() {}
85
+ function sandboxedGlobal(globals) {
86
+ SG.prototype = SandboxGlobal.prototype;
87
+ return SG;
88
+ function SG() {
89
+ for (const i in globals) this[i] = globals[i];
90
+ }
91
+ }
92
+ function createContext(sandbox, options) {
93
+ const sandboxSymbols = createSandboxSymbolContext(options.symbolWhitelist);
94
+ const sandboxGlobal = new (sandboxedGlobal(options.globals))();
95
+ const context = {
96
+ sandbox,
97
+ globalsWhitelist: new Set(Object.values(options.globals)),
98
+ prototypeWhitelist: new Map([...options.prototypeWhitelist].map((a) => [a[0].prototype, a[1]])),
99
+ sandboxSymbols,
100
+ options,
101
+ globalScope: new require_Scope.Scope(null, sandboxGlobal, sandboxGlobal),
102
+ sandboxGlobal,
103
+ ticks: {
104
+ ticks: 0n,
105
+ tickLimit: options.executionQuota,
106
+ nextYield: options.nonBlocking ? require_types.NON_BLOCKING_THRESHOLD : void 0
107
+ },
108
+ sandboxedFunctions: /* @__PURE__ */ new WeakSet()
109
+ };
110
+ context.prototypeWhitelist.set(Object.getPrototypeOf(sandboxGlobal), /* @__PURE__ */ new Set());
111
+ context.prototypeWhitelist.set(Object.getPrototypeOf([][Symbol.iterator]()), /* @__PURE__ */ new Set());
112
+ const genProto = Object.getPrototypeOf((function* () {})());
113
+ context.prototypeWhitelist.set(Object.getPrototypeOf(genProto), /* @__PURE__ */ new Set());
114
+ const asyncGenProto = Object.getPrototypeOf((async function* () {})());
115
+ context.prototypeWhitelist.set(Object.getPrototypeOf(asyncGenProto), /* @__PURE__ */ new Set());
116
+ if (typeof Response !== "undefined") context.prototypeWhitelist.set(Response.prototype, /* @__PURE__ */ new Set());
117
+ if (typeof Request !== "undefined") context.prototypeWhitelist.set(Request.prototype, /* @__PURE__ */ new Set());
118
+ if (typeof Headers !== "undefined") context.prototypeWhitelist.set(Headers.prototype, /* @__PURE__ */ new Set());
119
+ if (typeof FormData !== "undefined") context.prototypeWhitelist.set(FormData.prototype, /* @__PURE__ */ new Set());
120
+ if (typeof Blob !== "undefined") context.prototypeWhitelist.set(Blob.prototype, /* @__PURE__ */ new Set());
121
+ if (typeof URLSearchParams !== "undefined") context.prototypeWhitelist.set(URLSearchParams.prototype, /* @__PURE__ */ new Set());
122
+ if (typeof AbortController !== "undefined") context.prototypeWhitelist.set(AbortController.prototype, /* @__PURE__ */ new Set());
123
+ if (typeof ReadableStream !== "undefined") context.prototypeWhitelist.set(ReadableStream.prototype, /* @__PURE__ */ new Set());
124
+ if (typeof TransformStream !== "undefined") context.prototypeWhitelist.set(TransformStream.prototype, /* @__PURE__ */ new Set());
125
+ if (typeof WritableStream !== "undefined") context.prototypeWhitelist.set(WritableStream.prototype, /* @__PURE__ */ new Set());
126
+ if (typeof TextEncoder !== "undefined") context.prototypeWhitelist.set(TextEncoder.prototype, /* @__PURE__ */ new Set());
127
+ if (typeof TextDecoder !== "undefined") context.prototypeWhitelist.set(TextDecoder.prototype, /* @__PURE__ */ new Set());
128
+ return context;
129
+ }
130
+ function createExecContext(sandbox, executionTree, evalContext) {
131
+ const evals = /* @__PURE__ */ new Map();
132
+ const execContext = new ExecContext(sandbox.context, executionTree.constants, executionTree.tree, /* @__PURE__ */ new Set(), /* @__PURE__ */ new WeakMap(), /* @__PURE__ */ new WeakMap(), sandbox.setSubscriptions, sandbox.changeSubscriptions, evals, (fn) => sandbox.sandboxFunctions.set(fn, execContext), !!evalContext, evalContext);
133
+ if (evalContext) {
134
+ const func = evalContext.sandboxFunction(execContext);
135
+ const asyncFunc = evalContext.sandboxAsyncFunction(execContext);
136
+ const genFunc = evalContext.sandboxGeneratorFunction(execContext);
137
+ const asyncGenFunc = evalContext.sandboxAsyncGeneratorFunction(execContext);
138
+ const sandboxSymbol = evalContext.sandboxedSymbol(execContext);
139
+ evals.set(Function, func);
140
+ evals.set(require_types.AsyncFunction, asyncFunc);
141
+ evals.set(require_types.GeneratorFunction, genFunc);
142
+ evals.set(require_types.AsyncGeneratorFunction, asyncGenFunc);
143
+ evals.set(Symbol, sandboxSymbol);
144
+ evals.set(eval, evalContext.sandboxedEval(func, execContext));
145
+ evals.set(setTimeout, evalContext.sandboxedSetTimeout(func, execContext));
146
+ evals.set(setInterval, evalContext.sandboxedSetInterval(func, execContext));
147
+ evals.set(clearTimeout, evalContext.sandboxedClearTimeout(execContext));
148
+ evals.set(clearInterval, evalContext.sandboxedClearInterval(execContext));
149
+ for (const [original, factory] of require_functionReplacements.DEFAULT_FUNCTION_REPLACEMENTS) evals.set(original, factory(execContext));
150
+ for (const [original, factory] of sandbox.context.options.functionReplacements) evals.set(original, factory(execContext, evals.get(original)));
151
+ const ptwl = sandbox.context.prototypeWhitelist;
152
+ for (const [key, value] of evals) {
153
+ if (!ptwl.has(key.prototype)) ptwl.set(key.prototype, /* @__PURE__ */ new Set());
154
+ if (!ptwl.has(value.prototype)) ptwl.set(value.prototype, ptwl.get(key.prototype) || /* @__PURE__ */ new Set());
155
+ if (sandbox.context.globalsWhitelist.has(key)) sandbox.context.globalsWhitelist.add(value);
156
+ if (require_Prop.hasOwnProperty(sandbox.context.sandboxGlobal, key.name)) sandbox.context.sandboxGlobal[key.name] = value;
157
+ }
158
+ if (sandbox.context.sandboxGlobal.globalThis) sandbox.context.sandboxGlobal.globalThis = sandbox.context.sandboxGlobal;
159
+ }
160
+ return execContext;
161
+ }
162
+ function isLisp(item) {
163
+ return Array.isArray(item) && typeof item[0] === "number" && item[0] !== require_types.LispType.None && item[0] !== require_types.LispType.True;
164
+ }
165
+ //#endregion
166
+ exports.ExecContext = ExecContext;
167
+ exports.createContext = createContext;
168
+ exports.createExecContext = createExecContext;
169
+ exports.getSandboxSymbolCtor = getSandboxSymbolCtor;
170
+ exports.isLisp = isLisp;
171
+ exports.sandboxedGlobal = sandboxedGlobal;
@@ -0,0 +1,16 @@
1
+ import { IExecContext } from './types';
2
+ export declare class Prop<T = unknown> {
3
+ context: T;
4
+ prop: PropertyKey;
5
+ isConst: boolean;
6
+ isGlobal: boolean;
7
+ isVariable: boolean;
8
+ isInternal: boolean;
9
+ constructor(context: T, prop: PropertyKey, isConst?: boolean, isGlobal?: boolean, isVariable?: boolean, isInternal?: boolean);
10
+ get<T = unknown>(context: IExecContext): T;
11
+ }
12
+ export declare function hasOwnProperty(obj: unknown, prop: PropertyKey): boolean;
13
+ export declare function getReplacementReceiver(fn: Function): object | undefined;
14
+ export declare function resolveSandboxProp(val: unknown, context: IExecContext, prop?: Prop): Prop<{
15
+ [x: string]: unknown;
16
+ }> | undefined;
@@ -0,0 +1,81 @@
1
+ const require_functionReplacements = require("./functionReplacements.js");
2
+ //#region src/utils/Prop.ts
3
+ var boundFunctionCache = /* @__PURE__ */ new WeakMap();
4
+ var replacementReceiver = /* @__PURE__ */ new WeakMap();
5
+ var Prop = class {
6
+ constructor(context, prop, isConst = false, isGlobal = false, isVariable = false, isInternal = false) {
7
+ this.context = context;
8
+ this.prop = prop;
9
+ this.isConst = isConst;
10
+ this.isGlobal = isGlobal;
11
+ this.isVariable = isVariable;
12
+ this.isInternal = isInternal;
13
+ }
14
+ get(context) {
15
+ const ctx = this.context;
16
+ if (ctx === void 0) throw new ReferenceError(`${this.prop.toString()} is not defined`);
17
+ if (ctx === null) throw new TypeError(`Cannot read properties of null, (reading '${this.prop.toString()}')`);
18
+ context.getSubscriptions.forEach((cb) => cb(ctx, this.prop.toString()));
19
+ const val = ctx[this.prop];
20
+ return getReplacementValue(val, context, ctx);
21
+ }
22
+ };
23
+ function hasOwnProperty(obj, prop) {
24
+ return Object.prototype.hasOwnProperty.call(obj, prop);
25
+ }
26
+ function getReplacementReceiver(fn) {
27
+ return replacementReceiver.get(fn);
28
+ }
29
+ function resolveSandboxProp(val, context, prop) {
30
+ if (!val) return;
31
+ if (val instanceof Prop) {
32
+ if (!prop) prop = val;
33
+ val = val.get(context);
34
+ }
35
+ const p = prop?.prop || "prop";
36
+ if (val === globalThis) return new Prop({ [p]: context.ctx.sandboxGlobal }, p, prop?.isConst || false, false, prop?.isVariable || false);
37
+ if (prop && !prop.isVariable) return;
38
+ const replacement = getReplacementValue(val, context, prop?.context);
39
+ if (replacement !== val) return new Prop({ [p]: replacement }, p, prop?.isConst || false, prop?.isGlobal || false, prop?.isVariable || false);
40
+ }
41
+ function getReplacementValue(val, context, bindContext) {
42
+ if (typeof val !== "function") return val;
43
+ const replacement = context.evals.get(val);
44
+ if (replacement === void 0) return val;
45
+ if (!shouldBindReplacement(val, bindContext, context)) return replacement;
46
+ return bindReplacement(replacement, bindContext, val, context);
47
+ }
48
+ function shouldBindReplacement(original, bindContext, context) {
49
+ return require_functionReplacements.THIS_DEPENDENT_FUNCTION_REPLACEMENTS.has(original) && bindContext !== null && (typeof bindContext === "object" || typeof bindContext === "function") && bindContext !== context.ctx.sandboxGlobal && !context.ctx.globalsWhitelist.has(bindContext);
50
+ }
51
+ function bindReplacement(replacement, bindContext, original, context) {
52
+ let cache = boundFunctionCache.get(replacement);
53
+ if (!cache) {
54
+ cache = /* @__PURE__ */ new WeakMap();
55
+ boundFunctionCache.set(replacement, cache);
56
+ }
57
+ let bound = cache.get(bindContext);
58
+ if (bound) return bound;
59
+ bound = function(...args) {
60
+ return replacement.apply(this, args);
61
+ };
62
+ redefineFunctionMetadata(bound, original);
63
+ context.ctx.sandboxedFunctions.add(bound);
64
+ replacementReceiver.set(bound, bindContext);
65
+ cache.set(bindContext, bound);
66
+ return bound;
67
+ }
68
+ function redefineFunctionMetadata(target, source, overrides = {}) {
69
+ for (const key of ["name", "length"]) {
70
+ const descriptor = Object.getOwnPropertyDescriptor(source, key);
71
+ if (descriptor?.configurable) Object.defineProperty(target, key, {
72
+ ...descriptor,
73
+ value: overrides[key] ?? source[key]
74
+ });
75
+ }
76
+ }
77
+ //#endregion
78
+ exports.Prop = Prop;
79
+ exports.getReplacementReceiver = getReplacementReceiver;
80
+ exports.hasOwnProperty = hasOwnProperty;
81
+ exports.resolveSandboxProp = resolveSandboxProp;
@@ -0,0 +1,47 @@
1
+ import { VarType, IExecContext, IScope } from './types';
2
+ import { Prop } from './Prop';
3
+ export type Unknown = undefined | null | Record<string | number, unknown>;
4
+ export declare class Scope {
5
+ parent: Scope | null;
6
+ const: {
7
+ [key: string]: true;
8
+ };
9
+ let: {
10
+ [key: string]: true;
11
+ };
12
+ var: {
13
+ [key: string]: true;
14
+ };
15
+ internal: {
16
+ [key: string]: true;
17
+ };
18
+ globals: {
19
+ [key: string]: true;
20
+ };
21
+ allVars: {
22
+ [key: string]: unknown;
23
+ } & object;
24
+ internalVars: {
25
+ [key: string]: unknown;
26
+ };
27
+ functionThis?: Unknown;
28
+ constructor(parent: Scope | null, vars?: {}, functionThis?: Unknown);
29
+ get(key: string, internal: boolean): Prop;
30
+ set(key: string, val: unknown, internal: boolean): Prop<unknown>;
31
+ getWhereValScope(key: string, isThis: boolean, internal: boolean): Scope | null;
32
+ getWhereVarScope(key: string, localScope: boolean, internal: boolean): Scope;
33
+ declare(key: string, type: VarType, value: unknown, isGlobal: boolean, internal: boolean): Prop;
34
+ }
35
+ export declare class FunctionScope implements IScope {
36
+ }
37
+ export declare class LocalScope implements IScope {
38
+ }
39
+ export declare const optional: {};
40
+ export declare class DelayedSynchronousResult {
41
+ readonly result: unknown;
42
+ constructor(cb: () => unknown);
43
+ }
44
+ export declare function delaySynchronousResult(cb: () => Promise<unknown>): DelayedSynchronousResult;
45
+ export declare function sanitizeProp(value: unknown, context: IExecContext, cache?: WeakSet<object>): unknown;
46
+ export declare function sanitizeScope(scope: IScope, context: IExecContext, cache?: WeakSet<object>): void;
47
+ export declare function sanitizeScopes(scopes: IScope[], context: IExecContext, cache?: WeakSet<object>): void;
@@ -0,0 +1,127 @@
1
+ const require_errors = require("./errors.js");
2
+ const require_types = require("./types.js");
3
+ const require_Prop = require("./Prop.js");
4
+ //#region src/utils/Scope.ts
5
+ function keysOnly(obj) {
6
+ const ret = Object.assign({}, obj);
7
+ for (const key in ret) ret[key] = true;
8
+ return ret;
9
+ }
10
+ var Scope = class {
11
+ constructor(parent, vars = {}, functionThis) {
12
+ this.const = {};
13
+ this.let = {};
14
+ this.var = {};
15
+ this.internal = {};
16
+ this.internalVars = {};
17
+ const isFuncScope = functionThis !== void 0 || parent === null;
18
+ this.parent = parent;
19
+ this.allVars = vars;
20
+ this.let = isFuncScope ? this.let : keysOnly(vars);
21
+ this.var = isFuncScope ? keysOnly(vars) : this.var;
22
+ this.globals = parent === null ? keysOnly(vars) : {};
23
+ this.functionThis = functionThis;
24
+ }
25
+ get(key, internal) {
26
+ const isThis = key === "this";
27
+ const scope = this.getWhereValScope(key, isThis, internal);
28
+ if (scope && isThis) return new require_Prop.Prop({ this: scope.functionThis }, key, false, false, true);
29
+ if (!scope) return new require_Prop.Prop(void 0, key);
30
+ if (internal && scope.internalVars[key]) return new require_Prop.Prop(scope.internalVars, key, false, false, true, true);
31
+ return new require_Prop.Prop(scope.allVars, key, require_Prop.hasOwnProperty(scope.const, key), require_Prop.hasOwnProperty(scope.globals, key), true);
32
+ }
33
+ set(key, val, internal) {
34
+ if (key === "this") throw new SyntaxError("\"this\" cannot be assigned");
35
+ if (require_types.reservedWords.has(key)) throw new SyntaxError("Unexepected token '" + key + "'");
36
+ const prop = this.get(key, internal);
37
+ if (prop.context === void 0) throw new ReferenceError(`Variable '${key}' was not declared.`);
38
+ if (prop.context === null) throw new TypeError(`Cannot set properties of null, (setting '${key}')`);
39
+ if (prop.isConst) throw new TypeError(`Assignment to constant variable`);
40
+ if (prop.isGlobal) throw new require_errors.SandboxError(`Cannot override global variable '${key}'`);
41
+ prop.context[prop.prop] = val;
42
+ return prop;
43
+ }
44
+ getWhereValScope(key, isThis, internal) {
45
+ let scope = this;
46
+ if (isThis) {
47
+ do {
48
+ if (scope.functionThis !== void 0) return scope;
49
+ scope = scope.parent;
50
+ } while (scope !== null);
51
+ return null;
52
+ }
53
+ do {
54
+ if (internal && key in scope.internalVars && !(key in {} && !require_Prop.hasOwnProperty(scope.internalVars, key))) return scope;
55
+ if (key in scope.allVars && !(key in {} && !require_Prop.hasOwnProperty(scope.allVars, key))) return scope;
56
+ scope = scope.parent;
57
+ } while (scope !== null);
58
+ return null;
59
+ }
60
+ getWhereVarScope(key, localScope, internal) {
61
+ let scope = this;
62
+ do {
63
+ if (internal && key in scope.internalVars && !(key in {} && !require_Prop.hasOwnProperty(scope.internalVars, key))) return scope;
64
+ if (key in scope.allVars && !(key in {} && !require_Prop.hasOwnProperty(scope.allVars, key))) return scope;
65
+ if (scope.parent === null || localScope || scope.functionThis !== void 0) return scope;
66
+ scope = scope.parent;
67
+ } while (scope !== null);
68
+ return scope;
69
+ }
70
+ declare(key, type, value, isGlobal, internal) {
71
+ if (key === "this") throw new SyntaxError("\"this\" cannot be declared");
72
+ if (require_types.reservedWords.has(key)) throw new SyntaxError("Unexepected token '" + key + "'");
73
+ const existingScope = this.getWhereVarScope(key, type !== require_types.VarType.var, internal);
74
+ if (type === require_types.VarType.var) {
75
+ if (existingScope.var[key]) {
76
+ existingScope.allVars[key] = value;
77
+ if (!isGlobal) delete existingScope.globals[key];
78
+ else existingScope.globals[key] = true;
79
+ return new require_Prop.Prop(existingScope.allVars, key, false, existingScope.globals[key], true);
80
+ } else if (key in existingScope.allVars) throw new SyntaxError(`Identifier '${key}' has already been declared`);
81
+ }
82
+ if (key in existingScope.allVars || key in existingScope.internalVars) throw new SyntaxError(`Identifier '${key}' has already been declared`);
83
+ if (isGlobal) existingScope.globals[key] = true;
84
+ existingScope[type][key] = true;
85
+ if (type === require_types.VarType.internal) existingScope.internalVars[key] = value;
86
+ else existingScope.allVars[key] = value;
87
+ return new require_Prop.Prop(type === require_types.VarType.internal ? this.internalVars : this.allVars, key, type === require_types.VarType.const, isGlobal, true, type === require_types.VarType.internal);
88
+ }
89
+ };
90
+ var LocalScope = class {};
91
+ var optional = {};
92
+ var DelayedSynchronousResult = class {
93
+ constructor(cb) {
94
+ this.result = cb();
95
+ }
96
+ };
97
+ function delaySynchronousResult(cb) {
98
+ return new DelayedSynchronousResult(cb);
99
+ }
100
+ function sanitizeProp(value, context, cache = /* @__PURE__ */ new WeakSet()) {
101
+ if (value === null || typeof value !== "object" && typeof value !== "function") return value;
102
+ value = require_Prop.resolveSandboxProp(value, context) || value;
103
+ if (value instanceof require_Prop.Prop) value = value.get(context);
104
+ if (value === optional) return;
105
+ return value;
106
+ }
107
+ function sanitizeScope(scope, context, cache = /* @__PURE__ */ new WeakSet()) {
108
+ if (cache.has(scope)) return;
109
+ cache.add(scope);
110
+ for (const key in scope) {
111
+ const val = scope[key];
112
+ if (val !== null && typeof val === "object") sanitizeScope(val, context, cache);
113
+ scope[key] = sanitizeProp(val, context);
114
+ }
115
+ }
116
+ function sanitizeScopes(scopes, context, cache = /* @__PURE__ */ new WeakSet()) {
117
+ for (const scope of scopes) sanitizeScope(scope, context, cache);
118
+ }
119
+ //#endregion
120
+ exports.DelayedSynchronousResult = DelayedSynchronousResult;
121
+ exports.LocalScope = LocalScope;
122
+ exports.Scope = Scope;
123
+ exports.delaySynchronousResult = delaySynchronousResult;
124
+ exports.optional = optional;
125
+ exports.sanitizeProp = sanitizeProp;
126
+ exports.sanitizeScope = sanitizeScope;
127
+ exports.sanitizeScopes = sanitizeScopes;
@@ -0,0 +1,10 @@
1
+ export declare class SandboxError extends Error {
2
+ }
3
+ export declare class SandboxExecutionQuotaExceededError extends SandboxError {
4
+ }
5
+ export declare class SandboxExecutionTreeError extends SandboxError {
6
+ }
7
+ export declare class SandboxCapabilityError extends SandboxError {
8
+ }
9
+ export declare class SandboxAccessError extends SandboxError {
10
+ }
@@ -0,0 +1,12 @@
1
+ //#region src/utils/errors.ts
2
+ var SandboxError = class extends Error {};
3
+ var SandboxExecutionQuotaExceededError = class extends SandboxError {};
4
+ var SandboxExecutionTreeError = class extends SandboxError {};
5
+ var SandboxCapabilityError = class extends SandboxError {};
6
+ var SandboxAccessError = class extends SandboxError {};
7
+ //#endregion
8
+ exports.SandboxAccessError = SandboxAccessError;
9
+ exports.SandboxCapabilityError = SandboxCapabilityError;
10
+ exports.SandboxError = SandboxError;
11
+ exports.SandboxExecutionQuotaExceededError = SandboxExecutionQuotaExceededError;
12
+ exports.SandboxExecutionTreeError = SandboxExecutionTreeError;
@@ -0,0 +1,11 @@
1
+ import { IExecContext } from './types';
2
+ /**
3
+ * Checks if adding `expectTicks` would exceed the tick limit, and throws SandboxExecutionQuotaExceededError
4
+ * (which bypasses user try/catch) if so. Otherwise increments the tick counter.
5
+ */
6
+ export declare function checkTicksAndThrow(ctx: IExecContext, expectTicks: bigint): void;
7
+ export declare const typedArrayProtos: Set<any>;
8
+ type Factory = (ctx: IExecContext) => Function;
9
+ export declare const DEFAULT_FUNCTION_REPLACEMENTS: Map<Function, Factory>;
10
+ export declare const THIS_DEPENDENT_FUNCTION_REPLACEMENTS: Set<Function>;
11
+ export {};