bl 3.0.0 → 4.0.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/bl.js CHANGED
@@ -1,18 +1,18 @@
1
1
  'use strict'
2
- var DuplexStream = require('readable-stream').Duplex
3
- , util = require('util')
4
2
 
5
- function BufferList (callback) {
6
- if (!(this instanceof BufferList))
7
- return new BufferList(callback)
3
+ const DuplexStream = require('readable-stream').Duplex
4
+ const inherits = require('inherits')
5
+ const BufferList = require('./BufferList')
8
6
 
9
- this._bufs = []
10
- this.length = 0
7
+ function BufferListStream (callback) {
8
+ if (!(this instanceof BufferListStream)) {
9
+ return new BufferListStream(callback)
10
+ }
11
11
 
12
- if (typeof callback == 'function') {
12
+ if (typeof callback === 'function') {
13
13
  this._callback = callback
14
14
 
15
- var piper = function piper (err) {
15
+ const piper = function piper (err) {
16
16
  if (this._callback) {
17
17
  this._callback(err)
18
18
  this._callback = null
@@ -25,88 +25,40 @@ function BufferList (callback) {
25
25
  this.on('unpipe', function onUnpipe (src) {
26
26
  src.removeListener('error', piper)
27
27
  })
28
- } else {
29
- this.append(callback)
30
- }
31
-
32
- DuplexStream.call(this)
33
- }
34
28
 
35
-
36
- util.inherits(BufferList, DuplexStream)
37
-
38
-
39
- BufferList.prototype._offset = function _offset (offset) {
40
- var tot = 0, i = 0, _t
41
- if (offset === 0) return [ 0, 0 ]
42
- for (; i < this._bufs.length; i++) {
43
- _t = tot + this._bufs[i].length
44
- if (offset < _t || i == this._bufs.length - 1) {
45
- return [ i, offset - tot ]
46
- }
47
- tot = _t
29
+ callback = null
48
30
  }
49
- }
50
31
 
51
- BufferList.prototype._reverseOffset = function (blOffset) {
52
- var bufferId = blOffset[0]
53
- var offset = blOffset[1]
54
- for (var i = 0; i < bufferId; i++) {
55
- offset += this._bufs[i].length
56
- }
57
- return offset
58
- }
59
-
60
- BufferList.prototype.append = function append (buf) {
61
- var i = 0
62
-
63
- if (Buffer.isBuffer(buf)) {
64
- this._appendBuffer(buf)
65
- } else if (Array.isArray(buf)) {
66
- for (; i < buf.length; i++)
67
- this.append(buf[i])
68
- } else if (buf instanceof BufferList) {
69
- // unwrap argument into individual BufferLists
70
- for (; i < buf._bufs.length; i++)
71
- this.append(buf._bufs[i])
72
- } else if (buf != null) {
73
- // coerce number arguments to strings, since Buffer(number) does
74
- // uninitialized memory allocation
75
- if (typeof buf == 'number')
76
- buf = buf.toString()
77
-
78
- this._appendBuffer(Buffer.from(buf))
79
- }
80
-
81
- return this
32
+ BufferList._init.call(this, callback)
33
+ DuplexStream.call(this)
82
34
  }
83
35
 
36
+ inherits(BufferListStream, DuplexStream)
37
+ Object.assign(BufferListStream.prototype, BufferList.prototype)
84
38
 
85
- BufferList.prototype._appendBuffer = function appendBuffer (buf) {
86
- this._bufs.push(buf)
87
- this.length += buf.length
39
+ BufferListStream.prototype._new = function _new (callback) {
40
+ return new BufferListStream(callback)
88
41
  }
89
42
 
90
-
91
- BufferList.prototype._write = function _write (buf, encoding, callback) {
43
+ BufferListStream.prototype._write = function _write (buf, encoding, callback) {
92
44
  this._appendBuffer(buf)
93
45
 
94
- if (typeof callback == 'function')
46
+ if (typeof callback === 'function') {
95
47
  callback()
48
+ }
96
49
  }
97
50
 
98
-
99
- BufferList.prototype._read = function _read (size) {
100
- if (!this.length)
51
+ BufferListStream.prototype._read = function _read (size) {
52
+ if (!this.length) {
101
53
  return this.push(null)
54
+ }
102
55
 
103
56
  size = Math.min(size, this.length)
104
57
  this.push(this.slice(0, size))
105
58
  this.consume(size)
106
59
  }
107
60
 
108
-
109
- BufferList.prototype.end = function end (chunk) {
61
+ BufferListStream.prototype.end = function end (chunk) {
110
62
  DuplexStream.prototype.end.call(this, chunk)
111
63
 
112
64
  if (this._callback) {
@@ -115,268 +67,18 @@ BufferList.prototype.end = function end (chunk) {
115
67
  }
116
68
  }
117
69
 
118
-
119
- BufferList.prototype.get = function get (index) {
120
- if (index > this.length || index < 0) {
121
- return undefined
122
- }
123
- var offset = this._offset(index)
124
- return this._bufs[offset[0]][offset[1]]
125
- }
126
-
127
-
128
- BufferList.prototype.slice = function slice (start, end) {
129
- if (typeof start == 'number' && start < 0)
130
- start += this.length
131
- if (typeof end == 'number' && end < 0)
132
- end += this.length
133
- return this.copy(null, 0, start, end)
134
- }
135
-
136
-
137
- BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
138
- if (typeof srcStart != 'number' || srcStart < 0)
139
- srcStart = 0
140
- if (typeof srcEnd != 'number' || srcEnd > this.length)
141
- srcEnd = this.length
142
- if (srcStart >= this.length)
143
- return dst || Buffer.alloc(0)
144
- if (srcEnd <= 0)
145
- return dst || Buffer.alloc(0)
146
-
147
- var copy = !!dst
148
- , off = this._offset(srcStart)
149
- , len = srcEnd - srcStart
150
- , bytes = len
151
- , bufoff = (copy && dstStart) || 0
152
- , start = off[1]
153
- , l
154
- , i
155
-
156
- // copy/slice everything
157
- if (srcStart === 0 && srcEnd == this.length) {
158
- if (!copy) { // slice, but full concat if multiple buffers
159
- return this._bufs.length === 1
160
- ? this._bufs[0]
161
- : Buffer.concat(this._bufs, this.length)
162
- }
163
-
164
- // copy, need to copy individual buffers
165
- for (i = 0; i < this._bufs.length; i++) {
166
- this._bufs[i].copy(dst, bufoff)
167
- bufoff += this._bufs[i].length
168
- }
169
-
170
- return dst
171
- }
172
-
173
- // easy, cheap case where it's a subset of one of the buffers
174
- if (bytes <= this._bufs[off[0]].length - start) {
175
- return copy
176
- ? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes)
177
- : this._bufs[off[0]].slice(start, start + bytes)
178
- }
179
-
180
- if (!copy) // a slice, we need something to copy in to
181
- dst = Buffer.allocUnsafe(len)
182
-
183
- for (i = off[0]; i < this._bufs.length; i++) {
184
- l = this._bufs[i].length - start
185
-
186
- if (bytes > l) {
187
- this._bufs[i].copy(dst, bufoff, start)
188
- } else {
189
- this._bufs[i].copy(dst, bufoff, start, start + bytes)
190
- break
191
- }
192
-
193
- bufoff += l
194
- bytes -= l
195
-
196
- if (start)
197
- start = 0
198
- }
199
-
200
- return dst
201
- }
202
-
203
- BufferList.prototype.shallowSlice = function shallowSlice (start, end) {
204
- start = start || 0
205
- end = typeof end !== 'number' ? this.length : end
206
-
207
- if (start < 0)
208
- start += this.length
209
- if (end < 0)
210
- end += this.length
211
-
212
- if (start === end) {
213
- return new BufferList()
214
- }
215
- var startOffset = this._offset(start)
216
- , endOffset = this._offset(end)
217
- , buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
218
-
219
- if (endOffset[1] == 0)
220
- buffers.pop()
221
- else
222
- buffers[buffers.length-1] = buffers[buffers.length-1].slice(0, endOffset[1])
223
-
224
- if (startOffset[1] != 0)
225
- buffers[0] = buffers[0].slice(startOffset[1])
226
-
227
- return new BufferList(buffers)
228
- }
229
-
230
- BufferList.prototype.toString = function toString (encoding, start, end) {
231
- return this.slice(start, end).toString(encoding)
232
- }
233
-
234
- BufferList.prototype.consume = function consume (bytes) {
235
- while (this._bufs.length) {
236
- if (bytes >= this._bufs[0].length) {
237
- bytes -= this._bufs[0].length
238
- this.length -= this._bufs[0].length
239
- this._bufs.shift()
240
- } else {
241
- this._bufs[0] = this._bufs[0].slice(bytes)
242
- this.length -= bytes
243
- break
244
- }
245
- }
246
- return this
247
- }
248
-
249
-
250
- BufferList.prototype.duplicate = function duplicate () {
251
- var i = 0
252
- , copy = new BufferList()
253
-
254
- for (; i < this._bufs.length; i++)
255
- copy.append(this._bufs[i])
256
-
257
- return copy
258
- }
259
-
260
-
261
- BufferList.prototype._destroy = function _destroy (err, cb) {
70
+ BufferListStream.prototype._destroy = function _destroy (err, cb) {
262
71
  this._bufs.length = 0
263
72
  this.length = 0
264
73
  cb(err)
265
74
  }
266
75
 
267
-
268
- BufferList.prototype.indexOf = function (search, offset, encoding) {
269
- if (encoding === undefined && typeof offset === 'string') {
270
- encoding = offset
271
- offset = undefined
272
- }
273
- if (typeof search === 'function' || Array.isArray(search)) {
274
- throw new TypeError('The "value" argument must be one of type string, Buffer, BufferList, or Uint8Array.')
275
- } else if (typeof search === 'number') {
276
- search = Buffer.from([search])
277
- } else if (typeof search === 'string') {
278
- search = Buffer.from(search, encoding)
279
- } else if (search instanceof BufferList) {
280
- search = search.slice()
281
- } else if (!Buffer.isBuffer(search)) {
282
- search = Buffer.from(search)
283
- }
284
-
285
- offset = Number(offset || 0)
286
- if (isNaN(offset)) {
287
- offset = 0
288
- }
289
-
290
- if (offset < 0) {
291
- offset = this.length + offset
292
- }
293
-
294
- if (offset < 0) {
295
- offset = 0
296
- }
297
-
298
- if (search.length === 0) {
299
- return offset > this.length ? this.length : offset
300
- }
301
-
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
305
-
306
- // scan over each buffer
307
- for (blIndex; blIndex < this._bufs.length; blIndex++) {
308
- var buff = this._bufs[blIndex]
309
- while(buffOffset < buff.length) {
310
- var availableWindow = buff.length - buffOffset
311
- if (availableWindow >= search.length) {
312
- var nativeSearchResult = buff.indexOf(search, buffOffset)
313
- if (nativeSearchResult !== -1) {
314
- return this._reverseOffset([blIndex, nativeSearchResult])
315
- }
316
- buffOffset = buff.length - search.length + 1 // end of native search window
317
- } else {
318
- var revOffset = this._reverseOffset([blIndex, buffOffset])
319
- if (this._match(revOffset, search)) {
320
- return revOffset
321
- }
322
- buffOffset++
323
- }
324
- }
325
- buffOffset = 0
326
- }
327
- return -1
328
- }
329
-
330
- BufferList.prototype._match = function(offset, search) {
331
- if (this.length - offset < search.length) {
332
- return false
333
- }
334
- for (var searchOffset = 0; searchOffset < search.length ; searchOffset++) {
335
- if(this.get(offset + searchOffset) !== search[searchOffset]){
336
- return false
337
- }
338
- }
339
- return true
76
+ BufferListStream.prototype._isBufferList = function _isBufferList (b) {
77
+ return b instanceof BufferListStream || b instanceof BufferList || BufferListStream.isBufferList(b)
340
78
  }
341
79
 
80
+ BufferListStream.isBufferList = BufferList.isBufferList
342
81
 
343
- ;(function () {
344
- var methods = {
345
- 'readDoubleBE' : 8
346
- , 'readDoubleLE' : 8
347
- , 'readFloatBE' : 4
348
- , 'readFloatLE' : 4
349
- , 'readInt32BE' : 4
350
- , 'readInt32LE' : 4
351
- , 'readUInt32BE' : 4
352
- , 'readUInt32LE' : 4
353
- , 'readInt16BE' : 2
354
- , 'readInt16LE' : 2
355
- , 'readUInt16BE' : 2
356
- , 'readUInt16LE' : 2
357
- , 'readInt8' : 1
358
- , 'readUInt8' : 1
359
- , 'readIntBE' : null
360
- , 'readIntLE' : null
361
- , 'readUIntBE' : null
362
- , 'readUIntLE' : null
363
- }
364
-
365
- for (var m in methods) {
366
- (function (m) {
367
- if (methods[m] === null) {
368
- BufferList.prototype[m] = function (offset, byteLength) {
369
- return this.slice(offset, offset + byteLength)[m](0, byteLength)
370
- }
371
- }
372
- else {
373
- BufferList.prototype[m] = function (offset) {
374
- return this.slice(offset, offset + methods[m])[m](0)
375
- }
376
- }
377
- }(m))
378
- }
379
- }())
380
-
381
-
382
- module.exports = BufferList
82
+ module.exports = BufferListStream
83
+ module.exports.BufferListStream = BufferListStream
84
+ module.exports.BufferList = BufferList
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "bl",
3
- "version": "3.0.0",
3
+ "version": "4.0.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"
7
+ "lint": "standard *.js test/*.js",
8
+ "test": "npm run lint && node test/test.js | faucet"
8
9
  },
9
10
  "repository": {
10
11
  "type": "git",
@@ -24,11 +25,13 @@
24
25
  ],
25
26
  "license": "MIT",
26
27
  "dependencies": {
27
- "readable-stream": "^3.0.1"
28
+ "buffer": "^5.5.0",
29
+ "inherits": "^2.0.4",
30
+ "readable-stream": "^3.4.0"
28
31
  },
29
32
  "devDependencies": {
30
- "faucet": "0.0.1",
31
- "hash_file": "~0.1.1",
32
- "tape": "~4.9.1"
33
+ "faucet": "~0.0.1",
34
+ "standard": "^14.3.0",
35
+ "tape": "^4.11.0"
33
36
  }
34
37
  }
@@ -0,0 +1,21 @@
1
+ 'use strict'
2
+
3
+ const tape = require('tape')
4
+ const { BufferList, BufferListStream } = require('../')
5
+ const { Buffer } = require('buffer')
6
+
7
+ tape('convert from BufferList to BufferListStream', (t) => {
8
+ const data = Buffer.from(`TEST-${Date.now()}`)
9
+ const bl = new BufferList(data)
10
+ const bls = new BufferListStream(bl)
11
+ t.ok(bl.slice().equals(bls.slice()))
12
+ t.end()
13
+ })
14
+
15
+ tape('convert from BufferListStream to BufferList', (t) => {
16
+ const data = Buffer.from(`TEST-${Date.now()}`)
17
+ const bls = new BufferListStream(data)
18
+ const bl = new BufferList(bls)
19
+ t.ok(bl.slice().equals(bls.slice()))
20
+ t.end()
21
+ })