@@ -18,21 +18,22 @@ function makeErrorModule(error: any) {
1818 } ) ;
1919}
2020
21- export let Kerberos : typeof import ( 'kerberos' ) | { kModuleError : MongoMissingDependencyError } =
22- makeErrorModule (
23- new MongoMissingDependencyError (
24- 'Optional module `kerberos` not found. Please install it to enable kerberos authentication'
25- )
26- ) ;
21+ export type Kerberos = typeof import ( 'kerberos' ) | { kModuleError : MongoMissingDependencyError } ;
2722
28- export function getKerberos ( ) : typeof Kerberos | { kModuleError : MongoMissingDependencyError } {
23+ export function getKerberos ( ) : Kerberos {
24+ let kerberos : Kerberos ;
2925 try {
3026 // Ensure you always wrap an optional require in the try block NODE-3199
31- Kerberos = require ( 'kerberos' ) ;
32- return Kerberos ;
33- } catch {
34- return Kerberos ;
27+ kerberos = require ( 'kerberos' ) ;
28+ } catch ( error ) {
29+ kerberos = makeErrorModule (
30+ new MongoMissingDependencyError (
31+ 'Optional module `kerberos` not found. Please install it to enable kerberos authentication' ,
32+ { cause : error , dependencyName : 'kerberos' }
33+ )
34+ ) ;
3535 }
36+ return kerberos ;
3637}
3738
3839export interface KerberosClient {
@@ -57,20 +58,22 @@ type ZStandardLib = {
5758 decompress ( buf : Buffer ) : Promise < Buffer > ;
5859} ;
5960
60- export let ZStandard : ZStandardLib | { kModuleError : MongoMissingDependencyError } =
61- makeErrorModule (
62- new MongoMissingDependencyError (
63- 'Optional module `@mongodb-js/zstd` not found. Please install it to enable zstd compression'
64- )
65- ) ;
61+ export type ZStandard = ZStandardLib | { kModuleError : MongoMissingDependencyError } ;
6662
67- export function getZstdLibrary ( ) : typeof ZStandard | { kModuleError : MongoMissingDependencyError } {
63+ export function getZstdLibrary ( ) : ZStandardLib | { kModuleError : MongoMissingDependencyError } {
64+ let ZStandard : ZStandardLib | { kModuleError : MongoMissingDependencyError } ;
6865 try {
6966 ZStandard = require ( '@mongodb-js/zstd' ) ;
70- return ZStandard ;
71- } catch {
72- return ZStandard ;
67+ } catch ( error ) {
68+ ZStandard = makeErrorModule (
69+ new MongoMissingDependencyError (
70+ 'Optional module `@mongodb-js/zstd` not found. Please install it to enable zstd compression' ,
71+ { cause : error , dependencyName : 'zstd' }
72+ )
73+ ) ;
7374 }
75+
76+ return ZStandard ;
7477}
7578
7679/**
@@ -100,11 +103,12 @@ export function getAwsCredentialProvider():
100103 // Ensure you always wrap an optional require in the try block NODE-3199
101104 const credentialProvider = require ( '@aws-sdk/credential-providers' ) ;
102105 return credentialProvider ;
103- } catch {
106+ } catch ( error ) {
104107 return makeErrorModule (
105108 new MongoMissingDependencyError (
106109 'Optional module `@aws-sdk/credential-providers` not found.' +
107- ' Please install it to enable getting aws credentials via the official sdk.'
110+ ' Please install it to enable getting aws credentials via the official sdk.' ,
111+ { cause : error , dependencyName : '@aws-sdk/credential-providers' }
108112 )
109113 ) ;
110114 }
@@ -120,11 +124,12 @@ export function getGcpMetadata(): GcpMetadata {
120124 // Ensure you always wrap an optional require in the try block NODE-3199
121125 const credentialProvider = require ( 'gcp-metadata' ) ;
122126 return credentialProvider ;
123- } catch {
127+ } catch ( error ) {
124128 return makeErrorModule (
125129 new MongoMissingDependencyError (
126130 'Optional module `gcp-metadata` not found.' +
127- ' Please install it to enable getting gcp credentials via the official sdk.'
131+ ' Please install it to enable getting gcp credentials via the official sdk.' ,
132+ { cause : error , dependencyName : 'gcp-metadata' }
128133 )
129134 ) ;
130135 }
@@ -150,10 +155,10 @@ export function getSnappy(): SnappyLib | { kModuleError: MongoMissingDependencyE
150155 // Ensure you always wrap an optional require in the try block NODE-3199
151156 const value = require ( 'snappy' ) ;
152157 return value ;
153- } catch ( cause ) {
158+ } catch ( error ) {
154159 const kModuleError = new MongoMissingDependencyError (
155160 'Optional module `snappy` not found. Please install it to enable snappy compression' ,
156- { cause }
161+ { cause : error , dependencyName : 'snappy' }
157162 ) ;
158163 return { kModuleError } ;
159164 }
@@ -184,10 +189,10 @@ export function getSocks(): SocksLib | { kModuleError: MongoMissingDependencyErr
184189 // Ensure you always wrap an optional require in the try block NODE-3199
185190 const value = require ( 'socks' ) ;
186191 return value ;
187- } catch ( cause ) {
192+ } catch ( error ) {
188193 const kModuleError = new MongoMissingDependencyError (
189194 'Optional module `socks` not found. Please install it to connections over a SOCKS5 proxy' ,
190- { cause }
195+ { cause : error , dependencyName : 'socks' }
191196 ) ;
192197 return { kModuleError } ;
193198 }
@@ -234,16 +239,23 @@ interface AWS4 {
234239 } ;
235240}
236241
237- export let aws4 : AWS4 | { kModuleError : MongoMissingDependencyError } = makeErrorModule (
238- new MongoMissingDependencyError (
239- 'Optional module `aws4` not found. Please install it to enable AWS authentication'
240- )
241- ) ;
242+ export const aws4 : AWS4 | { kModuleError : MongoMissingDependencyError } = loadAws4 ( ) ;
242243
243- try {
244- // Ensure you always wrap an optional require in the try block NODE-3199
245- aws4 = require ( 'aws4' ) ;
246- } catch { } // eslint-disable-line
244+ function loadAws4 ( ) {
245+ let aws4 : AWS4 | { kModuleError : MongoMissingDependencyError } ;
246+ try {
247+ aws4 = require ( 'aws4' ) ;
248+ } catch ( error ) {
249+ aws4 = makeErrorModule (
250+ new MongoMissingDependencyError (
251+ 'Optional module `aws4` not found. Please install it to enable AWS authentication' ,
252+ { cause : error , dependencyName : 'aws4' }
253+ )
254+ ) ;
255+ }
256+
257+ return aws4 ;
258+ }
247259
248260/** A utility function to get the instance of mongodb-client-encryption, if it exists. */
249261export function getMongoDBClientEncryption ( ) :
@@ -256,10 +268,10 @@ export function getMongoDBClientEncryption():
256268 // Cannot be moved to helper utility function, bundlers search and replace the actual require call
257269 // in a way that makes this line throw at bundle time, not runtime, catching here will make bundling succeed
258270 mongodbClientEncryption = require ( 'mongodb-client-encryption' ) ;
259- } catch ( cause ) {
271+ } catch ( error ) {
260272 const kModuleError = new MongoMissingDependencyError (
261273 'Optional module `mongodb-client-encryption` not found. Please install it to use auto encryption or ClientEncryption.' ,
262- { cause }
274+ { cause : error , dependencyName : 'mongodb-client-encryption' }
263275 ) ;
264276 return { kModuleError } ;
265277 }
0 commit comments