Skip to content

Commit 7b7ca4e

Browse files
authored
chore(data-service): remove dependency on compass-preferences-model COMPASS-8225 (#6174)
1 parent 560ba32 commit 7b7ca4e

File tree

9 files changed

+80
-47
lines changed

9 files changed

+80
-47
lines changed

package-lock.json

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

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import type {
1818
import { createConnectionAttempt } from 'mongodb-data-service';
1919
import { UUID } from 'bson';
2020
import { assign, cloneDeep, isEqual, merge } from 'lodash';
21-
import type { PreferencesAccess } from 'compass-preferences-model/provider';
21+
import {
22+
proxyPreferenceToProxyOptions,
23+
type PreferencesAccess,
24+
} from 'compass-preferences-model/provider';
2225
import { getNotificationTriggers } from '../components/connection-status-notifications';
2326
import { openToast, showConfirmation } from '@mongodb-js/compass-components';
2427
import { adjustConnectionOptionsBeforeConnect } from '@mongodb-js/connection-form';
@@ -1588,6 +1591,9 @@ export const connect = (
15881591

15891592
const connectionAttempt = createConnectionAttempt({
15901593
logger: log.unbound,
1594+
proxyOptions: proxyPreferenceToProxyOptions(
1595+
preferences.getPreferences().proxy
1596+
),
15911597
connectFn,
15921598
});
15931599

packages/data-service/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
"@mongodb-js/devtools-connect": "^3.2.5",
5757
"@mongodb-js/devtools-proxy-support": "^0.3.6",
5858
"bson": "^6.7.0",
59-
"compass-preferences-model": "^2.27.0",
6059
"lodash": "^4.17.21",
6160
"mongodb": "^6.8.0",
6261
"mongodb-build-info": "^1.7.2",

packages/data-service/src/connect-mongo-client.spec.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ describe('connectMongoClient', function () {
275275
describe('prepareOIDCOptions', function () {
276276
it('defaults allowedFlows to "auth-code"', async function () {
277277
const options = prepareOIDCOptions({
278-
connectionString: 'mongodb://localhost:27017',
278+
connectionOptions: {
279+
connectionString: 'mongodb://localhost:27017',
280+
},
279281
});
280282

281283
expect(await (options.oidc.allowedFlows as any)()).to.deep.equal([
@@ -285,9 +287,11 @@ describe('prepareOIDCOptions', function () {
285287

286288
it('does not override allowedFlows when set', async function () {
287289
const options = prepareOIDCOptions({
288-
connectionString: 'mongodb://localhost:27017',
289-
oidc: {
290-
allowedFlows: ['auth-code', 'device-auth'],
290+
connectionOptions: {
291+
connectionString: 'mongodb://localhost:27017',
292+
oidc: {
293+
allowedFlows: ['auth-code', 'device-auth'],
294+
},
291295
},
292296
});
293297
expect(await (options.oidc.allowedFlows as any)()).to.deep.equal([
@@ -299,9 +303,11 @@ describe('prepareOIDCOptions', function () {
299303
it('maps ALLOWED_HOSTS on the authMechanismProperties (non-url) when enableUntrustedEndpoints is true', function () {
300304
function actual(connectionString: string) {
301305
return prepareOIDCOptions({
302-
connectionString,
303-
oidc: {
304-
enableUntrustedEndpoints: true,
306+
connectionOptions: {
307+
connectionString,
308+
oidc: {
309+
enableUntrustedEndpoints: true,
310+
},
305311
},
306312
}).authMechanismProperties;
307313
}
@@ -341,20 +347,22 @@ describe('prepareOIDCOptions', function () {
341347

342348
it('does not set ALLOWED_HOSTS on the authMechanismProperties (non-url) when enableUntrustedEndpoints is not set', function () {
343349
const options = prepareOIDCOptions({
344-
connectionString: 'mongodb://localhost:27017',
350+
connectionOptions: {
351+
connectionString: 'mongodb://localhost:27017',
352+
},
345353
});
346354

347355
expect(options.authMechanismProperties).to.deep.equal({});
348356
});
349357

350358
it('passes through a signal argument', function () {
351359
const signal = AbortSignal.abort();
352-
const options = prepareOIDCOptions(
353-
{
360+
const options = prepareOIDCOptions({
361+
connectionOptions: {
354362
connectionString: 'mongodb://localhost:27017',
355363
},
356-
signal
357-
);
364+
signal,
365+
});
358366

359367
expect(options.oidc.signal).to.equal(signal);
360368
});

packages/data-service/src/connect-mongo-client.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ import {
1010
hookLogger as hookProxyLogger,
1111
createAgent,
1212
} from '@mongodb-js/devtools-proxy-support';
13-
import type { Tunnel } from '@mongodb-js/devtools-proxy-support';
13+
import type {
14+
DevtoolsProxyOptions,
15+
Tunnel,
16+
} from '@mongodb-js/devtools-proxy-support';
1417
import EventEmitter from 'events';
1518
import ConnectionString from 'mongodb-connection-string-url';
1619
import _ from 'lodash';
1720

1821
import { redactConnectionOptions, redactConnectionString } from './redact';
1922
import type { ConnectionOptions } from './connection-options';
20-
import {
21-
getCurrentApplicationProxyOptions,
22-
getTunnelOptions,
23-
waitForTunnelError,
24-
} from './ssh-tunnel-helpers';
23+
import { getTunnelOptions, waitForTunnelError } from './ssh-tunnel-helpers';
2524
import { runCommand } from './run-command';
2625
import type { UnboundDataServiceImplLogger } from './logger';
2726
import { debug as _debug } from './logger';
@@ -65,11 +64,17 @@ function matchingAllowedHosts(
6564
return [...new Set(suffixes)];
6665
}
6766

68-
export function prepareOIDCOptions(
69-
connectionOptions: Readonly<ConnectionOptions>,
70-
signal?: AbortSignal,
71-
reauthenticationHandler?: ReauthenticationHandler
72-
): Required<
67+
export function prepareOIDCOptions({
68+
connectionOptions,
69+
proxyOptions = {},
70+
signal,
71+
reauthenticationHandler,
72+
}: {
73+
connectionOptions: Readonly<ConnectionOptions>;
74+
proxyOptions?: DevtoolsProxyOptions;
75+
signal?: AbortSignal;
76+
reauthenticationHandler?: ReauthenticationHandler;
77+
}): Required<
7378
Pick<
7479
DevtoolsConnectOptions,
7580
'oidc' | 'authMechanismProperties' | 'applyProxyToOIDC'
@@ -108,7 +113,7 @@ export function prepareOIDCOptions(
108113
options.applyProxyToOIDC = true;
109114
} else {
110115
options.oidc.customHttpOptions = {
111-
agent: createAgent(getCurrentApplicationProxyOptions()),
116+
agent: createAgent(proxyOptions),
112117
};
113118
}
114119

@@ -119,6 +124,7 @@ export function prepareOIDCOptions(
119124

120125
export async function connectMongoClientDataService({
121126
connectionOptions,
127+
proxyOptions = {},
122128
setupListeners,
123129
signal,
124130
logger,
@@ -127,6 +133,7 @@ export async function connectMongoClientDataService({
127133
reauthenticationHandler,
128134
}: {
129135
connectionOptions: Readonly<ConnectionOptions>;
136+
proxyOptions?: Readonly<DevtoolsProxyOptions>;
130137
setupListeners: (client: MongoClient) => void;
131138
signal?: AbortSignal;
132139
logger?: UnboundDataServiceImplLogger;
@@ -147,11 +154,12 @@ export async function connectMongoClientDataService({
147154
redactConnectionOptions(connectionOptions)
148155
);
149156

150-
const oidcOptions = prepareOIDCOptions(
157+
const oidcOptions = prepareOIDCOptions({
151158
connectionOptions,
159+
proxyOptions,
152160
signal,
153-
reauthenticationHandler
154-
);
161+
reauthenticationHandler,
162+
});
155163

156164
const url = connectionOptions.connectionString;
157165
const options: DevtoolsConnectOptions = {
@@ -189,7 +197,7 @@ export async function connectMongoClientDataService({
189197
// If connectionOptions.sshTunnel is not defined, the tunnel
190198
// will also be undefined.
191199
const tunnel = createSocks5Tunnel(
192-
getTunnelOptions(connectionOptions),
200+
getTunnelOptions(connectionOptions, proxyOptions),
193201
'generate-credentials',
194202
'mongodb://'
195203
);

packages/data-service/src/connect.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@ import type { ConnectionOptions } from './connection-options';
22
import type { DataService } from './data-service';
33
import type { DataServiceImplLogger } from './logger';
44
import { DataServiceImpl } from './data-service';
5+
import type { DevtoolsProxyOptions } from '@mongodb-js/devtools-proxy-support';
56

67
export default async function connect({
78
connectionOptions,
9+
proxyOptions,
810
signal,
911
logger,
1012
productName,
1113
productDocsLink,
1214
}: {
1315
connectionOptions: ConnectionOptions;
16+
proxyOptions?: DevtoolsProxyOptions;
1417
signal?: AbortSignal;
1518
logger?: DataServiceImplLogger;
1619
productName?: string;
1720
productDocsLink?: string;
1821
}): Promise<DataService> {
19-
const dataService = new DataServiceImpl(connectionOptions, logger);
22+
const dataService = new DataServiceImpl(
23+
connectionOptions,
24+
logger,
25+
proxyOptions
26+
);
2027
await dataService.connect({
2128
signal,
2229
productName,

packages/data-service/src/connection-attempt.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { UnboundDataServiceImplLogger } from './logger';
55
import connect from './connect';
66
import type { DataService } from './data-service';
77
import type { ConnectionOptions } from './connection-options';
8+
import type { DevtoolsProxyOptions } from '@mongodb-js/devtools-proxy-support';
89

910
const { mongoLogId } = createLogger('CONNECTION-ATTEMPT');
1011

@@ -18,16 +19,20 @@ export class ConnectionAttempt {
1819
_connectFn: typeof connect;
1920
_dataService: DataService | null = null;
2021
_logger: UnboundDataServiceImplLogger;
22+
_proxyOptions: DevtoolsProxyOptions | undefined;
2123

2224
constructor({
2325
connectFn,
2426
logger,
27+
proxyOptions,
2528
}: {
2629
connectFn: typeof connect;
2730
logger: UnboundDataServiceImplLogger;
31+
proxyOptions?: DevtoolsProxyOptions;
2832
}) {
2933
this._logger = logger;
3034
this._connectFn = connectFn;
35+
this._proxyOptions = proxyOptions;
3136
this._abortController = new AbortController();
3237
}
3338

@@ -61,6 +66,7 @@ export class ConnectionAttempt {
6166
connectionOptions,
6267
signal: this._abortController.signal,
6368
logger: this._logger,
69+
proxyOptions: this._proxyOptions,
6470
});
6571
return this._dataService;
6672
} catch (err) {
@@ -127,13 +133,16 @@ export class ConnectionAttempt {
127133

128134
export function createConnectionAttempt({
129135
logger,
136+
proxyOptions,
130137
connectFn = connect,
131138
}: {
132139
logger: UnboundDataServiceImplLogger;
140+
proxyOptions: DevtoolsProxyOptions;
133141
connectFn?: typeof connect;
134142
}): ConnectionAttempt {
135143
return new ConnectionAttempt({
136144
logger,
145+
proxyOptions,
137146
connectFn,
138147
});
139148
}

packages/data-service/src/data-service.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import type { Tunnel } from '@mongodb-js/devtools-proxy-support';
1+
import type {
2+
DevtoolsProxyOptions,
3+
Tunnel,
4+
} from '@mongodb-js/devtools-proxy-support';
25
import { EventEmitter } from 'events';
36
import { ExplainVerbosity, ClientEncryption } from 'mongodb';
47
import type {
@@ -926,6 +929,7 @@ function op<T extends unknown[], K>(
926929

927930
class DataServiceImpl extends WithLogContext implements DataService {
928931
private readonly _connectionOptions: Readonly<ConnectionOptions>;
932+
private readonly _proxyOptions: Readonly<DevtoolsProxyOptions>;
929933
private _isConnecting = false;
930934
private _mongoClientConnectionOptions?: {
931935
url: string;
@@ -970,11 +974,13 @@ class DataServiceImpl extends WithLogContext implements DataService {
970974

971975
constructor(
972976
connectionOptions: Readonly<ConnectionOptions>,
973-
logger?: DataServiceImplLogger
977+
logger?: DataServiceImplLogger,
978+
proxyOptions?: DevtoolsProxyOptions
974979
) {
975980
super();
976981
this._id = id++;
977982
this._connectionOptions = connectionOptions;
983+
this._proxyOptions = proxyOptions ?? {};
978984
const logComponent = 'COMPASS-DATA-SERVICE';
979985
const logCtx = `Connection ${this._id}`;
980986
this._logger = {
@@ -1465,6 +1471,7 @@ class DataServiceImpl extends WithLogContext implements DataService {
14651471
const [metadataClient, crudClient, tunnel, state, connectionOptions] =
14661472
await connectMongoClient({
14671473
connectionOptions: this._connectionOptions,
1474+
proxyOptions: this._proxyOptions,
14681475
setupListeners: this._setupListeners.bind(this),
14691476
signal,
14701477
logger: this._unboundLogger,

packages/data-service/src/ssh-tunnel-helpers.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import type {
33
DevtoolsProxyOptions,
44
Tunnel,
55
} from '@mongodb-js/devtools-proxy-support';
6-
import {
7-
defaultPreferencesInstance,
8-
proxyPreferenceToProxyOptions,
9-
} from 'compass-preferences-model';
106

117
export async function waitForTunnelError(
128
tunnel: Tunnel | undefined
@@ -16,17 +12,12 @@ export async function waitForTunnelError(
1612
});
1713
}
1814

19-
export function getCurrentApplicationProxyOptions() {
20-
return proxyPreferenceToProxyOptions(
21-
defaultPreferencesInstance.getPreferences().proxy
22-
);
23-
}
24-
2515
export function getTunnelOptions(
26-
connectionOptions: ConnectionOptions
16+
connectionOptions: ConnectionOptions,
17+
appLevelProxyOptions: DevtoolsProxyOptions
2718
): DevtoolsProxyOptions {
2819
if (connectionOptions.useApplicationLevelProxy) {
29-
return getCurrentApplicationProxyOptions();
20+
return appLevelProxyOptions;
3021
}
3122
if (connectionOptions.sshTunnel) {
3223
const {

0 commit comments

Comments
 (0)