Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@sanity/eventsource
Advanced tools
@sanity/eventsource is a JavaScript library that provides a polyfill for the EventSource API, which is used for receiving server-sent events (SSE). This package allows you to establish a persistent connection to a server and receive updates in real-time, making it useful for applications that require live data updates, such as chat applications, live feeds, or notifications.
Establishing a Connection
This feature allows you to establish a connection to a server that sends events. The code sample demonstrates how to create a new EventSource instance and listen for incoming messages.
const EventSource = require('@sanity/eventsource');
const es = new EventSource('http://example.com/events');
es.onmessage = function(event) {
console.log('New message:', event.data);
};
Handling Different Event Types
This feature allows you to listen for specific types of events sent by the server. The code sample shows how to add an event listener for a custom event type.
const EventSource = require('@sanity/eventsource');
const es = new EventSource('http://example.com/events');
es.addEventListener('customEvent', function(event) {
console.log('Custom event received:', event.data);
});
Handling Connection Errors
This feature allows you to handle errors that occur during the connection. The code sample demonstrates how to set up an error handler for the EventSource instance.
const EventSource = require('@sanity/eventsource');
const es = new EventSource('http://example.com/events');
es.onerror = function(event) {
console.error('Error occurred:', event);
};
The 'eventsource' package is another polyfill for the EventSource API. It provides similar functionality to @sanity/eventsource, allowing you to receive server-sent events in environments that do not natively support EventSource. Both packages offer similar APIs and can be used interchangeably.
The 'sse.js' package is a lightweight library for handling server-sent events. It provides a simple API for establishing connections and listening for events. Compared to @sanity/eventsource, sse.js is more minimalistic and may lack some advanced features but is easier to use for basic use cases.
The 'reconnecting-eventsource' package extends the EventSource API with automatic reconnection capabilities. This package is useful if you need a more robust solution that can handle intermittent network issues. It offers additional features compared to @sanity/eventsource, such as configurable reconnection strategies.
Meta-package to make browsers and Node use different EventSource polyfills
npm install @sanity/eventsource
import polyfilledEventSource from '@sanity/eventsource'
The polyfill adds support for auth headers, which isn't part of the EventSource spec.
In NodeJS environments it's the implementation from eventsource package. While browsers use event-source-polyfill.
To get the browser implementation, no matter how your bundler or runtime understands package.json
exports
, you can use the following:
import polyfilledEventSource from '@sanity/eventsource/browser'
To force the Node implementation, use the following:
import polyfilledEventSource from '@sanity/eventsource/node'
MIT © Sanity.io
FAQs
EventSource polyfill for browser and node.js
The npm package @sanity/eventsource receives a total of 247,253 weekly downloads. As such, @sanity/eventsource popularity was classified as popular.
We found that @sanity/eventsource demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 48 open source maintainers 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
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.