stream-promise
Advanced tools
Comparing version
@@ -5,2 +5,17 @@ # Change Log | ||
<a name="3.1.0"></a> | ||
# [3.1.0](https://github.com/medikoo/stream-promise/compare/v3.0.0...v3.1.0) (2019-01-17) | ||
### Bug Fixes | ||
* writable streams should resolve with udefined ([ab21212](https://github.com/medikoo/stream-promise/commit/ab21212)) | ||
### Features | ||
* expose emitted data at emittedData property ([b35d101](https://github.com/medikoo/stream-promise/commit/b35d101)) | ||
<a name="3.0.0"></a> | ||
@@ -7,0 +22,0 @@ # [3.0.0](https://github.com/medikoo/stream-promise/compare/v1.0.0...v3.0.0) (2019-01-17) |
@@ -6,2 +6,9 @@ "use strict"; | ||
module.exports = stream => toThenable(stream, toPromise(stream)); | ||
module.exports = stream => { | ||
const promise = toPromise(stream); | ||
return Object.defineProperty(toThenable(stream, promise), "emittedData", { | ||
configurable: true, | ||
enumerable: true, | ||
get: () => promise.emittedData | ||
}); | ||
}; |
{ | ||
"name": "stream-promise", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Promise that shares Node.js Stream interface", | ||
@@ -5,0 +5,0 @@ "author": "Mariusz Nowak <medyk@medikoo.com> (http://www.medikoo.com/)", |
@@ -23,5 +23,5 @@ [![*nix build status][nix-build-image]][nix-build-url] | ||
In case of readable stream promise resolves with concatenated output, in case of writable streams resolves simply with `null`. | ||
In case of readable streams, promise resolves with concatenated output, in case of writable streams resolve with `undefined` | ||
To achieve expected result stream should to be converted immediately after initialization. | ||
To achieve expected result stream should be converted immediately after initialization. | ||
@@ -44,2 +44,4 @@ ```javascript | ||
Already emitted data is accessible at `emittedData` property | ||
## Non-destructive way | ||
@@ -73,1 +75,2 @@ | ||
[npm-image]: https://img.shields.io/npm/v/stream-promise.svg | ||
[npm-url]: https://www.npmjs.com/package/stream-promise |
@@ -16,2 +16,9 @@ "use strict"; | ||
}); | ||
it("Should expose emitted data", () => { | ||
let counter = 2; | ||
const stream = Object.assign(new Readable({ encoding: "utf8" }), { | ||
_read() { this.push(counter-- ? String(counter) : null); } | ||
}); | ||
return streamPromise(stream).then(() => { assert.equal(stream.emittedData, "10"); }); | ||
}); | ||
}); |
@@ -41,3 +41,3 @@ "use strict"; | ||
}); | ||
return toPromise(stream).then(result => { assert.equal(result, 10); }); | ||
return toPromise(stream).then(result => { assert.equal(result, "10"); }); | ||
}); | ||
@@ -54,2 +54,13 @@ it("Object streams should resolve with an array", () => { | ||
}); | ||
it("Should expose already emitted data", () => { | ||
let counter = 2; | ||
const stream = Object.assign(new Readable({ encoding: "utf8" }), { | ||
_read() { | ||
if (counter--) this.push(String(counter)); | ||
else this.push(null); | ||
} | ||
}); | ||
const promise = toPromise(stream); | ||
return promise.then(() => { assert.equal(promise.emittedData, "10"); }); | ||
}); | ||
}); | ||
@@ -74,5 +85,5 @@ describe("Writable streams", () => { | ||
stream.end(); | ||
return result.then(result => assert.equal(result, null)); | ||
return result.then(result => assert.equal(result, undefined)); | ||
}); | ||
}); | ||
}); |
@@ -13,6 +13,6 @@ "use strict"; | ||
} | ||
return new Promise((resolve, reject) => { | ||
let result = null; | ||
const promise = new Promise((resolve, reject) => { | ||
stream.on("error", reject); | ||
let result = null; | ||
if (isStream.readable(stream)) { | ||
@@ -26,14 +26,14 @@ if (stream._readableState.objectMode) { | ||
if (!result) { | ||
result = data; | ||
promise.emittedData = result = data; | ||
} else if (Buffer.isBuffer(data)) { | ||
result = Buffer.concat([data, Buffer.from(data)]); | ||
promise.emittedData = result = Buffer.concat([data, Buffer.from(data)]); | ||
} else { | ||
result += data; | ||
promise.emittedData = result += data; | ||
} | ||
} else if (!result) { | ||
result = data; | ||
promise.emittedData = result = data; | ||
} else if (Buffer.isBuffer(result)) { | ||
result = Buffer.concat([result, data]); | ||
promise.emittedData = result = Buffer.concat([result, data]); | ||
} else { | ||
result = Buffer.concat([Buffer.from(result), data]); | ||
promise.emittedData = result = Buffer.concat([Buffer.from(result), data]); | ||
} | ||
@@ -44,5 +44,7 @@ }); | ||
} else { | ||
stream.on("finish", () => resolve(result)); | ||
stream.on("finish", () => resolve()); | ||
} | ||
}); | ||
if (isStream.readable) promise.emittedData = result; | ||
return promise; | ||
}; |
10996
16.02%160
20.3%74
4.23%