`parse-sse` is a lightweight, spec-compliant JavaScript library designed for parsing Server-Sent Events (SSE) from a standard Web `Response` object, typically obtained via the native Fetch API. Currently at version 0.1.0, it is a new release focused on providing a robust and efficient way to consume streaming APIs. The library differentiates itself by being fully compatible with native `ReadableStream` and `TransformStream` interfaces, enabling maximum composability with other stream-based operations. It is particularly well-suited for integrating with modern streaming services like OpenAI and Anthropic, which extensively utilize SSE for real-time data delivery. As a relatively new package, a specific release cadence has not yet been established, but its focus on web platform standards and minimal dependencies suggests a stable foundation.
npm install parse-sseVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to consume a streaming chat completion from OpenAI using `parse-sse` with the Fetch API, showing real-time output and handling the `[DONE]` signal.
Ensure your project runs on Node.js 20+ and uses ES module syntax (`import`/`export`). For CommonJS projects, consider using a tool like `esm` or migrating to ESM.
Pipe your byte stream through `new TextDecoderStream()` before `ServerSentEventTransformStream`, e.g., `response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new ServerSentEventTransformStream())`.
Always serialize complex data (like multi-line strings or objects) within the `data` field as JSON, and then parse it on the client side, e.g., `JSON.parse(event.data)`. This ensures proper handling of internal newlines.
Change `const { parseServerSentEvents } = require('parse-sse');` to `import { parseServerSentEvents } from 'parse-sse';` and ensure your `package.json` has `"type": "module"` or your file has `.mjs` extension.Verify that your environment is set up for ES modules (Node.js 20+ and correct `package.json` `type` field) and that you are using `import { parseServerSentEvents } from 'parse-sse';`.Ensure the server is actually sending a stream (`Content-Type: text/event-stream`) and the `response.ok` is true. Handle cases where `response.body` might be `null` or already consumed before passing it to `parseServerSentEvents`.
Prepend a `TextDecoderStream` to the pipeline: `response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new ServerSentEventTransformStream())`.
No dependency data recorded yet.