Skip to content

Commit

Permalink
fix: Support bson property on Topology class
Browse files Browse the repository at this point in the history
This addresses an incompatibility with the existing versions of
mongodb-client-encryption which falls back on the bson library
being attached to the topology class.

NODE-3041
  • Loading branch information
nbbeeken committed Jan 27, 2021
1 parent c0d8a72 commit 25fea33
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/sdam/topology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ import type { CloseOptions } from '../cmap/connection_pool';
import { DestroyOptions, Connection } from '../cmap/connection';
import type { MongoClientOptions } from '../mongo_client';
import { DEFAULT_OPTIONS } from '../connection_string';
import * as BSON from '../bson';
import type { deserialize } from 'bson';

// Global state
let globalTopologyCounter = 0;
Expand Down Expand Up @@ -188,13 +190,27 @@ export class Topology extends EventEmitter {
static readonly OPEN = 'open' as const;
/** @event */
static readonly CONNECT = 'connect' as const;
/**
* @internal
*
* @privateRemarks
* mongodb-client-encryption's class ClientEncryption falls back to finding the bson lib
* defined on client.topology.bson, in order to maintain compatibility with any version
* of mongodb-client-encryption we keep a reference to serialize and deserialize here.
*/
bson: { serialize: typeof BSON.serialize; deserialize: typeof BSON.deserialize };

/**
* @param seedlist - a list of HostAddress instances to connect to
*/
constructor(seeds: string | string[] | HostAddress | HostAddress[], options: TopologyOptions) {
super();

// Legacy CSFLE support
this.bson = Object.create(null);
this.bson.serialize = BSON.serialize;
this.bson.deserialize = BSON.deserialize;

// Options should only be undefined in tests, MongoClient will always have defined options
options = options ?? {
hosts: [HostAddress.fromString('localhost:27017')],
Expand Down
30 changes: 30 additions & 0 deletions test/unit/legacy_compat.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const { expect } = require('chai');
const { MongoClient } = require('../../src');

describe('Legacy 3.x features', function () {
it('Should have bson defined on topology', function () {
const client = new MongoClient(this.configuration.url());
return client
.connect()
.then(client => {
expect(client.topology).to.have.property('bson');
expect(client.topology.bson).to.have.property('serialize');
expect(client.topology.bson).to.have.property('deserialize');
})
.finally(() => client.close());
});

it('Should allow legacy option useUnifiedTopology', function () {
const url = this.configuration.url();
expect(() => new MongoClient(url, { useUnifiedTopology: true })).to.not.throw;
expect(() => new MongoClient(url, { useUnifiedTopology: false })).to.not.throw;
});

it('Should allow legacy option useNewUrlParser', function () {
const url = this.configuration.url();
expect(() => new MongoClient(url, { useNewUrlParser: true })).to.not.throw;
expect(() => new MongoClient(url, { useNewUrlParser: false })).to.not.throw;
});
});

0 comments on commit 25fea33

Please sign in to comment.