11import fs from 'fs' ;
2- import net from 'net' ;
32import path from 'path' ;
43
54import {
@@ -10,8 +9,7 @@ import {
109 confit ,
1110} from '@sesamecare-oss/confit' ;
1211
13- import { findPort } from '../development/port-finder' ;
14- import { isTest } from '../env' ;
12+ import { getAvailablePort } from '../development/port-finder' ;
1513
1614import type { ConfigurationSchema } from './schema' ;
1715
@@ -57,28 +55,6 @@ async function addDefaultConfiguration<Config extends ConfigurationSchema = Conf
5755 }
5856}
5957
60- async function getEphemeralPort ( ) : Promise < number > {
61- return new Promise ( ( resolve , reject ) => {
62- const server = net . createServer ( ) ;
63-
64- server . listen ( 0 , ( ) => {
65- const address = server . address ( ) ;
66- if ( typeof address === 'string' || ! address ) {
67- reject ( new Error ( 'Invalid address' ) ) ;
68- return ;
69- }
70- const port = address . port ; // Retrieve the ephemeral port
71- server . close ( ( err ) => {
72- if ( err ) {
73- reject ( err ) ;
74- } else {
75- resolve ( port ) ;
76- }
77- } ) ;
78- } ) ;
79- } ) ;
80- }
81-
8258export interface ServiceConfigurationSpec {
8359 // The LAST configuration is the most "specific" - if a configuration value
8460 // exists in all directories, the last one wins
@@ -117,8 +93,7 @@ export async function loadConfiguration<Config extends ConfigurationSchema>({
11793 // configured to auto-select
11894 const serverConfig = loaded . get ( ) . server ;
11995 if ( serverConfig . port === 0 ) {
120- const portPromise : Promise < number > = ( isTest ( ) || process . env . TEST_RUNNER ) ? getEphemeralPort ( ) : findPort ( 8001 ) ;
121- const port = await portPromise ;
96+ const port = await getAvailablePort ( 8001 ) ;
12297 const store = loaded . get ( ) ;
12398 store . server = store . server || { } ;
12499 store . server . port = port ;
0 commit comments