File tree Expand file tree Collapse file tree 1 file changed +18
-19
lines changed Expand file tree Collapse file tree 1 file changed +18
-19
lines changed Original file line number Diff line number Diff line change @@ -13,30 +13,29 @@ export async function fetchSSE(resource, options) {
13
13
}
14
14
const parser = createParser ( ( event ) => {
15
15
if ( event . type === 'event' ) {
16
- onMessage ( event . data )
16
+ if ( event . data === '[DONE]' ) {
17
+ onMessage ( event . data )
18
+ } else {
19
+ try {
20
+ JSON . parse ( event . data )
21
+ onMessage ( event . data )
22
+ } catch ( error ) {
23
+ console . error ( 'json error' , error )
24
+ onMessage (
25
+ event . data
26
+ . replace ( / ^ " | " $ / g, '' )
27
+ . replaceAll ( '\\"' , '"' )
28
+ . replaceAll ( '\\\\u' , '\\u' )
29
+ . replaceAll ( '\\\\n' , '\\n' ) ,
30
+ )
31
+ }
32
+ }
17
33
}
18
34
} )
19
35
let hasStarted = false
20
36
for await ( const chunk of streamAsyncIterable ( resp . body ) ) {
21
37
const str = new TextDecoder ( ) . decode ( chunk )
22
- if ( ! str . startsWith ( '{' ) && ! str . startsWith ( '"{' ) ) {
23
- parser . feed ( str )
24
- } else {
25
- try {
26
- const formattedData = JSON . parse (
27
- str
28
- . replace ( / ^ " | " $ / g, '' )
29
- . replaceAll ( '\\"' , '"' )
30
- . replaceAll ( '\\\\u' , '\\u' )
31
- . replaceAll ( '\\\\n' , '\\n' ) ,
32
- )
33
- const formattedStr = 'data: ' + JSON . stringify ( formattedData ) + '\n\ndata: [DONE]\n\n'
34
- parser . feed ( formattedStr )
35
- } catch ( error ) {
36
- console . debug ( 'json error' , error )
37
- parser . feed ( str )
38
- }
39
- }
38
+ parser . feed ( str )
40
39
41
40
if ( ! hasStarted ) {
42
41
hasStarted = true
You can’t perform that action at this time.
0 commit comments