mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-10-03 20:20:35 +00:00
Add method readAsStream on fs model
This commit is contained in:
+28
-1
@@ -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
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -111,6 +114,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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user