Skip to content

Commit 3b42fb2

Browse files
committed
fix: update web server binding to IPv6 localhost
Change default hostname from 'localhost' to '::1' for IPv6 support. Add PTY_WEB_HOSTNAME environment variable to allow configuration. Update tests and documentation accordingly.
1 parent 56c5890 commit 3b42fb2

4 files changed

Lines changed: 19 additions & 19 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,10 @@ This eliminates the need for polling—perfect for long-running processes like b
222222

223223
### Environment Variables
224224

225-
| Variable | Default | Description |
226-
| ---------------------- | ------- | -------------------------------------------------- |
227-
| `PTY_MAX_BUFFER_LINES` | `50000` | Maximum lines to keep in output buffer per session |
225+
| Variable | Default | Description |
226+
| ---------------------- | ---------- | -------------------------------------------------- |
227+
| `PTY_MAX_BUFFER_LINES` | `50000` | Maximum lines to keep in output buffer per session |
228+
| `PTY_WEB_HOSTNAME` | `::1` | Hostname for the web server to bind to |
228229

229230
### Permissions
230231

src/web/server/server.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import type { Server } from 'bun'
2+
import { routes } from '../shared/routes.ts'
3+
import { CallbackManager } from './callback-manager.ts'
24
import { handleHealth } from './handlers/health.ts'
35
import {
4-
getSessions,
5-
createSession,
6+
cleanupSession,
67
clearSessions,
8+
createSession,
9+
getPlainBuffer,
10+
getRawBuffer,
711
getSession,
8-
sendInput,
12+
getSessions,
913
killSession,
10-
getRawBuffer,
11-
getPlainBuffer,
12-
cleanupSession,
14+
sendInput,
1315
} from './handlers/sessions.ts'
14-
1516
import { buildStaticRoutes } from './handlers/static.ts'
1617
import { handleUpgrade } from './handlers/upgrade.ts'
1718
import { handleWebSocketMessage } from './handlers/websocket.ts'
18-
import { CallbackManager } from './callback-manager.ts'
19-
20-
import { routes } from '../shared/routes.ts'
2119

2220
export class PTYServer implements Disposable {
2321
public readonly server: Server<undefined>
@@ -44,6 +42,7 @@ export class PTYServer implements Disposable {
4442
private startWebServer(): Server<undefined> {
4543
return Bun.serve({
4644
port: 0,
45+
hostname: process.env.PTY_WEB_HOSTNAME ?? '::1',
4746

4847
routes: {
4948
...this.staticRoutes,

test/e2e/fixtures.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { type ChildProcess, spawn } from 'node:child_process'
12
import { test as base, type WorkerInfo } from '@playwright/test'
2-
import { spawn, type ChildProcess } from 'node:child_process'
33

44
import { createApiClient } from '../../src/web/shared/api-client.ts'
55
import { ManagedTestClient } from '../utils'
@@ -90,12 +90,12 @@ export const test = base.extend<TestFixtures, WorkerFixtures>({
9090
const serverURL = serverURLText.trim()
9191

9292
// Parse URL to extract port number
93-
const urlMatch = serverURL.match(/http:\/\/localhost:(\d+)/)
93+
const urlMatch = serverURL.match(/http:\/\/\[::1\]:(\d+)/)
9494
if (!urlMatch || !urlMatch[1]) {
9595
throw new Error(`Invalid port file format: ${serverURL}`)
9696
}
9797
const port = parseInt(urlMatch[1], 10)
98-
const baseURL = `http://localhost:${port}`
98+
const baseURL = `http://[::1]:${port}`
9999

100100
await waitForServer(baseURL, 15000)
101101

test/web-server.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { describe, it, expect, afterAll, beforeAll } from 'bun:test'
1+
import { afterAll, beforeAll, describe, expect, it } from 'bun:test'
22
import {
33
manager,
44
registerRawOutputCallback,
55
registerSessionUpdateCallback,
66
} from '../src/plugin/pty/manager.ts'
7-
import { PTYServer } from '../src/web/server/server.ts'
87
import type { PTYSessionInfo } from '../src/plugin/pty/types.ts'
8+
import { PTYServer } from '../src/web/server/server.ts'
99
import { ManagedTestServer } from './utils.ts'
1010

1111
describe('Web Server', () => {
1212
describe('Server Lifecycle', () => {
1313
it('should start server successfully', async () => {
1414
await using server = await PTYServer.createServer()
1515
const url = server.server.url
16-
expect(url.hostname).toBe('localhost')
16+
expect(url.hostname).toBe('[::1]')
1717
expect(url.protocol).toBe('http:')
1818
expect(url.port).not.toBe(0)
1919
expect(url.port).not.toBe(8080) // Default port should be avoided

0 commit comments

Comments
 (0)