sync-disk-cache
Advanced tools
Comparing version 0.0.2 to 1.0.0
60
index.js
@@ -9,6 +9,7 @@ 'use strict'; | ||
var rimraf = require('rimraf').sync; | ||
var unlink = fs.unlink; | ||
var unlink = fs.unlinkSync; | ||
var chmod = fs.chmodSync; | ||
var tmpDir = require('os').tmpDir(); | ||
var debug = require('debug')('sync-disk-cache'); | ||
var zlib = require('zlib'); | ||
@@ -20,3 +21,2 @@ var mode = { | ||
var CacheEntry = require('./lib/cache-entry'); | ||
/* | ||
@@ -53,2 +53,18 @@ * @private | ||
var COMPRESSIONS = { | ||
deflate: { | ||
in: zlib.deflateSync, | ||
out: zlib.inflateSync, | ||
}, | ||
deflateRaw: { | ||
in: zlib.deflateRawSync, | ||
out: zlib.inflateRawSync, | ||
}, | ||
gzip: { | ||
in: zlib.gzipSync, | ||
out: zlib.gunzipSync, | ||
}, | ||
}; | ||
/* | ||
@@ -58,11 +74,13 @@ * | ||
* @param {String} key the global key that represents this cache in its final location | ||
* @param {String} location an optional string path to the location for the | ||
* @param {String} options optional string path to the location for the | ||
* cache. If omitted the system tmpdir is used | ||
*/ | ||
function Cache(key, location) { | ||
this.tmpDir = location|| tmpDir; | ||
function Cache(key, _) { | ||
var options = _ || {}; | ||
this.tmpDir = options.location|| tmpDir; | ||
this.compression = options.compression; | ||
this.key = key || 'default-disk-cache'; | ||
this.root = path.join(this.tmpDir, this.key); | ||
debug('new Cache { root: %s }', this.root); | ||
debug('new Cache { root: %s, compression: %s }', this.root, this.compression); | ||
} | ||
@@ -113,3 +131,3 @@ | ||
try { | ||
return processFile(filePath, readFile(filePath)); | ||
return processFile(filePath, this.decompress(readFile(filePath))); | ||
} catch(e) { | ||
@@ -134,3 +152,3 @@ return handleENOENT(e); | ||
mkdirp(path.dirname(filePath), mode); | ||
writeFile(filePath, value, mode); | ||
writeFile(filePath, this.compress(value), mode); | ||
chmod(filePath, mode.mode); | ||
@@ -166,4 +184,2 @@ | ||
* @returns the path where the key's value may reside | ||
* | ||
* | ||
*/ | ||
@@ -174,2 +190,26 @@ Cache.prototype.pathFor = function(key) { | ||
/* | ||
* @public | ||
* | ||
* @method decompress | ||
* @param {String} compressedValue | ||
* @returns decompressedValue | ||
*/ | ||
Cache.prototype.decompress = function(value) { | ||
if (!this.compression) { return value; } | ||
return COMPRESSIONS[this.compression].out(value); | ||
}; | ||
/* | ||
* @public | ||
* | ||
* @method compress | ||
* @param {String} value | ||
* @returns compressedValue | ||
*/ | ||
Cache.prototype.compress = function(value) { | ||
if (!this.compression) { return value; } | ||
return COMPRESSIONS[this.compression].in(value); | ||
}; | ||
module.exports = Cache; |
{ | ||
"name": "sync-disk-cache", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"description": "sync disk cache", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -40,4 +40,14 @@ # sync-disk-cache [![Build status](https://ci.appveyor.com/api/projects/status/lfliompah66m611x?svg=true)](https://ci.appveyor.com/project/embercli/sync-disk-cache) | ||
Enable compression: | ||
```js | ||
var Cache = require('sync-disk-cache'); | ||
var cache = new Cache('my-cache', { | ||
compression: 'gzip' | 'deflate' | 'deflateRaw' // basically just what nodes zlib's ships with | ||
}) | ||
``` | ||
## License | ||
Licensed under the MIT License, Copyright 2015 Stefan Penner |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7437
192
1
53