Skip to content

Commit 4c31948

Browse files
committed
Make getConnectExtraInfo async
1 parent 02f8b80 commit 4c31948

File tree

2 files changed

+63
-59
lines changed

2 files changed

+63
-59
lines changed

packages/service-provider-core/src/connect-info.spec.ts

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { expect } from 'chai';
1+
import chai, { expect } from 'chai';
2+
import chaiAsPromised from 'chai-as-promised';
3+
4+
chai.use(chaiAsPromised);
5+
26
import getConnectExtraInfo from './connect-info';
37
import { ConnectionString } from 'mongodb-connection-string-url';
48

@@ -42,18 +46,15 @@ describe('getConnectInfo', function () {
4246
ok: 1,
4347
};
4448

45-
const ATLAS_VERSION = {
46-
atlasVersion: '20210330.0.0.1617063608',
47-
gitVersion: '8f7e5bdde713391e8123a463895bb7fb660a5ffd',
48-
};
49+
const ATLAS_VERSION = '20210330.0.0.1617063608';
4950

5051
const ATLAS_URI =
5152
'mongodb+srv://admin:[email protected]/admin';
5253

5354
const ATLAS_URI_WITH_AUTH =
5455
'mongodb+srv://admin:[email protected]/admin?authMechanism=PLAIN&authSource=%24external';
5556

56-
it('reports on an enterprise version >=3.2 of mongodb with credentials', function () {
57+
it('reports on an enterprise version >=3.2 of mongodb with credentials', async function () {
5758
const output = {
5859
is_atlas: true,
5960
is_atlas_url: true,
@@ -64,7 +65,7 @@ describe('getConnectInfo', function () {
6465
auth_type: 'PLAIN',
6566
is_data_federation: false,
6667
is_stream: false,
67-
dl_version: null,
68+
dl_version: undefined,
6869
atlas_version: '20210330.0.0.1617063608',
6970
is_genuine: true,
7071
non_genuine_server_name: 'mongodb',
@@ -74,18 +75,18 @@ describe('getConnectInfo', function () {
7475
uri: ATLAS_URI_WITH_AUTH,
7576
is_local_atlas: false,
7677
};
77-
expect(
78+
await expect(
7879
getConnectExtraInfo({
7980
connectionString: new ConnectionString(ATLAS_URI_WITH_AUTH),
80-
buildInfo: BUILD_INFO,
81-
atlasVersion: ATLAS_VERSION,
81+
buildInfo: Promise.resolve(BUILD_INFO),
82+
atlasVersion: Promise.resolve(ATLAS_VERSION),
8283
resolvedHostname: 'test-data-sets-00-02-a011bb.mongodb.net',
83-
isLocalAtlas: false,
84+
isLocalAtlas: Promise.resolve(false),
8485
})
85-
).to.deep.equal(output);
86+
).to.eventually.deep.equal(output);
8687
});
8788

88-
it('reports on an enterprise version >=3.2 of mongodb with no credentials', function () {
89+
it('reports on an enterprise version >=3.2 of mongodb with no credentials', async function () {
8990
const output = {
9091
is_atlas: true,
9192
is_atlas_url: true,
@@ -96,7 +97,7 @@ describe('getConnectInfo', function () {
9697
auth_type: undefined,
9798
is_data_federation: false,
9899
is_stream: false,
99-
dl_version: null,
100+
dl_version: undefined,
100101
atlas_version: '20210330.0.0.1617063608',
101102
is_genuine: true,
102103
non_genuine_server_name: 'mongodb',
@@ -106,18 +107,18 @@ describe('getConnectInfo', function () {
106107
uri: ATLAS_URI,
107108
is_local_atlas: false,
108109
};
109-
expect(
110+
await expect(
110111
getConnectExtraInfo({
111112
connectionString: new ConnectionString(ATLAS_URI),
112-
buildInfo: BUILD_INFO,
113-
atlasVersion: ATLAS_VERSION,
113+
buildInfo: Promise.resolve(BUILD_INFO),
114+
atlasVersion: Promise.resolve(ATLAS_VERSION),
114115
resolvedHostname: 'test-data-sets-00-02-a011bb.mongodb.net',
115-
isLocalAtlas: false,
116+
isLocalAtlas: Promise.resolve(false),
116117
})
117-
).to.deep.equal(output);
118+
).to.eventually.deep.equal(output);
118119
});
119120

120-
it('reports correct information when a stream uri is passed', function () {
121+
it('reports correct information when a stream uri is passed', async function () {
121122
const streamUri =
122123
'mongodb://atlas-stream-67b8e1cd6d60357be377be7b-1dekw.virginia-usa.a.query.mongodb-dev.net/';
123124
const output = {
@@ -130,8 +131,8 @@ describe('getConnectInfo', function () {
130131
auth_type: undefined,
131132
is_data_federation: false,
132133
is_stream: true,
133-
dl_version: null,
134-
atlas_version: null,
134+
dl_version: undefined,
135+
atlas_version: undefined,
135136
is_genuine: true,
136137
is_local_atlas: false,
137138
non_genuine_server_name: 'mongodb',
@@ -140,19 +141,19 @@ describe('getConnectInfo', function () {
140141
server_os: 'osx',
141142
uri: streamUri,
142143
};
143-
expect(
144+
await expect(
144145
getConnectExtraInfo({
145146
connectionString: new ConnectionString(streamUri),
146-
buildInfo: BUILD_INFO,
147-
atlasVersion: null,
147+
buildInfo: Promise.resolve(BUILD_INFO),
148+
atlasVersion: Promise.resolve(undefined),
148149
resolvedHostname:
149150
'atlas-stream-67b8e1cd6d60357be377be7b-1dekw.virginia-usa.a.query.mongodb-dev.net',
150-
isLocalAtlas: false,
151+
isLocalAtlas: Promise.resolve(false),
151152
})
152-
).to.deep.equal(output);
153+
).to.eventually.deep.equal(output);
153154
});
154155

155-
it('reports correct information when an empty uri is passed', function () {
156+
it('reports correct information when an empty uri is passed', async function () {
156157
const output = {
157158
is_atlas: false,
158159
is_atlas_url: false,
@@ -163,8 +164,8 @@ describe('getConnectInfo', function () {
163164
auth_type: undefined,
164165
is_data_federation: false,
165166
is_stream: false,
166-
dl_version: null,
167-
atlas_version: null,
167+
dl_version: undefined,
168+
atlas_version: undefined,
168169
is_genuine: true,
169170
non_genuine_server_name: 'mongodb',
170171
server_arch: 'x86_64',
@@ -173,17 +174,17 @@ describe('getConnectInfo', function () {
173174
uri: '',
174175
is_local_atlas: true,
175176
};
176-
expect(
177+
await expect(
177178
getConnectExtraInfo({
178-
buildInfo: BUILD_INFO,
179-
atlasVersion: null,
179+
buildInfo: Promise.resolve(BUILD_INFO),
180+
atlasVersion: Promise.resolve(undefined),
180181
resolvedHostname: 'localhost',
181-
isLocalAtlas: true,
182+
isLocalAtlas: Promise.resolve(true),
182183
})
183-
).to.deep.equal(output);
184+
).to.eventually.deep.equal(output);
184185
});
185186

186-
it('does not fail when buildInfo is unavailable', function () {
187+
it('does not fail when buildInfo is unavailable', async function () {
187188
const output = {
188189
is_atlas: false,
189190
is_atlas_url: false,
@@ -194,22 +195,22 @@ describe('getConnectInfo', function () {
194195
auth_type: undefined,
195196
is_data_federation: false,
196197
is_stream: false,
197-
dl_version: null,
198-
atlas_version: null,
198+
dl_version: undefined,
199+
atlas_version: undefined,
199200
is_genuine: true,
200201
non_genuine_server_name: 'mongodb',
201-
server_arch: null,
202202
node_version: process.version,
203-
server_os: null,
203+
server_arch: undefined,
204+
server_os: undefined,
204205
uri: '',
205206
is_local_atlas: false,
206207
};
207-
expect(
208+
await expect(
208209
getConnectExtraInfo({
209-
buildInfo: null,
210-
atlasVersion: null,
211-
isLocalAtlas: false,
210+
buildInfo: Promise.resolve(null),
211+
atlasVersion: Promise.resolve(undefined),
212+
isLocalAtlas: Promise.resolve(false),
212213
})
213-
).to.deep.equal(output);
214+
).to.eventually.deep.equal(output);
214215
});
215216
});

packages/service-provider-core/src/connect-info.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// ^ segment data is in snake_case: forgive me javascript, for i have sinned.
22

3-
import getBuildInfo from 'mongodb-build-info';
3+
import * as getBuildInfo from 'mongodb-build-info';
44
import type { ConnectionString } from 'mongodb-connection-string-url';
55

6+
import type { Document } from './all-transport-types';
7+
68
export type ConnectionExtraInfo = {
79
is_atlas?: boolean;
810
server_version?: string;
@@ -59,49 +61,50 @@ function getHostInformation(host?: string): HostInformation {
5961
};
6062
}
6163

62-
export default function getConnectExtraInfo({
64+
export default async function getConnectExtraInfo({
6365
connectionString,
6466
buildInfo,
6567
atlasVersion,
6668
resolvedHostname,
6769
isLocalAtlas,
6870
}: {
6971
connectionString?: ConnectionString;
70-
buildInfo: any;
71-
atlasVersion: any;
72+
buildInfo: Promise<Document | null>;
73+
atlasVersion: Promise<string | undefined>;
7274
resolvedHostname?: string;
73-
isLocalAtlas: boolean;
74-
}): ConnectionExtraInfo {
75+
isLocalAtlas: Promise<boolean>;
76+
}): Promise<ConnectionExtraInfo> {
7577
const auth_type =
7678
connectionString?.searchParams.get('authMechanism') ?? undefined;
7779
const uri = connectionString?.toString() ?? '';
7880

79-
buildInfo ??= {}; // We're currently not getting buildInfo with --apiStrict.
8081
const { isGenuine: is_genuine, serverName: non_genuine_server_name } =
8182
getBuildInfo.getGenuineMongoDB(uri);
8283
// Atlas Data Lake has been renamed to Atlas Data Federation
8384
const { isDataLake: is_data_federation, dlVersion } =
84-
getBuildInfo.getDataLake(buildInfo);
85+
getBuildInfo.getDataLake(await buildInfo);
8586

86-
const { serverOs, serverArch } = getBuildInfo.getBuildEnv(buildInfo);
87-
const isAtlas = !!atlasVersion?.atlasVersion || getBuildInfo.isAtlas(uri);
87+
const { serverOs, serverArch } = getBuildInfo.getBuildEnv(await buildInfo);
88+
const isAtlas = !!(await atlasVersion) || getBuildInfo.isAtlas(uri);
8889

8990
return {
9091
...getHostInformation(resolvedHostname || uri),
9192
is_atlas: isAtlas,
92-
server_version: buildInfo.version,
93+
server_version: await buildInfo.then((info) =>
94+
typeof info?.version === 'string' ? info.version : undefined
95+
),
9396
node_version: process.version,
9497
server_os: serverOs || undefined,
9598
uri,
9699
server_arch: serverArch || undefined,
97-
is_enterprise: getBuildInfo.isEnterprise(buildInfo),
100+
is_enterprise: getBuildInfo.isEnterprise(await buildInfo),
98101
auth_type,
99102
is_data_federation,
100103
is_stream: getBuildInfo.isAtlasStream(uri),
101104
dl_version: dlVersion || undefined,
102-
atlas_version: atlasVersion?.atlasVersion ?? null,
105+
atlas_version: await atlasVersion,
103106
is_genuine,
104107
non_genuine_server_name,
105-
is_local_atlas: isLocalAtlas,
108+
is_local_atlas: await isLocalAtlas,
106109
};
107110
}

0 commit comments

Comments
 (0)