Skip to content

Commit 8db4753

Browse files
committed
chore(compass-web): build devtools-connect into compass web COMPASS-9793
1 parent f6f6c48 commit 8db4753

File tree

10 files changed

+106
-25
lines changed

10 files changed

+106
-25
lines changed

package-lock.json

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

packages/compass-web/.depcheckrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ignores:
88
- '@types/chai-dom'
99
- '@types/react'
1010
- '@types/react-dom'
11+
- 'devtools-connect-original'
1112
# Used in electron-proxy through @ts-check, but depcheckrc can't detect it
1213
- '@types/express-http-proxy'
1314
# Used in webpack config as polyfills, depcheck can't detect that because of `/`

packages/compass-web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
"@mongodb-js/compass-workspaces": "^0.69.2",
100100
"@mongodb-js/connection-info": "^0.23.0",
101101
"@mongodb-js/connection-storage": "^0.62.2",
102+
"@mongodb-js/devtools-connect": "^3.10.0",
102103
"@mongodb-js/devtools-proxy-support": "^0.5.5",
103104
"@mongodb-js/eslint-config-compass": "^1.4.12",
104105
"@mongodb-js/mocha-config-compass": "^1.7.2",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module 'devtools-connect-original' {
2+
export * from '@mongodb-js/devtools-connect';
3+
}
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import * as devtools_connect from 'devtools-connect-original';
2+
import { createMongoDBOIDCPlugin } from '../oidc-plugin';
3+
14
export function hookLogger() {
25
/* no-op */
36
}
7+
48
export async function connectMongoClient(
59
url: string,
610
options: any,
@@ -16,23 +20,21 @@ export async function connectMongoClient(
1620
delete options.parentState;
1721
delete options.parentHandle;
1822
options.__skipPingOnConnect = true;
19-
const client = new MongoClient(url, options);
20-
await client.connect();
23+
const { client } = await devtools_connect.connectMongoClient(
24+
url,
25+
options,
26+
logger,
27+
MongoClient
28+
);
2129
return {
2230
client,
2331
state: {
24-
getStateShareServer() {
25-
return Promise.resolve('Not Available');
26-
},
27-
oidcPlugin: {
28-
logger,
29-
serialize() {
30-
return Promise.resolve(undefined);
31-
},
32-
},
33-
destroy() {
34-
return Promise.resolve();
32+
// eslint-disable-next-line @typescript-eslint/require-await
33+
async getStateShareServer() {
34+
return 'Not Available';
3535
},
36+
oidcPlugin: createMongoDBOIDCPlugin({ logger }),
37+
async destroy() {},
3638
},
3739
};
3840
}

packages/compass-web/polyfills/@mongodb-js/devtools-proxy-support/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ export function createSocks5Tunnel(): void {
1010
export function hookLogger(): void {
1111
// no-op
1212
}
13-
export function createFetch(): never {
14-
throw new Error('node-fetch like-API not available in compass-web');
13+
export function createFetch(): typeof fetch {
14+
// eslint-disable-next-line @typescript-eslint/require-await
15+
return async () => {
16+
throw new Error('node-fetch not available in compass web');
17+
};
1518
}
16-
export function systemCA(): never {
17-
throw new Error('system CA access not available in compass-web');
19+
// eslint-disable-next-line @typescript-eslint/require-await
20+
export async function systemCA(): Promise<never> {
21+
const error = new Error('system CA access not available in compass-web');
22+
(error as unknown as { code: 'OUT_OF_MEM' }).code = 'OUT_OF_MEM'; // This is a "tls" error code that makes devtools-connect not use the systemCA
23+
throw error;
1824
}
1925
export function resetSystemCACache(): never {
20-
throw new Error('system CA access not available in compass-web');
26+
throw new Error('reset system CA access not available in compass-web');
2127
}
2228

2329
// Explicitly web-compatible
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function hookLoggerToMongoLogWriter() {}
2+
export function createMongoDBOIDCPlugin({ logger }: any) {
3+
return {
4+
mongoClientOptions: {},
5+
logger,
6+
/* eslint-disable @typescript-eslint/require-await */
7+
serialize: async () => '',
8+
destroy: () => {},
9+
};
10+
}

packages/compass-web/polyfills/net/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ class Socket extends Duplex {
2727
lookup?: ConnectionOptions['lookup'];
2828
tls?: boolean;
2929
}) {
30+
// WS does not support callback lookup
31+
if ((lookup?.length ?? 0) > 0) lookup = undefined;
32+
3033
const { wsURL, ...atlasOptions } =
3134
lookup?.() ?? ({} as { wsURL?: string; clusterName?: string });
35+
3236
this._ws = new WebSocket(wsURL ?? '/ws-proxy');
3337
this._ws.binaryType = 'arraybuffer';
3438
this._ws.addEventListener(
@@ -156,8 +160,6 @@ export { isIPv4, isIPv6 } from 'is-ip';
156160
export const isIP = (input: string) => ipVersion(input) ?? 0;
157161
export const createConnection = (options: { host: string; port: number }) => {
158162
const socket = new Socket();
159-
setTimeout(() => {
160-
socket.connect(options);
161-
});
163+
socket.connect(options);
162164
return socket;
163165
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { resolveSrv, resolveTxt } from '../dns';
2+
3+
export const wasNativelyLookedUp = () => false;
4+
export const withNodeFallback = {
5+
resolveSrv,
6+
resolveTxt,
7+
};

packages/compass-web/webpack.config.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module.exports = (env, args) => {
4545
'@mongodb-js/devtools-proxy-support': localPolyfill(
4646
'@mongodb-js/devtools-proxy-support'
4747
),
48+
'@mongodb-js/oidc-plugin': localPolyfill('@mongodb-js/oidc-plugin'),
4849

4950
...(config.mode === 'production'
5051
? {
@@ -56,14 +57,17 @@ module.exports = (env, args) => {
5657
}
5758
: {}),
5859

59-
// Replace 'devtools-connect' with a package that just directly connects
60-
// using the driver (= web-compatible driver) logic, because devtools-connect
61-
// contains a lot of logic that makes sense in a desktop application/CLI but
62-
// not in a web environment (DNS resolution, OIDC, CSFLE/QE, etc.)
60+
// We replace the direct import without own the sanitizes unsupported options
61+
// from the web env that are never relevant to connecting on web.
6362
'@mongodb-js/devtools-connect': localPolyfill(
6463
'@mongodb-js/devtools-connect'
6564
),
6665

66+
// Then we call into the real devtools-connect package
67+
'devtools-connect-original': require.resolve(
68+
'@mongodb-js/devtools-connect'
69+
),
70+
6771
// TODO(COMPASS-7407): compass-logging
6872
// hard to disable the whole thing while there are direct dependencies
6973
// on log-writer
@@ -124,6 +128,7 @@ module.exports = (env, args) => {
124128
os: require.resolve('os-browserify/browser'),
125129
crypto: require.resolve('crypto-browserify'),
126130
dns: localPolyfill('dns'),
131+
'os-dns-native': localPolyfill('os-dns-native'),
127132
// Built-in Node.js modules imported by the driver directly and used in
128133
// ways that requires us to provide a no-op polyfill
129134
zlib: localPolyfill('zlib'),

0 commit comments

Comments
 (0)