@poe-platform/safe-js 0.1.162 → 0.1.163

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.
@@ -9070,221 +9070,6 @@ async function withRunResources(signal, execute) {
9070
9070
  return result;
9071
9071
  }
9072
9072
 
9073
- // packages/safe-js/src/interp/patterns.ts
9074
- async function bindPattern(pattern, value, target, scope, context) {
9075
- switch (pattern.type) {
9076
- case "Identifier":
9077
- bindIdentifier(pattern, value, target, scope);
9078
- return { ok: true };
9079
- case "MemberExpression":
9080
- if ("kind" in target) {
9081
- throw new TypeError("Destructuring declarations cannot bind to member expressions.");
9082
- }
9083
- return bindMemberExpression(pattern, value, scope, context);
9084
- case "AssignmentPattern":
9085
- return bindAssignmentPattern(pattern, value, target, scope, context);
9086
- case "ArrayPattern":
9087
- return bindArrayPattern(pattern, value, target, scope, context);
9088
- case "ObjectPattern":
9089
- return bindObjectPattern(pattern, value, target, scope, context);
9090
- case "RestElement":
9091
- return bindPattern(pattern.argument, value, target, scope, context);
9092
- }
9093
- }
9094
- function bindIdentifier(pattern, value, target, scope) {
9095
- if ("assign" in target || target.kind === "var" && target.initialize !== true) {
9096
- if ("assign" in target) {
9097
- const binding = scope.lookup(pattern.name);
9098
- if (!binding.found) {
9099
- throw new ReferenceError(`Cannot assign to undeclared binding '${pattern.name}'.`);
9100
- }
9101
- if (binding.kind === "const") {
9102
- throw new TypeError(`Cannot assign to const '${pattern.name}'`);
9103
- }
9104
- }
9105
- scope.assign(pattern.name, value);
9106
- return;
9107
- }
9108
- scope.declare(pattern.name, target.kind, value);
9109
- }
9110
- async function bindAssignmentPattern(pattern, value, target, scope, context) {
9111
- if (value !== void 0) {
9112
- return bindPattern(pattern.left, value, target, scope, context);
9113
- }
9114
- const defaultValue = await context.evaluate(
9115
- pattern.right,
9116
- pattern.left.type === "Identifier" ? pattern.left.name : void 0
9117
- );
9118
- if (defaultValue.kind !== "normal") {
9119
- return { ok: false, result: defaultValue };
9120
- }
9121
- return bindPattern(pattern.left, defaultValue.value, target, scope, context);
9122
- }
9123
- async function bindArrayPattern(pattern, value, target, scope, context) {
9124
- const iterator = isSandboxCollectionIterator(value) ? value : void 0;
9125
- const values = iterator === void 0 ? getArrayPatternValues(value) : void 0;
9126
- let cursor = 0;
9127
- let done = false;
9128
- const next = async () => {
9129
- if (iterator !== void 0) return nextCollectionIterator(iterator, context.budget);
9130
- if (done || cursor >= values.length) {
9131
- done = true;
9132
- return { done: true, value: void 0 };
9133
- }
9134
- return { done: false, value: await context.getProperty(values, cursor++) };
9135
- };
9136
- for (let index = 0; index < pattern.elements.length; index += 1) {
9137
- const element = pattern.elements[index];
9138
- if (element === null) {
9139
- await next();
9140
- continue;
9141
- }
9142
- let elementValue;
9143
- if (element.type === "RestElement") {
9144
- const rest = [];
9145
- for (let entry = await next(); !entry.done; entry = await next()) {
9146
- context.budget?.allocateArrayLength(rest.length + 1);
9147
- rest.push(entry.value);
9148
- }
9149
- elementValue = rest;
9150
- } else {
9151
- elementValue = (await next()).value;
9152
- }
9153
- const binding = await bindPattern(element, elementValue, target, scope, context);
9154
- if (!binding.ok) {
9155
- return binding;
9156
- }
9157
- }
9158
- return { ok: true };
9159
- }
9160
- async function bindObjectPattern(pattern, value, target, scope, context) {
9161
- if (typeof value !== "object" || value === null) {
9162
- throw new TypeError("Object destructuring declarations require a non-null object value.");
9163
- }
9164
- const excludedKeys = /* @__PURE__ */ new Set();
9165
- for (const property of pattern.properties) {
9166
- if (property.type === "RestElement") {
9167
- const binding2 = await bindPattern(
9168
- property,
9169
- await copyObjectRestValue(value, excludedKeys, context),
9170
- target,
9171
- scope,
9172
- context
9173
- );
9174
- if (!binding2.ok) {
9175
- return binding2;
9176
- }
9177
- continue;
9178
- }
9179
- const key = await evaluatePatternKey(property, context);
9180
- if (!key.ok) {
9181
- return key;
9182
- }
9183
- excludedKeys.add(typeof key.value === "symbol" ? key.value : String(key.value));
9184
- const binding = await bindPattern(
9185
- property.value,
9186
- await context.getProperty(value, key.value),
9187
- target,
9188
- scope,
9189
- context
9190
- );
9191
- if (!binding.ok) {
9192
- return binding;
9193
- }
9194
- }
9195
- return { ok: true };
9196
- }
9197
- async function bindMemberExpression(pattern, value, scope, context) {
9198
- const object = await context.evaluate(pattern.object);
9199
- if (object.kind !== "normal") {
9200
- return { ok: false, result: object };
9201
- }
9202
- const property = pattern.computed ? await context.evaluate(pattern.property) : { kind: "normal", value: getStaticPropertyName(pattern.property) };
9203
- if (property.kind !== "normal") {
9204
- return { ok: false, result: property };
9205
- }
9206
- if (object.value === null || object.value === void 0) {
9207
- throw new TypeError("Cannot assign properties of null or undefined.");
9208
- }
9209
- if (!isIndexableValue(object.value)) {
9210
- throw new TypeError("Assignment expressions require a sandbox object property.");
9211
- }
9212
- await context.setProperty(object.value, await context.toPropertyKey(property.value), value);
9213
- return { ok: true };
9214
- }
9215
- async function evaluatePatternKey(property, context) {
9216
- return property.computed ? evaluateProperty(property.key, context) : { ok: true, value: getStaticPropertyName(property.key) };
9217
- }
9218
- async function evaluateProperty(property, context) {
9219
- const result = await context.evaluate(property);
9220
- if (result.kind !== "normal") {
9221
- return { ok: false, result };
9222
- }
9223
- return { ok: true, value: await context.toPropertyKey(result.value) };
9224
- }
9225
- function getStaticPropertyName(property) {
9226
- if (property.type === "Identifier") {
9227
- return property.name;
9228
- }
9229
- if (property.type === "StringLiteral" || property.type === "NumericLiteral") {
9230
- return property.value;
9231
- }
9232
- throw new TypeError(`Unsupported static property node '${property.type}'.`);
9233
- }
9234
- function getArrayPatternValues(value) {
9235
- if (Array.isArray(value)) {
9236
- return value;
9237
- }
9238
- if (typeof value === "string") {
9239
- return Array.from(value);
9240
- }
9241
- if (isSandboxMap(value)) {
9242
- return Array.from(value.entries, ([key, entry]) => [key, entry]);
9243
- }
9244
- if (isSandboxSet(value)) {
9245
- return [...value.values];
9246
- }
9247
- if (isIterableValue(value)) {
9248
- throw new TypeError(
9249
- `Array destructuring declarations support only arrays and strings; received ${describeRuntimeValue(value)}.`
9250
- );
9251
- }
9252
- throw new TypeError("Array destructuring declarations require an array or string iterable.");
9253
- }
9254
- function isIterableValue(value) {
9255
- return typeof value === "object" && value !== null && Symbol.iterator in value && typeof value[Symbol.iterator] === "function";
9256
- }
9257
- function describeRuntimeValue(value) {
9258
- if (value === null) return "null";
9259
- if (value === void 0) return "undefined";
9260
- if (typeof value === "object") return value.constructor?.name ?? "Object";
9261
- return typeof value;
9262
- }
9263
- async function copyObjectRestValue(value, excludedKeys, context) {
9264
- const rest = /* @__PURE__ */ Object.create(null);
9265
- const release = context.budget === void 0 ? () => void 0 : retainValues(context.budget, () => [value, rest]);
9266
- try {
9267
- for (const key of ownEnumerableSandboxKeys(value, true)) {
9268
- if (excludedKeys.has(key) || !hasOwnSandboxProperty(value, key, true)) continue;
9269
- defineProperty(rest, key, await context.getProperty(value, key));
9270
- }
9271
- return rest;
9272
- } finally {
9273
- release();
9274
- }
9275
- }
9276
- function isIndexableValue(value) {
9277
- return typeof value === "object" && value !== null;
9278
- }
9279
- function defineProperty(target, key, value) {
9280
- Object.defineProperty(target, key, {
9281
- configurable: true,
9282
- enumerable: true,
9283
- value,
9284
- writable: true
9285
- });
9286
- }
9287
-
9288
9073
  // packages/safe-js/src/interp/jobs.ts
9289
9074
  import { AsyncLocalStorage as AsyncLocalStorage4 } from "node:async_hooks";
9290
9075
  var activeJob = new AsyncLocalStorage4();
@@ -9377,31 +9162,6 @@ async function suspendJob(pending) {
9377
9162
  }
9378
9163
  }
9379
9164
 
9380
- // packages/safe-js/src/interp/var-hoist.ts
9381
- function hoistVarDeclarations(node, scope) {
9382
- for (const declaration of hoistedVarDeclarations([node])) {
9383
- for (const declarator of declaration.declarations) {
9384
- for (const identifier of boundIdentifiers(declarator.id)) {
9385
- scope.declareVar(identifier.name);
9386
- }
9387
- }
9388
- }
9389
- }
9390
-
9391
- // packages/safe-js/src/interp/data-checkpoint.ts
9392
- function createDataCheckpoint(budget, context) {
9393
- let estimatedDataSize = 0;
9394
- return (value, growth = 0, force = false) => {
9395
- const limit = budget.limits.dataSize;
9396
- if (limit === void 0) return;
9397
- estimatedDataSize = Math.max(estimatedDataSize, budget.currentDataSize) + growth;
9398
- if (!force && estimatedDataSize <= limit) return;
9399
- if (context?.reconcileData !== void 0) context.reconcileData(value);
9400
- else reconcileCompiledValues(budget, [value], context?.compilation);
9401
- estimatedDataSize = budget.currentDataSize;
9402
- };
9403
- }
9404
-
9405
9165
  // packages/safe-js/src/interp/iteration.ts
9406
9166
  async function acquireSandboxIterator(value, budget, context, asyncProtocol = false, signal) {
9407
9167
  const key = asyncProtocol ? Symbol.asyncIterator : Symbol.iterator;
@@ -9715,6 +9475,249 @@ function syncIterator(iterator) {
9715
9475
  };
9716
9476
  }
9717
9477
 
9478
+ // packages/safe-js/src/interp/patterns.ts
9479
+ async function bindPattern(pattern, value, target, scope, context) {
9480
+ switch (pattern.type) {
9481
+ case "Identifier":
9482
+ bindIdentifier(pattern, value, target, scope);
9483
+ return { ok: true };
9484
+ case "MemberExpression":
9485
+ if ("kind" in target) {
9486
+ throw new TypeError("Destructuring declarations cannot bind to member expressions.");
9487
+ }
9488
+ return bindMemberExpression(pattern, value, scope, context);
9489
+ case "AssignmentPattern":
9490
+ return bindAssignmentPattern(pattern, value, target, scope, context);
9491
+ case "ArrayPattern":
9492
+ return bindArrayPattern(pattern, value, target, scope, context);
9493
+ case "ObjectPattern":
9494
+ return bindObjectPattern(pattern, value, target, scope, context);
9495
+ case "RestElement":
9496
+ return bindPattern(pattern.argument, value, target, scope, context);
9497
+ }
9498
+ }
9499
+ function bindIdentifier(pattern, value, target, scope) {
9500
+ if ("assign" in target || target.kind === "var" && target.initialize !== true) {
9501
+ if ("assign" in target) {
9502
+ const binding = scope.lookup(pattern.name);
9503
+ if (!binding.found) {
9504
+ throw new ReferenceError(`Cannot assign to undeclared binding '${pattern.name}'.`);
9505
+ }
9506
+ if (binding.kind === "const") {
9507
+ throw new TypeError(`Cannot assign to const '${pattern.name}'`);
9508
+ }
9509
+ }
9510
+ scope.assign(pattern.name, value);
9511
+ return;
9512
+ }
9513
+ scope.declare(pattern.name, target.kind, value);
9514
+ }
9515
+ async function bindAssignmentPattern(pattern, value, target, scope, context) {
9516
+ if (value !== void 0) {
9517
+ return bindPattern(pattern.left, value, target, scope, context);
9518
+ }
9519
+ const defaultValue = await context.evaluate(
9520
+ pattern.right,
9521
+ pattern.left.type === "Identifier" ? pattern.left.name : void 0
9522
+ );
9523
+ if (defaultValue.kind !== "normal") {
9524
+ return { ok: false, result: defaultValue };
9525
+ }
9526
+ return bindPattern(pattern.left, defaultValue.value, target, scope, context);
9527
+ }
9528
+ async function bindArrayPattern(pattern, value, target, scope, context) {
9529
+ const budget = context.budget ?? new Budget();
9530
+ const iterator = await acquireSandboxIterator(
9531
+ value,
9532
+ budget,
9533
+ context.callContext ?? {
9534
+ stack: [],
9535
+ thisValue: void 0,
9536
+ getProperty: context.getProperty
9537
+ }
9538
+ );
9539
+ if (iterator === void 0) throw new TypeError("Array destructuring requires an iterable.");
9540
+ let done = false;
9541
+ const next = async (readValue = true) => {
9542
+ if (done) return { value: void 0 };
9543
+ try {
9544
+ const result = await iterator.next();
9545
+ if (typeof result !== "object" && typeof result !== "function" || result === null)
9546
+ throw new TypeError("Iterator result must be an object.");
9547
+ done = Boolean((await readIteratorResult(iterator, result, "done")).value);
9548
+ return done || !readValue ? { value: void 0 } : await readIteratorResult(iterator, result, "value");
9549
+ } catch (error) {
9550
+ done = true;
9551
+ throw error;
9552
+ }
9553
+ };
9554
+ let retained;
9555
+ const release = retainValues(budget, () => [value, iterator.retainedValue, retained]);
9556
+ try {
9557
+ for (let index = 0; index < pattern.elements.length; index += 1) {
9558
+ const element = pattern.elements[index];
9559
+ if (element === null) {
9560
+ await next(false);
9561
+ continue;
9562
+ }
9563
+ let elementValue;
9564
+ if (element.type === "RestElement") {
9565
+ const rest = [];
9566
+ retained = rest;
9567
+ for (let entry = await next(); !done; entry = await next()) {
9568
+ budget.allocateArrayLength(rest.length + 1);
9569
+ rest.push(entry.value);
9570
+ }
9571
+ elementValue = rest;
9572
+ } else {
9573
+ elementValue = (await next()).value;
9574
+ }
9575
+ retained = elementValue;
9576
+ const binding = await bindPattern(element, elementValue, target, scope, context);
9577
+ if (!binding.ok) {
9578
+ if (!done) {
9579
+ done = true;
9580
+ await closeIterator(iterator, binding.result.kind === "throw");
9581
+ }
9582
+ return binding;
9583
+ }
9584
+ }
9585
+ if (!done) {
9586
+ done = true;
9587
+ await closeIterator(iterator);
9588
+ }
9589
+ return { ok: true };
9590
+ } catch (error) {
9591
+ if (!done && !isFatalSandboxError(error)) await closeIterator(iterator, true);
9592
+ throw error;
9593
+ } finally {
9594
+ release();
9595
+ }
9596
+ }
9597
+ async function bindObjectPattern(pattern, value, target, scope, context) {
9598
+ if (typeof value !== "object" || value === null) {
9599
+ throw new TypeError("Object destructuring declarations require a non-null object value.");
9600
+ }
9601
+ const excludedKeys = /* @__PURE__ */ new Set();
9602
+ for (const property of pattern.properties) {
9603
+ if (property.type === "RestElement") {
9604
+ const binding2 = await bindPattern(
9605
+ property,
9606
+ await copyObjectRestValue(value, excludedKeys, context),
9607
+ target,
9608
+ scope,
9609
+ context
9610
+ );
9611
+ if (!binding2.ok) {
9612
+ return binding2;
9613
+ }
9614
+ continue;
9615
+ }
9616
+ const key = await evaluatePatternKey(property, context);
9617
+ if (!key.ok) {
9618
+ return key;
9619
+ }
9620
+ excludedKeys.add(typeof key.value === "symbol" ? key.value : String(key.value));
9621
+ const binding = await bindPattern(
9622
+ property.value,
9623
+ await context.getProperty(value, key.value),
9624
+ target,
9625
+ scope,
9626
+ context
9627
+ );
9628
+ if (!binding.ok) {
9629
+ return binding;
9630
+ }
9631
+ }
9632
+ return { ok: true };
9633
+ }
9634
+ async function bindMemberExpression(pattern, value, scope, context) {
9635
+ const object = await context.evaluate(pattern.object);
9636
+ if (object.kind !== "normal") {
9637
+ return { ok: false, result: object };
9638
+ }
9639
+ const property = pattern.computed ? await context.evaluate(pattern.property) : { kind: "normal", value: getStaticPropertyName(pattern.property) };
9640
+ if (property.kind !== "normal") {
9641
+ return { ok: false, result: property };
9642
+ }
9643
+ if (object.value === null || object.value === void 0) {
9644
+ throw new TypeError("Cannot assign properties of null or undefined.");
9645
+ }
9646
+ if (!isIndexableValue(object.value)) {
9647
+ throw new TypeError("Assignment expressions require a sandbox object property.");
9648
+ }
9649
+ await context.setProperty(object.value, await context.toPropertyKey(property.value), value);
9650
+ return { ok: true };
9651
+ }
9652
+ async function evaluatePatternKey(property, context) {
9653
+ return property.computed ? evaluateProperty(property.key, context) : { ok: true, value: getStaticPropertyName(property.key) };
9654
+ }
9655
+ async function evaluateProperty(property, context) {
9656
+ const result = await context.evaluate(property);
9657
+ if (result.kind !== "normal") {
9658
+ return { ok: false, result };
9659
+ }
9660
+ return { ok: true, value: await context.toPropertyKey(result.value) };
9661
+ }
9662
+ function getStaticPropertyName(property) {
9663
+ if (property.type === "Identifier") {
9664
+ return property.name;
9665
+ }
9666
+ if (property.type === "StringLiteral" || property.type === "NumericLiteral") {
9667
+ return property.value;
9668
+ }
9669
+ throw new TypeError(`Unsupported static property node '${property.type}'.`);
9670
+ }
9671
+ async function copyObjectRestValue(value, excludedKeys, context) {
9672
+ const rest = /* @__PURE__ */ Object.create(null);
9673
+ const release = context.budget === void 0 ? () => void 0 : retainValues(context.budget, () => [value, rest]);
9674
+ try {
9675
+ for (const key of ownEnumerableSandboxKeys(value, true)) {
9676
+ if (excludedKeys.has(key) || !hasOwnSandboxProperty(value, key, true)) continue;
9677
+ defineProperty(rest, key, await context.getProperty(value, key));
9678
+ }
9679
+ return rest;
9680
+ } finally {
9681
+ release();
9682
+ }
9683
+ }
9684
+ function isIndexableValue(value) {
9685
+ return typeof value === "object" && value !== null;
9686
+ }
9687
+ function defineProperty(target, key, value) {
9688
+ Object.defineProperty(target, key, {
9689
+ configurable: true,
9690
+ enumerable: true,
9691
+ value,
9692
+ writable: true
9693
+ });
9694
+ }
9695
+
9696
+ // packages/safe-js/src/interp/var-hoist.ts
9697
+ function hoistVarDeclarations(node, scope) {
9698
+ for (const declaration of hoistedVarDeclarations([node])) {
9699
+ for (const declarator of declaration.declarations) {
9700
+ for (const identifier of boundIdentifiers(declarator.id)) {
9701
+ scope.declareVar(identifier.name);
9702
+ }
9703
+ }
9704
+ }
9705
+ }
9706
+
9707
+ // packages/safe-js/src/interp/data-checkpoint.ts
9708
+ function createDataCheckpoint(budget, context) {
9709
+ let estimatedDataSize = 0;
9710
+ return (value, growth = 0, force = false) => {
9711
+ const limit = budget.limits.dataSize;
9712
+ if (limit === void 0) return;
9713
+ estimatedDataSize = Math.max(estimatedDataSize, budget.currentDataSize) + growth;
9714
+ if (!force && estimatedDataSize <= limit) return;
9715
+ if (context?.reconcileData !== void 0) context.reconcileData(value);
9716
+ else reconcileCompiledValues(budget, [value], context?.compilation);
9717
+ estimatedDataSize = budget.currentDataSize;
9718
+ };
9719
+ }
9720
+
9718
9721
  // packages/safe-js/src/interp/globals/numeric-parsers.ts
9719
9722
  function createNumericParsers(budget) {
9720
9723
  return {
@@ -15492,6 +15495,7 @@ function createPatternContext(context, scope = context.scope, evaluate = evaluat
15492
15495
  const evaluationContext = { ...context, scope };
15493
15496
  return {
15494
15497
  budget: context.budget,
15498
+ callContext: createCoercionContext(evaluationContext),
15495
15499
  evaluate: (node, inferredName) => evaluate(node, { ...evaluationContext, inferredName }),
15496
15500
  toPropertyKey: (value) => toPropertyKey(value, context.budget, createCoercionContext(evaluationContext)),
15497
15501
  getProperty: (value, key) => getPropertyValue(value, key, evaluationContext),
@@ -35882,4 +35886,4 @@ export {
35882
35886
  FileSnapshotBackend,
35883
35887
  run
35884
35888
  };
35885
- //# sourceMappingURL=chunk-56XYHJCY.js.map
35889
+ //# sourceMappingURL=chunk-AW3IBVVD.js.map