Skip to content

Commit 438430b

Browse files
committed
fix(NODE-4425): webpack optional import issue
1 parent 4aa9d9d commit 438430b

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

src/encrypter.ts

Lines changed: 4 additions & 6 deletions
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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { LEGACY_HELLO_COMMAND } from './constants';
1212
import type { AbstractCursor } from './cursor/abstract_cursor';
1313
import type { FindCursor } from './cursor/find_cursor';
1414
import type { Db } from './db';
15+
import type { AutoEncrypter } from './deps';
1516
import {
1617
AnyError,
1718
MongoCompatibilityError,
@@ -1409,24 +1410,34 @@ export function commandSupportsReadConcern(command: Document, options?: Document
14091410
return false;
14101411
}
14111412

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;
1413+
/** A utility function to get the instance of mongodb-client-encryption, if it exists. */
1414+
export function getMongoDBClientEncryption(): {
1415+
extension: (mdb: typeof import('../src/index')) => {
1416+
AutoEncrypter: any;
1417+
ClientEncryption: any;
1418+
};
1419+
} | null {
1420+
let mongodbClientEncryption = null;
14201421

14211422
// NOTE(NODE-4254): This is to get around the circular dependency between
14221423
// mongodb-client-encryption and the driver in the test scenarios.
14231424
if (
14241425
typeof process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE === 'string' &&
14251426
process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE.length > 0
14261427
) {
1427-
mongodbClientEncryption = require(process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE);
1428+
try {
1429+
// NOTE(NODE-3199): Ensure you always wrap an optional require in the try block
1430+
mongodbClientEncryption = require(process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE);
1431+
} catch {
1432+
// ignore
1433+
}
14281434
} else {
1429-
mongodbClientEncryption = require('mongodb-client-encryption');
1435+
try {
1436+
// NOTE(NODE-3199): Ensure you always wrap an optional require in the try block
1437+
mongodbClientEncryption = require('mongodb-client-encryption');
1438+
} catch {
1439+
// ignore
1440+
}
14301441
}
14311442

14321443
return mongodbClientEncryption;

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

Lines changed: 5 additions & 0 deletions
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)