This commit is contained in:
lloydzhou 2024-09-30 00:38:30 +08:00
parent 7df308d655
commit b5f6e5a598
3 changed files with 16 additions and 22 deletions

View File

@ -7,7 +7,7 @@ import yaml from "js-yaml";
import { adapter, getOperationId } from "../utils"; import { adapter, getOperationId } from "../utils";
import { useAccessStore } from "./access"; import { useAccessStore } from "./access";
const isApp = getClientConfig()?.isApp; const isApp = getClientConfig()?.isApp !== false;
export type Plugin = { export type Plugin = {
id: string; id: string;

View File

@ -32,15 +32,13 @@ export function fetch(url: string, options?: RequestInit): Promise<any> {
const ts = new TransformStream(); const ts = new TransformStream();
const writer = ts.writable.getWriter(); const writer = ts.writable.getWriter();
let closed = false;
const close = () => { const close = () => {
if (closed) return;
closed = true;
unlisten && unlisten(); unlisten && unlisten();
writer.ready.then(() => { writer.ready.then(() => {
try { writer.close().catch((e) => console.error(e));
writer.releaseLock();
ts.writable.close();
} catch (e) {
console.error(e);
}
}); });
}; };
@ -55,9 +53,8 @@ export function fetch(url: string, options?: RequestInit): Promise<any> {
return; return;
} }
if (chunk) { if (chunk) {
writer &&
writer.ready.then(() => { writer.ready.then(() => {
writer && writer.write(new Uint8Array(chunk)); writer.write(new Uint8Array(chunk));
}); });
} else if (status === 0) { } else if (status === 0) {
// end of body // end of body
@ -67,13 +64,8 @@ export function fetch(url: string, options?: RequestInit): Promise<any> {
.then((u: Function) => (unlisten = u)); .then((u: Function) => (unlisten = u));
const headers = { const headers = {
Accept: "*", Accept: "application/json, text/plain, */*",
Connection: "close", "Accept-Language": "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7",
Origin: "http://localhost:3000",
Referer: "http://localhost:3000/",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site",
"User-Agent": navigator.userAgent, "User-Agent": navigator.userAgent,
}; };
for (const item of new Headers(_headers || {})) { for (const item of new Headers(_headers || {})) {
@ -81,7 +73,7 @@ export function fetch(url: string, options?: RequestInit): Promise<any> {
} }
return window.__TAURI__ return window.__TAURI__
.invoke("stream_fetch", { .invoke("stream_fetch", {
method, method: method.toUpperCase(),
url, url,
headers, headers,
// TODO FormData // TODO FormData

View File

@ -8,7 +8,7 @@ use reqwest::header::{HeaderName, HeaderMap};
static mut REQUEST_COUNTER: u32 = 0; static mut REQUEST_COUNTER: u32 = 0;
#[derive(Clone, serde::Serialize)] #[derive(Debug, Clone, serde::Serialize)]
pub struct StreamResponse { pub struct StreamResponse {
request_id: u32, request_id: u32,
status: u16, status: u16,
@ -66,6 +66,7 @@ pub async fn stream_fetch(
let client = Client::builder() 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") .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) .default_headers(_headers)
.redirect(reqwest::redirect::Policy::limited(3))
.build() .build()
.map_err(|err| format!("failed to generate client: {}", err))?; .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(); window.emit(event_name, ChunkPayload{ request_id, chunk: bytes }).unwrap();
} }
Err(err) => { Err(err) => {
println!("Error: {:?}", err); println!("Error chunk: {:?}", err);
} }
} }
} }
@ -119,7 +120,7 @@ pub async fn stream_fetch(
} }
} }
Err(err) => { Err(err) => {
println!("Error: {:?}", err.source().expect("REASON").to_string()); println!("Error response: {:?}", err.source().expect("REASON").to_string());
StreamResponse { StreamResponse {
request_id, request_id,
status: 599, status: 599,
@ -128,6 +129,7 @@ pub async fn stream_fetch(
} }
} }
}; };
println!("Response: {:?}", response);
Ok(response) Ok(response)
} }