@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,146 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ServerHeartbeatFailedEvent = exports.ServerHeartbeatSucceededEvent = exports.ServerHeartbeatStartedEvent = exports.TopologyClosedEvent = exports.TopologyOpeningEvent = exports.TopologyDescriptionChangedEvent = exports.ServerClosedEvent = exports.ServerOpeningEvent = exports.ServerDescriptionChangedEvent = void 0;
4
+ const constants_1 = require("../constants");
5
+ /**
6
+ * Emitted when server description changes, but does NOT include changes to the RTT.
7
+ * @public
8
+ * @category Event
9
+ */
10
+ class ServerDescriptionChangedEvent {
11
+ /** @internal */
12
+ constructor(topologyId, address, previousDescription, newDescription) {
13
+ this.name = constants_1.SERVER_DESCRIPTION_CHANGED;
14
+ this.topologyId = topologyId;
15
+ this.address = address;
16
+ this.previousDescription = previousDescription;
17
+ this.newDescription = newDescription;
18
+ }
19
+ }
20
+ exports.ServerDescriptionChangedEvent = ServerDescriptionChangedEvent;
21
+ /**
22
+ * Emitted when server is initialized.
23
+ * @public
24
+ * @category Event
25
+ */
26
+ class ServerOpeningEvent {
27
+ /** @internal */
28
+ constructor(topologyId, address) {
29
+ /** @internal */
30
+ this.name = constants_1.SERVER_OPENING;
31
+ this.topologyId = topologyId;
32
+ this.address = address;
33
+ }
34
+ }
35
+ exports.ServerOpeningEvent = ServerOpeningEvent;
36
+ /**
37
+ * Emitted when server is closed.
38
+ * @public
39
+ * @category Event
40
+ */
41
+ class ServerClosedEvent {
42
+ /** @internal */
43
+ constructor(topologyId, address) {
44
+ /** @internal */
45
+ this.name = constants_1.SERVER_CLOSED;
46
+ this.topologyId = topologyId;
47
+ this.address = address;
48
+ }
49
+ }
50
+ exports.ServerClosedEvent = ServerClosedEvent;
51
+ /**
52
+ * Emitted when topology description changes.
53
+ * @public
54
+ * @category Event
55
+ */
56
+ class TopologyDescriptionChangedEvent {
57
+ /** @internal */
58
+ constructor(topologyId, previousDescription, newDescription) {
59
+ /** @internal */
60
+ this.name = constants_1.TOPOLOGY_DESCRIPTION_CHANGED;
61
+ this.topologyId = topologyId;
62
+ this.previousDescription = previousDescription;
63
+ this.newDescription = newDescription;
64
+ }
65
+ }
66
+ exports.TopologyDescriptionChangedEvent = TopologyDescriptionChangedEvent;
67
+ /**
68
+ * Emitted when topology is initialized.
69
+ * @public
70
+ * @category Event
71
+ */
72
+ class TopologyOpeningEvent {
73
+ /** @internal */
74
+ constructor(topologyId) {
75
+ /** @internal */
76
+ this.name = constants_1.TOPOLOGY_OPENING;
77
+ this.topologyId = topologyId;
78
+ }
79
+ }
80
+ exports.TopologyOpeningEvent = TopologyOpeningEvent;
81
+ /**
82
+ * Emitted when topology is closed.
83
+ * @public
84
+ * @category Event
85
+ */
86
+ class TopologyClosedEvent {
87
+ /** @internal */
88
+ constructor(topologyId) {
89
+ /** @internal */
90
+ this.name = constants_1.TOPOLOGY_CLOSED;
91
+ this.topologyId = topologyId;
92
+ }
93
+ }
94
+ exports.TopologyClosedEvent = TopologyClosedEvent;
95
+ /**
96
+ * Emitted when the server monitor’s hello command is started - immediately before
97
+ * the hello command is serialized into raw BSON and written to the socket.
98
+ *
99
+ * @public
100
+ * @category Event
101
+ */
102
+ class ServerHeartbeatStartedEvent {
103
+ /** @internal */
104
+ constructor(connectionId, awaited) {
105
+ /** @internal */
106
+ this.name = constants_1.SERVER_HEARTBEAT_STARTED;
107
+ this.connectionId = connectionId;
108
+ this.awaited = awaited;
109
+ }
110
+ }
111
+ exports.ServerHeartbeatStartedEvent = ServerHeartbeatStartedEvent;
112
+ /**
113
+ * Emitted when the server monitor’s hello succeeds.
114
+ * @public
115
+ * @category Event
116
+ */
117
+ class ServerHeartbeatSucceededEvent {
118
+ /** @internal */
119
+ constructor(connectionId, duration, reply, awaited) {
120
+ /** @internal */
121
+ this.name = constants_1.SERVER_HEARTBEAT_SUCCEEDED;
122
+ this.connectionId = connectionId;
123
+ this.duration = duration;
124
+ this.reply = reply ?? {};
125
+ this.awaited = awaited;
126
+ }
127
+ }
128
+ exports.ServerHeartbeatSucceededEvent = ServerHeartbeatSucceededEvent;
129
+ /**
130
+ * Emitted when the server monitor’s hello fails, either with an “ok: 0” or a socket exception.
131
+ * @public
132
+ * @category Event
133
+ */
134
+ class ServerHeartbeatFailedEvent {
135
+ /** @internal */
136
+ constructor(connectionId, duration, failure, awaited) {
137
+ /** @internal */
138
+ this.name = constants_1.SERVER_HEARTBEAT_FAILED;
139
+ this.connectionId = connectionId;
140
+ this.duration = duration;
141
+ this.failure = failure;
142
+ this.awaited = awaited;
143
+ }
144
+ }
145
+ exports.ServerHeartbeatFailedEvent = ServerHeartbeatFailedEvent;
146
+ //# sourceMappingURL=events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.js","sourceRoot":"","sources":["../../src/sdam/events.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,544 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RTTSampler = exports.MonitorInterval = exports.RTTPinger = exports.Monitor = exports.ServerMonitoringMode = void 0;
4
+ const timers_1 = require("timers");
5
+ const bson_1 = require("../bson");
6
+ const connect_1 = require("../cmap/connect");
7
+ const client_metadata_1 = require("../cmap/handshake/client_metadata");
8
+ const constants_1 = require("../constants");
9
+ const error_1 = require("../error");
10
+ const mongo_logger_1 = require("../mongo_logger");
11
+ const mongo_types_1 = require("../mongo_types");
12
+ const utils_1 = require("../utils");
13
+ const common_1 = require("./common");
14
+ const events_1 = require("./events");
15
+ const server_1 = require("./server");
16
+ const STATE_IDLE = 'idle';
17
+ const STATE_MONITORING = 'monitoring';
18
+ const stateTransition = (0, utils_1.makeStateMachine)({
19
+ [common_1.STATE_CLOSING]: [common_1.STATE_CLOSING, STATE_IDLE, common_1.STATE_CLOSED],
20
+ [common_1.STATE_CLOSED]: [common_1.STATE_CLOSED, STATE_MONITORING],
21
+ [STATE_IDLE]: [STATE_IDLE, STATE_MONITORING, common_1.STATE_CLOSING],
22
+ [STATE_MONITORING]: [STATE_MONITORING, STATE_IDLE, common_1.STATE_CLOSING]
23
+ });
24
+ const INVALID_REQUEST_CHECK_STATES = new Set([common_1.STATE_CLOSING, common_1.STATE_CLOSED, STATE_MONITORING]);
25
+ function isInCloseState(monitor) {
26
+ return monitor.s.state === common_1.STATE_CLOSED || monitor.s.state === common_1.STATE_CLOSING;
27
+ }
28
+ /** @public */
29
+ exports.ServerMonitoringMode = Object.freeze({
30
+ auto: 'auto',
31
+ poll: 'poll',
32
+ stream: 'stream'
33
+ });
34
+ /** @internal */
35
+ class Monitor extends mongo_types_1.TypedEventEmitter {
36
+ constructor(server, options) {
37
+ super();
38
+ /** @internal */
39
+ this.component = mongo_logger_1.MongoLoggableComponent.TOPOLOGY;
40
+ this.on('error', utils_1.noop);
41
+ this.server = server;
42
+ this.connection = null;
43
+ this.cancellationToken = new mongo_types_1.CancellationToken();
44
+ this.cancellationToken.setMaxListeners(Infinity);
45
+ this.monitorId = undefined;
46
+ this.s = {
47
+ state: common_1.STATE_CLOSED
48
+ };
49
+ this.address = server.description.address;
50
+ this.options = Object.freeze({
51
+ connectTimeoutMS: options.connectTimeoutMS ?? 10000,
52
+ heartbeatFrequencyMS: options.heartbeatFrequencyMS ?? 10000,
53
+ minHeartbeatFrequencyMS: options.minHeartbeatFrequencyMS ?? 500,
54
+ serverMonitoringMode: options.serverMonitoringMode
55
+ });
56
+ this.isRunningInFaasEnv = (0, client_metadata_1.getFAASEnv)() != null;
57
+ this.mongoLogger = this.server.topology.client?.mongoLogger;
58
+ this.rttSampler = new RTTSampler(10);
59
+ const cancellationToken = this.cancellationToken;
60
+ // TODO: refactor this to pull it directly from the pool, requires new ConnectionPool integration
61
+ const connectOptions = {
62
+ id: '<monitor>',
63
+ generation: server.pool.generation,
64
+ cancellationToken,
65
+ hostAddress: server.description.hostAddress,
66
+ ...options,
67
+ // force BSON serialization options
68
+ raw: false,
69
+ useBigInt64: false,
70
+ promoteLongs: true,
71
+ promoteValues: true,
72
+ promoteBuffers: true
73
+ };
74
+ // ensure no authentication is used for monitoring
75
+ delete connectOptions.credentials;
76
+ if (connectOptions.autoEncrypter) {
77
+ delete connectOptions.autoEncrypter;
78
+ }
79
+ this.connectOptions = Object.freeze(connectOptions);
80
+ }
81
+ connect() {
82
+ if (this.s.state !== common_1.STATE_CLOSED) {
83
+ return;
84
+ }
85
+ // start
86
+ const heartbeatFrequencyMS = this.options.heartbeatFrequencyMS;
87
+ const minHeartbeatFrequencyMS = this.options.minHeartbeatFrequencyMS;
88
+ this.monitorId = new MonitorInterval(monitorServer(this), {
89
+ heartbeatFrequencyMS: heartbeatFrequencyMS,
90
+ minHeartbeatFrequencyMS: minHeartbeatFrequencyMS,
91
+ immediate: true
92
+ });
93
+ }
94
+ requestCheck() {
95
+ if (INVALID_REQUEST_CHECK_STATES.has(this.s.state)) {
96
+ return;
97
+ }
98
+ this.monitorId?.wake();
99
+ }
100
+ reset() {
101
+ const topologyVersion = this.server.description.topologyVersion;
102
+ if (isInCloseState(this) || topologyVersion == null) {
103
+ return;
104
+ }
105
+ stateTransition(this, common_1.STATE_CLOSING);
106
+ resetMonitorState(this);
107
+ // restart monitor
108
+ stateTransition(this, STATE_IDLE);
109
+ // restart monitoring
110
+ const heartbeatFrequencyMS = this.options.heartbeatFrequencyMS;
111
+ const minHeartbeatFrequencyMS = this.options.minHeartbeatFrequencyMS;
112
+ this.monitorId = new MonitorInterval(monitorServer(this), {
113
+ heartbeatFrequencyMS: heartbeatFrequencyMS,
114
+ minHeartbeatFrequencyMS: minHeartbeatFrequencyMS
115
+ });
116
+ }
117
+ close() {
118
+ if (isInCloseState(this)) {
119
+ return;
120
+ }
121
+ stateTransition(this, common_1.STATE_CLOSING);
122
+ resetMonitorState(this);
123
+ // close monitor
124
+ this.emit('close');
125
+ stateTransition(this, common_1.STATE_CLOSED);
126
+ }
127
+ get roundTripTime() {
128
+ return this.rttSampler.average();
129
+ }
130
+ get minRoundTripTime() {
131
+ return this.rttSampler.min();
132
+ }
133
+ get latestRtt() {
134
+ return this.rttSampler.last;
135
+ }
136
+ addRttSample(rtt) {
137
+ this.rttSampler.addSample(rtt);
138
+ }
139
+ clearRttSamples() {
140
+ this.rttSampler.clear();
141
+ }
142
+ }
143
+ exports.Monitor = Monitor;
144
+ function resetMonitorState(monitor) {
145
+ monitor.monitorId?.stop();
146
+ monitor.monitorId = undefined;
147
+ monitor.rttPinger?.close();
148
+ monitor.rttPinger = undefined;
149
+ monitor.cancellationToken.emit('cancel');
150
+ monitor.connection?.destroy();
151
+ monitor.connection = null;
152
+ monitor.clearRttSamples();
153
+ }
154
+ function useStreamingProtocol(monitor, topologyVersion) {
155
+ // If we have no topology version we always poll no matter
156
+ // what the user provided, since the server does not support
157
+ // the streaming protocol.
158
+ if (topologyVersion == null)
159
+ return false;
160
+ const serverMonitoringMode = monitor.options.serverMonitoringMode;
161
+ if (serverMonitoringMode === exports.ServerMonitoringMode.poll)
162
+ return false;
163
+ if (serverMonitoringMode === exports.ServerMonitoringMode.stream)
164
+ return true;
165
+ // If we are in auto mode, we need to figure out if we're in a FaaS
166
+ // environment or not and choose the appropriate mode.
167
+ if (monitor.isRunningInFaasEnv)
168
+ return false;
169
+ return true;
170
+ }
171
+ function checkServer(monitor, callback) {
172
+ let start;
173
+ let awaited;
174
+ const topologyVersion = monitor.server.description.topologyVersion;
175
+ const isAwaitable = useStreamingProtocol(monitor, topologyVersion);
176
+ monitor.emitAndLogHeartbeat(server_1.Server.SERVER_HEARTBEAT_STARTED, monitor.server.topology.s.id, undefined, new events_1.ServerHeartbeatStartedEvent(monitor.address, isAwaitable));
177
+ function onHeartbeatFailed(err) {
178
+ monitor.connection?.destroy();
179
+ monitor.connection = null;
180
+ monitor.emitAndLogHeartbeat(server_1.Server.SERVER_HEARTBEAT_FAILED, monitor.server.topology.s.id, undefined, new events_1.ServerHeartbeatFailedEvent(monitor.address, (0, utils_1.calculateDurationInMs)(start), err, awaited));
181
+ const error = !(err instanceof error_1.MongoError)
182
+ ? new error_1.MongoError(error_1.MongoError.buildErrorMessage(err), { cause: err })
183
+ : err;
184
+ error.addErrorLabel(error_1.MongoErrorLabel.ResetPool);
185
+ if (error instanceof error_1.MongoNetworkTimeoutError) {
186
+ error.addErrorLabel(error_1.MongoErrorLabel.InterruptInUseConnections);
187
+ }
188
+ monitor.emit('resetServer', error);
189
+ callback(err);
190
+ }
191
+ function onHeartbeatSucceeded(hello) {
192
+ if (!('isWritablePrimary' in hello)) {
193
+ // Provide hello-style response document.
194
+ hello.isWritablePrimary = hello[constants_1.LEGACY_HELLO_COMMAND];
195
+ }
196
+ // NOTE: here we use the latestRtt as this measurement corresponds with the value
197
+ // obtained for this successful heartbeat, if there is no latestRtt, then we calculate the
198
+ // duration
199
+ const duration = isAwaitable && monitor.rttPinger
200
+ ? (monitor.rttPinger.latestRtt ?? (0, utils_1.calculateDurationInMs)(start))
201
+ : (0, utils_1.calculateDurationInMs)(start);
202
+ monitor.addRttSample(duration);
203
+ monitor.emitAndLogHeartbeat(server_1.Server.SERVER_HEARTBEAT_SUCCEEDED, monitor.server.topology.s.id, hello.connectionId, new events_1.ServerHeartbeatSucceededEvent(monitor.address, duration, hello, isAwaitable));
204
+ if (isAwaitable) {
205
+ // If we are using the streaming protocol then we immediately issue another 'started'
206
+ // event, otherwise the "check" is complete and return to the main monitor loop
207
+ monitor.emitAndLogHeartbeat(server_1.Server.SERVER_HEARTBEAT_STARTED, monitor.server.topology.s.id, undefined, new events_1.ServerHeartbeatStartedEvent(monitor.address, true));
208
+ // We have not actually sent an outgoing handshake, but when we get the next response we
209
+ // want the duration to reflect the time since we last heard from the server
210
+ start = (0, utils_1.processTimeMS)();
211
+ }
212
+ else {
213
+ monitor.rttPinger?.close();
214
+ monitor.rttPinger = undefined;
215
+ callback(undefined, hello);
216
+ }
217
+ }
218
+ const { connection } = monitor;
219
+ if (connection && !connection.closed) {
220
+ const { serverApi, helloOk } = connection;
221
+ const connectTimeoutMS = monitor.options.connectTimeoutMS;
222
+ const maxAwaitTimeMS = monitor.options.heartbeatFrequencyMS;
223
+ const cmd = {
224
+ [serverApi?.version || helloOk ? 'hello' : constants_1.LEGACY_HELLO_COMMAND]: 1,
225
+ ...(isAwaitable && topologyVersion
226
+ ? { maxAwaitTimeMS, topologyVersion: makeTopologyVersion(topologyVersion) }
227
+ : {})
228
+ };
229
+ const options = isAwaitable
230
+ ? {
231
+ socketTimeoutMS: connectTimeoutMS ? connectTimeoutMS + maxAwaitTimeMS : 0,
232
+ exhaustAllowed: true
233
+ }
234
+ : { socketTimeoutMS: connectTimeoutMS };
235
+ if (isAwaitable && monitor.rttPinger == null) {
236
+ monitor.rttPinger = new RTTPinger(monitor);
237
+ }
238
+ // Record new start time before sending handshake
239
+ start = (0, utils_1.processTimeMS)();
240
+ if (isAwaitable) {
241
+ awaited = true;
242
+ return connection.exhaustCommand((0, utils_1.ns)('admin.$cmd'), cmd, options, (error, hello) => {
243
+ if (error)
244
+ return onHeartbeatFailed(error);
245
+ return onHeartbeatSucceeded(hello);
246
+ });
247
+ }
248
+ awaited = false;
249
+ connection
250
+ .command((0, utils_1.ns)('admin.$cmd'), cmd, options)
251
+ .then(onHeartbeatSucceeded, onHeartbeatFailed);
252
+ return;
253
+ }
254
+ // connecting does an implicit `hello`
255
+ (async () => {
256
+ const socket = await (0, connect_1.makeSocket)(monitor.connectOptions);
257
+ const connection = (0, connect_1.makeConnection)(monitor.connectOptions, socket);
258
+ // The start time is after socket creation but before the handshake
259
+ start = (0, utils_1.processTimeMS)();
260
+ try {
261
+ await (0, connect_1.performInitialHandshake)(connection, monitor.connectOptions);
262
+ return connection;
263
+ }
264
+ catch (error) {
265
+ connection.destroy();
266
+ throw error;
267
+ }
268
+ })().then(connection => {
269
+ if (isInCloseState(monitor)) {
270
+ connection.destroy();
271
+ return;
272
+ }
273
+ const duration = (0, utils_1.calculateDurationInMs)(start);
274
+ monitor.addRttSample(duration);
275
+ monitor.connection = connection;
276
+ monitor.emitAndLogHeartbeat(server_1.Server.SERVER_HEARTBEAT_SUCCEEDED, monitor.server.topology.s.id, connection.hello?.connectionId, new events_1.ServerHeartbeatSucceededEvent(monitor.address, duration, connection.hello, useStreamingProtocol(monitor, connection.hello?.topologyVersion)));
277
+ callback(undefined, connection.hello);
278
+ }, error => {
279
+ monitor.connection = null;
280
+ awaited = false;
281
+ onHeartbeatFailed(error);
282
+ });
283
+ }
284
+ function monitorServer(monitor) {
285
+ return (callback) => {
286
+ if (monitor.s.state === STATE_MONITORING) {
287
+ queueMicrotask(callback);
288
+ return;
289
+ }
290
+ stateTransition(monitor, STATE_MONITORING);
291
+ function done() {
292
+ if (!isInCloseState(monitor)) {
293
+ stateTransition(monitor, STATE_IDLE);
294
+ }
295
+ callback();
296
+ }
297
+ checkServer(monitor, (err, hello) => {
298
+ if (err) {
299
+ // otherwise an error occurred on initial discovery, also bail
300
+ if (monitor.server.description.type === common_1.ServerType.Unknown) {
301
+ return done();
302
+ }
303
+ }
304
+ // if the check indicates streaming is supported, immediately reschedule monitoring
305
+ if (useStreamingProtocol(monitor, hello?.topologyVersion)) {
306
+ (0, timers_1.setTimeout)(() => {
307
+ if (!isInCloseState(monitor)) {
308
+ monitor.monitorId?.wake();
309
+ }
310
+ }, 0);
311
+ }
312
+ done();
313
+ });
314
+ };
315
+ }
316
+ function makeTopologyVersion(tv) {
317
+ return {
318
+ processId: tv.processId,
319
+ // tests mock counter as just number, but in a real situation counter should always be a Long
320
+ // TODO(NODE-2674): Preserve int64 sent from MongoDB
321
+ counter: bson_1.Long.isLong(tv.counter) ? tv.counter : bson_1.Long.fromNumber(tv.counter)
322
+ };
323
+ }
324
+ /** @internal */
325
+ class RTTPinger {
326
+ constructor(monitor) {
327
+ this.connection = undefined;
328
+ this.cancellationToken = monitor.cancellationToken;
329
+ this.closed = false;
330
+ this.monitor = monitor;
331
+ this.latestRtt = monitor.latestRtt ?? undefined;
332
+ const heartbeatFrequencyMS = monitor.options.heartbeatFrequencyMS;
333
+ this.monitorId = (0, timers_1.setTimeout)(() => this.measureRoundTripTime(), heartbeatFrequencyMS);
334
+ }
335
+ get roundTripTime() {
336
+ return this.monitor.roundTripTime;
337
+ }
338
+ get minRoundTripTime() {
339
+ return this.monitor.minRoundTripTime;
340
+ }
341
+ close() {
342
+ this.closed = true;
343
+ (0, timers_1.clearTimeout)(this.monitorId);
344
+ this.connection?.destroy();
345
+ this.connection = undefined;
346
+ }
347
+ measureAndReschedule(start, conn) {
348
+ if (this.closed) {
349
+ conn?.destroy();
350
+ return;
351
+ }
352
+ if (this.connection == null) {
353
+ this.connection = conn;
354
+ }
355
+ this.latestRtt = (0, utils_1.calculateDurationInMs)(start);
356
+ this.monitorId = (0, timers_1.setTimeout)(() => this.measureRoundTripTime(), this.monitor.options.heartbeatFrequencyMS);
357
+ }
358
+ measureRoundTripTime() {
359
+ const start = (0, utils_1.processTimeMS)();
360
+ if (this.closed) {
361
+ return;
362
+ }
363
+ const connection = this.connection;
364
+ if (connection == null) {
365
+ (0, connect_1.connect)(this.monitor.connectOptions).then(connection => {
366
+ this.measureAndReschedule(start, connection);
367
+ }, () => {
368
+ this.connection = undefined;
369
+ });
370
+ return;
371
+ }
372
+ const commandName = connection.serverApi?.version || connection.helloOk ? 'hello' : constants_1.LEGACY_HELLO_COMMAND;
373
+ connection.command((0, utils_1.ns)('admin.$cmd'), { [commandName]: 1 }, undefined).then(() => this.measureAndReschedule(start), () => {
374
+ this.connection?.destroy();
375
+ this.connection = undefined;
376
+ return;
377
+ });
378
+ }
379
+ }
380
+ exports.RTTPinger = RTTPinger;
381
+ /**
382
+ * @internal
383
+ */
384
+ class MonitorInterval {
385
+ constructor(fn, options = {}) {
386
+ this.isExpeditedCallToFnScheduled = false;
387
+ this.stopped = false;
388
+ this.isExecutionInProgress = false;
389
+ this.hasExecutedOnce = false;
390
+ this._executeAndReschedule = () => {
391
+ if (this.stopped)
392
+ return;
393
+ if (this.timerId) {
394
+ (0, timers_1.clearTimeout)(this.timerId);
395
+ }
396
+ this.isExpeditedCallToFnScheduled = false;
397
+ this.isExecutionInProgress = true;
398
+ this.fn(() => {
399
+ this.lastExecutionEnded = (0, utils_1.processTimeMS)();
400
+ this.isExecutionInProgress = false;
401
+ this._reschedule(this.heartbeatFrequencyMS);
402
+ });
403
+ };
404
+ this.fn = fn;
405
+ this.lastExecutionEnded = -Infinity;
406
+ this.heartbeatFrequencyMS = options.heartbeatFrequencyMS ?? 1000;
407
+ this.minHeartbeatFrequencyMS = options.minHeartbeatFrequencyMS ?? 500;
408
+ if (options.immediate) {
409
+ this._executeAndReschedule();
410
+ }
411
+ else {
412
+ this._reschedule(undefined);
413
+ }
414
+ }
415
+ wake() {
416
+ const currentTime = (0, utils_1.processTimeMS)();
417
+ const timeSinceLastCall = currentTime - this.lastExecutionEnded;
418
+ // TODO(NODE-4674): Add error handling and logging to the monitor
419
+ if (timeSinceLastCall < 0) {
420
+ return this._executeAndReschedule();
421
+ }
422
+ if (this.isExecutionInProgress) {
423
+ return;
424
+ }
425
+ // debounce multiple calls to wake within the `minInterval`
426
+ if (this.isExpeditedCallToFnScheduled) {
427
+ return;
428
+ }
429
+ // reschedule a call as soon as possible, ensuring the call never happens
430
+ // faster than the `minInterval`
431
+ if (timeSinceLastCall < this.minHeartbeatFrequencyMS) {
432
+ this.isExpeditedCallToFnScheduled = true;
433
+ this._reschedule(this.minHeartbeatFrequencyMS - timeSinceLastCall);
434
+ return;
435
+ }
436
+ this._executeAndReschedule();
437
+ }
438
+ stop() {
439
+ this.stopped = true;
440
+ if (this.timerId) {
441
+ (0, timers_1.clearTimeout)(this.timerId);
442
+ this.timerId = undefined;
443
+ }
444
+ this.lastExecutionEnded = -Infinity;
445
+ this.isExpeditedCallToFnScheduled = false;
446
+ }
447
+ toString() {
448
+ return JSON.stringify(this);
449
+ }
450
+ toJSON() {
451
+ const currentTime = (0, utils_1.processTimeMS)();
452
+ const timeSinceLastCall = currentTime - this.lastExecutionEnded;
453
+ return {
454
+ timerId: this.timerId != null ? 'set' : 'cleared',
455
+ lastCallTime: this.lastExecutionEnded,
456
+ isExpeditedCheckScheduled: this.isExpeditedCallToFnScheduled,
457
+ stopped: this.stopped,
458
+ heartbeatFrequencyMS: this.heartbeatFrequencyMS,
459
+ minHeartbeatFrequencyMS: this.minHeartbeatFrequencyMS,
460
+ currentTime,
461
+ timeSinceLastCall
462
+ };
463
+ }
464
+ _reschedule(ms) {
465
+ if (this.stopped)
466
+ return;
467
+ if (this.timerId) {
468
+ (0, timers_1.clearTimeout)(this.timerId);
469
+ }
470
+ this.timerId = (0, timers_1.setTimeout)(this._executeAndReschedule, ms || this.heartbeatFrequencyMS);
471
+ }
472
+ }
473
+ exports.MonitorInterval = MonitorInterval;
474
+ /** @internal
475
+ * This class implements the RTT sampling logic specified for [CSOT](https://github.com/mongodb/specifications/blob/bbb335e60cd7ea1e0f7cd9a9443cb95fc9d3b64d/source/client-side-operations-timeout/client-side-operations-timeout.md#drivers-use-minimum-rtt-to-short-circuit-operations)
476
+ *
477
+ * This is implemented as a [circular buffer](https://en.wikipedia.org/wiki/Circular_buffer) keeping
478
+ * the most recent `windowSize` samples
479
+ * */
480
+ class RTTSampler {
481
+ constructor(windowSize = 10) {
482
+ this.rttSamples = new Float64Array(windowSize);
483
+ this.length = 0;
484
+ this.writeIndex = 0;
485
+ }
486
+ /**
487
+ * Adds an rtt sample to the end of the circular buffer
488
+ * When `windowSize` samples have been collected, `addSample` overwrites the least recently added
489
+ * sample
490
+ */
491
+ addSample(sample) {
492
+ this.rttSamples[this.writeIndex++] = sample;
493
+ if (this.length < this.rttSamples.length) {
494
+ this.length++;
495
+ }
496
+ this.writeIndex %= this.rttSamples.length;
497
+ }
498
+ /**
499
+ * When \< 2 samples have been collected, returns 0
500
+ * Otherwise computes the minimum value samples contained in the buffer
501
+ */
502
+ min() {
503
+ if (this.length < 2)
504
+ return 0;
505
+ let min = this.rttSamples[0];
506
+ for (let i = 1; i < this.length; i++) {
507
+ if (this.rttSamples[i] < min)
508
+ min = this.rttSamples[i];
509
+ }
510
+ return min;
511
+ }
512
+ /**
513
+ * Returns mean of samples contained in the buffer
514
+ */
515
+ average() {
516
+ if (this.length === 0)
517
+ return 0;
518
+ let sum = 0;
519
+ for (let i = 0; i < this.length; i++) {
520
+ sum += this.rttSamples[i];
521
+ }
522
+ return sum / this.length;
523
+ }
524
+ /**
525
+ * Returns most recently inserted element in the buffer
526
+ * Returns null if the buffer is empty
527
+ * */
528
+ get last() {
529
+ if (this.length === 0)
530
+ return null;
531
+ return this.rttSamples[this.writeIndex === 0 ? this.length - 1 : this.writeIndex - 1];
532
+ }
533
+ /**
534
+ * Clear the buffer
535
+ * NOTE: this does not overwrite the data held in the internal array, just the pointers into
536
+ * this array
537
+ */
538
+ clear() {
539
+ this.length = 0;
540
+ this.writeIndex = 0;
541
+ }
542
+ }
543
+ exports.RTTSampler = RTTSampler;
544
+ //# sourceMappingURL=monitor.js.map