Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: upgrade mongodb depns to V4 #430

Merged
merged 5 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/sanity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]
node-version: [12.x, 14.x, 15.x, 16.x]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!


steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"branches": 75,
"lines": 80,
"functions": 80,
"functions": 78,
"statements": 80,
"exclude": ["src/test"]
"exclude": ["src/test", "src/lib/MongoStore.spec.ts"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this accept wildcard syntax such that you could put "*.spec.ts"?

}
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### **BREAKING CHANGES**

- Drop Node 10 support

### Changed

- Upgrade `mongodb` to V4 [#422] [#426]

### Fixed

- Move `writeConcern` away from top-level option to fix deprecation warning [#422](https://github.com/jdesboeufs/connect-mongo/issues/422)


## [4.4.1] - 2021-03-23

### Fixed
Expand Down
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
26 changes: 17 additions & 9 deletions src/lib/MongoStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ test.serial('create store w/o provide required options', (t) => {
})
})

test.serial('create store with clientPromise', (t) => {
test.serial('create store with clientPromise', async (t) => {
const clientP = MongoClient.connect('mongodb://root:example@127.0.0.1:27017')
const store = MongoStore.create({ clientPromise: clientP })
t.not(store, null)
t.not(store, undefined)
await store.collectionP
store.close()
})

Expand All @@ -46,6 +47,7 @@ test.serial('create store with client', async (t) => {
const store = MongoStore.create({ client: client })
t.not(store, null)
t.not(store, undefined)
await store.collectionP
store.close()
})

Expand Down Expand Up @@ -237,10 +239,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 +255,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 +273,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
40 changes: 21 additions & 19 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 @@ -198,13 +198,13 @@ export default class MongoStore extends session.Store {
assert(!!_clientP, 'Client is null|undefined')
this.clientP = _clientP
this.options = options
this.collectionP = _clientP
.then((con) => con.db(options.dbName))
.then((db) => db.collection(options.collectionName))
.then((collection) => {
this.setAutoRemove(collection)
return collection
})
this.collectionP = _clientP.then(async (con) => {
const collection = con
.db(options.dbName)
.collection(options.collectionName)
await this.setAutoRemove(collection)
return collection
})
if (options.crypto.secret) {
this.crypto = require('kruptein')(options.crypto)
}
Expand All @@ -214,7 +214,7 @@ export default class MongoStore extends session.Store {
return new MongoStore(options)
}

private setAutoRemove(collection: Collection) {
private setAutoRemove(collection: Collection): Promise<unknown> {
const removeQuery = () => ({
expires: {
$lt: new Date(),
Expand All @@ -223,14 +223,14 @@ export default class MongoStore extends session.Store {
switch (this.options.autoRemove) {
case 'native':
debug('Creating MongoDB TTL index')
collection.createIndex(
return collection.createIndex(
{ expires: 1 },
{
background: true,
expireAfterSeconds: 0,
writeConcern: this.options.writeOperationOptions,
}
)
break
case 'interval':
debug('create Timer to remove expired sessions')
this.timer = setInterval(
Expand All @@ -244,10 +244,10 @@ export default class MongoStore extends session.Store {
this.options.autoRemoveInterval * 1000 * 60
)
this.timer.unref()
break
return Promise.resolve()
case 'disabled':
default:
break
return Promise.resolve()
}
}

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