[6a3a178] | 1 |
|
---|
| 2 | # engine.io-parser
|
---|
| 3 |
|
---|
| 4 | [![Build Status](https://github.com/socketio/engine.io-parser/workflows/CI/badge.svg)](https://github.com/socketio/engine.io-parser/actions)
|
---|
| 5 | [![NPM version](https://badge.fury.io/js/engine.io-parser.svg)](https://npmjs.com/package/engine.io-parser)
|
---|
| 6 |
|
---|
| 7 | This is the JavaScript parser for the engine.io protocol encoding,
|
---|
| 8 | shared by both
|
---|
| 9 | [engine.io-client](https://github.com/socketio/engine.io-client) and
|
---|
| 10 | [engine.io](https://github.com/socketio/engine.io).
|
---|
| 11 |
|
---|
| 12 | ## How to use
|
---|
| 13 |
|
---|
| 14 | ### Standalone
|
---|
| 15 |
|
---|
| 16 | The parser can encode/decode packets, payloads, and payloads as binary
|
---|
| 17 | with the following methods: `encodePacket`, `decodePacket`, `encodePayload`,
|
---|
| 18 | `decodePayload`.
|
---|
| 19 |
|
---|
| 20 | Example:
|
---|
| 21 |
|
---|
| 22 | ```js
|
---|
| 23 | const parser = require("engine.io-parser");
|
---|
| 24 | const data = Buffer.from([ 1, 2, 3, 4 ]);
|
---|
| 25 |
|
---|
| 26 | parser.encodePacket({ type: "message", data }, encoded => {
|
---|
| 27 | const decodedData = parser.decodePacket(encoded); // decodedData === data
|
---|
| 28 | });
|
---|
| 29 | ```
|
---|
| 30 |
|
---|
| 31 | ### With browserify
|
---|
| 32 |
|
---|
| 33 | Engine.IO Parser is a commonjs module, which means you can include it by using
|
---|
| 34 | `require` on the browser and package using [browserify](http://browserify.org/):
|
---|
| 35 |
|
---|
| 36 | 1. install the parser package
|
---|
| 37 |
|
---|
| 38 | ```shell
|
---|
| 39 | npm install engine.io-parser
|
---|
| 40 | ```
|
---|
| 41 |
|
---|
| 42 | 1. write your app code
|
---|
| 43 |
|
---|
| 44 | ```js
|
---|
| 45 | const parser = require("engine.io-parser");
|
---|
| 46 |
|
---|
| 47 | const testBuffer = new Int8Array(10);
|
---|
| 48 | for (let i = 0; i < testBuffer.length; i++) testBuffer[i] = i;
|
---|
| 49 |
|
---|
| 50 | const packets = [{ type: "message", data: testBuffer.buffer }, { type: "message", data: "hello" }];
|
---|
| 51 |
|
---|
| 52 | parser.encodePayload(packets, encoded => {
|
---|
| 53 | parser.decodePayload(encoded,
|
---|
| 54 | (packet, index, total) => {
|
---|
| 55 | const isLast = index + 1 == total;
|
---|
| 56 | if (!isLast) {
|
---|
| 57 | const buffer = new Int8Array(packet.data); // testBuffer
|
---|
| 58 | } else {
|
---|
| 59 | const message = packet.data; // "hello"
|
---|
| 60 | }
|
---|
| 61 | });
|
---|
| 62 | });
|
---|
| 63 | ```
|
---|
| 64 |
|
---|
| 65 | 1. build your app bundle
|
---|
| 66 |
|
---|
| 67 | ```bash
|
---|
| 68 | $ browserify app.js > bundle.js
|
---|
| 69 | ```
|
---|
| 70 |
|
---|
| 71 | 1. include on your page
|
---|
| 72 |
|
---|
| 73 | ```html
|
---|
| 74 | <script src="/path/to/bundle.js"></script>
|
---|
| 75 | ```
|
---|
| 76 |
|
---|
| 77 | ## Features
|
---|
| 78 |
|
---|
| 79 | - Runs on browser and node.js seamlessly
|
---|
| 80 | - Runs inside HTML5 WebWorker
|
---|
| 81 | - Can encode and decode packets
|
---|
| 82 | - Encodes from/to ArrayBuffer or Blob when in browser, and Buffer or ArrayBuffer in Node
|
---|
| 83 |
|
---|
| 84 | ## API
|
---|
| 85 |
|
---|
| 86 | Note: `cb(type)` means the type is a callback function that contains a parameter of type `type` when called.
|
---|
| 87 |
|
---|
| 88 | ### Node
|
---|
| 89 |
|
---|
| 90 | - `encodePacket`
|
---|
| 91 | - Encodes a packet.
|
---|
| 92 | - **Parameters**
|
---|
| 93 | - `Object`: the packet to encode, has `type` and `data`.
|
---|
| 94 | - `data`: can be a `String`, `Number`, `Buffer`, `ArrayBuffer`
|
---|
| 95 | - `Boolean`: binary support
|
---|
| 96 | - `Function`: callback, returns the encoded packet (`cb(String)`)
|
---|
| 97 | - `decodePacket`
|
---|
| 98 | - Decodes a packet. Data also available as an ArrayBuffer if requested.
|
---|
| 99 | - Returns data as `String` or (`Blob` on browser, `ArrayBuffer` on Node)
|
---|
| 100 | - **Parameters**
|
---|
| 101 | - `String` | `ArrayBuffer`: the packet to decode, has `type` and `data`
|
---|
| 102 | - `String`: optional, the binary type
|
---|
| 103 |
|
---|
| 104 | - `encodePayload`
|
---|
| 105 | - Encodes multiple messages (payload).
|
---|
| 106 | - If any contents are binary, they will be encoded as base64 strings. Base64
|
---|
| 107 | encoded strings are marked with a b before the length specifier
|
---|
| 108 | - **Parameters**
|
---|
| 109 | - `Array`: an array of packets
|
---|
| 110 | - `Function`: callback, returns the encoded payload (`cb(String)`)
|
---|
| 111 | - `decodePayload`
|
---|
| 112 | - Decodes data when a payload is maybe expected. Possible binary contents are
|
---|
| 113 | decoded from their base64 representation.
|
---|
| 114 | - **Parameters**
|
---|
| 115 | - `String`: the payload
|
---|
| 116 | - `Function`: callback, returns (cb(`Object`: packet, `Number`:packet index, `Number`:packet total))
|
---|
| 117 |
|
---|
| 118 | ## Tests
|
---|
| 119 |
|
---|
| 120 | Standalone tests can be run with `npm test` which will run the node.js tests.
|
---|
| 121 |
|
---|
| 122 | Browser tests are run using [zuul](https://github.com/defunctzombie/zuul).
|
---|
| 123 | (You must have zuul setup with a saucelabs account.)
|
---|
| 124 |
|
---|
| 125 | You can run the tests locally using the following command:
|
---|
| 126 |
|
---|
| 127 | ```
|
---|
| 128 | npm run test:browser
|
---|
| 129 | ```
|
---|
| 130 |
|
---|
| 131 | ## Support
|
---|
| 132 |
|
---|
| 133 | The support channels for `engine.io-parser` are the same as `socket.io`:
|
---|
| 134 | - irc.freenode.net **#socket.io**
|
---|
| 135 | - [Github Discussions](https://github.com/socketio/socket.io/discussions)
|
---|
| 136 | - [Website](https://socket.io)
|
---|
| 137 |
|
---|
| 138 | ## Development
|
---|
| 139 |
|
---|
| 140 | To contribute patches, run tests or benchmarks, make sure to clone the
|
---|
| 141 | repository:
|
---|
| 142 |
|
---|
| 143 | ```bash
|
---|
| 144 | git clone git://github.com/socketio/engine.io-parser.git
|
---|
| 145 | ```
|
---|
| 146 |
|
---|
| 147 | Then:
|
---|
| 148 |
|
---|
| 149 | ```bash
|
---|
| 150 | cd engine.io-parser
|
---|
| 151 | npm ci
|
---|
| 152 | ```
|
---|
| 153 |
|
---|
| 154 | See the `Tests` section above for how to run tests before submitting any patches.
|
---|
| 155 |
|
---|
| 156 | ## License
|
---|
| 157 |
|
---|
| 158 | MIT
|
---|