read-all-stream
Advanced tools
Comparing version
42
index.js
'use strict'; | ||
var concat = require('concat-stream'); | ||
var Writable = require('readable-stream').Writable; | ||
var inherits = require('util').inherits; | ||
function BufferStream() { | ||
Writable.call(this, { objectMode: true }); | ||
this.buffer = []; | ||
this.length = 0; | ||
} | ||
inherits(BufferStream, Writable); | ||
BufferStream.prototype._write = function(chunk, enc, next) { | ||
if (!Buffer.isBuffer(chunk)) { | ||
chunk = new Buffer(chunk); | ||
} | ||
this.buffer.push(chunk); | ||
this.length += chunk.length; | ||
next(); | ||
}; | ||
module.exports = function read(stream, options, cb) { | ||
@@ -25,11 +43,17 @@ if (!stream) { | ||
stream | ||
.once('error', cb) | ||
.pipe(concat({encoding: 'buffer'}, function (data) { | ||
if (options.encoding) { | ||
data = data.toString(options.encoding); | ||
} | ||
var sink = new BufferStream(); | ||
cb(null, data); | ||
})); | ||
sink.on('finish', function () { | ||
var data = Buffer.concat(this.buffer, this.length); | ||
if (options.encoding) { | ||
data = data.toString(options.encoding); | ||
} | ||
cb(null, data); | ||
}); | ||
stream.once('error', cb); | ||
stream.pipe(sink); | ||
}; |
{ | ||
"name": "read-all-stream", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"description": "Read all stream content and pass it to callback", | ||
@@ -15,4 +15,3 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "mocha", | ||
"coverage": "istanbul cover _mocha -- -R spec" | ||
"test": "mocha" | ||
}, | ||
@@ -29,9 +28,7 @@ "files": [ | ||
"devDependencies": { | ||
"fast-apply": "0.0.3", | ||
"istanbul": "^0.3.2", | ||
"mocha": "*" | ||
}, | ||
"dependencies": { | ||
"concat-stream": "^1.4.8" | ||
"readable-stream": "~1.1.13" | ||
} | ||
} |
2897
14.69%1
-66.67%43
65.38%+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed