Skip to content

Commit 329f081

Browse files
fix(NODE-4074): ensure getTopology doesn't throw synchronously (#3172)
1 parent 799689e commit 329f081

File tree

17 files changed

+160
-90
lines changed

17 files changed

+160
-90
lines changed

src/admin.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
ValidateCollectionOperation,
1515
ValidateCollectionOptions
1616
} from './operations/validate_collection';
17-
import { Callback, getTopology } from './utils';
17+
import type { Callback } from './utils';
1818

1919
/** @internal */
2020
export interface AdminPrivate {
@@ -83,7 +83,7 @@ export class Admin {
8383
options = Object.assign({ dbName: 'admin' }, options);
8484

8585
return executeOperation(
86-
getTopology(this.s.db),
86+
this.s.db,
8787
new RunCommandOperation(this.s.db, command, options),
8888
callback
8989
);
@@ -207,7 +207,7 @@ export class Admin {
207207
options = Object.assign({ dbName: 'admin' }, options);
208208

209209
return executeOperation(
210-
getTopology(this.s.db),
210+
this.s.db,
211211
new AddUserOperation(this.s.db, username, password, options),
212212
callback
213213
);
@@ -233,7 +233,7 @@ export class Admin {
233233
options = Object.assign({ dbName: 'admin' }, options);
234234

235235
return executeOperation(
236-
getTopology(this.s.db),
236+
this.s.db,
237237
new RemoveUserOperation(this.s.db, username, options),
238238
callback
239239
);
@@ -263,7 +263,7 @@ export class Admin {
263263
options = options ?? {};
264264

265265
return executeOperation(
266-
getTopology(this.s.db),
266+
this.s.db,
267267
new ValidateCollectionOperation(this, collectionName, options),
268268
callback
269269
);
@@ -286,11 +286,7 @@ export class Admin {
286286
if (typeof options === 'function') (callback = options), (options = {});
287287
options = options ?? {};
288288

289-
return executeOperation(
290-
getTopology(this.s.db),
291-
new ListDatabasesOperation(this.s.db, options),
292-
callback
293-
);
289+
return executeOperation(this.s.db, new ListDatabasesOperation(this.s.db, options), callback);
294290
}
295291

296292
/**

src/bulk/common.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,19 +655,19 @@ function executeCommands(
655655
try {
656656
if (isInsertBatch(batch)) {
657657
executeOperation(
658-
bulkOperation.s.topology,
658+
bulkOperation.s.collection,
659659
new InsertOperation(bulkOperation.s.namespace, batch.operations, finalOptions),
660660
resultHandler
661661
);
662662
} else if (isUpdateBatch(batch)) {
663663
executeOperation(
664-
bulkOperation.s.topology,
664+
bulkOperation.s.collection,
665665
new UpdateOperation(bulkOperation.s.namespace, batch.operations, finalOptions),
666666
resultHandler
667667
);
668668
} else if (isDeleteBatch(batch)) {
669669
executeOperation(
670-
bulkOperation.s.topology,
670+
bulkOperation.s.collection,
671671
new DeleteOperation(bulkOperation.s.namespace, batch.operations, finalOptions),
672672
resultHandler
673673
);
@@ -1285,7 +1285,7 @@ export abstract class BulkOperationBase {
12851285
const finalOptions = { ...this.s.options, ...options };
12861286
const operation = new BulkWriteShimOperation(this, finalOptions);
12871287

1288-
return executeOperation(this.s.topology, operation, callback);
1288+
return executeOperation(this.s.collection, operation, callback);
12891289
}
12901290

12911291
/**

src/change_stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ export class ChangeStreamCursor<TSchema extends Document = Document> extends Abs
511511
session
512512
});
513513

514-
executeOperation(this.topology, aggregateOperation, (err, response) => {
514+
executeOperation(session, aggregateOperation, (err, response) => {
515515
if (err || response == null) {
516516
return callback(err);
517517
}

src/collection.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ export class Collection<TSchema extends Document = Document> {
296296
}
297297

298298
return executeOperation(
299-
getTopology(this),
299+
this,
300300
new InsertOneOperation(
301301
this as TODO_NODE_3286,
302302
doc,
@@ -338,7 +338,7 @@ export class Collection<TSchema extends Document = Document> {
338338
options = options ? Object.assign({}, options) : { ordered: true };
339339

340340
return executeOperation(
341-
getTopology(this),
341+
this,
342342
new InsertManyOperation(
343343
this as TODO_NODE_3286,
344344
docs,
@@ -406,7 +406,7 @@ export class Collection<TSchema extends Document = Document> {
406406
}
407407

408408
return executeOperation(
409-
getTopology(this),
409+
this,
410410
new BulkWriteOperation(
411411
this as TODO_NODE_3286,
412412
operations as TODO_NODE_3286,
@@ -453,7 +453,7 @@ export class Collection<TSchema extends Document = Document> {
453453
if (typeof options === 'function') (callback = options), (options = {});
454454

455455
return executeOperation(
456-
getTopology(this),
456+
this,
457457
new UpdateOneOperation(
458458
this as TODO_NODE_3286,
459459
filter,
@@ -501,7 +501,7 @@ export class Collection<TSchema extends Document = Document> {
501501
if (typeof options === 'function') (callback = options), (options = {});
502502

503503
return executeOperation(
504-
getTopology(this),
504+
this,
505505
new ReplaceOneOperation(
506506
this as TODO_NODE_3286,
507507
filter,
@@ -549,7 +549,7 @@ export class Collection<TSchema extends Document = Document> {
549549
if (typeof options === 'function') (callback = options), (options = {});
550550

551551
return executeOperation(
552-
getTopology(this),
552+
this,
553553
new UpdateManyOperation(
554554
this as TODO_NODE_3286,
555555
filter,
@@ -583,7 +583,7 @@ export class Collection<TSchema extends Document = Document> {
583583
if (typeof options === 'function') (callback = options), (options = {});
584584

585585
return executeOperation(
586-
getTopology(this),
586+
this,
587587
new DeleteOneOperation(this as TODO_NODE_3286, filter, resolveOptions(this, options)),
588588
callback
589589
);
@@ -623,7 +623,7 @@ export class Collection<TSchema extends Document = Document> {
623623
}
624624

625625
return executeOperation(
626-
getTopology(this),
626+
this,
627627
new DeleteManyOperation(this as TODO_NODE_3286, filter, resolveOptions(this, options)),
628628
callback
629629
);
@@ -652,7 +652,7 @@ export class Collection<TSchema extends Document = Document> {
652652

653653
// Intentionally, we do not inherit options from parent for this operation.
654654
return executeOperation(
655-
getTopology(this),
655+
this,
656656
new RenameOperation(this as TODO_NODE_3286, newName, {
657657
...options,
658658
readPreference: ReadPreference.PRIMARY
@@ -679,7 +679,7 @@ export class Collection<TSchema extends Document = Document> {
679679
options = options ?? {};
680680

681681
return executeOperation(
682-
getTopology(this),
682+
this,
683683
new DropCollectionOperation(this.s.db, this.collectionName, options),
684684
callback
685685
);
@@ -783,7 +783,7 @@ export class Collection<TSchema extends Document = Document> {
783783
if (typeof options === 'function') (callback = options), (options = {});
784784

785785
return executeOperation(
786-
getTopology(this),
786+
this,
787787
new OptionsOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
788788
callback
789789
);
@@ -806,7 +806,7 @@ export class Collection<TSchema extends Document = Document> {
806806
if (typeof options === 'function') (callback = options), (options = {});
807807

808808
return executeOperation(
809-
getTopology(this),
809+
this,
810810
new IsCappedOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
811811
callback
812812
);
@@ -857,7 +857,7 @@ export class Collection<TSchema extends Document = Document> {
857857
if (typeof options === 'function') (callback = options), (options = {});
858858

859859
return executeOperation(
860-
getTopology(this),
860+
this,
861861
new CreateIndexOperation(
862862
this as TODO_NODE_3286,
863863
this.collectionName,
@@ -918,7 +918,7 @@ export class Collection<TSchema extends Document = Document> {
918918
if (typeof options.maxTimeMS !== 'number') delete options.maxTimeMS;
919919

920920
return executeOperation(
921-
getTopology(this),
921+
this,
922922
new CreateIndexesOperation(
923923
this as TODO_NODE_3286,
924924
this.collectionName,
@@ -952,7 +952,7 @@ export class Collection<TSchema extends Document = Document> {
952952
options.readPreference = ReadPreference.primary;
953953

954954
return executeOperation(
955-
getTopology(this),
955+
this,
956956
new DropIndexOperation(this as TODO_NODE_3286, indexName, options),
957957
callback
958958
);
@@ -975,7 +975,7 @@ export class Collection<TSchema extends Document = Document> {
975975
if (typeof options === 'function') (callback = options), (options = {});
976976

977977
return executeOperation(
978-
getTopology(this),
978+
this,
979979
new DropIndexesOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
980980
callback
981981
);
@@ -1013,7 +1013,7 @@ export class Collection<TSchema extends Document = Document> {
10131013
if (typeof options === 'function') (callback = options), (options = {});
10141014

10151015
return executeOperation(
1016-
getTopology(this),
1016+
this,
10171017
new IndexExistsOperation(this as TODO_NODE_3286, indexes, resolveOptions(this, options)),
10181018
callback
10191019
);
@@ -1036,7 +1036,7 @@ export class Collection<TSchema extends Document = Document> {
10361036
if (typeof options === 'function') (callback = options), (options = {});
10371037

10381038
return executeOperation(
1039-
getTopology(this),
1039+
this,
10401040
new IndexInformationOperation(this.s.db, this.collectionName, resolveOptions(this, options)),
10411041
callback
10421042
);
@@ -1058,7 +1058,7 @@ export class Collection<TSchema extends Document = Document> {
10581058
): Promise<number> | void {
10591059
if (typeof options === 'function') (callback = options), (options = {});
10601060
return executeOperation(
1061-
getTopology(this),
1061+
this,
10621062
new EstimatedDocumentCountOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
10631063
callback
10641064
);
@@ -1118,7 +1118,7 @@ export class Collection<TSchema extends Document = Document> {
11181118

11191119
filter ??= {};
11201120
return executeOperation(
1121-
getTopology(this),
1121+
this,
11221122
new CountDocumentsOperation(
11231123
this as TODO_NODE_3286,
11241124
filter as Document,
@@ -1193,7 +1193,7 @@ export class Collection<TSchema extends Document = Document> {
11931193

11941194
filter ??= {};
11951195
return executeOperation(
1196-
getTopology(this),
1196+
this,
11971197
new DistinctOperation(
11981198
this as TODO_NODE_3286,
11991199
key as TODO_NODE_3286,
@@ -1221,7 +1221,7 @@ export class Collection<TSchema extends Document = Document> {
12211221
if (typeof options === 'function') (callback = options), (options = {});
12221222

12231223
return executeOperation(
1224-
getTopology(this),
1224+
this,
12251225
new IndexesOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
12261226
callback
12271227
);
@@ -1245,7 +1245,7 @@ export class Collection<TSchema extends Document = Document> {
12451245
options = options ?? {};
12461246

12471247
return executeOperation(
1248-
getTopology(this),
1248+
this,
12491249
new CollStatsOperation(this as TODO_NODE_3286, options),
12501250
callback
12511251
);
@@ -1277,7 +1277,7 @@ export class Collection<TSchema extends Document = Document> {
12771277
if (typeof options === 'function') (callback = options), (options = {});
12781278

12791279
return executeOperation(
1280-
getTopology(this),
1280+
this,
12811281
new FindOneAndDeleteOperation(
12821282
this as TODO_NODE_3286,
12831283
filter,
@@ -1324,7 +1324,7 @@ export class Collection<TSchema extends Document = Document> {
13241324
if (typeof options === 'function') (callback = options), (options = {});
13251325

13261326
return executeOperation(
1327-
getTopology(this),
1327+
this,
13281328
new FindOneAndReplaceOperation(
13291329
this as TODO_NODE_3286,
13301330
filter,
@@ -1372,7 +1372,7 @@ export class Collection<TSchema extends Document = Document> {
13721372
if (typeof options === 'function') (callback = options), (options = {});
13731373

13741374
return executeOperation(
1375-
getTopology(this),
1375+
this,
13761376
new FindOneAndUpdateOperation(
13771377
this as TODO_NODE_3286,
13781378
filter,
@@ -1495,7 +1495,7 @@ export class Collection<TSchema extends Document = Document> {
14951495
}
14961496

14971497
return executeOperation(
1498-
getTopology(this),
1498+
this,
14991499
new MapReduceOperation(
15001500
this as TODO_NODE_3286,
15011501
map,
@@ -1636,7 +1636,7 @@ export class Collection<TSchema extends Document = Document> {
16361636

16371637
filter ??= {};
16381638
return executeOperation(
1639-
getTopology(this),
1639+
this,
16401640
new CountOperation(
16411641
MongoDBNamespace.fromString(this.namespace),
16421642
filter,

src/cursor/abstract_cursor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ export abstract class AbstractCursor<
618618
batchSize
619619
});
620620

621-
executeOperation(this.topology, getMoreOperation, callback);
621+
executeOperation(this, getMoreOperation, callback);
622622
}
623623
}
624624

src/cursor/aggregation_cursor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchem
6868
session
6969
});
7070

71-
executeOperation(this.topology, aggregateOperation, (err, response) => {
71+
executeOperation(this, aggregateOperation, (err, response) => {
7272
if (err || response == null) return callback(err);
7373

7474
// TODO: NODE-2882
@@ -88,7 +88,7 @@ export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchem
8888
if (verbosity == null) verbosity = true;
8989

9090
return executeOperation(
91-
this.topology,
91+
this,
9292
new AggregateOperation(this.namespace, this[kPipeline], {
9393
...this[kOptions], // NOTE: order matters here, we may need to refine this
9494
...this.cursorOptions,

0 commit comments

Comments
 (0)