Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/mongodb-build-info/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"typescript": "^5.0.4"
},
"dependencies": {
"debug": "^4.4.0",
"mongodb-connection-string-url": "^3.0.0"
}
}
76 changes: 76 additions & 0 deletions packages/mongodb-build-info/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import ConnectionString from 'mongodb-connection-string-url';
import { debug as createDebug } from 'debug';

const debug = createDebug('mongodb-build-info');

type Document = Record<string, unknown>;

const ATLAS_REGEX = /\.mongodb(-dev|-qa|-stage)?\.net$/i;
const ATLAS_STREAM_REGEX = /^atlas-stream-.+/i;
Expand All @@ -7,6 +12,7 @@ const LOCALHOST_REGEX =
const DIGITAL_OCEAN_REGEX = /\.mongo\.ondigitalocean\.com$/i;
const COSMOS_DB_REGEX = /\.cosmos\.azure\.com$/i;
const DOCUMENT_DB_REGEX = /docdb(-elastic)?\.amazonaws\.com$/i;
const FIRESTORE_REGEX = /\.firestore.goog$/i;

function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
Expand Down Expand Up @@ -109,6 +115,9 @@ export function isDigitalOcean(uri: string): boolean {
return !!getHostnameFromUrl(uri).match(DIGITAL_OCEAN_REGEX);
}

/**
* @deprecated Use `identifyServerName` instead.
*/
export function getGenuineMongoDB(uri: string): {
isGenuine: boolean;
serverName: string;
Expand All @@ -134,6 +143,73 @@ export function getGenuineMongoDB(uri: string): {
};
}

type IdentifyServerNameOptions = {
connectionString: string;
adminCommand: (command: Document) => Promise<Document>;
};

/**
* Identify the server name based on connection string and server responses.
* @returns A name of the server, "unknown" if we fail to identify it.
*/
export async function identifyServerName({
connectionString,
adminCommand,
}: IdentifyServerNameOptions): Promise<string> {
try {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we short-circuit on mongodb.net hosts here to make sure we're not wasting time running these commands in web?

const hostname = getHostnameFromUrl(connectionString);
if (hostname.match(COSMOS_DB_REGEX)) {
return 'cosmosdb';
}

if (hostname.match(DOCUMENT_DB_REGEX)) {
return 'documentdb';
}

if (hostname.match(FIRESTORE_REGEX)) {
return 'firestore';
}

const candidates = await Promise.all([
adminCommand({ buildInfo: 1 }).then(
(response) => {
if ('ferretdb' in response) {
return ['ferretdb'];
} else {
return [];
}
},
(error: unknown) => {
debug('buildInfo command failed %O', error);
return [];
},
),
adminCommand({ getParameter: 'foo' }).then(
// A successful response doesn't represent a signal
() => [],
(error: unknown) => {
if (error instanceof Error && /documentdb_api/.test(error.message)) {
return ['pg_documentdb'];
} else {
return [];
}
},
),
]).then((results) => results.flat());

if (candidates.length === 0) {
return 'mongodb';
} else if (candidates.length === 1) {
return candidates[0];
} else {
return 'unknown';
}
} catch (error) {
debug('Failed to identify server name', error);
return 'unknown';
}
}

export function getBuildEnv(buildInfo: unknown): {
serverOs: string | null;
serverArch: string | null;
Expand Down
4 changes: 4 additions & 0 deletions packages/mongodb-build-info/test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export const DOCUMENT_DB_URIS = [
'mongodb://x:y@elastic-docdb-123456789.eu-central-1.docdb-elastic.amazonaws.com:27017',
];

export const FIRESTORE_URIS = [
'mongodb://x:y@bbccdaf5-527a-4be5-9881-b7073e92002b.europe-north2.firestore.goog:443/test-db?loadBalanced=true&tls=true&authMechanism=SCRAM-SHA-256&retryWrites=false',
];

export const COSMOSDB_BUILD_INFO = {
_t: 'BuildInfoResponse',
ok: 1,
Expand Down
72 changes: 72 additions & 0 deletions packages/mongodb-build-info/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';

import * as fixtures from './fixtures';
import {
isAtlas,
Expand All @@ -10,6 +11,7 @@ import {
getBuildEnv,
isEnterprise,
getGenuineMongoDB,
identifyServerName,
} from '../src/index';

describe('mongodb-build-info', function () {
Expand Down Expand Up @@ -428,4 +430,74 @@ describe('mongodb-build-info', function () {
expect(isGenuine.serverName).to.equal('mongodb');
});
});

context('identifyServerName', function () {
function fail() {
return Promise.reject(new Error('Should not be called'));
}

it('reports CosmosDB', async function () {
for (const connectionString of fixtures.COSMOS_DB_URI) {
const result = await identifyServerName({
connectionString,
adminCommand: fail,
});
expect(result).to.equal('cosmosdb');
}
});

it('reports DocumentDB', async function () {
for (const connectionString of fixtures.DOCUMENT_DB_URIS) {
const result = await identifyServerName({
connectionString,
adminCommand: fail,
});
expect(result).to.equal('documentdb');
}
});

it('reports Firestore', async function () {
for (const connectionString of fixtures.FIRESTORE_URIS) {
const result = await identifyServerName({
connectionString,
adminCommand: fail,
});
expect(result).to.equal('firestore');
}
});

it('reports FerretDB', async function () {
const result = await identifyServerName({
connectionString: '',
adminCommand(req) {
if ('buildInfo' in req) {
return Promise.resolve({
ferretdb: {},
});
} else {
return Promise.resolve({});
}
},
});
expect(result).to.equal('ferretdb');
});

it('reports PG DocumentDB', async function () {
const result = await identifyServerName({
connectionString: '',
adminCommand(req) {
if ('getParameter' in req) {
return Promise.reject(
new Error(
'function documentdb_api.get_parameter(boolean, boolean, text[]) does not exist',
),
);
} else {
return Promise.resolve({});
}
},
});
expect(result).to.equal('pg_documentdb');
});
});
});