bl 1.0.1 → 1.1.2
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 +3 -2
- package/README.md +3 -3
- package/bl.js +56 -34
- package/package.json +4 -6
- package/test/test.js +637 -6
- package/test/basic-test.js +0 -557
- package/test/sauce.js +0 -38
package/.travis.yml
CHANGED
package/README.md
CHANGED
|
@@ -96,7 +96,7 @@ bl.pipe(fs.createWriteStream('gibberish.txt'))
|
|
|
96
96
|
|
|
97
97
|
--------------------------------------------------------
|
|
98
98
|
<a name="ctor"></a>
|
|
99
|
-
### new BufferList([ callback |
|
|
99
|
+
### new BufferList([ callback | Buffer | Buffer array | BufferList | BufferList array | String ])
|
|
100
100
|
The constructor takes an optional callback, if supplied, the callback will be called with an error argument followed by a reference to the **bl** instance, when `bl.end()` is called (i.e. from a piped stream). This is a convenient method of collecting the entire contents of a stream, particularly when the stream is *chunky*, such as a network stream.
|
|
101
101
|
|
|
102
102
|
Normally, no arguments are required for the constructor, but you can initialise the list by passing in a single `Buffer` object or an array of `Buffer` object.
|
|
@@ -120,8 +120,8 @@ Get the length of the list in bytes. This is the sum of the lengths of all of th
|
|
|
120
120
|
|
|
121
121
|
--------------------------------------------------------
|
|
122
122
|
<a name="append"></a>
|
|
123
|
-
### bl.append(
|
|
124
|
-
`append(buffer)` adds an additional buffer or BufferList to the internal list.
|
|
123
|
+
### bl.append(Buffer | Buffer array | BufferList | BufferList array | String)
|
|
124
|
+
`append(buffer)` adds an additional buffer or BufferList to the internal list. `this` is returned so it can be chained.
|
|
125
125
|
|
|
126
126
|
--------------------------------------------------------
|
|
127
127
|
<a name="get"></a>
|
package/bl.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
var DuplexStream = require('readable-stream/duplex')
|
|
2
2
|
, util = require('util')
|
|
3
3
|
|
|
4
|
+
|
|
4
5
|
function BufferList (callback) {
|
|
5
6
|
if (!(this instanceof BufferList))
|
|
6
7
|
return new BufferList(callback)
|
|
@@ -11,34 +12,31 @@ function BufferList (callback) {
|
|
|
11
12
|
if (typeof callback == 'function') {
|
|
12
13
|
this._callback = callback
|
|
13
14
|
|
|
14
|
-
var piper = function (err) {
|
|
15
|
+
var piper = function piper (err) {
|
|
15
16
|
if (this._callback) {
|
|
16
17
|
this._callback(err)
|
|
17
18
|
this._callback = null
|
|
18
19
|
}
|
|
19
20
|
}.bind(this)
|
|
20
21
|
|
|
21
|
-
this.on('pipe', function (src) {
|
|
22
|
+
this.on('pipe', function onPipe (src) {
|
|
22
23
|
src.on('error', piper)
|
|
23
24
|
})
|
|
24
|
-
this.on('unpipe', function (src) {
|
|
25
|
+
this.on('unpipe', function onUnpipe (src) {
|
|
25
26
|
src.removeListener('error', piper)
|
|
26
27
|
})
|
|
27
|
-
}
|
|
28
|
-
else if (Buffer.isBuffer(callback))
|
|
28
|
+
} else {
|
|
29
29
|
this.append(callback)
|
|
30
|
-
else if (Array.isArray(callback)) {
|
|
31
|
-
callback.forEach(function (b) {
|
|
32
|
-
Buffer.isBuffer(b) && this.append(b)
|
|
33
|
-
}.bind(this))
|
|
34
30
|
}
|
|
35
31
|
|
|
36
32
|
DuplexStream.call(this)
|
|
37
33
|
}
|
|
38
34
|
|
|
35
|
+
|
|
39
36
|
util.inherits(BufferList, DuplexStream)
|
|
40
37
|
|
|
41
|
-
|
|
38
|
+
|
|
39
|
+
BufferList.prototype._offset = function _offset (offset) {
|
|
42
40
|
var tot = 0, i = 0, _t
|
|
43
41
|
for (; i < this._bufs.length; i++) {
|
|
44
42
|
_t = tot + this._bufs[i].length
|
|
@@ -48,35 +46,52 @@ BufferList.prototype._offset = function (offset) {
|
|
|
48
46
|
}
|
|
49
47
|
}
|
|
50
48
|
|
|
51
|
-
BufferList.prototype.append = function (buf) {
|
|
52
|
-
var isBuffer = Buffer.isBuffer(buf) ||
|
|
53
|
-
buf instanceof BufferList
|
|
54
49
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
50
|
+
BufferList.prototype.append = function append (buf) {
|
|
51
|
+
var i = 0
|
|
52
|
+
, newBuf
|
|
53
|
+
|
|
54
|
+
if (Array.isArray(buf)) {
|
|
55
|
+
for (; i < buf.length; i++)
|
|
56
|
+
this.append(buf[i])
|
|
57
|
+
} else if (buf instanceof BufferList) {
|
|
58
|
+
// unwrap argument into individual BufferLists
|
|
59
|
+
for (; i < buf._bufs.length; i++)
|
|
60
|
+
this.append(buf._bufs[i])
|
|
61
|
+
} else if (buf != null) {
|
|
62
|
+
// coerce number arguments to strings, since Buffer(number) does
|
|
63
|
+
// uninitialized memory allocation
|
|
64
|
+
if (typeof buf == 'number')
|
|
65
|
+
buf = buf.toString()
|
|
66
|
+
|
|
67
|
+
newBuf = Buffer.isBuffer(buf) ? buf : new Buffer(buf)
|
|
68
|
+
this._bufs.push(newBuf)
|
|
69
|
+
this.length += newBuf.length
|
|
70
|
+
}
|
|
59
71
|
|
|
60
|
-
this._bufs.push(isBuffer ? buf : new Buffer(buf))
|
|
61
|
-
this.length += buf.length
|
|
62
72
|
return this
|
|
63
73
|
}
|
|
64
74
|
|
|
65
|
-
|
|
75
|
+
|
|
76
|
+
BufferList.prototype._write = function _write (buf, encoding, callback) {
|
|
66
77
|
this.append(buf)
|
|
67
|
-
|
|
78
|
+
|
|
79
|
+
if (typeof callback == 'function')
|
|
68
80
|
callback()
|
|
69
81
|
}
|
|
70
82
|
|
|
71
|
-
|
|
83
|
+
|
|
84
|
+
BufferList.prototype._read = function _read (size) {
|
|
72
85
|
if (!this.length)
|
|
73
86
|
return this.push(null)
|
|
87
|
+
|
|
74
88
|
size = Math.min(size, this.length)
|
|
75
89
|
this.push(this.slice(0, size))
|
|
76
90
|
this.consume(size)
|
|
77
91
|
}
|
|
78
92
|
|
|
79
|
-
|
|
93
|
+
|
|
94
|
+
BufferList.prototype.end = function end (chunk) {
|
|
80
95
|
DuplexStream.prototype.end.call(this, chunk)
|
|
81
96
|
|
|
82
97
|
if (this._callback) {
|
|
@@ -85,15 +100,18 @@ BufferList.prototype.end = function (chunk) {
|
|
|
85
100
|
}
|
|
86
101
|
}
|
|
87
102
|
|
|
88
|
-
|
|
103
|
+
|
|
104
|
+
BufferList.prototype.get = function get (index) {
|
|
89
105
|
return this.slice(index, index + 1)[0]
|
|
90
106
|
}
|
|
91
107
|
|
|
92
|
-
|
|
108
|
+
|
|
109
|
+
BufferList.prototype.slice = function slice (start, end) {
|
|
93
110
|
return this.copy(null, 0, start, end)
|
|
94
111
|
}
|
|
95
112
|
|
|
96
|
-
|
|
113
|
+
|
|
114
|
+
BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
|
|
97
115
|
if (typeof srcStart != 'number' || srcStart < 0)
|
|
98
116
|
srcStart = 0
|
|
99
117
|
if (typeof srcEnd != 'number' || srcEnd > this.length)
|
|
@@ -156,13 +174,13 @@ BufferList.prototype.copy = function (dst, dstStart, srcStart, srcEnd) {
|
|
|
156
174
|
return dst
|
|
157
175
|
}
|
|
158
176
|
|
|
159
|
-
BufferList.prototype.toString = function (encoding, start, end) {
|
|
177
|
+
BufferList.prototype.toString = function toString (encoding, start, end) {
|
|
160
178
|
return this.slice(start, end).toString(encoding)
|
|
161
179
|
}
|
|
162
180
|
|
|
163
|
-
BufferList.prototype.consume = function (bytes) {
|
|
181
|
+
BufferList.prototype.consume = function consume (bytes) {
|
|
164
182
|
while (this._bufs.length) {
|
|
165
|
-
if (bytes
|
|
183
|
+
if (bytes >= this._bufs[0].length) {
|
|
166
184
|
bytes -= this._bufs[0].length
|
|
167
185
|
this.length -= this._bufs[0].length
|
|
168
186
|
this._bufs.shift()
|
|
@@ -175,7 +193,8 @@ BufferList.prototype.consume = function (bytes) {
|
|
|
175
193
|
return this
|
|
176
194
|
}
|
|
177
195
|
|
|
178
|
-
|
|
196
|
+
|
|
197
|
+
BufferList.prototype.duplicate = function duplicate () {
|
|
179
198
|
var i = 0
|
|
180
199
|
, copy = new BufferList()
|
|
181
200
|
|
|
@@ -185,12 +204,14 @@ BufferList.prototype.duplicate = function () {
|
|
|
185
204
|
return copy
|
|
186
205
|
}
|
|
187
206
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
this.length = 0
|
|
191
|
-
this.
|
|
207
|
+
|
|
208
|
+
BufferList.prototype.destroy = function destroy () {
|
|
209
|
+
this._bufs.length = 0
|
|
210
|
+
this.length = 0
|
|
211
|
+
this.push(null)
|
|
192
212
|
}
|
|
193
213
|
|
|
214
|
+
|
|
194
215
|
;(function () {
|
|
195
216
|
var methods = {
|
|
196
217
|
'readDoubleBE' : 8
|
|
@@ -218,4 +239,5 @@ BufferList.prototype.destroy = function () {
|
|
|
218
239
|
}
|
|
219
240
|
}())
|
|
220
241
|
|
|
242
|
+
|
|
221
243
|
module.exports = BufferList
|
package/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bl",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.2",
|
|
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": {
|
|
7
|
-
"test": "node test/test.js | faucet"
|
|
8
|
-
"test-local": "brtapsauce-local test/basic-test.js"
|
|
7
|
+
"test": "node test/test.js | faucet"
|
|
9
8
|
},
|
|
10
9
|
"repository": {
|
|
11
10
|
"type": "git",
|
|
@@ -28,9 +27,8 @@
|
|
|
28
27
|
"readable-stream": "~2.0.5"
|
|
29
28
|
},
|
|
30
29
|
"devDependencies": {
|
|
31
|
-
"
|
|
30
|
+
"faucet": "0.0.1",
|
|
32
31
|
"hash_file": "~0.1.1",
|
|
33
|
-
"
|
|
34
|
-
"brtapsauce": "~0.3.0"
|
|
32
|
+
"tape": "~4.4.0"
|
|
35
33
|
}
|
|
36
34
|
}
|