@@ -18,21 +18,22 @@ function makeErrorModule(error: any) {
18
18
} ) ;
19
19
}
20
20
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 } ;
27
22
28
- export function getKerberos ( ) : typeof Kerberos | { kModuleError : MongoMissingDependencyError } {
23
+ export function getKerberos ( ) : Kerberos {
24
+ let kerberos : Kerberos ;
29
25
try {
30
26
// 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
+ ) ;
35
35
}
36
+ return kerberos ;
36
37
}
37
38
38
39
export interface KerberosClient {
@@ -57,20 +58,22 @@ type ZStandardLib = {
57
58
decompress ( buf : Buffer ) : Promise < Buffer > ;
58
59
} ;
59
60
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 } ;
66
62
67
- export function getZstdLibrary ( ) : typeof ZStandard | { kModuleError : MongoMissingDependencyError } {
63
+ export function getZstdLibrary ( ) : ZStandardLib | { kModuleError : MongoMissingDependencyError } {
64
+ let ZStandard : ZStandardLib | { kModuleError : MongoMissingDependencyError } ;
68
65
try {
69
66
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
+ ) ;
73
74
}
75
+
76
+ return ZStandard ;
74
77
}
75
78
76
79
/**
@@ -100,11 +103,12 @@ export function getAwsCredentialProvider():
100
103
// Ensure you always wrap an optional require in the try block NODE-3199
101
104
const credentialProvider = require ( '@aws-sdk/credential-providers' ) ;
102
105
return credentialProvider ;
103
- } catch {
106
+ } catch ( error ) {
104
107
return makeErrorModule (
105
108
new MongoMissingDependencyError (
106
109
'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' }
108
112
)
109
113
) ;
110
114
}
@@ -120,11 +124,12 @@ export function getGcpMetadata(): GcpMetadata {
120
124
// Ensure you always wrap an optional require in the try block NODE-3199
121
125
const credentialProvider = require ( 'gcp-metadata' ) ;
122
126
return credentialProvider ;
123
- } catch {
127
+ } catch ( error ) {
124
128
return makeErrorModule (
125
129
new MongoMissingDependencyError (
126
130
'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' }
128
133
)
129
134
) ;
130
135
}
@@ -150,10 +155,10 @@ export function getSnappy(): SnappyLib | { kModuleError: MongoMissingDependencyE
150
155
// Ensure you always wrap an optional require in the try block NODE-3199
151
156
const value = require ( 'snappy' ) ;
152
157
return value ;
153
- } catch ( cause ) {
158
+ } catch ( error ) {
154
159
const kModuleError = new MongoMissingDependencyError (
155
160
'Optional module `snappy` not found. Please install it to enable snappy compression' ,
156
- { cause }
161
+ { cause : error , dependencyName : 'snappy' }
157
162
) ;
158
163
return { kModuleError } ;
159
164
}
@@ -184,10 +189,10 @@ export function getSocks(): SocksLib | { kModuleError: MongoMissingDependencyErr
184
189
// Ensure you always wrap an optional require in the try block NODE-3199
185
190
const value = require ( 'socks' ) ;
186
191
return value ;
187
- } catch ( cause ) {
192
+ } catch ( error ) {
188
193
const kModuleError = new MongoMissingDependencyError (
189
194
'Optional module `socks` not found. Please install it to connections over a SOCKS5 proxy' ,
190
- { cause }
195
+ { cause : error , dependencyName : 'socks' }
191
196
) ;
192
197
return { kModuleError } ;
193
198
}
@@ -234,16 +239,23 @@ interface AWS4 {
234
239
} ;
235
240
}
236
241
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 ( ) ;
242
243
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
+ }
247
259
248
260
/** A utility function to get the instance of mongodb-client-encryption, if it exists. */
249
261
export function getMongoDBClientEncryption ( ) :
@@ -256,10 +268,10 @@ export function getMongoDBClientEncryption():
256
268
// Cannot be moved to helper utility function, bundlers search and replace the actual require call
257
269
// in a way that makes this line throw at bundle time, not runtime, catching here will make bundling succeed
258
270
mongodbClientEncryption = require ( 'mongodb-client-encryption' ) ;
259
- } catch ( cause ) {
271
+ } catch ( error ) {
260
272
const kModuleError = new MongoMissingDependencyError (
261
273
'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' }
263
275
) ;
264
276
return { kModuleError } ;
265
277
}
0 commit comments