Skip to content

Commit

Permalink
Breaking: Use cloneable-readable to clone streams (closes #85) (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Sep 27, 2016
1 parent 40fd58c commit 74242a8
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 7 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ var isStream = require('./lib/isStream');
var isNull = require('./lib/isNull');
var inspectStream = require('./lib/inspectStream');
var normalize = require('./lib/normalize');
var Stream = require('readable-stream');
var replaceExt = require('replace-ext');
var cloneable = require('cloneable-readable');

var builtInFields = [
'_contents', '_symlink', 'contents', 'stat', 'history', 'path',
Expand Down Expand Up @@ -111,8 +111,11 @@ File.prototype.clone = function(opt) {
// Clone our file contents
var contents;
if (this.isStream()) {
contents = this.contents.pipe(new Stream.PassThrough());
this.contents = this.contents.pipe(new Stream.PassThrough());
if (typeof this.contents.clone !== 'function') {
this.contents = cloneable(this.contents);
}

contents = this.contents.clone();
} else if (this.isBuffer()) {
contents = opt.contents ? cloneBuffer(this.contents) : this.contents;
}
Expand Down Expand Up @@ -173,6 +176,11 @@ Object.defineProperty(File.prototype, 'contents', {
if (!isBuffer(val) && !isStream(val) && !isNull(val)) {
throw new Error('File.contents can only be a Buffer, a Stream, or null.');
}

if (isStream(val)) {
val = cloneable(val);
}

this._contents = val;
},
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"clone": "^1.0.0",
"clone-stats": "^1.0.0",
"cloneable-readable": "^0.4.0",
"readable-stream": "^2.1.0",
"replace-ext": "^1.0.0"
},
Expand Down
92 changes: 88 additions & 4 deletions test/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,90 @@ describe('File', function() {
done();
});

it('should not start flowing until all clones flows', function(done) {
var contents = new Stream.PassThrough();
var options = {
cwd: '/',
base: '/test/',
path: '/test/test.coffee',
contents: contents,
};
var file = new File(options);
var file2 = file.clone();
var ends = 2;

function latch() {
if (--ends === 0) {
done();
}
}

contents.write(new Buffer('wa'));

process.nextTick(function() {
contents.write(new Buffer('dup'));
contents.end();
});

// Start flowing file2
file2.contents.on('data', function() {});

file2.contents.once('data', function() {
process.nextTick(function() {
// Starts flowing file
file.contents.on('data', function() {
ends.should.equal(2);
});
});
});

file2.contents.on('end', latch);
file.contents.on('end', latch);
});

it('should not start flowing until all clones flows', function(done) {
var contents = new Stream.PassThrough();
var options = {
cwd: '/',
base: '/test/',
path: '/test/test.coffee',
contents: contents,
};
var file = new File(options);
var file2 = file.clone();
var ends = 2;

function latch() {
if (--ends === 0) {
done();
}
}

contents.write(new Buffer('wa'));

process.nextTick(function() {
contents.write(new Buffer('dup'));
contents.end();
});

// Start flowing file2
file2.contents.on('readable', function() {
this.read();
});

file2.contents.once('readable', function() {
process.nextTick(function() {
// Starts flowing file
file.contents.on('readable', function() {
ends.should.equal(2);
});
});
});

file2.contents.on('end', latch);
file.contents.on('end', latch);
});

it('should copy all attributes over with null', function(done) {
var options = {
cwd: '/',
Expand Down Expand Up @@ -636,7 +720,7 @@ describe('File', function() {
path: '/test/test.coffee',
contents: new Stream.PassThrough(),
});
file.inspect().should.equal('<File "test.coffee" <PassThroughStream>>');
file.inspect().should.equal('<File "test.coffee" <CloneableStream>>');
done();
});

Expand All @@ -661,11 +745,11 @@ describe('File', function() {
done();
});

it('should work with Stream', function(done) {
var val = new Stream.PassThrough();
it('should wrap Stream in Cloneable', function(done) {
var val = new Stream();
var file = new File();
file.contents = val;
file.contents.should.equal(val);
(typeof file.contents.clone).should.equal('function');
done();
});

Expand Down

0 comments on commit 74242a8

Please sign in to comment.