Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
Merge branch 'support-ws-buffer-binaryType' into node-v6-support
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlavin committed Sep 13, 2016
2 parents a66a912 + d40de86 commit cfa321e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ function setupWindow(window, args) {
url = resourceLoader.resolveResourceUrl(document, url);
const origin = `${window.location.protocol}//${window.location.host}`;
const ws = new WebSocket(url, { origin, protocol });

// The < 1.x implementations of ws used to allows 'buffer' to be defined
// as the binary type in node environments. Now, the supported type is
// 'nodebuffer'. Version of engine.io-client <= 1.6.12 use the 'buffer'
// type and this is a shim to allow that to keep working unti that version
// of engine.io-client does not need to be supported anymore
const origProperty = Object.getOwnPropertyDescriptor(WebSocket.prototype, 'binaryType');
Object.defineProperty(ws, 'binaryType', {
get: function get() {
return origProperty.get.call(this);
},
set: function set(type) {
if (type === 'buffer') {
type = 'nodebuffer';
}
return origProperty.set.call(this, type);
}
});
window._allWebSockets.push(ws);
return ws;
};
Expand Down
52 changes: 52 additions & 0 deletions test/websocket_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,56 @@ describe('WebSockets', function() {
browser.destroy();
});
});

describe('binary data', function() {
const browser = new Browser();

before(function() {
brains.static('/websockets-binary', `
<html>
<script>
var ws = new WebSocket('ws://example.com:3004');
ws.onmessage = function(message) {
// If message is received in a destroyed browser, this will throw an exception.
setTimeout(function() {
alert('timeout');
}, 100);
// Allow 'buffer' to act as an alias for 'nodebuffer' because
// versions of engine.io-client < 1.6.12 used 'buffer' for node clients
ws.binaryType = 'buffer';
alert(ws.binaryType);
};
</script>
</html>
`);
});

before(function(done) {
const self = this;
browser.on('alert', function(binaryType) {
self.binaryType = binaryType;
done();
});

browser.visit('/websockets-binary', ()=> null);
});

it('should convert buffer binary types to nodebuffer', function(done) {
browser.visit('/');

assert.equal(this.binaryType, 'nodebuffer');

serverWS.send('after destroy');

browser.wait(function() {
assert.equal(serverWS.readyState, WebSocket.CLOSED);
done();
});
});

after(function() {
browser.destroy();
});
});
});

0 comments on commit cfa321e

Please sign in to comment.