Skip to content

Commit 5c322b6

Browse files
authored
feat(NODE-4519): deprecate promiseLibrary and PromiseProvider (#3403)
1 parent 12e951b commit 5c322b6

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

src/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ export {
9797
Logger,
9898
MongoClient,
9999
OrderedBulkOperation,
100-
// Utils
101-
PromiseProvider as Promise,
102100
UnorderedBulkOperation
103101
};
104102

103+
// Deprecated, remove in next major
104+
export { PromiseProvider as Promise };
105+
105106
// enums
106107
export { BatchType } from './bulk/common';
107108
export { GSSAPICanonicalizationValue } from './cmap/auth/gssapi';

src/mongo_client.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@ export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeC
230230
raw?: boolean;
231231
/** A primary key factory function for generation of custom `_id` keys */
232232
pkFactory?: PkFactory;
233-
/** A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible */
233+
/**
234+
* A Promise library class the application wishes to use such as Bluebird, must be ES6 compatible
235+
* @deprecated Setting a custom promise library is deprecated the next major version will use the global Promise constructor only.
236+
*/
234237
promiseLibrary?: any;
235238
/** The logging level */
236239
loggerLevel?: LoggerLevel;

src/promise_provider.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,24 @@ const store: PromiseStore = {
1313

1414
/**
1515
* Global promise store allowing user-provided promises
16+
* @deprecated Setting a custom promise library is deprecated the next major version will use the global Promise constructor only.
1617
* @public
1718
*/
1819
export class PromiseProvider {
19-
/** Validates the passed in promise library */
20+
/**
21+
* Validates the passed in promise library
22+
* @deprecated Setting a custom promise library is deprecated the next major version will use the global Promise constructor only.
23+
*/
2024
static validate(lib: unknown): lib is PromiseConstructor {
2125
if (typeof lib !== 'function')
2226
throw new MongoInvalidArgumentError(`Promise must be a function, got ${lib}`);
2327
return !!lib;
2428
}
2529

26-
/** Sets the promise library */
30+
/**
31+
* Sets the promise library
32+
* @deprecated Setting a custom promise library is deprecated the next major version will use the global Promise constructor only.
33+
*/
2734
static set(lib: PromiseConstructor): void {
2835
if (!PromiseProvider.validate(lib)) {
2936
// validate
@@ -32,7 +39,10 @@ export class PromiseProvider {
3239
store[kPromise] = lib;
3340
}
3441

35-
/** Get the stored promise library, or resolves passed in */
42+
/**
43+
* Get the stored promise library, or resolves passed in
44+
* @deprecated Setting a custom promise library is deprecated the next major version will use the global Promise constructor only.
45+
*/
3646
static get(): PromiseConstructor {
3747
return store[kPromise] as PromiseConstructor;
3848
}

test/integration/node-specific/custom_promise_library.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22

3+
const { once } = require('events');
34
const { expect } = require('chai');
45
const { PromiseProvider } = require('../../../src/promise_provider');
6+
const { MongoClient } = require('../../../src/mongo_client');
57

68
class CustomPromise extends Promise {}
79
CustomPromise.prototype.isCustomMongo = true;
@@ -11,6 +13,13 @@ describe('Optional PromiseLibrary', function () {
1113
PromiseProvider.set(Promise);
1214
});
1315

16+
it('should emit a deprecation warning when a promiseLibrary is set', async () => {
17+
const willEmitWarning = once(process, 'warning');
18+
new MongoClient('mongodb://iLoveJavascript', { promiseLibrary: () => {} });
19+
const [warning] = await willEmitWarning;
20+
expect(warning).to.have.property('message', 'promiseLibrary is a deprecated option');
21+
});
22+
1423
it('should correctly implement custom dependency-less promise', function (done) {
1524
const getCustomPromise = v => new CustomPromise(resolve => resolve(v));
1625
const getNativePromise = v => new Promise(resolve => resolve(v));

test/types/mongodb.test-d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,20 @@ expectDeprecated(Db.prototype.unref);
2323
expectDeprecated(MongoDBDriver.ObjectID);
2424
expectNotDeprecated(MongoDBDriver.ObjectId);
2525

26+
// We cannot attach a deprecation tag to an export
27+
// We tried export const Promise = PromiseProvider;
28+
// but then api-extractor claims PromiseProvider is not exported
29+
// Instead we've deprecated all the methods on the class
30+
expectNotDeprecated(MongoDBDriver.Promise);
31+
expectDeprecated(MongoDBDriver.Promise.validate);
32+
expectDeprecated(MongoDBDriver.Promise.get);
33+
expectDeprecated(MongoDBDriver.Promise.set);
34+
2635
declare const options: MongoDBDriver.MongoClientOptions;
2736
expectDeprecated(options.w);
2837
expectDeprecated(options.journal);
2938
expectDeprecated(options.wtimeoutMS);
39+
expectDeprecated(options.promiseLibrary);
3040
expectNotDeprecated(options.writeConcern);
3141
expectType<WriteConcernSettings | WriteConcern | undefined>(options.writeConcern);
3242

0 commit comments

Comments
 (0)