connect-session-knex
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -8,229 +8,234 @@ var knex = require('knex'); | ||
/** | ||
* Connect's Store. | ||
*/ | ||
var Store = (connect.session) ? connect.session.Store : connect.Store; | ||
/** | ||
* Connect's Store. | ||
*/ | ||
var Store = (connect.session) ? connect.session.Store : connect.Store; | ||
/** | ||
* Return an ISO compliant string of the current time | ||
* @api private | ||
* @return {String} an ISO compliant string of the current time | ||
*/ | ||
function nowAsISO() { | ||
return (new Date()).toISOString(); | ||
} | ||
/** | ||
* Return an ISO compliant string of the current time | ||
* @api private | ||
* @return {String} an ISO compliant string of the current time | ||
*/ | ||
function nowAsISO() { | ||
return (new Date()).toISOString(); | ||
} | ||
/** | ||
* Remove expired sessions from database. | ||
* @param {Object} store | ||
* @api private | ||
*/ | ||
function dbCleanup(store) { | ||
return store.ready.then(function () { | ||
store.knex(store.tablename).del().whereRaw('"expired" < CAST(? as timestamp)', nowAsISO()); | ||
}); | ||
} | ||
/** | ||
* Remove expired sessions from database. | ||
* @param {Object} store | ||
* @api private | ||
*/ | ||
function dbCleanup(store) { | ||
return store.ready.then(function () { | ||
store.knex(store.tablename).del().whereRaw('"expired" < CAST(? as timestamp)', nowAsISO()); | ||
}); | ||
} | ||
/** | ||
* Initialize KnexStore with the given options. | ||
* | ||
* @param {Object} options | ||
* @api public | ||
*/ | ||
function KnexStore(options) { | ||
var that = this; | ||
/** | ||
* Initialize KnexStore with the given options. | ||
* | ||
* @param {Object} options | ||
* @api public | ||
*/ | ||
function KnexStore(options) { | ||
var that = this; | ||
options = options || {}; | ||
Store.call(this, options); | ||
options = options || {}; | ||
Store.call(this, options); | ||
this.tablename = options.tablename || 'sessions'; | ||
this.knex = options.knex || require('knex')({ | ||
client: 'sqlite3', | ||
// debug: true, | ||
connection: { | ||
filename: "connect-session-knex.sqlite" | ||
} | ||
}); | ||
this.tablename = options.tablename || 'sessions'; | ||
this.knex = options.knex || require('knex')({ | ||
client: 'sqlite3', | ||
// debug: true, | ||
connection: { | ||
filename: "connect-session-knex.sqlite" | ||
} | ||
}); | ||
this.ready = that.knex.schema.hasTable(that.tablename).then(function (exists) { | ||
if (!exists) { | ||
return that.knex.schema.createTable(that.tablename, function (table) { | ||
table.string('sid').primary(); | ||
table.json('sess').notNullable(); | ||
table.timestamp('expired', 'true').notNullable(); | ||
}); | ||
} | ||
}) | ||
.then(function () { | ||
dbCleanup(that); | ||
setInterval(dbCleanup, oneDay, that).unref(); | ||
}); | ||
} | ||
this.ready = that.knex.schema.hasTable(that.tablename) | ||
.then(function (exists) { | ||
if (!exists) { | ||
return that.knex.schema.createTable(that.tablename, function (table) { | ||
table.string('sid').primary(); | ||
table.json('sess').notNullable(); | ||
table.timestamp('expired', 'true').notNullable(); | ||
}); | ||
} | ||
}) | ||
.then(function () { | ||
dbCleanup(that); | ||
setInterval(dbCleanup, oneDay, that).unref(); | ||
}); | ||
} | ||
// KnexStore.prototype.__proto__ = Store.prototype; | ||
util.inherits(KnexStore, Store); | ||
// KnexStore.prototype.__proto__ = Store.prototype; | ||
util.inherits(KnexStore, Store); | ||
/** | ||
* Attempt to fetch session by the given sid. | ||
* | ||
* @param {String} sid | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
KnexStore.prototype.get = function(sid, fn) { | ||
var that = this; | ||
return that.ready.then(function () { | ||
return that.knex | ||
.select('sess') | ||
.from(that.tablename) | ||
.where('sid', '=', sid) | ||
.andWhereRaw('CAST(? as timestamp) <= "expired"', nowAsISO()) | ||
.then(function (response) { | ||
if (fn) { | ||
if (response[0]) { | ||
var sess = response[0].sess; | ||
if(typeof(sess) === "string") { | ||
sess = JSON.parse(sess); | ||
} | ||
fn(null, sess); | ||
} else{ | ||
fn(); | ||
} | ||
} | ||
}).catch(function(err) { | ||
fn(err); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Attempt to fetch session by the given sid. | ||
* | ||
* @param {String} sid | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
KnexStore.prototype.get = function(sid, fn) { | ||
var that = this; | ||
return that.ready.then(function () { | ||
return that.knex | ||
.select('sess') | ||
.from(that.tablename) | ||
.where('sid', '=', sid) | ||
.andWhereRaw('CAST(? as timestamp) <= "expired"', nowAsISO()) | ||
.then(function (response) { | ||
if (fn) { | ||
if (response[0]) { | ||
var sess = response[0].sess; | ||
if(typeof(sess) === "string") { | ||
sess = JSON.parse(sess); | ||
} | ||
fn(null, sess); | ||
} else{ | ||
fn(); | ||
} | ||
} | ||
}).catch(function(err) { | ||
fn(err); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Commit the given `sess` object associated with the given `sid`. | ||
* | ||
* @param {String} sid | ||
* @param {Session} sess | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
KnexStore.prototype.set = function(sid, sess, fn) { | ||
var that = this; | ||
var maxAge = sess.cookie.maxAge; | ||
var now = new Date().getTime(); | ||
var expired = maxAge ? now + maxAge : now + oneDay; | ||
sess = JSON.stringify(sess); | ||
var postgresfastq = 'with new_values (sid, expired, sess) as (' + | ||
' values ($1, $2::timestamp without time zone, $3::json)' + | ||
'), ' + | ||
'upsert as ' + | ||
'( ' + | ||
' update ' + that.tablename + ' cs set ' + | ||
' sid = nv.sid, ' + | ||
' expired = nv.expired, ' + | ||
' sess = nv.sess ' + | ||
' from new_values nv ' + | ||
' where cs.sid = nv.sid ' + | ||
' returning cs.* ' + | ||
')' + | ||
'insert into ' + that.tablename + ' (sid, expired, sess) ' + | ||
'select sid, expired, sess ' + | ||
'from new_values ' + | ||
'where not exists (select 1 from upsert up where up.sid = new_values.sid)'; | ||
/** | ||
* Commit the given `sess` object associated with the given `sid`. | ||
* | ||
* @param {String} sid | ||
* @param {Session} sess | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
KnexStore.prototype.set = function(sid, sess, fn) { | ||
var that = this; | ||
var maxAge = sess.cookie.maxAge; | ||
var now = new Date().getTime(); | ||
var expired = maxAge ? now + maxAge : now + oneDay; | ||
sess = JSON.stringify(sess); | ||
var postgresfastq = 'with new_values (sid, expired, sess) as (' + | ||
' values ($1, $2::timestamp without time zone, $3::json)' + | ||
'), ' + | ||
'upsert as ' + | ||
'( ' + | ||
' update ' + that.tablename + ' cs set ' + | ||
' sid = nv.sid, ' + | ||
' expired = nv.expired, ' + | ||
' sess = nv.sess ' + | ||
' from new_values nv ' + | ||
' where cs.sid = nv.sid ' + | ||
' returning cs.* ' + | ||
')' + | ||
'insert into ' + that.tablename + ' (sid, expired, sess) ' + | ||
'select sid, expired, sess ' + | ||
'from new_values ' + | ||
'where not exists (select 1 from upsert up where up.sid = new_values.sid)'; | ||
if ('postgresql' === that.knex.client.dialect) { | ||
// postgresql optimized query | ||
return that.ready.then(function () { | ||
return that.knex.raw(postgresfastq, [sid, new Date(expired), sess ]) | ||
.then(function (result) { | ||
if (fn) { | ||
fn(null, result); | ||
} | ||
}); | ||
}); | ||
} else { | ||
return that.ready.then(function () { | ||
return that.knex.transaction(function (trx) { | ||
return trx.select('*').forUpdate().from(that.tablename).where('sid', '=', sid) | ||
.then(function (foundKeys) { | ||
if (foundKeys.length == 0) { | ||
return trx.from(that.tablename) | ||
.insert({ | ||
sid: sid, | ||
expired: new Date(expired), | ||
sess: sess | ||
}); | ||
} else { | ||
return trx(that.tablename) | ||
.where('sid', '=', sid) | ||
.update({ | ||
expired: new Date(expired), | ||
sess: sess | ||
}); | ||
} | ||
}) | ||
.then(function (res) { | ||
if (fn) { | ||
fn(null, res); | ||
} | ||
return res; | ||
}); | ||
}); | ||
}); | ||
} | ||
if ('postgresql' === that.knex.client.dialect) { | ||
// postgresql optimized query | ||
return that.ready.then(function () { | ||
return that.knex.raw(postgresfastq, [sid, new Date(expired), sess ]) | ||
.then(function (result) { | ||
if (fn) { | ||
fn(null, result); | ||
} | ||
}); | ||
}); | ||
} else { | ||
return that.ready.then(function () { | ||
return that.knex.transaction(function (trx) { | ||
return trx.select('*').forUpdate().from(that.tablename).where('sid', '=', sid) | ||
.then(function (foundKeys) { | ||
if (foundKeys.length == 0) { | ||
return trx.from(that.tablename) | ||
.insert({ | ||
sid: sid, | ||
expired: new Date(expired), | ||
sess: sess | ||
}); | ||
} else { | ||
return trx(that.tablename) | ||
.where('sid', '=', sid) | ||
.update({ | ||
expired: new Date(expired), | ||
sess: sess | ||
}); | ||
} | ||
}) | ||
}) | ||
.then(function (res) { | ||
if (fn) { | ||
fn(null, res); | ||
} | ||
return res; | ||
}) | ||
.catch(function(err) { | ||
fn(err); | ||
}); | ||
}); | ||
} | ||
}; | ||
/** | ||
* Destroy the session associated with the given `sid`. | ||
* | ||
* @param {String} sid | ||
* @api public | ||
*/ | ||
KnexStore.prototype.destroy = function(sid, fn) { | ||
var that = this; | ||
return that.ready.then(function () { | ||
return that.knex.del().from(that.tablename).where('sid', '=', sid).then(function (response) { | ||
if (fn) { | ||
fn(null, true); | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Destroy the session associated with the given `sid`. | ||
* | ||
* @param {String} sid | ||
* @api public | ||
*/ | ||
KnexStore.prototype.destroy = function(sid, fn) { | ||
var that = this; | ||
return that.ready.then(function () { | ||
return that.knex.del().from(that.tablename).where('sid', '=', sid).then(function (response) { | ||
if (fn) { | ||
fn(null, true); | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Fetch number of sessions. | ||
* | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
KnexStore.prototype.length = function(fn) { | ||
var that=this; | ||
return that.ready.then(function () { | ||
return that.knex.count('sid as count').from(that.tablename).then(function (response) { | ||
if (fn) { | ||
fn(null, response[0].count); | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Fetch number of sessions. | ||
* | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
KnexStore.prototype.length = function(fn) { | ||
var that=this; | ||
return that.ready.then(function () { | ||
return that.knex.count('sid as count').from(that.tablename).then(function (response) { | ||
if (fn) { | ||
fn(null, response[0].count); | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Clear all sessions. | ||
* | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
KnexStore.prototype.clear = function(fn) { | ||
var that=this; | ||
return that.ready.then(function () { | ||
return that.knex.del().from(that.tablename).then(function (response) { | ||
if (fn) { | ||
fn(null, true); | ||
} | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Clear all sessions. | ||
* | ||
* @param {Function} fn | ||
* @api public | ||
*/ | ||
KnexStore.prototype.clear = function(fn) { | ||
var that=this; | ||
return that.ready.then(function () { | ||
return that.knex.del().from(that.tablename).then(function (response) { | ||
if (fn) { | ||
fn(null, true); | ||
} | ||
}); | ||
}); | ||
}; | ||
return KnexStore; | ||
return KnexStore; | ||
}; |
{ | ||
"name": "connect-session-knex", | ||
"description": "Knex.js session store for Connect", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"main": "lib/connect-session-knex", | ||
"dependencies": { | ||
"knex": "^0.6.21" | ||
"knex": "^0.7.3" | ||
}, | ||
"devDependencies": { | ||
"connect": "^3.0.2", | ||
"express-session": "^1.6.4", | ||
"pg": "^3.3.0", | ||
"should": "^4.0.4", | ||
"sqlite3": "^2.2.3" | ||
}, | ||
"engines": { | ||
@@ -30,3 +23,8 @@ "node": ">=0.4.x" | ||
}, | ||
"license": "MIT" | ||
"license": "ISC", | ||
"devDependencies": { | ||
"connect": "^3.3.3", | ||
"express-session": "^1.9.3", | ||
"should": "^4.3.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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
3
0
11992
8
290
+ Addedansi-regex@0.2.1(transitive)
+ Addedansi-styles@1.1.0(transitive)
+ Addedbluebird@2.11.0(transitive)
+ Addedchalk@0.5.1(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedextend@1.3.0(transitive)
+ Addedflagged-respawn@0.3.2(transitive)
+ Addedhas-ansi@0.1.0(transitive)
+ Addedknex@0.7.6(transitive)
+ Addedliftoff@0.13.6(transitive)
+ Addedminimist@1.1.3(transitive)
+ Addedresolve@1.0.0(transitive)
+ Addedstrip-ansi@0.3.0(transitive)
+ Addedsupports-color@0.2.0(transitive)
+ Addedtildify@1.0.0(transitive)
+ Addeduser-home@1.1.1(transitive)
- Removedansi-styles@1.0.0(transitive)
- Removedbluebird@1.2.4(transitive)
- Removedchalk@0.4.0(transitive)
- Removedextend@1.2.1(transitive)
- Removedhas-color@0.1.7(transitive)
- Removedknex@0.6.23(transitive)
- Removedliftoff@0.11.3(transitive)
- Removedminimist@0.0.100.1.0(transitive)
- Removedresolve@0.7.4(transitive)
- Removedsemver@2.3.2(transitive)
- Removedstrip-ansi@0.1.1(transitive)
- Removedtildify@0.2.0(transitive)
Updatedknex@^0.7.3