feat: support for non-streamed responses (#294)

This commit is contained in:
josc146
2023-05-08 21:46:35 +08:00
parent 004ae8723c
commit b886d7e944
2 changed files with 17 additions and 5 deletions
+11 -1
View File
@@ -19,7 +19,17 @@ export async function fetchSSE(resource, options) {
let hasStarted = false
for await (const chunk of streamAsyncIterable(resp.body)) {
const str = new TextDecoder().decode(chunk)
parser.feed(str)
if (!str.startsWith('{') && !str.startsWith('"{')) {
parser.feed(str)
} else {
const formattedStr =
'data: ' +
JSON.stringify(
JSON.parse(str.replace(/^"|"$/g, '').replaceAll('\\"', '"').replaceAll('\\\\u', '\\u')),
).replaceAll('\\\\n', '\\n') +
'\n\ndata: [DONE]\n\n'
parser.feed(formattedStr)
}
if (!hasStarted) {
hasStarted = true