Sha256: aff1e97de8765d84efde24e78b3912f99d65f5cb63bba2dbb25a71902feb5007

Contents?: true

Size: 1.55 KB

Versions: 30

Compression:

Stored size: 1.55 KB

Contents

var fs = require('fs');
var path = require('path');
var pify = require('pify');

var stat = pify(fs.stat);
var readFile = pify(fs.readFile);
var resolve = path.resolve;

var cache = Object.create(null);

function convert(content, encoding) {
	if (Buffer.isEncoding(encoding)) {
		return content.toString(encoding);
	}
	return content;
}

module.exports = function (path, encoding) {
	path = resolve(path);

	return stat(path).then(function (stats) {
		var item = cache[path];

		if (item && item.mtime.getTime() === stats.mtime.getTime()) {
			return convert(item.content, encoding);
		}

		return readFile(path).then(function (data) {
			cache[path] = {
				mtime: stats.mtime,
				content: data
			};

			return convert(data, encoding);
		});
	}).catch(function (err) {
		cache[path] = null;
		return Promise.reject(err);
	});
};

module.exports.sync = function (path, encoding) {
	path = resolve(path);

	try {
		var stats = fs.statSync(path);
		var item = cache[path];

		if (item && item.mtime.getTime() === stats.mtime.getTime()) {
			return convert(item.content, encoding);
		}

		var data = fs.readFileSync(path);

		cache[path] = {
			mtime: stats.mtime,
			content: data
		};

		return convert(data, encoding);
	} catch (err) {
		cache[path] = null;
		throw err;
	}

};

module.exports.get = function (path, encoding) {
	path = resolve(path);
	if (cache[path]) {
		return convert(cache[path].content, encoding);
	}
	return null;
};

module.exports.clear = function () {
	cache = Object.create(null);
};

Version data entries

30 entries across 29 versions & 10 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/read-cache/index.js
decidim-0.26.8 packages/eslint-config/node_modules/read-cache/index.js
disco_app-0.18.0 test/dummy/node_modules/read-cache/index.js
disco_app-0.18.2 test/dummy/node_modules/read-cache/index.js
disco_app-0.16.1 test/dummy/node_modules/read-cache/index.js
disco_app-0.15.2 test/dummy/node_modules/read-cache/index.js
disco_app-0.18.4 test/dummy/node_modules/read-cache/index.js
disco_app-0.18.1 test/dummy/node_modules/read-cache/index.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/read-cache/index.js
disco_app-0.14.0 test/dummy/node_modules/read-cache/index.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/read-cache/index.js
tang-0.2.1 spec/tang_app/node_modules/read-cache/index.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/read-cache/index.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/read-cache/index.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/read-cache/index.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/read-cache/index.js
tang-0.2.0 spec/tang_app/node_modules/read-cache/index.js
tang-0.1.0 spec/tang_app/node_modules/read-cache/index.js
tang-0.0.9 spec/tang_app/node_modules/read-cache/index.js
enju_library-0.3.8 spec/dummy/node_modules/read-cache/index.js