bl 3.0.1 → 4.0.3
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 +396 -0
- package/LICENSE.md +1 -1
- package/README.md +60 -31
- package/bl.js +30 -337
- package/package.json +10 -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,8 +457,8 @@ 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
|
})
|
|
@@ -448,45 +480,47 @@ tape('uninitialized memory', function (t) {
|
|
|
448
480
|
})
|
|
449
481
|
|
|
450
482
|
!process.browser && tape('test stream', function (t) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
483
|
+
const random = crypto.randomBytes(65534)
|
|
484
|
+
|
|
485
|
+
const bl = new BufferList((err, buf) => {
|
|
486
|
+
t.ok(Buffer.isBuffer(buf))
|
|
487
|
+
t.ok(err === null)
|
|
488
|
+
t.ok(random.equals(bl.slice()))
|
|
489
|
+
t.ok(random.equals(buf.slice()))
|
|
490
|
+
|
|
491
|
+
bl.pipe(fs.createWriteStream('/tmp/bl_test_rnd_out.dat'))
|
|
492
|
+
.on('close', function () {
|
|
493
|
+
const rndhash = crypto.createHash('md5').update(random).digest('hex')
|
|
494
|
+
const md5sum = crypto.createHash('md5')
|
|
495
|
+
const s = fs.createReadStream('/tmp/bl_test_rnd_out.dat')
|
|
496
|
+
|
|
497
|
+
s.on('data', md5sum.update.bind(md5sum))
|
|
498
|
+
s.on('end', function () {
|
|
499
|
+
t.equal(rndhash, md5sum.digest('hex'), 'woohoo! correct hash!')
|
|
500
|
+
t.end()
|
|
501
|
+
})
|
|
470
502
|
})
|
|
503
|
+
})
|
|
471
504
|
|
|
472
505
|
fs.writeFileSync('/tmp/bl_test_rnd.dat', random)
|
|
473
506
|
fs.createReadStream('/tmp/bl_test_rnd.dat').pipe(bl)
|
|
474
507
|
})
|
|
475
508
|
|
|
476
509
|
tape('instantiation with Buffer', function (t) {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
510
|
+
const buf = crypto.randomBytes(1024)
|
|
511
|
+
const buf2 = crypto.randomBytes(1024)
|
|
512
|
+
let b = BufferList(buf)
|
|
480
513
|
|
|
481
514
|
t.equal(buf.toString('hex'), b.slice().toString('hex'), 'same buffer')
|
|
482
|
-
b = BufferList([
|
|
483
|
-
t.equal(b.slice().toString('hex'), Buffer.concat([
|
|
515
|
+
b = BufferList([buf, buf2])
|
|
516
|
+
t.equal(b.slice().toString('hex'), Buffer.concat([buf, buf2]).toString('hex'), 'same buffer')
|
|
517
|
+
|
|
484
518
|
t.end()
|
|
485
519
|
})
|
|
486
520
|
|
|
487
521
|
tape('test String appendage', function (t) {
|
|
488
|
-
|
|
489
|
-
|
|
522
|
+
const bl = new BufferList()
|
|
523
|
+
const b = Buffer.from('abcdefghij\xff\x00')
|
|
490
524
|
|
|
491
525
|
bl.append('abcd')
|
|
492
526
|
bl.append('efg')
|
|
@@ -495,15 +529,15 @@ tape('test String appendage', function (t) {
|
|
|
495
529
|
bl.append('\xff\x00')
|
|
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
|
})
|
|
503
537
|
|
|
504
538
|
tape('test Number appendage', function (t) {
|
|
505
|
-
|
|
506
|
-
|
|
539
|
+
const bl = new BufferList()
|
|
540
|
+
const b = Buffer.from('1234567890')
|
|
507
541
|
|
|
508
542
|
bl.append(1234)
|
|
509
543
|
bl.append(567)
|
|
@@ -511,8 +545,8 @@ tape('test Number appendage', function (t) {
|
|
|
511
545
|
bl.append(0)
|
|
512
546
|
|
|
513
547
|
encodings.forEach(function (enc) {
|
|
514
|
-
|
|
515
|
-
|
|
548
|
+
t.equal(bl.toString(enc), b.toString(enc))
|
|
549
|
+
})
|
|
516
550
|
|
|
517
551
|
t.end()
|
|
518
552
|
})
|
|
@@ -529,10 +563,12 @@ tape('write nothing, should get empty buffer', function (t) {
|
|
|
529
563
|
|
|
530
564
|
tape('unicode string', function (t) {
|
|
531
565
|
t.plan(2)
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
566
|
+
|
|
567
|
+
const inp1 = '\u2600'
|
|
568
|
+
const inp2 = '\u2603'
|
|
569
|
+
const exp = inp1 + ' and ' + inp2
|
|
570
|
+
const bl = BufferList()
|
|
571
|
+
|
|
536
572
|
bl.write(inp1)
|
|
537
573
|
bl.write(' and ')
|
|
538
574
|
bl.write(inp2)
|
|
@@ -541,8 +577,8 @@ tape('unicode string', function (t) {
|
|
|
541
577
|
})
|
|
542
578
|
|
|
543
579
|
tape('should emit finish', function (t) {
|
|
544
|
-
|
|
545
|
-
|
|
580
|
+
const source = BufferList()
|
|
581
|
+
const dest = BufferList()
|
|
546
582
|
|
|
547
583
|
source.write('hello')
|
|
548
584
|
source.pipe(dest)
|
|
@@ -554,90 +590,100 @@ tape('should emit finish', function (t) {
|
|
|
554
590
|
})
|
|
555
591
|
|
|
556
592
|
tape('basic copy', function (t) {
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
593
|
+
const buf = crypto.randomBytes(1024)
|
|
594
|
+
const buf2 = Buffer.alloc(1024)
|
|
595
|
+
const b = BufferList(buf)
|
|
560
596
|
|
|
561
597
|
b.copy(buf2)
|
|
562
598
|
t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
599
|
+
|
|
563
600
|
t.end()
|
|
564
601
|
})
|
|
565
602
|
|
|
566
603
|
tape('copy after many appends', function (t) {
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
604
|
+
const buf = crypto.randomBytes(512)
|
|
605
|
+
const buf2 = Buffer.alloc(1024)
|
|
606
|
+
const b = BufferList(buf)
|
|
570
607
|
|
|
571
608
|
b.append(buf)
|
|
572
609
|
b.copy(buf2)
|
|
573
610
|
t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
611
|
+
|
|
574
612
|
t.end()
|
|
575
613
|
})
|
|
576
614
|
|
|
577
615
|
tape('copy at a precise position', function (t) {
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
616
|
+
const buf = crypto.randomBytes(1004)
|
|
617
|
+
const buf2 = Buffer.alloc(1024)
|
|
618
|
+
const b = BufferList(buf)
|
|
581
619
|
|
|
582
620
|
b.copy(buf2, 20)
|
|
583
621
|
t.equal(b.slice().toString('hex'), buf2.slice(20).toString('hex'), 'same buffer')
|
|
622
|
+
|
|
584
623
|
t.end()
|
|
585
624
|
})
|
|
586
625
|
|
|
587
626
|
tape('copy starting from a precise location', function (t) {
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
627
|
+
const buf = crypto.randomBytes(10)
|
|
628
|
+
const buf2 = Buffer.alloc(5)
|
|
629
|
+
const b = BufferList(buf)
|
|
591
630
|
|
|
592
631
|
b.copy(buf2, 0, 5)
|
|
593
632
|
t.equal(b.slice(5).toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
633
|
+
|
|
594
634
|
t.end()
|
|
595
635
|
})
|
|
596
636
|
|
|
597
637
|
tape('copy in an interval', function (t) {
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
638
|
+
const rnd = crypto.randomBytes(10)
|
|
639
|
+
const b = BufferList(rnd) // put the random bytes there
|
|
640
|
+
const actual = Buffer.alloc(3)
|
|
641
|
+
const expected = Buffer.alloc(3)
|
|
602
642
|
|
|
603
643
|
rnd.copy(expected, 0, 5, 8)
|
|
604
644
|
b.copy(actual, 0, 5, 8)
|
|
605
645
|
|
|
606
646
|
t.equal(actual.toString('hex'), expected.toString('hex'), 'same buffer')
|
|
647
|
+
|
|
607
648
|
t.end()
|
|
608
649
|
})
|
|
609
650
|
|
|
610
651
|
tape('copy an interval between two buffers', function (t) {
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
652
|
+
const buf = crypto.randomBytes(10)
|
|
653
|
+
const buf2 = Buffer.alloc(10)
|
|
654
|
+
const b = BufferList(buf)
|
|
614
655
|
|
|
615
656
|
b.append(buf)
|
|
616
657
|
b.copy(buf2, 0, 5, 15)
|
|
617
658
|
|
|
618
659
|
t.equal(b.slice(5, 15).toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
660
|
+
|
|
619
661
|
t.end()
|
|
620
662
|
})
|
|
621
663
|
|
|
622
664
|
tape('shallow slice across buffer boundaries', function (t) {
|
|
623
|
-
|
|
665
|
+
const bl = new BufferList(['First', 'Second', 'Third'])
|
|
624
666
|
|
|
625
667
|
t.equal(bl.shallowSlice(3, 13).toString(), 'stSecondTh')
|
|
668
|
+
|
|
626
669
|
t.end()
|
|
627
670
|
})
|
|
628
671
|
|
|
629
672
|
tape('shallow slice within single buffer', function (t) {
|
|
630
673
|
t.plan(2)
|
|
631
|
-
|
|
674
|
+
|
|
675
|
+
const bl = new BufferList(['First', 'Second', 'Third'])
|
|
632
676
|
|
|
633
677
|
t.equal(bl.shallowSlice(5, 10).toString(), 'Secon')
|
|
634
678
|
t.equal(bl.shallowSlice(7, 10).toString(), 'con')
|
|
679
|
+
|
|
635
680
|
t.end()
|
|
636
681
|
})
|
|
637
682
|
|
|
638
683
|
tape('shallow slice single buffer', function (t) {
|
|
639
684
|
t.plan(3)
|
|
640
|
-
|
|
685
|
+
|
|
686
|
+
const bl = new BufferList(['First', 'Second', 'Third'])
|
|
641
687
|
|
|
642
688
|
t.equal(bl.shallowSlice(0, 5).toString(), 'First')
|
|
643
689
|
t.equal(bl.shallowSlice(5, 11).toString(), 'Second')
|
|
@@ -646,7 +692,8 @@ tape('shallow slice single buffer', function (t) {
|
|
|
646
692
|
|
|
647
693
|
tape('shallow slice with negative or omitted indices', function (t) {
|
|
648
694
|
t.plan(4)
|
|
649
|
-
|
|
695
|
+
|
|
696
|
+
const bl = new BufferList(['First', 'Second', 'Third'])
|
|
650
697
|
|
|
651
698
|
t.equal(bl.shallowSlice().toString(), 'FirstSecondThird')
|
|
652
699
|
t.equal(bl.shallowSlice(5).toString(), 'SecondThird')
|
|
@@ -656,8 +703,9 @@ tape('shallow slice with negative or omitted indices', function (t) {
|
|
|
656
703
|
|
|
657
704
|
tape('shallow slice does not make a copy', function (t) {
|
|
658
705
|
t.plan(1)
|
|
659
|
-
|
|
660
|
-
|
|
706
|
+
|
|
707
|
+
const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
|
|
708
|
+
const bl = (new BufferList(buffers)).shallowSlice(5, -3)
|
|
661
709
|
|
|
662
710
|
buffers[1].fill('h')
|
|
663
711
|
buffers[2].fill('h')
|
|
@@ -667,23 +715,27 @@ tape('shallow slice does not make a copy', function (t) {
|
|
|
667
715
|
|
|
668
716
|
tape('shallow slice with 0 length', function (t) {
|
|
669
717
|
t.plan(1)
|
|
670
|
-
|
|
671
|
-
|
|
718
|
+
|
|
719
|
+
const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
|
|
720
|
+
const bl = (new BufferList(buffers)).shallowSlice(0, 0)
|
|
721
|
+
|
|
672
722
|
t.equal(bl.length, 0)
|
|
673
723
|
})
|
|
674
724
|
|
|
675
725
|
tape('shallow slice with 0 length from middle', function (t) {
|
|
676
726
|
t.plan(1)
|
|
677
|
-
|
|
678
|
-
|
|
727
|
+
|
|
728
|
+
const buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
|
|
729
|
+
const bl = (new BufferList(buffers)).shallowSlice(10, 10)
|
|
730
|
+
|
|
679
731
|
t.equal(bl.length, 0)
|
|
680
732
|
})
|
|
681
733
|
|
|
682
734
|
tape('duplicate', function (t) {
|
|
683
735
|
t.plan(2)
|
|
684
736
|
|
|
685
|
-
|
|
686
|
-
|
|
737
|
+
const bl = new BufferList('abcdefghij\xff\x00')
|
|
738
|
+
const dup = bl.duplicate()
|
|
687
739
|
|
|
688
740
|
t.equal(bl.prototype, dup.prototype)
|
|
689
741
|
t.equal(bl.toString('hex'), dup.toString('hex'))
|
|
@@ -692,7 +744,8 @@ tape('duplicate', function (t) {
|
|
|
692
744
|
tape('destroy no pipe', function (t) {
|
|
693
745
|
t.plan(2)
|
|
694
746
|
|
|
695
|
-
|
|
747
|
+
const bl = new BufferList('alsdkfja;lsdkfja;lsdk')
|
|
748
|
+
|
|
696
749
|
bl.destroy()
|
|
697
750
|
|
|
698
751
|
t.equal(bl._bufs.length, 0)
|
|
@@ -702,8 +755,9 @@ tape('destroy no pipe', function (t) {
|
|
|
702
755
|
tape('destroy with error', function (t) {
|
|
703
756
|
t.plan(3)
|
|
704
757
|
|
|
705
|
-
|
|
706
|
-
|
|
758
|
+
const bl = new BufferList('alsdkfja;lsdkfja;lsdk')
|
|
759
|
+
const err = new Error('kaboom')
|
|
760
|
+
|
|
707
761
|
bl.destroy(err)
|
|
708
762
|
bl.on('error', function (_err) {
|
|
709
763
|
t.equal(_err, err)
|
|
@@ -716,22 +770,22 @@ tape('destroy with error', function (t) {
|
|
|
716
770
|
!process.browser && tape('destroy with pipe before read end', function (t) {
|
|
717
771
|
t.plan(2)
|
|
718
772
|
|
|
719
|
-
|
|
720
|
-
fs.createReadStream(__dirname
|
|
773
|
+
const bl = new BufferList()
|
|
774
|
+
fs.createReadStream(path.join(__dirname, '/test.js'))
|
|
721
775
|
.pipe(bl)
|
|
722
776
|
|
|
723
777
|
bl.destroy()
|
|
724
778
|
|
|
725
779
|
t.equal(bl._bufs.length, 0)
|
|
726
780
|
t.equal(bl.length, 0)
|
|
727
|
-
|
|
728
781
|
})
|
|
729
782
|
|
|
730
783
|
!process.browser && tape('destroy with pipe before read end with race', function (t) {
|
|
731
784
|
t.plan(2)
|
|
732
785
|
|
|
733
|
-
|
|
734
|
-
|
|
786
|
+
const bl = new BufferList()
|
|
787
|
+
|
|
788
|
+
fs.createReadStream(path.join(__dirname, '/test.js'))
|
|
735
789
|
.pipe(bl)
|
|
736
790
|
|
|
737
791
|
setTimeout(function () {
|
|
@@ -746,8 +800,9 @@ tape('destroy with error', function (t) {
|
|
|
746
800
|
!process.browser && tape('destroy with pipe after read end', function (t) {
|
|
747
801
|
t.plan(2)
|
|
748
802
|
|
|
749
|
-
|
|
750
|
-
|
|
803
|
+
const bl = new BufferList()
|
|
804
|
+
|
|
805
|
+
fs.createReadStream(path.join(__dirname, '/test.js'))
|
|
751
806
|
.on('end', onEnd)
|
|
752
807
|
.pipe(bl)
|
|
753
808
|
|
|
@@ -762,10 +817,10 @@ tape('destroy with error', function (t) {
|
|
|
762
817
|
!process.browser && tape('destroy with pipe while writing to a destination', function (t) {
|
|
763
818
|
t.plan(4)
|
|
764
819
|
|
|
765
|
-
|
|
766
|
-
|
|
820
|
+
const bl = new BufferList()
|
|
821
|
+
const ds = new BufferList()
|
|
767
822
|
|
|
768
|
-
fs.createReadStream(__dirname
|
|
823
|
+
fs.createReadStream(path.join(__dirname, '/test.js'))
|
|
769
824
|
.on('end', onEnd)
|
|
770
825
|
.pipe(bl)
|
|
771
826
|
|
|
@@ -782,13 +837,13 @@ tape('destroy with error', function (t) {
|
|
|
782
837
|
|
|
783
838
|
t.equals(bl._bufs.length, 0)
|
|
784
839
|
t.equals(bl.length, 0)
|
|
785
|
-
|
|
786
840
|
}, 100)
|
|
787
841
|
}
|
|
788
842
|
})
|
|
789
843
|
|
|
790
844
|
!process.browser && tape('handle error', function (t) {
|
|
791
845
|
t.plan(2)
|
|
846
|
+
|
|
792
847
|
fs.createReadStream('/does/not/exist').pipe(BufferList(function (err, data) {
|
|
793
848
|
t.ok(err instanceof Error, 'has error')
|
|
794
849
|
t.notOk(data, 'no data')
|