@poe-platform/safe-js 0.1.314 → 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,7 +14961,7 @@ 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");
14880
14965
  const bridge = {
14881
14966
  ...context,
14882
14967
  stack: context?.stack ?? [],
@@ -14897,16 +14982,17 @@ 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
14987
  let length;
14902
- if (isFloat32Array(source)) length = float32Storage(source).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
14993
  length = Number.isNaN(size) || size <= 0 ? 0 : Math.min(Math.trunc(size), Number.MAX_SAFE_INTEGER);
14908
14994
  }
14909
- if (offset + length > 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
14997
  else for (let index = 0; index < length; index++) {
14912
14998
  budget.visitNode();
@@ -14925,8 +15011,15 @@ function getFloat32Member(value, property, budget) {
14925
15011
  const start = relativeIndex(await sandboxNumber(args[0], budget, bridge), storage2.length);
14926
15012
  const end = args[1] === void 0 ? storage2.length : relativeIndex(await sandboxNumber(args[1], budget, bridge), storage2.length);
14927
15013
  const length = Math.max(end - start, 0);
14928
- if (key === "subarray")
14929
- return new Float32Array(storage2.buffer, storage2.byteOffset + start * 4, length);
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
+ }
14930
15023
  checkFloat32Allocation(length, budget);
14931
15024
  const result = new Float32Array(length);
14932
15025
  new Uint8Array(result.buffer).set(
@@ -21046,18 +21139,20 @@ function serializeArray(value, serializeValue) {
21046
21139
  }
21047
21140
 
21048
21141
  // packages/safe-js/src/snapshot/float32array.ts
21049
- function encodeFloat32Storage(value, id, buffers, reference) {
21142
+ function encodeFloat32Layout(value) {
21050
21143
  const storage = float32Storage(value);
21051
- const existing = buffers.get(storage.buffer);
21052
- 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.");
21053
21147
  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) }
21148
+ byteOffset: layout.byteOffset,
21149
+ length: layout.length ?? 0,
21150
+ ...layout.length === void 0 ? { lengthTracking: true } : {}
21058
21151
  };
21059
21152
  }
21060
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.");
21061
21156
  if (!Number.isSafeInteger(value.length) || Number(value.length) < 0 || !Number.isSafeInteger(value.byteOffset) || Number(value.byteOffset) < 0 || Number(value.byteOffset) % 4 !== 0)
21062
21157
  throw new TypeError("Invalid Float32Array view dimensions.");
21063
21158
  if (Object.hasOwn(value, "bytes") === Object.hasOwn(value, "buffer"))
@@ -21067,7 +21162,7 @@ function validateFloat32Storage(value) {
21067
21162
  throw new TypeError("Invalid Float32Array backing bytes.");
21068
21163
  }
21069
21164
  }
21070
- function decodeFloat32Storage(value, resolve) {
21165
+ function decodeFloat32Storage(value, resolve, budget) {
21071
21166
  validateFloat32Storage(value);
21072
21167
  let buffer;
21073
21168
  if (Array.isArray(value.bytes)) {
@@ -21075,10 +21170,50 @@ function decodeFloat32Storage(value, resolve) {
21075
21170
  new Uint8Array(buffer).set(value.bytes);
21076
21171
  } else {
21077
21172
  const referenced = resolve(value.buffer);
21078
- if (!isFloat32Array(referenced)) throw new TypeError("Invalid Float32Array backing reference.");
21079
- 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.");
21080
21176
  }
21081
- 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.");
21082
21217
  }
21083
21218
 
21084
21219
  // packages/safe-js/src/snapshot/date-properties.ts
@@ -21209,7 +21344,7 @@ function serializeDumpValue(value, path, state) {
21209
21344
  if (hasGuestObjectState(value) && !(state.trustedRunReplay && (isSandboxDate(value) || isSandboxPromise(value) || isPromiseResolvingFunction(value)))) {
21210
21345
  throw new TypeError("Guest function properties and prototype links cannot be serialized.");
21211
21346
  }
21212
- 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);
21213
21348
  if (Array.isArray(value)) {
21214
21349
  const reference2 = serializeHeapReference(value, path, state);
21215
21350
  if (reference2 !== void 0) {
@@ -21281,11 +21416,21 @@ function serializeHeapReference(value, path, state) {
21281
21416
  const serialized = serializeDumpValue(entry, `${path}.<date-property>`, state);
21282
21417
  return serialized === SKIP_VALUE ? { kind: "undefined" } : serialized;
21283
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
+ };
21284
21427
  } else if (isFloat32Array(value)) {
21285
- const storage = encodeFloat32Storage(value, id, state.float32Buffers, (id2) => ({
21286
- kind: "ref",
21287
- id: id2
21288
- }));
21428
+ const backing = float32Storage(value);
21429
+ const storage = {
21430
+ kind: "float32array",
21431
+ ...encodeFloat32Layout(value),
21432
+ buffer: serializeHeapReference(backing.buffer, `${path}.buffer`, state)
21433
+ };
21289
21434
  const entries = /* @__PURE__ */ Object.create(null);
21290
21435
  state.heap[String(id)] = { ...storage, entries };
21291
21436
  for (const [key, descriptor] of float32DataProperties(value)) {
@@ -21351,7 +21496,7 @@ function indexHeapContainers(snapshot) {
21351
21496
  const heapIds = /* @__PURE__ */ new Map();
21352
21497
  let nextId = 1;
21353
21498
  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)) {
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)) {
21355
21500
  heapIds.set(value, nextId);
21356
21501
  nextId += 1;
21357
21502
  }
@@ -21383,12 +21528,16 @@ function collectContainerStats(value, stats, ancestors, guestValues, depth = 0)
21383
21528
  stat2.expanded = true;
21384
21529
  ancestors.add(value);
21385
21530
  const guestEntries = [];
21386
- 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) => {
21387
21536
  guestEntries.push(entry);
21388
21537
  return null;
21389
21538
  });
21390
21539
  if (guest !== void 0) {
21391
- guestValues.add(value);
21540
+ if (!isSandboxArrayBuffer(value)) guestValues.add(value);
21392
21541
  for (const entry of guestEntries) {
21393
21542
  collectContainerStats(entry, stats, ancestors, guestValues, depth + 1);
21394
21543
  if (entry !== null && typeof entry === "object") stats.get(entry).forceHeap = true;
@@ -21396,6 +21545,7 @@ function collectContainerStats(value, stats, ancestors, guestValues, depth = 0)
21396
21545
  ancestors.delete(value);
21397
21546
  return;
21398
21547
  }
21548
+ for (const entry of guestEntries) collectContainerStats(entry, stats, ancestors, guestValues, depth + 1);
21399
21549
  if (!Array.isArray(value) && !isPlainObject(value) && !isFloat32Array(value) && !isSandboxDate(value)) {
21400
21550
  ancestors.delete(value);
21401
21551
  return;
@@ -21715,11 +21865,24 @@ function encodeReplayData(value, options = {}) {
21715
21865
  ...symbolProperties.length === 0 ? {} : { symbolProperties },
21716
21866
  ...Object.isExtensible(entry) ? {} : { extensible: false }
21717
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 };
21718
21883
  } else if (isFloat32Array(entry)) {
21719
- const storage = encodeFloat32Storage(entry, id, float32Buffers, (id2) => ({
21720
- tag: "ref",
21721
- id: id2
21722
- }));
21884
+ const backing = float32Storage(entry);
21885
+ const storage = { kind: "float32array", ...encodeFloat32Layout(entry), buffer: child(backing.buffer, "<buffer>") };
21723
21886
  const properties = /* @__PURE__ */ Object.create(null);
21724
21887
  for (const [key, descriptor] of float32DataProperties(entry)) {
21725
21888
  properties[key] = {
@@ -21822,7 +21985,7 @@ function decodeReplayData(input, options = {}, parent) {
21822
21985
  const graph = record2(input);
21823
21986
  const nodes = list(own(graph, "nodes"));
21824
21987
  const restored = /* @__PURE__ */ new Map();
21825
- const initializeIterators = [];
21988
+ const initializeValues = [];
21826
21989
  const decode = (entry, depth = 0) => {
21827
21990
  if (depth > MAX_DATA_DEPTH) throw new TypeError("Replay data exceeds the nesting limit.");
21828
21991
  if (entry === null || typeof entry === "boolean" || typeof entry === "string") return entry;
@@ -21947,10 +22110,20 @@ function decodeReplayData(input, options = {}, parent) {
21947
22110
  if (node.extensible === false) Object.preventExtensions(result3);
21948
22111
  return result3;
21949
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
+ }
21950
22123
  if (kind === "float32array") {
21951
22124
  if (typeof node.extensible !== "boolean")
21952
22125
  throw new TypeError("Invalid Float32Array extensibility.");
21953
- const result3 = decodeFloat32Storage(node, child);
22126
+ const result3 = decodeFloat32Storage(node, child, compilation.owner?.budget);
21954
22127
  restored.set(id, result3);
21955
22128
  defineProperties(result3, record2(own(node, "properties")), child);
21956
22129
  if (!node.extensible) Object.preventExtensions(result3);
@@ -21986,7 +22159,7 @@ function decodeReplayData(input, options = {}, parent) {
21986
22159
  restored.set(id, result3);
21987
22160
  const collection = child(own(node, "collection"));
21988
22161
  if (collection !== void 0 && !isSandboxMap(collection) && !isSandboxSet(collection)) throw new TypeError("Invalid replay collection iterator source.");
21989
- initializeIterators.push(() => {
22162
+ initializeValues.push(() => {
21990
22163
  restoreSandboxCollectionIterator({ collection, collectionKind, method, index, exhausted }, result3);
21991
22164
  });
21992
22165
  defineProperties(result3, record2(own(node, "properties")), child);
@@ -22057,7 +22230,7 @@ function decodeReplayData(input, options = {}, parent) {
22057
22230
  return result2;
22058
22231
  };
22059
22232
  const result = decode(own(graph, "root"));
22060
- for (const initialize of initializeIterators) initialize();
22233
+ for (const initialize of initializeValues) initialize();
22061
22234
  if (parent !== void 0) compilation.forward(compilation.tickets, parent);
22062
22235
  return result;
22063
22236
  } finally {
@@ -22836,12 +23009,43 @@ function copyHostValueToSandbox(value, stackFrames, options, state, path) {
22836
23009
  state.seen.set(value, date);
22837
23010
  return date;
22838
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
+ }
22839
23036
  if (isFloat32Array(value)) {
22840
23037
  const existing = state.seen.get(value);
22841
23038
  if (existing !== void 0) return existing;
22842
23039
  checkFloat32Allocation(Math.ceil(float32Storage(value).byteLength / 4), budget);
22843
23040
  const copy = copyFloat32Storage(value, state);
22844
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
+ );
22845
23049
  for (const [key, descriptor] of float32DataProperties(value)) {
22846
23050
  Object.defineProperty(copy, key, {
22847
23051
  ...descriptor,
@@ -24867,6 +25071,128 @@ function createBigIntGlobal(budget) {
24867
25071
  return constructor;
24868
25072
  }
24869
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
+
24870
25196
  // packages/safe-js/src/interp/globals.ts
24871
25197
  function createBuiltinBindings(options) {
24872
25198
  const bindings = {
@@ -24879,6 +25205,7 @@ function createBuiltinBindings(options) {
24879
25205
  ...createErrorGlobals({ ...options, errorPrototypes: options.errorPrototypes !== false }),
24880
25206
  ...createMathGlobals({ random: options.random, budget: options.budget }),
24881
25207
  ...createObjectArrayGlobals(options),
25208
+ ArrayBuffer: createArrayBufferGlobal(options.budget),
24882
25209
  ...createMiscGlobals(options),
24883
25210
  ...createUriGlobals(options.budget),
24884
25211
  ...createPromiseGlobals(options),
@@ -25615,6 +25942,11 @@ function validateDumpHeap(root, state) {
25615
25942
  validateDateRecord(entry, path);
25616
25943
  continue;
25617
25944
  }
25945
+ if (entry.kind === "arraybuffer") {
25946
+ validateArrayBufferStorage(entry);
25947
+ validateGuestHeapNode({ kind: "guest-object", state: entry.state }, heap, state.limits.maxEntries);
25948
+ continue;
25949
+ }
25618
25950
  if (entry.kind === "float32array") {
25619
25951
  validateFloat32Storage(entry);
25620
25952
  requireRecord(entry.entries, `${path}.entries`);
@@ -25838,7 +26170,7 @@ function validateHeapValue(value, path, state, heap) {
25838
26170
  fail("invalidValue", path, String(error));
25839
26171
  }
25840
26172
  validateErrorType(record4, path);
25841
- 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)))
25842
26174
  fail("unknownTag", `${path}.kind`, "unknown heap tag");
25843
26175
  validateValue(record4, path, 1, state);
25844
26176
  if (record4.kind === "symbol") validateSymbolRecord(record4, path, state);
@@ -25847,6 +26179,10 @@ function validateHeapValue(value, path, state, heap) {
25847
26179
  if (record4.kind === "object") requireRecord(record4.entries, `${path}.entries`);
25848
26180
  if (record4.kind === "boxed") validateBoxedRecord(record4, path, heap);
25849
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
+ }
25850
26186
  if (record4.kind === "float32array") {
25851
26187
  validateFloat32Storage(record4);
25852
26188
  requireRecord(record4.entries, `${path}.entries`);
@@ -26043,7 +26379,7 @@ function validateGenericValue(value, path, depth, state) {
26043
26379
  if (typeof value === "object" && value !== null && hasGuestObjectState(value)) {
26044
26380
  fail("invalidState", path, "guest function properties, prototype links and custom descriptors cannot be restored");
26045
26381
  }
26046
- if (state.dataPropertiesOnly && types6.isProxy(value)) {
26382
+ if (state.dataPropertiesOnly && types7.isProxy(value)) {
26047
26383
  fail("invalidType", path, "proxy objects are not snapshot data");
26048
26384
  }
26049
26385
  if (depth > state.limits.maxDepth)
@@ -26842,12 +27178,10 @@ function measureSandboxData(values, options = {}) {
26842
27178
  }
26843
27179
  const prototype = getSandboxPrototype(value);
26844
27180
  if (prototype !== null) visit(prototype, depth + 1);
27181
+ if (isSandboxArrayBuffer(value)) usage += arrayBufferLength(value);
26845
27182
  if (isFloat32Array(value)) {
26846
27183
  const storage = float32Storage(value);
26847
- if (!seen.has(storage.buffer)) {
26848
- seen.add(storage.buffer);
26849
- usage += storage.byteLength;
26850
- }
27184
+ visit(storage.buffer, depth + 1);
26851
27185
  for (const [key, descriptor] of float32Properties(value)) {
26852
27186
  usage += 1;
26853
27187
  if (typeof key === "string") usage += key.length;
@@ -26986,7 +27320,7 @@ function measureSandboxData(values, options = {}) {
26986
27320
  const descriptor = Object.getOwnPropertyDescriptor(value, key);
26987
27321
  if (descriptor !== void 0) descriptors2.push([key, descriptor]);
26988
27322
  }
26989
- const includeNonEnumerable = isSandboxDate(value) || sandboxErrorTypes.has(value) || hasManagedDescriptors(value);
27323
+ const includeNonEnumerable = isSandboxDate(value) || isSandboxArrayBuffer(value) || sandboxErrorTypes.has(value) || hasManagedDescriptors(value);
26990
27324
  for (const [key, descriptor] of descriptors2) {
26991
27325
  if (!descriptor.enumerable && !includeNonEnumerable) continue;
26992
27326
  usage += 1 + key.length;
@@ -27182,6 +27516,20 @@ function copyToSandbox(value, state, path = "<root>", cloneSandboxCollections =
27182
27516
  }
27183
27517
  return sandboxPromise;
27184
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
+ }
27185
27533
  if (nodeTypes.isDate(value)) {
27186
27534
  const existing = state.seen.get(value);
27187
27535
  if (existing !== void 0) return existing;
@@ -27200,6 +27548,7 @@ function copyToSandbox(value, state, path = "<root>", cloneSandboxCollections =
27200
27548
  if (existing !== void 0) return existing;
27201
27549
  const copy = copyFloat32Storage(value, state);
27202
27550
  state.seen.set(value, copy);
27551
+ copyToSandbox(float32Storage(value).buffer, state, `${path}.buffer`, cloneSandboxCollections, depth + 1);
27203
27552
  for (const [key, descriptor] of float32DataProperties(value)) {
27204
27553
  Object.defineProperty(copy, key, {
27205
27554
  ...descriptor,
@@ -27421,6 +27770,7 @@ function copyFromSandbox(value, state, path = "<root>", options, depth = 0) {
27421
27770
  if (existing !== void 0) return existing;
27422
27771
  const copy = copyFloat32Storage(value, state);
27423
27772
  state.seen.set(value, copy);
27773
+ copyFromSandbox(float32Storage(value).buffer, state, `${path}.buffer`, options, depth + 1);
27424
27774
  for (const [key, descriptor] of float32DataProperties(value)) {
27425
27775
  Object.defineProperty(copy, key, {
27426
27776
  ...descriptor,
@@ -27430,6 +27780,20 @@ function copyFromSandbox(value, state, path = "<root>", options, depth = 0) {
27430
27780
  if (!Object.isExtensible(value)) Object.preventExtensions(copy);
27431
27781
  return copy;
27432
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
+ }
27433
27797
  if (isSandboxDate(value)) {
27434
27798
  const existing = state.seen.get(value);
27435
27799
  if (existing !== void 0) return existing;
@@ -27591,6 +27955,16 @@ function allocateSandboxValue2(value, budget, seen) {
27591
27955
  budget.allocateString(value);
27592
27956
  return;
27593
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
+ }
27594
27968
  if (isSandboxBox(value)) {
27595
27969
  if (seen.has(value)) return;
27596
27970
  seen.add(value);
@@ -28336,8 +28710,29 @@ function normalize(value, seen) {
28336
28710
  try {
28337
28711
  const date = copyNativeDate(value);
28338
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
+ }
28339
28730
  if (isFloat32Array(value)) {
28340
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.");
28341
28736
  const properties = /* @__PURE__ */ Object.create(null);
28342
28737
  for (const [key, descriptor] of float32DataProperties(value).sort(
28343
28738
  ([left], [right]) => left < right ? -1 : left > right ? 1 : 0
@@ -28349,7 +28744,12 @@ function normalize(value, seen) {
28349
28744
  bytes: normalize(Array.from(new Uint8Array(storage.buffer)), seen),
28350
28745
  byteOffset: storage.byteOffset,
28351
28746
  length: storage.length,
28352
- 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
+ } }
28353
28753
  });
28354
28754
  }
28355
28755
  if (Array.isArray(value)) {
@@ -29301,6 +29701,7 @@ function classDefinitionContains(node, expression) {
29301
29701
  var KNOWN_RUNTIME_GLOBALS = [
29302
29702
  "AggregateError",
29303
29703
  "Array",
29704
+ "ArrayBuffer",
29304
29705
  "Boolean",
29305
29706
  "BigInt",
29306
29707
  "Date",
@@ -39693,7 +40094,7 @@ function createReplayableRandom(options = {}) {
39693
40094
 
39694
40095
  // packages/safe-js/src/realm.ts
39695
40096
  import { AsyncLocalStorage as AsyncLocalStorage6 } from "node:async_hooks";
39696
- import { types as types7 } from "node:util";
40097
+ import { types as types8 } from "node:util";
39697
40098
 
39698
40099
  // packages/safe-js/src/modules/registry.ts
39699
40100
  function createUnknownModuleMessage2(moduleName, moduleNames) {
@@ -39832,7 +40233,7 @@ var RealmState = class {
39832
40233
  throw new TypeError("Realm limits must be positive safe integers with supported names.");
39833
40234
  this.limits[name] = Number(value);
39834
40235
  }
39835
- 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)))
39836
40237
  throw new TypeError("Extensions must be a registration array.");
39837
40238
  const registrations = options.extensions ?? [];
39838
40239
  const extensions = [];
@@ -40067,7 +40468,7 @@ var RealmState = class {
40067
40468
  return this.phase.run(phase, () => {
40068
40469
  try {
40069
40470
  const result = call();
40070
- 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) {
40071
40472
  return Promise.resolve(result).then(
40072
40473
  async (value) => {
40073
40474
  await Promise.allSettled(phase.pending);
@@ -40136,7 +40537,7 @@ var RealmState = class {
40136
40537
  },
40137
40538
  read: (operation, validate) => {
40138
40539
  const value = this.invokeHost(operation, operation);
40139
- if (types7.isPromise(value)) {
40540
+ if (types8.isPromise(value)) {
40140
40541
  void Promise.resolve(value).catch(() => void 0);
40141
40542
  throw new TypeError("Live property operations must be synchronous.");
40142
40543
  }
@@ -40144,7 +40545,7 @@ var RealmState = class {
40144
40545
  },
40145
40546
  write: (operation, value) => {
40146
40547
  const result = this.invokeHost(operation, () => operation(this.exportValue(value)));
40147
- if (types7.isPromise(result)) {
40548
+ if (types8.isPromise(result)) {
40148
40549
  void Promise.resolve(result).catch(() => void 0);
40149
40550
  throw new TypeError("Live property setters must be synchronous.");
40150
40551
  }
@@ -40353,7 +40754,7 @@ var RealmState = class {
40353
40754
  }
40354
40755
  });
40355
40756
  const output = getExtensionSetup(extension)(context);
40356
- if (types7.isPromise(output)) {
40757
+ if (types8.isPromise(output)) {
40357
40758
  void Promise.resolve(output).catch(() => void 0);
40358
40759
  throw new TypeError("Extension setup must be synchronous.");
40359
40760
  }
@@ -40545,7 +40946,7 @@ var RealmState = class {
40545
40946
  };
40546
40947
  function readModules(input) {
40547
40948
  const entries = (value, label) => {
40548
- if (types7.isMap(value) && !types7.isProxy(value)) {
40949
+ if (types8.isMap(value) && !types8.isProxy(value)) {
40549
40950
  const result = [...Map.prototype.entries.call(value)];
40550
40951
  if (result.length > 4096 || result.some(([key]) => typeof key !== "string" || key.length === 0))
40551
40952
  throw new TypeError(`${label} requires bounded string keys.`);
@@ -41814,4 +42215,4 @@ export {
41814
42215
  FileSnapshotBackend,
41815
42216
  run
41816
42217
  };
41817
- //# sourceMappingURL=chunk-PT7PBLVT.js.map
42218
+ //# sourceMappingURL=chunk-C26TRM5L.js.map