mongodb 7.5.0 → 7.6.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 (107) hide show
  1. package/README.md +9 -1
  2. package/lib/bulk/common.js +27 -5
  3. package/lib/bulk/common.js.map +1 -1
  4. package/lib/bulk/ordered.js +25 -7
  5. package/lib/bulk/ordered.js.map +1 -1
  6. package/lib/bulk/unordered.js +25 -7
  7. package/lib/bulk/unordered.js.map +1 -1
  8. package/lib/client-side-encryption/auto_encrypter.js +8 -2
  9. package/lib/client-side-encryption/auto_encrypter.js.map +1 -1
  10. package/lib/client-side-encryption/client_encryption.js +12 -4
  11. package/lib/client-side-encryption/client_encryption.js.map +1 -1
  12. package/lib/client-side-encryption/kms_options.js +3 -0
  13. package/lib/client-side-encryption/kms_options.js.map +1 -0
  14. package/lib/client-side-encryption/state_machine.js +54 -7
  15. package/lib/client-side-encryption/state_machine.js.map +1 -1
  16. package/lib/cmap/auth/gssapi.js +2 -1
  17. package/lib/cmap/auth/gssapi.js.map +1 -1
  18. package/lib/cmap/auth/mongo_credentials.js +0 -1
  19. package/lib/cmap/auth/mongo_credentials.js.map +1 -1
  20. package/lib/cmap/auth/mongodb_aws.js +0 -3
  21. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  22. package/lib/cmap/command_monitoring_events.js +6 -7
  23. package/lib/cmap/command_monitoring_events.js.map +1 -1
  24. package/lib/cmap/commands.js +13 -0
  25. package/lib/cmap/commands.js.map +1 -1
  26. package/lib/cmap/connect.js +2 -2
  27. package/lib/cmap/connect.js.map +1 -1
  28. package/lib/cmap/connection.js +3 -1
  29. package/lib/cmap/connection.js.map +1 -1
  30. package/lib/cmap/handshake/client_metadata.js +2 -1
  31. package/lib/cmap/handshake/client_metadata.js.map +1 -1
  32. package/lib/cmap/wire_protocol/constants.js +2 -2
  33. package/lib/connection_string.js +4 -0
  34. package/lib/connection_string.js.map +1 -1
  35. package/lib/cursor/abstract_cursor.js.map +1 -1
  36. package/lib/error.js +7 -11
  37. package/lib/error.js.map +1 -1
  38. package/lib/gridfs/download.js +1 -1
  39. package/lib/gridfs/download.js.map +1 -1
  40. package/lib/index.js.map +1 -1
  41. package/lib/operations/command.js.map +1 -1
  42. package/lib/operations/delete.js +7 -10
  43. package/lib/operations/delete.js.map +1 -1
  44. package/lib/operations/execute_operation.js +29 -2
  45. package/lib/operations/execute_operation.js.map +1 -1
  46. package/lib/operations/find_and_modify.js +1 -5
  47. package/lib/operations/find_and_modify.js.map +1 -1
  48. package/lib/operations/get_more.js +2 -3
  49. package/lib/operations/get_more.js.map +1 -1
  50. package/lib/operations/indexes.js +3 -9
  51. package/lib/operations/indexes.js.map +1 -1
  52. package/lib/operations/insert.js +6 -2
  53. package/lib/operations/insert.js.map +1 -1
  54. package/lib/operations/list_collections.js +2 -3
  55. package/lib/operations/list_collections.js.map +1 -1
  56. package/lib/operations/list_databases.js +2 -2
  57. package/lib/operations/list_databases.js.map +1 -1
  58. package/lib/operations/update.js +6 -2
  59. package/lib/operations/update.js.map +1 -1
  60. package/lib/runtime_adapters.js +15 -16
  61. package/lib/runtime_adapters.js.map +1 -1
  62. package/lib/sdam/server.js +1 -1
  63. package/lib/sdam/server.js.map +1 -1
  64. package/lib/sessions.js +2 -2
  65. package/lib/sessions.js.map +1 -1
  66. package/lib/utils.js +1 -3
  67. package/lib/utils.js.map +1 -1
  68. package/mongodb.d.ts +77 -20
  69. package/package.json +3 -2
  70. package/src/bulk/common.ts +37 -5
  71. package/src/bulk/ordered.ts +23 -7
  72. package/src/bulk/unordered.ts +23 -8
  73. package/src/client-side-encryption/auto_encrypter.ts +19 -3
  74. package/src/client-side-encryption/client_encryption.ts +29 -9
  75. package/src/client-side-encryption/kms_options.ts +93 -0
  76. package/src/client-side-encryption/state_machine.ts +68 -47
  77. package/src/cmap/auth/gssapi.ts +3 -4
  78. package/src/cmap/auth/mongo_credentials.ts +0 -1
  79. package/src/cmap/auth/mongodb_aws.ts +2 -12
  80. package/src/cmap/command_monitoring_events.ts +9 -10
  81. package/src/cmap/commands.ts +17 -0
  82. package/src/cmap/connect.ts +3 -3
  83. package/src/cmap/connection.ts +4 -2
  84. package/src/cmap/handshake/client_metadata.ts +2 -1
  85. package/src/cmap/wire_protocol/constants.ts +2 -2
  86. package/src/connection_string.ts +4 -0
  87. package/src/cursor/abstract_cursor.ts +0 -5
  88. package/src/error.ts +7 -19
  89. package/src/gridfs/download.ts +1 -1
  90. package/src/index.ts +7 -6
  91. package/src/mongo_client.ts +1 -1
  92. package/src/operations/command.ts +0 -5
  93. package/src/operations/delete.ts +17 -15
  94. package/src/operations/execute_operation.ts +29 -2
  95. package/src/operations/find_and_modify.ts +3 -15
  96. package/src/operations/get_more.ts +3 -5
  97. package/src/operations/indexes.ts +6 -16
  98. package/src/operations/insert.ts +13 -2
  99. package/src/operations/list_collections.ts +5 -6
  100. package/src/operations/list_databases.ts +3 -3
  101. package/src/operations/update.ts +9 -2
  102. package/src/operations/validate_collection.ts +1 -1
  103. package/src/runtime_adapters.ts +19 -18
  104. package/src/sdam/server.ts +1 -1
  105. package/src/sessions.ts +2 -5
  106. package/src/utils.ts +1 -3
  107. package/tsconfig.json +9 -2
@@ -1,8 +1,9 @@
1
1
  /* eslint-disable no-restricted-imports*/
2
2
 
3
- // We squash the restricted import errors here because we are using type-only imports, which
4
- // do not impact the driver's actual runtime dependencies.
5
- // We also allow restricted imports in this file, because we expect this file to be the only place actually importing restricted Node APIs.
3
+ // We squash the restricted import errors here: the module-scope imports are type-only, and the
4
+ // one runtime dependency this file takes — the dynamic `import('os')` fallback in
5
+ // resolveRuntimeAdapters is deliberate: this file is expected to be the only place that loads
6
+ // restricted Node APIs at runtime.
6
7
 
7
8
  import type * as os from 'os';
8
9
 
@@ -11,8 +12,9 @@ import { type MongoClientOptions } from './mongo_client';
11
12
  /**
12
13
  * @internal
13
14
  *
14
- * This propery can be set on the global object to allow the driver to require otherwise blocked modules.
15
- * This is used by our test suite to allow tests to access the `os` module without allowing user code to do so.
15
+ * Legacy escape hatch for the test sandbox's restricted `require`: the driver no longer sets this
16
+ * property (the os adapter loads via dynamic `import()`), but the vm test harness
17
+ * still checks it. Kept until the sandbox contract is revisited in a follow-up.
16
18
  */
17
19
  export const ALLOWED_DRIVER_REQUIRE_PROPERTY_NAME = 'allowedDriverRequire';
18
20
 
@@ -47,18 +49,17 @@ export interface Runtime {
47
49
  /**
48
50
  * @internal
49
51
  *
50
- * Given a MongoClientOptions, this function resolves the set of runtime options, providing Nodejs implementations if
51
- * not provided by in `options`, and returns a `Runtime`.
52
+ * Given a MongoClientOptions, this function resolves the set of runtime options, providing Nodejs
53
+ * implementations if not provided in `options`, and returns a `Runtime`.
54
+ *
55
+ * Resolution is asynchronous because the default `os` adapter is loaded via a dynamic `import()`.
56
+ * Unlike `require`, dynamic import exists in every module system the driver ships into or is
57
+ * bundled into (CJS, ESM, and bundled ESM output), and the literal specifier keeps it
58
+ * statically analyzable for bundlers. The promise is created during synchronous
59
+ * options parsing and awaited later by consumers, so the public constructor stays synchronous.
52
60
  */
53
- export function resolveRuntimeAdapters(options: MongoClientOptions): Runtime {
54
- (globalThis as any)[ALLOWED_DRIVER_REQUIRE_PROPERTY_NAME] = true;
55
- try {
56
- const runtime = {
57
- // eslint-disable-next-line @typescript-eslint/no-require-imports
58
- os: options.runtimeAdapters?.os ?? require('os')
59
- };
60
- return runtime;
61
- } finally {
62
- (globalThis as any)[ALLOWED_DRIVER_REQUIRE_PROPERTY_NAME] = false;
63
- }
61
+ export async function resolveRuntimeAdapters(options: MongoClientOptions): Promise<Runtime> {
62
+ return {
63
+ os: options.runtimeAdapters?.os ?? (await import('os'))
64
+ };
64
65
  }
@@ -494,7 +494,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
494
494
  } else {
495
495
  if (
496
496
  (isRetryableWritesEnabled(this.topology) || isTransactionCommand(cmd)) &&
497
- needsRetryableWriteLabel(error, maxWireVersion(this), this.description.type) &&
497
+ needsRetryableWriteLabel(error) &&
498
498
  !inActiveTransaction(session, cmd)
499
499
  ) {
500
500
  error.addErrorLabel(MongoErrorLabel.RetryableWriteError);
package/src/sessions.ts CHANGED
@@ -771,7 +771,7 @@ export class ClientSession
771
771
  ) {
772
772
  // 2. If `transactionAttempt` > 0:
773
773
  if (isRetry) {
774
- // 2.1 Calculate backoffMS to be jitter * min(BACKOFF_INITIAL * 1.5 ** (transactionAttempt - 1), BACKOFF_MAX).
774
+ // 2.1 Calculate backoffMS to be jitter * min(BACKOFF_INITIAL * 1.5 ** (transactionAttempt), BACKOFF_MAX).
775
775
  // If elapsed time + backoffMS > TIMEOUT_MS, then propagate the previously encountered error to the caller of
776
776
  // withTransaction as per timeout error propagation and return immediately. Otherwise, sleep for backoffMS.
777
777
  // 2.1.1 jitter is a random float between [0, 1), optionally including 1, depending on what is most natural
@@ -785,10 +785,7 @@ export class ClientSession
785
785
  const jitter = Math.random();
786
786
  const backoffMS =
787
787
  jitter *
788
- Math.min(
789
- BACKOFF_INITIAL_MS * BACKOFF_GROWTH ** (transactionAttempt - 1),
790
- BACKOFF_MAX_MS
791
- );
788
+ Math.min(BACKOFF_INITIAL_MS * BACKOFF_GROWTH ** transactionAttempt, BACKOFF_MAX_MS);
792
789
 
793
790
  if (processTimeMS() + backoffMS >= deadline) {
794
791
  throw makeTimeoutError(
package/src/utils.ts CHANGED
@@ -343,9 +343,7 @@ export function maxWireVersion(handshakeAware?: Connection | Topology | Server):
343
343
  // object being checked will be a connection, and we will have a hello response on
344
344
  // it. For other cases, such as retryable writes, the object will be a server or
345
345
  // topology, and there will be no hello response on those objects, so we return
346
- // the max wire version so we support retryability. Once we have a min supported
347
- // wire version of 9, then the needsRetryableWriteLabel() check can remove the
348
- // usage of passing the wire version into it.
346
+ // the max wire version so we support retryability.
349
347
  if (handshakeAware.loadBalanced) {
350
348
  return MAX_SUPPORTED_WIRE_VERSION;
351
349
  }
package/tsconfig.json CHANGED
@@ -5,8 +5,15 @@
5
5
  "strict": true,
6
6
  "alwaysStrict": true,
7
7
  "target": "ES2023",
8
- "module": "commonJS",
9
- "moduleResolution": "node",
8
+ // node16 still emits CommonJS for this package (no "type": "module") but, unlike
9
+ // `module: commonjs`, models Node's real semantics: dynamic `import()` is legal inside CJS
10
+ // and is preserved verbatim instead of being downleveled to `require()` (NODE-7603).
11
+ "module": "node16",
12
+ "moduleResolution": "node16",
13
+ // node16 defaults esModuleInterop on; keep it off to preserve this package's historical
14
+ // emit exactly (`import * as x` stays `const x = require(...)`, no __importStar wrappers —
15
+ // which sinon-based module stubbing in the test suite also relies on).
16
+ "esModuleInterop": false,
10
17
  "skipLibCheck": true,
11
18
  "lib": [
12
19
  "es2023"