diff options
Diffstat (limited to 'node_modules/connect-mongo/src')
| -rw-r--r-- | node_modules/connect-mongo/src/index.js | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/node_modules/connect-mongo/src/index.js b/node_modules/connect-mongo/src/index.js index 78e3141..a211770 100644 --- a/node_modules/connect-mongo/src/index.js +++ b/node_modules/connect-mongo/src/index.js @@ -1,4 +1,5 @@ 'use strict'; +/* eslint indent: [error, 4] */ const Promise = require('bluebird'); const MongoClient = require('mongodb'); @@ -122,11 +123,11 @@ module.exports = function connectMongo(connect) { } setAutoRemoveAsync() { + let removeQuery = { expires: { $lt: new Date() } }; switch (this.autoRemove) { case 'native': return this.collection.ensureIndexAsync({ expires: 1 }, { expireAfterSeconds: 0 }); case 'interval': - let removeQuery = { expires: { $lt: new Date() } }; this.timer = setInterval(() => this.collection.remove(removeQuery, { w: 0 }), this.autoRemoveInterval * 1000 * 60); this.timer.unref(); return Promise.resolve(); @@ -150,9 +151,10 @@ module.exports = function connectMongo(connect) { this.collection = collection; // Promisify used collection methods - ['count', 'findOne', 'remove', 'drop', 'update', 'ensureIndex'].forEach(method => { - collection[method + 'Async'] = Promise.promisify(collection[method], collection); + ['count', 'findOne', 'remove', 'drop', 'ensureIndex'].forEach(method => { + collection[method + 'Async'] = Promise.promisify(collection[method], { context: collection }); }); + collection.updateAsync = Promise.promisify(collection.update, { context: collection, multiArgs: true }); return this; } @@ -199,28 +201,28 @@ module.exports = function connectMongo(connect) { })) .then(session => { if (session) { - var s = this.transformFunctions.unserialize(session.session); - if(this.options.touchAfter > 0 && session.lastModified){ + const s = this.transformFunctions.unserialize(session.session); + if (this.options.touchAfter > 0 && session.lastModified) { s.lastModified = session.lastModified; } this.emit('touch', sid); return s; } }) - .nodeify(callback); + .asCallback(callback); } set(sid, session, callback) { // removing the lastModified prop from the session object before update - if(this.options.touchAfter > 0 && session && session.lastModified){ + if (this.options.touchAfter > 0 && session && session.lastModified) { delete session.lastModified; } - var s; + let s; try { - s = { _id: this.computeStorageId(sid), session: this.transformFunctions.serialize(session)}; + s = { _id: this.computeStorageId(sid), session: this.transformFunctions.serialize(session) }; } catch (err) { return callback(err); } @@ -238,18 +240,26 @@ module.exports = function connectMongo(connect) { s.expires = new Date(Date.now() + this.ttl * 1000); } - if(this.options.touchAfter > 0){ + if (this.options.touchAfter > 0) { s.lastModified = new Date(); } return this.collectionReady() .then(collection => collection.updateAsync({ _id: this.computeStorageId(sid) }, s, { upsert: true })) - .then(() => this.emit('set', sid)) - .nodeify(callback); + .then(responseArray => { + const rawResponse = responseArray.length === 2 ? responseArray[1] : responseArray[0].result; + if (rawResponse.upserted) { + this.emit('create', sid); + } else { + this.emit('update', sid); + } + this.emit('set', sid); + }) + .asCallback(callback); } touch(sid, session, callback) { - var updateFields = {}, + const updateFields = {}, touchAfter = this.options.touchAfter * 1000, lastModified = session.lastModified ? session.lastModified.getTime() : 0, currentDate = new Date(); @@ -257,11 +267,11 @@ module.exports = function connectMongo(connect) { // if the given options has a touchAfter property, check if the // current timestamp - lastModified timestamp is bigger than // the specified, if it's not, don't touch the session - if(touchAfter > 0 && lastModified > 0){ + if (touchAfter > 0 && lastModified > 0) { - var timeElapsed = currentDate.getTime() - session.lastModified; + const timeElapsed = currentDate.getTime() - session.lastModified; - if(timeElapsed < touchAfter){ + if (timeElapsed < touchAfter) { return callback(); } else { updateFields.lastModified = currentDate; @@ -284,26 +294,26 @@ module.exports = function connectMongo(connect) { this.emit('touch', sid); } }) - .nodeify(callback); + .asCallback(callback); } destroy(sid, callback) { return this.collectionReady() .then(collection => collection.removeAsync({ _id: this.computeStorageId(sid) })) .then(() => this.emit('destroy', sid)) - .nodeify(callback); + .asCallback(callback); } length(callback) { return this.collectionReady() .then(collection => collection.countAsync({})) - .nodeify(callback); + .asCallback(callback); } clear(callback) { return this.collectionReady() .then(collection => collection.dropAsync()) - .nodeify(callback); + .asCallback(callback); } close() { |
