@@ -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 ;
0 commit comments