bl 3.0.0 → 4.0.2
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/.travis.yml +3 -0
- package/BufferList.js +387 -0
- package/LICENSE.md +1 -1
- package/README.md +60 -31
- package/bl.js +30 -328
- package/package.json +9 -6
- package/test/convert.js +21 -0
- package/test/indexOf.js +78 -49
- package/test/isBufferList.js +32 -0
- package/test/test.js +212 -157
- package/.jshintrc +0 -60
package/test/test.js
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
('hex utf8 utf-8 ascii binary base64'
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
// run the indexOf tests
|
|
3
|
+
const tape = require('tape')
|
|
4
|
+
const crypto = require('crypto')
|
|
5
|
+
const fs = require('fs')
|
|
6
|
+
const path = require('path')
|
|
7
|
+
const BufferList = require('../')
|
|
8
|
+
const { Buffer } = require('buffer')
|
|
9
|
+
|
|
10
|
+
const encodings =
|
|
11
|
+
('hex utf8 utf-8 ascii binary base64' +
|
|
12
|
+
(process.browser ? '' : ' ucs2 ucs-2 utf16le utf-16le')).split(' ')
|
|
13
|
+
|
|
15
14
|
require('./indexOf')
|
|
15
|
+
require('./isBufferList')
|
|
16
|
+
require('./convert')
|
|
16
17
|
|
|
17
18
|
tape('single bytes from single buffer', function (t) {
|
|
18
|
-
|
|
19
|
+
const bl = new BufferList()
|
|
20
|
+
|
|
19
21
|
bl.append(Buffer.from('abcd'))
|
|
20
22
|
|
|
21
23
|
t.equal(bl.length, 4)
|
|
@@ -30,7 +32,8 @@ tape('single bytes from single buffer', function (t) {
|
|
|
30
32
|
})
|
|
31
33
|
|
|
32
34
|
tape('single bytes from multiple buffers', function (t) {
|
|
33
|
-
|
|
35
|
+
const bl = new BufferList()
|
|
36
|
+
|
|
34
37
|
bl.append(Buffer.from('abcd'))
|
|
35
38
|
bl.append(Buffer.from('efg'))
|
|
36
39
|
bl.append(Buffer.from('hi'))
|
|
@@ -48,11 +51,13 @@ tape('single bytes from multiple buffers', function (t) {
|
|
|
48
51
|
t.equal(bl.get(7), 104)
|
|
49
52
|
t.equal(bl.get(8), 105)
|
|
50
53
|
t.equal(bl.get(9), 106)
|
|
54
|
+
|
|
51
55
|
t.end()
|
|
52
56
|
})
|
|
53
57
|
|
|
54
58
|
tape('multi bytes from single buffer', function (t) {
|
|
55
|
-
|
|
59
|
+
const bl = new BufferList()
|
|
60
|
+
|
|
56
61
|
bl.append(Buffer.from('abcd'))
|
|
57
62
|
|
|
58
63
|
t.equal(bl.length, 4)
|
|
@@ -66,7 +71,8 @@ tape('multi bytes from single buffer', function (t) {
|
|
|
66
71
|
})
|
|
67
72
|
|
|
68
73
|
tape('multi bytes from single buffer (negative indexes)', function (t) {
|
|
69
|
-
|
|
74
|
+
const bl = new BufferList()
|
|
75
|
+
|
|
70
76
|
bl.append(Buffer.from('buffer'))
|
|
71
77
|
|
|
72
78
|
t.equal(bl.length, 6)
|
|
@@ -79,7 +85,7 @@ tape('multi bytes from single buffer (negative indexes)', function (t) {
|
|
|
79
85
|
})
|
|
80
86
|
|
|
81
87
|
tape('multiple bytes from multiple buffers', function (t) {
|
|
82
|
-
|
|
88
|
+
const bl = new BufferList()
|
|
83
89
|
|
|
84
90
|
bl.append(Buffer.from('abcd'))
|
|
85
91
|
bl.append(Buffer.from('efg'))
|
|
@@ -99,10 +105,10 @@ tape('multiple bytes from multiple buffers', function (t) {
|
|
|
99
105
|
})
|
|
100
106
|
|
|
101
107
|
tape('multiple bytes from multiple buffer lists', function (t) {
|
|
102
|
-
|
|
108
|
+
const bl = new BufferList()
|
|
103
109
|
|
|
104
|
-
bl.append(new BufferList([
|
|
105
|
-
bl.append(new BufferList([
|
|
110
|
+
bl.append(new BufferList([Buffer.from('abcd'), Buffer.from('efg')]))
|
|
111
|
+
bl.append(new BufferList([Buffer.from('hi'), Buffer.from('j')]))
|
|
106
112
|
|
|
107
113
|
t.equal(bl.length, 10)
|
|
108
114
|
|
|
@@ -118,16 +124,16 @@ tape('multiple bytes from multiple buffer lists', function (t) {
|
|
|
118
124
|
|
|
119
125
|
// same data as previous test, just using nested constructors
|
|
120
126
|
tape('multiple bytes from crazy nested buffer lists', function (t) {
|
|
121
|
-
|
|
127
|
+
const bl = new BufferList()
|
|
122
128
|
|
|
123
129
|
bl.append(new BufferList([
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
130
|
+
new BufferList([
|
|
131
|
+
new BufferList(Buffer.from('abc')),
|
|
132
|
+
Buffer.from('d'),
|
|
133
|
+
new BufferList(Buffer.from('efg'))
|
|
134
|
+
]),
|
|
135
|
+
new BufferList([Buffer.from('hi')]),
|
|
136
|
+
new BufferList(Buffer.from('j'))
|
|
131
137
|
]))
|
|
132
138
|
|
|
133
139
|
t.equal(bl.length, 10)
|
|
@@ -143,49 +149,69 @@ tape('multiple bytes from crazy nested buffer lists', function (t) {
|
|
|
143
149
|
})
|
|
144
150
|
|
|
145
151
|
tape('append accepts arrays of Buffers', function (t) {
|
|
146
|
-
|
|
152
|
+
const bl = new BufferList()
|
|
153
|
+
|
|
147
154
|
bl.append(Buffer.from('abc'))
|
|
148
|
-
bl.append([
|
|
149
|
-
bl.append([
|
|
150
|
-
bl.append([
|
|
155
|
+
bl.append([Buffer.from('def')])
|
|
156
|
+
bl.append([Buffer.from('ghi'), Buffer.from('jkl')])
|
|
157
|
+
bl.append([Buffer.from('mnop'), Buffer.from('qrstu'), Buffer.from('vwxyz')])
|
|
151
158
|
t.equal(bl.length, 26)
|
|
152
159
|
t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
|
|
160
|
+
|
|
161
|
+
t.end()
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
tape('append accepts arrays of Uint8Arrays', function (t) {
|
|
165
|
+
const bl = new BufferList()
|
|
166
|
+
|
|
167
|
+
bl.append(new Uint8Array([97, 98, 99]))
|
|
168
|
+
bl.append([Uint8Array.from([100, 101, 102])])
|
|
169
|
+
bl.append([new Uint8Array([103, 104, 105]), new Uint8Array([106, 107, 108])])
|
|
170
|
+
bl.append([new Uint8Array([109, 110, 111, 112]), new Uint8Array([113, 114, 115, 116, 117]), new Uint8Array([118, 119, 120, 121, 122])])
|
|
171
|
+
t.equal(bl.length, 26)
|
|
172
|
+
t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
|
|
173
|
+
|
|
153
174
|
t.end()
|
|
154
175
|
})
|
|
155
176
|
|
|
156
177
|
tape('append accepts arrays of BufferLists', function (t) {
|
|
157
|
-
|
|
178
|
+
const bl = new BufferList()
|
|
179
|
+
|
|
158
180
|
bl.append(Buffer.from('abc'))
|
|
159
|
-
bl.append([
|
|
160
|
-
bl.append(new BufferList([
|
|
161
|
-
bl.append([
|
|
181
|
+
bl.append([new BufferList('def')])
|
|
182
|
+
bl.append(new BufferList([Buffer.from('ghi'), new BufferList('jkl')]))
|
|
183
|
+
bl.append([Buffer.from('mnop'), new BufferList([Buffer.from('qrstu'), Buffer.from('vwxyz')])])
|
|
162
184
|
t.equal(bl.length, 26)
|
|
163
185
|
t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
|
|
186
|
+
|
|
164
187
|
t.end()
|
|
165
188
|
})
|
|
166
189
|
|
|
167
190
|
tape('append chainable', function (t) {
|
|
168
|
-
|
|
191
|
+
const bl = new BufferList()
|
|
192
|
+
|
|
169
193
|
t.ok(bl.append(Buffer.from('abcd')) === bl)
|
|
170
|
-
t.ok(bl.append([
|
|
194
|
+
t.ok(bl.append([Buffer.from('abcd')]) === bl)
|
|
171
195
|
t.ok(bl.append(new BufferList(Buffer.from('abcd'))) === bl)
|
|
172
|
-
t.ok(bl.append([
|
|
196
|
+
t.ok(bl.append([new BufferList(Buffer.from('abcd'))]) === bl)
|
|
197
|
+
|
|
173
198
|
t.end()
|
|
174
199
|
})
|
|
175
200
|
|
|
176
201
|
tape('append chainable (test results)', function (t) {
|
|
177
|
-
|
|
178
|
-
.append([
|
|
179
|
-
.append(new BufferList([
|
|
180
|
-
.append([
|
|
202
|
+
const bl = new BufferList('abc')
|
|
203
|
+
.append([new BufferList('def')])
|
|
204
|
+
.append(new BufferList([Buffer.from('ghi'), new BufferList('jkl')]))
|
|
205
|
+
.append([Buffer.from('mnop'), new BufferList([Buffer.from('qrstu'), Buffer.from('vwxyz')])])
|
|
181
206
|
|
|
182
207
|
t.equal(bl.length, 26)
|
|
183
208
|
t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
|
|
209
|
+
|
|
184
210
|
t.end()
|
|
185
211
|
})
|
|
186
212
|
|
|
187
213
|
tape('consuming from multiple buffers', function (t) {
|
|
188
|
-
|
|
214
|
+
const bl = new BufferList()
|
|
189
215
|
|
|
190
216
|
bl.append(Buffer.from('abcd'))
|
|
191
217
|
bl.append(Buffer.from('efg'))
|
|
@@ -220,7 +246,7 @@ tape('consuming from multiple buffers', function (t) {
|
|
|
220
246
|
})
|
|
221
247
|
|
|
222
248
|
tape('complete consumption', function (t) {
|
|
223
|
-
|
|
249
|
+
const bl = new BufferList()
|
|
224
250
|
|
|
225
251
|
bl.append(Buffer.from('a'))
|
|
226
252
|
bl.append(Buffer.from('b'))
|
|
@@ -234,10 +260,10 @@ tape('complete consumption', function (t) {
|
|
|
234
260
|
})
|
|
235
261
|
|
|
236
262
|
tape('test readUInt8 / readInt8', function (t) {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
263
|
+
const buf1 = Buffer.alloc(1)
|
|
264
|
+
const buf2 = Buffer.alloc(3)
|
|
265
|
+
const buf3 = Buffer.alloc(3)
|
|
266
|
+
const bl = new BufferList()
|
|
241
267
|
|
|
242
268
|
buf2[1] = 0x3
|
|
243
269
|
buf2[2] = 0x4
|
|
@@ -256,14 +282,15 @@ tape('test readUInt8 / readInt8', function (t) {
|
|
|
256
282
|
t.equal(bl.readInt8(4), 0x23)
|
|
257
283
|
t.equal(bl.readUInt8(5), 0x42)
|
|
258
284
|
t.equal(bl.readInt8(5), 0x42)
|
|
285
|
+
|
|
259
286
|
t.end()
|
|
260
287
|
})
|
|
261
288
|
|
|
262
289
|
tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
290
|
+
const buf1 = Buffer.alloc(1)
|
|
291
|
+
const buf2 = Buffer.alloc(3)
|
|
292
|
+
const buf3 = Buffer.alloc(3)
|
|
293
|
+
const bl = new BufferList()
|
|
267
294
|
|
|
268
295
|
buf2[1] = 0x3
|
|
269
296
|
buf2[2] = 0x4
|
|
@@ -286,14 +313,15 @@ tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t
|
|
|
286
313
|
t.equal(bl.readUInt16LE(4), 0x4223)
|
|
287
314
|
t.equal(bl.readInt16BE(4), 0x2342)
|
|
288
315
|
t.equal(bl.readInt16LE(4), 0x4223)
|
|
316
|
+
|
|
289
317
|
t.end()
|
|
290
318
|
})
|
|
291
319
|
|
|
292
320
|
tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t) {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
321
|
+
const buf1 = Buffer.alloc(1)
|
|
322
|
+
const buf2 = Buffer.alloc(3)
|
|
323
|
+
const buf3 = Buffer.alloc(3)
|
|
324
|
+
const bl = new BufferList()
|
|
297
325
|
|
|
298
326
|
buf2[1] = 0x3
|
|
299
327
|
buf2[2] = 0x4
|
|
@@ -308,14 +336,15 @@ tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t
|
|
|
308
336
|
t.equal(bl.readUInt32LE(2), 0x42230403)
|
|
309
337
|
t.equal(bl.readInt32BE(2), 0x03042342)
|
|
310
338
|
t.equal(bl.readInt32LE(2), 0x42230403)
|
|
339
|
+
|
|
311
340
|
t.end()
|
|
312
341
|
})
|
|
313
342
|
|
|
314
343
|
tape('test readUIntLE / readUIntBE / readIntLE / readIntBE', function (t) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
344
|
+
const buf1 = Buffer.alloc(1)
|
|
345
|
+
const buf2 = Buffer.alloc(3)
|
|
346
|
+
const buf3 = Buffer.alloc(3)
|
|
347
|
+
const bl = new BufferList()
|
|
319
348
|
|
|
320
349
|
buf2[0] = 0x2
|
|
321
350
|
buf2[1] = 0x3
|
|
@@ -352,14 +381,15 @@ tape('test readUIntLE / readUIntBE / readIntLE / readIntBE', function (t) {
|
|
|
352
381
|
t.equal(bl.readIntLE(1, 4), 0x23040302)
|
|
353
382
|
t.equal(bl.readIntLE(1, 5), 0x4223040302)
|
|
354
383
|
t.equal(bl.readIntLE(1, 6), 0x614223040302)
|
|
384
|
+
|
|
355
385
|
t.end()
|
|
356
386
|
})
|
|
357
387
|
|
|
358
388
|
tape('test readFloatLE / readFloatBE', function (t) {
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
389
|
+
const buf1 = Buffer.alloc(1)
|
|
390
|
+
const buf2 = Buffer.alloc(3)
|
|
391
|
+
const buf3 = Buffer.alloc(3)
|
|
392
|
+
const bl = new BufferList()
|
|
363
393
|
|
|
364
394
|
buf2[1] = 0x00
|
|
365
395
|
buf2[2] = 0x00
|
|
@@ -371,14 +401,15 @@ tape('test readFloatLE / readFloatBE', function (t) {
|
|
|
371
401
|
bl.append(buf3)
|
|
372
402
|
|
|
373
403
|
t.equal(bl.readFloatLE(2), 0x01)
|
|
404
|
+
|
|
374
405
|
t.end()
|
|
375
406
|
})
|
|
376
407
|
|
|
377
408
|
tape('test readDoubleLE / readDoubleBE', function (t) {
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
409
|
+
const buf1 = Buffer.alloc(1)
|
|
410
|
+
const buf2 = Buffer.alloc(3)
|
|
411
|
+
const buf3 = Buffer.alloc(10)
|
|
412
|
+
const bl = new BufferList()
|
|
382
413
|
|
|
383
414
|
buf2[1] = 0x55
|
|
384
415
|
buf2[2] = 0x55
|
|
@@ -394,11 +425,12 @@ tape('test readDoubleLE / readDoubleBE', function (t) {
|
|
|
394
425
|
bl.append(buf3)
|
|
395
426
|
|
|
396
427
|
t.equal(bl.readDoubleLE(2), 0.3333333333333333)
|
|
428
|
+
|
|
397
429
|
t.end()
|
|
398
430
|
})
|
|
399
431
|
|
|
400
432
|
tape('test toString', function (t) {
|
|
401
|
-
|
|
433
|
+
const bl = new BufferList()
|
|
402
434
|
|
|
403
435
|
bl.append(Buffer.from('abcd'))
|
|
404
436
|
bl.append(Buffer.from('efg'))
|
|
@@ -415,8 +447,8 @@ tape('test toString', function (t) {
|
|
|
415
447
|
})
|
|
416
448
|
|
|
417
449
|
tape('test toString encoding', function (t) {
|
|
418
|
-
|
|
419
|
-
|
|
450
|
+
const bl = new BufferList()
|
|
451
|
+
const b = Buffer.from('abcdefghij\xff\x00')
|
|
420
452
|
|
|
421
453
|
bl.append(Buffer.from('abcd'))
|
|
422
454
|
bl.append(Buffer.from('efg'))
|
|
@@ -425,52 +457,54 @@ tape('test toString encoding', function (t) {
|
|
|
425
457
|
bl.append(Buffer.from('\xff\x00'))
|
|
426
458
|
|
|
427
459
|
encodings.forEach(function (enc) {
|
|
428
|
-
|
|
429
|
-
|
|
460
|
+
t.equal(bl.toString(enc), b.toString(enc), enc)
|
|
461
|
+
})
|
|
430
462
|
|
|
431
463
|
t.end()
|
|
432
464
|
})
|
|
433
465
|
|
|
434
466
|
!process.browser && tape('test stream', function (t) {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
467
|
+
const random = crypto.randomBytes(65534)
|
|
468
|
+
|
|
469
|
+
const bl = new BufferList((err, buf) => {
|
|
470
|
+
t.ok(Buffer.isBuffer(buf))
|
|
471
|
+
t.ok(err === null)
|
|
472
|
+
t.ok(random.equals(bl.slice()))
|
|
473
|
+
t.ok(random.equals(buf.slice()))
|
|
474
|
+
|
|
475
|
+
bl.pipe(fs.createWriteStream('/tmp/bl_test_rnd_out.dat'))
|
|
476
|
+
.on('close', function () {
|
|
477
|
+
const rndhash = crypto.createHash('md5').update(random).digest('hex')
|
|
478
|
+
const md5sum = crypto.createHash('md5')
|
|
479
|
+
const s = fs.createReadStream('/tmp/bl_test_rnd_out.dat')
|
|
480
|
+
|
|
481
|
+
s.on('data', md5sum.update.bind(md5sum))
|
|
482
|
+
s.on('end', function () {
|
|
483
|
+
t.equal(rndhash, md5sum.digest('hex'), 'woohoo! correct hash!')
|
|
484
|
+
t.end()
|
|
485
|
+
})
|
|
454
486
|
})
|
|
487
|
+
})
|
|
455
488
|
|
|
456
489
|
fs.writeFileSync('/tmp/bl_test_rnd.dat', random)
|
|
457
490
|
fs.createReadStream('/tmp/bl_test_rnd.dat').pipe(bl)
|
|
458
491
|
})
|
|
459
492
|
|
|
460
493
|
tape('instantiation with Buffer', function (t) {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
494
|
+
const buf = crypto.randomBytes(1024)
|
|
495
|
+
const buf2 = crypto.randomBytes(1024)
|
|
496
|
+
let b = BufferList(buf)
|
|
464
497
|
|
|
465
498
|
t.equal(buf.toString('hex'), b.slice().toString('hex'), 'same buffer')
|
|
466
|
-
b = BufferList([
|
|
467
|
-
t.equal(b.slice().toString('hex'), Buffer.concat([
|
|
499
|
+
b = BufferList([buf, buf2])
|
|
500
|
+
t.equal(b.slice().toString('hex'), Buffer.concat([buf, buf2]).toString('hex'), 'same buffer')
|
|
501
|
+
|
|
468
502
|
t.end()
|
|
469
503
|
})
|
|
470
504
|
|
|
471
505
|
tape('test String appendage', function (t) {
|
|
472
|
-
|
|
473
|
-
|
|
506
|
+
const bl = new BufferList()
|
|
507
|
+
const b = Buffer.from('abcdefghij\xff\x00')
|
|
474
508
|
|
|
475
509
|
bl.append('abcd')
|
|
476
510
|
bl.append('efg')
|
|
@@ -479,15 +513,15 @@ tape('test String appendage', function (t) {
|
|
|
479
513
|
bl.append('\xff\x00')
|
|
480
514
|
|
|
481
515
|
encodings.forEach(function (enc) {
|
|
482
|
-
|
|
483
|
-
|
|
516
|
+
t.equal(bl.toString(enc), b.toString(enc))
|
|
517
|
+
})
|
|
484
518
|
|
|
485
519
|
t.end()
|
|
486
520
|
})
|
|
487
521
|
|
|
488
522
|
tape('test Number appendage', function (t) {
|
|
489
|
-
|
|
490
|
-
|
|
523
|
+
const bl = new BufferList()
|
|
524
|
+
const b = Buffer.from('1234567890')
|
|
491
525
|
|
|
492
526
|
bl.append(1234)
|
|
493
527
|
bl.append(567)
|
|
@@ -495,8 +529,8 @@ tape('test Number appendage', function (t) {
|
|
|
495
529
|
bl.append(0)
|
|
496
530
|
|
|
497
531
|
encodings.forEach(function (enc) {
|
|
498
|
-
|
|
499
|
-
|
|
532
|
+
t.equal(bl.toString(enc), b.toString(enc))
|
|
533
|
+
})
|
|
500
534
|
|
|
501
535
|
t.end()
|
|
502
536
|
})
|
|
@@ -513,10 +547,12 @@ tape('write nothing, should get empty buffer', function (t) {
|
|
|
513
547
|
|
|
514
548
|
tape('unicode string', function (t) {
|
|
515
549
|
t.plan(2)
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
550
|
+
|
|
551
|
+
const inp1 = '\u2600'
|
|
552
|
+
const inp2 = '\u2603'
|
|
553
|
+
const exp = inp1 + ' and ' + inp2
|
|
554
|
+
const bl = BufferList()
|
|
555
|
+
|
|
520
556
|
bl.write(inp1)
|
|
521
557
|
bl.write(' and ')
|
|
522
558
|
bl.write(inp2)
|
|
@@ -525,8 +561,8 @@ tape('unicode string', function (t) {
|
|
|
525
561
|
})
|
|
526
562
|
|
|
527
563
|
tape('should emit finish', function (t) {
|
|
528
|
-
|
|
529
|
-
|
|
564
|
+
const source = BufferList()
|
|
565
|
+
const dest = BufferList()
|
|
530
566
|
|
|
531
567
|
source.write('hello')
|
|
532
568
|
source.pipe(dest)
|
|
@@ -538,90 +574,100 @@ tape('should emit finish', function (t) {
|
|
|
538
574
|
})
|
|
539
575
|
|
|
540
576
|
tape('basic copy', function (t) {
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
577
|
+
const buf = crypto.randomBytes(1024)
|
|
578
|
+
const buf2 = Buffer.alloc(1024)
|
|
579
|
+
const b = BufferList(buf)
|
|
544
580
|
|
|
545
581
|
b.copy(buf2)
|
|
546
582
|
t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
583
|
+
|
|
547
584
|
t.end()
|
|
548
585
|
})
|
|
549
586
|
|
|
550
587
|
tape('copy after many appends', function (t) {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
588
|
+
const buf = crypto.randomBytes(512)
|
|
589
|
+
const buf2 = Buffer.alloc(1024)
|
|
590
|
+
const b = BufferList(buf)
|
|
554
591
|
|
|
555
592
|
b.append(buf)
|
|
556
593
|
b.copy(buf2)
|
|
557
594
|
t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
595
|
+
|
|
558
596
|
t.end()
|
|
559
597
|
})
|
|
560
598
|
|
|
561
599
|
tape('copy at a precise position', function (t) {
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
600
|
+
const buf = crypto.randomBytes(1004)
|
|
601
|
+
const buf2 = Buffer.alloc(1024)
|
|
602
|
+
const b = BufferList(buf)
|
|
565
603
|
|
|
566
604
|
b.copy(buf2, 20)
|
|
567
605
|
t.equal(b.slice().toString('hex'), buf2.slice(20).toString('hex'), 'same buffer')
|
|
606
|
+
|
|
568
607
|
t.end()
|
|
569
608
|
})
|
|
570
609
|
|
|
571
610
|
tape('copy starting from a precise location', function (t) {
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
611
|
+
const buf = crypto.randomBytes(10)
|
|
612
|
+
const buf2 = Buffer.alloc(5)
|
|
613
|
+
const b = BufferList(buf)
|
|
575
614
|
|
|
576
615
|
b.copy(buf2, 0, 5)
|
|
577
616
|
t.equal(b.slice(5).toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
617
|
+
|
|
578
618
|
t.end()
|
|
579
619
|
})
|
|
580
620
|
|
|
581
621
|
tape('copy in an interval', function (t) {
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
622
|
+
const rnd = crypto.randomBytes(10)
|
|
623
|
+
const b = BufferList(rnd) // put the random bytes there
|
|
624
|
+
const actual = Buffer.alloc(3)
|
|
625
|
+
const expected = Buffer.alloc(3)
|
|
586
626
|
|
|
587
627
|
rnd.copy(expected, 0, 5, 8)
|
|
588
628
|
b.copy(actual, 0, 5, 8)
|
|
589
629
|
|
|
590
630
|
t.equal(actual.toString('hex'), expected.toString('hex'), 'same buffer')
|
|
631
|
+
|
|
591
632
|
t.end()
|
|
592
633
|
})
|
|
593
634
|
|
|
594
635
|
tape('copy an interval between two buffers', function (t) {
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
636
|
+
const buf = crypto.randomBytes(10)
|
|
637
|
+
const buf2 = Buffer.alloc(10)
|
|
638
|
+
const b = BufferList(buf)
|
|
598
639
|
|
|
599
640
|
b.append(buf)
|
|
600
641
|
b.copy(buf2, 0, 5, 15)
|
|
601
642
|
|
|
602
643
|
t.equal(b.slice(5, 15).toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
644
|
+
|
|
603
645
|
t.end()
|
|
604
646
|
})
|
|
605
647
|
|
|
606
648
|
tape('shallow slice across buffer boundaries', function (t) {
|
|
607
|
-
|
|
649
|
+
const bl = new BufferList(['First', 'Second', 'Third'])
|
|
608
650
|
|
|
609
651
|
t.equal(bl.shallowSlice(3, 13).toString(), 'stSecondTh')
|
|
652
|
+
|
|
610
653
|
t.end()
|
|
611
654
|
})
|
|
612
655
|
|
|
613
656
|
tape('shallow slice within single buffer', function (t) {
|
|
614
657
|
t.plan(2)
|
|
615
|
-
|
|
658
|
+
|
|
659
|
+
const bl = new BufferList(['First', 'Second', 'Third'])
|
|
616
660
|
|
|
617
661
|
t.equal(bl.shallowSlice(5, 10).toString(), 'Secon')
|
|
618
662
|
t.equal(bl.shallowSlice(7, 10).toString(), 'con')
|
|
663
|
+
|
|
619
664
|
t.end()
|
|
620
665
|
})
|
|
621
666
|
|
|
622
667
|
tape('shallow slice single buffer', function (t) {
|
|
623
668
|
t.plan(3)
|
|
624
|
-
|
|
669
|
+
|
|
670
|
+
const bl = new BufferList(['First', 'Second', 'Third'])
|
|
625
671
|
|
|
626
672
|
t.equal(bl.shallowSlice(0, 5).toString(), 'First')
|
|
627
673
|
t.equal(bl.shallowSlice(5, 11).toString(), 'Second')
|
|
@@ -630,7 +676,8 @@ tape('shallow slice single buffer', function (t) {
|
|
|
630
676
|
|
|
631
677
|
tape('shallow slice with negative or omitted indices', function (t) {
|
|
632
678
|
t.plan(4)
|
|
633
|
-
|
|
679
|
+
|
|
680
|
+
const bl = new BufferList(['First', 'Second', 'Third'])
|
|
634
681
|
|
|
635
682
|
t.equal(bl.shallowSlice().toString(), 'FirstSecondThird')
|
|
636
683
|
t.equal(bl.shallowSlice(5).toString(), 'SecondThird')
|
|
@@ -640,8 +687,9 @@ tape('shallow slice with negative or omitted indices', function (t) {
|
|
|
640
687
|
|
|
641
688
|
tape('shallow slice does not make a copy', function (t) {
|
|
642
689
|
t.plan(1)
|
|
643
|
-
|
|
644
|
-
|
|
690
|
+
|
|
691
|
+
const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
|
|
692
|
+
const bl = (new BufferList(buffers)).shallowSlice(5, -3)
|
|
645
693
|
|
|
646
694
|
buffers[1].fill('h')
|
|
647
695
|
buffers[2].fill('h')
|
|
@@ -651,23 +699,27 @@ tape('shallow slice does not make a copy', function (t) {
|
|
|
651
699
|
|
|
652
700
|
tape('shallow slice with 0 length', function (t) {
|
|
653
701
|
t.plan(1)
|
|
654
|
-
|
|
655
|
-
|
|
702
|
+
|
|
703
|
+
const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
|
|
704
|
+
const bl = (new BufferList(buffers)).shallowSlice(0, 0)
|
|
705
|
+
|
|
656
706
|
t.equal(bl.length, 0)
|
|
657
707
|
})
|
|
658
708
|
|
|
659
709
|
tape('shallow slice with 0 length from middle', function (t) {
|
|
660
710
|
t.plan(1)
|
|
661
|
-
|
|
662
|
-
|
|
711
|
+
|
|
712
|
+
const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
|
|
713
|
+
const bl = (new BufferList(buffers)).shallowSlice(10, 10)
|
|
714
|
+
|
|
663
715
|
t.equal(bl.length, 0)
|
|
664
716
|
})
|
|
665
717
|
|
|
666
718
|
tape('duplicate', function (t) {
|
|
667
719
|
t.plan(2)
|
|
668
720
|
|
|
669
|
-
|
|
670
|
-
|
|
721
|
+
const bl = new BufferList('abcdefghij\xff\x00')
|
|
722
|
+
const dup = bl.duplicate()
|
|
671
723
|
|
|
672
724
|
t.equal(bl.prototype, dup.prototype)
|
|
673
725
|
t.equal(bl.toString('hex'), dup.toString('hex'))
|
|
@@ -676,7 +728,8 @@ tape('duplicate', function (t) {
|
|
|
676
728
|
tape('destroy no pipe', function (t) {
|
|
677
729
|
t.plan(2)
|
|
678
730
|
|
|
679
|
-
|
|
731
|
+
const bl = new BufferList('alsdkfja;lsdkfja;lsdk')
|
|
732
|
+
|
|
680
733
|
bl.destroy()
|
|
681
734
|
|
|
682
735
|
t.equal(bl._bufs.length, 0)
|
|
@@ -686,8 +739,9 @@ tape('destroy no pipe', function (t) {
|
|
|
686
739
|
tape('destroy with error', function (t) {
|
|
687
740
|
t.plan(3)
|
|
688
741
|
|
|
689
|
-
|
|
690
|
-
|
|
742
|
+
const bl = new BufferList('alsdkfja;lsdkfja;lsdk')
|
|
743
|
+
const err = new Error('kaboom')
|
|
744
|
+
|
|
691
745
|
bl.destroy(err)
|
|
692
746
|
bl.on('error', function (_err) {
|
|
693
747
|
t.equal(_err, err)
|
|
@@ -700,22 +754,22 @@ tape('destroy with error', function (t) {
|
|
|
700
754
|
!process.browser && tape('destroy with pipe before read end', function (t) {
|
|
701
755
|
t.plan(2)
|
|
702
756
|
|
|
703
|
-
|
|
704
|
-
fs.createReadStream(__dirname
|
|
757
|
+
const bl = new BufferList()
|
|
758
|
+
fs.createReadStream(path.join(__dirname, '/test.js'))
|
|
705
759
|
.pipe(bl)
|
|
706
760
|
|
|
707
761
|
bl.destroy()
|
|
708
762
|
|
|
709
763
|
t.equal(bl._bufs.length, 0)
|
|
710
764
|
t.equal(bl.length, 0)
|
|
711
|
-
|
|
712
765
|
})
|
|
713
766
|
|
|
714
767
|
!process.browser && tape('destroy with pipe before read end with race', function (t) {
|
|
715
768
|
t.plan(2)
|
|
716
769
|
|
|
717
|
-
|
|
718
|
-
|
|
770
|
+
const bl = new BufferList()
|
|
771
|
+
|
|
772
|
+
fs.createReadStream(path.join(__dirname, '/test.js'))
|
|
719
773
|
.pipe(bl)
|
|
720
774
|
|
|
721
775
|
setTimeout(function () {
|
|
@@ -730,8 +784,9 @@ tape('destroy with error', function (t) {
|
|
|
730
784
|
!process.browser && tape('destroy with pipe after read end', function (t) {
|
|
731
785
|
t.plan(2)
|
|
732
786
|
|
|
733
|
-
|
|
734
|
-
|
|
787
|
+
const bl = new BufferList()
|
|
788
|
+
|
|
789
|
+
fs.createReadStream(path.join(__dirname, '/test.js'))
|
|
735
790
|
.on('end', onEnd)
|
|
736
791
|
.pipe(bl)
|
|
737
792
|
|
|
@@ -746,10 +801,10 @@ tape('destroy with error', function (t) {
|
|
|
746
801
|
!process.browser && tape('destroy with pipe while writing to a destination', function (t) {
|
|
747
802
|
t.plan(4)
|
|
748
803
|
|
|
749
|
-
|
|
750
|
-
|
|
804
|
+
const bl = new BufferList()
|
|
805
|
+
const ds = new BufferList()
|
|
751
806
|
|
|
752
|
-
fs.createReadStream(__dirname
|
|
807
|
+
fs.createReadStream(path.join(__dirname, '/test.js'))
|
|
753
808
|
.on('end', onEnd)
|
|
754
809
|
.pipe(bl)
|
|
755
810
|
|
|
@@ -766,13 +821,13 @@ tape('destroy with error', function (t) {
|
|
|
766
821
|
|
|
767
822
|
t.equals(bl._bufs.length, 0)
|
|
768
823
|
t.equals(bl.length, 0)
|
|
769
|
-
|
|
770
824
|
}, 100)
|
|
771
825
|
}
|
|
772
826
|
})
|
|
773
827
|
|
|
774
828
|
!process.browser && tape('handle error', function (t) {
|
|
775
829
|
t.plan(2)
|
|
830
|
+
|
|
776
831
|
fs.createReadStream('/does/not/exist').pipe(BufferList(function (err, data) {
|
|
777
832
|
t.ok(err instanceof Error, 'has error')
|
|
778
833
|
t.notOk(data, 'no data')
|