mongodb 6.16.0 → 6.17.0-dev.20250605.sha.57ef31be
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.
- package/README.md +2 -0
- package/lib/beta.d.ts +160 -49
- package/lib/bulk/common.js +4 -4
- package/lib/bulk/common.js.map +1 -1
- package/lib/change_stream.js.map +1 -1
- package/lib/client-side-encryption/auto_encrypter.js +5 -2
- package/lib/client-side-encryption/auto_encrypter.js.map +1 -1
- package/lib/client-side-encryption/client_encryption.js.map +1 -1
- package/lib/client-side-encryption/state_machine.js +1 -1
- package/lib/client-side-encryption/state_machine.js.map +1 -1
- package/lib/cmap/auth/mongodb_oidc/automated_callback_workflow.js +6 -0
- package/lib/cmap/auth/mongodb_oidc/automated_callback_workflow.js.map +1 -1
- package/lib/cmap/auth/mongodb_oidc/azure_machine_workflow.js +21 -33
- package/lib/cmap/auth/mongodb_oidc/azure_machine_workflow.js.map +1 -1
- package/lib/cmap/auth/mongodb_oidc/gcp_machine_workflow.js +14 -21
- package/lib/cmap/auth/mongodb_oidc/gcp_machine_workflow.js.map +1 -1
- package/lib/cmap/auth/mongodb_oidc/k8s_machine_workflow.js +19 -26
- package/lib/cmap/auth/mongodb_oidc/k8s_machine_workflow.js.map +1 -1
- package/lib/cmap/auth/mongodb_oidc/token_machine_workflow.js +12 -24
- package/lib/cmap/auth/mongodb_oidc/token_machine_workflow.js.map +1 -1
- package/lib/cmap/auth/mongodb_oidc.js +5 -4
- package/lib/cmap/auth/mongodb_oidc.js.map +1 -1
- package/lib/cmap/commands.js +10 -8
- package/lib/cmap/commands.js.map +1 -1
- package/lib/cmap/connect.js +4 -3
- package/lib/cmap/connect.js.map +1 -1
- package/lib/cmap/connection.js +8 -3
- package/lib/cmap/connection.js.map +1 -1
- package/lib/cmap/connection_pool.js +6 -1
- package/lib/cmap/connection_pool.js.map +1 -1
- package/lib/cmap/handshake/client_metadata.js +1 -1
- package/lib/cmap/handshake/client_metadata.js.map +1 -1
- package/lib/cmap/wire_protocol/compression.js.map +1 -1
- package/lib/cmap/wire_protocol/constants.js +2 -2
- package/lib/cmap/wire_protocol/on_data.js +4 -0
- package/lib/cmap/wire_protocol/on_data.js.map +1 -1
- package/lib/cmap/wire_protocol/on_demand/document.js +16 -15
- package/lib/cmap/wire_protocol/on_demand/document.js.map +1 -1
- package/lib/cmap/wire_protocol/responses.js +11 -4
- package/lib/cmap/wire_protocol/responses.js.map +1 -1
- package/lib/collection.js.map +1 -1
- package/lib/connection_string.js +4 -0
- package/lib/connection_string.js.map +1 -1
- package/lib/constants.js +0 -1
- package/lib/constants.js.map +1 -1
- package/lib/cursor/abstract_cursor.js.map +1 -1
- package/lib/encrypter.js +3 -7
- package/lib/encrypter.js.map +1 -1
- package/lib/error.js +28 -1
- package/lib/error.js.map +1 -1
- package/lib/index.js +4 -3
- package/lib/index.js.map +1 -1
- package/lib/mongo_client.js +66 -20
- package/lib/mongo_client.js.map +1 -1
- package/lib/operations/aggregate.js +0 -1
- package/lib/operations/aggregate.js.map +1 -1
- package/lib/operations/client_bulk_write/command_builder.js +20 -13
- package/lib/operations/client_bulk_write/command_builder.js.map +1 -1
- package/lib/operations/distinct.js +1 -0
- package/lib/operations/distinct.js.map +1 -1
- package/lib/operations/find_and_modify.js +1 -1
- package/lib/operations/find_and_modify.js.map +1 -1
- package/lib/operations/rename.js.map +1 -1
- package/lib/operations/run_command.js.map +1 -1
- package/lib/operations/search_indexes/create.js.map +1 -1
- package/lib/operations/search_indexes/drop.js.map +1 -1
- package/lib/operations/search_indexes/update.js.map +1 -1
- package/lib/operations/update.js +6 -2
- package/lib/operations/update.js.map +1 -1
- package/lib/sdam/server.js +4 -1
- package/lib/sdam/server.js.map +1 -1
- package/lib/sdam/topology.js +9 -4
- package/lib/sdam/topology.js.map +1 -1
- package/lib/sort.js +13 -6
- package/lib/sort.js.map +1 -1
- package/lib/utils.js +15 -2
- package/lib/utils.js.map +1 -1
- package/lib/write_concern.js +2 -4
- package/lib/write_concern.js.map +1 -1
- package/mongodb.d.ts +160 -49
- package/package.json +21 -21
- package/src/bulk/common.ts +14 -11
- package/src/change_stream.ts +38 -13
- package/src/client-side-encryption/auto_encrypter.ts +10 -2
- package/src/client-side-encryption/client_encryption.ts +5 -0
- package/src/client-side-encryption/state_machine.ts +8 -5
- package/src/cmap/auth/mongodb_oidc/automated_callback_workflow.ts +6 -0
- package/src/cmap/auth/mongodb_oidc/azure_machine_workflow.ts +23 -35
- package/src/cmap/auth/mongodb_oidc/gcp_machine_workflow.ts +16 -23
- package/src/cmap/auth/mongodb_oidc/k8s_machine_workflow.ts +17 -25
- package/src/cmap/auth/mongodb_oidc/token_machine_workflow.ts +11 -24
- package/src/cmap/auth/mongodb_oidc.ts +13 -8
- package/src/cmap/commands.ts +31 -16
- package/src/cmap/connect.ts +4 -3
- package/src/cmap/connection.ts +11 -4
- package/src/cmap/connection_pool.ts +8 -1
- package/src/cmap/handshake/client_metadata.ts +5 -1
- package/src/cmap/wire_protocol/compression.ts +2 -1
- package/src/cmap/wire_protocol/constants.ts +2 -2
- package/src/cmap/wire_protocol/on_data.ts +5 -0
- package/src/cmap/wire_protocol/on_demand/document.ts +20 -15
- package/src/cmap/wire_protocol/responses.ts +10 -10
- package/src/collection.ts +2 -1
- package/src/connection_string.ts +4 -0
- package/src/constants.ts +0 -1
- package/src/cursor/abstract_cursor.ts +6 -4
- package/src/encrypter.ts +3 -10
- package/src/error.ts +28 -0
- package/src/index.ts +3 -0
- package/src/mongo_client.ts +73 -21
- package/src/operations/aggregate.ts +0 -1
- package/src/operations/client_bulk_write/command_builder.ts +32 -14
- package/src/operations/client_bulk_write/common.ts +5 -0
- package/src/operations/distinct.ts +1 -0
- package/src/operations/find_and_modify.ts +1 -1
- package/src/operations/rename.ts +8 -5
- package/src/operations/run_command.ts +17 -4
- package/src/operations/search_indexes/create.ts +6 -4
- package/src/operations/search_indexes/drop.ts +6 -4
- package/src/operations/search_indexes/update.ts +8 -5
- package/src/operations/update.ts +12 -3
- package/src/sdam/server.ts +5 -1
- package/src/sdam/topology.ts +10 -4
- package/src/sort.ts +32 -23
- package/src/utils.ts +29 -9
- package/src/write_concern.ts +2 -4
- package/tsconfig.json +2 -1
- package/lib/cmap/auth/mongodb_oidc/machine_workflow.js +0 -110
- package/lib/cmap/auth/mongodb_oidc/machine_workflow.js.map +0 -1
- package/src/cmap/auth/mongodb_oidc/machine_workflow.ts +0 -142
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MachineWorkflow = void 0;
|
|
4
|
-
const promises_1 = require("timers/promises");
|
|
5
|
-
const utils_1 = require("../../../utils");
|
|
6
|
-
const command_builders_1 = require("./command_builders");
|
|
7
|
-
/** The time to throttle callback calls. */
|
|
8
|
-
const THROTTLE_MS = 100;
|
|
9
|
-
/**
|
|
10
|
-
* Common behaviour for OIDC machine workflows.
|
|
11
|
-
* @internal
|
|
12
|
-
*/
|
|
13
|
-
class MachineWorkflow {
|
|
14
|
-
/**
|
|
15
|
-
* Instantiate the machine workflow.
|
|
16
|
-
*/
|
|
17
|
-
constructor(cache) {
|
|
18
|
-
this.cache = cache;
|
|
19
|
-
this.callback = this.withLock(this.getToken.bind(this));
|
|
20
|
-
this.lastExecutionTime = Date.now() - THROTTLE_MS;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Execute the workflow. Gets the token from the subclass implementation.
|
|
24
|
-
*/
|
|
25
|
-
async execute(connection, credentials) {
|
|
26
|
-
const token = await this.getTokenFromCacheOrEnv(connection, credentials);
|
|
27
|
-
const command = (0, command_builders_1.finishCommandDocument)(token);
|
|
28
|
-
await connection.command((0, utils_1.ns)(credentials.source), command, undefined);
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Reauthenticate on a machine workflow just grabs the token again since the server
|
|
32
|
-
* has said the current access token is invalid or expired.
|
|
33
|
-
*/
|
|
34
|
-
async reauthenticate(connection, credentials) {
|
|
35
|
-
if (this.cache.hasAccessToken) {
|
|
36
|
-
// Reauthentication implies the token has expired.
|
|
37
|
-
if (connection.accessToken === this.cache.getAccessToken()) {
|
|
38
|
-
// If connection's access token is the same as the cache's, remove
|
|
39
|
-
// the token from the cache and connection.
|
|
40
|
-
this.cache.removeAccessToken();
|
|
41
|
-
delete connection.accessToken;
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
// If the connection's access token is different from the cache's, set
|
|
45
|
-
// the cache's token on the connection and do not remove from the
|
|
46
|
-
// cache.
|
|
47
|
-
connection.accessToken = this.cache.getAccessToken();
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
await this.execute(connection, credentials);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Get the document to add for speculative authentication.
|
|
54
|
-
*/
|
|
55
|
-
async speculativeAuth(connection, credentials) {
|
|
56
|
-
// The spec states only cached access tokens can use speculative auth.
|
|
57
|
-
if (!this.cache.hasAccessToken) {
|
|
58
|
-
return {};
|
|
59
|
-
}
|
|
60
|
-
const token = await this.getTokenFromCacheOrEnv(connection, credentials);
|
|
61
|
-
const document = (0, command_builders_1.finishCommandDocument)(token);
|
|
62
|
-
document.db = credentials.source;
|
|
63
|
-
return { speculativeAuthenticate: document };
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Get the token from the cache or environment.
|
|
67
|
-
*/
|
|
68
|
-
async getTokenFromCacheOrEnv(connection, credentials) {
|
|
69
|
-
if (this.cache.hasAccessToken) {
|
|
70
|
-
const token = this.cache.getAccessToken();
|
|
71
|
-
// New connections won't have an access token so ensure we set here.
|
|
72
|
-
if (!connection.accessToken) {
|
|
73
|
-
connection.accessToken = token;
|
|
74
|
-
}
|
|
75
|
-
return token;
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
const token = await this.callback(credentials);
|
|
79
|
-
this.cache.put({ accessToken: token.access_token, expiresInSeconds: token.expires_in });
|
|
80
|
-
// Put the access token on the connection as well.
|
|
81
|
-
connection.accessToken = token.access_token;
|
|
82
|
-
return token.access_token;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Ensure the callback is only executed one at a time, and throttled to
|
|
87
|
-
* only once per 100ms.
|
|
88
|
-
*/
|
|
89
|
-
withLock(callback) {
|
|
90
|
-
let lock = Promise.resolve();
|
|
91
|
-
return async (credentials) => {
|
|
92
|
-
// We do this to ensure that we would never return the result of the
|
|
93
|
-
// previous lock, only the current callback's value would get returned.
|
|
94
|
-
await lock;
|
|
95
|
-
lock = lock
|
|
96
|
-
.catch(() => null)
|
|
97
|
-
.then(async () => {
|
|
98
|
-
const difference = Date.now() - this.lastExecutionTime;
|
|
99
|
-
if (difference <= THROTTLE_MS) {
|
|
100
|
-
await (0, promises_1.setTimeout)(THROTTLE_MS - difference);
|
|
101
|
-
}
|
|
102
|
-
this.lastExecutionTime = Date.now();
|
|
103
|
-
return await callback(credentials);
|
|
104
|
-
});
|
|
105
|
-
return await lock;
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
exports.MachineWorkflow = MachineWorkflow;
|
|
110
|
-
//# sourceMappingURL=machine_workflow.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"machine_workflow.js","sourceRoot":"","sources":["../../../../src/cmap/auth/mongodb_oidc/machine_workflow.ts"],"names":[],"mappings":";;;AAAA,8CAA6C;AAG7C,0CAAoC;AAIpC,yDAA2D;AAG3D,2CAA2C;AAC3C,MAAM,WAAW,GAAG,GAAG,CAAC;AAcxB;;;GAGG;AACH,MAAsB,eAAe;IAKnC;;OAEG;IACH,YAAY,KAAiB;QAC3B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;QACxD,IAAI,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AAC,GAAG,EAAE,GAAG,IAAI,CAAC,iBAAiB,CAAC;gBACvD,IAAI,UAAU,IAAI,WAAW,EAAE,CAAC;oBAC9B,MAAM,IAAA,qBAAU,EAAC,WAAW,GAAG,UAAU,CAAC,CAAC;gBAC7C,CAAC;gBACD,IAAI,CAAC,iBAAiB,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;gBACpC,OAAO,MAAM,QAAQ,CAAC,WAAW,CAAC,CAAC;YACrC,CAAC,CAAC,CAAC;YACL,OAAO,MAAM,IAAI,CAAC;QACpB,CAAC,CAAC;IACJ,CAAC;CAMF;AAhHD,0CAgHC"}
|
|
@@ -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
|
-
}
|