mirror of
				https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
				synced 2025-11-04 16:23:41 +08:00 
			
		
		
		
	stash code
This commit is contained in:
		@@ -21,7 +21,12 @@ type StreamResponse = {
 | 
			
		||||
 | 
			
		||||
export function fetch(url: string, options?: RequestInit): Promise<any> {
 | 
			
		||||
  if (window.__TAURI__) {
 | 
			
		||||
    const { signal, method = "GET", headers = {}, body = [] } = options || {};
 | 
			
		||||
    const {
 | 
			
		||||
      signal,
 | 
			
		||||
      method = "GET",
 | 
			
		||||
      headers: _headers = {},
 | 
			
		||||
      body = [],
 | 
			
		||||
    } = options || {};
 | 
			
		||||
    let unlisten: Function | undefined;
 | 
			
		||||
    let request_id = 0;
 | 
			
		||||
    const ts = new TransformStream();
 | 
			
		||||
@@ -32,10 +37,10 @@ export function fetch(url: string, options?: RequestInit): Promise<any> {
 | 
			
		||||
      writer.ready.then(() => {
 | 
			
		||||
        try {
 | 
			
		||||
          writer.releaseLock();
 | 
			
		||||
          ts.writable.close();
 | 
			
		||||
        } catch (e) {
 | 
			
		||||
          console.error(e);
 | 
			
		||||
        }
 | 
			
		||||
        ts.writable.close();
 | 
			
		||||
      });
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
@@ -61,6 +66,19 @@ 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",
 | 
			
		||||
      "User-Agent": navigator.userAgent,
 | 
			
		||||
    };
 | 
			
		||||
    for (const item of new Headers(_headers || {})) {
 | 
			
		||||
      headers[item[0]] = item[1];
 | 
			
		||||
    }
 | 
			
		||||
    return window.__TAURI__
 | 
			
		||||
      .invoke("stream_fetch", {
 | 
			
		||||
        method,
 | 
			
		||||
 
 | 
			
		||||
@@ -53,15 +53,33 @@ pub async fn stream_fetch(
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  let mut _headers = HeaderMap::new();
 | 
			
		||||
  for (key, value) in headers {
 | 
			
		||||
      _headers.insert(key.parse::<HeaderName>().unwrap(), value.parse().unwrap());
 | 
			
		||||
  for (key, value) in &headers {
 | 
			
		||||
    _headers.insert(key.parse::<HeaderName>().unwrap(), value.parse().unwrap());
 | 
			
		||||
  }
 | 
			
		||||
  let body = bytes::Bytes::from(body);
 | 
			
		||||
 | 
			
		||||
  let response_future = Client::new().request(
 | 
			
		||||
    method.parse::<reqwest::Method>().map_err(|err| format!("failed to parse method: {}", err))?,
 | 
			
		||||
  println!("method: {:?}", method);
 | 
			
		||||
  println!("url: {:?}", url);
 | 
			
		||||
  println!("headers: {:?}", headers);
 | 
			
		||||
  println!("headers: {:?}", _headers);
 | 
			
		||||
 | 
			
		||||
  let method = method.parse::<reqwest::Method>().map_err(|err| format!("failed to parse method: {}", err))?;
 | 
			
		||||
  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)
 | 
			
		||||
    .build()
 | 
			
		||||
    .map_err(|err| format!("failed to generate client: {}", err))?;
 | 
			
		||||
 | 
			
		||||
  let mut request = client.request(
 | 
			
		||||
    method.clone(),
 | 
			
		||||
    url.parse::<reqwest::Url>().map_err(|err| format!("failed to parse url: {}", err))?
 | 
			
		||||
  ).headers(_headers).body(body).send();
 | 
			
		||||
  );
 | 
			
		||||
 | 
			
		||||
  if method == reqwest::Method::POST {
 | 
			
		||||
    let body = bytes::Bytes::from(body);
 | 
			
		||||
    println!("body: {:?}", body);
 | 
			
		||||
    request = request.body(body);
 | 
			
		||||
  }
 | 
			
		||||
  let response_future = request.send();
 | 
			
		||||
 | 
			
		||||
  let res = response_future.await;
 | 
			
		||||
  let response = match res {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user