bireader 1.0.44 → 1.0.46

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.
package/lib/esm/index.mjs CHANGED
@@ -170,7 +170,7 @@ function addData(_this, data, consume, offset, replace) {
170
170
  }
171
171
  var needed_size = offset || _this.offset;
172
172
  if (replace) {
173
- needed_size = offset || _this.offset + data.length;
173
+ needed_size = (offset || _this.offset) + data.length;
174
174
  }
175
175
  if (needed_size > _this.size) {
176
176
  if (_this.strict == false) {
@@ -183,35 +183,29 @@ function addData(_this, data, consume, offset, replace) {
183
183
  _this.size = _this.data.length;
184
184
  }
185
185
  if (replace) {
186
+ const part1 = _this.data.subarray(0, needed_size - data.length);
187
+ const part2 = _this.data.subarray(needed_size, _this.size);
186
188
  if (isBuffer(_this.data)) {
187
- const part1 = _this.data.subarray(0, needed_size - data.length);
188
- const part2 = _this.data.subarray(needed_size, _this.size);
189
189
  _this.data = Buffer.concat([part1, data, part2]);
190
- _this.size = _this.data.length;
191
190
  }
192
191
  else {
193
- const part1 = _this.data.subarray(0, needed_size - data.length);
194
- const part2 = _this.data.subarray(needed_size, _this.size);
195
192
  _this.data = new Uint8Array([...part1, ...data, ...part2]);
196
- _this.size = _this.data.length;
197
193
  }
194
+ _this.size = _this.data.length;
198
195
  }
199
196
  else {
197
+ const part1 = _this.data.subarray(0, needed_size);
198
+ const part2 = _this.data.subarray(needed_size, _this.size);
200
199
  if (isBuffer(_this.data)) {
201
- const part1 = _this.data.subarray(0, needed_size);
202
- const part2 = _this.data.subarray(needed_size, _this.size);
203
200
  _this.data = Buffer.concat([part1, data, part2]);
204
- _this.size = _this.data.length;
205
201
  }
206
202
  else {
207
- const part1 = _this.data.subarray(0, needed_size);
208
- const part2 = _this.data.subarray(needed_size, _this.size);
209
203
  _this.data = new Uint8Array([...part1, ...data, ...part2]);
210
- _this.size = _this.data.length;
211
204
  }
205
+ _this.size = _this.data.length;
212
206
  }
213
207
  if (consume) {
214
- _this.offset = needed_size;
208
+ _this.offset = (offset || _this.offset) + data.length;
215
209
  _this.bitoffset = 0;
216
210
  }
217
211
  }
@@ -1643,6 +1637,16 @@ export class bireader {
1643
1637
  this.strict = false;
1644
1638
  this.errorDump = true;
1645
1639
  this.data = [];
1640
+ if (data == undefined) {
1641
+ throw new Error("Data required");
1642
+ }
1643
+ else {
1644
+ if (!this.isBufferOrUint8Array(data)) {
1645
+ throw new Error("Write data must be Uint8Array or Buffer");
1646
+ }
1647
+ }
1648
+ this.size = data.length;
1649
+ this.data = data;
1646
1650
  if (endianness != undefined && typeof endianness != "string") {
1647
1651
  throw new Error("Endian must be big or little");
1648
1652
  }
@@ -1650,6 +1654,14 @@ export class bireader {
1650
1654
  throw new Error("Byte order must be big or little");
1651
1655
  }
1652
1656
  this.endian = endianness || "little";
1657
+ if (typeof strict == "boolean") {
1658
+ this.strict = strict;
1659
+ }
1660
+ else {
1661
+ if (strict != undefined) {
1662
+ throw new Error("Strict mode must be true of false");
1663
+ }
1664
+ }
1653
1665
  if (byteOffset != undefined || bitOffset != undefined) {
1654
1666
  this.offset = ((Math.abs(byteOffset || 0)) + Math.ceil((Math.abs(bitOffset || 0)) / 8));
1655
1667
  // Adjust byte offset based on bit overflow
@@ -1660,25 +1672,15 @@ export class bireader {
1660
1672
  this.bitoffset = Math.min(Math.max(this.bitoffset, 0), 7);
1661
1673
  // Ensure offset doesn't go negative
1662
1674
  this.offset = Math.max(this.offset, 0);
1663
- }
1664
- if (typeof strict == "boolean") {
1665
- this.strict = strict;
1666
- }
1667
- else {
1668
- if (strict != undefined) {
1669
- throw new Error("Strict mode must be true of false");
1670
- }
1671
- }
1672
- if (data == undefined) {
1673
- throw new Error("Data required");
1674
- }
1675
- else {
1676
- if (!this.isBufferOrUint8Array(data)) {
1677
- throw new Error("Write data must be Uint8Array or Buffer");
1675
+ if (this.offset > this.size) {
1676
+ if (this.strict == false) {
1677
+ this.extendArray(this.offset - this.size);
1678
+ }
1679
+ else {
1680
+ throw new Error(`Starting offset outside of size: ${this.offset} of ${this.size}`);
1681
+ }
1678
1682
  }
1679
1683
  }
1680
- this.size = data.length;
1681
- this.data = data;
1682
1684
  }
1683
1685
  /**
1684
1686
  *
@@ -2374,7 +2376,7 @@ export class bireader {
2374
2376
  * @param {number} offset - Byte position to add at (defaults to current position)
2375
2377
  */
2376
2378
  insert(data, consume, offset) {
2377
- return addData(this, data, consume || false, offset || this.offset);
2379
+ return addData(this, data, consume || false, offset || this.offset, false);
2378
2380
  }
2379
2381
  /**
2380
2382
  * Inserts data into data
@@ -2385,7 +2387,7 @@ export class bireader {
2385
2387
  * @param {number} offset - Byte position to add at (defaults to current position)
2386
2388
  */
2387
2389
  place(data, consume, offset) {
2388
- return addData(this, data, consume || false, offset || this.offset);
2390
+ return addData(this, data, consume || false, offset || this.offset, false);
2389
2391
  }
2390
2392
  /**
2391
2393
  * Replaces data in data
@@ -2417,7 +2419,7 @@ export class bireader {
2417
2419
  * @param {boolean} consume - Move current write position to end of data (default false)
2418
2420
  */
2419
2421
  unshift(data, consume) {
2420
- return addData(this, data, consume || false, 0);
2422
+ return addData(this, data, consume || false, 0, false);
2421
2423
  }
2422
2424
  /**
2423
2425
  * Adds data to start of supplied data
@@ -2427,7 +2429,7 @@ export class bireader {
2427
2429
  * @param {boolean} consume - Move current write position to end of data (default false)
2428
2430
  */
2429
2431
  prepend(data, consume) {
2430
- return addData(this, data, consume || false, 0);
2432
+ return addData(this, data, consume || false, 0, false);
2431
2433
  }
2432
2434
  /**
2433
2435
  * Adds data to end of supplied data
@@ -2437,7 +2439,7 @@ export class bireader {
2437
2439
  * @param {boolean} consume - Move current write position to end of data (default false)
2438
2440
  */
2439
2441
  push(data, consume) {
2440
- return addData(this, data, consume || false, this.size);
2442
+ return addData(this, data, consume || false, this.size, false);
2441
2443
  }
2442
2444
  /**
2443
2445
  * Adds data to end of supplied data
@@ -2447,7 +2449,7 @@ export class bireader {
2447
2449
  * @param {boolean} consume - Move current write position to end of data (default false)
2448
2450
  */
2449
2451
  append(data, consume) {
2450
- return addData(this, data, consume || false, this.size);
2452
+ return addData(this, data, consume || false, this.size, false);
2451
2453
  }
2452
2454
  //
2453
2455
  //finishing
@@ -6209,11 +6211,34 @@ export class biwriter {
6209
6211
  this.strict = false;
6210
6212
  this.errorDump = true;
6211
6213
  this.data = [];
6214
+ if (data == undefined) {
6215
+ if (typeof Buffer !== 'undefined') {
6216
+ this.data = Buffer.alloc(this.offset || 1 + (this.bitoffset != 0 ? 1 : 0));
6217
+ }
6218
+ else {
6219
+ this.data = new Uint8Array(this.offset || 1 + (this.bitoffset != 0 ? 1 : 0));
6220
+ }
6221
+ }
6222
+ else {
6223
+ if (!this.isBufferOrUint8Array(data)) {
6224
+ throw new Error("Write data must be Uint8Array or Buffer.");
6225
+ }
6226
+ this.data = data;
6227
+ }
6228
+ this.size = this.data.length;
6229
+ if (typeof strict == "boolean") {
6230
+ this.strict = strict;
6231
+ }
6232
+ else {
6233
+ if (strict != undefined) {
6234
+ throw new Error("Strict mode must be true of false.");
6235
+ }
6236
+ }
6212
6237
  if (endianness != undefined && typeof endianness != "string") {
6213
- throw new Error("endianness must be big or little");
6238
+ throw new Error("endianness must be big or little.");
6214
6239
  }
6215
6240
  if (endianness != undefined && !(endianness == "big" || endianness == "little")) {
6216
- throw new Error("Endianness must be big or little");
6241
+ throw new Error("Endianness must be big or little.");
6217
6242
  }
6218
6243
  this.endian = endianness || "little";
6219
6244
  if (byteOffset != undefined || bitOffset != undefined) {
@@ -6226,30 +6251,15 @@ export class biwriter {
6226
6251
  this.bitoffset = Math.min(Math.max(this.bitoffset, 0), 7);
6227
6252
  // Ensure offset doesn't go negative
6228
6253
  this.offset = Math.max(this.offset, 0);
6229
- }
6230
- if (typeof strict == "boolean") {
6231
- this.strict = strict;
6232
- }
6233
- else {
6234
- if (strict != undefined) {
6235
- throw new Error("Strict mode must be true of false");
6236
- }
6237
- }
6238
- if (data == undefined) {
6239
- if (typeof Buffer !== 'undefined') {
6240
- this.data = Buffer.alloc(this.offset || 1 + (this.bitoffset != 0 ? 1 : 0));
6241
- }
6242
- else {
6243
- this.data = new Uint8Array(this.offset || 1 + (this.bitoffset != 0 ? 1 : 0));
6244
- }
6245
- }
6246
- else {
6247
- if (!this.isBufferOrUint8Array(data)) {
6248
- throw new Error("Write data must be Uint8Array or Buffer");
6254
+ if (this.offset > this.size) {
6255
+ if (this.strict == false) {
6256
+ this.extendArray(this.offset - this.size);
6257
+ }
6258
+ else {
6259
+ throw new Error(`Starting offset outside of size: ${this.offset} of ${this.size}`);
6260
+ }
6249
6261
  }
6250
6262
  }
6251
- this.data = data;
6252
- this.size = this.data.length;
6253
6263
  }
6254
6264
  /**
6255
6265
  * Change Endian (default little)
@@ -6951,7 +6961,7 @@ export class biwriter {
6951
6961
  * @param {number} offset - Byte position to add at (defaults to current position)
6952
6962
  */
6953
6963
  insert(data, consume, offset) {
6954
- return addData(this, data, consume || false, offset || this.offset);
6964
+ return addData(this, data, consume || false, offset || this.offset, false);
6955
6965
  }
6956
6966
  /**
6957
6967
  * Inserts data into data
@@ -6962,7 +6972,7 @@ export class biwriter {
6962
6972
  * @param {number} offset - Byte position to add at (defaults to current position)
6963
6973
  */
6964
6974
  place(data, consume, offset) {
6965
- return addData(this, data, consume || false, offset || this.offset);
6975
+ return addData(this, data, consume || false, offset || this.offset, false);
6966
6976
  }
6967
6977
  /**
6968
6978
  * Replaces data in data
@@ -6994,7 +7004,7 @@ export class biwriter {
6994
7004
  * @param {boolean} consume - Move current write position to end of data (default false)
6995
7005
  */
6996
7006
  unshift(data, consume) {
6997
- return addData(this, data, consume || false, 0);
7007
+ return addData(this, data, consume || false, 0, false);
6998
7008
  }
6999
7009
  /**
7000
7010
  * Adds data to start of supplied data
@@ -7004,7 +7014,7 @@ export class biwriter {
7004
7014
  * @param {boolean} consume - Move current write position to end of data (default false)
7005
7015
  */
7006
7016
  prepend(data, consume) {
7007
- return addData(this, data, consume || false, 0);
7017
+ return addData(this, data, consume || false, 0, false);
7008
7018
  }
7009
7019
  /**
7010
7020
  * Adds data to end of supplied data
@@ -7014,7 +7024,7 @@ export class biwriter {
7014
7024
  * @param {boolean} consume - Move current write position to end of data (default false)
7015
7025
  */
7016
7026
  push(data, consume) {
7017
- return addData(this, data, consume || false, this.size);
7027
+ return addData(this, data, consume || false, this.size, false);
7018
7028
  }
7019
7029
  /**
7020
7030
  * Adds data to end of supplied data
@@ -7024,7 +7034,7 @@ export class biwriter {
7024
7034
  * @param {boolean} consume - Move current write position to end of data (default false)
7025
7035
  */
7026
7036
  append(data, consume) {
7027
- return addData(this, data, consume || false, this.size);
7037
+ return addData(this, data, consume || false, this.size, false);
7028
7038
  }
7029
7039
  //
7030
7040
  //finishing
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bireader",
3
- "version": "1.0.44",
3
+ "version": "1.0.46",
4
4
  "description": "Read and write data in binary",
5
5
  "module": "lib/esm/index.mjs",
6
6
  "main": "lib/cjs/index.js",