Skip to content

Commit cb84e1f

Browse files
committed
add a createWriteStream method and expose the stream constructors on the FQ prototype
1 parent b590e5b commit cb84e1f

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

lib/methods.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// All of the fs methods we want exposed on fq
22
// This should pass the Grep Test (http://jamie-wong.com/2013/07/12/grep-test/)
3-
4-
var FQReadStream = require('./stream');
3+
4+
var stream = require('./stream');
55

66
module.exports = function (FQ) {
77

@@ -165,9 +165,11 @@ module.exports = function (FQ) {
165165
this.execQueue();
166166
};
167167

168+
FQ.prototype.ReadStream = stream.ReadStream;
169+
168170
FQ.prototype.createReadStream = function(path, options) {
169171

170-
var readStream = new FQReadStream(path, options);
172+
var readStream = new this.ReadStream(path, options);
171173

172174
this.addToQueue(function(fs, cb) {
173175

@@ -181,4 +183,22 @@ module.exports = function (FQ) {
181183
return readStream;
182184
};
183185

186+
FQ.prototype.WriteStream = stream.WriteStream;
187+
188+
FQ.prototype.createWriteStream = function(path, options) {
189+
190+
var writeStream = new this.WriteStream(path, options);
191+
192+
this.addToQueue(function(fs, cb) {
193+
194+
writeStream.on('close', cb);
195+
196+
writeStream.open();
197+
});
198+
199+
this.execQueue();
200+
201+
return writeStream;
202+
};
203+
184204
};

lib/stream.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
var fs = require('fs');
22
var util = require('util');
33

4+
var fq = exports;
5+
46
util.inherits(FQReadStream, fs.ReadStream);
7+
fq.ReadStream = FQReadStream;
58

69
function FQReadStream(path, options) {
710

@@ -16,4 +19,18 @@ function FQReadStream(path, options) {
1619
return this;
1720
}
1821

19-
module.exports = FQReadStream;
22+
util.inherits(FQWriteStream, fs.WriteStream);
23+
fq.WriteStream = FQWriteStream;
24+
25+
function FQWriteStream(path, options) {
26+
27+
// temporarily change our #open method so the ReadStream initializer doesn't open the file
28+
var open = this.open;
29+
this.open = function() {};
30+
31+
fs.WriteStream.call(this, path, options);
32+
33+
this.open = open;
34+
35+
return this;
36+
}

0 commit comments

Comments
 (0)