memoize-fs
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -0,1 +1,5 @@ | ||
### 1.3.0 (2017-08-26) | ||
* exporting getCacheFilePath method | ||
### 1.2.0 (2017-08-26) | ||
@@ -2,0 +6,0 @@ |
59
index.js
@@ -11,3 +11,32 @@ 'use strict' | ||
module.exports = function (options) { | ||
module.exports = buildMemoizer | ||
module.exports.getCacheFilePath = getCacheFilePath | ||
function serialize (val) { | ||
var circRefColl = [] | ||
return JSON.stringify(val, function (name, value) { | ||
if (typeof value === 'function') { | ||
return // ignore arguments and attributes of type function silently | ||
} | ||
if (typeof value === 'object' && value !== null) { | ||
if (circRefColl.indexOf(value) !== -1) { | ||
// circular reference found, discard key | ||
return | ||
} | ||
// store value in collection | ||
circRefColl.push(value) | ||
} | ||
return value | ||
}) | ||
} | ||
function getCacheFilePath (fn, args, opt) { | ||
var salt = opt.salt || '' | ||
var fnStr = (opt.noBody ? '' : opt.astBody ? JSON.stringify(parseScript(String(fn))) : String(fn)) | ||
var argsStr = serialize(args) | ||
var hash = crypto.createHash('md5').update(fnStr + argsStr + salt).digest('hex') | ||
return path.join(opt.cachePath, opt.cacheId, hash) | ||
} | ||
function buildMemoizer (options) { | ||
// check args | ||
@@ -34,28 +63,2 @@ if (typeof options !== 'object') { | ||
function serialize (val) { | ||
var circRefColl = [] | ||
return JSON.stringify(val, function (name, value) { | ||
if (typeof value === 'function') { | ||
return // ignore arguments and attributes of type function silently | ||
} | ||
if (typeof value === 'object' && value !== null) { | ||
if (circRefColl.indexOf(value) !== -1) { | ||
// circular reference found, discard key | ||
return | ||
} | ||
// store value in collection | ||
circRefColl.push(value) | ||
} | ||
return value | ||
}) | ||
} | ||
function getCacheFilePath (fn, args, opt) { | ||
var salt = opt.salt || '' | ||
var fnStr = (opt.noBody ? '' : opt.astBody ? JSON.stringify(parseScript(String(fn))) : String(fn)) | ||
var argsStr = serialize(args) | ||
var hash = crypto.createHash('md5').update(fnStr + argsStr + salt).digest('hex') | ||
return path.join(options.cachePath, opt.cacheId, hash) | ||
} | ||
function memoizeFn (fn, opt) { | ||
@@ -98,3 +101,3 @@ function checkOptions (optExt) { | ||
return new Promise(function (resolve, reject) { | ||
var filePath = getCacheFilePath(fn, args, optExt) | ||
var filePath = getCacheFilePath(fn, args, Object.assign({}, optExt, {cachePath: options.cachePath})) | ||
@@ -101,0 +104,0 @@ function cacheAndReturn () { |
{ | ||
"name": "memoize-fs", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "memoize/cache in file system solution for Node.js", | ||
@@ -5,0 +5,0 @@ "author": "Boris Diakur <contact@borisdiakur.com> (https://github.com/borisdiakur)", |
@@ -55,3 +55,3 @@ # memoize-fs | ||
memoise-fs assumes a function asynchronous if the last argument it accepts is of type `function` and that function itself accepts at least one argument. | ||
memoize-fs assumes a function asynchronous if the last argument it accepts is of type `function` and that function itself accepts at least one argument. | ||
So basically you don't have to do anything differently than when memoizing synchronous functions. Just make sure the above condition is fulfilled. | ||
@@ -183,2 +183,11 @@ Here is an example of memoizing a function with a callback: | ||
The hash is created from the serialized arguments, the function body and the [salt](#salt) (if provided as an option). | ||
You can generate this hash using `memoize.getCacheFilePath`: | ||
```js | ||
var memoize = require('memoize-fs') | ||
memoize.getCacheFilePath(function () {}, ['arg', 'arg'], {cacheId: 'foobar', cachePath: '/'}) | ||
// -> '/foobar/06f254...' | ||
``` | ||
Since memoize-fs is using JSON for serialization, __you should know__ how it works around some of its "limitations": | ||
@@ -185,0 +194,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
90980
10
2382
241
1