@@ -33,12 +33,13 @@ function convertHeaders(
33
33
const waitForEndpoint = async (
34
34
endpoint : URL ,
35
35
deadline : number
36
- ) : Promise < { deadline : number } > => {
36
+ ) : Promise < { timeout : number } > => {
37
37
const start = Date . now ( ) ;
38
+ const timeout = deadline - start ;
38
39
39
40
// Stop recursing if the deadline has passed
40
- if ( deadline < start ) {
41
- return { deadline : 0 } ;
41
+ if ( timeout < 0 ) {
42
+ return { timeout : 0 } ;
42
43
}
43
44
44
45
const hostname = endpoint . hostname ;
@@ -55,13 +56,13 @@ const waitForEndpoint = async (
55
56
) ;
56
57
} ;
57
58
58
- socket . setTimeout ( Date . now ( ) - deadline ) ;
59
+ socket . setTimeout ( deadline - start ) ;
59
60
socket . once ( "error" , onError ) ;
60
61
socket . once ( "timeout" , onError ) ;
61
62
62
63
socket . connect ( port , hostname , ( ) => {
63
64
socket . end ( ) ;
64
- resolve ( { deadline : deadline - ( Date . now ( ) - start ) } ) ;
65
+ resolve ( { timeout : deadline - Date . now ( ) } ) ;
65
66
} ) ;
66
67
} ) ;
67
68
} ;
@@ -70,7 +71,7 @@ export const endpointProxy = async ({
70
71
requestId,
71
72
endpoint,
72
73
event,
73
- initialDeadline ,
74
+ deadline ,
74
75
} : EndpointRequest ) : Promise < EndpointResponse > => {
75
76
const {
76
77
requestContext,
@@ -82,15 +83,12 @@ export const endpointProxy = async ({
82
83
} = event ;
83
84
const method = requestContext . http . method ;
84
85
85
- log ( "Waiting for endpoint to start" , { endpoint, initialDeadline } ) ;
86
- const { deadline } = await waitForEndpoint ( endpoint , initialDeadline ) ;
87
- log ( "Endpoint started" , { endpoint, deadline } ) ;
86
+ log ( "Waiting for endpoint to start" , { endpoint, deadline } ) ;
87
+ const { timeout } = await waitForEndpoint ( endpoint , deadline ) ;
88
88
89
- if ( ! deadline ) {
89
+ if ( ! timeout ) {
90
90
throw new Error (
91
- `${ endpoint . toString ( ) } took longer than ${ Math . floor (
92
- initialDeadline / 1000
93
- ) } second(s) to start.`
91
+ `${ endpoint . toString ( ) } took longer than ${ deadline } milliseconds to start.`
94
92
) ;
95
93
}
96
94
@@ -102,14 +100,14 @@ export const endpointProxy = async ({
102
100
const decodedBody =
103
101
isBase64Encoded && rawBody ? Buffer . from ( rawBody , "base64" ) : rawBody ;
104
102
105
- log ( "Proxying request" , { url, method, rawHeaders, decodedBody, deadline } ) ;
103
+ log ( "Proxying request" , { url, method, rawHeaders, decodedBody, timeout } ) ;
106
104
107
105
const response = await axios . request ( {
108
106
method : method . toLowerCase ( ) ,
109
107
url : url . toString ( ) ,
110
108
headers : rawHeaders ,
111
109
data : decodedBody ,
112
- timeout : Date . now ( ) - deadline ,
110
+ timeout,
113
111
responseType : "arraybuffer" ,
114
112
} ) ;
115
113
0 commit comments