Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch stream compatibility enhance #1551

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion web/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
declare module 'lamejs';
declare module 'react-18-input-autosize';
declare module 'react-18-input-autosize';
declare module 'fetch-readablestream' {
export default function fetchReadableStream(url: string, options?: RequestInit): Promise<Response>
}
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"echarts": "^5.4.1",
"echarts-for-react": "^3.0.2",
"emoji-mart": "^5.5.2",
"fetch-readablestream": "^0.2.0",
"i18next": "^22.4.13",
"i18next-resources-to-backend": "^1.1.3",
"immer": "^9.0.19",
Expand Down
8 changes: 7 additions & 1 deletion web/service/base.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import fetchStream from 'fetch-readablestream'
import { API_PREFIX, IS_CE_EDITION, PUBLIC_API_PREFIX } from '@/config'
import Toast from '@/app/components/base/toast'
import type { MessageEnd, MessageReplace, ThoughtItem } from '@/app/components/app/chat/type'
import { isSupportNativeFetchStream } from '@/utils/stream'

const TIME_OUT = 100000
const supportNativeFetchStream = isSupportNativeFetchStream()

const ContentType = {
json: 'application/json',
Expand Down Expand Up @@ -220,6 +223,9 @@ const baseFetch = <T>(
if (body && bodyStringify)
options.body = JSON.stringify(body)

// for those do not support native fetch stream, we use fetch-readablestream as polyfill
const doFetch = supportNativeFetchStream ? globalThis.fetch : fetchStream

// Handle timeout
return Promise.race([
new Promise((resolve, reject) => {
Expand All @@ -228,7 +234,7 @@ const baseFetch = <T>(
}, TIME_OUT)
}),
new Promise((resolve, reject) => {
globalThis.fetch(urlWithPrefix, options as RequestInit)
doFetch(urlWithPrefix, options as RequestInit)
.then((res) => {
const resClone = res.clone()
// Error handler
Expand Down
21 changes: 21 additions & 0 deletions web/utils/stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// https://developer.chrome.com/articles/fetch-streaming-requests/#feature-detection
export const isSupportNativeFetchStream = () => {
const supportsRequestStreams = (() => {
let duplexAccessed = false

const params = {
body: new ReadableStream(),
method: 'POST',
get duplex() {
duplexAccessed = true
return 'half'
},
}

const hasContentType = new Request('', params).headers.has('Content-Type')

return duplexAccessed && !hasContentType
})()

return supportsRequestStreams
}
Loading