Synchronous file reading

Reviewed By: matryoshcow

Differential Revision: D4021460

fbshipit-source-id: 88e4846d4434468d68e6071d05b27e3b7d2ed325
This commit is contained in:
Christoph Pojer 2016-10-16 18:32:08 -07:00 committed by Facebook Github Bot
parent f53d694120
commit bfc35d528c
1 changed files with 14 additions and 6 deletions

View File

@ -8,15 +8,11 @@
*/ */
'use strict'; 'use strict';
const denodeify = require('denodeify');
const fs = require('fs'); const fs = require('fs');
const path = require('./fastpath'); const path = require('./fastpath');
const {EventEmitter} = require('events'); const {EventEmitter} = require('events');
const readFile = denodeify(fs.readFile);
const stat = denodeify(fs.stat);
const NOT_FOUND_IN_ROOTS = 'NotFoundInRootsError'; const NOT_FOUND_IN_ROOTS = 'NotFoundInRootsError';
class Fastfs extends EventEmitter { class Fastfs extends EventEmitter {
@ -222,14 +218,26 @@ class File {
read() { read() {
if (!this._read) { if (!this._read) {
this._read = readFile(this.path, 'utf8'); this._read = new Promise((resolve, reject) => {
try {
resolve(fs.readFileSync(this.path, 'utf8'));
} catch (e) {
reject(e);
}
});
} }
return this._read; return this._read;
} }
stat() { stat() {
if (!this._stat) { if (!this._stat) {
this._stat = stat(this.path); this._stat = new Promise((resolve, reject) => {
try {
resolve(fs.statSync(this.path));
} catch (e) {
reject(e);
}
});
} }
return this._stat; return this._stat;