sessionstore
Advanced tools
Comparing version 0.4.2 to 0.4.3
var util = require(process.binding('natives').util ? 'util' : 'sys'), | ||
Session = require('express').session, | ||
Session = require('../library').getExpressSession(), | ||
_ = require('lodash'), | ||
@@ -7,3 +7,3 @@ async = require('async'), | ||
var CouchDbSessionStore = function(options, callback) { | ||
var CouchDbSessionStore = function (options, callback) { | ||
options = options || {}; | ||
@@ -18,3 +18,3 @@ Session.Store.call(this, options); | ||
}; | ||
_.defaults(options, defaults); | ||
@@ -34,3 +34,3 @@ | ||
var self = this; | ||
var client = new(cradle.Connection)(options.host, options.port, options.options); | ||
var client = new (cradle.Connection)(options.host, options.port, options.options); | ||
var db = client.database(options.dbName); | ||
@@ -44,3 +44,3 @@ db.exists(function (err, exists) { | ||
db.get('_design/collection', function(err, obj) { | ||
db.get('_design/collection', function (err, obj) { | ||
@@ -50,3 +50,3 @@ var view = { | ||
findAll: { | ||
map: function(doc) { | ||
map: function (doc) { | ||
emit(doc.collectionName, doc); | ||
@@ -59,10 +59,16 @@ } | ||
if (err && err.error == 'not_found') { | ||
db.save('_design/collection', view, function(err) { | ||
if (callback) { return callback(null); } | ||
db.save('_design/collection', view, function (err) { | ||
if (callback) { | ||
return callback(null); | ||
} | ||
}); | ||
} else if (err) { | ||
if (callback) { return callback(err); } | ||
if (callback) { | ||
return callback(err); | ||
} | ||
} else { | ||
db.save('_design/collection', obj._rev, view, function(err) { | ||
if (callback) { return callback(null); } | ||
db.save('_design/collection', obj._rev, view, function (err) { | ||
if (callback) { | ||
return callback(null); | ||
} | ||
}); | ||
@@ -75,5 +81,7 @@ } | ||
if (err) { | ||
if (callback) { return callback(err); } | ||
if (callback) { | ||
return callback(err); | ||
} | ||
} else if (!exists) { | ||
db.create(function(err) { | ||
db.create(function (err) { | ||
finish(); | ||
@@ -98,14 +106,14 @@ }); | ||
CouchDbSessionStore.prototype.destroy = function(hash, callback) { | ||
CouchDbSessionStore.prototype.destroy = function (hash, callback) { | ||
this.db.remove(hash, callback); | ||
}; | ||
CouchDbSessionStore.prototype.all = function(callback) { | ||
this.db.view('collection/findAll', { key: this.collectionName }, function(err, docs) { | ||
CouchDbSessionStore.prototype.all = function (callback) { | ||
this.db.view('collection/findAll', { key: this.collectionName }, function (err, docs) { | ||
var res = []; | ||
for(var i = 0, len = docs.length; i < len; i++){ | ||
for (var i = 0, len = docs.length; i < len; i++) { | ||
var obj = docs[i].value; | ||
obj.id = obj._id; | ||
var found = _.find(res, function(r) { | ||
var found = _.find(res, function (r) { | ||
return r.id === obj.id; | ||
@@ -123,4 +131,4 @@ }); | ||
CouchDbSessionStore.prototype.length = function(callback) { | ||
this.all(function(err, res) { | ||
CouchDbSessionStore.prototype.length = function (callback) { | ||
this.all(function (err, res) { | ||
callback(res.length); | ||
@@ -130,8 +138,8 @@ }); | ||
CouchDbSessionStore.prototype.clear = function(callback) { | ||
CouchDbSessionStore.prototype.clear = function (callback) { | ||
var self = this; | ||
this.all(function(err, results) { | ||
async.forEach(results, function(item, clb) { | ||
this.all(function (err, results) { | ||
async.forEach(results, function (item, clb) { | ||
self.db.remove(item._id, item._rev, clb); | ||
}, function(err) { | ||
}, function (err) { | ||
callback(err); | ||
@@ -138,0 +146,0 @@ }); |
var util = require(process.binding('natives').util ? 'util' : 'sys'), | ||
express = require('express'), | ||
Session = express.session, | ||
_ = require('lodash'), | ||
memjs = require('memjs'); | ||
Session = require('../library').getExpressSession(), | ||
_ = require('lodash'), | ||
memjs = require('memjs'); | ||
var MemcachedSessionStore = function(options, callback) { | ||
var MemcachedSessionStore = function (options, callback) { | ||
options = options || {}; | ||
@@ -38,3 +37,3 @@ Session.Store.call(this, options); | ||
if (callback) callback(null, this); | ||
}; | ||
@@ -55,3 +54,3 @@ | ||
this.client.set(prefixedSid, sessString, function(err, res) { | ||
this.client.set(prefixedSid, sessString, function (err, res) { | ||
if (callback) callback(err, res); | ||
@@ -64,3 +63,3 @@ }); | ||
this.client.get(prefixedSid, function(err, value, key) { | ||
this.client.get(prefixedSid, function (err, value, key) { | ||
if (err) { | ||
@@ -71,3 +70,3 @@ if (callback) callback(err); | ||
if (!value){ | ||
if (!value) { | ||
if (callback) callback(null, null); | ||
@@ -90,6 +89,6 @@ return; | ||
MemcachedSessionStore.prototype.destroy = function(sid, callback){ | ||
MemcachedSessionStore.prototype.destroy = function (sid, callback) { | ||
var prefixedSid = this.prefix + sid; | ||
this.client.delete(prefixedSid, function(err, res) { | ||
this.client.delete(prefixedSid, function (err, res) { | ||
if (callback) callback(err, res); | ||
@@ -96,0 +95,0 @@ }); |
var util = require(process.binding('natives').util ? 'util' : 'sys'), | ||
Session = require('express').session, | ||
Session = require('../library').getExpressSession(), | ||
mongo = require('mongodb'), | ||
@@ -16,3 +16,3 @@ ObjectID = mongo.BSONPure.ObjectID, | ||
} | ||
} | ||
@@ -22,3 +22,3 @@ return data; | ||
var MongoSessionStore = function(options, callback) { | ||
var MongoSessionStore = function (options, callback) { | ||
options = options || {}; | ||
@@ -37,3 +37,3 @@ Session.Store.call(this, options); | ||
}; | ||
_.defaults(options, defaults); | ||
@@ -50,3 +50,3 @@ | ||
setInterval(function(){ | ||
setInterval(function () { | ||
self.reap(options.maxAge); | ||
@@ -56,7 +56,7 @@ }, options.reapInterval); | ||
var server = new mongo.Server(options.host, options.port, options.options); | ||
new mongo.Db(options.dbName , server, { safe: true }).open(function(err, client) { | ||
new mongo.Db(options.dbName, server, { safe: true }).open(function (err, client) { | ||
if (err) { | ||
if (callback) callback(err); | ||
} else { | ||
var finish = function(err) { | ||
var finish = function (err) { | ||
self.client = client; | ||
@@ -78,10 +78,14 @@ self.sessions = new mongo.Collection(client, options.collectionName); | ||
MongoSessionStore.prototype.reap = function(ms, callback) { | ||
MongoSessionStore.prototype.reap = function (ms, callback) { | ||
var thresh = Number(new Date(Number(new Date()) - ms)); | ||
this.sessions.remove({ '$or': [{ "lastAccess" : { "$lt" : thresh }}, { "lastAccess" : { "$exists" : false }}] }, callback || function() {}); | ||
this.sessions.remove({ '$or': [ | ||
{ "lastAccess": { "$lt": thresh }}, | ||
{ "lastAccess": { "$exists": false }} | ||
] }, callback || function () { | ||
}); | ||
}; | ||
MongoSessionStore.prototype.set = function(sid, sess, callback) { | ||
MongoSessionStore.prototype.set = function (sid, sess, callback) { | ||
var self = this; | ||
this.sessions.findOne({ _sessionid: sid }, function(err, session_data) { | ||
this.sessions.findOne({ _sessionid: sid }, function (err, session_data) { | ||
if (err) { | ||
@@ -96,3 +100,3 @@ if (callback) callback(err); | ||
} | ||
self.sessions[method](sess, function(err, document) { | ||
self.sessions[method](sess, function (err, document) { | ||
if (err) { | ||
@@ -108,4 +112,4 @@ if (callback) callback(err); | ||
MongoSessionStore.prototype.get = function(sid, callback) { | ||
this.sessions.findOne({ _sessionid: sid }, function(err, session_data) { | ||
MongoSessionStore.prototype.get = function (sid, callback) { | ||
this.sessions.findOne({ _sessionid: sid }, function (err, session_data) { | ||
if (err) { | ||
@@ -122,17 +126,19 @@ if (callback) callback(err); | ||
MongoSessionStore.prototype.destroy = function(sid, callback) { | ||
this.sessions.remove({ _sessionid: sid }, callback || function() {}); | ||
MongoSessionStore.prototype.destroy = function (sid, callback) { | ||
this.sessions.remove({ _sessionid: sid }, callback || function () { | ||
}); | ||
}; | ||
MongoSessionStore.prototype.length = function(callback) { | ||
this.sessions.count(callback || function() {}); | ||
MongoSessionStore.prototype.length = function (callback) { | ||
this.sessions.count(callback || function () { | ||
}); | ||
}; | ||
MongoSessionStore.prototype.all = function(callback) { | ||
MongoSessionStore.prototype.all = function (callback) { | ||
var arr = []; | ||
this.sessions.find(function(err, cursor){ | ||
cursor.each(function(err, d) { | ||
this.sessions.find(function (err, cursor) { | ||
cursor.each(function (err, d) { | ||
d = cleanSessionData(d); | ||
arr.push(d); | ||
if(!d._id){ | ||
if (!d._id) { | ||
if (callback) callback(arr); | ||
@@ -144,6 +150,7 @@ } | ||
MongoSessionStore.prototype.clear = function(callback) { | ||
this.sessions.remove(callback || function() {}); | ||
MongoSessionStore.prototype.clear = function (callback) { | ||
this.sessions.remove(callback || function () { | ||
}); | ||
}; | ||
module.exports = MongoSessionStore; |
var util = require(process.binding('natives').util ? 'util' : 'sys'), | ||
Session = require('express').session, | ||
Session = require('../library').getExpressSession(), | ||
_ = require('lodash'), | ||
nStore = require('nstore'); | ||
var NStoreSessionStore = function(options, callback) { | ||
var NStoreSessionStore = function (options, callback) { | ||
options = options || {}; | ||
@@ -14,3 +14,3 @@ Session.Store.call(this, options); | ||
}; | ||
_.defaults(options, defaults); | ||
@@ -31,16 +31,16 @@ | ||
NStoreSessionStore.prototype.get = function (hash, callback) { | ||
this.db.get(hash, function(err, data, meta){ | ||
if(err instanceof Error) { | ||
callback(); | ||
}else{ | ||
callback(null, data, meta); | ||
} | ||
}); | ||
this.db.get(hash, function (err, data, meta) { | ||
if (err instanceof Error) { | ||
callback(); | ||
} else { | ||
callback(null, data, meta); | ||
} | ||
}); | ||
}; | ||
NStoreSessionStore.prototype.destroy = function(hash, callback) { | ||
NStoreSessionStore.prototype.destroy = function (hash, callback) { | ||
this.db.remove(hash, callback); | ||
}; | ||
NStoreSessionStore.prototype.length = function(callback) { | ||
NStoreSessionStore.prototype.length = function (callback) { | ||
process.nextTick(function () { | ||
@@ -51,7 +51,10 @@ callback(this.db.length); | ||
NStoreSessionStore.prototype.clear = function(callback) { | ||
NStoreSessionStore.prototype.clear = function (callback) { | ||
var count = this.db.length; | ||
Object.keys(this.db.index).forEach(function (key) { | ||
this.remove(key, function (err) { | ||
if (err) { callback(err); return; } | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
count--; | ||
@@ -58,0 +61,0 @@ if (count === 0) { |
var util = require(process.binding('natives').util ? 'util' : 'sys'), | ||
express = require('express'), | ||
Session = express.session, | ||
_ = require('lodash'), | ||
redis = require('redis'); | ||
Session = require('../library').getExpressSession(), | ||
_ = require('lodash'), | ||
redis = require('redis'); | ||
var RedisSessionStore = function(options, callback) { | ||
var RedisSessionStore = function (options, callback) { | ||
options = options || {}; | ||
@@ -16,3 +15,3 @@ Session.Store.call(this, options); | ||
port: 6379, | ||
prefix:'sess', | ||
prefix: 'sess', | ||
ttl: 804600 | ||
@@ -31,3 +30,3 @@ }; | ||
this.client.select(options.db); | ||
this.client.on("connect", function() { | ||
this.client.on("connect", function () { | ||
self.client.send_anyways = true; | ||
@@ -56,3 +55,3 @@ self.client.select(options.db); | ||
}); | ||
}; | ||
@@ -69,4 +68,4 @@ | ||
this.client.setex(prefixedSid, ttl, sess, function(err, result){ | ||
if(callback){ | ||
this.client.setex(prefixedSid, ttl, sess, function (err, result) { | ||
if (callback) { | ||
callback(err, result); | ||
@@ -76,3 +75,3 @@ } | ||
} catch (err) { | ||
if(callback){ | ||
if (callback) { | ||
callback(err); | ||
@@ -86,5 +85,5 @@ } | ||
this.client.get(prefixedSid, function(err, data){ | ||
this.client.get(prefixedSid, function (err, data) { | ||
if (err) { | ||
if(callback){ | ||
if (callback) { | ||
callback(err); | ||
@@ -94,4 +93,4 @@ } | ||
} | ||
if (!data){ | ||
if(callback){ | ||
if (!data) { | ||
if (callback) { | ||
callback(null, null); | ||
@@ -107,3 +106,3 @@ } | ||
} catch (error) { | ||
if(callback){ | ||
if (callback) { | ||
callback(error); | ||
@@ -114,3 +113,3 @@ } | ||
if(callback){ | ||
if (callback) { | ||
callback(null, result); | ||
@@ -121,3 +120,3 @@ } | ||
RedisSessionStore.prototype.destroy = function(sid, callback){ | ||
RedisSessionStore.prototype.destroy = function (sid, callback) { | ||
var prefixedSid = this.prefix + sid; | ||
@@ -124,0 +123,0 @@ this.client.del(prefixedSid, callback); |
var util = require(process.binding('natives').util ? 'util' : 'sys'), | ||
Session = require('express').session, | ||
Session = require('../library').getExpressSession(), | ||
tingodb = require('tingodb')(), | ||
@@ -16,3 +16,3 @@ ObjectID = tingodb.ObjectID, | ||
} | ||
} | ||
@@ -22,3 +22,3 @@ return data; | ||
var TingoSessionStore = function(options, callback) { | ||
var TingoSessionStore = function (options, callback) { | ||
options = options || {}; | ||
@@ -35,6 +35,6 @@ Session.Store.call(this, options); | ||
}; | ||
_.defaults(options, defaults); | ||
setInterval(function(){ | ||
setInterval(function () { | ||
self.reap(options.maxAge); | ||
@@ -53,10 +53,14 @@ }, options.reapInterval); | ||
TingoSessionStore.prototype.reap = function(ms, callback) { | ||
TingoSessionStore.prototype.reap = function (ms, callback) { | ||
var thresh = Number(new Date(Number(new Date()) - ms)); | ||
this.sessions.remove({ '$or': [{ "lastAccess" : { "$lt" : thresh }}, { "lastAccess" : { "$exists" : false }}] }, callback || function() {}); | ||
this.sessions.remove({ '$or': [ | ||
{ "lastAccess": { "$lt": thresh }}, | ||
{ "lastAccess": { "$exists": false }} | ||
] }, callback || function () { | ||
}); | ||
}; | ||
TingoSessionStore.prototype.set = function(sid, sess, callback) { | ||
TingoSessionStore.prototype.set = function (sid, sess, callback) { | ||
var self = this; | ||
this.sessions.findOne({ _sessionid: sid }, function(err, session_data) { | ||
this.sessions.findOne({ _sessionid: sid }, function (err, session_data) { | ||
if (err) { | ||
@@ -71,3 +75,3 @@ if (callback) callback(err); | ||
} | ||
self.sessions[method](sess, function(err, document) { | ||
self.sessions[method](sess, function (err, document) { | ||
if (err) { | ||
@@ -83,4 +87,4 @@ if (callback) callback(err); | ||
TingoSessionStore.prototype.get = function(sid, callback) { | ||
this.sessions.findOne({ _sessionid: sid }, function(err, session_data) { | ||
TingoSessionStore.prototype.get = function (sid, callback) { | ||
this.sessions.findOne({ _sessionid: sid }, function (err, session_data) { | ||
if (err) { | ||
@@ -97,14 +101,14 @@ if (callback) callback(err); | ||
TingoSessionStore.prototype.destroy = function(sid, callback) { | ||
TingoSessionStore.prototype.destroy = function (sid, callback) { | ||
this.sessions.remove({ _sessionid: sid }, callback); | ||
}; | ||
TingoSessionStore.prototype.length = function(callback) { | ||
TingoSessionStore.prototype.length = function (callback) { | ||
this.sessions.count(callback); | ||
}; | ||
TingoSessionStore.prototype.all = function(callback) { | ||
TingoSessionStore.prototype.all = function (callback) { | ||
var arr = []; | ||
this.sessions.find(function(err, cursor) { | ||
cursor.each(function(d) { | ||
this.sessions.find(function (err, cursor) { | ||
cursor.each(function (d) { | ||
d = cleanSessionData(d); | ||
@@ -117,4 +121,4 @@ arr.push(d); | ||
TingoSessionStore.prototype.clear = function(callback) { | ||
this.sessions.remove(function(err) { | ||
TingoSessionStore.prototype.clear = function (callback) { | ||
this.sessions.remove(function (err) { | ||
if (callback) callback(err); | ||
@@ -121,0 +125,0 @@ }); |
@@ -0,1 +1,3 @@ | ||
var library = require('../lib/library'); | ||
function getSpecificStore(options) { | ||
@@ -5,3 +7,3 @@ options = options || { type: 'inMemory' }; | ||
if (options.type === 'inMemory') { | ||
return require('express').session.MemoryStore; | ||
return library.getExpressSession().MemoryStore; | ||
} else { | ||
@@ -27,3 +29,3 @@ | ||
} else { | ||
return require('express').session.MemoryStore; | ||
return library.getExpressSession().MemoryStore; | ||
} | ||
@@ -30,0 +32,0 @@ } |
{ | ||
"author": "adrai", | ||
"name": "sessionstore", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"private": false, | ||
@@ -20,2 +20,3 @@ "main": "index.js", | ||
"express": ">= 2.5.0", | ||
"express-session": ">= 1.0.0", | ||
"memjs": ">= 0.8.4", | ||
@@ -22,0 +23,0 @@ "mocha": ">= 1.0.1", |
@@ -85,2 +85,3 @@ # Introduction | ||
# Database Support | ||
@@ -87,0 +88,0 @@ Currently these databases are supported: |
@@ -5,2 +5,11 @@ var expect = require('expect.js'), | ||
describe('SessionStore', function() { | ||
describe('calling library.getExpressSession()', function() { | ||
it('should return a valid session function', function(){ | ||
var library = require('../lib/library'); | ||
var session = library.getExpressSession(); | ||
expect(session).to.be.a('function'); | ||
}); | ||
}); | ||
describe('calling createSessionStore', function() { | ||
@@ -10,3 +19,3 @@ describe('without options', function() { | ||
var ss = sessionStore.createSessionStore(); | ||
expect(ss).to.be.a(require('express').session.MemoryStore); | ||
expect(ss).to.be.a('object'); | ||
}); | ||
@@ -16,3 +25,3 @@ }); | ||
describe('with options containing a type property with the value of', function() { | ||
describe('an existing db implementation', function() { | ||
describe('an existing db implementation of mongoDb', function() { | ||
it('it should return a new store', function() { | ||
@@ -52,2 +61,3 @@ var ss = sessionStore.createSessionStore({ type: 'mongoDb' }); | ||
expect(result).to.be(1); | ||
done(); | ||
@@ -90,3 +100,3 @@ }); | ||
expect(err).to.be(null); | ||
expect(result).to.be(1); | ||
expect(result).to.be.ok(); | ||
done(); | ||
@@ -105,3 +115,4 @@ }); | ||
var ss = sessionStore.createSessionStore({ type: 'strangeDb' }); | ||
expect(ss).to.be.a(require('express').session.MemoryStore); | ||
expect(ss).to.be.a('object'); | ||
}); | ||
@@ -108,0 +119,0 @@ }); |
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
31657
16
716
117
8
9