Skip to content

Commit

Permalink
fix: properly encode empty buffer in base64 encoding (#131)
Browse files Browse the repository at this point in the history
An empty buffer was encoded into `bundefined` instead of `b`
(reproduced on Chrome v108 / Ubuntu).

Co-authored-by: Mauricio Narvaez <nvz@fb.com>
  • Loading branch information
maunvz and Mauricio Narvaez committed Jan 6, 2023
1 parent 7fbea71 commit 351ba82
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/encodePacket.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const encodeBlobAsBase64 = (
const fileReader = new FileReader();
fileReader.onload = function() {
const content = (fileReader.result as string).split(",")[1];
callback("b" + content);
callback("b" + (content || ""));
};
return fileReader.readAsDataURL(data);
};
Expand Down
12 changes: 12 additions & 0 deletions test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ describe("engine.io-parser (browser only)", () => {
done();
});
});

it("should encode/decode a string + a 0-length ArrayBuffer payload", done => {
const packets: Packet[] = [
{ type: "message", data: "test" },
{ type: "message", data: createArrayBuffer([]) }
];
encodePayload(packets, payload => {
expect(payload).to.eql("4test\x1eb");
expect(decodePayload(payload, "arraybuffer")).to.eql(packets);
done();
});
});
}
});
});

0 comments on commit 351ba82

Please sign in to comment.