@@ -17,6 +17,37 @@ const facilitatorTimeout = env.FACILITATOR_REQUEST_TIMEOUT || 20000;
1717
1818type FacilitatorMethod = 'verify' | 'settle' ;
1919
20+ async function fetchWithTimeout (
21+ url : string ,
22+ options : RequestInit ,
23+ timeoutMs : number ,
24+ method : FacilitatorMethod
25+ ) : Promise < Response > {
26+ const abortController = new AbortController ( ) ;
27+ const timeoutId = setTimeout ( ( ) => {
28+ abortController . abort ( ) ;
29+ logger . warn (
30+ `Proxy facilitator ${ method } request timed out after ${ timeoutMs } ms`
31+ ) ;
32+ } , Number ( timeoutMs ) ) ;
33+
34+ try {
35+ const res = await fetch ( url , {
36+ ...options ,
37+ signal : abortController . signal ,
38+ } ) ;
39+ clearTimeout ( timeoutId ) ;
40+ return res ;
41+ } catch ( error ) {
42+ clearTimeout ( timeoutId ) ;
43+ logMetric ( 'facilitator_proxy_failure' , 1 , {
44+ method,
45+ error : error instanceof Error ? error . message : 'unknown' ,
46+ } ) ;
47+ throw new FacilitatorProxyError ( ) ;
48+ }
49+ }
50+
2051/**
2152 * Executes a facilitator request via the proxy router
2253 *
@@ -53,21 +84,16 @@ export async function facilitatorProxy<
5384 paymentRequirements : toJsonSafe ( paymentRequirements ) ,
5485 } ;
5586
56- const abortController = new AbortController ( ) ;
57- const timeoutId = setTimeout ( ( ) => {
58- abortController . abort ( ) ;
59- logger . warn (
60- `Proxy facilitator ${ method } request timed out after ${ facilitatorTimeout } ms`
61- ) ;
62- } , Number ( facilitatorTimeout ) ) ;
63- const res = await fetch ( `${ PROXY_FACILITATOR_URL } /${ method } ` , {
64- method : 'POST' ,
65- headers,
66- body : JSON . stringify ( requestBody ) ,
67- signal : abortController . signal ,
68- } ) ;
69-
70- clearTimeout ( timeoutId ) ;
87+ const res = await fetchWithTimeout (
88+ `${ PROXY_FACILITATOR_URL } /${ method } ` ,
89+ {
90+ method : 'POST' ,
91+ headers,
92+ body : JSON . stringify ( requestBody ) ,
93+ } ,
94+ facilitatorTimeout ,
95+ method
96+ ) ;
7197
7298 if ( res . status !== 200 ) {
7399 logMetric ( 'facilitator_proxy_failure' , 1 , {
0 commit comments