Skip to content

Commit faf0116

Browse files
committed
fix(Adapters): ensure mongodb metadata is passed from the first handshake
1 parent a2292d0 commit faf0116

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

spec/GridFSBucketStorageAdapter.spec.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const GridFSBucketAdapter = require('../lib/Adapters/Files/GridFSBucketAdapter')
22
.GridFSBucketAdapter;
33
const { randomString } = require('../lib/cryptoUtils');
4-
const { MongoClient } = require('mongodb');
54
const databaseURI = 'mongodb://localhost:27017/parse';
65
const request = require('../lib/request');
76

@@ -479,11 +478,9 @@ describe_only_db('mongo')('GridFSBucket', () => {
479478

480479
it('pass metadata to MongoClient', async () => {
481480
const pkg = require('../package.json');
482-
spyOn(MongoClient.prototype, 'appendMetadata').and.callThrough();
483-
484481
const gfsAdapter = new GridFSBucketAdapter(databaseURI);
485482
await gfsAdapter._connect();
486-
expect(MongoClient.prototype.appendMetadata).toHaveBeenCalledWith({
483+
expect(gfsAdapter._client.s.options.driverInfo).toEqual({
487484
name: 'Parse Server',
488485
version: pkg.version,
489486
});

spec/MongoStorageAdapter.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,11 +451,9 @@ describe_only_db('mongo')('MongoStorageAdapter', () => {
451451

452452
it('pass metadata to MongoClient', async () => {
453453
const pkg = require('../package.json');
454-
spyOn(MongoClient.prototype, 'appendMetadata').and.callThrough();
455-
456454
const adapter = new MongoStorageAdapter({ uri: databaseURI });
457455
await adapter.connect();
458-
expect(adapter.client.appendMetadata).toHaveBeenCalledWith({
456+
expect(adapter.client.s.options.driverInfo).toEqual({
459457
name: 'Parse Server',
460458
version: pkg.version,
461459
});

src/Adapters/Files/GridFSBucketAdapter.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ export class GridFSBucketAdapter extends FilesAdapter {
4646

4747
_connect() {
4848
if (!this._connectionPromise) {
49-
this._connectionPromise = MongoClient.connect(this._databaseURI, this._mongoOptions).then(
49+
// Add wrapping library metadata.
50+
const driverInfo = {
51+
name: 'Parse Server',
52+
version: pkg.version,
53+
};
54+
const mongoclient = new MongoClient(this._databaseURI, this._mongoOptions);
55+
mongoclient.appendMetadata(driverInfo);
56+
this._connectionPromise = mongoclient.connect().then(
5057
client => {
51-
client.appendMetadata?.({
52-
name: 'Parse Server',
53-
version: pkg.version,
54-
});
5558
this._client = client;
5659
return client.db(client.s.options.dbName);
5760
}

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,18 @@ export class MongoStorageAdapter implements StorageAdapter {
180180
// parsing and re-formatting causes the auth value (if there) to get URI
181181
// encoded
182182
const encodedUri = formatUrl(parseUrl(this._uri));
183-
this.connectionPromise = MongoClient.connect(encodedUri, this._mongoOptions)
183+
// Add wrapping library metadata.
184+
const driverInfo = {
185+
name: 'Parse Server',
186+
version: pkg.version,
187+
}
188+
const mongoclient = new MongoClient(encodedUri, this._mongoOptions)
189+
mongoclient.appendMetadata(driverInfo);
190+
this.connectionPromise = mongoclient.connect()
184191
.then(client => {
185192
// Starting mongoDB 3.0, the MongoClient.connect don't return a DB anymore but a client
186193
// Fortunately, we can get back the options and use them to select the proper DB.
187194
// https://github.com/mongodb/node-mongodb-native/blob/2c35d76f08574225b8db02d7bef687123e6bb018/lib/mongo_client.js#L885
188-
client.appendMetadata?.({
189-
name: 'Parse Server',
190-
version: pkg.version,
191-
});
192195
const options = client.s.options;
193196
const database = client.db(options.dbName);
194197
if (!database) {

0 commit comments

Comments
 (0)