bireader 3.1.9 → 3.1.10

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.
@@ -1,3 +1,8 @@
1
+ const MIN_SAFE = BigInt(Number.MIN_SAFE_INTEGER);
2
+ const MAX_SAFE = BigInt(Number.MAX_SAFE_INTEGER);
3
+ function isSafeInt64(big) {
4
+ return big >= MIN_SAFE && big <= MAX_SAFE;
5
+ }
1
6
  function isBuffer(obj) {
2
7
  return buffcheck(obj);
3
8
  }
@@ -1350,6 +1355,14 @@ function rint64(ctx, unsigned, endian) {
1350
1355
  }
1351
1356
  }
1352
1357
  ctx.bitoffset = 0;
1358
+ if (ctx.enforceBigInt) {
1359
+ return value;
1360
+ }
1361
+ else {
1362
+ if (isSafeInt64(value)) {
1363
+ return Number(value);
1364
+ }
1365
+ }
1353
1366
  return value;
1354
1367
  }
1355
1368
  function wint64(ctx, value, unsigned, endian) {
@@ -1436,9 +1449,9 @@ function wdfloat(ctx, value, endian) {
1436
1449
  function rdfloat(ctx, endian) {
1437
1450
  endian = (endian == undefined ? ctx.endian : endian);
1438
1451
  var uint64Value = ctx.readInt64(true, endian);
1439
- const sign = (uint64Value & 0x8000000000000000n) >> 63n;
1440
- const exponent = Number((uint64Value & 0x7ff0000000000000n) >> 52n) - 1023;
1441
- const fraction = Number(uint64Value & 0x000fffffffffffffn) / Math.pow(2, 52);
1452
+ const sign = (BigInt(uint64Value) & 0x8000000000000000n) >> 63n;
1453
+ const exponent = Number((BigInt(uint64Value) & 0x7ff0000000000000n) >> 52n) - 1023;
1454
+ const fraction = Number(BigInt(uint64Value) & 0x000fffffffffffffn) / Math.pow(2, 52);
1442
1455
  var floatValue;
1443
1456
  if (exponent == -1023) {
1444
1457
  if (fraction == 0) {
@@ -1748,6 +1761,7 @@ class BiBase {
1748
1761
  */
1749
1762
  this.strDefaults = { stringType: "utf-8", terminateValue: 0x0 };
1750
1763
  this.maxFileSize = null;
1764
+ this.enforceBigInt = false;
1751
1765
  }
1752
1766
  /**
1753
1767
  * Settings for when using .str
@@ -3425,9 +3439,11 @@ class BiBase {
3425
3439
  /**
3426
3440
  * Read signed 64 bit integer.
3427
3441
  *
3442
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
3443
+ *
3428
3444
  * @param {boolean} unsigned - if value is unsigned or not
3429
3445
  * @param {endian?} endian - ``big`` or ``little``
3430
- * @returns {bigint}
3446
+ * @returns {BigValue}
3431
3447
  */
3432
3448
  readInt64(unsigned, endian) {
3433
3449
  return rint64(this, unsigned, endian);
@@ -3486,7 +3502,9 @@ class BiBase {
3486
3502
  /**
3487
3503
  * Read unsigned 64 bit integer.
3488
3504
  *
3489
- * @returns {bigint}
3505
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
3506
+ *
3507
+ * @returns {BigValue}
3490
3508
  */
3491
3509
  readUInt64() {
3492
3510
  return this.readInt64(true);
@@ -3494,7 +3512,9 @@ class BiBase {
3494
3512
  /**
3495
3513
  * Read signed 64 bit integer.
3496
3514
  *
3497
- * @returns {bigint}
3515
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
3516
+ *
3517
+ * @returns {BigValue}
3498
3518
  */
3499
3519
  readInt64BE() {
3500
3520
  return this.readInt64(false, "big");
@@ -3502,7 +3522,9 @@ class BiBase {
3502
3522
  /**
3503
3523
  * Read unsigned 64 bit integer.
3504
3524
  *
3505
- * @returns {bigint}
3525
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
3526
+ *
3527
+ * @returns {BigValue}
3506
3528
  */
3507
3529
  readUInt64BE() {
3508
3530
  return this.readInt64(true, "big");
@@ -3510,7 +3532,9 @@ class BiBase {
3510
3532
  /**
3511
3533
  * Read signed 64 bit integer.
3512
3534
  *
3513
- * @returns {bigint}
3535
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
3536
+ *
3537
+ * @returns {BigValue}
3514
3538
  */
3515
3539
  readInt64LE() {
3516
3540
  return this.readInt64(false, "little");
@@ -3518,7 +3542,9 @@ class BiBase {
3518
3542
  /**
3519
3543
  * Read unsigned 64 bit integer.
3520
3544
  *
3521
- * @returns {bigint}
3545
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
3546
+ *
3547
+ * @returns {BigValue}
3522
3548
  */
3523
3549
  readUInt64LE() {
3524
3550
  return this.readInt64(true, "little");
@@ -3610,11 +3636,12 @@ class BiBase {
3610
3636
  *
3611
3637
  * @param {Buffer|Uint8Array} data - ``Buffer`` or ``Uint8Array``. Always found in ``BiReader.data``
3612
3638
  * @param {BiOptions?} options - Any options to set at start
3613
- * @param {number?} options.byteOffset - Byte offset to start reader (default ``0``)
3614
- * @param {number?} options.bitOffset - Bit offset 0-7 to start reader (default ``0``)
3615
- * @param {string?} options.endianness - Endianness ``big`` or ``little`` (default ``little``)
3616
- * @param {boolean?} options.strict - Strict mode: if ``true`` does not extend supplied array on outside write (default ``true``)
3617
- * @param {number?} options.extendBufferSize - Amount of data to add when extending the buffer array when strict mode is false. Note: Changes logic in ``.get`` and ``.return``.
3639
+ * @param {BiOptions["byteOffset"]?} options.byteOffset - Byte offset to start writer (default ``0``)
3640
+ * @param {BiOptions["bitOffset"]?} options.bitOffset - Bit offset 0-7 to start writer (default ``0``)
3641
+ * @param {BiOptions["endianness"]?} options.endianness - Endianness ``big`` or ``little`` (default ``little``)
3642
+ * @param {BiOptions["strict"]?} options.strict - Strict mode: if ``true`` does not extend supplied array on outside write (default ``false``)
3643
+ * @param {BiOptions["extendBufferSize"]?} options.extendBufferSize - Amount of data to add when extending the buffer array when strict mode is false. Note: Changes logic in ``.get`` and ``.return``.
3644
+ * @param {BiOptions["enforceBigInt"]?} options.enforceBigInt - 64 bit value reads will always stay ``BigInt``.
3618
3645
  *
3619
3646
  * @since 2.0
3620
3647
  */
@@ -3624,11 +3651,12 @@ class BiReader extends BiBase {
3624
3651
  *
3625
3652
  * @param {Buffer|Uint8Array} data - ``Buffer`` or ``Uint8Array``. Always found in ``BiReader.data``
3626
3653
  * @param {BiOptions?} options - Any options to set at start
3627
- * @param {number?} options.byteOffset - Byte offset to start reader (default ``0``)
3628
- * @param {number?} options.bitOffset - Bit offset 0-7 to start reader (default ``0``)
3629
- * @param {string?} options.endianness - Endianness ``big`` or ``little`` (default ``little``)
3630
- * @param {boolean?} options.strict - Strict mode: if ``true`` does not extend supplied array on outside write (default ``true``)
3631
- * @param {number?} options.extendBufferSize - Amount of data to add when extending the buffer array when strict mode is false. Note: Changes logic in ``.get`` and ``.return``.
3654
+ * @param {BiOptions["byteOffset"]?} options.byteOffset - Byte offset to start writer (default ``0``)
3655
+ * @param {BiOptions["bitOffset"]?} options.bitOffset - Bit offset 0-7 to start writer (default ``0``)
3656
+ * @param {BiOptions["endianness"]?} options.endianness - Endianness ``big`` or ``little`` (default ``little``)
3657
+ * @param {BiOptions["strict"]?} options.strict - Strict mode: if ``true`` does not extend supplied array on outside write (default ``false``)
3658
+ * @param {BiOptions["extendBufferSize"]?} options.extendBufferSize - Amount of data to add when extending the buffer array when strict mode is false. Note: Changes logic in ``.get`` and ``.return``.
3659
+ * @param {BiOptions["enforceBigInt"]?} options.enforceBigInt - 64 bit value reads will always stay ``BigInt``.
3632
3660
  */
3633
3661
  constructor(data, options = {}) {
3634
3662
  super();
@@ -3642,6 +3670,7 @@ class BiReader extends BiBase {
3642
3670
  }
3643
3671
  this.data = data;
3644
3672
  }
3673
+ this.enforceBigInt = options?.enforceBigInt ?? false;
3645
3674
  if (options.extendBufferSize != undefined && options.extendBufferSize != 0) {
3646
3675
  this.extendBufferSize = options.extendBufferSize;
3647
3676
  }
@@ -6142,7 +6171,9 @@ class BiReader extends BiBase {
6142
6171
  /**
6143
6172
  * Read signed 64 bit integer
6144
6173
  *
6145
- * @returns {bigint}
6174
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6175
+ *
6176
+ * @returns {BigValue}
6146
6177
  */
6147
6178
  get int64() {
6148
6179
  return this.readInt64();
@@ -6150,7 +6181,9 @@ class BiReader extends BiBase {
6150
6181
  /**
6151
6182
  * Read signed 64 bit integer.
6152
6183
  *
6153
- * @returns {bigint}
6184
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6185
+ *
6186
+ * @returns {BigValue}
6154
6187
  */
6155
6188
  get bigint() {
6156
6189
  return this.readInt64();
@@ -6158,7 +6191,9 @@ class BiReader extends BiBase {
6158
6191
  /**
6159
6192
  * Read signed 64 bit integer.
6160
6193
  *
6161
- * @returns {bigint}
6194
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6195
+ *
6196
+ * @returns {BigValue}
6162
6197
  */
6163
6198
  get quad() {
6164
6199
  return this.readInt64();
@@ -6166,7 +6201,9 @@ class BiReader extends BiBase {
6166
6201
  /**
6167
6202
  * Read unsigned 64 bit integer.
6168
6203
  *
6169
- * @returns {bigint}
6204
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6205
+ *
6206
+ * @returns {BigValue}
6170
6207
  */
6171
6208
  get uint64() {
6172
6209
  return this.readInt64(true);
@@ -6174,7 +6211,9 @@ class BiReader extends BiBase {
6174
6211
  /**
6175
6212
  * Read unsigned 64 bit integer.
6176
6213
  *
6177
- * @returns {bigint}
6214
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6215
+ *
6216
+ * @returns {BigValue}
6178
6217
  */
6179
6218
  get ubigint() {
6180
6219
  return this.readInt64(true);
@@ -6182,7 +6221,9 @@ class BiReader extends BiBase {
6182
6221
  /**
6183
6222
  * Read unsigned 64 bit integer.
6184
6223
  *
6185
- * @returns {bigint}
6224
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6225
+ *
6226
+ * @returns {BigValue}
6186
6227
  */
6187
6228
  get uquad() {
6188
6229
  return this.readInt64(true);
@@ -6190,7 +6231,9 @@ class BiReader extends BiBase {
6190
6231
  /**
6191
6232
  * Read signed 64 bit integer.
6192
6233
  *
6193
- * @returns {bigint}
6234
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6235
+ *
6236
+ * @returns {BigValue}
6194
6237
  */
6195
6238
  get int64be() {
6196
6239
  return this.readInt64(false, "big");
@@ -6198,7 +6241,9 @@ class BiReader extends BiBase {
6198
6241
  /**
6199
6242
  * Read signed 64 bit integer.
6200
6243
  *
6201
- * @returns {bigint}
6244
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6245
+ *
6246
+ * @returns {BigValue}
6202
6247
  */
6203
6248
  get bigintbe() {
6204
6249
  return this.readInt64(false, "big");
@@ -6206,7 +6251,9 @@ class BiReader extends BiBase {
6206
6251
  /**
6207
6252
  * Read signed 64 bit integer.
6208
6253
  *
6209
- * @returns {bigint}
6254
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6255
+ *
6256
+ * @returns {BigValue}
6210
6257
  */
6211
6258
  get quadbe() {
6212
6259
  return this.readInt64(false, "big");
@@ -6214,7 +6261,9 @@ class BiReader extends BiBase {
6214
6261
  /**
6215
6262
  * Read unsigned 64 bit integer.
6216
6263
  *
6217
- * @returns {bigint}
6264
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6265
+ *
6266
+ * @returns {BigValue}
6218
6267
  */
6219
6268
  get uint64be() {
6220
6269
  return this.readInt64(true, "big");
@@ -6222,7 +6271,9 @@ class BiReader extends BiBase {
6222
6271
  /**
6223
6272
  * Read unsigned 64 bit integer.
6224
6273
  *
6225
- * @returns {bigint}
6274
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6275
+ *
6276
+ * @returns {BigValue}
6226
6277
  */
6227
6278
  get ubigintbe() {
6228
6279
  return this.readInt64(true, "big");
@@ -6230,7 +6281,9 @@ class BiReader extends BiBase {
6230
6281
  /**
6231
6282
  * Read unsigned 64 bit integer.
6232
6283
  *
6233
- * @returns {bigint}
6284
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6285
+ *
6286
+ * @returns {BigValue}
6234
6287
  */
6235
6288
  get uquadbe() {
6236
6289
  return this.readInt64(true, "big");
@@ -6238,7 +6291,9 @@ class BiReader extends BiBase {
6238
6291
  /**
6239
6292
  * Read signed 64 bit integer.
6240
6293
  *
6241
- * @returns {bigint}
6294
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6295
+ *
6296
+ * @returns {BigValue}
6242
6297
  */
6243
6298
  get int64le() {
6244
6299
  return this.readInt64(false, "little");
@@ -6246,7 +6301,9 @@ class BiReader extends BiBase {
6246
6301
  /**
6247
6302
  * Read signed 64 bit integer.
6248
6303
  *
6249
- * @returns {bigint}
6304
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6305
+ *
6306
+ * @returns {BigValue}
6250
6307
  */
6251
6308
  get bigintle() {
6252
6309
  return this.readInt64(false, "little");
@@ -6254,7 +6311,9 @@ class BiReader extends BiBase {
6254
6311
  /**
6255
6312
  * Read signed 64 bit integer.
6256
6313
  *
6257
- * @returns {bigint}
6314
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6315
+ *
6316
+ * @returns {BigValue}
6258
6317
  */
6259
6318
  get quadle() {
6260
6319
  return this.readInt64(false, "little");
@@ -6262,7 +6321,9 @@ class BiReader extends BiBase {
6262
6321
  /**
6263
6322
  * Read unsigned 64 bit integer.
6264
6323
  *
6265
- * @returns {bigint}
6324
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6325
+ *
6326
+ * @returns {BigValue}
6266
6327
  */
6267
6328
  get uint64le() {
6268
6329
  return this.readInt64(true, "little");
@@ -6270,7 +6331,9 @@ class BiReader extends BiBase {
6270
6331
  /**
6271
6332
  * Read unsigned 64 bit integer.
6272
6333
  *
6273
- * @returns {bigint}
6334
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6335
+ *
6336
+ * @returns {BigValue}
6274
6337
  */
6275
6338
  get ubigintle() {
6276
6339
  return this.readInt64(true, "little");
@@ -6278,7 +6341,9 @@ class BiReader extends BiBase {
6278
6341
  /**
6279
6342
  * Read unsigned 64 bit integer.
6280
6343
  *
6281
- * @returns {bigint}
6344
+ * Note: If ``enforceBigInt`` was set to ``true``, this always returns a ``BigInt`` otherwise it will return a ``number`` if integer safe.
6345
+ *
6346
+ * @returns {BigValue}
6282
6347
  */
6283
6348
  get uquadle() {
6284
6349
  return this.readInt64(true, "little");
@@ -6675,6 +6740,7 @@ class BiReader extends BiBase {
6675
6740
  * @param {BiOptions["endianness"]?} options.endianness - Endianness ``big`` or ``little`` (default ``little``)
6676
6741
  * @param {BiOptions["strict"]?} options.strict - Strict mode: if ``true`` does not extend supplied array on outside write (default ``false``)
6677
6742
  * @param {BiOptions["extendBufferSize"]?} options.extendBufferSize - Amount of data to add when extending the buffer array when strict mode is false. Note: Changes logic in ``.get`` and ``.return``.
6743
+ * @param {BiOptions["enforceBigInt"]?} options.enforceBigInt - 64 bit value reads will always stay ``BigInt``.
6678
6744
  *
6679
6745
  * @since 2.0
6680
6746
  */
@@ -6689,6 +6755,7 @@ class BiWriter extends BiBase {
6689
6755
  * @param {BiOptions["endianness"]?} options.endianness - Endianness ``big`` or ``little`` (default ``little``)
6690
6756
  * @param {BiOptions["strict"]?} options.strict - Strict mode: if ``true`` does not extend supplied array on outside write (default ``false``)
6691
6757
  * @param {BiOptions["extendBufferSize"]?} options.extendBufferSize - Amount of data to add when extending the buffer array when strict mode is false. Note: Changes logic in ``.get`` and ``.return``.
6758
+ * @param {BiOptions["enforceBigInt"]?} options.enforceBigInt - 64 bit value reads will always stay ``BigInt``.
6692
6759
  */
6693
6760
  constructor(data, options = {}) {
6694
6761
  super();
@@ -6707,6 +6774,7 @@ class BiWriter extends BiBase {
6707
6774
  }
6708
6775
  this.data = data;
6709
6776
  }
6777
+ this.enforceBigInt = options?.enforceBigInt ?? false;
6710
6778
  if (options.extendBufferSize != undefined && options.extendBufferSize != 0) {
6711
6779
  this.extendBufferSize = options.extendBufferSize;
6712
6780
  }