bl 5.0.0 → 6.0.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.
- package/.github/dependabot.yml +16 -0
- package/.github/workflows/test-and-release.yml +61 -0
- package/BufferList.d.ts +418 -0
- package/BufferList.js +4 -0
- package/CHANGELOG.md +37 -0
- package/README.md +2 -2
- package/index.d.ts +88 -0
- package/package.json +91 -5
- package/test/test.js +162 -83
- package/.travis.yml +0 -17
package/test/test.js
CHANGED
|
@@ -1,22 +1,49 @@
|
|
|
1
|
+
// @ts-check
|
|
1
2
|
'use strict'
|
|
2
3
|
|
|
3
4
|
const tape = require('tape')
|
|
4
5
|
const crypto = require('crypto')
|
|
5
6
|
const fs = require('fs')
|
|
6
7
|
const path = require('path')
|
|
7
|
-
const
|
|
8
|
+
const os = require('os')
|
|
9
|
+
const BufferListStream = require('../')
|
|
8
10
|
const { Buffer } = require('buffer')
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* This typedef allows us to add _bufs to the API without declaring it publicly on types.
|
|
14
|
+
* @typedef { BufferListStream & { _bufs?: Buffer[] }} BufferListStreamWithPrivate
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Just for typechecking in js
|
|
19
|
+
* @type { NodeJS.Process & { browser?: boolean }}
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const process = globalThis.process
|
|
23
|
+
|
|
24
|
+
/** @type {BufferEncoding[]} */
|
|
25
|
+
const encodings = ['ascii', 'utf8', 'utf-8', 'hex', 'binary', 'base64']
|
|
26
|
+
|
|
27
|
+
if (process.browser) {
|
|
28
|
+
encodings.push(
|
|
29
|
+
'ucs2',
|
|
30
|
+
'ucs-2',
|
|
31
|
+
'utf16le',
|
|
32
|
+
/**
|
|
33
|
+
* This alias is not in typescript typings for BufferEncoding. Still have to fix
|
|
34
|
+
* @see https://nodejs.org/api/buffer.html#buffers-and-character-encodings
|
|
35
|
+
*/
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
'utf-16le'
|
|
38
|
+
)
|
|
39
|
+
}
|
|
13
40
|
|
|
14
41
|
require('./indexOf')
|
|
15
42
|
require('./isBufferList')
|
|
16
43
|
require('./convert')
|
|
17
44
|
|
|
18
45
|
tape('single bytes from single buffer', function (t) {
|
|
19
|
-
const bl = new
|
|
46
|
+
const bl = new BufferListStream()
|
|
20
47
|
|
|
21
48
|
bl.append(Buffer.from('abcd'))
|
|
22
49
|
|
|
@@ -32,7 +59,7 @@ tape('single bytes from single buffer', function (t) {
|
|
|
32
59
|
})
|
|
33
60
|
|
|
34
61
|
tape('single bytes from multiple buffers', function (t) {
|
|
35
|
-
const bl = new
|
|
62
|
+
const bl = new BufferListStream()
|
|
36
63
|
|
|
37
64
|
bl.append(Buffer.from('abcd'))
|
|
38
65
|
bl.append(Buffer.from('efg'))
|
|
@@ -56,7 +83,7 @@ tape('single bytes from multiple buffers', function (t) {
|
|
|
56
83
|
})
|
|
57
84
|
|
|
58
85
|
tape('multi bytes from single buffer', function (t) {
|
|
59
|
-
const bl = new
|
|
86
|
+
const bl = new BufferListStream()
|
|
60
87
|
|
|
61
88
|
bl.append(Buffer.from('abcd'))
|
|
62
89
|
|
|
@@ -71,7 +98,7 @@ tape('multi bytes from single buffer', function (t) {
|
|
|
71
98
|
})
|
|
72
99
|
|
|
73
100
|
tape('multi bytes from single buffer (negative indexes)', function (t) {
|
|
74
|
-
const bl = new
|
|
101
|
+
const bl = new BufferListStream()
|
|
75
102
|
|
|
76
103
|
bl.append(Buffer.from('buffer'))
|
|
77
104
|
|
|
@@ -85,7 +112,7 @@ tape('multi bytes from single buffer (negative indexes)', function (t) {
|
|
|
85
112
|
})
|
|
86
113
|
|
|
87
114
|
tape('multiple bytes from multiple buffers', function (t) {
|
|
88
|
-
const bl = new
|
|
115
|
+
const bl = new BufferListStream()
|
|
89
116
|
|
|
90
117
|
bl.append(Buffer.from('abcd'))
|
|
91
118
|
bl.append(Buffer.from('efg'))
|
|
@@ -105,10 +132,10 @@ tape('multiple bytes from multiple buffers', function (t) {
|
|
|
105
132
|
})
|
|
106
133
|
|
|
107
134
|
tape('multiple bytes from multiple buffer lists', function (t) {
|
|
108
|
-
const bl = new
|
|
135
|
+
const bl = new BufferListStream()
|
|
109
136
|
|
|
110
|
-
bl.append(new
|
|
111
|
-
bl.append(new
|
|
137
|
+
bl.append(new BufferListStream([Buffer.from('abcd'), Buffer.from('efg')]))
|
|
138
|
+
bl.append(new BufferListStream([Buffer.from('hi'), Buffer.from('j')]))
|
|
112
139
|
|
|
113
140
|
t.equal(bl.length, 10)
|
|
114
141
|
|
|
@@ -124,17 +151,19 @@ tape('multiple bytes from multiple buffer lists', function (t) {
|
|
|
124
151
|
|
|
125
152
|
// same data as previous test, just using nested constructors
|
|
126
153
|
tape('multiple bytes from crazy nested buffer lists', function (t) {
|
|
127
|
-
const bl = new
|
|
128
|
-
|
|
129
|
-
bl.append(
|
|
130
|
-
new
|
|
131
|
-
new
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
154
|
+
const bl = new BufferListStream()
|
|
155
|
+
|
|
156
|
+
bl.append(
|
|
157
|
+
new BufferListStream([
|
|
158
|
+
new BufferListStream([
|
|
159
|
+
new BufferListStream(Buffer.from('abc')),
|
|
160
|
+
Buffer.from('d'),
|
|
161
|
+
new BufferListStream(Buffer.from('efg'))
|
|
162
|
+
]),
|
|
163
|
+
new BufferListStream([Buffer.from('hi')]),
|
|
164
|
+
new BufferListStream(Buffer.from('j'))
|
|
165
|
+
])
|
|
166
|
+
)
|
|
138
167
|
|
|
139
168
|
t.equal(bl.length, 10)
|
|
140
169
|
|
|
@@ -149,7 +178,7 @@ tape('multiple bytes from crazy nested buffer lists', function (t) {
|
|
|
149
178
|
})
|
|
150
179
|
|
|
151
180
|
tape('append accepts arrays of Buffers', function (t) {
|
|
152
|
-
const bl = new
|
|
181
|
+
const bl = new BufferListStream()
|
|
153
182
|
|
|
154
183
|
bl.append(Buffer.from('abc'))
|
|
155
184
|
bl.append([Buffer.from('def')])
|
|
@@ -162,7 +191,7 @@ tape('append accepts arrays of Buffers', function (t) {
|
|
|
162
191
|
})
|
|
163
192
|
|
|
164
193
|
tape('append accepts arrays of Uint8Arrays', function (t) {
|
|
165
|
-
const bl = new
|
|
194
|
+
const bl = new BufferListStream()
|
|
166
195
|
|
|
167
196
|
bl.append(new Uint8Array([97, 98, 99]))
|
|
168
197
|
bl.append([Uint8Array.from([100, 101, 102])])
|
|
@@ -175,12 +204,17 @@ tape('append accepts arrays of Uint8Arrays', function (t) {
|
|
|
175
204
|
})
|
|
176
205
|
|
|
177
206
|
tape('append accepts arrays of BufferLists', function (t) {
|
|
178
|
-
const bl = new
|
|
207
|
+
const bl = new BufferListStream()
|
|
179
208
|
|
|
180
209
|
bl.append(Buffer.from('abc'))
|
|
181
|
-
bl.append([new
|
|
182
|
-
bl.append(
|
|
183
|
-
|
|
210
|
+
bl.append([new BufferListStream('def')])
|
|
211
|
+
bl.append(
|
|
212
|
+
new BufferListStream([Buffer.from('ghi'), new BufferListStream('jkl')])
|
|
213
|
+
)
|
|
214
|
+
bl.append([
|
|
215
|
+
Buffer.from('mnop'),
|
|
216
|
+
new BufferListStream([Buffer.from('qrstu'), Buffer.from('vwxyz')])
|
|
217
|
+
])
|
|
184
218
|
t.equal(bl.length, 26)
|
|
185
219
|
t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
|
|
186
220
|
|
|
@@ -188,21 +222,26 @@ tape('append accepts arrays of BufferLists', function (t) {
|
|
|
188
222
|
})
|
|
189
223
|
|
|
190
224
|
tape('append chainable', function (t) {
|
|
191
|
-
const bl = new
|
|
225
|
+
const bl = new BufferListStream()
|
|
192
226
|
|
|
193
227
|
t.ok(bl.append(Buffer.from('abcd')) === bl)
|
|
194
228
|
t.ok(bl.append([Buffer.from('abcd')]) === bl)
|
|
195
|
-
t.ok(bl.append(new
|
|
196
|
-
t.ok(bl.append([new
|
|
229
|
+
t.ok(bl.append(new BufferListStream(Buffer.from('abcd'))) === bl)
|
|
230
|
+
t.ok(bl.append([new BufferListStream(Buffer.from('abcd'))]) === bl)
|
|
197
231
|
|
|
198
232
|
t.end()
|
|
199
233
|
})
|
|
200
234
|
|
|
201
235
|
tape('append chainable (test results)', function (t) {
|
|
202
|
-
const bl = new
|
|
203
|
-
.append([new
|
|
204
|
-
.append(
|
|
205
|
-
|
|
236
|
+
const bl = new BufferListStream('abc')
|
|
237
|
+
.append([new BufferListStream('def')])
|
|
238
|
+
.append(
|
|
239
|
+
new BufferListStream([Buffer.from('ghi'), new BufferListStream('jkl')])
|
|
240
|
+
)
|
|
241
|
+
.append([
|
|
242
|
+
Buffer.from('mnop'),
|
|
243
|
+
new BufferListStream([Buffer.from('qrstu'), Buffer.from('vwxyz')])
|
|
244
|
+
])
|
|
206
245
|
|
|
207
246
|
t.equal(bl.length, 26)
|
|
208
247
|
t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
|
|
@@ -211,7 +250,7 @@ tape('append chainable (test results)', function (t) {
|
|
|
211
250
|
})
|
|
212
251
|
|
|
213
252
|
tape('consuming from multiple buffers', function (t) {
|
|
214
|
-
const bl = new
|
|
253
|
+
const bl = new BufferListStream()
|
|
215
254
|
|
|
216
255
|
bl.append(Buffer.from('abcd'))
|
|
217
256
|
bl.append(Buffer.from('efg'))
|
|
@@ -246,7 +285,8 @@ tape('consuming from multiple buffers', function (t) {
|
|
|
246
285
|
})
|
|
247
286
|
|
|
248
287
|
tape('complete consumption', function (t) {
|
|
249
|
-
|
|
288
|
+
/** @type {BufferListStreamWithPrivate} */
|
|
289
|
+
const bl = new BufferListStream()
|
|
250
290
|
|
|
251
291
|
bl.append(Buffer.from('a'))
|
|
252
292
|
bl.append(Buffer.from('b'))
|
|
@@ -263,7 +303,7 @@ tape('test readUInt8 / readInt8', function (t) {
|
|
|
263
303
|
const buf1 = Buffer.alloc(1)
|
|
264
304
|
const buf2 = Buffer.alloc(3)
|
|
265
305
|
const buf3 = Buffer.alloc(3)
|
|
266
|
-
const bl = new
|
|
306
|
+
const bl = new BufferListStream()
|
|
267
307
|
|
|
268
308
|
buf1[0] = 0x1
|
|
269
309
|
buf2[1] = 0x3
|
|
@@ -292,7 +332,7 @@ tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t
|
|
|
292
332
|
const buf1 = Buffer.alloc(1)
|
|
293
333
|
const buf2 = Buffer.alloc(3)
|
|
294
334
|
const buf3 = Buffer.alloc(3)
|
|
295
|
-
const bl = new
|
|
335
|
+
const bl = new BufferListStream()
|
|
296
336
|
|
|
297
337
|
buf1[0] = 0x1
|
|
298
338
|
buf2[1] = 0x3
|
|
@@ -326,7 +366,7 @@ tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t
|
|
|
326
366
|
const buf1 = Buffer.alloc(1)
|
|
327
367
|
const buf2 = Buffer.alloc(3)
|
|
328
368
|
const buf3 = Buffer.alloc(3)
|
|
329
|
-
const bl = new
|
|
369
|
+
const bl = new BufferListStream()
|
|
330
370
|
|
|
331
371
|
buf1[0] = 0x1
|
|
332
372
|
buf2[1] = 0x3
|
|
@@ -348,11 +388,45 @@ tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t
|
|
|
348
388
|
t.end()
|
|
349
389
|
})
|
|
350
390
|
|
|
391
|
+
tape('test readBigUInt64LE / readBigUInt64BE / readBigInt64LE / readBigInt64BE', function (t) {
|
|
392
|
+
const buf1 = Buffer.alloc(1)
|
|
393
|
+
const buf2 = Buffer.alloc(3)
|
|
394
|
+
const buf3 = Buffer.alloc(2)
|
|
395
|
+
const buf4 = Buffer.alloc(5)
|
|
396
|
+
const bl = new BufferListStream()
|
|
397
|
+
|
|
398
|
+
buf1[0] = 0x05
|
|
399
|
+
buf2[0] = 0x07
|
|
400
|
+
|
|
401
|
+
buf2[1] = 0x03
|
|
402
|
+
buf2[2] = 0x04
|
|
403
|
+
buf3[0] = 0x23
|
|
404
|
+
buf3[1] = 0x42
|
|
405
|
+
buf4[0] = 0x00
|
|
406
|
+
buf4[1] = 0x01
|
|
407
|
+
buf4[2] = 0x02
|
|
408
|
+
buf4[3] = 0x03
|
|
409
|
+
|
|
410
|
+
buf4[4] = 0x04
|
|
411
|
+
|
|
412
|
+
bl.append(buf1)
|
|
413
|
+
bl.append(buf2)
|
|
414
|
+
bl.append(buf3)
|
|
415
|
+
bl.append(buf4)
|
|
416
|
+
|
|
417
|
+
t.equal(bl.readBigUInt64BE(2), 0x0304234200010203n)
|
|
418
|
+
t.equal(bl.readBigUInt64LE(2), 0x0302010042230403n)
|
|
419
|
+
t.equal(bl.readBigInt64BE(2), 0x0304234200010203n)
|
|
420
|
+
t.equal(bl.readBigInt64LE(2), 0x0302010042230403n)
|
|
421
|
+
|
|
422
|
+
t.end()
|
|
423
|
+
})
|
|
424
|
+
|
|
351
425
|
tape('test readUIntLE / readUIntBE / readIntLE / readIntBE', function (t) {
|
|
352
426
|
const buf1 = Buffer.alloc(1)
|
|
353
427
|
const buf2 = Buffer.alloc(3)
|
|
354
428
|
const buf3 = Buffer.alloc(3)
|
|
355
|
-
const bl = new
|
|
429
|
+
const bl = new BufferListStream()
|
|
356
430
|
|
|
357
431
|
buf2[0] = 0x2
|
|
358
432
|
buf2[1] = 0x3
|
|
@@ -397,7 +471,7 @@ tape('test readFloatLE / readFloatBE', function (t) {
|
|
|
397
471
|
const buf1 = Buffer.alloc(1)
|
|
398
472
|
const buf2 = Buffer.alloc(3)
|
|
399
473
|
const buf3 = Buffer.alloc(3)
|
|
400
|
-
const bl = new
|
|
474
|
+
const bl = new BufferListStream()
|
|
401
475
|
|
|
402
476
|
buf1[0] = 0x01
|
|
403
477
|
buf2[1] = 0x00
|
|
@@ -422,7 +496,7 @@ tape('test readDoubleLE / readDoubleBE', function (t) {
|
|
|
422
496
|
const buf1 = Buffer.alloc(1)
|
|
423
497
|
const buf2 = Buffer.alloc(3)
|
|
424
498
|
const buf3 = Buffer.alloc(10)
|
|
425
|
-
const bl = new
|
|
499
|
+
const bl = new BufferListStream()
|
|
426
500
|
|
|
427
501
|
buf1[0] = 0x01
|
|
428
502
|
buf2[1] = 0x55
|
|
@@ -448,7 +522,7 @@ tape('test readDoubleLE / readDoubleBE', function (t) {
|
|
|
448
522
|
})
|
|
449
523
|
|
|
450
524
|
tape('test toString', function (t) {
|
|
451
|
-
const bl = new
|
|
525
|
+
const bl = new BufferListStream()
|
|
452
526
|
|
|
453
527
|
bl.append(Buffer.from('abcd'))
|
|
454
528
|
bl.append(Buffer.from('efg'))
|
|
@@ -465,7 +539,7 @@ tape('test toString', function (t) {
|
|
|
465
539
|
})
|
|
466
540
|
|
|
467
541
|
tape('test toString encoding', function (t) {
|
|
468
|
-
const bl = new
|
|
542
|
+
const bl = new BufferListStream()
|
|
469
543
|
const b = Buffer.from('abcdefghij\xff\x00')
|
|
470
544
|
|
|
471
545
|
bl.append(Buffer.from('abcd'))
|
|
@@ -485,7 +559,7 @@ tape('uninitialized memory', function (t) {
|
|
|
485
559
|
const secret = crypto.randomBytes(256)
|
|
486
560
|
for (let i = 0; i < 1e6; i++) {
|
|
487
561
|
const clone = Buffer.from(secret)
|
|
488
|
-
const bl = new
|
|
562
|
+
const bl = new BufferListStream()
|
|
489
563
|
bl.append(Buffer.from('a'))
|
|
490
564
|
bl.consume(-1024)
|
|
491
565
|
const buf = bl.slice(1)
|
|
@@ -500,17 +574,17 @@ tape('uninitialized memory', function (t) {
|
|
|
500
574
|
!process.browser && tape('test stream', function (t) {
|
|
501
575
|
const random = crypto.randomBytes(65534)
|
|
502
576
|
|
|
503
|
-
const bl = new
|
|
577
|
+
const bl = new BufferListStream((err, buf) => {
|
|
504
578
|
t.ok(Buffer.isBuffer(buf))
|
|
505
579
|
t.ok(err === null)
|
|
506
580
|
t.ok(random.equals(bl.slice()))
|
|
507
581
|
t.ok(random.equals(buf.slice()))
|
|
508
582
|
|
|
509
|
-
bl.pipe(fs.createWriteStream('
|
|
583
|
+
bl.pipe(fs.createWriteStream(path.join(os.tmpdir(), 'bl_test_rnd_out.dat')))
|
|
510
584
|
.on('close', function () {
|
|
511
585
|
const rndhash = crypto.createHash('md5').update(random).digest('hex')
|
|
512
586
|
const md5sum = crypto.createHash('md5')
|
|
513
|
-
const s = fs.createReadStream('
|
|
587
|
+
const s = fs.createReadStream(path.join(os.tmpdir(), 'bl_test_rnd_out.dat'))
|
|
514
588
|
|
|
515
589
|
s.on('data', md5sum.update.bind(md5sum))
|
|
516
590
|
s.on('end', function () {
|
|
@@ -520,24 +594,24 @@ tape('uninitialized memory', function (t) {
|
|
|
520
594
|
})
|
|
521
595
|
})
|
|
522
596
|
|
|
523
|
-
fs.writeFileSync('
|
|
524
|
-
fs.createReadStream('
|
|
597
|
+
fs.writeFileSync(path.join(os.tmpdir(), 'bl_test_rnd.dat'), random)
|
|
598
|
+
fs.createReadStream(path.join(os.tmpdir(), 'bl_test_rnd.dat')).pipe(bl)
|
|
525
599
|
})
|
|
526
600
|
|
|
527
601
|
tape('instantiation with Buffer', function (t) {
|
|
528
602
|
const buf = crypto.randomBytes(1024)
|
|
529
603
|
const buf2 = crypto.randomBytes(1024)
|
|
530
|
-
let b =
|
|
604
|
+
let b = BufferListStream(buf)
|
|
531
605
|
|
|
532
606
|
t.equal(buf.toString('hex'), b.slice().toString('hex'), 'same buffer')
|
|
533
|
-
b =
|
|
607
|
+
b = BufferListStream([buf, buf2])
|
|
534
608
|
t.equal(b.slice().toString('hex'), Buffer.concat([buf, buf2]).toString('hex'), 'same buffer')
|
|
535
609
|
|
|
536
610
|
t.end()
|
|
537
611
|
})
|
|
538
612
|
|
|
539
613
|
tape('test String appendage', function (t) {
|
|
540
|
-
const bl = new
|
|
614
|
+
const bl = new BufferListStream()
|
|
541
615
|
const b = Buffer.from('abcdefghij\xff\x00')
|
|
542
616
|
|
|
543
617
|
bl.append('abcd')
|
|
@@ -554,7 +628,7 @@ tape('test String appendage', function (t) {
|
|
|
554
628
|
})
|
|
555
629
|
|
|
556
630
|
tape('test Number appendage', function (t) {
|
|
557
|
-
const bl = new
|
|
631
|
+
const bl = new BufferListStream()
|
|
558
632
|
const b = Buffer.from('1234567890')
|
|
559
633
|
|
|
560
634
|
bl.append(1234)
|
|
@@ -571,7 +645,7 @@ tape('test Number appendage', function (t) {
|
|
|
571
645
|
|
|
572
646
|
tape('write nothing, should get empty buffer', function (t) {
|
|
573
647
|
t.plan(3)
|
|
574
|
-
|
|
648
|
+
BufferListStream(function (err, data) {
|
|
575
649
|
t.notOk(err, 'no error')
|
|
576
650
|
t.ok(Buffer.isBuffer(data), 'got a buffer')
|
|
577
651
|
t.equal(0, data.length, 'got a zero-length buffer')
|
|
@@ -585,7 +659,7 @@ tape('unicode string', function (t) {
|
|
|
585
659
|
const inp1 = '\u2600'
|
|
586
660
|
const inp2 = '\u2603'
|
|
587
661
|
const exp = inp1 + ' and ' + inp2
|
|
588
|
-
const bl =
|
|
662
|
+
const bl = BufferListStream()
|
|
589
663
|
|
|
590
664
|
bl.write(inp1)
|
|
591
665
|
bl.write(' and ')
|
|
@@ -595,8 +669,8 @@ tape('unicode string', function (t) {
|
|
|
595
669
|
})
|
|
596
670
|
|
|
597
671
|
tape('should emit finish', function (t) {
|
|
598
|
-
const source =
|
|
599
|
-
const dest =
|
|
672
|
+
const source = BufferListStream()
|
|
673
|
+
const dest = BufferListStream()
|
|
600
674
|
|
|
601
675
|
source.write('hello')
|
|
602
676
|
source.pipe(dest)
|
|
@@ -610,7 +684,7 @@ tape('should emit finish', function (t) {
|
|
|
610
684
|
tape('basic copy', function (t) {
|
|
611
685
|
const buf = crypto.randomBytes(1024)
|
|
612
686
|
const buf2 = Buffer.alloc(1024)
|
|
613
|
-
const b =
|
|
687
|
+
const b = BufferListStream(buf)
|
|
614
688
|
|
|
615
689
|
b.copy(buf2)
|
|
616
690
|
t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
@@ -621,7 +695,7 @@ tape('basic copy', function (t) {
|
|
|
621
695
|
tape('copy after many appends', function (t) {
|
|
622
696
|
const buf = crypto.randomBytes(512)
|
|
623
697
|
const buf2 = Buffer.alloc(1024)
|
|
624
|
-
const b =
|
|
698
|
+
const b = BufferListStream(buf)
|
|
625
699
|
|
|
626
700
|
b.append(buf)
|
|
627
701
|
b.copy(buf2)
|
|
@@ -633,7 +707,7 @@ tape('copy after many appends', function (t) {
|
|
|
633
707
|
tape('copy at a precise position', function (t) {
|
|
634
708
|
const buf = crypto.randomBytes(1004)
|
|
635
709
|
const buf2 = Buffer.alloc(1024)
|
|
636
|
-
const b =
|
|
710
|
+
const b = BufferListStream(buf)
|
|
637
711
|
|
|
638
712
|
b.copy(buf2, 20)
|
|
639
713
|
t.equal(b.slice().toString('hex'), buf2.slice(20).toString('hex'), 'same buffer')
|
|
@@ -644,7 +718,7 @@ tape('copy at a precise position', function (t) {
|
|
|
644
718
|
tape('copy starting from a precise location', function (t) {
|
|
645
719
|
const buf = crypto.randomBytes(10)
|
|
646
720
|
const buf2 = Buffer.alloc(5)
|
|
647
|
-
const b =
|
|
721
|
+
const b = BufferListStream(buf)
|
|
648
722
|
|
|
649
723
|
b.copy(buf2, 0, 5)
|
|
650
724
|
t.equal(b.slice(5).toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
@@ -654,7 +728,7 @@ tape('copy starting from a precise location', function (t) {
|
|
|
654
728
|
|
|
655
729
|
tape('copy in an interval', function (t) {
|
|
656
730
|
const rnd = crypto.randomBytes(10)
|
|
657
|
-
const b =
|
|
731
|
+
const b = BufferListStream(rnd) // put the random bytes there
|
|
658
732
|
const actual = Buffer.alloc(3)
|
|
659
733
|
const expected = Buffer.alloc(3)
|
|
660
734
|
|
|
@@ -669,7 +743,7 @@ tape('copy in an interval', function (t) {
|
|
|
669
743
|
tape('copy an interval between two buffers', function (t) {
|
|
670
744
|
const buf = crypto.randomBytes(10)
|
|
671
745
|
const buf2 = Buffer.alloc(10)
|
|
672
|
-
const b =
|
|
746
|
+
const b = BufferListStream(buf)
|
|
673
747
|
|
|
674
748
|
b.append(buf)
|
|
675
749
|
b.copy(buf2, 0, 5, 15)
|
|
@@ -680,7 +754,7 @@ tape('copy an interval between two buffers', function (t) {
|
|
|
680
754
|
})
|
|
681
755
|
|
|
682
756
|
tape('shallow slice across buffer boundaries', function (t) {
|
|
683
|
-
const bl = new
|
|
757
|
+
const bl = new BufferListStream(['First', 'Second', 'Third'])
|
|
684
758
|
|
|
685
759
|
t.equal(bl.shallowSlice(3, 13).toString(), 'stSecondTh')
|
|
686
760
|
|
|
@@ -690,7 +764,7 @@ tape('shallow slice across buffer boundaries', function (t) {
|
|
|
690
764
|
tape('shallow slice within single buffer', function (t) {
|
|
691
765
|
t.plan(2)
|
|
692
766
|
|
|
693
|
-
const bl = new
|
|
767
|
+
const bl = new BufferListStream(['First', 'Second', 'Third'])
|
|
694
768
|
|
|
695
769
|
t.equal(bl.shallowSlice(5, 10).toString(), 'Secon')
|
|
696
770
|
t.equal(bl.shallowSlice(7, 10).toString(), 'con')
|
|
@@ -701,7 +775,7 @@ tape('shallow slice within single buffer', function (t) {
|
|
|
701
775
|
tape('shallow slice single buffer', function (t) {
|
|
702
776
|
t.plan(3)
|
|
703
777
|
|
|
704
|
-
const bl = new
|
|
778
|
+
const bl = new BufferListStream(['First', 'Second', 'Third'])
|
|
705
779
|
|
|
706
780
|
t.equal(bl.shallowSlice(0, 5).toString(), 'First')
|
|
707
781
|
t.equal(bl.shallowSlice(5, 11).toString(), 'Second')
|
|
@@ -711,7 +785,7 @@ tape('shallow slice single buffer', function (t) {
|
|
|
711
785
|
tape('shallow slice with negative or omitted indices', function (t) {
|
|
712
786
|
t.plan(4)
|
|
713
787
|
|
|
714
|
-
const bl = new
|
|
788
|
+
const bl = new BufferListStream(['First', 'Second', 'Third'])
|
|
715
789
|
|
|
716
790
|
t.equal(bl.shallowSlice().toString(), 'FirstSecondThird')
|
|
717
791
|
t.equal(bl.shallowSlice(5).toString(), 'SecondThird')
|
|
@@ -723,7 +797,7 @@ tape('shallow slice does not make a copy', function (t) {
|
|
|
723
797
|
t.plan(1)
|
|
724
798
|
|
|
725
799
|
const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
|
|
726
|
-
const bl =
|
|
800
|
+
const bl = new BufferListStream(buffers).shallowSlice(5, -3)
|
|
727
801
|
|
|
728
802
|
buffers[1].fill('h')
|
|
729
803
|
buffers[2].fill('h')
|
|
@@ -735,7 +809,7 @@ tape('shallow slice with 0 length', function (t) {
|
|
|
735
809
|
t.plan(1)
|
|
736
810
|
|
|
737
811
|
const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
|
|
738
|
-
const bl = (new
|
|
812
|
+
const bl = (new BufferListStream(buffers)).shallowSlice(0, 0)
|
|
739
813
|
|
|
740
814
|
t.equal(bl.length, 0)
|
|
741
815
|
})
|
|
@@ -744,7 +818,7 @@ tape('shallow slice with 0 length from middle', function (t) {
|
|
|
744
818
|
t.plan(1)
|
|
745
819
|
|
|
746
820
|
const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
|
|
747
|
-
const bl = (new
|
|
821
|
+
const bl = (new BufferListStream(buffers)).shallowSlice(10, 10)
|
|
748
822
|
|
|
749
823
|
t.equal(bl.length, 0)
|
|
750
824
|
})
|
|
@@ -752,7 +826,7 @@ tape('shallow slice with 0 length from middle', function (t) {
|
|
|
752
826
|
tape('duplicate', function (t) {
|
|
753
827
|
t.plan(2)
|
|
754
828
|
|
|
755
|
-
const bl = new
|
|
829
|
+
const bl = new BufferListStream('abcdefghij\xff\x00')
|
|
756
830
|
const dup = bl.duplicate()
|
|
757
831
|
|
|
758
832
|
t.equal(bl.prototype, dup.prototype)
|
|
@@ -762,7 +836,8 @@ tape('duplicate', function (t) {
|
|
|
762
836
|
tape('destroy no pipe', function (t) {
|
|
763
837
|
t.plan(2)
|
|
764
838
|
|
|
765
|
-
|
|
839
|
+
/** @type {BufferListStreamWithPrivate} */
|
|
840
|
+
const bl = new BufferListStream('alsdkfja;lsdkfja;lsdk')
|
|
766
841
|
|
|
767
842
|
bl.destroy()
|
|
768
843
|
|
|
@@ -773,7 +848,8 @@ tape('destroy no pipe', function (t) {
|
|
|
773
848
|
tape('destroy with error', function (t) {
|
|
774
849
|
t.plan(3)
|
|
775
850
|
|
|
776
|
-
|
|
851
|
+
/** @type {BufferListStreamWithPrivate} */
|
|
852
|
+
const bl = new BufferListStream('alsdkfja;lsdkfja;lsdk')
|
|
777
853
|
const err = new Error('kaboom')
|
|
778
854
|
|
|
779
855
|
bl.destroy(err)
|
|
@@ -788,7 +864,8 @@ tape('destroy with error', function (t) {
|
|
|
788
864
|
!process.browser && tape('destroy with pipe before read end', function (t) {
|
|
789
865
|
t.plan(2)
|
|
790
866
|
|
|
791
|
-
|
|
867
|
+
/** @type {BufferListStreamWithPrivate} */
|
|
868
|
+
const bl = new BufferListStream()
|
|
792
869
|
fs.createReadStream(path.join(__dirname, '/test.js'))
|
|
793
870
|
.pipe(bl)
|
|
794
871
|
|
|
@@ -801,7 +878,8 @@ tape('destroy with error', function (t) {
|
|
|
801
878
|
!process.browser && tape('destroy with pipe before read end with race', function (t) {
|
|
802
879
|
t.plan(2)
|
|
803
880
|
|
|
804
|
-
|
|
881
|
+
/** @type {BufferListStreamWithPrivate} */
|
|
882
|
+
const bl = new BufferListStream()
|
|
805
883
|
|
|
806
884
|
fs.createReadStream(path.join(__dirname, '/test.js'))
|
|
807
885
|
.pipe(bl)
|
|
@@ -818,8 +896,8 @@ tape('destroy with error', function (t) {
|
|
|
818
896
|
!process.browser && tape('destroy with pipe after read end', function (t) {
|
|
819
897
|
t.plan(2)
|
|
820
898
|
|
|
821
|
-
|
|
822
|
-
|
|
899
|
+
/** @type {BufferListStreamWithPrivate} */
|
|
900
|
+
const bl = new BufferListStream()
|
|
823
901
|
fs.createReadStream(path.join(__dirname, '/test.js'))
|
|
824
902
|
.on('end', onEnd)
|
|
825
903
|
.pipe(bl)
|
|
@@ -835,8 +913,9 @@ tape('destroy with error', function (t) {
|
|
|
835
913
|
!process.browser && tape('destroy with pipe while writing to a destination', function (t) {
|
|
836
914
|
t.plan(4)
|
|
837
915
|
|
|
838
|
-
|
|
839
|
-
const
|
|
916
|
+
/** @type {BufferListStreamWithPrivate} */
|
|
917
|
+
const bl = new BufferListStream()
|
|
918
|
+
const ds = new BufferListStream()
|
|
840
919
|
|
|
841
920
|
fs.createReadStream(path.join(__dirname, '/test.js'))
|
|
842
921
|
.on('end', onEnd)
|
|
@@ -862,7 +941,7 @@ tape('destroy with error', function (t) {
|
|
|
862
941
|
!process.browser && tape('handle error', function (t) {
|
|
863
942
|
t.plan(2)
|
|
864
943
|
|
|
865
|
-
fs.createReadStream('/does/not/exist').pipe(
|
|
944
|
+
fs.createReadStream('/does/not/exist').pipe(BufferListStream(function (err, data) {
|
|
866
945
|
t.ok(err instanceof Error, 'has error')
|
|
867
946
|
t.notOk(data, 'no data')
|
|
868
947
|
}))
|