You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

event-stream-parser

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

event-stream-parser

Server-Sent Events (SSE) Parser / Event Stream Parser implementation following HTML Standard

1.0.2
latest
Source
npmnpm
Version published
Weekly downloads
775
-32.67%
Maintainers
1
Weekly downloads
 
Created
Source

Server-Sent Events (SSE) Parser / Event Stream Parser

A lightweight Server-Sent Events (SSE) parser implementation following the HTML Living Standard specification. This package is designed to work with the Web Streams API and is fully compatible with modern browsers and Node.js.

Installation

npm install event-stream-parser
# or
yarn add event-stream-parser
# or
pnpm add event-stream-parser

Usage

You can import the package in two ways:

// Using v1 explicitly
import { parse } from 'event-stream-parser/v1';
// Using default import (same as v1)
import { parse } from 'event-stream-parser';

// Example with fetch
const response = await fetch('https://api.example.com/events', {
  headers: {
    Accept: 'text/event-stream',
  },
});

if (!response.body) {
  throw new Error('Response body is null');
}

const eventStream = await parse(response.body);

eventStream.pipeTo(
  new WritableStream({
    write(event) {
      console.log('Event type:', event.type);
      console.log('Event data:', event.data);
      console.log('Last event ID:', event.lastEventId);
    },
  })
);

Features

  • 🚀 Lightweight and zero dependencies
  • 🌊 Works with Web Streams API
  • 📦 Full TypeScript support
  • ✨ Follows SSE specification
  • 🔍 Handles all SSE fields (event, data, id, retry)

API

parse(stream: ReadableStream): Promise<ReadableStream>

Parses a readable stream of SSE data and returns a readable stream of MessageEvent objects.

Parameters

  • stream: A ReadableStream<Uint8Array> containing the SSE data

Returns

  • A Promise<ReadableStream<MessageEvent>> that yields parsed SSE events

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Keywords

sse

FAQs

Package last updated on 05 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts