Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export {
Decimal128
};
export { BSONValue } from './bson_value';
export { BSONError, BSONVersionError } from './error';
export { BSONError, BSONVersionError, BSONRuntimeError } from './error';
export { BSONType } from './constants';
export { EJSON } from './extended_json';

Expand Down
11 changes: 11 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@ export class BSONVersionError extends BSONError {
);
}
}

/** @public */
Comment thread
nbbeeken marked this conversation as resolved.
Outdated
export class BSONRuntimeError extends BSONError {
Comment thread
nbbeeken marked this conversation as resolved.
get name(): 'BSONRuntimeError' {
return 'BSONRuntimeError';
}

constructor(message: string) {
super(message);
}
}
6 changes: 5 additions & 1 deletion src/extended_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { DBRef, isDBRefLike } from './db_ref';
import { Decimal128 } from './decimal128';
import { Double } from './double';
import { BSONError, BSONVersionError } from './error';
import { BSONError, BSONRuntimeError, BSONVersionError } from './error';
import { Int32 } from './int_32';
import { Long } from './long';
import { MaxKey } from './max_key';
Expand Down Expand Up @@ -125,10 +125,14 @@ function deserializeValue(value: any, options: EJSONOptions = {}) {
if (options.legacy) {
if (typeof d === 'number') date.setTime(d);
else if (typeof d === 'string') date.setTime(Date.parse(d));
else if (typeof d === 'bigint') date.setTime(Number(d));
else throw new BSONRuntimeError('Unrecognized type for EJSON date');
Comment thread
nbbeeken marked this conversation as resolved.
Outdated
} else {
if (typeof d === 'string') date.setTime(Date.parse(d));
else if (Long.isLong(d)) date.setTime(d.toNumber());
else if (typeof d === 'number' && options.relaxed) date.setTime(d);
else if (typeof d === 'bigint') date.setTime(Number(d));
else throw new BSONRuntimeError('Unrecognized type for EJSON date');
Comment thread
nbbeeken marked this conversation as resolved.
Outdated
}
return date;
}
Expand Down
25 changes: 24 additions & 1 deletion test/node/extended_json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as BSON from '../register-bson';
const EJSON = BSON.EJSON;
import * as vm from 'node:vm';
import { expect } from 'chai';
import { BSONVersionError } from '../../src';
import { BSONVersionError, BSONRuntimeError } from '../../src';

// BSON types
const Binary = BSON.Binary;
Expand Down Expand Up @@ -440,6 +440,29 @@ describe('Extended JSON', function () {
expect(bson).to.deep.equal(doc);
});
});

context('when using useBigInt64=true', function () {
it('parses $date.$numberLong with millis since epoch', function () {
if (BSON.__noBigInt__) {
this.skip();
}
const date = new Date(1676315495987);
const doc = { field: date };
const stringified = EJSON.stringify(doc, { relaxed: false });
const parsedDoc = EJSON.parse(stringified, { useBigInt64: true, relaxed: false });
expect(parsedDoc).to.deep.equal(doc);
});
});

context('when deserializing object with invalid $date key', function () {
it('throws a BSONRuntimeError', function () {
const doc = { field: { $date: new ArrayBuffer(10) } };
const s = EJSON.stringify(doc, { relaxed: false });
expect(() => {
EJSON.parse(s, { relaxed: false });
}).to.throw(BSONRuntimeError, /Unrecognized type/i);
});
});
});

context('when deserializing regex', function () {
Expand Down