Skip to content
Merged
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
19 changes: 15 additions & 4 deletions src/lib/secret-stream-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { fetch as secretStreamFetchOrig } from 'secret-stream-http'
import { errors } from './errors.js'
import { isArrayReadonly } from './utils.js'

const CONNECTION_TIMEOUT_MS = 5000 // 5s

/**
* A wrapper around secret-stream-http's fetch that tries multiple URLs until one works.
* This is useful when the server has multiple IPs for different network interfaces.
Expand All @@ -21,15 +23,24 @@ export async function secretStreamFetch(
// not all of them may be on the same network as us, so try each URL until
// one works
for (const url of urls) {
const controller = new AbortController()
const timeout = setTimeout(() => {
controller.abort()
}, CONNECTION_TIMEOUT_MS)
const signal = options?.signal
? AbortSignal.any([options.signal, controller.signal])
: controller.signal
try {
response = (await secretStreamFetchOrig(
url,
options,
)) as unknown as Response // Subtle difference bewteen Undici fetch Response and whatwg Response
response = (await secretStreamFetchOrig(url, {
...options,
signal,
})) as unknown as Response // Subtle difference bewteen Undici fetch Response and whatwg Response
break // Exit loop on successful fetch
} catch (err) {
error = err
// Ignore errors and try the next URL
} finally {
clearTimeout(timeout)
}
}
if (!response) {
Expand Down
Loading