Gathering detailed insights and metrics for @fortaine/fetch-event-source
Gathering detailed insights and metrics for @fortaine/fetch-event-source
Gathering detailed insights and metrics for @fortaine/fetch-event-source
Gathering detailed insights and metrics for @fortaine/fetch-event-source
minipass
minimal implementation of a PassThrough stream
@microsoft/fetch-event-source
A better API for making Event Source requests, with all the features of fetch()
fetch-event-stream
A tiny (736b) utility for Server Sent Event (SSE) streaming via `fetch` and Web Streams API
opentelemetry-instrumentation-fetch-node
OpenTelemetry Node 18+ native fetch automatic instrumentation package
npm install @fortaine/fetch-event-source
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,816 Stars
20 Commits
143 Forks
36 Watching
1 Branches
10,000 Contributors
Updated on 28 Nov 2024
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-43.4%
2,537
Compared to previous day
Last week
-9.2%
21,693
Compared to previous week
Last month
-10.2%
107,405
Compared to previous month
Last year
-46.1%
1,876,464
Compared to previous year
7
This package provides a better API for making Event Source requests - also known as server-sent events - with all the features available in the Fetch API.
The default browser EventSource API imposes several restrictions on the type of request you're allowed to make: the only parameters you're allowed to pass in are the url
and withCredentials
, so:
This library provides an alternate interface for consuming server-sent events, based on the Fetch API. It is fully compatible with the Event Stream format, so if you already have a server emitting these events, you can consume it just like before. However, you now have greater control over the request and response so:
In addition, this library also plugs into the browser's Page Visibility API so the connection closes if the document is hidden (e.g., the user minimizes the window), and automatically retries with the last event ID when it becomes visible again. This reduces the load on your server by not having open connections unnecessarily (but you can opt out of this behavior if you want.)
1npm install @microsoft/fetch-event-source
1// BEFORE: 2const sse = new EventSource('/api/sse'); 3sse.onmessage = (ev) => { 4 console.log(ev.data); 5}; 6 7// AFTER: 8import { fetchEventSource } from '@microsoft/fetch-event-source'; 9 10await fetchEventSource('/api/sse', { 11 onmessage(ev) { 12 console.log(ev.data); 13 } 14});
You can pass in all the other parameters exposed by the default fetch API, for example:
1const ctrl = new AbortController();
2fetchEventSource('/api/sse', {
3 method: 'POST',
4 headers: {
5 'Content-Type': 'application/json',
6 },
7 body: JSON.stringify({
8 foo: 'bar'
9 }),
10 signal: ctrl.signal,
11});
You can add better error handling, for example:
1class RetriableError extends Error { } 2class FatalError extends Error { } 3 4fetchEventSource('/api/sse', { 5 async onopen(response) { 6 if (response.ok && response.headers.get('content-type') === EventStreamContentType) { 7 return; // everything's good 8 } else if (response.status >= 400 && response.status < 500 && response.status !== 429) { 9 // client-side errors are usually non-retriable: 10 throw new FatalError(); 11 } else { 12 throw new RetriableError(); 13 } 14 }, 15 onmessage(msg) { 16 // if the server emits an error message, throw an exception 17 // so it gets handled by the onerror callback below: 18 if (msg.event === 'FatalError') { 19 throw new FatalError(msg.data); 20 } 21 }, 22 onclose() { 23 // if the server closes the connection unexpectedly, retry: 24 throw new RetriableError(); 25 }, 26 onerror(err) { 27 if (err instanceof FatalError) { 28 throw err; // rethrow to stop the operation 29 } else { 30 // do nothing to automatically retry. You can also 31 // return a specific retry interval here. 32 } 33 } 34});
This library is written in typescript and targets ES2017 features supported by all evergreen browsers (Chrome, Firefox, Safari, Edge.) You might need to polyfill TextDecoder for old Edge (versions < 79), though:
1require('fast-text-encoding');
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
security policy file detected
Details
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
Found 1/19 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More