using stream: schema to fetch in App

This commit is contained in:
lloydzhou
2024-09-28 15:05:41 +08:00
parent d84d51b475
commit 2d920f7ccc
7 changed files with 204 additions and 122 deletions

1
app/global.d.ts vendored
View File

@@ -12,6 +12,7 @@ declare module "*.svg";
declare interface Window {
__TAURI__?: {
convertFileSrc(url: string, protocol?: string): string;
writeText(text: string): Promise<void>;
invoke(command: string, payload?: Record<string, unknown>): Promise<any>;
dialog: {

View File

@@ -3,6 +3,7 @@ import { showToast } from "./components/ui-lib";
import Locale from "./locales";
import { RequestMessage } from "./client/api";
import { ServiceProvider } from "./constant";
import { fetch } from "./utils/stream";
export function trimTopic(topic: string) {
// Fix an issue where double quotes still show in the Indonesian language
@@ -286,46 +287,6 @@ export function showPlugins(provider: ServiceProvider, model: string) {
return false;
}
export function fetch(
url: string,
options?: Record<string, unknown>,
): Promise<any> {
if (window.__TAURI__) {
const tauriUri = window.__TAURI__.convertFileSrc(url, "sse");
return window.fetch(tauriUri, options).then((r) => {
// 1. create response,
// TODO using event to get status and statusText and headers
const { status, statusText } = r;
const { readable, writable } = new TransformStream();
const res = new Response(readable, { status, statusText });
// 2. call fetch_read_body multi times, and write to Response.body
const writer = writable.getWriter();
let unlisten;
window.__TAURI__.event
.listen("sse-response", (e) => {
const { id, payload } = e;
console.log("event", id, payload);
writer.ready.then(() => {
if (payload !== 0) {
writer.write(new Uint8Array(payload));
} else {
writer.releaseLock();
writable.close();
unlisten && unlisten();
}
});
})
.then((u) => (unlisten = u));
return res;
});
}
return window.fetch(url, options);
}
if (undefined !== window) {
window.tauriFetch = fetch;
}
export function adapter(config: Record<string, unknown>) {
const { baseURL, url, params, ...rest } = config;
const path = baseURL ? `${baseURL}${url}` : url;

100
app/utils/stream.ts Normal file
View File

@@ -0,0 +1,100 @@
// using tauri register_uri_scheme_protocol, register `stream:` protocol
// see src-tauri/src/stream.rs, and src-tauri/src/main.rs
// 1. window.fetch(`stream://localhost/${fetchUrl}`), get request_id
// 2. listen event: `stream-response` multi times to get response headers and body
type ResponseEvent = {
id: number;
payload: {
request_id: number;
status?: number;
error?: string;
name?: string;
value?: string;
chunk?: number[];
};
};
export function fetch(url: string, options?: RequestInit): Promise<any> {
if (window.__TAURI__) {
const tauriUri = window.__TAURI__.convertFileSrc(url, "stream");
const { signal, ...rest } = options || {};
return window
.fetch(tauriUri, rest)
.then((r) => r.text())
.then((rid) => parseInt(rid))
.then((request_id: number) => {
// 1. using event to get status and statusText and headers, and resolve it
let resolve: Function | undefined;
let reject: Function | undefined;
let status: number;
let writable: WritableStream | undefined;
let writer: WritableStreamDefaultWriter | undefined;
const headers = new Headers();
let unlisten: Function | undefined;
if (signal) {
signal.addEventListener("abort", () => {
// Reject the promise with the abort reason.
unlisten && unlisten();
reject && reject(signal.reason);
});
}
// @ts-ignore 2. listen response multi times, and write to Response.body
window.__TAURI__.event
.listen("stream-response", (e: ResponseEvent) => {
const { id, payload } = e;
const {
request_id: rid,
status: _status,
name,
value,
error,
chunk,
} = payload;
if (request_id != rid) {
return;
}
/**
* 1. get status code
* 2. get headers
* 3. start get body, then resolve response
* 4. get body chunk
*/
if (error) {
unlisten && unlisten();
return reject && reject(error);
} else if (_status) {
status = _status;
} else if (name && value) {
headers.append(name, value);
} else if (chunk) {
if (resolve) {
const ts = new TransformStream();
writable = ts.writable;
writer = writable.getWriter();
resolve(new Response(ts.readable, { status, headers }));
resolve = undefined;
}
writer &&
writer.ready.then(() => {
writer && writer.write(new Uint8Array(chunk));
});
} else if (_status === 0) {
// end of body
unlisten && unlisten();
writer &&
writer.ready.then(() => {
writer && writer.releaseLock();
writable && writable.close();
});
}
})
.then((u: Function) => (unlisten = u));
return new Promise(
(_resolve, _reject) => ([resolve, reject] = [_resolve, _reject]),
);
});
}
return window.fetch(url, options);
}