@@ -177,6 +177,34 @@ function createReadyFetchProxyProc(fetchMessage: { url: string; optionsJson?: st
177177 return proc
178178}
179179
180+ /** A worker whose code starts a POST it never awaits and returns at once. */
181+ function createUnawaitedFetchProc ( ) : MockProc {
182+ const proc = createBaseProc ( )
183+ proc . send = ( message : unknown ) => {
184+ const msg = message as { type ?: string ; executionId ?: number }
185+ if ( msg . type === 'execute' ) {
186+ setImmediate ( ( ) => {
187+ proc . emit ( 'message' , {
188+ type : 'fetch' ,
189+ executionId : msg . executionId ,
190+ fetchId : 1 ,
191+ requestId : 'req-unawaited' ,
192+ url : 'https://hook.example/write' ,
193+ optionsJson : JSON . stringify ( { method : 'POST' } ) ,
194+ } )
195+ proc . emit ( 'message' , {
196+ type : 'result' ,
197+ executionId : msg . executionId ,
198+ result : { result : 'returned' , stdout : '' } ,
199+ } )
200+ } )
201+ }
202+ return true
203+ }
204+ setImmediate ( ( ) => proc . emit ( 'message' , { type : 'ready' } ) )
205+ return proc
206+ }
207+
180208const { mockSpawn, mockExecSync } = vi . hoisted ( ( ) => ( {
181209 mockSpawn : vi . fn ( ) ,
182210 mockExecSync : vi . fn ( ( ) => Buffer . from ( 'v23.11.0' ) ) ,
@@ -1101,33 +1129,9 @@ describe('isolated-vm scheduler', () => {
11011129 } )
11021130
11031131 it ( 'returns a result only once the fetch policy decided a request the code never awaited' , async ( ) => {
1104- const unawaitedFetchProc = ( ) => {
1105- const proc = createBaseProc ( )
1106- proc . send = ( message : unknown ) => {
1107- const msg = message as { type ?: string ; executionId ?: number }
1108- if ( msg . type === 'execute' ) {
1109- setImmediate ( ( ) => {
1110- proc . emit ( 'message' , {
1111- type : 'fetch' ,
1112- executionId : msg . executionId ,
1113- fetchId : 1 ,
1114- requestId : 'req-unawaited' ,
1115- url : 'https://hook.example/write' ,
1116- optionsJson : JSON . stringify ( { method : 'POST' } ) ,
1117- } )
1118- proc . emit ( 'message' , {
1119- type : 'result' ,
1120- executionId : msg . executionId ,
1121- result : { result : 'returned' , stdout : '' } ,
1122- } )
1123- } )
1124- }
1125- return true
1126- }
1127- setImmediate ( ( ) => proc . emit ( 'message' , { type : 'ready' } ) )
1128- return proc
1129- }
1130- const { executeInIsolatedVM } = await loadExecutionModule ( { spawns : [ unawaitedFetchProc ] } )
1132+ const { executeInIsolatedVM } = await loadExecutionModule ( {
1133+ spawns : [ createUnawaitedFetchProc ] ,
1134+ } )
11311135 inputValidationMockFns . mockValidateUrlWithDNS . mockResolvedValue ( {
11321136 isValid : true ,
11331137 resolvedIP : '93.184.216.34' ,
@@ -1167,6 +1171,44 @@ describe('isolated-vm scheduler', () => {
11671171 expect ( inputValidationMockFns . mockSecureFetchWithPinnedIP ) . not . toHaveBeenCalled ( )
11681172 } )
11691173
1174+ it ( 'stops waiting for an unawaited request’s decision at the execution’s deadline' , async ( ) => {
1175+ vi . useFakeTimers ( { toFake : [ 'setTimeout' , 'clearTimeout' , 'Date' ] } )
1176+ try {
1177+ const { executeInIsolatedVM } = await loadExecutionModule ( {
1178+ spawns : [ createUnawaitedFetchProc ] ,
1179+ } )
1180+ inputValidationMockFns . mockValidateUrlWithDNS . mockResolvedValue ( {
1181+ isValid : true ,
1182+ resolvedIP : '93.184.216.34' ,
1183+ originalHostname : 'hook.example' ,
1184+ } )
1185+ const decided : string [ ] = [ ]
1186+
1187+ const running = executeInIsolatedVM (
1188+ {
1189+ code : 'fetch("https://hook.example/write", { method: "POST" }); return "returned"' ,
1190+ params : { } ,
1191+ envVars : { } ,
1192+ contextVariables : { } ,
1193+ timeoutMs : 100 ,
1194+ requestId : 'req-unawaited-slow' ,
1195+ } ,
1196+ {
1197+ fetchPolicy : ( request ) => {
1198+ decided . push ( request . method )
1199+ return new Promise < 'stub' > ( ( ) => { } )
1200+ } ,
1201+ }
1202+ )
1203+ await vi . waitFor ( ( ) => expect ( decided ) . toEqual ( [ 'POST' ] ) )
1204+ await vi . advanceTimersByTimeAsync ( 1100 )
1205+
1206+ expect ( ( await running ) . result ) . toBe ( 'returned' )
1207+ } finally {
1208+ vi . useRealTimers ( )
1209+ }
1210+ } )
1211+
11701212 it ( 'does not log a resolved secret from a failed fetch URL or error' , async ( ) => {
11711213 const secret = 'fetch-path-secret-value'
11721214 const requestUrl = `https://example.com/${ secret } `
0 commit comments