cache-manager 2.10.1 → 2.11.1

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/.eslintrc ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "env": {
3
+ "es6": true,
4
+ "browser": false,
5
+ "node": true,
6
+ "mocha": true
7
+ },
8
+
9
+ "parserOptions": {
10
+ "ecmaVersion": 2017
11
+ },
12
+
13
+ "rules": {
14
+ "array-bracket-spacing": ["error", "never"],
15
+ "indent": ["error", 4, {"SwitchCase": 1}],
16
+ "comma-dangle": ["error", "never"],
17
+ "no-dupe-keys": "error",
18
+ "no-mixed-requires": [0, false],
19
+ "no-empty": "error",
20
+ "no-unreachable": "error",
21
+ "no-extra-bind": "error",
22
+ "no-multi-spaces": "error",
23
+ "no-redeclare": "error",
24
+ "no-undef": "error",
25
+ "no-unused-vars": "error",
26
+ "no-multiple-empty-lines": ["error", {"max": 1, "maxEOF": 0, "maxBOF": 0}],
27
+ "one-var": ["error", "never"],
28
+ "brace-style": ["error", "1tbs", {"allowSingleLine": true}],
29
+ "object-curly-spacing": ["error", "never"],
30
+ "space-before-function-paren": ["error", {
31
+ "anonymous": "never",
32
+ "named": "never",
33
+ "asyncArrow": "always"
34
+ }],
35
+ "space-before-blocks": "error",
36
+ "keyword-spacing": "error",
37
+ "arrow-parens": ["error", "as-needed"],
38
+ "space-in-parens": ["error", "never"],
39
+ "space-infix-ops": ["error", {"int32Hint": false}],
40
+ "block-spacing": "error",
41
+ "comma-spacing": ["error", {"before": false, "after": true}],
42
+ "key-spacing": ["error", {"beforeColon": false, "afterColon": true}],
43
+ "no-trailing-spaces": "error",
44
+ "operator-linebreak": ["error", "after"],
45
+ "no-unneeded-ternary": "error",
46
+ "strict": [0, "global"],
47
+ "semi": [2, "always"],
48
+ "no-extra-semi": "error",
49
+ "curly": "error",
50
+ "no-bitwise": 1,
51
+ "max-len": ["error", 140, 4],
52
+ "vars-on-top": 0,
53
+ "guard-for-in": 1
54
+ },
55
+
56
+ "globals": {
57
+ "main": "true",
58
+ "require": "true",
59
+ "module": "true"
60
+ }
61
+ }
package/.nycrc.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "check-coverage": true,
3
+ "lines": 95,
4
+ "statements": 95,
5
+ "functions": 95,
6
+ "branches": 75,
7
+ "include": [
8
+ "lib/**/*.js"
9
+ ],
10
+ "exclude": [
11
+ "**/datamigration/**",
12
+ "**/new-schema-migration/**"
13
+ ],
14
+ "reporter": [
15
+ "lcov",
16
+ "text"
17
+ ],
18
+ "cache": true,
19
+ "all": true
20
+ }
package/.travis.yml CHANGED
@@ -1,8 +1,10 @@
1
1
  language: node_js
2
2
  node_js:
3
- - '0.12'
4
- - '5.5'
3
+ - '6.10.3'
4
+ - '10'
5
+ - '12'
5
6
  script: 'make test-travis'
6
7
  branches:
7
8
  only:
9
+ - feature/fix-npm-vulnerabilities
8
10
  - master
package/History.md CHANGED
@@ -1,3 +1,15 @@
1
+ - 2.11.1 2020-02-03
2
+ - Bugfix: Preserve function prototypes in memory store (#133). -@ryanbecker
3
+
4
+ - 2.11.0 2020-01-31
5
+ - Use eslint instead of jshint/jscs; use nyc instead of istanbul; mocha/coveralls upgrades.
6
+
7
+ - 2.10.3 2020-01-31
8
+ - Preserve functions in memory store when cloning objects (#132).
9
+
10
+ - 2.10.2 2020-01-30
11
+ - Disallow mutation of values in memory store (#103).
12
+
1
13
  - 2.10.1 2019-11-06
2
14
  - Add .js to module path to fix compilers (#131). -@imjohnbo
3
15
 
package/Makefile CHANGED
@@ -1,15 +1,15 @@
1
1
  BASE = .
2
2
 
3
- ISTANBUL = ./node_modules/.bin/istanbul
3
+ NYC = ./node_modules/.bin/nyc
4
4
  COVERAGE_OPTS = --lines 99 --statements 95 --branches 90 --functions 95
5
5
 
6
6
  main: lint test docs
7
7
 
8
8
  cover:
9
- $(ISTANBUL) cover test/run.js
9
+ $(NYC) test/run.js
10
10
 
11
11
  check-coverage:
12
- $(ISTANBUL) check-coverage $(COVERAGE_OPTS)
12
+ $(NYC) check-coverage $(COVERAGE_OPTS)
13
13
 
14
14
  test: cover check-coverage
15
15
 
@@ -18,15 +18,12 @@ test-cov: cover check-coverage
18
18
  open coverage/lcov-report/index.html
19
19
 
20
20
  test-travis: lint
21
- ./node_modules/.bin/istanbul cover test/run.js --report lcovonly \
21
+ ./node_modules/.bin/nyc test/run.js --report lcovonly \
22
22
  -- -T unit,functional -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage
23
23
  lint:
24
- ./node_modules/.bin/jscs ./lib && \
25
- ./node_modules/.bin/jscs ./test && \
26
- ./node_modules/.bin/jscs ./examples && \
27
- ./node_modules/.bin/jshint ./lib --config $(BASE)/.jshintrc && \
28
- ./node_modules/.bin/jshint ./test --config $(BASE)/.jshintrc && \
29
- ./node_modules/.bin/jshint ./examples --config $(BASE)/.jshintrc
24
+ ./node_modules/.bin/eslint ./lib && \
25
+ ./node_modules/.bin/eslint ./test && \
26
+ ./node_modules/.bin/eslint ./examples
30
27
 
31
28
  docs:
32
29
  ./node_modules/.bin/jsdoc lib --recurse --readme README.md --package package.json
@@ -3,7 +3,6 @@
3
3
  var cacheManager = require('../');
4
4
  var memoryCache = cacheManager.caching({store: 'memory', max: 100, ttl: 10});
5
5
  var memoryCache2 = cacheManager.caching({store: 'memory', max: 100, ttl: 100});
6
- var ttl; //Can't use a different ttl per set() call with memory cache
7
6
 
8
7
  //
9
8
  // Basic usage
@@ -36,7 +35,8 @@ var key = 'user_' + userId;
36
35
  // wrap() example
37
36
  //
38
37
 
39
- // Instead of manually managing the cache like this:
38
+ /**
39
+ Instead of manually managing the cache like this:
40
40
  function getCachedUserManually(id, cb) {
41
41
  memoryCache.get(id, function(err, result) {
42
42
  if (err) { return cb(err); }
@@ -52,6 +52,7 @@ function getCachedUserManually(id, cb) {
52
52
  });
53
53
  });
54
54
  }
55
+ */
55
56
 
56
57
  // ... you can instead use the `wrap` function:
57
58
  function getCachedUser(id, cb) {
@@ -309,11 +309,11 @@ var multiCaching = function(caches, options) {
309
309
  return new Promise(function(resolve, reject) {
310
310
  self.wrap(key, function(cb) {
311
311
  Promise.resolve()
312
- .then(promise)
313
- .then(function(result) {
314
- cb(null, result);
315
- })
316
- .catch(cb);
312
+ .then(promise)
313
+ .then(function(result) {
314
+ cb(null, result);
315
+ })
316
+ .catch(cb);
317
317
  }, options, function(err, result) {
318
318
  if (err) {
319
319
  return reject(err);
@@ -1,13 +1,22 @@
1
+ /*eslint no-unused-vars:0*/
1
2
  var Lru = require("lru-cache");
3
+ var cloneDeep = require('lodash.clonedeep');
2
4
  var utils = require('../utils');
3
5
  var isObject = utils.isObject;
4
6
 
7
+ function clone(object) {
8
+ if (typeof object === 'object' && object !== null) {
9
+ return cloneDeep(object);
10
+ }
11
+ return object;
12
+ }
13
+
5
14
  var memoryStore = function(args) {
6
15
  args = args || {};
7
16
  var self = {};
8
17
  self.name = 'memory';
9
18
  var Promise = args.promiseDependency || global.Promise;
10
- self.usePromises = (typeof Promise === 'undefined' || args.noPromises) ? false : true;
19
+ self.usePromises = !((typeof Promise === 'undefined' || args.noPromises));
11
20
 
12
21
  var ttl = args.ttl;
13
22
  var lruOpts = {
@@ -31,6 +40,8 @@ var memoryStore = function(args) {
31
40
  };
32
41
 
33
42
  self.set = function(key, value, options, cb) {
43
+ value = clone(value);
44
+
34
45
  if (typeof options === 'function') {
35
46
  cb = options;
36
47
  options = {};
@@ -1,3 +1,4 @@
1
+ /*eslint no-unused-vars:0*/
1
2
  var utils = require('../utils');
2
3
  var isObject = utils.isObject;
3
4
 
@@ -11,7 +12,7 @@ var noneStore = function(args) {
11
12
 
12
13
  var self = {};
13
14
  self.name = 'none';
14
- self.usePromises = (typeof Promise === 'undefined' || args.noPromises) ? false : true;
15
+ self.usePromises = !((typeof Promise === 'undefined' || args.noPromises));
15
16
 
16
17
  self.set = function(key, value, options, cb) {
17
18
  if (typeof options === 'function') {
package/lib/utils.js CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  var isObject = function isObject(value) {
3
2
  return value instanceof Object && value.constructor === Object;
4
3
  };
@@ -39,5 +38,5 @@ var parseWrapArguments = function parseWrapArguments(args) {
39
38
 
40
39
  module.exports = {
41
40
  isObject: isObject,
42
- parseWrapArguments: parseWrapArguments,
41
+ parseWrapArguments: parseWrapArguments
43
42
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cache-manager",
3
- "version": "2.10.1",
3
+ "version": "2.11.1",
4
4
  "description": "Cache module for Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -20,17 +20,17 @@
20
20
  "author": "Bryan Donovan",
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
+ "lodash.clonedeep": "4.5.0",
23
24
  "async": "1.5.2",
24
25
  "lru-cache": "4.0.0"
25
26
  },
26
27
  "devDependencies": {
27
- "coveralls": "^2.3.0",
28
+ "coveralls": "3.0.9",
28
29
  "es6-promise": "^3.0.2",
29
- "istanbul": "0.4.2",
30
- "jscs": "2.11.0",
30
+ "eslint": "5.16.0",
31
31
  "jsdoc": "3.5.5",
32
- "jshint": "2.9.1",
33
- "mocha": "2.4.5",
32
+ "mocha": "7.0.1",
33
+ "nyc": "14.1.1",
34
34
  "optimist": "0.6.1",
35
35
  "sinon": "1.17.3"
36
36
  }
@@ -20,7 +20,7 @@ var methods = {
20
20
  process.nextTick(function() {
21
21
  cb(null, names.map(function(name) { return {name: name}; }));
22
22
  });
23
- },
23
+ }
24
24
  };
25
25
 
26
26
  describe("caching", function() {
@@ -66,10 +66,10 @@ describe("caching", function() {
66
66
  cache.set(key, value, {ttl: defaultTtl});
67
67
  setTimeout(function() {
68
68
  cache.get(key)
69
- .then(function(result) {
70
- assert.equal(result, value);
71
- done();
72
- });
69
+ .then(function(result) {
70
+ assert.equal(result, value);
71
+ done();
72
+ });
73
73
  }, 20);
74
74
  });
75
75
 
@@ -130,11 +130,11 @@ describe("caching", function() {
130
130
  cache.mset(key, value, key2, value2, {ttl: defaultTtl});
131
131
  setTimeout(function() {
132
132
  cache.mget(key, key2)
133
- .then(function(result) {
134
- assert.equal(result[0], value);
135
- assert.equal(result[1], value2);
136
- done();
137
- });
133
+ .then(function(result) {
134
+ assert.equal(result[0], value);
135
+ assert.equal(result[1], value2);
136
+ done();
137
+ });
138
138
  }, 20);
139
139
  });
140
140
 
@@ -209,20 +209,20 @@ describe("caching", function() {
209
209
  it("lets us delete data using promises", function(done) {
210
210
  key = support.random.string();
211
211
  cache.set(key, value)
212
- .then(function() {
213
- return cache.get(key);
214
- }).then(function(val) {
215
- assert.equal(val, value);
216
- }).then(function() {
217
- return cache.del(key);
218
- }).then(function() {
219
- return cache.get(key);
220
- }).then(function(val) {
221
- assert.strictEqual(val, undefined);
222
- }).then(done)
223
- .catch(function(err) {
224
- done(err);
225
- });
212
+ .then(function() {
213
+ return cache.get(key);
214
+ }).then(function(val) {
215
+ assert.equal(val, value);
216
+ }).then(function() {
217
+ return cache.del(key);
218
+ }).then(function() {
219
+ return cache.get(key);
220
+ }).then(function(val) {
221
+ assert.strictEqual(val, undefined);
222
+ }).then(done)
223
+ .catch(function(err) {
224
+ done(err);
225
+ });
226
226
  });
227
227
 
228
228
  describe('with multiple keys', function() {
@@ -330,7 +330,7 @@ describe("caching", function() {
330
330
  beforeEach(function() {
331
331
  fakeStore = {
332
332
  get: function() {},
333
- set: function() {},
333
+ set: function() {}
334
334
  };
335
335
  });
336
336
 
@@ -420,13 +420,13 @@ describe("caching", function() {
420
420
 
421
421
  it("lets us set and get data without a callback, returning a promise", function(done) {
422
422
  cache.keys()
423
- .then(function(keys) {
424
- assert.deepEqual(keys.sort(), savedKeys.sort());
425
- done();
426
- })
427
- .catch(function(err) {
428
- done(err);
429
- });
423
+ .then(function(keys) {
424
+ assert.deepEqual(keys.sort(), savedKeys.sort());
425
+ done();
426
+ })
427
+ .catch(function(err) {
428
+ done(err);
429
+ });
430
430
  });
431
431
 
432
432
  context("when not using promises", function() {
@@ -1186,10 +1186,10 @@ describe("caching", function() {
1186
1186
  cache.wrap('key', function() {
1187
1187
  return 'OK';
1188
1188
  })
1189
- .then(function(res) {
1190
- assert.equal(res, 'OK');
1191
- done();
1192
- });
1189
+ .then(function(res) {
1190
+ assert.equal(res, 'OK');
1191
+ done();
1192
+ });
1193
1193
  });
1194
1194
 
1195
1195
  it("should be able to chain with cache function as a promise", function(done) {
@@ -1198,10 +1198,10 @@ describe("caching", function() {
1198
1198
  resolve('OK');
1199
1199
  });
1200
1200
  })
1201
- .then(function(res) {
1202
- assert.equal(res, 'OK');
1203
- done();
1204
- });
1201
+ .then(function(res) {
1202
+ assert.equal(res, 'OK');
1203
+ done();
1204
+ });
1205
1205
  });
1206
1206
 
1207
1207
  it("should be able to catch errors in cache function as a promise", function(done) {
@@ -1210,22 +1210,22 @@ describe("caching", function() {
1210
1210
  reject('NOK');
1211
1211
  });
1212
1212
  })
1213
- .then(function() {
1214
- done(new Error('It should not call then since there is an error in the cache function!'));
1215
- })
1216
- .catch(function() {
1217
- done();
1218
- });
1213
+ .then(function() {
1214
+ done(new Error('It should not call then since there is an error in the cache function!'));
1215
+ })
1216
+ .catch(function() {
1217
+ done();
1218
+ });
1219
1219
  });
1220
1220
 
1221
1221
  it("should be able to chain with non-cacheable value", function(done) {
1222
1222
  cache.wrap('key', function() {
1223
1223
  return;
1224
1224
  })
1225
- .then(function(res) {
1226
- assert.equal(res, undefined);
1227
- done();
1228
- });
1225
+ .then(function(res) {
1226
+ assert.equal(res, undefined);
1227
+ done();
1228
+ });
1229
1229
  });
1230
1230
  });
1231
1231
  });
@@ -1293,7 +1293,7 @@ describe("caching", function() {
1293
1293
  }
1294
1294
  },
1295
1295
  get: function() {},
1296
- set: function() {},
1296
+ set: function() {}
1297
1297
  };
1298
1298
 
1299
1299
  cache = caching({store: store});
@@ -25,7 +25,7 @@ var methods = {
25
25
  return 444;
26
26
  }
27
27
  return 777;
28
- },
28
+ }
29
29
  };
30
30
 
31
31
  describe("multiCaching", function() {
@@ -96,15 +96,15 @@ describe("multiCaching", function() {
96
96
 
97
97
  memoryCache.get(key, function(err, result) {
98
98
  checkErr(err);
99
- assert.equal(result, value);
99
+ assert.deepEqual(result, value);
100
100
 
101
101
  memoryCache2.get(key, function(err, result) {
102
102
  checkErr(err);
103
- assert.equal(result, value);
103
+ assert.deepEqual(result, value);
104
104
 
105
105
  memoryCache3.get(key, function(err, result) {
106
106
  checkErr(err);
107
- assert.equal(result, value);
107
+ assert.deepEqual(result, value);
108
108
  done();
109
109
  });
110
110
  });
@@ -368,30 +368,30 @@ describe("multiCaching", function() {
368
368
  describe('using promises', function() {
369
369
  it('gets data from first cache that has it', function(done) {
370
370
  memoryCache3.set(key, value)
371
- .then(function() {
372
- return multiCache.get(key);
373
- })
374
- .then(function(result) {
375
- assert.equal(result, value);
376
- })
377
- .then(done);
371
+ .then(function() {
372
+ return multiCache.get(key);
373
+ })
374
+ .then(function(result) {
375
+ assert.equal(result, value);
376
+ })
377
+ .then(done);
378
378
  });
379
379
 
380
380
  it("passes any options to underlying caches", function(done) {
381
381
  var opts = {foo: 'bar'};
382
382
 
383
383
  multiCache.set(key, value)
384
- .then(function() {
385
- sinon.spy(memoryCache.store, 'get');
386
- return multiCache.get(key, opts);
387
- })
388
- .then(function(result) {
389
- assert.equal(result, value);
390
- assert.ok(memoryCache.store.get.calledWith(key, opts));
384
+ .then(function() {
385
+ sinon.spy(memoryCache.store, 'get');
386
+ return multiCache.get(key, opts);
387
+ })
388
+ .then(function(result) {
389
+ assert.equal(result, value);
390
+ assert.ok(memoryCache.store.get.calledWith(key, opts));
391
391
 
392
- memoryCache.store.get.restore();
393
- })
394
- .then(done);
392
+ memoryCache.store.get.restore();
393
+ })
394
+ .then(done);
395
395
  });
396
396
  });
397
397
  });
@@ -467,32 +467,32 @@ describe("multiCaching", function() {
467
467
  describe('using promises', function() {
468
468
  it('gets data from first cache that has it', function(done) {
469
469
  memoryCache3.mset(key, value, key2, value2)
470
- .then(function() {
471
- return multiCache.mget(key, key2);
472
- })
473
- .then(function(result) {
474
- assert.equal(result[0], value);
475
- assert.equal(result[1], value2);
476
- })
477
- .then(done);
470
+ .then(function() {
471
+ return multiCache.mget(key, key2);
472
+ })
473
+ .then(function(result) {
474
+ assert.equal(result[0], value);
475
+ assert.equal(result[1], value2);
476
+ })
477
+ .then(done);
478
478
  });
479
479
 
480
480
  it("passes any options to underlying caches", function(done) {
481
481
  var opts = {foo: 'bar'};
482
482
 
483
483
  multiCache.mset(key, value, key2, value2)
484
- .then(function() {
485
- sinon.spy(memoryCache.store, 'mget');
486
- return multiCache.mget(key, key2, opts);
487
- })
488
- .then(function(result) {
489
- assert.equal(result[0], value);
490
- assert.equal(result[1], value2);
491
- assert.ok(memoryCache.store.mget.calledWith(key, key2, opts));
484
+ .then(function() {
485
+ sinon.spy(memoryCache.store, 'mget');
486
+ return multiCache.mget(key, key2, opts);
487
+ })
488
+ .then(function(result) {
489
+ assert.equal(result[0], value);
490
+ assert.equal(result[1], value2);
491
+ assert.ok(memoryCache.store.mget.calledWith(key, key2, opts));
492
492
 
493
- memoryCache.store.mget.restore();
494
- })
495
- .then(done);
493
+ memoryCache.store.mget.restore();
494
+ })
495
+ .then(done);
496
496
  });
497
497
  });
498
498
  });
@@ -658,13 +658,13 @@ describe("multiCaching", function() {
658
658
 
659
659
  it("gets data from first cache that has it using promises", function(done) {
660
660
  memoryCache3.set(key, value)
661
- .then(function() {
662
- return multiCache.getAndPassUp(key);
663
- })
664
- .then(function(result) {
665
- assert.equal(result, value);
666
- done();
667
- });
661
+ .then(function() {
662
+ return multiCache.getAndPassUp(key);
663
+ })
664
+ .then(function(result) {
665
+ assert.equal(result, value);
666
+ done();
667
+ });
668
668
  });
669
669
  });
670
670
 
@@ -732,21 +732,21 @@ describe("multiCaching", function() {
732
732
 
733
733
  it("checks to see if higher levels have item using promises", function(done) {
734
734
  memoryCache3.set(key, value)
735
- .then(function() {
736
- return multiCache.getAndPassUp(key);
737
- })
738
- .then(function(result) {
739
- assert.equal(result, value);
740
- })
741
- .then(function() {
742
- process.nextTick(function() {
743
- memoryCache.get(key)
744
- .then(function(fetchedValue) {
745
- assert.equal(fetchedValue, value);
735
+ .then(function() {
736
+ return multiCache.getAndPassUp(key);
737
+ })
738
+ .then(function(result) {
739
+ assert.equal(result, value);
740
+ })
741
+ .then(function() {
742
+ process.nextTick(function() {
743
+ memoryCache.get(key)
744
+ .then(function(fetchedValue) {
745
+ assert.equal(fetchedValue, value);
746
+ });
746
747
  });
747
- });
748
- })
749
- .then(done);
748
+ })
749
+ .then(done);
750
750
  });
751
751
 
752
752
  context("when a cache store calls back with an error", function() {
@@ -776,11 +776,11 @@ describe("multiCaching", function() {
776
776
 
777
777
  it("bubbles up errors from caches and reject promise", function(done) {
778
778
  multiCache.getAndPassUp(key)
779
- .catch(function(err) {
780
- assert.ok(memoryStoreStub.get.called);
781
- assert.equal(err, fakeError);
782
- done();
783
- });
779
+ .catch(function(err) {
780
+ assert.ok(memoryStoreStub.get.called);
781
+ assert.equal(err, fakeError);
782
+ done();
783
+ });
784
784
  });
785
785
  });
786
786
  });
@@ -1669,10 +1669,10 @@ describe("multiCaching", function() {
1669
1669
  multiCache.wrap('key', function() {
1670
1670
  return 'OK';
1671
1671
  })
1672
- .then(function(res) {
1673
- assert.equal(res, 'OK');
1674
- done();
1675
- });
1672
+ .then(function(res) {
1673
+ assert.equal(res, 'OK');
1674
+ done();
1675
+ });
1676
1676
  });
1677
1677
 
1678
1678
  it("should be able to chain with cache function as a promise", function(done) {
@@ -1681,10 +1681,10 @@ describe("multiCaching", function() {
1681
1681
  resolve('OK');
1682
1682
  });
1683
1683
  })
1684
- .then(function(res) {
1685
- assert.equal(res, 'OK');
1686
- done();
1687
- });
1684
+ .then(function(res) {
1685
+ assert.equal(res, 'OK');
1686
+ done();
1687
+ });
1688
1688
  });
1689
1689
 
1690
1690
  it("should be able to catch errors in cache function as a promise", function(done) {
@@ -1693,34 +1693,34 @@ describe("multiCaching", function() {
1693
1693
  reject('NOK');
1694
1694
  });
1695
1695
  })
1696
- .then(function() {
1697
- done(new Error('It should not call then since there is an error in the cache function!'));
1698
- })
1699
- .catch(function() {
1700
- done();
1701
- });
1696
+ .then(function() {
1697
+ done(new Error('It should not call then since there is an error in the cache function!'));
1698
+ })
1699
+ .catch(function() {
1700
+ done();
1701
+ });
1702
1702
  });
1703
1703
 
1704
1704
  it("should be able to catch a throw in cache function as a promise", function(done) {
1705
1705
  multiCache.wrap('key', function() {
1706
1706
  throw 'NOK';
1707
1707
  })
1708
- .then(function() {
1709
- done(new Error('It should not call then since there is an error in the cache function!'));
1710
- })
1711
- .catch(function() {
1712
- done();
1713
- });
1708
+ .then(function() {
1709
+ done(new Error('It should not call then since there is an error in the cache function!'));
1710
+ })
1711
+ .catch(function() {
1712
+ done();
1713
+ });
1714
1714
  });
1715
1715
 
1716
1716
  it("should be able to chain with non-cacheable value", function(done) {
1717
1717
  multiCache.wrap('key', function() {
1718
1718
  return;
1719
1719
  })
1720
- .then(function(res) {
1721
- assert.equal(res, undefined);
1722
- done();
1723
- });
1720
+ .then(function(res) {
1721
+ assert.equal(res, undefined);
1722
+ done();
1723
+ });
1724
1724
  });
1725
1725
  });
1726
1726
  });
package/test/run.js CHANGED
@@ -7,8 +7,8 @@ var optimist = require('optimist');
7
7
  var walkDir = require('./support').walkDir;
8
8
 
9
9
  var argv = optimist
10
- .usage("Usage: $0 -t [types] --reporter [reporter] --timeout [timeout]")['default'](
11
- {types: 'unit,functional', reporter: 'spec', timeout: 6000})
10
+ .usage("Usage: $0 -t [types] --reporter [reporter] --timeout [timeout]")['default'](
11
+ {types: 'unit,functional', reporter: 'spec', timeout: 6000})
12
12
  .describe('types', 'The types of tests to run, separated by commas. E.g., unit,functional,acceptance')
13
13
  .describe('reporter', 'The mocha test reporter to use.')
14
14
  .describe('timeout', 'The mocha timeout to use per test (ms).')
@@ -1,5 +1,7 @@
1
1
  var assert = require('assert');
2
2
  var support = require('../support');
3
+ var checkErr = support.checkErr;
4
+ var caching = require('../../index').caching;
3
5
  var memoryStore = require('../../lib/stores/memory');
4
6
 
5
7
  describe("memory store", function() {
@@ -34,4 +36,150 @@ describe("memory store", function() {
34
36
  }, 10);
35
37
  });
36
38
  });
39
+
40
+ describe("when used with wrap() function", function() {
41
+ var cache;
42
+ var key;
43
+ var opts = {};
44
+
45
+ beforeEach(function() {
46
+ key = support.random.string(20);
47
+ });
48
+
49
+ context("when cache misses", function() {
50
+ function getCachedObject(name, cb) {
51
+ cache.wrap(key, function(cacheCb) {
52
+ cacheCb(null, {foo: 'bar'});
53
+ }, opts, cb);
54
+ }
55
+
56
+ function getCachedString(name, cb) {
57
+ cache.wrap(key, function(cacheCb) {
58
+ cacheCb(null, 'bar');
59
+ }, opts, cb);
60
+ }
61
+
62
+ function getCachedArray(name, cb) {
63
+ cache.wrap(key, function(cacheCb) {
64
+ cacheCb(null, [1, 2, 3]);
65
+ }, opts, cb);
66
+ }
67
+
68
+ function getCachedNumber(name, cb) {
69
+ cache.wrap(key, function(cacheCb) {
70
+ cacheCb(null, 34);
71
+ }, opts, cb);
72
+ }
73
+
74
+ function getCachedFunction(name, cb) {
75
+ cache.wrap(key, function(cacheCb) {
76
+ cacheCb(null, function() {
77
+ return 'foo';
78
+ });
79
+ }, opts, cb);
80
+ }
81
+
82
+ function getCachedObjectWithPrototype(name, cb) {
83
+ function Thing() {}
84
+ Thing.prototype.f = function() {
85
+ return 'foo';
86
+ };
87
+ cache.wrap(key, function(cacheCb) {
88
+ cacheCb(null, new Thing());
89
+ }, opts, cb);
90
+ }
91
+
92
+ function assertCachedObjectWithPrototype(result) {
93
+ assert.equal(typeof result, 'object');
94
+ var prototype = Object.getPrototypeOf(result);
95
+ assert.equal(typeof prototype.f, 'function', 'prototype does not have function f');
96
+ assert.equal(result.f(), 'foo', 'prototype function f does not return expected value');
97
+ }
98
+
99
+ beforeEach(function() {
100
+ cache = caching({store: 'memory', ttl: opts.ttl, ignoreCacheErrors: false});
101
+ });
102
+
103
+ it("does not allow mutation of objects", function(done) {
104
+ getCachedObject('foo', function(err, result) {
105
+ checkErr(err);
106
+ result.foo = 'buzz';
107
+
108
+ getCachedObject('foo', function(err, result) {
109
+ checkErr(err);
110
+ assert.equal(result.foo, 'bar');
111
+ done();
112
+ });
113
+ });
114
+ });
115
+
116
+ it("does not allow mutation of arrays", function(done) {
117
+ getCachedArray('foo', function(err, result) {
118
+ checkErr(err);
119
+ assert.ok(result);
120
+ result = ['a', 'b', 'c'];
121
+
122
+ getCachedArray('foo', function(err, result) {
123
+ checkErr(err);
124
+ assert.deepEqual(result, [1, 2, 3]);
125
+ done();
126
+ });
127
+ });
128
+ });
129
+
130
+ it("does not allow mutation of strings", function(done) {
131
+ getCachedString('foo', function(err, result) {
132
+ checkErr(err);
133
+ assert.ok(result);
134
+ result = 'buzz';
135
+
136
+ getCachedString('foo', function(err, result) {
137
+ checkErr(err);
138
+ assert.equal(result, 'bar');
139
+ done();
140
+ });
141
+ });
142
+ });
143
+
144
+ it("does not allow mutation of numbers", function(done) {
145
+ getCachedNumber('foo', function(err, result) {
146
+ checkErr(err);
147
+ assert.ok(result);
148
+ result = 12;
149
+
150
+ getCachedNumber('foo', function(err, result) {
151
+ checkErr(err);
152
+ assert.equal(result, 34);
153
+ done();
154
+ });
155
+ });
156
+ });
157
+
158
+ it("preserves functions", function(done) {
159
+ getCachedFunction('foo', function(err, result) {
160
+ checkErr(err);
161
+ assert.equal(typeof result, 'function');
162
+
163
+ getCachedFunction('foo', function(err, result) {
164
+ checkErr(err);
165
+ assert.equal(typeof result, 'function');
166
+ done();
167
+ });
168
+ });
169
+ });
170
+
171
+ it("preserves object prototype", function(done) {
172
+ getCachedObjectWithPrototype('foo', function(err, result) {
173
+ checkErr(err);
174
+ assertCachedObjectWithPrototype(result);
175
+
176
+ getCachedObjectWithPrototype('foo', function(err, result) {
177
+ checkErr(err);
178
+ assertCachedObjectWithPrototype(result);
179
+ done();
180
+ });
181
+ });
182
+ });
183
+ });
184
+ });
37
185
  });
@@ -1,4 +1,3 @@
1
-
2
1
  var utils = require('../lib/utils');
3
2
  var assert = require('assert');
4
3
 
package/.jscs.json DELETED
@@ -1,106 +0,0 @@
1
- {
2
- "requireCurlyBraces": ["if", "else", "for", "while", "do", "try", "catch"],
3
- "requireSpaceAfterKeywords": ["if", "else", "for", "while", "do", "switch", "return", "try", "catch"],
4
-
5
- "requireSpaceBeforeKeywords": [
6
- "else",
7
- "while",
8
- "catch"
9
- ],
10
-
11
- "requireSpaceBeforeBinaryOperators": ["?", "+", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
12
- "requireSpaceAfterBinaryOperators": ["?", "+", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="],
13
- "disallowSpaceAfterBinaryOperators": ["!"],
14
- "disallowSpaceBeforeBinaryOperators": [","],
15
- "requireCamelCaseOrUpperCaseIdentifiers": true,
16
-
17
- "disallowMultipleVarDecl": true,
18
- "disallowEmptyBlocks": true,
19
- "disallowKeywords": ["with"],
20
- "disallowKeywordsOnNewLine": ["else"],
21
- "disallowSpacesInsideObjectBrackets": true,
22
- "disallowSpacesInsideArrayBrackets": true,
23
- "disallowSpacesInsideParentheses": true,
24
- "disallowSpaceBeforePostfixUnaryOperators": ["++", "--"],
25
- "disallowMultipleLineStrings": true,
26
- "disallowTrailingWhitespace": true,
27
- "disallowPaddingNewlinesInBlocks": true,
28
-
29
- "requireCommaBeforeLineBreak": true,
30
-
31
- "requireParenthesesAroundIIFE": true,
32
-
33
- "requireSpaceBeforeBlockStatements": true,
34
- "disallowNewlineBeforeBlockStatements": true,
35
-
36
- "requireSpacesInConditionalExpression": true,
37
-
38
- "requireSpacesInFunctionExpression": {
39
- "beforeOpeningCurlyBrace": true
40
- },
41
-
42
- "disallowSpacesInAnonymousFunctionExpression": {
43
- "beforeOpeningRoundBrace": true
44
- },
45
-
46
- "disallowSpacesInFunctionDeclaration": {
47
- "beforeOpeningRoundBrace": true
48
- },
49
-
50
- "disallowSpacesInFunctionExpression": {
51
- "beforeOpeningRoundBrace": true
52
- },
53
-
54
- "requireSpaceBeforeBinaryOperators": [
55
- "+",
56
- "-",
57
- "/",
58
- "*",
59
- "=",
60
- "==",
61
- "===",
62
- "!=",
63
- "!=="
64
- ],
65
-
66
- "requireOperatorBeforeLineBreak": [
67
- "?",
68
- "+",
69
- "-",
70
- "/",
71
- "*",
72
- "=",
73
- "==",
74
- "===",
75
- "!=",
76
- "!==",
77
- ">",
78
- ">=",
79
- "<",
80
- "<="
81
- ],
82
-
83
- "safeContextKeyword": "self",
84
-
85
- "maximumLineLength": {
86
- "value": 120,
87
- "allowUrlComments": true,
88
- "allowRegex": true
89
- },
90
-
91
- "validateIndentation": 4,
92
- "validateParameterSeparator": ", ",
93
-
94
- "excludeFiles": [
95
- "test/data/**",
96
- "test/support/fakes/**"
97
- ],
98
- "jsDoc": {
99
- "checkParamNames": true,
100
- "requireParamTypes": true
101
- },
102
-
103
- "disallowMultipleLineBreaks": true,
104
- "validateLineBreaks": "LF",
105
- "disallowYodaConditions": true
106
- }
package/.jshintrc DELETED
@@ -1,87 +0,0 @@
1
- {
2
- // Settings
3
- "passfail" : false, // Stop on first error.
4
- "maxerr" : 500, // Maximum errors before stopping.
5
- "multistr" : true,
6
-
7
-
8
- // Predefined globals whom JSHint will ignore.
9
- "browser" : true, // Standard browser globals e.g. `window`, `document`.
10
-
11
- "node" : false,
12
- "rhino" : false,
13
- "couch" : false,
14
- "wsh" : true, // Windows Scripting Host.
15
-
16
- "jquery" : true,
17
- "prototypejs" : false,
18
- "mootools" : false,
19
- "dojo" : false,
20
-
21
- "predef" : [ // Extra globals.
22
- "__dirname",
23
- "Buffer",
24
- "event",
25
- "exports",
26
- "global",
27
- "module",
28
- "process",
29
- "require",
30
- "daisyjs",
31
- "after",
32
- "afterEach",
33
- "before",
34
- "beforeEach",
35
- "context",
36
- "describe",
37
- "it"
38
- ],
39
-
40
- // Development.
41
- "debug" : false, // Allow debugger statements e.g. browser breakpoints.
42
- "devel" : true, // Allow development statements e.g. `console.log();`.
43
-
44
-
45
- // EcmaScript 5.
46
- "strict" : false, // Require `use strict` pragma in every file.
47
- "globalstrict" : false, // Allow global "use strict" (also enables 'strict').
48
-
49
-
50
- "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons).
51
- "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons.
52
- "bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.).
53
- "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments.
54
- "curly" : true, // Require {} for every new block or scope.
55
- "eqeqeq" : false, // Require triple equals i.e. `===`.
56
- "eqnull" : false, // Tolerate use of `== null`.
57
- "evil" : false, // Tolerate use of `eval`.
58
- "expr" : false, // Tolerate `ExpressionStatement` as Programs.
59
- "forin" : false, // Tolerate `for in` loops without `hasOwnProperty`.
60
- "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
61
- "latedef" : "nofunc", // Prohibit variable use before definition.
62
- "loopfunc" : true, // Allow functions to be defined within loops.
63
- "maxparams" : 4,
64
- "maxdepth" : 5,
65
- "maxcomplexity" : 8,
66
- "maxstatements" : 40,
67
- "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`.
68
- "regexp" : false, // Prohibit `.` and `[^...]` in regular expressions.
69
- "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`.
70
- "scripturl" : true, // Tolerate script-targeted URLs.
71
- "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`.
72
- "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`.
73
- "undef" : true, // Require all non-global variables be declared before they are used.
74
-
75
-
76
- "newcap" : false, // Require capitalization of all constructor functions e.g. `new F()`.
77
- "noempty" : true, // Prohibit use of empty blocks.
78
- "nonew" : false, // Prohibit use of constructors for side-effects.
79
- "nomen" : false, // Prohibit use of initial or trailing underbars in names.
80
- "onevar" : false, // Allow only one `var` statement per function.
81
- "plusplus" : false, // Prohibit use of `++` & `--`.
82
- "sub" : true, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`.
83
- "trailing" : true, // Prohibit trailing whitespaces. (only works if white is 'true')
84
- "white" : true, // Check against strict whitespace and indentation rules.
85
- "indent" : 4,
86
- "unused" : true
87
- }