Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better buffer handling #20

Merged
merged 1 commit into from Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions .travis.yml
Expand Up @@ -5,6 +5,7 @@ node_js:
- 4
- 5
- 6
- 8

before_script:
- export DISPLAY=:99.0
Expand All @@ -13,6 +14,3 @@ before_script:
notifications:
email:
- damon.oehlman@gmail.com



1 change: 1 addition & 0 deletions package.json
Expand Up @@ -25,6 +25,7 @@
"homepage": "https://github.com/DamonOehlman/pull-ws",
"dependencies": {
"relative-url": "^1.0.2",
"safe-buffer": "^5.1.1",
"ws": "^1.1.0"
},
"devDependencies": {
Expand Down
14 changes: 11 additions & 3 deletions source.js
Expand Up @@ -6,6 +6,15 @@
<<< examples/read.js

**/
var Buffer = require('safe-buffer').Buffer;

// copied from github.com/feross/buffer
// Some ArrayBuffers are not passing the instanceof check, so we need to do a bit more work :(
function isArrayBuffer (obj) {
return obj instanceof ArrayBuffer ||
(obj != null && obj.constructor != null && obj.constructor.name === 'ArrayBuffer' &&
typeof obj.byteLength === 'number')
}

module.exports = function(socket, cb) {
var buffer = [];
Expand All @@ -14,9 +23,8 @@ module.exports = function(socket, cb) {
var started = false;
socket.addEventListener('message', function(evt) {
var data = evt.data;

if (data instanceof ArrayBuffer) {
data = new Buffer(data);
if (isArrayBuffer(data)) {
data = Buffer.from(data);
}

if (receiver) {
Expand Down