Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/acp/_sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from __future__ import annotations

import codecs
import json
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -62,11 +63,12 @@ async def parse_sse_stream(chunks: AsyncIterator[bytes]) -> AsyncIterator[dict[s
Multi-line ``data:`` fields are concatenated with newlines per the spec. A
blank line dispatches the buffered event.
"""
decoder = codecs.getincrementaldecoder("utf-8")()
buffer = ""
data_lines: list[str] = []

async for chunk in chunks:
buffer += chunk.decode("utf-8")
buffer += decoder.decode(chunk)
while "\n" in buffer:
line, buffer = buffer.split("\n", 1)
line = line.rstrip("\r")
Expand All @@ -78,6 +80,8 @@ async def parse_sse_stream(chunks: AsyncIterator[bytes]) -> AsyncIterator[dict[s
elif not line.startswith(":"):
_append_field(line, data_lines)

decoder.decode(b"", final=True)

# Flush a trailing event with no terminating blank line.
event = _decode_event(data_lines)
if event is not None:
Expand Down
11 changes: 11 additions & 0 deletions tests/http/test_sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ async def test_parse_multiple_events_split_across_chunks() -> None:
assert events == [{"id": 1}, {"id": 2}]


@pytest.mark.asyncio
async def test_parse_multibyte_utf8_split_across_chunks() -> None:
frame = 'data: {"text":"你好"}\n\n'.encode()
split_at = frame.index("你".encode()) + 1
stream = _aiter([frame[:split_at], frame[split_at:]])

events = [event async for event in parse_sse_stream(stream)]

assert events == [{"text": "你好"}]


@pytest.mark.asyncio
async def test_parse_ignores_comments_and_other_fields() -> None:
stream = _aiter([b': keepalive\n\nevent: message\ndata: {"id":7}\n\n'])
Expand Down
Loading