55 */
66
77import * as net from 'net' ;
8- import { Promise as bluebird } from 'bluebird' ;
98
109export const localAddresses = [ '127.0.0.1' , 'localhost' , '0.0.0.0' , '::1' ] ;
1110
@@ -27,28 +26,19 @@ export const portCheck = (port: number, host: string): Promise<void> =>
2726 } ) ;
2827
2928export const isPortAvailable = ( port : number , host : string ) : Promise < void > =>
30- Promise . resolve (
31- bluebird
32- . map (
33- localAddresses ,
34- // we meed to wrap the built-in Promise with bluebird.reflect() so we can
35- // test the result of the promise without failing bluebird.map()
36- ( h ) => bluebird . resolve ( portCheck ( port , h ) ) . reflect ( ) ,
37- // do each port check sequentially (as localhost & 127.0.0.1 will conflict on most default environments)
38- { concurrency : 1 }
39- )
40- . then ( ( inspections ) => {
41- // if every port check failed, then fail the `isPortAvailable` check
42- if ( inspections . every ( ( inspection ) => ! inspection . isFulfilled ( ) ) ) {
43- return Promise . reject (
44- new Error ( `Cannot open port ${ port } on ipv4 or ipv6 interfaces` )
45- ) ;
46- }
29+ Promise . allSettled (
30+ localAddresses . map ( ( localHost ) => portCheck ( port , localHost ) )
31+ ) . then ( ( settledPortChecks ) => {
32+ // if every port check failed, then fail the `isPortAvailable` check
33+ if ( settledPortChecks . every ( ( result ) => result . status === 'rejected' ) ) {
34+ return Promise . reject (
35+ new Error ( `Cannot open port ${ port } on ipv4 or ipv6 interfaces` )
36+ ) ;
37+ }
4738
48- // the local addresses passed - now check the host that the user has specified
49- return portCheck ( port , host ) ;
50- } )
51- ) ;
39+ // the local addresses passed - now check the host that the user has specified
40+ return portCheck ( port , host ) ;
41+ } ) ;
5242
5343export const freePort = ( ) : Promise < number > => {
5444 return new Promise ( ( res ) => {
0 commit comments