-
Notifications
You must be signed in to change notification settings - Fork 239
feat: support configurable HTTP bind host via --host / DBHUB_HOST env #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Isla-Liu
wants to merge
8
commits into
bytebase:main
Choose a base branch
from
Isla-Liu:feat/http-bind-host
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+672
−28
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
152bd8f
feat: support configurable HTTP bind host via --host / HOST env
1527f6a
refactor: harden --host parsing and keep dev backend hint on localhost
185fa52
fix: reject --host= empty value and support IPv6 Host header in rebin…
d32c7ab
fix: rename HOST env var to DBHUB_HOST and attach bind error listener…
0d35b8d
fix: tighten --host parsing and honestly document cross-origin guard
b084cc4
fix: tighten cross-origin guard input validation
101773f
fix: trim DBHUB_HOST and clear kill timeout on clean process exit
94be454
fix: trim --host CLI value and reject whitespace-only
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { describe, it, expect, beforeAll, afterAll } from 'vitest'; | ||
| import { spawn, ChildProcess } from 'child_process'; | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import os from 'os'; | ||
|
|
||
| describe('HTTP bind host integration', () => { | ||
| let serverProcess: ChildProcess | null = null; | ||
| let testDbPath: string; | ||
| const testPort = 3002; | ||
| const testHost = '127.0.0.1'; | ||
| const startupLogs: string[] = []; | ||
|
|
||
| beforeAll(async () => { | ||
| testDbPath = path.join(os.tmpdir(), `bind_host_test_${Date.now()}_${Math.random().toString(36).substr(2, 9)}.db`); | ||
|
|
||
| // Invoke tsx directly via node to avoid pnpm.cmd resolution issues on Windows. | ||
| const tsxCli = path.resolve(process.cwd(), 'node_modules', 'tsx', 'dist', 'cli.mjs'); | ||
| const entry = path.resolve(process.cwd(), 'src', 'index.ts'); | ||
|
|
||
| serverProcess = spawn(process.execPath, [tsxCli, entry, '--transport=http'], { | ||
| env: { | ||
| ...process.env, | ||
| DSN: `sqlite://${testDbPath}`, | ||
| DBHUB_HOST: testHost, | ||
| PORT: testPort.toString(), | ||
| NODE_ENV: 'test', | ||
| }, | ||
| stdio: 'pipe', | ||
| }); | ||
|
|
||
| serverProcess.stdout?.on('data', (data) => { | ||
| startupLogs.push(data.toString()); | ||
| }); | ||
| serverProcess.stderr?.on('data', (data) => { | ||
| startupLogs.push(data.toString()); | ||
| }); | ||
|
|
||
| // Wait for /healthz to respond on the configured host | ||
| let ready = false; | ||
| for (let i = 0; i < 30; i++) { | ||
| await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
| try { | ||
| const res = await fetch(`http://${testHost}:${testPort}/healthz`); | ||
| if (res.status === 200) { | ||
| ready = true; | ||
| break; | ||
| } | ||
| } catch { | ||
| // not ready yet | ||
| } | ||
| } | ||
|
|
||
| if (!ready) { | ||
| throw new Error(`Server did not bind to ${testHost}:${testPort} within timeout. Logs:\n${startupLogs.join('')}`); | ||
| } | ||
| }, 45000); | ||
|
|
||
| afterAll(async () => { | ||
| if (serverProcess) { | ||
| serverProcess.kill('SIGTERM'); | ||
| await new Promise<void>((resolve) => { | ||
| if (!serverProcess) return resolve(); | ||
| // Without clearing this on normal exit, the pending timer keeps | ||
| // the Vitest process alive until the 5s tail elapses. | ||
| const killTimeout = setTimeout(() => { | ||
| if (serverProcess && !serverProcess.killed) serverProcess.kill('SIGKILL'); | ||
| resolve(); | ||
| }, 5000); | ||
| serverProcess.on('exit', () => { | ||
| clearTimeout(killTimeout); | ||
| resolve(); | ||
| }); | ||
| }); | ||
| } | ||
| if (testDbPath && fs.existsSync(testDbPath)) { | ||
| fs.unlinkSync(testDbPath); | ||
| } | ||
| }); | ||
|
|
||
| it('responds on the configured host', async () => { | ||
| const res = await fetch(`http://${testHost}:${testPort}/healthz`); | ||
| expect(res.status).toBe(200); | ||
| }); | ||
|
|
||
| it('logs the actual bound address at startup', () => { | ||
| const allLogs = startupLogs.join(''); | ||
| expect(allLogs).toContain(`${testHost}:${testPort}`); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Integration test uses a fixed port (3002). This can make the test flaky on CI/dev machines where that port is already in use. Prefer selecting an available ephemeral port at runtime (e.g., bind a temporary server to port 0 to discover a free port, or use a small helper like
get-port) and pass that value viaPORTwhen spawning DBHub.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Declining for scope. The existing integration test in this repo (
src/__tests__/json-rpc-integration.test.ts) already uses a fixed port (testPort = 3001); I deliberately picked3002for the newhttp-bind-host.integration.test.tsso it follows the same convention and cannot collide with that one. Switching only this test to an ephemeral port would leave two integration tests with inconsistent patterns; the cleanup you describe is a sensible improvement, but it belongs in its own PR that migrates both tests (and any future ones) together rather than being bundled into a--hostbind feature.