Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eventsource

Package Overview
Dependencies
Maintainers
2
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eventsource - npm Package Compare versions

Comparing version 2.0.2 to 3.0.0-beta.0

dist/index.cjs

154

package.json
{
"name": "eventsource",
"version": "2.0.2",
"description": "W3C compliant EventSource client for Node.js and browser (polyfill)",
"keywords": [
"eventsource",
"http",
"streaming",
"sse",
"polyfill"
"version": "3.0.0-beta.0",
"description": "WhatWG/W3C compliant EventSource client for Node.js and browsers",
"sideEffects": false,
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"deno": "./dist/index.js",
"bun": "./dist/index.js",
"source": "./src/index.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./package.json": "./package.json"
},
"scripts": {
"build": "pkg-utils build && pkg-utils --strict",
"build:watch": "pkg-utils watch",
"clean": "rimraf dist coverage",
"lint": "eslint . && tsc --noEmit",
"posttest": "npm run lint",
"prebuild": "npm run clean",
"prepublishOnly": "npm run build",
"test": "npm run test:node && npm run test:browser",
"test:browser": "tsx test/browser/client.browser.test.ts",
"test:bun": "bun run test/bun/client.bun.test.ts",
"test:deno": "deno run --allow-net --allow-read --allow-env --unstable-sloppy-imports test/deno/client.deno.test.ts",
"test:node": "tsx test/node/client.node.test.ts"
},
"files": [
"!dist/stats.html",
"dist",
"src"
],
"homepage": "http://github.com/EventSource/eventsource",
"author": "Aslak Hellesøy <aslak.hellesoy@gmail.com>",
"repository": {
"type": "git",
"url": "git://github.com/EventSource/eventsource.git"
"url": "git+ssh://git@github.com/eventsource/eventsource.git"
},
"bugs": {
"url": "http://github.com/EventSource/eventsource/issues"
"keywords": [
"sse",
"eventsource",
"server-sent-events"
],
"author": "Espen Hovlandsdal <espen@hovlandsdal.com>",
"contributors": [
"Aslak Hellesøy <aslak.hellesoy@gmail.com>",
"Einar Otto Stangvik <einaro.s@gmail.com>"
],
"license": "MIT",
"engines": {
"node": ">=18.0.0"
},
"directories": {
"lib": "./lib"
"browserslist": [
"node >= 18",
"chrome >= 71",
"safari >= 14.1",
"firefox >= 105",
"edge >= 79"
],
"dependencies": {
"eventsource-parser": "^3.0.0"
},
"main": "./lib/eventsource",
"license": "MIT",
"licenses": [
{
"type": "MIT",
"url": "http://github.com/EventSource/eventsource/raw/master/LICENSE"
}
],
"devDependencies": {
"buffer-from": "^1.1.1",
"express": "^4.15.3",
"mocha": "^3.5.3",
"nyc": "^11.2.1",
"serve-static": "^1.12.3",
"ssestream": "^1.0.0",
"standard": "^10.0.2",
"webpack": "^3.5.6"
"@sanity/pkg-utils": "^6.11.10",
"@sanity/semantic-release-preset": "^5.0.0",
"@tsconfig/strictest": "^2.0.5",
"@types/sinon": "^17.0.3",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.11.0",
"esbuild": "^0.24.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-sanity": "^7.1.3",
"eventsource-encoder": "^1.0.0",
"playwright": "^1.48.2",
"prettier": "^3.2.5",
"rimraf": "^5.0.5",
"rollup-plugin-visualizer": "^5.12.0",
"semantic-release": "^24.2.0",
"sinon": "^17.0.1",
"tsx": "^4.19.2",
"typescript": "^5.6.3",
"undici": "^6.20.1"
},
"scripts": {
"test": "mocha --reporter spec && standard",
"polyfill": "webpack lib/eventsource-polyfill.js example/eventsource-polyfill.js",
"postpublish": "git push && git push --tags",
"coverage": "nyc --reporter=html --reporter=text _mocha --reporter spec"
"bugs": {
"url": "https://github.com/eventsource/eventsource/issues"
},
"engines": {
"node": ">=12.0.0"
"homepage": "https://github.com/eventsource/eventsource#readme",
"prettier": {
"semi": false,
"printWidth": 100,
"bracketSpacing": false,
"singleQuote": true
},
"dependencies": {},
"standard": {
"ignore": [
"example/eventsource-polyfill.js"
"eslintConfig": {
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"ecmaFeatures": {
"modules": true
}
},
"extends": [
"sanity",
"sanity/typescript",
"prettier"
],
"globals": [
"URL"
]
"ignorePatterns": [
"lib/**/"
],
"globals": {
"globalThis": false
},
"rules": {
"no-undef": "off",
"no-empty": "off"
}
}
}

@@ -1,91 +0,141 @@

# EventSource [![npm version](http://img.shields.io/npm/v/eventsource.svg?style=flat-square)](https://www.npmjs.com/package/eventsource)[![NPM Downloads](https://img.shields.io/npm/dm/eventsource.svg?style=flat-square)](http://npm-stat.com/charts.html?package=eventsource&from=2015-09-01)[![Dependencies](https://img.shields.io/david/EventSource/eventsource.svg?style=flat-square)](https://david-dm.org/EventSource/eventsource)
# eventsource
![Build](https://github.com/EventSource/eventsource/actions/workflows/build.yml/badge.svg)
[![npm version](https://img.shields.io/npm/v/eventsource.svg?style=flat-square)](http://npmjs.org/package/eventsource)[![npm bundle size](https://img.shields.io/bundlephobia/minzip/eventsource?style=flat-square)](https://bundlephobia.com/result?p=eventsource)
This library is a pure JavaScript implementation of the [EventSource](https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events) client. The API aims to be W3C compatible.
WhatWG/W3C-compatible [server-sent events/eventsource](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) client. The module attempts to implement an absolute minimal amount of features/changes beyond the specification.
You can use it with Node.js or as a browser polyfill for
[browsers that don't have native `EventSource` support](http://caniuse.com/#feat=eventsource).
If you're looking for a modern alternative with a less constrained API, check out the [`eventsource-client` package](https://www.npmjs.com/package/eventsource-client).
## Install
## Installation
npm install eventsource
```bash
npm install --save eventsource
```
## Example
## Supported engines
npm install
node ./example/sse-server.js
node ./example/sse-client.js # Node.js client
open http://localhost:8080 # Browser client - both native and polyfill
curl http://localhost:8080/sse # Enjoy the simplicity of SSE
- Node.js >= 18
- Chrome >= 63
- Safari >= 11.3
- Firefox >= 65
- Edge >= 79
- Deno >= 1.30
- Bun >= 1.1.23
## Browser Polyfill
Basically, any environment that supports:
Just add `example/eventsource-polyfill.js` file to your web page:
- [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch)
- [ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream)
- [TextDecoderStream](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream)
- [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL)
- [Event](https://developer.mozilla.org/en-US/docs/Web/API/Event), [MessageEvent](https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent), [EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget)
```html
<script src=/eventsource-polyfill.js></script>
```
If you need to support older runtimes, try the `1.x` branch/version range (note: 1.x branch is primarily targetted at Node.js, not browsers).
Now you will have two global constructors:
## Usage
```javascript
window.EventSourcePolyfill
window.EventSource // Unchanged if browser has defined it. Otherwise, same as window.EventSourcePolyfill
```
```ts
import {EventSource} from 'eventsource'
If you're using [webpack](https://webpack.github.io/) or [browserify](http://browserify.org/)
you can of course build your own. (The `example/eventsource-polyfill.js` is built with webpack).
const es = new EventSource('https://my-server.com/sse')
## Extensions to the W3C API
/*
* This will listen for events with the field `event: notice`.
*/
es.addEventListener('notice', (event) => {
console.log(event.data)
})
### Setting HTTP request headers
/*
* This will listen for events with the field `event: update`.
*/
es.addEventListener('update', (event) => {
console.log(event.data)
})
You can define custom HTTP headers for the initial HTTP request. This can be useful for e.g. sending cookies
or to specify an initial `Last-Event-ID` value.
/*
* The event "message" is a special case, as it will capture events _without_ an
* event field, as well as events that have the specific type `event: message`.
* It will not trigger on any other event type.
*/
es.addEventListener('message', (event) => {
console.log(event.data)
})
HTTP headers are defined by assigning a `headers` attribute to the optional `eventSourceInitDict` argument:
/**
* To explicitly close the connection, call the `close` method.
* This will prevent any reconnection from happening.
*/
setTimeout(() => {
es.close()
}, 10_000)
```
```javascript
var eventSourceInitDict = {headers: {'Cookie': 'test=test'}};
var es = new EventSource(url, eventSourceInitDict);
## Extensions to the WhatWG/W3C API
### Message and code properties on errors
The `error` event has a `message` and `code` property that can be used to get more information about the error. In the specification, the Event
```ts
es.addEventListener('error', (err) => {
if (err.code === 401 || err.code === 403) {
console.log('not authorized')
}
})
```
### Allow unauthorized HTTPS requests
### Specify `fetch` implementation
By default, https requests that cannot be authorized will cause the connection to fail and an exception
to be emitted. You can override this behaviour, along with other https options:
The `EventSource` constructor accepts an optional `fetch` property in the second argument that can be used to specify the `fetch` implementation to use.
```javascript
var eventSourceInitDict = {https: {rejectUnauthorized: false}};
var es = new EventSource(url, eventSourceInitDict);
This can be useful in environments where the global `fetch` function is not available - but it can also be used to alter the request/response behaviour.
#### Setting HTTP request headers
```ts
const es = new EventSource('https://my-server.com/sse', {
fetch: (input, init) =>
fetch(input, {
...init,
headers: {
...init.headers,
Authorization: 'Bearer myToken',
},
}),
})
```
Note that for Node.js < v0.10.x this option has no effect - unauthorized HTTPS requests are *always* allowed.
#### HTTP/HTTPS proxy
### HTTP status code on error events
Use a package like [`node-fetch-native`](https://github.com/unjs/node-fetch-native) to add proxy support, either through environment variables or explicit configuration.
Unauthorized and redirect error status codes (for example 401, 403, 301, 307) are available in the `status` property in the error event.
```ts
// npm install node-fetch-native --save
import {fetch} from 'node-fetch-native/proxy'
```javascript
es.onerror = function (err) {
if (err) {
if (err.status === 401 || err.status === 403) {
console.log('not authorized');
}
}
};
const es = new EventSource('https://my-server.com/sse', {
fetch: (input, init) => fetch(input, init),
})
```
### HTTP/HTTPS proxy
#### Allow unauthorized HTTPS requests
You can define a `proxy` option for the HTTP request to be used. This is typically useful if you are behind a corporate firewall.
Use a package like [`undici`](https://github.com/nodejs/undici) for more control of fetch options through the use of an [`Agent`](https://undici.nodejs.org/#/docs/api/Agent.md).
```javascript
var es = new EventSource(url, {proxy: 'http://your.proxy.com'});
```ts
// npm install undici --save
import {fetch, Agent} from 'undici'
await fetch('https://my-server.com/sse', {
dispatcher: new Agent({
connect: {
rejectUnauthorized: false,
},
}),
})
```
## License
MIT-licensed. See LICENSE
MIT-licensed. See [LICENSE](LICENSE).
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc