Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit dfc75be

Browse files
committed
Merge branch 'stream'
Conflicts: lib/MemoryFileSystem.js package.json
2 parents da7fb39 + c9a4674 commit dfc75be

File tree

3 files changed

+136
-3
lines changed

3 files changed

+136
-3
lines changed

lib/MemoryFileSystem.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
var normalize = require("./normalize");
77
var errors = require("errno");
8+
var stream = require("readable-stream");
9+
10+
var ReadableStream = stream.Readable;
11+
var WritableStream = stream.Writable;
812

913
function MemoryFileSystemError(err, path) {
1014
Error.call(this)
@@ -213,6 +217,60 @@ MemoryFileSystem.prototype.join = require("./join");
213217

214218
MemoryFileSystem.prototype.normalize = normalize;
215219

220+
// stream functions
221+
222+
MemoryFileSystem.prototype.createReadStream = function(path, options) {
223+
var stream = new ReadableStream();
224+
var done = false;
225+
var data;
226+
try {
227+
data = this.readFileSync(path);
228+
} catch (e) {
229+
stream._read = function() {
230+
if (done) {
231+
return;
232+
}
233+
done = true;
234+
this.emit('error', e);
235+
this.push(null);
236+
};
237+
return stream;
238+
}
239+
options = options || { };
240+
options.start = options.start || 0;
241+
options.end = options.end || data.length;
242+
stream._read = function() {
243+
if (done) {
244+
return;
245+
}
246+
done = true;
247+
this.push(data.slice(options.start, options.end));
248+
this.push(null);
249+
};
250+
return stream;
251+
};
252+
253+
MemoryFileSystem.prototype.createWriteStream = function(path, options) {
254+
var stream = new WritableStream(), self = this;
255+
try {
256+
// Zero the file and make sure it is writable
257+
this.writeFileSync(path, new Buffer(0));
258+
} catch(e) {
259+
// This or setImmediate?
260+
stream.once('prefinish', function() {
261+
stream.emit('error', e);
262+
});
263+
return stream;
264+
}
265+
var bl = [ ], len = 0;
266+
stream._write = function(chunk, encoding, callback) {
267+
bl.push(chunk);
268+
len += chunk.length;
269+
self.writeFile(path, Buffer.concat(bl, len), callback);
270+
}
271+
return stream;
272+
};
273+
216274
// async functions
217275

218276
["stat", "readdir", "mkdirp", "mkdir", "rmdir", "unlink", "readlink"].forEach(function(fn) {

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@
2525
"url": "https://github.com/webpack/memory-fs/issues"
2626
},
2727
"homepage": "https://github.com/webpack/memory-fs",
28-
"dependencies": {
29-
"errno": "^0.1.3"
30-
},
3128
"devDependencies": {
29+
"bl": "^1.0.0",
3230
"codecov.io": "^0.1.4",
3331
"coveralls": "^2.11.2",
3432
"istanbul": "^0.2.13",
3533
"mocha": "^1.20.1",
3634
"should": "^4.0.4"
35+
},
36+
"dependencies": {
37+
"errno": "^0.1.3",
38+
"readable-stream": "^2.0.1"
3739
}
3840
}

test/MemoryFileSystem.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var bl = require("bl");
12
var should = require("should");
23
var MemoryFileSystem = require("../lib/MemoryFileSystem");
34

@@ -231,6 +232,78 @@ describe("async", function() {
231232
});
232233
});
233234
});
235+
describe("streams", function() {
236+
describe("writable streams", function() {
237+
it("should write files", function() {
238+
var fs = new MemoryFileSystem();
239+
fs.createWriteStream("/file").end("Hello");
240+
fs.readFileSync("/file", "utf8").should.be.eql("Hello");
241+
});
242+
it("should zero files", function() {
243+
var fs = new MemoryFileSystem();
244+
fs.createWriteStream("/file").end();
245+
fs.readFileSync("/file", "utf8").should.be.eql("");
246+
});
247+
it("should accept pipes", function(done) {
248+
// TODO: Any way to avoid the asyncness of this?
249+
var fs = new MemoryFileSystem();
250+
bl(new Buffer("Hello"))
251+
.pipe(fs.createWriteStream("/file"))
252+
.once('finish', function() {
253+
fs.readFileSync("/file", "utf8").should.be.eql("Hello");
254+
done();
255+
});
256+
});
257+
it("should propagate errors", function(done) {
258+
var fs = new MemoryFileSystem();
259+
var stream = fs.createWriteStream("file");
260+
var err = false;
261+
stream.once('error', function() {
262+
err = true;
263+
}).once('finish', function() {
264+
err.should.eql(true);
265+
done();
266+
});
267+
stream.end();
268+
});
269+
});
270+
describe("readable streams", function() {
271+
it("should read files", function(done) {
272+
var fs = new MemoryFileSystem();
273+
fs.writeFileSync("/file", "Hello");
274+
fs.createReadStream("/file").pipe(bl(function(err, data) {
275+
data.toString('utf8').should.be.eql("Hello");
276+
done();
277+
}));
278+
});
279+
it("should respect start/end", function(done) {
280+
var fs = new MemoryFileSystem();
281+
fs.writeFileSync("/file", "Hello");
282+
fs.createReadStream("/file", {
283+
start: 1,
284+
end: 3
285+
}).pipe(bl(function(err, data) {
286+
data.toString('utf8').should.be.eql("el");
287+
done();
288+
}));
289+
});
290+
it("should propagate errors", function(done) {
291+
var fs = new MemoryFileSystem();
292+
var stream = fs.createReadStream("file");
293+
var err = false;
294+
// Why does this dummy event need to be here? It looks like it
295+
// either has to be this or data before the stream will actually
296+
// do anything.
297+
stream.on('readable', function() { }).on('error', function() {
298+
err = true;
299+
}).on('end', function() {
300+
err.should.eql(true);
301+
done();
302+
});
303+
stream.read(0);
304+
});
305+
});
306+
});
234307
describe("normalize", function() {
235308
it("should normalize paths", function() {
236309
var fs = new MemoryFileSystem();

0 commit comments

Comments
 (0)