@poe-platform/safe-js 0.1.401 → 0.1.402

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.
@@ -27,7 +27,7 @@ import {
27
27
  validateMigrationSemantics,
28
28
  validateSnapshotData,
29
29
  validateSnapshotMigration
30
- } from "./chunk-HDEGJ4AI.js";
30
+ } from "./chunk-5RNME7ZM.js";
31
31
 
32
32
  // packages/safe-js/src/migrate.ts
33
33
  import { createHash } from "node:crypto";
@@ -8249,4 +8249,4 @@ export {
8249
8249
  parseMcpConfig,
8250
8250
  makeMcpModule
8251
8251
  };
8252
- //# sourceMappingURL=chunk-YFCLIG72.js.map
8252
+ //# sourceMappingURL=chunk-3DXEWN6Z.js.map
@@ -8812,12 +8812,23 @@ async function flushPromiseJobs() {
8812
8812
  var promiseStates = /* @__PURE__ */ new WeakMap();
8813
8813
 
8814
8814
  // packages/safe-js/src/interp/promise-continuations.ts
8815
+ var promiseAggregateHandlers = /* @__PURE__ */ new WeakMap();
8816
+ var promiseAggregateStates = /* @__PURE__ */ new WeakMap();
8817
+ var promiseAggregateEntries = /* @__PURE__ */ new WeakMap();
8815
8818
  var promiseAdoptions = /* @__PURE__ */ new WeakMap();
8816
8819
  var promiseAdoptionBridges = /* @__PURE__ */ new WeakMap();
8817
8820
  var promiseAdoptionResolvers = /* @__PURE__ */ new WeakMap();
8818
8821
  var promiseContinuations = /* @__PURE__ */ new WeakMap();
8819
8822
  var promiseReactionResults = /* @__PURE__ */ new WeakMap();
8820
8823
  var promiseProducers = /* @__PURE__ */ new WeakMap();
8824
+ function linkPromiseAggregateProducer(producer, aggregate) {
8825
+ const continuation = promiseContinuations.get(producer);
8826
+ if (continuation?.kind !== "reaction") return;
8827
+ continuation.aggregate = aggregate;
8828
+ let producers = promiseProducers.get(aggregate);
8829
+ if (producers === void 0) promiseProducers.set(aggregate, producers = /* @__PURE__ */ new Set());
8830
+ producers.add(producer);
8831
+ }
8821
8832
  function trackPromiseContinuation(promise, continuation) {
8822
8833
  promiseContinuations.set(promise, continuation);
8823
8834
  if (continuation.kind === "reaction") {
@@ -8842,6 +8853,11 @@ function trackPromiseContinuation(promise, continuation) {
8842
8853
  producers?.delete(promise);
8843
8854
  if (producers?.size === 0) promiseProducers.delete(continuation.capability.promise);
8844
8855
  }
8856
+ if (continuation.aggregate !== void 0) {
8857
+ const producers = promiseProducers.get(continuation.aggregate);
8858
+ producers?.delete(promise);
8859
+ if (producers?.size === 0) promiseProducers.delete(continuation.aggregate);
8860
+ }
8845
8861
  }
8846
8862
  };
8847
8863
  promise.promise.then(release, release);
@@ -21757,34 +21773,9 @@ async function settleIterable(iterable, method, budget, constructor, context) {
21757
21773
  const prototype = getPromisePrototype(budget);
21758
21774
  if (isSandboxPromise(capability.promise)) unrepresentedPromiseContinuations.add(capability.promise);
21759
21775
  const values = [];
21760
- let remaining = 1;
21761
- const complete2 = async () => {
21762
- if (remaining !== 0 || method === "race") return;
21763
- if (method === "any") {
21764
- const error = createSubsetErrorValue(
21765
- "AggregateError",
21766
- "All promises were rejected",
21767
- [],
21768
- budget
21769
- );
21770
- error.errors = values;
21771
- await callPromiseClosure(
21772
- capability.reject,
21773
- [budgetSandboxValue2(error, budget)],
21774
- void 0,
21775
- budget,
21776
- context
21777
- );
21778
- } else {
21779
- await callPromiseClosure(
21780
- capability.resolve,
21781
- [budgetSandboxValue2(values, budget)],
21782
- void 0,
21783
- budget,
21784
- context
21785
- );
21786
- }
21787
- };
21776
+ const aggregate = { method, capability, values, remaining: 1, size: 0, iteration: "active" };
21777
+ promiseAggregateStates.set(aggregate, aggregate);
21778
+ let represented = true;
21788
21779
  try {
21789
21780
  const promiseResolve = await readPromiseReceiverProperty(constructor, "resolve", prototype, context);
21790
21781
  if (!isSandboxClosure(promiseResolve))
@@ -21793,7 +21784,6 @@ async function settleIterable(iterable, method, budget, constructor, context) {
21793
21784
  if (iterator === void 0) throw new TypeError("Promise helpers require an iterable.");
21794
21785
  const releaseIterator = retainValues(budget, () => [iterator.retainedValue]);
21795
21786
  try {
21796
- let index = 0;
21797
21787
  while (true) {
21798
21788
  budget.visitNode();
21799
21789
  const next = iterator.generator || iterator.asynchronous ? await iterator.next() : iterator.next();
@@ -21802,8 +21792,8 @@ async function settleIterable(iterable, method, budget, constructor, context) {
21802
21792
  if ((await readIteratorResult(iterator, next, "done")).value) break;
21803
21793
  const value = (await readIteratorResult(iterator, next, "value")).value;
21804
21794
  try {
21805
- budget.allocateArrayLength(index + 1);
21806
- const entryIndex = index++;
21795
+ budget.allocateArrayLength(aggregate.size + 1);
21796
+ const entryIndex = aggregate.size++;
21807
21797
  if (method !== "race") values.push(void 0);
21808
21798
  const entry = await callPromiseClosure(
21809
21799
  promiseResolve,
@@ -21812,45 +21802,41 @@ async function settleIterable(iterable, method, budget, constructor, context) {
21812
21802
  budget,
21813
21803
  context
21814
21804
  );
21815
- let called = false;
21805
+ const aggregateEntry = { aggregate, index: entryIndex, called: false };
21816
21806
  const handlers = ["fulfilled", "rejected"].map((state) => {
21817
21807
  if (method === "race" || method === "any" && state === "fulfilled")
21818
21808
  return state === "fulfilled" ? capability.resolve : capability.reject;
21819
21809
  if (method === "all" && state === "rejected") return capability.reject;
21820
- return createSandboxClosure({
21821
- sandbox: true,
21822
- retainedValues: () => [
21823
- capability.promise,
21824
- capability.resolve,
21825
- capability.reject,
21826
- values
21827
- ],
21828
- call: async ([settlement]) => {
21829
- if (called) return void 0;
21830
- called = true;
21831
- values[entryIndex] = method === "allSettled" ? state === "fulfilled" ? { status: state, value: settlement } : { status: state, reason: settlement } : settlement;
21832
- remaining--;
21833
- await complete2();
21834
- return void 0;
21835
- }
21836
- });
21810
+ return createPromiseAggregateHandler(aggregateEntry, state, budget, context);
21837
21811
  });
21838
- remaining++;
21812
+ aggregate.remaining++;
21839
21813
  const then = await readPromiseProperty(entry, "then", prototype, budget, context);
21840
21814
  if (!isSandboxClosure(then))
21841
21815
  throw new TypeError("Promise resolver result requires a callable then.");
21842
- await callPromiseClosure(then, handlers, entry, budget, context);
21816
+ const previousReactions = isSandboxPromise(entry) ? new Set(promiseReactionResults.get(entry)) : void 0;
21817
+ const completion = await callPromiseClosure(then, handlers, entry, budget, context);
21818
+ if (!intrinsicPromiseThenMethods.has(then) || !isSandboxPromise(entry) || !isSandboxPromise(completion)) represented = false;
21819
+ else if (isSandboxPromise(capability.promise)) {
21820
+ for (const producer of promiseReactionResults.get(entry) ?? []) {
21821
+ const reaction = promiseContinuations.get(producer);
21822
+ if (!previousReactions?.has(producer) && reaction?.kind === "reaction" && reaction.onFulfilled === handlers[0] && reaction.onRejected === handlers[1])
21823
+ linkPromiseAggregateProducer(producer, capability.promise);
21824
+ }
21825
+ }
21843
21826
  } catch (error) {
21844
21827
  await closeIterator(iterator, true);
21845
21828
  throw error;
21846
21829
  }
21847
21830
  }
21848
- remaining--;
21849
- await complete2();
21831
+ aggregate.remaining--;
21832
+ aggregate.iteration = "complete";
21833
+ await completePromiseAggregate(aggregate, budget, context);
21834
+ if (represented && isSandboxPromise(capability.promise)) unrepresentedPromiseContinuations.delete(capability.promise);
21850
21835
  } finally {
21851
21836
  releaseIterator();
21852
21837
  }
21853
21838
  } catch (error) {
21839
+ aggregate.iteration = "abrupt";
21854
21840
  if (error instanceof SandboxError && (error.code === "budgetExceeded" || error.code === "reentry")) {
21855
21841
  if (isSandboxPromise(capability.promise)) observeSandboxPromise(capability.promise);
21856
21842
  throw error;
@@ -21865,6 +21851,39 @@ async function settleIterable(iterable, method, budget, constructor, context) {
21865
21851
  }
21866
21852
  return capability.promise;
21867
21853
  }
21854
+ async function completePromiseAggregate(aggregate, budget, context) {
21855
+ const { method, capability, values, remaining } = aggregate;
21856
+ if (remaining !== 0 || method === "race") return;
21857
+ if (method === "any") {
21858
+ const error = createSubsetErrorValue("AggregateError", "All promises were rejected", [], budget);
21859
+ error.errors = values;
21860
+ await callPromiseClosure(capability.reject, [budgetSandboxValue2(error, budget)], void 0, budget, context);
21861
+ } else {
21862
+ await callPromiseClosure(capability.resolve, [budgetSandboxValue2(values, budget)], void 0, budget, context);
21863
+ }
21864
+ }
21865
+ function createPromiseAggregateHandler(entry, action, budget, context) {
21866
+ const { aggregate } = entry;
21867
+ promiseAggregateEntries.set(entry, entry);
21868
+ const handler = createSandboxClosure({
21869
+ sandbox: true,
21870
+ guest: true,
21871
+ name: "",
21872
+ length: 1,
21873
+ retainedValues: () => [aggregate.capability.promise, aggregate.capability.resolve, aggregate.capability.reject, aggregate.values],
21874
+ call: async ([settlement]) => {
21875
+ if (entry.called) return void 0;
21876
+ entry.called = true;
21877
+ const { method, values } = aggregate;
21878
+ values[entry.index] = method === "allSettled" ? action === "fulfilled" ? { status: action, value: settlement } : { status: action, reason: settlement } : settlement;
21879
+ aggregate.remaining--;
21880
+ await completePromiseAggregate(aggregate, budget, context);
21881
+ return void 0;
21882
+ }
21883
+ });
21884
+ promiseAggregateHandlers.set(handler, { entry, action });
21885
+ return handler;
21886
+ }
21868
21887
  function schedulePromise(promise, budget) {
21869
21888
  return Promise.resolve().then(
21870
21889
  () => promise.then(
@@ -22340,6 +22359,29 @@ function serializeCollectionProperties(value, encode, dataOnly = false) {
22340
22359
 
22341
22360
  // packages/safe-js/src/snapshot/guest-heap.ts
22342
22361
  function captureGuestHeapNode(value, encode) {
22362
+ const aggregate = promiseAggregateStates.get(value);
22363
+ if (aggregate !== void 0) {
22364
+ if (aggregate.iteration === "active") throw new TypeError("Cannot snapshot an active promise aggregate iterator.");
22365
+ return {
22366
+ kind: "promise-aggregate",
22367
+ method: aggregate.method,
22368
+ remaining: aggregate.remaining,
22369
+ size: aggregate.size,
22370
+ iteration: aggregate.iteration,
22371
+ values: encode(aggregate.values),
22372
+ capability: {
22373
+ promise: encode(aggregate.capability.promise),
22374
+ resolve: encode(aggregate.capability.resolve),
22375
+ reject: encode(aggregate.capability.reject)
22376
+ }
22377
+ };
22378
+ }
22379
+ const aggregateEntry = promiseAggregateEntries.get(value);
22380
+ if (aggregateEntry !== void 0)
22381
+ return { kind: "aggregate-entry", aggregate: encode(aggregateEntry.aggregate), index: aggregateEntry.index, called: aggregateEntry.called };
22382
+ const aggregateHandler = isSandboxClosure(value) ? promiseAggregateHandlers.get(value) : void 0;
22383
+ if (aggregateHandler !== void 0)
22384
+ return { kind: "aggregate-handler", entry: encode(aggregateHandler.entry), action: aggregateHandler.action, state: captureObjectState(value, encode) };
22343
22385
  const bridge = promiseAdoptionBridges.get(value);
22344
22386
  if (bridge !== void 0) {
22345
22387
  if (bridge.settled || bridge.owner === void 0) return void 0;
@@ -22391,6 +22433,7 @@ function captureGuestHeapNode(value, encode) {
22391
22433
  kind: "promise-reaction",
22392
22434
  source: encode(continuation.source),
22393
22435
  ...producerState,
22436
+ ...continuation.aggregate === void 0 ? {} : { aggregate: encode(continuation.aggregate) },
22394
22437
  ...continuation.capability === void 0 ? {} : { capability: {
22395
22438
  promise: encode(continuation.capability.promise),
22396
22439
  resolve: encode(continuation.capability.resolve),
@@ -27858,7 +27901,7 @@ function validateGuestHeapNode(raw, heap, maxArrayLength = 4294967295) {
27858
27901
  createRawJson(node.text);
27859
27902
  return true;
27860
27903
  }
27861
- if (!["intrinsic", "bound-function", "promise-resolver", "pending-promise", "promise-reaction", "promise-adoption", "adoption-resolver", "guest-function", "guest-class", "guest-generator", "scope-frame", "guest-object", "guest-array", "guest-boxed", "guest-date", "guest-regex", "guest-promise", "array-iterator", "string-iterator", "iterator-wrapper", "iterator-helper", "guest-collection-iterator", "guest-regexp-iterator", "map", "set"].includes(String(node.kind))) return false;
27904
+ if (!["promise-aggregate", "aggregate-entry", "aggregate-handler", "intrinsic", "bound-function", "promise-resolver", "pending-promise", "promise-reaction", "promise-adoption", "adoption-resolver", "guest-function", "guest-class", "guest-generator", "scope-frame", "guest-object", "guest-array", "guest-boxed", "guest-date", "guest-regex", "guest-promise", "array-iterator", "string-iterator", "iterator-wrapper", "iterator-helper", "guest-collection-iterator", "guest-regexp-iterator", "map", "set"].includes(String(node.kind))) return false;
27862
27905
  const reference = (value, kinds) => {
27863
27906
  const ref = record3(value);
27864
27907
  fields(ref, ["kind", "id"]);
@@ -27934,7 +27977,9 @@ function validateGuestHeapNode(raw, heap, maxArrayLength = 4294967295) {
27934
27977
  const producers = /* @__PURE__ */ new Set();
27935
27978
  for (const entry of array(node.producers)) {
27936
27979
  const producer = reference(entry, ["promise-reaction"]);
27937
- if (producers.has(producer) || reference(record3(producer.capability).promise) !== node)
27980
+ const capabilityOwner = producer.capability === void 0 ? void 0 : reference(record3(producer.capability).promise);
27981
+ const aggregateOwner = producer.aggregate === void 0 ? void 0 : reference(producer.aggregate);
27982
+ if (producers.has(producer) || capabilityOwner !== node && aggregateOwner !== node)
27938
27983
  throw new TypeError("Invalid promise producer ownership.");
27939
27984
  producers.add(producer);
27940
27985
  }
@@ -27948,7 +27993,30 @@ function validateGuestHeapNode(raw, heap, maxArrayLength = 4294967295) {
27948
27993
  });
27949
27994
  return false;
27950
27995
  }
27951
- if (node.kind === "promise-adoption") {
27996
+ if (node.kind === "promise-aggregate") {
27997
+ fields(node, ["kind", "method", "capability", "values", "remaining", "size", "iteration"]);
27998
+ if (!["all", "allSettled", "race", "any"].includes(String(node.method))) throw new TypeError("Invalid promise aggregate method.");
27999
+ const size = integer(node.size);
28000
+ if (size > maxArrayLength || node.iteration !== "complete" && node.iteration !== "abrupt") throw new TypeError("Invalid promise aggregate iteration.");
28001
+ if (integer(node.remaining) > size + (node.iteration === "abrupt" ? 1 : 0)) throw new TypeError("Invalid promise aggregate remaining count.");
28002
+ reference(node.values, ["array", "guest-array"]);
28003
+ const capability = record3(node.capability);
28004
+ fields(capability, ["promise", "resolve", "reject"]);
28005
+ if (absent(capability.resolve) || absent(capability.reject)) throw new TypeError("Missing promise aggregate resolver.");
28006
+ callable(capability.resolve);
28007
+ callable(capability.reject);
28008
+ } else if (node.kind === "aggregate-entry") {
28009
+ fields(node, ["kind", "aggregate", "index", "called"]);
28010
+ const aggregate = reference(node.aggregate, ["promise-aggregate"]);
28011
+ if (integer(node.index) >= integer(aggregate.size) || typeof node.called !== "boolean") throw new TypeError("Invalid promise aggregate entry.");
28012
+ } else if (node.kind === "aggregate-handler") {
28013
+ fields(node, ["kind", "entry", "action", "state"]);
28014
+ state(node.state);
28015
+ const entry = reference(node.entry, ["aggregate-entry"]);
28016
+ const aggregate = reference(entry.aggregate, ["promise-aggregate"]);
28017
+ if (node.action !== "fulfilled" && node.action !== "rejected" || aggregate.method === "all" && node.action !== "fulfilled" || aggregate.method === "any" && node.action !== "rejected" || aggregate.method === "race")
28018
+ throw new TypeError("Invalid promise aggregate handler action.");
28019
+ } else if (node.kind === "promise-adoption") {
27952
28020
  fields(node, ["kind", "owner", "source"]);
27953
28021
  const owner = reference(node.owner, ["pending-promise"]);
27954
28022
  if (reference(owner.adoption, ["promise-adoption"]) !== node) throw new TypeError("Invalid promise adoption owner.");
@@ -27980,12 +28048,29 @@ function validateGuestHeapNode(raw, heap, maxArrayLength = 4294967295) {
27980
28048
  throw new TypeError("Invalid promise resolver action.");
27981
28049
  state(node.state);
27982
28050
  } else if (node.kind === "pending-promise" || node.kind === "promise-reaction") {
27983
- fields(node, node.kind === "pending-promise" ? ["kind", "reactions", "state"] : ["kind", "source", "onFulfilled", "onRejected", "reactions", "state"], node.kind === "pending-promise" ? ["adoption", "producers"] : ["capability", "producers"]);
28051
+ fields(node, node.kind === "pending-promise" ? ["kind", "reactions", "state"] : ["kind", "source", "onFulfilled", "onRejected", "reactions", "state"], node.kind === "pending-promise" ? ["adoption", "producers"] : ["capability", "aggregate", "producers"]);
27984
28052
  if (node.kind === "pending-promise" && Object.hasOwn(node, "adoption")) {
27985
28053
  const bridge = reference(node.adoption, ["promise-adoption"]);
27986
28054
  if (reference(bridge.owner, ["pending-promise"]) !== node) throw new TypeError("Invalid promise adoption owner.");
27987
28055
  }
27988
28056
  if (node.kind === "promise-reaction") {
28057
+ if (Object.hasOwn(node, "aggregate")) {
28058
+ const aggregate = reference(node.aggregate, ["pending-promise", "guest-promise", "promise-reaction"]);
28059
+ if (aggregate === node || !Array.isArray(aggregate.producers) || !aggregate.producers.some((entry) => reference(entry, ["promise-reaction"]) === node))
28060
+ throw new TypeError("Invalid promise aggregate producer ownership.");
28061
+ for (const value of [node.onFulfilled, node.onRejected]) {
28062
+ if (absent(value)) continue;
28063
+ const handler = reference(value);
28064
+ let owner;
28065
+ if (handler.kind === "promise-resolver") owner = reference(handler.promise);
28066
+ else if (handler.kind === "aggregate-handler") {
28067
+ const entry = reference(handler.entry, ["aggregate-entry"]);
28068
+ const state2 = reference(entry.aggregate, ["promise-aggregate"]);
28069
+ owner = reference(record3(state2.capability).promise);
28070
+ }
28071
+ if (owner !== void 0 && owner !== aggregate) throw new TypeError("Invalid promise aggregate handler ownership.");
28072
+ }
28073
+ }
27989
28074
  if (Object.hasOwn(node, "capability")) {
27990
28075
  const capability = record3(node.capability);
27991
28076
  fields(capability, ["promise", "resolve", "reject"]);
@@ -30255,6 +30340,7 @@ function measureSandboxData(values, options = {}) {
30255
30340
  visit(continuation.capability.resolve, depth + 1);
30256
30341
  visit(continuation.capability.reject, depth + 1);
30257
30342
  }
30343
+ if (continuation.aggregate !== void 0) visit(continuation.aggregate, depth + 1);
30258
30344
  }
30259
30345
  for (const reaction of promiseReactionResults.get(value) ?? []) visit(reaction, depth + 1);
30260
30346
  for (const producer of promiseProducers.get(value) ?? []) visit(producer, depth + 1);
@@ -45273,4 +45359,4 @@ export {
45273
45359
  FileSnapshotBackend,
45274
45360
  run
45275
45361
  };
45276
- //# sourceMappingURL=chunk-HDEGJ4AI.js.map
45362
+ //# sourceMappingURL=chunk-5RNME7ZM.js.map