Skip to content

Commit 59177c6

Browse files
majiayu000claude
andcommitted
refactor(http): extract timeout parsing into shared helper function
Extract duplicated timeout parsing logic from handleInternalRequest and handleProxyRequest into a shared parseTimeout() helper function: - Define DEFAULT_TIMEOUT_MS and MAX_TIMEOUT_MS as module-level constants - Add parseTimeout() function with proper TSDoc documentation - Replace inline timeout parsing in both functions This addresses the code duplication noted in the Greptile review. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 467b529 commit 59177c6

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

apps/sim/tools/index.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ const MAX_REQUEST_BODY_SIZE_BYTES = 10 * 1024 * 1024 // 10MB
4949
const BODY_SIZE_LIMIT_ERROR_MESSAGE =
5050
'Request body size limit exceeded (10MB). The workflow data is too large to process. Try reducing the size of variables, inputs, or data being passed between blocks.'
5151

52+
/** Default timeout for HTTP requests in milliseconds (2 minutes) */
53+
const DEFAULT_TIMEOUT_MS = 120000
54+
55+
/** Maximum allowed timeout for HTTP requests in milliseconds (10 minutes) */
56+
const MAX_TIMEOUT_MS = 600000
57+
58+
/**
59+
* Parses and validates a timeout value from params
60+
* @param timeout - The timeout value (number or string) from params
61+
* @returns The validated timeout in milliseconds, capped at MAX_TIMEOUT_MS
62+
*/
63+
function parseTimeout(timeout: number | string | undefined): number {
64+
if (typeof timeout === 'number' && timeout > 0) {
65+
return Math.min(timeout, MAX_TIMEOUT_MS)
66+
}
67+
if (typeof timeout === 'string') {
68+
const parsed = Number.parseInt(timeout, 10)
69+
if (!Number.isNaN(parsed) && parsed > 0) {
70+
return Math.min(parsed, MAX_TIMEOUT_MS)
71+
}
72+
}
73+
return DEFAULT_TIMEOUT_MS
74+
}
75+
5276
/**
5377
* Validates request body size and throws a user-friendly error if exceeded
5478
* @param body - The request body string to check
@@ -650,19 +674,7 @@ async function handleInternalRequest(
650674
// Check request body size before sending to detect potential size limit issues
651675
validateRequestBodySize(requestParams.body, requestId, toolId)
652676

653-
// Determine timeout: use params.timeout if provided, otherwise default to 120000ms (2 min)
654-
// Max timeout is 600000ms (10 minutes) to prevent indefinite waits
655-
const DEFAULT_TIMEOUT_MS = 120000
656-
const MAX_TIMEOUT_MS = 600000
657-
let timeoutMs = DEFAULT_TIMEOUT_MS
658-
if (typeof params.timeout === 'number' && params.timeout > 0) {
659-
timeoutMs = Math.min(params.timeout, MAX_TIMEOUT_MS)
660-
} else if (typeof params.timeout === 'string') {
661-
const parsed = Number.parseInt(params.timeout, 10)
662-
if (!Number.isNaN(parsed) && parsed > 0) {
663-
timeoutMs = Math.min(parsed, MAX_TIMEOUT_MS)
664-
}
665-
}
677+
const timeoutMs = parseTimeout(params.timeout)
666678

667679
// Prepare request options with timeout signal
668680
const requestOptions: RequestInit = {
@@ -897,19 +909,7 @@ async function handleProxyRequest(
897909
// Check request body size before sending
898910
validateRequestBodySize(body, requestId, `proxy:${toolId}`)
899911

900-
// Determine timeout for proxy request: use params.timeout if provided, otherwise default
901-
// This ensures the proxy fetch itself doesn't timeout before the actual API request
902-
const DEFAULT_TIMEOUT_MS = 120000
903-
const MAX_TIMEOUT_MS = 600000
904-
let timeoutMs = DEFAULT_TIMEOUT_MS
905-
if (typeof params.timeout === 'number' && params.timeout > 0) {
906-
timeoutMs = Math.min(params.timeout, MAX_TIMEOUT_MS)
907-
} else if (typeof params.timeout === 'string') {
908-
const parsed = Number.parseInt(params.timeout, 10)
909-
if (!Number.isNaN(parsed) && parsed > 0) {
910-
timeoutMs = Math.min(parsed, MAX_TIMEOUT_MS)
911-
}
912-
}
912+
const timeoutMs = parseTimeout(params.timeout)
913913

914914
let response: Response
915915
try {

0 commit comments

Comments
 (0)