From 3ab1de23d37e3c513865dd177419040fc8928fad Mon Sep 17 00:00:00 2001 From: Tyler Date: Sat, 20 Jun 2026 15:52:05 -0600 Subject: [PATCH] refactor(api): _ndjson_line helper for streaming responses --- backend/src/cyclone/api.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/backend/src/cyclone/api.py b/backend/src/cyclone/api.py index 68d94d4..d3592e5 100644 --- a/backend/src/cyclone/api.py +++ b/backend/src/cyclone/api.py @@ -60,6 +60,17 @@ from cyclone.store import ( log = logging.getLogger(__name__) +def _ndjson_line(event: dict) -> bytes: + """Serialize one event dict as a single NDJSON line (UTF-8, trailing ``\\n``). + + Used by the live-tail streaming endpoints to emit a uniform wire format + that the frontend ``tail-stream.ts`` parser can split on newlines. + Compact separators keep each line small and avoid ambiguity with embedded + whitespace. + """ + return (json.dumps(event, separators=(",", ":")) + "\n").encode("utf-8") + + @asynccontextmanager async def lifespan(app: FastAPI) -> AsyncIterator[None]: """Initialize per-process resources (DB + EventBus) before the app