From 97c15f47429acd325710a83c13740614f41ed322 Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Fri, 9 Sep 2022 10:48:37 -0400 Subject: [PATCH 1/5] feat(NODE-4519): deprecate promiseLibrary and check for emitted warning --- src/mongo_client.ts | 5 ++++- .../node-specific/custom_promise_library.test.js | 9 +++++++++ test/types/mongodb.test-d.ts | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/mongo_client.ts b/src/mongo_client.ts index d980f3e792..af178d0b55 100644 --- a/src/mongo_client.ts +++ b/src/mongo_client.ts @@ -230,7 +230,10 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC raw?: boolean; /** A primary key factory function for generation of custom `_id` keys */ pkFactory?: PkFactory; - /** A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible */ + /** + * A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible + * @deprecated Setting a custom promise library is deprecated the next major version will use the global Promise constructor only. + */ promiseLibrary?: any; /** The logging level */ loggerLevel?: LoggerLevel; diff --git a/test/integration/node-specific/custom_promise_library.test.js b/test/integration/node-specific/custom_promise_library.test.js index ed0a931004..2f663459b4 100644 --- a/test/integration/node-specific/custom_promise_library.test.js +++ b/test/integration/node-specific/custom_promise_library.test.js @@ -1,7 +1,9 @@ 'use strict'; +const { once } = require('events'); const { expect } = require('chai'); const { PromiseProvider } = require('../../../src/promise_provider'); +const { MongoClient } = require('../../../src/mongo_client'); class CustomPromise extends Promise {} CustomPromise.prototype.isCustomMongo = true; @@ -11,6 +13,13 @@ describe('Optional PromiseLibrary', function () { PromiseProvider.set(Promise); }); + it('should emit a deprecation warning when a promiseLibrary is set', async () => { + const willEmitWarning = once(process, 'warning'); + new MongoClient('mongodb://iLoveJavascript', { promiseLibrary: () => {} }); + const [warning] = await willEmitWarning; + expect(warning).to.have.property('message', 'promiseLibrary is a deprecated option'); + }); + it('should correctly implement custom dependency-less promise', function (done) { const getCustomPromise = v => new CustomPromise(resolve => resolve(v)); const getNativePromise = v => new Promise(resolve => resolve(v)); diff --git a/test/types/mongodb.test-d.ts b/test/types/mongodb.test-d.ts index 75c17ab3b0..7eeee78223 100644 --- a/test/types/mongodb.test-d.ts +++ b/test/types/mongodb.test-d.ts @@ -27,6 +27,7 @@ declare const options: MongoDBDriver.MongoClientOptions; expectDeprecated(options.w); expectDeprecated(options.journal); expectDeprecated(options.wtimeoutMS); +expectDeprecated(options.promiseLibrary); expectNotDeprecated(options.writeConcern); expectType(options.writeConcern); From 5725fe7de6d88051902ba423bdfb6d04876b07c9 Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Mon, 12 Sep 2022 10:22:17 -0400 Subject: [PATCH 2/5] deprecate promise provider --- src/index.ts | 5 +++-- src/promise_provider.ts | 1 + test/types/mongodb.test-d.ts | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 60ee8d107e..51144356b8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -97,11 +97,12 @@ export { Logger, MongoClient, OrderedBulkOperation, - // Utils - PromiseProvider as Promise, UnorderedBulkOperation }; +/** @deprecated Setting a custom promise library is deprecated the next major version will use the global Promise constructor only. */ +export const Promise = PromiseProvider; + // enums export { BatchType } from './bulk/common'; export { GSSAPICanonicalizationValue } from './cmap/auth/gssapi'; diff --git a/src/promise_provider.ts b/src/promise_provider.ts index a6aeb548eb..f09193f1fd 100644 --- a/src/promise_provider.ts +++ b/src/promise_provider.ts @@ -13,6 +13,7 @@ const store: PromiseStore = { /** * Global promise store allowing user-provided promises + * @deprecated Setting a custom promise library is deprecated the next major version will use the global Promise constructor only. * @public */ export class PromiseProvider { diff --git a/test/types/mongodb.test-d.ts b/test/types/mongodb.test-d.ts index 7eeee78223..36ea17dd3c 100644 --- a/test/types/mongodb.test-d.ts +++ b/test/types/mongodb.test-d.ts @@ -21,6 +21,7 @@ expectDeprecated(FindCursor.prototype.count); expectDeprecated(Topology.prototype.unref); expectDeprecated(Db.prototype.unref); expectDeprecated(MongoDBDriver.ObjectID); +expectDeprecated(MongoDBDriver.Promise); expectNotDeprecated(MongoDBDriver.ObjectId); declare const options: MongoDBDriver.MongoClientOptions; From a7ed660aafb3e84aaf7ac183d0d48a4e1d933d95 Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Mon, 12 Sep 2022 10:43:46 -0400 Subject: [PATCH 3/5] fix lint --- src/index.ts | 4 ++-- test/types/mongodb.test-d.ts | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 51144356b8..04bc2f9b2a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -100,8 +100,8 @@ export { UnorderedBulkOperation }; -/** @deprecated Setting a custom promise library is deprecated the next major version will use the global Promise constructor only. */ -export const Promise = PromiseProvider; +// Deprecated, remove in next major +export { PromiseProvider as Promise }; // enums export { BatchType } from './bulk/common'; diff --git a/test/types/mongodb.test-d.ts b/test/types/mongodb.test-d.ts index 36ea17dd3c..a6952201d8 100644 --- a/test/types/mongodb.test-d.ts +++ b/test/types/mongodb.test-d.ts @@ -21,9 +21,11 @@ expectDeprecated(FindCursor.prototype.count); expectDeprecated(Topology.prototype.unref); expectDeprecated(Db.prototype.unref); expectDeprecated(MongoDBDriver.ObjectID); -expectDeprecated(MongoDBDriver.Promise); expectNotDeprecated(MongoDBDriver.ObjectId); +// Unfortunately we cannot deprecate the export will also satisfying our tooling that requires every public symbol be exported +expectNotDeprecated(MongoDBDriver.Promise); + declare const options: MongoDBDriver.MongoClientOptions; expectDeprecated(options.w); expectDeprecated(options.journal); From 29df4bb731e4cf82db887f8e55dc0a0fed0980ce Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Mon, 12 Sep 2022 11:04:35 -0400 Subject: [PATCH 4/5] add more deprecations --- src/promise_provider.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/promise_provider.ts b/src/promise_provider.ts index f09193f1fd..575175ff5b 100644 --- a/src/promise_provider.ts +++ b/src/promise_provider.ts @@ -17,14 +17,20 @@ const store: PromiseStore = { * @public */ export class PromiseProvider { - /** Validates the passed in promise library */ + /** + * Validates the passed in promise library + * @deprecated Setting a custom promise library is deprecated the next major version will use the global Promise constructor only. + */ static validate(lib: unknown): lib is PromiseConstructor { if (typeof lib !== 'function') throw new MongoInvalidArgumentError(`Promise must be a function, got ${lib}`); return !!lib; } - /** Sets the promise library */ + /** + * Sets the promise library + * @deprecated Setting a custom promise library is deprecated the next major version will use the global Promise constructor only. + */ static set(lib: PromiseConstructor): void { if (!PromiseProvider.validate(lib)) { // validate @@ -33,7 +39,10 @@ export class PromiseProvider { store[kPromise] = lib; } - /** Get the stored promise library, or resolves passed in */ + /** + * Get the stored promise library, or resolves passed in + * @deprecated Setting a custom promise library is deprecated the next major version will use the global Promise constructor only. + */ static get(): PromiseConstructor { return store[kPromise] as PromiseConstructor; } From 624a1492b37b68e2fd7f85e7ee7b435ccebeb144 Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Mon, 12 Sep 2022 11:20:30 -0400 Subject: [PATCH 5/5] added more context --- test/types/mongodb.test-d.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/types/mongodb.test-d.ts b/test/types/mongodb.test-d.ts index a6952201d8..89e2371719 100644 --- a/test/types/mongodb.test-d.ts +++ b/test/types/mongodb.test-d.ts @@ -23,8 +23,14 @@ expectDeprecated(Db.prototype.unref); expectDeprecated(MongoDBDriver.ObjectID); expectNotDeprecated(MongoDBDriver.ObjectId); -// Unfortunately we cannot deprecate the export will also satisfying our tooling that requires every public symbol be exported +// We cannot attach a deprecation tag to an export +// We tried export const Promise = PromiseProvider; +// but then api-extractor claims PromiseProvider is not exported +// Instead we've deprecated all the methods on the class expectNotDeprecated(MongoDBDriver.Promise); +expectDeprecated(MongoDBDriver.Promise.validate); +expectDeprecated(MongoDBDriver.Promise.get); +expectDeprecated(MongoDBDriver.Promise.set); declare const options: MongoDBDriver.MongoClientOptions; expectDeprecated(options.w);