Skip to content

Commit 25fea33

Browse files
committed
fix: Support bson property on Topology class
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
1 parent c0d8a72 commit 25fea33

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/sdam/topology.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ 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 * as BSON from '../bson';
56+
import type { deserialize } from 'bson';
5557

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

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

209+
// Legacy CSFLE support
210+
this.bson = Object.create(null);
211+
this.bson.serialize = BSON.serialize;
212+
this.bson.deserialize = BSON.deserialize;
213+
198214
// Options should only be undefined in tests, MongoClient will always have defined options
199215
options = options ?? {
200216
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)