UNPKG

read-file-cache

Version:
58 lines (49 loc) 1.25 kB
"use strict"; var read = require("read-utf8"), abs = require("abs"); var _cache = {}; /** * readFileCache * Reads the file asyncronously. * * @name readFileCache * @function * @param {String} path The file path. * @param {Boolean} noCache If `true`, the file will be read from the disk. * @param {Function} cb The callback function. */ function readFileCache(path, noCache, cb) { path = abs(path); if (typeof noCache === "function") { cb = noCache; noCache = false; } // TODO: Callback buffering if (_cache[path] && noCache !== true) { return cb(null, _cache[path]); } read(path, function (err, data) { if (err) { return cb(err); } cb(null, _cache[path] = data); }); } readFileCache._cache = _cache; /** * sync * The syncronous version of the function. * * @name sync * @function * @param {String} path The file path. * @param {Boolean} noCache If `true`, the file will be read from the disk. */ readFileCache.sync = function (path, noCache) { path = abs(path); if (noCache) { return _cache[path] = read(path); } return _cache[path] || (_cache[path] = read(path)); }; module.exports = readFileCache;