Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

stream-collect

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stream-collect - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

24

index.js

@@ -105,10 +105,32 @@ /* jshint node: true */

* Returns a PassThrough stream augmented with collect
* The PassThrough stream is also a thenable and can be used as a promise
* @param {Object} [options]
*/
function PassThrough(options) {
return addToStream( new stream.PassThrough(options) );
stream.PassThrough.call( this, options );
addToStream(this);
this._resolved = null;
}
util.inherits( PassThrough, stream.PassThrough );
PassThrough.prototype.then = function( resolve, reject ) {
if ( !this._resolved ) {
this._resolved = promiseUtil.defer();
this
.on( 'collect', this._resolved.resolve )
.on( 'error', this._resolved.reject );
}
return this._resolved.then( resolve, reject );
};
PassThrough.prototype.catch = function( reject ) {
return this.then( null, reject );
};
module.exports = collect;
collect.addToStream = addToStream;
collect.PassThrough = PassThrough;

8

package.json
{
"name": "stream-collect",
"version": "1.0.2",
"description": "Collects the output of a stream",
"version": "1.1.0",
"description": "Collects the output of a stream as callback or promise",
"main": "index.js",

@@ -14,4 +14,4 @@ "scripts": {

"devDependencies": {
"mocha": "^2.1.0",
"expect": "^1.4.0"
"mocha": "*",
"expect": "*"
},

@@ -18,0 +18,0 @@ "author": "Daniel Lewis",

@@ -46,5 +46,11 @@ # Stream collect

A PassThrough stream that has been augmented with a collect event. This will be emitted in the `end` event with the collected contents of the stream.
Creates a PassThrough stream that that has an additional 'collect' and acts as a Promise.
The `collect` event returns the collected data. It is called as part of the `end` event.
The PassThrough stream has `then` and `catch` methods added to acts like a Promise (or strictly a "thenable").
```js
// Using the collect event
var collect = require('stream-collect');

@@ -57,2 +63,10 @@

} );
// As a Promise
var file = fs.createReadableStream( 'myfile' );
file.pipe( new collect.PassThrough() )
.then(data) {
// data = contents of the file
} );
```

@@ -62,4 +76,4 @@

Augment any stream with the collect event used on `collect.PassThrough`.
Augment any stream with a `collect` event.
Returns the augmented stream.

@@ -112,4 +112,111 @@ /* jshint node: true, mocha: true */

describe( 'as a thenable', function() {
it( 'resolves as promise using the then method', function() {
var stream = new collect.PassThrough();
stream.write(testDataPart1);
stream.end(testDataPart2);
return stream.then( function(data) {
expect( Buffer.isBuffer(data) ).toBe(true);
expect( data.toString() ).toBe( completeData );
} );
} );
it( 'allows then to be chained like a promise', function() {
var stream = new collect.PassThrough();
stream.write(testDataPart1);
stream.end(testDataPart2);
return stream
.then( function(data) {
return 'some completly different data';
} )
.then( function(data) {
expect( data ).toBe('some completly different data');
} );
} );
it( 'returns the same data for subsequent calls to then', function() {
var stream = new collect.PassThrough();
stream.write(testDataPart1);
stream.end(testDataPart2);
return stream
.then( function(data) {
return stream.then();
} )
.then( function(data) {
expect( Buffer.isBuffer(data) ).toBe(true);
expect( data.toString() ).toBe( completeData );
} );
} );
it( 'rejects as a promise using the then method', function() {
var stream = new collect.PassThrough();
stream.write(testDataPart1);
var promise = stream
.then( function(data) {
throw new Error( 'Should not have been called' );
}, function(e) {
expect(e).toBe(error);
} );
// Need to create the error after the promise is created
var error = new Error('foo');
stream.emit('error', error);
return promise;
} );
it( 'rejects as a promise using the catch method', function() {
var stream = new collect.PassThrough();
stream.write(testDataPart1);
var promise = stream
.then( function(data) {
throw new Error( 'Should not have been called' );
} )
.catch( function(e) {
expect(e).toBe(error);
} );
// Need to create the error after the promise is created
var error = new Error('foo');
stream.emit('error', error);
return promise;
} );
/*it( 'subsequent calls to then return the same data', function() {
var stream = new collect.PassThrough();
stream.write(testDataPart1);
stream.end(testDataPart2);
return stream.then( function(data) {
return stream.then();
} )
} );*/
} );
} );

@@ -116,0 +223,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc