Comparing version 0.2.1 to 1.0.0
@@ -0,1 +1,10 @@ | ||
## 1.0.0 - Released 2019/01/04 | ||
1. drop node 4, add node 11 | ||
2. update deps | ||
3. rename 'test-all' to 'tests' | ||
4. rewrite as a class | ||
5. replace "new Buffer" with "Buffer.alloc". | ||
## 0.2.1 - Released 2018/07/18 | ||
@@ -2,0 +11,0 @@ |
212
lib/index.js
@@ -1,149 +0,151 @@ | ||
var Buffer = require('buffer').Buffer | ||
var Duplex = require('stream').Duplex | ||
const {Buffer} = require('buffer') | ||
const {Duplex} = require('stream') | ||
function Buffed(source) { | ||
Duplex.call(this, { decodeStrings: true }) | ||
class Buffed extends Duplex { | ||
if (source) { | ||
this._source = Array.isArray(source) ? source : [source] | ||
constructor(source) { | ||
super({ | ||
decodeStrings: true | ||
}) | ||
if (source) { | ||
this._source = Array.isArray(source) ? source : [source] | ||
} | ||
} | ||
} | ||
// extend Duplex | ||
Buffed.prototype = Object.create(Duplex.prototype) | ||
Buffed.prototype.constructor = Buffed | ||
// read from its source string or send null when it's all sent | ||
_read(size) { | ||
// read from its source string or send null when it's all sent | ||
Buffed.prototype._read = function(size) { | ||
if (this._source) { | ||
if (this._source) { | ||
switch (this._source.length) { | ||
switch (this._source.length) { | ||
case 0: | ||
this.push(null) | ||
break | ||
case 0: | ||
this.push(null) | ||
break | ||
case 1: | ||
this.push(this._source.pop()) | ||
break | ||
case 1: | ||
this.push(this._source.pop()) | ||
break | ||
default: // op in loop condition | ||
default: // op in loop condition | ||
while (this.push(this._source.shift()) && this._source.length) | ||
/* noop */ ; | ||
} | ||
while (this.push(this._source.shift()) && this._source.length) | ||
/* noop */ ; | ||
} | ||
else { | ||
this.push(null) | ||
} | ||
} | ||
else { | ||
this.push(null) | ||
} | ||
} | ||
// accumulates what's written into `buffers` | ||
_write(buffer, _, next) { | ||
// accumulates what's written into `buffers` | ||
Buffed.prototype._write = function(buffer, _, next) { | ||
// if we already have some then place the new one at the end | ||
// and add its byte count. | ||
if (this.buffers) { | ||
this.buffers[this.buffers.length] = buffer | ||
this.bufferBytes += buffer.length | ||
} | ||
// if we already have some then place the new one at the end | ||
// and add its byte count. | ||
if (this.buffers) { | ||
this.buffers[this.buffers.length] = buffer | ||
this.bufferBytes += buffer.length | ||
} | ||
// otherwise, create a new array with the buffer in it | ||
// and store its byte count. | ||
else { | ||
this.buffers = [buffer] | ||
this.bufferBytes = buffer.length | ||
} | ||
// otherwise, create a new array with the buffer in it | ||
// and store its byte count. | ||
else { | ||
this.buffers = [buffer] | ||
this.bufferBytes = buffer.length | ||
next() | ||
} | ||
next() | ||
} | ||
// combine all the gathered buffers into one buffer | ||
combine() { | ||
// combine all the gathered buffers into one buffer | ||
Buffed.prototype.combine = function combine() { | ||
// will hold a single Buffer with all bytes. | ||
let combined | ||
// will hold a single Buffer with all bytes. | ||
var combined | ||
// if we have stored buffers | ||
if (this.buffers) { | ||
// if we have stored buffers | ||
if (this.buffers) { | ||
// determine what to do by the number of buffers we have stored. | ||
switch (this.buffers.length) { | ||
// determine what to do by the number of buffers we have stored. | ||
switch (this.buffers.length) { | ||
case 0: // none, so, create an empty Buffer. | ||
combined = Buffer.allocUnsafe(0) | ||
break | ||
case 0: // none, so, create an empty Buffer. | ||
combined = new Buffer(0) | ||
break | ||
case 1: // only one, so, just return that one Buffer. | ||
combined = this.buffers[0] | ||
break | ||
case 1: // only one, so, just return that one Buffer. | ||
combined = this.buffers[0] | ||
break | ||
default: // more than one, so concat them all together. | ||
combined = Buffer.concat(this.buffers, this.bufferBytes) | ||
// also, take this opportunity to store our work by | ||
// replacing the array of Buffer's with our new single one. | ||
// this means calling `combine()` repeatedly will do this work once. | ||
this.buffers = [combined] | ||
} | ||
default: // more than one, so concat them all together. | ||
combined = Buffer.concat(this.buffers, this.bufferBytes) | ||
// also, take this opportunity to store our work by | ||
// replacing the array of Buffer's with our new single one. | ||
// this means calling `combine()` repeatedly will do this work once. | ||
this.buffers = [combined] | ||
} | ||
} | ||
// otherwise, we have nothing, so, create an empty Buffer. | ||
else { | ||
combined = Buffer.allocUnsafe(0) | ||
} | ||
// otherwise, we have nothing, so, create an empty Buffer. | ||
else { | ||
combined = new Buffer(0) | ||
// return the Buffer result. | ||
return combined | ||
} | ||
// return the Buffer result. | ||
return combined | ||
} | ||
// pipe a buffer to set its source, | ||
// pipe a stream to send its source to the stream. | ||
pipe(target) { | ||
// pipe a buffer to set its source, | ||
// pipe a stream to send its source to the stream. | ||
Buffed.prototype.pipe = function pipe(target) { | ||
// if: | ||
// 1. there isn't a target to pipe to | ||
// 2. or, the target is a Buffer | ||
// 3. or the target is an array | ||
if (!target || Buffer.isBuffer(target) || Array.isArray(target)) { | ||
// give the target to reset() | ||
return this.reset(target) | ||
} | ||
// if: | ||
// 1. there isn't a target to pipe to | ||
// 2. or, the target is a Buffer | ||
// 3. or the target is an array | ||
if (!target || Buffer.isBuffer(target) || Array.isArray(target)) { | ||
// give the target to reset() | ||
return this.reset(target) | ||
// otherwise, it's a stream, so use the Duplex constructor to | ||
// reinitialize streaming to this target. | ||
else { | ||
return Duplex.prototype.pipe.call(this, target) | ||
} | ||
} | ||
// otherwise, it's a stream, so use the Duplex constructor to | ||
// reinitialize streaming to this target. | ||
else { | ||
return Duplex.prototype.pipe.call(this, target) | ||
} | ||
} | ||
// make ready to use it again. | ||
// optionally allow a new source. | ||
reset(source) { | ||
// make ready to use it again. | ||
// optionally allow a new source. | ||
Buffed.prototype.reset = function reset(source) { | ||
// initialize with the constructor. we decode strings into bytes. | ||
Duplex.call(this, { decodeStrings: true }) | ||
// initialize with the constructor. we decode strings into bytes. | ||
Duplex.call(this, { decodeStrings: true }) | ||
// if an optional source was provided then store it as an array. | ||
// if it's already an array, then use it. | ||
if (source) { | ||
this._source = Array.isArray(source) ? source : [source] | ||
} | ||
// if an optional source was provided then store it as an array. | ||
// if it's already an array, then use it. | ||
if (source) { | ||
this._source = Array.isArray(source) ? source : [source] | ||
} | ||
// else there's no new source, so if there's a current source stored | ||
// then truncate its array to get rid of it. | ||
else if (this._source){ | ||
this._source.length = 0 | ||
} | ||
// else there's no new source, so if there's a current source stored | ||
// then truncate its array to get rid of it. | ||
else if (this._source){ | ||
this._source.length = 0 | ||
} | ||
// lastly, if there are stored buffers then truncate its array. | ||
if (this.buffers) { | ||
this.buffers.length = 0 | ||
} | ||
// lastly, if there are stored buffers then truncate its array. | ||
if (this.buffers) { | ||
this.buffers.length = 0 | ||
// return this for chaining. | ||
return this | ||
} | ||
// return this for chaining. | ||
return this | ||
} | ||
@@ -150,0 +152,0 @@ |
{ | ||
"name": "buffed", | ||
"version": "0.2.1", | ||
"version": "1.0.0", | ||
"description": "Acts as a stream to send a buffer, gather buffers, or both.", | ||
@@ -35,7 +35,7 @@ "main": "lib/index.js", | ||
"test": "_mocha --R spec --bail --check-leaks test/lib/*.js", | ||
"test4": "nave use 4.9 npm test", | ||
"test6": "nave use 6.14 npm test", | ||
"test8": "nave use 8.11 npm test", | ||
"test10": "nave use 10.6 npm test", | ||
"test-all": "npm run test4 && npm run test6 && npm run test8 && npm run test10", | ||
"test6": "nave use 6 npm test", | ||
"test8": "nave use 8 npm test", | ||
"test10": "nave use 10 npm test", | ||
"test11": "nave use 11 npm test", | ||
"tests": "npm run test6 && npm run test8 && npm run test10 && npm run test11", | ||
"coverage": "istanbul cover -x 'build/**' _mocha -- -R spec test/lib/*.js" | ||
@@ -53,8 +53,8 @@ }, | ||
"mocha-lcov-reporter": "^1.3.0", | ||
"through2": "^2.0.3" | ||
"through2": "^3.0.0" | ||
}, | ||
"dependencies": {}, | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=6" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11445
130
1