diff --git a/.env.demo b/.env.demo index 786570c4..9b01cb0d 100644 --- a/.env.demo +++ b/.env.demo @@ -68,6 +68,8 @@ OID4VP_AUTH_REQUEST_PROOF_REQUEST_EXPIRY=3600 APP_JSON_BODY_SIZE=5mb APP_URL_ENCODED_BODY_SIZE=5mb +# Webhook awaited expiry is in milliseconds +WEBHOOK_TIMEOUT_MS=10000 API_KEY=supersecret-that-too-16chars UPDATE_JWT_SECRET=false diff --git a/src/events/WebhookEvent.ts b/src/events/WebhookEvent.ts index 307bfb05..cdb4a353 100644 --- a/src/events/WebhookEvent.ts +++ b/src/events/WebhookEvent.ts @@ -2,16 +2,24 @@ import type { Logger } from '@credo-ts/core' import fetch from 'node-fetch' +const DEFAULT_TIMEOUT = 5000; + export const sendWebhookEvent = async ( webhookUrl: string, body: Record, logger: Logger, - timeoutMs = 5000, + timeoutMs?: number, ): Promise => { + + const envTimeout = parseInt(process.env.WEBHOOK_TIMEOUT_MS ?? '', 10) + const candidateTimeout = timeoutMs ?? envTimeout + const resolvedTimeout = candidateTimeout > 0 ? candidateTimeout : DEFAULT_TIMEOUT + + logger.info(`Sending webhook event to ${webhookUrl} with timeout of ${resolvedTimeout}ms`) // Abort the webhook send events if the request hangs-in for >5 secs // This can avoid failure of services due to bad webhook listners const controller = new AbortController() - const timeout = setTimeout(() => controller.abort(), timeoutMs) + const timeout = setTimeout(() => controller.abort(), resolvedTimeout) try { await fetch(webhookUrl, {