read-all-stream
Advanced tools
+19
-12
@@ -5,2 +5,3 @@ 'use strict'; | ||
| var inherits = require('util').inherits; | ||
| var Promise = require('pinkie-promise'); | ||
@@ -40,21 +41,27 @@ function BufferStream() { | ||
| if (!cb) { | ||
| throw new Error('callback argument is required'); | ||
| } | ||
| var promise = new Promise(function (resolve, reject) { | ||
| var sink = new BufferStream(); | ||
| var sink = new BufferStream(); | ||
| sink.on('finish', function () { | ||
| var data = Buffer.concat(this.buffer, this.length); | ||
| sink.on('finish', function () { | ||
| var data = Buffer.concat(this.buffer, this.length); | ||
| if (options.encoding) { | ||
| data = data.toString(options.encoding); | ||
| } | ||
| if (options.encoding) { | ||
| data = data.toString(options.encoding); | ||
| } | ||
| resolve(data); | ||
| }); | ||
| cb(null, data); | ||
| stream.once('error', reject); | ||
| stream.pipe(sink); | ||
| }); | ||
| stream.once('error', cb); | ||
| if (!cb) { | ||
| return promise; | ||
| } | ||
| stream.pipe(sink); | ||
| promise.then(function (data) { | ||
| cb(null, data); | ||
| }, cb); | ||
| }; |
+2
-1
| { | ||
| "name": "read-all-stream", | ||
| "version": "2.2.0", | ||
| "version": "3.0.0", | ||
| "description": "Read all stream content and pass it to callback", | ||
@@ -30,4 +30,5 @@ "license": "MIT", | ||
| "dependencies": { | ||
| "pinkie-promise": "^1.0.0", | ||
| "readable-stream": "^2.0.0" | ||
| } | ||
| } |
+9
-1
@@ -17,2 +17,6 @@ # read-all-stream [![Build Status][travis-image]][travis-url] | ||
| read(stream).then(function (data) { | ||
| console.log(data.length); | ||
| }); | ||
| read(stream, 'utf8', function (err, data) { | ||
@@ -27,4 +31,6 @@ console.log(data.length); | ||
| #### read(stream, [options], callback) | ||
| #### read(stream, [options], [callback]) | ||
| If callback is omitted, Promise will be returned. | ||
| ##### stream | ||
@@ -53,2 +59,4 @@ | ||
| Will be called after stream is read. | ||
| ###### err | ||
@@ -55,0 +63,0 @@ |
4333
8.3%49
13.95%73
12.31%2
100%+ Added
+ Added
+ Added