node-persist
Advanced tools
Comparing version 2.0.8 to 2.0.9
{ | ||
"name": "node-persist", | ||
"version": "2.0.8", | ||
"version": "2.0.9", | ||
"description": "Super-easy (and fast) persistent data structures in Node.js, modeled after HTML5 localStorage", | ||
@@ -5,0 +5,0 @@ "main": "./src/node-persist.js", |
@@ -67,2 +67,4 @@ # node-persist | ||
* no longer need/support a `options.ttlDir`, since the `ttls` are now stored in the same file as each value | ||
* added `expiredInterval` option | ||
* added `forgiveParseErrors` option | ||
@@ -93,10 +95,24 @@ ## 1.0.0 change logs | ||
storage.init({ | ||
dir:'relative/path/to/persist', | ||
dir: 'relative/path/to/persist', | ||
stringify: JSON.stringify, | ||
parse: JSON.parse, | ||
encoding: 'utf8', | ||
logging: false, // can also be custom logging function | ||
continuous: true, | ||
interval: false, // milliseconds | ||
continuous: true, // continously persist to disk | ||
interval: false, // milliseconds, persist to disk on an interval | ||
ttl: false, // ttl* [NEW], can be true for 24h default or a number in MILLISECONDS | ||
expiredInterval: 2 * 60 * 1000, // [NEW] every 2 minutes the process will clean-up the expired cache | ||
// in some cases, you (or some other service) might add non-valid storage files to your | ||
// storage dir, i.e. Google Drive, make this true if you'd like to ignore these files and not throw an error | ||
forgiveParseErrors: false // [NEW] | ||
}, /* optional callback */ ).then(onSuccess, onError); // or use the promise | ||
@@ -103,0 +119,0 @@ ``` |
@@ -22,2 +22,3 @@ /* | ||
expiredInterval: 2 * 60 * 1000, /* every 2 minutes */ | ||
forgiveParseErrors: false, | ||
ttl: false | ||
@@ -36,5 +37,3 @@ }, | ||
noop = function(err) { | ||
if (err) throw err; | ||
}, | ||
noop = function() {}, | ||
@@ -45,11 +44,5 @@ md5 = function (data) { | ||
/* | ||
* To support backward compatible callbacks, | ||
* i.e callback(data) vs callback(err, data); | ||
* replace with noop and fix args order, when ready to break backward compatibily for the following API functions | ||
* - values() | ||
* - valuesWithKeyMatch() | ||
* hint: look for 'todo-breaks-backward' in the source | ||
*/ | ||
noopWithoutError = function() {}; | ||
isValidStorageFileContent = function (content) { | ||
return content && content.key; | ||
}; | ||
@@ -622,12 +615,23 @@ var LocalStorage = function (userOptions) { | ||
var error = function (err) { | ||
deferred.reject(err); | ||
return callback(err); | ||
}; | ||
var done = function (input) { | ||
deferred.resolve(input); | ||
callback(null, input); | ||
}; | ||
fs.readFile(file, options.encoding, function (err, text) { | ||
if (err) { | ||
deferred.reject(err); | ||
return callback(err); | ||
return error(err); | ||
} | ||
var input = self.parse(text); | ||
if (!isValidStorageFileContent(input)) { | ||
return options.forgiveParseErrors ? done() : error(new Error('[PARSE-ERROR] ' + file + ' does not look like a valid storage file!')); | ||
} | ||
self.data[input.key] = input; | ||
self.log("loaded: " + dir + "/" + input.key); | ||
deferred.resolve(input); | ||
callback(null, input); | ||
done(input); | ||
}); | ||
@@ -642,2 +646,8 @@ | ||
var input = this.parse(fs.readFileSync(file, this.options.encoding)); | ||
if (!isValidStorageFileContent(input)) { | ||
if (this.options.forgiveParseErrors) { | ||
return; | ||
} | ||
throw Error('[PARSE-ERROR] ' + file + ' does not look like a valid storage file!'); | ||
} | ||
this.data[input.key] = input; | ||
@@ -644,0 +654,0 @@ this.log("loaded: " + dir + "/" + input.key); |
@@ -376,2 +376,77 @@ | ||
describe("Parsing errors", function() { | ||
it("should throw an error (sync) because of an invalid file in the storage dir", function (done) { | ||
this.timeout(5000); | ||
var dir = randDir(); | ||
var storage = nodePersist.create(); | ||
// make sure the dir is there, and write a random file in there | ||
mkdirp.sync(dir); | ||
fs.writeFileSync(dir + '/foo.bar', 'nothing that makes sense'); | ||
try { | ||
storage.initSync({ | ||
dir: dir | ||
}); | ||
} catch (e) { | ||
assert.equal(true, /^\[PARSE-ERROR\].*does not look like a valid storage file/.test(e.message)); | ||
done(); | ||
} | ||
}); | ||
it("should NOT throw an error (sync) because of an invalid file in the storage dir, because forgiveParseErrors=true", function (done) { | ||
this.timeout(5000); | ||
var dir = randDir(); | ||
var storage = nodePersist.create(); | ||
// make sure the dir is there, and write a random file in there | ||
mkdirp.sync(dir); | ||
fs.writeFileSync(dir + '/foo.bar', 'nothing that makes sense'); | ||
storage.initSync({ | ||
dir: dir, | ||
forgiveParseErrors: true | ||
}); | ||
assert.equal(storage.options.dir, dir, "options.dir don't match"); | ||
done(); | ||
}); | ||
it("should throw an error (async) because of an invalid file in the storage dir", function (done) { | ||
this.timeout(5000); | ||
var dir = randDir(); | ||
var storage = nodePersist.create(); | ||
// make sure the dir is there, and write a random file in there | ||
mkdirp.sync(dir); | ||
fs.writeFileSync(dir + '/foo.bar', 'nothing that makes sense'); | ||
storage.init({ | ||
dir: dir | ||
}).catch(function(err) { | ||
assert.equal(true, /^\[PARSE-ERROR\].*does not look like a valid storage file/.test(err.message)); | ||
done(); | ||
}); | ||
}); | ||
it("should NOT throw an error (async) because of an invalid file in the storage dir, because forgiveParseErrors=true", function (done) { | ||
this.timeout(5000); | ||
var dir = randDir(); | ||
var storage = nodePersist.create(); | ||
// make sure the dir is there, and write a random file in there | ||
mkdirp.sync(dir); | ||
fs.writeFileSync(dir + '/foo.bar', 'nothing that makes sense'); | ||
storage.init({ | ||
dir: dir, | ||
forgiveParseErrors: true | ||
}).then(function(options) { | ||
assert.equal(options.dir, dir, "options.dir don't match"); | ||
done(); | ||
}).catch(function(err) { | ||
throw err; | ||
}); | ||
}); | ||
}); | ||
after(function(done) { | ||
@@ -378,0 +453,0 @@ rmdir(TEST_BASE_DIR, done); |
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
53545
1085
310