fix: fetch无法返回正确从chunk

This commit is contained in:
shaohuzhang1 2024-01-03 18:10:40 +08:00
parent 9a11937a6b
commit 108086990c

View File

@ -273,6 +273,7 @@ function chatMessage() {
ChatManagement.addChatRecord(row, 50, loading)
ChatManagement.write(id)
const reader = response.body.getReader()
let tempResult = ''
/*eslint no-constant-condition: ["error", { "checkLoops": false }]*/
const write = ({ done, value }: { done: boolean; value: any }) => {
try {
@ -282,7 +283,16 @@ function chatMessage() {
}
const decoder = new TextDecoder('utf-8')
const str = decoder.decode(value, { stream: true })
let str = decoder.decode(value, { stream: true })
// start chunk chunkdata:{xxx}\n\n data:{ -> xxx}\n\n fetchchunkdata: \n\n
tempResult += str
if (tempResult.endsWith('\n\n')) {
str = tempResult
tempResult = ''
} else {
return reader.read().then(write)
}
// end
if (str && str.startsWith('data:')) {
const split = str.match(/data:.*}\n\n/g)
if (split) {