bl 2.1.2 → 3.0.1

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 CHANGED
@@ -1,10 +1,8 @@
1
1
  sudo: false
2
2
  language: node_js
3
3
  node_js:
4
- - '4'
5
4
  - '6'
6
5
  - '8'
7
- - '9'
8
6
  - '10'
9
7
  branches:
10
8
  only:
package/README.md CHANGED
@@ -15,11 +15,11 @@ The original buffers are kept intact and copies are only done as necessary. Any
15
15
  const BufferList = require('bl')
16
16
 
17
17
  var bl = new BufferList()
18
- bl.append(new Buffer('abcd'))
19
- bl.append(new Buffer('efg'))
18
+ bl.append(Buffer.from('abcd'))
19
+ bl.append(Buffer.from('efg'))
20
20
  bl.append('hi') // bl will also accept & convert Strings
21
- bl.append(new Buffer('j'))
22
- bl.append(new Buffer([ 0x3, 0x4 ]))
21
+ bl.append(Buffer.from('j'))
22
+ bl.append(Buffer.from([ 0x3, 0x4 ]))
23
23
 
24
24
  console.log(bl.length) // 12
25
25
 
@@ -29,6 +29,9 @@ console.log(bl.slice(3, 6).toString('ascii')) // 'def'
29
29
  console.log(bl.slice(3, 8).toString('ascii')) // 'defgh'
30
30
  console.log(bl.slice(5, 10).toString('ascii')) // 'fghij'
31
31
 
32
+ console.log(bl.indexOf('def')) // 3
33
+ console.log(bl.indexOf('asdf')) // -1
34
+
32
35
  // or just use toString!
33
36
  console.log(bl.toString()) // 'abcdefghij\u0003\u0004'
34
37
  console.log(bl.toString('ascii', 3, 8)) // 'defgh'
@@ -72,10 +75,10 @@ const BufferList = require('bl')
72
75
  , fs = require('fs')
73
76
 
74
77
  var bl = new BufferList()
75
- bl.append(new Buffer('abcd'))
76
- bl.append(new Buffer('efg'))
77
- bl.append(new Buffer('hi'))
78
- bl.append(new Buffer('j'))
78
+ bl.append(Buffer.from('abcd'))
79
+ bl.append(Buffer.from('efg'))
80
+ bl.append(Buffer.from('hi'))
81
+ bl.append(Buffer.from('j'))
79
82
 
80
83
  bl.pipe(fs.createWriteStream('gibberish.txt'))
81
84
  ```
@@ -86,6 +89,7 @@ bl.pipe(fs.createWriteStream('gibberish.txt'))
86
89
  * <a href="#length"><code>bl.<b>length</b></code></a>
87
90
  * <a href="#append"><code>bl.<b>append(buffer)</b></code></a>
88
91
  * <a href="#get"><code>bl.<b>get(index)</b></code></a>
92
+ * <a href="#indexOf"><code>bl.<b>indexOf(value[, byteOffset][, encoding])</b></code></a>
89
93
  * <a href="#slice"><code>bl.<b>slice([ start[, end ] ])</b></code></a>
90
94
  * <a href="#shallowSlice"><code>bl.<b>shallowSlice([ start[, end ] ])</b></code></a>
91
95
  * <a href="#copy"><code>bl.<b>copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ])</b></code></a>
@@ -129,6 +133,12 @@ Get the length of the list in bytes. This is the sum of the lengths of all of th
129
133
  ### bl.get(index)
130
134
  `get()` will return the byte at the specified index.
131
135
 
136
+ --------------------------------------------------------
137
+ <a name="indexOf"></a>
138
+ ### bl.indexOf(value[, byteOffset][, encoding])
139
+ `get()` will return the byte at the specified index.
140
+ `indexOf()` method returns the first index at which a given element can be found in the BufferList, or -1 if it is not present.
141
+
132
142
  --------------------------------------------------------
133
143
  <a name="slice"></a>
134
144
  ### bl.slice([ start, [ end ] ])
package/bl.js CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
  var DuplexStream = require('readable-stream').Duplex
3
3
  , util = require('util')
4
- , Buffer = require('safe-buffer').Buffer
5
4
 
6
5
  function BufferList (callback) {
7
6
  if (!(this instanceof BufferList))
@@ -186,30 +185,37 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
186
185
 
187
186
  if (bytes > l) {
188
187
  this._bufs[i].copy(dst, bufoff, start)
188
+ bufoff += l
189
189
  } else {
190
190
  this._bufs[i].copy(dst, bufoff, start, start + bytes)
191
+ bufoff += l
191
192
  break
192
193
  }
193
194
 
194
- bufoff += l
195
195
  bytes -= l
196
196
 
197
197
  if (start)
198
198
  start = 0
199
199
  }
200
200
 
201
+ // safeguard so that we don't return uninitialized memory
202
+ if (dst.length > bufoff) return dst.slice(0, bufoff)
203
+
201
204
  return dst
202
205
  }
203
206
 
204
207
  BufferList.prototype.shallowSlice = function shallowSlice (start, end) {
205
208
  start = start || 0
206
- end = end || this.length
209
+ end = typeof end !== 'number' ? this.length : end
207
210
 
208
211
  if (start < 0)
209
212
  start += this.length
210
213
  if (end < 0)
211
214
  end += this.length
212
215
 
216
+ if (start === end) {
217
+ return new BufferList()
218
+ }
213
219
  var startOffset = this._offset(start)
214
220
  , endOffset = this._offset(end)
215
221
  , buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
@@ -230,6 +236,11 @@ BufferList.prototype.toString = function toString (encoding, start, end) {
230
236
  }
231
237
 
232
238
  BufferList.prototype.consume = function consume (bytes) {
239
+ // first, normalize the argument, in accordance with how Buffer does it
240
+ bytes = Math.trunc(bytes)
241
+ // do nothing if not a positive number
242
+ if (Number.isNaN(bytes) || bytes <= 0) return this
243
+
233
244
  while (this._bufs.length) {
234
245
  if (bytes >= this._bufs[0].length) {
235
246
  bytes -= this._bufs[0].length
@@ -256,10 +267,10 @@ BufferList.prototype.duplicate = function duplicate () {
256
267
  }
257
268
 
258
269
 
259
- BufferList.prototype.destroy = function destroy () {
270
+ BufferList.prototype._destroy = function _destroy (err, cb) {
260
271
  this._bufs.length = 0
261
272
  this.length = 0
262
- this.push(null)
273
+ cb(err)
263
274
  }
264
275
 
265
276
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bl",
3
- "version": "2.1.2",
3
+ "version": "3.0.1",
4
4
  "description": "Buffer List: collect buffers and access with a standard readable Buffer interface, streamable too!",
5
5
  "main": "bl.js",
6
6
  "scripts": {
@@ -24,12 +24,11 @@
24
24
  ],
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
- "readable-stream": "^2.3.5",
28
- "safe-buffer": "^5.1.1"
27
+ "readable-stream": "^3.0.1"
29
28
  },
30
29
  "devDependencies": {
31
30
  "faucet": "0.0.1",
32
31
  "hash_file": "~0.1.1",
33
- "tape": "~4.9.0"
32
+ "tape": "~4.9.1"
34
33
  }
35
34
  }
package/test/test.js CHANGED
@@ -431,6 +431,22 @@ tape('test toString encoding', function (t) {
431
431
  t.end()
432
432
  })
433
433
 
434
+ tape('uninitialized memory', function (t) {
435
+ const secret = crypto.randomBytes(256)
436
+ for (let i = 0; i < 1e6; i++) {
437
+ const clone = Buffer.from(secret)
438
+ const bl = new BufferList()
439
+ bl.append(Buffer.from('a'))
440
+ bl.consume(-1024)
441
+ const buf = bl.slice(1)
442
+ if (buf.indexOf(clone) !== -1) {
443
+ t.fail(`Match (at ${i})`)
444
+ break
445
+ }
446
+ }
447
+ t.end()
448
+ })
449
+
434
450
  !process.browser && tape('test stream', function (t) {
435
451
  var random = crypto.randomBytes(65534)
436
452
  , rndhash = hash(random, 'md5')
@@ -649,6 +665,20 @@ tape('shallow slice does not make a copy', function (t) {
649
665
  t.equal(bl.toString(), 'hhhhhhhh')
650
666
  })
651
667
 
668
+ tape('shallow slice with 0 length', function (t) {
669
+ t.plan(1)
670
+ var buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
671
+ var bl = (new BufferList(buffers)).shallowSlice(0, 0)
672
+ t.equal(bl.length, 0)
673
+ })
674
+
675
+ tape('shallow slice with 0 length from middle', function (t) {
676
+ t.plan(1)
677
+ var buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
678
+ var bl = (new BufferList(buffers)).shallowSlice(10, 10)
679
+ t.equal(bl.length, 0)
680
+ })
681
+
652
682
  tape('duplicate', function (t) {
653
683
  t.plan(2)
654
684
 
@@ -669,6 +699,20 @@ tape('destroy no pipe', function (t) {
669
699
  t.equal(bl.length, 0)
670
700
  })
671
701
 
702
+ tape('destroy with error', function (t) {
703
+ t.plan(3)
704
+
705
+ var bl = new BufferList('alsdkfja;lsdkfja;lsdk')
706
+ var err = new Error('kaboom')
707
+ bl.destroy(err)
708
+ bl.on('error', function (_err) {
709
+ t.equal(_err, err)
710
+ })
711
+
712
+ t.equal(bl._bufs.length, 0)
713
+ t.equal(bl.length, 0)
714
+ })
715
+
672
716
  !process.browser && tape('destroy with pipe before read end', function (t) {
673
717
  t.plan(2)
674
718