cache-point
Advanced tools
+68
-4
@@ -7,5 +7,5 @@ 'use strict'; | ||
| var fs = require('then-fs'); | ||
| var path = require('path'); | ||
| var arrayify = require('array-back'); | ||
| var fs = require('fs'); | ||
@@ -17,2 +17,6 @@ var Cache = function () { | ||
| options = options || {}; | ||
| if (!options.cacheDir) { | ||
| var os = require('os'); | ||
| options.cacheDir = path.resolve(os.tmpdir(), 'cachePoint'); | ||
| } | ||
| this.cacheDir = options.cacheDir; | ||
@@ -28,3 +32,12 @@ try { | ||
| var blobPath = path.resolve(this.cacheDir, this.getChecksum(keys)); | ||
| return fs.readFile(blobPath).then(JSON.parse); | ||
| var promise = new Promise(function (resolve, reject) { | ||
| fs.readFile(blobPath, function (err, data) { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| resolve(data); | ||
| } | ||
| }); | ||
| }); | ||
| return promise.then(JSON.parse); | ||
| } | ||
@@ -35,3 +48,11 @@ }, { | ||
| var blobPath = path.resolve(this.cacheDir, this.getChecksum(keys)); | ||
| return fs.writeFile(blobPath, JSON.stringify(content)); | ||
| return new Promise(function (resolve, reject) { | ||
| fs.writeFile(blobPath, JSON.stringify(content), function (err) { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
@@ -48,2 +69,29 @@ }, { | ||
| } | ||
| }, { | ||
| key: 'clean', | ||
| value: function clean() { | ||
| var _this = this; | ||
| return new Promise(function (resolve, reject) { | ||
| fs.readdir(_this.cacheDir, function (err, files) { | ||
| if (err) { | ||
| reject(err); | ||
| } else { | ||
| var promises = files.map(function (file) { | ||
| return unlink(path.resolve(_this.cacheDir, file)); | ||
| }); | ||
| Promise.all(promises).then(resolve).catch(reject); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| }, { | ||
| key: 'remove', | ||
| value: function remove() { | ||
| var _this2 = this; | ||
| return this.clean().then(function () { | ||
| return rmdir(_this2.cacheDir); | ||
| }); | ||
| } | ||
| }]); | ||
@@ -54,2 +102,18 @@ | ||
| module.exports = Cache; | ||
| module.exports = Cache; | ||
| function unlink(filePath) { | ||
| return new Promise(function (resolve, reject) { | ||
| fs.unlink(filePath, function (err) { | ||
| if (err) reject(err);else resolve(); | ||
| }); | ||
| }); | ||
| } | ||
| function rmdir(dir) { | ||
| return new Promise(function (resolve, reject) { | ||
| fs.rmdir(dir, function (err) { | ||
| if (err) reject(err);else resolve(); | ||
| }); | ||
| }); | ||
| } |
+4
-0
| var detect = require('feature-detect-es6') | ||
| if (typeof Promise === 'undefined') { | ||
| require('core-js/es6/promise') | ||
| } | ||
| module.exports = detect.all('class', 'arrowFunction', 'templateStrings') | ||
| ? require('./lib/cache-point') | ||
| : require('./es5/cache-point') |
+69
-3
| 'use strict' | ||
| const fs = require('then-fs') | ||
| const path = require('path') | ||
| const arrayify = require('array-back') | ||
| const fs = require('fs') | ||
@@ -24,2 +24,6 @@ /** | ||
| options = options || {} | ||
| if (!options.cacheDir) { | ||
| var os = require('os') | ||
| options.cacheDir = path.resolve(os.tmpdir(), 'cachePoint') | ||
| } | ||
| this.cacheDir = options.cacheDir | ||
@@ -40,3 +44,12 @@ try { | ||
| const blobPath = path.resolve(this.cacheDir, this.getChecksum(keys)) | ||
| return fs.readFile(blobPath).then(JSON.parse) | ||
| const promise = new Promise((resolve, reject) => { | ||
| fs.readFile(blobPath, (err, data) => { | ||
| if (err) { | ||
| reject(err) | ||
| } else { | ||
| resolve(data) | ||
| } | ||
| }) | ||
| }) | ||
| return promise.then(JSON.parse) | ||
| } | ||
@@ -52,3 +65,11 @@ | ||
| const blobPath = path.resolve(this.cacheDir, this.getChecksum(keys)) | ||
| return fs.writeFile(blobPath, JSON.stringify(content)) | ||
| return new Promise((resolve, reject) => { | ||
| fs.writeFile(blobPath, JSON.stringify(content), err => { | ||
| if (err) { | ||
| reject(err) | ||
| } else { | ||
| resolve() | ||
| } | ||
| }) | ||
| }) | ||
| } | ||
@@ -67,4 +88,49 @@ | ||
| } | ||
| /** | ||
| * Cleans the cache. | ||
| * @returns {Promise} | ||
| */ | ||
| clean () { | ||
| return new Promise((resolve, reject) => { | ||
| fs.readdir(this.cacheDir, (err, files) => { | ||
| if (err) { | ||
| reject(err) | ||
| } else { | ||
| const promises = files.map(file => unlink(path.resolve(this.cacheDir, file))) | ||
| Promise.all(promises).then(resolve).catch(reject) | ||
| } | ||
| }) | ||
| }) | ||
| } | ||
| /** | ||
| * Cleans and removes the cache. | ||
| * @returns {Promise} | ||
| */ | ||
| remove () { | ||
| return this.clean().then(() => { | ||
| return rmdir(this.cacheDir) | ||
| }) | ||
| } | ||
| } | ||
| module.exports = Cache | ||
| function unlink (filePath) { | ||
| return new Promise((resolve, reject) => { | ||
| fs.unlink(filePath, err => { | ||
| if (err) reject(err) | ||
| else resolve() | ||
| }) | ||
| }) | ||
| } | ||
| function rmdir (dir) { | ||
| return new Promise((resolve, reject) => { | ||
| fs.rmdir(dir, err => { | ||
| if (err) reject(err) | ||
| else resolve() | ||
| }) | ||
| }) | ||
| } |
+3
-3
| { | ||
| "name": "cache-point", | ||
| "author": "Lloyd Brookes <75pound@gmail.com>", | ||
| "version": "0.1.0", | ||
| "version": "0.1.1", | ||
| "description": "cache-point", | ||
@@ -24,5 +24,5 @@ "repository": "https://github.com/75lb/cache-point.git", | ||
| "babel-preset-es2015": "^6.9.0", | ||
| "feature-detect-es6": "^1.3.1", | ||
| "then-fs": "^2.0.0" | ||
| "core-js": "^2.4.1", | ||
| "feature-detect-es6": "^1.3.1" | ||
| } | ||
| } |
+14
-0
@@ -22,2 +22,4 @@ [](https://www.npmjs.org/package/cache-point) | ||
| * [.getChecksum(keys)](#module_cache-point--Cache+getChecksum) ⇒ <code>string</code> | ||
| * [.clean()](#module_cache-point--Cache+clean) ⇒ <code>Promise</code> | ||
| * [.remove()](#module_cache-point--Cache+remove) ⇒ <code>Promise</code> | ||
@@ -71,5 +73,17 @@ <a name="exp_module_cache-point--Cache"></a> | ||
| <a name="module_cache-point--Cache+clean"></a> | ||
| #### cache.clean() ⇒ <code>Promise</code> | ||
| Cleans the cache. | ||
| **Kind**: instance method of <code>[Cache](#exp_module_cache-point--Cache)</code> | ||
| <a name="module_cache-point--Cache+remove"></a> | ||
| #### cache.remove() ⇒ <code>Promise</code> | ||
| Cleans and removes the cache. | ||
| **Kind**: instance method of <code>[Cache](#exp_module_cache-point--Cache)</code> | ||
| * * * | ||
| © 2016 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown). |
+22
-21
| 'use strict' | ||
| var test = require('tape') | ||
| var Cache = require('../') | ||
| var fs = require('fs') | ||
| var path = require('path') | ||
| function createDir(name) { | ||
| var os = require('os') | ||
| var tmpDir = path.resolve(os.tmpdir(), 'cacheTest') | ||
| return tmpDir | ||
| } | ||
| function rmDir (dir) { | ||
| fs.readdirSync(dir).forEach(function (file) { | ||
| fs.unlinkSync(path.resolve(dir, file)) | ||
| }) | ||
| fs.rmdirSync(dir) | ||
| } | ||
| test('string key', function (t) { | ||
| t.plan(1) | ||
| var tmpDir = createDir() | ||
| var cache = new Cache({ cacheDir: tmpDir }) | ||
| var cache = new Cache() | ||
| cache.write('one', 'test1') | ||
@@ -30,3 +14,3 @@ .then(function() { | ||
| t.strictEqual(data, 'test1') | ||
| rmDir(tmpDir) | ||
| cache.remove() | ||
| }) | ||
@@ -37,4 +21,3 @@ }) | ||
| t.plan(1) | ||
| var tmpDir = createDir() | ||
| var cache = new Cache({ cacheDir: tmpDir }) | ||
| var cache = new Cache() | ||
| var objectKey = { one: true } | ||
@@ -47,4 +30,22 @@ cache.write(objectKey, 'test1') | ||
| t.strictEqual(data, 'test1') | ||
| rmDir(tmpDir) | ||
| cache.remove() | ||
| }) | ||
| }) | ||
| test('.remove()', function (t) { | ||
| t.plan(1) | ||
| var cache = new Cache() | ||
| cache.write('one', 'test1') | ||
| .then(function() { | ||
| return cache.remove() | ||
| }) | ||
| .then(function() { | ||
| t.throws(function () { | ||
| fs.statSync(tmpDir) | ||
| }) | ||
| }) | ||
| .catch(function (err) { | ||
| console.error(err.stack) | ||
| t.fail(err.message) | ||
| }) | ||
| }) |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
13660
34.69%275
85.81%88
18.92%3
50%+ Added
- Removed
- Removed
- Removed
- Removed