Skip to content

Commit 5ab2b05

Browse files
authored
fix(NODE-4425): webpack optional import of FLE issue (#3324)
1 parent c5cfe21 commit 5ab2b05

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

src/encrypter.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { MongoInvalidArgumentError, MongoMissingDependencyError } from './error'
66
import { MongoClient, MongoClientOptions } from './mongo_client';
77
import { Callback, getMongoDBClientEncryption } from './utils';
88

9-
let AutoEncrypterClass: AutoEncrypter;
9+
let AutoEncrypterClass: { new (...args: ConstructorParameters<AutoEncrypter>): AutoEncrypter };
1010

1111
/** @internal */
1212
const kInternalClient = Symbol('internalClient');
@@ -123,15 +123,13 @@ export class Encrypter {
123123
}
124124

125125
static checkForMongoCrypt(): void {
126-
try {
127-
// NOTE(NODE-3199): Ensure you always wrap an optional require in the try block
128-
const mongodbClientEncryption = getMongoDBClientEncryption();
129-
AutoEncrypterClass = mongodbClientEncryption.extension(require('../lib/index')).AutoEncrypter;
130-
} catch {
126+
const mongodbClientEncryption = getMongoDBClientEncryption();
127+
if (mongodbClientEncryption == null) {
131128
throw new MongoMissingDependencyError(
132129
'Auto-encryption requested, but the module is not installed. ' +
133130
'Please add `mongodb-client-encryption` as a dependency of your project'
134131
);
135132
}
133+
AutoEncrypterClass = mongodbClientEncryption.extension(require('../lib/index')).AutoEncrypter;
136134
}
137135
}

src/utils.ts

+24-10
Original file line numberDiff line numberDiff line change
@@ -1409,24 +1409,38 @@ export function commandSupportsReadConcern(command: Document, options?: Document
14091409
return false;
14101410
}
14111411

1412-
/**
1413-
* A utility function to get the instance of mongodb-client-encryption, if it exists.
1414-
*
1415-
* @throws MongoMissingDependencyError if mongodb-client-encryption isn't installed.
1416-
* @returns
1417-
*/
1418-
export function getMongoDBClientEncryption() {
1419-
let mongodbClientEncryption;
1412+
/** A utility function to get the instance of mongodb-client-encryption, if it exists. */
1413+
export function getMongoDBClientEncryption(): {
1414+
extension: (mdb: unknown) => {
1415+
AutoEncrypter: any;
1416+
ClientEncryption: any;
1417+
};
1418+
} | null {
1419+
let mongodbClientEncryption = null;
14201420

14211421
// NOTE(NODE-4254): This is to get around the circular dependency between
14221422
// mongodb-client-encryption and the driver in the test scenarios.
14231423
if (
14241424
typeof process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE === 'string' &&
14251425
process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE.length > 0
14261426
) {
1427-
mongodbClientEncryption = require(process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE);
1427+
try {
1428+
// NOTE(NODE-3199): Ensure you always wrap an optional require literally in the try block
1429+
// Cannot be moved to helper utility function, bundlers search and replace the actual require call
1430+
// in a way that makes this line throw at bundle time, not runtime, catching here will make bundling succeed
1431+
mongodbClientEncryption = require(process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE);
1432+
} catch {
1433+
// ignore
1434+
}
14281435
} else {
1429-
mongodbClientEncryption = require('mongodb-client-encryption');
1436+
try {
1437+
// NOTE(NODE-3199): Ensure you always wrap an optional require literally in the try block
1438+
// Cannot be moved to helper utility function, bundlers search and replace the actual require call
1439+
// in a way that makes this line throw at bundle time, not runtime, catching here will make bundling succeed
1440+
mongodbClientEncryption = require('mongodb-client-encryption');
1441+
} catch {
1442+
// ignore
1443+
}
14301444
}
14311445

14321446
return mongodbClientEncryption;

test/tools/unified-spec-runner/unified-utils.ts

+5
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ export function makeConnectionString(
206206
export function getClientEncryptionClass(): ClientEncryption {
207207
try {
208208
const mongodbClientEncryption = getMongoDBClientEncryption();
209+
if (mongodbClientEncryption == null) {
210+
throw new MongoMissingDependencyError(
211+
'Attempting to import mongodb-client-encryption but it is not installed.'
212+
);
213+
}
209214

210215
// eslint-disable-next-line @typescript-eslint/no-var-requires
211216
const { ClientEncryption } = mongodbClientEncryption.extension(require('../../../src/index'));

0 commit comments

Comments
 (0)