bl 2.1.1 → 3.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/.jshintrc +2 -1
- package/.travis.yml +0 -2
- package/README.md +18 -8
- package/bl.js +18 -16
- package/package.json +3 -4
- package/test/test.js +28 -0
package/.jshintrc
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
, "boss": true
|
|
23
23
|
, "debug": true
|
|
24
24
|
, "eqnull": true
|
|
25
|
-
, "esnext":
|
|
25
|
+
, "esnext": false
|
|
26
26
|
, "evil": true
|
|
27
27
|
, "expr": true
|
|
28
28
|
, "funcscope": false
|
|
@@ -56,4 +56,5 @@
|
|
|
56
56
|
, "nomen": false
|
|
57
57
|
, "onevar": false
|
|
58
58
|
, "passfail": false
|
|
59
|
+
, "esversion": 3
|
|
59
60
|
}
|
package/.travis.yml
CHANGED
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(
|
|
19
|
-
bl.append(
|
|
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(
|
|
22
|
-
bl.append(
|
|
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(
|
|
76
|
-
bl.append(
|
|
77
|
-
bl.append(
|
|
78
|
-
bl.append(
|
|
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))
|
|
@@ -50,9 +49,9 @@ BufferList.prototype._offset = function _offset (offset) {
|
|
|
50
49
|
}
|
|
51
50
|
|
|
52
51
|
BufferList.prototype._reverseOffset = function (blOffset) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
for (
|
|
52
|
+
var bufferId = blOffset[0]
|
|
53
|
+
var offset = blOffset[1]
|
|
54
|
+
for (var i = 0; i < bufferId; i++) {
|
|
56
55
|
offset += this._bufs[i].length
|
|
57
56
|
}
|
|
58
57
|
return offset
|
|
@@ -121,7 +120,7 @@ BufferList.prototype.get = function get (index) {
|
|
|
121
120
|
if (index > this.length || index < 0) {
|
|
122
121
|
return undefined
|
|
123
122
|
}
|
|
124
|
-
|
|
123
|
+
var offset = this._offset(index)
|
|
125
124
|
return this._bufs[offset[0]][offset[1]]
|
|
126
125
|
}
|
|
127
126
|
|
|
@@ -203,13 +202,16 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
|
|
|
203
202
|
|
|
204
203
|
BufferList.prototype.shallowSlice = function shallowSlice (start, end) {
|
|
205
204
|
start = start || 0
|
|
206
|
-
end = end
|
|
205
|
+
end = typeof end !== 'number' ? this.length : end
|
|
207
206
|
|
|
208
207
|
if (start < 0)
|
|
209
208
|
start += this.length
|
|
210
209
|
if (end < 0)
|
|
211
210
|
end += this.length
|
|
212
211
|
|
|
212
|
+
if (start === end) {
|
|
213
|
+
return new BufferList()
|
|
214
|
+
}
|
|
213
215
|
var startOffset = this._offset(start)
|
|
214
216
|
, endOffset = this._offset(end)
|
|
215
217
|
, buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
|
|
@@ -256,10 +258,10 @@ BufferList.prototype.duplicate = function duplicate () {
|
|
|
256
258
|
}
|
|
257
259
|
|
|
258
260
|
|
|
259
|
-
BufferList.prototype.
|
|
261
|
+
BufferList.prototype._destroy = function _destroy (err, cb) {
|
|
260
262
|
this._bufs.length = 0
|
|
261
263
|
this.length = 0
|
|
262
|
-
|
|
264
|
+
cb(err)
|
|
263
265
|
}
|
|
264
266
|
|
|
265
267
|
|
|
@@ -297,23 +299,23 @@ BufferList.prototype.indexOf = function (search, offset, encoding) {
|
|
|
297
299
|
return offset > this.length ? this.length : offset
|
|
298
300
|
}
|
|
299
301
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
302
|
+
var blOffset = this._offset(offset)
|
|
303
|
+
var blIndex = blOffset[0] // index of which internal buffer we're working on
|
|
304
|
+
var buffOffset = blOffset[1] // offset of the internal buffer we're working on
|
|
303
305
|
|
|
304
306
|
// scan over each buffer
|
|
305
307
|
for (blIndex; blIndex < this._bufs.length; blIndex++) {
|
|
306
|
-
|
|
308
|
+
var buff = this._bufs[blIndex]
|
|
307
309
|
while(buffOffset < buff.length) {
|
|
308
|
-
|
|
310
|
+
var availableWindow = buff.length - buffOffset
|
|
309
311
|
if (availableWindow >= search.length) {
|
|
310
|
-
|
|
312
|
+
var nativeSearchResult = buff.indexOf(search, buffOffset)
|
|
311
313
|
if (nativeSearchResult !== -1) {
|
|
312
314
|
return this._reverseOffset([blIndex, nativeSearchResult])
|
|
313
315
|
}
|
|
314
316
|
buffOffset = buff.length - search.length + 1 // end of native search window
|
|
315
317
|
} else {
|
|
316
|
-
|
|
318
|
+
var revOffset = this._reverseOffset([blIndex, buffOffset])
|
|
317
319
|
if (this._match(revOffset, search)) {
|
|
318
320
|
return revOffset
|
|
319
321
|
}
|
|
@@ -329,7 +331,7 @@ BufferList.prototype._match = function(offset, search) {
|
|
|
329
331
|
if (this.length - offset < search.length) {
|
|
330
332
|
return false
|
|
331
333
|
}
|
|
332
|
-
for (
|
|
334
|
+
for (var searchOffset = 0; searchOffset < search.length ; searchOffset++) {
|
|
333
335
|
if(this.get(offset + searchOffset) !== search[searchOffset]){
|
|
334
336
|
return false
|
|
335
337
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bl",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
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": "^
|
|
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.
|
|
32
|
+
"tape": "~4.9.1"
|
|
34
33
|
}
|
|
35
34
|
}
|
package/test/test.js
CHANGED
|
@@ -649,6 +649,20 @@ tape('shallow slice does not make a copy', function (t) {
|
|
|
649
649
|
t.equal(bl.toString(), 'hhhhhhhh')
|
|
650
650
|
})
|
|
651
651
|
|
|
652
|
+
tape('shallow slice with 0 length', function (t) {
|
|
653
|
+
t.plan(1)
|
|
654
|
+
var buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
|
|
655
|
+
var bl = (new BufferList(buffers)).shallowSlice(0, 0)
|
|
656
|
+
t.equal(bl.length, 0)
|
|
657
|
+
})
|
|
658
|
+
|
|
659
|
+
tape('shallow slice with 0 length from middle', function (t) {
|
|
660
|
+
t.plan(1)
|
|
661
|
+
var buffers = [Buffer.from('First'), Buffer.from('Second'), Buffer.from('Third')]
|
|
662
|
+
var bl = (new BufferList(buffers)).shallowSlice(10, 10)
|
|
663
|
+
t.equal(bl.length, 0)
|
|
664
|
+
})
|
|
665
|
+
|
|
652
666
|
tape('duplicate', function (t) {
|
|
653
667
|
t.plan(2)
|
|
654
668
|
|
|
@@ -669,6 +683,20 @@ tape('destroy no pipe', function (t) {
|
|
|
669
683
|
t.equal(bl.length, 0)
|
|
670
684
|
})
|
|
671
685
|
|
|
686
|
+
tape('destroy with error', function (t) {
|
|
687
|
+
t.plan(3)
|
|
688
|
+
|
|
689
|
+
var bl = new BufferList('alsdkfja;lsdkfja;lsdk')
|
|
690
|
+
var err = new Error('kaboom')
|
|
691
|
+
bl.destroy(err)
|
|
692
|
+
bl.on('error', function (_err) {
|
|
693
|
+
t.equal(_err, err)
|
|
694
|
+
})
|
|
695
|
+
|
|
696
|
+
t.equal(bl._bufs.length, 0)
|
|
697
|
+
t.equal(bl.length, 0)
|
|
698
|
+
})
|
|
699
|
+
|
|
672
700
|
!process.browser && tape('destroy with pipe before read end', function (t) {
|
|
673
701
|
t.plan(2)
|
|
674
702
|
|