@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
package/build/utils.js DELETED
@@ -1,362 +0,0 @@
1
- // Reusable AsyncFunction constructor reference
2
- export const AsyncFunction = Object.getPrototypeOf(async function () { }).constructor;
3
- export const GeneratorFunction = Object.getPrototypeOf(function* () { }).constructor;
4
- export const AsyncGeneratorFunction = Object.getPrototypeOf(async function* () { }).constructor;
5
- export const SandboxGlobal = function SandboxGlobal(globals) {
6
- for (const i in globals) {
7
- this[i] = globals[i];
8
- }
9
- };
10
- export class ExecContext {
11
- constructor(ctx, constants, tree, getSubscriptions, setSubscriptions, changeSubscriptions, setSubscriptionsGlobal, changeSubscriptionsGlobal, evals, registerSandboxFunction, allowJit, evalContext) {
12
- this.ctx = ctx;
13
- this.constants = constants;
14
- this.tree = tree;
15
- this.getSubscriptions = getSubscriptions;
16
- this.setSubscriptions = setSubscriptions;
17
- this.changeSubscriptions = changeSubscriptions;
18
- this.setSubscriptionsGlobal = setSubscriptionsGlobal;
19
- this.changeSubscriptionsGlobal = changeSubscriptionsGlobal;
20
- this.evals = evals;
21
- this.registerSandboxFunction = registerSandboxFunction;
22
- this.allowJit = allowJit;
23
- this.evalContext = evalContext;
24
- }
25
- }
26
- export function createContext(sandbox, options) {
27
- const sandboxGlobal = new SandboxGlobal(options.globals);
28
- const context = {
29
- sandbox: sandbox,
30
- globalsWhitelist: new Set(Object.values(options.globals)),
31
- prototypeWhitelist: new Map([...options.prototypeWhitelist].map((a) => [a[0].prototype, a[1]])),
32
- options,
33
- globalScope: new Scope(null, options.globals, sandboxGlobal),
34
- sandboxGlobal,
35
- ticks: { ticks: 0n, tickLimit: options.executionQuota },
36
- sandboxedFunctions: new WeakSet(),
37
- };
38
- context.prototypeWhitelist.set(Object.getPrototypeOf([][Symbol.iterator]()), new Set());
39
- // Fetch API 构造函数本身(静态方法如 Response.json)也加白名单
40
- if (typeof Response !== 'undefined')
41
- context.prototypeWhitelist.set(Response.prototype, new Set());
42
- if (typeof Request !== 'undefined')
43
- context.prototypeWhitelist.set(Request.prototype, new Set());
44
- if (typeof Headers !== 'undefined')
45
- context.prototypeWhitelist.set(Headers.prototype, new Set());
46
- if (typeof FormData !== 'undefined')
47
- context.prototypeWhitelist.set(FormData.prototype, new Set());
48
- if (typeof Blob !== 'undefined')
49
- context.prototypeWhitelist.set(Blob.prototype, new Set());
50
- if (typeof URLSearchParams !== 'undefined')
51
- context.prototypeWhitelist.set(URLSearchParams.prototype, new Set());
52
- if (typeof AbortController !== 'undefined')
53
- context.prototypeWhitelist.set(AbortController.prototype, new Set());
54
- if (typeof ReadableStream !== 'undefined')
55
- context.prototypeWhitelist.set(ReadableStream.prototype, new Set());
56
- if (typeof TransformStream !== 'undefined')
57
- context.prototypeWhitelist.set(TransformStream.prototype, new Set());
58
- if (typeof WritableStream !== 'undefined')
59
- context.prototypeWhitelist.set(WritableStream.prototype, new Set());
60
- if (typeof TextEncoder !== 'undefined')
61
- context.prototypeWhitelist.set(TextEncoder.prototype, new Set());
62
- if (typeof TextDecoder !== 'undefined')
63
- context.prototypeWhitelist.set(TextDecoder.prototype, new Set());
64
- return context;
65
- }
66
- export function createExecContext(sandbox, executionTree, evalContext) {
67
- const evals = new Map();
68
- const execContext = new ExecContext(sandbox.context, executionTree.constants, executionTree.tree, new Set(), new WeakMap(), new WeakMap(), sandbox.setSubscriptions, sandbox.changeSubscriptions, evals, (fn) => sandbox.sandboxFunctions.set(fn, execContext), !!evalContext, evalContext);
69
- if (evalContext) {
70
- const func = evalContext.sandboxFunction(execContext);
71
- const asyncFunc = evalContext.sandboxAsyncFunction(execContext);
72
- evals.set(Function, func);
73
- evals.set(AsyncFunction, asyncFunc);
74
- evals.set(GeneratorFunction, func);
75
- evals.set(AsyncGeneratorFunction, asyncFunc);
76
- evals.set(eval, evalContext.sandboxedEval(func, execContext));
77
- evals.set(setTimeout, evalContext.sandboxedSetTimeout(func, execContext));
78
- evals.set(setInterval, evalContext.sandboxedSetInterval(func, execContext));
79
- evals.set(clearTimeout, evalContext.sandboxedClearTimeout(execContext));
80
- evals.set(clearInterval, evalContext.sandboxedClearInterval(execContext));
81
- for (const [key, value] of evals) {
82
- sandbox.context.prototypeWhitelist.set(value.prototype, new Set());
83
- sandbox.context.prototypeWhitelist.set(key.prototype, new Set());
84
- }
85
- }
86
- return execContext;
87
- }
88
- export class CodeString {
89
- constructor(str) {
90
- this.ref = { str: '' };
91
- if (str instanceof CodeString) {
92
- this.ref = str.ref;
93
- this.start = str.start;
94
- this.end = str.end;
95
- }
96
- else {
97
- this.ref.str = str;
98
- this.start = 0;
99
- this.end = str.length;
100
- }
101
- }
102
- substring(start, end) {
103
- if (!this.length)
104
- return this;
105
- start = this.start + start;
106
- if (start < 0) {
107
- start = 0;
108
- }
109
- if (start > this.end) {
110
- start = this.end;
111
- }
112
- end = end === undefined ? this.end : this.start + end;
113
- if (end < 0) {
114
- end = 0;
115
- }
116
- if (end > this.end) {
117
- end = this.end;
118
- }
119
- const code = new CodeString(this);
120
- code.start = start;
121
- code.end = end;
122
- return code;
123
- }
124
- get length() {
125
- const len = this.end - this.start;
126
- return len < 0 ? 0 : len;
127
- }
128
- char(i) {
129
- if (this.start === this.end)
130
- return undefined;
131
- return this.ref.str[this.start + i];
132
- }
133
- toString() {
134
- return this.ref.str.substring(this.start, this.end);
135
- }
136
- trimStart() {
137
- const found = /^\s+/.exec(this.toString());
138
- const code = new CodeString(this);
139
- if (found) {
140
- code.start += found[0].length;
141
- }
142
- return code;
143
- }
144
- slice(start, end) {
145
- if (start < 0) {
146
- start = this.end - this.start + start;
147
- }
148
- if (start < 0) {
149
- start = 0;
150
- }
151
- if (end === undefined) {
152
- end = this.end - this.start;
153
- }
154
- if (end < 0) {
155
- end = this.end - this.start + end;
156
- }
157
- if (end < 0) {
158
- end = 0;
159
- }
160
- return this.substring(start, end);
161
- }
162
- trim() {
163
- const code = this.trimStart();
164
- const found = /\s+$/.exec(code.toString());
165
- if (found) {
166
- code.end -= found[0].length;
167
- }
168
- return code;
169
- }
170
- valueOf() {
171
- return this.toString();
172
- }
173
- }
174
- function keysOnly(obj) {
175
- const ret = Object.assign({}, obj);
176
- for (const key in ret) {
177
- ret[key] = true;
178
- }
179
- return ret;
180
- }
181
- export const reservedWords = new Set([
182
- 'await',
183
- 'break',
184
- 'case',
185
- 'catch',
186
- 'class',
187
- 'const',
188
- 'continue',
189
- 'debugger',
190
- 'default',
191
- 'delete',
192
- 'do',
193
- 'else',
194
- 'enum',
195
- 'export',
196
- 'extends',
197
- 'false',
198
- 'finally',
199
- 'for',
200
- 'function',
201
- 'if',
202
- 'implements',
203
- 'import',
204
- 'in',
205
- 'instanceof',
206
- 'let',
207
- 'new',
208
- 'null',
209
- 'return',
210
- 'super',
211
- 'switch',
212
- 'this',
213
- 'throw',
214
- 'true',
215
- 'try',
216
- 'typeof',
217
- 'var',
218
- 'void',
219
- 'while',
220
- 'with',
221
- ]);
222
- export class Scope {
223
- constructor(parent, vars = {}, functionThis) {
224
- this.const = {};
225
- this.let = {};
226
- this.var = {};
227
- const isFuncScope = functionThis !== undefined || parent === null;
228
- this.parent = parent;
229
- this.allVars = vars;
230
- this.let = isFuncScope ? this.let : keysOnly(vars);
231
- this.var = isFuncScope ? keysOnly(vars) : this.var;
232
- this.globals = parent === null ? keysOnly(vars) : {};
233
- this.functionThis = functionThis;
234
- }
235
- get(key) {
236
- const isThis = key === 'this';
237
- const scope = this.getWhereValScope(key, isThis);
238
- if (scope && isThis) {
239
- return new Prop({ this: scope.functionThis }, key, false, false, true);
240
- }
241
- if (!scope) {
242
- return new Prop(undefined, key);
243
- }
244
- return new Prop(scope.allVars, key, key in scope.const, key in scope.globals, true);
245
- }
246
- set(key, val) {
247
- if (key === 'this')
248
- throw new SyntaxError('"this" cannot be assigned');
249
- if (reservedWords.has(key))
250
- throw new SyntaxError("Unexepected token '" + key + "'");
251
- const prop = this.get(key);
252
- if (prop.context === undefined) {
253
- throw new ReferenceError(`Variable '${key}' was not declared.`);
254
- }
255
- if (prop.context === null) {
256
- throw new TypeError(`Cannot set properties of null, (setting '${key}')`);
257
- }
258
- if (prop.isConst) {
259
- throw new TypeError(`Assignment to constant variable`);
260
- }
261
- if (prop.isGlobal) {
262
- throw new SandboxError(`Cannot override global variable '${key}'`);
263
- }
264
- prop.context[prop.prop] = val;
265
- return prop;
266
- }
267
- getWhereValScope(key, isThis) {
268
- if (isThis) {
269
- if (this.functionThis !== undefined) {
270
- return this;
271
- }
272
- else {
273
- return this.parent?.getWhereValScope(key, isThis) || null;
274
- }
275
- }
276
- if (key in this.allVars && !(key in {} && !hasOwnProperty(this.allVars, key))) {
277
- return this;
278
- }
279
- return this.parent?.getWhereValScope(key, isThis) || null;
280
- }
281
- getWhereVarScope(key, localScope = false) {
282
- if (key in this.allVars && !(key in {} && !hasOwnProperty(this.allVars, key))) {
283
- return this;
284
- }
285
- if (this.parent === null || localScope || this.functionThis !== undefined) {
286
- return this;
287
- }
288
- return this.parent.getWhereVarScope(key, localScope);
289
- }
290
- declare(key, type, value = undefined, isGlobal = false) {
291
- if (key === 'this')
292
- throw new SyntaxError('"this" cannot be declared');
293
- if (reservedWords.has(key))
294
- throw new SyntaxError("Unexepected token '" + key + "'");
295
- const existingScope = this.getWhereVarScope(key, type !== "var" /* VarType.var */);
296
- if (type === "var" /* VarType.var */) {
297
- if (existingScope.var[key]) {
298
- existingScope.allVars[key] = value;
299
- if (!isGlobal) {
300
- delete existingScope.globals[key];
301
- }
302
- else {
303
- existingScope.globals[key] = true;
304
- }
305
- return new Prop(existingScope.allVars, key, false, existingScope.globals[key], true);
306
- }
307
- else if (key in existingScope.allVars) {
308
- throw new SyntaxError(`Identifier '${key}' has already been declared`);
309
- }
310
- }
311
- if (key in existingScope.allVars) {
312
- throw new SyntaxError(`Identifier '${key}' has already been declared`);
313
- }
314
- if (isGlobal) {
315
- existingScope.globals[key] = true;
316
- }
317
- existingScope[type][key] = true;
318
- existingScope.allVars[key] = value;
319
- return new Prop(this.allVars, key, type === "const" /* VarType.const */, isGlobal, true);
320
- }
321
- }
322
- export class FunctionScope {
323
- }
324
- export class LocalScope {
325
- }
326
- export class SandboxError extends Error {
327
- }
328
- export class SandboxExecutionQuotaExceededError extends SandboxError {
329
- }
330
- export class SandboxExecutionTreeError extends SandboxError {
331
- }
332
- export class SandboxCapabilityError extends SandboxError {
333
- }
334
- export class SandboxAccessError extends SandboxError {
335
- }
336
- export function isLisp(item) {
337
- return (Array.isArray(item) &&
338
- typeof item[0] === 'number' &&
339
- item[0] !== 0 /* LispType.None */ &&
340
- item[0] !== 88 /* LispType.True */);
341
- }
342
- export class Prop {
343
- constructor(context, prop, isConst = false, isGlobal = false, isVariable = false) {
344
- this.context = context;
345
- this.prop = prop;
346
- this.isConst = isConst;
347
- this.isGlobal = isGlobal;
348
- this.isVariable = isVariable;
349
- }
350
- get(context) {
351
- const ctx = this.context;
352
- if (ctx === undefined)
353
- throw new ReferenceError(`${this.prop.toString()} is not defined`);
354
- if (ctx === null)
355
- throw new TypeError(`Cannot read properties of null, (reading '${this.prop.toString()}')`);
356
- context.getSubscriptions.forEach((cb) => cb(ctx, this.prop.toString()));
357
- return ctx[this.prop];
358
- }
359
- }
360
- export function hasOwnProperty(obj, prop) {
361
- return Object.prototype.hasOwnProperty.call(obj, prop);
362
- }
package/dist/Sandbox.d.ts DELETED
@@ -1,25 +0,0 @@
1
- import { IExecContext, IOptionParams, IScope } from './utils.js';
2
- import { ExecReturn } from './executor.js';
3
- import SandboxExec from './SandboxExec.js';
4
- export { LocalScope, SandboxExecutionTreeError, SandboxCapabilityError, SandboxAccessError, SandboxError, } from './utils.js';
5
- export default class Sandbox extends SandboxExec {
6
- constructor(options?: IOptionParams);
7
- static audit<T>(code: string, scopes?: IScope[]): ExecReturn<T>;
8
- static parse(code: string): import("./parser.js").IExecutionTree;
9
- compile<T>(code: string, optimize?: boolean): (...scopes: IScope[]) => {
10
- context: IExecContext;
11
- run: () => T;
12
- };
13
- compileAsync<T>(code: string, optimize?: boolean): (...scopes: IScope[]) => {
14
- context: IExecContext;
15
- run: () => Promise<T>;
16
- };
17
- compileExpression<T>(code: string, optimize?: boolean): (...scopes: IScope[]) => {
18
- context: IExecContext;
19
- run: () => T;
20
- };
21
- compileExpressionAsync<T>(code: string, optimize?: boolean): (...scopes: IScope[]) => {
22
- context: IExecContext;
23
- run: () => Promise<T>;
24
- };
25
- }
package/dist/Sandbox.js DELETED
@@ -1,270 +0,0 @@
1
- import { createExecContext } from './utils.js';
2
- export { LocalScope, SandboxAccessError, SandboxCapabilityError, SandboxError, SandboxExecutionTreeError } from './utils.js';
3
- import { createFunction, createFunctionAsync } from './executor.js';
4
- import parse, { lispifyFunction } from './parser.js';
5
- import SandboxExec from './SandboxExec.js';
6
-
7
- function createEvalContext() {
8
- return {
9
- sandboxFunction,
10
- sandboxAsyncFunction,
11
- sandboxedEval,
12
- sandboxedSetTimeout,
13
- sandboxedSetInterval,
14
- sandboxedClearTimeout,
15
- sandboxedClearInterval,
16
- lispifyFunction,
17
- };
18
- }
19
- function SB() { }
20
- function sandboxFunction(context) {
21
- SandboxFunction.prototype = SB.prototype;
22
- return SandboxFunction;
23
- function SandboxFunction(...params) {
24
- const code = params.pop() || '';
25
- const parsed = parse(code);
26
- return createFunction(params, parsed.tree, context.ctx.ticks, {
27
- ...context,
28
- constants: parsed.constants,
29
- tree: parsed.tree,
30
- }, undefined, 'anonymous');
31
- }
32
- }
33
- function SAF() { }
34
- function sandboxAsyncFunction(context) {
35
- SandboxAsyncFunction.prototype = SAF.prototype;
36
- return SandboxAsyncFunction;
37
- function SandboxAsyncFunction(...params) {
38
- const code = params.pop() || '';
39
- const parsed = parse(code);
40
- return createFunctionAsync(params, parsed.tree, context.ctx.ticks, {
41
- ...context,
42
- constants: parsed.constants,
43
- tree: parsed.tree,
44
- }, undefined, 'anonymous');
45
- }
46
- }
47
- function SE() { }
48
- function sandboxedEval(func, context) {
49
- sandboxEval.prototype = SE.prototype;
50
- return sandboxEval;
51
- function sandboxEval(code) {
52
- // Parse the code and wrap last statement in return for completion value
53
- const parsed = parse(code);
54
- const tree = wrapLastStatementInReturn(parsed.tree);
55
- // Create and execute function with modified tree
56
- return createFunction([], tree, context.ctx.ticks, {
57
- ...context,
58
- constants: parsed.constants,
59
- tree,
60
- }, undefined, 'anonymous')();
61
- }
62
- }
63
- function wrapLastStatementInReturn(tree) {
64
- if (tree.length === 0)
65
- return tree;
66
- const newTree = [...tree];
67
- const lastIndex = newTree.length - 1;
68
- const lastStmt = newTree[lastIndex];
69
- // Only wrap if it's not already a return or throw
70
- if (Array.isArray(lastStmt) && lastStmt.length >= 1) {
71
- const op = lastStmt[0];
72
- // Don't wrap Return (8) or Throw (47) - they already control flow
73
- if (op === 8 /* LispType.Return */ || op === 46 /* LispType.Throw */) {
74
- return newTree;
75
- }
76
- // List of statement types that should have undefined completion value
77
- // These match JavaScript semantics where declarations and control structures
78
- // don't produce a completion value
79
- const statementTypes = [
80
- 3 /* LispType.Let */, // 3
81
- 4 /* LispType.Const */, // 4
82
- 34 /* LispType.Var */, // 35
83
- 37 /* LispType.Function */, // 38
84
- 13 /* LispType.If */, // 14
85
- 38 /* LispType.Loop */, // 39
86
- 39 /* LispType.Try */, // 40
87
- 40 /* LispType.Switch */, // 41
88
- 42 /* LispType.Block */, // 43
89
- 43 /* LispType.Expression */, // 44
90
- ];
91
- // If the last statement is a declaration or control structure,
92
- // don't wrap it (it will naturally return undefined)
93
- if (statementTypes.includes(op)) {
94
- return newTree;
95
- }
96
- // For all other types (expressions, operators, etc.),
97
- // wrap in return to capture the completion value
98
- newTree[lastIndex] = [8 /* LispType.Return */, 0 /* LispType.None */, lastStmt];
99
- }
100
- return newTree;
101
- }
102
- function sST() { }
103
- function sandboxedSetTimeout(func, context) {
104
- sandboxSetTimeout.prototype = sST.prototype;
105
- return sandboxSetTimeout;
106
- function sandboxSetTimeout(handler, timeout, ...args) {
107
- const sandbox = context.ctx.sandbox;
108
- const exec = (...a) => {
109
- const h = typeof handler === 'string' ? func(handler) : handler;
110
- haltsub.unsubscribe();
111
- contsub.unsubscribe();
112
- sandbox.setTimeoutHandles.delete(sandBoxhandle);
113
- return h(...a);
114
- };
115
- const sandBoxhandle = ++sandbox.timeoutHandleCounter;
116
- let start = Date.now();
117
- let handle = setTimeout(exec, timeout, ...args);
118
- let elapsed = 0;
119
- const haltsub = sandbox.subscribeHalt(() => {
120
- elapsed = Date.now() - start + elapsed;
121
- clearTimeout(handle);
122
- });
123
- const contsub = sandbox.subscribeResume(() => {
124
- start = Date.now();
125
- const remaining = Math.floor((timeout || 0) - elapsed);
126
- handle = setTimeout(exec, remaining, ...args);
127
- sandbox.setTimeoutHandles.set(sandBoxhandle, {
128
- handle,
129
- haltsub,
130
- contsub,
131
- });
132
- });
133
- sandbox.setTimeoutHandles.set(sandBoxhandle, {
134
- handle,
135
- haltsub,
136
- contsub,
137
- });
138
- return sandBoxhandle;
139
- }
140
- }
141
- function sCT() { }
142
- function sandboxedClearTimeout(context) {
143
- sandboxClearTimeout.prototype = sCT.prototype;
144
- return sandboxClearTimeout;
145
- function sandboxClearTimeout(handle) {
146
- const sandbox = context.ctx.sandbox;
147
- const timeoutHandle = sandbox.setTimeoutHandles.get(handle);
148
- if (timeoutHandle) {
149
- clearTimeout(timeoutHandle.handle);
150
- timeoutHandle.haltsub.unsubscribe();
151
- timeoutHandle.contsub.unsubscribe();
152
- sandbox.setTimeoutHandles.delete(handle);
153
- }
154
- }
155
- }
156
- function sCI() { }
157
- function sandboxedClearInterval(context) {
158
- sandboxClearInterval.prototype = sCI.prototype;
159
- return sandboxClearInterval;
160
- function sandboxClearInterval(handle) {
161
- const sandbox = context.ctx.sandbox;
162
- const intervalHandle = sandbox.setIntervalHandles.get(handle);
163
- if (intervalHandle) {
164
- clearInterval(intervalHandle.handle);
165
- intervalHandle.haltsub.unsubscribe();
166
- intervalHandle.contsub.unsubscribe();
167
- sandbox.setIntervalHandles.delete(handle);
168
- }
169
- }
170
- }
171
- function sSI() { }
172
- function sandboxedSetInterval(func, context) {
173
- sandboxSetInterval.prototype = sSI.prototype;
174
- return sandboxSetInterval;
175
- function sandboxSetInterval(handler, timeout, ...args) {
176
- const sandbox = context.ctx.sandbox;
177
- const h = typeof handler === 'string' ? func(handler) : handler;
178
- const exec = (...a) => {
179
- start = Date.now();
180
- elapsed = 0;
181
- return h(...a);
182
- };
183
- const sandBoxhandle = ++sandbox.timeoutHandleCounter;
184
- let start = Date.now();
185
- let handle = setInterval(exec, timeout, ...args);
186
- let elapsed = 0;
187
- const haltsub = sandbox.subscribeHalt(() => {
188
- elapsed = Date.now() - start + elapsed;
189
- clearInterval(handle);
190
- });
191
- const contsub = sandbox.subscribeResume(() => {
192
- start = Date.now();
193
- handle = setTimeout(() => {
194
- start = Date.now();
195
- elapsed = 0;
196
- handle = setInterval(exec, timeout, ...args);
197
- exec(...args);
198
- }, Math.floor((timeout || 0) - elapsed), ...args);
199
- handlObj.handle = handle;
200
- });
201
- const handlObj = {
202
- handle,
203
- haltsub,
204
- contsub,
205
- };
206
- sandbox.setIntervalHandles.set(sandBoxhandle, handlObj);
207
- return sandBoxhandle;
208
- }
209
- }
210
-
211
- class Sandbox extends SandboxExec {
212
- constructor(options) {
213
- super(options, createEvalContext());
214
- }
215
- static audit(code, scopes = []) {
216
- const globals = {};
217
- for (const i of Object.getOwnPropertyNames(globalThis)) {
218
- globals[i] = globalThis[i];
219
- }
220
- const sandbox = new SandboxExec({
221
- globals,
222
- audit: true,
223
- });
224
- return sandbox.executeTree(createExecContext(sandbox, parse(code, true), createEvalContext()), scopes);
225
- }
226
- static parse(code) {
227
- return parse(code);
228
- }
229
- compile(code, optimize = false) {
230
- const parsed = parse(code, optimize);
231
- const exec = (...scopes) => {
232
- const context = createExecContext(this, parsed, this.evalContext);
233
- return { context, run: () => this.executeTree(context, [...scopes]).result };
234
- };
235
- return exec;
236
- }
237
- compileAsync(code, optimize = false) {
238
- const parsed = parse(code, optimize);
239
- const exec = (...scopes) => {
240
- const context = createExecContext(this, parsed, this.evalContext);
241
- return {
242
- context,
243
- run: () => this.executeTreeAsync(context, [...scopes]).then((ret) => ret.result),
244
- };
245
- };
246
- return exec;
247
- }
248
- compileExpression(code, optimize = false) {
249
- const parsed = parse(code, optimize, true);
250
- const exec = (...scopes) => {
251
- const context = createExecContext(this, parsed, this.evalContext);
252
- return { context, run: () => this.executeTree(context, [...scopes]).result };
253
- };
254
- return exec;
255
- }
256
- compileExpressionAsync(code, optimize = false) {
257
- const parsed = parse(code, optimize, true);
258
- const exec = (...scopes) => {
259
- const context = createExecContext(this, parsed, this.evalContext);
260
- return {
261
- context,
262
- run: () => this.executeTreeAsync(context, [...scopes]).then((ret) => ret.result),
263
- };
264
- };
265
- return exec;
266
- }
267
- }
268
-
269
- export { Sandbox as default };
270
- //# sourceMappingURL=Sandbox.js.map