@poe-platform/safe-js 0.1.314 → 0.1.316

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]);
@@ -14809,7 +14893,8 @@ function createFloat32ArrayPrototypes(budget, constructor) {
14809
14893
  if (args[0] !== void 0) separator = await sandboxString(args[0], budget, context);
14810
14894
  for (let index = 0; index < length; index++) {
14811
14895
  budget.visitNode();
14812
- text = budget.allocateString(text + (index === 0 ? "" : separator) + String(receiver[index]));
14896
+ const value = receiver[index];
14897
+ text = budget.allocateString(text + (index === 0 ? "" : separator) + (value === void 0 ? "" : String(value)));
14813
14898
  }
14814
14899
  return text;
14815
14900
  } finally {
@@ -14836,6 +14921,7 @@ function createFloat32ArrayPrototypes(budget, constructor) {
14836
14921
  length: 0,
14837
14922
  call: (args, context) => {
14838
14923
  if (!isFloat32Array(context?.thisValue)) throw new TypeError(`TypedArray ${key} requires a typed array receiver.`);
14924
+ float32Storage(context.thisValue, true);
14839
14925
  return invokeBuiltinClosure(method, args, budget, context, context.thisValue);
14840
14926
  }
14841
14927
  });
@@ -14876,7 +14962,7 @@ function getFloat32Member(value, property, budget) {
14876
14962
  const receiver = context?.thisValue;
14877
14963
  if (!isFloat32Array(receiver))
14878
14964
  throw new TypeError(`Float32Array#${key} requires a Float32Array receiver.`);
14879
- const storage2 = float32Storage(receiver);
14965
+ const storage2 = float32Storage(receiver, key === "slice");
14880
14966
  const bridge = {
14881
14967
  ...context,
14882
14968
  stack: context?.stack ?? [],
@@ -14897,16 +14983,17 @@ function getFloat32Member(value, property, budget) {
14897
14983
  const number = await sandboxNumber(offsetValue, budget, bridge);
14898
14984
  const offset = Number.isNaN(number) ? 0 : Math.trunc(number);
14899
14985
  if (offset < 0) throw new RangeError("Float32Array#set offset is out of bounds.");
14986
+ const targetStorage = float32Storage(receiver, true);
14900
14987
  if (source === null || source === void 0) throw new TypeError("Float32Array#set requires a non-null source.");
14901
14988
  let length;
14902
- if (isFloat32Array(source)) length = float32Storage(source).length;
14989
+ if (isFloat32Array(source)) length = float32Storage(source, true).length;
14903
14990
  else {
14904
14991
  sourceObject = typeof source === "object" ? source : createSandboxBox(source);
14905
14992
  current = await bridge.getProperty(sourceObject, "length");
14906
14993
  const size = await sandboxNumber(current, budget, bridge);
14907
14994
  length = Number.isNaN(size) || size <= 0 ? 0 : Math.min(Math.trunc(size), Number.MAX_SAFE_INTEGER);
14908
14995
  }
14909
- if (offset + length > storage2.length) throw new RangeError("Float32Array#set source is out of bounds.");
14996
+ if (offset + length > targetStorage.length) throw new RangeError("Float32Array#set source is out of bounds.");
14910
14997
  if (isFloat32Array(source)) Float32Array.prototype.set.call(receiver, source, offset);
14911
14998
  else for (let index = 0; index < length; index++) {
14912
14999
  budget.visitNode();
@@ -14925,8 +15012,15 @@ function getFloat32Member(value, property, budget) {
14925
15012
  const start = relativeIndex(await sandboxNumber(args[0], budget, bridge), storage2.length);
14926
15013
  const end = args[1] === void 0 ? storage2.length : relativeIndex(await sandboxNumber(args[1], budget, bridge), storage2.length);
14927
15014
  const length = Math.max(end - start, 0);
14928
- if (key === "subarray")
14929
- return new Float32Array(storage2.buffer, storage2.byteOffset + start * 4, length);
15015
+ if (key === "subarray") {
15016
+ const layout = float32ViewLayouts.get(receiver);
15017
+ const offset = (layout?.byteOffset ?? storage2.byteOffset) + start * 4;
15018
+ const tracking = layout !== void 0 && layout.length === void 0 && args[1] === void 0;
15019
+ const result2 = new Float32Array(storage2.buffer, offset, tracking ? void 0 : length);
15020
+ if (arrayBufferOptions(storage2.buffer) !== void 0)
15021
+ float32ViewLayouts.set(result2, { byteOffset: offset, ...tracking ? {} : { length } });
15022
+ return result2;
15023
+ }
14930
15024
  checkFloat32Allocation(length, budget);
14931
15025
  const result = new Float32Array(length);
14932
15026
  new Uint8Array(result.buffer).set(
@@ -21046,18 +21140,20 @@ function serializeArray(value, serializeValue) {
21046
21140
  }
21047
21141
 
21048
21142
  // packages/safe-js/src/snapshot/float32array.ts
21049
- function encodeFloat32Storage(value, id, buffers, reference) {
21143
+ function encodeFloat32Layout(value) {
21050
21144
  const storage = float32Storage(value);
21051
- const existing = buffers.get(storage.buffer);
21052
- if (existing === void 0) buffers.set(storage.buffer, id);
21145
+ if (arrayBufferOptions(storage.buffer) === void 0) return { byteOffset: storage.byteOffset, length: storage.length };
21146
+ const layout = float32ViewLayouts.get(value);
21147
+ if (layout === void 0) throw new TypeError("Resizable Float32Array snapshots require known view layout.");
21053
21148
  return {
21054
- kind: "float32array",
21055
- byteOffset: storage.byteOffset,
21056
- length: storage.length,
21057
- ...existing === void 0 ? { bytes: Array.from(new Uint8Array(storage.buffer)) } : { buffer: reference(existing) }
21149
+ byteOffset: layout.byteOffset,
21150
+ length: layout.length ?? 0,
21151
+ ...layout.length === void 0 ? { lengthTracking: true } : {}
21058
21152
  };
21059
21153
  }
21060
21154
  function validateFloat32Storage(value) {
21155
+ if (Object.hasOwn(value, "lengthTracking") && (value.lengthTracking !== true || value.length !== 0 || Object.hasOwn(value, "bytes")))
21156
+ throw new TypeError("Invalid Float32Array length-tracking layout.");
21061
21157
  if (!Number.isSafeInteger(value.length) || Number(value.length) < 0 || !Number.isSafeInteger(value.byteOffset) || Number(value.byteOffset) < 0 || Number(value.byteOffset) % 4 !== 0)
21062
21158
  throw new TypeError("Invalid Float32Array view dimensions.");
21063
21159
  if (Object.hasOwn(value, "bytes") === Object.hasOwn(value, "buffer"))
@@ -21067,7 +21163,7 @@ function validateFloat32Storage(value) {
21067
21163
  throw new TypeError("Invalid Float32Array backing bytes.");
21068
21164
  }
21069
21165
  }
21070
- function decodeFloat32Storage(value, resolve) {
21166
+ function decodeFloat32Storage(value, resolve, budget) {
21071
21167
  validateFloat32Storage(value);
21072
21168
  let buffer;
21073
21169
  if (Array.isArray(value.bytes)) {
@@ -21075,10 +21171,50 @@ function decodeFloat32Storage(value, resolve) {
21075
21171
  new Uint8Array(buffer).set(value.bytes);
21076
21172
  } else {
21077
21173
  const referenced = resolve(value.buffer);
21078
- if (!isFloat32Array(referenced)) throw new TypeError("Invalid Float32Array backing reference.");
21079
- buffer = float32Storage(referenced).buffer;
21174
+ if (isSandboxArrayBuffer(referenced)) buffer = referenced;
21175
+ else if (isFloat32Array(referenced)) buffer = float32Storage(referenced).buffer;
21176
+ else throw new TypeError("Invalid Float32Array backing reference.");
21080
21177
  }
21081
- return new Float32Array(buffer, Number(value.byteOffset), Number(value.length));
21178
+ if (value.lengthTracking === true && arrayBufferOptions(buffer) === void 0)
21179
+ throw new TypeError("Length-tracking layout requires resizable backing storage.");
21180
+ return restoreFloat32View(buffer, Number(value.byteOffset), value.lengthTracking === true ? void 0 : Number(value.length), budget);
21181
+ }
21182
+
21183
+ // packages/safe-js/src/snapshot/array-buffer.ts
21184
+ function captureArrayBufferState(value, encode) {
21185
+ return {
21186
+ properties: serializePropertyDescriptors(value, encode),
21187
+ ...hasExplicitSandboxPrototype(value) ? { prototype: encode(getSandboxPrototype(value)) } : {}
21188
+ };
21189
+ }
21190
+ function encodeArrayBufferStorage(value, id, buffers, reference) {
21191
+ arrayBufferLength(value);
21192
+ const existing = buffers.get(value);
21193
+ if (existing === void 0) buffers.set(value, id);
21194
+ return { kind: "arraybuffer", ...existing === void 0 ? { bytes: Array.from(new Uint8Array(value)), ...arrayBufferOptions(value) } : { buffer: reference(existing) } };
21195
+ }
21196
+ function validateArrayBufferStorage(value) {
21197
+ if (Object.hasOwn(value, "bytes") === Object.hasOwn(value, "buffer"))
21198
+ throw new TypeError("ArrayBuffer requires one backing storage description.");
21199
+ if (Object.hasOwn(value, "bytes") && (!Array.isArray(value.bytes) || value.bytes.some((byte) => !Number.isInteger(byte) || byte < 0 || byte > 255)))
21200
+ throw new TypeError("Invalid ArrayBuffer bytes.");
21201
+ if (Object.hasOwn(value, "maxByteLength") && (!Array.isArray(value.bytes) || !Number.isSafeInteger(value.maxByteLength) || Number(value.maxByteLength) < value.bytes.length))
21202
+ throw new TypeError("Invalid ArrayBuffer maximum length.");
21203
+ }
21204
+ function decodeArrayBufferStorage(value, resolve) {
21205
+ validateArrayBufferStorage(value);
21206
+ if (Array.isArray(value.bytes)) {
21207
+ const buffer = Reflect.construct(ArrayBuffer, [
21208
+ value.bytes.length,
21209
+ Object.hasOwn(value, "maxByteLength") ? { maxByteLength: value.maxByteLength } : void 0
21210
+ ]);
21211
+ new Uint8Array(buffer).set(value.bytes);
21212
+ return buffer;
21213
+ }
21214
+ const referenced = resolve(value.buffer);
21215
+ if (isSandboxArrayBuffer(referenced)) return referenced;
21216
+ if (isFloat32Array(referenced)) return float32Storage(referenced).buffer;
21217
+ throw new TypeError("Invalid ArrayBuffer backing reference.");
21082
21218
  }
21083
21219
 
21084
21220
  // packages/safe-js/src/snapshot/date-properties.ts
@@ -21209,7 +21345,7 @@ function serializeDumpValue(value, path, state) {
21209
21345
  if (hasGuestObjectState(value) && !(state.trustedRunReplay && (isSandboxDate(value) || isSandboxPromise(value) || isPromiseResolvingFunction(value)))) {
21210
21346
  throw new TypeError("Guest function properties and prototype links cannot be serialized.");
21211
21347
  }
21212
- if (isSandboxRegExpIterator(value) || isSandboxRegex(value) && hasCustomRegexProperties(value) || isSandboxBox(value) || isSandboxDate(value) || isFloat32Array(value)) return serializeHeapReference(value, path, state);
21348
+ if (isSandboxArrayBuffer(value) || isSandboxRegExpIterator(value) || isSandboxRegex(value) && hasCustomRegexProperties(value) || isSandboxBox(value) || isSandboxDate(value) || isFloat32Array(value)) return serializeHeapReference(value, path, state);
21213
21349
  if (Array.isArray(value)) {
21214
21350
  const reference2 = serializeHeapReference(value, path, state);
21215
21351
  if (reference2 !== void 0) {
@@ -21281,11 +21417,21 @@ function serializeHeapReference(value, path, state) {
21281
21417
  const serialized = serializeDumpValue(entry, `${path}.<date-property>`, state);
21282
21418
  return serialized === SKIP_VALUE ? { kind: "undefined" } : serialized;
21283
21419
  });
21420
+ } else if (isSandboxArrayBuffer(value)) {
21421
+ state.heap[String(id)] = {
21422
+ ...encodeArrayBufferStorage(value, id, state.float32Buffers, (id2) => ({ kind: "ref", id: id2 })),
21423
+ state: captureArrayBufferState(value, (entry) => {
21424
+ const serialized = serializeDumpValue(entry, `${path}.<buffer>`, state);
21425
+ return serialized === SKIP_VALUE ? { kind: "undefined" } : serialized;
21426
+ })
21427
+ };
21284
21428
  } else if (isFloat32Array(value)) {
21285
- const storage = encodeFloat32Storage(value, id, state.float32Buffers, (id2) => ({
21286
- kind: "ref",
21287
- id: id2
21288
- }));
21429
+ const backing = float32Storage(value);
21430
+ const storage = {
21431
+ kind: "float32array",
21432
+ ...encodeFloat32Layout(value),
21433
+ buffer: serializeHeapReference(backing.buffer, `${path}.buffer`, state)
21434
+ };
21289
21435
  const entries = /* @__PURE__ */ Object.create(null);
21290
21436
  state.heap[String(id)] = { ...storage, entries };
21291
21437
  for (const [key, descriptor] of float32DataProperties(value)) {
@@ -21351,7 +21497,7 @@ function indexHeapContainers(snapshot) {
21351
21497
  const heapIds = /* @__PURE__ */ new Map();
21352
21498
  let nextId = 1;
21353
21499
  for (const [value, stat2] of stats.entries()) {
21354
- 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)) {
21500
+ 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)) {
21355
21501
  heapIds.set(value, nextId);
21356
21502
  nextId += 1;
21357
21503
  }
@@ -21383,12 +21529,16 @@ function collectContainerStats(value, stats, ancestors, guestValues, depth = 0)
21383
21529
  stat2.expanded = true;
21384
21530
  ancestors.add(value);
21385
21531
  const guestEntries = [];
21386
- const guest = captureGuestHeapNode(value, (entry) => {
21532
+ if (isFloat32Array(value)) guestEntries.push(float32Storage(value).buffer);
21533
+ const guest = isSandboxArrayBuffer(value) ? { kind: "arraybuffer", state: captureArrayBufferState(value, (entry) => {
21534
+ guestEntries.push(entry);
21535
+ return null;
21536
+ }) } : captureGuestHeapNode(value, (entry) => {
21387
21537
  guestEntries.push(entry);
21388
21538
  return null;
21389
21539
  });
21390
21540
  if (guest !== void 0) {
21391
- guestValues.add(value);
21541
+ if (!isSandboxArrayBuffer(value)) guestValues.add(value);
21392
21542
  for (const entry of guestEntries) {
21393
21543
  collectContainerStats(entry, stats, ancestors, guestValues, depth + 1);
21394
21544
  if (entry !== null && typeof entry === "object") stats.get(entry).forceHeap = true;
@@ -21396,6 +21546,7 @@ function collectContainerStats(value, stats, ancestors, guestValues, depth = 0)
21396
21546
  ancestors.delete(value);
21397
21547
  return;
21398
21548
  }
21549
+ for (const entry of guestEntries) collectContainerStats(entry, stats, ancestors, guestValues, depth + 1);
21399
21550
  if (!Array.isArray(value) && !isPlainObject(value) && !isFloat32Array(value) && !isSandboxDate(value)) {
21400
21551
  ancestors.delete(value);
21401
21552
  return;
@@ -21715,11 +21866,24 @@ function encodeReplayData(value, options = {}) {
21715
21866
  ...symbolProperties.length === 0 ? {} : { symbolProperties },
21716
21867
  ...Object.isExtensible(entry) ? {} : { extensible: false }
21717
21868
  };
21869
+ } else if (isSandboxArrayBuffer(entry)) {
21870
+ const storage = encodeArrayBufferStorage(entry, id, float32Buffers, (id2) => ({ tag: "ref", id: id2 }));
21871
+ const properties = /* @__PURE__ */ Object.create(null);
21872
+ for (const [key, descriptor] of arrayBufferDataProperties(entry)) {
21873
+ if (typeof key === "symbol") continue;
21874
+ properties[key] = {
21875
+ value: child(descriptor.value, key),
21876
+ configurable: descriptor.configurable === true,
21877
+ enumerable: descriptor.enumerable === true,
21878
+ writable: descriptor.writable === true
21879
+ };
21880
+ }
21881
+ let symbolIndex = 0;
21882
+ const symbolEntries = serializeSymbolProperties(entry, (value2) => encode(value2, depth + 1, [...path, { symbol: Math.floor(symbolIndex++ / 2) }]));
21883
+ nodes[id] = { ...storage, properties, extensible: Object.isExtensible(entry), symbolEntries };
21718
21884
  } else if (isFloat32Array(entry)) {
21719
- const storage = encodeFloat32Storage(entry, id, float32Buffers, (id2) => ({
21720
- tag: "ref",
21721
- id: id2
21722
- }));
21885
+ const backing = float32Storage(entry);
21886
+ const storage = { kind: "float32array", ...encodeFloat32Layout(entry), buffer: child(backing.buffer, "<buffer>") };
21723
21887
  const properties = /* @__PURE__ */ Object.create(null);
21724
21888
  for (const [key, descriptor] of float32DataProperties(entry)) {
21725
21889
  properties[key] = {
@@ -21822,7 +21986,7 @@ function decodeReplayData(input, options = {}, parent) {
21822
21986
  const graph = record2(input);
21823
21987
  const nodes = list(own(graph, "nodes"));
21824
21988
  const restored = /* @__PURE__ */ new Map();
21825
- const initializeIterators = [];
21989
+ const initializeValues = [];
21826
21990
  const decode = (entry, depth = 0) => {
21827
21991
  if (depth > MAX_DATA_DEPTH) throw new TypeError("Replay data exceeds the nesting limit.");
21828
21992
  if (entry === null || typeof entry === "boolean" || typeof entry === "string") return entry;
@@ -21947,10 +22111,20 @@ function decodeReplayData(input, options = {}, parent) {
21947
22111
  if (node.extensible === false) Object.preventExtensions(result3);
21948
22112
  return result3;
21949
22113
  }
22114
+ if (kind === "arraybuffer") {
22115
+ if (typeof node.extensible !== "boolean") throw new TypeError("Invalid ArrayBuffer extensibility.");
22116
+ const result3 = decodeArrayBufferStorage(node, child);
22117
+ restored.set(id, result3);
22118
+ initializeValues.push(() => {
22119
+ defineProperties(result3, record2(own(node, "properties")), child, node.symbolEntries);
22120
+ if (!node.extensible) Object.preventExtensions(result3);
22121
+ });
22122
+ return result3;
22123
+ }
21950
22124
  if (kind === "float32array") {
21951
22125
  if (typeof node.extensible !== "boolean")
21952
22126
  throw new TypeError("Invalid Float32Array extensibility.");
21953
- const result3 = decodeFloat32Storage(node, child);
22127
+ const result3 = decodeFloat32Storage(node, child, compilation.owner?.budget);
21954
22128
  restored.set(id, result3);
21955
22129
  defineProperties(result3, record2(own(node, "properties")), child);
21956
22130
  if (!node.extensible) Object.preventExtensions(result3);
@@ -21986,7 +22160,7 @@ function decodeReplayData(input, options = {}, parent) {
21986
22160
  restored.set(id, result3);
21987
22161
  const collection = child(own(node, "collection"));
21988
22162
  if (collection !== void 0 && !isSandboxMap(collection) && !isSandboxSet(collection)) throw new TypeError("Invalid replay collection iterator source.");
21989
- initializeIterators.push(() => {
22163
+ initializeValues.push(() => {
21990
22164
  restoreSandboxCollectionIterator({ collection, collectionKind, method, index, exhausted }, result3);
21991
22165
  });
21992
22166
  defineProperties(result3, record2(own(node, "properties")), child);
@@ -22057,7 +22231,7 @@ function decodeReplayData(input, options = {}, parent) {
22057
22231
  return result2;
22058
22232
  };
22059
22233
  const result = decode(own(graph, "root"));
22060
- for (const initialize of initializeIterators) initialize();
22234
+ for (const initialize of initializeValues) initialize();
22061
22235
  if (parent !== void 0) compilation.forward(compilation.tickets, parent);
22062
22236
  return result;
22063
22237
  } finally {
@@ -22836,12 +23010,43 @@ function copyHostValueToSandbox(value, stackFrames, options, state, path) {
22836
23010
  state.seen.set(value, date);
22837
23011
  return date;
22838
23012
  }
23013
+ if (isSandboxArrayBuffer(value)) {
23014
+ const existing = state.seen.get(value);
23015
+ if (existing !== void 0) return existing;
23016
+ const length = arrayBufferLength(value);
23017
+ budget.allocateArrayLength(length);
23018
+ budget.provisionDataUsage(length + 1)();
23019
+ const copy = copyArrayBufferStorage(value, state);
23020
+ state.seen.set(value, copy);
23021
+ for (const [key, descriptor] of arrayBufferDataProperties(value)) {
23022
+ if (typeof key === "symbol") throw new TypeError("Host ArrayBuffer symbol properties require an explicit capability path.");
23023
+ Object.defineProperty(copy, key, {
23024
+ ...descriptor,
23025
+ value: copyHostValueToSandbox(
23026
+ descriptor.value,
23027
+ stackFrames,
23028
+ { ...options, capabilityPath: [...options.capabilityPath ?? [], key] },
23029
+ state,
23030
+ joinPath(path, key)
23031
+ )
23032
+ });
23033
+ }
23034
+ if (!Object.isExtensible(value)) Object.preventExtensions(copy);
23035
+ return copy;
23036
+ }
22839
23037
  if (isFloat32Array(value)) {
22840
23038
  const existing = state.seen.get(value);
22841
23039
  if (existing !== void 0) return existing;
22842
23040
  checkFloat32Allocation(Math.ceil(float32Storage(value).byteLength / 4), budget);
22843
23041
  const copy = copyFloat32Storage(value, state);
22844
23042
  state.seen.set(value, copy);
23043
+ copyHostValueToSandbox(
23044
+ float32Storage(value).buffer,
23045
+ stackFrames,
23046
+ { ...options, capabilityPath: [...options.capabilityPath ?? [], "buffer"] },
23047
+ state,
23048
+ `${path}.buffer`
23049
+ );
22845
23050
  for (const [key, descriptor] of float32DataProperties(value)) {
22846
23051
  Object.defineProperty(copy, key, {
22847
23052
  ...descriptor,
@@ -24867,6 +25072,128 @@ function createBigIntGlobal(budget) {
24867
25072
  return constructor;
24868
25073
  }
24869
25074
 
25075
+ // packages/safe-js/src/interp/globals/array-buffer.ts
25076
+ var resizeBuffer2 = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "resize").value;
25077
+ function createArrayBufferGlobal(budget) {
25078
+ const prototype = /* @__PURE__ */ Object.create(null);
25079
+ const constructor = createSandboxClosure({
25080
+ guest: true,
25081
+ sandbox: true,
25082
+ name: "ArrayBuffer",
25083
+ length: 1,
25084
+ call: () => {
25085
+ throw new TypeError("ArrayBuffer requires new.");
25086
+ },
25087
+ construct: async (args, context) => {
25088
+ let selected;
25089
+ let current;
25090
+ const bridge = {
25091
+ ...context,
25092
+ stack: context?.stack ?? [],
25093
+ thisValue: void 0,
25094
+ getProperty: context?.getProperty ?? ((value, key) => {
25095
+ const descriptor = getSandboxPropertyDescriptor(value, key, budget);
25096
+ return descriptor === void 0 ? getSandboxDataProperty(value, key, budget) : readPropertyDescriptor(descriptor, value, bridge);
25097
+ }),
25098
+ invokeClosure: context?.invokeClosure ?? ((callee, values, receiver, construct) => invokeBuiltinClosure(callee, values, budget, context, receiver, construct))
25099
+ };
25100
+ const release = retainValues(budget, () => [selected, current, ...args]);
25101
+ try {
25102
+ const number = await sandboxNumber(args[0], budget, bridge);
25103
+ const length = Number.isNaN(number) ? 0 : Math.trunc(number);
25104
+ if (length < 0 || !Number.isSafeInteger(length)) throw new RangeError("Invalid ArrayBuffer length.");
25105
+ let maxByteLength;
25106
+ if (args[1] !== null && typeof args[1] === "object") {
25107
+ current = await bridge.getProperty(args[1], "maxByteLength");
25108
+ if (current !== void 0) {
25109
+ const maximum = await sandboxNumber(current, budget, bridge);
25110
+ maxByteLength = Number.isNaN(maximum) ? 0 : Math.trunc(maximum);
25111
+ if (!Number.isSafeInteger(maxByteLength) || maxByteLength < length)
25112
+ throw new RangeError("Invalid ArrayBuffer maximum length.");
25113
+ }
25114
+ }
25115
+ const target = context?.newTarget ?? constructor;
25116
+ selected = await bridge.getProperty(target, "prototype");
25117
+ budget.allocateArrayLength(maxByteLength ?? length);
25118
+ budget.provisionDataUsage(length + 1)();
25119
+ const result = Reflect.construct(ArrayBuffer, [length, maxByteLength === void 0 ? void 0 : { maxByteLength }]);
25120
+ setSandboxPrototype(result, selected !== null && typeof selected === "object" ? selected : prototype, budget);
25121
+ return result;
25122
+ } finally {
25123
+ release();
25124
+ }
25125
+ }
25126
+ });
25127
+ Object.defineProperty(materializeFunctionProperties(constructor), "prototype", { value: prototype, writable: false });
25128
+ Object.defineProperty(materializeFunctionProperties(constructor), "isView", {
25129
+ writable: true,
25130
+ configurable: true,
25131
+ value: createSandboxClosure({
25132
+ guest: true,
25133
+ sandbox: true,
25134
+ name: "isView",
25135
+ length: 1,
25136
+ call: (args) => ArrayBuffer.isView(args[0])
25137
+ })
25138
+ });
25139
+ Object.defineProperties(prototype, {
25140
+ constructor: { value: constructor, writable: true, configurable: true },
25141
+ [Symbol.toStringTag]: { value: "ArrayBuffer", configurable: true }
25142
+ });
25143
+ const getters = [];
25144
+ Object.defineProperty(prototype, "resize", {
25145
+ writable: true,
25146
+ configurable: true,
25147
+ value: createSandboxClosure({
25148
+ guest: true,
25149
+ sandbox: true,
25150
+ name: "resize",
25151
+ length: 1,
25152
+ call: async (args, context) => {
25153
+ const receiver = context?.thisValue;
25154
+ if (!isSandboxArrayBuffer(receiver) || arrayBufferOptions(receiver) === void 0)
25155
+ throw new TypeError("ArrayBuffer resize requires a resizable buffer receiver.");
25156
+ const release = retainValues(budget, () => [receiver, ...args]);
25157
+ try {
25158
+ const number = await sandboxNumber(args[0], budget, context);
25159
+ const length = Number.isNaN(number) ? 0 : Math.trunc(number);
25160
+ if (!Number.isSafeInteger(length) || length < 0 || length > arrayBufferOptions(receiver).maxByteLength)
25161
+ throw new RangeError("Invalid ArrayBuffer resize length.");
25162
+ budget.allocateArrayLength(length);
25163
+ budget.provisionDataUsage(Math.max(length - arrayBufferLength(receiver), 0))();
25164
+ Reflect.apply(resizeBuffer2, receiver, [length]);
25165
+ return void 0;
25166
+ } finally {
25167
+ release();
25168
+ }
25169
+ }
25170
+ })
25171
+ });
25172
+ for (const key of ["byteLength", "maxByteLength", "resizable"]) {
25173
+ const getter = createSandboxClosure({
25174
+ guest: true,
25175
+ sandbox: true,
25176
+ name: `get ${key}`,
25177
+ length: 0,
25178
+ call: (_args, context) => {
25179
+ if (!isSandboxArrayBuffer(context?.thisValue)) throw new TypeError(`ArrayBuffer ${key} requires a buffer receiver.`);
25180
+ if (key === "byteLength") return arrayBufferLength(context.thisValue);
25181
+ const options = arrayBufferOptions(context.thisValue);
25182
+ return key === "resizable" ? options !== void 0 : options?.maxByteLength ?? arrayBufferLength(context.thisValue);
25183
+ }
25184
+ });
25185
+ Object.defineProperty(prototype, key, { get: accessorAdapter(getter, "get"), configurable: true });
25186
+ getters.push(getter);
25187
+ }
25188
+ setSandboxPrototype(prototype, getSandboxPrototype(/* @__PURE__ */ Object.create(null), budget));
25189
+ arrayBufferPrototypes.set(budget, prototype);
25190
+ registerBuiltinIdentities(budget, { ArrayBuffer: constructor });
25191
+ registerIntrinsicFunction(budget, constructor);
25192
+ for (const getter of getters) registerIntrinsicFunction(budget, getter);
25193
+ registerIntrinsicObject(budget, prototype);
25194
+ return constructor;
25195
+ }
25196
+
24870
25197
  // packages/safe-js/src/interp/globals.ts
24871
25198
  function createBuiltinBindings(options) {
24872
25199
  const bindings = {
@@ -24879,6 +25206,7 @@ function createBuiltinBindings(options) {
24879
25206
  ...createErrorGlobals({ ...options, errorPrototypes: options.errorPrototypes !== false }),
24880
25207
  ...createMathGlobals({ random: options.random, budget: options.budget }),
24881
25208
  ...createObjectArrayGlobals(options),
25209
+ ArrayBuffer: createArrayBufferGlobal(options.budget),
24882
25210
  ...createMiscGlobals(options),
24883
25211
  ...createUriGlobals(options.budget),
24884
25212
  ...createPromiseGlobals(options),
@@ -25615,6 +25943,11 @@ function validateDumpHeap(root, state) {
25615
25943
  validateDateRecord(entry, path);
25616
25944
  continue;
25617
25945
  }
25946
+ if (entry.kind === "arraybuffer") {
25947
+ validateArrayBufferStorage(entry);
25948
+ validateGuestHeapNode({ kind: "guest-object", state: entry.state }, heap, state.limits.maxEntries);
25949
+ continue;
25950
+ }
25618
25951
  if (entry.kind === "float32array") {
25619
25952
  validateFloat32Storage(entry);
25620
25953
  requireRecord(entry.entries, `${path}.entries`);
@@ -25838,7 +26171,7 @@ function validateHeapValue(value, path, state, heap) {
25838
26171
  fail("invalidValue", path, String(error));
25839
26172
  }
25840
26173
  validateErrorType(record4, path);
25841
- if (!["symbol", "arguments", "array", "object", "map", "set", "float32array", "date", "boxed", "collection-iterator", "regexp-iterator", "regex-object"].includes(String(record4.kind)))
26174
+ if (!["symbol", "arguments", "array", "object", "map", "set", "float32array", "arraybuffer", "date", "boxed", "collection-iterator", "regexp-iterator", "regex-object"].includes(String(record4.kind)))
25842
26175
  fail("unknownTag", `${path}.kind`, "unknown heap tag");
25843
26176
  validateValue(record4, path, 1, state);
25844
26177
  if (record4.kind === "symbol") validateSymbolRecord(record4, path, state);
@@ -25847,6 +26180,10 @@ function validateHeapValue(value, path, state, heap) {
25847
26180
  if (record4.kind === "object") requireRecord(record4.entries, `${path}.entries`);
25848
26181
  if (record4.kind === "boxed") validateBoxedRecord(record4, path, heap);
25849
26182
  if (record4.kind === "date") validateDateRecord(record4, path);
26183
+ if (record4.kind === "arraybuffer") {
26184
+ validateArrayBufferStorage(record4);
26185
+ validateGuestHeapNode({ kind: "guest-object", state: record4.state }, heap, state.limits.maxEntries);
26186
+ }
25850
26187
  if (record4.kind === "float32array") {
25851
26188
  validateFloat32Storage(record4);
25852
26189
  requireRecord(record4.entries, `${path}.entries`);
@@ -26043,7 +26380,7 @@ function validateGenericValue(value, path, depth, state) {
26043
26380
  if (typeof value === "object" && value !== null && hasGuestObjectState(value)) {
26044
26381
  fail("invalidState", path, "guest function properties, prototype links and custom descriptors cannot be restored");
26045
26382
  }
26046
- if (state.dataPropertiesOnly && types6.isProxy(value)) {
26383
+ if (state.dataPropertiesOnly && types7.isProxy(value)) {
26047
26384
  fail("invalidType", path, "proxy objects are not snapshot data");
26048
26385
  }
26049
26386
  if (depth > state.limits.maxDepth)
@@ -26842,12 +27179,10 @@ function measureSandboxData(values, options = {}) {
26842
27179
  }
26843
27180
  const prototype = getSandboxPrototype(value);
26844
27181
  if (prototype !== null) visit(prototype, depth + 1);
27182
+ if (isSandboxArrayBuffer(value)) usage += arrayBufferLength(value);
26845
27183
  if (isFloat32Array(value)) {
26846
27184
  const storage = float32Storage(value);
26847
- if (!seen.has(storage.buffer)) {
26848
- seen.add(storage.buffer);
26849
- usage += storage.byteLength;
26850
- }
27185
+ visit(storage.buffer, depth + 1);
26851
27186
  for (const [key, descriptor] of float32Properties(value)) {
26852
27187
  usage += 1;
26853
27188
  if (typeof key === "string") usage += key.length;
@@ -26986,7 +27321,7 @@ function measureSandboxData(values, options = {}) {
26986
27321
  const descriptor = Object.getOwnPropertyDescriptor(value, key);
26987
27322
  if (descriptor !== void 0) descriptors2.push([key, descriptor]);
26988
27323
  }
26989
- const includeNonEnumerable = isSandboxDate(value) || sandboxErrorTypes.has(value) || hasManagedDescriptors(value);
27324
+ const includeNonEnumerable = isSandboxDate(value) || isSandboxArrayBuffer(value) || sandboxErrorTypes.has(value) || hasManagedDescriptors(value);
26990
27325
  for (const [key, descriptor] of descriptors2) {
26991
27326
  if (!descriptor.enumerable && !includeNonEnumerable) continue;
26992
27327
  usage += 1 + key.length;
@@ -27182,6 +27517,20 @@ function copyToSandbox(value, state, path = "<root>", cloneSandboxCollections =
27182
27517
  }
27183
27518
  return sandboxPromise;
27184
27519
  }
27520
+ if (isSandboxArrayBuffer(value)) {
27521
+ const existing = state.seen.get(value);
27522
+ if (existing !== void 0) return existing;
27523
+ const copy = copyArrayBufferStorage(value, state);
27524
+ state.seen.set(value, copy);
27525
+ for (const [key, descriptor] of arrayBufferDataProperties(value)) {
27526
+ Object.defineProperty(copy, key, {
27527
+ ...descriptor,
27528
+ value: copyToSandbox(descriptor.value, state, joinPath2(path, key), cloneSandboxCollections, depth + 1)
27529
+ });
27530
+ }
27531
+ if (!Object.isExtensible(value)) Object.preventExtensions(copy);
27532
+ return copy;
27533
+ }
27185
27534
  if (nodeTypes.isDate(value)) {
27186
27535
  const existing = state.seen.get(value);
27187
27536
  if (existing !== void 0) return existing;
@@ -27200,6 +27549,7 @@ function copyToSandbox(value, state, path = "<root>", cloneSandboxCollections =
27200
27549
  if (existing !== void 0) return existing;
27201
27550
  const copy = copyFloat32Storage(value, state);
27202
27551
  state.seen.set(value, copy);
27552
+ copyToSandbox(float32Storage(value).buffer, state, `${path}.buffer`, cloneSandboxCollections, depth + 1);
27203
27553
  for (const [key, descriptor] of float32DataProperties(value)) {
27204
27554
  Object.defineProperty(copy, key, {
27205
27555
  ...descriptor,
@@ -27421,6 +27771,7 @@ function copyFromSandbox(value, state, path = "<root>", options, depth = 0) {
27421
27771
  if (existing !== void 0) return existing;
27422
27772
  const copy = copyFloat32Storage(value, state);
27423
27773
  state.seen.set(value, copy);
27774
+ copyFromSandbox(float32Storage(value).buffer, state, `${path}.buffer`, options, depth + 1);
27424
27775
  for (const [key, descriptor] of float32DataProperties(value)) {
27425
27776
  Object.defineProperty(copy, key, {
27426
27777
  ...descriptor,
@@ -27430,6 +27781,20 @@ function copyFromSandbox(value, state, path = "<root>", options, depth = 0) {
27430
27781
  if (!Object.isExtensible(value)) Object.preventExtensions(copy);
27431
27782
  return copy;
27432
27783
  }
27784
+ if (isSandboxArrayBuffer(value)) {
27785
+ const existing = state.seen.get(value);
27786
+ if (existing !== void 0) return existing;
27787
+ const copy = copyArrayBufferStorage(value, state);
27788
+ state.seen.set(value, copy);
27789
+ for (const [key, descriptor] of arrayBufferDataProperties(value)) {
27790
+ Object.defineProperty(copy, key, {
27791
+ ...descriptor,
27792
+ value: copyFromSandbox(descriptor.value, state, joinPath2(path, key), options, depth + 1)
27793
+ });
27794
+ }
27795
+ if (!Object.isExtensible(value)) Object.preventExtensions(copy);
27796
+ return copy;
27797
+ }
27433
27798
  if (isSandboxDate(value)) {
27434
27799
  const existing = state.seen.get(value);
27435
27800
  if (existing !== void 0) return existing;
@@ -27591,6 +27956,16 @@ function allocateSandboxValue2(value, budget, seen) {
27591
27956
  budget.allocateString(value);
27592
27957
  return;
27593
27958
  }
27959
+ if (isSandboxArrayBuffer(value)) {
27960
+ if (seen.has(value)) return;
27961
+ seen.add(value);
27962
+ budget.allocateArrayLength(arrayBufferLength(value));
27963
+ for (const [key, descriptor] of arrayBufferDataProperties(value)) {
27964
+ if (typeof key === "string") budget.allocateString(key);
27965
+ allocateSandboxValue2(descriptor.value, budget, seen);
27966
+ }
27967
+ return;
27968
+ }
27594
27969
  if (isSandboxBox(value)) {
27595
27970
  if (seen.has(value)) return;
27596
27971
  seen.add(value);
@@ -28336,8 +28711,29 @@ function normalize(value, seen) {
28336
28711
  try {
28337
28712
  const date = copyNativeDate(value);
28338
28713
  if (date !== void 0) return Object.assign(/* @__PURE__ */ Object.create(null), { $type: "date", time: serializedDateTime(date) });
28714
+ if (isSandboxArrayBuffer(value)) {
28715
+ arrayBufferLength(value);
28716
+ const properties = /* @__PURE__ */ Object.create(null);
28717
+ const entries = [];
28718
+ for (const [key, descriptor] of arrayBufferDataProperties(value)) {
28719
+ if (typeof key === "symbol") throw new TypeError("ArrayBuffer symbol properties require an explicit host-call identity.");
28720
+ entries.push([key, descriptor]);
28721
+ }
28722
+ for (const [key, descriptor] of entries.sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0))
28723
+ defineOwnDataProperty(properties, key, normalize(descriptor.value, seen));
28724
+ return Object.assign(/* @__PURE__ */ Object.create(null), {
28725
+ $type: "arraybuffer",
28726
+ bytes: normalize(Array.from(new Uint8Array(value)), seen),
28727
+ ...arrayBufferOptions(value),
28728
+ properties
28729
+ });
28730
+ }
28339
28731
  if (isFloat32Array(value)) {
28340
28732
  const storage = float32Storage(value);
28733
+ const resizable = arrayBufferOptions(storage.buffer) !== void 0;
28734
+ const layout = resizable ? float32ViewLayouts.get(value) : void 0;
28735
+ if (resizable && layout === void 0)
28736
+ throw new TypeError("Resizable Float32Array host-call identity requires known view layout.");
28341
28737
  const properties = /* @__PURE__ */ Object.create(null);
28342
28738
  for (const [key, descriptor] of float32DataProperties(value).sort(
28343
28739
  ([left], [right]) => left < right ? -1 : left > right ? 1 : 0
@@ -28349,7 +28745,12 @@ function normalize(value, seen) {
28349
28745
  bytes: normalize(Array.from(new Uint8Array(storage.buffer)), seen),
28350
28746
  byteOffset: storage.byteOffset,
28351
28747
  length: storage.length,
28352
- properties
28748
+ properties,
28749
+ ...resizable || Reflect.ownKeys(storage.buffer).length > 0 ? { buffer: normalize(storage.buffer, seen) } : {},
28750
+ ...layout === void 0 ? {} : { viewLayout: {
28751
+ byteOffset: layout.byteOffset,
28752
+ length: layout.length ?? null
28753
+ } }
28353
28754
  });
28354
28755
  }
28355
28756
  if (Array.isArray(value)) {
@@ -29301,6 +29702,7 @@ function classDefinitionContains(node, expression) {
29301
29702
  var KNOWN_RUNTIME_GLOBALS = [
29302
29703
  "AggregateError",
29303
29704
  "Array",
29705
+ "ArrayBuffer",
29304
29706
  "Boolean",
29305
29707
  "BigInt",
29306
29708
  "Date",
@@ -39693,7 +40095,7 @@ function createReplayableRandom(options = {}) {
39693
40095
 
39694
40096
  // packages/safe-js/src/realm.ts
39695
40097
  import { AsyncLocalStorage as AsyncLocalStorage6 } from "node:async_hooks";
39696
- import { types as types7 } from "node:util";
40098
+ import { types as types8 } from "node:util";
39697
40099
 
39698
40100
  // packages/safe-js/src/modules/registry.ts
39699
40101
  function createUnknownModuleMessage2(moduleName, moduleNames) {
@@ -39832,7 +40234,7 @@ var RealmState = class {
39832
40234
  throw new TypeError("Realm limits must be positive safe integers with supported names.");
39833
40235
  this.limits[name] = Number(value);
39834
40236
  }
39835
- if (options.extensions !== void 0 && (!Array.isArray(options.extensions) || types7.isProxy(options.extensions)))
40237
+ if (options.extensions !== void 0 && (!Array.isArray(options.extensions) || types8.isProxy(options.extensions)))
39836
40238
  throw new TypeError("Extensions must be a registration array.");
39837
40239
  const registrations = options.extensions ?? [];
39838
40240
  const extensions = [];
@@ -40067,7 +40469,7 @@ var RealmState = class {
40067
40469
  return this.phase.run(phase, () => {
40068
40470
  try {
40069
40471
  const result = call();
40070
- if (types7.isPromise(result) || phase.pending.size > 0 || phase.failure !== void 0) {
40472
+ if (types8.isPromise(result) || phase.pending.size > 0 || phase.failure !== void 0) {
40071
40473
  return Promise.resolve(result).then(
40072
40474
  async (value) => {
40073
40475
  await Promise.allSettled(phase.pending);
@@ -40136,7 +40538,7 @@ var RealmState = class {
40136
40538
  },
40137
40539
  read: (operation, validate) => {
40138
40540
  const value = this.invokeHost(operation, operation);
40139
- if (types7.isPromise(value)) {
40541
+ if (types8.isPromise(value)) {
40140
40542
  void Promise.resolve(value).catch(() => void 0);
40141
40543
  throw new TypeError("Live property operations must be synchronous.");
40142
40544
  }
@@ -40144,7 +40546,7 @@ var RealmState = class {
40144
40546
  },
40145
40547
  write: (operation, value) => {
40146
40548
  const result = this.invokeHost(operation, () => operation(this.exportValue(value)));
40147
- if (types7.isPromise(result)) {
40549
+ if (types8.isPromise(result)) {
40148
40550
  void Promise.resolve(result).catch(() => void 0);
40149
40551
  throw new TypeError("Live property setters must be synchronous.");
40150
40552
  }
@@ -40353,7 +40755,7 @@ var RealmState = class {
40353
40755
  }
40354
40756
  });
40355
40757
  const output = getExtensionSetup(extension)(context);
40356
- if (types7.isPromise(output)) {
40758
+ if (types8.isPromise(output)) {
40357
40759
  void Promise.resolve(output).catch(() => void 0);
40358
40760
  throw new TypeError("Extension setup must be synchronous.");
40359
40761
  }
@@ -40545,7 +40947,7 @@ var RealmState = class {
40545
40947
  };
40546
40948
  function readModules(input) {
40547
40949
  const entries = (value, label) => {
40548
- if (types7.isMap(value) && !types7.isProxy(value)) {
40950
+ if (types8.isMap(value) && !types8.isProxy(value)) {
40549
40951
  const result = [...Map.prototype.entries.call(value)];
40550
40952
  if (result.length > 4096 || result.some(([key]) => typeof key !== "string" || key.length === 0))
40551
40953
  throw new TypeError(`${label} requires bounded string keys.`);
@@ -41814,4 +42216,4 @@ export {
41814
42216
  FileSnapshotBackend,
41815
42217
  run
41816
42218
  };
41817
- //# sourceMappingURL=chunk-PT7PBLVT.js.map
42219
+ //# sourceMappingURL=chunk-G47EGC6S.js.map