mirror of
https://github.com/socketio/socket.io.git
synced 2026-01-14 01:17:55 -05:00
Separated the encoding and decoding into two public-facing objects, Encoder and Decoder. Both objects take nothing on construction. Encoder has a single method, encode, that mimics the previous version's function encode (takes a packet object and a callback). Decoder has a single method too, add, that takes any object (packet string or binary data). Decoder emits a 'decoded' event when it has received all of the parts of a packet. The only parameter for the decoded event is the reconstructed packet. I am hesitant about the Encoder.encode vs Decoder.add thing. Should it be more consistent, or should it stay like this where the function names are more descriptive? Also, rewrote the test helper functions to deal with new event-based decoding. Wrote a new test in test/arraybuffer.js that tests for memory leaks in Decoder as well.
48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
var parser = require('../index.js');
|
|
var expect = require('expect.js');
|
|
var helpers = require('./helpers.js');
|
|
var encoder = new parser.Encoder();
|
|
|
|
describe('parser', function() {
|
|
it('encodes an ArrayBuffer', function() {
|
|
var packet = {
|
|
type: parser.BINARY_EVENT,
|
|
data: new ArrayBuffer(2),
|
|
id: 0,
|
|
nsp: '/'
|
|
};
|
|
helpers.test_bin(packet);
|
|
});
|
|
|
|
it('encodes ArrayBuffers deep in JSON', function() {
|
|
var packet = {
|
|
type: parser.BINARY_EVENT,
|
|
data: {a: 'hi', b: {why: new ArrayBuffer(3)}, c: {a: 'bye', b: { a: new ArrayBuffer(6)}}},
|
|
id: 999,
|
|
nsp: '/deep'
|
|
};
|
|
helpers.test_bin(packet);
|
|
});
|
|
|
|
it('cleans itself up on close', function() {
|
|
var packet = {
|
|
type: parser.BINARY_EVENT,
|
|
data: [new ArrayBuffer(2), new ArrayBuffer(3)],
|
|
id: 0,
|
|
nsp: '/'
|
|
};
|
|
|
|
encoder.encode(packet, function(encodedPackets) {
|
|
var decoder = new parser.Decoder();
|
|
decoder.on('decoded', function(packet) {
|
|
throw new Error("received a packet when not all binary data was sent.");
|
|
});
|
|
|
|
decoder.add(encodedPackets[0]); // add metadata
|
|
decoder.add(encodedPackets[1]); // add first attachment
|
|
decoder.destroy(); // destroy before all data added
|
|
expect(decoder.reconstructor.buffers.length).to.be(0); // expect that buffer is clean
|
|
});
|
|
});
|
|
});
|