Skip to content

Commit

Permalink
chore: fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
daprahamian committed Mar 8, 2019
1 parent f4b16d9 commit 53ef8ad
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
8 changes: 6 additions & 2 deletions lib/db_ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ Object.defineProperty(DBRef.prototype, '_bsontype', { value: 'DBRef' });
// the 1.x parser used a "namespace" property, while 4.x uses "collection". To ensure backwards
// compatibility, let's expose "namespace"
Object.defineProperty(DBRef.prototype, 'namespace', {
get() { return this.collection; },
set(val) { this.collection = val; },
get() {
return this.collection;
},
set(val) {
this.collection = val;
},
configurable: false
});
module.exports = DBRef;
22 changes: 10 additions & 12 deletions lib/extended_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,15 @@ const BSON_TYPE_MAPPINGS = {
Decimal128: o => new Decimal128(o.bytes),
Double: o => new Double(o.value),
Int32: o => new Int32(o.value),
Long: o => Long.fromBits( // underscore variants for 1.x backwards compatibility
o.low != null ? o.low : o.low_,
o.low != null ? o.high : o.high_,
o.low != null ? o.unsigned : o.unsigned_
),
MaxKey: o => new MaxKey(),
MinKey: o => new MinKey(),
Long: o =>
Long.fromBits(
// underscore variants for 1.x backwards compatibility
o.low != null ? o.low : o.low_,
o.low != null ? o.high : o.high_,
o.low != null ? o.unsigned : o.unsigned_
),
MaxKey: () => new MaxKey(),
MinKey: () => new MinKey(),
ObjectID: o => new ObjectId(o),
ObjectId: o => new ObjectId(o), // support 4.0.0/4.0.1 before _bsontype was reverted back to ObjectID
BSONRegExp: o => new BSONRegExp(o.pattern, o.options),
Expand All @@ -291,16 +293,13 @@ function serializeDocument(doc, options) {

const bsontype = doc._bsontype;
if (typeof bsontype === 'undefined') {

// It's a regular object. Recursively serialize its property values.
const _doc = {};
for (let name in doc) {
_doc[name] = serializeValue(doc[name], options);
}
return _doc;

} else if (typeof bsontype === 'string') {

// the "document" is really just a BSON type object
let _doc = doc;
if (typeof _doc.toExtendedJSON !== 'function') {
Expand All @@ -315,15 +314,14 @@ function serializeDocument(doc, options) {
_doc = mapper(_doc);
}

// Two BSON types may have nested objects that may need to be serialized too
// Two BSON types may have nested objects that may need to be serialized too
if (bsontype === 'Code' && _doc.scope) {
_doc = new Code(_doc.code, serializeValue(_doc.scope, options));
} else if (bsontype === 'DBRef' && _doc.oid) {
_doc = new DBRef(_doc.collection, serializeValue(_doc.oid, options), _doc.db, _doc.fields);
}

return _doc.toExtendedJSON(options);

} else {
throw new Error('_bsontype must be a string, but was: ' + typeof bsontype);
}
Expand Down
6 changes: 5 additions & 1 deletion test/node/bson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2351,7 +2351,11 @@ describe('BSON', function() {
});

// Object
const record = { oldBsonType: new ObjectIdv400(), reallyOldBsonType: new OldObjectID, newBsonType: new ObjectId() };
const record = {
oldBsonType: new ObjectIdv400(),
reallyOldBsonType: new OldObjectID(),
newBsonType: new ObjectId()
};
const deserializedObject = BSON.deserialize(BSON.serialize(record));
expect(deserializedObject).to.have.keys(['oldBsonType', 'reallyOldBsonType', 'newBsonType']);
expect(record.oldBsonType.toString()).to.equal(deserializedObject.oldBsonType.toString());
Expand Down
46 changes: 33 additions & 13 deletions test/node/extended_json_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ describe('Extended JSON', function() {
it.skip('skipping 4.x/1.x interop tests', () => {});
} else {
it('should interoperate 4.x with 1.x versions of this library', function() {

const buffer = Buffer.alloc(64);
for (var i = 0; i < buffer.length; i++) {
buffer[i] = i;
Expand Down Expand Up @@ -363,10 +362,11 @@ describe('Extended JSON', function() {
oldObjectOldSerializer: OldBSON.serialize(oldBsonObject, serializationOptions),
oldObjectNewSerializer: BSON.serialize(oldBsonObject, serializationOptions),
newObjectOldSerializer: OldBSON.serialize(newBsonObject, serializationOptions),
newObjectNewSerializer: BSON.serialize(newBsonObject, serializationOptions),
newObjectNewSerializer: BSON.serialize(newBsonObject, serializationOptions)
};

const expectedBufferBase64 = 'VgEAAAViaW5hcnkAQAAAAAAAAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/DWNvZGUADgAAAGZ1bmN0aW9uKCkge30AA2RiUmVmACwAAAACJHJlZgAGAAAAdGVzdHMAECRpZAABAAAAAiRkYgAFAAAAdGVzdAAAE2RlY2ltYWwxMjgA//837RjxE6AdAgAAAABAMAFkb3VibGUAMzMzMzMzJEAQaW50MzIACgAAABJsb25nAP//38RiSvoQf21heEtleQAHb2JqZWN0SWQAERERERERERERERERB29iamVjdElEABEREREREREREREREQtic29uUmVnRXhwAGhlbGxvIHdvcmxkAGkADnN5bWJvbAAHAAAAc3ltYm9sABF0aW1lc3RhbXAAAAAAAAAAAAAA';
const expectedBufferBase64 =
'VgEAAAViaW5hcnkAQAAAAAAAAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/DWNvZGUADgAAAGZ1bmN0aW9uKCkge30AA2RiUmVmACwAAAACJHJlZgAGAAAAdGVzdHMAECRpZAABAAAAAiRkYgAFAAAAdGVzdAAAE2RlY2ltYWwxMjgA//837RjxE6AdAgAAAABAMAFkb3VibGUAMzMzMzMzJEAQaW50MzIACgAAABJsb25nAP//38RiSvoQf21heEtleQAHb2JqZWN0SWQAERERERERERERERERB29iamVjdElEABEREREREREREREREQtic29uUmVnRXhwAGhlbGxvIHdvcmxkAGkADnN5bWJvbAAHAAAAc3ltYm9sABF0aW1lc3RhbXAAAAAAAAAAAAAA';
const expectedBuffer = Buffer.from(expectedBufferBase64, 'base64');

// Regardless of which library version created the objects, and which library version
Expand All @@ -383,20 +383,28 @@ describe('Extended JSON', function() {
const deserializationOptions = { promoteValues: false };
const deserialized = {
usingOldDeserializer: OldBSON.deserialize(expectedBuffer, deserializationOptions),
usingNewDeserializer: BSON.deserialize(expectedBuffer, deserializationOptions),
usingNewDeserializer: BSON.deserialize(expectedBuffer, deserializationOptions)
};
// Apparently the Symbol BSON type was deprecated in V4. Symbols in BSON are deserialized as strings in V4
// Therefore, for this type we know there will be a difference between the V1 library and the V4 library,
// so remove Symbol from the list of BSON types that are being compared.
// Browser tests currently don't handle BSON Symbol correctly, so only test this under Node where OldBSON !=== BSON module.
if (BSON !== OldBSON) {
expect(deserialized.usingOldDeserializer['symbol'].value).to.equal(deserialized.usingNewDeserializer['symbol']);
expect(deserialized.usingOldDeserializer['symbol'].value).to.equal(
deserialized.usingNewDeserializer['symbol']
);
}
delete deserialized.usingOldDeserializer['symbol'];
delete deserialized.usingNewDeserializer['symbol'];

const ejsonExpected = {
binary: { $binary: { base64: 'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+Pw==', subType: '00' } },
binary: {
$binary: {
base64:
'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+Pw==',
subType: '00'
}
},
code: { $code: 'function() {}' },
dbRef: { $ref: 'tests', $id: { $numberInt: '1' }, $db: 'test' },
decimal128: { $numberDecimal: '9991223372036854775807' },
Expand All @@ -412,9 +420,15 @@ describe('Extended JSON', function() {
timestamp: { $timestamp: { t: 0, i: 0 } }
};
const ejsonSerializationOptions = { relaxed: false };
const resultOld = EJSON.serialize(deserialized.usingOldDeserializer, ejsonSerializationOptions);
const resultOld = EJSON.serialize(
deserialized.usingOldDeserializer,
ejsonSerializationOptions
);
expect(resultOld).to.deep.equal(ejsonExpected);
const resultNew = EJSON.serialize(deserialized.usingNewDeserializer, ejsonSerializationOptions);
const resultNew = EJSON.serialize(
deserialized.usingNewDeserializer,
ejsonSerializationOptions
);
expect(resultNew).to.deep.equal(ejsonExpected);
});

Expand Down Expand Up @@ -444,7 +458,7 @@ describe('Extended JSON', function() {
oldObjectOldSerializer: OldBSON.serialize(oldMinKey, serializationOptions),
oldObjectNewSerializer: BSON.serialize(oldMinKey, serializationOptions),
newObjectOldSerializer: OldBSON.serialize(newMinKey, serializationOptions),
newObjectNewSerializer: BSON.serialize(newMinKey, serializationOptions),
newObjectNewSerializer: BSON.serialize(newMinKey, serializationOptions)
};

expect(expectedBufferMinKey).to.deep.equal(bsonBuffersMinKey.newObjectNewSerializer);
Expand All @@ -453,17 +467,23 @@ describe('Extended JSON', function() {
expect(expectedBufferMinKey).to.deep.equal(bsonBuffersMinKey.oldObjectOldSerializer);

const ejsonExpected = {
minKey: { $minKey: 1 },
minKey: { $minKey: 1 }
};

const deserialized = {
usingOldDeserializer: OldBSON.deserialize(expectedBufferMinKey, deserializationOptions),
usingNewDeserializer: BSON.deserialize(expectedBufferMinKey, deserializationOptions),
usingNewDeserializer: BSON.deserialize(expectedBufferMinKey, deserializationOptions)
};
const ejsonSerializationOptions = { relaxed: false };
const resultOld = EJSON.serialize(deserialized.usingOldDeserializer, ejsonSerializationOptions);
const resultOld = EJSON.serialize(
deserialized.usingOldDeserializer,
ejsonSerializationOptions
);
expect(resultOld).to.deep.equal(ejsonExpected);
const resultNew = EJSON.serialize(deserialized.usingNewDeserializer, ejsonSerializationOptions);
const resultNew = EJSON.serialize(
deserialized.usingNewDeserializer,
ejsonSerializationOptions
);
expect(resultNew).to.deep.equal(ejsonExpected);
});
}
Expand Down

0 comments on commit 53ef8ad

Please sign in to comment.