stream-promise
Advanced tools
Comparing version
@@ -5,3 +5,26 @@ # Change Log | ||
<a name="3.0.0"></a> | ||
# [3.0.0](https://github.com/medikoo/stream-promise/compare/v1.0.0...v3.0.0) (2019-01-17) | ||
### Chores | ||
* publish as steam-promise ([feff7a5](https://github.com/medikoo/stream-promise/commit/feff7a5)) | ||
### Features | ||
* stream to promise approach ([8669fab](https://github.com/medikoo/stream-promise/commit/8669fab)) | ||
### BREAKING CHANGES | ||
* Remove non that useful StreamPromise construtor with utility | ||
that converts initialized stream to promise | ||
* We're skipping v1 to avoid semver confusion with | ||
previously published version by other author | ||
<a name="1.0.0"></a> | ||
# 1.0.0 (2019-01-14) |
15
index.js
"use strict"; | ||
const Stream = require("stream"); | ||
const toThenable = require("2-thenable") | ||
, toPromise = require("./to-promise"); | ||
class StreamEmitter extends Promise { | ||
constructor(executor) { | ||
super(executor); | ||
Stream.call(this); | ||
} | ||
} | ||
const EventEmitterPrototype = Object.getPrototypeOf(Stream.prototype); | ||
Object.assign(StreamEmitter.prototype, EventEmitterPrototype, Stream.prototype); | ||
module.exports = StreamEmitter; | ||
module.exports = stream => toThenable(stream, toPromise(stream)); |
{ | ||
"name": "stream-promise", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Promise that shares Node.js Stream interface", | ||
@@ -13,2 +13,7 @@ "author": "Mariusz Nowak <medyk@medikoo.com> (http://www.medikoo.com/)", | ||
"repository": "medikoo/stream-promise", | ||
"dependencies": { | ||
"2-thenable": "1", | ||
"es5-ext": "^0.10.47", | ||
"is-stream": "^1.1" | ||
}, | ||
"devDependencies": { | ||
@@ -40,5 +45,5 @@ "chai": "^4.2", | ||
"lint": "eslint --ignore-path=.gitignore .", | ||
"test": "mocha \"test/**.js\"" | ||
"test": "mocha" | ||
}, | ||
"license": "ISC" | ||
} |
@@ -5,8 +5,9 @@ [![*nix build status][nix-build-image]][nix-build-url] | ||
![Transpilation status][transpilation-image] | ||
[![npm version][npm-image]][npm-url] | ||
# stream-promise | ||
## Promise that's a also a Node.js [Stream](https://nodejs.org/api/stream.html#stream_stream) | ||
## Convert any [Stream](https://nodejs.org/api/stream.html#stream_stream) instance to thenable | ||
Useful when we want to serve both a [`Stream`](https://nodejs.org/api/stream.html#stream_stream) instance and a `Promise` instance as one object | ||
So it can be consumed both as a promise and as a stream | ||
@@ -21,15 +22,36 @@ ### Installation | ||
Stream must be either readable or writable. | ||
In case of readable stream promise resolves with concatenated output, in case of writable streams resolves simply with `null`. | ||
To achieve expected result stream should to be converted immediately after initialization. | ||
```javascript | ||
const StreamPromise = require("stream-promise"); | ||
const streamPromise = require("stream-promise"); | ||
const streamPromise = new StreamPromise((resolve, reject) => { | ||
... | ||
streamPromise(someReadableStream); | ||
someReadableStream.then(result => { | ||
console.log("Concatenated stream output", result); | ||
}); | ||
streamPromise.addListener("someevent", event => { | ||
... | ||
streamPromise(someWritabletream); | ||
someReadableStream.then(result => { | ||
console.log("Cumulated stream output", result); | ||
}); | ||
streamPromise.emit("someevent", { ... }); | ||
``` | ||
stream.pipe(otherStream); | ||
## Non-destructive way | ||
Sepearate promise (without touching stream object) can be created with `to-promise` util: | ||
```javascript | ||
const streamToPromise = require("stream-promise/to-promise"); | ||
const someReadableStreamPromise = streamPromiseTo(someReadableStream); | ||
someReadableStreamPromise.then(result => { | ||
console.log("Concatenated stream output", result); | ||
}); | ||
``` | ||
@@ -36,0 +58,0 @@ |
"use strict"; | ||
const { assert } = require("chai") | ||
, Stream = require("stream") | ||
, StreamEmitter = require("../"); | ||
const isThenable = require("es5-ext/object/is-thenable") | ||
, { assert } = require("chai") | ||
, { Readable } = require("stream") | ||
, streamPromise = require("../"); | ||
describe("(main)", () => { | ||
it( | ||
"Should create promise", | ||
() => | ||
new StreamEmitter(resolve => resolve("foo").then(result => assert.equal(result, "foo"))) | ||
); | ||
it("Should create an emitter", () => { | ||
const streamEmitter = new StreamEmitter(resolve => resolve("result")); | ||
let eventData; | ||
streamEmitter.addListener("elo", event => { eventData = event; }); | ||
return streamEmitter.then(result => { | ||
assert.equal(result, "result"); | ||
streamEmitter.emit("elo", "bar"); | ||
assert.equal(eventData, "bar"); | ||
}); | ||
it("Should convert stream to promise", () => { | ||
const stream = Object.assign(new Readable(), { _read() { this.push(null); } }); | ||
const result = streamPromise(stream); | ||
assert.equal(isThenable(result), true); | ||
assert.equal(result, stream); | ||
return result; | ||
}); | ||
it("Should create a stream", () => { | ||
const streamEmitter = new StreamEmitter(resolve => resolve("result")); | ||
assert.equal(streamEmitter.pipe, Stream.prototype.pipe); | ||
}); | ||
}); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
9478
92.37%9
28.57%133
269.44%71
44.9%0
-100%3
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added