Skip to content

Commit 3c178eb

Browse files
committed
Check networkTraffic preference and move into compass-connections
1 parent 097eb5e commit 3c178eb

File tree

8 files changed

+79
-116
lines changed

8 files changed

+79
-116
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-connections/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
"react": "^17.0.2",
7070
"react-redux": "^8.1.3",
7171
"redux": "^4.2.1",
72-
"redux-thunk": "^2.4.2"
72+
"redux-thunk": "^2.4.2",
73+
"semver": "^7.6.2"
7374
},
7475
"devDependencies": {
7576
"@mongodb-js/eslint-config-compass": "^1.3.8",
@@ -82,6 +83,7 @@
8283
"@types/mocha": "^9.0.0",
8384
"@types/react": "^17.0.5",
8485
"@types/react-dom": "^17.0.10",
86+
"@types/semver": "^7.3.9",
8587
"@types/sinon-chai": "^3.2.5",
8688
"chai": "^4.3.4",
8789
"depcheck": "^1.4.1",

packages/compass-connections/src/stores/connections-store-redux.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ import { showEndOfLifeMongoDBWarningModal as _showEndOfLifeMongoDBWarningModal }
3434
import ConnectionString from 'mongodb-connection-string-url';
3535
import type { ExtraConnectionData as ExtraConnectionDataForTelemetry } from '@mongodb-js/compass-telemetry';
3636
import { connectable } from '../utils/connection-supports';
37+
import {
38+
getLatestEndOfLifeServerVersion,
39+
isEndOfLifeVersion,
40+
} from '../utils/end-of-life-server';
3741

3842
export type ConnectionsEventMap = {
3943
connected: (
@@ -1819,25 +1823,28 @@ const connectWithOptions = (
18191823
.isGenuine === false
18201824
) {
18211825
dispatch(showNonGenuineMongoDBWarningModal(connectionInfo.id));
1822-
} else {
1823-
void dataService.instance().then(
1824-
(instance) => {
1825-
if (instance.build.isEndOfLife) {
1826+
} else if (preferences.getPreferences().networkTraffic) {
1827+
void dataService
1828+
.instance()
1829+
.then(async (instance) => {
1830+
const { version } = instance.build;
1831+
const latestEndOfLifeServerVersion =
1832+
await getLatestEndOfLifeServerVersion();
1833+
if (isEndOfLifeVersion(version, latestEndOfLifeServerVersion)) {
18261834
dispatch(
18271835
showEndOfLifeMongoDBWarningModal(
18281836
connectionInfo.id,
18291837
instance.build.version
18301838
)
18311839
);
18321840
}
1833-
},
1834-
(err) => {
1841+
})
1842+
.catch((err) => {
18351843
debug(
18361844
'failed to get instance details to determine if the server version is end-of-life',
18371845
err
18381846
);
1839-
}
1840-
);
1847+
});
18411848
}
18421849
} catch (err) {
18431850
dispatch(connectionAttemptError(connectionInfo, err));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { expect } from 'chai';
2+
import { isEndOfLifeVersion } from './end-of-life-server';
3+
4+
describe('isEndOfLifeVersion', function () {
5+
const LATEST_END_OF_LIFE_VERSION = '4.4.x';
6+
7+
function expectVersions(versions: string[], expected: boolean) {
8+
for (const version of versions) {
9+
expect(isEndOfLifeVersion(version, LATEST_END_OF_LIFE_VERSION)).to.equal(
10+
expected,
11+
`Expected ${version} to be ${
12+
expected ? 'end of life' : 'not end of life'
13+
}`
14+
);
15+
}
16+
}
17+
18+
it('returns true for v4.4 and below', () => {
19+
expectVersions(
20+
['4.4.0', '4.3.0', '4.0', '4.0-beta.0', '1.0.0', '0.0.1', '3.999.0'],
21+
true
22+
);
23+
});
24+
25+
it('returns true for v4.5 and above', () => {
26+
expectVersions(
27+
['4.5.0', '5.0.0', '5.0.25', '6.0.0', '7.0.0', '8.0.0'],
28+
false
29+
);
30+
});
31+
});

packages/data-service/src/end-of-life-server.ts renamed to packages/compass-connections/src/utils/end-of-life-server.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import semverSatisfies from 'semver/functions/satisfies';
2+
import semverCoerce from 'semver/functions/coerce';
3+
14
import { createLogger } from '@mongodb-js/compass-logging';
2-
const { mongoLogId, log } = createLogger('END-OF-LIFE-SERVER');
5+
6+
const { mongoLogId, log, debug } = createLogger('END-OF-LIFE-SERVER');
37

48
const FALLBACK_END_OF_LIFE_SERVER_VERSION = '4.4';
59
const {
@@ -61,3 +65,19 @@ export async function getLatestEndOfLifeServerVersion(): Promise<string> {
6165
// Return a cached or in-flight value
6266
return latestEndOfLifeServerVersion;
6367
}
68+
69+
export function isEndOfLifeVersion(
70+
version: string,
71+
latestEndOfLifeServerVersion: string
72+
) {
73+
try {
74+
const coercedVersion = semverCoerce(version);
75+
return coercedVersion
76+
? semverSatisfies(coercedVersion, `<=${latestEndOfLifeServerVersion}`)
77+
: false;
78+
} catch (error) {
79+
debug('Error comparing versions', { error });
80+
// If the version is not a valid semver, we can't reliably determine if it's EOL
81+
return false;
82+
}
83+
}

packages/data-service/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@
6060
"mongodb": "^6.16.0",
6161
"mongodb-build-info": "^1.7.2",
6262
"mongodb-connection-string-url": "^3.0.1",
63-
"mongodb-ns": "^2.4.2",
64-
"semver": "^7.6.2"
63+
"mongodb-ns": "^2.4.2"
6564
},
6665
"devDependencies": {
6766
"@mongodb-js/compass-test-server": "^0.3.8",
@@ -72,7 +71,6 @@
7271
"@mongodb-js/prettier-config-compass": "^1.2.8",
7372
"@mongodb-js/tsconfig-compass": "^1.2.8",
7473
"@types/lodash": "^4.14.188",
75-
"@types/semver": "^7.3.9",
7674
"@types/whatwg-url": "^8.2.1",
7775
"chai": "^4.2.0",
7876
"chai-as-promised": "^7.1.1",

packages/data-service/src/instance-detail-helper.spec.ts

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import {
77
getDatabasesByRoles,
88
getPrivilegesByDatabaseAndCollection,
99
getInstance,
10-
isEndOfLifeVersion,
11-
adaptBuildInfo,
1210
} from './instance-detail-helper';
1311

1412
import * as fixtures from '../test/fixtures';
@@ -631,70 +629,4 @@ describe('instance-detail-helper', function () {
631629
).to.deep.equal(['aws', 'local']);
632630
});
633631
});
634-
635-
describe('isEndOfLifeVersion', function () {
636-
const LATEST_END_OF_LIFE_VERSION = '4.4.x';
637-
638-
function expectVersions(versions: string[], expected: boolean) {
639-
for (const version of versions) {
640-
expect(
641-
isEndOfLifeVersion(version, LATEST_END_OF_LIFE_VERSION)
642-
).to.equal(
643-
expected,
644-
`Expected ${version} to be ${
645-
expected ? 'end of life' : 'not end of life'
646-
}`
647-
);
648-
}
649-
}
650-
651-
it('returns true for v4.4 and below', () => {
652-
expectVersions(
653-
['4.4.0', '4.3.0', '4.0', '4.0-beta.0', '1.0.0', '0.0.1', '3.999.0'],
654-
true
655-
);
656-
});
657-
658-
it('returns true for v4.5 and above', () => {
659-
expectVersions(
660-
['4.5.0', '5.0.0', '5.0.25', '6.0.0', '7.0.0', '8.0.0'],
661-
false
662-
);
663-
});
664-
});
665-
666-
describe('adaptBuildInfo', function () {
667-
const LATEST_END_OF_LIFE_VERSION = '4.4.0';
668-
it('propagate isEndOfLife as expected', function () {
669-
expect(
670-
adaptBuildInfo({ version: '4.4.0' }, LATEST_END_OF_LIFE_VERSION)
671-
).to.deep.equal({
672-
version: '4.4.0',
673-
isEndOfLife: true,
674-
isEnterprise: false,
675-
});
676-
// Missing version
677-
expect(adaptBuildInfo({}, LATEST_END_OF_LIFE_VERSION)).to.deep.equal({
678-
version: '',
679-
isEndOfLife: false,
680-
isEnterprise: false,
681-
});
682-
// Malformed version
683-
expect(
684-
adaptBuildInfo({ version: 'what?' }, LATEST_END_OF_LIFE_VERSION)
685-
).to.deep.equal({
686-
version: 'what?',
687-
isEndOfLife: false,
688-
isEnterprise: false,
689-
});
690-
// Newer version
691-
expect(
692-
adaptBuildInfo({ version: '8.0.0' }, LATEST_END_OF_LIFE_VERSION)
693-
).to.deep.equal({
694-
version: '8.0.0',
695-
isEndOfLife: false,
696-
isEnterprise: false,
697-
});
698-
});
699-
});
700632
});

packages/data-service/src/instance-detail-helper.ts

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ import {
1414
} from 'mongodb-build-info';
1515
import toNS from 'mongodb-ns';
1616

17-
import semverSatisfies from 'semver/functions/satisfies';
18-
import semverCoerce from 'semver/functions/coerce';
19-
2017
import type {
2118
BuildInfo,
2219
CollectionInfo,
@@ -28,11 +25,9 @@ import type {
2825
} from './run-command';
2926
import { runCommand } from './run-command';
3027
import { debug } from './logger';
31-
import { getLatestEndOfLifeServerVersion } from './end-of-life-server';
3228

3329
type BuildInfoDetails = {
3430
isEnterprise: boolean;
35-
isEndOfLife: boolean;
3631
version: string;
3732
};
3833

@@ -127,7 +122,6 @@ export async function getInstance(
127122
getParameterResult,
128123
atlasVersionResult,
129124
isLocalAtlas,
130-
latestEndOfLifeServerVersion,
131125
] = await Promise.all([
132126
runCommand(
133127
adminDb,
@@ -164,14 +158,13 @@ export async function getInstance(
164158
return await client.db(db).collection(collection).countDocuments(query);
165159
}
166160
),
167-
getLatestEndOfLifeServerVersion(),
168161
]);
169162

170163
const isAtlas = !!atlasVersionResult.atlasVersion || checkIsAtlas(uri);
171164

172165
return {
173166
auth: adaptAuthInfo(connectionStatus),
174-
build: adaptBuildInfo(buildInfoResult, latestEndOfLifeServerVersion),
167+
build: adaptBuildInfo(buildInfoResult),
175168
host: adaptHostInfo(hostInfoResult),
176169
genuineMongoDB: buildGenuineMongoDBInfo(uri),
177170
dataLake: buildDataLakeInfo(buildInfoResult),
@@ -357,33 +350,13 @@ function adaptHostInfo(rawHostInfo: Partial<HostInfo>): HostInfoDetails {
357350
};
358351
}
359352

360-
export function isEndOfLifeVersion(
361-
version: string,
362-
latestEndOfLifeServerVersion: string
363-
) {
364-
try {
365-
const coercedVersion = semverCoerce(version);
366-
return coercedVersion
367-
? semverSatisfies(coercedVersion, `<=${latestEndOfLifeServerVersion}`)
368-
: false;
369-
} catch (error) {
370-
debug('Error comparing versions', { error });
371-
// If the version is not a valid semver, we can't reliably determine if it's EOL
372-
return false;
373-
}
374-
}
375-
376353
export function adaptBuildInfo(
377-
rawBuildInfo: Partial<BuildInfo>,
378-
latestEndOfLifeServerVersion: string
354+
rawBuildInfo: Partial<BuildInfo>
379355
): BuildInfoDetails {
380356
return {
381357
version: rawBuildInfo.version ?? '',
382358
// Cover both cases of detecting enterprise module, see SERVER-18099.
383359
isEnterprise: isEnterprise(rawBuildInfo),
384-
isEndOfLife: rawBuildInfo.version
385-
? isEndOfLifeVersion(rawBuildInfo.version, latestEndOfLifeServerVersion)
386-
: false,
387360
};
388361
}
389362

0 commit comments

Comments
 (0)