From 6c94b4a826f51796a23d26f0d1976e5dfcd88d88 Mon Sep 17 00:00:00 2001 From: Warren James <28974128+W-A-James@users.noreply.github.com> Date: Wed, 25 Jan 2023 11:32:15 -0500 Subject: [PATCH] feat(NODE-4992): Deprecate methods and options that reference legacy logger (#3532) --- src/db.ts | 6 +++++- src/gridfs/download.ts | 36 ++++++++++++++++++++---------------- src/gridfs/index.ts | 6 +++++- src/logger.ts | 20 ++++++++++++++++---- src/mongo_client.ts | 16 +++++++++++++--- 5 files changed, 59 insertions(+), 25 deletions(-) diff --git a/src/db.ts b/src/db.ts index c6ab0bdc8d..5e8d339c7b 100644 --- a/src/db.ts +++ b/src/db.ts @@ -774,11 +774,15 @@ export class Db { return new ChangeStream(this, pipeline, resolveOptions(this, options)); } - /** Return the db logger */ + /** + * Return the db logger + * @deprecated The Legacy Logger is deprecated and will be removed in the next major version. + */ getLogger(): Logger { return this.s.logger; } + /** @deprecated The Legacy Logger is deprecated and will be removed in the next major version. */ get logger(): Logger { return this.s.logger; } diff --git a/src/gridfs/download.ts b/src/gridfs/download.ts index 32946f6737..41a67d915d 100644 --- a/src/gridfs/download.ts +++ b/src/gridfs/download.ts @@ -12,7 +12,7 @@ import { import type { FindOptions } from '../operations/find'; import type { ReadPreference } from '../read_preference'; import type { Sort } from '../sort'; -import type { Callback } from '../utils'; +import { Callback, maybeCallback } from '../utils'; import type { GridFSChunk } from './upload'; /** @public */ @@ -185,22 +185,26 @@ export class GridFSBucketReadStream extends Readable implements NodeJS.ReadableS * * @param callback - called when the cursor is successfully closed or an error occurred. */ - abort(callback?: Callback): void { - this.push(null); - this.destroyed = true; - if (this.s.cursor) { - this.s.cursor.close(error => { - this.emit(GridFSBucketReadStream.CLOSE); - callback && callback(error); - }); - } else { - if (!this.s.init) { - // If not initialized, fire close event because we will never - // get a cursor - this.emit(GridFSBucketReadStream.CLOSE); + abort(): Promise; + /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ + abort(callback: Callback): void; + abort(callback?: Callback): Promise | void { + return maybeCallback(async () => { + this.push(null); + this.destroyed = true; + if (this.s.cursor) { + await this.s.cursor.close().catch(error => { + this.emit(GridFSBucketReadStream.CLOSE); + throw error; + }); + } else { + if (!this.s.init) { + // If not initialized, fire close event because we will never + // get a cursor + this.emit(GridFSBucketReadStream.CLOSE); + } } - callback && callback(); - } + }, callback); } } diff --git a/src/gridfs/index.ts b/src/gridfs/index.ts index 874762b013..ef23fbef49 100644 --- a/src/gridfs/index.ts +++ b/src/gridfs/index.ts @@ -226,7 +226,11 @@ export class GridFSBucket extends TypedEventEmitter { }, callback); } - /** Get the Db scoped logger. */ + /** + * Get the Db scoped logger. + * + * @deprecated Legacy Logger is deprecated and will be removed in the next major version. + */ getLogger(): Logger { return this.s.db.s.logger; } diff --git a/src/logger.ts b/src/logger.ts index ecbbe9cbfb..2e967f0fc3 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -15,7 +15,10 @@ const pid = process.pid; // eslint-disable-next-line no-console let currentLogger: LoggerFunction = console.warn; -/** @public */ +/** + * @public + * @deprecated The Legacy Logger is deprecated and will be removed in the next major version. + */ export const LoggerLevel = Object.freeze({ ERROR: 'error', WARN: 'warn', @@ -27,13 +30,22 @@ export const LoggerLevel = Object.freeze({ debug: 'debug' } as const); -/** @public */ +/** + * @public + * @deprecated The Legacy Logger is deprecated and will be removed in the next major version. + */ export type LoggerLevel = typeof LoggerLevel[keyof typeof LoggerLevel]; -/** @public */ +/** + * @public + * @deprecated The Legacy Logger is deprecated and will be removed in the next major version. + */ export type LoggerFunction = (message?: any, ...optionalParams: any[]) => void; -/** @public */ +/** + * @public + * @deprecated The Legacy Logger is deprecated and will be removed in the next major version. + */ export interface LoggerOptions { logger?: LoggerFunction; loggerLevel?: LoggerLevel; diff --git a/src/mongo_client.ts b/src/mongo_client.ts index 4d94d87747..77fc4953e1 100644 --- a/src/mongo_client.ts +++ b/src/mongo_client.ts @@ -233,9 +233,15 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC * @deprecated Setting a custom promise library is deprecated the next major version will use the global Promise constructor only. */ promiseLibrary?: any; - /** The logging level */ + /** + * The logging level + * @deprecated The Legacy Logger is deprecated and will be removed in the next major version. + */ loggerLevel?: LegacyLoggerLevel; - /** Custom logger object */ + /** + * Custom logger object + * @deprecated The Legacy Logger is deprecated and will be removed in the next major version. + */ logger?: LegacyLogger; /** Enable command monitoring for this client */ monitorCommands?: boolean; @@ -421,6 +427,7 @@ export class MongoClient extends TypedEventEmitter { return this.s.bsonOptions; } + /** @deprecated The Legacy Logger is deprecated and will be removed in the next major version. */ get logger(): LegacyLogger { return this.s.logger; } @@ -711,7 +718,10 @@ export class MongoClient extends TypedEventEmitter { return new ChangeStream(this, pipeline, resolveOptions(this, options)); } - /** Return the mongo client logger */ + /** + * Return the mongo client logger + * @deprecated The Legacy Logger is deprecated and will be removed in the next major version. + */ getLogger(): LegacyLogger { return this.s.logger; }