Comparing version 0.4.8 to 0.4.9
@@ -0,1 +1,6 @@ | ||
v0.4.9 - Mar 21st 2012 | ||
===================== | ||
* Various hixie bugfixes (such as proper close frame handling). [einaros] | ||
v0.4.8 - Feb 29th 2012 | ||
@@ -2,0 +7,0 @@ ===================== |
@@ -83,3 +83,4 @@ /*! | ||
this.reset(); | ||
this.emit('text', output.toString('utf8')); | ||
if (output == '') this.emit('close'); | ||
else this.emit('text', output.toString('utf8')); | ||
return tail; | ||
@@ -86,0 +87,0 @@ } |
@@ -38,2 +38,4 @@ /*! | ||
} | ||
if (data == '') return; | ||
var length = Buffer.byteLength(data) | ||
@@ -54,2 +56,33 @@ , buffer = new Buffer(2 + length); | ||
/** | ||
* Sends a close instruction to the remote party. | ||
* | ||
* @api public | ||
*/ | ||
Sender.prototype.close = function(code, data, mask, cb) { | ||
var buffer = new Buffer([0x0, 0xff]); | ||
try { | ||
this.socket.write(buffer, 'binary', cb); | ||
} catch (e) { | ||
this.error(e.toString()); | ||
} | ||
} | ||
/** | ||
* Sends a ping message to the remote party. Not available for hixie. | ||
* | ||
* @api public | ||
*/ | ||
Sender.prototype.ping = function(data, options) {} | ||
/** | ||
* Sends a pong message to the remote party. Not available for hixie. | ||
* | ||
* @api public | ||
*/ | ||
Sender.prototype.pong = function(data, options) {} | ||
/** | ||
* Handles an error | ||
@@ -56,0 +89,0 @@ * |
@@ -5,3 +5,3 @@ { | ||
"description": "simple to use, blazing fast and thoroughly tested websocket client, server and console for node.js, up-to-date against RFC-6455", | ||
"version": "0.4.8", | ||
"version": "0.4.9", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
var assert = require('assert') | ||
, expect = require('expect.js') | ||
, Receiver = require('../lib/Receiver.hixie'); | ||
require('should'); | ||
require('./hybi-common'); | ||
@@ -18,3 +18,3 @@ | ||
p.add(getBufferFromHexString(packet)); | ||
gotData.should.be.ok; | ||
expect(gotData).to.equal(true); | ||
}); | ||
@@ -34,5 +34,5 @@ | ||
p.add(getBufferFromHexString(packet)); | ||
gotData.should.be.ok; | ||
expect(gotData).to.equal(true); | ||
for (var i = 0; i < 2; ++i) { | ||
messages[i].should.eql('Hello'); | ||
expect(messages[i]).to.equal('Hello'); | ||
} | ||
@@ -62,14 +62,14 @@ }); | ||
} | ||
gotData.should.be.ok; | ||
expect(gotData).to.equal(true); | ||
for (var i = 0; i < 2; ++i) { | ||
messages[i].should.eql('Hello'); | ||
expect(messages[i]).to.equal('Hello'); | ||
} | ||
}); | ||
it('ignores empty messages', function() { | ||
it('emits an error if a payload doesnt start with 0x00', function() { | ||
var p = new Receiver(); | ||
var packets = [ | ||
'00 ff', | ||
'00 ff 00', | ||
'ff 00 ff 00 ff', | ||
'00 6c ff', | ||
'00 6c ff ff', | ||
'ff 00 6c ff 00 6c ff', | ||
'00', | ||
@@ -81,2 +81,3 @@ '6c 6c 6f', | ||
var gotData = false; | ||
var gotError = false; | ||
var messages = []; | ||
@@ -87,33 +88,25 @@ p.on('text', function(data) { | ||
}); | ||
p.on('error', function(reason, code) { | ||
gotError = code == true; | ||
}); | ||
for (var i = 0; i < packets.length; ++i) { | ||
for (var i = 0; i < packets.length && !gotError; ++i) { | ||
p.add(getBufferFromHexString(packets[i])); | ||
} | ||
gotData.should.be.ok; | ||
messages[0].should.eql(''); | ||
messages[1].should.eql(''); | ||
messages[2].should.eql(''); | ||
messages[3].should.eql(''); | ||
messages[4].should.eql(''); | ||
messages[5].should.eql('llo'); | ||
messages.length.should.eql(6); | ||
expect(gotError).to.equal(true); | ||
expect(messages[0]).to.equal('l'); | ||
expect(messages[1]).to.equal('l'); | ||
expect(messages.length).to.equal(2); | ||
}); | ||
it('emits an error if a payload doesnt start with 0x00', function() { | ||
it('can parse close messages', function() { | ||
var p = new Receiver(); | ||
var packets = [ | ||
'00 ff', | ||
'00 ff ff', | ||
'ff 00 ff 00 ff', | ||
'00', | ||
'6c 6c 6f', | ||
'ff' | ||
'00 ff' | ||
]; | ||
var gotData = false; | ||
var gotClose = false; | ||
var gotError = false; | ||
var messages = []; | ||
p.on('text', function(data) { | ||
gotData = true; | ||
messages.push(data); | ||
p.on('close', function() { | ||
gotClose = true; | ||
}); | ||
@@ -127,7 +120,5 @@ p.on('error', function(reason, code) { | ||
} | ||
gotError.should.be.ok; | ||
messages[0].should.eql(''); | ||
messages[1].should.eql(''); | ||
messages.length.should.eql(2); | ||
expect(gotClose).to.equal(true); | ||
expect(gotError).to.equal(false); | ||
}); | ||
}); |
@@ -7,30 +7,60 @@ var assert = require('assert') | ||
describe('Sender', function() { | ||
it('frames and sends a text message', function(done) { | ||
var message = 'Hello world'; | ||
var received; | ||
var socket = { | ||
write: function(data, encoding, cb) { | ||
received = data; | ||
process.nextTick(cb); | ||
} | ||
}; | ||
var sender = new Sender(socket, {}); | ||
sender.send(message, {}, function() { | ||
received.toString('utf8').should.eql('\u0000' + message + '\ufffd'); | ||
describe('#send', function() { | ||
it('frames and sends a text message', function(done) { | ||
var message = 'Hello world'; | ||
var received; | ||
var socket = { | ||
write: function(data, encoding, cb) { | ||
received = data; | ||
process.nextTick(cb); | ||
} | ||
}; | ||
var sender = new Sender(socket, {}); | ||
sender.send(message, {}, function() { | ||
received.toString('utf8').should.eql('\u0000' + message + '\ufffd'); | ||
done(); | ||
}); | ||
}); | ||
it('silently ignores empty messages (which would be close frames)', function(done) { | ||
var socket = { | ||
write: function(data, encoding, cb) { | ||
done(new Error('should not write this empty message')); | ||
} | ||
}; | ||
var sender = new Sender(socket, {}); | ||
sender.send('', {}, function() {}); | ||
done(); | ||
}); | ||
it('throws an exception for binary data', function(done) { | ||
var socket = { | ||
write: function(data, encoding, cb) { | ||
process.nextTick(cb); | ||
} | ||
}; | ||
var sender = new Sender(socket, {}); | ||
sender.on('error', function() { | ||
done(); | ||
}); | ||
sender.send(new Buffer(100), {binary: true}, function() {}); | ||
}); | ||
}); | ||
it('throws an exception for binary data', function(done) { | ||
var socket = { | ||
write: function(data, encoding, cb) { | ||
process.nextTick(cb); | ||
} | ||
}; | ||
var sender = new Sender(socket, {}); | ||
sender.on('error', function() { | ||
done(); | ||
describe('#close', function() { | ||
it('sends a hixie close frame', function(done) { | ||
var received; | ||
var socket = { | ||
write: function(data, encoding, cb) { | ||
received = data; | ||
process.nextTick(cb); | ||
} | ||
}; | ||
var sender = new Sender(socket, {}); | ||
sender.close(null, null, null, function() { | ||
received.toString('utf8').should.eql('\u0000\ufffd'); | ||
done(); | ||
}); | ||
}); | ||
sender.send(new Buffer(100), {binary: true}, function() {}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
213645
5570