mongodb 4.3.1 → 4.5.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.
- package/README.md +7 -6
- package/lib/admin.js +5 -6
- package/lib/admin.js.map +1 -1
- package/lib/bulk/common.js +31 -7
- package/lib/bulk/common.js.map +1 -1
- package/lib/bulk/unordered.js.map +1 -1
- package/lib/change_stream.js +29 -20
- package/lib/change_stream.js.map +1 -1
- package/lib/cmap/auth/gssapi.js +49 -7
- package/lib/cmap/auth/gssapi.js.map +1 -1
- package/lib/cmap/auth/mongo_credentials.js +12 -1
- package/lib/cmap/auth/mongo_credentials.js.map +1 -1
- package/lib/cmap/auth/mongocr.js.map +1 -1
- package/lib/cmap/auth/mongodb_aws.js.map +1 -1
- package/lib/cmap/auth/plain.js.map +1 -1
- package/lib/cmap/auth/scram.js +1 -0
- package/lib/cmap/auth/scram.js.map +1 -1
- package/lib/cmap/auth/x509.js.map +1 -1
- package/lib/cmap/commands.js.map +1 -1
- package/lib/cmap/connect.js +0 -6
- package/lib/cmap/connect.js.map +1 -1
- package/lib/cmap/connection.js +111 -86
- package/lib/cmap/connection.js.map +1 -1
- package/lib/cmap/errors.js.map +1 -1
- package/lib/cmap/message_stream.js.map +1 -1
- package/lib/cmap/stream_description.js +3 -0
- package/lib/cmap/stream_description.js.map +1 -1
- package/lib/collection.js +29 -28
- package/lib/collection.js.map +1 -1
- package/lib/connection_string.js +53 -40
- package/lib/connection_string.js.map +1 -1
- package/lib/cursor/abstract_cursor.js +64 -42
- package/lib/cursor/abstract_cursor.js.map +1 -1
- package/lib/cursor/aggregation_cursor.js +2 -2
- package/lib/cursor/aggregation_cursor.js.map +1 -1
- package/lib/cursor/find_cursor.js +4 -3
- package/lib/cursor/find_cursor.js.map +1 -1
- package/lib/db.js +13 -13
- package/lib/db.js.map +1 -1
- package/lib/encrypter.js +17 -9
- package/lib/encrypter.js.map +1 -1
- package/lib/error.js +99 -48
- package/lib/error.js.map +1 -1
- package/lib/gridfs/download.js +2 -0
- package/lib/gridfs/download.js.map +1 -1
- package/lib/gridfs/index.js +42 -51
- package/lib/gridfs/index.js.map +1 -1
- package/lib/gridfs/upload.js.map +1 -1
- package/lib/index.js +9 -2
- package/lib/index.js.map +1 -1
- package/lib/mongo_client.js +14 -27
- package/lib/mongo_client.js.map +1 -1
- package/lib/operations/add_user.js +8 -1
- package/lib/operations/add_user.js.map +1 -1
- package/lib/operations/aggregate.js +5 -0
- package/lib/operations/aggregate.js.map +1 -1
- package/lib/operations/bulk_write.js.map +1 -1
- package/lib/operations/collections.js.map +1 -1
- package/lib/operations/command.js.map +1 -1
- package/lib/operations/common_functions.js +8 -1
- package/lib/operations/common_functions.js.map +1 -1
- package/lib/operations/count.js.map +1 -1
- package/lib/operations/count_documents.js.map +1 -1
- package/lib/operations/create_collection.js.map +1 -1
- package/lib/operations/delete.js +5 -3
- package/lib/operations/delete.js.map +1 -1
- package/lib/operations/distinct.js.map +1 -1
- package/lib/operations/drop.js.map +1 -1
- package/lib/operations/estimated_document_count.js.map +1 -1
- package/lib/operations/eval.js.map +1 -1
- package/lib/operations/execute_operation.js +70 -79
- package/lib/operations/execute_operation.js.map +1 -1
- package/lib/operations/find.js +3 -1
- package/lib/operations/find.js.map +1 -1
- package/lib/operations/find_and_modify.js +5 -0
- package/lib/operations/find_and_modify.js.map +1 -1
- package/lib/operations/get_more.js +5 -0
- package/lib/operations/get_more.js.map +1 -1
- package/lib/operations/indexes.js +8 -9
- package/lib/operations/indexes.js.map +1 -1
- package/lib/operations/insert.js +3 -1
- package/lib/operations/insert.js.map +1 -1
- package/lib/operations/is_capped.js.map +1 -1
- package/lib/operations/list_collections.js +10 -42
- package/lib/operations/list_collections.js.map +1 -1
- package/lib/operations/list_databases.js +5 -0
- package/lib/operations/list_databases.js.map +1 -1
- package/lib/operations/map_reduce.js +1 -2
- package/lib/operations/map_reduce.js.map +1 -1
- package/lib/operations/operation.js +1 -3
- package/lib/operations/operation.js.map +1 -1
- package/lib/operations/options_operation.js.map +1 -1
- package/lib/operations/profiling_level.js.map +1 -1
- package/lib/operations/remove_user.js.map +1 -1
- package/lib/operations/rename.js +1 -1
- package/lib/operations/rename.js.map +1 -1
- package/lib/operations/run_command.js.map +1 -1
- package/lib/operations/set_profiling_level.js.map +1 -1
- package/lib/operations/stats.js.map +1 -1
- package/lib/operations/update.js +5 -0
- package/lib/operations/update.js.map +1 -1
- package/lib/operations/validate_collection.js.map +1 -1
- package/lib/read_concern.js +1 -0
- package/lib/read_concern.js.map +1 -1
- package/lib/sdam/common.js +1 -7
- package/lib/sdam/common.js.map +1 -1
- package/lib/sdam/events.js +1 -1
- package/lib/sdam/events.js.map +1 -1
- package/lib/sdam/monitor.js +1 -2
- package/lib/sdam/monitor.js.map +1 -1
- package/lib/sdam/server.js +79 -57
- package/lib/sdam/server.js.map +1 -1
- package/lib/sdam/topology.js +16 -33
- package/lib/sdam/topology.js.map +1 -1
- package/lib/sdam/topology_description.js +1 -3
- package/lib/sdam/topology_description.js.map +1 -1
- package/lib/sessions.js +93 -68
- package/lib/sessions.js.map +1 -1
- package/lib/utils.js +21 -97
- package/lib/utils.js.map +1 -1
- package/mongodb.d.ts +188 -29
- package/package.json +46 -46
- package/src/admin.ts +6 -10
- package/src/bulk/common.ts +42 -14
- package/src/bulk/unordered.ts +1 -1
- package/src/change_stream.ts +58 -42
- package/src/cmap/auth/gssapi.ts +58 -7
- package/src/cmap/auth/mongo_credentials.ts +17 -2
- package/src/cmap/auth/mongocr.ts +1 -1
- package/src/cmap/auth/mongodb_aws.ts +1 -1
- package/src/cmap/auth/plain.ts +1 -1
- package/src/cmap/auth/scram.ts +3 -2
- package/src/cmap/auth/x509.ts +6 -2
- package/src/cmap/commands.ts +3 -0
- package/src/cmap/connect.ts +2 -20
- package/src/cmap/connection.ts +162 -111
- package/src/cmap/errors.ts +2 -2
- package/src/cmap/message_stream.ts +2 -2
- package/src/cmap/stream_description.ts +4 -1
- package/src/collection.ts +37 -33
- package/src/connection_string.ts +77 -45
- package/src/cursor/abstract_cursor.ts +85 -56
- package/src/cursor/aggregation_cursor.ts +5 -5
- package/src/cursor/find_cursor.ts +19 -11
- package/src/db.ts +15 -19
- package/src/deps.ts +52 -0
- package/src/encrypter.ts +18 -10
- package/src/error.ts +145 -76
- package/src/gridfs/download.ts +3 -1
- package/src/gridfs/index.ts +51 -68
- package/src/gridfs/upload.ts +12 -12
- package/src/index.ts +10 -1
- package/src/mongo_client.ts +19 -41
- package/src/operations/add_user.ts +14 -3
- package/src/operations/aggregate.ts +15 -5
- package/src/operations/bulk_write.ts +6 -2
- package/src/operations/collections.ts +6 -2
- package/src/operations/command.ts +23 -8
- package/src/operations/common_functions.ts +8 -1
- package/src/operations/count.ts +6 -2
- package/src/operations/count_documents.ts +5 -1
- package/src/operations/create_collection.ts +6 -2
- package/src/operations/delete.ts +19 -13
- package/src/operations/distinct.ts +6 -2
- package/src/operations/drop.ts +12 -4
- package/src/operations/estimated_document_count.ts +11 -3
- package/src/operations/eval.ts +6 -2
- package/src/operations/execute_operation.ts +102 -101
- package/src/operations/find.ts +9 -5
- package/src/operations/find_and_modify.ts +21 -2
- package/src/operations/get_more.ts +20 -6
- package/src/operations/indexes.ts +54 -36
- package/src/operations/insert.ts +20 -6
- package/src/operations/is_capped.ts +6 -2
- package/src/operations/list_collections.ts +24 -59
- package/src/operations/list_databases.ts +13 -3
- package/src/operations/map_reduce.ts +7 -6
- package/src/operations/operation.ts +10 -9
- package/src/operations/options_operation.ts +6 -2
- package/src/operations/profiling_level.ts +6 -2
- package/src/operations/remove_user.ts +6 -2
- package/src/operations/rename.ts +7 -3
- package/src/operations/run_command.ts +6 -2
- package/src/operations/set_profiling_level.ts +6 -2
- package/src/operations/stats.ts +12 -4
- package/src/operations/update.ts +21 -9
- package/src/operations/validate_collection.ts +6 -2
- package/src/read_concern.ts +1 -0
- package/src/sdam/common.ts +0 -6
- package/src/sdam/events.ts +2 -2
- package/src/sdam/monitor.ts +4 -5
- package/src/sdam/server.ts +95 -90
- package/src/sdam/topology.ts +9 -53
- package/src/sdam/topology_description.ts +1 -3
- package/src/sessions.ts +108 -78
- package/src/utils.ts +38 -118
- package/tsconfig.json +40 -0
- package/mongodb.ts34.d.ts +0 -5649
|
@@ -9,7 +9,7 @@ export type RunCommandOptions = CommandOperationOptions;
|
|
|
9
9
|
|
|
10
10
|
/** @internal */
|
|
11
11
|
export class RunCommandOperation<T = Document> extends CommandOperation<T> {
|
|
12
|
-
options: RunCommandOptions;
|
|
12
|
+
override options: RunCommandOptions;
|
|
13
13
|
command: Document;
|
|
14
14
|
|
|
15
15
|
constructor(parent: OperationParent | undefined, command: Document, options?: RunCommandOptions) {
|
|
@@ -18,7 +18,11 @@ export class RunCommandOperation<T = Document> extends CommandOperation<T> {
|
|
|
18
18
|
this.command = command;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
execute(
|
|
21
|
+
override execute(
|
|
22
|
+
server: Server,
|
|
23
|
+
session: ClientSession | undefined,
|
|
24
|
+
callback: Callback<T>
|
|
25
|
+
): void {
|
|
22
26
|
const command = this.command;
|
|
23
27
|
this.executeCommand(server, session, command, callback);
|
|
24
28
|
}
|
|
@@ -23,7 +23,7 @@ export type SetProfilingLevelOptions = CommandOperationOptions;
|
|
|
23
23
|
|
|
24
24
|
/** @internal */
|
|
25
25
|
export class SetProfilingLevelOperation extends CommandOperation<ProfilingLevel> {
|
|
26
|
-
options: SetProfilingLevelOptions;
|
|
26
|
+
override options: SetProfilingLevelOptions;
|
|
27
27
|
level: ProfilingLevel;
|
|
28
28
|
profile: 0 | 1 | 2;
|
|
29
29
|
|
|
@@ -48,7 +48,11 @@ export class SetProfilingLevelOperation extends CommandOperation<ProfilingLevel>
|
|
|
48
48
|
this.level = level;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
execute(
|
|
51
|
+
override execute(
|
|
52
|
+
server: Server,
|
|
53
|
+
session: ClientSession | undefined,
|
|
54
|
+
callback: Callback<ProfilingLevel>
|
|
55
|
+
): void {
|
|
52
56
|
const level = this.level;
|
|
53
57
|
|
|
54
58
|
if (!levelValues.has(level)) {
|
package/src/operations/stats.ts
CHANGED
|
@@ -18,7 +18,7 @@ export interface CollStatsOptions extends CommandOperationOptions {
|
|
|
18
18
|
* @internal
|
|
19
19
|
*/
|
|
20
20
|
export class CollStatsOperation extends CommandOperation<Document> {
|
|
21
|
-
options: CollStatsOptions;
|
|
21
|
+
override options: CollStatsOptions;
|
|
22
22
|
collectionName: string;
|
|
23
23
|
|
|
24
24
|
/**
|
|
@@ -33,7 +33,11 @@ export class CollStatsOperation extends CommandOperation<Document> {
|
|
|
33
33
|
this.collectionName = collection.collectionName;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
execute(
|
|
36
|
+
override execute(
|
|
37
|
+
server: Server,
|
|
38
|
+
session: ClientSession | undefined,
|
|
39
|
+
callback: Callback<CollStats>
|
|
40
|
+
): void {
|
|
37
41
|
const command: Document = { collStats: this.collectionName };
|
|
38
42
|
if (this.options.scale != null) {
|
|
39
43
|
command.scale = this.options.scale;
|
|
@@ -51,14 +55,18 @@ export interface DbStatsOptions extends CommandOperationOptions {
|
|
|
51
55
|
|
|
52
56
|
/** @internal */
|
|
53
57
|
export class DbStatsOperation extends CommandOperation<Document> {
|
|
54
|
-
options: DbStatsOptions;
|
|
58
|
+
override options: DbStatsOptions;
|
|
55
59
|
|
|
56
60
|
constructor(db: Db, options: DbStatsOptions) {
|
|
57
61
|
super(db, options);
|
|
58
62
|
this.options = options;
|
|
59
63
|
}
|
|
60
64
|
|
|
61
|
-
execute(
|
|
65
|
+
override execute(
|
|
66
|
+
server: Server,
|
|
67
|
+
session: ClientSession | undefined,
|
|
68
|
+
callback: Callback<Document>
|
|
69
|
+
): void {
|
|
62
70
|
const command: Document = { dbStats: true };
|
|
63
71
|
if (this.options.scale != null) {
|
|
64
72
|
command.scale = this.options.scale;
|
package/src/operations/update.ts
CHANGED
|
@@ -63,7 +63,7 @@ export interface UpdateStatement {
|
|
|
63
63
|
|
|
64
64
|
/** @internal */
|
|
65
65
|
export class UpdateOperation extends CommandOperation<Document> {
|
|
66
|
-
options: UpdateOptions & { ordered?: boolean };
|
|
66
|
+
override options: UpdateOptions & { ordered?: boolean };
|
|
67
67
|
statements: UpdateStatement[];
|
|
68
68
|
|
|
69
69
|
constructor(
|
|
@@ -78,7 +78,7 @@ export class UpdateOperation extends CommandOperation<Document> {
|
|
|
78
78
|
this.statements = statements;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
get canRetryWrite(): boolean {
|
|
81
|
+
override get canRetryWrite(): boolean {
|
|
82
82
|
if (super.canRetryWrite === false) {
|
|
83
83
|
return false;
|
|
84
84
|
}
|
|
@@ -86,7 +86,11 @@ export class UpdateOperation extends CommandOperation<Document> {
|
|
|
86
86
|
return this.statements.every(op => op.multi == null || op.multi === false);
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
execute(
|
|
89
|
+
override execute(
|
|
90
|
+
server: Server,
|
|
91
|
+
session: ClientSession | undefined,
|
|
92
|
+
callback: Callback<Document>
|
|
93
|
+
): void {
|
|
90
94
|
const options = this.options ?? {};
|
|
91
95
|
const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;
|
|
92
96
|
const command: Document = {
|
|
@@ -103,6 +107,12 @@ export class UpdateOperation extends CommandOperation<Document> {
|
|
|
103
107
|
command.let = options.let;
|
|
104
108
|
}
|
|
105
109
|
|
|
110
|
+
// we check for undefined specifically here to allow falsy values
|
|
111
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
112
|
+
if (options.comment !== undefined) {
|
|
113
|
+
command.comment = options.comment;
|
|
114
|
+
}
|
|
115
|
+
|
|
106
116
|
const statementWithCollation = this.statements.find(statement => !!statement.collation);
|
|
107
117
|
if (
|
|
108
118
|
collationNotSupported(server, options) ||
|
|
@@ -152,9 +162,9 @@ export class UpdateOneOperation extends UpdateOperation {
|
|
|
152
162
|
}
|
|
153
163
|
}
|
|
154
164
|
|
|
155
|
-
execute(
|
|
165
|
+
override execute(
|
|
156
166
|
server: Server,
|
|
157
|
-
session: ClientSession,
|
|
167
|
+
session: ClientSession | undefined,
|
|
158
168
|
callback: Callback<UpdateResult | Document>
|
|
159
169
|
): void {
|
|
160
170
|
super.execute(server, session, (err, res) => {
|
|
@@ -189,9 +199,9 @@ export class UpdateManyOperation extends UpdateOperation {
|
|
|
189
199
|
}
|
|
190
200
|
}
|
|
191
201
|
|
|
192
|
-
execute(
|
|
202
|
+
override execute(
|
|
193
203
|
server: Server,
|
|
194
|
-
session: ClientSession,
|
|
204
|
+
session: ClientSession | undefined,
|
|
195
205
|
callback: Callback<UpdateResult | Document>
|
|
196
206
|
): void {
|
|
197
207
|
super.execute(server, session, (err, res) => {
|
|
@@ -222,6 +232,8 @@ export interface ReplaceOptions extends CommandOperationOptions {
|
|
|
222
232
|
hint?: string | Document;
|
|
223
233
|
/** When true, creates a new document if no document matches the query */
|
|
224
234
|
upsert?: boolean;
|
|
235
|
+
/** Map of parameter names and values that can be accessed using $$var (requires MongoDB 5.0). */
|
|
236
|
+
let?: Document;
|
|
225
237
|
}
|
|
226
238
|
|
|
227
239
|
/** @internal */
|
|
@@ -243,9 +255,9 @@ export class ReplaceOneOperation extends UpdateOperation {
|
|
|
243
255
|
}
|
|
244
256
|
}
|
|
245
257
|
|
|
246
|
-
execute(
|
|
258
|
+
override execute(
|
|
247
259
|
server: Server,
|
|
248
|
-
session: ClientSession,
|
|
260
|
+
session: ClientSession | undefined,
|
|
249
261
|
callback: Callback<UpdateResult | Document>
|
|
250
262
|
): void {
|
|
251
263
|
super.execute(server, session, (err, res) => {
|
|
@@ -14,7 +14,7 @@ export interface ValidateCollectionOptions extends CommandOperationOptions {
|
|
|
14
14
|
|
|
15
15
|
/** @internal */
|
|
16
16
|
export class ValidateCollectionOperation extends CommandOperation<Document> {
|
|
17
|
-
options: ValidateCollectionOptions;
|
|
17
|
+
override options: ValidateCollectionOptions;
|
|
18
18
|
collectionName: string;
|
|
19
19
|
command: Document;
|
|
20
20
|
|
|
@@ -34,7 +34,11 @@ export class ValidateCollectionOperation extends CommandOperation<Document> {
|
|
|
34
34
|
this.collectionName = collectionName;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
execute(
|
|
37
|
+
override execute(
|
|
38
|
+
server: Server,
|
|
39
|
+
session: ClientSession | undefined,
|
|
40
|
+
callback: Callback<Document>
|
|
41
|
+
): void {
|
|
38
42
|
const collectionName = this.collectionName;
|
|
39
43
|
|
|
40
44
|
super.executeCommand(server, session, this.command, (err, doc) => {
|
package/src/read_concern.ts
CHANGED
package/src/sdam/common.ts
CHANGED
|
@@ -53,12 +53,6 @@ export function drainTimerQueue(queue: TimerQueue): void {
|
|
|
53
53
|
queue.clear();
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
/** @internal */
|
|
57
|
-
export function clearAndRemoveTimerFrom(timer: NodeJS.Timeout, timers: TimerQueue): boolean {
|
|
58
|
-
clearTimeout(timer);
|
|
59
|
-
return timers.delete(timer);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
56
|
/** @public */
|
|
63
57
|
export interface ClusterTime {
|
|
64
58
|
clusterTime: Timestamp;
|
package/src/sdam/events.ts
CHANGED
|
@@ -153,10 +153,10 @@ export class ServerHeartbeatSucceededEvent {
|
|
|
153
153
|
reply: Document;
|
|
154
154
|
|
|
155
155
|
/** @internal */
|
|
156
|
-
constructor(connectionId: string, duration: number, reply: Document) {
|
|
156
|
+
constructor(connectionId: string, duration: number, reply: Document | null) {
|
|
157
157
|
this.connectionId = connectionId;
|
|
158
158
|
this.duration = duration;
|
|
159
|
-
this.reply = reply;
|
|
159
|
+
this.reply = reply ?? {};
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
|
package/src/sdam/monitor.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Document, Long } from '../bson';
|
|
|
2
2
|
import { connect } from '../cmap/connect';
|
|
3
3
|
import { Connection, ConnectionOptions } from '../cmap/connection';
|
|
4
4
|
import { LEGACY_HELLO_COMMAND } from '../constants';
|
|
5
|
-
import {
|
|
5
|
+
import { MongoNetworkError } from '../error';
|
|
6
6
|
import { CancellationToken, TypedEventEmitter } from '../mongo_types';
|
|
7
7
|
import type { Callback, InterruptibleAsyncInterval } from '../utils';
|
|
8
8
|
import {
|
|
@@ -207,11 +207,11 @@ function resetMonitorState(monitor: Monitor) {
|
|
|
207
207
|
monitor[kConnection] = undefined;
|
|
208
208
|
}
|
|
209
209
|
|
|
210
|
-
function checkServer(monitor: Monitor, callback: Callback<Document>) {
|
|
210
|
+
function checkServer(monitor: Monitor, callback: Callback<Document | null>) {
|
|
211
211
|
let start = now();
|
|
212
212
|
monitor.emit(Server.SERVER_HEARTBEAT_STARTED, new ServerHeartbeatStartedEvent(monitor.address));
|
|
213
213
|
|
|
214
|
-
function failureHandler(err:
|
|
214
|
+
function failureHandler(err: Error) {
|
|
215
215
|
monitor[kConnection]?.destroy({ force: true });
|
|
216
216
|
monitor[kConnection] = undefined;
|
|
217
217
|
|
|
@@ -259,8 +259,7 @@ function checkServer(monitor: Monitor, callback: Callback<Document>) {
|
|
|
259
259
|
|
|
260
260
|
connection.command(ns('admin.$cmd'), cmd, options, (err, hello) => {
|
|
261
261
|
if (err) {
|
|
262
|
-
failureHandler(err);
|
|
263
|
-
return;
|
|
262
|
+
return failureHandler(err);
|
|
264
263
|
}
|
|
265
264
|
|
|
266
265
|
if (!('isWritablePrimary' in hello)) {
|
package/src/sdam/server.ts
CHANGED
|
@@ -27,14 +27,16 @@ import type { AutoEncrypter } from '../deps';
|
|
|
27
27
|
import {
|
|
28
28
|
isNetworkErrorBeforeHandshake,
|
|
29
29
|
isNodeShuttingDownError,
|
|
30
|
-
isRetryableWriteError,
|
|
31
30
|
isSDAMUnrecoverableError,
|
|
32
31
|
MongoCompatibilityError,
|
|
33
32
|
MongoError,
|
|
33
|
+
MongoErrorLabel,
|
|
34
34
|
MongoInvalidArgumentError,
|
|
35
35
|
MongoNetworkError,
|
|
36
36
|
MongoNetworkTimeoutError,
|
|
37
|
-
MongoServerClosedError
|
|
37
|
+
MongoServerClosedError,
|
|
38
|
+
MongoUnexpectedServerResponseError,
|
|
39
|
+
needsRetryableWriteLabel
|
|
38
40
|
} from '../error';
|
|
39
41
|
import { Logger } from '../logger';
|
|
40
42
|
import type { ServerApi } from '../mongo_client';
|
|
@@ -43,7 +45,6 @@ import type { ClientSession } from '../sessions';
|
|
|
43
45
|
import { isTransactionCommand } from '../transactions';
|
|
44
46
|
import {
|
|
45
47
|
Callback,
|
|
46
|
-
CallbackWithType,
|
|
47
48
|
collationNotSupported,
|
|
48
49
|
EventEmitterWithState,
|
|
49
50
|
makeStateMachine,
|
|
@@ -119,7 +120,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
|
|
|
119
120
|
s: ServerPrivate;
|
|
120
121
|
serverApi?: ServerApi;
|
|
121
122
|
hello?: Document;
|
|
122
|
-
[kMonitor]: Monitor;
|
|
123
|
+
[kMonitor]: Monitor | null;
|
|
123
124
|
|
|
124
125
|
/** @event */
|
|
125
126
|
static readonly SERVER_HEARTBEAT_STARTED = SERVER_HEARTBEAT_STARTED;
|
|
@@ -163,22 +164,27 @@ export class Server extends TypedEventEmitter<ServerEvents> {
|
|
|
163
164
|
this.clusterTime = clusterTime;
|
|
164
165
|
});
|
|
165
166
|
|
|
166
|
-
|
|
167
|
-
|
|
167
|
+
if (this.loadBalanced) {
|
|
168
|
+
this[kMonitor] = null;
|
|
169
|
+
// monitoring is disabled in load balancing mode
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
168
172
|
|
|
169
173
|
// create the monitor
|
|
170
|
-
|
|
174
|
+
// TODO(NODE-4144): Remove new variable for type narrowing
|
|
175
|
+
const monitor = new Monitor(this, this.s.options);
|
|
176
|
+
this[kMonitor] = monitor;
|
|
171
177
|
|
|
172
178
|
for (const event of HEARTBEAT_EVENTS) {
|
|
173
|
-
|
|
179
|
+
monitor.on(event, (e: any) => this.emit(event, e));
|
|
174
180
|
}
|
|
175
181
|
|
|
176
|
-
|
|
182
|
+
monitor.on('resetConnectionPool', () => {
|
|
177
183
|
this.s.pool.clear();
|
|
178
184
|
});
|
|
179
185
|
|
|
180
|
-
|
|
181
|
-
|
|
186
|
+
monitor.on('resetServer', (error: MongoError) => markServerUnknown(this, error));
|
|
187
|
+
monitor.on(Server.SERVER_HEARTBEAT_SUCCEEDED, (event: ServerHeartbeatSucceededEvent) => {
|
|
182
188
|
this.emit(
|
|
183
189
|
Server.DESCRIPTION_RECEIVED,
|
|
184
190
|
new ServerDescription(this.description.hostAddress, event.reply, {
|
|
@@ -213,6 +219,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
|
|
|
213
219
|
if (this.s.options && this.s.options.autoEncrypter) {
|
|
214
220
|
return this.s.options.autoEncrypter;
|
|
215
221
|
}
|
|
222
|
+
return;
|
|
216
223
|
}
|
|
217
224
|
|
|
218
225
|
get loadBalanced(): boolean {
|
|
@@ -233,7 +240,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
|
|
|
233
240
|
// a load balancer. It never transitions out of this state and
|
|
234
241
|
// has no monitor.
|
|
235
242
|
if (!this.loadBalanced) {
|
|
236
|
-
this[kMonitor]
|
|
243
|
+
this[kMonitor]?.connect();
|
|
237
244
|
} else {
|
|
238
245
|
stateTransition(this, STATE_CONNECTED);
|
|
239
246
|
this.emit(Server.CONNECT, this);
|
|
@@ -256,7 +263,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
|
|
|
256
263
|
stateTransition(this, STATE_CLOSING);
|
|
257
264
|
|
|
258
265
|
if (!this.loadBalanced) {
|
|
259
|
-
this[kMonitor]
|
|
266
|
+
this[kMonitor]?.close();
|
|
260
267
|
}
|
|
261
268
|
|
|
262
269
|
this.s.pool.close(options, err => {
|
|
@@ -274,7 +281,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
|
|
|
274
281
|
*/
|
|
275
282
|
requestCheck(): void {
|
|
276
283
|
if (!this.loadBalanced) {
|
|
277
|
-
this[kMonitor]
|
|
284
|
+
this[kMonitor]?.requestCheck();
|
|
278
285
|
}
|
|
279
286
|
}
|
|
280
287
|
|
|
@@ -282,24 +289,12 @@ export class Server extends TypedEventEmitter<ServerEvents> {
|
|
|
282
289
|
* Execute a command
|
|
283
290
|
* @internal
|
|
284
291
|
*/
|
|
285
|
-
command(ns: MongoDBNamespace, cmd: Document, callback: Callback): void;
|
|
286
|
-
/** @internal */
|
|
287
292
|
command(
|
|
288
293
|
ns: MongoDBNamespace,
|
|
289
294
|
cmd: Document,
|
|
290
295
|
options: CommandOptions,
|
|
291
296
|
callback: Callback<Document>
|
|
292
|
-
): void;
|
|
293
|
-
command(
|
|
294
|
-
ns: MongoDBNamespace,
|
|
295
|
-
cmd: Document,
|
|
296
|
-
options?: CommandOptions | Callback<Document>,
|
|
297
|
-
callback?: Callback<Document>
|
|
298
297
|
): void {
|
|
299
|
-
if (typeof options === 'function') {
|
|
300
|
-
(callback = options), (options = {}), (options = options ?? {});
|
|
301
|
-
}
|
|
302
|
-
|
|
303
298
|
if (callback == null) {
|
|
304
299
|
throw new MongoInvalidArgumentError('Callback must be provided');
|
|
305
300
|
}
|
|
@@ -344,7 +339,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
|
|
|
344
339
|
}
|
|
345
340
|
|
|
346
341
|
session.pin(checkedOut);
|
|
347
|
-
this.command(ns, cmd, finalOptions, callback
|
|
342
|
+
this.command(ns, cmd, finalOptions, callback);
|
|
348
343
|
});
|
|
349
344
|
|
|
350
345
|
return;
|
|
@@ -362,7 +357,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
|
|
|
362
357
|
ns,
|
|
363
358
|
cmd,
|
|
364
359
|
finalOptions,
|
|
365
|
-
makeOperationHandler(this, conn, cmd, finalOptions, cb)
|
|
360
|
+
makeOperationHandler(this, conn, cmd, finalOptions, cb)
|
|
366
361
|
);
|
|
367
362
|
},
|
|
368
363
|
callback
|
|
@@ -387,12 +382,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
|
|
|
387
382
|
return cb(err);
|
|
388
383
|
}
|
|
389
384
|
|
|
390
|
-
conn.query(
|
|
391
|
-
ns,
|
|
392
|
-
cmd,
|
|
393
|
-
options,
|
|
394
|
-
makeOperationHandler(this, conn, cmd, options, cb) as Callback
|
|
395
|
-
);
|
|
385
|
+
conn.query(ns, cmd, options, makeOperationHandler(this, conn, cmd, options, cb));
|
|
396
386
|
},
|
|
397
387
|
callback
|
|
398
388
|
);
|
|
@@ -421,12 +411,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
|
|
|
421
411
|
return cb(err);
|
|
422
412
|
}
|
|
423
413
|
|
|
424
|
-
conn.getMore(
|
|
425
|
-
ns,
|
|
426
|
-
cursorId,
|
|
427
|
-
options,
|
|
428
|
-
makeOperationHandler(this, conn, {}, options, cb) as Callback
|
|
429
|
-
);
|
|
414
|
+
conn.getMore(ns, cursorId, options, makeOperationHandler(this, conn, {}, options, cb));
|
|
430
415
|
},
|
|
431
416
|
callback
|
|
432
417
|
);
|
|
@@ -462,7 +447,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
|
|
|
462
447
|
ns,
|
|
463
448
|
cursorIds,
|
|
464
449
|
options,
|
|
465
|
-
makeOperationHandler(this, conn, {}, undefined, cb)
|
|
450
|
+
makeOperationHandler(this, conn, {}, undefined, cb)
|
|
466
451
|
);
|
|
467
452
|
},
|
|
468
453
|
callback
|
|
@@ -486,7 +471,7 @@ function markServerUnknown(server: Server, error?: MongoError) {
|
|
|
486
471
|
}
|
|
487
472
|
|
|
488
473
|
if (error instanceof MongoNetworkError && !(error instanceof MongoNetworkTimeoutError)) {
|
|
489
|
-
server[kMonitor]
|
|
474
|
+
server[kMonitor]?.reset();
|
|
490
475
|
}
|
|
491
476
|
|
|
492
477
|
server.emit(
|
|
@@ -546,67 +531,87 @@ function makeOperationHandler(
|
|
|
546
531
|
cmd: Document,
|
|
547
532
|
options: CommandOptions | GetMoreOptions | undefined,
|
|
548
533
|
callback: Callback
|
|
549
|
-
):
|
|
534
|
+
): Callback {
|
|
550
535
|
const session = options?.session;
|
|
551
|
-
return function handleOperationResult(
|
|
552
|
-
if (
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
session.serverSession.isDirty = true;
|
|
556
|
-
}
|
|
536
|
+
return function handleOperationResult(error, result) {
|
|
537
|
+
if (result != null) {
|
|
538
|
+
return callback(undefined, result);
|
|
539
|
+
}
|
|
557
540
|
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
}
|
|
541
|
+
if (!error) {
|
|
542
|
+
return callback(new MongoUnexpectedServerResponseError('Empty response with no error'));
|
|
543
|
+
}
|
|
562
544
|
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
) {
|
|
568
|
-
err.addErrorLabel('RetryableWriteError');
|
|
569
|
-
}
|
|
545
|
+
if (!(error instanceof MongoError)) {
|
|
546
|
+
// Node.js or some other error we have not special handling for
|
|
547
|
+
return callback(error);
|
|
548
|
+
}
|
|
570
549
|
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
550
|
+
if (connectionIsStale(server.s.pool, connection)) {
|
|
551
|
+
return callback(error);
|
|
552
|
+
}
|
|
574
553
|
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
554
|
+
if (error instanceof MongoNetworkError) {
|
|
555
|
+
if (session && !session.hasEnded && session.serverSession) {
|
|
556
|
+
session.serverSession.isDirty = true;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// inActiveTransaction check handles commit and abort.
|
|
560
|
+
if (
|
|
561
|
+
inActiveTransaction(session, cmd) &&
|
|
562
|
+
!error.hasErrorLabel(MongoErrorLabel.TransientTransactionError)
|
|
563
|
+
) {
|
|
564
|
+
error.addErrorLabel(MongoErrorLabel.TransientTransactionError);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if (
|
|
568
|
+
(isRetryableWritesEnabled(server.s.topology) || isTransactionCommand(cmd)) &&
|
|
569
|
+
supportsRetryableWrites(server) &&
|
|
570
|
+
!inActiveTransaction(session, cmd)
|
|
571
|
+
) {
|
|
572
|
+
error.addErrorLabel(MongoErrorLabel.RetryableWriteError);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (!(error instanceof MongoNetworkTimeoutError) || isNetworkErrorBeforeHandshake(error)) {
|
|
576
|
+
// In load balanced mode we never mark the server as unknown and always
|
|
577
|
+
// clear for the specific service id.
|
|
578
|
+
|
|
579
|
+
server.s.pool.clear(connection.serviceId);
|
|
580
|
+
if (!server.loadBalanced) {
|
|
581
|
+
markServerUnknown(server, error);
|
|
589
582
|
}
|
|
583
|
+
}
|
|
584
|
+
} else {
|
|
585
|
+
if (
|
|
586
|
+
(isRetryableWritesEnabled(server.s.topology) || isTransactionCommand(cmd)) &&
|
|
587
|
+
needsRetryableWriteLabel(error, maxWireVersion(server)) &&
|
|
588
|
+
!inActiveTransaction(session, cmd)
|
|
589
|
+
) {
|
|
590
|
+
error.addErrorLabel(MongoErrorLabel.RetryableWriteError);
|
|
591
|
+
}
|
|
590
592
|
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
593
|
+
if (isSDAMUnrecoverableError(error)) {
|
|
594
|
+
if (shouldHandleStateChangeError(server, error)) {
|
|
595
|
+
if (maxWireVersion(server) <= 7 || isNodeShuttingDownError(error)) {
|
|
596
|
+
server.s.pool.clear(connection.serviceId);
|
|
597
|
+
}
|
|
596
598
|
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
}
|
|
599
|
+
if (!server.loadBalanced) {
|
|
600
|
+
markServerUnknown(server, error);
|
|
601
|
+
process.nextTick(() => server.requestCheck());
|
|
601
602
|
}
|
|
602
603
|
}
|
|
603
604
|
}
|
|
605
|
+
}
|
|
604
606
|
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
607
|
+
if (
|
|
608
|
+
session &&
|
|
609
|
+
session.isPinned &&
|
|
610
|
+
error.hasErrorLabel(MongoErrorLabel.TransientTransactionError)
|
|
611
|
+
) {
|
|
612
|
+
session.unpin({ force: true });
|
|
608
613
|
}
|
|
609
614
|
|
|
610
|
-
callback(
|
|
615
|
+
return callback(error);
|
|
611
616
|
};
|
|
612
617
|
}
|