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

@jonny/engine.io-parser

v2.2.0

Published

Parser for the client for the realtime Engine

Downloads

129

Readme

engine.io-parser

Build Status NPM version

This is the JavaScript parser for the engine.io protocol encoding, shared by both engine.io-client and engine.io.

How to use

Standalone

The parser can encode/decode packets, payloads, and payloads as binary with the following methods: encodePacket, decodePacket, encodePayload, decodePayload, encodePayloadAsBinary, decodePayloadAsBinary.

The browser-side parser also includes encodePayloadAsArrayBuffer and encodePayloadAsBlob.

Example:

var parser = require('engine.io-parser');

var data = new Buffer(5);
for (var i = 0; i < data.length; i++) { data[i] = i; }

parser.encodePacket({ type: 'message', data: data }, function(encoded) {
  var decodedData = parser.decodePacket(encoded); // { type: 'message', data: data }
});

With browserify

Engine.IO Parser is a commonjs module, which means you can include it by using require on the browser and package using browserify:

  1. install the parser package

    npm install engine.io-parser
  2. write your app code

    var parser = require('engine.io-parser');
    
    var testBuffer = new Int8Array(10);
    for (var i = 0; i < testBuffer.length; i++) testBuffer[i] = i;
    
    var packets = [{ type: 'message', data: testBuffer.buffer }, { type: 'message', data: 'hello' }];
    
    parser.encodePayload(packets, function(encoded) {
      parser.decodePayload(encoded,
        function(packet, index, total) {
          var isLast = index + 1 == total;
          if (!isLast) {
            var buffer = new Int8Array(packet.data); // testBuffer
          } else {
            var message = packet.data; // 'hello'
          }
        });
    });
  3. build your app bundle

    $ browserify app.js > bundle.js
  4. include on your page

    <script src="/path/to/bundle.js"></script>

Features

  • Runs on browser and node.js seamlessly
  • Runs inside HTML5 WebWorker
  • Can encode and decode packets
    • Encodes from/to ArrayBuffer or Blob when in browser, and Buffer or ArrayBuffer in Node

API

Note: cb(type) means the type is a callback function that contains a parameter of type type when called.

Node

  • encodePacket

    • Encodes a packet.
    • Parameters
      • Object: the packet to encode, has type and data.
        • data: can be a String, Number, Buffer, ArrayBuffer
      • Boolean: optional, binary support
      • Function: callback, returns the encoded packet (cb(String))
  • decodePacket

    • Decodes a packet. Data also available as an ArrayBuffer if requested.
    • Returns data as String or (Blob on browser, ArrayBuffer on Node)
    • Parameters
      • String | ArrayBuffer: the packet to decode, has type and data
      • String: optional, the binary type
  • encodeBase64Packet

    • Encodes a packet with binary data in a base64 string (String)
    • Parameters
      • Object: the packet to encode, has type and data
      • Function: callback, returns the base64 encoded message (cb(String))
  • decodeBase64Packet

    • Decodes a packet encoded in a base64 string.
    • Parameters
      • String: the base64 encoded message
      • String: optional, the binary type
  • encodePayload

    • Encodes multiple messages (payload).
    • If any contents are binary, they will be encoded as base64 strings. Base64 encoded strings are marked with a b before the length specifier
    • Parameters
      • Array: an array of packets
      • Boolean: optional, binary support
      • Function: callback, returns the encoded payload (cb(String))
  • decodePayload

    • Decodes data when a payload is maybe expected. Possible binary contents are decoded from their base64 representation.
    • Parameters
      • String: the payload
      • String: optional, the binary type
      • Function: callback, returns (cb(Object: packet, Number:packet index, Number:packet total))
  • encodePayloadAsBinary

    • Encodes multiple messages (payload) as binary.
    • Parameters
      • Array: an array of packets
      • Function: callback, returns the encoded payload (cb(Buffer))
  • decodePayloadAsBinary

    • Decodes data when a payload is maybe expected. Strings are decoded by interpreting each byte as a key code for entries marked to start with 0. See description of encodePayloadAsBinary.
    • Parameters
      • Buffer: the buffer
      • String: optional, the binary type
      • Function: callback, returns the decoded packet (cb(Object))

Browser

  • encodePayloadAsArrayBuffer
    • Encodes multiple messages (payload) as binary.
    • Parameters
      • Array: an array of packets
      • Function: callback, returns the encoded payload (cb(ArrayBuffer))
  • encodePayloadAsBlob
    • Encodes multiple messages (payload) as blob.
    • Parameters
      • Array: an array of packets
      • Function: callback, returns the encoded payload (cb(Blob))

Tests

Standalone tests can be run with make test which will run both node.js and browser tests.

Browser tests are run using zuul. (You must have zuul setup with a saucelabs account.)

You can run the tests locally using the following command:

./node_modules/.bin/zuul --local 8080 -- test/index.js

Support

The support channels for engine.io-parser are the same as socket.io:

Development

To contribute patches, run tests or benchmarks, make sure to clone the repository:

git clone git://github.com/LearnBoost/engine.io-parser.git

Then:

cd engine.io-parser
npm install

See the Tests section above for how to run tests before submitting any patches.

License

MIT