@@ -11,6 +11,33 @@ import { config } from './config'
11
11
// TODO: import this from `bun-plugin-stx`. Oddly, there seemingly are issues right now
12
12
import { plugin as stxPlugin } from './plugin'
13
13
14
+ /**
15
+ * Find an available port starting from the given port
16
+ */
17
+ async function findAvailablePort ( startPort : number , maxAttempts = 10 ) : Promise < number > {
18
+ for ( let i = 0 ; i < maxAttempts ; i ++ ) {
19
+ const port = startPort + i
20
+ try {
21
+ // Try to create a temporary server to check if port is available
22
+ const testServer = serve ( {
23
+ port,
24
+ fetch : ( ) => new Response ( 'test' ) ,
25
+ } )
26
+ testServer . stop ( )
27
+ return port
28
+ }
29
+ catch ( error : any ) {
30
+ // Port is in use, try next one
31
+ if ( error . code === 'EADDRINUSE' ) {
32
+ continue
33
+ }
34
+ // Other error, rethrow
35
+ throw error
36
+ }
37
+ }
38
+ throw new Error ( `Could not find an available port between ${ startPort } and ${ startPort + maxAttempts - 1 } ` )
39
+ }
40
+
14
41
// ANSI color codes for terminal output
15
42
const colors = {
16
43
reset : '\x1B[0m' ,
@@ -433,10 +460,23 @@ async function serveMarkdownFile(filePath: string, options: DevServerOptions = {
433
460
return false
434
461
}
435
462
463
+ // Find an available port (with fallback)
464
+ let actualPort = port
465
+ try {
466
+ actualPort = await findAvailablePort ( port )
467
+ if ( actualPort !== port ) {
468
+ console . log ( `${ colors . yellow } Port ${ port } is busy, using port ${ actualPort } instead${ colors . reset } ` )
469
+ }
470
+ }
471
+ catch ( error ) {
472
+ console . error ( `${ colors . red } Could not find an available port${ colors . reset } ` )
473
+ return false
474
+ }
475
+
436
476
// Start a server
437
- console . log ( `${ colors . blue } Starting server on ${ colors . cyan } http://localhost:${ port } /${ colors . reset } ...` )
477
+ console . log ( `${ colors . blue } Starting server on ${ colors . cyan } http://localhost:${ actualPort } /${ colors . reset } ...` )
438
478
const server = serve ( {
439
- port,
479
+ port : actualPort ,
440
480
fetch ( request ) {
441
481
const url = new URL ( request . url )
442
482
@@ -591,10 +631,23 @@ export async function serveStxFile(filePath: string, options: DevServerOptions =
591
631
return false
592
632
}
593
633
634
+ // Find an available port (with fallback)
635
+ let actualPort = port
636
+ try {
637
+ actualPort = await findAvailablePort ( port )
638
+ if ( actualPort !== port ) {
639
+ console . log ( `${ colors . yellow } Port ${ port } is busy, using port ${ actualPort } instead${ colors . reset } ` )
640
+ }
641
+ }
642
+ catch ( error ) {
643
+ console . error ( `${ colors . red } Could not find an available port${ colors . reset } ` )
644
+ return false
645
+ }
646
+
594
647
// Start a server
595
- console . log ( `${ colors . blue } Starting server on ${ colors . cyan } http://localhost:${ port } /${ colors . reset } ...` )
648
+ console . log ( `${ colors . blue } Starting server on ${ colors . cyan } http://localhost:${ actualPort } /${ colors . reset } ...` )
596
649
const server = serve ( {
597
- port,
650
+ port : actualPort ,
598
651
fetch ( request ) {
599
652
const url = new URL ( request . url )
600
653
@@ -1103,10 +1156,23 @@ export async function serveMultipleStxFiles(filePaths: string[], options: DevSer
1103
1156
return false
1104
1157
}
1105
1158
1159
+ // Find an available port (with fallback)
1160
+ let actualPort = port
1161
+ try {
1162
+ actualPort = await findAvailablePort ( port )
1163
+ if ( actualPort !== port ) {
1164
+ console . log ( `${ colors . yellow } Port ${ port } is busy, using port ${ actualPort } instead${ colors . reset } ` )
1165
+ }
1166
+ }
1167
+ catch ( error ) {
1168
+ console . error ( `${ colors . red } Could not find an available port${ colors . reset } ` )
1169
+ return false
1170
+ }
1171
+
1106
1172
// Start a server
1107
- console . log ( `${ colors . blue } Starting server on ${ colors . cyan } http://localhost:${ port } /${ colors . reset } ...` )
1173
+ console . log ( `${ colors . blue } Starting server on ${ colors . cyan } http://localhost:${ actualPort } /${ colors . reset } ...` )
1108
1174
const server = serve ( {
1109
- port,
1175
+ port : actualPort ,
1110
1176
fetch ( request ) {
1111
1177
const url = new URL ( request . url )
1112
1178
0 commit comments