bare-buffer 3.3.1 → 3.4.0

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.
Files changed (3) hide show
  1. package/index.d.ts +16 -0
  2. package/index.js +284 -8
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -73,6 +73,9 @@ interface Buffer extends Uint8Array {
73
73
  readInt32BE(offset?: number): number
74
74
  readInt32LE(offset?: number): number
75
75
 
76
+ readIntBE(offset: number, byteLength: number): number
77
+ readIntLE(offset: number, byteLength: number): number
78
+
76
79
  readBigInt64BE(offset?: number): bigint
77
80
  readBigInt64LE(offset?: number): bigint
78
81
 
@@ -89,6 +92,11 @@ interface Buffer extends Uint8Array {
89
92
  readUInt32LE(offset?: number): number
90
93
  readUint32LE(offset?: number): number
91
94
 
95
+ readUIntBE(offset: number, byteLength: number): number
96
+ readUintBE(offset: number, byteLength: number): number
97
+ readUIntLE(offset: number, byteLength: number): number
98
+ readUintLE(offset: number, byteLength: number): number
99
+
92
100
  readBigUInt64BE(offset?: number): bigint
93
101
  readBigUint64BE(offset?: number): bigint
94
102
  readBigUInt64LE(offset?: number): bigint
@@ -117,6 +125,9 @@ interface Buffer extends Uint8Array {
117
125
  writeInt32BE(value: number, offset?: number): number
118
126
  writeInt32LE(value: number, offset?: number): number
119
127
 
128
+ writeIntBE(value: number, offset: number, byteLength: number): number
129
+ writeIntLE(value: number, offset: number, byteLength: number): number
130
+
120
131
  writeBigInt64BE(value: bigint, offset?: number): number
121
132
  writeBigInt64LE(value: bigint, offset?: number): number
122
133
 
@@ -133,6 +144,11 @@ interface Buffer extends Uint8Array {
133
144
  writeUInt32LE(value: number, offset?: number): number
134
145
  writeUint32LE(value: number, offset?: number): number
135
146
 
147
+ writeUIntBE(value: number, offset: number, byteLength: number): number
148
+ writeUintBE(value: number, offset: number, byteLength: number): number
149
+ writeUIntLE(value: number, offset: number, byteLength: number): number
150
+ writeUintLE(value: number, offset: number, byteLength: number): number
151
+
136
152
  writeBigUint64BE(value: bigint, offset?: number): number
137
153
  writeBigUInt64BE(value: bigint, offset?: number): number
138
154
  writeBigUint64LE(value: bigint, offset?: number): number
package/index.js CHANGED
@@ -104,7 +104,6 @@ module.exports = exports = class Buffer extends Uint8Array {
104
104
  const source = this
105
105
 
106
106
  if (source === target) return true
107
-
108
107
  if (source.byteLength !== target.byteLength) return false
109
108
 
110
109
  return (
@@ -164,14 +163,13 @@ module.exports = exports = class Buffer extends Uint8Array {
164
163
 
165
164
  fill(value, offset = 0, end = this.byteLength, encoding = 'utf8') {
166
165
  if (typeof value === 'string') {
167
- // fill(string, encoding)
168
166
  if (typeof offset === 'string') {
167
+ // fill(string, encoding)
169
168
  encoding = offset
170
169
  offset = 0
171
170
  end = this.byteLength
172
-
173
- // fill(string, offset, encoding)
174
171
  } else if (typeof end === 'string') {
172
+ // fill(string, offset, encoding)
175
173
  encoding = end
176
174
  end = this.byteLength
177
175
  }
@@ -299,14 +297,13 @@ module.exports = exports = class Buffer extends Uint8Array {
299
297
  // write(string)
300
298
  if (arguments.length === 1) return utf8.write(this, string)
301
299
 
302
- // write(string, encoding)
303
300
  if (typeof offset === 'string') {
301
+ // write(string, encoding)
304
302
  encoding = offset
305
303
  offset = 0
306
304
  length = this.byteLength
307
-
308
- // write(string, offset, encoding)
309
305
  } else if (typeof length === 'string') {
306
+ // write(string, offset, encoding)
310
307
  encoding = length
311
308
  length = this.byteLength - offset
312
309
  }
@@ -376,6 +373,26 @@ module.exports = exports = class Buffer extends Uint8Array {
376
373
  return viewOf(this).getInt32(offset, true)
377
374
  }
378
375
 
376
+ readIntBE(offset, byteLength) {
377
+ if (byteLength === 6) return readInt48BE(viewOf(this), offset)
378
+ if (byteLength === 5) return readInt40BE(viewOf(this), offset)
379
+ if (byteLength === 3) return readInt24BE(viewOf(this), offset)
380
+ if (byteLength === 4) return this.readInt32BE(offset)
381
+ if (byteLength === 2) return this.readInt16BE(offset)
382
+ if (byteLength === 1) return this.readInt8(offset)
383
+ throw new RangeError(`Byte length must be between 1 and 6`)
384
+ }
385
+
386
+ readIntLE(offset, byteLength) {
387
+ if (byteLength === 6) return readInt48LE(viewOf(this), offset)
388
+ if (byteLength === 5) return readInt40LE(viewOf(this), offset)
389
+ if (byteLength === 3) return readInt24LE(viewOf(this), offset)
390
+ if (byteLength === 4) return this.readInt32LE(offset)
391
+ if (byteLength === 2) return this.readInt16LE(offset)
392
+ if (byteLength === 1) return this.readInt8(offset)
393
+ throw new RangeError(`Byte length must be between 1 and 6`)
394
+ }
395
+
379
396
  readUint8(offset = 0) {
380
397
  return viewOf(this).getUint8(offset)
381
398
  }
@@ -394,6 +411,26 @@ module.exports = exports = class Buffer extends Uint8Array {
394
411
  return viewOf(this).getUint32(offset, true)
395
412
  }
396
413
 
414
+ readUintBE(offset, byteLength) {
415
+ if (byteLength === 6) return readUint48BE(viewOf(this), offset)
416
+ if (byteLength === 5) return readUint40BE(viewOf(this), offset)
417
+ if (byteLength === 3) return readUint24BE(viewOf(this), offset)
418
+ if (byteLength === 4) return this.readUint32BE(offset)
419
+ if (byteLength === 2) return this.readUint16BE(offset)
420
+ if (byteLength === 1) return this.readUint8(offset)
421
+ throw new RangeError(`Byte length must be between 1 and 6`)
422
+ }
423
+
424
+ readUintLE(offset, byteLength) {
425
+ if (byteLength === 6) return readUint48LE(viewOf(this), offset)
426
+ if (byteLength === 5) return readUint40LE(viewOf(this), offset)
427
+ if (byteLength === 3) return readUint24LE(viewOf(this), offset)
428
+ if (byteLength === 4) return this.readUint32LE(offset)
429
+ if (byteLength === 2) return this.readUint16LE(offset)
430
+ if (byteLength === 1) return this.readUint8(offset)
431
+ throw new RangeError(`Byte length must be between 1 and 6`)
432
+ }
433
+
397
434
  readBigUInt64BE(...args) {
398
435
  return this.readBigUint64BE(...args)
399
436
  }
@@ -419,6 +456,13 @@ module.exports = exports = class Buffer extends Uint8Array {
419
456
  return this.readUint32LE(...args)
420
457
  }
421
458
 
459
+ readUIntBE(...args) {
460
+ return this.readUintBE(...args)
461
+ }
462
+ readUIntLE(...args) {
463
+ return this.readUintLE(...args)
464
+ }
465
+
422
466
  writeBigInt64BE(value, offset = 0) {
423
467
  viewOf(this).setBigInt64(offset, value, false)
424
468
  return offset + 8
@@ -478,6 +522,26 @@ module.exports = exports = class Buffer extends Uint8Array {
478
522
  return offset + 4
479
523
  }
480
524
 
525
+ writeIntBE(value, offset, byteLength) {
526
+ if (byteLength === 6) return writeInt48BE(value, viewOf(this), offset)
527
+ if (byteLength === 5) return writeInt40BE(value, viewOf(this), offset)
528
+ if (byteLength === 3) return writeInt24BE(value, viewOf(this), offset)
529
+ if (byteLength === 4) return this.writeInt32BE(value, offset)
530
+ if (byteLength === 2) return this.writeInt16BE(value, offset)
531
+ if (byteLength === 1) return this.writeInt8(value, offset)
532
+ throw new RangeError(`Byte length must be between 1 and 6`)
533
+ }
534
+
535
+ writeIntLE(value, offset, byteLength) {
536
+ if (byteLength === 6) return writeInt48LE(value, viewOf(this), offset)
537
+ if (byteLength === 5) return writeInt40LE(value, viewOf(this), offset)
538
+ if (byteLength === 3) return writeInt24LE(value, viewOf(this), offset)
539
+ if (byteLength === 4) return this.writeInt32LE(value, offset)
540
+ if (byteLength === 2) return this.writeInt16LE(value, offset)
541
+ if (byteLength === 1) return this.writeInt8(value, offset)
542
+ throw new RangeError(`Byte length must be between 1 and 6`)
543
+ }
544
+
481
545
  writeUint8(value, offset = 0) {
482
546
  viewOf(this).setUint8(offset, value, true)
483
547
  return offset + 1
@@ -501,6 +565,26 @@ module.exports = exports = class Buffer extends Uint8Array {
501
565
  return offset + 4
502
566
  }
503
567
 
568
+ writeUintBE(value, offset, byteLength) {
569
+ if (byteLength === 6) return writeUint48BE(value, viewOf(this), offset)
570
+ if (byteLength === 5) return writeUint40BE(value, viewOf(this), offset)
571
+ if (byteLength === 3) return writeUint24BE(value, viewOf(this), offset)
572
+ if (byteLength === 4) return this.writeUint32BE(value, offset)
573
+ if (byteLength === 2) return this.writeUint16BE(value, offset)
574
+ if (byteLength === 1) return this.writeUint8(value, offset)
575
+ throw new RangeError(`Byte length must be between 1 and 6`)
576
+ }
577
+
578
+ writeUintLE(value, offset, byteLength) {
579
+ if (byteLength === 6) return writeUint48LE(value, viewOf(this), offset)
580
+ if (byteLength === 5) return writeUint40LE(value, viewOf(this), offset)
581
+ if (byteLength === 3) return writeUint24LE(value, viewOf(this), offset)
582
+ if (byteLength === 4) return this.writeUint32LE(value, offset)
583
+ if (byteLength === 2) return this.writeUint16LE(value, offset)
584
+ if (byteLength === 1) return this.writeUint8(value, offset)
585
+ throw new RangeError(`Byte length must be between 1 and 6`)
586
+ }
587
+
504
588
  writeBigUInt64BE(...args) {
505
589
  return this.writeBigUint64BE(...args)
506
590
  }
@@ -525,6 +609,13 @@ module.exports = exports = class Buffer extends Uint8Array {
525
609
  writeUInt32LE(...args) {
526
610
  return this.writeUint32LE(...args)
527
611
  }
612
+
613
+ writeUIntBE(...args) {
614
+ return this.writeUintBE(...args)
615
+ }
616
+ writeUIntLE(...args) {
617
+ return this.writeUintLE(...args)
618
+ }
528
619
  }
529
620
 
530
621
  const Buffer = exports
@@ -630,7 +721,6 @@ exports.concat = function concat(buffers, length) {
630
721
  }
631
722
 
632
723
  result.set(buffer, offset)
633
-
634
724
  offset += buffer.byteLength
635
725
  }
636
726
 
@@ -752,3 +842,189 @@ exports.btoa = function btoa(data) {
752
842
 
753
843
  return Buffer.from(data, 'latin1').toString('base64')
754
844
  }
845
+
846
+ function readInt48BE(view, offset) {
847
+ const hi = view.getUint16(offset, false)
848
+ const lo = view.getUint32(offset + 2, false)
849
+ let value = lo + hi * 0x100000000
850
+ if (hi & 0x8000) value -= 0x1000000000000
851
+ return value
852
+ }
853
+
854
+ function readInt48LE(view, offset) {
855
+ const lo = view.getUint32(offset, true)
856
+ const hi = view.getUint16(offset + 4, true)
857
+ let value = lo + hi * 0x100000000
858
+ if (hi & 0x8000) value -= 0x1000000000000
859
+ return value
860
+ }
861
+
862
+ function readInt40BE(view, offset) {
863
+ const hi = view.getUint8(offset)
864
+ const lo = view.getUint32(offset + 1, false)
865
+ let value = lo + hi * 0x100000000
866
+ if (hi & 0x80) value -= 0x10000000000
867
+ return value
868
+ }
869
+
870
+ function readInt40LE(view, offset) {
871
+ const lo = view.getUint32(offset, true)
872
+ const hi = view.getUint8(offset + 4)
873
+ let value = lo + hi * 0x100000000
874
+ if (hi & 0x80) value -= 0x10000000000
875
+ return value
876
+ }
877
+
878
+ function readInt24BE(view, offset) {
879
+ const value =
880
+ (view.getUint8(offset) << 16) |
881
+ (view.getUint8(offset + 1) << 8) |
882
+ view.getUint8(offset + 2)
883
+ return value & 0x800000 ? value - 0x1000000 : value
884
+ }
885
+
886
+ function readInt24LE(view, offset) {
887
+ const value =
888
+ view.getUint8(offset) |
889
+ (view.getUint8(offset + 1) << 8) |
890
+ (view.getUint8(offset + 2) << 16)
891
+ return value & 0x800000 ? value - 0x1000000 : value
892
+ }
893
+
894
+ function readUint48BE(view, offset) {
895
+ const hi = view.getUint16(offset, false)
896
+ const lo = view.getUint32(offset + 2, false)
897
+ return lo + hi * 0x100000000
898
+ }
899
+
900
+ function readUint48LE(view, offset) {
901
+ const lo = view.getUint32(offset, true)
902
+ const hi = view.getUint16(offset + 4, true)
903
+ return lo + hi * 0x100000000
904
+ }
905
+
906
+ function readUint40BE(view, offset) {
907
+ const hi = view.getUint8(offset)
908
+ const lo = view.getUint32(offset + 1, false)
909
+ return lo + hi * 0x100000000
910
+ }
911
+
912
+ function readUint40LE(view, offset) {
913
+ const lo = view.getUint32(offset, true)
914
+ const hi = view.getUint8(offset + 4)
915
+ return lo + hi * 0x100000000
916
+ }
917
+
918
+ function readUint24BE(view, offset) {
919
+ return (
920
+ (view.getUint8(offset) << 16) |
921
+ (view.getUint8(offset + 1) << 8) |
922
+ view.getUint8(offset + 2)
923
+ )
924
+ }
925
+
926
+ function readUint24LE(view, offset) {
927
+ return (
928
+ view.getUint8(offset) |
929
+ (view.getUint8(offset + 1) << 8) |
930
+ (view.getUint8(offset + 2) << 16)
931
+ )
932
+ }
933
+
934
+ function writeInt48BE(value, view, offset) {
935
+ if (value < 0) value += 0x1000000000000
936
+ const hi = Math.floor(value / 0x100000000)
937
+ const lo = value >>> 0
938
+ view.setUint16(offset, hi, false)
939
+ view.setUint32(offset + 2, lo, false)
940
+ return offset + 6
941
+ }
942
+
943
+ function writeInt48LE(value, view, offset) {
944
+ if (value < 0) value += 0x1000000000000
945
+ const hi = Math.floor(value / 0x100000000)
946
+ const lo = value >>> 0
947
+ view.setUint32(offset, lo, true)
948
+ view.setUint16(offset + 4, hi, true)
949
+ return offset + 6
950
+ }
951
+
952
+ function writeInt40BE(value, view, offset) {
953
+ if (value < 0) value += 0x10000000000
954
+ const hi = Math.floor(value / 0x100000000)
955
+ const lo = value >>> 0
956
+ view.setUint8(offset, hi)
957
+ view.setUint32(offset + 1, lo, false)
958
+ return offset + 5
959
+ }
960
+
961
+ function writeInt40LE(value, view, offset) {
962
+ if (value < 0) value += 0x10000000000
963
+ const hi = Math.floor(value / 0x100000000)
964
+ const lo = value >>> 0
965
+ view.setUint32(offset, lo, true)
966
+ view.setUint8(offset + 4, hi)
967
+ return offset + 5
968
+ }
969
+
970
+ function writeInt24BE(value, view, offset) {
971
+ if (value < 0) value += 0x1000000
972
+ view.setUint8(offset, (value >> 16) & 0xff)
973
+ view.setUint8(offset + 1, (value >> 8) & 0xff)
974
+ view.setUint8(offset + 2, value & 0xff)
975
+ return offset + 3
976
+ }
977
+
978
+ function writeInt24LE(value, view, offset) {
979
+ if (value < 0) value += 0x1000000
980
+ view.setUint8(offset, value & 0xff)
981
+ view.setUint8(offset + 1, (value >> 8) & 0xff)
982
+ view.setUint8(offset + 2, (value >> 16) & 0xff)
983
+ return offset + 3
984
+ }
985
+
986
+ function writeUint48BE(value, view, offset) {
987
+ const hi = Math.floor(value / 0x100000000)
988
+ const lo = value >>> 0
989
+ view.setUint16(offset, hi, false)
990
+ view.setUint32(offset + 2, lo, false)
991
+ return offset + 6
992
+ }
993
+
994
+ function writeUint48LE(value, view, offset) {
995
+ const hi = Math.floor(value / 0x100000000)
996
+ const lo = value >>> 0
997
+ view.setUint32(offset, lo, true)
998
+ view.setUint16(offset + 4, hi, true)
999
+ return offset + 6
1000
+ }
1001
+
1002
+ function writeUint40BE(value, view, offset) {
1003
+ const hi = Math.floor(value / 0x100000000)
1004
+ const lo = value >>> 0
1005
+ view.setUint8(offset, hi)
1006
+ view.setUint32(offset + 1, lo, false)
1007
+ return offset + 5
1008
+ }
1009
+
1010
+ function writeUint40LE(value, view, offset) {
1011
+ const hi = Math.floor(value / 0x100000000)
1012
+ const lo = value >>> 0
1013
+ view.setUint32(offset, lo, true)
1014
+ view.setUint8(offset + 4, hi)
1015
+ return offset + 5
1016
+ }
1017
+
1018
+ function writeUint24BE(value, view, offset) {
1019
+ view.setUint8(offset, (value >> 16) & 0xff)
1020
+ view.setUint8(offset + 1, (value >> 8) & 0xff)
1021
+ view.setUint8(offset + 2, value & 0xff)
1022
+ return offset + 3
1023
+ }
1024
+
1025
+ function writeUint24LE(value, view, offset) {
1026
+ view.setUint8(offset, value & 0xff)
1027
+ view.setUint8(offset + 1, (value >> 8) & 0xff)
1028
+ view.setUint8(offset + 2, (value >> 16) & 0xff)
1029
+ return offset + 3
1030
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-buffer",
3
- "version": "3.3.1",
3
+ "version": "3.4.0",
4
4
  "description": "Native buffers for JavaScript",
5
5
  "exports": {
6
6
  "./package": "./package.json",