Skip to content

Commit

Permalink
fix: Compatibility with mongodb-client-encryption (#2713)
Browse files Browse the repository at this point in the history
Permits legacy options being used by mongodb-client-encryption's
use of the MongoClient constructor. Permits legacy WC option style in
insertOne.

NODE-3006
  • Loading branch information
nbbeeken authored Jan 27, 2021
1 parent 12d011b commit d08ddb9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,16 @@ export class Collection {
options?: InsertOneOptions | Callback<InsertOneResult>,
callback?: Callback<InsertOneResult>
): Promise<InsertOneResult> | void {
if (typeof options === 'function') (callback = options), (options = {});
if (typeof options === 'function') {
callback = options;
options = {};
}

// CSFLE passes in { w: 'majority' } to ensure the lib works in both 3.x and 4.x
// we support that option style here only
if (options && Reflect.get(options, 'w')) {
options.writeConcern = WriteConcern.fromOptions(Reflect.get(options, 'w'));
}

return executeOperation(
getTopology(this),
Expand Down
7 changes: 5 additions & 2 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ export function parseOptions(

checkTLSOptions(mongoOptions);
if (mongoClient && options.autoEncryption) {
mongoOptions.autoEncrypter = createAutoEncrypter(mongoClient, options);
mongoOptions.autoEncrypter = createAutoEncrypter(mongoClient, mongoOptions);
}
if (options.promiseLibrary) PromiseProvider.set(options.promiseLibrary);

Expand Down Expand Up @@ -1062,7 +1062,10 @@ export const OPTIONS = {
passphrase: { type: 'any' },
pfx: { type: 'any' },
secureProtocol: { type: 'any' },
index: { type: 'any' }
index: { type: 'any' },
// Legacy Options, these are unused but left here to avoid errors with CSFLE lib
useNewUrlParser: { type: 'boolean' } as OptionDescriptor,
useUnifiedTopology: { type: 'boolean' } as OptionDescriptor
} as Record<keyof MongoClientOptions, OptionDescriptor>;

export const DEFAULT_OPTIONS = new CaseInsensitiveMap(
Expand Down
4 changes: 2 additions & 2 deletions src/operations/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { resolveSRVRecord } from '../connection_string';
import { emitDeprecationWarning, Callback } from '../utils';
import { CMAP_EVENT_NAMES } from '../cmap/events';
import * as BSON from '../bson';
import type { MongoClient, MongoOptions, MongoClientOptions } from '../mongo_client';
import type { MongoClient, MongoOptions } from '../mongo_client';
import { Connection } from '../cmap/connection';
import { Server } from '../sdam/server';
import type { AutoEncrypter } from '../deps';
Expand Down Expand Up @@ -116,7 +116,7 @@ function registerDeprecatedEventNotifiers(client: MongoClient) {
*/
export function createAutoEncrypter(
client: MongoClient,
options: MongoClientOptions
options: MongoOptions
): AutoEncrypter | undefined {
if (!options.autoEncryption) {
return;
Expand Down
2 changes: 1 addition & 1 deletion test/tools/runner/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class TestConfiguration {
} else {
multipleHosts = this.options.hostAddresses
.reduce((built, host) => {
built.push(host.type === 'tcp' ? `${host.host}:${host.port}` : host.host);
built.push(typeof host.port === 'number' ? `${host.host}:${host.port}` : host.host);
return built;
}, [])
.join(',');
Expand Down

0 comments on commit d08ddb9

Please sign in to comment.