fix: add timeout for fetch
This commit is contained in:
@@ -12,6 +12,8 @@ import {
|
|||||||
import { isBg } from "./browser";
|
import { isBg } from "./browser";
|
||||||
import { newCacheReq, newTransReq } from "./req";
|
import { newCacheReq, newTransReq } from "./req";
|
||||||
|
|
||||||
|
const TIMEOUT = 8000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 油猴脚本的请求封装
|
* 油猴脚本的请求封装
|
||||||
* @param {*} input
|
* @param {*} input
|
||||||
@@ -26,6 +28,7 @@ export const fetchGM = async (input, { method = "GET", headers, body } = {}) =>
|
|||||||
headers,
|
headers,
|
||||||
data: body,
|
data: body,
|
||||||
// withCredentials: true,
|
// withCredentials: true,
|
||||||
|
timeout: TIMEOUT,
|
||||||
onload: ({ response, responseHeaders, status, statusText, ...opts }) => {
|
onload: ({ response, responseHeaders, status, statusText, ...opts }) => {
|
||||||
const headers = {};
|
const headers = {};
|
||||||
responseHeaders.split("\n").forEach((line) => {
|
responseHeaders.split("\n").forEach((line) => {
|
||||||
@@ -86,6 +89,10 @@ export const fetchApi = async ({ input, init, transOpts, apiSetting }) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (AbortSignal?.timeout) {
|
||||||
|
Object.assign(init, { signal: AbortSignal.timeout(TIMEOUT) });
|
||||||
|
}
|
||||||
|
|
||||||
return fetch(input, init);
|
return fetch(input, init);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user