Skip to content

Commit 1b6670b

Browse files
committed
feat: support driver info for drivers wrapping the node driver
This allows drivers that wrap the node driver (e.g. Mongoose) report information about themselves in the client metadata generated by the driver.
1 parent 4ccb08a commit 1b6670b

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

lib/core/topologies/shared.js

+15
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ function createClientInfo(options) {
5656
clientInfo.application = { name: appname };
5757
}
5858

59+
// support optionally provided wrapping driver info
60+
if (options.driverInfo) {
61+
if (options.driverInfo.name) {
62+
clientInfo.driver.name = `${clientInfo.driver.name}|${options.driverInfo.name}`;
63+
}
64+
65+
if (options.driverInfo.version) {
66+
clientInfo.driver.version = `${clientInfo.driver.version}|${options.driverInfo.version}`;
67+
}
68+
69+
if (options.driverInfo.platform) {
70+
clientInfo.platform = `${clientInfo.platform}|${options.driverInfo.platform}`;
71+
}
72+
}
73+
5974
return clientInfo;
6075
}
6176

lib/mongo_client.js

+10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ const CloseOperation = require('./operations/close');
5454
* @see https://docs.mongodb.com/manual/reference/read-concern/index.html#read-concern-levels
5555
*/
5656

57+
/**
58+
* Configuration options for drivers wrapping the node driver.
59+
*
60+
* @typedef {Object} DriverInfoOptions
61+
* @property {string} [name] The name of the driver
62+
* @property {string} [version] The version of the driver
63+
* @property {string} [platform] Optional platform information
64+
*/
65+
5766
/**
5867
* Creates a new MongoClient instance
5968
* @class
@@ -120,6 +129,7 @@ const CloseOperation = require('./operations/close');
120129
* @param {boolean} [options.useNewUrlParser=true] Determines whether or not to use the new url parser. Enables the new, spec-compliant, url parser shipped in the core driver. This url parser fixes a number of problems with the original parser, and aims to outright replace that parser in the near future. Defaults to true, and must be explicitly set to false to use the legacy url parser.
121130
* @param {boolean} [options.useUnifiedTopology] Enables the new unified topology layer
122131
* @param {AutoEncrypter~AutoEncryptionOptions} [options.autoEncryption] Optionally enable client side auto encryption
132+
* @param {DriverInfoOptions} [options.driverInfo] Allows a wrapping driver to amend the client metadata generated by the driver to include information about the wrapping driver
123133
* @param {MongoClient~connectCallback} [callback] The command result callback
124134
* @return {MongoClient} a MongoClient instance
125135
*/

lib/operations/connect.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ const validOptionNames = [
137137
'useUnifiedTopology',
138138
'serverSelectionTimeoutMS',
139139
'useRecoveryToken',
140-
'autoEncryption'
140+
'autoEncryption',
141+
'driverInfo'
141142
];
142143

143144
const ignoreOptionNames = ['native_parser'];

test/unit/client_tests.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const mock = require('mongodb-mock-server');
5+
6+
describe('Client (unit)', function() {
7+
let server;
8+
9+
afterEach(() => mock.cleanup());
10+
beforeEach(() => {
11+
return mock.createServer().then(_server => (server = _server));
12+
});
13+
14+
it('should let wrapping libraries amend the client metadata', function() {
15+
let handshake;
16+
server.setMessageHandler(request => {
17+
const doc = request.document;
18+
if (doc.ismaster) {
19+
handshake = doc;
20+
request.reply(Object.assign({}, mock.DEFAULT_ISMASTER));
21+
} else if (doc.endSessions) {
22+
request.reply({ ok: 1 });
23+
}
24+
});
25+
26+
const client = this.configuration.newClient(`mongodb://${server.uri()}/`, {
27+
useUnifiedTopology: true,
28+
driverInfo: {
29+
name: 'mongoose',
30+
version: '5.7.10',
31+
platform: 'llama edition'
32+
}
33+
});
34+
35+
return client.connect().then(() => {
36+
this.defer(() => client.close());
37+
38+
expect(handshake)
39+
.nested.property('client.driver')
40+
.to.deep.equal({
41+
name: 'nodejs|mongoose',
42+
version: '3.3.4|5.7.10'
43+
});
44+
45+
expect(handshake)
46+
.nested.property('client.platform')
47+
.to.match(/llama edition/);
48+
});
49+
});
50+
});

0 commit comments

Comments
 (0)