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

node-fetch-event-source

v1.0.4

Published

This is my attempt to make node-fetch support event source.

Downloads

5

Readme

Node Fetch Event Source

This package provides a better API for making Event Source requests - also known as server-sent events - with all the features available in the Node-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:

  • You cannot pass in a request body: you have to encode all the information necessary to execute the request inside the URL, which is limited to 2000 characters in most platforms.
  • You cannot pass in custom request headers
  • You can only make GET requests - there is no way to specify another method.
  • If the connection is cut, you don't have any control over the retry strategy: the browser will silently retry for you a few times and then stop, which is not good enough for any sort of robust application.

This library provides an alternate interface for consuming server-sent events, based on the Node-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:

  • You can use any request method/headers/body, plus all the other functionality exposed by Node-Fetch API. You can even provide an alternate fetch implementation, if the default implementation doesn't work for you.
  • You have access to the response object if you want to do some custom validation/processing before parsing the event source. This is useful in case you have API gateways (like nginx) in front of your application server: if the gateway returns an error, you might want to handle it correctly.
  • If the connection gets cut or an error occurs, you have full control over the retry strategy.

Install

npm install node-fetch-event-source

Usage

const fetch = require("./node-fetch-event-source");

await fetch("/api/sse", {
    onmessage(ev) {
        console.log(ev.data);
    }
});

You can pass in all the other parameters exposed by the default fetch API, for example:

const controller = new AbortController();
setTimeout(() => {
    controller.abort();
    console.log("Aborted! (timeout 3s)");
}, 3000);

await fetch(
    "/api/sse",
    {
        async onopen(response) {
            console.log("onopen", response);
        },
        onmessage(msg) {
            console.log("onmessage", msg);
        },
        onclose() {
            console.log("onclose");
        },
        onerror(err) {
            console.log("onerror", err);
        },
        signal: controller.signal,
        method: "POST",
        headers: {
            Authorization: `Bearer ${<<< YOUR AUTH TOKEN HERE >>>}`,
        },
        body: JSON.stringify(<<< YOUR JSON BODY HERE >>>);
    }
);

Typings

This library is written in JavaScript, in case you want the TypeScript typings just ping me! I don't need those so I didn't bother adding them.

Contributing

In case you want to contribute to this project, then just fork the repository, make a PR, and I'll review and if everything is fine merge it.