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

Commit

Permalink
feat(binary): removing browser buffer support
Browse files Browse the repository at this point in the history
Removes the fallback to UInt8Array if Buffer does not exist
(a.k.a. on web). Now requires user to polyfill Buffer

BREAKING CHANGE:
Removes out-of-box support for Binary serialization on web platforms.
Users will have to polyfill node Buffer type
  • Loading branch information
daprahamian authored and mbroadst committed Nov 6, 2017
1 parent 7818d61 commit dc556c8
Showing 1 changed file with 2 additions and 17 deletions.
19 changes: 2 additions & 17 deletions lib/bson/binary.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
'use strict';

if (typeof Buffer !== 'undefined') {
var bufferConstructor = new Buffer(1) instanceof Uint8Array ? Buffer : Uint8Array;
} else {
bufferConstructor = Uint8Array;
}

/**
* Module dependencies.
* @ignore
Expand All @@ -16,7 +10,7 @@ function convert(integer) {
}

function toExtendedJSON(obj) {
var base64String = typeof Buffer !== 'undefined' ? obj.buffer.toString('base64') : obj.toBase64();
var base64String = obj.buffer.toString('base64');

return {
$binary: {
Expand All @@ -29,16 +23,7 @@ function toExtendedJSON(obj) {
function fromExtendedJSON(BSON, doc) {
var type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;

if (typeof Buffer !== 'undefined' && bufferConstructor === Buffer) {
var data = new Buffer(doc.$binary.base64, 'base64');
} else {
data = new Uint8Array(
BSON.Binary
.fromBase64(doc.$binary.base64)
.split('')
.map(c => c.charCodeAt(0))
);
}
var data = new Buffer(doc.$binary.base64, 'base64');

return new BSON.Binary(data, type);
}
Expand Down

0 comments on commit dc556c8

Please sign in to comment.