@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
@@ -1,264 +0,0 @@
1
- export declare const AsyncFunction: Function;
2
- export declare const GeneratorFunction: Function;
3
- export declare const AsyncGeneratorFunction: Function;
4
- import { IEvalContext } from './eval';
5
- import { Change, Unknown } from './executor';
6
- import { IConstants, IExecutionTree, Lisp, LispItem } from './parser';
7
- import SandboxExec from './SandboxExec';
8
- export type replacementCallback = (obj: any, isStaticAccess: boolean) => any;
9
- export interface IOptionParams {
10
- audit?: boolean;
11
- forbidFunctionCalls?: boolean;
12
- forbidFunctionCreation?: boolean;
13
- prototypeReplacements?: Map<Function, replacementCallback>;
14
- prototypeWhitelist?: Map<Function, Set<string>>;
15
- globals?: IGlobals;
16
- executionQuota?: bigint;
17
- haltOnSandboxError?: boolean;
18
- }
19
- export interface IOptions {
20
- audit: boolean;
21
- forbidFunctionCalls: boolean;
22
- forbidFunctionCreation: boolean;
23
- prototypeReplacements: Map<Function, replacementCallback>;
24
- prototypeWhitelist: Map<Function, Set<string>>;
25
- globals: IGlobals;
26
- executionQuota?: bigint;
27
- haltOnSandboxError?: boolean;
28
- }
29
- export interface IContext {
30
- sandbox: SandboxExec;
31
- globalScope: Scope;
32
- sandboxGlobal: ISandboxGlobal;
33
- globalsWhitelist: Set<any>;
34
- prototypeWhitelist: Map<any, Set<PropertyKey>>;
35
- sandboxedFunctions: WeakSet<Function>;
36
- options: IOptions;
37
- auditReport?: IAuditReport;
38
- ticks: Ticks;
39
- }
40
- export interface IAuditReport {
41
- globalsAccess: Set<unknown>;
42
- prototypeAccess: {
43
- [name: string]: Set<PropertyKey>;
44
- };
45
- }
46
- export interface Ticks {
47
- ticks: bigint;
48
- tickLimit?: bigint;
49
- }
50
- export type SubscriptionSubject = object;
51
- export interface IExecContext extends IExecutionTree {
52
- ctx: IContext;
53
- getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>;
54
- setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
55
- changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
56
- setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
57
- changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
58
- registerSandboxFunction: (fn: (...args: any[]) => any) => void;
59
- evals: Map<Function, Function>;
60
- allowJit: boolean;
61
- evalContext?: IEvalContext;
62
- }
63
- export interface ISandboxGlobal {
64
- [key: string]: unknown;
65
- }
66
- interface SandboxGlobalConstructor {
67
- new (globals: IGlobals): ISandboxGlobal;
68
- }
69
- export declare const SandboxGlobal: SandboxGlobalConstructor;
70
- export type IGlobals = ISandboxGlobal;
71
- export declare class ExecContext implements IExecContext {
72
- ctx: IContext;
73
- constants: IConstants;
74
- tree: Lisp[];
75
- getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>;
76
- setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
77
- changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
78
- setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
79
- changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
80
- evals: Map<any, any>;
81
- registerSandboxFunction: (fn: (...args: any[]) => any) => void;
82
- allowJit: boolean;
83
- evalContext?: IEvalContext | undefined;
84
- 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);
85
- }
86
- export declare function createContext(sandbox: SandboxExec, options: IOptions): IContext;
87
- export declare function createExecContext(sandbox: {
88
- readonly setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
89
- readonly changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
90
- readonly sandboxFunctions: WeakMap<(...args: any[]) => any, IExecContext>;
91
- readonly context: IContext;
92
- }, executionTree: IExecutionTree, evalContext?: IEvalContext): IExecContext;
93
- export declare class CodeString {
94
- start: number;
95
- end: number;
96
- ref: {
97
- str: string;
98
- };
99
- constructor(str: string | CodeString);
100
- substring(start: number, end?: number): CodeString;
101
- get length(): number;
102
- char(i: number): string | undefined;
103
- toString(): string;
104
- trimStart(): CodeString;
105
- slice(start: number, end?: number): CodeString;
106
- trim(): CodeString;
107
- valueOf(): string;
108
- }
109
- export declare const reservedWords: Set<string>;
110
- export declare const enum VarType {
111
- let = "let",
112
- const = "const",
113
- var = "var"
114
- }
115
- export declare class Scope {
116
- parent: Scope | null;
117
- const: {
118
- [key: string]: true;
119
- };
120
- let: {
121
- [key: string]: true;
122
- };
123
- var: {
124
- [key: string]: true;
125
- };
126
- globals: {
127
- [key: string]: true;
128
- };
129
- allVars: {
130
- [key: string]: unknown;
131
- } & object;
132
- functionThis?: Unknown;
133
- constructor(parent: Scope | null, vars?: {}, functionThis?: Unknown);
134
- get(key: string): Prop;
135
- set(key: string, val: unknown): Prop<unknown>;
136
- getWhereValScope(key: string, isThis: boolean): Scope | null;
137
- getWhereVarScope(key: string, localScope?: boolean): Scope;
138
- declare(key: string, type: VarType, value?: unknown, isGlobal?: boolean): Prop;
139
- }
140
- export interface IScope {
141
- [key: string]: any;
142
- }
143
- export declare class FunctionScope implements IScope {
144
- }
145
- export declare class LocalScope implements IScope {
146
- }
147
- export declare class SandboxError extends Error {
148
- }
149
- export declare class SandboxExecutionQuotaExceededError extends SandboxError {
150
- }
151
- export declare class SandboxExecutionTreeError extends SandboxError {
152
- }
153
- export declare class SandboxCapabilityError extends SandboxError {
154
- }
155
- export declare class SandboxAccessError extends SandboxError {
156
- }
157
- export declare function isLisp<Type extends Lisp = Lisp>(item: LispItem | LispItem): item is Type;
158
- export declare const enum LispType {
159
- None = 0,
160
- Prop = 1,
161
- StringIndex = 2,
162
- Let = 3,
163
- Const = 4,
164
- Call = 5,
165
- KeyVal = 6,
166
- Number = 7,
167
- Return = 8,
168
- Assign = 9,
169
- InlineFunction = 10,
170
- ArrowFunction = 11,
171
- CreateArray = 12,
172
- If = 13,
173
- IfCase = 14,
174
- InlineIf = 15,
175
- InlineIfCase = 16,
176
- SpreadObject = 17,
177
- SpreadArray = 18,
178
- ArrayProp = 19,
179
- PropOptional = 20,
180
- CallOptional = 21,
181
- CreateObject = 22,
182
- Group = 23,
183
- Not = 24,
184
- IncrementBefore = 25,
185
- IncrementAfter = 26,
186
- DecrementBefore = 27,
187
- DecrementAfter = 28,
188
- And = 29,
189
- Or = 30,
190
- StrictNotEqual = 31,
191
- StrictEqual = 32,
192
- Plus = 33,
193
- Var = 34,
194
- GlobalSymbol = 35,
195
- Literal = 36,
196
- Function = 37,
197
- Loop = 38,
198
- Try = 39,
199
- Switch = 40,
200
- SwitchCase = 41,
201
- Block = 42,
202
- Expression = 43,
203
- Await = 44,
204
- New = 45,
205
- Throw = 46,
206
- Minus = 47,
207
- Divide = 48,
208
- Power = 49,
209
- Multiply = 50,
210
- Modulus = 51,
211
- Equal = 52,
212
- NotEqual = 53,
213
- SmallerEqualThan = 54,
214
- LargerEqualThan = 55,
215
- SmallerThan = 56,
216
- LargerThan = 57,
217
- Negative = 58,
218
- Positive = 59,
219
- Typeof = 60,
220
- Delete = 61,
221
- Instanceof = 62,
222
- In = 63,
223
- Inverse = 64,
224
- SubractEquals = 65,
225
- AddEquals = 66,
226
- DivideEquals = 67,
227
- PowerEquals = 68,
228
- MultiplyEquals = 69,
229
- ModulusEquals = 70,
230
- BitNegateEquals = 71,
231
- BitAndEquals = 72,
232
- BitOrEquals = 73,
233
- UnsignedShiftRightEquals = 74,
234
- ShiftRightEquals = 75,
235
- ShiftLeftEquals = 76,
236
- BitAnd = 77,
237
- BitOr = 78,
238
- BitNegate = 79,
239
- BitShiftLeft = 80,
240
- BitShiftRight = 81,
241
- BitUnsignedShiftRight = 82,
242
- BigInt = 83,
243
- LiteralIndex = 84,
244
- RegexIndex = 85,
245
- LoopAction = 86,
246
- Void = 87,
247
- True = 88,
248
- NullishCoalescing = 89,
249
- AndEquals = 90,
250
- OrEquals = 91,
251
- NullishCoalescingEquals = 92,
252
- LispEnumSize = 93
253
- }
254
- export declare class Prop<T = unknown> {
255
- context: T;
256
- prop: PropertyKey;
257
- isConst: boolean;
258
- isGlobal: boolean;
259
- isVariable: boolean;
260
- constructor(context: T, prop: PropertyKey, isConst?: boolean, isGlobal?: boolean, isVariable?: boolean);
261
- get<T = unknown>(context: IExecContext): T;
262
- }
263
- export declare function hasOwnProperty(obj: unknown, prop: PropertyKey): boolean;
264
- export {};
@@ -1,385 +0,0 @@
1
- 'use strict';
2
-
3
- // Reusable AsyncFunction constructor reference
4
- const AsyncFunction = Object.getPrototypeOf(async function () { }).constructor;
5
- const GeneratorFunction = Object.getPrototypeOf(function* () { }).constructor;
6
- const AsyncGeneratorFunction = Object.getPrototypeOf(async function* () { }).constructor;
7
- const SandboxGlobal = function SandboxGlobal(globals) {
8
- for (const i in globals) {
9
- this[i] = globals[i];
10
- }
11
- };
12
- class ExecContext {
13
- constructor(ctx, constants, tree, getSubscriptions, setSubscriptions, changeSubscriptions, setSubscriptionsGlobal, changeSubscriptionsGlobal, evals, registerSandboxFunction, allowJit, evalContext) {
14
- this.ctx = ctx;
15
- this.constants = constants;
16
- this.tree = tree;
17
- this.getSubscriptions = getSubscriptions;
18
- this.setSubscriptions = setSubscriptions;
19
- this.changeSubscriptions = changeSubscriptions;
20
- this.setSubscriptionsGlobal = setSubscriptionsGlobal;
21
- this.changeSubscriptionsGlobal = changeSubscriptionsGlobal;
22
- this.evals = evals;
23
- this.registerSandboxFunction = registerSandboxFunction;
24
- this.allowJit = allowJit;
25
- this.evalContext = evalContext;
26
- }
27
- }
28
- function createContext(sandbox, options) {
29
- const sandboxGlobal = new SandboxGlobal(options.globals);
30
- const context = {
31
- sandbox: sandbox,
32
- globalsWhitelist: new Set(Object.values(options.globals)),
33
- prototypeWhitelist: new Map([...options.prototypeWhitelist].map((a) => [a[0].prototype, a[1]])),
34
- options,
35
- globalScope: new Scope(null, options.globals, sandboxGlobal),
36
- sandboxGlobal,
37
- ticks: { ticks: 0n, tickLimit: options.executionQuota },
38
- sandboxedFunctions: new WeakSet(),
39
- };
40
- context.prototypeWhitelist.set(Object.getPrototypeOf([][Symbol.iterator]()), new Set());
41
- // Fetch API 构造函数本身(静态方法如 Response.json)也加白名单
42
- if (typeof Response !== 'undefined')
43
- context.prototypeWhitelist.set(Response.prototype, new Set());
44
- if (typeof Request !== 'undefined')
45
- context.prototypeWhitelist.set(Request.prototype, new Set());
46
- if (typeof Headers !== 'undefined')
47
- context.prototypeWhitelist.set(Headers.prototype, new Set());
48
- if (typeof FormData !== 'undefined')
49
- context.prototypeWhitelist.set(FormData.prototype, new Set());
50
- if (typeof Blob !== 'undefined')
51
- context.prototypeWhitelist.set(Blob.prototype, new Set());
52
- if (typeof URLSearchParams !== 'undefined')
53
- context.prototypeWhitelist.set(URLSearchParams.prototype, new Set());
54
- if (typeof AbortController !== 'undefined')
55
- context.prototypeWhitelist.set(AbortController.prototype, new Set());
56
- if (typeof ReadableStream !== 'undefined')
57
- context.prototypeWhitelist.set(ReadableStream.prototype, new Set());
58
- if (typeof TransformStream !== 'undefined')
59
- context.prototypeWhitelist.set(TransformStream.prototype, new Set());
60
- if (typeof WritableStream !== 'undefined')
61
- context.prototypeWhitelist.set(WritableStream.prototype, new Set());
62
- if (typeof TextEncoder !== 'undefined')
63
- context.prototypeWhitelist.set(TextEncoder.prototype, new Set());
64
- if (typeof TextDecoder !== 'undefined')
65
- context.prototypeWhitelist.set(TextDecoder.prototype, new Set());
66
- return context;
67
- }
68
- function createExecContext(sandbox, executionTree, evalContext) {
69
- const evals = new Map();
70
- 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);
71
- if (evalContext) {
72
- const func = evalContext.sandboxFunction(execContext);
73
- const asyncFunc = evalContext.sandboxAsyncFunction(execContext);
74
- evals.set(Function, func);
75
- evals.set(AsyncFunction, asyncFunc);
76
- evals.set(GeneratorFunction, func);
77
- evals.set(AsyncGeneratorFunction, asyncFunc);
78
- evals.set(eval, evalContext.sandboxedEval(func, execContext));
79
- evals.set(setTimeout, evalContext.sandboxedSetTimeout(func, execContext));
80
- evals.set(setInterval, evalContext.sandboxedSetInterval(func, execContext));
81
- evals.set(clearTimeout, evalContext.sandboxedClearTimeout(execContext));
82
- evals.set(clearInterval, evalContext.sandboxedClearInterval(execContext));
83
- for (const [key, value] of evals) {
84
- sandbox.context.prototypeWhitelist.set(value.prototype, new Set());
85
- sandbox.context.prototypeWhitelist.set(key.prototype, new Set());
86
- }
87
- }
88
- return execContext;
89
- }
90
- class CodeString {
91
- constructor(str) {
92
- this.ref = { str: '' };
93
- if (str instanceof CodeString) {
94
- this.ref = str.ref;
95
- this.start = str.start;
96
- this.end = str.end;
97
- }
98
- else {
99
- this.ref.str = str;
100
- this.start = 0;
101
- this.end = str.length;
102
- }
103
- }
104
- substring(start, end) {
105
- if (!this.length)
106
- return this;
107
- start = this.start + start;
108
- if (start < 0) {
109
- start = 0;
110
- }
111
- if (start > this.end) {
112
- start = this.end;
113
- }
114
- end = end === undefined ? this.end : this.start + end;
115
- if (end < 0) {
116
- end = 0;
117
- }
118
- if (end > this.end) {
119
- end = this.end;
120
- }
121
- const code = new CodeString(this);
122
- code.start = start;
123
- code.end = end;
124
- return code;
125
- }
126
- get length() {
127
- const len = this.end - this.start;
128
- return len < 0 ? 0 : len;
129
- }
130
- char(i) {
131
- if (this.start === this.end)
132
- return undefined;
133
- return this.ref.str[this.start + i];
134
- }
135
- toString() {
136
- return this.ref.str.substring(this.start, this.end);
137
- }
138
- trimStart() {
139
- const found = /^\s+/.exec(this.toString());
140
- const code = new CodeString(this);
141
- if (found) {
142
- code.start += found[0].length;
143
- }
144
- return code;
145
- }
146
- slice(start, end) {
147
- if (start < 0) {
148
- start = this.end - this.start + start;
149
- }
150
- if (start < 0) {
151
- start = 0;
152
- }
153
- if (end === undefined) {
154
- end = this.end - this.start;
155
- }
156
- if (end < 0) {
157
- end = this.end - this.start + end;
158
- }
159
- if (end < 0) {
160
- end = 0;
161
- }
162
- return this.substring(start, end);
163
- }
164
- trim() {
165
- const code = this.trimStart();
166
- const found = /\s+$/.exec(code.toString());
167
- if (found) {
168
- code.end -= found[0].length;
169
- }
170
- return code;
171
- }
172
- valueOf() {
173
- return this.toString();
174
- }
175
- }
176
- function keysOnly(obj) {
177
- const ret = Object.assign({}, obj);
178
- for (const key in ret) {
179
- ret[key] = true;
180
- }
181
- return ret;
182
- }
183
- const reservedWords = new Set([
184
- 'await',
185
- 'break',
186
- 'case',
187
- 'catch',
188
- 'class',
189
- 'const',
190
- 'continue',
191
- 'debugger',
192
- 'default',
193
- 'delete',
194
- 'do',
195
- 'else',
196
- 'enum',
197
- 'export',
198
- 'extends',
199
- 'false',
200
- 'finally',
201
- 'for',
202
- 'function',
203
- 'if',
204
- 'implements',
205
- 'import',
206
- 'in',
207
- 'instanceof',
208
- 'let',
209
- 'new',
210
- 'null',
211
- 'return',
212
- 'super',
213
- 'switch',
214
- 'this',
215
- 'throw',
216
- 'true',
217
- 'try',
218
- 'typeof',
219
- 'var',
220
- 'void',
221
- 'while',
222
- 'with',
223
- ]);
224
- class Scope {
225
- constructor(parent, vars = {}, functionThis) {
226
- this.const = {};
227
- this.let = {};
228
- this.var = {};
229
- const isFuncScope = functionThis !== undefined || parent === null;
230
- this.parent = parent;
231
- this.allVars = vars;
232
- this.let = isFuncScope ? this.let : keysOnly(vars);
233
- this.var = isFuncScope ? keysOnly(vars) : this.var;
234
- this.globals = parent === null ? keysOnly(vars) : {};
235
- this.functionThis = functionThis;
236
- }
237
- get(key) {
238
- const isThis = key === 'this';
239
- const scope = this.getWhereValScope(key, isThis);
240
- if (scope && isThis) {
241
- return new Prop({ this: scope.functionThis }, key, false, false, true);
242
- }
243
- if (!scope) {
244
- return new Prop(undefined, key);
245
- }
246
- return new Prop(scope.allVars, key, key in scope.const, key in scope.globals, true);
247
- }
248
- set(key, val) {
249
- if (key === 'this')
250
- throw new SyntaxError('"this" cannot be assigned');
251
- if (reservedWords.has(key))
252
- throw new SyntaxError("Unexepected token '" + key + "'");
253
- const prop = this.get(key);
254
- if (prop.context === undefined) {
255
- throw new ReferenceError(`Variable '${key}' was not declared.`);
256
- }
257
- if (prop.context === null) {
258
- throw new TypeError(`Cannot set properties of null, (setting '${key}')`);
259
- }
260
- if (prop.isConst) {
261
- throw new TypeError(`Assignment to constant variable`);
262
- }
263
- if (prop.isGlobal) {
264
- throw new SandboxError(`Cannot override global variable '${key}'`);
265
- }
266
- prop.context[prop.prop] = val;
267
- return prop;
268
- }
269
- getWhereValScope(key, isThis) {
270
- if (isThis) {
271
- if (this.functionThis !== undefined) {
272
- return this;
273
- }
274
- else {
275
- return this.parent?.getWhereValScope(key, isThis) || null;
276
- }
277
- }
278
- if (key in this.allVars && !(key in {} && !hasOwnProperty(this.allVars, key))) {
279
- return this;
280
- }
281
- return this.parent?.getWhereValScope(key, isThis) || null;
282
- }
283
- getWhereVarScope(key, localScope = false) {
284
- if (key in this.allVars && !(key in {} && !hasOwnProperty(this.allVars, key))) {
285
- return this;
286
- }
287
- if (this.parent === null || localScope || this.functionThis !== undefined) {
288
- return this;
289
- }
290
- return this.parent.getWhereVarScope(key, localScope);
291
- }
292
- declare(key, type, value = undefined, isGlobal = false) {
293
- if (key === 'this')
294
- throw new SyntaxError('"this" cannot be declared');
295
- if (reservedWords.has(key))
296
- throw new SyntaxError("Unexepected token '" + key + "'");
297
- const existingScope = this.getWhereVarScope(key, type !== "var" /* VarType.var */);
298
- if (type === "var" /* VarType.var */) {
299
- if (existingScope.var[key]) {
300
- existingScope.allVars[key] = value;
301
- if (!isGlobal) {
302
- delete existingScope.globals[key];
303
- }
304
- else {
305
- existingScope.globals[key] = true;
306
- }
307
- return new Prop(existingScope.allVars, key, false, existingScope.globals[key], true);
308
- }
309
- else if (key in existingScope.allVars) {
310
- throw new SyntaxError(`Identifier '${key}' has already been declared`);
311
- }
312
- }
313
- if (key in existingScope.allVars) {
314
- throw new SyntaxError(`Identifier '${key}' has already been declared`);
315
- }
316
- if (isGlobal) {
317
- existingScope.globals[key] = true;
318
- }
319
- existingScope[type][key] = true;
320
- existingScope.allVars[key] = value;
321
- return new Prop(this.allVars, key, type === "const" /* VarType.const */, isGlobal, true);
322
- }
323
- }
324
- class FunctionScope {
325
- }
326
- class LocalScope {
327
- }
328
- class SandboxError extends Error {
329
- }
330
- class SandboxExecutionQuotaExceededError extends SandboxError {
331
- }
332
- class SandboxExecutionTreeError extends SandboxError {
333
- }
334
- class SandboxCapabilityError extends SandboxError {
335
- }
336
- class SandboxAccessError extends SandboxError {
337
- }
338
- function isLisp(item) {
339
- return (Array.isArray(item) &&
340
- typeof item[0] === 'number' &&
341
- item[0] !== 0 /* LispType.None */ &&
342
- item[0] !== 88 /* LispType.True */);
343
- }
344
- class Prop {
345
- constructor(context, prop, isConst = false, isGlobal = false, isVariable = false) {
346
- this.context = context;
347
- this.prop = prop;
348
- this.isConst = isConst;
349
- this.isGlobal = isGlobal;
350
- this.isVariable = isVariable;
351
- }
352
- get(context) {
353
- const ctx = this.context;
354
- if (ctx === undefined)
355
- throw new ReferenceError(`${this.prop.toString()} is not defined`);
356
- if (ctx === null)
357
- throw new TypeError(`Cannot read properties of null, (reading '${this.prop.toString()}')`);
358
- context.getSubscriptions.forEach((cb) => cb(ctx, this.prop.toString()));
359
- return ctx[this.prop];
360
- }
361
- }
362
- function hasOwnProperty(obj, prop) {
363
- return Object.prototype.hasOwnProperty.call(obj, prop);
364
- }
365
-
366
- exports.AsyncFunction = AsyncFunction;
367
- exports.AsyncGeneratorFunction = AsyncGeneratorFunction;
368
- exports.CodeString = CodeString;
369
- exports.ExecContext = ExecContext;
370
- exports.FunctionScope = FunctionScope;
371
- exports.GeneratorFunction = GeneratorFunction;
372
- exports.LocalScope = LocalScope;
373
- exports.Prop = Prop;
374
- exports.SandboxAccessError = SandboxAccessError;
375
- exports.SandboxCapabilityError = SandboxCapabilityError;
376
- exports.SandboxError = SandboxError;
377
- exports.SandboxExecutionQuotaExceededError = SandboxExecutionQuotaExceededError;
378
- exports.SandboxExecutionTreeError = SandboxExecutionTreeError;
379
- exports.SandboxGlobal = SandboxGlobal;
380
- exports.Scope = Scope;
381
- exports.createContext = createContext;
382
- exports.createExecContext = createExecContext;
383
- exports.hasOwnProperty = hasOwnProperty;
384
- exports.isLisp = isLisp;
385
- exports.reservedWords = reservedWords;