|
1 | 1 | import { computed, type ComputedRef, type Ref, ref, watch } from "vue"; |
2 | 2 |
|
3 | 3 | import { useStorage } from "@vueuse/core"; |
4 | | -import { FetchRequest, JsonRpcProvider } from "ethers"; |
| 4 | +import { FetchRequest, JsonRpcProvider, makeError } from "ethers"; |
5 | 5 |
|
6 | 6 | import useEnvironmentConfig from "./useEnvironmentConfig"; |
7 | 7 | import { DEFAULT_NETWORK } from "./useRuntimeConfig"; |
@@ -41,13 +41,35 @@ function getRpcRequest(network: NetworkConfig) { |
41 | 41 | } |
42 | 42 |
|
43 | 43 | const request = new FetchRequest(`${network.apiUrl}/rpc`); |
44 | | - request.getUrlFunc = async (req) => { |
45 | | - const response = await fetch(req.url, { |
46 | | - method: req.method, |
47 | | - headers: req.headers, |
48 | | - body: req.body, |
49 | | - credentials: "include", |
| 44 | + // Overriding getUrlFunc replaces ethers' own fetch, which arms `req.timeout` and forwards |
| 45 | + // cancellation, so both are reproduced here to keep requests bounded and abortable. |
| 46 | + request.getUrlFunc = async (req, signal) => { |
| 47 | + const controller = new AbortController(); |
| 48 | + let abortError: Error | null = null; |
| 49 | + const timer = setTimeout(() => { |
| 50 | + abortError = makeError("request timeout", "TIMEOUT"); |
| 51 | + controller.abort(); |
| 52 | + }, req.timeout); |
| 53 | + signal?.addListener(() => { |
| 54 | + abortError = makeError("request cancelled", "CANCELLED"); |
| 55 | + controller.abort(); |
50 | 56 | }); |
| 57 | + |
| 58 | + let response: Response; |
| 59 | + try { |
| 60 | + response = await fetch(req.url, { |
| 61 | + method: req.method, |
| 62 | + headers: req.headers, |
| 63 | + body: req.body, |
| 64 | + credentials: "include", |
| 65 | + signal: controller.signal, |
| 66 | + }); |
| 67 | + } catch (error) { |
| 68 | + throw abortError ?? error; |
| 69 | + } finally { |
| 70 | + clearTimeout(timer); |
| 71 | + } |
| 72 | + |
51 | 73 | const headers: Record<string, string> = {}; |
52 | 74 | response.headers.forEach((value, key) => { |
53 | 75 | headers[key.toLowerCase()] = value; |
|
0 commit comments