pax_global_header00006660000000000000000000000064126446053560014525gustar00rootroot0000000000000052 comment=d44d4fcae1b3ed2ee5f22cf7ad2ead6f665949ae read-cache-1.0.0/000077500000000000000000000000001264460535600134775ustar00rootroot00000000000000read-cache-1.0.0/.gitignore000066400000000000000000000000251264460535600154640ustar00rootroot00000000000000node_modules fixture read-cache-1.0.0/.travis.yml000066400000000000000000000001021264460535600156010ustar00rootroot00000000000000sudo: false language: node_js node_js: - "5" - "4" - "0.12" read-cache-1.0.0/LICENSE000066400000000000000000000021121264460535600145000ustar00rootroot00000000000000The MIT License (MIT) Copyright 2016 Bogdan Chadkin Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. read-cache-1.0.0/README.md000066400000000000000000000014031264460535600147540ustar00rootroot00000000000000# read-cache [![Build Status](https://travis-ci.org/TrySound/read-cache.svg?branch=master)](https://travis-ci.org/TrySound/read-cache) Reads and caches the entire contents of a file until it is modified. ## Install ``` $ npm i read-cache ``` ## Usage ```js // foo.js var readCache = require('read-cache'); readCache('foo.js').then(function (contents) { console.log(contents); }); ``` ## API ### readCache(path[, encoding]) Returns a promise that resolves with the file's contents. ### readCache.sync(path[, encoding]) Returns the content of the file. ### readCache.get(path[, encoding]) Returns the content of cached file or null. ### readCache.clear() Clears the contents of the cache. ## License MIT © [Bogdan Chadkin](mailto:trysound@yandex.ru) read-cache-1.0.0/index.js000066400000000000000000000027511264460535600151510ustar00rootroot00000000000000var 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); }; read-cache-1.0.0/package.json000066400000000000000000000013101264460535600157600ustar00rootroot00000000000000{ "name": "read-cache", "version": "1.0.0", "description": "Reads and caches the entire contents of a file until it is modified", "files": [ "index.js" ], "main": "index.js", "scripts": { "test": "ava" }, "repository": { "type": "git", "url": "git+https://github.com/TrySound/read-cache.git" }, "keywords": [ "fs", "read", "cache" ], "author": "Bogdan Chadkin ", "license": "MIT", "bugs": { "url": "https://github.com/TrySound/read-cache/issues" }, "homepage": "https://github.com/TrySound/read-cache#readme", "devDependencies": { "ava": "^0.9.1", "del": "^2.2.0" }, "dependencies": { "pify": "^2.3.0" } } read-cache-1.0.0/test.js000066400000000000000000000055031264460535600150170ustar00rootroot00000000000000import test from 'ava'; import fs from 'fs'; import del from 'del'; import pify from 'pify'; import readCache from './'; const readFile = pify(fs.readFile); const writeFile = pify(fs.writeFile); test.serial('async', t => { readCache.clear(); t.is(readCache.get('fixture'), null); return writeFile('fixture', 'data').then(() => { return Promise.all([ readCache('fixture'), readCache('node_modules/../fixture') ]); }).then(contents => { t.ok(contents[0] instanceof Buffer); t.ok(contents[1] instanceof Buffer); t.is(contents[0].toString(), 'data'); t.is(contents[1].toString(), 'data'); return writeFile('fixture', 'changed data'); }).then(() => { return Promise.all([ readCache('fixture', 'utf-8'), readCache('node_modules/../fixture', 'utf-8') ]); }).then(contents => { t.is(contents[0], 'changed data'); t.is(contents[1], 'changed data'); return del('fixture'); }).then(() => { return readCache('fixture'); }).then(() => { t.fail(`should reject an error if 'fixture' doesn't exist`); }).catch(() => { t.is(readCache.get('fixture'), null); }); }); test.serial('sync', t => { readCache.clear(); t.is(readCache.get('fixture'), null); fs.writeFileSync('fixture', 'data') t.is(readCache.sync('fixture', 'utf-8'), 'data'); t.is(readCache.sync('node_modules/../fixture', 'utf-8'), 'data'); return new Promise(function (resolve) { setTimeout(function () { fs.writeFileSync('fixture', 'changed data') var contents = [ readCache.sync('fixture'), readCache.sync('node_modules/../fixture') ]; t.ok(contents[0] instanceof Buffer); t.ok(contents[1] instanceof Buffer); t.is(contents[0].toString(), 'changed data'); t.is(contents[1].toString(), 'changed data'); del.sync('fixture'); try { readCache.sync('fixture'); t.fail(`should throw an error if 'fixture' doesn't exist`); } catch (e) { t.is(readCache.get('fixture'), null); } resolve(); }, 1000); }); }); test.serial('get', t => { return writeFile('fixture', 'data').then(() => { return readCache('fixture', 'utf-8'); }).then(() => { var contents = [ readCache.get('node_modules/../fixture'), readCache.get('fixture', 'utf-8') ]; t.ok(contents[0] instanceof Buffer); t.is(contents[0].toString(), 'data'); t.is(contents[1], 'data'); return del('fixture'); }).then(() => { var contents = [ readCache.get('node_modules/../fixture', 'utf-8'), readCache.get('fixture') ]; t.is(contents[0], 'data'); t.ok(contents[1] instanceof Buffer); t.is(contents[1].toString(), 'data'); }); }); test.serial('clear', t => { return writeFile('fixture', 'data').then(() => { return readCache('fixture', 'utf-8'); }).then(content => { t.is(content, 'data'); t.is(readCache.get('fixture', 'utf-8'), 'data'); readCache.clear(); t.is(readCache.get('fixture', 'utf-8'), null); }); });