Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mightycache

Package Overview
Dependencies
Maintainers
2
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mightycache - npm Package Compare versions

Comparing version 2.1.0 to 3.0.0

lib/cacheImpl/mem.js

28

index.js

@@ -1,6 +0,7 @@

(function(module, util, handler, cacheInterface) {
(function (module, util, handler, cacheInterface, setInterface) {
'use strict';
module.exports.cacheInterface = cacheInterface;
module.exports.setInterface = setInterface;
module.exports.handler = function Handler (cacheImpl, options) {
module.exports.handler = function Handler(cacheImpl, options) {
if (!cacheImpl) {

@@ -28,8 +29,21 @@ throw new Error('A Cache Implementation is required');

var modulePath = './lib/impl/' + impl;
var cacheModule = './lib/cacheImpl/' + impl,
setModule = './lib/setImpl/' + impl,
setClass,
cacheClass;
try {
return require(modulePath)(options);
setClass = require(setModule);
}
catch (err) {
console.warn('Implementation [%s] does not provide a set class. Set Functionality is not required but is recommended', impl);
}
try {
cacheClass = require(cacheModule);
if (setClass) {
cacheClass.Set = setClass;
}
} catch (err) {
if (err && err.code === 'MODULE_NOT_FOUND' && err.message === 'Cannot find module \'' + modulePath + '\'') {
if (err && err.code === 'MODULE_NOT_FOUND' && err.message === 'Cannot find module \'' + cacheModule + '\'') {
throw new Error(util.format('Implementation [%s] does not exist', impl));

@@ -39,3 +53,5 @@ }

}
return cacheClass(options);
};
}(module, require('util'), require('./lib/handler'), require('./lib/cacheInterface')));
}(module, require('util'), require('./lib/handler'), require('./lib/cacheInterface'), require('./lib/setInterface')));

@@ -24,23 +24,5 @@ /*

*/
(function(module, util){
(function (module, interfaceFactory) {
'use strict';
function notImplemented(method){
throw new Error(util.format('function %s not implemented', method));
}
function CacheInterface(){}
var proto = {};
var interfaces = ['save', 'restore', 'remove'];
interfaces.forEach(function(method){
proto[method] = function(){
notImplemented(method);
};
});
CacheInterface.prototype = proto;
module.exports = CacheInterface;
}(module, require('util')));
module.exports = interfaceFactory(['save', 'restore', 'remove', 'keys', 'set']);
}(module, require('./interfaceFactory')));

@@ -24,6 +24,6 @@ /*

*/
(function(module, util){
(function (module, util) {
'use strict';
function CacheError(name, message, code){
function CacheError(name, message, code) {
if (!(this instanceof CacheError)) {

@@ -61,6 +61,9 @@ return new CacheError(name, message, code);

code: 1
},
SET_NOT_DEFINED: {
name: 'SetNotDefined',
message: 'Set Class is not associated with this cache',
code: 5
}
};
}(module, require('util')));
{
"name": "mightycache",
"version": "2.1.0",
"version": "3.0.0",
"description": "Module providing multiple implementations of a cache backed by a data store.",

@@ -45,4 +45,7 @@ "keywords": [

"dependencies": {
"inherit-multiple": "^1.x",
"jssha": "^1.x",
"q": "^1.x",
"s3fs": "0.0.1",
"uuid": "^2.x",
"redis": "^0.x",

@@ -52,19 +55,19 @@ "s3fs": "^1.x"

"devDependencies": {
"bower": "^1.x",
"buddy.js": "^0.x",
"chai": "<1.10.0",
"chai-as-promised": "^4.x",
"dirty-chai": "^1.0.0",
"istanbul": "^0.x",
"mocha": "^1.x",
"should": "^4.x",
"supertest": "^0.x",
"xunit-file": "^0.x",
"jscs": "^1.x",
"jshint": "^2.x",
"jscs": "^1.x",
"jsinspect": "^0.x",
"buddy.js": "^0.x",
"nsp": "^0.x",
"mocha": "^2.x",
"nsp": "^1.x",
"should": "^4.x",
"reporter-file": "^0.x"
},
"scripts": {
"test": "MOCHA_REPORTER=Spec MOCHA_REPORTER_FILE=reports/xunit.xml istanbul cover node_modules/mocha/bin/_mocha -- --check-leaks --timeout 60000 --reporter reporter-file test/",
"inspect": "jshint --show-non-errors --reporter=jslint . > reports/jshint.xml | echo 'Oh hai' && jscs --reporter checkstyle . > reports/checkstyle.xml | echo 'Herro' && jsinspect . > reports/jsinspect.json | echo 'Boo Urns!' && buddy --reporter json ./routers ./test ./vhosts app.js > reports/buddy.json | echo 'Woo' && nsp audit-package >& reports/security.txt | echo 'Bond, James Bond.'"
"test": "MOCHA_REPORTER=Spec MOCHA_REPORTER_FILE=reports/xunit.xml istanbul cover node_modules/mocha/bin/_mocha -- --check-leaks --timeout 60000 --reporter reporter-file test/ && jshint --show-non-errors . && jscs . && buddy ./lib ./test index.js && nsp audit-package",
"inspect": "jshint --show-non-errors --reporter=jslint . > reports/jshint.xml | echo 'Oh hai' && jscs --reporter checkstyle . > reports/checkstyle.xml | echo 'Herro' && jsinspect . > reports/jsinspect.json | echo 'Boo Urns!' && buddy --reporter json ./lib ./test index.js > reports/buddy.json | echo 'Woo' && nsp audit-package >& reports/security.txt | echo 'Bond, James Bond.'"
}
}

@@ -206,2 +206,36 @@ # Mighty Cache

### keys()
Returns an array of all of the saved keys in the cache or within the cache set (if called on a Set).
```js
var cache = mightyCache.cache(cacheImplName, options);
cache.keys().then(function(keys) {
// array of keys
}, function(reason) {
// Something went wrong
});
```
### set(key)
Creates an instance of a Set. This allows grouping many items under any given key. The Set instance supports all
the methods of a Cache instance with the exception of set. You are not allowed to create a set form a set instance. Multiple
sets can be created for each cache, if the same set key is requested a new one will not be create but it will be retrieve
a cached set.
* key `String`. **Required**. Key of the hash set
```js
var cache = mightyCache.cache(cacheImplName, options);
cache.set('myNewSet').then(function (cacheSet) {
cacheSet.save('Test Data', 'test-key').then(function (data) {
// Data successfully stored `data.etag` has the etag that was generated
}, function (reason) {
// Something went wrong
});
},
function (reason) {
// Something went wrong
});
```
## Testing

@@ -227,6 +261,6 @@ This repository uses [Mocha](http://mochajs.org/) as its test runner. Tests can be run by executing the following command:

=============================== Coverage summary ===============================
Statements : 78.07% ( 356/456 )
Branches : 50.23% ( 107/213 )
Functions : 74.77% ( 83/111 )
Lines : 78.07% ( 356/456 )
Statements : 83.05% ( 397/478 )
Branches : 77.84% ( 130/167 )
Functions : 84.85% ( 112/132 )
Lines : 83.05% ( 397/478 )
================================================================================

@@ -233,0 +267,0 @@ ```

@@ -1,2 +0,3 @@

(function (should, errors) {
(function (expect, errors) {
'use strict';
describe('Errors Implementation', function () {

@@ -7,3 +8,3 @@ it('Should be able to instantiate an error without the \'new\' keyword', function () {

errObj.constructor.name.should.be.exactly('CacheError');
expect(errObj).to.be.an.instanceof(errors.CacheError);
});

@@ -14,5 +15,5 @@ it('Should be able to instantiate an error with the \'new\' keyword', function () {

errObj.constructor.name.should.be.exactly('CacheError');
expect(errObj).to.be.an.instanceof(errors.CacheError);
});
});
}(require('should'), require('../lib/errors')));
}(require('./chaiPromise').expect, require('../lib/errors')));

@@ -1,26 +0,27 @@

(function (should, lib, cacheInterface) {
(function (expect, lib, CacheInterface) {
'use strict';
describe('Handler Implementation', function () {
it('Shouldn\'t be able to instantiate the a handler without a handler', function () {
(function () {
expect(function () {
lib.handler();
}).should.throw('A Cache Implementation is required');
}).to.throw(Error, 'A Cache Implementation is required');
});
it('Shouldn\'t be able to instantiate a handler without options', function () {
(function () {
expect(function () {
lib.handler({});
}).should.throw('Options is required and must be an object');
}).to.throw(Error, 'Options is required and must be an object');
});
it('Shouldn\'t be able to instantiate a handler with an invalid handler', function () {
(function () {
lib.handler(new cacheInterface(), {});
}).should.throw('Missing Required Argument [keyFunc]');
expect(function () {
lib.handler(new CacheInterface(), {});
}).to.throw(Error, 'Missing Required Argument [keyFunc]');
});
it('Shouldn\'t be able to instantiate the handler without a key function', function () {
(function () {
lib.handler(new cacheInterface(), {});
}).should.throw('Missing Required Argument [keyFunc]');
expect(function () {
lib.handler(new CacheInterface(), {});
}).to.throw(Error, 'Missing Required Argument [keyFunc]');
});
it('Shouldn\'t be able to instantiate the handler with an invalid key function', function () {
(function () {
lib.handler(new cacheInterface(),
expect(function () {
lib.handler(new CacheInterface(),
{

@@ -30,3 +31,3 @@ keyFunc: 'test'

);
}).should.throw('Invalid Argument Type Expected [function] for [keyFunc] but got [string]');
}).to.throw(Error, 'Invalid Argument Type Expected [function] for [keyFunc] but got [string]');
});

@@ -41,3 +42,3 @@ it('Should instantiate the Memory handler', function () {

);
cache.constructor.name.should.be.exactly('Object');
expect(cache).to.be.ok();
});

@@ -56,3 +57,3 @@ it('Should be able to instantiate the Redis handler', function () {

);
cache.constructor.name.should.be.exactly('Object');
expect(cache).to.be.ok();
});

@@ -71,3 +72,3 @@ it('Should be able to instantiate the S3 handler', function () {

);
cache.constructor.name.should.be.exactly('Object');
expect(cache).to.be.ok();
});

@@ -89,6 +90,7 @@ it('Should be able to save a cached value', function (done) {

},
MockResponse(function (res) {
new MockResponse(function (res) {
try {
res.headers.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
res.statusCode.should.be.exactly(200);
expect(res.headers.etag).to.equal('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
expect(res.statusCode).to.equal(200);
done();

@@ -118,6 +120,6 @@ } catch (err) {

},
MockResponse(function (res) {
new MockResponse(function (res) {
try {
res.headers.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
res.statusCode.should.be.exactly(200);
expect(res.headers.etag).to.equal('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
expect(res.statusCode).to.equal(200);
done();

@@ -145,3 +147,3 @@ } catch (err) {

},
MockResponse(function () {
new MockResponse(function () {
cache.save(

@@ -156,6 +158,6 @@ {

},
MockResponse(function (res) {
new MockResponse(function (res) {
try {
res.headers.etag.should.be.exactly('8d8dbf068de76b07ecd87c58f228c8dfdce138dd');
res.statusCode.should.be.exactly(200);
expect(res.headers.etag).to.equal('8d8dbf068de76b07ecd87c58f228c8dfdce138dd');
expect(res.statusCode).to.equal(200);
done();

@@ -185,6 +187,6 @@ } catch (err) {

},
MockResponse(function (res) {
new MockResponse(function (res) {
try {
res.headers.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
res.statusCode.should.be.exactly(200);
expect(res.headers.etag).to.equal('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
expect(res.statusCode).to.equal(200);
done();

@@ -212,7 +214,7 @@ } catch (err) {

},
MockResponse(function () {
cache.restore({ headers: {} }, MockResponse(function (res) {
new MockResponse(function () {
cache.restore({headers: {}}, new MockResponse(function (res) {
try {
res.headers.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
res.statusCode.should.be.exactly(200);
expect(res.headers.etag).to.equal('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
expect(res.statusCode).to.equal(200);
done();

@@ -241,7 +243,7 @@ } catch (err) {

},
MockResponse(function () {
cache.restore({ headers: { 'if-none-match': '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0'} }, MockResponse(function (res) {
new MockResponse(function () {
cache.restore({headers: {'if-none-match': '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0'}}, new MockResponse(function (res) {
try {
res.headers.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
res.statusCode.should.be.exactly(304);
expect(res.headers.etag).to.equal('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
expect(res.statusCode).to.equal(304);
done();

@@ -270,7 +272,7 @@ } catch (err) {

},
MockResponse(function () {
cache.restore({ headers: { 'if-none-match': 'test-hash'} }, MockResponse(function (res) {
new MockResponse(function () {
cache.restore({headers: {'if-none-match': 'test-hash'}}, new MockResponse(function (res) {
try {
res.headers.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
res.statusCode.should.be.exactly(200);
expect(res.headers.etag).to.equal('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
expect(res.statusCode).to.equal(200);
done();

@@ -292,6 +294,6 @@ } catch (err) {

);
cache.restore({ headers: { 'if-none-match': 'test-hash'} }, MockResponse(function (res) {
cache.restore({headers: {'if-none-match': 'test-hash'}}, new MockResponse(function (res) {
try {
res.body.should.be.exactly('Cache for [test-restore-no-exist] not found');
res.statusCode.should.be.exactly(404);
expect(res.body).to.equal('Cache for [test-restore-no-exist] not found');
expect(res.statusCode).to.equal(404);
done();

@@ -318,6 +320,6 @@ } catch (err) {

},
MockResponse(function (data) {
cache.remove({ headers: {} }, MockResponse(function (res) {
new MockResponse(function () {
cache.remove({headers: {}}, new MockResponse(function (res) {
try {
res.statusCode.should.be.exactly(204);
expect(res.statusCode).to.equal(204);
done();

@@ -346,6 +348,6 @@ } catch (err) {

},
MockResponse(function () {
cache.remove({ headers: { 'if-none-match': '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0' } }, MockResponse(function (res) {
new MockResponse(function () {
cache.remove({headers: {'if-none-match': '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0'}}, new MockResponse(function (res) {
try {
res.statusCode.should.be.exactly(204);
expect(res.statusCode).to.equal(204);
done();

@@ -374,7 +376,7 @@ } catch (err) {

},
MockResponse(function () {
cache.remove({ headers: { 'if-none-match': 'incorrect-hash' } }, MockResponse(function (res) {
new MockResponse(function () {
cache.remove({headers: {'if-none-match': 'incorrect-hash'}}, new MockResponse(function (res) {
try {
res.body.should.be.exactly('Provided Hash [incorrect-hash] doesn\'t match current hash [4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0]');
res.statusCode.should.be.exactly(412);
expect(res.body).to.equal('Provided Hash [incorrect-hash] doesn\'t match current hash [4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0]');
expect(res.statusCode).to.equal(412);
done();

@@ -408,4 +410,4 @@ } catch (err) {

cb(this);
}
};
}
}(require('should'), require('../index'), require('../lib/cacheInterface')));
}(require('./chaiPromise').expect, require('../index'), require('../lib/cacheInterface')));

@@ -1,211 +0,8 @@

(function (should, util, lib, errors) {
describe('Memory Cache Implementation', function () {
(function (expect, lib) {
'use strict';
describe('Memory Specific Implementation', function () {
it('Should instantiate the test cache implementation', function () {
var cache = lib.cache('mem',{});
cache.constructor.name.should.be.exactly('TestCache');
expect(lib.cache('mem', {})).to.be.ok();
});
it('Should be able to save a cached value', function (done) {
var cache = lib.cache('mem',{});
cache.save(JSON.stringify({ name: 'Zul' }), 'save-test').then(function (data) {
try {
data.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
});
it('Should be able to save a cached value with a hash that doesn\'t exist', function (done) {
var cache = lib.cache('mem',{});
cache.save(JSON.stringify({ name: 'Zul' }), 'save-test-no-exist', '5bf48f033197ecd3635f459c145b0815').then(function (data) {
try {
data.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
});
it('Should be able to update a cached value', function (done) {
var cache = lib.cache('mem',{});
cache.save(JSON.stringify({ name: 'Zul' }), 'update-test').then(function (data) {
cache.save(JSON.stringify({ name: 'Odoyle Rules!' }), 'update-test', '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0').then(function (data) {
try {
data.etag.should.be.exactly('8d8dbf068de76b07ecd87c58f228c8dfdce138dd');
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Should be able to update a cached value that no longer exists', function (done) {
var cache = lib.cache('mem',{});
cache.save(JSON.stringify({ name: 'Zul' }), 'does-not-exist').then(function (data) {
try {
data.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t be able to update a cached value when you have the wrong hash', function (done) {
var cache = lib.cache('mem',{});
cache.save(JSON.stringify({ name: 'Zul' }), 'update-test').then(function (data) {
cache.save(JSON.stringify({ name: 'Odoyle Rules!' }), 'update-test', '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec01').then(function (data) {
done(new Error('This should have failed'));
}, function (reason) {
try {
var errCode = errors.errorCodes.HASH_MISMATCH;
reason.code.should.be.exactly(errCode.code);
reason.message.should.be.exactly(util.format(errCode.message, '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec01', '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0'));
reason.name.should.be.exactly(errCode.name);
done();
} catch(err) {
done(err);
}
});
}, function (reason) {
done(reason);
});
});
it('Should be able to restore a cached value', function (done) {
var cache = lib.cache('mem',{});
cache.save(JSON.stringify({ name: 'Zul' }), 'restore-test').then(function (data) {
cache.restore('restore-test').then(function (data) {
try {
data.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
data.body.should.be.exactly(JSON.stringify({ name: 'Zul' }));
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t restore the same version', function (done) {
var cache = lib.cache('mem',{});
cache.save(JSON.stringify({ name: 'Zul' }), 'restore-test').then(function (data) {
cache.restore('restore-test', '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0').then(function (data) {
try {
data.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
should.not.exist(data.body);
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Should restore if a different version exists', function (done) {
var cache = lib.cache('mem',{});
cache.save(JSON.stringify({ name: 'Zul' }), 'restore-test').then(function (data) {
cache.restore('restore-test', 'test-hash').then(function (data) {
try {
data.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
data.body.should.be.exactly(JSON.stringify({ name: 'Zul' }));
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t restore if a value doesn\'t exist', function (done) {
var cache = lib.cache('mem',{});
cache.restore('doesnt-exist').then(function (data) {
done(new Error('Should Have Returned Error'));
}, function (reason) {
try {
reason.code.should.be.exactly(2);
reason.message.should.be.exactly('Cache for [doesnt-exist] not found');
reason.name.should.be.exactly('CacheNotFound');
done();
} catch (err) {
done(err);
}
});
});
it('Should be able to remove a cached value', function (done) {
var cache = lib.cache('mem',{});
cache.save(JSON.stringify({ name: 'Zul' }), 'delete-test').then(function (data) {
cache.remove('delete-test').then(function (data) {
done();
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Should be able to remove a cached value with a hash', function (done) {
var cache = lib.cache('mem',{});
cache.save(JSON.stringify({ name: 'Zul' }), 'delete-test').then(function (data) {
cache.remove('delete-test', '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0').then(function (data) {
done();
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t be able to remove a cached value with an incorrect hash', function (done) {
var cache = lib.cache('mem',{});
cache.save(JSON.stringify({ name: 'Zul' }), 'delete-test').then(function (data) {
cache.remove('delete-test', 'incorrect-hash').then(function (data) {
done(new Error('Should Have Returned Error'));
}, function (reason) {
try {
reason.code.should.be.exactly(0);
reason.message.should.be.exactly('Provided Hash [incorrect-hash] doesn\'t match current hash [4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0]');
reason.name.should.be.exactly('HashMismatch');
done();
} catch (err) {
done(err);
}
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t be able to remove a cached value that doesn\'t exist', function (done) {
var cache = lib.cache('mem',{});
cache.remove('delete-test-no-exist').then(function (data) {
done(new Error('Should Have Returned Error'));
}, function (reason) {
try {
reason.code.should.be.exactly(2);
reason.message.should.be.exactly('Cache for [delete-test-no-exist] not found');
reason.name.should.be.exactly('CacheNotFound');
done();
} catch (err) {
done(err);
}
});
});
});
}(require('should'), require('util'), require('../index'), require('../lib/errors')));
}(require('./chaiPromise').expect, require('../index')));

@@ -1,27 +0,11 @@

(function (should, util, lib, errors, redis) {
"use strict";
var redisClient;
(function (expect, lib) {
'use strict';
describe('Redis Cache Implementation', function () {
before(function(done) {
redisClient = redis.createClient();
redisClient.on('connect', done);
redisClient.on('error', done);
});
beforeEach(function(done) {
/* jshint camelcase: false */
redisClient.send_command('flushall', [], function(err) {
done(err);
});
/* jshint camelcase: true */
});
it('Shouldn\'t be able to instantiate the Redis cache implementation without a host', function () {
(function () {
expect(function () {
lib.cache('redis', {});
}).should.throw('Missing Required Argument [host]');
}).to.throw(Error, 'Missing Required Argument [host]');
});
it('Shouldn\'t be able to instantiate the Redis cache implementation with an invalid host', function () {
(function () {
expect(function () {
lib.cache('redis',

@@ -32,6 +16,6 @@ {

);
}).should.throw('Invalid Argument Type Expected [string] for [host] but got [object]');
}).to.throw(Error, 'Invalid Argument Type Expected [string] for [host] but got [object]');
});
it('Shouldn\'t be able to instantiate the Redis cache implementation without a port', function () {
(function () {
expect(function () {
lib.cache('redis',

@@ -42,6 +26,6 @@ {

);
}).should.throw('Missing Required Argument [port]');
}).to.throw(Error, 'Missing Required Argument [port]');
});
it('Shouldn\'t be able to instantiate the Redis cache implementation with an invalid port', function () {
(function () {
expect(function () {
lib.cache('redis',

@@ -53,6 +37,6 @@ {

);
}).should.throw('Invalid Argument Type Expected [number] for [port] but got [object]');
}).to.throw(Error, 'Invalid Argument Type Expected [number] for [port] but got [object]');
});
it('Shouldn\'t be able to instantiate the Redis cache implementation without options', function () {
(function () {
expect(function () {
lib.cache('redis',

@@ -64,6 +48,6 @@ {

);
}).should.throw('Missing Required Argument [options]');
}).to.throw(Error, 'Missing Required Argument [options]');
});
it('Shouldn\'t be able to instantiate the Redis cache implementation with invalid options', function () {
(function () {
expect(function () {
lib.cache('redis',

@@ -76,3 +60,3 @@ {

);
}).should.throw('Invalid Argument Type Expected [object] for [options] but got [string]');
}).to.throw(Error, 'Invalid Argument Type Expected [object] for [options] but got [string]');
});

@@ -87,296 +71,6 @@ it('Should instantiate the Redis cache implementation', function () {

);
cache.constructor.name.should.be.exactly('RedisCache');
expect(cache).to.be.ok();
});
it('Redis cache implementation should inherit from the EventEmitter', function () {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.should.be.instanceOf(require('events').EventEmitter);
});
it('Should be able to save a cached value', function (done) {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'save-test').then(function (data) {
try {
data.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
});
it('Should be able to save a cached value with a hash that doesn\'t exist', function (done) {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'save-test-no-exist', '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0').then(function (data) {
try {
data.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
});
it('Should be able to update a cached value', function (done) {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'update-test').then(function (data) {
cache.save(JSON.stringify({ name: 'Odoyle Rules!' }), 'update-test', '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0').then(function (data) {
try {
data.etag.should.be.exactly('8d8dbf068de76b07ecd87c58f228c8dfdce138dd');
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t be able to update a cached value when you have the wrong hash', function (done) {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'update-test').then(function (data) {
cache.save(JSON.stringify({ name: 'Odoyle Rules!' }), 'update-test', '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec01').then(function (data) {
done(new Error('This should have failed'));
}, function (reason) {
try {
var errCode = errors.errorCodes.HASH_MISMATCH;
reason.code.should.be.exactly(errCode.code);
reason.message.should.be.exactly(util.format(errCode.message, '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec01', '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0'));
reason.name.should.be.exactly(errCode.name);
done();
} catch(err) {
done(err);
}
});
}, function (reason) {
done(reason);
});
});
it('Should be able to update a cached value that no longer exists', function (done) {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'does-not-exist').then(function (data) {
try {
data.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
});
it('Should be able to restore a cached value', function (done) {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'restore-test').then(function (data) {
cache.restore('restore-test').then(function (data) {
try {
data.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
data.body.should.be.exactly(JSON.stringify({ name: 'Zul' }));
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t restore the same version', function (done) {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'restore-test').then(function (data) {
cache.restore('restore-test', '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0').then(function (data) {
try {
data.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
should.not.exist(data.body);
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Should restore if a different version exists', function (done) {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'restore-test').then(function (data) {
cache.restore('restore-test', 'test-hash').then(function (data) {
try {
data.etag.should.be.exactly('4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0');
data.body.should.be.exactly(JSON.stringify({ name: 'Zul' }));
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t restore if a value doesn\'t exist', function (done) {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.restore('doesnt-exist').then(function (data) {
done(new Error('Should Have Returned Error'));
}, function (reason) {
try {
reason.code.should.be.exactly(2);
reason.message.should.be.exactly('Cache for [doesnt-exist] not found');
reason.name.should.be.exactly('CacheNotFound');
done();
} catch (err) {
done(err);
}
});
});
it('Should be able to remove a cached value', function (done) {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'delete-test').then(function (data) {
cache.remove('delete-test').then(function (data) {
done();
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Should be able to remove a cached value with a hash', function (done) {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'delete-test').then(function (data) {
cache.remove('delete-test', '4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0').then(function (data) {
done();
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t be able to remove a cached value with an incorrect hash', function (done) {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'delete-test').then(function (data) {
cache.remove('delete-test', 'incorrect-hash').then(function (data) {
done(new Error('Should Have Returned Error'));
}, function (reason) {
try {
reason.code.should.be.exactly(0);
reason.message.should.be.exactly('Provided Hash [incorrect-hash] doesn\'t match current hash [4cdbc5ffe38a19ec2fd3c1625f92c14e2e0b4ec0]');
reason.name.should.be.exactly('HashMismatch');
done();
} catch (err) {
done(err);
}
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t be able to remove a cached value that doesn\'t exist', function (done) {
var cache = lib.cache('redis',
{
host: 'localhost',
port: 6379,
options: {}
}
);
cache.remove('delete-test-no-exist').then(function (data) {
done(new Error('Should Have Returned Error'));
}, function (reason) {
try {
reason.code.should.be.exactly(2);
reason.message.should.be.exactly('Cache for [delete-test-no-exist] not found');
reason.name.should.be.exactly('CacheNotFound');
done();
} catch (err) {
done(err);
}
});
});
});
}(require('should'), require('util'), require('../index'), require('../lib/errors'), require('redis')));
}(require('./chaiPromise').expect, require('../index')));

@@ -1,42 +0,11 @@

(function (should, util, lib, errors, S3FS) {
"use strict";
var s3Credentials,
bucketName,
s3fsImpl;
describe('S3 Cache Implementation', function () {
beforeEach(function (done) {
s3Credentials = {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_KEY,
region: process.env.AWS_REGION
};
bucketName = 's3fs-cache-test-bucket-' + (Math.random() + '').slice(2, 8);
s3fsImpl = new S3FS(s3Credentials, bucketName);
s3fsImpl.create().then(function() {
done();
}, done);
});
afterEach(function (done) {
s3fsImpl.destroy().then(function () {
done();
}, function (reason) {
if (reason.code === 'NoSuchBucket') {
// If the bucket doesn't exist during cleanup we don't need to consider it an issue
done();
} else {
done(reason);
}
});
});
(function (expect, lib) {
'use strict';
describe('S3 Specific Implementation', function () {
it('Shouldn\'t be able to instantiate the S3 cache implementation without a bucket', function () {
(function () {
expect(function () {
lib.cache('s3', {});
}).should.throw('Missing Required Argument [bucket]');
}).to.throw(Error, 'Missing Required Argument [bucket]');
});
it('Shouldn\'t be able to instantiate the S3 cache implementation with an invalid bucket', function () {
(function () {
expect(function () {
lib.cache('s3',

@@ -47,6 +16,6 @@ {

);
}).should.throw('Invalid Argument Type Expected [string] for [bucket] but got [object]');
}).to.throw(Error, 'Invalid Argument Type Expected [string] for [bucket] but got [object]');
});
it('Shouldn\'t be able to instantiate the S3 cache implementation without an access key id', function () {
(function () {
expect(function () {
lib.cache('s3',

@@ -57,6 +26,6 @@ {

);
}).should.throw('Missing Required Argument [accessKeyId]');
}).to.throw(Error, 'Missing Required Argument [accessKeyId]');
});
it('Shouldn\'t be able to instantiate the S3 cache implementation with an invalid access key id', function () {
(function () {
expect(function () {
lib.cache('s3',

@@ -68,6 +37,6 @@ {

);
}).should.throw('Invalid Argument Type Expected [string] for [accessKeyId] but got [object]');
}).to.throw(Error, 'Invalid Argument Type Expected [string] for [accessKeyId] but got [object]');
});
it('Shouldn\'t be able to instantiate the S3 cache implementation without a secret access key', function () {
(function () {
expect(function () {
lib.cache('s3',

@@ -79,6 +48,6 @@ {

);
}).should.throw('Missing Required Argument [secretAccessKey]');
}).to.throw(Error, 'Missing Required Argument [secretAccessKey]');
});
it('Shouldn\'t be able to instantiate the S3 cache implementation with an invalid secret access key', function () {
(function () {
expect(function () {
lib.cache('s3',

@@ -91,3 +60,3 @@ {

);
}).should.throw('Invalid Argument Type Expected [string] for [secretAccessKey] but got [object]');
}).to.throw(Error, 'Invalid Argument Type Expected [string] for [secretAccessKey] but got [object]');
});

@@ -102,318 +71,5 @@ it('Should instantiate the S3 cache implementation', function () {

);
cache.constructor.name.should.be.exactly('S3Cache');
expect(cache.constructor.name).to.equal('S3Cache');
});
it('Should be able to save a cached value', function (done) {
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'save-test').then(function (data) {
try {
data.etag.should.be.exactly('5bf48f033197ecd3635f459c145b0815');
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
});
it('Should be able to save a cached value with a hash that doesn\'t exist', function (done) {
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'save-test-no-exist', '5bf48f033197ecd3635f459c145b0815').then(function (data) {
try {
data.etag.should.be.exactly('5bf48f033197ecd3635f459c145b0815');
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
});
it('Should be able to update a cached value', function (done) {
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'update-test').then(function (data) {
cache.save(JSON.stringify({ name: 'Odoyle Rules!' }), 'update-test', '5bf48f033197ecd3635f459c145b0815').then(function (data) {
try {
data.etag.should.be.exactly('d9b4bc4b39054b07b6f2512abcdad03f');
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t be able to update a cached value when you have the wrong hash', function (done) {
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'update-test').then(function (data) {
cache.save(JSON.stringify({ name: 'Odoyle Rules!' }), 'update-test', '5bf48f033197ecd3635f459c145b08151').then(function (data) {
done(new Error('This should have failed'));
}, function (reason) {
try {
var errCode = errors.errorCodes.HASH_MISMATCH;
reason.code.should.be.exactly(errCode.code);
reason.message.should.be.exactly(util.format(errCode.message, '5bf48f033197ecd3635f459c145b08151', '5bf48f033197ecd3635f459c145b0815'));
reason.name.should.be.exactly(errCode.name);
done();
} catch(err) {
done(err);
}
});
}, function (reason) {
done(reason);
});
});
it('Should be able to update a cached value that no longer exists', function (done) {
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'does-not-exist').then(function (data) {
try {
data.etag.should.be.exactly('5bf48f033197ecd3635f459c145b0815');
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
});
it('Should be able to restore a cached value', function (done) {
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'restore-test').then(function (data) {
cache.restore('restore-test').then(function (data) {
try {
data.etag.should.be.exactly('5bf48f033197ecd3635f459c145b0815');
data.body.should.be.exactly(JSON.stringify({ name: 'Zul' }));
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t restore the same version', function (done) {
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'restore-test').then(function (data) {
cache.restore('restore-test', '5bf48f033197ecd3635f459c145b0815').then(function (data) {
try {
data.etag.should.be.exactly('5bf48f033197ecd3635f459c145b0815');
should.not.exist(data.body);
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Should restore if a different version exists', function (done) {
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'restore-test').then(function (data) {
cache.restore('restore-test', 'test-hash').then(function (data) {
try {
data.etag.should.be.exactly('5bf48f033197ecd3635f459c145b0815');
data.body.should.be.exactly(JSON.stringify({ name: 'Zul' }));
done();
} catch (err) {
done(err);
}
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t restore if a value doesn\'t exist', function (done) {
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
cache.restore('doesnt-exist').then(function (data) {
done(new Error('Should Have Returned Error'));
}, function (reason) {
try {
reason.code.should.be.exactly(2);
reason.message.should.be.exactly('Cache for [doesnt-exist] not found');
reason.name.should.be.exactly('CacheNotFound');
done();
} catch (err) {
done(err);
}
});
});
it('Should be able to remove a cached value', function (done) {
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'delete-test').then(function (data) {
cache.remove('delete-test').then(function (data) {
done();
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Should be able to remove a cached value with a hash', function (done) {
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'delete-test').then(function (data) {
cache.remove('delete-test', '5bf48f033197ecd3635f459c145b0815').then(function (data) {
done();
}, function (reason) {
done(reason);
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t be able to remove a cached value with an incorrect hash', function (done) {
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
cache.save(JSON.stringify({ name: 'Zul' }), 'delete-test').then(function (data) {
cache.remove('delete-test', 'incorrect-hash').then(function (data) {
done(new Error('Should Have Returned Error'));
}, function (reason) {
try {
reason.code.should.be.exactly(0);
reason.message.should.be.exactly('Provided Hash [incorrect-hash] doesn\'t match current hash [5bf48f033197ecd3635f459c145b0815]');
reason.name.should.be.exactly('HashMismatch');
done();
} catch (err) {
done(err);
}
});
}, function (reason) {
done(reason);
});
});
it('Shouldn\'t be able to remove a cached value that doesn\'t exist', function (done) {
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
cache.remove('delete-test-no-exist').then(function (data) {
done(new Error('Should Have Returned Error'));
}, function (reason) {
try {
reason.code.should.be.exactly(2);
reason.message.should.be.exactly('Cache for [delete-test-no-exist] not found');
reason.name.should.be.exactly('CacheNotFound');
done();
} catch (err) {
done(err);
}
});
});
it('Should be able to save, restore, and delete a cached value with invalid characters', function (done) {
// See http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#object-keys for where these characters came from
var cache = lib.cache('s3',
{
bucket: bucketName,
accessKeyId: s3Credentials.accessKeyId,
secretAccessKey: s3Credentials.secretAccessKey
}
);
var dataToCache = JSON.stringify({ name: 'Zul' }),
key = '*$@=;:+ ,?\\{^}%`]\"\'>[~<#|';
cache.save(dataToCache, key).then(function () {
return cache.restore(key).then(function (data) {
data.body.should.be.exactly(dataToCache);
return cache.remove(key).then(function() {
return cache.restore(key).then(function () {
done(new Error('Should Have Returned Error'));
}, function (reason) {
try {
reason.code.should.be.exactly(2);
reason.name.should.be.exactly('CacheNotFound');
done();
} catch (err) {
done(err);
}
});
});
});
}, function (reason) {
done(reason);
});
});
});
}(require('should'), require('util'), require('../index'), require('../lib/errors'), require('s3fs')));
}(require('./chaiPromise').expect, require('../index')));

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc