Skip to content

Commit bacf7cf

Browse files
committed
chore(browser): ensure we are requiring buffer everywhere we use it
1 parent 4bf6afc commit bacf7cf

13 files changed

+52
-53
lines changed

lib/objectid.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const Buffer = require('buffer').Buffer;
34
const hostname = require('os').hostname;
45
const fnv1a24 = require('./fnv1a').fnv1a24;
56

@@ -42,14 +43,16 @@ function convertToHex(bytes) {
4243
}
4344

4445
/**
45-
* Create a new ObjectID instance
46-
*
47-
* @class
48-
* @param {(string|number)} id Can be a 24 byte hex string, 12 byte binary string or a Number.
49-
* @property {number} generationTime The generation time of this ObjectId instance
50-
* @return {ObjectID} instance of ObjectID.
46+
* A class representation of the BSON ObjectId type.
5147
*/
5248
class ObjectID {
49+
/**
50+
* Create an ObjectId type
51+
*
52+
* @param {(string|number)} id Can be a 24 byte hex string, 12 byte binary string or a Number.
53+
* @property {number} generationTime The generation time of this ObjectId instance
54+
* @return {ObjectID} instance of ObjectID.
55+
*/
5356
constructor(id) {
5457
// Duck-typing to support ObjectId from different npm packages
5558
if (id instanceof ObjectID) return id;

rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const pkg = require('./package.json');
34
const commonjs = require('rollup-plugin-commonjs');
45
const nodeBuiltins = require('rollup-plugin-node-builtins');
56
const nodeResolve = require('rollup-plugin-node-resolve');

test/node/bson_compliance_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const Buffer = require('buffer').Buffer;
4-
const BSON = require('../..');
4+
const BSON = require('../../lib/bson');
55
const Code = BSON.Code;
66
const Binary = BSON.Binary;
77
const Timestamp = BSON.Timestamp;

test/node/bson_corpus_tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const Buffer = require('buffer').Buffer;
4-
const BSON = require('../..');
4+
const BSON = require('../../lib/bson');
55
const Decimal128 = BSON.Decimal128;
66
const expect = require('chai').expect;
77

test/node/bson_test.js

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

3+
const Buffer = require('buffer').Buffer;
34
const expect = require('chai').expect;
4-
const BSON = require('../..');
5+
const BSON = require('../../lib/bson');
56
const Code = BSON.Code;
67
const BSONRegExp = BSON.BSONRegExp;
78
const Binary = BSON.Binary;
@@ -19,7 +20,6 @@ const MaxKey = BSON.MaxKey;
1920
const BinaryParser = require('../binary_parser').BinaryParser;
2021
const vm = require('vm');
2122
const assertBuffersEqual = require('./tools/utils').assertBuffersEqual;
22-
const normalizedFunctionString = require('../../lib/parser/utils').normalizedFunctionString;
2323

2424
// for tests
2525
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
@@ -94,7 +94,7 @@ describe('BSON', function() {
9494
* @ignore
9595
*/
9696
it('Should Correctly get BSON types from require', function(done) {
97-
var _mongodb = require('../..');
97+
var _mongodb = require('../../lib/bson');
9898
expect(_mongodb.ObjectID === ObjectID).to.be.ok;
9999
expect(_mongodb.Binary === Binary).to.be.ok;
100100
expect(_mongodb.Long === Long).to.be.ok;
@@ -212,24 +212,16 @@ describe('BSON', function() {
212212
0,
213213
0
214214
];
215-
var serialized_data = '';
215+
let serialized_data = '';
216216
// Convert to chars
217-
for (var i = 0; i < bytes.length; i++) {
217+
for (let i = 0; i < bytes.length; i++) {
218218
serialized_data = serialized_data + BinaryParser.fromByte(bytes[i]);
219219
}
220220

221-
runTestsOnBytesForBufferAndUint8Array(bytes, data => {
222-
let object = BSON.deserialize(data);
223-
expect('a_1').to.equal(object.name);
224-
expect(false).to.equal(object.unique);
225-
expect(1).to.equal(object.key.a);
226-
227-
object = BSON.deserialize(Uint8Array.from(bytes));
228-
expect('a_1').to.equal(object.name);
229-
expect(false).to.equal(object.unique);
230-
expect(1).to.equal(object.key.a);
231-
});
232-
221+
var object = BSON.deserialize(new Buffer(serialized_data, 'binary'));
222+
expect('a_1').to.equal(object.name);
223+
expect(false).to.equal(object.unique);
224+
expect(1).to.equal(object.key.a);
233225
done();
234226
});
235227

@@ -521,27 +513,29 @@ describe('BSON', function() {
521513
0,
522514
0
523515
];
524-
var serialized_data = '';
525-
526-
runTestsOnBytesForBufferAndUint8Array(bytes, data => {
527-
const object = BSON.deserialize(data);
528-
// Perform tests
529-
expect('hello').to.equal(object.string);
530-
expect([1, 2, 3]).to.deep.equal(object.array);
531-
expect(1).to.equal(object.hash.a);
532-
expect(2).to.equal(object.hash.b);
533-
expect(object.date != null).to.be.ok;
534-
expect(object.oid != null).to.be.ok;
535-
expect(object.binary != null).to.be.ok;
536-
expect(42).to.equal(object.int);
537-
expect(33.3333).to.equal(object.float);
538-
expect(object.regexp != null).to.be.ok;
539-
expect(true).to.equal(object.boolean);
540-
expect(object.where != null).to.be.ok;
541-
expect(object.dbref != null).to.be.ok;
542-
expect(object[null] == null).to.be.ok;
543-
});
516+
let serialized_data = '';
517+
518+
// Convert to chars
519+
for (let i = 0; i < bytes.length; i++) {
520+
serialized_data = serialized_data + BinaryParser.fromByte(bytes[i]);
521+
}
544522

523+
const object = BSON.deserialize(new Buffer(serialized_data, 'binary'));
524+
// Perform tests
525+
expect('hello').to.equal(object.string);
526+
expect([1, 2, 3]).to.deep.equal(object.array);
527+
expect(1).to.equal(object.hash.a);
528+
expect(2).to.equal(object.hash.b);
529+
expect(object.date != null).to.be.ok;
530+
expect(object.oid != null).to.be.ok;
531+
expect(object.binary != null).to.be.ok;
532+
expect(42).to.equal(object.int);
533+
expect(33.3333).to.equal(object.float);
534+
expect(object.regexp != null).to.be.ok;
535+
expect(true).to.equal(object.boolean);
536+
expect(object.where != null).to.be.ok;
537+
expect(object.dbref != null).to.be.ok;
538+
expect(object[null] == null).to.be.ok;
545539
done();
546540
});
547541

test/node/decimal128_tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const BSON = require('../..');
3+
const BSON = require('../../lib/bson');
44
const Decimal128 = BSON.Decimal128;
55
const expect = require('chai').expect;
66

test/node/detect_cyclic_dep_tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const BSON = require('../..');
3+
const BSON = require('../../lib/bson');
44
const expect = require('chai').expect;
55

66
describe('Cyclic Dependencies', function() {

test/node/map_tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const M = require('../../lib/map');
4-
const BSON = require('../..');
4+
const BSON = require('../../lib/bson');
55
const expect = require('chai').expect;
66

77
describe('Map', function() {

test/node/object_id_tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

3-
const BSON = require('../..');
3+
const Buffer = require('buffer').Buffer;
4+
const BSON = require('../../lib/bson');
45
const util = require('util');
56
const ObjectId = BSON.ObjectID;
67
const expect = require('chai').expect;

test/node/promote_values_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const Buffer = require('buffer').Buffer;
4-
const BSON = require('../..');
4+
const BSON = require('../../lib/bson');
55
const Int32 = BSON.Int32;
66
const Double = BSON.Double;
77
const BinaryParser = require('../binary_parser').BinaryParser;

test/node/serialize_with_buffer_tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const Buffer = require('buffer').Buffer;
4-
const BSON = require('../..');
4+
const BSON = require('../../lib/bson');
55
const expect = require('chai').expect;
66

77
describe('serializeWithBuffer', function() {

test/node/test_full_bson.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const BSON = require('../..');
3+
const BSON = require('../../lib/bson');
44
const Buffer = require('buffer').Buffer;
55
const BinaryParser = require('../binary_parser').BinaryParser;
66
const ObjectID = BSON.ObjectID;

test/node/to_bson_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const BSON = require('../..');
3+
const BSON = require('../../lib/bson');
44
const ObjectID = BSON.ObjectID;
55
const expect = require('chai').expect;
66

0 commit comments

Comments
 (0)