Registry / messaging / fetch-event-source

fetch-event-source

JSON →
library1.0.0-alpha.2jsnpmunverified

A lightweight (4kb, 2kb gzip) TypeScript library that implements the EventSource API using the Fetch API, enabling HTTP header customization and server-sent events support in Node.js and browsers. Current stable version is 1.0.0-alpha.2, released with limited update cadence. Differentiators include smaller size than native polyfills, full TypeScript typings, and cross-platform Node.js/browser support.

npm install fetch-event-source
INSTALL
IMPORT
SIG · FETCH-EVENT-SOURCE
F
fetch-event-source
messagingjavascriptv1.0.0-alpha.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

FetchEventSource
import FetchEventSource from 'fetch-event-source'
const FetchEventSource = require('fetch-event-source')
Default export; CJS require works if using Node.js with the described server path.
FetchEventSource (server)
import FetchEventSource from 'fetch-event-source/server'
import { FetchEventSource } from 'fetch-event-source/server'
For Node.js environments, import from the server subpath.
FetchEventSourceConfiguration
import type { FetchEventSourceConfiguration } from 'fetch-event-source'
import { FetchEventSourceConfiguration } from 'fetch-event-source'
The interface is exported as a type; import it with 'type' modifier to avoid runtime inclusion.

Creates a FetchEventSource with custom headers, auto-start enabled, and event listeners for open, message, and error events.

import FetchEventSource from 'fetch-event-source'; const config = { url: 'https://example.com/sse', headers: { Authorization: 'Bearer ' + (process.env.TOKEN ?? '') }, withCredentials: false, autoStart: true, }; const eventSource = new FetchEventSource(config); eventSource.addListener('open', () => console.log('Connected')); eventSource.addListener('message', (e) => console.log('Data:', e.data)); eventSource.addListener('error', (err) => console.error('Error:', err)); eventSource.start();
Debug
Known issues
gotchaThe library is in early alpha; API may break without major version bump.
fix
Pin to exact version and test upgrades carefully.
affects: >=1.0.0-alpha.1 <1.0.0
gotchaThe constructor expects a configuration object, not a string URL like native EventSource.
fix
Pass a FetchEventSourceConfiguration object with url property instead of a string.
affects: >=0.0.1
gotchaThe 'message' listener receives a MessageEvent-like object, but 'data' is a string (not parsed JSON).
fix
Parse manually: JSON.parse(e.data);
affects: >=0.0.1
gotchaIn Node.js, you must import from 'fetch-event-source/server' to avoid browser-specific APIs.
fix
Use 'import FetchEventSource from 'fetch-event-source/server';' in Node.js.
affects: >=0.0.1
Errors
Common errors & fixes
TypeError: FetchEventSource is not a constructor
Importing the default export incorrectly using named import syntax.
fix
Use default import: import FetchEventSource from 'fetch-event-source';
ReferenceError: fetch is not defined
Running in Node.js without a global fetch polyfill (e.g., node-fetch).
fix
Install node-fetch and import it globally, or use the custom fetch in config.
Error: EventSource is not a valid constructor
Passing a string URL instead of a configuration object to the constructor.
fix
Pass { url: '...', ... } object to new FetchEventSource(config).
Upgrade
Version history
1.0.0-alpha.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
14
Amazon
1
OpenAI (training)
1
Resources
fetch-event-source — npm install fetch-event-source · libregistry