@poe-platform/safe-js 0.1.799 → 0.1.801
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.
- package/README.md +4 -2
- package/dist/safe-js/chunks/{chunk-KULEMOAE.js → chunk-4KMUWNZD.js} +5 -156
- package/dist/safe-js/chunks/{chunk-ULPZIHB5.js → chunk-5RLD4TVI.js} +174 -127
- package/dist/safe-js/chunks/{chunk-MCFRB5X3.js → chunk-6H5VC5W5.js} +84 -14
- package/dist/safe-js/chunks/{chunk-TBZILU4U.js → chunk-7THYP46K.js} +1 -1
- package/dist/safe-js/chunks/{chunk-UU4T2KIT.js → chunk-CK3YK2EC.js} +1 -1
- package/dist/safe-js/chunks/{chunk-3O7HA6WT.js → chunk-F6Y734YA.js} +2 -2
- package/dist/safe-js/chunks/{chunk-M6S3F3WM.js → chunk-HFRBLE6Y.js} +42 -36
- package/dist/safe-js/chunks/{chunk-OPYN6N5M.js → chunk-HPHCQNFV.js} +154 -1
- package/dist/safe-js/chunks/{chunk-W6C3Q6U4.js → chunk-IB6WAQAE.js} +2 -2
- package/dist/safe-js/chunks/{chunk-ZKLJW27N.js → chunk-PMOXSLM5.js} +1 -1
- package/dist/safe-js/chunks/chunk-QRWRR6IA.js +78 -0
- package/dist/safe-js/chunks/{chunk-MDJ7MJYI.js → chunk-XOMGWE37.js} +2 -2
- package/dist/safe-js/chunks/{chunk-QTCPJ7FN.js → chunk-XV5OYKND.js} +1 -1
- package/dist/safe-js/chunks/{chunk-OTVZWRYE.js → chunk-YCFOANSU.js} +23 -18
- package/dist/safe-js/chunks/{cli-runtime-DYXJZCGV.js → cli-runtime-L4IABN2W.js} +15 -15
- package/dist/safe-js/chunks/{dump-XDJEF2LG.js → dump-KUPBSXSL.js} +2 -2
- package/dist/safe-js/chunks/{harness-32OOFC5A.js → harness-3DDUS3RY.js} +2 -2
- package/dist/safe-js/chunks/{mcp-PTCA53MN.js → mcp-5TMFK3UE.js} +3 -3
- package/dist/safe-js/chunks/{migration-file-EEGIOHQB.js → migration-file-4CYSJ6LO.js} +3 -3
- package/dist/safe-js/chunks/{restore-U3CCH36B.js → restore-GDHBT5ZD.js} +3 -3
- package/dist/safe-js/chunks/{run-Q74MU3QQ.js → run-M6N5KCPF.js} +5 -5
- package/dist/safe-js/chunks/{runtime-modules-SL5D6LAJ.js → runtime-modules-MGSXY4BN.js} +2 -2
- package/dist/safe-js/cli.js +2 -2
- package/dist/safe-js/core.d.ts +6 -0
- package/dist/safe-js/core.js +33 -7
- package/dist/safe-js/index.js +24 -93
- package/dist/safe-js/interp/data-copy.d.ts +3 -2
- package/dist/safe-js/interp/host-bridge.d.ts +5 -4
- package/dist/safe-js/modules/fs-config.d.ts +1 -0
- package/dist/safe-js/modules/fs.d.ts +2 -0
- package/dist/safe-js/modules/fs.js +2 -2
- package/dist/safe-js/node-unavailable.d.ts +1 -0
- package/dist/safe-js/portable/chunks/chunk-2FNMGWDK.js +58703 -0
- package/dist/safe-js/portable/chunks/chunk-H5X3T5VP.js +909 -0
- package/dist/safe-js/portable/chunks/chunk-XCTU25SU.js +111 -0
- package/dist/safe-js/portable/chunks/source-files-CVU252FB.js +7 -0
- package/dist/safe-js/portable/core.js +14763 -0
- package/dist/safe-js/portable/modules/fs.js +9 -0
- package/dist/safe-js/snapshot/arguments.d.ts +2 -0
- package/dist/safe-js/snapshot/property-descriptors.d.ts +2 -0
- package/dist/safe-js/snapshot/regexp-properties.d.ts +2 -0
- package/dist/safe-js/snapshot/symbols.d.ts +2 -0
- package/dist/safe-js/workerd.d.ts +3 -0
- package/dist/safe-js/workerd.js +74436 -0
- package/package.json +30 -6
|
@@ -0,0 +1,909 @@
|
|
|
1
|
+
// packages/safe-js/src/interp/internal-symbols.ts
|
|
2
|
+
var internalSymbols = /* @__PURE__ */ new Set();
|
|
3
|
+
|
|
4
|
+
// packages/safe-js/src/error/shape.ts
|
|
5
|
+
var wrappedErrorCause = /* @__PURE__ */ Symbol("wrappedErrorCause");
|
|
6
|
+
internalSymbols.add(wrappedErrorCause);
|
|
7
|
+
var sandboxErrorNames = [
|
|
8
|
+
"Error",
|
|
9
|
+
"TypeError",
|
|
10
|
+
"RangeError",
|
|
11
|
+
"ReferenceError",
|
|
12
|
+
"SyntaxError",
|
|
13
|
+
"URIError",
|
|
14
|
+
"EvalError",
|
|
15
|
+
"AbortError",
|
|
16
|
+
"AggregateError",
|
|
17
|
+
"SuppressedError",
|
|
18
|
+
"HarnessFailure"
|
|
19
|
+
];
|
|
20
|
+
var sandboxErrorTypes = /* @__PURE__ */ new WeakMap();
|
|
21
|
+
var hostErrorData = /* @__PURE__ */ new WeakMap();
|
|
22
|
+
function formatErrorStack(name, message, stackFrames = []) {
|
|
23
|
+
const header = message.length === 0 ? name : `${name}: ${message}`;
|
|
24
|
+
return [header, ...stackFrames].join("\n");
|
|
25
|
+
}
|
|
26
|
+
function replaceErrorStack(error, stackFrames = []) {
|
|
27
|
+
error.stack = formatErrorStack(error.name, error.message, stackFrames);
|
|
28
|
+
}
|
|
29
|
+
function attachErrorSpan(error, span) {
|
|
30
|
+
if (span === void 0 || hasOwnProperty(error, "span")) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
Object.defineProperty(error, "span", {
|
|
34
|
+
configurable: true,
|
|
35
|
+
value: span
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function attachErrorCause(error, cause) {
|
|
39
|
+
if (cause === void 0 || hasOwnProperty(error, "cause")) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
Object.defineProperty(error, "cause", {
|
|
43
|
+
configurable: true,
|
|
44
|
+
value: cause
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function attachWrappedErrorCause(error, cause) {
|
|
48
|
+
if (cause === void 0 || wrappedErrorCause in error) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
Object.defineProperty(error, wrappedErrorCause, {
|
|
52
|
+
configurable: true,
|
|
53
|
+
value: cause
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
function materializeWrappedErrorCause(value) {
|
|
57
|
+
if (typeof value !== "object" || value === null || !(wrappedErrorCause in value)) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
attachErrorCause(value, value[wrappedErrorCause]);
|
|
61
|
+
}
|
|
62
|
+
function readErrorSpan(value) {
|
|
63
|
+
if (typeof value !== "object" || value === null || !hasOwnProperty(value, "span")) {
|
|
64
|
+
return void 0;
|
|
65
|
+
}
|
|
66
|
+
const span = value.span;
|
|
67
|
+
return isErrorSourceSpan(span) ? span : void 0;
|
|
68
|
+
}
|
|
69
|
+
function readErrorCause(value) {
|
|
70
|
+
return typeof value === "object" && value !== null && hasOwnProperty(value, "cause") ? value.cause : void 0;
|
|
71
|
+
}
|
|
72
|
+
function isErrorSourceSpan(value) {
|
|
73
|
+
if (typeof value !== "object" || value === null || !hasOwnProperty(value, "start") || !hasOwnProperty(value, "end")) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return isErrorSourcePosition(value.start) && isErrorSourcePosition(value.end);
|
|
77
|
+
}
|
|
78
|
+
function createSourceSpan(source, line, column, endLine, endColumn) {
|
|
79
|
+
const start = createSourcePosition(source, line, column);
|
|
80
|
+
const rawEnd = createSourcePosition(source, endLine, endColumn);
|
|
81
|
+
const end = rawEnd.offset > start.offset ? rawEnd : createSourcePositionFromOffset(source, Math.min(start.offset + 1, source.length));
|
|
82
|
+
return { start, end };
|
|
83
|
+
}
|
|
84
|
+
function describeThrownValue(value) {
|
|
85
|
+
if (value instanceof Error) {
|
|
86
|
+
return value.message.length > 0 ? `${value.name}: ${value.message}` : value.name;
|
|
87
|
+
}
|
|
88
|
+
if (typeof value === "string") {
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
if (value === void 0 || value === null || typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" || typeof value === "symbol") {
|
|
92
|
+
return String(value);
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
const serialized = JSON.stringify(value);
|
|
96
|
+
return serialized === void 0 ? String(value) : serialized;
|
|
97
|
+
} catch {
|
|
98
|
+
return String(value);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function isErrorSourcePosition(value) {
|
|
102
|
+
return typeof value === "object" && value !== null && hasOwnProperty(value, "line") && typeof value.line === "number" && hasOwnProperty(value, "column") && typeof value.column === "number" && hasOwnProperty(value, "offset") && typeof value.offset === "number";
|
|
103
|
+
}
|
|
104
|
+
function hasOwnProperty(value, name) {
|
|
105
|
+
return Object.prototype.hasOwnProperty.call(value, name);
|
|
106
|
+
}
|
|
107
|
+
function createSourcePosition(source, line, column) {
|
|
108
|
+
return createSourcePositionFromOffset(source, findSourceOffset(source, line, column));
|
|
109
|
+
}
|
|
110
|
+
function createSourcePositionFromOffset(source, offset) {
|
|
111
|
+
let line = 1;
|
|
112
|
+
let column = 1;
|
|
113
|
+
for (let index = 0; index < offset; index += 1) {
|
|
114
|
+
const character = source[index];
|
|
115
|
+
if (character === "\r" || character === "\n" || character === "\u2028" || character === "\u2029") {
|
|
116
|
+
if (character === "\r" && source[index + 1] === "\n" && index + 1 < offset) index += 1;
|
|
117
|
+
line += 1;
|
|
118
|
+
column = 1;
|
|
119
|
+
} else {
|
|
120
|
+
column += 1;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
column,
|
|
125
|
+
line,
|
|
126
|
+
offset
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function findSourceOffset(source, line, column) {
|
|
130
|
+
let currentLine = 1;
|
|
131
|
+
let currentColumn = 1;
|
|
132
|
+
for (let offset = 0; offset <= source.length; offset += 1) {
|
|
133
|
+
if (currentLine === line && currentColumn === column) {
|
|
134
|
+
return offset;
|
|
135
|
+
}
|
|
136
|
+
const character = source[offset];
|
|
137
|
+
if (character === void 0) {
|
|
138
|
+
return source.length;
|
|
139
|
+
}
|
|
140
|
+
if (character === "\r" || character === "\n" || character === "\u2028" || character === "\u2029") {
|
|
141
|
+
if (character === "\r" && source[offset + 1] === "\n") offset += 1;
|
|
142
|
+
currentLine += 1;
|
|
143
|
+
currentColumn = 1;
|
|
144
|
+
} else {
|
|
145
|
+
currentColumn += 1;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return source.length;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// packages/safe-js/src/interp/template-cache.ts
|
|
152
|
+
var templateRealms = /* @__PURE__ */ new WeakMap();
|
|
153
|
+
function releaseTemplateObjects(budget) {
|
|
154
|
+
const realm = templateRealms.get(budget);
|
|
155
|
+
if (realm !== void 0) budget.setRetainedValues(realm, void 0);
|
|
156
|
+
templateRealms.delete(budget);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// packages/safe-js/src/interp/budget.ts
|
|
160
|
+
var REGEX_COMPILE_LIMITS = Object.freeze({
|
|
161
|
+
sourceLength: Infinity,
|
|
162
|
+
flagsLength: Infinity,
|
|
163
|
+
depth: Infinity,
|
|
164
|
+
allocations: Infinity
|
|
165
|
+
});
|
|
166
|
+
var DEADLINE_CHECK_INTERVAL = 1024;
|
|
167
|
+
var SandboxError = class extends Error {
|
|
168
|
+
code;
|
|
169
|
+
budget;
|
|
170
|
+
current;
|
|
171
|
+
limit;
|
|
172
|
+
constructor(input) {
|
|
173
|
+
super(
|
|
174
|
+
input === "aborted" ? "aborted" : input === "reentry" ? "Sandbox object is already running." : `Sandbox budget exceeded for ${input.budget}: ${input.current} > ${input.limit}.`
|
|
175
|
+
);
|
|
176
|
+
this.name = "SandboxError";
|
|
177
|
+
replaceErrorStack(this);
|
|
178
|
+
if (input === "aborted") {
|
|
179
|
+
this.code = "aborted";
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (input === "reentry") {
|
|
183
|
+
this.code = "reentry";
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
this.code = "budgetExceeded";
|
|
187
|
+
this.budget = input.budget;
|
|
188
|
+
this.current = input.current;
|
|
189
|
+
this.limit = input.limit;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
function isFatalSandboxError(error) {
|
|
193
|
+
return error instanceof SandboxError && (error.code === "budgetExceeded" || error.code === "reentry");
|
|
194
|
+
}
|
|
195
|
+
var BudgetAccounting = class {
|
|
196
|
+
deadline;
|
|
197
|
+
limits;
|
|
198
|
+
stepsUsed = 0;
|
|
199
|
+
peakCallDepth = 0;
|
|
200
|
+
currentDataSize = 0;
|
|
201
|
+
peakDataSize = 0;
|
|
202
|
+
currentCallDepth = 0;
|
|
203
|
+
allChecksSuspended = 0;
|
|
204
|
+
deadlineChecksSuspended = 0;
|
|
205
|
+
visitsUntilDeadlineCheck = DEADLINE_CHECK_INTERVAL;
|
|
206
|
+
pendingDeadlines;
|
|
207
|
+
earliestPendingDeadline;
|
|
208
|
+
retainedDataSize = 0;
|
|
209
|
+
retainedData = /* @__PURE__ */ new Map();
|
|
210
|
+
retainedValueSources = /* @__PURE__ */ new Map();
|
|
211
|
+
compileGeneration = 0;
|
|
212
|
+
activeCompileOwner;
|
|
213
|
+
defaultCompileOwner;
|
|
214
|
+
compileUses = 0;
|
|
215
|
+
provisionalScopes = 0;
|
|
216
|
+
compileTickets = /* @__PURE__ */ new Map();
|
|
217
|
+
completedCompileTickets = /* @__PURE__ */ new Set();
|
|
218
|
+
reconciliationHolds = 0;
|
|
219
|
+
deferredCompileDiscards = /* @__PURE__ */ new Set();
|
|
220
|
+
realmViews;
|
|
221
|
+
constructor(options = {}) {
|
|
222
|
+
this.deadline = normalizeDeadline(options.deadline);
|
|
223
|
+
this.limits = Object.freeze({
|
|
224
|
+
maxSteps: normalizeLimit("maxSteps", options.maxSteps),
|
|
225
|
+
maxCallDepth: normalizeLimit("maxCallDepth", options.maxCallDepth),
|
|
226
|
+
stringLength: normalizeLimit("stringLength", options.stringLength),
|
|
227
|
+
...options.regexSourceLength === void 0 ? {} : {
|
|
228
|
+
regexSourceLength: normalizeRegexLimit("regexSourceLength", options.regexSourceLength)
|
|
229
|
+
},
|
|
230
|
+
...options.regexCompileAllocations === void 0 ? {} : {
|
|
231
|
+
regexCompileAllocations: normalizeRegexLimit("regexCompileAllocations", options.regexCompileAllocations)
|
|
232
|
+
},
|
|
233
|
+
arrayLength: normalizeLimit("arrayLength", options.arrayLength),
|
|
234
|
+
dataSize: normalizeLimit("dataSize", options.dataSize)
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
var Budget = class _Budget {
|
|
239
|
+
accounting;
|
|
240
|
+
realmView = false;
|
|
241
|
+
realmLease = false;
|
|
242
|
+
constructor(options = {}) {
|
|
243
|
+
this.accounting = new BudgetAccounting(options);
|
|
244
|
+
}
|
|
245
|
+
get deadline() {
|
|
246
|
+
return this.accounting.deadline;
|
|
247
|
+
}
|
|
248
|
+
get limits() {
|
|
249
|
+
return this.accounting.limits;
|
|
250
|
+
}
|
|
251
|
+
get stepsUsed() {
|
|
252
|
+
return this.accounting.stepsUsed;
|
|
253
|
+
}
|
|
254
|
+
set stepsUsed(value) {
|
|
255
|
+
this.accounting.stepsUsed = value;
|
|
256
|
+
}
|
|
257
|
+
get peakCallDepth() {
|
|
258
|
+
return this.accounting.peakCallDepth;
|
|
259
|
+
}
|
|
260
|
+
set peakCallDepth(value) {
|
|
261
|
+
this.accounting.peakCallDepth = value;
|
|
262
|
+
}
|
|
263
|
+
get currentDataSize() {
|
|
264
|
+
return this.accounting.currentDataSize;
|
|
265
|
+
}
|
|
266
|
+
set currentDataSize(value) {
|
|
267
|
+
this.accounting.currentDataSize = value;
|
|
268
|
+
}
|
|
269
|
+
get peakDataSize() {
|
|
270
|
+
return this.accounting.peakDataSize;
|
|
271
|
+
}
|
|
272
|
+
set peakDataSize(value) {
|
|
273
|
+
this.accounting.peakDataSize = value;
|
|
274
|
+
}
|
|
275
|
+
get currentCallDepth() {
|
|
276
|
+
return this.accounting.currentCallDepth;
|
|
277
|
+
}
|
|
278
|
+
set currentCallDepth(value) {
|
|
279
|
+
this.accounting.currentCallDepth = value;
|
|
280
|
+
}
|
|
281
|
+
// Pending host operations keep their elapsed deadline active during sampled
|
|
282
|
+
// synchronous work. Linked private records need no observable native hooks.
|
|
283
|
+
acquireDeadline(deadline, onExceeded) {
|
|
284
|
+
if (!Number.isFinite(deadline)) throw new TypeError("Deadline must be a finite timestamp.");
|
|
285
|
+
const accounting = this.accounting;
|
|
286
|
+
const pending = {
|
|
287
|
+
deadline,
|
|
288
|
+
onExceeded,
|
|
289
|
+
next: accounting.pendingDeadlines,
|
|
290
|
+
active: true
|
|
291
|
+
};
|
|
292
|
+
if (pending.next !== void 0) pending.next.previous = pending;
|
|
293
|
+
accounting.pendingDeadlines = pending;
|
|
294
|
+
if (accounting.earliestPendingDeadline === void 0 || pending.deadline < accounting.earliestPendingDeadline.deadline)
|
|
295
|
+
accounting.earliestPendingDeadline = pending;
|
|
296
|
+
return () => {
|
|
297
|
+
if (!pending.active) return;
|
|
298
|
+
pending.active = false;
|
|
299
|
+
if (pending.previous === void 0) accounting.pendingDeadlines = pending.next;
|
|
300
|
+
else pending.previous.next = pending.next;
|
|
301
|
+
if (pending.next !== void 0) pending.next.previous = pending.previous;
|
|
302
|
+
pending.previous = pending.next = void 0;
|
|
303
|
+
pending.onExceeded = void 0;
|
|
304
|
+
if (accounting.earliestPendingDeadline !== pending) return;
|
|
305
|
+
accounting.earliestPendingDeadline = void 0;
|
|
306
|
+
for (let next = accounting.pendingDeadlines; next !== void 0; next = next.next)
|
|
307
|
+
if (accounting.earliestPendingDeadline === void 0 || next.deadline < accounting.earliestPendingDeadline.deadline)
|
|
308
|
+
accounting.earliestPendingDeadline = next;
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
forkRealm() {
|
|
312
|
+
const view = new _Budget();
|
|
313
|
+
view.accounting = this.accounting;
|
|
314
|
+
view.realmView = true;
|
|
315
|
+
const views = this.accounting.realmViews ??= /* @__PURE__ */ new Set([new WeakRef(this)]);
|
|
316
|
+
views.add(new WeakRef(view));
|
|
317
|
+
return view;
|
|
318
|
+
}
|
|
319
|
+
acquireRealmOwner() {
|
|
320
|
+
if (this.realmLease) throw new SandboxError("reentry");
|
|
321
|
+
const lease = this.acquireCompileOwner(
|
|
322
|
+
!this.realmView,
|
|
323
|
+
this.realmView ? this.accounting.activeCompileOwner : void 0
|
|
324
|
+
);
|
|
325
|
+
this.realmLease = true;
|
|
326
|
+
let released = false;
|
|
327
|
+
return {
|
|
328
|
+
owner: lease.owner,
|
|
329
|
+
release: () => {
|
|
330
|
+
if (released) return;
|
|
331
|
+
released = true;
|
|
332
|
+
this.realmLease = false;
|
|
333
|
+
lease.release();
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
visitNode(units = 1) {
|
|
338
|
+
if (!Number.isSafeInteger(units) || units < 0) {
|
|
339
|
+
throw new Error("units must be a non-negative safe integer.");
|
|
340
|
+
}
|
|
341
|
+
if (units === 0) return;
|
|
342
|
+
this.accounting.stepsUsed += units;
|
|
343
|
+
this.checkSampledDeadline(units);
|
|
344
|
+
if (this.accounting.allChecksSuspended === 0 && this.accounting.limits.maxSteps !== void 0 && this.accounting.stepsUsed > this.accounting.limits.maxSteps) {
|
|
345
|
+
throw new SandboxError({
|
|
346
|
+
budget: "steps",
|
|
347
|
+
current: this.accounting.stepsUsed,
|
|
348
|
+
limit: this.accounting.limits.maxSteps
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
allocateString(value) {
|
|
353
|
+
this.allocateStringLength(value.length);
|
|
354
|
+
return value;
|
|
355
|
+
}
|
|
356
|
+
allocateStringLength(length) {
|
|
357
|
+
if (this.accounting.allChecksSuspended === 0 && this.accounting.limits.stringLength !== void 0 && length > this.accounting.limits.stringLength) {
|
|
358
|
+
throw new SandboxError({
|
|
359
|
+
budget: "stringLength",
|
|
360
|
+
current: length,
|
|
361
|
+
limit: this.accounting.limits.stringLength
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
reserveString(length) {
|
|
366
|
+
if (!Number.isSafeInteger(length) || length < 0) {
|
|
367
|
+
throw new RangeError("string length must be a nonnegative safe integer");
|
|
368
|
+
}
|
|
369
|
+
if (this.accounting.allChecksSuspended === 0 && this.accounting.limits.stringLength !== void 0 && length > this.accounting.limits.stringLength) {
|
|
370
|
+
throw new SandboxError({ budget: "stringLength", current: length, limit: this.accounting.limits.stringLength });
|
|
371
|
+
}
|
|
372
|
+
return this.provisionDataUsage(length);
|
|
373
|
+
}
|
|
374
|
+
allocateArrayLength(length) {
|
|
375
|
+
if (this.accounting.allChecksSuspended === 0 && this.accounting.limits.arrayLength !== void 0 && length > this.accounting.limits.arrayLength) {
|
|
376
|
+
throw new SandboxError({
|
|
377
|
+
budget: "arrayLength",
|
|
378
|
+
current: length,
|
|
379
|
+
limit: this.accounting.limits.arrayLength
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
allocateCollectionEntries(count) {
|
|
384
|
+
this.allocateArrayLength(count);
|
|
385
|
+
}
|
|
386
|
+
reconcileDataUsage(usage) {
|
|
387
|
+
const measured = usage + this.accounting.retainedDataSize;
|
|
388
|
+
const total = this.reconciliationDeferred ? Math.max(measured, this.accounting.currentDataSize) : measured;
|
|
389
|
+
this.checkDataUsage(total);
|
|
390
|
+
this.accounting.currentDataSize = total;
|
|
391
|
+
this.accounting.peakDataSize = Math.max(this.accounting.peakDataSize, total);
|
|
392
|
+
}
|
|
393
|
+
setRetainedDataUsage(owner, usage) {
|
|
394
|
+
if (!Number.isSafeInteger(usage) || usage < 0) {
|
|
395
|
+
throw new TypeError("Retained data usage must be a non-negative safe integer.");
|
|
396
|
+
}
|
|
397
|
+
const delta = usage - (this.accounting.retainedData.get(owner) ?? 0);
|
|
398
|
+
const total = this.accounting.currentDataSize + delta;
|
|
399
|
+
this.checkDataUsage(total);
|
|
400
|
+
if (usage === 0) this.accounting.retainedData.delete(owner);
|
|
401
|
+
else this.accounting.retainedData.set(owner, usage);
|
|
402
|
+
this.accounting.retainedDataSize += delta;
|
|
403
|
+
this.accounting.currentDataSize = total;
|
|
404
|
+
this.accounting.peakDataSize = Math.max(this.accounting.peakDataSize, total);
|
|
405
|
+
}
|
|
406
|
+
setRetainedValues(owner, values) {
|
|
407
|
+
if (values === void 0) this.accounting.retainedValueSources.delete(owner);
|
|
408
|
+
else this.accounting.retainedValueSources.set(owner, values);
|
|
409
|
+
}
|
|
410
|
+
get reconciliationDeferred() {
|
|
411
|
+
return this.accounting.reconciliationHolds > 0;
|
|
412
|
+
}
|
|
413
|
+
deferReconciliation() {
|
|
414
|
+
this.accounting.reconciliationHolds++;
|
|
415
|
+
const generation = this.accounting.compileGeneration;
|
|
416
|
+
let released = false;
|
|
417
|
+
return () => {
|
|
418
|
+
if (released) return;
|
|
419
|
+
released = true;
|
|
420
|
+
if (generation !== this.accounting.compileGeneration) return;
|
|
421
|
+
if (--this.accounting.reconciliationHolds !== 0) return;
|
|
422
|
+
for (const ticket of this.accounting.deferredCompileDiscards)
|
|
423
|
+
this.accounting.completedCompileTickets.add(ticket);
|
|
424
|
+
this.accounting.deferredCompileDiscards.clear();
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
*retainedValues() {
|
|
428
|
+
for (const values of this.accounting.retainedValueSources.values()) {
|
|
429
|
+
const retained = values();
|
|
430
|
+
if (retained !== void 0) yield* retained;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
acquireCompileOwner(reset = false, owner) {
|
|
434
|
+
if (owner !== void 0 && (owner.budget.accounting !== this.accounting || owner.generation !== this.accounting.compileGeneration) || this.accounting.activeCompileOwner !== void 0 && this.accounting.activeCompileOwner !== owner || reset && (this.accounting.compileUses !== 0 || owner !== void 0)) {
|
|
435
|
+
throw new SandboxError("reentry");
|
|
436
|
+
}
|
|
437
|
+
if (reset) this.reset();
|
|
438
|
+
const selected = owner ?? (this.accounting.defaultCompileOwner ??= Object.freeze({ budget: this, generation: this.accounting.compileGeneration }));
|
|
439
|
+
this.accounting.activeCompileOwner = selected;
|
|
440
|
+
this.accounting.compileUses += 1;
|
|
441
|
+
let released = false;
|
|
442
|
+
return {
|
|
443
|
+
owner: selected,
|
|
444
|
+
release: () => {
|
|
445
|
+
if (released) return;
|
|
446
|
+
released = true;
|
|
447
|
+
this.accounting.compileUses -= 1;
|
|
448
|
+
if (this.accounting.compileUses === 0) this.accounting.activeCompileOwner = void 0;
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
createCompileTicket(owner) {
|
|
453
|
+
if (owner.budget.accounting !== this.accounting || owner.generation !== this.accounting.compileGeneration || this.accounting.activeCompileOwner !== owner) {
|
|
454
|
+
throw new SandboxError("reentry");
|
|
455
|
+
}
|
|
456
|
+
const ticket = Object.freeze({ owner });
|
|
457
|
+
this.accounting.compileTickets.set(ticket, 0);
|
|
458
|
+
return ticket;
|
|
459
|
+
}
|
|
460
|
+
compileTicketUsage(ticket) {
|
|
461
|
+
return ticket.owner.generation === this.accounting.compileGeneration ? this.accounting.compileTickets.get(ticket) ?? 0 : 0;
|
|
462
|
+
}
|
|
463
|
+
resizeCompileTicket(ticket, usage) {
|
|
464
|
+
if (ticket.owner.generation !== this.accounting.compileGeneration || !this.accounting.compileTickets.has(ticket)) {
|
|
465
|
+
throw new SandboxError("reentry");
|
|
466
|
+
}
|
|
467
|
+
if (this.reconciliationDeferred) usage = Math.max(usage, this.compileTicketUsage(ticket));
|
|
468
|
+
this.setRetainedDataUsage(ticket, usage);
|
|
469
|
+
this.accounting.compileTickets.set(ticket, usage);
|
|
470
|
+
}
|
|
471
|
+
discardCompileTicket(ticket) {
|
|
472
|
+
if (ticket.owner.generation !== this.accounting.compileGeneration) return;
|
|
473
|
+
const usage = this.accounting.compileTickets.get(ticket);
|
|
474
|
+
if (usage === void 0) return;
|
|
475
|
+
if (this.reconciliationDeferred) {
|
|
476
|
+
this.accounting.deferredCompileDiscards.add(ticket);
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
this.accounting.compileTickets.delete(ticket);
|
|
480
|
+
this.accounting.completedCompileTickets.delete(ticket);
|
|
481
|
+
this.accounting.retainedData.delete(ticket);
|
|
482
|
+
this.accounting.retainedDataSize -= usage;
|
|
483
|
+
this.accounting.currentDataSize -= usage;
|
|
484
|
+
}
|
|
485
|
+
reconcileCompileData(usage, included, transferred = included, retainedOwner, complete = false) {
|
|
486
|
+
if (this.reconciliationDeferred) {
|
|
487
|
+
if (retainedOwner !== void 0) this.setRetainedDataUsage(retainedOwner, usage);
|
|
488
|
+
else {
|
|
489
|
+
let includedUsage2 = 0;
|
|
490
|
+
for (const ticket of included) includedUsage2 += this.compileTicketUsage(ticket);
|
|
491
|
+
const measured2 = usage + this.accounting.retainedDataSize - includedUsage2;
|
|
492
|
+
const total2 = Math.max(measured2, this.accounting.currentDataSize);
|
|
493
|
+
this.checkDataUsage(total2);
|
|
494
|
+
this.accounting.currentDataSize = total2;
|
|
495
|
+
this.accounting.peakDataSize = Math.max(this.accounting.peakDataSize, total2);
|
|
496
|
+
}
|
|
497
|
+
return /* @__PURE__ */ new Set();
|
|
498
|
+
}
|
|
499
|
+
let includedUsage = 0;
|
|
500
|
+
let transferredUsage = 0;
|
|
501
|
+
let discardedUsage = 0;
|
|
502
|
+
const releasing = [];
|
|
503
|
+
const retained = /* @__PURE__ */ new Set();
|
|
504
|
+
for (const ticket of included) {
|
|
505
|
+
const charge = this.compileTicketUsage(ticket);
|
|
506
|
+
includedUsage += charge;
|
|
507
|
+
if (charge > 0 && transferred.has(ticket) && (this.accounting.provisionalScopes === 0 || retainedOwner !== void 0)) {
|
|
508
|
+
releasing.push(ticket);
|
|
509
|
+
transferredUsage += charge;
|
|
510
|
+
} else if (charge > 0 && complete && transferred.has(ticket)) {
|
|
511
|
+
retained.add(ticket);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
if (retainedOwner === void 0) {
|
|
515
|
+
for (const ticket of this.accounting.completedCompileTickets) {
|
|
516
|
+
if (included.has(ticket)) continue;
|
|
517
|
+
releasing.push(ticket);
|
|
518
|
+
discardedUsage += this.compileTicketUsage(ticket);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
const oldOwnerUsage = retainedOwner === void 0 ? 0 : this.accounting.retainedData.get(retainedOwner) ?? 0;
|
|
522
|
+
const nextRetained = this.accounting.retainedDataSize - transferredUsage - discardedUsage + (retainedOwner === void 0 ? 0 : usage - oldOwnerUsage);
|
|
523
|
+
const measured = retainedOwner === void 0 ? usage - includedUsage + transferredUsage : this.accounting.currentDataSize - this.accounting.retainedDataSize;
|
|
524
|
+
const total = measured + nextRetained;
|
|
525
|
+
this.checkDataUsage(total);
|
|
526
|
+
for (const ticket of releasing) {
|
|
527
|
+
this.accounting.compileTickets.delete(ticket);
|
|
528
|
+
this.accounting.completedCompileTickets.delete(ticket);
|
|
529
|
+
this.accounting.retainedData.delete(ticket);
|
|
530
|
+
}
|
|
531
|
+
if (retainedOwner !== void 0) {
|
|
532
|
+
if (usage === 0) this.accounting.retainedData.delete(retainedOwner);
|
|
533
|
+
else this.accounting.retainedData.set(retainedOwner, usage);
|
|
534
|
+
}
|
|
535
|
+
this.accounting.retainedDataSize = nextRetained;
|
|
536
|
+
this.accounting.currentDataSize = total;
|
|
537
|
+
this.accounting.peakDataSize = Math.max(this.accounting.peakDataSize, total);
|
|
538
|
+
for (const ticket of retained) this.accounting.completedCompileTickets.add(ticket);
|
|
539
|
+
return retained;
|
|
540
|
+
}
|
|
541
|
+
chargeDataUsage(usage) {
|
|
542
|
+
const total = this.accounting.currentDataSize + usage;
|
|
543
|
+
this.checkDataUsage(total);
|
|
544
|
+
this.accounting.currentDataSize = total;
|
|
545
|
+
this.accounting.peakDataSize = Math.max(this.accounting.peakDataSize, total);
|
|
546
|
+
}
|
|
547
|
+
provisionDataUsage(usage) {
|
|
548
|
+
const previous = this.accounting.currentDataSize;
|
|
549
|
+
const previousRetained = this.accounting.retainedDataSize;
|
|
550
|
+
const next = previous + usage;
|
|
551
|
+
this.checkDataUsage(next);
|
|
552
|
+
this.accounting.currentDataSize = next;
|
|
553
|
+
this.accounting.peakDataSize = Math.max(this.accounting.peakDataSize, next);
|
|
554
|
+
const generation = this.accounting.compileGeneration;
|
|
555
|
+
this.accounting.provisionalScopes += 1;
|
|
556
|
+
let released = false;
|
|
557
|
+
return (commit = false) => {
|
|
558
|
+
if (released) return;
|
|
559
|
+
released = true;
|
|
560
|
+
if (generation !== this.accounting.compileGeneration) return;
|
|
561
|
+
this.accounting.provisionalScopes -= 1;
|
|
562
|
+
if (!commit) {
|
|
563
|
+
const restored = previous + this.accounting.retainedDataSize - previousRetained;
|
|
564
|
+
this.accounting.currentDataSize = this.reconciliationDeferred ? Math.max(restored, this.accounting.currentDataSize) : restored;
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
enterCall() {
|
|
569
|
+
return this.enterDepth();
|
|
570
|
+
}
|
|
571
|
+
enterAwait() {
|
|
572
|
+
return this.enterDepth();
|
|
573
|
+
}
|
|
574
|
+
reset() {
|
|
575
|
+
if (this.accounting.compileUses !== 0) throw new SandboxError("reentry");
|
|
576
|
+
releaseTemplateObjects(this);
|
|
577
|
+
for (const reference of this.accounting.realmViews ?? []) {
|
|
578
|
+
const view = reference.deref();
|
|
579
|
+
if (view === void 0) this.accounting.realmViews.delete(reference);
|
|
580
|
+
else if (view !== this) releaseTemplateObjects(view);
|
|
581
|
+
}
|
|
582
|
+
this.accounting.compileGeneration += 1;
|
|
583
|
+
this.accounting.defaultCompileOwner = void 0;
|
|
584
|
+
this.accounting.provisionalScopes = 0;
|
|
585
|
+
this.accounting.compileTickets.clear();
|
|
586
|
+
this.accounting.completedCompileTickets.clear();
|
|
587
|
+
this.accounting.reconciliationHolds = 0;
|
|
588
|
+
this.accounting.deferredCompileDiscards.clear();
|
|
589
|
+
this.accounting.stepsUsed = 0;
|
|
590
|
+
this.accounting.peakCallDepth = 0;
|
|
591
|
+
this.accounting.currentCallDepth = 0;
|
|
592
|
+
this.accounting.currentDataSize = 0;
|
|
593
|
+
this.accounting.peakDataSize = 0;
|
|
594
|
+
this.accounting.retainedDataSize = 0;
|
|
595
|
+
this.accounting.retainedData.clear();
|
|
596
|
+
this.accounting.retainedValueSources.clear();
|
|
597
|
+
this.accounting.allChecksSuspended = 0;
|
|
598
|
+
this.accounting.deadlineChecksSuspended = 0;
|
|
599
|
+
this.accounting.visitsUntilDeadlineCheck = DEADLINE_CHECK_INTERVAL;
|
|
600
|
+
}
|
|
601
|
+
suspendChecks() {
|
|
602
|
+
this.accounting.allChecksSuspended += 1;
|
|
603
|
+
let resumed = false;
|
|
604
|
+
return () => {
|
|
605
|
+
if (resumed) {
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
resumed = true;
|
|
609
|
+
this.accounting.allChecksSuspended -= 1;
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
suspendDeadlineChecks() {
|
|
613
|
+
this.accounting.deadlineChecksSuspended += 1;
|
|
614
|
+
let resumed = false;
|
|
615
|
+
return () => {
|
|
616
|
+
if (resumed) {
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
resumed = true;
|
|
620
|
+
this.accounting.deadlineChecksSuspended -= 1;
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
checkDeadline() {
|
|
624
|
+
const pending = this.accounting.earliestPendingDeadline;
|
|
625
|
+
const deadline = this.accounting.deadline === void 0 ? pending?.deadline : Math.min(this.accounting.deadline, pending?.deadline ?? Infinity);
|
|
626
|
+
if (this.accounting.allChecksSuspended > 0 || this.accounting.deadlineChecksSuspended > 0 || deadline === void 0) {
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
const now = Date.now();
|
|
630
|
+
if (now <= deadline) {
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
const reason = new SandboxError({
|
|
634
|
+
budget: "deadline",
|
|
635
|
+
current: now,
|
|
636
|
+
limit: deadline
|
|
637
|
+
});
|
|
638
|
+
if (pending?.deadline === deadline) {
|
|
639
|
+
const notify = pending.onExceeded;
|
|
640
|
+
pending.onExceeded = void 0;
|
|
641
|
+
notify?.(reason);
|
|
642
|
+
}
|
|
643
|
+
throw reason;
|
|
644
|
+
}
|
|
645
|
+
checkDataUsage(usage) {
|
|
646
|
+
if (this.accounting.allChecksSuspended === 0 && this.accounting.limits.dataSize !== void 0 && usage > this.accounting.limits.dataSize) {
|
|
647
|
+
throw new SandboxError({
|
|
648
|
+
budget: "dataSize",
|
|
649
|
+
current: usage,
|
|
650
|
+
limit: this.accounting.limits.dataSize
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
checkSampledDeadline(units) {
|
|
655
|
+
if (this.accounting.allChecksSuspended > 0 || this.accounting.deadlineChecksSuspended > 0 || this.accounting.deadline === void 0 && this.accounting.earliestPendingDeadline === void 0) {
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
this.accounting.visitsUntilDeadlineCheck -= units;
|
|
659
|
+
if (this.accounting.visitsUntilDeadlineCheck > 0) {
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
this.accounting.visitsUntilDeadlineCheck = DEADLINE_CHECK_INTERVAL - -this.accounting.visitsUntilDeadlineCheck % DEADLINE_CHECK_INTERVAL;
|
|
663
|
+
this.checkDeadline();
|
|
664
|
+
}
|
|
665
|
+
enterDepth() {
|
|
666
|
+
const nextDepth = this.accounting.currentCallDepth + 1;
|
|
667
|
+
if (this.accounting.allChecksSuspended === 0 && this.accounting.limits.maxCallDepth !== void 0 && nextDepth > this.accounting.limits.maxCallDepth) {
|
|
668
|
+
throw new SandboxError({
|
|
669
|
+
budget: "callDepth",
|
|
670
|
+
current: nextDepth,
|
|
671
|
+
limit: this.accounting.limits.maxCallDepth
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
this.accounting.currentCallDepth = nextDepth;
|
|
675
|
+
if (nextDepth > this.accounting.peakCallDepth) {
|
|
676
|
+
this.accounting.peakCallDepth = nextDepth;
|
|
677
|
+
}
|
|
678
|
+
let left = false;
|
|
679
|
+
return () => {
|
|
680
|
+
if (left) {
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
left = true;
|
|
684
|
+
this.accounting.currentCallDepth -= 1;
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
function normalizeDeadline(deadline) {
|
|
689
|
+
if (deadline === void 0 || deadline === Infinity) {
|
|
690
|
+
return void 0;
|
|
691
|
+
}
|
|
692
|
+
return deadline instanceof Date ? deadline.getTime() : deadline;
|
|
693
|
+
}
|
|
694
|
+
function normalizeLimit(name, value) {
|
|
695
|
+
if (value === void 0 || value === Infinity) {
|
|
696
|
+
return void 0;
|
|
697
|
+
}
|
|
698
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
699
|
+
throw new Error(`${name} must be a non-negative integer.`);
|
|
700
|
+
}
|
|
701
|
+
return value;
|
|
702
|
+
}
|
|
703
|
+
function normalizeRegexLimit(name, value) {
|
|
704
|
+
if (value === void 0 || value === Infinity) return void 0;
|
|
705
|
+
if (!Number.isSafeInteger(value) || value < 1)
|
|
706
|
+
throw new RangeError(`${name} must be a positive safe integer.`);
|
|
707
|
+
return value;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// packages/safe-js/src/platform/context.ts
|
|
711
|
+
var StackContext = class _StackContext {
|
|
712
|
+
static stores = /* @__PURE__ */ new Set();
|
|
713
|
+
value;
|
|
714
|
+
generation = 0;
|
|
715
|
+
constructor() {
|
|
716
|
+
_StackContext.stores.add(new WeakRef(this));
|
|
717
|
+
}
|
|
718
|
+
getStore() {
|
|
719
|
+
return this.value;
|
|
720
|
+
}
|
|
721
|
+
run(value, callback) {
|
|
722
|
+
const previous = this.value;
|
|
723
|
+
const generation = this.generation;
|
|
724
|
+
this.value = value;
|
|
725
|
+
try {
|
|
726
|
+
return callback();
|
|
727
|
+
} finally {
|
|
728
|
+
if (this.generation === generation) this.value = previous;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
exit(callback) {
|
|
732
|
+
const previous = this.value;
|
|
733
|
+
const generation = this.generation;
|
|
734
|
+
this.value = void 0;
|
|
735
|
+
try {
|
|
736
|
+
return callback();
|
|
737
|
+
} finally {
|
|
738
|
+
if (this.generation === generation) this.value = previous;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
disable() {
|
|
742
|
+
this.value = void 0;
|
|
743
|
+
this.generation++;
|
|
744
|
+
}
|
|
745
|
+
static snapshot() {
|
|
746
|
+
const values = [];
|
|
747
|
+
for (const reference of _StackContext.stores) {
|
|
748
|
+
const store = reference.deref();
|
|
749
|
+
if (store === void 0) _StackContext.stores.delete(reference);
|
|
750
|
+
else values.push([store, store.value, store.generation]);
|
|
751
|
+
}
|
|
752
|
+
return (callback) => {
|
|
753
|
+
const restore = values.map(([store]) => store.value);
|
|
754
|
+
for (const [store, value, generation] of values) if (store.generation === generation) store.value = value;
|
|
755
|
+
try {
|
|
756
|
+
return callback();
|
|
757
|
+
} finally {
|
|
758
|
+
values.forEach(([store, , generation], index) => {
|
|
759
|
+
if (store.generation === generation) store.value = restore[index];
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
|
|
766
|
+
// packages/safe-js/src/platform/types.ts
|
|
767
|
+
function slot(method, value) {
|
|
768
|
+
if (typeof value !== "object" || value === null) return false;
|
|
769
|
+
try {
|
|
770
|
+
Reflect.apply(method, value, []);
|
|
771
|
+
return true;
|
|
772
|
+
} catch {
|
|
773
|
+
return false;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
var getter = (prototype, key) => Object.getOwnPropertyDescriptor(prototype, key).get;
|
|
777
|
+
var regexpPrototype = RegExp.prototype;
|
|
778
|
+
var regexpSource = getter(regexpPrototype, "source");
|
|
779
|
+
var booleanValue = Boolean.prototype.valueOf;
|
|
780
|
+
var numberValue = Number.prototype.valueOf;
|
|
781
|
+
var stringValue = String.prototype.valueOf;
|
|
782
|
+
var bigintValue = BigInt.prototype.valueOf;
|
|
783
|
+
var symbolValue = Symbol.prototype.valueOf;
|
|
784
|
+
var dateValue = Date.prototype.getTime;
|
|
785
|
+
var asyncFunctionPrototype = Object.getPrototypeOf(async function() {
|
|
786
|
+
});
|
|
787
|
+
var generatorFunctionPrototype = Object.getPrototypeOf(function* () {
|
|
788
|
+
});
|
|
789
|
+
var arrayBufferLength = getter(ArrayBuffer.prototype, "byteLength");
|
|
790
|
+
var dataViewBuffer = getter(DataView.prototype, "buffer");
|
|
791
|
+
var mapSize = getter(Map.prototype, "size");
|
|
792
|
+
var promiseThen = Promise.prototype.then;
|
|
793
|
+
var NativeError = Error;
|
|
794
|
+
var nativeIsError = Reflect.get(NativeError, "isError");
|
|
795
|
+
var proxies = /* @__PURE__ */ new WeakSet();
|
|
796
|
+
function createTrackedProxy(target, handler) {
|
|
797
|
+
const proxy = new Proxy(target, handler);
|
|
798
|
+
proxies.add(proxy);
|
|
799
|
+
return proxy;
|
|
800
|
+
}
|
|
801
|
+
var types = {
|
|
802
|
+
isProxy: (value) => (typeof value === "object" && value !== null || typeof value === "function") && proxies.has(value),
|
|
803
|
+
isBooleanObject: (value) => typeof value === "object" && value !== null && slot(booleanValue, value),
|
|
804
|
+
isNumberObject: (value) => typeof value === "object" && value !== null && slot(numberValue, value),
|
|
805
|
+
isStringObject: (value) => typeof value === "object" && slot(stringValue, value),
|
|
806
|
+
isBigIntObject: (value) => typeof value === "object" && value !== null && slot(bigintValue, value),
|
|
807
|
+
isSymbolObject: (value) => typeof value === "object" && value !== null && slot(symbolValue, value),
|
|
808
|
+
isDate: (value) => slot(dateValue, value),
|
|
809
|
+
isRegExp: (value) => value !== regexpPrototype && slot(regexpSource, value),
|
|
810
|
+
isArrayBuffer: (value) => slot(arrayBufferLength, value),
|
|
811
|
+
isDataView: (value) => slot(dataViewBuffer, value),
|
|
812
|
+
isMap: (value) => slot(mapSize, value),
|
|
813
|
+
isPromise(value) {
|
|
814
|
+
try {
|
|
815
|
+
Reflect.apply(promiseThen, value, [() => void 0, () => void 0]);
|
|
816
|
+
return true;
|
|
817
|
+
} catch {
|
|
818
|
+
return false;
|
|
819
|
+
}
|
|
820
|
+
},
|
|
821
|
+
isAsyncFunction: (value) => typeof value === "function" && Object.getPrototypeOf(value) === asyncFunctionPrototype,
|
|
822
|
+
isGeneratorFunction: (value) => typeof value === "function" && Object.getPrototypeOf(value) === generatorFunctionPrototype,
|
|
823
|
+
isNativeError: (value) => typeof value === "object" && value !== null && (typeof nativeIsError === "function" ? Reflect.apply(nativeIsError, NativeError, [value]) === true : value instanceof NativeError)
|
|
824
|
+
};
|
|
825
|
+
|
|
826
|
+
// packages/safe-js/src/platform/workerd.ts
|
|
827
|
+
var AsyncLocalStorage = Reflect.get(globalThis, "AsyncLocalStorage") ?? StackContext;
|
|
828
|
+
var cloneSharedBufferWrapper = structuredClone;
|
|
829
|
+
function createHostCallbackContext() {
|
|
830
|
+
let storage = new AsyncLocalStorage();
|
|
831
|
+
return {
|
|
832
|
+
run(value, callback) {
|
|
833
|
+
if (storage === void 0) throw new SandboxError("reentry");
|
|
834
|
+
return storage.run(value, callback);
|
|
835
|
+
},
|
|
836
|
+
getStore() {
|
|
837
|
+
return storage?.getStore();
|
|
838
|
+
},
|
|
839
|
+
disable() {
|
|
840
|
+
storage = void 0;
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
var accessDeniedSystemError = [-13, "permission denied"];
|
|
845
|
+
var yieldToHost = () => new Promise((resolve2) => setTimeout(resolve2, 0));
|
|
846
|
+
var hostCwd = () => "/";
|
|
847
|
+
var hostPlatform = "workerd";
|
|
848
|
+
var fsConstants = { F_OK: 0, R_OK: 4, W_OK: 2, X_OK: 1, COPYFILE_EXCL: 1 };
|
|
849
|
+
var hostFs = new Proxy({}, {
|
|
850
|
+
get() {
|
|
851
|
+
throw new TypeError("A filesystem adapter is required on this host.");
|
|
852
|
+
}
|
|
853
|
+
});
|
|
854
|
+
function inspect(value, _options) {
|
|
855
|
+
if (typeof value === "string") return "'" + value.replaceAll("'", "\\'") + "'";
|
|
856
|
+
if (typeof value === "object" && value !== null) return Array.isArray(value) ? "[Array]" : "[Object]";
|
|
857
|
+
return String(value);
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
// packages/safe-js/src/modules/paths.ts
|
|
861
|
+
import { basename, dirname, extname, isAbsolutePath, joinPath, relativePath, resolvePath } from "@poe-platform/safe-fs/core";
|
|
862
|
+
var sep = "/";
|
|
863
|
+
function resolve(...paths) {
|
|
864
|
+
return resolvePath(hostCwd(), ...paths);
|
|
865
|
+
}
|
|
866
|
+
function relative(from, to) {
|
|
867
|
+
return relativePath(resolve(from), resolve(to));
|
|
868
|
+
}
|
|
869
|
+
var paths_default = { basename, dirname, extname, isAbsolute: isAbsolutePath, join: joinPath, relative, resolve, sep };
|
|
870
|
+
|
|
871
|
+
export {
|
|
872
|
+
createTrackedProxy,
|
|
873
|
+
types,
|
|
874
|
+
internalSymbols,
|
|
875
|
+
sandboxErrorNames,
|
|
876
|
+
sandboxErrorTypes,
|
|
877
|
+
hostErrorData,
|
|
878
|
+
formatErrorStack,
|
|
879
|
+
replaceErrorStack,
|
|
880
|
+
attachErrorSpan,
|
|
881
|
+
attachWrappedErrorCause,
|
|
882
|
+
materializeWrappedErrorCause,
|
|
883
|
+
readErrorSpan,
|
|
884
|
+
readErrorCause,
|
|
885
|
+
createSourceSpan,
|
|
886
|
+
describeThrownValue,
|
|
887
|
+
templateRealms,
|
|
888
|
+
releaseTemplateObjects,
|
|
889
|
+
REGEX_COMPILE_LIMITS,
|
|
890
|
+
SandboxError,
|
|
891
|
+
isFatalSandboxError,
|
|
892
|
+
Budget,
|
|
893
|
+
AsyncLocalStorage,
|
|
894
|
+
cloneSharedBufferWrapper,
|
|
895
|
+
createHostCallbackContext,
|
|
896
|
+
accessDeniedSystemError,
|
|
897
|
+
yieldToHost,
|
|
898
|
+
hostPlatform,
|
|
899
|
+
fsConstants,
|
|
900
|
+
hostFs,
|
|
901
|
+
inspect,
|
|
902
|
+
basename,
|
|
903
|
+
dirname,
|
|
904
|
+
isAbsolutePath,
|
|
905
|
+
sep,
|
|
906
|
+
resolve,
|
|
907
|
+
relative,
|
|
908
|
+
paths_default
|
|
909
|
+
};
|