mongodb 6.16.0 → 6.17.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 (95) hide show
  1. package/README.md +2 -0
  2. package/lib/beta.d.ts +132 -33
  3. package/lib/bulk/common.js +4 -4
  4. package/lib/bulk/common.js.map +1 -1
  5. package/lib/client-side-encryption/auto_encrypter.js +5 -2
  6. package/lib/client-side-encryption/auto_encrypter.js.map +1 -1
  7. package/lib/client-side-encryption/client_encryption.js.map +1 -1
  8. package/lib/client-side-encryption/state_machine.js +1 -1
  9. package/lib/client-side-encryption/state_machine.js.map +1 -1
  10. package/lib/cmap/auth/mongodb_oidc/automated_callback_workflow.js +6 -0
  11. package/lib/cmap/auth/mongodb_oidc/automated_callback_workflow.js.map +1 -1
  12. package/lib/cmap/auth/mongodb_oidc/azure_machine_workflow.js +21 -33
  13. package/lib/cmap/auth/mongodb_oidc/azure_machine_workflow.js.map +1 -1
  14. package/lib/cmap/auth/mongodb_oidc/gcp_machine_workflow.js +14 -21
  15. package/lib/cmap/auth/mongodb_oidc/gcp_machine_workflow.js.map +1 -1
  16. package/lib/cmap/auth/mongodb_oidc/k8s_machine_workflow.js +19 -26
  17. package/lib/cmap/auth/mongodb_oidc/k8s_machine_workflow.js.map +1 -1
  18. package/lib/cmap/auth/mongodb_oidc/token_machine_workflow.js +12 -24
  19. package/lib/cmap/auth/mongodb_oidc/token_machine_workflow.js.map +1 -1
  20. package/lib/cmap/auth/mongodb_oidc.js +5 -4
  21. package/lib/cmap/auth/mongodb_oidc.js.map +1 -1
  22. package/lib/cmap/connect.js +4 -3
  23. package/lib/cmap/connect.js.map +1 -1
  24. package/lib/cmap/connection.js +8 -3
  25. package/lib/cmap/connection.js.map +1 -1
  26. package/lib/cmap/connection_pool.js +6 -1
  27. package/lib/cmap/connection_pool.js.map +1 -1
  28. package/lib/cmap/wire_protocol/constants.js +2 -2
  29. package/lib/cmap/wire_protocol/responses.js.map +1 -1
  30. package/lib/collection.js.map +1 -1
  31. package/lib/connection_string.js +2 -0
  32. package/lib/connection_string.js.map +1 -1
  33. package/lib/constants.js +0 -1
  34. package/lib/constants.js.map +1 -1
  35. package/lib/encrypter.js +3 -7
  36. package/lib/encrypter.js.map +1 -1
  37. package/lib/error.js +28 -1
  38. package/lib/error.js.map +1 -1
  39. package/lib/index.js +4 -3
  40. package/lib/index.js.map +1 -1
  41. package/lib/mongo_client.js +66 -20
  42. package/lib/mongo_client.js.map +1 -1
  43. package/lib/operations/aggregate.js +0 -1
  44. package/lib/operations/aggregate.js.map +1 -1
  45. package/lib/operations/client_bulk_write/command_builder.js +20 -13
  46. package/lib/operations/client_bulk_write/command_builder.js.map +1 -1
  47. package/lib/operations/find_and_modify.js +1 -1
  48. package/lib/operations/find_and_modify.js.map +1 -1
  49. package/lib/operations/update.js +6 -2
  50. package/lib/operations/update.js.map +1 -1
  51. package/lib/sdam/server.js +4 -1
  52. package/lib/sdam/server.js.map +1 -1
  53. package/lib/sdam/topology.js +9 -4
  54. package/lib/sdam/topology.js.map +1 -1
  55. package/lib/sort.js +13 -6
  56. package/lib/sort.js.map +1 -1
  57. package/lib/utils.js +15 -1
  58. package/lib/utils.js.map +1 -1
  59. package/mongodb.d.ts +132 -33
  60. package/package.json +19 -19
  61. package/src/bulk/common.ts +11 -6
  62. package/src/client-side-encryption/auto_encrypter.ts +10 -2
  63. package/src/client-side-encryption/client_encryption.ts +5 -0
  64. package/src/client-side-encryption/state_machine.ts +1 -1
  65. package/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts +6 -0
  66. package/src/cmap/auth/mongodb_oidc/azure_machine_workflow.ts +23 -35
  67. package/src/cmap/auth/mongodb_oidc/gcp_machine_workflow.ts +16 -23
  68. package/src/cmap/auth/mongodb_oidc/k8s_machine_workflow.ts +17 -25
  69. package/src/cmap/auth/mongodb_oidc/token_machine_workflow.ts +11 -24
  70. package/src/cmap/auth/mongodb_oidc.ts +13 -8
  71. package/src/cmap/connect.ts +4 -3
  72. package/src/cmap/connection.ts +11 -4
  73. package/src/cmap/connection_pool.ts +8 -1
  74. package/src/cmap/wire_protocol/constants.ts +2 -2
  75. package/src/cmap/wire_protocol/on_demand/document.ts +1 -1
  76. package/src/cmap/wire_protocol/responses.ts +2 -2
  77. package/src/collection.ts +2 -1
  78. package/src/connection_string.ts +2 -0
  79. package/src/constants.ts +0 -1
  80. package/src/encrypter.ts +3 -10
  81. package/src/error.ts +28 -0
  82. package/src/index.ts +1 -0
  83. package/src/mongo_client.ts +73 -21
  84. package/src/operations/aggregate.ts +0 -1
  85. package/src/operations/client_bulk_write/command_builder.ts +32 -14
  86. package/src/operations/client_bulk_write/common.ts +5 -0
  87. package/src/operations/find_and_modify.ts +1 -1
  88. package/src/operations/update.ts +12 -3
  89. package/src/sdam/server.ts +5 -1
  90. package/src/sdam/topology.ts +10 -4
  91. package/src/sort.ts +32 -23
  92. package/src/utils.ts +21 -1
  93. package/lib/cmap/auth/mongodb_oidc/machine_workflow.js +0 -110
  94. package/lib/cmap/auth/mongodb_oidc/machine_workflow.js.map +0 -1
  95. package/src/cmap/auth/mongodb_oidc/machine_workflow.ts +0 -142
@@ -1,142 +0,0 @@
1
- import { setTimeout } from 'timers/promises';
2
-
3
- import { type Document } from '../../../bson';
4
- import { ns } from '../../../utils';
5
- import type { Connection } from '../../connection';
6
- import type { MongoCredentials } from '../mongo_credentials';
7
- import type { Workflow } from '../mongodb_oidc';
8
- import { finishCommandDocument } from './command_builders';
9
- import { type TokenCache } from './token_cache';
10
-
11
- /** The time to throttle callback calls. */
12
- const THROTTLE_MS = 100;
13
-
14
- /**
15
- * The access token format.
16
- * @internal
17
- */
18
- export interface AccessToken {
19
- access_token: string;
20
- expires_in?: number;
21
- }
22
-
23
- /** @internal */
24
- export type OIDCTokenFunction = (credentials: MongoCredentials) => Promise<AccessToken>;
25
-
26
- /**
27
- * Common behaviour for OIDC machine workflows.
28
- * @internal
29
- */
30
- export abstract class MachineWorkflow implements Workflow {
31
- cache: TokenCache;
32
- callback: OIDCTokenFunction;
33
- lastExecutionTime: number;
34
-
35
- /**
36
- * Instantiate the machine workflow.
37
- */
38
- constructor(cache: TokenCache) {
39
- this.cache = cache;
40
- this.callback = this.withLock(this.getToken.bind(this));
41
- this.lastExecutionTime = Date.now() - THROTTLE_MS;
42
- }
43
-
44
- /**
45
- * Execute the workflow. Gets the token from the subclass implementation.
46
- */
47
- async execute(connection: Connection, credentials: MongoCredentials): Promise<void> {
48
- const token = await this.getTokenFromCacheOrEnv(connection, credentials);
49
- const command = finishCommandDocument(token);
50
- await connection.command(ns(credentials.source), command, undefined);
51
- }
52
-
53
- /**
54
- * Reauthenticate on a machine workflow just grabs the token again since the server
55
- * has said the current access token is invalid or expired.
56
- */
57
- async reauthenticate(connection: Connection, credentials: MongoCredentials): Promise<void> {
58
- if (this.cache.hasAccessToken) {
59
- // Reauthentication implies the token has expired.
60
- if (connection.accessToken === this.cache.getAccessToken()) {
61
- // If connection's access token is the same as the cache's, remove
62
- // the token from the cache and connection.
63
- this.cache.removeAccessToken();
64
- delete connection.accessToken;
65
- } else {
66
- // If the connection's access token is different from the cache's, set
67
- // the cache's token on the connection and do not remove from the
68
- // cache.
69
- connection.accessToken = this.cache.getAccessToken();
70
- }
71
- }
72
- await this.execute(connection, credentials);
73
- }
74
-
75
- /**
76
- * Get the document to add for speculative authentication.
77
- */
78
- async speculativeAuth(connection: Connection, credentials: MongoCredentials): Promise<Document> {
79
- // The spec states only cached access tokens can use speculative auth.
80
- if (!this.cache.hasAccessToken) {
81
- return {};
82
- }
83
- const token = await this.getTokenFromCacheOrEnv(connection, credentials);
84
- const document = finishCommandDocument(token);
85
- document.db = credentials.source;
86
- return { speculativeAuthenticate: document };
87
- }
88
-
89
- /**
90
- * Get the token from the cache or environment.
91
- */
92
- private async getTokenFromCacheOrEnv(
93
- connection: Connection,
94
- credentials: MongoCredentials
95
- ): Promise<string> {
96
- if (this.cache.hasAccessToken) {
97
- const token = this.cache.getAccessToken();
98
- // New connections won't have an access token so ensure we set here.
99
- if (!connection.accessToken) {
100
- connection.accessToken = token;
101
- }
102
- return token;
103
- } else {
104
- const token = await this.callback(credentials);
105
- this.cache.put({ accessToken: token.access_token, expiresInSeconds: token.expires_in });
106
- // Put the access token on the connection as well.
107
- connection.accessToken = token.access_token;
108
- return token.access_token;
109
- }
110
- }
111
-
112
- /**
113
- * Ensure the callback is only executed one at a time, and throttled to
114
- * only once per 100ms.
115
- */
116
- private withLock(callback: OIDCTokenFunction): OIDCTokenFunction {
117
- let lock: Promise<any> = Promise.resolve();
118
- return async (credentials: MongoCredentials): Promise<AccessToken> => {
119
- // We do this to ensure that we would never return the result of the
120
- // previous lock, only the current callback's value would get returned.
121
- await lock;
122
- lock = lock
123
-
124
- .catch(() => null)
125
-
126
- .then(async () => {
127
- const difference = Date.now() - this.lastExecutionTime;
128
- if (difference <= THROTTLE_MS) {
129
- await setTimeout(THROTTLE_MS - difference);
130
- }
131
- this.lastExecutionTime = Date.now();
132
- return await callback(credentials);
133
- });
134
- return await lock;
135
- };
136
- }
137
-
138
- /**
139
- * Get the token from the environment or endpoint.
140
- */
141
- abstract getToken(credentials: MongoCredentials): Promise<AccessToken>;
142
- }