MCPcopy
hub / github.com/NitroRCr/AIaW / fetch

Function fetch

src/utils/tauri-stream.ts:29–108  ·  view source on GitHub ↗
(url: string, options?: RequestInit)

Source from the content-addressed store, hash-verified

27};
28
29export function fetch(url: string, options?: RequestInit): Promise<Response> {
30 if (!IsTauri) return window.fetch(url, options)
31 const {
32 signal,
33 method = 'GET',
34 headers: _headers = {},
35 body = []
36 } = options || {}
37 let unlisten: Function | undefined
38 let setRequestId: Function | undefined
39 const requestIdPromise = new Promise((resolve) => (setRequestId = resolve))
40 const ts = new TransformStream()
41 const writer = ts.writable.getWriter()
42
43 let closed = false
44 const close = () => {
45 if (closed) return
46 closed = true
47 unlisten && unlisten()
48 writer.ready.then(() => {
49 writer.close().catch((e) => console.error(e))
50 })
51 }
52
53 if (signal) {
54 signal.addEventListener('abort', () => close())
55 }
56 // @ts-ignore 2. listen response multi times, and write to Response.body
57 listen('stream-response', (e: ResponseEvent) =>
58 requestIdPromise.then((request_id) => {
59 const { request_id: rid, chunk, status } = e?.payload || {}
60 if (request_id != rid) {
61 return
62 }
63 if (chunk) {
64 writer.ready.then(() => {
65 writer.write(new Uint8Array(chunk))
66 })
67 } else if (status === 0) {
68 // end of body
69 close()
70 }
71 })
72 ).then((u: Function) => (unlisten = u))
73
74 const headers = new Headers({
75 Accept: 'application/json, text/plain, */*',
76 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7',
77 'User-Agent': navigator.userAgent
78 })
79 for (const [key, value] of new Headers(_headers || {})) {
80 headers.set(key, value)
81 }
82 return invoke('stream_fetch', {
83 method: method.toUpperCase(),
84 url,
85 headers: Object.fromEntries(headers.entries()),
86 // TODO FormData

Callers 11

updatefoundFunction · 0.85
corsFetchFunction · 0.85
checkUpdateFunction · 0.85
parseDocFunction · 0.85
openaiGetModelListFunction · 0.85
values.tsFile · 0.85
searchFunction · 0.85
crawlFunction · 0.85
predictFunction · 0.85
orderFunction · 0.85
installFunction · 0.85

Calls 1

closeFunction · 0.85

Tested by

no test coverage detected