Skip to content

Commit cc68d33

Browse files
nbbeekenljhaywar
authored andcommitted
fix: Compatibility with mongodb-client-encryption (#2713)
Permits legacy options being used by mongodb-client-encryption's use of the MongoClient constructor. Permits legacy WC option style in insertOne. NODE-3006
1 parent a2ac0ed commit cc68d33

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

src/collection.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,16 @@ export class Collection {
278278
options?: InsertOneOptions | Callback<InsertOneResult>,
279279
callback?: Callback<InsertOneResult>
280280
): Promise<InsertOneResult> | void {
281-
if (typeof options === 'function') (callback = options), (options = {});
281+
if (typeof options === 'function') {
282+
callback = options;
283+
options = {};
284+
}
285+
286+
// CSFLE passes in { w: 'majority' } to ensure the lib works in both 3.x and 4.x
287+
// we support that option style here only
288+
if (options && Reflect.get(options, 'w')) {
289+
options.writeConcern = WriteConcern.fromOptions(Reflect.get(options, 'w'));
290+
}
282291

283292
return executeOperation(
284293
getTopology(this),

src/connection_string.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ export function parseOptions(
418418

419419
checkTLSOptions(mongoOptions);
420420
if (mongoClient && options.autoEncryption) {
421-
mongoOptions.autoEncrypter = createAutoEncrypter(mongoClient, options);
421+
mongoOptions.autoEncrypter = createAutoEncrypter(mongoClient, mongoOptions);
422422
}
423423
if (options.promiseLibrary) PromiseProvider.set(options.promiseLibrary);
424424

@@ -1062,7 +1062,10 @@ export const OPTIONS = {
10621062
passphrase: { type: 'any' },
10631063
pfx: { type: 'any' },
10641064
secureProtocol: { type: 'any' },
1065-
index: { type: 'any' }
1065+
index: { type: 'any' },
1066+
// Legacy Options, these are unused but left here to avoid errors with CSFLE lib
1067+
useNewUrlParser: { type: 'boolean' } as OptionDescriptor,
1068+
useUnifiedTopology: { type: 'boolean' } as OptionDescriptor
10661069
} as Record<keyof MongoClientOptions, OptionDescriptor>;
10671070

10681071
export const DEFAULT_OPTIONS = new CaseInsensitiveMap(

src/operations/connect.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { resolveSRVRecord } from '../connection_string';
44
import { emitDeprecationWarning, Callback } from '../utils';
55
import { CMAP_EVENT_NAMES } from '../cmap/events';
66
import * as BSON from '../bson';
7-
import type { MongoClient, MongoOptions, MongoClientOptions } from '../mongo_client';
7+
import type { MongoClient, MongoOptions } from '../mongo_client';
88
import { Connection } from '../cmap/connection';
99
import { Server } from '../sdam/server';
1010
import type { AutoEncrypter } from '../deps';
@@ -116,7 +116,7 @@ function registerDeprecatedEventNotifiers(client: MongoClient) {
116116
*/
117117
export function createAutoEncrypter(
118118
client: MongoClient,
119-
options: MongoClientOptions
119+
options: MongoOptions
120120
): AutoEncrypter | undefined {
121121
if (!options.autoEncryption) {
122122
return;

test/tools/runner/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class TestConfiguration {
185185
} else {
186186
multipleHosts = this.options.hostAddresses
187187
.reduce((built, host) => {
188-
built.push(host.type === 'tcp' ? `${host.host}:${host.port}` : host.host);
188+
built.push(typeof host.port === 'number' ? `${host.host}:${host.port}` : host.host);
189189
return built;
190190
}, [])
191191
.join(',');

0 commit comments

Comments
 (0)