Skip to content

Commit 8fd1822

Browse files
nbbeekendurran
andauthored
refactor(NODE-4906): remove internal callback usage of public APIs (#3495)
Co-authored-by: Durran Jordan <[email protected]>
1 parent af36ebb commit 8fd1822

File tree

8 files changed

+84
-67
lines changed

8 files changed

+84
-67
lines changed

src/cursor/abstract_cursor.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,10 @@ function cleanupCursor(
841841

842842
if (session) {
843843
if (session.owner === cursor) {
844-
return session.endSession({ error }, callback);
844+
session.endSession({ error }).finally(() => {
845+
callback();
846+
});
847+
return;
845848
}
846849

847850
if (!session.inTransaction()) {
@@ -855,10 +858,11 @@ function cleanupCursor(
855858
function completeCleanup() {
856859
if (session) {
857860
if (session.owner === cursor) {
858-
return session.endSession({ error }, () => {
861+
session.endSession({ error }).finally(() => {
859862
cursor.emit(AbstractCursor.CLOSE);
860863
callback();
861864
});
865+
return;
862866
}
863867

864868
if (!session.inTransaction()) {
@@ -872,11 +876,13 @@ function cleanupCursor(
872876

873877
cursor[kKilled] = true;
874878

875-
return executeOperation(
879+
executeOperation(
876880
cursor[kClient],
877-
new KillCursorsOperation(cursorId, cursorNs, server, { session }),
878-
completeCleanup
879-
);
881+
new KillCursorsOperation(cursorId, cursorNs, server, { session })
882+
).finally(() => {
883+
completeCleanup();
884+
});
885+
return;
880886
}
881887

882888
/** @internal */

src/encrypter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
2+
23
import { deserialize, serialize } from './bson';
34
import { MONGO_CLIENT_EVENTS } from './constants';
45
import type { AutoEncrypter, AutoEncryptionOptions } from './deps';
@@ -114,7 +115,11 @@ export class Encrypter {
114115
this.autoEncrypter.teardown(!!force, e => {
115116
const internalClient = this[kInternalClient];
116117
if (internalClient != null && client !== internalClient) {
117-
return internalClient.close(force, callback);
118+
internalClient.close(force).then(
119+
() => callback(),
120+
error => callback(error)
121+
);
122+
return;
118123
}
119124
callback(e);
120125
});

src/operations/bulk_write.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,10 @@ export class BulkWriteOperation extends AbstractOperation<BulkWriteResult> {
5252
}
5353

5454
// Execute the bulk
55-
bulk.execute({ ...options, session }, (err, r) => {
56-
// We have connection level error
57-
if (!r && err) {
58-
return callback(err);
59-
}
60-
61-
// Return the results
62-
callback(undefined, r);
63-
});
55+
bulk.execute({ ...options, session }).then(
56+
result => callback(undefined, result),
57+
error => callback(error)
58+
);
6459
}
6560
}
6661

src/operations/collections.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,26 @@ export class CollectionsOperation extends AbstractOperation<Collection[]> {
2525
session: ClientSession | undefined,
2626
callback: Callback<Collection[]>
2727
): void {
28-
const db = this.db;
29-
3028
// Let's get the collection names
31-
db.listCollections(
32-
{},
33-
{ ...this.options, nameOnly: true, readPreference: this.readPreference, session }
34-
).toArray((err, documents) => {
35-
if (err || !documents) return callback(err);
36-
// Filter collections removing any illegal ones
37-
documents = documents.filter(doc => doc.name.indexOf('$') === -1);
38-
39-
// Return the collection objects
40-
callback(
41-
undefined,
42-
documents.map(d => {
43-
return new Collection(db, d.name, db.s.options);
44-
})
29+
this.db
30+
.listCollections(
31+
{},
32+
{ ...this.options, nameOnly: true, readPreference: this.readPreference, session }
33+
)
34+
.toArray()
35+
.then(
36+
documents => {
37+
const collections = [];
38+
for (const { name } of documents) {
39+
if (!name.includes('$')) {
40+
// Filter collections removing any illegal ones
41+
collections.push(new Collection(this.db, name, this.db.s.options));
42+
}
43+
}
44+
// Return the collection objects
45+
callback(undefined, collections);
46+
},
47+
error => callback(error)
4548
);
46-
});
4749
}
4850
}

src/operations/common_functions.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,15 @@ export function indexInformation(
6969
// Get the list of indexes of the specified collection
7070
db.collection(name)
7171
.listIndexes(options)
72-
.toArray((err, indexes) => {
73-
if (err) return callback(err);
74-
if (!Array.isArray(indexes)) return callback(undefined, []);
75-
if (full) return callback(undefined, indexes);
76-
callback(undefined, processResults(indexes));
77-
});
72+
.toArray()
73+
.then(
74+
indexes => {
75+
if (!Array.isArray(indexes)) return callback(undefined, []);
76+
if (full) return callback(undefined, indexes);
77+
callback(undefined, processResults(indexes));
78+
},
79+
error => callback(error)
80+
);
7881
}
7982

8083
export function prepareDocs(

src/operations/indexes.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Document } from '../bson';
22
import type { Collection } from '../collection';
33
import type { Db } from '../db';
4-
import { MongoCompatibilityError, MONGODB_ERROR_CODES, MongoServerError } from '../error';
4+
import { MongoCompatibilityError, MONGODB_ERROR_CODES, MongoError } from '../error';
55
import type { OneOrMore } from '../mongo_types';
66
import { ReadPreference } from '../read_preference';
77
import type { Server } from '../sdam/server';
@@ -320,22 +320,23 @@ export class EnsureIndexOperation extends CreateIndexOperation {
320320
override execute(server: Server, session: ClientSession | undefined, callback: Callback): void {
321321
const indexName = this.indexes[0].name;
322322
const cursor = this.db.collection(this.collectionName).listIndexes({ session });
323-
cursor.toArray((err, indexes) => {
324-
/// ignore "NamespaceNotFound" errors
325-
if (err && (err as MongoServerError).code !== MONGODB_ERROR_CODES.NamespaceNotFound) {
326-
return callback(err);
327-
}
328-
329-
if (indexes) {
323+
cursor.toArray().then(
324+
indexes => {
330325
indexes = Array.isArray(indexes) ? indexes : [indexes];
331326
if (indexes.some(index => index.name === indexName)) {
332327
callback(undefined, indexName);
333328
return;
334329
}
330+
super.execute(server, session, callback);
331+
},
332+
error => {
333+
if (error instanceof MongoError && error.code === MONGODB_ERROR_CODES.NamespaceNotFound) {
334+
// ignore "NamespaceNotFound" errors
335+
return super.execute(server, session, callback);
336+
}
337+
return callback(error);
335338
}
336-
337-
super.execute(server, session, callback);
338-
});
339+
);
339340
}
340341
}
341342

src/operations/is_capped.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ export class IsCappedOperation extends AbstractOperation<boolean> {
2828
{ name: coll.collectionName },
2929
{ ...this.options, nameOnly: false, readPreference: this.readPreference, session }
3030
)
31-
.toArray((err, collections) => {
32-
if (err || !collections) return callback(err);
33-
if (collections.length === 0) {
34-
// TODO(NODE-3485)
35-
return callback(new MongoAPIError(`collection ${coll.namespace} not found`));
36-
}
31+
.toArray()
32+
.then(
33+
collections => {
34+
if (collections.length === 0) {
35+
// TODO(NODE-3485)
36+
return callback(new MongoAPIError(`collection ${coll.namespace} not found`));
37+
}
3738

38-
const collOptions = collections[0].options;
39-
callback(undefined, !!(collOptions && collOptions.capped));
40-
});
39+
callback(undefined, !!collections[0].options?.capped);
40+
},
41+
error => callback(error)
42+
);
4143
}
4244
}

src/operations/options_operation.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ export class OptionsOperation extends AbstractOperation<Document> {
2929
{ name: coll.collectionName },
3030
{ ...this.options, nameOnly: false, readPreference: this.readPreference, session }
3131
)
32-
.toArray((err, collections) => {
33-
if (err || !collections) return callback(err);
34-
if (collections.length === 0) {
35-
// TODO(NODE-3485)
36-
return callback(new MongoAPIError(`collection ${coll.namespace} not found`));
37-
}
32+
.toArray()
33+
.then(
34+
collections => {
35+
if (collections.length === 0) {
36+
// TODO(NODE-3485)
37+
return callback(new MongoAPIError(`collection ${coll.namespace} not found`));
38+
}
3839

39-
callback(err, collections[0].options);
40-
});
40+
callback(undefined, collections[0].options);
41+
},
42+
error => callback(error)
43+
);
4144
}
4245
}

0 commit comments

Comments
 (0)