Skip to content

Commit

Permalink
Fix: Add option to end stream produced by .pipe, defaulted to true (f…
Browse files Browse the repository at this point in the history
…ixes #2)
  • Loading branch information
yocontra authored and phated committed Sep 27, 2016
1 parent 71e5ead commit b8099c4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ Returns true if file.contents is null.

Returns a new File object with all attributes cloned.

### pipe(stream)
### pipe(stream[, opt])

If file.contents is a Buffer, it will write it to the stream.

If file.contents is a Stream, it will pipe it to the stream.

If file.contents is null, it will do nothing.

If opt.end is true, the destination stream will not be ended (same as node core).

Returns the stream.

### inspect()
Expand Down
18 changes: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,26 @@ File.prototype.clone = function() {
});
};

File.prototype.pipe = function(stream) {
File.prototype.pipe = function(stream, opt) {
if (!opt) opt = {};
if (typeof opt.end === 'undefined') opt.end = true;

if (this.isStream()) {
return this.contents.pipe(stream);
return this.contents.pipe(stream, opt);
}
if (this.isBuffer()) {
stream.write(this.contents);
if (opt.end) {
stream.end(this.contents);
} else {
stream.write(this.contents);
}
return stream;
}
if (this.isNull()) {
if (opt.end) stream.end();
return stream;
}

// must be null, dont do anything
return stream;
};

Expand Down
71 changes: 71 additions & 0 deletions test/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ describe('File', function() {
should.exist(chunk);
(chunk instanceof Buffer).should.equal(true, 'should write as a buffer');
chunk.toString('utf8').should.equal(options.contents.toString('utf8'));
});
stream.on('end', function(chunk) {
done();
});
var ret = file.pipe(stream);
Expand Down Expand Up @@ -285,8 +287,77 @@ describe('File', function() {
stream.on('data', function(chunk) {
throw new Error("should not write");
});
stream.on('end', function() {
done();
});
var ret = file.pipe(stream);
ret.should.equal(stream, 'should return the stream');
});

it('should write to stream with Buffer', function(done) {
var options = {
cwd: "/",
base: "/test/",
path: "/test/test.coffee",
contents: new Buffer("test")
};
var file = new File(options);
var stream = new Stream.PassThrough();
stream.on('data', function(chunk) {
should.exist(chunk);
(chunk instanceof Buffer).should.equal(true, 'should write as a buffer');
chunk.toString('utf8').should.equal(options.contents.toString('utf8'));
done();
});
stream.on('end', function(chunk) {
throw new Error("should not end");
});
var ret = file.pipe(stream, {end: false});
ret.should.equal(stream, 'should return the stream');
});

it('should pipe to stream with Stream', function(done) {
var testChunk = new Buffer("test");
var options = {
cwd: "/",
base: "/test/",
path: "/test/test.coffee",
contents: new Stream.PassThrough()
};
var file = new File(options);
var stream = new Stream.PassThrough();
stream.on('data', function(chunk) {
should.exist(chunk);
(chunk instanceof Buffer).should.equal(true, 'should write as a buffer');
chunk.toString('utf8').should.equal(testChunk.toString('utf8'));
done();
});
stream.on('end', function(chunk) {
throw new Error("should not end");
});
var ret = file.pipe(stream, {end: false});
ret.should.equal(stream, 'should return the stream');

file.contents.write(testChunk);
});

it('should do nothing with null', function(done) {
var options = {
cwd: "/",
base: "/test/",
path: "/test/test.coffee",
contents: null
};
var file = new File(options);
var stream = new Stream.PassThrough();
stream.on('data', function(chunk) {
throw new Error("should not write");
});
stream.on('end', function(chunk) {
throw new Error("should not end");
});
var ret = file.pipe(stream, {end: false});
ret.should.equal(stream, 'should return the stream');
process.nextTick(done);
});
});
Expand Down

0 comments on commit b8099c4

Please sign in to comment.