bl 4.0.2 → 5.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/.travis.yml +5 -4
- package/BufferList.js +11 -2
- package/package.json +5 -5
- package/test/test.js +36 -2
package/.travis.yml
CHANGED
package/BufferList.js
CHANGED
|
@@ -134,12 +134,13 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
|
|
|
134
134
|
|
|
135
135
|
if (bytes > l) {
|
|
136
136
|
this._bufs[i].copy(dst, bufoff, start)
|
|
137
|
+
bufoff += l
|
|
137
138
|
} else {
|
|
138
139
|
this._bufs[i].copy(dst, bufoff, start, start + bytes)
|
|
140
|
+
bufoff += l
|
|
139
141
|
break
|
|
140
142
|
}
|
|
141
143
|
|
|
142
|
-
bufoff += l
|
|
143
144
|
bytes -= l
|
|
144
145
|
|
|
145
146
|
if (start) {
|
|
@@ -147,6 +148,9 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
|
|
|
147
148
|
}
|
|
148
149
|
}
|
|
149
150
|
|
|
151
|
+
// safeguard so that we don't return uninitialized memory
|
|
152
|
+
if (dst.length > bufoff) return dst.slice(0, bufoff)
|
|
153
|
+
|
|
150
154
|
return dst
|
|
151
155
|
}
|
|
152
156
|
|
|
@@ -188,6 +192,11 @@ BufferList.prototype.toString = function toString (encoding, start, end) {
|
|
|
188
192
|
}
|
|
189
193
|
|
|
190
194
|
BufferList.prototype.consume = function consume (bytes) {
|
|
195
|
+
// first, normalize the argument, in accordance with how Buffer does it
|
|
196
|
+
bytes = Math.trunc(bytes)
|
|
197
|
+
// do nothing if not a positive number
|
|
198
|
+
if (Number.isNaN(bytes) || bytes <= 0) return this
|
|
199
|
+
|
|
191
200
|
while (this._bufs.length) {
|
|
192
201
|
if (bytes >= this._bufs[0].length) {
|
|
193
202
|
bytes -= this._bufs[0].length
|
|
@@ -364,7 +373,7 @@ BufferList.prototype._match = function (offset, search) {
|
|
|
364
373
|
return this.slice(offset, offset + byteLength)[m](0, byteLength)
|
|
365
374
|
}
|
|
366
375
|
} else {
|
|
367
|
-
BufferList.prototype[m] = function (offset) {
|
|
376
|
+
BufferList.prototype[m] = function (offset = 0) {
|
|
368
377
|
return this.slice(offset, offset + methods[m])[m](0)
|
|
369
378
|
}
|
|
370
379
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bl",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "Buffer List: collect buffers and access with a standard readable Buffer interface, streamable too!",
|
|
5
|
+
"license": "MIT",
|
|
5
6
|
"main": "bl.js",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"lint": "standard *.js test/*.js",
|
|
@@ -23,15 +24,14 @@
|
|
|
23
24
|
"stream",
|
|
24
25
|
"awesomesauce"
|
|
25
26
|
],
|
|
26
|
-
"license": "MIT",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"buffer": "^
|
|
28
|
+
"buffer": "^6.0.3",
|
|
29
29
|
"inherits": "^2.0.4",
|
|
30
30
|
"readable-stream": "^3.4.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"faucet": "~0.0.1",
|
|
34
|
-
"standard": "^
|
|
35
|
-
"tape": "^
|
|
34
|
+
"standard": "^16.0.3",
|
|
35
|
+
"tape": "^5.2.2"
|
|
36
36
|
}
|
|
37
37
|
}
|
package/test/test.js
CHANGED
|
@@ -265,6 +265,7 @@ tape('test readUInt8 / readInt8', function (t) {
|
|
|
265
265
|
const buf3 = Buffer.alloc(3)
|
|
266
266
|
const bl = new BufferList()
|
|
267
267
|
|
|
268
|
+
buf1[0] = 0x1
|
|
268
269
|
buf2[1] = 0x3
|
|
269
270
|
buf2[2] = 0x4
|
|
270
271
|
buf3[0] = 0x23
|
|
@@ -274,6 +275,7 @@ tape('test readUInt8 / readInt8', function (t) {
|
|
|
274
275
|
bl.append(buf2)
|
|
275
276
|
bl.append(buf3)
|
|
276
277
|
|
|
278
|
+
t.equal(bl.readUInt8(), 0x1)
|
|
277
279
|
t.equal(bl.readUInt8(2), 0x3)
|
|
278
280
|
t.equal(bl.readInt8(2), 0x3)
|
|
279
281
|
t.equal(bl.readUInt8(3), 0x4)
|
|
@@ -292,6 +294,7 @@ tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t
|
|
|
292
294
|
const buf3 = Buffer.alloc(3)
|
|
293
295
|
const bl = new BufferList()
|
|
294
296
|
|
|
297
|
+
buf1[0] = 0x1
|
|
295
298
|
buf2[1] = 0x3
|
|
296
299
|
buf2[2] = 0x4
|
|
297
300
|
buf3[0] = 0x23
|
|
@@ -301,6 +304,8 @@ tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t
|
|
|
301
304
|
bl.append(buf2)
|
|
302
305
|
bl.append(buf3)
|
|
303
306
|
|
|
307
|
+
t.equal(bl.readUInt16BE(), 0x0100)
|
|
308
|
+
t.equal(bl.readUInt16LE(), 0x0001)
|
|
304
309
|
t.equal(bl.readUInt16BE(2), 0x0304)
|
|
305
310
|
t.equal(bl.readUInt16LE(2), 0x0403)
|
|
306
311
|
t.equal(bl.readInt16BE(2), 0x0304)
|
|
@@ -323,6 +328,7 @@ tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t
|
|
|
323
328
|
const buf3 = Buffer.alloc(3)
|
|
324
329
|
const bl = new BufferList()
|
|
325
330
|
|
|
331
|
+
buf1[0] = 0x1
|
|
326
332
|
buf2[1] = 0x3
|
|
327
333
|
buf2[2] = 0x4
|
|
328
334
|
buf3[0] = 0x23
|
|
@@ -332,6 +338,8 @@ tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t
|
|
|
332
338
|
bl.append(buf2)
|
|
333
339
|
bl.append(buf3)
|
|
334
340
|
|
|
341
|
+
t.equal(bl.readUInt32BE(), 0x01000304)
|
|
342
|
+
t.equal(bl.readUInt32LE(), 0x04030001)
|
|
335
343
|
t.equal(bl.readUInt32BE(2), 0x03042342)
|
|
336
344
|
t.equal(bl.readUInt32LE(2), 0x42230403)
|
|
337
345
|
t.equal(bl.readInt32BE(2), 0x03042342)
|
|
@@ -391,6 +399,7 @@ tape('test readFloatLE / readFloatBE', function (t) {
|
|
|
391
399
|
const buf3 = Buffer.alloc(3)
|
|
392
400
|
const bl = new BufferList()
|
|
393
401
|
|
|
402
|
+
buf1[0] = 0x01
|
|
394
403
|
buf2[1] = 0x00
|
|
395
404
|
buf2[2] = 0x00
|
|
396
405
|
buf3[0] = 0x80
|
|
@@ -400,7 +409,11 @@ tape('test readFloatLE / readFloatBE', function (t) {
|
|
|
400
409
|
bl.append(buf2)
|
|
401
410
|
bl.append(buf3)
|
|
402
411
|
|
|
403
|
-
|
|
412
|
+
const canonical = Buffer.concat([buf1, buf2, buf3])
|
|
413
|
+
t.equal(bl.readFloatLE(), canonical.readFloatLE())
|
|
414
|
+
t.equal(bl.readFloatBE(), canonical.readFloatBE())
|
|
415
|
+
t.equal(bl.readFloatLE(2), canonical.readFloatLE(2))
|
|
416
|
+
t.equal(bl.readFloatBE(2), canonical.readFloatBE(2))
|
|
404
417
|
|
|
405
418
|
t.end()
|
|
406
419
|
})
|
|
@@ -411,6 +424,7 @@ tape('test readDoubleLE / readDoubleBE', function (t) {
|
|
|
411
424
|
const buf3 = Buffer.alloc(10)
|
|
412
425
|
const bl = new BufferList()
|
|
413
426
|
|
|
427
|
+
buf1[0] = 0x01
|
|
414
428
|
buf2[1] = 0x55
|
|
415
429
|
buf2[2] = 0x55
|
|
416
430
|
buf3[0] = 0x55
|
|
@@ -424,7 +438,11 @@ tape('test readDoubleLE / readDoubleBE', function (t) {
|
|
|
424
438
|
bl.append(buf2)
|
|
425
439
|
bl.append(buf3)
|
|
426
440
|
|
|
427
|
-
|
|
441
|
+
const canonical = Buffer.concat([buf1, buf2, buf3])
|
|
442
|
+
t.equal(bl.readDoubleBE(), canonical.readDoubleBE())
|
|
443
|
+
t.equal(bl.readDoubleLE(), canonical.readDoubleLE())
|
|
444
|
+
t.equal(bl.readDoubleBE(2), canonical.readDoubleBE(2))
|
|
445
|
+
t.equal(bl.readDoubleLE(2), canonical.readDoubleLE(2))
|
|
428
446
|
|
|
429
447
|
t.end()
|
|
430
448
|
})
|
|
@@ -463,6 +481,22 @@ tape('test toString encoding', function (t) {
|
|
|
463
481
|
t.end()
|
|
464
482
|
})
|
|
465
483
|
|
|
484
|
+
tape('uninitialized memory', function (t) {
|
|
485
|
+
const secret = crypto.randomBytes(256)
|
|
486
|
+
for (let i = 0; i < 1e6; i++) {
|
|
487
|
+
const clone = Buffer.from(secret)
|
|
488
|
+
const bl = new BufferList()
|
|
489
|
+
bl.append(Buffer.from('a'))
|
|
490
|
+
bl.consume(-1024)
|
|
491
|
+
const buf = bl.slice(1)
|
|
492
|
+
if (buf.indexOf(clone) !== -1) {
|
|
493
|
+
t.fail(`Match (at ${i})`)
|
|
494
|
+
break
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
t.end()
|
|
498
|
+
})
|
|
499
|
+
|
|
466
500
|
!process.browser && tape('test stream', function (t) {
|
|
467
501
|
const random = crypto.randomBytes(65534)
|
|
468
502
|
|