npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

event-source-shim

v0.0.0

Published

An implementation of WHATWG EventSource interface.

Downloads

228

Readme

event-source-shim

npm version Downloads/month Build Status Coverage Status Dependency Status

An implementation of WHATWG EventSource interface.

You can support Server-sent events on also Internet Explorer and Edge. This package includes type definition for TypeScript.

💿 Installation

Use npm or a compatible tool to install.

npm install event-source-shim

📖 Usage

Use a bundler such as webpack. You have to configure the bundler to transpile this package and event-target-shim package to ES5 from ES2015 for Internet Explorer.

For example:

module.exports = {
    //....
    module: {
        rules: [
            {
                test: /\.mjs$/u,
                include: [
                    path.resolve(__dirname, "node_modules/event-source-shim"),
                    path.resolve(__dirname, "node_modules/event-target-shim"),
                ],
                loader: "babel-loader", // with @babel/preset-env.
            },
        ]
    },
    //....
}

📚 API References

§ EventSource class

This is same as standard. See MDN page of EventSource.

import { EventSource } from "event-source-shim"

const source = new EventSource("/events")
source.addEventListener("message", event => {
    console.log(event.data)
})

§ setDefaultReconnectionTime(value: number): void function

In the specification, the interval of reestablishing the connection is a user-agent-defined value (see reconnection time). As following the recommendation in the spec, this package set four seconds to the reconnection time by default. But you can configure the reconnection time with an arbitrary value.

import { setDefaultReconnectionTime } from "event-source-shim"

setDefaultReconnectionTime(10000) // 10sec.

§ setMaxBufferSize(value: number): void function

This package uses XMLHttpRequest and that onprogress event to implement EventSource class. This means that it must disconnect and reestablish the connection at random intervals in order to avoid memory leaks. This package does the reconnecting when the length of XMLHttpRequest#responseText gets greater than configured max buffer size. The max buffer size is 64KB by default. But you can configure the max buffer size with an arbitrary value.

import { setMaxBufferSize } from "event-source-shim"

setMaxBufferSize(256 * 1024) // 256KB.

§ setReconnectionTimeIncreasingRate(value: number): void function

The spec allows additional wait time between reestablishing the connection.

Optionally, wait some more. In particular, if the previous attempt failed, then user agents might introduce an exponential backoff delay to avoid overloading a potentially already overloaded server. Alternatively, if the operating system has reported that there is no network connectivity, user agents might wait for the operating system to announce that the network connection has returned before retrying.

https://html.spec.whatwg.org/multipage/server-sent-events.html#reestablish-the-connection

This package multiplies the reconnection time by the configured increasing rate on every disconnection. The increasing rate is 1.5 by default. But you can configure the increasing rate with an arbitrary value.

import { setReconnectionTimeIncreasingRate } from "event-source-shim"

setReconnectionTimeIncreasingRate(2.0) // x2 on every failure. E.g., 4sec → 8sec → 16sec → ....

⚠️ Known Limitations

  • Reconnecting happens at random intervals in order to clear XMLHttpRequest#responseText.
  • MessageEvent#origin is not supported on Internet Explorer because XMLHttpRequest#responseURL is not supported.

📰 Release notes

  • https://github.com/mysticatea/event-source-shim/releases

❤️ Contributions

Contributing is always welcome!

Please use GitHub issues and pull requests.

Development tools

  • npm run build generates files into dist directory.
  • npm run clean removes temporary files.
  • npm run coverage opens the coverage report the last npm test command generated.
  • npm run lint runs ESLint.
  • npm test runs tests.
  • npm run watch runs tests on each file edits.