/
index.js
1056 lines (990 loc) · 27.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {Buffer} from 'buffer'
import {Transform} from 'stream'
const td = new TextDecoder('utf8', {fatal: true, ignoreBOM: true})
/**
* @typedef {object} NoFilterOptions
* @property {string|Buffer} [input=null] Input source data.
* @property {BufferEncoding} [inputEncoding=null] Encoding name for input,
* ignored if input is not a String.
* @property {number} [highWaterMark=16384] The maximum number of bytes to
* store in the internal buffer before ceasing to read from the underlying
* resource. Default=16kb, or 16 for objectMode streams.
* @property {BufferEncoding} [encoding=null] If specified, then buffers
* will be decoded to strings using the specified encoding.
* @property {boolean} [objectMode=false] Whether this stream should behave
* as a stream of objects. Meaning that stream.read(n) returns a single
* value instead of a Buffer of size n.
* @property {boolean} [decodeStrings=true] Whether or not to decode
* strings into Buffers before passing them to _write().
* @property {boolean} [watchPipe=true] Whether to watch for 'pipe' events,
* setting this stream's objectMode based on the objectMode of the input
* stream.
* @property {boolean} [readError=false] If true, when a read() underflows,
* throw an error.
* @property {boolean} [allowHalfOpen=true] If set to false, then the
* stream will automatically end the writable side when the readable side
* ends.
* @property {boolean} [autoDestroy=true] Whether this stream should
* automatically call .destroy() on itself after ending.
* @property {BufferEncoding} [defaultEncoding='utf8'] The default encoding
* that is used when no encoding is specified as an argument to
* stream.write().
* @property {boolean} [emitClose=true] Whether or not the stream should
* emit 'close' after it has been destroyed.
* @property {number} [readableHighWaterMark] Sets highWaterMark for the
* readable side of the stream. Has no effect if highWaterMark is provided.
* @property {boolean} [readableObjectMode=false] Sets objectMode for
* readable side of the stream. Has no effect if objectMode is true.
* @property {number} [writableHighWaterMark] Sets highWaterMark for the
* writable side of the stream. Has no effect if highWaterMark is provided.
* @property {boolean} [writableObjectMode=false] Sets objectMode for
* writable side of the stream. Has no effect if objectMode is true.
*/
/**
* NoFilter stream. Can be used to sink or source data to and from
* other node streams. Implemented as the "identity" Transform stream
* (hence the name), but allows for inspecting data that is in-flight.
*
* Allows passing in source data (input, inputEncoding) at creation
* time. Source data can also be passed in the options object.
*
* @example <caption>source and sink</caption>
* const source = new NoFilter('Zm9v', 'base64')
* source.pipe(process.stdout)
* const sink = new Nofilter()
* // NOTE: 'finish' fires when the input is done writing
* sink.on('finish', () => console.log(n.toString('base64')))
* process.stdin.pipe(sink)
*/
class NoFilter extends Transform {
/**
* Create an instance of NoFilter.
*
* @param {string|Buffer|BufferEncoding|NoFilterOptions} [input] Source data.
* @param {BufferEncoding|NoFilterOptions} [inputEncoding] Encoding
* name for input, ignored if input is not a String.
* @param {NoFilterOptions} [options] Other options.
*/
constructor(input, inputEncoding, options = {}) {
let inp = null
let inpE = /** @type {BufferEncoding?} */ (null)
switch (typeof input) {
case 'object':
if (Buffer.isBuffer(input)) {
inp = input
} else if (input) {
options = input
}
break
case 'string':
inp = input
break
case 'undefined':
break
default:
throw new TypeError('Invalid input')
}
switch (typeof inputEncoding) {
case 'object':
if (inputEncoding) {
options = inputEncoding
}
break
case 'string':
inpE = /** @type {BufferEncoding} */ (inputEncoding)
break
case 'undefined':
break
default:
throw new TypeError('Invalid inputEncoding')
}
if (!options || typeof options !== 'object') {
throw new TypeError('Invalid options')
}
if (inp == null) {
inp = options.input
}
if (inpE == null) {
inpE = options.inputEncoding
}
delete options.input
delete options.inputEncoding
const watchPipe = options.watchPipe == null ? true : options.watchPipe
delete options.watchPipe
const readError = Boolean(options.readError)
delete options.readError
super(options)
this.readError = readError
if (watchPipe) {
this.on('pipe', readable => {
// @ts-ignore: TS2339 (using internal interface)
const om = readable._readableState.objectMode
// @ts-ignore: TS2339 (using internal interface)
if ((this.length > 0) && (om !== this._readableState.objectMode)) {
throw new Error(
'Do not switch objectMode in the middle of the stream'
)
}
// @ts-ignore: TS2339 (using internal interface)
this._readableState.objectMode = om
// @ts-ignore: TS2339 (using internal interface)
this._writableState.objectMode = om
})
}
if (inp != null) {
this.end(inp, inpE)
}
}
/**
* Is the given object a {NoFilter}?
*
* @param {object} obj The object to test.
* @returns {boolean} True if obj is a NoFilter.
*/
static isNoFilter(obj) {
return obj instanceof this
}
/**
* The same as nf1.compare(nf2). Useful for sorting an Array of NoFilters.
*
* @param {NoFilter} nf1 The first object to compare.
* @param {NoFilter} nf2 The second object to compare.
* @returns {number} - -1, 0, 1 for less, equal, greater.
* @throws {TypeError} Arguments not NoFilter instances.
* @example
* const arr = [new NoFilter('1234'), new NoFilter('0123')]
* arr.sort(NoFilter.compare)
*/
static compare(nf1, nf2) {
if (!(nf1 instanceof this)) {
throw new TypeError('Arguments must be NoFilters')
}
if (nf1 === nf2) {
return 0
}
return nf1.compare(nf2)
}
/**
* Returns a buffer which is the result of concatenating all the
* NoFilters in the list together. If the list has no items, or if
* the totalLength is 0, then it returns a zero-length buffer.
*
* If length is not provided, it is read from the buffers in the
* list. However, this adds an additional loop to the function, so
* it is faster to provide the length explicitly if you already know it.
*
* @param {Array<NoFilter>} list Inputs. Must not be all either in object
* mode, or all not in object mode.
* @param {number} [length=null] Number of bytes or objects to read.
* @returns {Buffer|Array} The concatenated values as an array if in object
* mode, otherwise a Buffer.
* @throws {TypeError} List not array of NoFilters.
*/
static concat(list, length) {
if (!Array.isArray(list)) {
throw new TypeError('list argument must be an Array of NoFilters')
}
if ((list.length === 0) || (length === 0)) {
return Buffer.alloc(0)
}
if ((length == null)) {
length = list.reduce((tot, nf) => {
if (!(nf instanceof NoFilter)) {
throw new TypeError('list argument must be an Array of NoFilters')
}
return tot + nf.length
}, 0)
}
let allBufs = true
let allObjs = true
const bufs = list.map(nf => {
if (!(nf instanceof NoFilter)) {
throw new TypeError('list argument must be an Array of NoFilters')
}
const buf = nf.slice()
if (Buffer.isBuffer(buf)) {
allObjs = false
} else {
allBufs = false
}
return buf
})
if (allBufs) {
// @ts-ignore: TS2322, tsc can't see the type checking above
return Buffer.concat(bufs, length)
}
if (allObjs) {
return [].concat(...bufs).slice(0, length)
}
// TODO: maybe coalesce buffers, counting bytes, and flatten in arrays
// counting objects? I can't imagine why that would be useful.
throw new Error('Concatenating mixed object and byte streams not supported')
}
/**
* @ignore
*/
_transform(chunk, encoding, callback) {
// @ts-ignore: TS2339 (using internal interface)
if (!this._readableState.objectMode && !Buffer.isBuffer(chunk)) {
chunk = Buffer.from(chunk, encoding)
}
this.push(chunk)
callback()
}
/**
* @returns {Buffer[]} The current internal buffers. They are layed out
* end to end.
* @ignore
*/
_bufArray() {
// @ts-ignore: TS2339 (using internal interface)
let bufs = this._readableState.buffer
// HACK: replace with something else one day. This is what I get for
// relying on internals.
if (!Array.isArray(bufs)) {
let b = bufs.head
bufs = []
while (b != null) {
bufs.push(b.data)
b = b.next
}
}
return bufs
}
/**
* Pulls some data out of the internal buffer and returns it.
* If there is no data available, then it will return null.
*
* If you pass in a size argument, then it will return that many bytes. If
* size bytes are not available, then it will return null, unless we've
* ended, in which case it will return the data remaining in the buffer.
*
* If you do not specify a size argument, then it will return all the data in
* the internal buffer.
*
* @param {number} [size=null] Number of bytes to read.
* @returns {string|Buffer|null} If no data or not enough data, null. If
* decoding output a string, otherwise a Buffer.
* @throws Error If readError is true and there was underflow.
* @fires NoFilter#read When read from.
*/
read(size) {
const buf = super.read(size)
if (buf != null) {
/**
* Read event. Fired whenever anything is read from the stream.
*
* @param {Buffer|string|object} buf What was read.
* @event NoFilter#read
*/
this.emit('read', buf)
if (this.readError && (buf.length < size)) {
throw new Error(`Read ${buf.length}, wanted ${size}`)
}
} else if (this.readError) {
throw new Error(`No data available, wanted ${size}`)
}
return buf
}
/**
* Read the full number of bytes asked for, no matter how long it takes.
* Fail if an error occurs in the meantime, or if the stream finishes before
* enough data is available.
*
* Note: This function won't work fully correctly if you are using
* stream-browserify (for example, on the Web).
*
* @param {number} size The number of bytes to read.
* @returns {Promise<string|Buffer>} A promise for the data read.
*/
readFull(size) {
let onReadable = null
let onFinish = null
let onError = null
return new Promise((resolve, reject) => {
if (this.length >= size) {
resolve(this.read(size))
return
}
// Added in Node 12.19. This won't work with stream-browserify yet.
// If it's needed, file a bug, and I'll do a work-around.
if (this.writableFinished) {
// Already finished writing, so no more coming.
reject(new Error(`Stream finished before ${size} bytes were available`))
return
}
onReadable = chunk => {
if (this.length >= size) {
resolve(this.read(size))
}
}
onFinish = () => {
reject(new Error(`Stream finished before ${size} bytes were available`))
}
onError = reject
this.on('readable', onReadable)
this.on('error', onError)
this.on('finish', onFinish)
}).finally(() => {
if (onReadable) {
this.removeListener('readable', onReadable)
this.removeListener('error', onError)
this.removeListener('finish', onFinish)
}
})
}
/**
* Return a promise fulfilled with the full contents, after the 'finish'
* event fires. Errors on the stream cause the promise to be rejected.
*
* @param {Function} [cb=null] Finished/error callback used in *addition*
* to the promise.
* @returns {Promise<Buffer|string>} Fulfilled when complete.
*/
promise(cb) {
let done = false
return new Promise((resolve, reject) => {
this.on('finish', () => {
const data = this.read()
if ((cb != null) && !done) {
done = true
cb(null, data)
}
resolve(data)
})
this.on('error', er => {
if ((cb != null) && !done) {
done = true
cb(er)
}
reject(er)
})
})
}
/**
* Returns a number indicating whether this comes before or after or is the
* same as the other NoFilter in sort order.
*
* @param {NoFilter} other The other object to compare.
* @returns {number} - -1, 0, 1 for less, equal, greater.
* @throws {TypeError} Arguments must be NoFilters.
*/
compare(other) {
if (!(other instanceof NoFilter)) {
throw new TypeError('Arguments must be NoFilters')
}
if (this === other) {
return 0
}
const buf1 = this.slice()
const buf2 = other.slice()
// These will both be buffers because of the check above.
if (Buffer.isBuffer(buf1) && Buffer.isBuffer(buf2)) {
return buf1.compare(buf2)
}
throw new Error('Cannot compare streams in object mode')
}
/**
* Do these NoFilter's contain the same bytes? Doesn't work if either is
* in object mode.
*
* @param {NoFilter} other Other NoFilter to compare against.
* @returns {boolean} Equal?
*/
equals(other) {
return this.compare(other) === 0
}
/**
* Read bytes or objects without consuming them. Useful for diagnostics.
* Note: as a side-effect, concatenates multiple writes together into what
* looks like a single write, so that this concat doesn't have to happen
* multiple times when you're futzing with the same NoFilter.
*
* @param {number} [start=0] Beginning offset.
* @param {number} [end=length] Ending offset.
* @returns {Buffer|Array} If in object mode, an array of objects. Otherwise,
* concatenated array of contents.
*/
slice(start, end) {
// @ts-ignore: TS2339 (using internal interface)
if (this._readableState.objectMode) {
return this._bufArray().slice(start, end)
}
const bufs = this._bufArray()
switch (bufs.length) {
case 0: return Buffer.alloc(0)
case 1: return bufs[0].slice(start, end)
default: {
const b = Buffer.concat(bufs)
// TODO: store the concatented bufs back
// @_readableState.buffer = [b]
return b.slice(start, end)
}
}
}
/**
* Get a byte by offset. I didn't want to get into metaprogramming
* to give you the `NoFilter[0]` syntax.
*
* @param {number} index The byte to retrieve.
* @returns {number} 0-255.
*/
get(index) {
return this.slice()[index]
}
/**
* Return an object compatible with Buffer's toJSON implementation, so that
* round-tripping will produce a Buffer.
*
* @returns {string|Array|{type: 'Buffer',data: number[]}} If in object mode,
* the objects. Otherwise, JSON text.
* @example <caption>output for 'foo', not in object mode</caption>
* ({
* type: 'Buffer',
* data: [102, 111, 111],
* })
*/
toJSON() {
const b = this.slice()
if (Buffer.isBuffer(b)) {
return b.toJSON()
}
return b
}
/**
* Decodes and returns a string from buffer data encoded using the specified
* character set encoding. If encoding is undefined or null, then encoding
* defaults to 'utf8'. The start and end parameters default to 0 and
* NoFilter.length when undefined.
*
* @param {BufferEncoding} [encoding='utf8'] Which to use for decoding?
* @param {number} [start=0] Start offset.
* @param {number} [end=length] End offset.
* @returns {string} String version of the contents.
*/
toString(encoding, start, end) {
const buf = this.slice(start, end)
if (!Buffer.isBuffer(buf)) {
return JSON.stringify(buf)
}
if (!encoding || (encoding === 'utf8')) {
return td.decode(buf)
}
return buf.toString(encoding)
}
/**
* @ignore
*/
[Symbol.for('nodejs.util.inspect.custom')](depth, options) {
const bufs = this._bufArray()
const hex = bufs.map(b => {
if (Buffer.isBuffer(b)) {
return options.stylize(b.toString('hex'), 'string')
}
return JSON.stringify(b)
}).join(', ')
return `${this.constructor.name} [${hex}]`
}
/**
* Current readable length, in bytes.
*
* @returns {number} Length of the contents.
*/
get length() {
// @ts-ignore: TS2339 (using internal interface)
return this._readableState.length
}
/**
* Write a JavaScript BigInt to the stream. Negative numbers will be
* written as their 2's complement version.
*
* @param {bigint} val The value to write.
* @returns {boolean} True on success.
*/
writeBigInt(val) {
let str = val.toString(16)
if (val < 0) {
// Two's complement
// Note: str always starts with '-' here.
const sz = BigInt(Math.floor(str.length / 2))
const mask = BigInt(1) << (sz * BigInt(8))
val = mask + val
str = val.toString(16)
}
if (str.length % 2) {
str = `0${str}`
}
return this.push(Buffer.from(str, 'hex'))
}
/**
* Read a variable-sized JavaScript unsigned BigInt from the stream.
*
* @param {number} [len=null] Number of bytes to read or all remaining
* if null.
* @returns {bigint} A BigInt.
*/
readUBigInt(len) {
const b = this.read(len)
if (!Buffer.isBuffer(b)) {
return null
}
return BigInt(`0x${b.toString('hex')}`)
}
/**
* Read a variable-sized JavaScript signed BigInt from the stream in 2's
* complement format.
*
* @param {number} [len=null] Number of bytes to read or all remaining
* if null.
* @returns {bigint} A BigInt.
*/
readBigInt(len) {
const b = this.read(len)
if (!Buffer.isBuffer(b)) {
return null
}
let ret = BigInt(`0x${b.toString('hex')}`)
// Negative?
if (b[0] & 0x80) {
// Two's complement
const mask = BigInt(1) << (BigInt(b.length) * BigInt(8))
ret -= mask
}
return ret
}
/**
* Write an 8-bit unsigned integer to the stream. Adds 1 byte.
*
* @param {number} value 0..255.
* @returns {boolean} True on success.
*/
writeUInt8(value) {
const b = Buffer.from([value])
return this.push(b)
}
/**
* Write a little-endian 16-bit unsigned integer to the stream. Adds
* 2 bytes.
*
* @param {number} value 0..65535.
* @returns {boolean} True on success.
*/
writeUInt16LE(value) {
const b = Buffer.alloc(2)
b.writeUInt16LE(value)
return this.push(b)
}
/**
* Write a big-endian 16-bit unsigned integer to the stream. Adds
* 2 bytes.
*
* @param {number} value 0..65535.
* @returns {boolean} True on success.
*/
writeUInt16BE(value) {
const b = Buffer.alloc(2)
b.writeUInt16BE(value)
return this.push(b)
}
/**
* Write a little-endian 32-bit unsigned integer to the stream. Adds
* 4 bytes.
*
* @param {number} value 0..2**32-1.
* @returns {boolean} True on success.
*/
writeUInt32LE(value) {
const b = Buffer.alloc(4)
b.writeUInt32LE(value)
return this.push(b)
}
/**
* Write a big-endian 32-bit unsigned integer to the stream. Adds
* 4 bytes.
*
* @param {number} value 0..2**32-1.
* @returns {boolean} True on success.
*/
writeUInt32BE(value) {
const b = Buffer.alloc(4)
b.writeUInt32BE(value)
return this.push(b)
}
/**
* Write a signed 8-bit integer to the stream. Adds 1 byte.
*
* @param {number} value (-128)..127.
* @returns {boolean} True on success.
*/
writeInt8(value) {
const b = Buffer.from([value])
return this.push(b)
}
/**
* Write a signed little-endian 16-bit integer to the stream. Adds 2 bytes.
*
* @param {number} value (-32768)..32767.
* @returns {boolean} True on success.
*/
writeInt16LE(value) {
const b = Buffer.alloc(2)
b.writeUInt16LE(value)
return this.push(b)
}
/**
* Write a signed big-endian 16-bit integer to the stream. Adds 2 bytes.
*
* @param {number} value (-32768)..32767.
* @returns {boolean} True on success.
*/
writeInt16BE(value) {
const b = Buffer.alloc(2)
b.writeUInt16BE(value)
return this.push(b)
}
/**
* Write a signed little-endian 32-bit integer to the stream. Adds 4 bytes.
*
* @param {number} value (-2**31)..(2**31-1).
* @returns {boolean} True on success.
*/
writeInt32LE(value) {
const b = Buffer.alloc(4)
b.writeUInt32LE(value)
return this.push(b)
}
/**
* Write a signed big-endian 32-bit integer to the stream. Adds 4 bytes.
*
* @param {number} value (-2**31)..(2**31-1).
* @returns {boolean} True on success.
*/
writeInt32BE(value) {
const b = Buffer.alloc(4)
b.writeUInt32BE(value)
return this.push(b)
}
/**
* Write a little-endian 32-bit float to the stream. Adds 4 bytes.
*
* @param {number} value 32-bit float.
* @returns {boolean} True on success.
*/
writeFloatLE(value) {
const b = Buffer.alloc(4)
b.writeFloatLE(value)
return this.push(b)
}
/**
* Write a big-endian 32-bit float to the stream. Adds 4 bytes.
*
* @param {number} value 32-bit float.
* @returns {boolean} True on success.
*/
writeFloatBE(value) {
const b = Buffer.alloc(4)
b.writeFloatBE(value)
return this.push(b)
}
/**
* Write a little-endian 64-bit double to the stream. Adds 8 bytes.
*
* @param {number} value 64-bit float.
* @returns {boolean} True on success.
*/
writeDoubleLE(value) {
const b = Buffer.alloc(8)
b.writeDoubleLE(value)
return this.push(b)
}
/**
* Write a big-endian 64-bit float to the stream. Adds 8 bytes.
*
* @param {number} value 64-bit float.
* @returns {boolean} True on success.
*/
writeDoubleBE(value) {
const b = Buffer.alloc(8)
b.writeDoubleBE(value)
return this.push(b)
}
/**
* Write a signed little-endian 64-bit BigInt to the stream. Adds 8 bytes.
*
* @param {bigint} value BigInt.
* @returns {boolean} True on success.
*/
writeBigInt64LE(value) {
const b = Buffer.alloc(8)
b.writeBigInt64LE(value)
return this.push(b)
}
/**
* Write a signed big-endian 64-bit BigInt to the stream. Adds 8 bytes.
*
* @param {bigint} value BigInt.
* @returns {boolean} True on success.
*/
writeBigInt64BE(value) {
const b = Buffer.alloc(8)
b.writeBigInt64BE(value)
return this.push(b)
}
/**
* Write an unsigned little-endian 64-bit BigInt to the stream. Adds 8 bytes.
*
* @param {bigint} value Non-negative BigInt.
* @returns {boolean} True on success.
*/
writeBigUInt64LE(value) {
const b = Buffer.alloc(8)
b.writeBigUInt64LE(value)
return this.push(b)
}
/**
* Write an unsigned big-endian 64-bit BigInt to the stream. Adds 8 bytes.
*
* @param {bigint} value Non-negative BigInt.
* @returns {boolean} True on success.
*/
writeBigUInt64BE(value) {
const b = Buffer.alloc(8)
b.writeBigUInt64BE(value)
return this.push(b)
}
/**
* Read an unsigned 8-bit integer from the stream. Consumes 1 byte.
*
* @returns {number} Value read.
*/
readUInt8() {
const b = this.read(1)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readUInt8()
}
/**
* Read a little-endian unsigned 16-bit integer from the stream.
* Consumes 2 bytes.
*
* @returns {number} Value read.
*/
readUInt16LE() {
const b = this.read(2)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readUInt16LE()
}
/**
* Read a little-endian unsigned 16-bit integer from the stream.
* Consumes 2 bytes.
*
* @returns {number} Value read.
*/
readUInt16BE() {
const b = this.read(2)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readUInt16BE()
}
/**
* Read a little-endian unsigned 32-bit integer from the stream.
* Consumes 4 bytes.
*
* @returns {number} Value read.
*/
readUInt32LE() {
const b = this.read(4)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readUInt32LE()
}
/**
* Read a little-endian unsigned 16-bit integer from the stream.
* Consumes 4 bytes.
*
* @returns {number} Value read.
*/
readUInt32BE() {
const b = this.read(4)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readUInt32BE()
}
/**
* Read a signed 8-bit integer from the stream. Consumes 1 byte.
*
* @returns {number} Value read.
*/
readInt8() {
const b = this.read(1)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readInt8()
}
/**
* Read a little-endian signed 16-bit integer from the stream.
* Consumes 2 bytes.
*
* @returns {number} Value read.
*/
readInt16LE() {
const b = this.read(2)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readInt16LE()
}
/**
* Read a little-endian signed 16-bit integer from the stream.
* Consumes 2 bytes.
*
* @returns {number} Value read.
*/
readInt16BE() {
const b = this.read(2)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readInt16BE()
}
/**
* Read a little-endian signed 32-bit integer from the stream.
* Consumes 4 bytes.
*
* @returns {number} Value read.
*/
readInt32LE() {
const b = this.read(4)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readInt32LE()
}
/**
* Read a little-endian signed 16-bit integer from the stream.
* Consumes 4 bytes.
*
* @returns {number} Value read.
*/
readInt32BE() {
const b = this.read(4)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readInt32BE()
}
/**
* Read a 32-bit little-endian float from the stream.
* Consumes 4 bytes.
*
* @returns {number} Value read.
*/
readFloatLE() {
const b = this.read(4)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readFloatLE()
}
/**
* Read a 32-bit big-endian float from the stream.
* Consumes 4 bytes.
*
* @returns {number} Value read.
*/
readFloatBE() {
const b = this.read(4)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readFloatBE()
}
/**
* Read a 64-bit little-endian float from the stream.
* Consumes 8 bytes.
*
* @returns {number} Value read.
*/
readDoubleLE() {
const b = this.read(8)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readDoubleLE()
}
/**
* Read a 64-bit big-endian float from the stream.
* Consumes 8 bytes.
*
* @returns {number} Value read.
*/
readDoubleBE() {
const b = this.read(8)
if (!Buffer.isBuffer(b)) {
return null
}
return b.readDoubleBE()
}
/**
* Read a signed 64-bit little-endian BigInt from the stream.
* Consumes 8 bytes.
*