Skip to content

Commit e6a0bfe

Browse files
committed
Breaking: Use cloneable-readable to clone streams - possibly breaking (closes #85)
1 parent d832c6e commit e6a0bfe

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

index.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ var isStream = require('./lib/isStream');
88
var isNull = require('./lib/isNull');
99
var inspectStream = require('./lib/inspectStream');
1010
var normalize = require('./lib/normalize');
11-
var Stream = require('readable-stream');
1211
var replaceExt = require('replace-ext');
12+
var cloneable = require('cloneable-readable');
1313

1414
var builtInFields = [
1515
'_contents', '_symlink', 'contents', 'stat', 'history', 'path',
@@ -111,8 +111,11 @@ File.prototype.clone = function(opt) {
111111
// Clone our file contents
112112
var contents;
113113
if (this.isStream()) {
114-
contents = this.contents.pipe(new Stream.PassThrough());
115-
this.contents = this.contents.pipe(new Stream.PassThrough());
114+
if (typeof this.contents.clone !== 'function') {
115+
this.contents = cloneable(this.contents);
116+
}
117+
118+
contents = this.contents.clone();
116119
} else if (this.isBuffer()) {
117120
contents = opt.contents ? cloneBuffer(this.contents) : this.contents;
118121
}
@@ -173,6 +176,11 @@ Object.defineProperty(File.prototype, 'contents', {
173176
if (!isBuffer(val) && !isStream(val) && !isNull(val)) {
174177
throw new Error('File.contents can only be a Buffer, a Stream, or null.');
175178
}
179+
180+
if (isStream(val)) {
181+
val = cloneable(val);
182+
}
183+
176184
this._contents = val;
177185
},
178186
});

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"dependencies": {
1414
"clone": "^1.0.0",
1515
"clone-stats": "^1.0.0",
16+
"cloneable-readable": "^0.1.0",
1617
"readable-stream": "^2.1.0",
1718
"replace-ext": "^1.0.0"
1819
},

test/File.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ describe('File', function() {
636636
path: '/test/test.coffee',
637637
contents: new Stream.PassThrough(),
638638
});
639-
file.inspect().should.equal('<File "test.coffee" <PassThroughStream>>');
639+
file.inspect().should.equal('<File "test.coffee" <CloneableStream>>');
640640
done();
641641
});
642642

@@ -661,11 +661,11 @@ describe('File', function() {
661661
done();
662662
});
663663

664-
it('should work with Stream', function(done) {
665-
var val = new Stream.PassThrough();
664+
it('should wrap Stream in Cloneable', function(done) {
665+
var val = new Stream();
666666
var file = new File();
667667
file.contents = val;
668-
file.contents.should.equal(val);
668+
(typeof file.contents.clone).should.equal('function');
669669
done();
670670
});
671671

0 commit comments

Comments
 (0)