@cuxt/sandboxjs 0.1.0 → 0.1.3

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 +70 -68
  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 -1546
  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 -1550
  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 -1567
  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
package/package.json CHANGED
@@ -1,68 +1,70 @@
1
- {
2
- "name": "@cuxt/sandboxjs",
3
- "version": "0.1.0",
4
- "description": "JavaScript sandboxing library with prototype whitelist and safe globals.",
5
- "main": "dist/node/Sandbox.js",
6
- "module": "build/Sandbox.js",
7
- "browser": "dist/Sandbox.min.js",
8
- "type": "module",
9
- "exports": {
10
- ".": {
11
- "import": "./build/Sandbox.js",
12
- "require": "./dist/node/Sandbox.js",
13
- "types": "./build/Sandbox.d.ts"
14
- }
15
- },
16
- "files": [
17
- "build",
18
- "dist"
19
- ],
20
- "scripts": {
21
- "test": "NODE_OPTIONS='--no-warnings=ExperimentalWarning' jest",
22
- "build": "tsc --project tsconfig.json --outDir build --declaration && rollup -c",
23
- "lint": "prettier --check \"**/*.+(ts|json)\" && eslint --ext .ts .",
24
- "lint:fix": "prettier --write \"**/*.+(ts|json)\" && eslint --ext .ts --fix .",
25
- "patch": "npm version patch",
26
- "minor": "npm version minor",
27
- "major": "npm version major"
28
- },
29
- "repository": {
30
- "type": "git",
31
- "url": "git@github.com:cuxt/SandboxJS.git"
32
- },
33
- "author": "",
34
- "license": "MIT",
35
- "bugs": {
36
- "url": "https://github.com/cuxt/SandboxJS/issues"
37
- },
38
- "homepage": "https://github.com/cuxt/SandboxJS#readme",
39
- "devDependencies": {
40
- "@rollup/plugin-node-resolve": "^16.0.3",
41
- "@rollup/plugin-terser": "^1.0.0",
42
- "@rollup/plugin-typescript": "^12.3.0",
43
- "@types/jest": "^30.0.0",
44
- "@typescript-eslint/eslint-plugin": "^8.53.0",
45
- "@typescript-eslint/parser": "^8.53.0",
46
- "eslint": "^9.39.2",
47
- "eslint-config-prettier": "^10.1.8",
48
- "husky": "^9.1.7",
49
- "jest": "^30.2.0",
50
- "lint-staged": "^16.2.7",
51
- "node-fetch": "^3.3.2",
52
- "prettier": "^3.8.0",
53
- "rollup": "^4.55.2",
54
- "rollup-plugin-bundle-stats": "^4.21.8",
55
- "ts-jest": "^29.4.6",
56
- "tslib": "^2.8.1",
57
- "typescript": "^5.9.3"
58
- },
59
- "lint-staged": {
60
- "*.ts": [
61
- "prettier --write",
62
- "eslint --fix"
63
- ],
64
- "*.json": [
65
- "prettier --write"
66
- ]
67
- }
68
- }
1
+ {
2
+ "name": "@cuxt/sandboxjs",
3
+ "version": "0.1.3",
4
+ "description": "JavaScript sandboxing library with prototype whitelist and safe globals.",
5
+ "main": "dist/node/Sandbox.js",
6
+ "module": "build/Sandbox.js",
7
+ "browser": "dist/Sandbox.min.js",
8
+ "type": "module",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./build/Sandbox.js",
12
+ "require": "./dist/node/Sandbox.js",
13
+ "types": "./build/Sandbox.d.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "build",
18
+ "dist"
19
+ ],
20
+ "scripts": {
21
+ "test": "jest",
22
+ "test:perf": "NODE_OPTIONS='--no-warnings=ExperimentalWarning' node --expose-gc test/performance.mjs",
23
+ "build": "node scripts/build.mjs",
24
+ "lint": "prettier --check \"**/*.+(ts|json)\" && eslint --ext .ts .",
25
+ "lint:fix": "prettier --write \"**/*.+(ts|json)\" && eslint --ext .ts --fix .",
26
+ "patch": "npm version patch",
27
+ "minor": "npm version minor",
28
+ "major": "npm version major"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git@github.com:cuxt/SandboxJS.git"
33
+ },
34
+ "author": "",
35
+ "license": "MIT",
36
+ "bugs": {
37
+ "url": "https://github.com/cuxt/SandboxJS/issues"
38
+ },
39
+ "homepage": "https://github.com/cuxt/SandboxJS#readme",
40
+ "devDependencies": {
41
+ "@types/jest": "^30.0.0",
42
+ "@typescript-eslint/eslint-plugin": "^8.53.0",
43
+ "@typescript-eslint/parser": "^8.53.0",
44
+ "chalk": "^5.6.2",
45
+ "cli-table3": "^0.6.5",
46
+ "eslint": "^9.39.2",
47
+ "eslint-config-prettier": "^10.1.8",
48
+ "husky": "^9.1.7",
49
+ "jest": "^30.2.0",
50
+ "lint-staged": "^16.2.7",
51
+ "node-fetch": "^3.3.2",
52
+ "prettier": "^3.8.0",
53
+ "terser": "^5.46.1",
54
+ "tinybench": "^6.0.0",
55
+ "ts-jest": "^29.4.6",
56
+ "tslib": "^2.8.1",
57
+ "typescript": "^5.9.3",
58
+ "vite": "^8.0.8",
59
+ "vite-plugin-dts": "^4.5.4"
60
+ },
61
+ "lint-staged": {
62
+ "*.ts": [
63
+ "prettier --write",
64
+ "eslint --fix"
65
+ ],
66
+ "*.json": [
67
+ "prettier --write"
68
+ ]
69
+ }
70
+ }
package/build/Sandbox.js DELETED
@@ -1,62 +0,0 @@
1
- import { createExecContext } from './utils.js';
2
- import { createEvalContext } from './eval.js';
3
- import parse from './parser.js';
4
- import SandboxExec from './SandboxExec.js';
5
- export { LocalScope, SandboxExecutionTreeError, SandboxCapabilityError, SandboxAccessError, SandboxError, } from './utils.js';
6
- export default class Sandbox extends SandboxExec {
7
- constructor(options) {
8
- super(options, createEvalContext());
9
- }
10
- static audit(code, scopes = []) {
11
- const globals = {};
12
- for (const i of Object.getOwnPropertyNames(globalThis)) {
13
- globals[i] = globalThis[i];
14
- }
15
- const sandbox = new SandboxExec({
16
- globals,
17
- audit: true,
18
- });
19
- return sandbox.executeTree(createExecContext(sandbox, parse(code, true), createEvalContext()), scopes);
20
- }
21
- static parse(code) {
22
- return parse(code);
23
- }
24
- compile(code, optimize = false) {
25
- const parsed = parse(code, optimize);
26
- const exec = (...scopes) => {
27
- const context = createExecContext(this, parsed, this.evalContext);
28
- return { context, run: () => this.executeTree(context, [...scopes]).result };
29
- };
30
- return exec;
31
- }
32
- compileAsync(code, optimize = false) {
33
- const parsed = parse(code, optimize);
34
- const exec = (...scopes) => {
35
- const context = createExecContext(this, parsed, this.evalContext);
36
- return {
37
- context,
38
- run: () => this.executeTreeAsync(context, [...scopes]).then((ret) => ret.result),
39
- };
40
- };
41
- return exec;
42
- }
43
- compileExpression(code, optimize = false) {
44
- const parsed = parse(code, optimize, true);
45
- const exec = (...scopes) => {
46
- const context = createExecContext(this, parsed, this.evalContext);
47
- return { context, run: () => this.executeTree(context, [...scopes]).result };
48
- };
49
- return exec;
50
- }
51
- compileExpressionAsync(code, optimize = false) {
52
- const parsed = parse(code, optimize, true);
53
- const exec = (...scopes) => {
54
- const context = createExecContext(this, parsed, this.evalContext);
55
- return {
56
- context,
57
- run: () => this.executeTreeAsync(context, [...scopes]).then((ret) => ret.result),
58
- };
59
- };
60
- return exec;
61
- }
62
- }
@@ -1,214 +0,0 @@
1
- import { executeTree, executeTreeAsync } from './executor.js';
2
- import { createContext, SandboxExecutionQuotaExceededError, SandboxGlobal, } from './utils.js';
3
- export { LocalScope, SandboxExecutionTreeError, SandboxCapabilityError, SandboxAccessError, SandboxError, } from './utils.js';
4
- function subscribeSet(obj, name, callback, context) {
5
- const names = context.setSubscriptions.get(obj) || new Map();
6
- context.setSubscriptions.set(obj, names);
7
- const callbacks = names.get(name) || new Set();
8
- names.set(name, callbacks);
9
- callbacks.add(callback);
10
- let changeCbs;
11
- const val = obj[name];
12
- if (val instanceof Object) {
13
- changeCbs = context.changeSubscriptions.get(val) || new Set();
14
- changeCbs.add(callback);
15
- context.changeSubscriptions.set(val, changeCbs);
16
- }
17
- return {
18
- unsubscribe: () => {
19
- callbacks.delete(callback);
20
- changeCbs?.delete(callback);
21
- },
22
- };
23
- }
24
- export default class SandboxExec {
25
- constructor(options, evalContext) {
26
- this.evalContext = evalContext;
27
- this.setSubscriptions = new WeakMap();
28
- this.changeSubscriptions = new WeakMap();
29
- this.sandboxFunctions = new WeakMap();
30
- this.haltSubscriptions = new Set();
31
- this.resumeSubscriptions = new Set();
32
- this.halted = false;
33
- this.timeoutHandleCounter = 0;
34
- this.setTimeoutHandles = new Map();
35
- this.setIntervalHandles = new Map();
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 = createContext(this, opt);
45
- }
46
- static get SAFE_GLOBALS() {
47
- return {
48
- globalThis,
49
- Function,
50
- eval,
51
- console: {
52
- debug: console.debug,
53
- error: console.error,
54
- info: console.info,
55
- log: console.log,
56
- table: console.table,
57
- warn: console.warn,
58
- },
59
- isFinite,
60
- isNaN,
61
- parseFloat,
62
- parseInt,
63
- decodeURI,
64
- decodeURIComponent,
65
- encodeURI,
66
- encodeURIComponent,
67
- escape,
68
- unescape,
69
- Boolean,
70
- Number,
71
- BigInt,
72
- String,
73
- Object,
74
- Array,
75
- Symbol,
76
- Error,
77
- EvalError,
78
- RangeError,
79
- ReferenceError,
80
- SyntaxError,
81
- TypeError,
82
- URIError,
83
- Int8Array,
84
- Uint8Array,
85
- Uint8ClampedArray,
86
- Int16Array,
87
- Uint16Array,
88
- Int32Array,
89
- Uint32Array,
90
- Float32Array,
91
- Float64Array,
92
- Map,
93
- Set,
94
- WeakMap,
95
- WeakSet,
96
- Promise,
97
- Intl,
98
- JSON,
99
- Math,
100
- Date,
101
- RegExp,
102
- };
103
- }
104
- static get SAFE_PROTOTYPES() {
105
- const protos = [
106
- SandboxGlobal,
107
- Function,
108
- Boolean,
109
- Number,
110
- BigInt,
111
- String,
112
- Date,
113
- Error,
114
- Array,
115
- Int8Array,
116
- Uint8Array,
117
- Uint8ClampedArray,
118
- Int16Array,
119
- Uint16Array,
120
- Int32Array,
121
- Uint32Array,
122
- Float32Array,
123
- Float64Array,
124
- Map,
125
- Set,
126
- WeakMap,
127
- WeakSet,
128
- Promise,
129
- Symbol,
130
- Date,
131
- RegExp,
132
- // Fetch API
133
- Response,
134
- Request,
135
- Headers,
136
- FormData,
137
- ];
138
- const map = new Map();
139
- protos.forEach((proto) => {
140
- map.set(proto, new Set());
141
- });
142
- map.set(Object, new Set([
143
- 'constructor',
144
- 'name',
145
- 'entries',
146
- 'fromEntries',
147
- 'getOwnPropertyNames',
148
- 'is',
149
- 'keys',
150
- 'hasOwnProperty',
151
- 'isPrototypeOf',
152
- 'propertyIsEnumerable',
153
- 'toLocaleString',
154
- 'toString',
155
- 'valueOf',
156
- 'values',
157
- ]));
158
- return map;
159
- }
160
- subscribeGet(callback, context) {
161
- context.getSubscriptions.add(callback);
162
- return { unsubscribe: () => context.getSubscriptions.delete(callback) };
163
- }
164
- subscribeSet(obj, name, callback, context) {
165
- return subscribeSet(obj, name, callback, context);
166
- }
167
- subscribeSetGlobal(obj, name, callback) {
168
- return subscribeSet(obj, name, callback, this);
169
- }
170
- subscribeHalt(cb) {
171
- this.haltSubscriptions.add(cb);
172
- return {
173
- unsubscribe: () => {
174
- this.haltSubscriptions.delete(cb);
175
- },
176
- };
177
- }
178
- subscribeResume(cb) {
179
- this.resumeSubscriptions.add(cb);
180
- return {
181
- unsubscribe: () => {
182
- this.resumeSubscriptions.delete(cb);
183
- },
184
- };
185
- }
186
- haltExecution(haltContext) {
187
- if (this.halted)
188
- return;
189
- this.halted = true;
190
- for (const cb of this.haltSubscriptions) {
191
- cb(haltContext);
192
- }
193
- }
194
- resumeExecution() {
195
- if (!this.halted)
196
- return;
197
- if (this.context.ticks.tickLimit && this.context.ticks.ticks >= this.context.ticks.tickLimit) {
198
- throw new SandboxExecutionQuotaExceededError('Cannot resume execution: tick limit exceeded');
199
- }
200
- this.halted = false;
201
- for (const cb of this.resumeSubscriptions) {
202
- cb();
203
- }
204
- }
205
- getContext(fn) {
206
- return this.sandboxFunctions.get(fn);
207
- }
208
- executeTree(context, scopes = []) {
209
- return executeTree(context.ctx.ticks, context, context.tree, scopes);
210
- }
211
- executeTreeAsync(context, scopes = []) {
212
- return executeTreeAsync(context.ctx.ticks, context, context.tree, scopes);
213
- }
214
- }
package/build/eval.js DELETED
@@ -1,205 +0,0 @@
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
- }