@typeberry/jam 0.1.3-135961b → 0.1.3-462ca77

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.
@@ -3902,6 +3902,20 @@ const result_Result = {
3902
3902
  },
3903
3903
  };
3904
3904
 
3905
+ ;// CONCATENATED MODULE: ./packages/core/utils/safe-alloc-uint8array.ts
3906
+ // about 2GB, the maximum ArrayBuffer length on Chrome confirmed by several sources:
3907
+ // - https://issues.chromium.org/issues/40055619
3908
+ // - https://stackoverflow.com/a/72124984
3909
+ // - https://onnxruntime.ai/docs/tutorials/web/large-models.html#maximum-size-of-arraybuffer
3910
+ const MAX_LENGTH = 2145386496;
3911
+ function safe_alloc_uint8array_safeAllocUint8Array(length) {
3912
+ if (length > MAX_LENGTH) {
3913
+ // biome-ignore lint/suspicious/noConsole: can't have a dependency on logger here
3914
+ console.warn(`Trying to allocate ${length} bytes, which is greater than the maximum of ${MAX_LENGTH}.`);
3915
+ }
3916
+ return new Uint8Array(Math.min(MAX_LENGTH, length));
3917
+ }
3918
+
3905
3919
  ;// CONCATENATED MODULE: external "node:assert"
3906
3920
  const external_node_assert_namespaceObject = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("node:assert");
3907
3921
  ;// CONCATENATED MODULE: ./packages/core/utils/test.ts
@@ -4145,6 +4159,7 @@ function isResult(x) {
4145
4159
 
4146
4160
 
4147
4161
 
4162
+
4148
4163
  ;// CONCATENATED MODULE: ./packages/core/bytes/bitvec.ts
4149
4164
 
4150
4165
  /**
@@ -4166,7 +4181,7 @@ class bitvec_BitVec {
4166
4181
  * Create new [`BitVec`] with all values set to `false`.
4167
4182
  */
4168
4183
  static empty(bitLength) {
4169
- const data = new Uint8Array(Math.ceil(bitLength / 8));
4184
+ const data = safe_alloc_uint8array_safeAllocUint8Array(Math.ceil(bitLength / 8));
4170
4185
  return new bitvec_BitVec(data, bitLength);
4171
4186
  }
4172
4187
  byteLength;
@@ -4366,7 +4381,7 @@ class bytes_BytesBlob {
4366
4381
  static blobFromParts(v, ...rest) {
4367
4382
  const vArr = v instanceof Uint8Array ? [v] : v;
4368
4383
  const totalLength = vArr.reduce((a, v) => a + v.length, 0) + rest.reduce((a, v) => a + v.length, 0);
4369
- const buffer = new Uint8Array(totalLength);
4384
+ const buffer = safe_alloc_uint8array_safeAllocUint8Array(totalLength);
4370
4385
  let offset = 0;
4371
4386
  for (const r of vArr) {
4372
4387
  buffer.set(r, offset);
@@ -4439,7 +4454,7 @@ class bytes_Bytes extends bytes_BytesBlob {
4439
4454
  }
4440
4455
  /** Create an empty [`Bytes<X>`] of given length. */
4441
4456
  static zero(len) {
4442
- return new bytes_Bytes(new Uint8Array(len), len);
4457
+ return new bytes_Bytes(safe_alloc_uint8array_safeAllocUint8Array(len), len);
4443
4458
  }
4444
4459
  // TODO [ToDr] `fill` should have the argments swapped to align with the rest.
4445
4460
  /** Create a [`Bytes<X>`] with all bytes filled with given input number. */
@@ -5109,7 +5124,7 @@ function addSizeHints(a, b) {
5109
5124
  };
5110
5125
  }
5111
5126
  const DEFAULT_START_LENGTH = 512; // 512B
5112
- const MAX_LENGTH = 10 * 1024 * 1024; // 10MB
5127
+ const encoder_MAX_LENGTH = 10 * 1024 * 1024; // 10MB
5113
5128
  /**
5114
5129
  * JAM encoder.
5115
5130
  */
@@ -5125,7 +5140,7 @@ class encoder_Encoder {
5125
5140
  return new encoder_Encoder(options.destination);
5126
5141
  }
5127
5142
  const startLength = options?.expectedLength ?? DEFAULT_START_LENGTH;
5128
- const buffer = new ArrayBuffer(Math.min(MAX_LENGTH, startLength), { maxByteLength: MAX_LENGTH });
5143
+ const buffer = new ArrayBuffer(Math.min(encoder_MAX_LENGTH, startLength), { maxByteLength: encoder_MAX_LENGTH });
5129
5144
  const destination = new Uint8Array(buffer);
5130
5145
  return new encoder_Encoder(destination, buffer);
5131
5146
  }
@@ -5458,11 +5473,11 @@ class encoder_Encoder {
5458
5473
  ensureBigEnough(length, options = { silent: false }) {
5459
5474
  debug_check `${length >= 0} Negative length given`;
5460
5475
  const newLength = this.offset + length;
5461
- if (newLength > MAX_LENGTH) {
5476
+ if (newLength > encoder_MAX_LENGTH) {
5462
5477
  if (options.silent) {
5463
5478
  return;
5464
5479
  }
5465
- throw new Error(`The encoded size would reach the maximum of ${MAX_LENGTH}.`);
5480
+ throw new Error(`The encoded size would reach the maximum of ${encoder_MAX_LENGTH}.`);
5466
5481
  }
5467
5482
  if (newLength > this.destination.length) {
5468
5483
  // we can try to resize the underlying buffer
@@ -5470,7 +5485,7 @@ class encoder_Encoder {
5470
5485
  // make sure we at least double the size of the buffer every time.
5471
5486
  const minExtend = Math.max(newLength, this.buffer.byteLength << 1);
5472
5487
  // but we must never exceed the max length.
5473
- this.buffer.resize(Math.min(MAX_LENGTH, minExtend));
5488
+ this.buffer.resize(Math.min(encoder_MAX_LENGTH, minExtend));
5474
5489
  }
5475
5490
  // and then check again
5476
5491
  if (newLength > this.destination.length) {
@@ -6802,7 +6817,7 @@ async function verify(input) {
6802
6817
  return Promise.resolve([]);
6803
6818
  }
6804
6819
  const dataLength = input.reduce((acc, { message, key, signature }) => acc + key.length + signature.length + message.length + 1, 0);
6805
- const data = new Uint8Array(dataLength);
6820
+ const data = safeAllocUint8Array(dataLength);
6806
6821
  let offset = 0;
6807
6822
  for (const { key, message, signature } of input) {
6808
6823
  data.set(key.raw, offset);
@@ -6889,7 +6904,7 @@ class allocator_SimpleAllocator {
6889
6904
  /** An allocator that works by allocating larger (continuous) pages of memory. */
6890
6905
  class PageAllocator {
6891
6906
  hashesPerPage;
6892
- page = new Uint8Array(0);
6907
+ page = safeAllocUint8Array(0);
6893
6908
  currentHash = 0;
6894
6909
  // TODO [ToDr] Benchmark the performance!
6895
6910
  constructor(hashesPerPage) {
@@ -6900,7 +6915,7 @@ class PageAllocator {
6900
6915
  resetPage() {
6901
6916
  const pageSizeBytes = this.hashesPerPage * HASH_SIZE;
6902
6917
  this.currentHash = 0;
6903
- this.page = new Uint8Array(pageSizeBytes);
6918
+ this.page = safeAllocUint8Array(pageSizeBytes);
6904
6919
  }
6905
6920
  emptyHash() {
6906
6921
  const startIdx = this.currentHash * HASH_SIZE;
@@ -11610,7 +11625,7 @@ class SerializedService {
11610
11625
  getStorage(rawKey) {
11611
11626
  if (Compatibility.isLessThan(GpVersion.V0_6_7)) {
11612
11627
  const SERVICE_ID_BYTES = 4;
11613
- const serviceIdAndKey = new Uint8Array(SERVICE_ID_BYTES + rawKey.length);
11628
+ const serviceIdAndKey = safe_alloc_uint8array_safeAllocUint8Array(SERVICE_ID_BYTES + rawKey.length);
11614
11629
  serviceIdAndKey.set(numbers_u32AsLeBytes(this.serviceId));
11615
11630
  serviceIdAndKey.set(rawKey.raw, SERVICE_ID_BYTES);
11616
11631
  const key = opaque_asOpaqueType(bytes_BytesBlob.blobFrom(hashBytes(serviceIdAndKey).raw));
@@ -11701,7 +11716,7 @@ class TrieNode {
11701
11716
  raw;
11702
11717
  constructor(
11703
11718
  /** Exactly 512 bits / 64 bytes */
11704
- raw = new Uint8Array(TRIE_NODE_BYTES)) {
11719
+ raw = safe_alloc_uint8array_safeAllocUint8Array(TRIE_NODE_BYTES)) {
11705
11720
  this.raw = raw;
11706
11721
  }
11707
11722
  /** Returns the type of the node */
@@ -14225,7 +14240,7 @@ class registers_Registers {
14225
14240
  bytes;
14226
14241
  asSigned;
14227
14242
  asUnsigned;
14228
- constructor(bytes = new Uint8Array(NO_OF_REGISTERS << REGISTER_SIZE_SHIFT)) {
14243
+ constructor(bytes = safeAllocUint8Array(NO_OF_REGISTERS << REGISTER_SIZE_SHIFT)) {
14229
14244
  this.bytes = bytes;
14230
14245
  check `${bytes.length === NO_OF_REGISTERS << REGISTER_SIZE_SHIFT} Invalid size of registers array.`;
14231
14246
  this.asSigned = new BigInt64Array(bytes.buffer, bytes.byteOffset);
@@ -14388,7 +14403,7 @@ class mask_Mask {
14388
14403
  return Math.min(this.lookupTableForward[index] ?? 0, MAX_INSTRUCTION_DISTANCE);
14389
14404
  }
14390
14405
  buildLookupTableForward(mask) {
14391
- const table = new Uint8Array(mask.bitLength);
14406
+ const table = safeAllocUint8Array(mask.bitLength);
14392
14407
  let lastInstructionOffset = 0;
14393
14408
  for (let i = mask.bitLength - 1; i >= 0; i--) {
14394
14409
  if (mask.isSet(i)) {
@@ -17899,7 +17914,7 @@ class HostCalls {
17899
17914
  const regs = pvmInstance.getRegisters();
17900
17915
  const maybeAddress = regs.getLowerU32(7);
17901
17916
  const maybeLength = regs.getLowerU32(8);
17902
- const result = new Uint8Array(maybeLength);
17917
+ const result = safeAllocUint8Array(maybeLength);
17903
17918
  const startAddress = tryAsMemoryIndex(maybeAddress);
17904
17919
  const loadResult = memory.loadInto(result, startAddress);
17905
17920
  if (loadResult.isError) {