feat: fetch stream compatibility enhance (#1551)

This commit is contained in:
Ricky 2023-11-20 15:30:32 +08:00 • committed by GitHub
commit 657334a5fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 2 deletions

View file

@ -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',
@ -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) => {
@ -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