diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 42ab3ab..91e444f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,16 +12,16 @@ jobs: fail-fast: false matrix: os: [ubuntu] - node: ['*', '16', '14'] - hapi: ['20', '19', '18'] + node: ['latest', '20', '18'] + hapi: ['21', '20'] include: - os: ubuntu - node: '*' + node: 'latest' hapi: 'latest' services: mongodb: - image: mongo:4 + image: mongo:6 ports: - 27017:27017 diff --git a/.nvmrc b/.nvmrc index 8351c19..3c03207 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -14 +18 diff --git a/LICENSE b/LICENSE index 1d61edc..a208f6f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014 Nicolas Morel +Copyright (c) 2014-2023 Nicolas Morel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/README.md b/README.md index 8b2184d..1d49691 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ launchServer().catch((err) => { ## Compatibility level -* Hapi >= 17 -* Node.js >= 8 +* Hapi >= 20 +* Node.js >= 18 -Ships with `mongodb` 3.x. +Ships with `mongodb` 6.x. diff --git a/lib/index.js b/lib/index.js index 934e45d..cd6cc8e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -32,17 +32,14 @@ exports.plugin = { const connect = async function (connectionOptions) { - if (connectionOptions.settings && typeof connectionOptions.settings.promiseLibrary === 'string') { - connectionOptions.settings.promiseLibrary = require(connectionOptions .settings.promiseLibrary); - } - const client = await MongoClient.connect(connectionOptions.url, connectionOptions.settings); const db = await client.db(); - const connectionOptionsToLog = Object.assign({}, connectionOptions, { + const connectionOptionsToLog = { + ...connectionOptions, url: connectionOptions.url.replace(/mongodb(\+srv)?:\/\/([^/]+?):([^@]+)@/, 'mongodb$1://$2:******@') - }); + }; - server.log(['hapi-mongodb', 'info'], 'MongoClient connection created for ' + JSON.stringify(connectionOptionsToLog)); + server.log(['hapi-mongodb', 'info'], `MongoClient connection created for ${JSON.stringify(connectionOptionsToLog)}`); if (typeof connectionOptions.decorate === 'string') { const decoration = Object.assign({ client, db }, expose); @@ -74,17 +71,12 @@ exports.plugin = { } } - server.events.on('stop', () => { - - for (const client of [].concat(expose.client)) { + server.events.on('stop', async () => { - client.close((err) => { - - if (err) { - server.log(['hapi-mongodb', 'error'], err); - } - }); - } + const closeResult = await Promise.allSettled([].concat(expose.client).map((client) => client.close())); + closeResult + .filter((result) => result.status === 'rejected') + .forEach((result) => server.log(['hapi-mongodb', 'error'], result.reason)); }); }, diff --git a/package.json b/package.json index 47b064f..98383d3 100644 --- a/package.json +++ b/package.json @@ -19,21 +19,20 @@ "url": "https://github.com/Marsup/hapi-mongodb/issues" }, "engines": { - "node": ">= v12.22.12" + "node": ">= v18" }, "dependencies": { - "joi": "^17.6.0", - "mongodb": "^4.16.0" + "joi": "^17.12.1", + "mongodb": "^6.3.0" }, "devDependencies": { - "@hapi/code": "^9.0.0", - "@hapi/hapi": "^20.2.2", - "@hapi/hoek": "^10.0.0", - "@hapi/lab": "^25.0.1", - "bluebird": "3.x", - "sinon": "^15.2.0" + "@hapi/code": "^9.0.3", + "@hapi/hapi": "^21.3.3", + "@hapi/hoek": "^11.0.4", + "@hapi/lab": "^25.2.0", + "sinon": "^17.0.1" }, "peerDependencies": { - "@hapi/hapi": ">= 18.0.0" + "@hapi/hapi": ">= 20.0.0" } } diff --git a/test/connection.js b/test/connection.js index ffed541..4b14ded 100644 --- a/test/connection.js +++ b/test/connection.js @@ -1,5 +1,6 @@ 'use strict'; +const Timers = require('node:timers/promises'); const Hapi = require('@hapi/hapi'); const Hoek = require('@hapi/hoek'); const Lab = require('@hapi/lab'); @@ -380,18 +381,6 @@ describe('Hapi server', () => { }); }); - it('should require the "promiseLibrary" before passing it to mongodb', async () => { - - await server.register({ - plugin: require('../'), - options: { - settings: { - promiseLibrary: 'bluebird' - } - } - }); - }); - it('should disconnect if the server stops', async () => { await server.register({ @@ -423,9 +412,10 @@ describe('Hapi server', () => { await server.initialize(); expect(server.plugins['hapi-mongodb'].client.topology.isConnected()).to.be.true(); - const closeStub = Sinon.stub(server.plugins['hapi-mongodb'].client, 'close').callsFake((cb) => { + const closeStub = Sinon.stub(server.plugins['hapi-mongodb'].client, 'close').callsFake(async () => { - setTimeout(cb, 0, new Error('Oops')); + await Timers.setTimeout(1); + throw new Error('Oops'); }); await server.stop();