Skip to content

Commit 9002ce2

Browse files
PR requested changes 0 :
1 parent 57036c0 commit 9002ce2

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

src/mongo_client.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -367,42 +367,40 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
367367
this.checkForNonGenuineHosts();
368368
}
369369

370+
DOCUMENT_DB_CHECK = /(\.docdb\.amazonaws\.com$)|(\.docdb-elastic\.amazonaws\.com$)/;
371+
COSMOS_DB_CHECK = /\.cosmos\.azure\.com$/;
372+
373+
static DOCUMENT_DB_MSG =
374+
'You appear to be connected to a DocumentDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/documentdb';
375+
static COSMOS_DB_MSG =
376+
'You appear to be connected to a CosmosDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb';
377+
378+
/** @internal */
379+
private isHostMatch(match: RegExp, host?: string): boolean {
380+
return host && match.test(host.toLowerCase()) ? true : false;
381+
}
382+
370383
/** @internal */
371384
private checkForNonGenuineHosts() {
372385
if (this.mongoLogger && this.mongoLogger.logDestination) {
373-
const documentDBHostnames = this[kOptions].hosts.filter(
374-
(hostAddress: HostAddress) =>
375-
hostAddress.host &&
376-
(hostAddress.host.toLowerCase().endsWith('.docdb.amazonaws.com') ||
377-
hostAddress.host.toLowerCase().endsWith('.docdb-elastic.amazonaws.com'))
386+
const documentDBHostnames = this[kOptions].hosts.filter((hostAddress: HostAddress) =>
387+
this.isHostMatch(this.DOCUMENT_DB_CHECK, hostAddress.host)
378388
);
379389

380-
const srvHostIsDocumentDB =
381-
this[kOptions].srvHost &&
382-
(this[kOptions].srvHost.toLowerCase().endsWith('.docdb.amazonaws.com') ||
383-
this[kOptions].srvHost.toLowerCase().endsWith('.docdb-elastic.amazonaws.com'));
390+
const srvHostIsDocumentDB = this.isHostMatch(this.DOCUMENT_DB_CHECK, this[kOptions].srvHost);
384391

385392
if (documentDBHostnames.length !== 0 || srvHostIsDocumentDB) {
386-
this.mongoLogger.info(
387-
'client',
388-
'You appear to be connected to a DocumentDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/documentdb'
389-
);
393+
this.mongoLogger.info('client', MongoClient.DOCUMENT_DB_MSG);
390394
}
391395

392-
const cosmosDBHostnames = this[kOptions].hosts.filter(
393-
(hostAddress: HostAddress) =>
394-
hostAddress.host && hostAddress.host.toLowerCase().endsWith('.cosmos.azure.com')
396+
const cosmosDBHostnames = this[kOptions].hosts.filter((hostAddress: HostAddress) =>
397+
this.isHostMatch(this.COSMOS_DB_CHECK, hostAddress.host)
395398
);
396399

397-
const srvHostIsCosmosDB =
398-
this[kOptions].srvHost &&
399-
this[kOptions].srvHost.toLowerCase().endsWith('.cosmos.azure.com');
400+
const srvHostIsCosmosDB = this.isHostMatch(this.COSMOS_DB_CHECK, this[kOptions].srvHost);
400401

401402
if (cosmosDBHostnames.length !== 0 || srvHostIsCosmosDB) {
402-
this.mongoLogger.info(
403-
'client',
404-
'You appear to be connected to a CosmosDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb'
405-
);
403+
this.mongoLogger.info('client', MongoClient.COSMOS_DB_MSG);
406404
}
407405
}
408406
}

test/unit/connection_string.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -894,10 +894,8 @@ describe('Connection String', function () {
894894
});
895895

896896
const loggerFeatureFlag = Symbol.for('@@mdb.enableMongoLogger');
897-
const docDBmsg =
898-
'You appear to be connected to a DocumentDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/documentdb';
899-
const cosmosDBmsg =
900-
'You appear to be connected to a CosmosDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb';
897+
const docDBmsg = MongoClient.DOCUMENT_DB_MSG;
898+
const cosmosDBmsg = MongoClient.COSMOS_DB_MSG;
901899
const test_cases = [
902900
['non-SRV example uri', 'mongodb://a.example.com:27017,b.example.com:27017/', ''],
903901
['non-SRV default uri', 'mongodb://a.mongodb.net:27017', ''],

0 commit comments

Comments
 (0)