@cuxt/sandboxjs 0.1.1 → 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 +11 -9
  188. package/build/Sandbox.js +0 -62
  189. package/build/SandboxExec.js +0 -214
  190. package/build/eval.js +0 -205
  191. package/build/executor.d.ts +0 -124
  192. package/build/executor.js +0 -1554
  193. package/build/parser.js +0 -1527
  194. package/build/unraw.js +0 -168
  195. package/build/utils.d.ts +0 -264
  196. package/build/utils.js +0 -362
  197. package/dist/Sandbox.d.ts +0 -25
  198. package/dist/Sandbox.js +0 -270
  199. package/dist/Sandbox.js.map +0 -1
  200. package/dist/Sandbox.min.js +0 -2
  201. package/dist/Sandbox.min.js.map +0 -1
  202. package/dist/SandboxExec.js +0 -218
  203. package/dist/SandboxExec.js.map +0 -1
  204. package/dist/SandboxExec.min.js +0 -2
  205. package/dist/SandboxExec.min.js.map +0 -1
  206. package/dist/executor.d.ts +0 -124
  207. package/dist/executor.js +0 -1558
  208. package/dist/executor.js.map +0 -1
  209. package/dist/node/Sandbox.js +0 -277
  210. package/dist/node/SandboxExec.d.ts +0 -66
  211. package/dist/node/SandboxExec.js +0 -225
  212. package/dist/node/eval.d.ts +0 -27
  213. package/dist/node/executor.d.ts +0 -124
  214. package/dist/node/executor.js +0 -1575
  215. package/dist/node/parser.d.ts +0 -154
  216. package/dist/node/parser.js +0 -1704
  217. package/dist/node/utils.d.ts +0 -264
  218. package/dist/node/utils.js +0 -385
  219. package/dist/parser.js +0 -1690
  220. package/dist/parser.js.map +0 -1
  221. package/dist/unraw.d.ts +0 -11
  222. package/dist/utils.d.ts +0 -264
  223. package/dist/utils.js +0 -365
  224. package/dist/utils.js.map +0 -1
  225. /package/{build → dist/cjs/utils}/unraw.d.ts +0 -0
  226. /package/dist/{node → esm/utils}/unraw.d.ts +0 -0
@@ -0,0 +1,7 @@
1
+ import "./unraw.js";
2
+ import "./errors.js";
3
+ import "./CodeString.js";
4
+ import "./types.js";
5
+ import "./Prop.js";
6
+ import "./Scope.js";
7
+ import "./ExecContext.js";
@@ -0,0 +1,221 @@
1
+ import { IEvalContext } from '../eval';
2
+ import { Change } from '../executor';
3
+ import { IExecutionTree } from '../parser';
4
+ import { default as SandboxExec } from '../SandboxExec';
5
+ import { Scope } from './Scope';
6
+ export declare const AsyncFunction: Function;
7
+ export declare const GeneratorFunction: Function;
8
+ export declare const AsyncGeneratorFunction: Function;
9
+ export interface IOptionParams {
10
+ audit?: boolean;
11
+ forbidFunctionCalls?: boolean;
12
+ forbidFunctionCreation?: boolean;
13
+ prototypeWhitelist?: Map<Function, Set<string>>;
14
+ globals?: IGlobals;
15
+ symbolWhitelist?: ISymbolWhitelist;
16
+ executionQuota?: bigint;
17
+ nonBlocking?: boolean;
18
+ haltOnSandboxError?: boolean;
19
+ maxParserRecursionDepth?: number;
20
+ /**
21
+ * Additional function replacements to merge with the built-in tick-checking replacements.
22
+ * Maps a native function to a factory that receives the IContext and returns the replacement.
23
+ * When sandboxed code accesses a property that returns a mapped function, the factory is
24
+ * called once per context and the result is cached and returned instead.
25
+ */
26
+ functionReplacements?: Map<Function, (ctx: IContext) => Function>;
27
+ }
28
+ export interface IOptions {
29
+ audit: boolean;
30
+ forbidFunctionCalls: boolean;
31
+ forbidFunctionCreation: boolean;
32
+ prototypeWhitelist: Map<Function, Set<PropertyKey>>;
33
+ globals: IGlobals;
34
+ symbolWhitelist: ISymbolWhitelist;
35
+ executionQuota?: bigint;
36
+ haltOnSandboxError?: boolean;
37
+ maxParserRecursionDepth: number;
38
+ nonBlocking: boolean;
39
+ functionReplacements: Map<Function, (ctx: IExecContext, builtInReplacement?: Function) => Function>;
40
+ }
41
+ export interface IContext {
42
+ sandbox: SandboxExec;
43
+ globalScope: Scope;
44
+ sandboxGlobal: ISandboxGlobal;
45
+ globalsWhitelist: Set<any>;
46
+ prototypeWhitelist: Map<any, Set<PropertyKey>>;
47
+ sandboxedFunctions: WeakSet<Function>;
48
+ sandboxSymbols: SandboxSymbolContext;
49
+ options: IOptions;
50
+ auditReport?: IAuditReport;
51
+ ticks: Ticks;
52
+ }
53
+ export interface IAuditReport {
54
+ globalsAccess: Set<unknown>;
55
+ prototypeAccess: {
56
+ [name: string]: Set<PropertyKey>;
57
+ };
58
+ }
59
+ export interface Ticks {
60
+ ticks: bigint;
61
+ tickLimit?: bigint;
62
+ nextYield?: bigint;
63
+ }
64
+ export type SubscriptionSubject = object;
65
+ export type HaltContext = {
66
+ type: 'error';
67
+ error: Error;
68
+ ticks: Ticks;
69
+ scope: Scope;
70
+ context: IExecContext;
71
+ } | {
72
+ type: 'manual';
73
+ error?: never;
74
+ ticks?: never;
75
+ scope?: never;
76
+ context?: never;
77
+ } | {
78
+ type: 'yield';
79
+ error?: never;
80
+ ticks?: never;
81
+ scope?: never;
82
+ context?: never;
83
+ };
84
+ export interface IExecContext extends IExecutionTree {
85
+ ctx: IContext;
86
+ getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>;
87
+ setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
88
+ changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
89
+ setSubscriptionsGlobal: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;
90
+ changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;
91
+ registerSandboxFunction: (fn: (...args: any[]) => any) => void;
92
+ evals: Map<Function, Function>;
93
+ allowJit: boolean;
94
+ evalContext?: IEvalContext;
95
+ }
96
+ export interface ISandboxGlobal {
97
+ [key: string]: unknown;
98
+ }
99
+ export interface ISymbolWhitelist {
100
+ [key: string]: symbol;
101
+ }
102
+ export interface SandboxSymbolContext {
103
+ ctor?: Function;
104
+ registry: Map<string, symbol>;
105
+ reverseRegistry: Map<symbol, string>;
106
+ whitelist: ISymbolWhitelist;
107
+ }
108
+ export type IGlobals = ISandboxGlobal;
109
+ export interface IScope {
110
+ [key: string]: any;
111
+ }
112
+ export declare const NON_BLOCKING_THRESHOLD = 50000n;
113
+ export declare const reservedWords: Set<string>;
114
+ export declare const enum VarType {
115
+ let = "let",
116
+ const = "const",
117
+ var = "var",
118
+ internal = "internal"
119
+ }
120
+ export declare const enum LispType {
121
+ None = 0,
122
+ Prop = 1,
123
+ StringIndex = 2,
124
+ Let = 3,
125
+ Const = 4,
126
+ Call = 5,
127
+ KeyVal = 6,
128
+ Number = 7,
129
+ Return = 8,
130
+ Assign = 9,
131
+ InlineFunction = 10,
132
+ ArrowFunction = 11,
133
+ CreateArray = 12,
134
+ If = 13,
135
+ IfCase = 14,
136
+ InlineIf = 15,
137
+ InlineIfCase = 16,
138
+ SpreadObject = 17,
139
+ SpreadArray = 18,
140
+ ArrayProp = 19,
141
+ PropOptional = 20,
142
+ CallOptional = 21,
143
+ CreateObject = 22,
144
+ Group = 23,
145
+ Not = 24,
146
+ IncrementBefore = 25,
147
+ IncrementAfter = 26,
148
+ DecrementBefore = 27,
149
+ DecrementAfter = 28,
150
+ And = 29,
151
+ Or = 30,
152
+ StrictNotEqual = 31,
153
+ StrictEqual = 32,
154
+ Plus = 33,
155
+ Var = 34,
156
+ GlobalSymbol = 35,
157
+ Literal = 36,
158
+ Function = 37,
159
+ Loop = 38,
160
+ Try = 39,
161
+ Switch = 40,
162
+ SwitchCase = 41,
163
+ InternalBlock = 42,
164
+ Expression = 43,
165
+ Await = 44,
166
+ New = 45,
167
+ Throw = 46,
168
+ Minus = 47,
169
+ Divide = 48,
170
+ Power = 49,
171
+ Multiply = 50,
172
+ Modulus = 51,
173
+ Equal = 52,
174
+ NotEqual = 53,
175
+ SmallerEqualThan = 54,
176
+ LargerEqualThan = 55,
177
+ SmallerThan = 56,
178
+ LargerThan = 57,
179
+ Negative = 58,
180
+ Positive = 59,
181
+ Typeof = 60,
182
+ Delete = 61,
183
+ Instanceof = 62,
184
+ In = 63,
185
+ Inverse = 64,
186
+ SubractEquals = 65,
187
+ AddEquals = 66,
188
+ DivideEquals = 67,
189
+ PowerEquals = 68,
190
+ MultiplyEquals = 69,
191
+ ModulusEquals = 70,
192
+ BitNegateEquals = 71,
193
+ BitAndEquals = 72,
194
+ BitOrEquals = 73,
195
+ UnsignedShiftRightEquals = 74,
196
+ ShiftRightEquals = 75,
197
+ ShiftLeftEquals = 76,
198
+ BitAnd = 77,
199
+ BitOr = 78,
200
+ BitNegate = 79,
201
+ BitShiftLeft = 80,
202
+ BitShiftRight = 81,
203
+ BitUnsignedShiftRight = 82,
204
+ BigInt = 83,
205
+ LiteralIndex = 84,
206
+ RegexIndex = 85,
207
+ LoopAction = 86,
208
+ Void = 87,
209
+ True = 88,
210
+ NullishCoalescing = 89,
211
+ AndEquals = 90,
212
+ OrEquals = 91,
213
+ NullishCoalescingEquals = 92,
214
+ Block = 93,
215
+ Labeled = 94,
216
+ Internal = 95,
217
+ Yield = 96,
218
+ YieldDelegate = 97,
219
+ Hole = 98,
220
+ LispEnumSize = 99
221
+ }
@@ -0,0 +1,160 @@
1
+ //#region src/utils/types.ts
2
+ var AsyncFunction = Object.getPrototypeOf(async function() {}).constructor;
3
+ var GeneratorFunction = Object.getPrototypeOf(function* () {}).constructor;
4
+ var AsyncGeneratorFunction = Object.getPrototypeOf(async function* () {}).constructor;
5
+ var NON_BLOCKING_THRESHOLD = 50000n;
6
+ var reservedWords = new Set([
7
+ "await",
8
+ "break",
9
+ "case",
10
+ "catch",
11
+ "class",
12
+ "const",
13
+ "continue",
14
+ "debugger",
15
+ "default",
16
+ "delete",
17
+ "do",
18
+ "else",
19
+ "enum",
20
+ "export",
21
+ "extends",
22
+ "false",
23
+ "finally",
24
+ "for",
25
+ "function",
26
+ "if",
27
+ "implements",
28
+ "import",
29
+ "in",
30
+ "instanceof",
31
+ "let",
32
+ "new",
33
+ "null",
34
+ "return",
35
+ "super",
36
+ "switch",
37
+ "this",
38
+ "throw",
39
+ "true",
40
+ "try",
41
+ "typeof",
42
+ "var",
43
+ "void",
44
+ "while",
45
+ "with"
46
+ ]);
47
+ var VarType = /* @__PURE__ */ function(VarType) {
48
+ VarType["let"] = "let";
49
+ VarType["const"] = "const";
50
+ VarType["var"] = "var";
51
+ VarType["internal"] = "internal";
52
+ return VarType;
53
+ }({});
54
+ var LispType = /* @__PURE__ */ function(LispType) {
55
+ LispType[LispType["None"] = 0] = "None";
56
+ LispType[LispType["Prop"] = 1] = "Prop";
57
+ LispType[LispType["StringIndex"] = 2] = "StringIndex";
58
+ LispType[LispType["Let"] = 3] = "Let";
59
+ LispType[LispType["Const"] = 4] = "Const";
60
+ LispType[LispType["Call"] = 5] = "Call";
61
+ LispType[LispType["KeyVal"] = 6] = "KeyVal";
62
+ LispType[LispType["Number"] = 7] = "Number";
63
+ LispType[LispType["Return"] = 8] = "Return";
64
+ LispType[LispType["Assign"] = 9] = "Assign";
65
+ LispType[LispType["InlineFunction"] = 10] = "InlineFunction";
66
+ LispType[LispType["ArrowFunction"] = 11] = "ArrowFunction";
67
+ LispType[LispType["CreateArray"] = 12] = "CreateArray";
68
+ LispType[LispType["If"] = 13] = "If";
69
+ LispType[LispType["IfCase"] = 14] = "IfCase";
70
+ LispType[LispType["InlineIf"] = 15] = "InlineIf";
71
+ LispType[LispType["InlineIfCase"] = 16] = "InlineIfCase";
72
+ LispType[LispType["SpreadObject"] = 17] = "SpreadObject";
73
+ LispType[LispType["SpreadArray"] = 18] = "SpreadArray";
74
+ LispType[LispType["ArrayProp"] = 19] = "ArrayProp";
75
+ LispType[LispType["PropOptional"] = 20] = "PropOptional";
76
+ LispType[LispType["CallOptional"] = 21] = "CallOptional";
77
+ LispType[LispType["CreateObject"] = 22] = "CreateObject";
78
+ LispType[LispType["Group"] = 23] = "Group";
79
+ LispType[LispType["Not"] = 24] = "Not";
80
+ LispType[LispType["IncrementBefore"] = 25] = "IncrementBefore";
81
+ LispType[LispType["IncrementAfter"] = 26] = "IncrementAfter";
82
+ LispType[LispType["DecrementBefore"] = 27] = "DecrementBefore";
83
+ LispType[LispType["DecrementAfter"] = 28] = "DecrementAfter";
84
+ LispType[LispType["And"] = 29] = "And";
85
+ LispType[LispType["Or"] = 30] = "Or";
86
+ LispType[LispType["StrictNotEqual"] = 31] = "StrictNotEqual";
87
+ LispType[LispType["StrictEqual"] = 32] = "StrictEqual";
88
+ LispType[LispType["Plus"] = 33] = "Plus";
89
+ LispType[LispType["Var"] = 34] = "Var";
90
+ LispType[LispType["GlobalSymbol"] = 35] = "GlobalSymbol";
91
+ LispType[LispType["Literal"] = 36] = "Literal";
92
+ LispType[LispType["Function"] = 37] = "Function";
93
+ LispType[LispType["Loop"] = 38] = "Loop";
94
+ LispType[LispType["Try"] = 39] = "Try";
95
+ LispType[LispType["Switch"] = 40] = "Switch";
96
+ LispType[LispType["SwitchCase"] = 41] = "SwitchCase";
97
+ LispType[LispType["InternalBlock"] = 42] = "InternalBlock";
98
+ LispType[LispType["Expression"] = 43] = "Expression";
99
+ LispType[LispType["Await"] = 44] = "Await";
100
+ LispType[LispType["New"] = 45] = "New";
101
+ LispType[LispType["Throw"] = 46] = "Throw";
102
+ LispType[LispType["Minus"] = 47] = "Minus";
103
+ LispType[LispType["Divide"] = 48] = "Divide";
104
+ LispType[LispType["Power"] = 49] = "Power";
105
+ LispType[LispType["Multiply"] = 50] = "Multiply";
106
+ LispType[LispType["Modulus"] = 51] = "Modulus";
107
+ LispType[LispType["Equal"] = 52] = "Equal";
108
+ LispType[LispType["NotEqual"] = 53] = "NotEqual";
109
+ LispType[LispType["SmallerEqualThan"] = 54] = "SmallerEqualThan";
110
+ LispType[LispType["LargerEqualThan"] = 55] = "LargerEqualThan";
111
+ LispType[LispType["SmallerThan"] = 56] = "SmallerThan";
112
+ LispType[LispType["LargerThan"] = 57] = "LargerThan";
113
+ LispType[LispType["Negative"] = 58] = "Negative";
114
+ LispType[LispType["Positive"] = 59] = "Positive";
115
+ LispType[LispType["Typeof"] = 60] = "Typeof";
116
+ LispType[LispType["Delete"] = 61] = "Delete";
117
+ LispType[LispType["Instanceof"] = 62] = "Instanceof";
118
+ LispType[LispType["In"] = 63] = "In";
119
+ LispType[LispType["Inverse"] = 64] = "Inverse";
120
+ LispType[LispType["SubractEquals"] = 65] = "SubractEquals";
121
+ LispType[LispType["AddEquals"] = 66] = "AddEquals";
122
+ LispType[LispType["DivideEquals"] = 67] = "DivideEquals";
123
+ LispType[LispType["PowerEquals"] = 68] = "PowerEquals";
124
+ LispType[LispType["MultiplyEquals"] = 69] = "MultiplyEquals";
125
+ LispType[LispType["ModulusEquals"] = 70] = "ModulusEquals";
126
+ LispType[LispType["BitNegateEquals"] = 71] = "BitNegateEquals";
127
+ LispType[LispType["BitAndEquals"] = 72] = "BitAndEquals";
128
+ LispType[LispType["BitOrEquals"] = 73] = "BitOrEquals";
129
+ LispType[LispType["UnsignedShiftRightEquals"] = 74] = "UnsignedShiftRightEquals";
130
+ LispType[LispType["ShiftRightEquals"] = 75] = "ShiftRightEquals";
131
+ LispType[LispType["ShiftLeftEquals"] = 76] = "ShiftLeftEquals";
132
+ LispType[LispType["BitAnd"] = 77] = "BitAnd";
133
+ LispType[LispType["BitOr"] = 78] = "BitOr";
134
+ LispType[LispType["BitNegate"] = 79] = "BitNegate";
135
+ LispType[LispType["BitShiftLeft"] = 80] = "BitShiftLeft";
136
+ LispType[LispType["BitShiftRight"] = 81] = "BitShiftRight";
137
+ LispType[LispType["BitUnsignedShiftRight"] = 82] = "BitUnsignedShiftRight";
138
+ LispType[LispType["BigInt"] = 83] = "BigInt";
139
+ LispType[LispType["LiteralIndex"] = 84] = "LiteralIndex";
140
+ LispType[LispType["RegexIndex"] = 85] = "RegexIndex";
141
+ LispType[LispType["LoopAction"] = 86] = "LoopAction";
142
+ LispType[LispType["Void"] = 87] = "Void";
143
+ LispType[LispType["True"] = 88] = "True";
144
+ LispType[LispType["NullishCoalescing"] = 89] = "NullishCoalescing";
145
+ LispType[LispType["AndEquals"] = 90] = "AndEquals";
146
+ LispType[LispType["OrEquals"] = 91] = "OrEquals";
147
+ LispType[LispType["NullishCoalescingEquals"] = 92] = "NullishCoalescingEquals";
148
+ LispType[LispType["Block"] = 93] = "Block";
149
+ LispType[LispType["Labeled"] = 94] = "Labeled";
150
+ LispType[LispType["Internal"] = 95] = "Internal";
151
+ LispType[LispType["Yield"] = 96] = "Yield";
152
+ LispType[LispType["YieldDelegate"] = 97] = "YieldDelegate";
153
+ LispType[LispType["Hole"] = 98] = "Hole";
154
+ LispType[LispType["LispEnumSize"] = 99] = "LispEnumSize";
155
+ return LispType;
156
+ }({});
157
+ //#endregion
158
+ export { AsyncFunction, AsyncGeneratorFunction, GeneratorFunction, LispType, NON_BLOCKING_THRESHOLD, VarType, reservedWords };
159
+
160
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","names":[],"sources":["../../../src/utils/types.ts"],"sourcesContent":["// Reusable AsyncFunction constructor references\nexport const AsyncFunction: Function = Object.getPrototypeOf(async function () {}).constructor;\nexport const GeneratorFunction: Function = Object.getPrototypeOf(function* () {}).constructor;\nexport const AsyncGeneratorFunction: Function = Object.getPrototypeOf(\n async function* () {},\n).constructor;\n\nimport type { IEvalContext } from '../eval';\nimport type { Change } from '../executor';\nimport type { IExecutionTree } from '../parser';\nimport type SandboxExec from '../SandboxExec';\nimport type { Scope } from './Scope';\n\nexport interface IOptionParams {\n audit?: boolean;\n forbidFunctionCalls?: boolean;\n forbidFunctionCreation?: boolean;\n prototypeWhitelist?: Map<Function, Set<string>>;\n globals?: IGlobals;\n symbolWhitelist?: ISymbolWhitelist;\n executionQuota?: bigint;\n nonBlocking?: boolean;\n haltOnSandboxError?: boolean;\n maxParserRecursionDepth?: number;\n /**\n * Additional function replacements to merge with the built-in tick-checking replacements.\n * Maps a native function to a factory that receives the IContext and returns the replacement.\n * When sandboxed code accesses a property that returns a mapped function, the factory is\n * called once per context and the result is cached and returned instead.\n */\n functionReplacements?: Map<Function, (ctx: IContext) => Function>;\n}\n\nexport interface IOptions {\n audit: boolean;\n forbidFunctionCalls: boolean;\n forbidFunctionCreation: boolean;\n prototypeWhitelist: Map<Function, Set<PropertyKey>>;\n globals: IGlobals;\n symbolWhitelist: ISymbolWhitelist;\n executionQuota?: bigint;\n haltOnSandboxError?: boolean;\n maxParserRecursionDepth: number;\n nonBlocking: boolean;\n functionReplacements: Map<\n Function,\n (ctx: IExecContext, builtInReplacement?: Function) => Function\n >;\n}\n\nexport interface IContext {\n sandbox: SandboxExec;\n globalScope: Scope;\n sandboxGlobal: ISandboxGlobal;\n globalsWhitelist: Set<any>;\n prototypeWhitelist: Map<any, Set<PropertyKey>>;\n sandboxedFunctions: WeakSet<Function>;\n sandboxSymbols: SandboxSymbolContext;\n options: IOptions;\n auditReport?: IAuditReport;\n ticks: Ticks;\n}\n\nexport interface IAuditReport {\n globalsAccess: Set<unknown>;\n prototypeAccess: { [name: string]: Set<PropertyKey> };\n}\n\nexport interface Ticks {\n ticks: bigint;\n tickLimit?: bigint;\n nextYield?: bigint;\n}\n\nexport type SubscriptionSubject = object;\n\nexport type HaltContext =\n | {\n type: 'error';\n error: Error;\n ticks: Ticks;\n scope: Scope;\n context: IExecContext;\n }\n | {\n type: 'manual';\n error?: never;\n ticks?: never;\n scope?: never;\n context?: never;\n }\n | {\n type: 'yield';\n error?: never;\n ticks?: never;\n scope?: never;\n context?: never;\n };\n\nexport interface IExecContext extends IExecutionTree {\n ctx: IContext;\n getSubscriptions: Set<(obj: SubscriptionSubject, name: string) => void>;\n setSubscriptions: WeakMap<SubscriptionSubject, Map<string, Set<(modification: Change) => void>>>;\n changeSubscriptions: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;\n setSubscriptionsGlobal: WeakMap<\n SubscriptionSubject,\n Map<string, Set<(modification: Change) => void>>\n >;\n changeSubscriptionsGlobal: WeakMap<SubscriptionSubject, Set<(modification: Change) => void>>;\n registerSandboxFunction: (fn: (...args: any[]) => any) => void;\n evals: Map<Function, Function>;\n allowJit: boolean;\n evalContext?: IEvalContext;\n}\n\nexport interface ISandboxGlobal {\n [key: string]: unknown;\n}\n\nexport interface ISymbolWhitelist {\n [key: string]: symbol;\n}\n\nexport interface SandboxSymbolContext {\n ctor?: Function;\n registry: Map<string, symbol>;\n reverseRegistry: Map<symbol, string>;\n whitelist: ISymbolWhitelist;\n}\n\nexport type IGlobals = ISandboxGlobal;\n\nexport interface IScope {\n [key: string]: any;\n}\n\nexport const NON_BLOCKING_THRESHOLD = 50_000n;\n\nexport const reservedWords = new Set([\n 'await',\n 'break',\n 'case',\n 'catch',\n 'class',\n 'const',\n 'continue',\n 'debugger',\n 'default',\n 'delete',\n 'do',\n 'else',\n 'enum',\n 'export',\n 'extends',\n 'false',\n 'finally',\n 'for',\n 'function',\n 'if',\n 'implements',\n 'import',\n 'in',\n 'instanceof',\n 'let',\n 'new',\n 'null',\n 'return',\n 'super',\n 'switch',\n 'this',\n 'throw',\n 'true',\n 'try',\n 'typeof',\n 'var',\n 'void',\n 'while',\n 'with',\n]);\n\nexport const enum VarType {\n let = 'let',\n const = 'const',\n var = 'var',\n internal = 'internal',\n}\n\nexport const enum LispType {\n None,\n Prop,\n StringIndex,\n Let,\n Const,\n Call,\n KeyVal,\n Number,\n Return,\n Assign,\n InlineFunction,\n ArrowFunction,\n CreateArray,\n If,\n IfCase,\n InlineIf,\n InlineIfCase,\n SpreadObject,\n SpreadArray,\n ArrayProp,\n PropOptional,\n CallOptional,\n CreateObject,\n Group,\n Not,\n IncrementBefore,\n IncrementAfter,\n DecrementBefore,\n DecrementAfter,\n And,\n Or,\n StrictNotEqual,\n StrictEqual,\n Plus,\n Var,\n GlobalSymbol,\n Literal,\n Function,\n Loop,\n Try,\n Switch,\n SwitchCase,\n InternalBlock,\n Expression,\n Await,\n New,\n Throw,\n Minus,\n Divide,\n Power,\n Multiply,\n Modulus,\n Equal,\n NotEqual,\n SmallerEqualThan,\n LargerEqualThan,\n SmallerThan,\n LargerThan,\n Negative,\n Positive,\n Typeof,\n Delete,\n Instanceof,\n In,\n Inverse,\n SubractEquals,\n AddEquals,\n DivideEquals,\n PowerEquals,\n MultiplyEquals,\n ModulusEquals,\n BitNegateEquals,\n BitAndEquals,\n BitOrEquals,\n UnsignedShiftRightEquals,\n ShiftRightEquals,\n ShiftLeftEquals,\n BitAnd,\n BitOr,\n BitNegate,\n BitShiftLeft,\n BitShiftRight,\n BitUnsignedShiftRight,\n BigInt,\n LiteralIndex,\n RegexIndex,\n LoopAction,\n Void,\n True,\n NullishCoalescing,\n AndEquals,\n OrEquals,\n NullishCoalescingEquals,\n Block,\n Labeled,\n Internal,\n Yield,\n YieldDelegate,\n Hole,\n\n LispEnumSize,\n}\n"],"mappings":";AACA,IAAa,gBAA0B,OAAO,eAAe,iBAAkB,GAAG,CAAC;AACnF,IAAa,oBAA8B,OAAO,eAAe,aAAa,GAAG,CAAC;AAClF,IAAa,yBAAmC,OAAO,eACrD,mBAAmB,GACpB,CAAC;AAmIF,IAAa,yBAAyB;AAEtC,IAAa,gBAAgB,IAAI,IAAI;CACnC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;AAEF,IAAkB,UAAX,yBAAA,SAAA;AACL,SAAA,SAAA;AACA,SAAA,WAAA;AACA,SAAA,SAAA;AACA,SAAA,cAAA;;KACD;AAED,IAAkB,WAAX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AAA,SAAA,YAAA,MAAA;AACA,UAAA,SAAA,kBAAA,MAAA;AACA,UAAA,SAAA,gBAAA,MAAA;AACA,UAAA,SAAA,gBAAA,MAAA;AACA,UAAA,SAAA,UAAA,MAAA;AACA,UAAA,SAAA,UAAA,MAAA;AACA,UAAA,SAAA,uBAAA,MAAA;AACA,UAAA,SAAA,eAAA,MAAA;AACA,UAAA,SAAA,cAAA,MAAA;AACA,UAAA,SAAA,6BAAA,MAAA;AACA,UAAA,SAAA,WAAA,MAAA;AACA,UAAA,SAAA,aAAA,MAAA;AACA,UAAA,SAAA,cAAA,MAAA;AACA,UAAA,SAAA,WAAA,MAAA;AACA,UAAA,SAAA,mBAAA,MAAA;AACA,UAAA,SAAA,UAAA,MAAA;AAEA,UAAA,SAAA,kBAAA,MAAA;;KACD"}
@@ -0,0 +1,147 @@
1
+ //#region src/utils/unraw.ts
2
+ /**
3
+ * Parse a string as a base-16 number. This is more strict than `parseInt` as it
4
+ * will not allow any other characters, including (for example) "+", "-", and
5
+ * ".".
6
+ * @param hex A string containing a hexadecimal number.
7
+ * @returns The parsed integer, or `NaN` if the string is not a valid hex
8
+ * number.
9
+ */
10
+ function parseHexToInt(hex) {
11
+ return !hex.match(/[^a-f0-9]/i) ? parseInt(hex, 16) : NaN;
12
+ }
13
+ /**
14
+ * Check the validity and length of a hexadecimal code and optionally enforces
15
+ * a specific number of hex digits.
16
+ * @param hex The string to validate and parse.
17
+ * @param errorName The name of the error message to throw a `SyntaxError` with
18
+ * if `hex` is invalid. This is used to index `errorMessages`.
19
+ * @param enforcedLength If provided, will throw an error if `hex` is not
20
+ * exactly this many characters.
21
+ * @returns The parsed hex number as a normal number.
22
+ * @throws {SyntaxError} If the code is not valid.
23
+ */
24
+ function validateAndParseHex(hex, errorName, enforcedLength) {
25
+ const parsedHex = parseHexToInt(hex);
26
+ if (Number.isNaN(parsedHex) || enforcedLength !== void 0 && enforcedLength !== hex.length) throw new SyntaxError(errorName + ": " + hex);
27
+ return parsedHex;
28
+ }
29
+ /**
30
+ * Parse a two-digit hexadecimal character escape code.
31
+ * @param code The two-digit hexadecimal number that represents the character to
32
+ * output.
33
+ * @returns The single character represented by the code.
34
+ * @throws {SyntaxError} If the code is not valid hex or is not the right
35
+ * length.
36
+ */
37
+ function parseHexadecimalCode(code) {
38
+ const parsedCode = validateAndParseHex(code, "Malformed Hexadecimal", 2);
39
+ return String.fromCharCode(parsedCode);
40
+ }
41
+ /**
42
+ * Parse a four-digit Unicode character escape code.
43
+ * @param code The four-digit unicode number that represents the character to
44
+ * output.
45
+ * @param surrogateCode Optional four-digit unicode surrogate that represents
46
+ * the other half of the character to output.
47
+ * @returns The single character represented by the code.
48
+ * @throws {SyntaxError} If the codes are not valid hex or are not the right
49
+ * length.
50
+ */
51
+ function parseUnicodeCode(code, surrogateCode) {
52
+ const parsedCode = validateAndParseHex(code, "Malformed Unicode", 4);
53
+ if (surrogateCode !== void 0) {
54
+ const parsedSurrogateCode = validateAndParseHex(surrogateCode, "Malformed Unicode", 4);
55
+ return String.fromCharCode(parsedCode, parsedSurrogateCode);
56
+ }
57
+ return String.fromCharCode(parsedCode);
58
+ }
59
+ /**
60
+ * Test if the text is surrounded by curly braces (`{}`).
61
+ * @param text Text to check.
62
+ * @returns `true` if the text is in the form `{*}`.
63
+ */
64
+ function isCurlyBraced(text) {
65
+ return text.charAt(0) === "{" && text.charAt(text.length - 1) === "}";
66
+ }
67
+ /**
68
+ * Parse a Unicode code point character escape code.
69
+ * @param codePoint A unicode escape code point, including the surrounding curly
70
+ * braces.
71
+ * @returns The single character represented by the code.
72
+ * @throws {SyntaxError} If the code is not valid hex or does not have the
73
+ * surrounding curly braces.
74
+ */
75
+ function parseUnicodeCodePointCode(codePoint) {
76
+ if (!isCurlyBraced(codePoint)) throw new SyntaxError("Malformed Unicode: +" + codePoint);
77
+ const parsedCode = validateAndParseHex(codePoint.slice(1, -1), "Malformed Unicode");
78
+ try {
79
+ return String.fromCodePoint(parsedCode);
80
+ } catch (err) {
81
+ throw err instanceof RangeError ? /* @__PURE__ */ new SyntaxError("Code Point Limit:" + parsedCode) : err;
82
+ }
83
+ }
84
+ /**
85
+ * Map of unescaped letters to their corresponding special JS escape characters.
86
+ * Intentionally does not include characters that map to themselves like "\'".
87
+ */
88
+ var singleCharacterEscapes = new Map([
89
+ ["b", "\b"],
90
+ ["f", "\f"],
91
+ ["n", "\n"],
92
+ ["r", "\r"],
93
+ ["t", " "],
94
+ ["v", "\v"],
95
+ ["0", "\0"]
96
+ ]);
97
+ /**
98
+ * Parse a single character escape sequence and return the matching character.
99
+ * If none is matched, defaults to `code`.
100
+ * @param code A single character code.
101
+ */
102
+ function parseSingleCharacterCode(code) {
103
+ return singleCharacterEscapes.get(code) || code;
104
+ }
105
+ /**
106
+ * Matches every escape sequence possible, including invalid ones.
107
+ *
108
+ * All capture groups (described below) are unique (only one will match), except
109
+ * for 4, which can only potentially match if 3 does.
110
+ *
111
+ * **Capture Groups:**
112
+ * 0. A single backslash
113
+ * 1. Hexadecimal code
114
+ * 2. Unicode code point code with surrounding curly braces
115
+ * 3. Unicode escape code with surrogate
116
+ * 4. Surrogate code
117
+ * 5. Unicode escape code without surrogate
118
+ * 6. Octal code _NOTE: includes "0"._
119
+ * 7. A single character (will never be \, x, u, or 0-3)
120
+ */
121
+ var escapeMatch = /\\(?:(\\)|x([\s\S]{0,2})|u(\{[^}]*\}?)|u([\s\S]{4})\\u([^{][\s\S]{0,3})|u([\s\S]{0,4})|([0-3]?[0-7]{1,2})|([\s\S])|$)/g;
122
+ /**
123
+ * Replace raw escape character strings with their escape characters.
124
+ * @param raw A string where escape characters are represented as raw string
125
+ * values like `\'` rather than `'`.
126
+ * @param allowOctals If `true`, will process the now-deprecated octal escape
127
+ * sequences (ie, `\111`).
128
+ * @returns The processed string, with escape characters replaced by their
129
+ * respective actual Unicode characters.
130
+ */
131
+ function unraw(raw) {
132
+ return raw.replace(escapeMatch, function(_, backslash, hex, codePoint, unicodeWithSurrogate, surrogate, unicode, octal, singleCharacter) {
133
+ if (backslash !== void 0) return "\\";
134
+ if (hex !== void 0) return parseHexadecimalCode(hex);
135
+ if (codePoint !== void 0) return parseUnicodeCodePointCode(codePoint);
136
+ if (unicodeWithSurrogate !== void 0) return parseUnicodeCode(unicodeWithSurrogate, surrogate);
137
+ if (unicode !== void 0) return parseUnicodeCode(unicode);
138
+ if (octal === "0") return "\0";
139
+ if (octal !== void 0) throw new SyntaxError("Octal Deprecation: " + octal);
140
+ if (singleCharacter !== void 0) return parseSingleCharacterCode(singleCharacter);
141
+ throw new SyntaxError("End of string");
142
+ });
143
+ }
144
+ //#endregion
145
+ export { unraw as default };
146
+
147
+ //# sourceMappingURL=unraw.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unraw.js","names":[],"sources":["../../../src/utils/unraw.ts"],"sourcesContent":["/**\n * Parse a string as a base-16 number. This is more strict than `parseInt` as it\n * will not allow any other characters, including (for example) \"+\", \"-\", and\n * \".\".\n * @param hex A string containing a hexadecimal number.\n * @returns The parsed integer, or `NaN` if the string is not a valid hex\n * number.\n */\nfunction parseHexToInt(hex: string): number {\n const isOnlyHexChars = !hex.match(/[^a-f0-9]/i);\n return isOnlyHexChars ? parseInt(hex, 16) : NaN;\n}\n\n/**\n * Check the validity and length of a hexadecimal code and optionally enforces\n * a specific number of hex digits.\n * @param hex The string to validate and parse.\n * @param errorName The name of the error message to throw a `SyntaxError` with\n * if `hex` is invalid. This is used to index `errorMessages`.\n * @param enforcedLength If provided, will throw an error if `hex` is not\n * exactly this many characters.\n * @returns The parsed hex number as a normal number.\n * @throws {SyntaxError} If the code is not valid.\n */\nfunction validateAndParseHex(hex: string, errorName: string, enforcedLength?: number): number {\n const parsedHex = parseHexToInt(hex);\n if (Number.isNaN(parsedHex) || (enforcedLength !== undefined && enforcedLength !== hex.length)) {\n throw new SyntaxError(errorName + ': ' + hex);\n }\n return parsedHex;\n}\n\n/**\n * Parse a two-digit hexadecimal character escape code.\n * @param code The two-digit hexadecimal number that represents the character to\n * output.\n * @returns The single character represented by the code.\n * @throws {SyntaxError} If the code is not valid hex or is not the right\n * length.\n */\nfunction parseHexadecimalCode(code: string): string {\n const parsedCode = validateAndParseHex(code, 'Malformed Hexadecimal', 2);\n return String.fromCharCode(parsedCode);\n}\n\n/**\n * Parse a four-digit Unicode character escape code.\n * @param code The four-digit unicode number that represents the character to\n * output.\n * @param surrogateCode Optional four-digit unicode surrogate that represents\n * the other half of the character to output.\n * @returns The single character represented by the code.\n * @throws {SyntaxError} If the codes are not valid hex or are not the right\n * length.\n */\nfunction parseUnicodeCode(code: string, surrogateCode?: string): string {\n const parsedCode = validateAndParseHex(code, 'Malformed Unicode', 4);\n\n if (surrogateCode !== undefined) {\n const parsedSurrogateCode = validateAndParseHex(surrogateCode, 'Malformed Unicode', 4);\n return String.fromCharCode(parsedCode, parsedSurrogateCode);\n }\n\n return String.fromCharCode(parsedCode);\n}\n\n/**\n * Test if the text is surrounded by curly braces (`{}`).\n * @param text Text to check.\n * @returns `true` if the text is in the form `{*}`.\n */\nfunction isCurlyBraced(text: string): boolean {\n return text.charAt(0) === '{' && text.charAt(text.length - 1) === '}';\n}\n\n/**\n * Parse a Unicode code point character escape code.\n * @param codePoint A unicode escape code point, including the surrounding curly\n * braces.\n * @returns The single character represented by the code.\n * @throws {SyntaxError} If the code is not valid hex or does not have the\n * surrounding curly braces.\n */\nfunction parseUnicodeCodePointCode(codePoint: string): string {\n if (!isCurlyBraced(codePoint)) {\n throw new SyntaxError('Malformed Unicode: +' + codePoint);\n }\n const withoutBraces = codePoint.slice(1, -1);\n const parsedCode = validateAndParseHex(withoutBraces, 'Malformed Unicode');\n\n try {\n return String.fromCodePoint(parsedCode);\n } catch (err) {\n throw err instanceof RangeError ? new SyntaxError('Code Point Limit:' + parsedCode) : err;\n }\n}\n\n/**\n * Map of unescaped letters to their corresponding special JS escape characters.\n * Intentionally does not include characters that map to themselves like \"\\'\".\n */\nconst singleCharacterEscapes = new Map<string, string>([\n ['b', '\\b'],\n ['f', '\\f'],\n ['n', '\\n'],\n ['r', '\\r'],\n ['t', '\\t'],\n ['v', '\\v'],\n ['0', '\\0'],\n]);\n\n/**\n * Parse a single character escape sequence and return the matching character.\n * If none is matched, defaults to `code`.\n * @param code A single character code.\n */\nfunction parseSingleCharacterCode(code: string): string {\n return singleCharacterEscapes.get(code) || code;\n}\n\n/**\n * Matches every escape sequence possible, including invalid ones.\n *\n * All capture groups (described below) are unique (only one will match), except\n * for 4, which can only potentially match if 3 does.\n *\n * **Capture Groups:**\n * 0. A single backslash\n * 1. Hexadecimal code\n * 2. Unicode code point code with surrounding curly braces\n * 3. Unicode escape code with surrogate\n * 4. Surrogate code\n * 5. Unicode escape code without surrogate\n * 6. Octal code _NOTE: includes \"0\"._\n * 7. A single character (will never be \\, x, u, or 0-3)\n */\nconst escapeMatch =\n /\\\\(?:(\\\\)|x([\\s\\S]{0,2})|u(\\{[^}]*\\}?)|u([\\s\\S]{4})\\\\u([^{][\\s\\S]{0,3})|u([\\s\\S]{0,4})|([0-3]?[0-7]{1,2})|([\\s\\S])|$)/g;\n\n/**\n * Replace raw escape character strings with their escape characters.\n * @param raw A string where escape characters are represented as raw string\n * values like `\\'` rather than `'`.\n * @param allowOctals If `true`, will process the now-deprecated octal escape\n * sequences (ie, `\\111`).\n * @returns The processed string, with escape characters replaced by their\n * respective actual Unicode characters.\n */\nexport function unraw(raw: string): string {\n return raw.replace(\n escapeMatch,\n function (\n _,\n backslash?: string,\n hex?: string,\n codePoint?: string,\n unicodeWithSurrogate?: string,\n surrogate?: string,\n unicode?: string,\n octal?: string,\n singleCharacter?: string,\n ): string {\n // Compare groups to undefined because empty strings mean different errors\n // Otherwise, `\\u` would fail the same as `\\` which is wrong.\n if (backslash !== undefined) {\n return '\\\\';\n }\n if (hex !== undefined) {\n return parseHexadecimalCode(hex);\n }\n if (codePoint !== undefined) {\n return parseUnicodeCodePointCode(codePoint);\n }\n if (unicodeWithSurrogate !== undefined) {\n return parseUnicodeCode(unicodeWithSurrogate, surrogate);\n }\n if (unicode !== undefined) {\n return parseUnicodeCode(unicode);\n }\n if (octal === '0') {\n return '\\0';\n }\n if (octal !== undefined) {\n throw new SyntaxError('Octal Deprecation: ' + octal);\n }\n if (singleCharacter !== undefined) {\n return parseSingleCharacterCode(singleCharacter);\n }\n throw new SyntaxError('End of string');\n },\n );\n}\nexport default unraw;\n"],"mappings":";;;;;;;;;AAQA,SAAS,cAAc,KAAqB;AAE1C,QADuB,CAAC,IAAI,MAAM,aAAa,GACvB,SAAS,KAAK,GAAG,GAAG;;;;;;;;;;;;;AAc9C,SAAS,oBAAoB,KAAa,WAAmB,gBAAiC;CAC5F,MAAM,YAAY,cAAc,IAAI;AACpC,KAAI,OAAO,MAAM,UAAU,IAAK,mBAAmB,KAAA,KAAa,mBAAmB,IAAI,OACrF,OAAM,IAAI,YAAY,YAAY,OAAO,IAAI;AAE/C,QAAO;;;;;;;;;;AAWT,SAAS,qBAAqB,MAAsB;CAClD,MAAM,aAAa,oBAAoB,MAAM,yBAAyB,EAAE;AACxE,QAAO,OAAO,aAAa,WAAW;;;;;;;;;;;;AAaxC,SAAS,iBAAiB,MAAc,eAAgC;CACtE,MAAM,aAAa,oBAAoB,MAAM,qBAAqB,EAAE;AAEpE,KAAI,kBAAkB,KAAA,GAAW;EAC/B,MAAM,sBAAsB,oBAAoB,eAAe,qBAAqB,EAAE;AACtF,SAAO,OAAO,aAAa,YAAY,oBAAoB;;AAG7D,QAAO,OAAO,aAAa,WAAW;;;;;;;AAQxC,SAAS,cAAc,MAAuB;AAC5C,QAAO,KAAK,OAAO,EAAE,KAAK,OAAO,KAAK,OAAO,KAAK,SAAS,EAAE,KAAK;;;;;;;;;;AAWpE,SAAS,0BAA0B,WAA2B;AAC5D,KAAI,CAAC,cAAc,UAAU,CAC3B,OAAM,IAAI,YAAY,yBAAyB,UAAU;CAG3D,MAAM,aAAa,oBADG,UAAU,MAAM,GAAG,GAAG,EACU,oBAAoB;AAE1E,KAAI;AACF,SAA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