flat-cache 1.0.9 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -29,6 +29,7 @@ cache.removeKey('key'); // removes a key from the cache
29
29
 
30
30
  // save it to disk
31
31
  cache.save(); // very important, if you don't save no changes will be persisted.
32
+ // cache.save( true /* noPrune */) // can be used to prevent the removal of non visited keys
32
33
 
33
34
  // loads the cache from a given directory, if one does
34
35
  // not exists for the given Id a new one will be prepared to be created
@@ -57,6 +58,8 @@ storage was needed and Bam! this module was born.
57
58
  - All the changes to the cache state are done to memory
58
59
  - I could have used a timer or `Object.observe` to deliver the changes to disk, but I wanted to keep this module
59
60
  intentionally dumb and simple
61
+ - Non visited keys are removed when `cache.save()` is called. If this is not desired, you can pass `true` to the save call
62
+ like: `cache.save( true /* noPrune */ )`.
60
63
 
61
64
  ## License
62
65
 
package/cache.js CHANGED
@@ -1,8 +1,8 @@
1
1
  var path = require( 'path' );
2
2
  var fs = require( 'graceful-fs' );
3
- var readJSON = require( 'read-json-sync' );
4
- var write = require( 'write' );
5
3
  var del = require( 'del' ).sync;
4
+ var utils = require( './utils' );
5
+ var writeJSON = utils.writeJSON;
6
6
 
7
7
  var cache = {
8
8
  /**
@@ -22,7 +22,7 @@ var cache = {
22
22
  me._pathToFile = cacheDir ? path.resolve( cacheDir, docId ) : path.resolve( __dirname, './.cache/', docId );
23
23
 
24
24
  if ( fs.existsSync( me._pathToFile ) ) {
25
- me._persisted = readJSON( me._pathToFile );
25
+ me._persisted = utils.tryParse( me._pathToFile, { } );
26
26
  }
27
27
  },
28
28
 
@@ -58,8 +58,8 @@ var cache = {
58
58
  * @param key {String} the key to remove from the object
59
59
  */
60
60
  removeKey: function ( key ) {
61
- delete this._visited[ key ];
62
- delete this._persisted[ key ];
61
+ delete this._visited[ key ]; // esfmt-ignore-line
62
+ delete this._persisted[ key ]; // esfmt-ignore-line
63
63
  },
64
64
  /**
65
65
  * Return the value of the provided key
@@ -100,13 +100,14 @@ var cache = {
100
100
  /**
101
101
  * Save the state of the cache identified by the docId to disk
102
102
  * as a JSON structure
103
+ * @param [noPrune=false] {Boolean} whether to remove from cache the non visited files
103
104
  * @method save
104
105
  */
105
- save: function () {
106
+ save: function ( noPrune ) {
106
107
  var me = this;
107
108
 
108
- me._prune();
109
- write.sync( me._pathToFile, JSON.stringify( me._persisted ) );
109
+ (!noPrune) && me._prune();
110
+ writeJSON( me._pathToFile, me._persisted );
110
111
  },
111
112
 
112
113
  /**
@@ -180,9 +181,8 @@ module.exports = {
180
181
  * @method clearAll
181
182
  * @returns {Boolean} true if the cache folder was deleted. False otherwise
182
183
  */
183
- clearAll: function () {
184
- return del( path.resolve( __dirname, './.cache/' ), {
185
- force: true
186
- } ).length > 0;
184
+ clearAll: function ( cacheDir ) {
185
+ var filePath = cacheDir ? path.resolve( cacheDir ) : path.resolve( __dirname, './.cache/' );
186
+ return del( filePath, { force: true } ).length > 0;
187
187
  }
188
188
  };
package/changelog.md CHANGED
@@ -1,5 +1,79 @@
1
1
 
2
2
  # flat-cache - Changelog
3
+ ## v1.2.2
4
+ - **Bug Fixes**
5
+ - Do not crash if cache file is invalid JSON. ([#13](https://github.com/royriojas/flat-cache/issues/13)) - [87beaa6]( https://github.com/royriojas/flat-cache/commit/87beaa6 ), [Roy Riojas](https://github.com/Roy Riojas), 19/12/2016 21:03:35
6
+
7
+ Fixes <a target="_blank" class="info-link" href="https://github.com/royriojas/flat-cache/issues/12"><span>#12</span></a>
8
+
9
+ Not sure under which situations a cache file might exist that does
10
+ not contain a valid JSON structure, but just in case to cover
11
+ the possibility of this happening a try catch block has been added
12
+
13
+ If the cache is somehow not valid the cache will be discarded an a
14
+ a new cache will be stored instead
15
+ - **Other changes**
16
+ - Added travis ci support for modern node versions ([#11](https://github.com/royriojas/flat-cache/issues/11)) - [1c2b1f7]( https://github.com/royriojas/flat-cache/commit/1c2b1f7 ), [Amila Welihinda](https://github.com/Amila Welihinda), 11/11/2016 02:47:52
17
+
18
+
19
+ - Bumping `circular-son` version ([#10](https://github.com/royriojas/flat-cache/issues/10)) - [4d5e861]( https://github.com/royriojas/flat-cache/commit/4d5e861 ), [Andrea Giammarchi](https://github.com/Andrea Giammarchi), 02/08/2016 09:13:52
20
+
21
+ As mentioned in https://github.com/WebReflection/circular-json/issues/25 `circular-json` wan't rightly implementing the license field.
22
+
23
+ Latest version bump changed only that bit so that ESLint should now be happy.
24
+ ## v1.2.1
25
+ - **Bug Fixes**
26
+ - Add missing utils.js file to the package. closes [#8](https://github.com/royriojas/flat-cache/issues/8) - [ec10cf2]( https://github.com/royriojas/flat-cache/commit/ec10cf2 ), [Roy Riojas](https://github.com/Roy Riojas), 01/08/2016 04:18:57
27
+
28
+
29
+ ## v1.2.0
30
+ - **Documentation**
31
+ - Add documentation about noPrune option - [23e11f9]( https://github.com/royriojas/flat-cache/commit/23e11f9 ), [Roy Riojas](https://github.com/Roy Riojas), 01/08/2016 04:06:49
32
+
33
+
34
+ ## v1.0.11
35
+ - **Features**
36
+ - Add noPrune option to cache.save() method. closes [#7](https://github.com/royriojas/flat-cache/issues/7) - [2c8016a]( https://github.com/royriojas/flat-cache/commit/2c8016a ), [Roy Riojas](https://github.com/Roy Riojas), 01/08/2016 04:00:29
37
+
38
+
39
+ - Add json read and write utility based on circular-json - [c31081e]( https://github.com/royriojas/flat-cache/commit/c31081e ), [Jean Ponchon](https://github.com/Jean Ponchon), 28/07/2016 10:58:17
40
+
41
+
42
+ - **Bug Fixes**
43
+ - Remove UTF16 BOM stripping - [4a41e22]( https://github.com/royriojas/flat-cache/commit/4a41e22 ), [Jean Ponchon](https://github.com/Jean Ponchon), 29/07/2016 04:18:06
44
+
45
+ Since we control both writing and reading of JSON stream, there no needs
46
+ to handle unicode BOM.
47
+ - Use circular-json to handle circular references (fix [#5](https://github.com/royriojas/flat-cache/issues/5)) - [cd7aeed]( https://github.com/royriojas/flat-cache/commit/cd7aeed ), [Jean Ponchon](https://github.com/Jean Ponchon), 25/07/2016 13:11:59
48
+
49
+
50
+ - **Tests Related fixes**
51
+ - Add missing file from eslint test - [d6fa3c3]( https://github.com/royriojas/flat-cache/commit/d6fa3c3 ), [Jean Ponchon](https://github.com/Jean Ponchon), 29/07/2016 04:15:51
52
+
53
+
54
+ - Add test for circular json serialization / deserialization - [07d2ddd]( https://github.com/royriojas/flat-cache/commit/07d2ddd ), [Jean Ponchon](https://github.com/Jean Ponchon), 28/07/2016 10:59:36
55
+
56
+
57
+ - **Refactoring**
58
+ - Remove unused read-json-sync - [2be1c24]( https://github.com/royriojas/flat-cache/commit/2be1c24 ), [Jean Ponchon](https://github.com/Jean Ponchon), 28/07/2016 10:59:18
59
+
60
+
61
+ - **Build Scripts Changes**
62
+ - travis tests on 0.12 and 4x - [3a613fd]( https://github.com/royriojas/flat-cache/commit/3a613fd ), [royriojas](https://github.com/royriojas), 15/11/2015 17:34:40
63
+
64
+
65
+ ## v1.0.10
66
+ - **Build Scripts Changes**
67
+ - add eslint-fix task - [fd29e52]( https://github.com/royriojas/flat-cache/commit/fd29e52 ), [royriojas](https://github.com/royriojas), 01/11/2015 18:04:08
68
+
69
+
70
+ - make sure the test script also verify beautification and linting of files before running tests - [e94e176]( https://github.com/royriojas/flat-cache/commit/e94e176 ), [royriojas](https://github.com/royriojas), 01/11/2015 14:54:48
71
+
72
+
73
+ - **Other changes**
74
+ - add clearAll for cacheDir - [97383d9]( https://github.com/royriojas/flat-cache/commit/97383d9 ), [xieyaowu](https://github.com/xieyaowu), 31/10/2015 23:02:18
75
+
76
+
3
77
  ## v1.0.9
4
78
  - **Bug Fixes**
5
79
  - wrong default values for changelogx user repo name - [7bb52d1]( https://github.com/royriojas/flat-cache/commit/7bb52d1 ), [royriojas](https://github.com/royriojas), 11/09/2015 17:59:30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flat-cache",
3
- "version": "1.0.9",
3
+ "version": "1.2.2",
4
4
  "description": "A stupidly simple key/value storage using files to persist some data",
5
5
  "repository": "royriojas/flat-cache",
6
6
  "license": "MIT",
@@ -10,7 +10,8 @@
10
10
  },
11
11
  "main": "cache.js",
12
12
  "files": [
13
- "cache.js"
13
+ "cache.js",
14
+ "utils.js"
14
15
  ],
15
16
  "engines": {
16
17
  "node": ">=0.10.0"
@@ -22,11 +23,13 @@
22
23
  "npm run verify --silent"
23
24
  ],
24
25
  "scripts": {
25
- "beautify": "esbeautifier 'cache.js' 'specs/**/*.js'",
26
- "beautify-check": "esbeautifier -k 'cache.js' 'specs/**/*.js'",
27
- "eslint": "eslinter 'cache.js' 'specs/**/*.js'",
28
- "lint": "npm run beautify && npm run eslint",
29
- "verify": "npm run beautify-check && npm run eslint",
26
+ "beautify": "esbeautifier 'cache.js' 'test/specs/**/*.js'",
27
+ "beautify-check": "npm run beautify -- -k",
28
+ "eslint": "eslinter 'cache.js' 'utils.js' 'specs/**/*.js'",
29
+ "eslint-fix": "npm run eslint -- --fix",
30
+ "autofix": "npm run beautify && npm run eslint-fix",
31
+ "check": "npm run beautify-check && npm run eslint",
32
+ "verify": "npm run check && npm run test:cache",
30
33
  "install-hooks": "prepush install && changelogx install-hook && precommit install",
31
34
  "changelog": "changelogx -f markdown -o ./changelog.md",
32
35
  "do-changelog": "npm run changelog && git add ./changelog.md && git commit -m 'DOC: Generate changelog' --no-verify",
@@ -35,7 +38,8 @@
35
38
  "bump-major": "npm run pre-v && npm version major -m 'BLD: Release v%s' && npm run post-v",
36
39
  "bump-minor": "npm run pre-v && npm version minor -m 'BLD: Release v%s' && npm run post-v",
37
40
  "bump-patch": "npm run pre-v && npm version patch -m 'BLD: Release v%s' && npm run post-v",
38
- "test": "mocha -R spec test/specs",
41
+ "test:cache": "mocha -R spec test/specs",
42
+ "test": "npm run verify --silent",
39
43
  "cover": "istanbul cover test/runner.js html text-summary",
40
44
  "watch": "watch-run -i -p 'test/specs/**/*.js' istanbul cover test/runner.js html text-summary"
41
45
  },
@@ -62,8 +66,8 @@
62
66
  "devDependencies": {
63
67
  "chai": "^3.2.0",
64
68
  "changelogx": "^1.0.18",
65
- "esbeautifier": "^4.2.11",
66
- "eslinter": "^2.3.3",
69
+ "esbeautifier": "^6.1.8",
70
+ "eslinter": "^3.2.1",
67
71
  "glob-expand": "^0.1.0",
68
72
  "istanbul": "^0.3.19",
69
73
  "mocha": "^2.3.2",
@@ -75,9 +79,9 @@
75
79
  "watch-run": "^1.2.2"
76
80
  },
77
81
  "dependencies": {
82
+ "circular-json": "^0.3.1",
78
83
  "del": "^2.0.2",
79
84
  "graceful-fs": "^4.1.2",
80
- "read-json-sync": "^1.1.0",
81
85
  "write": "^0.2.1"
82
86
  }
83
87
  }
package/utils.js ADDED
@@ -0,0 +1,39 @@
1
+ var fs = require( 'graceful-fs' );
2
+ var write = require( 'write' );
3
+ var circularJson = require( 'circular-json' );
4
+
5
+ module.exports = {
6
+
7
+ tryParse: function ( filePath, defaultValue) {
8
+ var result;
9
+ try {
10
+ result = this.readJSON( filePath );
11
+ } catch (ex) {
12
+ result = defaultValue;
13
+ }
14
+ return result;
15
+ },
16
+
17
+ /**
18
+ * Read json file synchronously using circular-json
19
+ *
20
+ * @method readJSON
21
+ * @param {String} filePath Json filepath
22
+ * @returns {*} parse result
23
+ */
24
+ readJSON: function ( filePath ) {
25
+ return circularJson.parse( fs.readFileSync( filePath ).toString() );
26
+ },
27
+
28
+ /**
29
+ * Write json file synchronously using circular-json
30
+ *
31
+ * @method writeJSON
32
+ * @param {String} filePath Json filepath
33
+ * @param {*} data Object to serialize
34
+ */
35
+ writeJSON: function (filePath, data ) {
36
+ write.sync( filePath, circularJson.stringify( data ) );
37
+ }
38
+
39
+ };