@depup/mongodb 7.1.0-depup.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 (396) hide show
  1. package/LICENSE.md +201 -0
  2. package/README.md +33 -0
  3. package/etc/prepare.js +12 -0
  4. package/lib/admin.js +136 -0
  5. package/lib/admin.js.map +1 -0
  6. package/lib/bson.js +85 -0
  7. package/lib/bson.js.map +1 -0
  8. package/lib/bulk/common.js +835 -0
  9. package/lib/bulk/common.js.map +1 -0
  10. package/lib/bulk/ordered.js +67 -0
  11. package/lib/bulk/ordered.js.map +1 -0
  12. package/lib/bulk/unordered.js +92 -0
  13. package/lib/bulk/unordered.js.map +1 -0
  14. package/lib/change_stream.js +513 -0
  15. package/lib/change_stream.js.map +1 -0
  16. package/lib/client-side-encryption/auto_encrypter.js +272 -0
  17. package/lib/client-side-encryption/auto_encrypter.js.map +1 -0
  18. package/lib/client-side-encryption/client_encryption.js +609 -0
  19. package/lib/client-side-encryption/client_encryption.js.map +1 -0
  20. package/lib/client-side-encryption/errors.js +138 -0
  21. package/lib/client-side-encryption/errors.js.map +1 -0
  22. package/lib/client-side-encryption/mongocryptd_manager.js +85 -0
  23. package/lib/client-side-encryption/mongocryptd_manager.js.map +1 -0
  24. package/lib/client-side-encryption/providers/aws.js +23 -0
  25. package/lib/client-side-encryption/providers/aws.js.map +1 -0
  26. package/lib/client-side-encryption/providers/azure.js +132 -0
  27. package/lib/client-side-encryption/providers/azure.js.map +1 -0
  28. package/lib/client-side-encryption/providers/gcp.js +16 -0
  29. package/lib/client-side-encryption/providers/gcp.js.map +1 -0
  30. package/lib/client-side-encryption/providers/index.js +43 -0
  31. package/lib/client-side-encryption/providers/index.js.map +1 -0
  32. package/lib/client-side-encryption/state_machine.js +427 -0
  33. package/lib/client-side-encryption/state_machine.js.map +1 -0
  34. package/lib/cmap/auth/auth_provider.js +51 -0
  35. package/lib/cmap/auth/auth_provider.js.map +1 -0
  36. package/lib/cmap/auth/aws4.js +161 -0
  37. package/lib/cmap/auth/aws4.js.map +1 -0
  38. package/lib/cmap/auth/aws_temporary_credentials.js +103 -0
  39. package/lib/cmap/auth/aws_temporary_credentials.js.map +1 -0
  40. package/lib/cmap/auth/gssapi.js +155 -0
  41. package/lib/cmap/auth/gssapi.js.map +1 -0
  42. package/lib/cmap/auth/mongo_credentials.js +169 -0
  43. package/lib/cmap/auth/mongo_credentials.js.map +1 -0
  44. package/lib/cmap/auth/mongodb_aws.js +128 -0
  45. package/lib/cmap/auth/mongodb_aws.js.map +1 -0
  46. package/lib/cmap/auth/mongodb_oidc/automated_callback_workflow.js +84 -0
  47. package/lib/cmap/auth/mongodb_oidc/automated_callback_workflow.js.map +1 -0
  48. package/lib/cmap/auth/mongodb_oidc/azure_machine_workflow.js +62 -0
  49. package/lib/cmap/auth/mongodb_oidc/azure_machine_workflow.js.map +1 -0
  50. package/lib/cmap/auth/mongodb_oidc/callback_workflow.js +141 -0
  51. package/lib/cmap/auth/mongodb_oidc/callback_workflow.js.map +1 -0
  52. package/lib/cmap/auth/mongodb_oidc/command_builders.js +44 -0
  53. package/lib/cmap/auth/mongodb_oidc/command_builders.js.map +1 -0
  54. package/lib/cmap/auth/mongodb_oidc/gcp_machine_workflow.js +39 -0
  55. package/lib/cmap/auth/mongodb_oidc/gcp_machine_workflow.js.map +1 -0
  56. package/lib/cmap/auth/mongodb_oidc/human_callback_workflow.js +122 -0
  57. package/lib/cmap/auth/mongodb_oidc/human_callback_workflow.js.map +1 -0
  58. package/lib/cmap/auth/mongodb_oidc/k8s_machine_workflow.js +32 -0
  59. package/lib/cmap/auth/mongodb_oidc/k8s_machine_workflow.js.map +1 -0
  60. package/lib/cmap/auth/mongodb_oidc/token_cache.js +52 -0
  61. package/lib/cmap/auth/mongodb_oidc/token_cache.js.map +1 -0
  62. package/lib/cmap/auth/mongodb_oidc/token_machine_workflow.js +23 -0
  63. package/lib/cmap/auth/mongodb_oidc/token_machine_workflow.js.map +1 -0
  64. package/lib/cmap/auth/mongodb_oidc.js +73 -0
  65. package/lib/cmap/auth/mongodb_oidc.js.map +1 -0
  66. package/lib/cmap/auth/plain.js +26 -0
  67. package/lib/cmap/auth/plain.js.map +1 -0
  68. package/lib/cmap/auth/providers.js +22 -0
  69. package/lib/cmap/auth/providers.js.map +1 -0
  70. package/lib/cmap/auth/scram.js +254 -0
  71. package/lib/cmap/auth/scram.js.map +1 -0
  72. package/lib/cmap/auth/x509.js +36 -0
  73. package/lib/cmap/auth/x509.js.map +1 -0
  74. package/lib/cmap/command_monitoring_events.js +223 -0
  75. package/lib/cmap/command_monitoring_events.js.map +1 -0
  76. package/lib/cmap/commands.js +535 -0
  77. package/lib/cmap/commands.js.map +1 -0
  78. package/lib/cmap/connect.js +381 -0
  79. package/lib/cmap/connect.js.map +1 -0
  80. package/lib/cmap/connection.js +571 -0
  81. package/lib/cmap/connection.js.map +1 -0
  82. package/lib/cmap/connection_pool.js +558 -0
  83. package/lib/cmap/connection_pool.js.map +1 -0
  84. package/lib/cmap/connection_pool_events.js +190 -0
  85. package/lib/cmap/connection_pool_events.js.map +1 -0
  86. package/lib/cmap/errors.js +108 -0
  87. package/lib/cmap/errors.js.map +1 -0
  88. package/lib/cmap/handshake/client_metadata.js +242 -0
  89. package/lib/cmap/handshake/client_metadata.js.map +1 -0
  90. package/lib/cmap/metrics.js +62 -0
  91. package/lib/cmap/metrics.js.map +1 -0
  92. package/lib/cmap/stream_description.js +70 -0
  93. package/lib/cmap/stream_description.js.map +1 -0
  94. package/lib/cmap/wire_protocol/compression.js +178 -0
  95. package/lib/cmap/wire_protocol/compression.js.map +1 -0
  96. package/lib/cmap/wire_protocol/constants.js +21 -0
  97. package/lib/cmap/wire_protocol/constants.js.map +1 -0
  98. package/lib/cmap/wire_protocol/on_data.js +111 -0
  99. package/lib/cmap/wire_protocol/on_data.js.map +1 -0
  100. package/lib/cmap/wire_protocol/on_demand/document.js +222 -0
  101. package/lib/cmap/wire_protocol/on_demand/document.js.map +1 -0
  102. package/lib/cmap/wire_protocol/responses.js +315 -0
  103. package/lib/cmap/wire_protocol/responses.js.map +1 -0
  104. package/lib/cmap/wire_protocol/shared.js +35 -0
  105. package/lib/cmap/wire_protocol/shared.js.map +1 -0
  106. package/lib/collection.js +751 -0
  107. package/lib/collection.js.map +1 -0
  108. package/lib/connection_string.js +1090 -0
  109. package/lib/connection_string.js.map +1 -0
  110. package/lib/constants.js +170 -0
  111. package/lib/constants.js.map +1 -0
  112. package/lib/cursor/abstract_cursor.js +924 -0
  113. package/lib/cursor/abstract_cursor.js.map +1 -0
  114. package/lib/cursor/aggregation_cursor.js +164 -0
  115. package/lib/cursor/aggregation_cursor.js.map +1 -0
  116. package/lib/cursor/change_stream_cursor.js +104 -0
  117. package/lib/cursor/change_stream_cursor.js.map +1 -0
  118. package/lib/cursor/client_bulk_write_cursor.js +52 -0
  119. package/lib/cursor/client_bulk_write_cursor.js.map +1 -0
  120. package/lib/cursor/explainable_cursor.js +36 -0
  121. package/lib/cursor/explainable_cursor.js.map +1 -0
  122. package/lib/cursor/find_cursor.js +399 -0
  123. package/lib/cursor/find_cursor.js.map +1 -0
  124. package/lib/cursor/list_collections_cursor.js +34 -0
  125. package/lib/cursor/list_collections_cursor.js.map +1 -0
  126. package/lib/cursor/list_indexes_cursor.js +32 -0
  127. package/lib/cursor/list_indexes_cursor.js.map +1 -0
  128. package/lib/cursor/list_search_indexes_cursor.js +14 -0
  129. package/lib/cursor/list_search_indexes_cursor.js.map +1 -0
  130. package/lib/cursor/run_command_cursor.js +94 -0
  131. package/lib/cursor/run_command_cursor.js.map +1 -0
  132. package/lib/db.js +419 -0
  133. package/lib/db.js.map +1 -0
  134. package/lib/deps.js +112 -0
  135. package/lib/deps.js.map +1 -0
  136. package/lib/encrypter.js +106 -0
  137. package/lib/encrypter.js.map +1 -0
  138. package/lib/error.js +1380 -0
  139. package/lib/error.js.map +1 -0
  140. package/lib/explain.js +59 -0
  141. package/lib/explain.js.map +1 -0
  142. package/lib/gridfs/download.js +305 -0
  143. package/lib/gridfs/download.js.map +1 -0
  144. package/lib/gridfs/index.js +164 -0
  145. package/lib/gridfs/index.js.map +1 -0
  146. package/lib/gridfs/upload.js +358 -0
  147. package/lib/gridfs/upload.js.map +1 -0
  148. package/lib/index.js +193 -0
  149. package/lib/index.js.map +1 -0
  150. package/lib/mongo_client.js +557 -0
  151. package/lib/mongo_client.js.map +1 -0
  152. package/lib/mongo_client_auth_providers.js +80 -0
  153. package/lib/mongo_client_auth_providers.js.map +1 -0
  154. package/lib/mongo_logger.js +655 -0
  155. package/lib/mongo_logger.js.map +1 -0
  156. package/lib/mongo_types.js +56 -0
  157. package/lib/mongo_types.js.map +1 -0
  158. package/lib/operations/aggregate.js +90 -0
  159. package/lib/operations/aggregate.js.map +1 -0
  160. package/lib/operations/client_bulk_write/client_bulk_write.js +51 -0
  161. package/lib/operations/client_bulk_write/client_bulk_write.js.map +1 -0
  162. package/lib/operations/client_bulk_write/command_builder.js +340 -0
  163. package/lib/operations/client_bulk_write/command_builder.js.map +1 -0
  164. package/lib/operations/client_bulk_write/common.js +3 -0
  165. package/lib/operations/client_bulk_write/common.js.map +1 -0
  166. package/lib/operations/client_bulk_write/executor.js +120 -0
  167. package/lib/operations/client_bulk_write/executor.js.map +1 -0
  168. package/lib/operations/client_bulk_write/results_merger.js +204 -0
  169. package/lib/operations/client_bulk_write/results_merger.js.map +1 -0
  170. package/lib/operations/command.js +83 -0
  171. package/lib/operations/command.js.map +1 -0
  172. package/lib/operations/count.js +45 -0
  173. package/lib/operations/count.js.map +1 -0
  174. package/lib/operations/create_collection.js +109 -0
  175. package/lib/operations/create_collection.js.map +1 -0
  176. package/lib/operations/delete.js +125 -0
  177. package/lib/operations/delete.js.map +1 -0
  178. package/lib/operations/distinct.js +61 -0
  179. package/lib/operations/distinct.js.map +1 -0
  180. package/lib/operations/drop.js +93 -0
  181. package/lib/operations/drop.js.map +1 -0
  182. package/lib/operations/end_sessions.js +34 -0
  183. package/lib/operations/end_sessions.js.map +1 -0
  184. package/lib/operations/estimated_document_count.js +41 -0
  185. package/lib/operations/estimated_document_count.js.map +1 -0
  186. package/lib/operations/execute_operation.js +239 -0
  187. package/lib/operations/execute_operation.js.map +1 -0
  188. package/lib/operations/find.js +148 -0
  189. package/lib/operations/find.js.map +1 -0
  190. package/lib/operations/find_and_modify.js +158 -0
  191. package/lib/operations/find_and_modify.js.map +1 -0
  192. package/lib/operations/get_more.js +62 -0
  193. package/lib/operations/get_more.js.map +1 -0
  194. package/lib/operations/indexes.js +186 -0
  195. package/lib/operations/indexes.js.map +1 -0
  196. package/lib/operations/insert.js +70 -0
  197. package/lib/operations/insert.js.map +1 -0
  198. package/lib/operations/kill_cursors.js +43 -0
  199. package/lib/operations/kill_cursors.js.map +1 -0
  200. package/lib/operations/list_collections.js +53 -0
  201. package/lib/operations/list_collections.js.map +1 -0
  202. package/lib/operations/list_databases.js +40 -0
  203. package/lib/operations/list_databases.js.map +1 -0
  204. package/lib/operations/operation.js +102 -0
  205. package/lib/operations/operation.js.map +1 -0
  206. package/lib/operations/profiling_level.js +43 -0
  207. package/lib/operations/profiling_level.js.map +1 -0
  208. package/lib/operations/remove_user.js +27 -0
  209. package/lib/operations/remove_user.js.map +1 -0
  210. package/lib/operations/rename.js +38 -0
  211. package/lib/operations/rename.js.map +1 -0
  212. package/lib/operations/run_command.js +47 -0
  213. package/lib/operations/run_command.js.map +1 -0
  214. package/lib/operations/search_indexes/create.js +33 -0
  215. package/lib/operations/search_indexes/create.js.map +1 -0
  216. package/lib/operations/search_indexes/drop.js +43 -0
  217. package/lib/operations/search_indexes/drop.js.map +1 -0
  218. package/lib/operations/search_indexes/update.js +35 -0
  219. package/lib/operations/search_indexes/update.js.map +1 -0
  220. package/lib/operations/set_profiling_level.js +53 -0
  221. package/lib/operations/set_profiling_level.js.map +1 -0
  222. package/lib/operations/stats.js +27 -0
  223. package/lib/operations/stats.js.map +1 -0
  224. package/lib/operations/update.js +188 -0
  225. package/lib/operations/update.js.map +1 -0
  226. package/lib/operations/validate_collection.js +37 -0
  227. package/lib/operations/validate_collection.js.map +1 -0
  228. package/lib/read_concern.js +73 -0
  229. package/lib/read_concern.js.map +1 -0
  230. package/lib/read_preference.js +191 -0
  231. package/lib/read_preference.js.map +1 -0
  232. package/lib/sdam/common.js +49 -0
  233. package/lib/sdam/common.js.map +1 -0
  234. package/lib/sdam/events.js +146 -0
  235. package/lib/sdam/events.js.map +1 -0
  236. package/lib/sdam/monitor.js +544 -0
  237. package/lib/sdam/monitor.js.map +1 -0
  238. package/lib/sdam/server.js +404 -0
  239. package/lib/sdam/server.js.map +1 -0
  240. package/lib/sdam/server_description.js +204 -0
  241. package/lib/sdam/server_description.js.map +1 -0
  242. package/lib/sdam/server_selection.js +294 -0
  243. package/lib/sdam/server_selection.js.map +1 -0
  244. package/lib/sdam/server_selection_events.js +85 -0
  245. package/lib/sdam/server_selection_events.js.map +1 -0
  246. package/lib/sdam/srv_polling.js +108 -0
  247. package/lib/sdam/srv_polling.js.map +1 -0
  248. package/lib/sdam/topology.js +652 -0
  249. package/lib/sdam/topology.js.map +1 -0
  250. package/lib/sdam/topology_description.js +383 -0
  251. package/lib/sdam/topology_description.js.map +1 -0
  252. package/lib/sessions.js +892 -0
  253. package/lib/sessions.js.map +1 -0
  254. package/lib/sort.js +103 -0
  255. package/lib/sort.js.map +1 -0
  256. package/lib/timeout.js +296 -0
  257. package/lib/timeout.js.map +1 -0
  258. package/lib/transactions.js +135 -0
  259. package/lib/transactions.js.map +1 -0
  260. package/lib/utils.js +1163 -0
  261. package/lib/utils.js.map +1 -0
  262. package/lib/write_concern.js +100 -0
  263. package/lib/write_concern.js.map +1 -0
  264. package/mongodb.d.ts +9018 -0
  265. package/package.json +197 -0
  266. package/src/admin.ts +173 -0
  267. package/src/bson.ts +149 -0
  268. package/src/bulk/common.ts +1251 -0
  269. package/src/bulk/ordered.ts +83 -0
  270. package/src/bulk/unordered.ts +115 -0
  271. package/src/change_stream.ts +1160 -0
  272. package/src/client-side-encryption/auto_encrypter.ts +469 -0
  273. package/src/client-side-encryption/client_encryption.ts +1150 -0
  274. package/src/client-side-encryption/errors.ts +144 -0
  275. package/src/client-side-encryption/mongocryptd_manager.ts +100 -0
  276. package/src/client-side-encryption/providers/aws.ts +33 -0
  277. package/src/client-side-encryption/providers/azure.ts +181 -0
  278. package/src/client-side-encryption/providers/gcp.ts +16 -0
  279. package/src/client-side-encryption/providers/index.ts +207 -0
  280. package/src/client-side-encryption/state_machine.ts +651 -0
  281. package/src/cmap/auth/auth_provider.ts +77 -0
  282. package/src/cmap/auth/aws4.ts +207 -0
  283. package/src/cmap/auth/aws_temporary_credentials.ts +129 -0
  284. package/src/cmap/auth/gssapi.ts +203 -0
  285. package/src/cmap/auth/mongo_credentials.ts +292 -0
  286. package/src/cmap/auth/mongodb_aws.ts +179 -0
  287. package/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts +88 -0
  288. package/src/cmap/auth/mongodb_oidc/azure_machine_workflow.ts +73 -0
  289. package/src/cmap/auth/mongodb_oidc/callback_workflow.ts +188 -0
  290. package/src/cmap/auth/mongodb_oidc/command_builders.ts +53 -0
  291. package/src/cmap/auth/mongodb_oidc/gcp_machine_workflow.ts +46 -0
  292. package/src/cmap/auth/mongodb_oidc/human_callback_workflow.ts +141 -0
  293. package/src/cmap/auth/mongodb_oidc/k8s_machine_workflow.ts +31 -0
  294. package/src/cmap/auth/mongodb_oidc/token_cache.ts +62 -0
  295. package/src/cmap/auth/mongodb_oidc/token_machine_workflow.ts +22 -0
  296. package/src/cmap/auth/mongodb_oidc.ts +185 -0
  297. package/src/cmap/auth/plain.ts +25 -0
  298. package/src/cmap/auth/providers.ts +22 -0
  299. package/src/cmap/auth/scram.ts +344 -0
  300. package/src/cmap/auth/x509.ts +43 -0
  301. package/src/cmap/command_monitoring_events.ts +316 -0
  302. package/src/cmap/commands.ts +788 -0
  303. package/src/cmap/connect.ts +518 -0
  304. package/src/cmap/connection.ts +931 -0
  305. package/src/cmap/connection_pool.ts +831 -0
  306. package/src/cmap/connection_pool_events.ts +300 -0
  307. package/src/cmap/errors.ts +119 -0
  308. package/src/cmap/handshake/client_metadata.ts +353 -0
  309. package/src/cmap/metrics.ts +58 -0
  310. package/src/cmap/stream_description.ts +96 -0
  311. package/src/cmap/wire_protocol/compression.ts +211 -0
  312. package/src/cmap/wire_protocol/constants.ts +17 -0
  313. package/src/cmap/wire_protocol/on_data.ts +139 -0
  314. package/src/cmap/wire_protocol/on_demand/document.ts +358 -0
  315. package/src/cmap/wire_protocol/responses.ts +393 -0
  316. package/src/cmap/wire_protocol/shared.ts +48 -0
  317. package/src/collection.ts +1314 -0
  318. package/src/connection_string.ts +1297 -0
  319. package/src/constants.ts +176 -0
  320. package/src/cursor/abstract_cursor.ts +1247 -0
  321. package/src/cursor/aggregation_cursor.ts +245 -0
  322. package/src/cursor/change_stream_cursor.ts +171 -0
  323. package/src/cursor/client_bulk_write_cursor.ts +83 -0
  324. package/src/cursor/explainable_cursor.ts +51 -0
  325. package/src/cursor/find_cursor.ts +497 -0
  326. package/src/cursor/list_collections_cursor.ts +50 -0
  327. package/src/cursor/list_indexes_cursor.ts +37 -0
  328. package/src/cursor/list_search_indexes_cursor.ts +20 -0
  329. package/src/cursor/run_command_cursor.ts +178 -0
  330. package/src/db.ts +621 -0
  331. package/src/deps.ts +227 -0
  332. package/src/encrypter.ts +127 -0
  333. package/src/error.ts +1567 -0
  334. package/src/explain.ts +124 -0
  335. package/src/gridfs/download.ts +483 -0
  336. package/src/gridfs/index.ts +264 -0
  337. package/src/gridfs/upload.ts +537 -0
  338. package/src/index.ts +633 -0
  339. package/src/mongo_client.ts +1155 -0
  340. package/src/mongo_client_auth_providers.ts +87 -0
  341. package/src/mongo_logger.ts +1081 -0
  342. package/src/mongo_types.ts +678 -0
  343. package/src/operations/aggregate.ts +153 -0
  344. package/src/operations/client_bulk_write/client_bulk_write.ts +72 -0
  345. package/src/operations/client_bulk_write/command_builder.ts +487 -0
  346. package/src/operations/client_bulk_write/common.ts +276 -0
  347. package/src/operations/client_bulk_write/executor.ts +149 -0
  348. package/src/operations/client_bulk_write/results_merger.ts +260 -0
  349. package/src/operations/command.ts +171 -0
  350. package/src/operations/count.ts +74 -0
  351. package/src/operations/create_collection.ts +218 -0
  352. package/src/operations/delete.ts +183 -0
  353. package/src/operations/distinct.ts +92 -0
  354. package/src/operations/drop.ts +130 -0
  355. package/src/operations/end_sessions.ts +44 -0
  356. package/src/operations/estimated_document_count.ts +61 -0
  357. package/src/operations/execute_operation.ts +319 -0
  358. package/src/operations/find.ts +254 -0
  359. package/src/operations/find_and_modify.ts +319 -0
  360. package/src/operations/get_more.ts +108 -0
  361. package/src/operations/indexes.ts +418 -0
  362. package/src/operations/insert.ts +108 -0
  363. package/src/operations/kill_cursors.ts +64 -0
  364. package/src/operations/list_collections.ts +108 -0
  365. package/src/operations/list_databases.ts +68 -0
  366. package/src/operations/operation.ts +181 -0
  367. package/src/operations/profiling_level.ts +46 -0
  368. package/src/operations/remove_user.ts +36 -0
  369. package/src/operations/rename.ts +60 -0
  370. package/src/operations/run_command.ts +79 -0
  371. package/src/operations/search_indexes/create.ts +56 -0
  372. package/src/operations/search_indexes/drop.ts +58 -0
  373. package/src/operations/search_indexes/update.ts +45 -0
  374. package/src/operations/set_profiling_level.ts +74 -0
  375. package/src/operations/stats.ts +37 -0
  376. package/src/operations/update.ts +318 -0
  377. package/src/operations/validate_collection.ts +50 -0
  378. package/src/read_concern.ts +88 -0
  379. package/src/read_preference.ts +256 -0
  380. package/src/sdam/common.ts +74 -0
  381. package/src/sdam/events.ts +219 -0
  382. package/src/sdam/monitor.ts +771 -0
  383. package/src/sdam/server.ts +595 -0
  384. package/src/sdam/server_description.ts +297 -0
  385. package/src/sdam/server_selection.ts +415 -0
  386. package/src/sdam/server_selection_events.ts +142 -0
  387. package/src/sdam/srv_polling.ts +146 -0
  388. package/src/sdam/topology.ts +1100 -0
  389. package/src/sdam/topology_description.ts +548 -0
  390. package/src/sessions.ts +1244 -0
  391. package/src/sort.ts +141 -0
  392. package/src/timeout.ts +405 -0
  393. package/src/transactions.ts +181 -0
  394. package/src/utils.ts +1434 -0
  395. package/src/write_concern.ts +183 -0
  396. package/tsconfig.json +46 -0
@@ -0,0 +1,651 @@
1
+ import * as fs from 'fs/promises';
2
+ import { type MongoCryptContext, type MongoCryptKMSRequest } from 'mongodb-client-encryption';
3
+ import * as net from 'net';
4
+ import * as process from 'process';
5
+ import * as tls from 'tls';
6
+
7
+ import {
8
+ type BSONSerializeOptions,
9
+ deserialize,
10
+ type Document,
11
+ pluckBSONSerializeOptions,
12
+ serialize
13
+ } from '../bson';
14
+ import { type ProxyOptions } from '../cmap/connection';
15
+ import { CursorTimeoutContext } from '../cursor/abstract_cursor';
16
+ import { getSocks, type SocksLib } from '../deps';
17
+ import { MongoOperationTimeoutError } from '../error';
18
+ import { type MongoClient, type MongoClientOptions } from '../mongo_client';
19
+ import { type Abortable } from '../mongo_types';
20
+ import { type CollectionInfo } from '../operations/list_collections';
21
+ import { Timeout, type TimeoutContext, TimeoutError } from '../timeout';
22
+ import {
23
+ addAbortListener,
24
+ BufferPool,
25
+ kDispose,
26
+ MongoDBCollectionNamespace,
27
+ promiseWithResolvers
28
+ } from '../utils';
29
+ import { autoSelectSocketOptions, type DataKey } from './client_encryption';
30
+ import { MongoCryptError } from './errors';
31
+ import { type MongocryptdManager } from './mongocryptd_manager';
32
+ import { type KMSProviders } from './providers';
33
+
34
+ let socks: SocksLib | null = null;
35
+ function loadSocks(): SocksLib {
36
+ if (socks == null) {
37
+ const socksImport = getSocks();
38
+ if ('kModuleError' in socksImport) {
39
+ throw socksImport.kModuleError;
40
+ }
41
+ socks = socksImport;
42
+ }
43
+ return socks;
44
+ }
45
+
46
+ // libmongocrypt states
47
+ const MONGOCRYPT_CTX_ERROR = 0;
48
+ const MONGOCRYPT_CTX_NEED_MONGO_COLLINFO = 1;
49
+ const MONGOCRYPT_CTX_NEED_MONGO_MARKINGS = 2;
50
+ const MONGOCRYPT_CTX_NEED_MONGO_KEYS = 3;
51
+ const MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS = 7;
52
+ const MONGOCRYPT_CTX_NEED_KMS = 4;
53
+ const MONGOCRYPT_CTX_READY = 5;
54
+ const MONGOCRYPT_CTX_DONE = 6;
55
+
56
+ const HTTPS_PORT = 443;
57
+
58
+ const stateToString = new Map([
59
+ [MONGOCRYPT_CTX_ERROR, 'MONGOCRYPT_CTX_ERROR'],
60
+ [MONGOCRYPT_CTX_NEED_MONGO_COLLINFO, 'MONGOCRYPT_CTX_NEED_MONGO_COLLINFO'],
61
+ [MONGOCRYPT_CTX_NEED_MONGO_MARKINGS, 'MONGOCRYPT_CTX_NEED_MONGO_MARKINGS'],
62
+ [MONGOCRYPT_CTX_NEED_MONGO_KEYS, 'MONGOCRYPT_CTX_NEED_MONGO_KEYS'],
63
+ [MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS, 'MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS'],
64
+ [MONGOCRYPT_CTX_NEED_KMS, 'MONGOCRYPT_CTX_NEED_KMS'],
65
+ [MONGOCRYPT_CTX_READY, 'MONGOCRYPT_CTX_READY'],
66
+ [MONGOCRYPT_CTX_DONE, 'MONGOCRYPT_CTX_DONE']
67
+ ]);
68
+
69
+ const INSECURE_TLS_OPTIONS = [
70
+ 'tlsInsecure',
71
+ 'tlsAllowInvalidCertificates',
72
+ 'tlsAllowInvalidHostnames'
73
+ ];
74
+
75
+ /**
76
+ * Helper function for logging. Enabled by setting the environment flag MONGODB_CRYPT_DEBUG.
77
+ * @param msg - Anything you want to be logged.
78
+ */
79
+ function debug(msg: unknown) {
80
+ if (process.env.MONGODB_CRYPT_DEBUG) {
81
+ // eslint-disable-next-line no-console
82
+ console.error(msg);
83
+ }
84
+ }
85
+
86
+ declare module 'mongodb-client-encryption' {
87
+ // the properties added to `MongoCryptContext` here are only used for the `StateMachine`'s
88
+ // execute method and are not part of the C++ bindings.
89
+ interface MongoCryptContext {
90
+ id: number;
91
+ document: Document;
92
+ ns: string;
93
+ }
94
+ }
95
+
96
+ /**
97
+ * @public
98
+ *
99
+ * TLS options to use when connecting. The spec specifically calls out which insecure
100
+ * tls options are not allowed:
101
+ *
102
+ * - tlsAllowInvalidCertificates
103
+ * - tlsAllowInvalidHostnames
104
+ * - tlsInsecure
105
+ *
106
+ * These options are not included in the type, and are ignored if provided.
107
+ */
108
+ export type ClientEncryptionTlsOptions = Pick<
109
+ MongoClientOptions,
110
+ 'tlsCAFile' | 'tlsCertificateKeyFile' | 'tlsCertificateKeyFilePassword' | 'secureContext'
111
+ >;
112
+
113
+ /** @public */
114
+ export type CSFLEKMSTlsOptions = {
115
+ aws?: ClientEncryptionTlsOptions;
116
+ gcp?: ClientEncryptionTlsOptions;
117
+ kmip?: ClientEncryptionTlsOptions;
118
+ local?: ClientEncryptionTlsOptions;
119
+ azure?: ClientEncryptionTlsOptions;
120
+
121
+ [key: string]: ClientEncryptionTlsOptions | undefined;
122
+ };
123
+
124
+ /**
125
+ * @public
126
+ *
127
+ * Socket options to use for KMS requests.
128
+ */
129
+ export type ClientEncryptionSocketOptions = Pick<
130
+ MongoClientOptions,
131
+ 'autoSelectFamily' | 'autoSelectFamilyAttemptTimeout'
132
+ >;
133
+
134
+ /**
135
+ * This is kind of a hack. For `rewrapManyDataKey`, we have tests that
136
+ * guarantee that when there are no matching keys, `rewrapManyDataKey` returns
137
+ * nothing. We also have tests for auto encryption that guarantee for `encrypt`
138
+ * we return an error when there are no matching keys. This error is generated in
139
+ * subsequent iterations of the state machine.
140
+ * Some apis (`encrypt`) throw if there are no filter matches and others (`rewrapManyDataKey`)
141
+ * do not. We set the result manually here, and let the state machine continue. `libmongocrypt`
142
+ * will inform us if we need to error by setting the state to `MONGOCRYPT_CTX_ERROR` but
143
+ * otherwise we'll return `{ v: [] }`.
144
+ */
145
+ let EMPTY_V;
146
+
147
+ /**
148
+ * @internal
149
+ *
150
+ * An interface representing an object that can be passed to the `StateMachine.execute` method.
151
+ *
152
+ * Not all properties are required for all operations.
153
+ */
154
+ export interface StateMachineExecutable {
155
+ _keyVaultNamespace: string;
156
+ _keyVaultClient: MongoClient;
157
+ askForKMSCredentials: () => Promise<KMSProviders>;
158
+
159
+ /** only used for auto encryption */
160
+ _metaDataClient?: MongoClient;
161
+ /** only used for auto encryption */
162
+ _mongocryptdClient?: MongoClient;
163
+ /** only used for auto encryption */
164
+ _mongocryptdManager?: MongocryptdManager;
165
+ }
166
+
167
+ export type StateMachineOptions = {
168
+ /** socks5 proxy options, if set. */
169
+ proxyOptions: ProxyOptions;
170
+
171
+ /** TLS options for KMS requests, if set. */
172
+ tlsOptions: CSFLEKMSTlsOptions;
173
+
174
+ /** Socket specific options we support. */
175
+ socketOptions: ClientEncryptionSocketOptions;
176
+ } & Pick<BSONSerializeOptions, 'promoteLongs' | 'promoteValues'>;
177
+
178
+ /**
179
+ * @internal
180
+ * An internal class that executes across a MongoCryptContext until either
181
+ * a finishing state or an error is reached. Do not instantiate directly.
182
+ */
183
+ // TODO(DRIVERS-2671): clarify CSOT behavior for FLE APIs
184
+ export class StateMachine {
185
+ private options: StateMachineOptions;
186
+ private bsonOptions: BSONSerializeOptions;
187
+
188
+ constructor(options: StateMachineOptions, bsonOptions = pluckBSONSerializeOptions(options)) {
189
+ this.options = options;
190
+ this.bsonOptions = bsonOptions;
191
+ }
192
+
193
+ /**
194
+ * Executes the state machine according to the specification
195
+ */
196
+ async execute(
197
+ executor: StateMachineExecutable,
198
+ context: MongoCryptContext,
199
+ options: { timeoutContext?: TimeoutContext } & Abortable
200
+ ): Promise<Uint8Array> {
201
+ const keyVaultNamespace = executor._keyVaultNamespace;
202
+ const keyVaultClient = executor._keyVaultClient;
203
+ const metaDataClient = executor._metaDataClient;
204
+ const mongocryptdClient = executor._mongocryptdClient;
205
+ const mongocryptdManager = executor._mongocryptdManager;
206
+ let result: Uint8Array | null = null;
207
+
208
+ // Typescript treats getters just like properties: Once you've tested it for equality
209
+ // it cannot change. Which is exactly the opposite of what we use state and status for.
210
+ // Every call to at least `addMongoOperationResponse` and `finalize` can change the state.
211
+ // These wrappers let us write code more naturally and not add compiler exceptions
212
+ // to conditions checks inside the state machine.
213
+ const getStatus = () => context.status;
214
+ const getState = () => context.state;
215
+
216
+ while (getState() !== MONGOCRYPT_CTX_DONE && getState() !== MONGOCRYPT_CTX_ERROR) {
217
+ options.signal?.throwIfAborted();
218
+ debug(`[context#${context.id}] ${stateToString.get(getState()) || getState()}`);
219
+
220
+ switch (getState()) {
221
+ case MONGOCRYPT_CTX_NEED_MONGO_COLLINFO: {
222
+ const filter = deserialize(context.nextMongoOperation());
223
+ if (!metaDataClient) {
224
+ throw new MongoCryptError(
225
+ 'unreachable state machine state: entered MONGOCRYPT_CTX_NEED_MONGO_COLLINFO but metadata client is undefined'
226
+ );
227
+ }
228
+
229
+ const collInfoCursor = this.fetchCollectionInfo(
230
+ metaDataClient,
231
+ context.ns,
232
+ filter,
233
+ options
234
+ );
235
+
236
+ for await (const collInfo of collInfoCursor) {
237
+ context.addMongoOperationResponse(serialize(collInfo));
238
+ if (getState() === MONGOCRYPT_CTX_ERROR) break;
239
+ }
240
+
241
+ if (getState() === MONGOCRYPT_CTX_ERROR) break;
242
+
243
+ context.finishMongoOperation();
244
+ break;
245
+ }
246
+
247
+ case MONGOCRYPT_CTX_NEED_MONGO_MARKINGS: {
248
+ const command = context.nextMongoOperation();
249
+ if (getState() === MONGOCRYPT_CTX_ERROR) break;
250
+
251
+ if (!mongocryptdClient) {
252
+ throw new MongoCryptError(
253
+ 'unreachable state machine state: entered MONGOCRYPT_CTX_NEED_MONGO_MARKINGS but mongocryptdClient is undefined'
254
+ );
255
+ }
256
+
257
+ // When we are using the shared library, we don't have a mongocryptd manager.
258
+ const markedCommand: Uint8Array = mongocryptdManager
259
+ ? await mongocryptdManager.withRespawn(
260
+ this.markCommand.bind(this, mongocryptdClient, context.ns, command, options)
261
+ )
262
+ : await this.markCommand(mongocryptdClient, context.ns, command, options);
263
+
264
+ context.addMongoOperationResponse(markedCommand);
265
+ context.finishMongoOperation();
266
+ break;
267
+ }
268
+
269
+ case MONGOCRYPT_CTX_NEED_MONGO_KEYS: {
270
+ const filter = context.nextMongoOperation();
271
+ const keys = await this.fetchKeys(keyVaultClient, keyVaultNamespace, filter, options);
272
+
273
+ if (keys.length === 0) {
274
+ // See docs on EMPTY_V
275
+ result = EMPTY_V ??= serialize({ v: [] });
276
+ }
277
+ for (const key of keys) {
278
+ context.addMongoOperationResponse(serialize(key));
279
+ }
280
+
281
+ context.finishMongoOperation();
282
+
283
+ break;
284
+ }
285
+
286
+ case MONGOCRYPT_CTX_NEED_KMS_CREDENTIALS: {
287
+ const kmsProviders = await executor.askForKMSCredentials();
288
+ context.provideKMSProviders(serialize(kmsProviders));
289
+ break;
290
+ }
291
+
292
+ case MONGOCRYPT_CTX_NEED_KMS: {
293
+ await Promise.all(this.requests(context, options));
294
+ context.finishKMSRequests();
295
+ break;
296
+ }
297
+
298
+ case MONGOCRYPT_CTX_READY: {
299
+ const finalizedContext = context.finalize();
300
+ if (getState() === MONGOCRYPT_CTX_ERROR) {
301
+ const message = getStatus().message || 'Finalization error';
302
+ throw new MongoCryptError(message);
303
+ }
304
+ result = finalizedContext;
305
+ break;
306
+ }
307
+
308
+ default:
309
+ throw new MongoCryptError(`Unknown state: ${getState()}`);
310
+ }
311
+ }
312
+
313
+ if (getState() === MONGOCRYPT_CTX_ERROR || result == null) {
314
+ const message = getStatus().message;
315
+ if (!message) {
316
+ debug(
317
+ `unidentifiable error in MongoCrypt - received an error status from \`libmongocrypt\` but received no error message.`
318
+ );
319
+ }
320
+ throw new MongoCryptError(
321
+ message ??
322
+ 'unidentifiable error in MongoCrypt - received an error status from `libmongocrypt` but received no error message.'
323
+ );
324
+ }
325
+
326
+ return result;
327
+ }
328
+
329
+ /**
330
+ * Handles the request to the KMS service. Exposed for testing purposes. Do not directly invoke.
331
+ * @param kmsContext - A C++ KMS context returned from the bindings
332
+ * @returns A promise that resolves when the KMS reply has be fully parsed
333
+ */
334
+ async kmsRequest(
335
+ request: MongoCryptKMSRequest,
336
+ options?: { timeoutContext?: TimeoutContext } & Abortable
337
+ ): Promise<void> {
338
+ const parsedUrl = request.endpoint.split(':');
339
+ const port = parsedUrl[1] != null ? Number.parseInt(parsedUrl[1], 10) : HTTPS_PORT;
340
+ const socketOptions: tls.ConnectionOptions & {
341
+ host: string;
342
+ port: number;
343
+ autoSelectFamily?: boolean;
344
+ autoSelectFamilyAttemptTimeout?: number;
345
+ } = {
346
+ host: parsedUrl[0],
347
+ servername: parsedUrl[0],
348
+ port,
349
+ ...autoSelectSocketOptions(this.options.socketOptions || {})
350
+ };
351
+ const message = request.message;
352
+ const buffer = new BufferPool();
353
+
354
+ let netSocket: net.Socket;
355
+ let socket: tls.TLSSocket;
356
+
357
+ function destroySockets() {
358
+ for (const sock of [socket, netSocket]) {
359
+ if (sock) {
360
+ sock.destroy();
361
+ }
362
+ }
363
+ }
364
+
365
+ function onerror(cause: Error) {
366
+ return new MongoCryptError('KMS request failed', { cause });
367
+ }
368
+
369
+ function onclose() {
370
+ return new MongoCryptError('KMS request closed');
371
+ }
372
+
373
+ const tlsOptions = this.options.tlsOptions;
374
+ if (tlsOptions) {
375
+ const kmsProvider = request.kmsProvider;
376
+ const providerTlsOptions = tlsOptions[kmsProvider];
377
+ if (providerTlsOptions) {
378
+ const error = this.validateTlsOptions(kmsProvider, providerTlsOptions);
379
+ if (error) {
380
+ throw error;
381
+ }
382
+ try {
383
+ await this.setTlsOptions(providerTlsOptions, socketOptions);
384
+ } catch (err) {
385
+ throw onerror(err);
386
+ }
387
+ }
388
+ }
389
+
390
+ let abortListener;
391
+
392
+ try {
393
+ if (this.options.proxyOptions && this.options.proxyOptions.proxyHost) {
394
+ netSocket = new net.Socket();
395
+
396
+ const {
397
+ promise: willConnect,
398
+ reject: rejectOnNetSocketError,
399
+ resolve: resolveOnNetSocketConnect
400
+ } = promiseWithResolvers<void>();
401
+
402
+ netSocket
403
+ .once('error', err => rejectOnNetSocketError(onerror(err)))
404
+ .once('close', () => rejectOnNetSocketError(onclose()))
405
+ .once('connect', () => resolveOnNetSocketConnect());
406
+
407
+ const netSocketOptions = {
408
+ ...socketOptions,
409
+ host: this.options.proxyOptions.proxyHost,
410
+ port: this.options.proxyOptions.proxyPort || 1080
411
+ };
412
+
413
+ netSocket.connect(netSocketOptions);
414
+
415
+ await willConnect;
416
+
417
+ try {
418
+ socks ??= loadSocks();
419
+ socketOptions.socket = (
420
+ await socks.SocksClient.createConnection({
421
+ existing_socket: netSocket,
422
+ command: 'connect',
423
+ destination: { host: socketOptions.host, port: socketOptions.port },
424
+ proxy: {
425
+ // host and port are ignored because we pass existing_socket
426
+ host: 'iLoveJavaScript',
427
+ port: 0,
428
+ type: 5,
429
+ userId: this.options.proxyOptions.proxyUsername,
430
+ password: this.options.proxyOptions.proxyPassword
431
+ }
432
+ })
433
+ ).socket;
434
+ } catch (err) {
435
+ throw onerror(err);
436
+ }
437
+ }
438
+
439
+ socket = tls.connect(socketOptions, () => {
440
+ socket.write(message);
441
+ });
442
+
443
+ const {
444
+ promise: willResolveKmsRequest,
445
+ reject: rejectOnTlsSocketError,
446
+ resolve
447
+ } = promiseWithResolvers<void>();
448
+
449
+ abortListener = addAbortListener(options?.signal, function () {
450
+ destroySockets();
451
+ rejectOnTlsSocketError(this.reason);
452
+ });
453
+
454
+ socket
455
+ .once('error', err => rejectOnTlsSocketError(onerror(err)))
456
+ .once('close', () => rejectOnTlsSocketError(onclose()))
457
+ .on('data', data => {
458
+ buffer.append(data);
459
+ while (request.bytesNeeded > 0 && buffer.length) {
460
+ const bytesNeeded = Math.min(request.bytesNeeded, buffer.length);
461
+ request.addResponse(buffer.read(bytesNeeded));
462
+ }
463
+
464
+ if (request.bytesNeeded <= 0) {
465
+ resolve();
466
+ }
467
+ });
468
+ await (options?.timeoutContext?.csotEnabled()
469
+ ? Promise.all([
470
+ willResolveKmsRequest,
471
+ Timeout.expires(options.timeoutContext?.remainingTimeMS)
472
+ ])
473
+ : willResolveKmsRequest);
474
+ } catch (error) {
475
+ if (error instanceof TimeoutError)
476
+ throw new MongoOperationTimeoutError('KMS request timed out');
477
+ throw error;
478
+ } finally {
479
+ // There's no need for any more activity on this socket at this point.
480
+ destroySockets();
481
+ abortListener?.[kDispose]();
482
+ }
483
+ }
484
+
485
+ *requests(context: MongoCryptContext, options?: { timeoutContext?: TimeoutContext } & Abortable) {
486
+ for (
487
+ let request = context.nextKMSRequest();
488
+ request != null;
489
+ request = context.nextKMSRequest()
490
+ ) {
491
+ yield this.kmsRequest(request, options);
492
+ }
493
+ }
494
+
495
+ /**
496
+ * Validates the provided TLS options are secure.
497
+ *
498
+ * @param kmsProvider - The KMS provider name.
499
+ * @param tlsOptions - The client TLS options for the provider.
500
+ *
501
+ * @returns An error if any option is invalid.
502
+ */
503
+ validateTlsOptions(
504
+ kmsProvider: string,
505
+ tlsOptions: ClientEncryptionTlsOptions
506
+ ): MongoCryptError | void {
507
+ const tlsOptionNames = Object.keys(tlsOptions);
508
+ for (const option of INSECURE_TLS_OPTIONS) {
509
+ if (tlsOptionNames.includes(option)) {
510
+ return new MongoCryptError(`Insecure TLS options prohibited for ${kmsProvider}: ${option}`);
511
+ }
512
+ }
513
+ }
514
+
515
+ /**
516
+ * Sets only the valid secure TLS options.
517
+ *
518
+ * @param tlsOptions - The client TLS options for the provider.
519
+ * @param options - The existing connection options.
520
+ */
521
+ async setTlsOptions(
522
+ tlsOptions: ClientEncryptionTlsOptions,
523
+ options: tls.ConnectionOptions
524
+ ): Promise<void> {
525
+ // If a secureContext is provided, ensure it is set.
526
+ if (tlsOptions.secureContext) {
527
+ options.secureContext = tlsOptions.secureContext;
528
+ }
529
+ if (tlsOptions.tlsCertificateKeyFile) {
530
+ const cert = await fs.readFile(tlsOptions.tlsCertificateKeyFile);
531
+ options.cert = options.key = cert;
532
+ }
533
+ if (tlsOptions.tlsCAFile) {
534
+ options.ca = await fs.readFile(tlsOptions.tlsCAFile);
535
+ }
536
+ if (tlsOptions.tlsCertificateKeyFilePassword) {
537
+ options.passphrase = tlsOptions.tlsCertificateKeyFilePassword;
538
+ }
539
+ }
540
+
541
+ /**
542
+ * Fetches collection info for a provided namespace, when libmongocrypt
543
+ * enters the `MONGOCRYPT_CTX_NEED_MONGO_COLLINFO` state. The result is
544
+ * used to inform libmongocrypt of the schema associated with this
545
+ * namespace. Exposed for testing purposes. Do not directly invoke.
546
+ *
547
+ * @param client - A MongoClient connected to the topology
548
+ * @param ns - The namespace to list collections from
549
+ * @param filter - A filter for the listCollections command
550
+ * @param callback - Invoked with the info of the requested collection, or with an error
551
+ */
552
+ fetchCollectionInfo(
553
+ client: MongoClient,
554
+ ns: string,
555
+ filter: Document,
556
+ options?: { timeoutContext?: TimeoutContext } & Abortable
557
+ ): AsyncIterable<CollectionInfo> {
558
+ const { db } = MongoDBCollectionNamespace.fromString(ns);
559
+
560
+ const cursor = client.db(db).listCollections(filter, {
561
+ promoteLongs: false,
562
+ promoteValues: false,
563
+ timeoutContext:
564
+ options?.timeoutContext && new CursorTimeoutContext(options?.timeoutContext, Symbol()),
565
+ signal: options?.signal,
566
+ nameOnly: false
567
+ });
568
+
569
+ return cursor;
570
+ }
571
+
572
+ /**
573
+ * Calls to the mongocryptd to provide markings for a command.
574
+ * Exposed for testing purposes. Do not directly invoke.
575
+ * @param client - A MongoClient connected to a mongocryptd
576
+ * @param ns - The namespace (database.collection) the command is being executed on
577
+ * @param command - The command to execute.
578
+ * @param callback - Invoked with the serialized and marked bson command, or with an error
579
+ */
580
+ async markCommand(
581
+ client: MongoClient,
582
+ ns: string,
583
+ command: Uint8Array,
584
+ options?: { timeoutContext?: TimeoutContext } & Abortable
585
+ ): Promise<Uint8Array> {
586
+ const { db } = MongoDBCollectionNamespace.fromString(ns);
587
+ const bsonOptions = { promoteLongs: false, promoteValues: false };
588
+ const rawCommand = deserialize(command, bsonOptions);
589
+
590
+ const commandOptions: {
591
+ timeoutMS?: number;
592
+ signal?: AbortSignal;
593
+ } = {
594
+ timeoutMS: undefined,
595
+ signal: undefined
596
+ };
597
+
598
+ if (options?.timeoutContext?.csotEnabled()) {
599
+ commandOptions.timeoutMS = options.timeoutContext.remainingTimeMS;
600
+ }
601
+ if (options?.signal) {
602
+ commandOptions.signal = options.signal;
603
+ }
604
+
605
+ const response = await client.db(db).command(rawCommand, {
606
+ ...bsonOptions,
607
+ ...commandOptions
608
+ });
609
+
610
+ return serialize(response, this.bsonOptions);
611
+ }
612
+
613
+ /**
614
+ * Requests keys from the keyVault collection on the topology.
615
+ * Exposed for testing purposes. Do not directly invoke.
616
+ * @param client - A MongoClient connected to the topology
617
+ * @param keyVaultNamespace - The namespace (database.collection) of the keyVault Collection
618
+ * @param filter - The filter for the find query against the keyVault Collection
619
+ * @param callback - Invoked with the found keys, or with an error
620
+ */
621
+ fetchKeys(
622
+ client: MongoClient,
623
+ keyVaultNamespace: string,
624
+ filter: Uint8Array,
625
+ options?: { timeoutContext?: TimeoutContext } & Abortable
626
+ ): Promise<Array<DataKey>> {
627
+ const { db: dbName, collection: collectionName } =
628
+ MongoDBCollectionNamespace.fromString(keyVaultNamespace);
629
+
630
+ const commandOptions: {
631
+ timeoutContext?: CursorTimeoutContext;
632
+ signal?: AbortSignal;
633
+ } = {
634
+ timeoutContext: undefined,
635
+ signal: undefined
636
+ };
637
+
638
+ if (options?.timeoutContext != null) {
639
+ commandOptions.timeoutContext = new CursorTimeoutContext(options.timeoutContext, Symbol());
640
+ }
641
+ if (options?.signal != null) {
642
+ commandOptions.signal = options.signal;
643
+ }
644
+
645
+ return client
646
+ .db(dbName)
647
+ .collection<DataKey>(collectionName, { readConcern: { level: 'majority' } })
648
+ .find(deserialize(filter), commandOptions)
649
+ .toArray();
650
+ }
651
+ }