Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"emoji-regex": "10.6.0",
"form-data": "4.0.4",
"ts-custom-error": "^3.2.0",
"undici": "^7.16.0",
"uuid": "11.1.0",
"zod": "4.1.12"
},
Expand Down
12 changes: 12 additions & 0 deletions src/utils/fetch-with-retry.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Agent } from 'undici'
import type { HttpResponse, RetryConfig, CustomFetch, CustomFetchResponse } from '../types/http'
import { isNetworkError } from '../types/http'

Expand All @@ -14,6 +15,15 @@ const DEFAULT_RETRY_CONFIG: RetryConfig = {
},
}

/**
* HTTP agent with keepAlive disabled to prevent hanging connections
* This ensures the process exits immediately after requests complete
*/
const httpAgent = new Agent({
keepAliveTimeout: 1, // Close connections after 1ms of idle time
keepAliveMaxTimeout: 1, // Maximum time to keep connections alive
})

/**
* Converts Headers object to a plain object
*/
Expand Down Expand Up @@ -113,6 +123,8 @@ export async function fetchWithRetry<T = unknown>(args: {
const nativeResponse = await fetch(url, {
...fetchOptions,
signal: requestSignal,
// @ts-expect-error - dispatcher is a valid option for Node.js fetch but not in the TS types
dispatcher: httpAgent,
})
fetchResponse = convertResponseToCustomFetch(nativeResponse)
}
Expand Down