Skip to content

Commit 8c929a4

Browse files
nbbeekenljhaywar
authored andcommitted
fix: Support bson property on Topology class (#2721)
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
1 parent fc53a72 commit 8c929a4

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/sdam/topology.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import type { CloseOptions } from '../cmap/connection_pool';
5252
import { DestroyOptions, Connection } from '../cmap/connection';
5353
import type { MongoClientOptions } from '../mongo_client';
5454
import { DEFAULT_OPTIONS } from '../connection_string';
55+
import { serialize, deserialize } from '../bson';
5556

5657
// Global state
5758
let globalTopologyCounter = 0;
@@ -188,13 +189,27 @@ export class Topology extends EventEmitter {
188189
static readonly OPEN = 'open' as const;
189190
/** @event */
190191
static readonly CONNECT = 'connect' as const;
192+
/**
193+
* @internal
194+
*
195+
* @privateRemarks
196+
* mongodb-client-encryption's class ClientEncryption falls back to finding the bson lib
197+
* defined on client.topology.bson, in order to maintain compatibility with any version
198+
* of mongodb-client-encryption we keep a reference to serialize and deserialize here.
199+
*/
200+
bson: { serialize: typeof serialize; deserialize: typeof deserialize };
191201

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

208+
// Legacy CSFLE support
209+
this.bson = Object.create(null);
210+
this.bson.serialize = serialize;
211+
this.bson.deserialize = deserialize;
212+
198213
// Options should only be undefined in tests, MongoClient will always have defined options
199214
options = options ?? {
200215
hosts: [HostAddress.fromString('localhost:27017')],

test/unit/legacy_compat.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const { expect } = require('chai');
4+
const { MongoClient } = require('../../src');
5+
6+
describe('Legacy 3.x features', function () {
7+
it('Should have bson defined on topology', function () {
8+
const client = new MongoClient(this.configuration.url());
9+
return client
10+
.connect()
11+
.then(client => {
12+
expect(client.topology).to.have.property('bson');
13+
expect(client.topology.bson).to.have.property('serialize');
14+
expect(client.topology.bson).to.have.property('deserialize');
15+
})
16+
.finally(() => client.close());
17+
});
18+
19+
it('Should allow legacy option useUnifiedTopology', function () {
20+
const url = this.configuration.url();
21+
expect(() => new MongoClient(url, { useUnifiedTopology: true })).to.not.throw;
22+
expect(() => new MongoClient(url, { useUnifiedTopology: false })).to.not.throw;
23+
});
24+
25+
it('Should allow legacy option useNewUrlParser', function () {
26+
const url = this.configuration.url();
27+
expect(() => new MongoClient(url, { useNewUrlParser: true })).to.not.throw;
28+
expect(() => new MongoClient(url, { useNewUrlParser: false })).to.not.throw;
29+
});
30+
});

0 commit comments

Comments
 (0)