engine.io-parser
Advanced tools
Comparing version
1.2.0 / 2015-01-11 | ||
================== | ||
* fix return type for decodePacket | ||
* README fixes | ||
* use travis matrix for better test runs | ||
* encode into binary only if needed | ||
* add test cases for base64 object encoding. | ||
* add encodeBase64Object to encoder for browser | ||
* avoid sending Blobs on PhantomJS (as on Android) | ||
* test that utf8 encoding is not on by default but can be switched on manually | ||
1.1.0 / 2014-07-16 | ||
@@ -3,0 +15,0 @@ ================== |
@@ -6,2 +6,3 @@ /** | ||
var keys = require('./keys'); | ||
var hasBinary = require('has-binary'); | ||
var sliceBuffer = require('arraybuffer.slice'); | ||
@@ -22,2 +23,16 @@ var base64encoder = require('base64-arraybuffer'); | ||
/** | ||
* Check if we are running in PhantomJS. | ||
* Uploading a Blob with PhantomJS does not work correctly, as reported here: | ||
* https://github.com/ariya/phantomjs/issues/11395 | ||
* @type boolean | ||
*/ | ||
var isPhantomJS = /PhantomJS/i.test(navigator.userAgent); | ||
/** | ||
* When true, avoids using Blobs to encode payloads. | ||
* @type boolean | ||
*/ | ||
var dontSendBlobs = isAndroid || isPhantomJS; | ||
/** | ||
* Current protocol version. | ||
@@ -93,2 +108,7 @@ */ | ||
// might be an object with { base64: true, data: dataAsBase64String } | ||
if (data && data.base64) { | ||
return encodeBase64Object(packet, callback); | ||
} | ||
// Sending data as a utf-8 string | ||
@@ -106,2 +126,8 @@ var encoded = packets[packet.type]; | ||
function encodeBase64Object(packet, callback) { | ||
// packet data is an object { base64: true, data: dataAsBase64String } | ||
var message = 'b' + exports.packets[packet.type] + packet.data.data; | ||
return callback(message); | ||
} | ||
/** | ||
@@ -146,3 +172,3 @@ * Encode packet helpers for binary types | ||
if (isAndroid) { | ||
if (dontSendBlobs) { | ||
return encodeBlobAsArrayBuffer(packet, supportsBinary, callback); | ||
@@ -279,4 +305,4 @@ } | ||
if (supportsBinary) { | ||
if (Blob && !isAndroid) { | ||
if (supportsBinary && hasBinary(packets)) { | ||
if (Blob && !dontSendBlobs) { | ||
return exports.encodePayloadAsBlob(packets, callback); | ||
@@ -283,0 +309,0 @@ } |
@@ -6,2 +6,3 @@ /** | ||
var utf8 = require('utf8'); | ||
var hasBinary = require('has-binary'); | ||
var after = require('after'); | ||
@@ -230,3 +231,3 @@ var keys = require('./keys'); | ||
if (supportsBinary) { | ||
if (supportsBinary && hasBinary(packets)) { | ||
return exports.encodePayloadAsBinary(packets, callback); | ||
@@ -233,0 +234,0 @@ } |
{ | ||
"name": "engine.io-parser", | ||
"description": "Parser for the client for the realtime Engine", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"homepage": "https://github.com/LearnBoost/engine.io-protocol", | ||
"devDependencies": { | ||
"mocha": "*", | ||
"expect.js": "*", | ||
"zuul": "1.6.3" | ||
"expect.js": "0.3.1", | ||
"mocha": "2.1.0", | ||
"zuul": "1.10.2" | ||
}, | ||
"dependencies": { | ||
"base64-arraybuffer": "0.1.2", | ||
"after": "0.8.1", | ||
"arraybuffer.slice": "0.0.6", | ||
"base64-arraybuffer": "0.1.2", | ||
"blob": "0.0.2", | ||
"has-binary": "0.1.5", | ||
"utf8": "2.0.0" | ||
@@ -17,0 +18,0 @@ }, |
188
Readme.md
@@ -12,4 +12,192 @@ | ||
## 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: | ||
```js | ||
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](http://browserify.org/): | ||
1. install the parser package | ||
```shell | ||
npm install engine.io-parser | ||
``` | ||
1. write your app code | ||
```js | ||
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' | ||
} | ||
}); | ||
}); | ||
``` | ||
1. build your app bundle | ||
```bash | ||
$ browserify app.js > bundle.js | ||
``` | ||
1. include on your page | ||
```html | ||
<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](https://github.com/defunctzombie/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`: | ||
- irc.freenode.net **#socket.io** | ||
- [Google Groups](http://groups.google.com/group/socket_io) | ||
- [Website](http://socket.io) | ||
## Development | ||
To contribute patches, run tests or benchmarks, make sure to clone the | ||
repository: | ||
```bash | ||
git clone git://github.com/LearnBoost/engine.io-parser.git | ||
``` | ||
Then: | ||
```bash | ||
cd engine.io-parser | ||
npm install | ||
``` | ||
See the `Tests` section above for how to run tests before submitting any patches. | ||
## License | ||
MIT |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
38606
29.55%895
2.64%203
1253.33%6
20%+ Added
+ Added
+ Added