File tree 1 file changed +12
-3
lines changed
packages/@ackee/antonio-core/src/modules/response/iterableStream
1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -6,24 +6,33 @@ export async function* iterableStream(stream: Body['body'], contentTypeIsJson: b
6
6
}
7
7
8
8
const reader = stream . getReader ( ) ;
9
- const decoder = new TextDecoder ( ) ;
10
- const parser = contentTypeIsJson ? new ChunkToJsonParser ( ) : null ;
11
9
12
10
try {
11
+ const decoder = new TextDecoder ( ) ;
12
+ const parser = contentTypeIsJson ? new ChunkToJsonParser ( ) : null ;
13
13
let result = await reader . read ( ) ;
14
+ let yieldedSome = false ;
15
+ let parsedChunk : string | any [ ] | undefined ;
14
16
15
17
while ( ! result . done ) {
16
18
const arrayBuffer = result . value ;
17
19
const decodedChunk = decoder . decode ( arrayBuffer , { stream : true } ) ;
18
20
19
- const parsedChunk = parser ? parser . parse ( decodedChunk ) : decodedChunk ;
21
+ parsedChunk = parser ? parser . parse ( decodedChunk ) : decodedChunk ;
20
22
21
23
if ( parsedChunk . length > 0 ) {
22
24
yield parsedChunk ;
25
+ yieldedSome = true ;
23
26
}
24
27
25
28
result = await reader . read ( ) ;
26
29
}
30
+
31
+ // If every chunk was empty, yield now, at the end, the empty chunk as the last value.
32
+ // Otherwise the generator wouldn't ever end.
33
+ if ( ! yieldedSome ) {
34
+ yield parsedChunk ;
35
+ }
27
36
} catch ( e ) {
28
37
reader . releaseLock ( ) ;
29
38
await stream . cancel ( ) ;
You can’t perform that action at this time.
0 commit comments