Registry / http-networking / event-stream-parser

event-stream-parser

JSON →
library1.0.2jsnpmunverified

The `event-stream-parser` package provides a lightweight, zero-dependency implementation for parsing Server-Sent Events (SSE) streams, strictly adhering to the HTML Living Standard specification. Designed to leverage the Web Streams API, it offers full compatibility with modern web browsers and Node.js environments (requiring Node.js >=18.0.0). The current stable version is `1.0.2`, with a release cadence that has been fairly rapid since its initial v1.0.0 release in early 2024. Its key differentiators include its minimalistic design, native Web Streams integration for efficient processing, and comprehensive TypeScript support, making it an ideal choice for applications requiring robust and standards-compliant SSE consumption without external bloat.

npm install event-stream-parser
INSTALL
IMPORT
SIG · EVENT-STREAM-PARSE
E
event-stream-parser
http-networkingjavascriptv1.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
import { parse } from 'event-stream-parser';
import parse from 'event-stream-parser';
The `parse` function is a named export. Default imports will not work. This package is ESM-only.
parse
import { parse } from 'event-stream-parser/v1';
const { parse } = require('event-stream-parser');
Explicitly importing from '/v1' is supported. CommonJS `require()` syntax is not supported as the package is ESM-only.
MessageEvent (type)
import type { MessageEvent } from 'event-stream-parser';
For type-checking in TypeScript, import the `MessageEvent` type if you need to explicitly type stream events.

Demonstrates how to fetch a Server-Sent Events stream using `fetch`, parse it with `event-stream-parser`, and consume the resulting `MessageEvent` objects via a `WritableStream`.

import { parse } from 'event-stream-parser'; async function fetchAndParseEvents() { const sseApiUrl = process.env.SSE_API_URL ?? 'https://demo.vercel.events/api/kitchensink'; // Example SSE endpoint try { const response = await fetch(sseApiUrl, { headers: { 'Accept': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' }, }); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } if (!response.body) { throw new Error('Response body is null; server might not be sending a stream.'); } console.log(`Connected to SSE stream at ${sseApiUrl}`); const eventStream = await parse(response.body); await eventStream.pipeTo( new WritableStream<MessageEvent>({ write(event) { console.log('\n--- Event Received ---'); console.log(`Type: ${event.type || '[default]'}`); console.log(`Data: ${event.data}`); if (event.lastEventId) { console.log(`Last Event ID: ${event.lastEventId}`); } }, close() { console.log('\n--- Event Stream Closed ---'); }, abort(reason) { console.error('\n--- Event Stream Aborted ---', reason); } }) ); } catch (error) { console.error('Failed to fetch or process event stream:', error); } } fetchAndParseEvents();
Debug
Known issues
gotchaThe `parse` function returns a `Promise<ReadableStream<MessageEvent>>`, not directly a `ReadableStream`. Ensure you `await` the result of `parse` before attempting to pipe or read from the event stream.
fix
Use `const eventStream = await parse(response.body);` instead of `const eventStream = parse(response.body);`
affects: >=1.0.0
gotchaThis package requires Node.js version 18.0.0 or higher due to its reliance on modern Web Streams API features and ESM module system. Running on older Node.js versions will result in compatibility errors.
fix
Upgrade your Node.js environment to version 18 or newer (e.g., `nvm install 18 && nvm use 18`).
affects: >=1.0.0
gotchaWhen using `fetch` to consume an SSE stream, it is crucial to set the `Accept: 'text/event-stream'` header. Additionally, `Cache-Control: 'no-cache'` and `Connection: 'keep-alive'` are often necessary for proper streaming behavior, although not strictly required by the parser itself.
fix
Ensure your `fetch` request includes `{ headers: { Accept: 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' } }`.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined in ES module scope
Attempting to use CommonJS `require()` syntax in an ESM-only project, or an older Node.js environment that does not fully support ESM.
fix
Use `import { parse } from 'event-stream-parser';` for importing. Ensure your Node.js version is >=18 and your project is configured for ESM (e.g., `"type": "module"` in `package.json`).
TypeError: response.body is null
The `fetch` API returned a response without a readable body, which can happen if the server did not respond with a stream, or if the `Content-Type` was not `text/event-stream` as expected.
fix
Verify the server endpoint is correctly configured to send Server-Sent Events, and ensure your `fetch` request includes the `Accept: 'text/event-stream'` header.
TypeError: (intermediate value).pipeTo is not a function
This error occurs if you try to call `.pipeTo()` on the `Promise` returned by `parse()` instead of on the `ReadableStream` it resolves to.
fix
Always `await` the result of the `parse` function: `const eventStream = await parse(response.body);`.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
12
Amazon
1
OpenAI (training)
1
Resources
event-stream-parser — npm install event-stream-parser · libregistry