bl 1.1.2 → 1.2.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 CHANGED
@@ -1,13 +1,13 @@
1
1
  sudo: false
2
2
  language: node_js
3
3
  node_js:
4
- - '0.10'
5
- - '0.12'
6
- - '4'
7
- - '5'
8
- branches:
9
- only:
10
- - master
4
+ - '6'
5
+ - '8'
6
+ - '10'
7
+ - '12'
8
+ - '14'
9
+ - lts/*
11
10
  notifications:
12
11
  email:
13
12
  - rod@vagg.org
13
+ - matteo.collina@gmail.com
package/LICENSE.md CHANGED
@@ -1,7 +1,7 @@
1
1
  The MIT License (MIT)
2
2
  =====================
3
3
 
4
- Copyright (c) 2014 bl contributors
4
+ Copyright (c) 2013-2016 bl contributors
5
5
  ----------------------------------
6
6
 
7
7
  *bl contributors listed at <https://github.com/rvagg/bl#contributors>*
package/README.md CHANGED
@@ -87,6 +87,7 @@ bl.pipe(fs.createWriteStream('gibberish.txt'))
87
87
  * <a href="#append"><code>bl.<b>append(buffer)</b></code></a>
88
88
  * <a href="#get"><code>bl.<b>get(index)</b></code></a>
89
89
  * <a href="#slice"><code>bl.<b>slice([ start[, end ] ])</b></code></a>
90
+ * <a href="#shallowSlice"><code>bl.<b>shallowSlice([ start[, end ] ])</b></code></a>
90
91
  * <a href="#copy"><code>bl.<b>copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ])</b></code></a>
91
92
  * <a href="#duplicate"><code>bl.<b>duplicate()</b></code></a>
92
93
  * <a href="#consume"><code>bl.<b>consume(bytes)</b></code></a>
@@ -107,7 +108,7 @@ Normally, no arguments are required for the constructor, but you can initialise
107
108
  var bl = require('bl')
108
109
  var myinstance = bl()
109
110
 
110
- // equivilant to:
111
+ // equivalent to:
111
112
 
112
113
  var BufferList = require('bl')
113
114
  var myinstance = new BufferList()
@@ -135,6 +136,13 @@ Get the length of the list in bytes. This is the sum of the lengths of all of th
135
136
 
136
137
  If the requested range spans a single internal buffer then a slice of that buffer will be returned which shares the original memory range of that Buffer. If the range spans multiple buffers then copy operations will likely occur to give you a uniform Buffer.
137
138
 
139
+ --------------------------------------------------------
140
+ <a name="shallowSlice"></a>
141
+ ### bl.shallowSlice([ start, [ end ] ])
142
+ `shallowSlice()` returns a new `BufferList` object containing the bytes within the range specified. Both `start` and `end` are optional and will default to the beginning and end of the list respectively.
143
+
144
+ No copies will be performed. All buffers in the result share memory with the original list.
145
+
138
146
  --------------------------------------------------------
139
147
  <a name="copy"></a>
140
148
  ### bl.copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ])
@@ -195,6 +203,6 @@ See the <b><code>[Buffer](http://nodejs.org/docs/latest/api/buffer.html)</code><
195
203
  <a name="license"></a>
196
204
  ## License &amp; copyright
197
205
 
198
- Copyright (c) 2013-2014 bl contributors (listed above).
206
+ Copyright (c) 2013-2016 bl contributors (listed above).
199
207
 
200
208
  bl is licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details.
package/bl.js CHANGED
@@ -1,5 +1,6 @@
1
1
  var DuplexStream = require('readable-stream/duplex')
2
2
  , util = require('util')
3
+ , Buffer = require('safe-buffer').Buffer
3
4
 
4
5
 
5
6
  function BufferList (callback) {
@@ -38,9 +39,10 @@ util.inherits(BufferList, DuplexStream)
38
39
 
39
40
  BufferList.prototype._offset = function _offset (offset) {
40
41
  var tot = 0, i = 0, _t
42
+ if (offset === 0) return [ 0, 0 ]
41
43
  for (; i < this._bufs.length; i++) {
42
44
  _t = tot + this._bufs[i].length
43
- if (offset < _t)
45
+ if (offset < _t || i == this._bufs.length - 1)
44
46
  return [ i, offset - tot ]
45
47
  tot = _t
46
48
  }
@@ -49,9 +51,10 @@ BufferList.prototype._offset = function _offset (offset) {
49
51
 
50
52
  BufferList.prototype.append = function append (buf) {
51
53
  var i = 0
52
- , newBuf
53
54
 
54
- if (Array.isArray(buf)) {
55
+ if (Buffer.isBuffer(buf)) {
56
+ this._appendBuffer(buf);
57
+ } else if (Array.isArray(buf)) {
55
58
  for (; i < buf.length; i++)
56
59
  this.append(buf[i])
57
60
  } else if (buf instanceof BufferList) {
@@ -64,17 +67,21 @@ BufferList.prototype.append = function append (buf) {
64
67
  if (typeof buf == 'number')
65
68
  buf = buf.toString()
66
69
 
67
- newBuf = Buffer.isBuffer(buf) ? buf : new Buffer(buf)
68
- this._bufs.push(newBuf)
69
- this.length += newBuf.length
70
+ this._appendBuffer(Buffer.from(buf));
70
71
  }
71
72
 
72
73
  return this
73
74
  }
74
75
 
75
76
 
77
+ BufferList.prototype._appendBuffer = function appendBuffer (buf) {
78
+ this._bufs.push(buf)
79
+ this.length += buf.length
80
+ }
81
+
82
+
76
83
  BufferList.prototype._write = function _write (buf, encoding, callback) {
77
- this.append(buf)
84
+ this._appendBuffer(buf)
78
85
 
79
86
  if (typeof callback == 'function')
80
87
  callback()
@@ -107,6 +114,10 @@ BufferList.prototype.get = function get (index) {
107
114
 
108
115
 
109
116
  BufferList.prototype.slice = function slice (start, end) {
117
+ if (typeof start == 'number' && start < 0)
118
+ start += this.length
119
+ if (typeof end == 'number' && end < 0)
120
+ end += this.length
110
121
  return this.copy(null, 0, start, end)
111
122
  }
112
123
 
@@ -117,9 +128,9 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
117
128
  if (typeof srcEnd != 'number' || srcEnd > this.length)
118
129
  srcEnd = this.length
119
130
  if (srcStart >= this.length)
120
- return dst || new Buffer(0)
131
+ return dst || Buffer.alloc(0)
121
132
  if (srcEnd <= 0)
122
- return dst || new Buffer(0)
133
+ return dst || Buffer.alloc(0)
123
134
 
124
135
  var copy = !!dst
125
136
  , off = this._offset(srcStart)
@@ -132,8 +143,11 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
132
143
 
133
144
  // copy/slice everything
134
145
  if (srcStart === 0 && srcEnd == this.length) {
135
- if (!copy) // slice, just return a full concat
136
- return Buffer.concat(this._bufs)
146
+ if (!copy) { // slice, but full concat if multiple buffers
147
+ return this._bufs.length === 1
148
+ ? this._bufs[0]
149
+ : Buffer.concat(this._bufs, this.length)
150
+ }
137
151
 
138
152
  // copy, need to copy individual buffers
139
153
  for (i = 0; i < this._bufs.length; i++) {
@@ -152,33 +166,66 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
152
166
  }
153
167
 
154
168
  if (!copy) // a slice, we need something to copy in to
155
- dst = new Buffer(len)
169
+ dst = Buffer.allocUnsafe(len)
156
170
 
157
171
  for (i = off[0]; i < this._bufs.length; i++) {
158
172
  l = this._bufs[i].length - start
159
173
 
160
174
  if (bytes > l) {
161
175
  this._bufs[i].copy(dst, bufoff, start)
176
+ bufoff += l
162
177
  } else {
163
178
  this._bufs[i].copy(dst, bufoff, start, start + bytes)
179
+ bufoff += l
164
180
  break
165
181
  }
166
182
 
167
- bufoff += l
168
183
  bytes -= l
169
184
 
170
185
  if (start)
171
186
  start = 0
172
187
  }
173
188
 
189
+ // safeguard so that we don't return uninitialized memory
190
+ if (dst.length > bufoff) return dst.slice(0, bufoff)
191
+
174
192
  return dst
175
193
  }
176
194
 
195
+ BufferList.prototype.shallowSlice = function shallowSlice (start, end) {
196
+ start = start || 0
197
+ end = end || this.length
198
+
199
+ if (start < 0)
200
+ start += this.length
201
+ if (end < 0)
202
+ end += this.length
203
+
204
+ var startOffset = this._offset(start)
205
+ , endOffset = this._offset(end)
206
+ , buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
207
+
208
+ if (endOffset[1] == 0)
209
+ buffers.pop()
210
+ else
211
+ buffers[buffers.length-1] = buffers[buffers.length-1].slice(0, endOffset[1])
212
+
213
+ if (startOffset[1] != 0)
214
+ buffers[0] = buffers[0].slice(startOffset[1])
215
+
216
+ return new BufferList(buffers)
217
+ }
218
+
177
219
  BufferList.prototype.toString = function toString (encoding, start, end) {
178
220
  return this.slice(start, end).toString(encoding)
179
221
  }
180
222
 
181
223
  BufferList.prototype.consume = function consume (bytes) {
224
+ // first, normalize the argument, in accordance with how Buffer does it
225
+ bytes = Math.trunc(bytes)
226
+ // do nothing if not a positive number
227
+ if (Number.isNaN(bytes) || bytes <= 0) return this
228
+
182
229
  while (this._bufs.length) {
183
230
  if (bytes >= this._bufs[0].length) {
184
231
  bytes -= this._bufs[0].length
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bl",
3
- "version": "1.1.2",
3
+ "version": "1.2.3",
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,11 +24,12 @@
24
24
  ],
25
25
  "license": "MIT",
26
26
  "dependencies": {
27
- "readable-stream": "~2.0.5"
27
+ "readable-stream": "^2.3.5",
28
+ "safe-buffer": "^5.1.1"
28
29
  },
29
30
  "devDependencies": {
30
31
  "faucet": "0.0.1",
31
32
  "hash_file": "~0.1.1",
32
- "tape": "~4.4.0"
33
+ "tape": "~4.9.0"
33
34
  }
34
35
  }
package/test/test.js CHANGED
@@ -3,6 +3,7 @@ var tape = require('tape')
3
3
  , fs = require('fs')
4
4
  , hash = require('hash_file')
5
5
  , BufferList = require('../')
6
+ , Buffer = require('safe-buffer').Buffer
6
7
 
7
8
  , encodings =
8
9
  ('hex utf8 utf-8 ascii binary base64'
@@ -10,7 +11,7 @@ var tape = require('tape')
10
11
 
11
12
  tape('single bytes from single buffer', function (t) {
12
13
  var bl = new BufferList()
13
- bl.append(new Buffer('abcd'))
14
+ bl.append(Buffer.from('abcd'))
14
15
 
15
16
  t.equal(bl.length, 4)
16
17
 
@@ -24,10 +25,10 @@ tape('single bytes from single buffer', function (t) {
24
25
 
25
26
  tape('single bytes from multiple buffers', function (t) {
26
27
  var bl = new BufferList()
27
- bl.append(new Buffer('abcd'))
28
- bl.append(new Buffer('efg'))
29
- bl.append(new Buffer('hi'))
30
- bl.append(new Buffer('j'))
28
+ bl.append(Buffer.from('abcd'))
29
+ bl.append(Buffer.from('efg'))
30
+ bl.append(Buffer.from('hi'))
31
+ bl.append(Buffer.from('j'))
31
32
 
32
33
  t.equal(bl.length, 10)
33
34
 
@@ -46,13 +47,27 @@ tape('single bytes from multiple buffers', function (t) {
46
47
 
47
48
  tape('multi bytes from single buffer', function (t) {
48
49
  var bl = new BufferList()
49
- bl.append(new Buffer('abcd'))
50
+ bl.append(Buffer.from('abcd'))
50
51
 
51
52
  t.equal(bl.length, 4)
52
53
 
53
54
  t.equal(bl.slice(0, 4).toString('ascii'), 'abcd')
54
55
  t.equal(bl.slice(0, 3).toString('ascii'), 'abc')
55
56
  t.equal(bl.slice(1, 4).toString('ascii'), 'bcd')
57
+ t.equal(bl.slice(-4, -1).toString('ascii'), 'abc')
58
+
59
+ t.end()
60
+ })
61
+
62
+ tape('multi bytes from single buffer (negative indexes)', function (t) {
63
+ var bl = new BufferList()
64
+ bl.append(Buffer.from('buffer'))
65
+
66
+ t.equal(bl.length, 6)
67
+
68
+ t.equal(bl.slice(-6, -1).toString('ascii'), 'buffe')
69
+ t.equal(bl.slice(-6, -2).toString('ascii'), 'buff')
70
+ t.equal(bl.slice(-5, -2).toString('ascii'), 'uff')
56
71
 
57
72
  t.end()
58
73
  })
@@ -60,10 +75,10 @@ tape('multi bytes from single buffer', function (t) {
60
75
  tape('multiple bytes from multiple buffers', function (t) {
61
76
  var bl = new BufferList()
62
77
 
63
- bl.append(new Buffer('abcd'))
64
- bl.append(new Buffer('efg'))
65
- bl.append(new Buffer('hi'))
66
- 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'))
67
82
 
68
83
  t.equal(bl.length, 10)
69
84
 
@@ -72,6 +87,7 @@ tape('multiple bytes from multiple buffers', function (t) {
72
87
  t.equal(bl.slice(3, 6).toString('ascii'), 'def')
73
88
  t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
74
89
  t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
90
+ t.equal(bl.slice(-7, -4).toString('ascii'), 'def')
75
91
 
76
92
  t.end()
77
93
  })
@@ -79,8 +95,8 @@ tape('multiple bytes from multiple buffers', function (t) {
79
95
  tape('multiple bytes from multiple buffer lists', function (t) {
80
96
  var bl = new BufferList()
81
97
 
82
- bl.append(new BufferList([ new Buffer('abcd'), new Buffer('efg') ]))
83
- bl.append(new BufferList([ new Buffer('hi'), new Buffer('j') ]))
98
+ bl.append(new BufferList([ Buffer.from('abcd'), Buffer.from('efg') ]))
99
+ bl.append(new BufferList([ Buffer.from('hi'), Buffer.from('j') ]))
84
100
 
85
101
  t.equal(bl.length, 10)
86
102
 
@@ -100,12 +116,12 @@ tape('multiple bytes from crazy nested buffer lists', function (t) {
100
116
 
101
117
  bl.append(new BufferList([
102
118
  new BufferList([
103
- new BufferList(new Buffer('abc'))
104
- , new Buffer('d')
105
- , new BufferList(new Buffer('efg'))
119
+ new BufferList(Buffer.from('abc'))
120
+ , Buffer.from('d')
121
+ , new BufferList(Buffer.from('efg'))
106
122
  ])
107
- , new BufferList([ new Buffer('hi') ])
108
- , new BufferList(new Buffer('j'))
123
+ , new BufferList([ Buffer.from('hi') ])
124
+ , new BufferList(Buffer.from('j'))
109
125
  ]))
110
126
 
111
127
  t.equal(bl.length, 10)
@@ -122,10 +138,10 @@ tape('multiple bytes from crazy nested buffer lists', function (t) {
122
138
 
123
139
  tape('append accepts arrays of Buffers', function (t) {
124
140
  var bl = new BufferList()
125
- bl.append(new Buffer('abc'))
126
- bl.append([ new Buffer('def') ])
127
- bl.append([ new Buffer('ghi'), new Buffer('jkl') ])
128
- bl.append([ new Buffer('mnop'), new Buffer('qrstu'), new Buffer('vwxyz') ])
141
+ bl.append(Buffer.from('abc'))
142
+ bl.append([ Buffer.from('def') ])
143
+ bl.append([ Buffer.from('ghi'), Buffer.from('jkl') ])
144
+ bl.append([ Buffer.from('mnop'), Buffer.from('qrstu'), Buffer.from('vwxyz') ])
129
145
  t.equal(bl.length, 26)
130
146
  t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
131
147
  t.end()
@@ -133,10 +149,10 @@ tape('append accepts arrays of Buffers', function (t) {
133
149
 
134
150
  tape('append accepts arrays of BufferLists', function (t) {
135
151
  var bl = new BufferList()
136
- bl.append(new Buffer('abc'))
152
+ bl.append(Buffer.from('abc'))
137
153
  bl.append([ new BufferList('def') ])
138
- bl.append(new BufferList([ new Buffer('ghi'), new BufferList('jkl') ]))
139
- bl.append([ new Buffer('mnop'), new BufferList([ new Buffer('qrstu'), new Buffer('vwxyz') ]) ])
154
+ bl.append(new BufferList([ Buffer.from('ghi'), new BufferList('jkl') ]))
155
+ bl.append([ Buffer.from('mnop'), new BufferList([ Buffer.from('qrstu'), Buffer.from('vwxyz') ]) ])
140
156
  t.equal(bl.length, 26)
141
157
  t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
142
158
  t.end()
@@ -144,18 +160,18 @@ tape('append accepts arrays of BufferLists', function (t) {
144
160
 
145
161
  tape('append chainable', function (t) {
146
162
  var bl = new BufferList()
147
- t.ok(bl.append(new Buffer('abcd')) === bl)
148
- t.ok(bl.append([ new Buffer('abcd') ]) === bl)
149
- t.ok(bl.append(new BufferList(new Buffer('abcd'))) === bl)
150
- t.ok(bl.append([ new BufferList(new Buffer('abcd')) ]) === bl)
163
+ t.ok(bl.append(Buffer.from('abcd')) === bl)
164
+ t.ok(bl.append([ Buffer.from('abcd') ]) === bl)
165
+ t.ok(bl.append(new BufferList(Buffer.from('abcd'))) === bl)
166
+ t.ok(bl.append([ new BufferList(Buffer.from('abcd')) ]) === bl)
151
167
  t.end()
152
168
  })
153
169
 
154
170
  tape('append chainable (test results)', function (t) {
155
171
  var bl = new BufferList('abc')
156
172
  .append([ new BufferList('def') ])
157
- .append(new BufferList([ new Buffer('ghi'), new BufferList('jkl') ]))
158
- .append([ new Buffer('mnop'), new BufferList([ new Buffer('qrstu'), new Buffer('vwxyz') ]) ])
173
+ .append(new BufferList([ Buffer.from('ghi'), new BufferList('jkl') ]))
174
+ .append([ Buffer.from('mnop'), new BufferList([ Buffer.from('qrstu'), Buffer.from('vwxyz') ]) ])
159
175
 
160
176
  t.equal(bl.length, 26)
161
177
  t.equal(bl.slice().toString('ascii'), 'abcdefghijklmnopqrstuvwxyz')
@@ -165,10 +181,10 @@ tape('append chainable (test results)', function (t) {
165
181
  tape('consuming from multiple buffers', function (t) {
166
182
  var bl = new BufferList()
167
183
 
168
- bl.append(new Buffer('abcd'))
169
- bl.append(new Buffer('efg'))
170
- bl.append(new Buffer('hi'))
171
- bl.append(new Buffer('j'))
184
+ bl.append(Buffer.from('abcd'))
185
+ bl.append(Buffer.from('efg'))
186
+ bl.append(Buffer.from('hi'))
187
+ bl.append(Buffer.from('j'))
172
188
 
173
189
  t.equal(bl.length, 10)
174
190
 
@@ -200,8 +216,8 @@ tape('consuming from multiple buffers', function (t) {
200
216
  tape('complete consumption', function (t) {
201
217
  var bl = new BufferList()
202
218
 
203
- bl.append(new Buffer('a'))
204
- bl.append(new Buffer('b'))
219
+ bl.append(Buffer.from('a'))
220
+ bl.append(Buffer.from('b'))
205
221
 
206
222
  bl.consume(2)
207
223
 
@@ -212,9 +228,9 @@ tape('complete consumption', function (t) {
212
228
  })
213
229
 
214
230
  tape('test readUInt8 / readInt8', function (t) {
215
- var buf1 = new Buffer(1)
216
- , buf2 = new Buffer(3)
217
- , buf3 = new Buffer(3)
231
+ var buf1 = Buffer.alloc(1)
232
+ , buf2 = Buffer.alloc(3)
233
+ , buf3 = Buffer.alloc(3)
218
234
  , bl = new BufferList()
219
235
 
220
236
  buf2[1] = 0x3
@@ -238,9 +254,9 @@ tape('test readUInt8 / readInt8', function (t) {
238
254
  })
239
255
 
240
256
  tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t) {
241
- var buf1 = new Buffer(1)
242
- , buf2 = new Buffer(3)
243
- , buf3 = new Buffer(3)
257
+ var buf1 = Buffer.alloc(1)
258
+ , buf2 = Buffer.alloc(3)
259
+ , buf3 = Buffer.alloc(3)
244
260
  , bl = new BufferList()
245
261
 
246
262
  buf2[1] = 0x3
@@ -268,9 +284,9 @@ tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t
268
284
  })
269
285
 
270
286
  tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t) {
271
- var buf1 = new Buffer(1)
272
- , buf2 = new Buffer(3)
273
- , buf3 = new Buffer(3)
287
+ var buf1 = Buffer.alloc(1)
288
+ , buf2 = Buffer.alloc(3)
289
+ , buf3 = Buffer.alloc(3)
274
290
  , bl = new BufferList()
275
291
 
276
292
  buf2[1] = 0x3
@@ -290,9 +306,9 @@ tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t
290
306
  })
291
307
 
292
308
  tape('test readFloatLE / readFloatBE', function (t) {
293
- var buf1 = new Buffer(1)
294
- , buf2 = new Buffer(3)
295
- , buf3 = new Buffer(3)
309
+ var buf1 = Buffer.alloc(1)
310
+ , buf2 = Buffer.alloc(3)
311
+ , buf3 = Buffer.alloc(3)
296
312
  , bl = new BufferList()
297
313
 
298
314
  buf2[1] = 0x00
@@ -309,9 +325,9 @@ tape('test readFloatLE / readFloatBE', function (t) {
309
325
  })
310
326
 
311
327
  tape('test readDoubleLE / readDoubleBE', function (t) {
312
- var buf1 = new Buffer(1)
313
- , buf2 = new Buffer(3)
314
- , buf3 = new Buffer(10)
328
+ var buf1 = Buffer.alloc(1)
329
+ , buf2 = Buffer.alloc(3)
330
+ , buf3 = Buffer.alloc(10)
315
331
  , bl = new BufferList()
316
332
 
317
333
  buf2[1] = 0x55
@@ -334,10 +350,10 @@ tape('test readDoubleLE / readDoubleBE', function (t) {
334
350
  tape('test toString', function (t) {
335
351
  var bl = new BufferList()
336
352
 
337
- bl.append(new Buffer('abcd'))
338
- bl.append(new Buffer('efg'))
339
- bl.append(new Buffer('hi'))
340
- bl.append(new Buffer('j'))
353
+ bl.append(Buffer.from('abcd'))
354
+ bl.append(Buffer.from('efg'))
355
+ bl.append(Buffer.from('hi'))
356
+ bl.append(Buffer.from('j'))
341
357
 
342
358
  t.equal(bl.toString('ascii', 0, 10), 'abcdefghij')
343
359
  t.equal(bl.toString('ascii', 3, 10), 'defghij')
@@ -350,13 +366,13 @@ tape('test toString', function (t) {
350
366
 
351
367
  tape('test toString encoding', function (t) {
352
368
  var bl = new BufferList()
353
- , b = new Buffer('abcdefghij\xff\x00')
369
+ , b = Buffer.from('abcdefghij\xff\x00')
354
370
 
355
- bl.append(new Buffer('abcd'))
356
- bl.append(new Buffer('efg'))
357
- bl.append(new Buffer('hi'))
358
- bl.append(new Buffer('j'))
359
- bl.append(new Buffer('\xff\x00'))
371
+ bl.append(Buffer.from('abcd'))
372
+ bl.append(Buffer.from('efg'))
373
+ bl.append(Buffer.from('hi'))
374
+ bl.append(Buffer.from('j'))
375
+ bl.append(Buffer.from('\xff\x00'))
360
376
 
361
377
  encodings.forEach(function (enc) {
362
378
  t.equal(bl.toString(enc), b.toString(enc), enc)
@@ -365,6 +381,22 @@ tape('test toString encoding', function (t) {
365
381
  t.end()
366
382
  })
367
383
 
384
+ tape('uninitialized memory', function (t) {
385
+ const secret = crypto.randomBytes(256)
386
+ for (let i = 0; i < 1e6; i++) {
387
+ const clone = Buffer.from(secret)
388
+ const bl = new BufferList()
389
+ bl.append(Buffer.from('a'))
390
+ bl.consume(-1024)
391
+ const buf = bl.slice(1)
392
+ if (buf.indexOf(clone) !== -1) {
393
+ t.fail(`Match (at ${i})`)
394
+ break
395
+ }
396
+ }
397
+ t.end()
398
+ })
399
+
368
400
  !process.browser && tape('test stream', function (t) {
369
401
  var random = crypto.randomBytes(65534)
370
402
  , rndhash = hash(random, 'md5')
@@ -404,7 +436,7 @@ tape('instantiation with Buffer', function (t) {
404
436
 
405
437
  tape('test String appendage', function (t) {
406
438
  var bl = new BufferList()
407
- , b = new Buffer('abcdefghij\xff\x00')
439
+ , b = Buffer.from('abcdefghij\xff\x00')
408
440
 
409
441
  bl.append('abcd')
410
442
  bl.append('efg')
@@ -421,7 +453,7 @@ tape('test String appendage', function (t) {
421
453
 
422
454
  tape('test Number appendage', function (t) {
423
455
  var bl = new BufferList()
424
- , b = new Buffer('1234567890')
456
+ , b = Buffer.from('1234567890')
425
457
 
426
458
  bl.append(1234)
427
459
  bl.append(567)
@@ -455,7 +487,7 @@ tape('unicode string', function (t) {
455
487
  bl.write(' and ')
456
488
  bl.write(inp2)
457
489
  t.equal(exp, bl.toString())
458
- t.equal(new Buffer(exp).toString('hex'), bl.toString('hex'))
490
+ t.equal(Buffer.from(exp).toString('hex'), bl.toString('hex'))
459
491
  })
460
492
 
461
493
  tape('should emit finish', function (t) {
@@ -473,7 +505,7 @@ tape('should emit finish', function (t) {
473
505
 
474
506
  tape('basic copy', function (t) {
475
507
  var buf = crypto.randomBytes(1024)
476
- , buf2 = new Buffer(1024)
508
+ , buf2 = Buffer.alloc(1024)
477
509
  , b = BufferList(buf)
478
510
 
479
511
  b.copy(buf2)
@@ -483,7 +515,7 @@ tape('basic copy', function (t) {
483
515
 
484
516
  tape('copy after many appends', function (t) {
485
517
  var buf = crypto.randomBytes(512)
486
- , buf2 = new Buffer(1024)
518
+ , buf2 = Buffer.alloc(1024)
487
519
  , b = BufferList(buf)
488
520
 
489
521
  b.append(buf)
@@ -494,7 +526,7 @@ tape('copy after many appends', function (t) {
494
526
 
495
527
  tape('copy at a precise position', function (t) {
496
528
  var buf = crypto.randomBytes(1004)
497
- , buf2 = new Buffer(1024)
529
+ , buf2 = Buffer.alloc(1024)
498
530
  , b = BufferList(buf)
499
531
 
500
532
  b.copy(buf2, 20)
@@ -504,7 +536,7 @@ tape('copy at a precise position', function (t) {
504
536
 
505
537
  tape('copy starting from a precise location', function (t) {
506
538
  var buf = crypto.randomBytes(10)
507
- , buf2 = new Buffer(5)
539
+ , buf2 = Buffer.alloc(5)
508
540
  , b = BufferList(buf)
509
541
 
510
542
  b.copy(buf2, 0, 5)
@@ -515,8 +547,8 @@ tape('copy starting from a precise location', function (t) {
515
547
  tape('copy in an interval', function (t) {
516
548
  var rnd = crypto.randomBytes(10)
517
549
  , b = BufferList(rnd) // put the random bytes there
518
- , actual = new Buffer(3)
519
- , expected = new Buffer(3)
550
+ , actual = Buffer.alloc(3)
551
+ , expected = Buffer.alloc(3)
520
552
 
521
553
  rnd.copy(expected, 0, 5, 8)
522
554
  b.copy(actual, 0, 5, 8)
@@ -527,7 +559,7 @@ tape('copy in an interval', function (t) {
527
559
 
528
560
  tape('copy an interval between two buffers', function (t) {
529
561
  var buf = crypto.randomBytes(10)
530
- , buf2 = new Buffer(10)
562
+ , buf2 = Buffer.alloc(10)
531
563
  , b = BufferList(buf)
532
564
 
533
565
  b.append(buf)
@@ -537,6 +569,52 @@ tape('copy an interval between two buffers', function (t) {
537
569
  t.end()
538
570
  })
539
571
 
572
+ tape('shallow slice across buffer boundaries', function (t) {
573
+ var bl = new BufferList(['First', 'Second', 'Third'])
574
+
575
+ t.equal(bl.shallowSlice(3, 13).toString(), 'stSecondTh')
576
+ t.end()
577
+ })
578
+
579
+ tape('shallow slice within single buffer', function (t) {
580
+ t.plan(2)
581
+ var bl = new BufferList(['First', 'Second', 'Third'])
582
+
583
+ t.equal(bl.shallowSlice(5, 10).toString(), 'Secon')
584
+ t.equal(bl.shallowSlice(7, 10).toString(), 'con')
585
+ t.end()
586
+ })
587
+
588
+ tape('shallow slice single buffer', function (t) {
589
+ t.plan(3)
590
+ var bl = new BufferList(['First', 'Second', 'Third'])
591
+
592
+ t.equal(bl.shallowSlice(0, 5).toString(), 'First')
593
+ t.equal(bl.shallowSlice(5, 11).toString(), 'Second')
594
+ t.equal(bl.shallowSlice(11, 16).toString(), 'Third')
595
+ })
596
+
597
+ tape('shallow slice with negative or omitted indices', function (t) {
598
+ t.plan(4)
599
+ var bl = new BufferList(['First', 'Second', 'Third'])
600
+
601
+ t.equal(bl.shallowSlice().toString(), 'FirstSecondThird')
602
+ t.equal(bl.shallowSlice(5).toString(), 'SecondThird')
603
+ t.equal(bl.shallowSlice(5, -3).toString(), 'SecondTh')
604
+ t.equal(bl.shallowSlice(-8).toString(), 'ondThird')
605
+ })
606
+
607
+ tape('shallow slice does not make a copy', function (t) {
608
+ t.plan(1)
609
+ var buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
610
+ var bl = (new BufferList(buffers)).shallowSlice(5, -3)
611
+
612
+ buffers[1].fill('h')
613
+ buffers[2].fill('h')
614
+
615
+ t.equal(bl.toString(), 'hhhhhhhh')
616
+ })
617
+
540
618
  tape('duplicate', function (t) {
541
619
  t.plan(2)
542
620
 
package/.npmignore DELETED
@@ -1 +0,0 @@
1
- node_modules/