@poe-platform/safe-js 0.1.99 → 0.1.100

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.
@@ -8184,6 +8184,28 @@ function isCounter(value) {
8184
8184
  // packages/safe-js/src/interp/cancel.ts
8185
8185
  import { AsyncLocalStorage as AsyncLocalStorage4 } from "node:async_hooks";
8186
8186
 
8187
+ // packages/safe-js/src/interp/builtin-call.ts
8188
+ async function invokeBuiltinClosure(closure, args, budget, context, thisValue, construct = false) {
8189
+ if (context?.invokeClosure !== void 0) {
8190
+ return context.invokeClosure(closure, args, thisValue, construct);
8191
+ }
8192
+ const leaveCall = budget.enterCall();
8193
+ try {
8194
+ const invoke = construct ? closure.construct : closure.call;
8195
+ if (invoke === void 0) throw new TypeError("Value is not a constructor.");
8196
+ const result = await invoke(args, {
8197
+ ...context,
8198
+ stack: context?.stack ?? [],
8199
+ thisValue,
8200
+ invokeClosure: (target, values, receiver, asConstructor) => invokeBuiltinClosure(target, values, budget, context, receiver, asConstructor)
8201
+ });
8202
+ if (isSandboxPromise(result)) await result.synchronousPrefix;
8203
+ return result;
8204
+ } finally {
8205
+ leaveCall();
8206
+ }
8207
+ }
8208
+
8187
8209
  // packages/safe-js/src/interp/regex/engine.ts
8188
8210
  function matchRegex(pattern, input, lastIndex = 0) {
8189
8211
  const startIndex = pattern.flags.global ? normalizeLastIndex(lastIndex) : 0;
@@ -8485,49 +8507,39 @@ function toMatchArray(match, input) {
8485
8507
 
8486
8508
  // packages/safe-js/src/interp/string-coercion.ts
8487
8509
  var defaultStringHook = /* @__PURE__ */ Symbol("defaultStringHook");
8510
+ var defaultValueHook = /* @__PURE__ */ Symbol("defaultValueHook");
8511
+ function sandboxNumber(value, budget, context) {
8512
+ if (value === null || typeof value !== "object") {
8513
+ if (typeof value === "function") throw new TypeError("Expected a sandbox value.");
8514
+ return Number(value);
8515
+ }
8516
+ return objectToPrimitive(value, budget, context, /* @__PURE__ */ new Set(), "number").then(Number);
8517
+ }
8488
8518
  function sandboxString(value, budget, context, joining = /* @__PURE__ */ new Set()) {
8489
8519
  if (value === null || typeof value !== "object") {
8490
8520
  if (typeof value === "function") throw new TypeError("Expected a sandbox value.");
8491
8521
  return budget.allocateString(String(value));
8492
8522
  }
8493
- const invocation = context?.invokeClosure !== void 0 ? context : {
8494
- ...context,
8495
- stack: context?.stack ?? [],
8496
- thisValue: context?.thisValue,
8497
- invokeClosure: async (closure, args, thisValue) => {
8498
- const leaveCall = budget.enterCall();
8499
- try {
8500
- const result = closure.call(args, { ...invocation, thisValue });
8501
- if (isSandboxPromise(result)) {
8502
- await result.synchronousPrefix;
8503
- return result;
8504
- }
8505
- return await result;
8506
- } finally {
8507
- leaveCall();
8508
- }
8509
- }
8510
- };
8511
- return stringifyObject(value, budget, invocation, joining);
8523
+ return objectToPrimitive(value, budget, context, joining, "string").then((primitive) => budget.allocateString(String(primitive)));
8512
8524
  }
8513
- async function stringifyObject(value, budget, context, joining) {
8525
+ async function objectToPrimitive(value, budget, context, joining, hint) {
8514
8526
  const leaveCall = budget.enterCall();
8515
8527
  try {
8516
8528
  budget.visitNode();
8517
- for (const name of ["toString", "valueOf"]) {
8529
+ for (const name of hint === "string" ? ["toString", "valueOf"] : ["valueOf", "toString"]) {
8518
8530
  const hook = conversionHook(value, name, budget);
8519
8531
  let result;
8520
8532
  if (hook === defaultStringHook) {
8521
8533
  result = await defaultToString(value, budget, context, joining);
8534
+ } else if (hook === defaultValueHook) {
8535
+ result = isSandboxDate(value) ? dateTime(value) : value;
8522
8536
  } else {
8523
8537
  if (!isSandboxClosure(hook)) continue;
8524
- if (context?.invokeClosure === void 0) {
8525
- throw new TypeError("String hooks require a sandbox call context.");
8526
- }
8527
- result = await context.invokeClosure(hook, [], value);
8538
+ result = await invokeBuiltinClosure(hook, [], budget, context, value);
8528
8539
  }
8529
8540
  if (result === null || typeof result !== "object") {
8530
- return sandboxString(result, budget, context, joining);
8541
+ if (typeof result === "function") throw new TypeError("Expected a sandbox value.");
8542
+ return result;
8531
8543
  }
8532
8544
  }
8533
8545
  throw new TypeError("Cannot convert object to primitive value");
@@ -8549,7 +8561,7 @@ function conversionHook(value, name, budget) {
8549
8561
  }
8550
8562
  const parent = getSandboxPrototype(current, budget);
8551
8563
  if (current === value && (implicitBuiltin || parent === null && !hasExplicitSandboxPrototype(value))) {
8552
- return name === "toString" ? defaultStringHook : void 0;
8564
+ return name === "toString" ? defaultStringHook : defaultValueHook;
8553
8565
  }
8554
8566
  current = parent;
8555
8567
  if (current !== null) {
@@ -8574,10 +8586,7 @@ async function defaultToString(value, budget, context, joining) {
8574
8586
  const join = ownDataValue(value, "join");
8575
8587
  if (!isSandboxClosure(join))
8576
8588
  return isFloat32Array(value) ? "[object Float32Array]" : "[object Array]";
8577
- if (context?.invokeClosure === void 0) {
8578
- throw new TypeError("String hooks require a sandbox call context.");
8579
- }
8580
- return context.invokeClosure(join, [], value);
8589
+ return invokeBuiltinClosure(join, [], budget, context, value);
8581
8590
  }
8582
8591
  if (joining.has(value)) return "";
8583
8592
  joining.add(value);
@@ -28080,7 +28089,9 @@ function createCoercionContext(context) {
28080
28089
  stack: context.callStack,
28081
28090
  thisValue: void 0,
28082
28091
  compilation: context.compilation,
28083
- invokeClosure: (closure, args, thisValue) => invokeSandboxClosure(closure, args, context, context.callStack, void 0, thisValue)
28092
+ getProperty: (value, property) => getPropertyValue(value, property, context),
28093
+ reconcileData: (value) => reconcileDataBudget(context.budget, context.stats, context.scope, value, context.compilation, context.compilation?.parent),
28094
+ invokeClosure: (closure, args, thisValue, construct) => invokeSandboxClosure(closure, args, context, context.callStack, void 0, thisValue, construct)
28084
28095
  };
28085
28096
  }
28086
28097
  function hasSandboxProperty(value, key, context) {
@@ -28457,7 +28468,9 @@ async function invokeSandboxClosure(callee, args, context, stack, span, thisValu
28457
28468
  stack,
28458
28469
  thisValue,
28459
28470
  compilation: context.compilation,
28460
- invokeClosure: (closure, argumentsList, receiver) => invokeSandboxClosure(closure, argumentsList, context, stack, span, receiver),
28471
+ getProperty: (value, property) => getPropertyValue(value, property, context),
28472
+ reconcileData: (value) => reconcileDataBudget(context.budget, context.stats, context.scope, value, context.compilation, context.compilation?.parent),
28473
+ invokeClosure: (closure, argumentsList, receiver, asConstructor) => invokeSandboxClosure(closure, argumentsList, context, stack, span, receiver, asConstructor),
28461
28474
  ...span === void 0 ? {} : { span }
28462
28475
  }
28463
28476
  ]);
@@ -30153,7 +30166,7 @@ async function stringifyValue(value, state, indent) {
30153
30166
  return stringifyArray(value, state, indent);
30154
30167
  }
30155
30168
  if (isStringifyObject(value)) {
30156
- return stringifyObject2(value, state, indent);
30169
+ return stringifyObject(value, state, indent);
30157
30170
  }
30158
30171
  return void 0;
30159
30172
  }
@@ -30179,7 +30192,7 @@ ${indent}]`;
30179
30192
  leaveStringifyObject(value, state);
30180
30193
  }
30181
30194
  }
30182
- async function stringifyObject2(value, state, indent) {
30195
+ async function stringifyObject(value, state, indent) {
30183
30196
  enterStringifyObject(value, state);
30184
30197
  try {
30185
30198
  const nextIndent = indent + state.gap;
@@ -30550,7 +30563,7 @@ function createObjectArrayGlobals(options) {
30550
30563
  }),
30551
30564
  from: createSandboxClosure({
30552
30565
  sandbox: true,
30553
- call: (args) => arrayFromSandboxValues(args, options.budget),
30566
+ call: (args, context) => arrayFromSandboxValues(args, options.budget, context),
30554
30567
  name: "from"
30555
30568
  }),
30556
30569
  of: createSandboxClosure({
@@ -30730,40 +30743,86 @@ function defineDataProperty2(target, key, descriptor, budget) {
30730
30743
  function isAssignableSandboxTarget(value) {
30731
30744
  return typeof value === "object" && value !== null && !isSandboxClosure(value) && !isSandboxGenerator(value) && !isSandboxMap(value) && !isSandboxSet(value) && !isSandboxPromise(value) && !isSandboxRegex(value);
30732
30745
  }
30733
- async function arrayFromSandboxValues(args, budget) {
30746
+ async function arrayFromSandboxValues(args, budget, context) {
30734
30747
  const [items, mapFn, thisValue] = args;
30748
+ if (mapFn !== void 0 && !isSandboxClosure(mapFn)) {
30749
+ throw new TypeError("Array.from mapping callback must be a function.");
30750
+ }
30751
+ if (items === null || items === void 0) {
30752
+ throw new TypeError("Array.from requires a non-null input.");
30753
+ }
30754
+ const read = (property) => context?.getProperty !== void 0 ? context.getProperty(items, property) : getSandboxDataProperty(items, property, budget);
30735
30755
  const iterator = getSandboxIterator(items);
30736
- if (isGuestHostObject(items) && iterator !== void 0) {
30737
- if (mapFn !== void 0 && !isSandboxClosure(mapFn))
30738
- throw new TypeError("Array.from mapping callback must be a function.");
30739
- const values2 = [];
30740
- while (true) {
30741
- const next = await iterator.next();
30742
- if (next.done) break;
30743
- budget.allocateArrayLength(values2.length + 1);
30744
- const value = mapFn === void 0 ? next.value : await mapFn.call([next.value, values2.length], { stack: [], thisValue });
30745
- if (isSandboxPromise(value) && value.synchronousPrefix !== void 0)
30746
- await value.synchronousPrefix;
30747
- values2.push(value);
30756
+ const constructor = context?.thisValue;
30757
+ let result;
30758
+ let currentValue;
30759
+ let failure;
30760
+ let estimatedDataSize = 0;
30761
+ const retained = {};
30762
+ budget.setRetainedValues(retained, () => [items, mapFn, constructor, result, currentValue, failure]);
30763
+ const checkData = (growth = 0, force = false) => {
30764
+ const limit = budget.limits.dataSize;
30765
+ if (limit === void 0) return;
30766
+ estimatedDataSize = Math.max(estimatedDataSize, budget.currentDataSize) + growth;
30767
+ if (!force && estimatedDataSize <= limit) return;
30768
+ if (context?.reconcileData !== void 0) context.reconcileData(result);
30769
+ else reconcileCompiledValues(budget, [], context?.compilation);
30770
+ estimatedDataSize = budget.currentDataSize;
30771
+ };
30772
+ const closeOnThrow = async (error) => {
30773
+ failure = isCapturedException(error) ? error.reason : error;
30774
+ try {
30775
+ await iterator?.return?.();
30776
+ } catch (closeError) {
30777
+ if (!isFatalSandboxError(error) && isFatalSandboxError(closeError)) throw closeError;
30748
30778
  }
30749
- return allocateProducedSandboxValue(values2, budget);
30750
- }
30751
- const values = iterator === void 0 ? Reflect.apply(Array.from, Array, [items]) : await collectIteratorValues(iterator);
30752
- if (mapFn === void 0 || !isSandboxClosure(mapFn)) {
30753
- if (mapFn !== void 0) {
30754
- throw new TypeError("Array.from mapping callback must be a function.");
30779
+ throw error;
30780
+ };
30781
+ try {
30782
+ let length = 0;
30783
+ if (iterator === void 0) {
30784
+ const number = await sandboxNumber(read("length"), budget, context);
30785
+ length = Number.isNaN(number) || number <= 0 ? 0 : Math.min(Math.trunc(number), Number.MAX_SAFE_INTEGER);
30755
30786
  }
30756
- return budgetSandboxValue2(values, budget);
30757
- }
30758
- const mappedValues = [];
30759
- for (const [index, value] of values.entries()) {
30760
- const result = await mapFn.call([value, index], { stack: [], thisValue });
30761
- if (isSandboxPromise(result) && result.synchronousPrefix !== void 0) {
30762
- await result.synchronousPrefix;
30787
+ result = isSandboxClosure(constructor) && constructor.construct !== void 0 ? await invokeBuiltinClosure(constructor, iterator === void 0 ? [length] : [], budget, context, void 0, true) : createArrayFromConstructorArgs([length], budget);
30788
+ checkData(0, true);
30789
+ let index = 0;
30790
+ while (iterator !== void 0 || index < length) {
30791
+ try {
30792
+ budget.visitNode();
30793
+ if (iterator !== void 0 && index >= Number.MAX_SAFE_INTEGER) throw new TypeError("Array.from input is too long.");
30794
+ } catch (error) {
30795
+ await closeOnThrow(error);
30796
+ }
30797
+ if (iterator !== void 0) {
30798
+ const next = await iterator.next();
30799
+ if (typeof next !== "object" || next === null) throw new TypeError("Iterator result must be an object.");
30800
+ if (next.done) break;
30801
+ currentValue = next.value;
30802
+ } else {
30803
+ currentValue = read(index);
30804
+ }
30805
+ try {
30806
+ if (Array.isArray(result)) budget.allocateArrayLength(index + 1);
30807
+ if (mapFn !== void 0) currentValue = await invokeBuiltinClosure(mapFn, [currentValue, index], budget, context, thisValue);
30808
+ const key = String(index);
30809
+ const growth = key.length + 1 + (Array.isArray(result) ? Math.max(0, index + 1 - result.length) : 0) + (typeof currentValue === "string" ? currentValue.length : 0);
30810
+ const graphChanged = typeof currentValue === "object" && currentValue !== null || isSandboxClosure(result);
30811
+ budget.visitNode();
30812
+ defineOwnDataProperty(objectProperties(result, true), key, currentValue);
30813
+ currentValue = void 0;
30814
+ checkData(growth, graphChanged);
30815
+ } catch (error) {
30816
+ await closeOnThrow(error);
30817
+ }
30818
+ index += 1;
30763
30819
  }
30764
- mappedValues.push(result);
30820
+ setSandboxProperty(result, "length", index, budget);
30821
+ checkData(0, true);
30822
+ return allocateProducedSandboxValue(result, budget);
30823
+ } finally {
30824
+ budget.setRetainedValues(retained, void 0);
30765
30825
  }
30766
- return budgetSandboxValue2(mappedValues, budget);
30767
30826
  }
30768
30827
  function createArrayFromConstructorArgs(args, budget) {
30769
30828
  if (args.length !== 1) {
@@ -30777,14 +30836,11 @@ function createArrayFromConstructorArgs(args, budget) {
30777
30836
  throw new RangeError("Invalid array length.");
30778
30837
  }
30779
30838
  budget.allocateArrayLength(lengthOrValue);
30780
- return new Array(lengthOrValue);
30781
- }
30782
- async function collectIteratorValues(iterator) {
30783
- const values = [];
30784
- while (true) {
30785
- const result = await iterator.next();
30786
- if (result.done) return values;
30787
- values.push(result.value);
30839
+ const release = budget.provisionDataUsage(lengthOrValue + 1);
30840
+ try {
30841
+ return new Array(lengthOrValue);
30842
+ } finally {
30843
+ release();
30788
30844
  }
30789
30845
  }
30790
30846
  function getOwnEnumerableKeys(value) {
@@ -32944,4 +33000,4 @@ export {
32944
33000
  FileSnapshotBackend,
32945
33001
  run
32946
33002
  };
32947
- //# sourceMappingURL=chunk-ZFJQGJ3M.js.map
33003
+ //# sourceMappingURL=chunk-GUU3L2FD.js.map