Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/legacy_wrappers/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,53 @@

const { toLegacy, maybeCallback } = require('../utils');

const { version } = require('../../package.json');

module.exports = Object.create(null);
Object.defineProperty(module.exports, '__esModule', { value: true });

module.exports.makeLegacyMongoClient = function (baseClass) {
class LegacyMongoClient extends baseClass {
// constructor adds client metadata before constructing final client
constructor(connectionString, options) {
if (options == null) {
options = {};
}

const incorrectOptionsType = typeof options !== 'object';
const incorrectDriverInfo =
options.driverInfo != null && typeof options.driverInfo !== 'object';
if (incorrectOptionsType || incorrectDriverInfo) {
// Pass this mistake along to the MongoClient constructor
super(connectionString, options);
return;
}

options = {
...options,
driverInfo: options.driverInfo == null ? {} : { ...options.driverInfo }
};

const infoParts = {
name: ['mongodb-legacy'],
version: [version]
};

// name handling
if (typeof options.driverInfo.name === 'string') {
infoParts.name.push(options.driverInfo.name);
}
options.driverInfo.name = infoParts.name.join('|');

// version handling
if (typeof options.driverInfo.version === 'string') {
infoParts.version.push(options.driverInfo.version);
}
options.driverInfo.version = infoParts.version.join('|');

super(connectionString, options);
}

static connect(url, options, callback) {
callback =
typeof callback === 'function'
Expand Down
88 changes: 88 additions & 0 deletions test/unit/legacy_wrappers/mongo_client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const { expect } = require('chai');

const iLoveJs = 'mongodb://iLoveJavascript';

const currentLegacyVersion = require('../../../package.json').version;

describe('legacy_wrappers/mongo_client.js', () => {
let client;
beforeEach(async function () {
Expand All @@ -22,6 +24,92 @@ describe('legacy_wrappers/mongo_client.js', () => {
await client.close();
});

describe('calling the constructor with invalid types', () => {
it('should not throw when passing a non-object type as the options', () => {
// The driver ignores non-object types in the options arg position
// so this confirms our logic for adding metadata or any other handling
// does not introduce an error for non-object types
expect(() => {
new LegacyMongoClient(iLoveJs, true);
}).to.not.throw();
});
});

describe('setting client metadata', () => {
it('does not mutate the input options', () => {
expect(() => {
new LegacyMongoClient(iLoveJs, Object.freeze({}));
}).to.not.throw();
});

it('does not mutate the input options.driverInfo', () => {
expect(() => {
new LegacyMongoClient(iLoveJs, Object.freeze({ driverInfo: Object.freeze({}) }));
}).to.not.throw();
});

describe('when driverInfo.name is provided', () => {
const client = new LegacyMongoClient(iLoveJs, { driverInfo: { name: 'mongoose' } });

it('should prepend mongodb-legacy to user passed driverInfo.name', () =>
expect(client.options.metadata).to.have.nested.property(
'driver.name',
'nodejs|mongodb-legacy|mongoose'
));

it('should include version in package.json in client metadata', () =>
expect(client.options.metadata)
.to.have.property('version')
.that.includes(currentLegacyVersion));
});

describe('when driverInfo.name is provided and driverInfo.version is provided', () => {
const client = new LegacyMongoClient(iLoveJs, {
driverInfo: { name: 'mongoose', version: '99.99.99' }
});

it('should prepend mongodb-legacy to user passed driverInfo.name', () =>
expect(client.options.metadata)
.to.have.nested.property('driver.name')
.that.equals('nodejs|mongodb-legacy|mongoose'));

it('should prepend version in package.json to user driverInfo.version', () =>
expect(client.options.metadata)
.to.have.property('version')
.that.includes(`${currentLegacyVersion}|99.99.99`));
});

describe('when driverInfo.version is provided', () => {
const client = new LegacyMongoClient(iLoveJs, {
driverInfo: { version: '99.99.99' }
});

it('should include mongodb-legacy in client metadata', () =>
expect(client.options.metadata)
.to.have.nested.property('driver.name')
.that.equals('nodejs|mongodb-legacy'));

it('should prepend version in package.json to user driverInfo.version', () =>
expect(client.options.metadata)
.to.have.property('version')
.that.includes(`${currentLegacyVersion}|99.99.99`));
});

describe('when driverInfo is not provided', () => {
const client = new LegacyMongoClient(iLoveJs);

it('should include mongodb-legacy in client metadata', () =>
expect(client.options.metadata)
.to.have.nested.property('driver.name')
.that.equals('nodejs|mongodb-legacy'));

it('should include version in package.json in client metadata', () =>
expect(client.options.metadata)
.to.have.property('version')
.that.includes(currentLegacyVersion));
});
});

it('calling MongoClient.connect() returns promise', async () => {
sinon.stub(mongodbDriver.MongoClient.prototype, 'connect').returns(Promise.resolve(2));
expect(client).to.have.property('connect').that.is.a('function');
Expand Down