Skip to content

Commit

Permalink
fix: upgrade mongodb driver to v4
Browse files Browse the repository at this point in the history
  • Loading branch information
mingchuno committed Aug 17, 2021
1 parent cceec18 commit ccd716a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 80 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,15 @@
"dependencies": {
"debug": "^4.3.1",
"kruptein": "^3.0.0",
"mongodb": "3.6.5"
"mongodb": "4.1.0"
},
"devDependencies": {
"@ava/typescript": "^1.1.1",
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@types/express": "^4.17.9",
"@types/express-session": "^1.17.3",
"@types/mongodb": "^3.6.18",
"@types/express": "^4.17.13",
"@types/express-session": "^1.17.4",
"@types/node": "^14.14.20",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.12.0",
Expand Down
22 changes: 14 additions & 8 deletions src/lib/MongoStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,12 @@ test.serial('touch ops', async (t) => {
const collection = await store.collectionP
const session = await collection.findOne({ _id: sid })
await new Promise((resolve) => setTimeout(resolve, 500))
await storePromise.touch(sid, session.session)
t.not(session, undefined)
await storePromise.touch(sid, session?.session)
const session2 = await collection.findOne({ _id: sid })
t.not(session2, undefined)
// Check if both expiry date are different
t.truthy(session2.expires.getTime() > session.expires.getTime())
t.truthy(session2?.expires.getTime() > session?.expires.getTime())
})

test.serial('touch ops with touchAfter', async (t) => {
Expand All @@ -251,10 +253,12 @@ test.serial('touch ops with touchAfter', async (t) => {
await storePromise.set(sid, orgSession)
const collection = await store.collectionP
const session = await collection.findOne({ _id: sid })
const lastModifiedBeforeTouch = session.lastModified.getTime()
await storePromise.touch(sid, session)
const lastModifiedBeforeTouch = session?.lastModified.getTime()
t.not(session, undefined)
await storePromise.touch(sid, session as SessionData)
const session2 = await collection.findOne({ _id: sid })
const lastModifiedAfterTouch = session2.lastModified.getTime()
t.not(session2, undefined)
const lastModifiedAfterTouch = session2?.lastModified.getTime()
// Check if both expiry date are different
t.is(lastModifiedBeforeTouch, lastModifiedAfterTouch)
})
Expand All @@ -267,11 +271,13 @@ test.serial('touch ops with touchAfter with touch', async (t) => {
await storePromise.set(sid, orgSession)
const collection = await store.collectionP
const session = await collection.findOne({ _id: sid })
const lastModifiedBeforeTouch = session.lastModified.getTime()
const lastModifiedBeforeTouch = session?.lastModified.getTime()
await new Promise((resolve) => setTimeout(resolve, 1200))
await storePromise.touch(sid, session)
t.not(session, undefined)
await storePromise.touch(sid, session as SessionData)
const session2 = await collection.findOne({ _id: sid })
const lastModifiedAfterTouch = session2.lastModified.getTime()
t.not(session2, undefined)
const lastModifiedAfterTouch = session2?.lastModified.getTime()
// Check if both expiry date are different
t.truthy(lastModifiedAfterTouch > lastModifiedBeforeTouch)
})
Expand Down
16 changes: 9 additions & 7 deletions src/lib/MongoStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import util from 'util'
import * as session from 'express-session'
import {
Collection,
CommonOptions,
MongoClient,
MongoClientOptions,
WriteConcernSettings,
} from 'mongodb'
import Debug from 'debug'
import Kruptein from 'kruptein'
Expand Down Expand Up @@ -38,7 +38,7 @@ export type ConnectMongoOptions = {
// FIXME: remove those any
serialize?: (a: any) => any
unserialize?: (a: any) => any
writeOperationOptions?: CommonOptions['writeConcern']
writeOperationOptions?: WriteConcernSettings
transformId?: (a: any) => any
crypto?: CryptoOptions
}
Expand All @@ -61,7 +61,7 @@ type ConcretConnectMongoOptions = {
// FIXME: remove those any
serialize?: (a: any) => any
unserialize?: (a: any) => any
writeOperationOptions?: CommonOptions['writeConcern']
writeOperationOptions?: WriteConcernSettings
transformId?: (a: any) => any
// FIXME: remove above any
crypto: ConcretCryptoOptions
Expand Down Expand Up @@ -138,7 +138,7 @@ export default class MongoStore extends session.Store {
constructor({
collectionName = 'sessions',
ttl = 1209600,
mongoOptions = { useUnifiedTopology: true },
mongoOptions = {},
autoRemove = 'native',
autoRemoveInterval = 10,
touchAfter = 0,
Expand Down Expand Up @@ -311,15 +311,17 @@ export default class MongoStore extends session.Store {
],
})
if (this.crypto && session) {
await this.decryptSession(session).catch((err) => callback(err))
await this.decryptSession(
session as session.SessionData
).catch((err) => callback(err))
}
const s =
session && this.transformFunctions.unserialize(session.session)
if (this.options.touchAfter > 0 && session?.lastModified) {
s.lastModified = session.lastModified
}
this.emit('get', sid)
callback(null, s)
callback(null, s === undefined ? null : s)
} catch (error) {
callback(error)
}
Expand Down Expand Up @@ -481,7 +483,7 @@ export default class MongoStore extends session.Store {
const results: session.SessionData[] = []
for await (const session of sessions) {
if (this.crypto && session) {
await this.decryptSession(session)
await this.decryptSession(session as session.SessionData)
}
results.push(this.transformFunctions.unserialize(session.session))
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/testHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const makeDataNoCookie = () => {
export const createStoreHelper = (opt: Partial<ConnectMongoOptions> = {}) => {
const store = MongoStore.create({
mongoUrl: 'mongodb://root:example@127.0.0.1:27017',
mongoOptions: { useUnifiedTopology: true },
mongoOptions: {},
dbName: 'testDb',
collectionName: 'test-collection',
...opt,
Expand Down
Loading

0 comments on commit ccd716a

Please sign in to comment.