Skip to content

Commit a156075

Browse files
committed
feat: expose package version on MongoClient
Provide the current package version statically. The idea here is to use this to solve NODE-3043, by allowing mongodb-client-encryption to pass the correct options for a specific driver version.
1 parent 622c87e commit a156075

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/mongo_client.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
resolveOptions,
1212
ClientMetadata,
1313
ns,
14-
HostAddress
14+
HostAddress,
15+
NODE_DRIVER_VERSION
1516
} from './utils';
1617
import { deprecate } from 'util';
1718
import { connect } from './operations/connect';
@@ -579,6 +580,11 @@ export class MongoClient extends EventEmitter {
579580
if (typeof options === 'function') (callback = options), (options = {});
580581
if (typeof callback === 'function') callback(undefined, true);
581582
}, 'Multiple authentication is prohibited on a connected client, please only authenticate once per MongoClient');
583+
584+
/** Provides the version of the 'mongodb' package. */
585+
static get version(): string {
586+
return NODE_DRIVER_VERSION;
587+
}
582588
}
583589

584590
/**

src/utils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,9 @@ export interface ClientMetadataOptions {
889889
appName?: string;
890890
}
891891

892+
/** @internal */
892893
// eslint-disable-next-line @typescript-eslint/no-var-requires
893-
const NODE_DRIVER_VERSION = require('../package.json').version;
894+
export const NODE_DRIVER_VERSION = require('../package.json').version;
894895

895896
export function makeClientMetadata(options?: ClientMetadataOptions): ClientMetadata {
896897
options = options ?? {};

test/functional/mongo_client.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,27 @@ describe('MongoClient', function () {
170170
}
171171
});
172172

173+
it('Should provide the same package version in metadata and on the MongoClient class', {
174+
metadata: {
175+
requires: {
176+
topology: ['single', 'replicaset', 'sharded']
177+
}
178+
},
179+
180+
test: function (done) {
181+
var configuration = this.configuration;
182+
var url = configuration.url();
183+
184+
const client = configuration.newClient(url, { appname: 'hello world' });
185+
client.connect(err => {
186+
expect(err).to.not.exist;
187+
test.equal(client.topology.clientMetadata.driver.version, client.constructor.version);
188+
189+
client.close(done);
190+
});
191+
}
192+
});
193+
173194
it('Should correctly pass through socketTimeoutMS and connectTimeoutMS', {
174195
metadata: {
175196
requires: {

0 commit comments

Comments
 (0)