diff --git a/app/store/plugin.ts b/app/store/plugin.ts index 40abdc8d9..b3d9f6d8c 100644 --- a/app/store/plugin.ts +++ b/app/store/plugin.ts @@ -7,7 +7,7 @@ import yaml from "js-yaml"; import { adapter, getOperationId } from "../utils"; import { useAccessStore } from "./access"; -const isApp = getClientConfig()?.isApp; +const isApp = getClientConfig()?.isApp !== false; export type Plugin = { id: string; diff --git a/app/utils/stream.ts b/app/utils/stream.ts index dd665e71c..625ecea1f 100644 --- a/app/utils/stream.ts +++ b/app/utils/stream.ts @@ -32,15 +32,13 @@ export function fetch(url: string, options?: RequestInit): Promise { const ts = new TransformStream(); const writer = ts.writable.getWriter(); + let closed = false; const close = () => { + if (closed) return; + closed = true; unlisten && unlisten(); writer.ready.then(() => { - try { - writer.releaseLock(); - ts.writable.close(); - } catch (e) { - console.error(e); - } + writer.close().catch((e) => console.error(e)); }); }; @@ -55,10 +53,9 @@ export function fetch(url: string, options?: RequestInit): Promise { return; } if (chunk) { - writer && - writer.ready.then(() => { - writer && writer.write(new Uint8Array(chunk)); - }); + writer.ready.then(() => { + writer.write(new Uint8Array(chunk)); + }); } else if (status === 0) { // end of body close(); @@ -67,13 +64,8 @@ export function fetch(url: string, options?: RequestInit): Promise { .then((u: Function) => (unlisten = u)); const headers = { - Accept: "*", - Connection: "close", - Origin: "http://localhost:3000", - Referer: "http://localhost:3000/", - "Sec-Fetch-Dest": "empty", - "Sec-Fetch-Mode": "cors", - "Sec-Fetch-Site": "cross-site", + Accept: "application/json, text/plain, */*", + "Accept-Language": "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7", "User-Agent": navigator.userAgent, }; for (const item of new Headers(_headers || {})) { @@ -81,7 +73,7 @@ export function fetch(url: string, options?: RequestInit): Promise { } return window.__TAURI__ .invoke("stream_fetch", { - method, + method: method.toUpperCase(), url, headers, // TODO FormData diff --git a/src-tauri/src/stream.rs b/src-tauri/src/stream.rs index 97989ba7e..a35e2a001 100644 --- a/src-tauri/src/stream.rs +++ b/src-tauri/src/stream.rs @@ -8,7 +8,7 @@ use reqwest::header::{HeaderName, HeaderMap}; static mut REQUEST_COUNTER: u32 = 0; -#[derive(Clone, serde::Serialize)] +#[derive(Debug, Clone, serde::Serialize)] pub struct StreamResponse { request_id: u32, status: u16, @@ -66,6 +66,7 @@ pub async fn stream_fetch( let client = Client::builder() .user_agent("Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15") .default_headers(_headers) + .redirect(reqwest::redirect::Policy::limited(3)) .build() .map_err(|err| format!("failed to generate client: {}", err))?; @@ -104,7 +105,7 @@ pub async fn stream_fetch( window.emit(event_name, ChunkPayload{ request_id, chunk: bytes }).unwrap(); } Err(err) => { - println!("Error: {:?}", err); + println!("Error chunk: {:?}", err); } } } @@ -119,7 +120,7 @@ pub async fn stream_fetch( } } Err(err) => { - println!("Error: {:?}", err.source().expect("REASON").to_string()); + println!("Error response: {:?}", err.source().expect("REASON").to_string()); StreamResponse { request_id, status: 599, @@ -128,6 +129,7 @@ pub async fn stream_fetch( } } }; + println!("Response: {:?}", response); Ok(response) }