diff --git a/src/cursor/abstract_cursor.ts b/src/cursor/abstract_cursor.ts index 95b8bdaef7c..de3a0cb9775 100644 --- a/src/cursor/abstract_cursor.ts +++ b/src/cursor/abstract_cursor.ts @@ -841,7 +841,10 @@ function cleanupCursor( if (session) { if (session.owner === cursor) { - return session.endSession({ error }, callback); + session.endSession({ error }).finally(() => { + callback(); + }); + return; } if (!session.inTransaction()) { @@ -855,10 +858,11 @@ function cleanupCursor( function completeCleanup() { if (session) { if (session.owner === cursor) { - return session.endSession({ error }, () => { + session.endSession({ error }).finally(() => { cursor.emit(AbstractCursor.CLOSE); callback(); }); + return; } if (!session.inTransaction()) { @@ -872,11 +876,13 @@ function cleanupCursor( cursor[kKilled] = true; - return executeOperation( + executeOperation( cursor[kClient], - new KillCursorsOperation(cursorId, cursorNs, server, { session }), - completeCleanup - ); + new KillCursorsOperation(cursorId, cursorNs, server, { session }) + ).finally(() => { + completeCleanup(); + }); + return; } /** @internal */ diff --git a/src/encrypter.ts b/src/encrypter.ts index 8fe18aa3675..ee95f0ba1bf 100644 --- a/src/encrypter.ts +++ b/src/encrypter.ts @@ -1,4 +1,5 @@ /* eslint-disable @typescript-eslint/no-var-requires */ + import { deserialize, serialize } from './bson'; import { MONGO_CLIENT_EVENTS } from './constants'; import type { AutoEncrypter, AutoEncryptionOptions } from './deps'; @@ -114,7 +115,11 @@ export class Encrypter { this.autoEncrypter.teardown(!!force, e => { const internalClient = this[kInternalClient]; if (internalClient != null && client !== internalClient) { - return internalClient.close(force, callback); + internalClient.close(force).then( + () => callback(), + error => callback(error) + ); + return; } callback(e); }); diff --git a/src/operations/bulk_write.ts b/src/operations/bulk_write.ts index 627dc709a70..f56fd8c5a17 100644 --- a/src/operations/bulk_write.ts +++ b/src/operations/bulk_write.ts @@ -52,15 +52,10 @@ export class BulkWriteOperation extends AbstractOperation { } // Execute the bulk - bulk.execute({ ...options, session }, (err, r) => { - // We have connection level error - if (!r && err) { - return callback(err); - } - - // Return the results - callback(undefined, r); - }); + bulk.execute({ ...options, session }).then( + result => callback(undefined, result), + error => callback(error) + ); } } diff --git a/src/operations/collections.ts b/src/operations/collections.ts index 8b314865a92..14a1ccbb3c7 100644 --- a/src/operations/collections.ts +++ b/src/operations/collections.ts @@ -25,24 +25,26 @@ export class CollectionsOperation extends AbstractOperation { session: ClientSession | undefined, callback: Callback ): void { - const db = this.db; - // Let's get the collection names - db.listCollections( - {}, - { ...this.options, nameOnly: true, readPreference: this.readPreference, session } - ).toArray((err, documents) => { - if (err || !documents) return callback(err); - // Filter collections removing any illegal ones - documents = documents.filter(doc => doc.name.indexOf('$') === -1); - - // Return the collection objects - callback( - undefined, - documents.map(d => { - return new Collection(db, d.name, db.s.options); - }) + this.db + .listCollections( + {}, + { ...this.options, nameOnly: true, readPreference: this.readPreference, session } + ) + .toArray() + .then( + documents => { + const collections = []; + for (const { name } of documents) { + if (!name.includes('$')) { + // Filter collections removing any illegal ones + collections.push(new Collection(this.db, name, this.db.s.options)); + } + } + // Return the collection objects + callback(undefined, collections); + }, + error => callback(error) ); - }); } } diff --git a/src/operations/common_functions.ts b/src/operations/common_functions.ts index a439aca7eb4..63f7e1f2525 100644 --- a/src/operations/common_functions.ts +++ b/src/operations/common_functions.ts @@ -69,12 +69,15 @@ export function indexInformation( // Get the list of indexes of the specified collection db.collection(name) .listIndexes(options) - .toArray((err, indexes) => { - if (err) return callback(err); - if (!Array.isArray(indexes)) return callback(undefined, []); - if (full) return callback(undefined, indexes); - callback(undefined, processResults(indexes)); - }); + .toArray() + .then( + indexes => { + if (!Array.isArray(indexes)) return callback(undefined, []); + if (full) return callback(undefined, indexes); + callback(undefined, processResults(indexes)); + }, + error => callback(error) + ); } export function prepareDocs( diff --git a/src/operations/indexes.ts b/src/operations/indexes.ts index 3a3b72dc30c..8d095e16501 100644 --- a/src/operations/indexes.ts +++ b/src/operations/indexes.ts @@ -1,7 +1,7 @@ import type { Document } from '../bson'; import type { Collection } from '../collection'; import type { Db } from '../db'; -import { MongoCompatibilityError, MONGODB_ERROR_CODES, MongoServerError } from '../error'; +import { MongoCompatibilityError, MONGODB_ERROR_CODES, MongoError } from '../error'; import type { OneOrMore } from '../mongo_types'; import { ReadPreference } from '../read_preference'; import type { Server } from '../sdam/server'; @@ -320,22 +320,23 @@ export class EnsureIndexOperation extends CreateIndexOperation { override execute(server: Server, session: ClientSession | undefined, callback: Callback): void { const indexName = this.indexes[0].name; const cursor = this.db.collection(this.collectionName).listIndexes({ session }); - cursor.toArray((err, indexes) => { - /// ignore "NamespaceNotFound" errors - if (err && (err as MongoServerError).code !== MONGODB_ERROR_CODES.NamespaceNotFound) { - return callback(err); - } - - if (indexes) { + cursor.toArray().then( + indexes => { indexes = Array.isArray(indexes) ? indexes : [indexes]; if (indexes.some(index => index.name === indexName)) { callback(undefined, indexName); return; } + super.execute(server, session, callback); + }, + error => { + if (error instanceof MongoError && error.code === MONGODB_ERROR_CODES.NamespaceNotFound) { + // ignore "NamespaceNotFound" errors + return super.execute(server, session, callback); + } + return callback(error); } - - super.execute(server, session, callback); - }); + ); } } diff --git a/src/operations/is_capped.ts b/src/operations/is_capped.ts index 938c72f41cf..1219e79cf72 100644 --- a/src/operations/is_capped.ts +++ b/src/operations/is_capped.ts @@ -28,15 +28,17 @@ export class IsCappedOperation extends AbstractOperation { { name: coll.collectionName }, { ...this.options, nameOnly: false, readPreference: this.readPreference, session } ) - .toArray((err, collections) => { - if (err || !collections) return callback(err); - if (collections.length === 0) { - // TODO(NODE-3485) - return callback(new MongoAPIError(`collection ${coll.namespace} not found`)); - } + .toArray() + .then( + collections => { + if (collections.length === 0) { + // TODO(NODE-3485) + return callback(new MongoAPIError(`collection ${coll.namespace} not found`)); + } - const collOptions = collections[0].options; - callback(undefined, !!(collOptions && collOptions.capped)); - }); + callback(undefined, !!collections[0].options?.capped); + }, + error => callback(error) + ); } } diff --git a/src/operations/options_operation.ts b/src/operations/options_operation.ts index 27853d34edb..9c053938bf3 100644 --- a/src/operations/options_operation.ts +++ b/src/operations/options_operation.ts @@ -29,14 +29,17 @@ export class OptionsOperation extends AbstractOperation { { name: coll.collectionName }, { ...this.options, nameOnly: false, readPreference: this.readPreference, session } ) - .toArray((err, collections) => { - if (err || !collections) return callback(err); - if (collections.length === 0) { - // TODO(NODE-3485) - return callback(new MongoAPIError(`collection ${coll.namespace} not found`)); - } + .toArray() + .then( + collections => { + if (collections.length === 0) { + // TODO(NODE-3485) + return callback(new MongoAPIError(`collection ${coll.namespace} not found`)); + } - callback(err, collections[0].options); - }); + callback(undefined, collections[0].options); + }, + error => callback(error) + ); } }