forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeps.ts
More file actions
227 lines (205 loc) · 7.51 KB
/
deps.ts
File metadata and controls
227 lines (205 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { type Stream } from './cmap/connect';
import { MongoMissingDependencyError } from './error';
import type { Callback } from './utils';
function makeErrorModule(error: any) {
const props = error ? { kModuleError: error } : {};
return new Proxy(props, {
get: (_: any, key: any) => {
if (key === 'kModuleError') {
return error;
}
throw error;
},
set: () => {
throw error;
}
});
}
export type Kerberos = typeof import('kerberos') | { kModuleError: MongoMissingDependencyError };
export function getKerberos(): Kerberos {
let kerberos: Kerberos;
try {
// Ensure you always wrap an optional require in the try block NODE-3199
// eslint-disable-next-line @typescript-eslint/no-require-imports
kerberos = require('kerberos');
} catch (error) {
kerberos = makeErrorModule(
new MongoMissingDependencyError(
'Optional module `kerberos` not found. Please install it to enable kerberos authentication',
{ cause: error, dependencyName: 'kerberos' }
)
);
}
return kerberos;
}
export interface KerberosClient {
step(challenge: string): Promise<string>;
step(challenge: string, callback: Callback<string>): void;
wrap(challenge: string, options: { user: string }): Promise<string>;
wrap(challenge: string, options: { user: string }, callback: Callback<string>): void;
unwrap(challenge: string): Promise<string>;
unwrap(challenge: string, callback: Callback<string>): void;
}
type ZStandardLib = {
/**
* Compress using zstd.
* @param buf - Buffer to be compressed.
*/
compress(buf: Buffer, level?: number): Promise<Buffer>;
/**
* Decompress using zstd.
*/
decompress(buf: Buffer): Promise<Buffer>;
};
export type ZStandard = ZStandardLib | { kModuleError: MongoMissingDependencyError };
export function getZstdLibrary(): ZStandardLib | { kModuleError: MongoMissingDependencyError } {
let ZStandard: ZStandardLib | { kModuleError: MongoMissingDependencyError };
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
ZStandard = require('@mongodb-js/zstd');
} catch (error) {
ZStandard = makeErrorModule(
new MongoMissingDependencyError(
'Optional module `@mongodb-js/zstd` not found. Please install it to enable zstd compression',
{ cause: error, dependencyName: 'zstd' }
)
);
}
return ZStandard;
}
/**
* @public
* Copy of the AwsCredentialIdentityProvider interface from [`smithy/types`](https://socket.dev/npm/package/\@smithy/types/files/1.1.1/dist-types/identity/awsCredentialIdentity.d.ts),
* the return type of the aws-sdk's `fromNodeProviderChain().provider()`.
*/
export interface AWSCredentials {
accessKeyId: string;
secretAccessKey: string;
sessionToken?: string;
expiration?: Date;
}
export type CredentialProvider = {
fromNodeProviderChain(
this: void,
options: { clientConfig: { region: string } }
): () => Promise<AWSCredentials>;
fromNodeProviderChain(this: void): () => Promise<AWSCredentials>;
};
export function getAwsCredentialProvider():
| CredentialProvider
| { kModuleError: MongoMissingDependencyError } {
try {
// Ensure you always wrap an optional require in the try block NODE-3199
// eslint-disable-next-line @typescript-eslint/no-require-imports
const credentialProvider = require('@aws-sdk/credential-providers');
return credentialProvider;
} catch (error) {
return makeErrorModule(
new MongoMissingDependencyError(
'Optional module `@aws-sdk/credential-providers` not found.' +
' Please install it to enable getting aws credentials via the official sdk.',
{ cause: error, dependencyName: '@aws-sdk/credential-providers' }
)
);
}
}
/** @internal */
export type GcpMetadata =
| typeof import('gcp-metadata')
| { kModuleError: MongoMissingDependencyError };
export function getGcpMetadata(): GcpMetadata {
try {
// Ensure you always wrap an optional require in the try block NODE-3199
// eslint-disable-next-line @typescript-eslint/no-require-imports
const credentialProvider = require('gcp-metadata');
return credentialProvider;
} catch (error) {
return makeErrorModule(
new MongoMissingDependencyError(
'Optional module `gcp-metadata` not found.' +
' Please install it to enable getting gcp credentials via the official sdk.',
{ cause: error, dependencyName: 'gcp-metadata' }
)
);
}
}
/** @internal */
export type SnappyLib = {
/**
* In order to support both we must check the return value of the function
* @param buf - Buffer to be compressed
*/
compress(buf: Buffer): Promise<Buffer>;
/**
* In order to support both we must check the return value of the function
* @param buf - Buffer to be compressed
*/
uncompress(buf: Buffer, opt: { asBuffer: true }): Promise<Buffer>;
};
export function getSnappy(): SnappyLib | { kModuleError: MongoMissingDependencyError } {
try {
// Ensure you always wrap an optional require in the try block NODE-3199
// eslint-disable-next-line @typescript-eslint/no-require-imports
const value = require('snappy');
return value;
} catch (error) {
const kModuleError = new MongoMissingDependencyError(
'Optional module `snappy` not found. Please install it to enable snappy compression',
{ cause: error, dependencyName: 'snappy' }
);
return { kModuleError };
}
}
export type SocksLib = {
SocksClient: {
createConnection(options: {
command: 'connect';
destination: { host: string; port: number };
proxy: {
/** host and port are ignored because we pass existing_socket */
host: 'iLoveJavaScript';
port: 0;
type: 5;
userId?: string;
password?: string;
};
timeout?: number;
/** We always create our own socket, and pass it to this API for proxy negotiation */
existing_socket: Stream;
}): Promise<{ socket: Stream }>;
};
};
export function getSocks(): SocksLib | { kModuleError: MongoMissingDependencyError } {
try {
// Ensure you always wrap an optional require in the try block NODE-3199
// eslint-disable-next-line @typescript-eslint/no-require-imports
const value = require('socks');
return value;
} catch (error) {
const kModuleError = new MongoMissingDependencyError(
'Optional module `socks` not found. Please install it to connections over a SOCKS5 proxy',
{ cause: error, dependencyName: 'socks' }
);
return { kModuleError };
}
}
/** A utility function to get the instance of mongodb-client-encryption, if it exists. */
export function getMongoDBClientEncryption():
| typeof import('mongodb-client-encryption')
| { kModuleError: MongoMissingDependencyError } {
let mongodbClientEncryption = null;
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
// eslint-disable-next-line @typescript-eslint/no-require-imports
mongodbClientEncryption = require('mongodb-client-encryption');
} catch (error) {
const kModuleError = new MongoMissingDependencyError(
'Optional module `mongodb-client-encryption` not found. Please install it to use auto encryption or ClientEncryption.',
{ cause: error, dependencyName: 'mongodb-client-encryption' }
);
return { kModuleError };
}
return mongodbClientEncryption;
}