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

tiny-eventsource

v3.0.8

Published

Tiny EventSource simplifies server-sent events for API servers

Downloads

48

Readme

Tiny EventSource

Tiny EventSource simplifies server-sent events for API servers.

Example

Using the factory

import {eventsource} from "tiny-eventsource";
import {STATUS_CODES} from "node:http";

const streams = new Map();

export function stream (req, res) {
	if (req.isAuthenticated()) {
		const id = req.user.id;

		if (!streams.has(id)) {
			streams.set(id, eventsource({ms: 2e4}, "connected"));
		}

		streams.get(id).init(req, res);
	} else {
		res.statusCode = 401;
		res.writeHead(res.statusCode, {headers: {"cache-control": "no-cache, must re-validate"}})
		res.end(STATUS_CODES[res.statusCode]);
	}
};

Using the Class

import {EventSource} from "tiny-eventsource";

Testing

Tiny EventSource has 100% code coverage with its tests.

  Testing functionality
    ✔ It should do nothing with stock configuration
    ✔ It should have an accurate listener count
    ✔ It should have a heartbeat (502ms)
    ✔ It should send custom events


  4 passing (508ms)

----------------------|---------|----------|---------|---------|----------------------
File                  | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s    
----------------------|---------|----------|---------|---------|----------------------
All files             |     100 |    72.22 |     100 |     100 |                      
 tiny-eventsource.cjs |     100 |    72.22 |     100 |     100 | 35-36,49,56,66,68-88 
----------------------|---------|----------|---------|---------|----------------------

Options

event

Default is message.

ms

Default is 0. If greater than 0 a heart beat will be created from init().

msg

Default is ping. Message sent if ms is greater than 0.

Events

close

Emitted when an EventSource request is closed.

API

constructor({options}, [...msgs])

Creates an EventSource instance with optional messages to be transmitted on successful connection.

init(req, res)

Initializes an Event Source stream.

listenerCount()

Returns the number of listeners on the EventSource instance.

send(msg[, event, id]);

Sends a message over an EventSource instance.

setMaxListeners(n)

Sets the maximum listeners on the EventSource instance; default is 0.

License

Copyright (c) 2023 Jason Mulligan Licensed under the BSD-3 license.