bl 4.0.1 → 4.1.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 +12 -2
- package/bl.js +2 -2
- package/package.json +4 -2
- package/test/convert.js +1 -1
- package/test/indexOf.js +1 -1
- package/test/isBufferList.js +1 -1
- package/test/test.js +37 -3
package/.travis.yml
CHANGED
package/BufferList.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const { Buffer } = require('buffer')
|
|
3
4
|
const symbol = Symbol.for('BufferList')
|
|
4
5
|
|
|
5
6
|
function BufferList (buf) {
|
|
@@ -133,12 +134,13 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
|
|
|
133
134
|
|
|
134
135
|
if (bytes > l) {
|
|
135
136
|
this._bufs[i].copy(dst, bufoff, start)
|
|
137
|
+
bufoff += l
|
|
136
138
|
} else {
|
|
137
139
|
this._bufs[i].copy(dst, bufoff, start, start + bytes)
|
|
140
|
+
bufoff += l
|
|
138
141
|
break
|
|
139
142
|
}
|
|
140
143
|
|
|
141
|
-
bufoff += l
|
|
142
144
|
bytes -= l
|
|
143
145
|
|
|
144
146
|
if (start) {
|
|
@@ -146,6 +148,9 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
|
|
|
146
148
|
}
|
|
147
149
|
}
|
|
148
150
|
|
|
151
|
+
// safeguard so that we don't return uninitialized memory
|
|
152
|
+
if (dst.length > bufoff) return dst.slice(0, bufoff)
|
|
153
|
+
|
|
149
154
|
return dst
|
|
150
155
|
}
|
|
151
156
|
|
|
@@ -187,6 +192,11 @@ BufferList.prototype.toString = function toString (encoding, start, end) {
|
|
|
187
192
|
}
|
|
188
193
|
|
|
189
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
|
+
|
|
190
200
|
while (this._bufs.length) {
|
|
191
201
|
if (bytes >= this._bufs[0].length) {
|
|
192
202
|
bytes -= this._bufs[0].length
|
|
@@ -363,7 +373,7 @@ BufferList.prototype._match = function (offset, search) {
|
|
|
363
373
|
return this.slice(offset, offset + byteLength)[m](0, byteLength)
|
|
364
374
|
}
|
|
365
375
|
} else {
|
|
366
|
-
BufferList.prototype[m] = function (offset) {
|
|
376
|
+
BufferList.prototype[m] = function (offset = 0) {
|
|
367
377
|
return this.slice(offset, offset + methods[m])[m](0)
|
|
368
378
|
}
|
|
369
379
|
}
|
package/bl.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const DuplexStream = require('readable-stream').Duplex
|
|
4
|
-
const
|
|
4
|
+
const inherits = require('inherits')
|
|
5
5
|
const BufferList = require('./BufferList')
|
|
6
6
|
|
|
7
7
|
function BufferListStream (callback) {
|
|
@@ -33,7 +33,7 @@ function BufferListStream (callback) {
|
|
|
33
33
|
DuplexStream.call(this)
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
inherits(BufferListStream, DuplexStream)
|
|
37
37
|
Object.assign(BufferListStream.prototype, BufferList.prototype)
|
|
38
38
|
|
|
39
39
|
BufferListStream.prototype._new = function _new (callback) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bl",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.1.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,8 +24,9 @@
|
|
|
23
24
|
"stream",
|
|
24
25
|
"awesomesauce"
|
|
25
26
|
],
|
|
26
|
-
"license": "MIT",
|
|
27
27
|
"dependencies": {
|
|
28
|
+
"buffer": "^5.5.0",
|
|
29
|
+
"inherits": "^2.0.4",
|
|
28
30
|
"readable-stream": "^3.4.0"
|
|
29
31
|
},
|
|
30
32
|
"devDependencies": {
|
package/test/convert.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const tape = require('tape')
|
|
4
4
|
const { BufferList, BufferListStream } = require('../')
|
|
5
|
-
const { Buffer } = require('
|
|
5
|
+
const { Buffer } = require('buffer')
|
|
6
6
|
|
|
7
7
|
tape('convert from BufferList to BufferListStream', (t) => {
|
|
8
8
|
const data = Buffer.from(`TEST-${Date.now()}`)
|
package/test/indexOf.js
CHANGED
package/test/isBufferList.js
CHANGED
package/test/test.js
CHANGED
|
@@ -5,7 +5,7 @@ const crypto = require('crypto')
|
|
|
5
5
|
const fs = require('fs')
|
|
6
6
|
const path = require('path')
|
|
7
7
|
const BufferList = require('../')
|
|
8
|
-
const Buffer = require('
|
|
8
|
+
const { Buffer } = require('buffer')
|
|
9
9
|
|
|
10
10
|
const encodings =
|
|
11
11
|
('hex utf8 utf-8 ascii binary base64' +
|
|
@@ -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
|
|