@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,77 @@
1
+ import type { Document } from '../../bson';
2
+ import { MongoRuntimeError } from '../../error';
3
+ import type { HandshakeDocument } from '../connect';
4
+ import type { Connection, ConnectionOptions } from '../connection';
5
+ import type { MongoCredentials } from './mongo_credentials';
6
+
7
+ /**
8
+ * Context used during authentication
9
+ * @internal
10
+ */
11
+ export class AuthContext {
12
+ /** The connection to authenticate */
13
+ connection: Connection;
14
+ /** The credentials to use for authentication */
15
+ credentials?: MongoCredentials;
16
+ /** If the context is for reauthentication. */
17
+ reauthenticating = false;
18
+ /** The options passed to the `connect` method */
19
+ options: ConnectionOptions;
20
+
21
+ /** A response from an initial auth attempt, only some mechanisms use this (e.g, SCRAM) */
22
+ response?: Document;
23
+ /** A random nonce generated for use in an authentication conversation */
24
+ nonce?: Buffer;
25
+
26
+ constructor(
27
+ connection: Connection,
28
+ credentials: MongoCredentials | undefined,
29
+ options: ConnectionOptions
30
+ ) {
31
+ this.connection = connection;
32
+ this.credentials = credentials;
33
+ this.options = options;
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Provider used during authentication.
39
+ * @internal
40
+ */
41
+ export abstract class AuthProvider {
42
+ /**
43
+ * Prepare the handshake document before the initial handshake.
44
+ *
45
+ * @param handshakeDoc - The document used for the initial handshake on a connection
46
+ * @param authContext - Context for authentication flow
47
+ */
48
+ async prepare(
49
+ handshakeDoc: HandshakeDocument,
50
+ _authContext: AuthContext
51
+ ): Promise<HandshakeDocument> {
52
+ return handshakeDoc;
53
+ }
54
+
55
+ /**
56
+ * Authenticate
57
+ *
58
+ * @param context - A shared context for authentication flow
59
+ */
60
+ abstract auth(context: AuthContext): Promise<void>;
61
+
62
+ /**
63
+ * Reauthenticate.
64
+ * @param context - The shared auth context.
65
+ */
66
+ async reauth(context: AuthContext): Promise<void> {
67
+ if (context.reauthenticating) {
68
+ throw new MongoRuntimeError('Reauthentication already in progress.');
69
+ }
70
+ try {
71
+ context.reauthenticating = true;
72
+ await this.auth(context);
73
+ } finally {
74
+ context.reauthenticating = false;
75
+ }
76
+ }
77
+ }
@@ -0,0 +1,207 @@
1
+ import { BSON } from '../../bson';
2
+ import { type AWSCredentials } from '../../deps';
3
+
4
+ export type AwsSigv4Options = {
5
+ path: '/';
6
+ body: string;
7
+ host: string;
8
+ method: 'POST';
9
+ headers: {
10
+ 'Content-Type': 'application/x-www-form-urlencoded';
11
+ 'Content-Length': number;
12
+ 'X-MongoDB-Server-Nonce': string;
13
+ 'X-MongoDB-GS2-CB-Flag': 'n';
14
+ };
15
+ service: string;
16
+ region: string;
17
+ date: Date;
18
+ };
19
+
20
+ export type SignedHeaders = {
21
+ Authorization: string;
22
+ 'X-Amz-Date': string;
23
+ };
24
+
25
+ /**
26
+ * Calculates the SHA-256 hash of a string.
27
+ *
28
+ * @param str - String to hash.
29
+ * @returns Hexadecimal representation of the hash.
30
+ */
31
+ const getHexSha256 = async (str: string): Promise<string> => {
32
+ const data = stringToBuffer(str);
33
+ const hashBuffer = await crypto.subtle.digest('SHA-256', data);
34
+ const hashHex = BSON.onDemand.ByteUtils.toHex(new Uint8Array(hashBuffer));
35
+ return hashHex;
36
+ };
37
+
38
+ /**
39
+ * Calculates the HMAC-SHA256 of a string using the provided key.
40
+ * @param key - Key to use for HMAC calculation. Can be a string or Uint8Array.
41
+ * @param str - String to calculate HMAC for.
42
+ * @returns Uint8Array containing the HMAC-SHA256 digest.
43
+ */
44
+ const getHmacSha256 = async (key: string | Uint8Array, str: string): Promise<Uint8Array> => {
45
+ let keyData: Uint8Array;
46
+ if (typeof key === 'string') {
47
+ keyData = stringToBuffer(key);
48
+ } else {
49
+ keyData = key;
50
+ }
51
+
52
+ const importedKey = await crypto.subtle.importKey(
53
+ 'raw',
54
+ keyData,
55
+ { name: 'HMAC', hash: { name: 'SHA-256' } },
56
+ false,
57
+ ['sign']
58
+ );
59
+ const strData = stringToBuffer(str);
60
+ const signature = await crypto.subtle.sign('HMAC', importedKey, strData);
61
+ const digest = new Uint8Array(signature);
62
+ return digest;
63
+ };
64
+
65
+ /**
66
+ * Converts header values according to AWS requirements,
67
+ * From https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv-create-signed-request.html#create-canonical-request
68
+ * For values, you must:
69
+ - trim any leading or trailing spaces.
70
+ - convert sequential spaces to a single space.
71
+ * @param value - Header value to convert.
72
+ * @returns - Converted header value.
73
+ */
74
+ const convertHeaderValue = (value: string | number) => {
75
+ return value.toString().trim().replace(/\s+/g, ' ');
76
+ };
77
+
78
+ /**
79
+ * Returns a Uint8Array representation of a string, encoded in UTF-8.
80
+ * @param str - String to convert.
81
+ * @returns Uint8Array containing the UTF-8 encoded string.
82
+ */
83
+ function stringToBuffer(str: string): Uint8Array {
84
+ const data = new Uint8Array(BSON.onDemand.ByteUtils.utf8ByteLength(str));
85
+ BSON.onDemand.ByteUtils.encodeUTF8Into(data, str, 0);
86
+ return data;
87
+ }
88
+
89
+ /**
90
+ * This method implements AWS Signature 4 logic for a very specific request format.
91
+ * The signing logic is described here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv-create-signed-request.html
92
+ */
93
+ export async function aws4Sign(
94
+ options: AwsSigv4Options,
95
+ credentials: AWSCredentials
96
+ ): Promise<SignedHeaders> {
97
+ /**
98
+ * From the spec: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv-create-signed-request.html
99
+ *
100
+ * Summary of signing steps
101
+ * 1. Create a canonical request
102
+ * Arrange the contents of your request (host, action, headers, etc.) into a standard canonical format. The canonical request is one of the inputs used to create the string to sign.
103
+ * 2. Create a hash of the canonical request
104
+ * Hash the canonical request using the same algorithm that you used to create the hash of the payload. The hash of the canonical request is a string of lowercase hexadecimal characters.
105
+ * 3. Create a string to sign
106
+ * Create a string to sign with the canonical request and extra information such as the algorithm, request date, credential scope, and the hash of the canonical request.
107
+ * 4. Derive a signing key
108
+ * Use the secret access key to derive the key used to sign the request.
109
+ * 5. Calculate the signature
110
+ * Perform a keyed hash operation on the string to sign using the derived signing key as the hash key.
111
+ * 6. Add the signature to the request
112
+ * Add the calculated signature to an HTTP header or to the query string of the request.
113
+ */
114
+
115
+ // 1: Create a canonical request
116
+
117
+ // Date – The date and time used to sign the request.
118
+ const date = options.date;
119
+ // RequestDateTime – The date and time used in the credential scope. This value is the current UTC time in ISO 8601 format (for example, 20130524T000000Z).
120
+ const requestDateTime = date.toISOString().replace(/[:-]|\.\d{3}/g, '');
121
+ // RequestDate – The date used in the credential scope. This value is the current UTC date in YYYYMMDD format (for example, 20130524).
122
+ const requestDate = requestDateTime.substring(0, 8);
123
+ // Method – The HTTP request method. For us, this is always 'POST'.
124
+ const method = options.method;
125
+ // CanonicalUri – The URI-encoded version of the absolute path component URI, starting with the / that follows the domain name and up to the end of the string
126
+ // For our requests, this is always '/'
127
+ const canonicalUri = options.path;
128
+ // CanonicalQueryString – The URI-encoded query string parameters. For our requests, there are no query string parameters, so this is always an empty string.
129
+ const canonicalQuerystring = '';
130
+
131
+ // CanonicalHeaders – A list of request headers with their values. Individual header name and value pairs are separated by the newline character ("\n").
132
+ // All of our known/expected headers are included here, there are no extra headers.
133
+ const headers = new Headers({
134
+ 'content-length': convertHeaderValue(options.headers['Content-Length']),
135
+ 'content-type': convertHeaderValue(options.headers['Content-Type']),
136
+ host: convertHeaderValue(options.host),
137
+ 'x-amz-date': convertHeaderValue(requestDateTime),
138
+ 'x-mongodb-gs2-cb-flag': convertHeaderValue(options.headers['X-MongoDB-GS2-CB-Flag']),
139
+ 'x-mongodb-server-nonce': convertHeaderValue(options.headers['X-MongoDB-Server-Nonce'])
140
+ });
141
+ // If session token is provided, include it in the headers
142
+ if ('sessionToken' in credentials && credentials.sessionToken) {
143
+ headers.append('x-amz-security-token', convertHeaderValue(credentials.sessionToken));
144
+ }
145
+
146
+ // Canonical headers are lowercased and sorted.
147
+ const canonicalHeaders = Array.from(headers.entries())
148
+ .map(([key, value]) => `${key.toLowerCase()}:${value}`)
149
+ .sort()
150
+ .join('\n');
151
+ const canonicalHeaderNames = Array.from(headers.keys()).map(header => header.toLowerCase());
152
+ // SignedHeaders – An alphabetically sorted, semicolon-separated list of lowercase request header names.
153
+ const signedHeaders = canonicalHeaderNames.sort().join(';');
154
+
155
+ // HashedPayload – A string created using the payload in the body of the HTTP request as input to a hash function. This string uses lowercase hexadecimal characters.
156
+ const hashedPayload = await getHexSha256(options.body);
157
+
158
+ // CanonicalRequest – A string that includes the above elements, separated by newline characters.
159
+ const canonicalRequest = [
160
+ method,
161
+ canonicalUri,
162
+ canonicalQuerystring,
163
+ canonicalHeaders + '\n',
164
+ signedHeaders,
165
+ hashedPayload
166
+ ].join('\n');
167
+
168
+ // 2. Create a hash of the canonical request
169
+ // HashedCanonicalRequest – A string created by using the canonical request as input to a hash function.
170
+ const hashedCanonicalRequest = await getHexSha256(canonicalRequest);
171
+
172
+ // 3. Create a string to sign
173
+ // Algorithm – The algorithm used to create the hash of the canonical request. For SigV4, use AWS4-HMAC-SHA256.
174
+ const algorithm = 'AWS4-HMAC-SHA256';
175
+ // CredentialScope – The credential scope, which restricts the resulting signature to the specified Region and service.
176
+ // Has the following format: YYYYMMDD/region/service/aws4_request.
177
+ const credentialScope = `${requestDate}/${options.region}/${options.service}/aws4_request`;
178
+ // StringToSign – A string that includes the above elements, separated by newline characters.
179
+ const stringToSign = [algorithm, requestDateTime, credentialScope, hashedCanonicalRequest].join(
180
+ '\n'
181
+ );
182
+
183
+ // 4. Derive a signing key
184
+ // To derive a signing key for SigV4, perform a succession of keyed hash operations (HMAC) on the request date, Region, and service, with your AWS secret access key as the key for the initial hashing operation.
185
+ const dateKey = await getHmacSha256('AWS4' + credentials.secretAccessKey, requestDate);
186
+ const dateRegionKey = await getHmacSha256(dateKey, options.region);
187
+ const dateRegionServiceKey = await getHmacSha256(dateRegionKey, options.service);
188
+ const signingKey = await getHmacSha256(dateRegionServiceKey, 'aws4_request');
189
+
190
+ // 5. Calculate the signature
191
+ const signatureBuffer = await getHmacSha256(signingKey, stringToSign);
192
+ const signature = BSON.onDemand.ByteUtils.toHex(signatureBuffer);
193
+
194
+ // 6. Add the signature to the request
195
+ // Calculate the Authorization header
196
+ const authorizationHeader = [
197
+ 'AWS4-HMAC-SHA256 Credential=' + credentials.accessKeyId + '/' + credentialScope,
198
+ 'SignedHeaders=' + signedHeaders,
199
+ 'Signature=' + signature
200
+ ].join(', ');
201
+
202
+ // Return the calculated headers
203
+ return {
204
+ Authorization: authorizationHeader,
205
+ 'X-Amz-Date': requestDateTime
206
+ };
207
+ }
@@ -0,0 +1,129 @@
1
+ import * as process from 'process';
2
+
3
+ import { type AWSCredentials, getAwsCredentialProvider } from '../../deps';
4
+ import { MongoAWSError } from '../../error';
5
+
6
+ /**
7
+ * @internal
8
+ * This interface matches the final result of fetching temporary credentials manually, outlined
9
+ * in the spec [here](https://github.com/mongodb/specifications/blob/master/source/auth/auth.md#ec2-endpoint).
10
+ *
11
+ * When we use the AWS SDK, we map the response from the SDK to conform to this interface.
12
+ */
13
+ export interface AWSTempCredentials {
14
+ AccessKeyId?: string;
15
+ SecretAccessKey?: string;
16
+ Token?: string;
17
+ RoleArn?: string;
18
+ Expiration?: Date;
19
+ }
20
+
21
+ /** @public **/
22
+ export type AWSCredentialProvider = () => Promise<AWSCredentials>;
23
+
24
+ /** @internal */
25
+ export class AWSSDKCredentialProvider {
26
+ private static _awsSDK: ReturnType<typeof getAwsCredentialProvider>;
27
+ private _provider?: AWSCredentialProvider;
28
+
29
+ /**
30
+ * Create the SDK credentials provider.
31
+ * @param credentialsProvider - The credentials provider.
32
+ */
33
+ constructor(credentialsProvider?: AWSCredentialProvider) {
34
+ if (credentialsProvider) {
35
+ this._provider = credentialsProvider;
36
+ }
37
+ }
38
+
39
+ static get awsSDK() {
40
+ AWSSDKCredentialProvider._awsSDK ??= getAwsCredentialProvider();
41
+ return AWSSDKCredentialProvider._awsSDK;
42
+ }
43
+
44
+ /**
45
+ * The AWS SDK caches credentials automatically and handles refresh when the credentials have expired.
46
+ * To ensure this occurs, we need to cache the `provider` returned by the AWS sdk and re-use it when fetching credentials.
47
+ */
48
+ private get provider(): () => Promise<AWSCredentials> {
49
+ if ('kModuleError' in AWSSDKCredentialProvider.awsSDK) {
50
+ throw AWSSDKCredentialProvider.awsSDK.kModuleError;
51
+ }
52
+ if (this._provider) {
53
+ return this._provider;
54
+ }
55
+ let { AWS_STS_REGIONAL_ENDPOINTS = '', AWS_REGION = '' } = process.env;
56
+ AWS_STS_REGIONAL_ENDPOINTS = AWS_STS_REGIONAL_ENDPOINTS.toLowerCase();
57
+ AWS_REGION = AWS_REGION.toLowerCase();
58
+
59
+ /** The option setting should work only for users who have explicit settings in their environment, the driver should not encode "defaults" */
60
+ const awsRegionSettingsExist =
61
+ AWS_REGION.length !== 0 && AWS_STS_REGIONAL_ENDPOINTS.length !== 0;
62
+
63
+ /**
64
+ * The following regions use the global AWS STS endpoint, sts.amazonaws.com, by default
65
+ * https://docs.aws.amazon.com/sdkref/latest/guide/feature-sts-regionalized-endpoints.html
66
+ */
67
+ const LEGACY_REGIONS = new Set([
68
+ 'ap-northeast-1',
69
+ 'ap-south-1',
70
+ 'ap-southeast-1',
71
+ 'ap-southeast-2',
72
+ 'aws-global',
73
+ 'ca-central-1',
74
+ 'eu-central-1',
75
+ 'eu-north-1',
76
+ 'eu-west-1',
77
+ 'eu-west-2',
78
+ 'eu-west-3',
79
+ 'sa-east-1',
80
+ 'us-east-1',
81
+ 'us-east-2',
82
+ 'us-west-1',
83
+ 'us-west-2'
84
+ ]);
85
+ /**
86
+ * If AWS_STS_REGIONAL_ENDPOINTS is set to regional, users are opting into the new behavior of respecting the region settings
87
+ *
88
+ * If AWS_STS_REGIONAL_ENDPOINTS is set to legacy, then "old" regions need to keep using the global setting.
89
+ * Technically the SDK gets this wrong, it reaches out to 'sts.us-east-1.amazonaws.com' when it should be 'sts.amazonaws.com'.
90
+ * That is not our bug to fix here. We leave that up to the SDK.
91
+ */
92
+ const useRegionalSts =
93
+ AWS_STS_REGIONAL_ENDPOINTS === 'regional' ||
94
+ (AWS_STS_REGIONAL_ENDPOINTS === 'legacy' && !LEGACY_REGIONS.has(AWS_REGION));
95
+
96
+ this._provider =
97
+ awsRegionSettingsExist && useRegionalSts
98
+ ? AWSSDKCredentialProvider.awsSDK.fromNodeProviderChain({
99
+ clientConfig: { region: AWS_REGION }
100
+ })
101
+ : AWSSDKCredentialProvider.awsSDK.fromNodeProviderChain();
102
+
103
+ return this._provider;
104
+ }
105
+
106
+ async getCredentials(): Promise<AWSTempCredentials> {
107
+ /*
108
+ * Creates a credential provider that will attempt to find credentials from the
109
+ * following sources (listed in order of precedence):
110
+ *
111
+ * - Environment variables exposed via process.env
112
+ * - SSO credentials from token cache
113
+ * - Web identity token credentials
114
+ * - Shared credentials and config ini files
115
+ * - The EC2/ECS Instance Metadata Service
116
+ */
117
+ try {
118
+ const creds = await this.provider();
119
+ return {
120
+ AccessKeyId: creds.accessKeyId,
121
+ SecretAccessKey: creds.secretAccessKey,
122
+ Token: creds.sessionToken,
123
+ Expiration: creds.expiration
124
+ };
125
+ } catch (error) {
126
+ throw new MongoAWSError(error.message, { cause: error });
127
+ }
128
+ }
129
+ }
@@ -0,0 +1,203 @@
1
+ import * as dns from 'dns';
2
+ import * as os from 'os';
3
+
4
+ import { getKerberos, type Kerberos, type KerberosClient } from '../../deps';
5
+ import { MongoInvalidArgumentError, MongoMissingCredentialsError } from '../../error';
6
+ import { ns } from '../../utils';
7
+ import type { Connection } from '../connection';
8
+ import { type AuthContext, AuthProvider } from './auth_provider';
9
+
10
+ /** @public */
11
+ export const GSSAPICanonicalizationValue = Object.freeze({
12
+ on: true,
13
+ off: false,
14
+ none: 'none',
15
+ forward: 'forward',
16
+ forwardAndReverse: 'forwardAndReverse'
17
+ } as const);
18
+
19
+ /** @public */
20
+ export type GSSAPICanonicalizationValue =
21
+ (typeof GSSAPICanonicalizationValue)[keyof typeof GSSAPICanonicalizationValue];
22
+
23
+ type MechanismProperties = {
24
+ CANONICALIZE_HOST_NAME?: GSSAPICanonicalizationValue;
25
+ SERVICE_HOST?: string;
26
+ SERVICE_NAME?: string;
27
+ SERVICE_REALM?: string;
28
+ };
29
+
30
+ async function externalCommand(
31
+ connection: Connection,
32
+ command: ReturnType<typeof saslStart> | ReturnType<typeof saslContinue>
33
+ ): Promise<{ payload: string; conversationId: number }> {
34
+ const response = await connection.command(ns('$external.$cmd'), command);
35
+ return response as { payload: string; conversationId: number };
36
+ }
37
+
38
+ let krb: Kerberos;
39
+
40
+ export class GSSAPI extends AuthProvider {
41
+ override async auth(authContext: AuthContext): Promise<void> {
42
+ const { connection, credentials } = authContext;
43
+ if (credentials == null) {
44
+ throw new MongoMissingCredentialsError('Credentials required for GSSAPI authentication');
45
+ }
46
+
47
+ const { username } = credentials;
48
+
49
+ const client = await makeKerberosClient(authContext);
50
+
51
+ const payload = await client.step('');
52
+
53
+ const saslStartResponse = await externalCommand(connection, saslStart(payload));
54
+
55
+ const negotiatedPayload = await negotiate(client, 10, saslStartResponse.payload);
56
+
57
+ const saslContinueResponse = await externalCommand(
58
+ connection,
59
+ saslContinue(negotiatedPayload, saslStartResponse.conversationId)
60
+ );
61
+
62
+ const finalizePayload = await finalize(client, username, saslContinueResponse.payload);
63
+
64
+ await externalCommand(connection, {
65
+ saslContinue: 1,
66
+ conversationId: saslContinueResponse.conversationId,
67
+ payload: finalizePayload
68
+ });
69
+ }
70
+ }
71
+
72
+ async function makeKerberosClient(authContext: AuthContext): Promise<KerberosClient> {
73
+ const { hostAddress } = authContext.options;
74
+ const { credentials } = authContext;
75
+ if (!hostAddress || typeof hostAddress.host !== 'string' || !credentials) {
76
+ throw new MongoInvalidArgumentError(
77
+ 'Connection must have host and port and credentials defined.'
78
+ );
79
+ }
80
+
81
+ loadKrb();
82
+ if ('kModuleError' in krb) {
83
+ throw krb['kModuleError'];
84
+ }
85
+ const { initializeClient } = krb;
86
+
87
+ const { username, password } = credentials;
88
+ const mechanismProperties = credentials.mechanismProperties as MechanismProperties;
89
+
90
+ const serviceName = mechanismProperties.SERVICE_NAME ?? 'mongodb';
91
+
92
+ const host = await performGSSAPICanonicalizeHostName(hostAddress.host, mechanismProperties);
93
+
94
+ const initOptions = {};
95
+ if (password != null) {
96
+ // TODO(NODE-5139): These do not match the typescript options in initializeClient
97
+ Object.assign(initOptions, { user: username, password: password });
98
+ }
99
+
100
+ const spnHost = mechanismProperties.SERVICE_HOST ?? host;
101
+ let spn = `${serviceName}${os.platform() === 'win32' ? '/' : '@'}${spnHost}`;
102
+ if ('SERVICE_REALM' in mechanismProperties) {
103
+ spn = `${spn}@${mechanismProperties.SERVICE_REALM}`;
104
+ }
105
+
106
+ return await initializeClient(spn, initOptions);
107
+ }
108
+
109
+ function saslStart(payload: string) {
110
+ return {
111
+ saslStart: 1,
112
+ mechanism: 'GSSAPI',
113
+ payload,
114
+ autoAuthorize: 1
115
+ } as const;
116
+ }
117
+
118
+ function saslContinue(payload: string, conversationId: number) {
119
+ return {
120
+ saslContinue: 1,
121
+ conversationId,
122
+ payload
123
+ } as const;
124
+ }
125
+
126
+ async function negotiate(
127
+ client: KerberosClient,
128
+ retries: number,
129
+ payload: string
130
+ ): Promise<string> {
131
+ try {
132
+ const response = await client.step(payload);
133
+ return response || '';
134
+ } catch (error) {
135
+ if (retries === 0) {
136
+ // Retries exhausted, raise error
137
+ throw error;
138
+ }
139
+ // Adjust number of retries and call step again
140
+ return await negotiate(client, retries - 1, payload);
141
+ }
142
+ }
143
+
144
+ async function finalize(client: KerberosClient, user: string, payload: string): Promise<string> {
145
+ // GSS Client Unwrap
146
+ const response = await client.unwrap(payload);
147
+ return await client.wrap(response || '', { user });
148
+ }
149
+
150
+ export async function performGSSAPICanonicalizeHostName(
151
+ host: string,
152
+ mechanismProperties: MechanismProperties
153
+ ): Promise<string> {
154
+ const mode = mechanismProperties.CANONICALIZE_HOST_NAME;
155
+ if (!mode || mode === GSSAPICanonicalizationValue.none) {
156
+ return host;
157
+ }
158
+
159
+ // If forward and reverse or true
160
+ if (
161
+ mode === GSSAPICanonicalizationValue.on ||
162
+ mode === GSSAPICanonicalizationValue.forwardAndReverse
163
+ ) {
164
+ // Perform the lookup of the ip address.
165
+ const { address } = await dns.promises.lookup(host);
166
+
167
+ try {
168
+ // Perform a reverse ptr lookup on the ip address.
169
+ const results = await dns.promises.resolvePtr(address);
170
+ // If the ptr did not error but had no results, return the host.
171
+ return results.length > 0 ? results[0] : host;
172
+ } catch {
173
+ // This can error as ptr records may not exist for all ips. In this case
174
+ // fallback to a cname lookup as dns.lookup() does not return the
175
+ // cname.
176
+ return await resolveCname(host);
177
+ }
178
+ } else {
179
+ // The case for forward is just to resolve the cname as dns.lookup()
180
+ // will not return it.
181
+ return await resolveCname(host);
182
+ }
183
+ }
184
+
185
+ export async function resolveCname(host: string): Promise<string> {
186
+ // Attempt to resolve the host name
187
+ try {
188
+ const results = await dns.promises.resolveCname(host);
189
+ // Get the first resolved host id
190
+ return results.length > 0 ? results[0] : host;
191
+ } catch {
192
+ return host;
193
+ }
194
+ }
195
+
196
+ /**
197
+ * Load the Kerberos library.
198
+ */
199
+ function loadKrb() {
200
+ if (!krb) {
201
+ krb = getKerberos();
202
+ }
203
+ }