@poe-platform/safe-js 0.1.313 → 0.1.315

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.
@@ -6843,26 +6843,83 @@ function readPendingHostCallPolicyModeValue(value) {
6843
6843
  // packages/safe-js/src/interp/host-call.ts
6844
6844
  import { createHash, randomUUID } from "node:crypto";
6845
6845
 
6846
+ // packages/safe-js/src/interp/array-buffer.ts
6847
+ import { types as types2 } from "node:util";
6848
+ var readLength = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength").get;
6849
+ var readResizable = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "resizable")?.get;
6850
+ var readMaxLength = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "maxByteLength")?.get;
6851
+ var arrayBufferPrototypes = /* @__PURE__ */ new WeakMap();
6852
+ function isSandboxArrayBuffer(value) {
6853
+ return types2.isArrayBuffer(value) && Object.getPrototypeOf(value) === ArrayBuffer.prototype;
6854
+ }
6855
+ function arrayBufferLength(value) {
6856
+ return Reflect.apply(readLength, value, []);
6857
+ }
6858
+ function arrayBufferOptions(value) {
6859
+ return readResizable !== void 0 && Reflect.apply(readResizable, value, []) ? { maxByteLength: Reflect.apply(readMaxLength, value, []) } : void 0;
6860
+ }
6861
+ function copyArrayBufferStorage(value, state) {
6862
+ state.float32Buffers ??= /* @__PURE__ */ new WeakMap();
6863
+ let copy = state.float32Buffers.get(value);
6864
+ if (copy === void 0) {
6865
+ copy = Reflect.construct(ArrayBuffer, [arrayBufferLength(value), arrayBufferOptions(value)]);
6866
+ new Uint8Array(copy).set(new Uint8Array(value));
6867
+ state.float32Buffers.set(value, copy);
6868
+ }
6869
+ return copy;
6870
+ }
6871
+ function arrayBufferDataProperties(value) {
6872
+ return Reflect.ownKeys(value).map((key) => {
6873
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
6874
+ if (!("value" in descriptor)) throw new TypeError("ArrayBuffer accessors cannot be copied as data.");
6875
+ return [key, descriptor];
6876
+ });
6877
+ }
6878
+
6846
6879
  // packages/safe-js/src/interp/float32.ts
6847
6880
  var typedArrayPrototype = Object.getPrototypeOf(Float32Array.prototype);
6848
- var readLength = Object.getOwnPropertyDescriptor(typedArrayPrototype, "length").get;
6881
+ var readLength2 = Object.getOwnPropertyDescriptor(typedArrayPrototype, "length").get;
6849
6882
  var readOffset = Object.getOwnPropertyDescriptor(typedArrayPrototype, "byteOffset").get;
6850
6883
  var readBuffer = Object.getOwnPropertyDescriptor(typedArrayPrototype, "buffer").get;
6851
6884
  var readTag = Object.getOwnPropertyDescriptor(typedArrayPrototype, Symbol.toStringTag).get;
6885
+ var createValuesIterator = Object.getOwnPropertyDescriptor(typedArrayPrototype, "values").value;
6852
6886
  var bufferLength = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength").get;
6853
6887
  var bufferResizable = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "resizable")?.get;
6888
+ var float32ViewLayouts = /* @__PURE__ */ new WeakMap();
6889
+ var resizeBuffer = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "resize").value;
6890
+ function restoreFloat32View(buffer, byteOffset, length, budget) {
6891
+ const originalLength = arrayBufferLength(buffer);
6892
+ const required = byteOffset + (length ?? 0) * 4;
6893
+ const options = arrayBufferOptions(buffer);
6894
+ const grow = required > originalLength;
6895
+ if (grow) {
6896
+ if (options === void 0 || required > options.maxByteLength)
6897
+ throw new RangeError("Float32Array layout exceeds backing capacity.");
6898
+ budget?.allocateArrayLength(Math.ceil(required / 4));
6899
+ budget?.provisionDataUsage(required - originalLength)();
6900
+ Reflect.apply(resizeBuffer, buffer, [required]);
6901
+ }
6902
+ try {
6903
+ const view = new Float32Array(buffer, byteOffset, length);
6904
+ if (options !== void 0) float32ViewLayouts.set(view, { byteOffset, ...length === void 0 ? {} : { length } });
6905
+ return view;
6906
+ } finally {
6907
+ if (grow) Reflect.apply(resizeBuffer, buffer, [originalLength]);
6908
+ }
6909
+ }
6854
6910
  function isFloat32Array(value) {
6855
6911
  return ArrayBuffer.isView(value) && Object.getPrototypeOf(value) === Float32Array.prototype && Reflect.apply(readTag, value, []) === "Float32Array";
6856
6912
  }
6857
- function float32Storage(value) {
6913
+ function float32Storage(value, requireInBounds = false) {
6914
+ if (requireInBounds) Reflect.apply(createValuesIterator, value, []);
6858
6915
  const buffer = Reflect.apply(readBuffer, value, []);
6859
- if (Object.getPrototypeOf(buffer) !== ArrayBuffer.prototype || bufferResizable !== void 0 && Reflect.apply(bufferResizable, buffer, [])) {
6860
- throw new TypeError("Float32Array requires a fixed, non-shared ArrayBuffer.");
6916
+ if (Object.getPrototypeOf(buffer) !== ArrayBuffer.prototype) {
6917
+ throw new TypeError("Float32Array requires a non-shared ArrayBuffer.");
6861
6918
  }
6862
6919
  return {
6863
6920
  buffer,
6864
6921
  byteOffset: Reflect.apply(readOffset, value, []),
6865
- length: Reflect.apply(readLength, value, []),
6922
+ length: Reflect.apply(readLength2, value, []),
6866
6923
  byteLength: Reflect.apply(bufferLength, buffer, [])
6867
6924
  };
6868
6925
  }
@@ -6903,18 +6960,17 @@ function checkFloat32Allocation(length, budget) {
6903
6960
  }
6904
6961
  function copyFloat32Storage(value, state) {
6905
6962
  const storage = float32Storage(value);
6906
- state.float32Buffers ??= /* @__PURE__ */ new WeakMap();
6907
- let buffer = state.float32Buffers.get(storage.buffer);
6908
- if (buffer === void 0) {
6909
- buffer = new ArrayBuffer(storage.byteLength);
6910
- new Uint8Array(buffer).set(new Uint8Array(storage.buffer));
6911
- state.float32Buffers.set(storage.buffer, buffer);
6963
+ const buffer = copyArrayBufferStorage(storage.buffer, state);
6964
+ if (bufferResizable !== void 0 && Reflect.apply(bufferResizable, storage.buffer, [])) {
6965
+ const layout = float32ViewLayouts.get(value);
6966
+ if (layout === void 0) throw new TypeError("Resizable Float32Array copies require known view layout.");
6967
+ return restoreFloat32View(buffer, layout.byteOffset, layout.length);
6912
6968
  }
6913
6969
  return new Float32Array(buffer, storage.byteOffset, storage.length);
6914
6970
  }
6915
6971
 
6916
6972
  // packages/safe-js/src/interp/date.ts
6917
- import { types as types2 } from "node:util";
6973
+ import { types as types3 } from "node:util";
6918
6974
  var NativeDate = Date;
6919
6975
  var readTime = NativeDate.prototype.getTime;
6920
6976
  var writeTime = NativeDate.prototype.setTime;
@@ -6931,7 +6987,7 @@ function dateTime(value) {
6931
6987
  return Reflect.apply(readTime, value, []);
6932
6988
  }
6933
6989
  function copyNativeDate(value) {
6934
- if (!types2.isDate(value)) return void 0;
6990
+ if (!types3.isDate(value)) return void 0;
6935
6991
  if (Object.getPrototypeOf(value) !== NativeDate.prototype)
6936
6992
  throw new TypeError("Date subclasses are not supported.");
6937
6993
  return createSandboxDate(dateTime(value));
@@ -7366,7 +7422,7 @@ function copySandboxArgumentProperties(source, target, copyValue) {
7366
7422
  }
7367
7423
 
7368
7424
  // packages/safe-js/src/interp/boxed.ts
7369
- import { types as types3 } from "node:util";
7425
+ import { types as types4 } from "node:util";
7370
7426
  var boxes = /* @__PURE__ */ new WeakSet();
7371
7427
  var numberValue = Number.prototype.valueOf;
7372
7428
  var stringValue = String.prototype.valueOf;
@@ -7374,11 +7430,11 @@ var booleanValue = Boolean.prototype.valueOf;
7374
7430
  var symbolValue = Symbol.prototype.valueOf;
7375
7431
  var bigintValue = BigInt.prototype.valueOf;
7376
7432
  function nativeBoxedValue(value) {
7377
- if (types3.isNumberObject(value)) return Reflect.apply(numberValue, value, []);
7378
- if (types3.isStringObject(value)) return Reflect.apply(stringValue, value, []);
7379
- if (types3.isBooleanObject(value)) return Reflect.apply(booleanValue, value, []);
7380
- if (types3.isSymbolObject(value)) return Reflect.apply(symbolValue, value, []);
7381
- if (types3.isBigIntObject(value)) return Reflect.apply(bigintValue, value, []);
7433
+ if (types4.isNumberObject(value)) return Reflect.apply(numberValue, value, []);
7434
+ if (types4.isStringObject(value)) return Reflect.apply(stringValue, value, []);
7435
+ if (types4.isBooleanObject(value)) return Reflect.apply(booleanValue, value, []);
7436
+ if (types4.isSymbolObject(value)) return Reflect.apply(symbolValue, value, []);
7437
+ if (types4.isBigIntObject(value)) return Reflect.apply(bigintValue, value, []);
7382
7438
  return void 0;
7383
7439
  }
7384
7440
  function createSandboxBox(value) {
@@ -7557,7 +7613,7 @@ async function withRunResources(signal, execute) {
7557
7613
  }
7558
7614
 
7559
7615
  // packages/safe-js/src/interp/host-capabilities.ts
7560
- import { types as types4 } from "node:util";
7616
+ import { types as types5 } from "node:util";
7561
7617
  var MAX_INDEXED_LENGTH = 65536;
7562
7618
  var MAX_NAMED_KEYS = 65536;
7563
7619
  var MAX_NAMED_KEY_CODE_UNITS = 1048576;
@@ -7612,7 +7668,7 @@ function createLiveHostObject(definition, controller) {
7612
7668
  for (const name of ["keys", "get", "set", "delete"]) {
7613
7669
  const operation = data[name];
7614
7670
  if (operation === void 0 && (name === "set" || name === "delete")) continue;
7615
- if (typeof operation !== "function" || types4.isProxy(operation) || types4.isAsyncFunction(operation) || types4.isGeneratorFunction(operation))
7671
+ if (typeof operation !== "function" || types5.isProxy(operation) || types5.isAsyncFunction(operation) || types5.isGeneratorFunction(operation))
7616
7672
  throw new TypeError(`Named ${name} must be a synchronous non-generator function, not a proxy.`);
7617
7673
  }
7618
7674
  if (typeof data.maxKeys !== "number" || !Number.isInteger(data.maxKeys) || data.maxKeys < 1 || data.maxKeys > MAX_NAMED_KEYS)
@@ -7867,7 +7923,7 @@ function namedMutationKeys(state, key, create) {
7867
7923
  function namedKeys(state) {
7868
7924
  const named = state.named;
7869
7925
  return state.controller.read(named.keys, (value) => {
7870
- if (!Array.isArray(value) || types4.isProxy(value))
7926
+ if (!Array.isArray(value) || types5.isProxy(value))
7871
7927
  throw new TypeError("Named keys must be a dense own-data array of strings, not a proxy.");
7872
7928
  const length = Object.getOwnPropertyDescriptor(value, "length").value;
7873
7929
  if (length > named.maxKeys) throw new RangeError("Named keys exceed maxKeys.");
@@ -8132,11 +8188,13 @@ function releaseObjectPrototype(budget) {
8132
8188
  generatorPrototypes.delete(budget);
8133
8189
  errorPrototypes.delete(budget);
8134
8190
  float32Prototypes.delete(budget);
8191
+ arrayBufferPrototypes.delete(budget);
8135
8192
  initialRegexDescriptors.delete(budget);
8136
8193
  intrinsicPrototypes.delete(budget);
8137
8194
  }
8138
8195
  function getSandboxPrototype(value, budget) {
8139
8196
  if (prototypes.has(value)) return prototypes.get(value) ?? null;
8197
+ if (isSandboxArrayBuffer(value)) return budget === void 0 ? null : arrayBufferPrototypes.get(budget) ?? null;
8140
8198
  if (isFloat32Array(value)) return budget === void 0 ? null : float32Prototypes.get(budget) ?? null;
8141
8199
  const errorType = sandboxErrorTypes.get(value);
8142
8200
  if (budget !== void 0 && errorType !== void 0) {
@@ -8247,6 +8305,7 @@ function setSandboxPrototype(value, prototype, budget) {
8247
8305
  prototypes.set(value, prototype);
8248
8306
  }
8249
8307
  function isPrototypeRecord(value) {
8308
+ if (isSandboxArrayBuffer(value)) return true;
8250
8309
  if (isFloat32Array(value)) return true;
8251
8310
  if (isGuestClosure(value)) return true;
8252
8311
  if (isGuestHostObject(value)) return false;
@@ -8511,7 +8570,7 @@ function restoreRegexProperties(value, data, decode) {
8511
8570
  }
8512
8571
 
8513
8572
  // packages/safe-js/src/snapshot/validation.ts
8514
- import { types as types6 } from "node:util";
8573
+ import { types as types7 } from "node:util";
8515
8574
 
8516
8575
  // packages/safe-js/src/interp/builtin-call.ts
8517
8576
  async function invokeBuiltinClosure(closure, args, budget, context, thisValue, construct = false) {
@@ -10925,7 +10984,7 @@ function formatNumber(value, methodName, argument, budget) {
10925
10984
  }
10926
10985
 
10927
10986
  // packages/safe-js/src/interp/methods/string.ts
10928
- import { types as types5 } from "node:util";
10987
+ import { types as types6 } from "node:util";
10929
10988
 
10930
10989
  // packages/safe-js/src/interp/methods/string-locale.ts
10931
10990
  var collatorOptions = [
@@ -11076,7 +11135,7 @@ function callStringMethod(value, methodName, args, budget, callClosure = async (
11076
11135
  };
11077
11136
  const symbol = methodName === "match" ? Symbol.match : methodName === "search" ? Symbol.search : methodName === "matchAll" ? Symbol.matchAll : methodName === "replace" || methodName === "replaceAll" ? Symbol.replace : methodName === "split" ? Symbol.split : void 0;
11078
11137
  const pattern = args[0];
11079
- if (symbol !== void 0 && types5.isRegExp(pattern))
11138
+ if (symbol !== void 0 && types6.isRegExp(pattern))
11080
11139
  throw new TypeError(`String#${methodName} does not accept unbranded host RegExp values.`);
11081
11140
  if (symbol === void 0 || pattern === null || pattern === void 0) return fallback();
11082
11141
  const readProperty = (key) => context?.getProperty !== void 0 ? context.getProperty(pattern, key) : key === "flags" && isSandboxRegex(pattern) && getSandboxPropertyDescriptor(pattern, key, budget) === void 0 ? getRegexMember(pattern, key, budget, context) : getSandboxDataProperty(pattern, key, budget);
@@ -14557,6 +14616,31 @@ function createFloat32ArrayGlobal(budget, nativePrototype = false) {
14557
14616
  const prototype = candidate !== null && typeof candidate === "object" ? candidate : float32Prototypes.get(budget);
14558
14617
  const release = retainValues(budget, () => [prototype, ...args]);
14559
14618
  try {
14619
+ if (isSandboxArrayBuffer(args[0])) {
14620
+ const buffer = args[0];
14621
+ const number = await sandboxNumber(args[1], budget, context);
14622
+ const offset = Number.isNaN(number) ? 0 : Math.trunc(number);
14623
+ if (!Number.isSafeInteger(offset) || offset < 0 || offset % 4 !== 0)
14624
+ throw new RangeError("Invalid Float32Array buffer offset.");
14625
+ const bytes = arrayBufferLength(buffer);
14626
+ let length;
14627
+ if (args[2] === void 0) {
14628
+ if (arrayBufferOptions(buffer) === void 0 && bytes % 4 !== 0 || offset > bytes)
14629
+ throw new RangeError("Invalid Float32Array buffer length.");
14630
+ length = Math.floor((bytes - offset) / 4);
14631
+ } else {
14632
+ const size = await sandboxNumber(args[2], budget, context);
14633
+ length = Number.isNaN(size) ? 0 : Math.trunc(size);
14634
+ if (!Number.isSafeInteger(length) || length < 0 || offset + length * 4 > arrayBufferLength(buffer))
14635
+ throw new RangeError("Invalid Float32Array view length.");
14636
+ }
14637
+ budget.allocateArrayLength(length);
14638
+ const result2 = new Float32Array(buffer, offset, args[2] === void 0 ? void 0 : length);
14639
+ if (arrayBufferOptions(buffer) !== void 0)
14640
+ float32ViewLayouts.set(result2, { byteOffset: offset, ...args[2] === void 0 ? {} : { length } });
14641
+ setSandboxPrototype(result2, prototype, budget);
14642
+ return result2;
14643
+ }
14560
14644
  const result = args[0] !== null && typeof args[0] === "object" && !isFloat32Array(args[0]) ? await allocateFloat32Input(args[0], budget, context) : allocateFloat32Array(args[0], budget);
14561
14645
  setSandboxPrototype(result, prototype, budget);
14562
14646
  return result;
@@ -14771,7 +14855,7 @@ function createFloat32ArrayPrototypes(budget, constructor) {
14771
14855
  setSandboxPrototype(prototype, shared);
14772
14856
  setSandboxPrototype(shared, getSandboxPrototype(/* @__PURE__ */ Object.create(null), budget));
14773
14857
  const getters = [];
14774
- for (const key of ["length", "byteLength", "byteOffset"]) {
14858
+ for (const key of ["length", "byteLength", "byteOffset", "buffer"]) {
14775
14859
  const getter = createSandboxClosure({
14776
14860
  guest: true,
14777
14861
  sandbox: true,
@@ -14801,7 +14885,7 @@ function createFloat32ArrayPrototypes(budget, constructor) {
14801
14885
  call: async (args, context) => {
14802
14886
  if (!isFloat32Array(context?.thisValue)) throw new TypeError("TypedArray join requires a typed array receiver.");
14803
14887
  const receiver = context.thisValue;
14804
- const length = float32Storage(receiver).length;
14888
+ const length = float32Storage(receiver, true).length;
14805
14889
  let separator = ",";
14806
14890
  let text = "";
14807
14891
  const release = retainValues(budget, () => [receiver, separator, text, ...args]);
@@ -14836,6 +14920,7 @@ function createFloat32ArrayPrototypes(budget, constructor) {
14836
14920
  length: 0,
14837
14921
  call: (args, context) => {
14838
14922
  if (!isFloat32Array(context?.thisValue)) throw new TypeError(`TypedArray ${key} requires a typed array receiver.`);
14923
+ float32Storage(context.thisValue, true);
14839
14924
  return invokeBuiltinClosure(method, args, budget, context, context.thisValue);
14840
14925
  }
14841
14926
  });
@@ -14876,20 +14961,20 @@ function getFloat32Member(value, property, budget) {
14876
14961
  const receiver = context?.thisValue;
14877
14962
  if (!isFloat32Array(receiver))
14878
14963
  throw new TypeError(`Float32Array#${key} requires a Float32Array receiver.`);
14879
- const storage2 = float32Storage(receiver);
14964
+ const storage2 = float32Storage(receiver, key === "slice");
14965
+ const bridge = {
14966
+ ...context,
14967
+ stack: context?.stack ?? [],
14968
+ thisValue: receiver,
14969
+ getProperty: context?.getProperty ?? ((value2, property2) => {
14970
+ const descriptor2 = getSandboxPropertyDescriptor(value2, property2, budget);
14971
+ return descriptor2 === void 0 ? getSandboxDataProperty(value2, property2, budget) : readPropertyDescriptor(descriptor2, value2, bridge);
14972
+ }),
14973
+ invokeClosure: context?.invokeClosure ?? ((callee, values, thisValue, construct) => invokeBuiltinClosure(callee, values, budget, context, thisValue, construct))
14974
+ };
14880
14975
  if (key === "set") {
14881
14976
  return (async () => {
14882
14977
  const [source, offsetValue = 0] = args;
14883
- const bridge = {
14884
- ...context,
14885
- stack: context?.stack ?? [],
14886
- thisValue: receiver,
14887
- getProperty: context?.getProperty ?? ((value2, property2) => {
14888
- const descriptor2 = getSandboxPropertyDescriptor(value2, property2, budget);
14889
- return descriptor2 === void 0 ? getSandboxDataProperty(value2, property2, budget) : readPropertyDescriptor(descriptor2, value2, bridge);
14890
- }),
14891
- invokeClosure: context?.invokeClosure ?? ((callee, values, thisValue, construct) => invokeBuiltinClosure(callee, values, budget, context, thisValue, construct))
14892
- };
14893
14978
  let current;
14894
14979
  let sourceObject;
14895
14980
  const release = retainValues(budget, () => [receiver, sourceObject, current, ...args]);
@@ -14897,18 +14982,19 @@ function getFloat32Member(value, property, budget) {
14897
14982
  const number = await sandboxNumber(offsetValue, budget, bridge);
14898
14983
  const offset = Number.isNaN(number) ? 0 : Math.trunc(number);
14899
14984
  if (offset < 0) throw new RangeError("Float32Array#set offset is out of bounds.");
14985
+ const targetStorage = float32Storage(receiver, true);
14900
14986
  if (source === null || source === void 0) throw new TypeError("Float32Array#set requires a non-null source.");
14901
- let length2;
14902
- if (isFloat32Array(source)) length2 = float32Storage(source).length;
14987
+ let length;
14988
+ if (isFloat32Array(source)) length = float32Storage(source, true).length;
14903
14989
  else {
14904
14990
  sourceObject = typeof source === "object" ? source : createSandboxBox(source);
14905
14991
  current = await bridge.getProperty(sourceObject, "length");
14906
14992
  const size = await sandboxNumber(current, budget, bridge);
14907
- length2 = Number.isNaN(size) || size <= 0 ? 0 : Math.min(Math.trunc(size), Number.MAX_SAFE_INTEGER);
14993
+ length = Number.isNaN(size) || size <= 0 ? 0 : Math.min(Math.trunc(size), Number.MAX_SAFE_INTEGER);
14908
14994
  }
14909
- if (offset + length2 > storage2.length) throw new RangeError("Float32Array#set source is out of bounds.");
14995
+ if (offset + length > targetStorage.length) throw new RangeError("Float32Array#set source is out of bounds.");
14910
14996
  if (isFloat32Array(source)) Float32Array.prototype.set.call(receiver, source, offset);
14911
- else for (let index = 0; index < length2; index++) {
14997
+ else for (let index = 0; index < length; index++) {
14912
14998
  budget.visitNode();
14913
14999
  current = await bridge.getProperty(sourceObject, String(index));
14914
15000
  receiver[offset + index] = await sandboxNumber(current, budget, bridge);
@@ -14919,17 +15005,31 @@ function getFloat32Member(value, property, budget) {
14919
15005
  }
14920
15006
  })();
14921
15007
  }
14922
- const start = relativeIndex(args[0], storage2.length, 0);
14923
- const end = relativeIndex(args[1], storage2.length, storage2.length);
14924
- const length = Math.max(end - start, 0);
14925
- if (key === "subarray")
14926
- return new Float32Array(storage2.buffer, storage2.byteOffset + start * 4, length);
14927
- checkFloat32Allocation(length, budget);
14928
- const result = new Float32Array(length);
14929
- new Uint8Array(result.buffer).set(
14930
- new Uint8Array(storage2.buffer, storage2.byteOffset + start * 4, length * 4)
14931
- );
14932
- return result;
15008
+ return (async () => {
15009
+ const release = retainValues(budget, () => [receiver, ...args]);
15010
+ try {
15011
+ const start = relativeIndex(await sandboxNumber(args[0], budget, bridge), storage2.length);
15012
+ const end = args[1] === void 0 ? storage2.length : relativeIndex(await sandboxNumber(args[1], budget, bridge), storage2.length);
15013
+ const length = Math.max(end - start, 0);
15014
+ if (key === "subarray") {
15015
+ const layout = float32ViewLayouts.get(receiver);
15016
+ const offset = (layout?.byteOffset ?? storage2.byteOffset) + start * 4;
15017
+ const tracking = layout !== void 0 && layout.length === void 0 && args[1] === void 0;
15018
+ const result2 = new Float32Array(storage2.buffer, offset, tracking ? void 0 : length);
15019
+ if (arrayBufferOptions(storage2.buffer) !== void 0)
15020
+ float32ViewLayouts.set(result2, { byteOffset: offset, ...tracking ? {} : { length } });
15021
+ return result2;
15022
+ }
15023
+ checkFloat32Allocation(length, budget);
15024
+ const result = new Float32Array(length);
15025
+ new Uint8Array(result.buffer).set(
15026
+ new Uint8Array(storage2.buffer, storage2.byteOffset + start * 4, length * 4)
15027
+ );
15028
+ return result;
15029
+ } finally {
15030
+ release();
15031
+ }
15032
+ })();
14933
15033
  }
14934
15034
  });
14935
15035
  }
@@ -14951,9 +15051,7 @@ function setFloat32Member(value, property, entry, budget) {
14951
15051
  descriptor === void 0 ? { value: entry, configurable: true, enumerable: true, writable: true } : { value: entry }
14952
15052
  );
14953
15053
  }
14954
- function relativeIndex(value, length, fallback) {
14955
- if (value === void 0) return fallback;
14956
- const numeric = float32Number(value);
15054
+ function relativeIndex(numeric, length) {
14957
15055
  const integer2 = Number.isNaN(numeric) ? 0 : Math.trunc(numeric);
14958
15056
  return integer2 < 0 ? Math.max(length + integer2, 0) : Math.min(integer2, length);
14959
15057
  }
@@ -21041,18 +21139,20 @@ function serializeArray(value, serializeValue) {
21041
21139
  }
21042
21140
 
21043
21141
  // packages/safe-js/src/snapshot/float32array.ts
21044
- function encodeFloat32Storage(value, id, buffers, reference) {
21142
+ function encodeFloat32Layout(value) {
21045
21143
  const storage = float32Storage(value);
21046
- const existing = buffers.get(storage.buffer);
21047
- if (existing === void 0) buffers.set(storage.buffer, id);
21144
+ if (arrayBufferOptions(storage.buffer) === void 0) return { byteOffset: storage.byteOffset, length: storage.length };
21145
+ const layout = float32ViewLayouts.get(value);
21146
+ if (layout === void 0) throw new TypeError("Resizable Float32Array snapshots require known view layout.");
21048
21147
  return {
21049
- kind: "float32array",
21050
- byteOffset: storage.byteOffset,
21051
- length: storage.length,
21052
- ...existing === void 0 ? { bytes: Array.from(new Uint8Array(storage.buffer)) } : { buffer: reference(existing) }
21148
+ byteOffset: layout.byteOffset,
21149
+ length: layout.length ?? 0,
21150
+ ...layout.length === void 0 ? { lengthTracking: true } : {}
21053
21151
  };
21054
21152
  }
21055
21153
  function validateFloat32Storage(value) {
21154
+ if (Object.hasOwn(value, "lengthTracking") && (value.lengthTracking !== true || value.length !== 0 || Object.hasOwn(value, "bytes")))
21155
+ throw new TypeError("Invalid Float32Array length-tracking layout.");
21056
21156
  if (!Number.isSafeInteger(value.length) || Number(value.length) < 0 || !Number.isSafeInteger(value.byteOffset) || Number(value.byteOffset) < 0 || Number(value.byteOffset) % 4 !== 0)
21057
21157
  throw new TypeError("Invalid Float32Array view dimensions.");
21058
21158
  if (Object.hasOwn(value, "bytes") === Object.hasOwn(value, "buffer"))
@@ -21062,7 +21162,7 @@ function validateFloat32Storage(value) {
21062
21162
  throw new TypeError("Invalid Float32Array backing bytes.");
21063
21163
  }
21064
21164
  }
21065
- function decodeFloat32Storage(value, resolve) {
21165
+ function decodeFloat32Storage(value, resolve, budget) {
21066
21166
  validateFloat32Storage(value);
21067
21167
  let buffer;
21068
21168
  if (Array.isArray(value.bytes)) {
@@ -21070,10 +21170,50 @@ function decodeFloat32Storage(value, resolve) {
21070
21170
  new Uint8Array(buffer).set(value.bytes);
21071
21171
  } else {
21072
21172
  const referenced = resolve(value.buffer);
21073
- if (!isFloat32Array(referenced)) throw new TypeError("Invalid Float32Array backing reference.");
21074
- buffer = float32Storage(referenced).buffer;
21173
+ if (isSandboxArrayBuffer(referenced)) buffer = referenced;
21174
+ else if (isFloat32Array(referenced)) buffer = float32Storage(referenced).buffer;
21175
+ else throw new TypeError("Invalid Float32Array backing reference.");
21075
21176
  }
21076
- return new Float32Array(buffer, Number(value.byteOffset), Number(value.length));
21177
+ if (value.lengthTracking === true && arrayBufferOptions(buffer) === void 0)
21178
+ throw new TypeError("Length-tracking layout requires resizable backing storage.");
21179
+ return restoreFloat32View(buffer, Number(value.byteOffset), value.lengthTracking === true ? void 0 : Number(value.length), budget);
21180
+ }
21181
+
21182
+ // packages/safe-js/src/snapshot/array-buffer.ts
21183
+ function captureArrayBufferState(value, encode) {
21184
+ return {
21185
+ properties: serializePropertyDescriptors(value, encode),
21186
+ ...hasExplicitSandboxPrototype(value) ? { prototype: encode(getSandboxPrototype(value)) } : {}
21187
+ };
21188
+ }
21189
+ function encodeArrayBufferStorage(value, id, buffers, reference) {
21190
+ arrayBufferLength(value);
21191
+ const existing = buffers.get(value);
21192
+ if (existing === void 0) buffers.set(value, id);
21193
+ return { kind: "arraybuffer", ...existing === void 0 ? { bytes: Array.from(new Uint8Array(value)), ...arrayBufferOptions(value) } : { buffer: reference(existing) } };
21194
+ }
21195
+ function validateArrayBufferStorage(value) {
21196
+ if (Object.hasOwn(value, "bytes") === Object.hasOwn(value, "buffer"))
21197
+ throw new TypeError("ArrayBuffer requires one backing storage description.");
21198
+ if (Object.hasOwn(value, "bytes") && (!Array.isArray(value.bytes) || value.bytes.some((byte) => !Number.isInteger(byte) || byte < 0 || byte > 255)))
21199
+ throw new TypeError("Invalid ArrayBuffer bytes.");
21200
+ if (Object.hasOwn(value, "maxByteLength") && (!Array.isArray(value.bytes) || !Number.isSafeInteger(value.maxByteLength) || Number(value.maxByteLength) < value.bytes.length))
21201
+ throw new TypeError("Invalid ArrayBuffer maximum length.");
21202
+ }
21203
+ function decodeArrayBufferStorage(value, resolve) {
21204
+ validateArrayBufferStorage(value);
21205
+ if (Array.isArray(value.bytes)) {
21206
+ const buffer = Reflect.construct(ArrayBuffer, [
21207
+ value.bytes.length,
21208
+ Object.hasOwn(value, "maxByteLength") ? { maxByteLength: value.maxByteLength } : void 0
21209
+ ]);
21210
+ new Uint8Array(buffer).set(value.bytes);
21211
+ return buffer;
21212
+ }
21213
+ const referenced = resolve(value.buffer);
21214
+ if (isSandboxArrayBuffer(referenced)) return referenced;
21215
+ if (isFloat32Array(referenced)) return float32Storage(referenced).buffer;
21216
+ throw new TypeError("Invalid ArrayBuffer backing reference.");
21077
21217
  }
21078
21218
 
21079
21219
  // packages/safe-js/src/snapshot/date-properties.ts
@@ -21204,7 +21344,7 @@ function serializeDumpValue(value, path, state) {
21204
21344
  if (hasGuestObjectState(value) && !(state.trustedRunReplay && (isSandboxDate(value) || isSandboxPromise(value) || isPromiseResolvingFunction(value)))) {
21205
21345
  throw new TypeError("Guest function properties and prototype links cannot be serialized.");
21206
21346
  }
21207
- if (isSandboxRegExpIterator(value) || isSandboxRegex(value) && hasCustomRegexProperties(value) || isSandboxBox(value) || isSandboxDate(value) || isFloat32Array(value)) return serializeHeapReference(value, path, state);
21347
+ if (isSandboxArrayBuffer(value) || isSandboxRegExpIterator(value) || isSandboxRegex(value) && hasCustomRegexProperties(value) || isSandboxBox(value) || isSandboxDate(value) || isFloat32Array(value)) return serializeHeapReference(value, path, state);
21208
21348
  if (Array.isArray(value)) {
21209
21349
  const reference2 = serializeHeapReference(value, path, state);
21210
21350
  if (reference2 !== void 0) {
@@ -21276,11 +21416,21 @@ function serializeHeapReference(value, path, state) {
21276
21416
  const serialized = serializeDumpValue(entry, `${path}.<date-property>`, state);
21277
21417
  return serialized === SKIP_VALUE ? { kind: "undefined" } : serialized;
21278
21418
  });
21419
+ } else if (isSandboxArrayBuffer(value)) {
21420
+ state.heap[String(id)] = {
21421
+ ...encodeArrayBufferStorage(value, id, state.float32Buffers, (id2) => ({ kind: "ref", id: id2 })),
21422
+ state: captureArrayBufferState(value, (entry) => {
21423
+ const serialized = serializeDumpValue(entry, `${path}.<buffer>`, state);
21424
+ return serialized === SKIP_VALUE ? { kind: "undefined" } : serialized;
21425
+ })
21426
+ };
21279
21427
  } else if (isFloat32Array(value)) {
21280
- const storage = encodeFloat32Storage(value, id, state.float32Buffers, (id2) => ({
21281
- kind: "ref",
21282
- id: id2
21283
- }));
21428
+ const backing = float32Storage(value);
21429
+ const storage = {
21430
+ kind: "float32array",
21431
+ ...encodeFloat32Layout(value),
21432
+ buffer: serializeHeapReference(backing.buffer, `${path}.buffer`, state)
21433
+ };
21284
21434
  const entries = /* @__PURE__ */ Object.create(null);
21285
21435
  state.heap[String(id)] = { ...storage, entries };
21286
21436
  for (const [key, descriptor] of float32DataProperties(value)) {
@@ -21346,7 +21496,7 @@ function indexHeapContainers(snapshot) {
21346
21496
  const heapIds = /* @__PURE__ */ new Map();
21347
21497
  let nextId = 1;
21348
21498
  for (const [value, stat2] of stats.entries()) {
21349
- if (stat2.count > 1 || stat2.forceHeap === true || guestValues.has(value) || stat2.cyclic || hasNullObjectPrototype(value) || ownSerializableSymbolKeys(value).length > 0 || isSandboxBox(value) || isSandboxRegExpIterator(value) || isSandboxRegex(value) && hasCustomRegexProperties(value) || isSandboxDate(value) || isFloat32Array(value) || Array.isArray(value) && requiresArrayEntries(value) || isSandboxArguments(value) || sandboxErrorTypes.has(value)) {
21499
+ if (stat2.count > 1 || stat2.forceHeap === true || guestValues.has(value) || stat2.cyclic || hasNullObjectPrototype(value) || ownSerializableSymbolKeys(value).length > 0 || isSandboxBox(value) || isSandboxRegExpIterator(value) || isSandboxRegex(value) && hasCustomRegexProperties(value) || isSandboxDate(value) || isFloat32Array(value) || isSandboxArrayBuffer(value) || Array.isArray(value) && requiresArrayEntries(value) || isSandboxArguments(value) || sandboxErrorTypes.has(value)) {
21350
21500
  heapIds.set(value, nextId);
21351
21501
  nextId += 1;
21352
21502
  }
@@ -21378,12 +21528,16 @@ function collectContainerStats(value, stats, ancestors, guestValues, depth = 0)
21378
21528
  stat2.expanded = true;
21379
21529
  ancestors.add(value);
21380
21530
  const guestEntries = [];
21381
- const guest = captureGuestHeapNode(value, (entry) => {
21531
+ if (isFloat32Array(value)) guestEntries.push(float32Storage(value).buffer);
21532
+ const guest = isSandboxArrayBuffer(value) ? { kind: "arraybuffer", state: captureArrayBufferState(value, (entry) => {
21533
+ guestEntries.push(entry);
21534
+ return null;
21535
+ }) } : captureGuestHeapNode(value, (entry) => {
21382
21536
  guestEntries.push(entry);
21383
21537
  return null;
21384
21538
  });
21385
21539
  if (guest !== void 0) {
21386
- guestValues.add(value);
21540
+ if (!isSandboxArrayBuffer(value)) guestValues.add(value);
21387
21541
  for (const entry of guestEntries) {
21388
21542
  collectContainerStats(entry, stats, ancestors, guestValues, depth + 1);
21389
21543
  if (entry !== null && typeof entry === "object") stats.get(entry).forceHeap = true;
@@ -21391,6 +21545,7 @@ function collectContainerStats(value, stats, ancestors, guestValues, depth = 0)
21391
21545
  ancestors.delete(value);
21392
21546
  return;
21393
21547
  }
21548
+ for (const entry of guestEntries) collectContainerStats(entry, stats, ancestors, guestValues, depth + 1);
21394
21549
  if (!Array.isArray(value) && !isPlainObject(value) && !isFloat32Array(value) && !isSandboxDate(value)) {
21395
21550
  ancestors.delete(value);
21396
21551
  return;
@@ -21710,11 +21865,24 @@ function encodeReplayData(value, options = {}) {
21710
21865
  ...symbolProperties.length === 0 ? {} : { symbolProperties },
21711
21866
  ...Object.isExtensible(entry) ? {} : { extensible: false }
21712
21867
  };
21868
+ } else if (isSandboxArrayBuffer(entry)) {
21869
+ const storage = encodeArrayBufferStorage(entry, id, float32Buffers, (id2) => ({ tag: "ref", id: id2 }));
21870
+ const properties = /* @__PURE__ */ Object.create(null);
21871
+ for (const [key, descriptor] of arrayBufferDataProperties(entry)) {
21872
+ if (typeof key === "symbol") continue;
21873
+ properties[key] = {
21874
+ value: child(descriptor.value, key),
21875
+ configurable: descriptor.configurable === true,
21876
+ enumerable: descriptor.enumerable === true,
21877
+ writable: descriptor.writable === true
21878
+ };
21879
+ }
21880
+ let symbolIndex = 0;
21881
+ const symbolEntries = serializeSymbolProperties(entry, (value2) => encode(value2, depth + 1, [...path, { symbol: Math.floor(symbolIndex++ / 2) }]));
21882
+ nodes[id] = { ...storage, properties, extensible: Object.isExtensible(entry), symbolEntries };
21713
21883
  } else if (isFloat32Array(entry)) {
21714
- const storage = encodeFloat32Storage(entry, id, float32Buffers, (id2) => ({
21715
- tag: "ref",
21716
- id: id2
21717
- }));
21884
+ const backing = float32Storage(entry);
21885
+ const storage = { kind: "float32array", ...encodeFloat32Layout(entry), buffer: child(backing.buffer, "<buffer>") };
21718
21886
  const properties = /* @__PURE__ */ Object.create(null);
21719
21887
  for (const [key, descriptor] of float32DataProperties(entry)) {
21720
21888
  properties[key] = {
@@ -21817,7 +21985,7 @@ function decodeReplayData(input, options = {}, parent) {
21817
21985
  const graph = record2(input);
21818
21986
  const nodes = list(own(graph, "nodes"));
21819
21987
  const restored = /* @__PURE__ */ new Map();
21820
- const initializeIterators = [];
21988
+ const initializeValues = [];
21821
21989
  const decode = (entry, depth = 0) => {
21822
21990
  if (depth > MAX_DATA_DEPTH) throw new TypeError("Replay data exceeds the nesting limit.");
21823
21991
  if (entry === null || typeof entry === "boolean" || typeof entry === "string") return entry;
@@ -21942,10 +22110,20 @@ function decodeReplayData(input, options = {}, parent) {
21942
22110
  if (node.extensible === false) Object.preventExtensions(result3);
21943
22111
  return result3;
21944
22112
  }
22113
+ if (kind === "arraybuffer") {
22114
+ if (typeof node.extensible !== "boolean") throw new TypeError("Invalid ArrayBuffer extensibility.");
22115
+ const result3 = decodeArrayBufferStorage(node, child);
22116
+ restored.set(id, result3);
22117
+ initializeValues.push(() => {
22118
+ defineProperties(result3, record2(own(node, "properties")), child, node.symbolEntries);
22119
+ if (!node.extensible) Object.preventExtensions(result3);
22120
+ });
22121
+ return result3;
22122
+ }
21945
22123
  if (kind === "float32array") {
21946
22124
  if (typeof node.extensible !== "boolean")
21947
22125
  throw new TypeError("Invalid Float32Array extensibility.");
21948
- const result3 = decodeFloat32Storage(node, child);
22126
+ const result3 = decodeFloat32Storage(node, child, compilation.owner?.budget);
21949
22127
  restored.set(id, result3);
21950
22128
  defineProperties(result3, record2(own(node, "properties")), child);
21951
22129
  if (!node.extensible) Object.preventExtensions(result3);
@@ -21981,7 +22159,7 @@ function decodeReplayData(input, options = {}, parent) {
21981
22159
  restored.set(id, result3);
21982
22160
  const collection = child(own(node, "collection"));
21983
22161
  if (collection !== void 0 && !isSandboxMap(collection) && !isSandboxSet(collection)) throw new TypeError("Invalid replay collection iterator source.");
21984
- initializeIterators.push(() => {
22162
+ initializeValues.push(() => {
21985
22163
  restoreSandboxCollectionIterator({ collection, collectionKind, method, index, exhausted }, result3);
21986
22164
  });
21987
22165
  defineProperties(result3, record2(own(node, "properties")), child);
@@ -22052,7 +22230,7 @@ function decodeReplayData(input, options = {}, parent) {
22052
22230
  return result2;
22053
22231
  };
22054
22232
  const result = decode(own(graph, "root"));
22055
- for (const initialize of initializeIterators) initialize();
22233
+ for (const initialize of initializeValues) initialize();
22056
22234
  if (parent !== void 0) compilation.forward(compilation.tickets, parent);
22057
22235
  return result;
22058
22236
  } finally {
@@ -22831,12 +23009,43 @@ function copyHostValueToSandbox(value, stackFrames, options, state, path) {
22831
23009
  state.seen.set(value, date);
22832
23010
  return date;
22833
23011
  }
23012
+ if (isSandboxArrayBuffer(value)) {
23013
+ const existing = state.seen.get(value);
23014
+ if (existing !== void 0) return existing;
23015
+ const length = arrayBufferLength(value);
23016
+ budget.allocateArrayLength(length);
23017
+ budget.provisionDataUsage(length + 1)();
23018
+ const copy = copyArrayBufferStorage(value, state);
23019
+ state.seen.set(value, copy);
23020
+ for (const [key, descriptor] of arrayBufferDataProperties(value)) {
23021
+ if (typeof key === "symbol") throw new TypeError("Host ArrayBuffer symbol properties require an explicit capability path.");
23022
+ Object.defineProperty(copy, key, {
23023
+ ...descriptor,
23024
+ value: copyHostValueToSandbox(
23025
+ descriptor.value,
23026
+ stackFrames,
23027
+ { ...options, capabilityPath: [...options.capabilityPath ?? [], key] },
23028
+ state,
23029
+ joinPath(path, key)
23030
+ )
23031
+ });
23032
+ }
23033
+ if (!Object.isExtensible(value)) Object.preventExtensions(copy);
23034
+ return copy;
23035
+ }
22834
23036
  if (isFloat32Array(value)) {
22835
23037
  const existing = state.seen.get(value);
22836
23038
  if (existing !== void 0) return existing;
22837
23039
  checkFloat32Allocation(Math.ceil(float32Storage(value).byteLength / 4), budget);
22838
23040
  const copy = copyFloat32Storage(value, state);
22839
23041
  state.seen.set(value, copy);
23042
+ copyHostValueToSandbox(
23043
+ float32Storage(value).buffer,
23044
+ stackFrames,
23045
+ { ...options, capabilityPath: [...options.capabilityPath ?? [], "buffer"] },
23046
+ state,
23047
+ `${path}.buffer`
23048
+ );
22840
23049
  for (const [key, descriptor] of float32DataProperties(value)) {
22841
23050
  Object.defineProperty(copy, key, {
22842
23051
  ...descriptor,
@@ -24862,6 +25071,128 @@ function createBigIntGlobal(budget) {
24862
25071
  return constructor;
24863
25072
  }
24864
25073
 
25074
+ // packages/safe-js/src/interp/globals/array-buffer.ts
25075
+ var resizeBuffer2 = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "resize").value;
25076
+ function createArrayBufferGlobal(budget) {
25077
+ const prototype = /* @__PURE__ */ Object.create(null);
25078
+ const constructor = createSandboxClosure({
25079
+ guest: true,
25080
+ sandbox: true,
25081
+ name: "ArrayBuffer",
25082
+ length: 1,
25083
+ call: () => {
25084
+ throw new TypeError("ArrayBuffer requires new.");
25085
+ },
25086
+ construct: async (args, context) => {
25087
+ let selected;
25088
+ let current;
25089
+ const bridge = {
25090
+ ...context,
25091
+ stack: context?.stack ?? [],
25092
+ thisValue: void 0,
25093
+ getProperty: context?.getProperty ?? ((value, key) => {
25094
+ const descriptor = getSandboxPropertyDescriptor(value, key, budget);
25095
+ return descriptor === void 0 ? getSandboxDataProperty(value, key, budget) : readPropertyDescriptor(descriptor, value, bridge);
25096
+ }),
25097
+ invokeClosure: context?.invokeClosure ?? ((callee, values, receiver, construct) => invokeBuiltinClosure(callee, values, budget, context, receiver, construct))
25098
+ };
25099
+ const release = retainValues(budget, () => [selected, current, ...args]);
25100
+ try {
25101
+ const number = await sandboxNumber(args[0], budget, bridge);
25102
+ const length = Number.isNaN(number) ? 0 : Math.trunc(number);
25103
+ if (length < 0 || !Number.isSafeInteger(length)) throw new RangeError("Invalid ArrayBuffer length.");
25104
+ let maxByteLength;
25105
+ if (args[1] !== null && typeof args[1] === "object") {
25106
+ current = await bridge.getProperty(args[1], "maxByteLength");
25107
+ if (current !== void 0) {
25108
+ const maximum = await sandboxNumber(current, budget, bridge);
25109
+ maxByteLength = Number.isNaN(maximum) ? 0 : Math.trunc(maximum);
25110
+ if (!Number.isSafeInteger(maxByteLength) || maxByteLength < length)
25111
+ throw new RangeError("Invalid ArrayBuffer maximum length.");
25112
+ }
25113
+ }
25114
+ const target = context?.newTarget ?? constructor;
25115
+ selected = await bridge.getProperty(target, "prototype");
25116
+ budget.allocateArrayLength(maxByteLength ?? length);
25117
+ budget.provisionDataUsage(length + 1)();
25118
+ const result = Reflect.construct(ArrayBuffer, [length, maxByteLength === void 0 ? void 0 : { maxByteLength }]);
25119
+ setSandboxPrototype(result, selected !== null && typeof selected === "object" ? selected : prototype, budget);
25120
+ return result;
25121
+ } finally {
25122
+ release();
25123
+ }
25124
+ }
25125
+ });
25126
+ Object.defineProperty(materializeFunctionProperties(constructor), "prototype", { value: prototype, writable: false });
25127
+ Object.defineProperty(materializeFunctionProperties(constructor), "isView", {
25128
+ writable: true,
25129
+ configurable: true,
25130
+ value: createSandboxClosure({
25131
+ guest: true,
25132
+ sandbox: true,
25133
+ name: "isView",
25134
+ length: 1,
25135
+ call: (args) => ArrayBuffer.isView(args[0])
25136
+ })
25137
+ });
25138
+ Object.defineProperties(prototype, {
25139
+ constructor: { value: constructor, writable: true, configurable: true },
25140
+ [Symbol.toStringTag]: { value: "ArrayBuffer", configurable: true }
25141
+ });
25142
+ const getters = [];
25143
+ Object.defineProperty(prototype, "resize", {
25144
+ writable: true,
25145
+ configurable: true,
25146
+ value: createSandboxClosure({
25147
+ guest: true,
25148
+ sandbox: true,
25149
+ name: "resize",
25150
+ length: 1,
25151
+ call: async (args, context) => {
25152
+ const receiver = context?.thisValue;
25153
+ if (!isSandboxArrayBuffer(receiver) || arrayBufferOptions(receiver) === void 0)
25154
+ throw new TypeError("ArrayBuffer resize requires a resizable buffer receiver.");
25155
+ const release = retainValues(budget, () => [receiver, ...args]);
25156
+ try {
25157
+ const number = await sandboxNumber(args[0], budget, context);
25158
+ const length = Number.isNaN(number) ? 0 : Math.trunc(number);
25159
+ if (!Number.isSafeInteger(length) || length < 0 || length > arrayBufferOptions(receiver).maxByteLength)
25160
+ throw new RangeError("Invalid ArrayBuffer resize length.");
25161
+ budget.allocateArrayLength(length);
25162
+ budget.provisionDataUsage(Math.max(length - arrayBufferLength(receiver), 0))();
25163
+ Reflect.apply(resizeBuffer2, receiver, [length]);
25164
+ return void 0;
25165
+ } finally {
25166
+ release();
25167
+ }
25168
+ }
25169
+ })
25170
+ });
25171
+ for (const key of ["byteLength", "maxByteLength", "resizable"]) {
25172
+ const getter = createSandboxClosure({
25173
+ guest: true,
25174
+ sandbox: true,
25175
+ name: `get ${key}`,
25176
+ length: 0,
25177
+ call: (_args, context) => {
25178
+ if (!isSandboxArrayBuffer(context?.thisValue)) throw new TypeError(`ArrayBuffer ${key} requires a buffer receiver.`);
25179
+ if (key === "byteLength") return arrayBufferLength(context.thisValue);
25180
+ const options = arrayBufferOptions(context.thisValue);
25181
+ return key === "resizable" ? options !== void 0 : options?.maxByteLength ?? arrayBufferLength(context.thisValue);
25182
+ }
25183
+ });
25184
+ Object.defineProperty(prototype, key, { get: accessorAdapter(getter, "get"), configurable: true });
25185
+ getters.push(getter);
25186
+ }
25187
+ setSandboxPrototype(prototype, getSandboxPrototype(/* @__PURE__ */ Object.create(null), budget));
25188
+ arrayBufferPrototypes.set(budget, prototype);
25189
+ registerBuiltinIdentities(budget, { ArrayBuffer: constructor });
25190
+ registerIntrinsicFunction(budget, constructor);
25191
+ for (const getter of getters) registerIntrinsicFunction(budget, getter);
25192
+ registerIntrinsicObject(budget, prototype);
25193
+ return constructor;
25194
+ }
25195
+
24865
25196
  // packages/safe-js/src/interp/globals.ts
24866
25197
  function createBuiltinBindings(options) {
24867
25198
  const bindings = {
@@ -24874,6 +25205,7 @@ function createBuiltinBindings(options) {
24874
25205
  ...createErrorGlobals({ ...options, errorPrototypes: options.errorPrototypes !== false }),
24875
25206
  ...createMathGlobals({ random: options.random, budget: options.budget }),
24876
25207
  ...createObjectArrayGlobals(options),
25208
+ ArrayBuffer: createArrayBufferGlobal(options.budget),
24877
25209
  ...createMiscGlobals(options),
24878
25210
  ...createUriGlobals(options.budget),
24879
25211
  ...createPromiseGlobals(options),
@@ -25610,6 +25942,11 @@ function validateDumpHeap(root, state) {
25610
25942
  validateDateRecord(entry, path);
25611
25943
  continue;
25612
25944
  }
25945
+ if (entry.kind === "arraybuffer") {
25946
+ validateArrayBufferStorage(entry);
25947
+ validateGuestHeapNode({ kind: "guest-object", state: entry.state }, heap, state.limits.maxEntries);
25948
+ continue;
25949
+ }
25613
25950
  if (entry.kind === "float32array") {
25614
25951
  validateFloat32Storage(entry);
25615
25952
  requireRecord(entry.entries, `${path}.entries`);
@@ -25833,7 +26170,7 @@ function validateHeapValue(value, path, state, heap) {
25833
26170
  fail("invalidValue", path, String(error));
25834
26171
  }
25835
26172
  validateErrorType(record4, path);
25836
- if (!["symbol", "arguments", "array", "object", "map", "set", "float32array", "date", "boxed", "collection-iterator", "regexp-iterator", "regex-object"].includes(String(record4.kind)))
26173
+ if (!["symbol", "arguments", "array", "object", "map", "set", "float32array", "arraybuffer", "date", "boxed", "collection-iterator", "regexp-iterator", "regex-object"].includes(String(record4.kind)))
25837
26174
  fail("unknownTag", `${path}.kind`, "unknown heap tag");
25838
26175
  validateValue(record4, path, 1, state);
25839
26176
  if (record4.kind === "symbol") validateSymbolRecord(record4, path, state);
@@ -25842,6 +26179,10 @@ function validateHeapValue(value, path, state, heap) {
25842
26179
  if (record4.kind === "object") requireRecord(record4.entries, `${path}.entries`);
25843
26180
  if (record4.kind === "boxed") validateBoxedRecord(record4, path, heap);
25844
26181
  if (record4.kind === "date") validateDateRecord(record4, path);
26182
+ if (record4.kind === "arraybuffer") {
26183
+ validateArrayBufferStorage(record4);
26184
+ validateGuestHeapNode({ kind: "guest-object", state: record4.state }, heap, state.limits.maxEntries);
26185
+ }
25845
26186
  if (record4.kind === "float32array") {
25846
26187
  validateFloat32Storage(record4);
25847
26188
  requireRecord(record4.entries, `${path}.entries`);
@@ -26038,7 +26379,7 @@ function validateGenericValue(value, path, depth, state) {
26038
26379
  if (typeof value === "object" && value !== null && hasGuestObjectState(value)) {
26039
26380
  fail("invalidState", path, "guest function properties, prototype links and custom descriptors cannot be restored");
26040
26381
  }
26041
- if (state.dataPropertiesOnly && types6.isProxy(value)) {
26382
+ if (state.dataPropertiesOnly && types7.isProxy(value)) {
26042
26383
  fail("invalidType", path, "proxy objects are not snapshot data");
26043
26384
  }
26044
26385
  if (depth > state.limits.maxDepth)
@@ -26837,12 +27178,10 @@ function measureSandboxData(values, options = {}) {
26837
27178
  }
26838
27179
  const prototype = getSandboxPrototype(value);
26839
27180
  if (prototype !== null) visit(prototype, depth + 1);
27181
+ if (isSandboxArrayBuffer(value)) usage += arrayBufferLength(value);
26840
27182
  if (isFloat32Array(value)) {
26841
27183
  const storage = float32Storage(value);
26842
- if (!seen.has(storage.buffer)) {
26843
- seen.add(storage.buffer);
26844
- usage += storage.byteLength;
26845
- }
27184
+ visit(storage.buffer, depth + 1);
26846
27185
  for (const [key, descriptor] of float32Properties(value)) {
26847
27186
  usage += 1;
26848
27187
  if (typeof key === "string") usage += key.length;
@@ -26981,7 +27320,7 @@ function measureSandboxData(values, options = {}) {
26981
27320
  const descriptor = Object.getOwnPropertyDescriptor(value, key);
26982
27321
  if (descriptor !== void 0) descriptors2.push([key, descriptor]);
26983
27322
  }
26984
- const includeNonEnumerable = isSandboxDate(value) || sandboxErrorTypes.has(value) || hasManagedDescriptors(value);
27323
+ const includeNonEnumerable = isSandboxDate(value) || isSandboxArrayBuffer(value) || sandboxErrorTypes.has(value) || hasManagedDescriptors(value);
26985
27324
  for (const [key, descriptor] of descriptors2) {
26986
27325
  if (!descriptor.enumerable && !includeNonEnumerable) continue;
26987
27326
  usage += 1 + key.length;
@@ -27177,6 +27516,20 @@ function copyToSandbox(value, state, path = "<root>", cloneSandboxCollections =
27177
27516
  }
27178
27517
  return sandboxPromise;
27179
27518
  }
27519
+ if (isSandboxArrayBuffer(value)) {
27520
+ const existing = state.seen.get(value);
27521
+ if (existing !== void 0) return existing;
27522
+ const copy = copyArrayBufferStorage(value, state);
27523
+ state.seen.set(value, copy);
27524
+ for (const [key, descriptor] of arrayBufferDataProperties(value)) {
27525
+ Object.defineProperty(copy, key, {
27526
+ ...descriptor,
27527
+ value: copyToSandbox(descriptor.value, state, joinPath2(path, key), cloneSandboxCollections, depth + 1)
27528
+ });
27529
+ }
27530
+ if (!Object.isExtensible(value)) Object.preventExtensions(copy);
27531
+ return copy;
27532
+ }
27180
27533
  if (nodeTypes.isDate(value)) {
27181
27534
  const existing = state.seen.get(value);
27182
27535
  if (existing !== void 0) return existing;
@@ -27195,6 +27548,7 @@ function copyToSandbox(value, state, path = "<root>", cloneSandboxCollections =
27195
27548
  if (existing !== void 0) return existing;
27196
27549
  const copy = copyFloat32Storage(value, state);
27197
27550
  state.seen.set(value, copy);
27551
+ copyToSandbox(float32Storage(value).buffer, state, `${path}.buffer`, cloneSandboxCollections, depth + 1);
27198
27552
  for (const [key, descriptor] of float32DataProperties(value)) {
27199
27553
  Object.defineProperty(copy, key, {
27200
27554
  ...descriptor,
@@ -27416,6 +27770,7 @@ function copyFromSandbox(value, state, path = "<root>", options, depth = 0) {
27416
27770
  if (existing !== void 0) return existing;
27417
27771
  const copy = copyFloat32Storage(value, state);
27418
27772
  state.seen.set(value, copy);
27773
+ copyFromSandbox(float32Storage(value).buffer, state, `${path}.buffer`, options, depth + 1);
27419
27774
  for (const [key, descriptor] of float32DataProperties(value)) {
27420
27775
  Object.defineProperty(copy, key, {
27421
27776
  ...descriptor,
@@ -27425,6 +27780,20 @@ function copyFromSandbox(value, state, path = "<root>", options, depth = 0) {
27425
27780
  if (!Object.isExtensible(value)) Object.preventExtensions(copy);
27426
27781
  return copy;
27427
27782
  }
27783
+ if (isSandboxArrayBuffer(value)) {
27784
+ const existing = state.seen.get(value);
27785
+ if (existing !== void 0) return existing;
27786
+ const copy = copyArrayBufferStorage(value, state);
27787
+ state.seen.set(value, copy);
27788
+ for (const [key, descriptor] of arrayBufferDataProperties(value)) {
27789
+ Object.defineProperty(copy, key, {
27790
+ ...descriptor,
27791
+ value: copyFromSandbox(descriptor.value, state, joinPath2(path, key), options, depth + 1)
27792
+ });
27793
+ }
27794
+ if (!Object.isExtensible(value)) Object.preventExtensions(copy);
27795
+ return copy;
27796
+ }
27428
27797
  if (isSandboxDate(value)) {
27429
27798
  const existing = state.seen.get(value);
27430
27799
  if (existing !== void 0) return existing;
@@ -27586,6 +27955,16 @@ function allocateSandboxValue2(value, budget, seen) {
27586
27955
  budget.allocateString(value);
27587
27956
  return;
27588
27957
  }
27958
+ if (isSandboxArrayBuffer(value)) {
27959
+ if (seen.has(value)) return;
27960
+ seen.add(value);
27961
+ budget.allocateArrayLength(arrayBufferLength(value));
27962
+ for (const [key, descriptor] of arrayBufferDataProperties(value)) {
27963
+ if (typeof key === "string") budget.allocateString(key);
27964
+ allocateSandboxValue2(descriptor.value, budget, seen);
27965
+ }
27966
+ return;
27967
+ }
27589
27968
  if (isSandboxBox(value)) {
27590
27969
  if (seen.has(value)) return;
27591
27970
  seen.add(value);
@@ -28331,8 +28710,29 @@ function normalize(value, seen) {
28331
28710
  try {
28332
28711
  const date = copyNativeDate(value);
28333
28712
  if (date !== void 0) return Object.assign(/* @__PURE__ */ Object.create(null), { $type: "date", time: serializedDateTime(date) });
28713
+ if (isSandboxArrayBuffer(value)) {
28714
+ arrayBufferLength(value);
28715
+ const properties = /* @__PURE__ */ Object.create(null);
28716
+ const entries = [];
28717
+ for (const [key, descriptor] of arrayBufferDataProperties(value)) {
28718
+ if (typeof key === "symbol") throw new TypeError("ArrayBuffer symbol properties require an explicit host-call identity.");
28719
+ entries.push([key, descriptor]);
28720
+ }
28721
+ for (const [key, descriptor] of entries.sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0))
28722
+ defineOwnDataProperty(properties, key, normalize(descriptor.value, seen));
28723
+ return Object.assign(/* @__PURE__ */ Object.create(null), {
28724
+ $type: "arraybuffer",
28725
+ bytes: normalize(Array.from(new Uint8Array(value)), seen),
28726
+ ...arrayBufferOptions(value),
28727
+ properties
28728
+ });
28729
+ }
28334
28730
  if (isFloat32Array(value)) {
28335
28731
  const storage = float32Storage(value);
28732
+ const resizable = arrayBufferOptions(storage.buffer) !== void 0;
28733
+ const layout = resizable ? float32ViewLayouts.get(value) : void 0;
28734
+ if (resizable && layout === void 0)
28735
+ throw new TypeError("Resizable Float32Array host-call identity requires known view layout.");
28336
28736
  const properties = /* @__PURE__ */ Object.create(null);
28337
28737
  for (const [key, descriptor] of float32DataProperties(value).sort(
28338
28738
  ([left], [right]) => left < right ? -1 : left > right ? 1 : 0
@@ -28344,7 +28744,12 @@ function normalize(value, seen) {
28344
28744
  bytes: normalize(Array.from(new Uint8Array(storage.buffer)), seen),
28345
28745
  byteOffset: storage.byteOffset,
28346
28746
  length: storage.length,
28347
- properties
28747
+ properties,
28748
+ ...resizable || Reflect.ownKeys(storage.buffer).length > 0 ? { buffer: normalize(storage.buffer, seen) } : {},
28749
+ ...layout === void 0 ? {} : { viewLayout: {
28750
+ byteOffset: layout.byteOffset,
28751
+ length: layout.length ?? null
28752
+ } }
28348
28753
  });
28349
28754
  }
28350
28755
  if (Array.isArray(value)) {
@@ -29296,6 +29701,7 @@ function classDefinitionContains(node, expression) {
29296
29701
  var KNOWN_RUNTIME_GLOBALS = [
29297
29702
  "AggregateError",
29298
29703
  "Array",
29704
+ "ArrayBuffer",
29299
29705
  "Boolean",
29300
29706
  "BigInt",
29301
29707
  "Date",
@@ -39688,7 +40094,7 @@ function createReplayableRandom(options = {}) {
39688
40094
 
39689
40095
  // packages/safe-js/src/realm.ts
39690
40096
  import { AsyncLocalStorage as AsyncLocalStorage6 } from "node:async_hooks";
39691
- import { types as types7 } from "node:util";
40097
+ import { types as types8 } from "node:util";
39692
40098
 
39693
40099
  // packages/safe-js/src/modules/registry.ts
39694
40100
  function createUnknownModuleMessage2(moduleName, moduleNames) {
@@ -39827,7 +40233,7 @@ var RealmState = class {
39827
40233
  throw new TypeError("Realm limits must be positive safe integers with supported names.");
39828
40234
  this.limits[name] = Number(value);
39829
40235
  }
39830
- if (options.extensions !== void 0 && (!Array.isArray(options.extensions) || types7.isProxy(options.extensions)))
40236
+ if (options.extensions !== void 0 && (!Array.isArray(options.extensions) || types8.isProxy(options.extensions)))
39831
40237
  throw new TypeError("Extensions must be a registration array.");
39832
40238
  const registrations = options.extensions ?? [];
39833
40239
  const extensions = [];
@@ -40062,7 +40468,7 @@ var RealmState = class {
40062
40468
  return this.phase.run(phase, () => {
40063
40469
  try {
40064
40470
  const result = call();
40065
- if (types7.isPromise(result) || phase.pending.size > 0 || phase.failure !== void 0) {
40471
+ if (types8.isPromise(result) || phase.pending.size > 0 || phase.failure !== void 0) {
40066
40472
  return Promise.resolve(result).then(
40067
40473
  async (value) => {
40068
40474
  await Promise.allSettled(phase.pending);
@@ -40131,7 +40537,7 @@ var RealmState = class {
40131
40537
  },
40132
40538
  read: (operation, validate) => {
40133
40539
  const value = this.invokeHost(operation, operation);
40134
- if (types7.isPromise(value)) {
40540
+ if (types8.isPromise(value)) {
40135
40541
  void Promise.resolve(value).catch(() => void 0);
40136
40542
  throw new TypeError("Live property operations must be synchronous.");
40137
40543
  }
@@ -40139,7 +40545,7 @@ var RealmState = class {
40139
40545
  },
40140
40546
  write: (operation, value) => {
40141
40547
  const result = this.invokeHost(operation, () => operation(this.exportValue(value)));
40142
- if (types7.isPromise(result)) {
40548
+ if (types8.isPromise(result)) {
40143
40549
  void Promise.resolve(result).catch(() => void 0);
40144
40550
  throw new TypeError("Live property setters must be synchronous.");
40145
40551
  }
@@ -40348,7 +40754,7 @@ var RealmState = class {
40348
40754
  }
40349
40755
  });
40350
40756
  const output = getExtensionSetup(extension)(context);
40351
- if (types7.isPromise(output)) {
40757
+ if (types8.isPromise(output)) {
40352
40758
  void Promise.resolve(output).catch(() => void 0);
40353
40759
  throw new TypeError("Extension setup must be synchronous.");
40354
40760
  }
@@ -40540,7 +40946,7 @@ var RealmState = class {
40540
40946
  };
40541
40947
  function readModules(input) {
40542
40948
  const entries = (value, label) => {
40543
- if (types7.isMap(value) && !types7.isProxy(value)) {
40949
+ if (types8.isMap(value) && !types8.isProxy(value)) {
40544
40950
  const result = [...Map.prototype.entries.call(value)];
40545
40951
  if (result.length > 4096 || result.some(([key]) => typeof key !== "string" || key.length === 0))
40546
40952
  throw new TypeError(`${label} requires bounded string keys.`);
@@ -41809,4 +42215,4 @@ export {
41809
42215
  FileSnapshotBackend,
41810
42216
  run
41811
42217
  };
41812
- //# sourceMappingURL=chunk-HUG5DIKM.js.map
42218
+ //# sourceMappingURL=chunk-C26TRM5L.js.map