refactor: fetch timeout

This commit is contained in:
Gabe
2025-06-27 12:31:32 +08:00
parent eaa47af269
commit adbb421b7b

View File

@@ -1,8 +1,7 @@
import { isExt, isGm } from "./client"; import { isExt, isGm } from "./client";
import { sendBgMsg } from "./msg"; import { sendBgMsg } from "./msg";
import { taskPool } from "./pool"; import { taskPool } from "./pool";
import { storage,getSettingWithDefault } from "./storage"; import { getSettingWithDefault } from "./storage";
import { import {
MSG_FETCH, MSG_FETCH,
@@ -17,8 +16,6 @@ import { genTransReq } from "../apis/trans";
import { kissLog } from "./log"; import { kissLog } from "./log";
import { blobToBase64 } from "./utils"; import { blobToBase64 } from "./utils";
let TIMEOUT = HTTP_TIMEOUT;
/** /**
* 构造缓存 request * 构造缓存 request
* @param {*} input * @param {*} input
@@ -43,8 +40,7 @@ const newCacheReq = async (input, init) => {
* @param {*} init * @param {*} init
* @returns * @returns
*/ */
export const fetchGM = async (input, { method = "GET", headers, body } = {}) =>{ export const fetchGM = async (input, { method = "GET", headers, body, timeout = HTTP_TIMEOUT } = {}) =>
TIMEOUT = (await getSettingWithDefault()).httpTimeout;
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
GM.xmlHttpRequest({ GM.xmlHttpRequest({
method, method,
@@ -52,7 +48,7 @@ export const fetchGM = async (input, { method = "GET", headers, body } = {}) =>{
headers, headers,
data: body, data: body,
// withCredentials: true, // withCredentials: true,
timeout: TIMEOUT, timeout,
onload: ({ response, responseHeaders, status, statusText }) => { onload: ({ response, responseHeaders, status, statusText }) => {
const headers = {}; const headers = {};
responseHeaders.split("\n").forEach((line) => { responseHeaders.split("\n").forEach((line) => {
@@ -71,7 +67,7 @@ export const fetchGM = async (input, { method = "GET", headers, body } = {}) =>{
onerror: reject, onerror: reject,
}); });
}); });
}
/** /**
* 发起请求 * 发起请求
* @param {*} param0 * @param {*} param0
@@ -86,6 +82,14 @@ export const fetchPatcher = async (input, init, transOpts, apiSetting) => {
throw new Error("url is empty"); throw new Error("url is empty");
} }
let timeout = HTTP_TIMEOUT;
try {
// todo: 不必每次都查询,缓存参数
timeout = (await getSettingWithDefault()).httpTimeout;
} catch (err) {
//
}
if (isGm) { if (isGm) {
let info; let info;
if (window.KISS_GM) { if (window.KISS_GM) {
@@ -101,6 +105,9 @@ export const fetchPatcher = async (input, init, transOpts, apiSetting) => {
const isSafe = connects.find((item) => url.hostname.endsWith(item)); const isSafe = connects.find((item) => url.hostname.endsWith(item));
if (isSafe) { if (isSafe) {
// todo: 自定义接口 init 可能包含了 signal
Object.assign(init, { timeout });
const { body, headers, status, statusText } = window.KISS_GM const { body, headers, status, statusText } = window.KISS_GM
? await window.KISS_GM.fetch(input, init) ? await window.KISS_GM.fetch(input, init)
: await fetchGM(input, init); : await fetchGM(input, init);
@@ -113,12 +120,8 @@ export const fetchPatcher = async (input, init, transOpts, apiSetting) => {
} }
} }
//const kiss_setting = await storage.get("KISS-Translator_setting");
//TIMEOUT = JSON.parse(kiss_setting)?.httpTimeout || TIMEOUT;
TIMEOUT = (await getSettingWithDefault()).httpTimeout;
console.log("TIMEOUT", TIMEOUT);
if (AbortSignal?.timeout && !init.signal) { if (AbortSignal?.timeout && !init.signal) {
Object.assign(init, { signal: AbortSignal.timeout(TIMEOUT) }); Object.assign(init, { signal: AbortSignal.timeout(timeout) });
} }
return fetch(input, init); return fetch(input, init);