lloydzhou hace 1 año
padre
commit
b5f6e5a598
Se han modificado 3 ficheros con 16 adiciones y 22 borrados
  1. 1 1
      app/store/plugin.ts
  2. 10 18
      app/utils/stream.ts
  3. 5 3
      src-tauri/src/stream.rs

+ 1 - 1
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;

+ 10 - 18
app/utils/stream.ts

@@ -32,15 +32,13 @@ export function fetch(url: string, options?: RequestInit): Promise<any> {
     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<any> {
           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<any> {
       .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<any> {
     }
     return window.__TAURI__
       .invoke("stream_fetch", {
-        method,
+        method: method.toUpperCase(),
         url,
         headers,
         // TODO FormData

+ 5 - 3
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)
 }