Skip to content

Commit

Permalink
chore: timeout rpc tracking and track errors
Browse files Browse the repository at this point in the history
  • Loading branch information
frontendphil committed Jan 28, 2025
1 parent 24bc59a commit a909dfe
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions deployables/extension/src/background/rpcTracking.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sentry } from '@/sentry'
import { sendMessageToTab } from '@/utils'
import { RpcMessageType } from '@zodiac/messages'
import type { ChainId } from 'ser-kit'
Expand Down Expand Up @@ -41,11 +42,13 @@ export const trackRequests = (): TrackRequestsResult => {

chrome.webRequest.onBeforeRequest.addListener(
(details) => {
trackRequest(state, details).then(({ newEndpoint }) => {
if (newEndpoint) {
onNewRpcEndpointDetected.callListeners()
}
})
trackRequest(state, details)
.then(({ newEndpoint }) => {
if (newEndpoint) {
onNewRpcEndpointDetected.callListeners()
}
})
.catch((error) => sentry.captureException(error))
},
{
urls: ['<all_urls>'],
Expand Down Expand Up @@ -137,7 +140,9 @@ const detectNetworkOfRpcUrl = async (
if (!chainIdPromiseByRpcUrl.has(url)) {
chainIdPromiseByRpcUrl.set(
url,
sendMessageToTab(tabId, { type: RpcMessageType.PROBE_CHAIN_ID, url }),
timeout(
sendMessageToTab(tabId, { type: RpcMessageType.PROBE_CHAIN_ID, url }),
),
)
}

Expand Down Expand Up @@ -179,3 +184,9 @@ const trackRpcUrl = (
urls.add(url)
}
}

const timeout = <T>(promise: Promise<T>) =>
Promise.race([
promise,
new Promise<T>((_, reject) => setTimeout(() => reject(), 10_000)),
])

0 comments on commit a909dfe

Please sign in to comment.