Skip to content

Commit

Permalink
fix(NODE-4425): webpack optional import of FLE issue (#3324)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Jul 19, 2022
1 parent c5cfe21 commit 5ab2b05
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
10 changes: 4 additions & 6 deletions src/encrypter.ts
Expand Up @@ -6,7 +6,7 @@ import { MongoInvalidArgumentError, MongoMissingDependencyError } from './error'
import { MongoClient, MongoClientOptions } from './mongo_client';
import { Callback, getMongoDBClientEncryption } from './utils';

let AutoEncrypterClass: AutoEncrypter;
let AutoEncrypterClass: { new (...args: ConstructorParameters<AutoEncrypter>): AutoEncrypter };

/** @internal */
const kInternalClient = Symbol('internalClient');
Expand Down Expand Up @@ -123,15 +123,13 @@ export class Encrypter {
}

static checkForMongoCrypt(): void {
try {
// NOTE(NODE-3199): Ensure you always wrap an optional require in the try block
const mongodbClientEncryption = getMongoDBClientEncryption();
AutoEncrypterClass = mongodbClientEncryption.extension(require('../lib/index')).AutoEncrypter;
} catch {
const mongodbClientEncryption = getMongoDBClientEncryption();
if (mongodbClientEncryption == null) {
throw new MongoMissingDependencyError(
'Auto-encryption requested, but the module is not installed. ' +
'Please add `mongodb-client-encryption` as a dependency of your project'
);
}
AutoEncrypterClass = mongodbClientEncryption.extension(require('../lib/index')).AutoEncrypter;
}
}
34 changes: 24 additions & 10 deletions src/utils.ts
Expand Up @@ -1409,24 +1409,38 @@ export function commandSupportsReadConcern(command: Document, options?: Document
return false;
}

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

// NOTE(NODE-4254): This is to get around the circular dependency between
// mongodb-client-encryption and the driver in the test scenarios.
if (
typeof process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE === 'string' &&
process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE.length > 0
) {
mongodbClientEncryption = require(process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE);
try {
// NOTE(NODE-3199): Ensure you always wrap an optional require literally in the try block
// Cannot be moved to helper utility function, bundlers search and replace the actual require call
// in a way that makes this line throw at bundle time, not runtime, catching here will make bundling succeed
mongodbClientEncryption = require(process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE);
} catch {
// ignore
}
} else {
mongodbClientEncryption = require('mongodb-client-encryption');
try {
// NOTE(NODE-3199): Ensure you always wrap an optional require literally in the try block
// Cannot be moved to helper utility function, bundlers search and replace the actual require call
// in a way that makes this line throw at bundle time, not runtime, catching here will make bundling succeed
mongodbClientEncryption = require('mongodb-client-encryption');
} catch {
// ignore
}
}

return mongodbClientEncryption;
Expand Down
5 changes: 5 additions & 0 deletions test/tools/unified-spec-runner/unified-utils.ts
Expand Up @@ -206,6 +206,11 @@ export function makeConnectionString(
export function getClientEncryptionClass(): ClientEncryption {
try {
const mongodbClientEncryption = getMongoDBClientEncryption();
if (mongodbClientEncryption == null) {
throw new MongoMissingDependencyError(
'Attempting to import mongodb-client-encryption but it is not installed.'
);
}

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

0 comments on commit 5ab2b05

Please sign in to comment.