@@ -6,15 +6,52 @@ import * as Layer from "effect/Layer";
66import * as Predicate from "effect/Predicate" ;
77import * as Schema from "effect/Schema" ;
88
9- export class NetError extends Schema . TaggedErrorClass < NetError > ( ) ( "NetError" , {
10- host : Schema . String ,
11- cause : Schema . optional ( Schema . Defect ( ) ) ,
12- } ) {
9+ export class LoopbackPortListenError extends Schema . TaggedErrorClass < LoopbackPortListenError > ( ) (
10+ "LoopbackPortListenError" ,
11+ {
12+ host : Schema . String ,
13+ cause : Schema . Defect ( ) ,
14+ } ,
15+ ) {
16+ override get message ( ) : string {
17+ return `Failed to listen for an ephemeral loopback port on ${ this . host } .` ;
18+ }
19+ }
20+
21+ export class LoopbackPortAddressUnavailableError extends Schema . TaggedErrorClass < LoopbackPortAddressUnavailableError > ( ) (
22+ "LoopbackPortAddressUnavailableError" ,
23+ {
24+ host : Schema . String ,
25+ address : Schema . NullOr ( Schema . String ) ,
26+ family : Schema . NullOr ( Schema . String ) ,
27+ port : Schema . NullOr ( Schema . Number ) ,
28+ } ,
29+ ) {
1330 override get message ( ) : string {
14- return " Failed to reserve loopback port" ;
31+ return ` Failed to read a usable ephemeral loopback port for ${ this . host } (address ${ this . address ?? "unavailable" } , family ${ this . family ?? "unavailable" } , port ${ this . port ?? "unavailable" } ).` ;
1532 }
1633}
1734
35+ export class LoopbackPortReleaseError extends Schema . TaggedErrorClass < LoopbackPortReleaseError > ( ) (
36+ "LoopbackPortReleaseError" ,
37+ {
38+ host : Schema . String ,
39+ port : Schema . Number ,
40+ cause : Schema . Defect ( ) ,
41+ } ,
42+ ) {
43+ override get message ( ) : string {
44+ return `Failed to release ephemeral loopback port ${ this . port } on ${ this . host } .` ;
45+ }
46+ }
47+
48+ export const NetError = Schema . Union ( [
49+ LoopbackPortListenError ,
50+ LoopbackPortAddressUnavailableError ,
51+ LoopbackPortReleaseError ,
52+ ] ) ;
53+ export type NetError = typeof NetError . Type ;
54+
1855const isErrnoExceptionWithCode = (
1956 cause : unknown ,
2057) : cause is {
@@ -163,20 +200,55 @@ export const make = () => {
163200 } ;
164201
165202 probe . once ( "error" , ( cause ) => {
166- settle ( Effect . fail ( new NetError ( { host, cause } ) ) ) ;
203+ settle ( Effect . fail ( new LoopbackPortListenError ( { host, cause } ) ) ) ;
167204 } ) ;
168205
169- probe . listen ( 0 , host , ( ) => {
170- const address = probe . address ( ) ;
171- const port = typeof address === "object" && address !== null ? address . port : 0 ;
172- probe . close ( ( ) => {
173- if ( port > 0 ) {
174- settle ( Effect . succeed ( port ) ) ;
175- return ;
176- }
177- settle ( Effect . fail ( new NetError ( { host } ) ) ) ;
206+ try {
207+ probe . listen ( 0 , host , ( ) => {
208+ const address = probe . address ( ) ;
209+ const addressDetails =
210+ typeof address === "object" && address !== null
211+ ? {
212+ address : address . address ,
213+ family : address . family ,
214+ port : address . port ,
215+ }
216+ : {
217+ address,
218+ family : null ,
219+ port : null ,
220+ } ;
221+
222+ probe . close ( ( cause ) => {
223+ if ( cause ) {
224+ settle (
225+ Effect . fail (
226+ new LoopbackPortReleaseError ( {
227+ host,
228+ port : addressDetails . port ?? 0 ,
229+ cause,
230+ } ) ,
231+ ) ,
232+ ) ;
233+ return ;
234+ }
235+ if ( addressDetails . port !== null && addressDetails . port > 0 ) {
236+ settle ( Effect . succeed ( addressDetails . port ) ) ;
237+ return ;
238+ }
239+ settle (
240+ Effect . fail (
241+ new LoopbackPortAddressUnavailableError ( {
242+ host,
243+ ...addressDetails ,
244+ } ) ,
245+ ) ,
246+ ) ;
247+ } ) ;
178248 } ) ;
179- } ) ;
249+ } catch ( cause ) {
250+ settle ( Effect . fail ( new LoopbackPortListenError ( { host, cause } ) ) ) ;
251+ }
180252
181253 return Effect . sync ( ( ) => {
182254 closeServer ( probe ) ;
0 commit comments