Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support bson property on Topology class #2721

Merged
merged 1 commit into from
Jan 29, 2021
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
15 changes: 15 additions & 0 deletions src/sdam/topology.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ 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 { serialize, deserialize } from '../bson';

// Global state
let globalTopologyCounter = 0;
Expand Down Expand Up @@ -188,13 +189,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 serialize; deserialize: typeof 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 = serialize;
this.bson.deserialize = 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;
});
});