mongodb 5.6.0 → 5.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/README.md +12 -0
  2. package/lib/admin.js.map +1 -1
  3. package/lib/bulk/common.js +2 -2
  4. package/lib/bulk/common.js.map +1 -1
  5. package/lib/bulk/ordered.js.map +1 -1
  6. package/lib/bulk/unordered.js.map +1 -1
  7. package/lib/change_stream.js +1 -1
  8. package/lib/change_stream.js.map +1 -1
  9. package/lib/cmap/auth/gssapi.js +13 -3
  10. package/lib/cmap/auth/gssapi.js.map +1 -1
  11. package/lib/cmap/auth/mongo_credentials.js +9 -2
  12. package/lib/cmap/auth/mongo_credentials.js.map +1 -1
  13. package/lib/cmap/auth/mongocr.js.map +1 -1
  14. package/lib/cmap/auth/mongodb_aws.js +4 -41
  15. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  16. package/lib/cmap/auth/mongodb_oidc/azure_service_workflow.js +73 -0
  17. package/lib/cmap/auth/mongodb_oidc/azure_service_workflow.js.map +1 -0
  18. package/lib/cmap/auth/mongodb_oidc/azure_token_cache.js +49 -0
  19. package/lib/cmap/auth/mongodb_oidc/azure_token_cache.js.map +1 -0
  20. package/lib/cmap/auth/mongodb_oidc/cache.js +29 -2
  21. package/lib/cmap/auth/mongodb_oidc/cache.js.map +1 -1
  22. package/lib/cmap/auth/mongodb_oidc/callback_lock_cache.js +9 -3
  23. package/lib/cmap/auth/mongodb_oidc/callback_lock_cache.js.map +1 -1
  24. package/lib/cmap/auth/mongodb_oidc/callback_workflow.js +1 -1
  25. package/lib/cmap/auth/mongodb_oidc/callback_workflow.js.map +1 -1
  26. package/lib/cmap/auth/mongodb_oidc/service_workflow.js +2 -2
  27. package/lib/cmap/auth/mongodb_oidc/service_workflow.js.map +1 -1
  28. package/lib/cmap/auth/mongodb_oidc/token_entry_cache.js +9 -18
  29. package/lib/cmap/auth/mongodb_oidc/token_entry_cache.js.map +1 -1
  30. package/lib/cmap/auth/mongodb_oidc.js +2 -0
  31. package/lib/cmap/auth/mongodb_oidc.js.map +1 -1
  32. package/lib/cmap/auth/plain.js.map +1 -1
  33. package/lib/cmap/auth/scram.js +4 -2
  34. package/lib/cmap/auth/scram.js.map +1 -1
  35. package/lib/cmap/auth/x509.js.map +1 -1
  36. package/lib/cmap/command_monitoring_events.js.map +1 -1
  37. package/lib/cmap/connect.js +4 -7
  38. package/lib/cmap/connect.js.map +1 -1
  39. package/lib/cmap/connection.js +1 -1
  40. package/lib/cmap/connection.js.map +1 -1
  41. package/lib/cmap/connection_pool.js +1 -1
  42. package/lib/cmap/connection_pool.js.map +1 -1
  43. package/lib/cmap/message_stream.js.map +1 -1
  44. package/lib/cmap/metrics.js +1 -1
  45. package/lib/cmap/metrics.js.map +1 -1
  46. package/lib/cmap/wire_protocol/compression.js +48 -22
  47. package/lib/cmap/wire_protocol/compression.js.map +1 -1
  48. package/lib/collection.js +0 -28
  49. package/lib/collection.js.map +1 -1
  50. package/lib/connection_string.js +8 -2
  51. package/lib/connection_string.js.map +1 -1
  52. package/lib/cursor/abstract_cursor.js +99 -73
  53. package/lib/cursor/abstract_cursor.js.map +1 -1
  54. package/lib/cursor/aggregation_cursor.js.map +1 -1
  55. package/lib/cursor/find_cursor.js.map +1 -1
  56. package/lib/cursor/list_collections_cursor.js.map +1 -1
  57. package/lib/cursor/list_indexes_cursor.js.map +1 -1
  58. package/lib/cursor/list_search_indexes_cursor.js +1 -1
  59. package/lib/cursor/list_search_indexes_cursor.js.map +1 -1
  60. package/lib/cursor/run_command_cursor.js.map +1 -1
  61. package/lib/db.js +1 -1
  62. package/lib/db.js.map +1 -1
  63. package/lib/deps.js +31 -13
  64. package/lib/deps.js.map +1 -1
  65. package/lib/encrypter.js.map +1 -1
  66. package/lib/error.js +20 -2
  67. package/lib/error.js.map +1 -1
  68. package/lib/gridfs/download.js +1 -1
  69. package/lib/gridfs/download.js.map +1 -1
  70. package/lib/gridfs/index.js +1 -1
  71. package/lib/gridfs/index.js.map +1 -1
  72. package/lib/gridfs/upload.js +5 -17
  73. package/lib/gridfs/upload.js.map +1 -1
  74. package/lib/index.js +4 -3
  75. package/lib/index.js.map +1 -1
  76. package/lib/mongo_client.js.map +1 -1
  77. package/lib/operations/add_user.js +1 -1
  78. package/lib/operations/add_user.js.map +1 -1
  79. package/lib/operations/aggregate.js +3 -2
  80. package/lib/operations/aggregate.js.map +1 -1
  81. package/lib/operations/bulk_write.js +2 -2
  82. package/lib/operations/bulk_write.js.map +1 -1
  83. package/lib/operations/collections.js +2 -2
  84. package/lib/operations/collections.js.map +1 -1
  85. package/lib/operations/command.js +2 -2
  86. package/lib/operations/command.js.map +1 -1
  87. package/lib/operations/common_functions.js.map +1 -1
  88. package/lib/operations/count.js +1 -1
  89. package/lib/operations/count.js.map +1 -1
  90. package/lib/operations/count_documents.js +2 -2
  91. package/lib/operations/count_documents.js.map +1 -1
  92. package/lib/operations/create_collection.js +5 -5
  93. package/lib/operations/create_collection.js.map +1 -1
  94. package/lib/operations/delete.js +5 -5
  95. package/lib/operations/delete.js.map +1 -1
  96. package/lib/operations/distinct.js +1 -1
  97. package/lib/operations/distinct.js.map +1 -1
  98. package/lib/operations/drop.js +2 -2
  99. package/lib/operations/drop.js.map +1 -1
  100. package/lib/operations/estimated_document_count.js +1 -1
  101. package/lib/operations/estimated_document_count.js.map +1 -1
  102. package/lib/operations/eval.js +1 -1
  103. package/lib/operations/eval.js.map +1 -1
  104. package/lib/operations/execute_operation.js +5 -5
  105. package/lib/operations/execute_operation.js.map +1 -1
  106. package/lib/operations/find.js +1 -1
  107. package/lib/operations/find.js.map +1 -1
  108. package/lib/operations/find_and_modify.js +3 -2
  109. package/lib/operations/find_and_modify.js.map +1 -1
  110. package/lib/operations/get_more.js +2 -2
  111. package/lib/operations/get_more.js.map +1 -1
  112. package/lib/operations/indexes.js +16 -16
  113. package/lib/operations/indexes.js.map +1 -1
  114. package/lib/operations/insert.js +6 -6
  115. package/lib/operations/insert.js.map +1 -1
  116. package/lib/operations/is_capped.js +2 -2
  117. package/lib/operations/is_capped.js.map +1 -1
  118. package/lib/operations/kill_cursors.js +2 -2
  119. package/lib/operations/kill_cursors.js.map +1 -1
  120. package/lib/operations/list_collections.js +1 -1
  121. package/lib/operations/list_collections.js.map +1 -1
  122. package/lib/operations/list_databases.js +3 -3
  123. package/lib/operations/list_databases.js.map +1 -1
  124. package/lib/operations/operation.js +13 -4
  125. package/lib/operations/operation.js.map +1 -1
  126. package/lib/operations/options_operation.js +2 -2
  127. package/lib/operations/options_operation.js.map +1 -1
  128. package/lib/operations/profiling_level.js +1 -1
  129. package/lib/operations/profiling_level.js.map +1 -1
  130. package/lib/operations/remove_user.js +1 -1
  131. package/lib/operations/remove_user.js.map +1 -1
  132. package/lib/operations/rename.js +2 -2
  133. package/lib/operations/rename.js.map +1 -1
  134. package/lib/operations/run_command.js +1 -1
  135. package/lib/operations/run_command.js.map +1 -1
  136. package/lib/operations/search_indexes/create.js +2 -2
  137. package/lib/operations/search_indexes/create.js.map +1 -1
  138. package/lib/operations/search_indexes/drop.js +2 -2
  139. package/lib/operations/search_indexes/drop.js.map +1 -1
  140. package/lib/operations/search_indexes/update.js +2 -2
  141. package/lib/operations/search_indexes/update.js.map +1 -1
  142. package/lib/operations/set_profiling_level.js +1 -1
  143. package/lib/operations/set_profiling_level.js.map +1 -1
  144. package/lib/operations/stats.js +2 -2
  145. package/lib/operations/stats.js.map +1 -1
  146. package/lib/operations/update.js +7 -7
  147. package/lib/operations/update.js.map +1 -1
  148. package/lib/operations/validate_collection.js +1 -1
  149. package/lib/operations/validate_collection.js.map +1 -1
  150. package/lib/read_preference.js +1 -1
  151. package/lib/read_preference.js.map +1 -1
  152. package/lib/sdam/monitor.js.map +1 -1
  153. package/lib/sdam/server.js +3 -1
  154. package/lib/sdam/server.js.map +1 -1
  155. package/lib/sdam/server_description.js.map +1 -1
  156. package/lib/sdam/srv_polling.js +1 -1
  157. package/lib/sdam/srv_polling.js.map +1 -1
  158. package/lib/sdam/topology.js +1 -1
  159. package/lib/sdam/topology.js.map +1 -1
  160. package/lib/sdam/topology_description.js.map +1 -1
  161. package/lib/sessions.js +3 -2
  162. package/lib/sessions.js.map +1 -1
  163. package/lib/transactions.js.map +1 -1
  164. package/lib/utils.js +39 -0
  165. package/lib/utils.js.map +1 -1
  166. package/lib/write_concern.js +24 -9
  167. package/lib/write_concern.js.map +1 -1
  168. package/mongodb.d.ts +210 -39
  169. package/package.json +16 -7
  170. package/src/admin.ts +6 -6
  171. package/src/bulk/common.ts +13 -9
  172. package/src/bulk/ordered.ts +1 -1
  173. package/src/bulk/unordered.ts +7 -1
  174. package/src/change_stream.ts +19 -4
  175. package/src/cmap/auth/gssapi.ts +17 -5
  176. package/src/cmap/auth/mongo_credentials.ts +21 -3
  177. package/src/cmap/auth/mongocr.ts +1 -1
  178. package/src/cmap/auth/mongodb_aws.ts +2 -62
  179. package/src/cmap/auth/mongodb_oidc/azure_service_workflow.ts +86 -0
  180. package/src/cmap/auth/mongodb_oidc/azure_token_cache.ts +51 -0
  181. package/src/cmap/auth/mongodb_oidc/cache.ts +37 -1
  182. package/src/cmap/auth/mongodb_oidc/callback_lock_cache.ts +10 -3
  183. package/src/cmap/auth/mongodb_oidc/callback_workflow.ts +1 -1
  184. package/src/cmap/auth/mongodb_oidc/service_workflow.ts +3 -3
  185. package/src/cmap/auth/mongodb_oidc/token_entry_cache.ts +11 -21
  186. package/src/cmap/auth/mongodb_oidc.ts +4 -2
  187. package/src/cmap/auth/plain.ts +1 -1
  188. package/src/cmap/auth/scram.ts +8 -4
  189. package/src/cmap/auth/x509.ts +1 -1
  190. package/src/cmap/command_monitoring_events.ts +1 -1
  191. package/src/cmap/connect.ts +12 -10
  192. package/src/cmap/connection.ts +7 -7
  193. package/src/cmap/connection_pool.ts +3 -3
  194. package/src/cmap/message_stream.ts +10 -4
  195. package/src/cmap/wire_protocol/compression.ts +49 -27
  196. package/src/collection.ts +85 -51
  197. package/src/connection_string.ts +28 -11
  198. package/src/cursor/abstract_cursor.ts +106 -77
  199. package/src/cursor/aggregation_cursor.ts +2 -2
  200. package/src/cursor/find_cursor.ts +5 -5
  201. package/src/cursor/list_collections_cursor.ts +3 -3
  202. package/src/cursor/list_indexes_cursor.ts +2 -2
  203. package/src/cursor/list_search_indexes_cursor.ts +2 -2
  204. package/src/cursor/run_command_cursor.ts +2 -2
  205. package/src/db.ts +21 -18
  206. package/src/deps.ts +32 -18
  207. package/src/encrypter.ts +2 -2
  208. package/src/error.ts +19 -1
  209. package/src/gridfs/index.ts +10 -6
  210. package/src/gridfs/upload.ts +5 -18
  211. package/src/index.ts +8 -1
  212. package/src/mongo_client.ts +44 -17
  213. package/src/operations/add_user.ts +3 -3
  214. package/src/operations/aggregate.ts +6 -5
  215. package/src/operations/bulk_write.ts +3 -3
  216. package/src/operations/collections.ts +3 -3
  217. package/src/operations/command.ts +6 -6
  218. package/src/operations/common_functions.ts +1 -1
  219. package/src/operations/count.ts +2 -2
  220. package/src/operations/count_documents.ts +3 -3
  221. package/src/operations/create_collection.ts +8 -6
  222. package/src/operations/delete.ts +11 -7
  223. package/src/operations/distinct.ts +3 -3
  224. package/src/operations/drop.ts +3 -3
  225. package/src/operations/estimated_document_count.ts +2 -2
  226. package/src/operations/eval.ts +3 -3
  227. package/src/operations/execute_operation.ts +15 -15
  228. package/src/operations/find.ts +10 -5
  229. package/src/operations/find_and_modify.ts +22 -5
  230. package/src/operations/get_more.ts +9 -4
  231. package/src/operations/indexes.ts +30 -22
  232. package/src/operations/insert.ts +8 -8
  233. package/src/operations/is_capped.ts +3 -3
  234. package/src/operations/kill_cursors.ts +12 -3
  235. package/src/operations/list_collections.ts +3 -3
  236. package/src/operations/list_databases.ts +6 -5
  237. package/src/operations/operation.ts +21 -19
  238. package/src/operations/options_operation.ts +3 -3
  239. package/src/operations/profiling_level.ts +2 -2
  240. package/src/operations/remove_user.ts +2 -2
  241. package/src/operations/rename.ts +3 -3
  242. package/src/operations/run_command.ts +3 -3
  243. package/src/operations/search_indexes/create.ts +11 -5
  244. package/src/operations/search_indexes/drop.ts +7 -3
  245. package/src/operations/search_indexes/update.ts +7 -3
  246. package/src/operations/set_profiling_level.ts +2 -2
  247. package/src/operations/stats.ts +3 -3
  248. package/src/operations/update.ts +10 -10
  249. package/src/operations/validate_collection.ts +2 -2
  250. package/src/sdam/monitor.ts +4 -4
  251. package/src/sdam/server.ts +26 -10
  252. package/src/sdam/server_description.ts +2 -2
  253. package/src/sdam/topology.ts +9 -9
  254. package/src/sdam/topology_description.ts +1 -1
  255. package/src/sessions.ts +14 -8
  256. package/src/transactions.ts +1 -1
  257. package/src/utils.ts +70 -2
  258. package/src/write_concern.ts +64 -17
  259. package/tsconfig.json +0 -1
@@ -39,7 +39,7 @@ export class CallbackLockCache extends Cache<CallbacksEntry> {
39
39
  * Get the callbacks for the connection and credentials. If an entry does not
40
40
  * exist a new one will get set.
41
41
  */
42
- getCallbacks(connection: Connection, credentials: MongoCredentials): CallbacksEntry {
42
+ getEntry(connection: Connection, credentials: MongoCredentials): CallbacksEntry {
43
43
  const requestCallback = credentials.mechanismProperties.REQUEST_TOKEN_CALLBACK;
44
44
  const refreshCallback = credentials.mechanismProperties.REFRESH_TOKEN_CALLBACK;
45
45
  if (!requestCallback) {
@@ -51,13 +51,13 @@ export class CallbackLockCache extends Cache<CallbacksEntry> {
51
51
  if (entry) {
52
52
  return entry;
53
53
  }
54
- return this.setCallbacks(key, callbackHash, requestCallback, refreshCallback);
54
+ return this.addEntry(key, callbackHash, requestCallback, refreshCallback);
55
55
  }
56
56
 
57
57
  /**
58
58
  * Set locked callbacks on for connection and credentials.
59
59
  */
60
- private setCallbacks(
60
+ private addEntry(
61
61
  key: string,
62
62
  callbackHash: string,
63
63
  requestCallback: OIDCRequestFunction,
@@ -71,6 +71,13 @@ export class CallbackLockCache extends Cache<CallbacksEntry> {
71
71
  this.entries.set(key, entry);
72
72
  return entry;
73
73
  }
74
+
75
+ /**
76
+ * Create a cache key from the address and username.
77
+ */
78
+ cacheKey(address: string, username: string, callbackHash: string): string {
79
+ return this.hashedCacheKey(address, username, callbackHash);
80
+ }
74
81
  }
75
82
 
76
83
  /**
@@ -65,7 +65,7 @@ export class CallbackWorkflow implements Workflow {
65
65
  response?: Document
66
66
  ): Promise<Document> {
67
67
  // Get the callbacks with locks from the callback lock cache.
68
- const { requestCallback, refreshCallback, callbackHash } = this.callbackCache.getCallbacks(
68
+ const { requestCallback, refreshCallback, callbackHash } = this.callbackCache.getEntry(
69
69
  connection,
70
70
  credentials
71
71
  );
@@ -16,7 +16,7 @@ export abstract class ServiceWorkflow implements Workflow {
16
16
  * and then attempts to read the token from that path.
17
17
  */
18
18
  async execute(connection: Connection, credentials: MongoCredentials): Promise<Document> {
19
- const token = await this.getToken();
19
+ const token = await this.getToken(credentials);
20
20
  const command = commandDocument(token);
21
21
  return connection.commandAsync(ns(credentials.source), command, undefined);
22
22
  }
@@ -25,7 +25,7 @@ export abstract class ServiceWorkflow implements Workflow {
25
25
  * Get the document to add for speculative authentication.
26
26
  */
27
27
  async speculativeAuth(credentials: MongoCredentials): Promise<Document> {
28
- const token = await this.getToken();
28
+ const token = await this.getToken(credentials);
29
29
  const document = commandDocument(token);
30
30
  document.db = credentials.source;
31
31
  return { speculativeAuthenticate: document };
@@ -34,7 +34,7 @@ export abstract class ServiceWorkflow implements Workflow {
34
34
  /**
35
35
  * Get the token from the environment or endpoint.
36
36
  */
37
- abstract getToken(): Promise<string>;
37
+ abstract getToken(credentials: MongoCredentials): Promise<string>;
38
38
  }
39
39
 
40
40
  /**
@@ -1,31 +1,21 @@
1
1
  import type { IdPServerInfo, IdPServerResponse } from '../mongodb_oidc';
2
- import { Cache } from './cache';
2
+ import { Cache, ExpiringCacheEntry } from './cache';
3
3
 
4
- /* 5 minutes in milliseconds */
5
- const EXPIRATION_BUFFER_MS = 300000;
6
4
  /* Default expiration is now for when no expiration provided */
7
5
  const DEFAULT_EXPIRATION_SECS = 0;
6
+
8
7
  /** @internal */
9
- export class TokenEntry {
8
+ export class TokenEntry extends ExpiringCacheEntry {
10
9
  tokenResult: IdPServerResponse;
11
10
  serverInfo: IdPServerInfo;
12
- expiration: number;
13
11
 
14
12
  /**
15
13
  * Instantiate the entry.
16
14
  */
17
15
  constructor(tokenResult: IdPServerResponse, serverInfo: IdPServerInfo, expiration: number) {
16
+ super(expiration);
18
17
  this.tokenResult = tokenResult;
19
18
  this.serverInfo = serverInfo;
20
- this.expiration = expiration;
21
- }
22
-
23
- /**
24
- * The entry is still valid if the expiration is more than
25
- * 5 minutes from the expiration time.
26
- */
27
- isValid() {
28
- return this.expiration - Date.now() > EXPIRATION_BUFFER_MS;
29
19
  }
30
20
  }
31
21
 
@@ -47,7 +37,7 @@ export class TokenEntryCache extends Cache<TokenEntry> {
47
37
  const entry = new TokenEntry(
48
38
  tokenResult,
49
39
  serverInfo,
50
- expirationTime(tokenResult.expiresInSeconds)
40
+ tokenResult.expiresInSeconds ?? DEFAULT_EXPIRATION_SECS
51
41
  );
52
42
  this.entries.set(this.cacheKey(address, username, callbackHash), entry);
53
43
  return entry;
@@ -77,11 +67,11 @@ export class TokenEntryCache extends Cache<TokenEntry> {
77
67
  }
78
68
  }
79
69
  }
80
- }
81
70
 
82
- /**
83
- * Get an expiration time in milliseconds past epoch. Defaults to immediate.
84
- */
85
- function expirationTime(expiresInSeconds?: number): number {
86
- return Date.now() + (expiresInSeconds ?? DEFAULT_EXPIRATION_SECS) * 1000;
71
+ /**
72
+ * Create a cache key from the address and username.
73
+ */
74
+ cacheKey(address: string, username: string, callbackHash: string): string {
75
+ return this.hashedCacheKey(address, username, callbackHash);
76
+ }
87
77
  }
@@ -3,9 +3,10 @@ import type { Document } from 'bson';
3
3
  import { MongoInvalidArgumentError, MongoMissingCredentialsError } from '../../error';
4
4
  import type { HandshakeDocument } from '../connect';
5
5
  import type { Connection } from '../connection';
6
- import { AuthContext, AuthProvider } from './auth_provider';
6
+ import { type AuthContext, AuthProvider } from './auth_provider';
7
7
  import type { MongoCredentials } from './mongo_credentials';
8
8
  import { AwsServiceWorkflow } from './mongodb_oidc/aws_service_workflow';
9
+ import { AzureServiceWorkflow } from './mongodb_oidc/azure_service_workflow';
9
10
  import { CallbackWorkflow } from './mongodb_oidc/callback_workflow';
10
11
 
11
12
  /** Error when credentials are missing. */
@@ -60,7 +61,7 @@ export type OIDCRefreshFunction = (
60
61
  context: OIDCCallbackContext
61
62
  ) => Promise<IdPServerResponse>;
62
63
 
63
- type ProviderName = 'aws' | 'callback';
64
+ type ProviderName = 'aws' | 'azure' | 'callback';
64
65
 
65
66
  export interface Workflow {
66
67
  /**
@@ -84,6 +85,7 @@ export interface Workflow {
84
85
  export const OIDC_WORKFLOWS: Map<ProviderName, Workflow> = new Map();
85
86
  OIDC_WORKFLOWS.set('callback', new CallbackWorkflow());
86
87
  OIDC_WORKFLOWS.set('aws', new AwsServiceWorkflow());
88
+ OIDC_WORKFLOWS.set('azure', new AzureServiceWorkflow());
87
89
 
88
90
  /**
89
91
  * OIDC auth provider.
@@ -1,7 +1,7 @@
1
1
  import { Binary } from '../../bson';
2
2
  import { MongoMissingCredentialsError } from '../../error';
3
3
  import { ns } from '../../utils';
4
- import { AuthContext, AuthProvider } from './auth_provider';
4
+ import { type AuthContext, AuthProvider } from './auth_provider';
5
5
 
6
6
  export class Plain extends AuthProvider {
7
7
  override async auth(authContext: AuthContext): Promise<void> {
@@ -1,7 +1,7 @@
1
1
  import * as crypto from 'crypto';
2
2
  import { promisify } from 'util';
3
3
 
4
- import { Binary, Document } from '../../bson';
4
+ import { Binary, type Document } from '../../bson';
5
5
  import { saslprep } from '../../deps';
6
6
  import {
7
7
  MongoInvalidArgumentError,
@@ -10,7 +10,7 @@ import {
10
10
  } from '../../error';
11
11
  import { emitWarning, ns } from '../../utils';
12
12
  import type { HandshakeDocument } from '../connect';
13
- import { AuthContext, AuthProvider } from './auth_provider';
13
+ import { type AuthContext, AuthProvider } from './auth_provider';
14
14
  import type { MongoCredentials } from './mongo_credentials';
15
15
  import { AuthMechanism } from './providers';
16
16
 
@@ -34,7 +34,10 @@ class ScramSHA extends AuthProvider {
34
34
  if (!credentials) {
35
35
  throw new MongoMissingCredentialsError('AuthContext must provide credentials.');
36
36
  }
37
- if (cryptoMethod === 'sha256' && saslprep == null) {
37
+ if (
38
+ cryptoMethod === 'sha256' &&
39
+ ('kModuleError' in saslprep || typeof saslprep !== 'function')
40
+ ) {
38
41
  emitWarning('Warning: no saslprep library specified. Passwords will not be sanitized');
39
42
  }
40
43
 
@@ -140,7 +143,8 @@ async function continueScramConversation(
140
143
 
141
144
  let processedPassword;
142
145
  if (cryptoMethod === 'sha256') {
143
- processedPassword = 'kModuleError' in saslprep ? password : saslprep(password);
146
+ processedPassword =
147
+ 'kModuleError' in saslprep || typeof saslprep !== 'function' ? password : saslprep(password);
144
148
  } else {
145
149
  processedPassword = passwordDigest(username, password);
146
150
  }
@@ -2,7 +2,7 @@ import type { Document } from '../../bson';
2
2
  import { MongoMissingCredentialsError } from '../../error';
3
3
  import { ns } from '../../utils';
4
4
  import type { HandshakeDocument } from '../connect';
5
- import { AuthContext, AuthProvider } from './auth_provider';
5
+ import { type AuthContext, AuthProvider } from './auth_provider';
6
6
  import type { MongoCredentials } from './mongo_credentials';
7
7
 
8
8
  export class X509 extends AuthProvider {
@@ -7,7 +7,7 @@ import {
7
7
  LEGACY_HELLO_COMMAND_CAMEL_CASE
8
8
  } from '../constants';
9
9
  import { calculateDurationInMs, deepCopy } from '../utils';
10
- import { Msg, WriteProtocolMessageType } from './commands';
10
+ import { Msg, type WriteProtocolMessageType } from './commands';
11
11
  import type { Connection } from './connection';
12
12
 
13
13
  /**
@@ -5,7 +5,6 @@ import type { ConnectionOptions as TLSConnectionOpts, TLSSocket } from 'tls';
5
5
  import * as tls from 'tls';
6
6
 
7
7
  import type { Document } from '../bson';
8
- import { Int32 } from '../bson';
9
8
  import { LEGACY_HELLO_COMMAND } from '../constants';
10
9
  import {
11
10
  MongoCompatibilityError,
@@ -17,8 +16,8 @@ import {
17
16
  MongoRuntimeError,
18
17
  needsRetryableWriteLabel
19
18
  } from '../error';
20
- import { Callback, HostAddress, ns } from '../utils';
21
- import { AuthContext, AuthProvider } from './auth/auth_provider';
19
+ import { type Callback, HostAddress, ns } from '../utils';
20
+ import { AuthContext, type AuthProvider } from './auth/auth_provider';
22
21
  import { GSSAPI } from './auth/gssapi';
23
22
  import { MongoCR } from './auth/mongocr';
24
23
  import { MongoDBAWS } from './auth/mongodb_aws';
@@ -27,7 +26,12 @@ import { Plain } from './auth/plain';
27
26
  import { AuthMechanism } from './auth/providers';
28
27
  import { ScramSHA1, ScramSHA256 } from './auth/scram';
29
28
  import { X509 } from './auth/x509';
30
- import { CommandOptions, Connection, ConnectionOptions, CryptoConnection } from './connection';
29
+ import {
30
+ type CommandOptions,
31
+ Connection,
32
+ type ConnectionOptions,
33
+ CryptoConnection
34
+ } from './connection';
31
35
  import type { ClientMetadata } from './handshake/client_metadata';
32
36
  import {
33
37
  MAX_SUPPORTED_SERVER_VERSION,
@@ -75,14 +79,12 @@ export function connect(options: ConnectionOptions, callback: Callback<Connectio
75
79
  }
76
80
 
77
81
  function checkSupportedServer(hello: Document, options: ConnectionOptions) {
82
+ const maxWireVersion = Number(hello.maxWireVersion);
83
+ const minWireVersion = Number(hello.minWireVersion);
78
84
  const serverVersionHighEnough =
79
- hello &&
80
- (typeof hello.maxWireVersion === 'number' || hello.maxWireVersion instanceof Int32) &&
81
- hello.maxWireVersion >= MIN_SUPPORTED_WIRE_VERSION;
85
+ !Number.isNaN(maxWireVersion) && maxWireVersion >= MIN_SUPPORTED_WIRE_VERSION;
82
86
  const serverVersionLowEnough =
83
- hello &&
84
- (typeof hello.minWireVersion === 'number' || hello.minWireVersion instanceof Int32) &&
85
- hello.minWireVersion <= MAX_SUPPORTED_WIRE_VERSION;
87
+ !Number.isNaN(minWireVersion) && minWireVersion <= MAX_SUPPORTED_WIRE_VERSION;
86
88
 
87
89
  if (serverVersionHighEnough) {
88
90
  if (serverVersionLowEnough) {
@@ -23,15 +23,15 @@ import {
23
23
  MongoWriteConcernError
24
24
  } from '../error';
25
25
  import type { ServerApi, SupportedNodeConnectionOptions } from '../mongo_client';
26
- import { CancellationToken, TypedEventEmitter } from '../mongo_types';
26
+ import { type CancellationToken, TypedEventEmitter } from '../mongo_types';
27
27
  import type { ReadPreferenceLike } from '../read_preference';
28
- import { applySession, ClientSession, updateSessionFromResponse } from '../sessions';
28
+ import { applySession, type ClientSession, updateSessionFromResponse } from '../sessions';
29
29
  import {
30
30
  calculateDurationInMs,
31
- Callback,
31
+ type Callback,
32
32
  HostAddress,
33
33
  maxWireVersion,
34
- MongoDBNamespace,
34
+ type MongoDBNamespace,
35
35
  now,
36
36
  uuidV4
37
37
  } from '../utils';
@@ -43,11 +43,11 @@ import {
43
43
  CommandStartedEvent,
44
44
  CommandSucceededEvent
45
45
  } from './command_monitoring_events';
46
- import { BinMsg, Msg, Query, Response, WriteProtocolMessageType } from './commands';
46
+ import { type BinMsg, Msg, Query, type Response, type WriteProtocolMessageType } from './commands';
47
47
  import type { Stream } from './connect';
48
48
  import type { ClientMetadata } from './handshake/client_metadata';
49
- import { MessageStream, OperationDescription } from './message_stream';
50
- import { StreamDescription, StreamDescriptionOptions } from './stream_description';
49
+ import { MessageStream, type OperationDescription } from './message_stream';
50
+ import { StreamDescription, type StreamDescriptionOptions } from './stream_description';
51
51
  import { getReadPreference, isSharded } from './wire_protocol/shared';
52
52
 
53
53
  /** @internal */
@@ -16,7 +16,7 @@ import {
16
16
  CONNECTION_READY
17
17
  } from '../constants';
18
18
  import {
19
- AnyError,
19
+ type AnyError,
20
20
  MONGODB_ERROR_CODES,
21
21
  MongoError,
22
22
  MongoInvalidArgumentError,
@@ -27,9 +27,9 @@ import {
27
27
  } from '../error';
28
28
  import { CancellationToken, TypedEventEmitter } from '../mongo_types';
29
29
  import type { Server } from '../sdam/server';
30
- import { Callback, eachAsync, List, makeCounter } from '../utils';
30
+ import { type Callback, eachAsync, List, makeCounter } from '../utils';
31
31
  import { AUTH_PROVIDERS, connect } from './connect';
32
- import { Connection, ConnectionEvents, ConnectionOptions } from './connection';
32
+ import { Connection, type ConnectionEvents, type ConnectionOptions } from './connection';
33
33
  import {
34
34
  ConnectionCheckedInEvent,
35
35
  ConnectionCheckedOutEvent,
@@ -1,14 +1,20 @@
1
- import { Duplex, DuplexOptions } from 'stream';
1
+ import { Duplex, type DuplexOptions } from 'stream';
2
2
 
3
3
  import type { BSONSerializeOptions, Document } from '../bson';
4
4
  import { MongoDecompressionError, MongoParseError } from '../error';
5
5
  import type { ClientSession } from '../sessions';
6
- import { BufferPool, Callback } from '../utils';
7
- import { BinMsg, MessageHeader, Msg, Response, WriteProtocolMessageType } from './commands';
6
+ import { BufferPool, type Callback } from '../utils';
7
+ import {
8
+ BinMsg,
9
+ type MessageHeader,
10
+ Msg,
11
+ Response,
12
+ type WriteProtocolMessageType
13
+ } from './commands';
8
14
  import {
9
15
  compress,
10
16
  Compressor,
11
- CompressorName,
17
+ type CompressorName,
12
18
  decompress,
13
19
  uncompressibleCommands
14
20
  } from './wire_protocol/compression';
@@ -2,7 +2,7 @@ import { promisify } from 'util';
2
2
  import * as zlib from 'zlib';
3
3
 
4
4
  import { LEGACY_HELLO_COMMAND } from '../../constants';
5
- import { Snappy, ZStandard } from '../../deps';
5
+ import { getSnappy, getZstdLibrary, type SnappyLib, type ZStandard } from '../../deps';
6
6
  import { MongoDecompressionError, MongoInvalidArgumentError } from '../../error';
7
7
 
8
8
  /** @public */
@@ -37,6 +37,19 @@ const ZSTD_COMPRESSION_LEVEL = 3;
37
37
  const zlibInflate = promisify(zlib.inflate.bind(zlib));
38
38
  const zlibDeflate = promisify(zlib.deflate.bind(zlib));
39
39
 
40
+ let zstd: typeof ZStandard;
41
+ let Snappy: SnappyLib | null = null;
42
+ function loadSnappy() {
43
+ if (Snappy == null) {
44
+ const snappyImport = getSnappy();
45
+ if ('kModuleError' in snappyImport) {
46
+ throw snappyImport.kModuleError;
47
+ }
48
+ Snappy = snappyImport;
49
+ }
50
+ return Snappy;
51
+ }
52
+
40
53
  // Facilitate compressing a message using an agreed compressor
41
54
  export async function compress(
42
55
  options: { zlibCompressionLevel: number; agreedCompressor: CompressorName },
@@ -44,28 +57,28 @@ export async function compress(
44
57
  ): Promise<Buffer> {
45
58
  const zlibOptions = {} as zlib.ZlibOptions;
46
59
  switch (options.agreedCompressor) {
47
- case 'snappy':
48
- if ('kModuleError' in Snappy) {
49
- throw Snappy['kModuleError'];
50
- }
60
+ case 'snappy': {
61
+ Snappy ??= loadSnappy();
51
62
  return Snappy.compress(dataToBeCompressed);
52
-
53
- case 'zstd':
54
- if ('kModuleError' in ZStandard) {
55
- throw ZStandard['kModuleError'];
63
+ }
64
+ case 'zstd': {
65
+ loadZstd();
66
+ if ('kModuleError' in zstd) {
67
+ throw zstd['kModuleError'];
56
68
  }
57
- return ZStandard.compress(dataToBeCompressed, ZSTD_COMPRESSION_LEVEL);
58
-
59
- case 'zlib':
69
+ return zstd.compress(dataToBeCompressed, ZSTD_COMPRESSION_LEVEL);
70
+ }
71
+ case 'zlib': {
60
72
  if (options.zlibCompressionLevel) {
61
73
  zlibOptions.level = options.zlibCompressionLevel;
62
74
  }
63
75
  return zlibDeflate(dataToBeCompressed, zlibOptions);
64
-
65
- default:
76
+ }
77
+ default: {
66
78
  throw new MongoInvalidArgumentError(
67
79
  `Unknown compressor ${options.agreedCompressor} failed to compress`
68
80
  );
81
+ }
69
82
  }
70
83
  }
71
84
 
@@ -83,22 +96,31 @@ export async function decompress(compressorID: number, compressedData: Buffer):
83
96
  }
84
97
 
85
98
  switch (compressorID) {
86
- case Compressor.snappy:
87
- if ('kModuleError' in Snappy) {
88
- throw Snappy['kModuleError'];
89
- }
99
+ case Compressor.snappy: {
100
+ Snappy ??= loadSnappy();
90
101
  return Snappy.uncompress(compressedData, { asBuffer: true });
91
-
92
- case Compressor.zstd:
93
- if ('kModuleError' in ZStandard) {
94
- throw ZStandard['kModuleError'];
102
+ }
103
+ case Compressor.zstd: {
104
+ loadZstd();
105
+ if ('kModuleError' in zstd) {
106
+ throw zstd['kModuleError'];
95
107
  }
96
- return ZStandard.decompress(compressedData);
97
-
98
- case Compressor.zlib:
108
+ return zstd.decompress(compressedData);
109
+ }
110
+ case Compressor.zlib: {
99
111
  return zlibInflate(compressedData);
100
-
101
- default:
112
+ }
113
+ default: {
102
114
  return compressedData;
115
+ }
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Load ZStandard if it is not already set.
121
+ */
122
+ function loadZstd() {
123
+ if (!zstd) {
124
+ zstd = getZstdLibrary();
103
125
  }
104
126
  }