Comparing version 0.4.2 to 0.5.0
73
bl.js
@@ -68,34 +68,55 @@ const DuplexStream = require('stream').DuplexStream || require('readable-stream/duplex') | ||
BufferList.prototype.slice = function (start, end) { | ||
if (typeof start != 'number' || start < 0) | ||
start = 0 | ||
if (typeof end != 'number' || end > this.length) | ||
end = this.length | ||
if (start >= this.length) | ||
return new Buffer(0) | ||
if (end <= 0) | ||
return new Buffer(0) | ||
return this.copy(null, 0, start, end) | ||
} | ||
if (start === 0 && end == this.length) | ||
return Buffer.concat(this._bufs) | ||
BufferList.prototype.copy = function (dst, dstStart, srcStart, srcEnd) { | ||
if (typeof srcStart != 'number' || srcStart < 0) | ||
srcStart = 0 | ||
if (typeof srcEnd != 'number' || srcEnd > this.length) | ||
srcEnd = this.length | ||
if (srcStart >= this.length) | ||
return dst || new Buffer(0) | ||
if (srcEnd <= 0) | ||
return dst || new Buffer(0) | ||
var off = this._offset(start) | ||
, len = end - start | ||
var copy = !!dst | ||
, off = this._offset(srcStart) | ||
, len = srcEnd - srcStart | ||
, bytes = len | ||
, bufoff = 0 | ||
, buf, l, i | ||
, bufoff = (copy && dstStart) || 0 | ||
, start = off[1] | ||
, l | ||
, i | ||
start = off[1] | ||
// copy/slice everything | ||
if (srcStart === 0 && srcEnd == this.length) { | ||
if (!copy) // slice, just return a full concat | ||
return Buffer.concat(this._bufs) | ||
// copy, need to copy individual buffers | ||
for (i = 0; i < this._bufs.length; i++) { | ||
this._bufs[i].copy(dst, bufoff) | ||
bufoff += this._bufs[i].length | ||
} | ||
return dst | ||
} | ||
// easy, cheap case where it's a subset of one of the buffers | ||
if (bytes <= this._bufs[off[0]].length - start) | ||
return this._bufs[off[0]].slice(start, start + bytes) | ||
if (bytes <= this._bufs[off[0]].length - start) { | ||
return copy | ||
? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes) | ||
: this._bufs[off[0]].slice(start, start + bytes) | ||
} | ||
buf = new Buffer(len) | ||
if (!copy) // a slice, we need something to copy in to | ||
dst = new Buffer(len) | ||
for (i = off[0]; i < this._bufs.length; i++) { | ||
l = this._bufs[i].length - start | ||
if (bytes > l) { | ||
this._bufs[i].copy(buf, bufoff, start) | ||
this._bufs[i].copy(dst, bufoff, start) | ||
} else { | ||
this._bufs[i].copy(buf, bufoff, start, start + bytes) | ||
this._bufs[i].copy(dst, bufoff, start, start + bytes) | ||
break | ||
@@ -111,3 +132,3 @@ } | ||
return buf | ||
return dst | ||
} | ||
@@ -134,2 +155,12 @@ | ||
BufferList.prototype.duplicate = function () { | ||
var i = 0 | ||
, copy = new BufferList() | ||
for (; i < this._bufs.length; i++) | ||
copy.append(this._bufs[i]) | ||
return copy | ||
} | ||
;(function () { | ||
@@ -136,0 +167,0 @@ var methods = { |
{ | ||
"name" : "bl" | ||
, "version" : "0.4.2" | ||
, "version" : "0.5.0" | ||
, "description" : "Buffer List: collect buffers and access with a standard readable Buffer interface, streamable too!" | ||
@@ -5,0 +5,0 @@ , "main" : "bl.js" |
@@ -87,2 +87,4 @@ # bl *(BufferList)* | ||
* <a href="#slice"><code>bl.<b>slice([ start[, end ] ])</b></code></a> | ||
* <a href="#copy"><code>bl.<b>copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ])</b></code></a> | ||
* <a href="#duplicate"><code>bl.<b>duplicate()</b></code></a> | ||
* <a href="#consume"><code>bl.<b>consume(bytes)</b></code></a> | ||
@@ -135,2 +137,24 @@ * <a href="#toString"><code>bl.<b>toString([encoding, [ start, [ end ]]])</b></code></a> | ||
-------------------------------------------------------- | ||
<a name="copy"></a> | ||
### bl.copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ]) | ||
`copy()` copies the content of the list in the `dest` buffer, starting from `destStart` and containing the bytes within the range specified with `srcStart` to `srcEnd`. `destStart`, `start` and `end` are optional and will default to the beginning of the `dest` buffer, and the beginning and end of the list respectively. | ||
-------------------------------------------------------- | ||
<a name="duplicate"></a> | ||
### bl.duplicate() | ||
`duplicate()` performs a **shallow-copy** of the list. The internal Buffers remains the same, so if you change the underlying Buffers, the change will be reflected in both the original and the duplicate. This method is needed if you want to call `consume()` or `pipe()` and still keep the original list.Example: | ||
```js | ||
var bl = new BufferList() | ||
bl.append('hello') | ||
bl.append(' world') | ||
bl.append('\n') | ||
bl.duplicate().pipe(process.stdout, { end: false }) | ||
console.log(bl.toString()) | ||
``` | ||
-------------------------------------------------------- | ||
<a name="consume"></a> | ||
@@ -158,2 +182,4 @@ ### bl.consume(bytes) | ||
-------------------------------------------------------- | ||
## Contributors | ||
@@ -166,4 +192,6 @@ | ||
======= | ||
## License | ||
**bl** is Copyright (c) 2013 Rod Vagg [@rvagg](https://twitter.com/rvagg) and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details. |
78
test.js
@@ -357,1 +357,79 @@ const tape = require('tape') | ||
}) | ||
tape('basic copy', function (t) { | ||
var buf = crypto.randomBytes(1024) | ||
, buf2 = new Buffer(1024) | ||
, b = BufferList(buf) | ||
b.copy(buf2) | ||
t.equal(hash(b.slice(), 'md5'), hash(buf2, 'md5'), 'same hash!') | ||
t.end() | ||
}) | ||
tape('copy after many appends', function (t) { | ||
var buf = crypto.randomBytes(512) | ||
, buf2 = new Buffer(1024) | ||
, b = BufferList(buf) | ||
b.append(buf) | ||
b.copy(buf2) | ||
t.equal(hash(b.slice(), 'md5'), hash(buf2, 'md5'), 'same hash!') | ||
t.end() | ||
}) | ||
tape('copy at a precise position', function (t) { | ||
var buf = crypto.randomBytes(1004) | ||
, buf2 = new Buffer(1024) | ||
, b = BufferList(buf) | ||
b.copy(buf2, 20) | ||
t.equal(hash(b.slice(), 'md5'), hash(buf2.slice(20), 'md5'), 'same hash!') | ||
t.end() | ||
}) | ||
tape('copy starting from a precise location', function (t) { | ||
var buf = crypto.randomBytes(10) | ||
, buf2 = new Buffer(5) | ||
, b = BufferList(buf) | ||
b.copy(buf2, 0, 5) | ||
t.equal(hash(b.slice(5), 'md5'), hash(buf2, 'md5'), 'same hash!') | ||
t.end() | ||
}) | ||
tape('copy in an interval', function (t) { | ||
var buf = crypto.randomBytes(10) | ||
, buf2 = new Buffer(3) | ||
, b = BufferList(buf) | ||
, expected = new Buffer(3) | ||
// put the same old data there | ||
buf2.copy(expected) | ||
buf.copy(expected, 0, 5, 7) | ||
b.copy(buf2, 0, 5, 7) | ||
t.equal(hash(expected, 'md5'), hash(buf2, 'md5'), 'same hash!') | ||
t.end() | ||
}) | ||
tape('copy an interval between two buffers', function (t) { | ||
var buf = crypto.randomBytes(10) | ||
, buf2 = new Buffer(10) | ||
, b = BufferList(buf) | ||
b.append(buf) | ||
b.copy(buf2, 0, 5, 15) | ||
t.equal(hash(b.slice(5, 15), 'md5'), hash(buf2, 'md5'), 'same hash!') | ||
t.end() | ||
}) | ||
tape('duplicate', function (t) { | ||
t.plan(2) | ||
var bl = new BufferList('abcdefghij\xff\x00') | ||
, dup = bl.duplicate() | ||
t.equal(bl.prototype, dup.prototype) | ||
t.equal(bl.toString('hex'), dup.toString('hex')) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28949
503
194