Skip to content

Commit

Permalink
Merge pull request #50 from nkzawa/patch-5
Browse files Browse the repository at this point in the history
Fix encoding blob as base64
  • Loading branch information
rauchg committed Nov 22, 2015
2 parents 410d108 + 685cb71 commit 44b7d3e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function encodeBlob(packet, supportsBinary, callback) {

exports.encodeBase64Packet = function(packet, callback) {
var message = 'b' + exports.packets[packet.type];
if (Blob && packet.data instanceof Blob) {
if (Blob && packet.data instanceof global.Blob) {
var fr = new FileReader();
fr.onload = function() {
var b64 = fr.result.split(',')[1];
Expand Down
18 changes: 18 additions & 0 deletions test/browser/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

var parser = require('../../lib/browser.js');
var expect = require('expect.js');
var Blob = require('blob');

/**
* Shortcuts
Expand Down Expand Up @@ -71,4 +72,21 @@ describe('parser', function() {
fr.readAsArrayBuffer(data);
});
});

it('should encode blob as base64', function(done) {
var buf = new Uint8Array(5);
for (var i = 0; i < buf.length; i++) buf[i] = i;

encode({ type: 'message', data: new Blob([buf.buffer]) }, false, function(data) {
var packet = decode(data, 'blob');
expect(packet.type).to.eql('message');
expect(packet.data).to.be.a(global.Blob);
var fr = new FileReader();
fr.onload = function() {
expect(new Uint8Array(fr.result)).to.eql(buf);
done();
};
fr.readAsArrayBuffer(packet.data);
});
});
});

0 comments on commit 44b7d3e

Please # to comment.