Skip to content

Commit 8490213

Browse files
committed
Merge pull request #698 from rib/binarytype
lib/WebSocket.js: expose binaryType property
2 parents dd9611e + b80b335 commit 8490213

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

lib/WebSocket.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function WebSocket(address, protocols, options) {
7171
this.readyState = null;
7272
this.supports = {};
7373
this.extensions = {};
74+
this._binaryType = 'nodebuffer';
7475

7576
if (Array.isArray(address)) {
7677
initAsServerClient.apply(this, address.concat(options));
@@ -371,6 +372,27 @@ Object.defineProperty(WebSocket.prototype, 'bufferedAmount', {
371372
}
372373
});
373374

375+
/**
376+
* Expose binaryType
377+
*
378+
* This deviates from the W3C interface since ws doesn't support the required
379+
* default "blob" type (instead we define a custom "nodebuffer" type).
380+
*
381+
* @see http://dev.w3.org/html5/websockets/#the-websocket-interface
382+
* @api public
383+
*/
384+
Object.defineProperty(WebSocket.prototype, 'binaryType', {
385+
get: function get() {
386+
return this._binaryType;
387+
},
388+
set: function set(type) {
389+
if (type === 'arraybuffer' || type === 'nodebuffer')
390+
this._binaryType = type;
391+
else
392+
throw new SyntaxError('unsupported binaryType: must be either "nodebuffer" or "arraybuffer"');
393+
}
394+
});
395+
374396
/**
375397
* Emulates the W3C Browser based WebSocket interface using function members.
376398
*
@@ -415,6 +437,8 @@ WebSocket.prototype.addEventListener = function(method, listener) {
415437
var target = this;
416438

417439
function onMessage (data, flags) {
440+
if (flags.binary && this.binaryType === 'arraybuffer')
441+
data = new Uint8Array(data).buffer;
418442
listener.call(target, new MessageEvent(data, !!flags.binary, target));
419443
}
420444

test/WebSocket.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,10 @@ describe('WebSocket', function() {
15441544
ws.onclose = listener;
15451545
ws.onopen = listener;
15461546

1547+
assert.ok(ws.binaryType === 'nodebuffer');
1548+
ws.binaryType = 'arraybuffer';
1549+
assert.ok(ws.binaryType === 'arraybuffer');
1550+
15471551
assert.ok(ws.onopen === listener);
15481552
assert.ok(ws.onmessage === listener);
15491553
assert.ok(ws.onclose === listener);
@@ -1694,6 +1698,63 @@ describe('WebSocket', function() {
16941698
client.send('hi')
16951699
});
16961700
});
1701+
1702+
it('should pass binary data as a node.js Buffer by default', function(done) {
1703+
server.createServer(++port, function(srv) {
1704+
var ws = new WebSocket('ws://localhost:' + port);
1705+
var array = new Uint8Array(4096);
1706+
1707+
ws.onopen = function() {
1708+
ws.send(array, {binary: true});
1709+
};
1710+
ws.onmessage = function(messageEvent) {
1711+
assert.ok(messageEvent.binary);
1712+
assert.ok(ws.binaryType === 'nodebuffer');
1713+
assert.ok(messageEvent.data instanceof Buffer);
1714+
ws.terminate();
1715+
srv.close();
1716+
done();
1717+
};
1718+
});
1719+
});
1720+
1721+
it('should pass an ArrayBuffer for event.data if binaryType = arraybuffer', function(done) {
1722+
server.createServer(++port, function(srv) {
1723+
var ws = new WebSocket('ws://localhost:' + port);
1724+
ws.binaryType = 'arraybuffer';
1725+
var array = new Uint8Array(4096);
1726+
1727+
ws.onopen = function() {
1728+
ws.send(array, {binary: true});
1729+
};
1730+
ws.onmessage = function(messageEvent) {
1731+
assert.ok(messageEvent.binary);
1732+
assert.ok(messageEvent.data instanceof ArrayBuffer);
1733+
ws.terminate();
1734+
srv.close();
1735+
done();
1736+
};
1737+
});
1738+
});
1739+
1740+
it('should ignore binaryType for text messages', function(done) {
1741+
server.createServer(++port, function(srv) {
1742+
var ws = new WebSocket('ws://localhost:' + port);
1743+
ws.binaryType = 'arraybuffer';
1744+
1745+
ws.onopen = function() {
1746+
ws.send('foobar');
1747+
};
1748+
ws.onmessage = function(messageEvent) {
1749+
assert.ok(!messageEvent.binary);
1750+
assert.ok(typeof messageEvent.data === 'string');
1751+
ws.terminate();
1752+
srv.close();
1753+
done();
1754+
};
1755+
});
1756+
});
1757+
16971758
});
16981759

16991760
describe('ssl', function() {

0 commit comments

Comments
 (0)