bl 3.0.1 → 4.0.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/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
-
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
48
- }
49
- }
50
28
 
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
29
+ callback = null
56
30
  }
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
31
 
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,277 +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
- bufoff += l
189
- } else {
190
- this._bufs[i].copy(dst, bufoff, start, start + bytes)
191
- bufoff += l
192
- break
193
- }
194
-
195
- bytes -= l
196
-
197
- if (start)
198
- start = 0
199
- }
200
-
201
- // safeguard so that we don't return uninitialized memory
202
- if (dst.length > bufoff) return dst.slice(0, bufoff)
203
-
204
- return dst
205
- }
206
-
207
- BufferList.prototype.shallowSlice = function shallowSlice (start, end) {
208
- start = start || 0
209
- end = typeof end !== 'number' ? this.length : end
210
-
211
- if (start < 0)
212
- start += this.length
213
- if (end < 0)
214
- end += this.length
215
-
216
- if (start === end) {
217
- return new BufferList()
218
- }
219
- var startOffset = this._offset(start)
220
- , endOffset = this._offset(end)
221
- , buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
222
-
223
- if (endOffset[1] == 0)
224
- buffers.pop()
225
- else
226
- buffers[buffers.length-1] = buffers[buffers.length-1].slice(0, endOffset[1])
227
-
228
- if (startOffset[1] != 0)
229
- buffers[0] = buffers[0].slice(startOffset[1])
230
-
231
- return new BufferList(buffers)
232
- }
233
-
234
- BufferList.prototype.toString = function toString (encoding, start, end) {
235
- return this.slice(start, end).toString(encoding)
236
- }
237
-
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
-
244
- while (this._bufs.length) {
245
- if (bytes >= this._bufs[0].length) {
246
- bytes -= this._bufs[0].length
247
- this.length -= this._bufs[0].length
248
- this._bufs.shift()
249
- } else {
250
- this._bufs[0] = this._bufs[0].slice(bytes)
251
- this.length -= bytes
252
- break
253
- }
254
- }
255
- return this
256
- }
257
-
258
-
259
- BufferList.prototype.duplicate = function duplicate () {
260
- var i = 0
261
- , copy = new BufferList()
262
-
263
- for (; i < this._bufs.length; i++)
264
- copy.append(this._bufs[i])
265
-
266
- return copy
267
- }
268
-
269
-
270
- BufferList.prototype._destroy = function _destroy (err, cb) {
70
+ BufferListStream.prototype._destroy = function _destroy (err, cb) {
271
71
  this._bufs.length = 0
272
72
  this.length = 0
273
73
  cb(err)
274
74
  }
275
75
 
276
-
277
- BufferList.prototype.indexOf = function (search, offset, encoding) {
278
- if (encoding === undefined && typeof offset === 'string') {
279
- encoding = offset
280
- offset = undefined
281
- }
282
- if (typeof search === 'function' || Array.isArray(search)) {
283
- throw new TypeError('The "value" argument must be one of type string, Buffer, BufferList, or Uint8Array.')
284
- } else if (typeof search === 'number') {
285
- search = Buffer.from([search])
286
- } else if (typeof search === 'string') {
287
- search = Buffer.from(search, encoding)
288
- } else if (search instanceof BufferList) {
289
- search = search.slice()
290
- } else if (!Buffer.isBuffer(search)) {
291
- search = Buffer.from(search)
292
- }
293
-
294
- offset = Number(offset || 0)
295
- if (isNaN(offset)) {
296
- offset = 0
297
- }
298
-
299
- if (offset < 0) {
300
- offset = this.length + offset
301
- }
302
-
303
- if (offset < 0) {
304
- offset = 0
305
- }
306
-
307
- if (search.length === 0) {
308
- return offset > this.length ? this.length : offset
309
- }
310
-
311
- var blOffset = this._offset(offset)
312
- var blIndex = blOffset[0] // index of which internal buffer we're working on
313
- var buffOffset = blOffset[1] // offset of the internal buffer we're working on
314
-
315
- // scan over each buffer
316
- for (blIndex; blIndex < this._bufs.length; blIndex++) {
317
- var buff = this._bufs[blIndex]
318
- while(buffOffset < buff.length) {
319
- var availableWindow = buff.length - buffOffset
320
- if (availableWindow >= search.length) {
321
- var nativeSearchResult = buff.indexOf(search, buffOffset)
322
- if (nativeSearchResult !== -1) {
323
- return this._reverseOffset([blIndex, nativeSearchResult])
324
- }
325
- buffOffset = buff.length - search.length + 1 // end of native search window
326
- } else {
327
- var revOffset = this._reverseOffset([blIndex, buffOffset])
328
- if (this._match(revOffset, search)) {
329
- return revOffset
330
- }
331
- buffOffset++
332
- }
333
- }
334
- buffOffset = 0
335
- }
336
- return -1
76
+ BufferListStream.prototype._isBufferList = function _isBufferList (b) {
77
+ return b instanceof BufferListStream || b instanceof BufferList || BufferListStream.isBufferList(b)
337
78
  }
338
79
 
339
- BufferList.prototype._match = function(offset, search) {
340
- if (this.length - offset < search.length) {
341
- return false
342
- }
343
- for (var searchOffset = 0; searchOffset < search.length ; searchOffset++) {
344
- if(this.get(offset + searchOffset) !== search[searchOffset]){
345
- return false
346
- }
347
- }
348
- return true
349
- }
350
-
351
-
352
- ;(function () {
353
- var methods = {
354
- 'readDoubleBE' : 8
355
- , 'readDoubleLE' : 8
356
- , 'readFloatBE' : 4
357
- , 'readFloatLE' : 4
358
- , 'readInt32BE' : 4
359
- , 'readInt32LE' : 4
360
- , 'readUInt32BE' : 4
361
- , 'readUInt32LE' : 4
362
- , 'readInt16BE' : 2
363
- , 'readInt16LE' : 2
364
- , 'readUInt16BE' : 2
365
- , 'readUInt16LE' : 2
366
- , 'readInt8' : 1
367
- , 'readUInt8' : 1
368
- , 'readIntBE' : null
369
- , 'readIntLE' : null
370
- , 'readUIntBE' : null
371
- , 'readUIntLE' : null
372
- }
373
-
374
- for (var m in methods) {
375
- (function (m) {
376
- if (methods[m] === null) {
377
- BufferList.prototype[m] = function (offset, byteLength) {
378
- return this.slice(offset, offset + byteLength)[m](0, byteLength)
379
- }
380
- }
381
- else {
382
- BufferList.prototype[m] = function (offset) {
383
- return this.slice(offset, offset + methods[m])[m](0)
384
- }
385
- }
386
- }(m))
387
- }
388
- }())
389
-
80
+ BufferListStream.isBufferList = BufferList.isBufferList
390
81
 
391
- module.exports = BufferList
82
+ module.exports = BufferListStream
83
+ module.exports.BufferListStream = BufferListStream
84
+ module.exports.BufferList = BufferList
package/package.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "name": "bl",
3
- "version": "3.0.1",
3
+ "version": "4.0.3",
4
4
  "description": "Buffer List: collect buffers and access with a standard readable Buffer interface, streamable too!",
5
+ "license": "MIT",
5
6
  "main": "bl.js",
6
7
  "scripts": {
7
- "test": "node test/test.js | faucet"
8
+ "lint": "standard *.js test/*.js",
9
+ "test": "npm run lint && node test/test.js | faucet"
8
10
  },
9
11
  "repository": {
10
12
  "type": "git",
@@ -24,11 +26,13 @@
24
26
  ],
25
27
  "license": "MIT",
26
28
  "dependencies": {
27
- "readable-stream": "^3.0.1"
29
+ "buffer": "^5.5.0",
30
+ "inherits": "^2.0.4",
31
+ "readable-stream": "^3.4.0"
28
32
  },
29
33
  "devDependencies": {
30
- "faucet": "0.0.1",
31
- "hash_file": "~0.1.1",
32
- "tape": "~4.9.1"
34
+ "faucet": "~0.0.1",
35
+ "standard": "^14.3.0",
36
+ "tape": "^4.11.0"
33
37
  }
34
38
  }
@@ -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
+ })