Detalhes do pacote

tiny-eventsource

avoidwork89BSD-3-Clause3.0.8

Tiny EventSource simplifies server-sent events for API servers

eventsource, sse, push

readme (leia-me)

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.

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

3.0.8

  • Bump rollup from 4.2.0 to 4.3.0 #17
  • Bump eslint from 8.52.0 to 8.53.0 #16
  • Bump rollup from 4.1.5 to 4.2.0 #15
  • Lowering engines.node to >=6 for const, removing ./types/constants.d.ts 57155ec
  • Emitting CLOSE event when a request emits CLOSE for easy synchronization of state 208713b
  • Fixing the heartbeat test, updating README.md a688a7b

3.0.6

31 October 2023

  • Generated CHANGELOG.md f776b7c
  • Building 1efe982
  • Adding on() & off() to type definition of EventSource d6e8fd8

3.0.5

30 October 2023

3.0.4

30 October 2023

3.0.3

30 October 2023

  • Adding listenerCount() & setMaxListeners() to eventsource.d.ts & README.md a28c87f
  • Generated CHANGELOG.md 8eb851a

3.0.2

30 October 2023

  • Bump rollup from 4.1.4 to 4.1.5 #14
  • Bump actions/setup-node from 3 to 4 #13
  • Bump eslint from 8.51.0 to 8.52.0 #12
  • Bump @babel/traverse from 7.23.0 to 7.23.2 #11
  • Bump rollup from 4.0.2 to 4.1.4 #10
  • Exporting EventSource class, updating types 67d9eed
  • Generating CHANGELOG.md f67b69e
  • Generated CHANGELOG.md 1906a1f

3.0.1

10 October 2023

  • Bump eslint from 8.50.0 to 8.51.0 #9
  • Bump rollup from 4.0.0 to 4.0.2 #8
  • Bump rollup from 3.29.4 to 4.0.0 #7
  • Setting extension of main to .cjs for deprecated tooling d2b6a16
  • Generating CHANGELOG.md 01fca7a

3.0.0

2 October 2023

  • Adding constants, updating build files d684566
  • Updating tests for 100% coverage, updating README.md, updating ignore files b29618b
  • Updating README.md, LICENSE, & description in package.json c5486bf

2.0.0

4 October 2022

  • ES module syntax #6
  • Bump acorn from 7.1.0 to 7.1.1 #3
  • Bump mixin-deep from 1.3.1 to 1.3.2 #2
  • Bump js-yaml from 3.5.5 to 3.13.1 #1
  • Initial commit of refactoring to ES module syntax 10b72a5
  • Building ./dist & ./types files 40f5c50
  • Updating tests to run in mocha with assert 5384331

1.2.1

28 November 2019

  • Fixing cache-control header such that it's not cachable c8e04ed

1.2.0

18 December 2018

  • Fixing a customization gap for heart beats cc98b32

1.1.0

16 December 2018

  • Adding test 6121958
  • Adding this.heartbeat: {ms: 0, msg: "ping"} attribute which is utilized in init() to create a heart beat while there's listeners 184da9f
  • Adding test for heart beat 5b61032

1.0.3

15 December 2018

  • Setting MaxListeners to 0 (Infinity) 260a6a2

1.0.2

28 November 2018

1.0.1

28 November 2018

  • Fixing send() by removing JSON.stringify() of data c028ba8

1.0.0

19 November 2018