bireader 2.0.1 → 3.0.1

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.
@@ -43,7 +43,12 @@ function checkSize(_this, write_bytes, write_bit, offset) {
43
43
  if (needed_size > _this.size) {
44
44
  const dif = needed_size - _this.size;
45
45
  if (_this.strict == false) {
46
- _this.extendArray(dif);
46
+ if (_this.extendBufferSize != 0) {
47
+ _this.extendArray(_this.extendBufferSize);
48
+ }
49
+ else {
50
+ _this.extendArray(dif);
51
+ }
47
52
  }
48
53
  else {
49
54
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m hexdump:\n" + _this.hexdump() : "";
@@ -61,7 +66,12 @@ function skip(_this, bytes, bits) {
61
66
  }
62
67
  if (new_size > _this.size) {
63
68
  if (_this.strict == false) {
64
- _this.extendArray(new_size - _this.size);
69
+ if (_this.extendBufferSize != 0) {
70
+ _this.extendArray(_this.extendBufferSize);
71
+ }
72
+ else {
73
+ _this.extendArray(new_size - _this.size);
74
+ }
65
75
  }
66
76
  else {
67
77
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m hexdump:\n" + _this.hexdump() : "";
@@ -101,7 +111,12 @@ function goto(_this, bytes, bits) {
101
111
  }
102
112
  if (new_size > _this.size) {
103
113
  if (_this.strict == false) {
104
- _this.extendArray(new_size - _this.size);
114
+ if (_this.extendBufferSize != 0) {
115
+ _this.extendArray(_this.extendBufferSize);
116
+ }
117
+ else {
118
+ _this.extendArray(new_size - _this.size);
119
+ }
105
120
  }
106
121
  else {
107
122
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m hexdump:\n" + _this.hexdump() : "";
@@ -124,16 +139,21 @@ function remove(_this, startOffset, endOffset, consume, remove, fillValue) {
124
139
  const new_offset = (endOffset || _this.offset);
125
140
  if (new_offset > _this.size) {
126
141
  if (_this.strict == false) {
127
- _this.extendArray(new_offset - _this.size);
142
+ if (_this.extendBufferSize != 0) {
143
+ _this.extendArray(_this.extendBufferSize);
144
+ }
145
+ else {
146
+ _this.extendArray(new_offset - _this.size);
147
+ }
128
148
  }
129
149
  else {
130
150
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
131
- throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + endOffset + " of " + _this.size);
151
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset " + endOffset + " of " + _this.size);
132
152
  }
133
153
  }
134
154
  if (_this.strict == true && remove == true) {
135
155
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
136
- throw new Error("\x1b[33m[Strict mode]\x1b[0m: Can not remove data in strict mode: endOffset" + endOffset + " of " + _this.size);
156
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: Can not remove data in strict mode: endOffset " + endOffset + " of " + _this.size);
137
157
  }
138
158
  const data_removed = _this.data.slice(new_start, new_offset);
139
159
  if (remove) {
@@ -221,19 +241,16 @@ function addData(_this, data, consume, offset, replace) {
221
241
  }
222
242
  exports.addData = addData;
223
243
  /**
224
- * Console logs provided data as hex dump.
244
+ * Creates hex dump string. Will console log or return string if set in options.
225
245
  *
226
246
  * @param {Uint8Array|Buffer} src - Uint8Array or Buffer
227
- * @param {object} options
228
- * ```javascript
229
- * {
230
- * length: 192, // number of bytes to log, default 192 or end of data
231
- * startByte: 0, // byte to start dump (default 0)
232
- * supressUnicode: false // Supress unicode character preview for even columns
233
- * }
234
- * ```
247
+ * @param {hexdumpOptions?} options - hex dump options
248
+ * @param {number?} options.length - number of bytes to log, default ``192`` or end of data
249
+ * @param {number?} options.startByte - byte to start dump (default ``0``)
250
+ * @param {boolean?} options.supressUnicode - Supress unicode character preview for even columns.
251
+ * @param {boolean?} options.returnString - Returns the hex dump string instead of logging it.
235
252
  */
236
- function hexdump(src, options) {
253
+ function hexdump(src, options = {}) {
237
254
  if (!(src instanceof Uint8Array || isBuffer(src))) {
238
255
  throw new Error("Write data must be Uint8Array or Buffer.");
239
256
  }
@@ -244,10 +261,10 @@ function hexdump(src, options) {
244
261
  errorDump: true,
245
262
  extendArray: extendarray,
246
263
  };
247
- hexDump(fake_reader, options);
264
+ return hexDump(fake_reader, options);
248
265
  }
249
266
  exports.hexdump = hexdump;
250
- function hexDump(_this, options) {
267
+ function hexDump(_this, options = {}) {
251
268
  var length = options && options.length;
252
269
  var startByte = options && options.startByte;
253
270
  var supressUnicode = options && options.supressUnicode || false;
@@ -427,18 +444,28 @@ function hexDump(_this, options) {
427
444
  if (make_wide) {
428
445
  rows.push("*Removed character byte header on unicode detection");
429
446
  }
430
- console.log(rows.join("\n"));
447
+ if (options && options.returnString) {
448
+ return rows.join("\n");
449
+ }
450
+ else {
451
+ console.log(rows.join("\n"));
452
+ }
431
453
  }
432
454
  exports.hexDump = hexDump;
433
455
  function AND(_this, and_key, start, end, consume) {
434
456
  const input = _this.data;
435
457
  if ((end || 0) > _this.size) {
436
458
  if (_this.strict == false) {
437
- _this.extendArray((end || 0) - _this.size);
459
+ if (_this.extendBufferSize != 0) {
460
+ _this.extendArray(_this.extendBufferSize);
461
+ }
462
+ else {
463
+ _this.extendArray((end || 0) - _this.size);
464
+ }
438
465
  }
439
466
  else {
440
467
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
441
- throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
468
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset " + (end || 0) + " of " + _this.size);
442
469
  }
443
470
  }
444
471
  if (typeof and_key == "number") {
@@ -477,11 +504,16 @@ function OR(_this, or_key, start, end, consume) {
477
504
  const input = _this.data;
478
505
  if ((end || 0) > _this.size) {
479
506
  if (_this.strict == false) {
480
- _this.extendArray((end || 0) - _this.size);
507
+ if (_this.extendBufferSize != 0) {
508
+ _this.extendArray(_this.extendBufferSize);
509
+ }
510
+ else {
511
+ _this.extendArray((end || 0) - _this.size);
512
+ }
481
513
  }
482
514
  else {
483
515
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
484
- throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
516
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset " + (end || 0) + " of " + _this.size);
485
517
  }
486
518
  }
487
519
  if (typeof or_key == "number") {
@@ -520,11 +552,16 @@ function XOR(_this, xor_key, start, end, consume) {
520
552
  const input = _this.data;
521
553
  if ((end || 0) > _this.size) {
522
554
  if (_this.strict == false) {
523
- _this.extendArray((end || 0) - _this.size);
555
+ if (_this.extendBufferSize != 0) {
556
+ _this.extendArray(_this.extendBufferSize);
557
+ }
558
+ else {
559
+ _this.extendArray((end || 0) - _this.size);
560
+ }
524
561
  }
525
562
  else {
526
563
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
527
- throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
564
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset " + (end || 0) + " of " + _this.size);
528
565
  }
529
566
  }
530
567
  if (typeof xor_key == "number") {
@@ -562,11 +599,16 @@ exports.XOR = XOR;
562
599
  function NOT(_this, start, end, consume) {
563
600
  if ((end || 0) > _this.size) {
564
601
  if (_this.strict == false) {
565
- _this.extendArray((end || 0) - _this.size);
602
+ if (_this.extendBufferSize != 0) {
603
+ _this.extendArray(_this.extendBufferSize);
604
+ }
605
+ else {
606
+ _this.extendArray((end || 0) - _this.size);
607
+ }
566
608
  }
567
609
  else {
568
610
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
569
- throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
611
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset " + (end || 0) + " of " + _this.size);
570
612
  }
571
613
  }
572
614
  for (let i = (start || 0); i < Math.min(end || _this.size, _this.size); i++) {
@@ -582,11 +624,16 @@ function LSHIFT(_this, shift_key, start, end, consume) {
582
624
  const input = _this.data;
583
625
  if ((end || 0) > _this.size) {
584
626
  if (_this.strict == false) {
585
- _this.extendArray((end || 0) - _this.size);
627
+ if (_this.extendBufferSize != 0) {
628
+ _this.extendArray(_this.extendBufferSize);
629
+ }
630
+ else {
631
+ _this.extendArray((end || 0) - _this.size);
632
+ }
586
633
  }
587
634
  else {
588
635
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
589
- throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
636
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset " + (end || 0) + " of " + _this.size);
590
637
  }
591
638
  }
592
639
  if (typeof shift_key == "number") {
@@ -625,11 +672,16 @@ function RSHIFT(_this, shift_key, start, end, consume) {
625
672
  const input = _this.data;
626
673
  if ((end || 0) > _this.size) {
627
674
  if (_this.strict == false) {
628
- _this.extendArray((end || 0) - _this.size);
675
+ if (_this.extendBufferSize != 0) {
676
+ _this.extendArray(_this.extendBufferSize);
677
+ }
678
+ else {
679
+ _this.extendArray((end || 0) - _this.size);
680
+ }
629
681
  }
630
682
  else {
631
683
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
632
- throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
684
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset " + (end || 0) + " of " + _this.size);
633
685
  }
634
686
  }
635
687
  if (typeof shift_key == "number") {
@@ -668,11 +720,16 @@ function ADD(_this, add_key, start, end, consume) {
668
720
  const input = _this.data;
669
721
  if ((end || 0) > _this.size) {
670
722
  if (_this.strict == false) {
671
- _this.extendArray((end || 0) - _this.size);
723
+ if (_this.extendBufferSize != 0) {
724
+ _this.extendArray(_this.extendBufferSize);
725
+ }
726
+ else {
727
+ _this.extendArray((end || 0) - _this.size);
728
+ }
672
729
  }
673
730
  else {
674
731
  _this.errorDump ? "\x1b[31m[Error]\x1b[0m: hexdump:\n" + _this.hexdump() : "";
675
- throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset" + (end || 0) + " of " + _this.size);
732
+ throw new Error("\x1b[33m[Strict mode]\x1b[0m: End offset outside of data: endOffset " + (end || 0) + " of " + _this.size);
676
733
  }
677
734
  }
678
735
  if (typeof add_key == "number") {
@@ -953,7 +1010,12 @@ function wbit(_this, value, bits, unsigned, endian) {
953
1010
  const size_needed = ((((bits - 1) + _this.bitoffset) / 8) + _this.offset);
954
1011
  if (size_needed > _this.size) {
955
1012
  //add size
956
- _this.extendArray(size_needed - _this.size);
1013
+ if (_this.extendBufferSize != 0) {
1014
+ _this.extendArray(_this.extendBufferSize);
1015
+ }
1016
+ else {
1017
+ _this.extendArray(size_needed - _this.size);
1018
+ }
957
1019
  }
958
1020
  var off_in_bits = (_this.offset * 8) + _this.bitoffset;
959
1021
  for (var i = 0; i < bits;) {
@@ -1719,6 +1781,16 @@ class ReaderBase {
1719
1781
  * @type {Buffer|Uint8Array}
1720
1782
  */
1721
1783
  this.data = [];
1784
+ /**
1785
+ * When the data buffer needs to be extended while strict mode is ``false``, this will be the amount it extends.
1786
+ *
1787
+ * Otherwise it extends just the amount of the next written value.
1788
+ *
1789
+ * This can greatly speed up data writes when large files are being written.
1790
+ *
1791
+ * NOTE: Using ``BiWriter.get`` or ``BiWriter.return`` will now remove all data after the current write position. Use ``BiWriter.data`` to get the full buffer instead.
1792
+ */
1793
+ this.extendBufferSize = 0;
1722
1794
  }
1723
1795
  isBufferOrUint8Array(obj) {
1724
1796
  return arraybuffcheck(this, obj);
@@ -1996,32 +2068,44 @@ class ReaderBase {
1996
2068
  /**
1997
2069
  * Returns current data.
1998
2070
  *
2071
+ * Note: Will remove all data after current position if ``extendBufferSize`` was set.
2072
+ *
2073
+ * Use ``.data`` instead if you want the full buffer data.
2074
+ *
1999
2075
  * @returns {Buffer|Uint8Array} ``Buffer`` or ``Uint8Array``
2000
2076
  */
2001
2077
  get get() {
2078
+ if (this.extendBufferSize != 0) {
2079
+ this.trim();
2080
+ }
2002
2081
  return this.data;
2003
2082
  }
2004
2083
  /**
2005
2084
  * Returns current data.
2006
2085
  *
2086
+ * Note: Will remove all data after current position if ``extendBufferSize`` was set.
2087
+ *
2088
+ * Use ``.data`` instead if you want the full buffer data.
2089
+ *
2007
2090
  * @returns {Buffer|Uint8Array} ``Buffer`` or ``Uint8Array``
2008
2091
  */
2009
2092
  get return() {
2093
+ if (this.extendBufferSize != 0) {
2094
+ this.trim();
2095
+ }
2010
2096
  return this.data;
2011
2097
  }
2012
2098
  /**
2013
- * Console logs data as hex dump.
2099
+ * Creates hex dump string. Will console log or return string if set in options.
2014
2100
  *
2015
2101
  * @param {object} options
2016
- * ```javascript
2017
- * {
2018
- * length: 192, // number of bytes to log, default 192 or end of data
2019
- * startByte: 0, // byte to start dump (default current byte position)
2020
- * supressUnicode: false // Supress unicode character preview for even columns
2021
- * }
2022
- * ```
2102
+ * @param {hexdumpOptions?} options - hex dump options
2103
+ * @param {number?} options.length - number of bytes to log, default ``192`` or end of data
2104
+ * @param {number?} options.startByte - byte to start dump (default ``0``)
2105
+ * @param {boolean?} options.supressUnicode - Supress unicode character preview for even columns.
2106
+ * @param {boolean?} options.returnString - Returns the hex dump string instead of logging it.
2023
2107
  */
2024
- hexdump(options) {
2108
+ hexdump(options = {}) {
2025
2109
  return hexDump(this, options);
2026
2110
  }
2027
2111
  /**