output messed up when cdns buffering the response data
- Dominant language
- TypeScript
- Stars
- 1k
- Forks
- 267
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 9
Description
some cdn providers could buffer the response data, which might potentially affect sse response parsing.
looks like there s already some code handling this...
https://github.com/google/adk-web/blob/aebc8591ac27bf08d1b9ccf641ad9c8a5a05826d/src/app/core/services/agent.service.ts#L78-L96
but that actually behaves weird.
if more than one line is received, obviously only *the last line* could be incomplete.
when *the last line* throws a `SyntaxError`, all previous lines have already been successfully looped over and processed.
in this case `lastData` wont be cleared, so when the next data arrives, those lines will be processed again and again.
----
relying on trailing linebreaks is better than errors imo.
```diff
diff --git a/src/app/core/services/agent.service.ts b/src/app/core/services/agent.service.ts
index 0d0791a..1fc1418 100644
--- a/src/app/core/services/agent.service.ts
+++ b/src/app/core/services/agent.service.ts
@@ -76,22 +76,15 @@ export class AgentService {
return observer.complete();
}
const chunk = decoder.decode(value, {stream: true});
- lastData += chunk;
- try {
- const lines = lastData.split(/\r?\n/).filter(
- (line) => line.startsWith('data:'));
- lines.forEach((line) => {
+ lastData += chunk.replace(/\r/, '');
+ const lines = lastData.split("\n");
+ if (lines.length > 1) {
+ lines.slice(0, -1).filter(line => line.startsWith('data:')).forEach(line => {
const data = line.replace(/^data:\s*/, '');
JSON.parse(data);
self.zone.run(() => observer.next(data));
- });
- lastData = '';
- } catch (e) {
- // the data is not a valid json, it could be an incomplete
- // chunk. we ignore it and wait for the next chunk.
- if (e instanceof SyntaxError) {
- read();
- }
+ })
+ lastData = lines.at(-1)!;
}
read(); // Read the next chunk
})
```
Contributor guide
Assessment
This issue has not been assessed yet.