Skip to content

Commit

Permalink
[fix] Do not mutate the input upon packet encoding (#105)
Browse files Browse the repository at this point in the history
Fixes #95
  • Loading branch information
darrachequesne committed Nov 1, 2018
1 parent ebdf467 commit 2377dcc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
3 changes: 1 addition & 2 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ function encodeBlobAsArrayBuffer(packet, supportsBinary, callback) {

var fr = new FileReader();
fr.onload = function() {
packet.data = fr.result;
exports.encodePacket(packet, supportsBinary, true, callback);
exports.encodePacket({ type: packet.type, data: fr.result }, supportsBinary, true, callback);
};
return fr.readAsArrayBuffer(packet.data);
}
Expand Down
10 changes: 3 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {
if (Buffer.isBuffer(packet.data)) {
return encodeBuffer(packet, supportsBinary, callback);
} else if (packet.data && (packet.data.buffer || packet.data) instanceof ArrayBuffer) {
packet.data = arrayBufferToBuffer(packet.data);
return encodeBuffer(packet, supportsBinary, callback);
return encodeBuffer({ type: packet.type, data: arrayBufferToBuffer(packet.data) }, supportsBinary, callback);
}

// Sending data as a utf-8 string
Expand Down Expand Up @@ -102,12 +101,9 @@ function encodeBuffer(packet, supportsBinary, callback) {
*/

exports.encodeBase64Packet = function(packet, callback){
if (!Buffer.isBuffer(packet.data)) {
packet.data = arrayBufferToBuffer(packet.data);
}

var data = Buffer.isBuffer(packet.data) ? packet.data : arrayBufferToBuffer(packet.data);
var message = 'b' + packets[packet.type];
message += packet.data.toString('base64');
message += data.toString('base64');
return callback(message);
};

Expand Down

0 comments on commit 2377dcc

Please sign in to comment.