File tree Expand file tree Collapse file tree 3 files changed +48
-4
lines changed Expand file tree Collapse file tree 3 files changed +48
-4
lines changed Original file line number Diff line number Diff line change @@ -58,7 +58,7 @@ Creates a TCP server that allows PostgreSQL clients to connect to a PGlite datab
58
58
#### Options
59
59
60
60
- ` db: PGlite ` - The PGlite database instance
61
- - ` port?: number ` - The port to listen on (default: 5432)
61
+ - ` port?: number ` - The port to listen on (default: 5432). Use port 0 to let the OS assign an available port
62
62
- ` host?: string ` - The host to bind to (default: 127.0.0.1)
63
63
- ` path?: string ` - Unix socket path to bind to (takes precedence over host: port )
64
64
- ` inspect?: boolean ` - Print the incoming and outgoing data to the console (default: false)
@@ -156,7 +156,7 @@ pglite-server --help
156
156
### CLI Options
157
157
158
158
- ` -d, --db=PATH ` - Database path (default: memory://)
159
- - ` -p, --port=PORT ` - Port to listen on (default: 5432)
159
+ - ` -p, --port=PORT ` - Port to listen on (default: 5432). Use 0 to let the OS assign an available port
160
160
- ` -h, --host=HOST ` - Host to bind to (default: 127.0.0.1)
161
161
- ` -u, --path=UNIX ` - Unix socket to bind to (takes precedence over host: port )
162
162
- ` -v, --debug=LEVEL ` - Debug level 0-5 (default: 0)
Original file line number Diff line number Diff line change @@ -371,7 +371,12 @@ export class PGLiteSocketServer extends EventTarget {
371
371
if ( options . path ) {
372
372
this . path = options . path
373
373
} else {
374
- this . port = options . port || 5432
374
+ if ( typeof options . port === 'number' ) {
375
+ // Keep port undefined on port 0, will be set by the OS when we start the server.
376
+ this . port = options . port ?? options . port
377
+ } else {
378
+ this . port = 5432
379
+ }
375
380
this . host = options . host || '127.0.0.1'
376
381
}
377
382
this . inspect = options . inspect ?? false
@@ -429,7 +434,15 @@ export class PGLiteSocketServer extends EventTarget {
429
434
resolve ( )
430
435
} )
431
436
} else {
432
- this . server . listen ( this . port , this . host , ( ) => {
437
+ const server = this . server
438
+ server . listen ( this . port , this . host , ( ) => {
439
+ const address = server . address ( )
440
+ // We are not using pipes, so return type should be AddressInfo
441
+ if ( address === null || typeof address !== 'object' ) {
442
+ throw Error ( 'Expected address info' )
443
+ }
444
+ // Assign the new port number
445
+ this . port = address . port
433
446
this . log ( `start: server listening on ${ this . getServerConn ( ) } ` )
434
447
this . dispatchEvent (
435
448
new CustomEvent ( 'listening' , {
Original file line number Diff line number Diff line change @@ -476,6 +476,37 @@ testSocket(async (connOptions) => {
476
476
// Queue should be emptied
477
477
expect ( ( server as any ) . connectionQueue ) . toHaveLength ( 0 )
478
478
} )
479
+
480
+ it ( 'should start server with OS-assigned port when port is 0' , async ( ) => {
481
+ server = new PGLiteSocketServer ( {
482
+ db,
483
+ host : connOptions . host ,
484
+ port : 0 , // Let OS assign port
485
+ } )
486
+
487
+ await server . start ( )
488
+ const assignedPort = ( server as any ) . port
489
+ expect ( assignedPort ) . toBeGreaterThan ( 1024 )
490
+
491
+ // Try to connect to confirm server is running
492
+ const client = createConnection ( {
493
+ port : assignedPort ,
494
+ host : connOptions . host ,
495
+ } )
496
+
497
+ await new Promise < void > ( ( resolve , reject ) => {
498
+ client . on ( 'error' , ( ) => {
499
+ reject ( new Error ( 'Connection should have failed' ) )
500
+ } )
501
+ client . on ( 'connect' , ( ) => {
502
+ client . end ( )
503
+ resolve ( )
504
+ } )
505
+ setTimeout ( resolve , 100 )
506
+ } )
507
+
508
+ await server . stop ( )
509
+ } )
479
510
} )
480
511
} )
481
512
} )
You can’t perform that action at this time.
0 commit comments