Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
fetch-event-stream
Advanced tools
A tiny (745b) utility for Server Sent Event (SSE) streaming via `fetch` and Web Streams API
A tiny (745b) utility for Server Sent Event (SSE) streaming via
fetch
and Web Streams API
AbortController
for cancellable streams$ npm install --save fetch-event-stream
import { events, stream } from 'fetch-event-stream';
// or
import { events, stream } from 'https://deno.land/x/fetch_event_stream';
Convert a Response
body containing Server Sent Events (SSE) into an Async Iterator that yields
ServerSentEventMessage
objects.
Example
// Optional
let abort = new AbortController();
// Manually fetch a Response
let res = await fetch('https://...', {
method: 'POST',
signal: abort.signal,
headers: {
'api-key': 'token <value>',
'content-type': 'application/json',
},
body: JSON.stringify({
stream: true, // <- hypothetical
// ...
}),
});
if (res.ok) {
let stream = events(res, abort.signal);
for await (let event of stream) {
console.log('<<', event.data);
}
}
Type: Response
The Response
to consume. Must contain a body that follows the Server-Sent Event message protocol.
Type: AbortSignal
Optional. Use the AbortController
interface to stop iteration. The stream will be destroyed.
Convenience function that will fetch
with the given arguments and, if ok, will return the events
async iterator.
Note: Accepts the same arguments as
fetch
but does not return aResponse
!
Important: Will
throw
theResponse
if received non-2xx
status code.
Example
// NOTE: throws `Response` if not 2xx status
let events = await stream('https://api.openai.com/...', {
method: 'POST',
headers: {
'Authorization': 'Bearer <token>',
'Content-Type': 'application/json',
},
body: JSON.stringify({
stream: true,
// ...
}),
});
for await (let event of events) {
console.log('<<', JSON.parse(event.data));
}
Type: Request | URL | string
Refer to fetch#resource
documentation.
Type: RequestInit
Refer to fetch#options
documentation.
MIT © Luke Edwards
FAQs
A tiny (736b) utility for Server Sent Event (SSE) streaming via `fetch` and Web Streams API
The npm package fetch-event-stream receives a total of 22,592 weekly downloads. As such, fetch-event-stream popularity was classified as popular.
We found that fetch-event-stream demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.