diff options
author | Samy Pesse <samypesse@gmail.com> | 2016-05-06 10:53:55 +0200 |
---|---|---|
committer | Samy Pesse <samypesse@gmail.com> | 2016-05-06 10:53:55 +0200 |
commit | cf7e5884cd3908f2200fac3684ebebdb5cbeb577 (patch) | |
tree | bbde916802432d7820fc6c3d1ad047ace653c5b4 /lib/models | |
parent | 11a0a64d9e6bc357d85efe9d83183a214aa7f1dc (diff) | |
download | gitbook-cf7e5884cd3908f2200fac3684ebebdb5cbeb577.zip gitbook-cf7e5884cd3908f2200fac3684ebebdb5cbeb577.tar.gz gitbook-cf7e5884cd3908f2200fac3684ebebdb5cbeb577.tar.bz2 |
Add method readAsStream on fs model
Diffstat (limited to 'lib/models')
-rw-r--r-- | lib/models/fs.js | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/lib/models/fs.js b/lib/models/fs.js index ab65dd5..12aee53 100644 --- a/lib/models/fs.js +++ b/lib/models/fs.js @@ -1,5 +1,6 @@ var path = require('path'); var Immutable = require('immutable'); +var stream = require('stream'); var File = require('./file'); var Promise = require('../utils/promise'); @@ -13,7 +14,9 @@ var FS = Immutable.Record({ fsReadFile: Function(), fsStatFile: Function(), fsReadDir: Function(), - fsLoadObject: null + + fsLoadObject: null, + fsReadAsStream: null }); /** @@ -112,6 +115,30 @@ FS.prototype.readAsString = function(filename, encoding) { }; /** + Read file as a stream + + @param {String} filename + @return {Promise<Stream>} +*/ +FS.prototype.readAsStream = function(filename) { + var that = this; + var filepath = that.resolve(filename); + var fsReadAsStream = this.get('fsReadAsStream'); + + if (fsReadAsStream) { + return Promise(fsReadAsStream(filepath)); + } + + return this.read(filename) + .then(function(buf) { + var bufferStream = new stream.PassThrough(); + bufferStream.end(buf); + + return bufferStream; + }); +}; + +/** Read stat infos about a file @param {String} filename |