@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,788 @@
1
+ import type { BSONSerializeOptions, Document, Long } from '../bson';
2
+ import * as BSON from '../bson';
3
+ import { MongoInvalidArgumentError, MongoRuntimeError } from '../error';
4
+ import { type ReadPreference } from '../read_preference';
5
+ import type { ClientSession } from '../sessions';
6
+ import type { CommandOptions } from './connection';
7
+ import {
8
+ compress,
9
+ Compressor,
10
+ type CompressorName,
11
+ uncompressibleCommands
12
+ } from './wire_protocol/compression';
13
+ import { OP_COMPRESSED, OP_MSG, OP_QUERY } from './wire_protocol/constants';
14
+
15
+ // Incrementing request id
16
+ let _requestId = 0;
17
+
18
+ // Query flags
19
+ const OPTS_TAILABLE_CURSOR = 2;
20
+ const OPTS_SECONDARY = 4;
21
+ const OPTS_OPLOG_REPLAY = 8;
22
+ const OPTS_NO_CURSOR_TIMEOUT = 16;
23
+ const OPTS_AWAIT_DATA = 32;
24
+ const OPTS_EXHAUST = 64;
25
+ const OPTS_PARTIAL = 128;
26
+
27
+ // Response flags
28
+ const CURSOR_NOT_FOUND = 1;
29
+ const QUERY_FAILURE = 2;
30
+ const SHARD_CONFIG_STALE = 4;
31
+ const AWAIT_CAPABLE = 8;
32
+
33
+ const encodeUTF8Into = BSON.BSON.onDemand.ByteUtils.encodeUTF8Into;
34
+
35
+ /** @internal */
36
+ export type WriteProtocolMessageType = OpQueryRequest | OpMsgRequest;
37
+
38
+ /** @internal */
39
+ export interface OpQueryOptions extends CommandOptions {
40
+ socketTimeoutMS?: number;
41
+ session?: ClientSession;
42
+ numberToSkip?: number;
43
+ numberToReturn?: number;
44
+ returnFieldSelector?: Document;
45
+ pre32Limit?: number;
46
+ serializeFunctions?: boolean;
47
+ ignoreUndefined?: boolean;
48
+ maxBsonSize?: number;
49
+ checkKeys?: boolean;
50
+ secondaryOk?: boolean;
51
+
52
+ requestId?: number;
53
+ moreToCome?: boolean;
54
+ exhaustAllowed?: boolean;
55
+ }
56
+
57
+ /** @internal */
58
+ export class OpQueryRequest {
59
+ ns: string;
60
+ numberToSkip: number;
61
+ numberToReturn: number;
62
+ returnFieldSelector?: Document;
63
+ requestId: number;
64
+ pre32Limit?: number;
65
+ serializeFunctions: boolean;
66
+ ignoreUndefined: boolean;
67
+ maxBsonSize: number;
68
+ checkKeys: boolean;
69
+ batchSize: number;
70
+ tailable: boolean;
71
+ secondaryOk: boolean;
72
+ oplogReplay: boolean;
73
+ noCursorTimeout: boolean;
74
+ awaitData: boolean;
75
+ exhaust: boolean;
76
+ partial: boolean;
77
+ /** moreToCome is an OP_MSG only concept */
78
+ moreToCome = false;
79
+ databaseName: string;
80
+ query: Document;
81
+
82
+ constructor(databaseName: string, query: Document, options: OpQueryOptions) {
83
+ // Basic options needed to be passed in
84
+ // TODO(NODE-3483): Replace with MongoCommandError
85
+ const ns = `${databaseName}.$cmd`;
86
+ if (typeof databaseName !== 'string') {
87
+ throw new MongoRuntimeError('Database name must be a string for a query');
88
+ }
89
+ // TODO(NODE-3483): Replace with MongoCommandError
90
+ if (query == null) throw new MongoRuntimeError('A query document must be specified for query');
91
+
92
+ // Validate that we are not passing 0x00 in the collection name
93
+ if (ns.indexOf('\x00') !== -1) {
94
+ // TODO(NODE-3483): Use MongoNamespace static method
95
+ throw new MongoRuntimeError('Namespace cannot contain a null character');
96
+ }
97
+
98
+ // Basic optionsa
99
+ this.databaseName = databaseName;
100
+ this.query = query;
101
+ this.ns = ns;
102
+
103
+ // Additional options
104
+ this.numberToSkip = options.numberToSkip || 0;
105
+ this.numberToReturn = options.numberToReturn || 0;
106
+ this.returnFieldSelector = options.returnFieldSelector || undefined;
107
+ this.requestId = options.requestId ?? OpQueryRequest.getRequestId();
108
+
109
+ // special case for pre-3.2 find commands, delete ASAP
110
+ this.pre32Limit = options.pre32Limit;
111
+
112
+ // Serialization option
113
+ this.serializeFunctions =
114
+ typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
115
+ this.ignoreUndefined =
116
+ typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : false;
117
+ this.maxBsonSize = options.maxBsonSize || 1024 * 1024 * 16;
118
+ this.checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
119
+ this.batchSize = this.numberToReturn;
120
+
121
+ // Flags
122
+ this.tailable = false;
123
+ this.secondaryOk = typeof options.secondaryOk === 'boolean' ? options.secondaryOk : false;
124
+ this.oplogReplay = false;
125
+ this.noCursorTimeout = false;
126
+ this.awaitData = false;
127
+ this.exhaust = false;
128
+ this.partial = false;
129
+ }
130
+
131
+ /** Assign next request Id. */
132
+ incRequestId(): void {
133
+ this.requestId = _requestId++;
134
+ }
135
+
136
+ /** Peek next request Id. */
137
+ nextRequestId(): number {
138
+ return _requestId + 1;
139
+ }
140
+
141
+ /** Increment then return next request Id. */
142
+ static getRequestId(): number {
143
+ return ++_requestId;
144
+ }
145
+
146
+ // Uses a single allocated buffer for the process, avoiding multiple memory allocations
147
+ toBin(): Uint8Array[] {
148
+ const buffers = [];
149
+ let projection = null;
150
+
151
+ // Set up the flags
152
+ let flags = 0;
153
+ if (this.tailable) {
154
+ flags |= OPTS_TAILABLE_CURSOR;
155
+ }
156
+
157
+ if (this.secondaryOk) {
158
+ flags |= OPTS_SECONDARY;
159
+ }
160
+
161
+ if (this.oplogReplay) {
162
+ flags |= OPTS_OPLOG_REPLAY;
163
+ }
164
+
165
+ if (this.noCursorTimeout) {
166
+ flags |= OPTS_NO_CURSOR_TIMEOUT;
167
+ }
168
+
169
+ if (this.awaitData) {
170
+ flags |= OPTS_AWAIT_DATA;
171
+ }
172
+
173
+ if (this.exhaust) {
174
+ flags |= OPTS_EXHAUST;
175
+ }
176
+
177
+ if (this.partial) {
178
+ flags |= OPTS_PARTIAL;
179
+ }
180
+
181
+ // If batchSize is different to this.numberToReturn
182
+ if (this.batchSize !== this.numberToReturn) this.numberToReturn = this.batchSize;
183
+
184
+ // Allocate write protocol header buffer
185
+ const header = Buffer.alloc(
186
+ 4 * 4 + // Header
187
+ 4 + // Flags
188
+ Buffer.byteLength(this.ns) +
189
+ 1 + // namespace
190
+ 4 + // numberToSkip
191
+ 4 // numberToReturn
192
+ );
193
+
194
+ // Add header to buffers
195
+ buffers.push(header);
196
+
197
+ // Serialize the query
198
+ const query = BSON.serialize(this.query, {
199
+ checkKeys: this.checkKeys,
200
+ serializeFunctions: this.serializeFunctions,
201
+ ignoreUndefined: this.ignoreUndefined
202
+ });
203
+
204
+ // Add query document
205
+ buffers.push(query);
206
+
207
+ if (this.returnFieldSelector && Object.keys(this.returnFieldSelector).length > 0) {
208
+ // Serialize the projection document
209
+ projection = BSON.serialize(this.returnFieldSelector, {
210
+ checkKeys: this.checkKeys,
211
+ serializeFunctions: this.serializeFunctions,
212
+ ignoreUndefined: this.ignoreUndefined
213
+ });
214
+ // Add projection document
215
+ buffers.push(projection);
216
+ }
217
+
218
+ // Total message size
219
+ const totalLength = header.length + query.length + (projection ? projection.length : 0);
220
+
221
+ // Set up the index
222
+ let index = 4;
223
+
224
+ // Write total document length
225
+ header[3] = (totalLength >> 24) & 0xff;
226
+ header[2] = (totalLength >> 16) & 0xff;
227
+ header[1] = (totalLength >> 8) & 0xff;
228
+ header[0] = totalLength & 0xff;
229
+
230
+ // Write header information requestId
231
+ header[index + 3] = (this.requestId >> 24) & 0xff;
232
+ header[index + 2] = (this.requestId >> 16) & 0xff;
233
+ header[index + 1] = (this.requestId >> 8) & 0xff;
234
+ header[index] = this.requestId & 0xff;
235
+ index = index + 4;
236
+
237
+ // Write header information responseTo
238
+ header[index + 3] = (0 >> 24) & 0xff;
239
+ header[index + 2] = (0 >> 16) & 0xff;
240
+ header[index + 1] = (0 >> 8) & 0xff;
241
+ header[index] = 0 & 0xff;
242
+ index = index + 4;
243
+
244
+ // Write header information OP_QUERY
245
+ header[index + 3] = (OP_QUERY >> 24) & 0xff;
246
+ header[index + 2] = (OP_QUERY >> 16) & 0xff;
247
+ header[index + 1] = (OP_QUERY >> 8) & 0xff;
248
+ header[index] = OP_QUERY & 0xff;
249
+ index = index + 4;
250
+
251
+ // Write header information flags
252
+ header[index + 3] = (flags >> 24) & 0xff;
253
+ header[index + 2] = (flags >> 16) & 0xff;
254
+ header[index + 1] = (flags >> 8) & 0xff;
255
+ header[index] = flags & 0xff;
256
+ index = index + 4;
257
+
258
+ // Write collection name
259
+ index = index + header.write(this.ns, index, 'utf8') + 1;
260
+ header[index - 1] = 0;
261
+
262
+ // Write header information flags numberToSkip
263
+ header[index + 3] = (this.numberToSkip >> 24) & 0xff;
264
+ header[index + 2] = (this.numberToSkip >> 16) & 0xff;
265
+ header[index + 1] = (this.numberToSkip >> 8) & 0xff;
266
+ header[index] = this.numberToSkip & 0xff;
267
+ index = index + 4;
268
+
269
+ // Write header information flags numberToReturn
270
+ header[index + 3] = (this.numberToReturn >> 24) & 0xff;
271
+ header[index + 2] = (this.numberToReturn >> 16) & 0xff;
272
+ header[index + 1] = (this.numberToReturn >> 8) & 0xff;
273
+ header[index] = this.numberToReturn & 0xff;
274
+ index = index + 4;
275
+
276
+ // Return the buffers
277
+ return buffers;
278
+ }
279
+ }
280
+
281
+ /** @internal */
282
+ export interface MessageHeader {
283
+ length: number;
284
+ requestId: number;
285
+ responseTo: number;
286
+ opCode: number;
287
+ fromCompressed?: boolean;
288
+ }
289
+
290
+ /** @internal */
291
+ export class OpReply {
292
+ parsed: boolean;
293
+ raw: Buffer;
294
+ data: Buffer;
295
+ opts: BSONSerializeOptions;
296
+ length: number;
297
+ requestId: number;
298
+ responseTo: number;
299
+ opCode: number;
300
+ fromCompressed?: boolean;
301
+ responseFlags?: number;
302
+ cursorId?: Long;
303
+ startingFrom?: number;
304
+ numberReturned?: number;
305
+ cursorNotFound?: boolean;
306
+ queryFailure?: boolean;
307
+ shardConfigStale?: boolean;
308
+ awaitCapable?: boolean;
309
+ useBigInt64: boolean;
310
+ promoteLongs: boolean;
311
+ promoteValues: boolean;
312
+ promoteBuffers: boolean;
313
+ bsonRegExp?: boolean;
314
+ index = 0;
315
+ sections: Uint8Array[] = [];
316
+
317
+ /** moreToCome is an OP_MSG only concept */
318
+ moreToCome = false;
319
+
320
+ constructor(
321
+ message: Buffer,
322
+ msgHeader: MessageHeader,
323
+ msgBody: Buffer,
324
+ opts?: BSONSerializeOptions
325
+ ) {
326
+ this.parsed = false;
327
+ this.raw = message;
328
+ this.data = msgBody;
329
+ this.opts = opts ?? {
330
+ useBigInt64: false,
331
+ promoteLongs: true,
332
+ promoteValues: true,
333
+ promoteBuffers: false,
334
+ bsonRegExp: false
335
+ };
336
+
337
+ // Read the message header
338
+ this.length = msgHeader.length;
339
+ this.requestId = msgHeader.requestId;
340
+ this.responseTo = msgHeader.responseTo;
341
+ this.opCode = msgHeader.opCode;
342
+ this.fromCompressed = msgHeader.fromCompressed;
343
+
344
+ // Flag values
345
+ this.useBigInt64 = typeof this.opts.useBigInt64 === 'boolean' ? this.opts.useBigInt64 : false;
346
+ this.promoteLongs = typeof this.opts.promoteLongs === 'boolean' ? this.opts.promoteLongs : true;
347
+ this.promoteValues =
348
+ typeof this.opts.promoteValues === 'boolean' ? this.opts.promoteValues : true;
349
+ this.promoteBuffers =
350
+ typeof this.opts.promoteBuffers === 'boolean' ? this.opts.promoteBuffers : false;
351
+ this.bsonRegExp = typeof this.opts.bsonRegExp === 'boolean' ? this.opts.bsonRegExp : false;
352
+ }
353
+
354
+ isParsed(): boolean {
355
+ return this.parsed;
356
+ }
357
+
358
+ parse(): Uint8Array {
359
+ // Don't parse again if not needed
360
+ if (this.parsed) return this.sections[0];
361
+
362
+ // Position within OP_REPLY at which documents start
363
+ // (See https://www.mongodb.com/docs/manual/reference/mongodb-wire-protocol/#wire-op-reply)
364
+ this.index = 20;
365
+
366
+ // Read the message body
367
+ this.responseFlags = this.data.readInt32LE(0);
368
+ this.cursorId = new BSON.Long(this.data.readInt32LE(4), this.data.readInt32LE(8));
369
+ this.startingFrom = this.data.readInt32LE(12);
370
+ this.numberReturned = this.data.readInt32LE(16);
371
+
372
+ if (this.numberReturned < 0 || this.numberReturned > 2 ** 32 - 1) {
373
+ throw new RangeError(
374
+ `OP_REPLY numberReturned is an invalid array length ${this.numberReturned}`
375
+ );
376
+ }
377
+
378
+ this.cursorNotFound = (this.responseFlags & CURSOR_NOT_FOUND) !== 0;
379
+ this.queryFailure = (this.responseFlags & QUERY_FAILURE) !== 0;
380
+ this.shardConfigStale = (this.responseFlags & SHARD_CONFIG_STALE) !== 0;
381
+ this.awaitCapable = (this.responseFlags & AWAIT_CAPABLE) !== 0;
382
+
383
+ // Parse Body
384
+ for (let i = 0; i < this.numberReturned; i++) {
385
+ const bsonSize =
386
+ this.data[this.index] |
387
+ (this.data[this.index + 1] << 8) |
388
+ (this.data[this.index + 2] << 16) |
389
+ (this.data[this.index + 3] << 24);
390
+
391
+ const section = this.data.subarray(this.index, this.index + bsonSize);
392
+ this.sections.push(section);
393
+
394
+ // Adjust the index
395
+ this.index = this.index + bsonSize;
396
+ }
397
+
398
+ // Set parsed
399
+ this.parsed = true;
400
+
401
+ return this.sections[0];
402
+ }
403
+ }
404
+
405
+ // Msg Flags
406
+ const OPTS_CHECKSUM_PRESENT = 1;
407
+ const OPTS_MORE_TO_COME = 2;
408
+ const OPTS_EXHAUST_ALLOWED = 1 << 16;
409
+
410
+ /** @internal */
411
+ export interface OpMsgOptions {
412
+ socketTimeoutMS?: number;
413
+ session?: ClientSession;
414
+ numberToSkip?: number;
415
+ numberToReturn?: number;
416
+ returnFieldSelector?: Document;
417
+ pre32Limit?: number;
418
+ serializeFunctions?: boolean;
419
+ ignoreUndefined?: boolean;
420
+ maxBsonSize?: number;
421
+ checkKeys?: boolean;
422
+ secondaryOk?: boolean;
423
+
424
+ requestId?: number;
425
+ moreToCome?: boolean;
426
+ exhaustAllowed?: boolean;
427
+ readPreference: ReadPreference;
428
+ }
429
+
430
+ /** @internal */
431
+ export class DocumentSequence {
432
+ field: string;
433
+ documents: Document[];
434
+ serializedDocumentsLength: number;
435
+ private chunks: Uint8Array[];
436
+ private header: Buffer;
437
+
438
+ /**
439
+ * Create a new document sequence for the provided field.
440
+ * @param field - The field it will replace.
441
+ */
442
+ constructor(field: string, documents?: Document[]) {
443
+ this.field = field;
444
+ this.documents = [];
445
+ this.chunks = [];
446
+ this.serializedDocumentsLength = 0;
447
+ // Document sequences starts with type 1 at the first byte.
448
+ // Field strings must always be UTF-8.
449
+ const buffer = Buffer.allocUnsafe(1 + 4 + this.field.length + 1);
450
+ buffer[0] = 1;
451
+ // Third part is the field name at offset 5 with trailing null byte.
452
+ encodeUTF8Into(buffer, `${this.field}\0`, 5);
453
+ this.chunks.push(buffer);
454
+ this.header = buffer;
455
+ if (documents) {
456
+ for (const doc of documents) {
457
+ this.push(doc, BSON.serialize(doc));
458
+ }
459
+ }
460
+ }
461
+
462
+ /**
463
+ * Push a document to the document sequence. Will serialize the document
464
+ * as well and return the current serialized length of all documents.
465
+ * @param document - The document to add.
466
+ * @param buffer - The serialized document in raw BSON.
467
+ * @returns The new total document sequence length.
468
+ */
469
+ push(document: Document, buffer: Uint8Array): number {
470
+ this.serializedDocumentsLength += buffer.length;
471
+ // Push the document.
472
+ this.documents.push(document);
473
+ // Push the document raw bson.
474
+ this.chunks.push(buffer);
475
+ // Write the new length.
476
+ this.header?.writeInt32LE(4 + this.field.length + 1 + this.serializedDocumentsLength, 1);
477
+ return this.serializedDocumentsLength + this.header.length;
478
+ }
479
+
480
+ /**
481
+ * Get the fully serialized bytes for the document sequence section.
482
+ * @returns The section bytes.
483
+ */
484
+ toBin(): Uint8Array {
485
+ return Buffer.concat(this.chunks);
486
+ }
487
+ }
488
+
489
+ /** @internal */
490
+ export class OpMsgRequest {
491
+ requestId: number;
492
+ serializeFunctions: boolean;
493
+ ignoreUndefined: boolean;
494
+ checkKeys: boolean;
495
+ maxBsonSize: number;
496
+ checksumPresent: boolean;
497
+ moreToCome: boolean;
498
+ exhaustAllowed: boolean;
499
+ databaseName: string;
500
+ command: Document;
501
+ options: OpQueryOptions;
502
+
503
+ constructor(databaseName: string, command: Document, options: OpQueryOptions) {
504
+ // Basic options needed to be passed in
505
+ if (command == null)
506
+ throw new MongoInvalidArgumentError('Query document must be specified for query');
507
+
508
+ // Basic optionsa
509
+ this.databaseName = databaseName;
510
+ this.command = command;
511
+ this.command.$db = databaseName;
512
+
513
+ // Ensure empty options
514
+ this.options = options ?? {};
515
+
516
+ // Additional options
517
+ this.requestId = options.requestId ? options.requestId : OpMsgRequest.getRequestId();
518
+
519
+ // Serialization option
520
+ this.serializeFunctions =
521
+ typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
522
+ this.ignoreUndefined =
523
+ typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : false;
524
+ this.checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
525
+ this.maxBsonSize = options.maxBsonSize || 1024 * 1024 * 16;
526
+
527
+ // flags
528
+ this.checksumPresent = false;
529
+ this.moreToCome = options.moreToCome ?? command.writeConcern?.w === 0;
530
+ this.exhaustAllowed =
531
+ typeof options.exhaustAllowed === 'boolean' ? options.exhaustAllowed : false;
532
+ }
533
+
534
+ toBin(): Buffer[] {
535
+ const buffers: Buffer[] = [];
536
+ let flags = 0;
537
+
538
+ if (this.checksumPresent) {
539
+ flags |= OPTS_CHECKSUM_PRESENT;
540
+ }
541
+
542
+ if (this.moreToCome) {
543
+ flags |= OPTS_MORE_TO_COME;
544
+ }
545
+
546
+ if (this.exhaustAllowed) {
547
+ flags |= OPTS_EXHAUST_ALLOWED;
548
+ }
549
+
550
+ const header = Buffer.alloc(
551
+ 4 * 4 + // Header
552
+ 4 // Flags
553
+ );
554
+
555
+ buffers.push(header);
556
+
557
+ let totalLength = header.length;
558
+ const command = this.command;
559
+ totalLength += this.makeSections(buffers, command);
560
+
561
+ header.writeInt32LE(totalLength, 0); // messageLength
562
+ header.writeInt32LE(this.requestId, 4); // requestID
563
+ header.writeInt32LE(0, 8); // responseTo
564
+ header.writeInt32LE(OP_MSG, 12); // opCode
565
+ header.writeUInt32LE(flags, 16); // flags
566
+ return buffers;
567
+ }
568
+
569
+ /**
570
+ * Add the sections to the OP_MSG request's buffers and returns the length.
571
+ */
572
+ makeSections(buffers: Uint8Array[], document: Document): number {
573
+ const sequencesBuffer = this.extractDocumentSequences(document);
574
+ const payloadTypeBuffer = Buffer.allocUnsafe(1);
575
+ payloadTypeBuffer[0] = 0;
576
+
577
+ const documentBuffer = this.serializeBson(document);
578
+ // First section, type 0
579
+ buffers.push(payloadTypeBuffer);
580
+ buffers.push(documentBuffer);
581
+ // Subsequent sections, type 1
582
+ buffers.push(sequencesBuffer);
583
+
584
+ return payloadTypeBuffer.length + documentBuffer.length + sequencesBuffer.length;
585
+ }
586
+
587
+ /**
588
+ * Extracts the document sequences from the command document and returns
589
+ * a buffer to be added as multiple sections after the initial type 0
590
+ * section in the message.
591
+ */
592
+ extractDocumentSequences(document: Document): Uint8Array {
593
+ // Pull out any field in the command document that's value is a document sequence.
594
+ const chunks = [];
595
+ for (const [key, value] of Object.entries(document)) {
596
+ if (value instanceof DocumentSequence) {
597
+ chunks.push(value.toBin());
598
+ // Why are we removing the field from the command? This is because it needs to be
599
+ // removed in the OP_MSG request first section, and DocumentSequence is not a
600
+ // BSON type and is specific to the MongoDB wire protocol so there's nothing
601
+ // our BSON serializer can do about this. Since DocumentSequence is not exposed
602
+ // in the public API and only used internally, we are never mutating an original
603
+ // command provided by the user, just our own, and it's cheaper to delete from
604
+ // our own command than copying it.
605
+ delete document[key];
606
+ }
607
+ }
608
+ if (chunks.length > 0) {
609
+ return Buffer.concat(chunks);
610
+ }
611
+ // If we have no document sequences we return an empty buffer for nothing to add
612
+ // to the payload.
613
+ return Buffer.alloc(0);
614
+ }
615
+
616
+ serializeBson(document: Document): Uint8Array {
617
+ return BSON.serialize(document, {
618
+ checkKeys: this.checkKeys,
619
+ serializeFunctions: this.serializeFunctions,
620
+ ignoreUndefined: this.ignoreUndefined
621
+ });
622
+ }
623
+
624
+ static getRequestId(): number {
625
+ _requestId = (_requestId + 1) & 0x7fffffff;
626
+ return _requestId;
627
+ }
628
+ }
629
+
630
+ /** @internal */
631
+ export class OpMsgResponse {
632
+ parsed: boolean;
633
+ raw: Buffer;
634
+ data: Buffer;
635
+ opts: BSONSerializeOptions;
636
+ length: number;
637
+ requestId: number;
638
+ responseTo: number;
639
+ opCode: number;
640
+ fromCompressed?: boolean;
641
+ responseFlags: number;
642
+ checksumPresent: boolean;
643
+ /** Indicates the server will be sending more responses on this connection */
644
+ moreToCome: boolean;
645
+ exhaustAllowed: boolean;
646
+ useBigInt64: boolean;
647
+ promoteLongs: boolean;
648
+ promoteValues: boolean;
649
+ promoteBuffers: boolean;
650
+ bsonRegExp: boolean;
651
+ index = 0;
652
+ sections: Uint8Array[] = [];
653
+
654
+ constructor(
655
+ message: Buffer,
656
+ msgHeader: MessageHeader,
657
+ msgBody: Buffer,
658
+ opts?: BSONSerializeOptions
659
+ ) {
660
+ this.parsed = false;
661
+ this.raw = message;
662
+ this.data = msgBody;
663
+ this.opts = opts ?? {
664
+ useBigInt64: false,
665
+ promoteLongs: true,
666
+ promoteValues: true,
667
+ promoteBuffers: false,
668
+ bsonRegExp: false
669
+ };
670
+
671
+ // Read the message header
672
+ this.length = msgHeader.length;
673
+ this.requestId = msgHeader.requestId;
674
+ this.responseTo = msgHeader.responseTo;
675
+ this.opCode = msgHeader.opCode;
676
+ this.fromCompressed = msgHeader.fromCompressed;
677
+
678
+ // Read response flags
679
+ this.responseFlags = msgBody.readInt32LE(0);
680
+ this.checksumPresent = (this.responseFlags & OPTS_CHECKSUM_PRESENT) !== 0;
681
+ this.moreToCome = (this.responseFlags & OPTS_MORE_TO_COME) !== 0;
682
+ this.exhaustAllowed = (this.responseFlags & OPTS_EXHAUST_ALLOWED) !== 0;
683
+ this.useBigInt64 = typeof this.opts.useBigInt64 === 'boolean' ? this.opts.useBigInt64 : false;
684
+ this.promoteLongs = typeof this.opts.promoteLongs === 'boolean' ? this.opts.promoteLongs : true;
685
+ this.promoteValues =
686
+ typeof this.opts.promoteValues === 'boolean' ? this.opts.promoteValues : true;
687
+ this.promoteBuffers =
688
+ typeof this.opts.promoteBuffers === 'boolean' ? this.opts.promoteBuffers : false;
689
+ this.bsonRegExp = typeof this.opts.bsonRegExp === 'boolean' ? this.opts.bsonRegExp : false;
690
+ }
691
+
692
+ isParsed(): boolean {
693
+ return this.parsed;
694
+ }
695
+
696
+ parse(): Uint8Array {
697
+ // Don't parse again if not needed
698
+ if (this.parsed) return this.sections[0];
699
+
700
+ this.index = 4;
701
+
702
+ while (this.index < this.data.length) {
703
+ const payloadType = this.data.readUInt8(this.index++);
704
+ if (payloadType === 0) {
705
+ const bsonSize = this.data.readUInt32LE(this.index);
706
+ const bin = this.data.subarray(this.index, this.index + bsonSize);
707
+
708
+ this.sections.push(bin);
709
+
710
+ this.index += bsonSize;
711
+ } else if (payloadType === 1) {
712
+ // It was decided that no driver makes use of payload type 1
713
+
714
+ // TODO(NODE-3483): Replace with MongoDeprecationError
715
+ throw new MongoRuntimeError('OP_MSG Payload Type 1 detected unsupported protocol');
716
+ }
717
+ }
718
+
719
+ this.parsed = true;
720
+
721
+ return this.sections[0];
722
+ }
723
+ }
724
+
725
+ const MESSAGE_HEADER_SIZE = 16;
726
+ const COMPRESSION_DETAILS_SIZE = 9; // originalOpcode + uncompressedSize, compressorID
727
+
728
+ /**
729
+ * @internal
730
+ */
731
+ export interface OpCompressesRequestOptions {
732
+ zlibCompressionLevel: number;
733
+ agreedCompressor: CompressorName;
734
+ }
735
+
736
+ /**
737
+ * @internal
738
+ *
739
+ * An OP_COMPRESSED request wraps either an OP_QUERY or OP_MSG message.
740
+ */
741
+ export class OpCompressedRequest {
742
+ private command: WriteProtocolMessageType;
743
+ private options: OpCompressesRequestOptions;
744
+
745
+ constructor(command: WriteProtocolMessageType, options: OpCompressesRequestOptions) {
746
+ this.command = command;
747
+ this.options = {
748
+ zlibCompressionLevel: options.zlibCompressionLevel,
749
+ agreedCompressor: options.agreedCompressor
750
+ };
751
+ }
752
+
753
+ // Return whether a command contains an uncompressible command term
754
+ // Will return true if command contains no uncompressible command terms
755
+ static canCompress(command: WriteProtocolMessageType) {
756
+ const commandDoc = command instanceof OpMsgRequest ? command.command : command.query;
757
+ const commandName = Object.keys(commandDoc)[0];
758
+ return !uncompressibleCommands.has(commandName);
759
+ }
760
+
761
+ async toBin(): Promise<Buffer[]> {
762
+ const concatenatedOriginalCommandBuffer = Buffer.concat(this.command.toBin());
763
+ // otherwise, compress the message
764
+ const messageToBeCompressed = concatenatedOriginalCommandBuffer.slice(MESSAGE_HEADER_SIZE);
765
+
766
+ // Extract information needed for OP_COMPRESSED from the uncompressed message
767
+ const originalCommandOpCode = concatenatedOriginalCommandBuffer.readInt32LE(12);
768
+
769
+ // Compress the message body
770
+ const compressedMessage = await compress(this.options, messageToBeCompressed);
771
+ // Create the msgHeader of OP_COMPRESSED
772
+ const msgHeader = Buffer.alloc(MESSAGE_HEADER_SIZE);
773
+ msgHeader.writeInt32LE(
774
+ MESSAGE_HEADER_SIZE + COMPRESSION_DETAILS_SIZE + compressedMessage.length,
775
+ 0
776
+ ); // messageLength
777
+ msgHeader.writeInt32LE(this.command.requestId, 4); // requestID
778
+ msgHeader.writeInt32LE(0, 8); // responseTo (zero)
779
+ msgHeader.writeInt32LE(OP_COMPRESSED, 12); // opCode
780
+
781
+ // Create the compression details of OP_COMPRESSED
782
+ const compressionDetails = Buffer.alloc(COMPRESSION_DETAILS_SIZE);
783
+ compressionDetails.writeInt32LE(originalCommandOpCode, 0); // originalOpcode
784
+ compressionDetails.writeInt32LE(messageToBeCompressed.length, 4); // Size of the uncompressed compressedMessage, excluding the MsgHeader
785
+ compressionDetails.writeUInt8(Compressor[this.options.agreedCompressor], 8); // compressorID
786
+ return [msgHeader, compressionDetails, compressedMessage];
787
+ }
788
+ }