@poe-platform/safe-js 0.1.299 → 0.1.301

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.
@@ -7398,6 +7398,55 @@ function graphEntries(value) {
7398
7398
  return entries;
7399
7399
  }
7400
7400
 
7401
+ // packages/safe-js/src/interp/resources.ts
7402
+ import { AsyncLocalStorage } from "node:async_hooks";
7403
+ var runResources = new AsyncLocalStorage();
7404
+ function retainValues(budget, values) {
7405
+ const retained = {};
7406
+ budget.setRetainedValues(retained, values);
7407
+ const pending = runResources.getStore()?.referenceReleases;
7408
+ const release = () => {
7409
+ budget.setRetainedValues(retained, void 0);
7410
+ pending?.delete(release);
7411
+ };
7412
+ pending?.add(release);
7413
+ return release;
7414
+ }
7415
+ async function withRunResources(signal, execute) {
7416
+ const controller = new AbortController();
7417
+ const cancel = () => controller.abort(signal?.reason);
7418
+ const cleanups = /* @__PURE__ */ new Set();
7419
+ const resources = {
7420
+ signal: controller.signal,
7421
+ referenceReleases: /* @__PURE__ */ new Set(),
7422
+ add(close) {
7423
+ cleanups.add(close);
7424
+ }
7425
+ };
7426
+ signal?.addEventListener("abort", cancel, { once: true });
7427
+ if (signal?.aborted) cancel();
7428
+ let result;
7429
+ let failure;
7430
+ let errors = [];
7431
+ try {
7432
+ result = await runResources.run(resources, execute);
7433
+ } catch (error) {
7434
+ failure = { reason: error };
7435
+ } finally {
7436
+ signal?.removeEventListener("abort", cancel);
7437
+ controller.abort(new Error("SafeJS run finished."));
7438
+ for (const release of resources.referenceReleases) release();
7439
+ resources.referenceReleases.clear();
7440
+ const outcomes = await Promise.allSettled(
7441
+ [...cleanups].map((close) => Promise.resolve().then(close))
7442
+ );
7443
+ errors = outcomes.flatMap((outcome) => outcome.status === "rejected" ? [outcome.reason] : []);
7444
+ }
7445
+ if (failure !== void 0) throw failure.reason;
7446
+ if (errors.length > 0) throw new AggregateError(errors, "SafeJS resource cleanup failed.");
7447
+ return result;
7448
+ }
7449
+
7401
7450
  // packages/safe-js/src/interp/symbols.ts
7402
7451
  var wellKnownSymbols = Object.freeze({
7403
7452
  iterator: Symbol.iterator,
@@ -7813,6 +7862,7 @@ function namedKeys(state) {
7813
7862
  // packages/safe-js/src/interp/object-model.ts
7814
7863
  var guestClosures = /* @__PURE__ */ new WeakSet();
7815
7864
  var functionProperties = /* @__PURE__ */ new WeakMap();
7865
+ var functionPropertyRevisions = /* @__PURE__ */ new WeakMap();
7816
7866
  var prototypes = /* @__PURE__ */ new WeakMap();
7817
7867
  var intrinsicPrototypes = /* @__PURE__ */ new WeakMap();
7818
7868
  var boxedPrototypes = /* @__PURE__ */ new WeakMap();
@@ -7821,6 +7871,7 @@ var collectionPrototypes = /* @__PURE__ */ new WeakMap();
7821
7871
  var promisePrototypes = /* @__PURE__ */ new WeakMap();
7822
7872
  var datePrototypes = /* @__PURE__ */ new WeakMap();
7823
7873
  var arrayPrototypes = /* @__PURE__ */ new WeakMap();
7874
+ var functionPrototypes = /* @__PURE__ */ new WeakMap();
7824
7875
  var initialArrayMethods = /* @__PURE__ */ new WeakMap();
7825
7876
  var initialRegexDescriptors = /* @__PURE__ */ new WeakMap();
7826
7877
  var intrinsicPrototypeRoots = /* @__PURE__ */ new WeakMap();
@@ -7856,9 +7907,23 @@ function materializeFunctionProperties(closure) {
7856
7907
  descriptorObjects.add(prototype);
7857
7908
  Object.defineProperty(properties, "prototype", { value: prototype, writable: true });
7858
7909
  }
7859
- descriptorObjects.add(properties);
7860
- functionProperties.set(closure, properties);
7861
- return properties;
7910
+ const state = { revision: 0 };
7911
+ const tracked = new Proxy(properties, {
7912
+ defineProperty(target, key, descriptor) {
7913
+ const changed = Reflect.defineProperty(target, key, descriptor);
7914
+ if (changed) state.revision++;
7915
+ return changed;
7916
+ },
7917
+ deleteProperty(target, key) {
7918
+ const changed = Reflect.deleteProperty(target, key);
7919
+ if (changed) state.revision++;
7920
+ return changed;
7921
+ }
7922
+ });
7923
+ functionPropertyRevisions.set(tracked, state);
7924
+ descriptorObjects.add(tracked);
7925
+ functionProperties.set(closure, tracked);
7926
+ return tracked;
7862
7927
  }
7863
7928
  function getGuestFunctionProperty(closure, key) {
7864
7929
  let properties = functionProperties.get(closure);
@@ -7876,6 +7941,12 @@ function installObjectPrototype(budget, prototype, constructor) {
7876
7941
  intrinsicPrototypes.set(budget, prototype);
7877
7942
  registerIntrinsicPrototype(budget, prototype, constructor);
7878
7943
  }
7944
+ function installFunctionPrototype(budget, prototype) {
7945
+ functionPrototypes.set(budget, prototype);
7946
+ registerBuiltinIdentities(budget, { "%FunctionPrototype%": prototype });
7947
+ registerIntrinsicFunction(budget, prototype);
7948
+ registerIntrinsicObject(budget, materializeFunctionProperties(prototype));
7949
+ }
7879
7950
  function installBoxedPrototype(budget, prototype, constructor, kind = typeof boxedValue(prototype)) {
7880
7951
  let state = boxedPrototypes.get(budget);
7881
7952
  if (state === void 0) boxedPrototypes.set(budget, state = /* @__PURE__ */ new Map());
@@ -7962,6 +8033,9 @@ function trackIntrinsicState(budget, root, owner, targets) {
7962
8033
  explicit: hasExplicitSandboxPrototype(target)
7963
8034
  })).map((record4) => ({
7964
8035
  ...record4,
8036
+ revision: functionPropertyRevisions.get(record4.value),
8037
+ capturedRevision: -1,
8038
+ captured: [],
7965
8039
  extensible: Object.isExtensible(record4.value),
7966
8040
  descriptors: new Map(Reflect.ownKeys(record4.value).map((key) => [key, Object.getOwnPropertyDescriptor(record4.value, key)]))
7967
8041
  }));
@@ -7977,14 +8051,21 @@ function trackIntrinsicState(budget, root, owner, targets) {
7977
8051
  );
7978
8052
  budget.setRetainedValues(root, () => {
7979
8053
  const retained = [];
7980
- for (const { target, value, descriptors: descriptors2, prototype: parent } of records) {
8054
+ for (const record4 of records) {
8055
+ const { target, value, descriptors: descriptors2, prototype: parent, revision } = record4;
7981
8056
  const currentPrototype = getSandboxPrototype(target);
7982
8057
  if (currentPrototype !== parent) retained.push(currentPrototype);
7983
- for (const key of Reflect.ownKeys(value)) {
7984
- const descriptor = Object.getOwnPropertyDescriptor(value, key);
7985
- if (unchanged(descriptors2.get(key), descriptor)) continue;
7986
- retained.push(key, descriptor.value, ...retainedAccessorClosures(descriptor));
8058
+ if (revision === void 0 || revision.revision !== record4.capturedRevision) {
8059
+ const captured = [];
8060
+ for (const key of Reflect.ownKeys(value)) {
8061
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
8062
+ if (unchanged(descriptors2.get(key), descriptor)) continue;
8063
+ captured.push(key, descriptor.value, ...retainedAccessorClosures(descriptor));
8064
+ }
8065
+ record4.captured = captured;
8066
+ record4.capturedRevision = revision?.revision ?? -1;
7987
8067
  }
8068
+ for (const item of record4.captured) retained.push(item);
7988
8069
  }
7989
8070
  return retained;
7990
8071
  });
@@ -8000,11 +8081,14 @@ function releaseObjectPrototype(budget) {
8000
8081
  promisePrototypes.delete(budget);
8001
8082
  datePrototypes.delete(budget);
8002
8083
  arrayPrototypes.delete(budget);
8084
+ functionPrototypes.delete(budget);
8003
8085
  initialRegexDescriptors.delete(budget);
8004
8086
  intrinsicPrototypes.delete(budget);
8005
8087
  }
8006
8088
  function getSandboxPrototype(value, budget) {
8007
8089
  if (prototypes.has(value)) return prototypes.get(value) ?? null;
8090
+ if (budget !== void 0 && isGuestClosure(value) && runResources.getStore()?.functionSourceText !== false)
8091
+ return functionPrototypes.get(budget) ?? null;
8008
8092
  if (budget !== void 0 && Array.isArray(value)) return arrayPrototypes.get(budget) ?? null;
8009
8093
  if (budget !== void 0 && isSandboxDate(value)) return datePrototypes.get(budget) ?? null;
8010
8094
  if (budget !== void 0 && isSandboxPromise(value)) return promisePrototypes.get(budget) ?? null;
@@ -8030,7 +8114,14 @@ function getSandboxPropertyDescriptor(value, key, budget) {
8030
8114
  let current = value;
8031
8115
  let depth = 0;
8032
8116
  while (typeof current === "object" && current !== null && (Array.isArray(current) || isSandboxGenerator(current) || isSandboxDate(current) || isSandboxPromise(current) || isSandboxRegex(current) || isSandboxMap(current) || isSandboxSet(current) || isPrototypeRecord(current))) {
8033
- const properties = isSandboxGenerator(current) ? getGeneratorProperties(current) : isSandboxPromise(current) ? getPromiseProperties(current) : isGuestClosure(current) ? getGuestFunctionProperties(current) : isSandboxRegex(current) ? getRegexProperties(current) : isSandboxMap(current) || isSandboxSet(current) ? getCollectionProperties(current) : current;
8117
+ const properties = isSandboxGenerator(current) ? getGeneratorProperties(current) : isSandboxPromise(current) ? getPromiseProperties(current) : isGuestClosure(current) ? key === "prototype" && current.construct !== void 0 && current.boundTarget === void 0 ? materializeFunctionProperties(current) : getGuestFunctionProperties(current) : isSandboxRegex(current) ? getRegexProperties(current) : isSandboxMap(current) || isSandboxSet(current) ? getCollectionProperties(current) : current;
8118
+ if (properties === void 0 && isGuestClosure(current) && (key === "name" || key === "length"))
8119
+ return {
8120
+ value: key === "name" ? current.name ?? "" : current.length ?? 0,
8121
+ writable: false,
8122
+ enumerable: false,
8123
+ configurable: true
8124
+ };
8034
8125
  const descriptor = properties === void 0 ? void 0 : Object.getOwnPropertyDescriptor(properties, key);
8035
8126
  if (descriptor !== void 0) return descriptor;
8036
8127
  current = getSandboxPrototype(current, budget);
@@ -8141,7 +8232,7 @@ function isRawJson(value) {
8141
8232
  import { types as nodeTypes } from "node:util";
8142
8233
 
8143
8234
  // packages/safe-js/src/interp/promise-tracker.ts
8144
- import { AsyncLocalStorage } from "node:async_hooks";
8235
+ import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
8145
8236
  var SandboxPromiseRejectionTracker = class {
8146
8237
  records = /* @__PURE__ */ new Set();
8147
8238
  recordsByPromise = /* @__PURE__ */ new WeakMap();
@@ -8218,8 +8309,8 @@ var SandboxPromiseRejectionTracker = class {
8218
8309
  return void 0;
8219
8310
  }
8220
8311
  };
8221
- var activePromiseTracker = new AsyncLocalStorage();
8222
- var fatalRejectionCleanup = new AsyncLocalStorage();
8312
+ var activePromiseTracker = new AsyncLocalStorage2();
8313
+ var fatalRejectionCleanup = new AsyncLocalStorage2();
8223
8314
  function assertPromiseExecutionAllowed() {
8224
8315
  const failure = activePromiseTracker.getStore()?.failure;
8225
8316
  if (failure !== void 0 && fatalRejectionCleanup.getStore() !== true) throw failure;
@@ -8781,55 +8872,6 @@ function isLineTerminator(character) {
8781
8872
  return character === "\n" || character === "\r" || character === "\u2028" || character === "\u2029";
8782
8873
  }
8783
8874
 
8784
- // packages/safe-js/src/interp/resources.ts
8785
- import { AsyncLocalStorage as AsyncLocalStorage2 } from "node:async_hooks";
8786
- var runResources = new AsyncLocalStorage2();
8787
- function retainValues(budget, values) {
8788
- const retained = {};
8789
- budget.setRetainedValues(retained, values);
8790
- const pending = runResources.getStore()?.referenceReleases;
8791
- const release = () => {
8792
- budget.setRetainedValues(retained, void 0);
8793
- pending?.delete(release);
8794
- };
8795
- pending?.add(release);
8796
- return release;
8797
- }
8798
- async function withRunResources(signal, execute) {
8799
- const controller = new AbortController();
8800
- const cancel = () => controller.abort(signal?.reason);
8801
- const cleanups = /* @__PURE__ */ new Set();
8802
- const resources = {
8803
- signal: controller.signal,
8804
- referenceReleases: /* @__PURE__ */ new Set(),
8805
- add(close) {
8806
- cleanups.add(close);
8807
- }
8808
- };
8809
- signal?.addEventListener("abort", cancel, { once: true });
8810
- if (signal?.aborted) cancel();
8811
- let result;
8812
- let failure;
8813
- let errors = [];
8814
- try {
8815
- result = await runResources.run(resources, execute);
8816
- } catch (error) {
8817
- failure = { reason: error };
8818
- } finally {
8819
- signal?.removeEventListener("abort", cancel);
8820
- controller.abort(new Error("SafeJS run finished."));
8821
- for (const release of resources.referenceReleases) release();
8822
- resources.referenceReleases.clear();
8823
- const outcomes = await Promise.allSettled(
8824
- [...cleanups].map((close) => Promise.resolve().then(close))
8825
- );
8826
- errors = outcomes.flatMap((outcome) => outcome.status === "rejected" ? [outcome.reason] : []);
8827
- }
8828
- if (failure !== void 0) throw failure.reason;
8829
- if (errors.length > 0) throw new AggregateError(errors, "SafeJS resource cleanup failed.");
8830
- return result;
8831
- }
8832
-
8833
8875
  // packages/safe-js/src/interp/bigint-operators.ts
8834
8876
  function bigIntOperation(operator, left, right, budget) {
8835
8877
  if (operator === ">>>") throw new TypeError("BigInts have no unsigned right shift.");
@@ -13973,7 +14015,7 @@ function getFunctionMember(target, property, options) {
13973
14015
  } else if (Object.hasOwn(current, String(property))) {
13974
14016
  return current[String(property)];
13975
14017
  }
13976
- if (isSandboxClosure(current) && !hasExplicitSandboxPrototype(current)) break;
14018
+ if (isSandboxClosure(current) && !hasExplicitSandboxPrototype(current) && getSandboxPrototype(current, options.budget) === null) break;
13977
14019
  current = getSandboxPrototype(current, options.budget);
13978
14020
  if (current !== null) {
13979
14021
  options.budget?.visitNode();
@@ -18312,7 +18354,7 @@ async function objectToPrimitive(value, budget, context, joining, hint, ordinary
18312
18354
  }
18313
18355
  }
18314
18356
  function conversionHook(value, name, budget, context) {
18315
- const implicitBuiltin = !hasExplicitSandboxPrototype(value) && (Array.isArray(value) && getSandboxPrototype(value, budget) === null || isFloat32Array(value) || sandboxErrorTypes.has(value) || isSandboxClosure(value) || isSandboxMap(value) || isSandboxSet(value) || isSandboxCollectionIterator(value) || isSandboxRegExpIterator(value) || isSandboxPromise(value) || isSandboxRegex(value) && getSandboxPrototype(value, budget) === null || isSandboxGenerator(value) || isGuestHostObject(value));
18357
+ const implicitBuiltin = !hasExplicitSandboxPrototype(value) && (Array.isArray(value) && getSandboxPrototype(value, budget) === null || isFloat32Array(value) || sandboxErrorTypes.has(value) || isSandboxClosure(value) && getSandboxPrototype(value, budget) === null || isSandboxMap(value) || isSandboxSet(value) || isSandboxCollectionIterator(value) || isSandboxRegExpIterator(value) || isSandboxPromise(value) || isSandboxRegex(value) && getSandboxPrototype(value, budget) === null || isSandboxGenerator(value) || isGuestHostObject(value));
18316
18358
  let current = value;
18317
18359
  let depth = 0;
18318
18360
  while (current !== null) {
@@ -23901,6 +23943,31 @@ function createUriGlobals(budget) {
23901
23943
  }));
23902
23944
  }
23903
23945
 
23946
+ // packages/safe-js/src/interp/globals/function.ts
23947
+ function createFunctionPrototype(budget) {
23948
+ const prototype = createSandboxClosure({ guest: true, sandbox: true, name: "", length: 0, call: () => void 0 });
23949
+ const properties = materializeFunctionProperties(prototype);
23950
+ Object.defineProperty(properties, "constructor", { value: void 0, writable: true, configurable: true });
23951
+ for (const [name, length] of [["call", 1], ["apply", 2], ["bind", 1], ["toString", 0]]) {
23952
+ Object.defineProperty(properties, name, {
23953
+ writable: true,
23954
+ configurable: true,
23955
+ value: createSandboxClosure({
23956
+ guest: true,
23957
+ sandbox: true,
23958
+ name,
23959
+ length,
23960
+ call: (args, context) => callFunctionMethod(context?.thisValue, name, args, {
23961
+ budget,
23962
+ callClosure: (target, values, stack, thisValue, construct, newTarget) => context?.invokeClosure !== void 0 ? context.invokeClosure(target, values, thisValue, construct, newTarget) : invokeBuiltinClosure(target, values, budget, { ...context, stack, thisValue, newTarget }, thisValue, construct)
23963
+ }, context?.stack ?? [], context)
23964
+ })
23965
+ });
23966
+ }
23967
+ setSandboxPrototype(prototype, getSandboxPrototype(/* @__PURE__ */ Object.create(null), budget));
23968
+ installFunctionPrototype(budget, prototype);
23969
+ }
23970
+
23904
23971
  // packages/safe-js/src/interp/globals/symbol.ts
23905
23972
  var registries = /* @__PURE__ */ new WeakMap();
23906
23973
  function createSymbolGlobal(budget) {
@@ -24131,6 +24198,7 @@ function createBuiltinBindings(options) {
24131
24198
  ...createPromiseGlobals(options),
24132
24199
  ...createRegexGlobals(options)
24133
24200
  };
24201
+ createFunctionPrototype(options.budget);
24134
24202
  registerBuiltinIdentities(options.budget, bindings);
24135
24203
  return bindings;
24136
24204
  }
@@ -40948,12 +41016,12 @@ export {
40948
41016
  safeAddEvent,
40949
41017
  safeRecordException,
40950
41018
  safeEndSpan,
41019
+ runResources,
40951
41020
  defineExtension,
40952
41021
  encodeReplayData,
40953
41022
  HostOperationResumePolicyError,
40954
41023
  registerPendingHostCallPolicy,
40955
41024
  HostCallResumabilityError,
40956
- runResources,
40957
41025
  assertSnapshotInactive,
40958
41026
  isSandboxClosure,
40959
41027
  deepCopyToSandbox,
@@ -40981,4 +41049,4 @@ export {
40981
41049
  FileSnapshotBackend,
40982
41050
  run
40983
41051
  };
40984
- //# sourceMappingURL=chunk-3QW2X67U.js.map
41052
+ //# sourceMappingURL=chunk-M4L3EZHL.js.map