Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
jdesboeufs committed Mar 1, 2018
1 parent db34a49 commit 9f86f90
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 84 deletions.
149 changes: 74 additions & 75 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const MongoClient = require('mongodb')

function withCallback(promise, cb) {
// assume that cb is a function - type checks and handling type errors
// Assume that cb is a function - type checks and handling type errors
// can be done by caller
if (cb) {
promise
Expand All @@ -14,14 +14,14 @@ function withCallback(promise, cb) {
}

function defaultSerializeFunction(session) {
// Copy each property of the session to a new object
// Copy each property of the session to a new object
const obj = {}
let prop

for (prop in session) {
if (prop === 'cookie') {
// Convert the cookie instance to an object, if possible
// This gets rid of the duplicate object under session.cookie.data property
// Convert the cookie instance to an object, if possible
// This gets rid of the duplicate object under session.cookie.data property
obj.cookie = session.cookie.toJSON ? session.cookie.toJSON() : session.cookie
} else {
obj[prop] = session[prop]
Expand Down Expand Up @@ -59,18 +59,17 @@ module.exports = function (connect) {
const MemoryStore = connect.MemoryStore || connect.session.MemoryStore

class MongoStore extends Store {

constructor(options) {
options = options || {}

/* Fallback */
/* Fallback */
if (options.fallbackMemory && MemoryStore) {
return new MemoryStore()
}

super(options)

/* Options */
/* Options */
this.ttl = options.ttl || 1209600 // 14 days
this.collectionName = options.collection || 'sessions'
this.autoRemove = options.autoRemove || 'native'
Expand All @@ -90,26 +89,26 @@ module.exports = function (connect) {
}

if (options.url) {
// New native connection using url + mongoOptions
// New native connection using url + mongoOptions
MongoClient.connect(options.url, options.mongoOptions || {}, newConnectionCallback)
} else if (options.mongooseConnection) {
// Re-use existing or upcoming mongoose connection
// Re-use existing or upcoming mongoose connection
if (options.mongooseConnection.readyState === 1) {
this.handleNewConnectionAsync(options.mongooseConnection.db)
} else {
options.mongooseConnection.once('open', () => this.handleNewConnectionAsync(options.mongooseConnection.db))
}
} else if (options.db && options.db.listCollections) {
// Re-use existing or upcoming native connection
// Re-use existing or upcoming native connection
if (options.db.openCalled || options.db.openCalled === undefined) { // OpenCalled is undefined in [email protected]
this.handleNewConnectionAsync(options.db)
} else {
options.db.open(newConnectionCallback)
}
} else if (options.dbPromise) {
options.dbPromise
.then(db => this.handleNewConnectionAsync(db))
.catch(err => this.connectionFailed(err))
.then(db => this.handleNewConnectionAsync(db))
.catch(err => this.connectionFailed(err))
} else {
throw new Error('Connection strategy not found')
}
Expand All @@ -125,9 +124,9 @@ module.exports = function (connect) {
handleNewConnectionAsync(db) {
this.db = db
return this
.setCollection(db.collection(this.collectionName))
.setAutoRemoveAsync()
.then(() => this.changeState('connected'))
.setCollection(db.collection(this.collectionName))
.setAutoRemoveAsync()
.then(() => this.changeState('connected'))
}

setAutoRemoveAsync() {
Expand Down Expand Up @@ -185,32 +184,32 @@ module.exports = function (connect) {
return sessionId
}

/* Public API */
/* Public API */

get(sid, callback) {
return withCallback(this.collectionReady()
.then(collection => collection.findOne({
_id: this.computeStorageId(sid),
$or: [
{expires: {$exists: false}},
{expires: {$gt: new Date()}}
]
}))
.then(session => {
if (session) {
const s = this.transformFunctions.unserialize(session.session)
if (this.options.touchAfter > 0 && session.lastModified) {
s.lastModified = session.lastModified
}
this.emit('get', sid)
return s
}
})
, callback)
.then(collection => collection.findOne({
_id: this.computeStorageId(sid),
$or: [
{expires: {$exists: false}},
{expires: {$gt: new Date()}}
]
}))
.then(session => {
if (session) {
const s = this.transformFunctions.unserialize(session.session)
if (this.options.touchAfter > 0 && session.lastModified) {
s.lastModified = session.lastModified
}
this.emit('get', sid)
return s
}
})
, callback)
}

set(sid, session, callback) {
// Removing the lastModified prop from the session object before update
// Removing the lastModified prop from the session object before update
if (this.options.touchAfter > 0 && session && session.lastModified) {
delete session.lastModified
}
Expand All @@ -226,13 +225,13 @@ module.exports = function (connect) {
if (session && session.cookie && session.cookie.expires) {
s.expires = new Date(session.cookie.expires)
} else {
// If there's no expiration date specified, it is
// browser-session cookie or there is no cookie at all,
// as per the connect docs.
//
// So we set the expiration to two-weeks from now
// - as is common practice in the industry (e.g Django) -
// or the default specified in the options.
// If there's no expiration date specified, it is
// browser-session cookie or there is no cookie at all,
// as per the connect docs.
//
// So we set the expiration to two-weeks from now
// - as is common practice in the industry (e.g Django) -
// or the default specified in the options.
s.expires = new Date(Date.now() + (this.ttl * 1000))
}

Expand All @@ -241,19 +240,19 @@ module.exports = function (connect) {
}

return withCallback(this.collectionReady()
.then(collection => collection.update({_id: this.computeStorageId(sid)}, s, {upsert: true}))
.then(rawResponse => {
if (rawResponse.result) {
rawResponse = rawResponse.result
}
if (rawResponse && rawResponse.upserted) {
this.emit('create', sid)
} else {
this.emit('update', sid)
}
this.emit('set', sid)
})
, callback)
.then(collection => collection.update({_id: this.computeStorageId(sid)}, s, {upsert: true}))
.then(rawResponse => {
if (rawResponse.result) {
rawResponse = rawResponse.result
}
if (rawResponse && rawResponse.upserted) {
this.emit('create', sid)
} else {
this.emit('update', sid)
}
this.emit('set', sid)
})
, callback)
}

touch(sid, session, callback) {
Expand All @@ -262,9 +261,9 @@ module.exports = function (connect) {
const lastModified = session.lastModified ? session.lastModified.getTime() : 0
const currentDate = new Date()

// 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 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) {
const timeElapsed = currentDate.getTime() - session.lastModified

Expand All @@ -281,42 +280,42 @@ module.exports = function (connect) {
}

return withCallback(this.collectionReady()
.then(collection => collection.update({_id: this.computeStorageId(sid)}, {$set: updateFields}))
.then(result => {
if (result.nModified === 0) {
throw new Error('Unable to find the session to touch')
} else {
this.emit('touch', sid, session)
}
})
, callback)
.then(collection => collection.update({_id: this.computeStorageId(sid)}, {$set: updateFields}))
.then(result => {
if (result.nModified === 0) {
throw new Error('Unable to find the session to touch')
} else {
this.emit('touch', sid, session)
}
})
, callback)
}

destroy(sid, callback) {
return withCallback(this.collectionReady()
.then(collection => collection.remove({_id: this.computeStorageId(sid)}))
.then(() => this.emit('destroy', sid))
, callback)
.then(collection => collection.remove({_id: this.computeStorageId(sid)}))
.then(() => this.emit('destroy', sid))
, callback)
}

length(callback) {
return withCallback(this.collectionReady()
.then(collection => collection.count({}))
, callback)
.then(collection => collection.count({}))
, callback)
}

clear(callback) {
return withCallback(this.collectionReady()
.then(collection => collection.drop())
, callback)
.then(collection => collection.drop())
, callback)
}

close() {
if (this.db) {
this.db.close()
}
}
}
}

return MongoStore
}
2 changes: 1 addition & 1 deletion test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const expect = require('expect.js')

const expressSession = require('express-session')
const MongoStore = require('../')(expressSession)
const MongoStore = require('..')(expressSession)

const futureDate = new Date(2030, 1)

Expand Down
2 changes: 1 addition & 1 deletion test/legacy-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ process.on('unhandledRejection', (reason, err) => {
console.error(`Reason: ${reason}`)
console.error(err)
process.exit(1)
});
})

describe('Legacy tests', function () {
this.timeout(6000)
Expand Down
14 changes: 7 additions & 7 deletions test/legacy-tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const expressSession = require('express-session')
const MongoStore = require('../')(expressSession)
const MongoStore = require('..')(expressSession)
const assert = require('assert')

const connectionString = process.env.MONGODB_URL || 'mongodb://localhost/connect-mongo-test'
Expand Down Expand Up @@ -257,13 +257,13 @@ exports.test_get_promise = function (done) {
const sid = 'test_get_promise-sid'
collection.insert({_id: sid, session: JSON.stringify({key1: 1, key2: 'two'})}, () => {
store.get(sid)
.then((session) => {
.then(session => {
assert.deepEqual(session, {key1: 1, key2: 'two'})
cleanup(store, db, collection, () => {
done()
})
})
.catch(done)
.catch(done)
})
})
}
Expand All @@ -288,13 +288,13 @@ exports.test_length_promise = function (done) {
const sid = 'test_length_promise-sid'
collection.insert({_id: sid, session: JSON.stringify({key1: 1, key2: 'two'})}, () => {
store.length()
.then((length) => {
.then(length => {
assert.strictEqual(length, 1)
cleanup(store, db, collection, () => {
done()
})
})
.catch(done)
.catch(done)
})
})
}
Expand Down Expand Up @@ -323,7 +323,7 @@ exports.test_destroy_ok_promise = function (done) {
done()
})
})
.catch(done)
.catch(done)
})
})
}
Expand Down Expand Up @@ -359,7 +359,7 @@ exports.test_clear_promise = function (done) {
})
})
})
.catch(done)
.catch(done)
})
})
}
Expand Down

0 comments on commit 9f86f90

Please sign in to comment.