Skip to content
This repository has been archived by the owner on Feb 3, 2022. It is now read-only.

Commit

Permalink
fix(binary): generate valid base64 for non-Buffer types
Browse files Browse the repository at this point in the history
  • Loading branch information
kvwalker committed May 18, 2018
1 parent fcedb3d commit 44afbb8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/bson/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ function convert(integer) {
}

function toExtendedJSON(obj) {
var base64String = obj.buffer.toString('base64');
var base64String = Buffer.isBuffer(obj.buffer)
? obj.buffer.toString('base64')
: Buffer.from(obj.buffer).toString('base64');

return {
$binary: {
Expand Down
3 changes: 3 additions & 0 deletions test/extend_mongodb_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ describe('Extended JSON', function() {
_id: { $nin: [new ObjectID('591801a468f9e7024b6235ea')] }
});
expect(serialized).to.equal('{"_id":{"$nin":[{"$oid":"591801a468f9e7024b6235ea"}]}}');

serialized = extJSON.stringify(new Binary(new Uint8Array([1, 2, 3, 4, 5])));
expect(serialized).to.equal('{"$binary":{"base64":"AQIDBAU=","subType":"00"}}');
});

it('should correctly parse null values', function() {
Expand Down

0 comments on commit 44afbb8

Please sign in to comment.