Skip to content

Commit 73f6e71

Browse files
committed
Update signature of identifyServerName to take and options object and only adminCommand
1 parent 09c496b commit 73f6e71

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

packages/mongodb-build-info/src/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,20 @@ export function getGenuineMongoDB(uri: string): {
143143
};
144144
}
145145

146-
export async function identifyServerName(
147-
uri: string,
148-
runCommand: (command: Document) => Promise<Document>,
149-
adminCommand: (command: Document) => Promise<Document>,
150-
): Promise<string> {
151-
const hostname = getHostnameFromUrl(uri);
146+
type IdentifyServerNameOptions = {
147+
connectionString: string;
148+
adminCommand: (command: Document) => Promise<Document>;
149+
};
150+
151+
/**
152+
* Identify the server name based on connection string and server responses.
153+
* @returns A name of the server, "unknown" if we fail to identify it.
154+
*/
155+
export async function identifyServerName({
156+
connectionString,
157+
adminCommand,
158+
}: IdentifyServerNameOptions): Promise<string> {
159+
const hostname = getHostnameFromUrl(connectionString);
152160
if (hostname.match(COSMOS_DB_REGEX)) {
153161
return 'cosmosdb';
154162
}

packages/mongodb-build-info/test/index.spec.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -437,30 +437,39 @@ describe('mongodb-build-info', function () {
437437
}
438438

439439
it('reports CosmosDB', async function () {
440-
for (const uri of fixtures.COSMOS_DB_URI) {
441-
const result = await identifyServerName(uri, fail, fail);
440+
for (const connectionString of fixtures.COSMOS_DB_URI) {
441+
const result = await identifyServerName({
442+
connectionString,
443+
adminCommand: fail,
444+
});
442445
expect(result).to.equal('cosmosdb');
443446
}
444447
});
445448

446449
it('reports DocumentDB', async function () {
447-
for (const uri of fixtures.DOCUMENT_DB_URIS) {
448-
const result = await identifyServerName(uri, fail, fail);
450+
for (const connectionString of fixtures.DOCUMENT_DB_URIS) {
451+
const result = await identifyServerName({
452+
connectionString,
453+
adminCommand: fail,
454+
});
449455
expect(result).to.equal('documentdb');
450456
}
451457
});
452458

453459
it('reports Firestore', async function () {
454-
for (const uri of fixtures.FIRESTORE_URIS) {
455-
const result = await identifyServerName(uri, fail, fail);
460+
for (const connectionString of fixtures.FIRESTORE_URIS) {
461+
const result = await identifyServerName({
462+
connectionString,
463+
adminCommand: fail,
464+
});
456465
expect(result).to.equal('firestore');
457466
}
458467
});
459468

460469
it('reports FerretDB', async function () {
461-
const result = await identifyServerName(
462-
'',
463-
(req) => {
470+
const result = await identifyServerName({
471+
connectionString: '',
472+
adminCommand(req) {
464473
if ('buildInfo' in req) {
465474
return Promise.resolve({
466475
ferretdb: {},
@@ -469,16 +478,14 @@ describe('mongodb-build-info', function () {
469478
return Promise.resolve({});
470479
}
471480
},
472-
fail,
473-
);
481+
});
474482
expect(result).to.equal('ferretdb');
475483
});
476484

477485
it('reports PG DocumentDB', async function () {
478-
const result = await identifyServerName(
479-
'',
480-
() => Promise.resolve({}),
481-
(req) => {
486+
const result = await identifyServerName({
487+
connectionString: '',
488+
adminCommand(req) {
482489
if ('getParameter' in req) {
483490
return Promise.reject(
484491
new Error(
@@ -489,7 +496,7 @@ describe('mongodb-build-info', function () {
489496
return Promise.resolve({});
490497
}
491498
},
492-
);
499+
});
493500
expect(result).to.equal('pg_documentdb');
494501
});
495502
});

0 commit comments

Comments
 (0)