Skip to content

Commit 1e8debf

Browse files
committed
fix: to avoid port overlap prevent sandbox running with other commands
1 parent 7570add commit 1e8debf

File tree

4 files changed

+75
-24
lines changed

4 files changed

+75
-24
lines changed

packages/compass-web/.eslintrc.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,17 @@ module.exports = {
66
tsconfigRootDir: __dirname,
77
project: ['./tsconfig.json'],
88
},
9+
overrides: [
10+
{
11+
files: ['**/*.mjs'],
12+
parserOptions: {
13+
ecmaVersion: 2023,
14+
sourceType: 'module',
15+
},
16+
env: {
17+
es2023: true,
18+
node: true,
19+
},
20+
},
21+
],
922
};

packages/compass-web/scripts/sync-dist-to-mms.mjs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,28 @@ if (!process.env.MMS_HOME) {
1616
function isDevServerRunning(port, host = '127.0.0.1') {
1717
return new Promise((resolve) => {
1818
const socket = new net.Socket();
19-
socket.setTimeout(1000);
20-
socket.once('connect', () => {
21-
socket.destroy();
22-
resolve(true);
23-
});
24-
socket.once('timeout', () => {
25-
socket.destroy();
26-
resolve(false);
27-
});
28-
socket.once('error', () => {
29-
resolve(false);
30-
});
31-
socket.connect(port, host);
19+
socket
20+
.setTimeout(1000)
21+
.on('connect', () => {
22+
socket.destroy();
23+
resolve(true);
24+
})
25+
.on('error', () => {
26+
socket.destroy();
27+
resolve(false);
28+
})
29+
.on('timeout', () => {
30+
socket.destroy();
31+
resolve(false);
32+
})
33+
.connect(port, host);
3234
});
3335
}
3436

37+
const okToRunMMS = !process.argv.includes('--no-mms');
38+
3539
let devServer;
36-
if (!(await isDevServerRunning(8081))) {
40+
if (okToRunMMS || !(await isDevServerRunning(8081))) {
3741
console.log('mms dev server is not running... launching!');
3842
child_process.execFileSync('pnpm', ['install'], {
3943
cwd: process.env.MMS_HOME,
@@ -74,6 +78,8 @@ if (!(await isDevServerRunning(8081))) {
7478
} else {
7579
console.log('Dev server is ready!');
7680
}
81+
} else {
82+
console.log('Skipping running MMS dev server...');
7783
}
7884

7985
const srcDir = path.resolve(import.meta.dirname, '..', 'dist');

scripts/start.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,27 @@ Usage: start.mts [-h/--help] [targets... [targetOptions...]]
66

77
- `-h, --help` Show this help message
88

9-
## Targets (can be used in any combination)
9+
## Targets
1010

1111
- `desktop` Start MongoDB Compass Desktop (default if no targets specified)
1212
- `sandbox` Start MongoDB Compass Web Sandbox, useful for UI-only changes
1313
- `sync` Start Cloud Sync, in combination with redirector/redwood can be used to test data explorer changes
14+
- `--no-mms` can be passed to the sync subcommand to not run MMS's dev server.
1415

15-
## Examples
16+
**Note:** `sandbox` must be run alone and cannot be combined with other targets.
1617

17-
start.mts # Start desktop (default)
18-
start.mts desktop --production # Start desktop explicitly
19-
start.mts web # Start web sandbox only
20-
start.mts desktop web # Start both desktop and web
21-
start.mts sync --flagA -b web -c # Start mms-sync and web
18+
## Port Configuration
19+
20+
The `desktop` and `sandbox` targets both use the same webpack dev server port (4242) by design. When running both targets simultaneously, the sandbox will automatically detect that the desktop target's webpack dev server is already running and will skip starting its own, sharing the same webpack dev server instance.
21+
22+
Since `sync` must run alone, there are no port conflicts with other targets.
23+
24+
### Well-Known Ports
25+
26+
| Port | Service | Target | Description |
27+
| ---- | ---------------------- | -------------------- | ----------------------------------------------- |
28+
| 4242 | Webpack Dev Server | `desktop`, `sandbox` | Serves compiled frontend assets with hot reload |
29+
| 7777 | HTTP Proxy Server | `sandbox` | Express proxy server for Atlas API requests |
30+
| 1337 | WebSocket Proxy Server | `sandbox` | WebSocket proxy for MongoDB connections |
31+
| 8080 | Atlas Local Backend | `sync` | Local MMS backend server (when MMS_HOME is set) |
32+
| 8081 | MMS Dev Server | `sync` | Local MMS development server |

scripts/start.mts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ for (const arg of args) {
4545
// If arg is not a valid target and no current target, ignore it
4646
}
4747

48+
// Check for mutually exclusive targets
49+
if (
50+
targets.sandbox.enabled &&
51+
(targets.desktop.enabled || targets.sync.enabled)
52+
) {
53+
console.error('Error: sandbox target must be run alone.');
54+
console.error(
55+
'Please run sandbox by itself, not combined with other targets.'
56+
);
57+
process.exit(1);
58+
}
59+
4860
// If no targets specified, default to desktop
4961
if (
5062
!targets.desktop.enabled &&
@@ -62,14 +74,14 @@ const subProcesses: child_process.ChildProcess[] = [];
6274
async function cleanup(signal: NodeJS.Signals) {
6375
for (const p of subProcesses) p.kill(signal);
6476
console.log('\nstart | requested termination.');
65-
await timers.setTimeout(5000);
77+
await timers.setTimeout(10_000);
6678
const stillRunning = subProcesses.filter((p) => p.exitCode === null);
6779
for (const p of stillRunning) p.kill('SIGKILL');
6880
console.log('\nstart | done.');
6981
process.exit(0);
7082
}
7183

72-
process.on('SIGINT', cleanup).on('SIGTERM', cleanup);
84+
process.once('SIGINT', cleanup).once('SIGTERM', cleanup);
7385

7486
// Helper function to create a transform stream that prefixes lines
7587
function createPrefixTransform(prefix: string) {
@@ -125,7 +137,7 @@ function spawnTarget(
125137
...(args.length ? ['--', ...args] : []),
126138
],
127139
{
128-
stdio: 'pipe',
140+
stdio: ['pipe', 'pipe', 'pipe'], // stdin, stdout, stderr all piped
129141
env: { ...process.env, ...colorEnv },
130142
},
131143
];
@@ -206,3 +218,12 @@ if (targets.sandbox.enabled) {
206218
)
207219
);
208220
}
221+
222+
// Forward stdin to all subprocesses using pipeline
223+
for (const subProcess of subProcesses) {
224+
if (subProcess.stdin)
225+
pipeline(process.stdin, subProcess.stdin, { end: false }).catch((err) => {
226+
if (err.code !== 'EPIPE' && err.code !== 'ERR_STREAM_PREMATURE_CLOSE')
227+
console.error(`start | stdin pipeline error:`, err);
228+
});
229+
}

0 commit comments

Comments
 (0)