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

sse-transform

v1.0.0

Published

A transform stream for converting JavaScript objects to Server-Sent Events.

Downloads

2

Readme

sse-transform

Build Status

A transform stream for converting JavaScript objects or JSON streams into Server-Sent Events. Only known fields (including comments) are processed and all other fields are ignored.

Default Mode

In default mode JSON objects are delimited using CrLf (\r\n). Each encountered JSON object is parsed and transformed into SSE format. If a JSON object isn't able to be parsed an error event is emitted on the stream and no transform is performed.

Basic Usage
'use strict';

var sse = require('sse-transform');
var through = require('through2');


var xform = sse();

xform.pipe(through(function (chunk, encoding, done) {
    console.log(chunk.toString('utf8'));
    done();
}));

xform.write('{"data":"foo"}\r\n');
xform.write('{"id":123,"data":"abc"}\r\n');
xform.write('{"event":"myevent","data":"foo\\nbar"}\r\n');
xform.write('{"event":"myevent2","data":"moo\\nmar"}\r\n');
xform.end();
Output
data:foo


id:123
data:abc


event:myevent
data:foo
data:bar


event:myevent2
data:moo
data:mar

Object Mode

In object mode each field processed such that if it is a string, newline delimiting rules are applied and each newline results in a new field entry (see data fields below). If a non-string-literal is encountered, the value is JSON.stringified during transform.

Basic Usage
'use strict';

var sse = require('sse-transform');
var through = require('through2');


var xform = sse({ objectMode: true });

xform.pipe(through(function (chunk, encoding, done) {
    console.log(chunk.toString('utf8'));
    done();
}));

xform.write({ data: 'foo'});
xform.write({ id:123, data: 'abc' });
xform.write({ event: 'myevent', data: 'foo\nbar' });
xform.write({ event: 'myevent2', data: 'moo\nmar' });
xform.write({ event: 'myevent', data: { foo: true, bar: 123, baz: 'foo\nbar' }});
xform.end();
Output
data:foo


id:123
data:abc


event:myevent
data:foo
data:bar


event:myevent2
data:moo
data:mar


event:myevent
data:{"foo":true,"bar":123,"baz":"foo\nbar"}

Fields

The JSON or JavaScript object written to the stream can contain any properties, but only the following will be consumed by sse-transform, any other being ignored.

  • event - An event can contain any character besides carriage return or line feed. If the value contains a carriage return or line feed, the value is split on that character and the first segment used as the value.

  • data - The data field can include any string, including strings with carriage returns and/or line feeds. If the string contains newline characters, the string is split and multiple data fields are written per the SSE specification.

  • id - An id can contain any character besides carriage return or line feed. If the value contains a carriage return or line feed, the value is split on that character and the first segment used as the value.

  • retry - Per the Mozilla docs: "The reconnection time to use when attempting to send the event. This must be an integer, specifying the reconnection time in milliseconds. If a non-integer value is specified, the field is ignored."

  • $comment - Setting this property to include a comment in the event (any line starting with :). Comments can be used for keep-alives, etc.