Skip to content

Commit

Permalink
chore: upgrade dependencies and adjust support levels
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Feb 13, 2024
1 parent c17ffd4 commit c2bb71d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 50 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14
18
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
26 changes: 9 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
});
},

Expand Down
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
18 changes: 4 additions & 14 deletions test/connection.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit c2bb71d

Please sign in to comment.