@@ -4,7 +4,6 @@ import { generateRequestId } from '@/lib/core/utils/request'
44import {
55 checkWebhookPreprocessing ,
66 findAllWebhooksForPath ,
7- formatProviderErrorResponse ,
87 handlePreDeploymentVerification ,
98 handleProviderChallenges ,
109 handleProviderReachabilityTest ,
@@ -74,57 +73,35 @@ export async function POST(
7473 const responses : NextResponse [ ] = [ ]
7574
7675 for ( const { webhook : foundWebhook , workflow : foundWorkflow } of webhooksForPath ) {
77- const authError = await verifyProviderAuth (
78- foundWebhook ,
79- foundWorkflow ,
80- request ,
81- rawBody ,
82- requestId
83- )
76+ // Short-circuit: reachability test is a quick body-only check
77+ const reachabilityResponse = handleProviderReachabilityTest ( foundWebhook , body , requestId )
78+ if ( reachabilityResponse ) {
79+ return reachabilityResponse
80+ }
81+
82+ // Parallelize auth verification with preprocessing — they are independent
83+ // checkWebhookPreprocessing has its own try/catch and always returns WebhookPreprocessingResult
84+ const [ authError , preprocessResult ] = await Promise . all ( [
85+ verifyProviderAuth ( foundWebhook , foundWorkflow , request , rawBody , requestId ) ,
86+ checkWebhookPreprocessing ( foundWorkflow , foundWebhook , requestId ) ,
87+ ] )
88+
8489 if ( authError ) {
85- // For multi-webhook, log and continue to next webhook
8690 if ( webhooksForPath . length > 1 ) {
8791 logger . warn ( `[${ requestId } ] Auth failed for webhook ${ foundWebhook . id } , continuing to next` )
8892 continue
8993 }
9094 return authError
9195 }
9296
93- const reachabilityResponse = handleProviderReachabilityTest ( foundWebhook , body , requestId )
94- if ( reachabilityResponse ) {
95- // Reachability test should return immediately for the first webhook
96- return reachabilityResponse
97- }
98-
99- let preprocessError : NextResponse | null = null
100- try {
101- preprocessError = await checkWebhookPreprocessing ( foundWorkflow , foundWebhook , requestId )
102- if ( preprocessError ) {
103- if ( webhooksForPath . length > 1 ) {
104- logger . warn (
105- `[${ requestId } ] Preprocessing failed for webhook ${ foundWebhook . id } , continuing to next`
106- )
107- continue
108- }
109- return preprocessError
110- }
111- } catch ( error ) {
112- logger . error ( `[${ requestId } ] Unexpected error during webhook preprocessing` , {
113- error : error instanceof Error ? error . message : String ( error ) ,
114- stack : error instanceof Error ? error . stack : undefined ,
115- webhookId : foundWebhook . id ,
116- workflowId : foundWorkflow . id ,
117- } )
118-
97+ if ( preprocessResult . error ) {
11998 if ( webhooksForPath . length > 1 ) {
99+ logger . warn (
100+ `[${ requestId } ] Preprocessing failed for webhook ${ foundWebhook . id } , continuing to next`
101+ )
120102 continue
121103 }
122-
123- return formatProviderErrorResponse (
124- foundWebhook ,
125- 'An unexpected error occurred during preprocessing' ,
126- 500
127- )
104+ return preprocessResult . error
128105 }
129106
130107 if ( foundWebhook . blockId ) {
@@ -152,6 +129,7 @@ export async function POST(
152129 const response = await queueWebhookExecution ( foundWebhook , foundWorkflow , body , request , {
153130 requestId,
154131 path,
132+ actorUserId : preprocessResult . actorUserId ,
155133 } )
156134 responses . push ( response )
157135 }
0 commit comments