Skip to content

Commit d85c4a8

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 57cb4b6 commit d85c4a8

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
@@ -77,6 +77,15 @@ const CloseOperation = require('./operations/close');
7777
* @property {string[]} [extraOptions.mongocryptdSpawnArgs] Command line arguments to use when auto-spawning a mongocryptd
7878
*/
7979

80+
/**
81+
* Configuration options for drivers wrapping the node driver.
82+
*
83+
* @typedef {Object} DriverInfoOptions
84+
* @property {string} [name] The name of the driver
85+
* @property {string} [version] The version of the driver
86+
* @property {string} [platform] Optional platform information
87+
*/
88+
8089
/**
8190
* Creates a new MongoClient instance
8291
* @class
@@ -143,6 +152,7 @@ const CloseOperation = require('./operations/close');
143152
* @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.
144153
* @param {boolean} [options.useUnifiedTopology] Enables the new unified topology layer
145154
* @param {AutoEncryptionOptions} [options.autoEncryption] Optionally enable client side auto encryption
155+
* @param {DriverInfoOptions} [options.driverInfo] Allows a wrapping driver to amend the client metadata generated by the driver to include information about the wrapping driver
146156
* @param {MongoClient~connectCallback} [callback] The command result callback
147157
* @return {MongoClient} a MongoClient instance
148158
*/

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)