Skip to content

Commit

Permalink
fix: type issues with SerializeOptions and Long methods accepting Tim…
Browse files Browse the repository at this point in the history
…estamp

NODE-2724
  • Loading branch information
Thomas Reggi authored Sep 18, 2020
1 parent ae9ae2d commit c18ba71
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 44 deletions.
17 changes: 10 additions & 7 deletions src/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ import { MinKey } from './min_key';
import { ObjectId } from './objectid';
import { calculateObjectSize as internalCalculateObjectSize } from './parser/calculate_size';
// Parts of the parser
import { DeserializationOptions, deserialize as internalDeserialize } from './parser/deserializer';
import { SerializationOptions, serializeInto as internalSerialize } from './parser/serializer';
import { DeserializeOptions, deserialize as internalDeserialize } from './parser/deserializer';
import { SerializeOptions, serializeInto as internalSerialize } from './parser/serializer';
import { BSONRegExp } from './regexp';
import { BSONSymbol } from './symbol';
import { Timestamp } from './timestamp';

export { SerializeOptions, DeserializeOptions };

export {
BSON_BINARY_SUBTYPE_BYTE_ARRAY,
BSON_BINARY_SUBTYPE_DEFAULT,
Expand Down Expand Up @@ -118,7 +121,7 @@ export function setInternalBufferSize(size: number): void {
* @param object - the Javascript object to serialize.
* @returns Buffer object containing the serialized object.
*/
export function serialize(object: Document, options: SerializationOptions = {}): Buffer {
export function serialize(object: Document, options: SerializeOptions = {}): Buffer {
// Unpack the options
const checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
const serializeFunctions =
Expand Down Expand Up @@ -166,7 +169,7 @@ export function serialize(object: Document, options: SerializationOptions = {}):
export function serializeWithBufferAndIndex(
object: Document,
finalBuffer: Buffer,
options: SerializationOptions = {}
options: SerializeOptions = {}
): number {
// Unpack the options
const checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
Expand Down Expand Up @@ -198,13 +201,13 @@ export function serializeWithBufferAndIndex(
* @param buffer - the buffer containing the serialized set of BSON documents.
* @returns returns the deserialized Javascript Object.
*/
export function deserialize(buffer: Buffer, options: DeserializationOptions = {}): Document {
export function deserialize(buffer: Buffer, options: DeserializeOptions = {}): Document {
buffer = ensureBuffer(buffer);
return internalDeserialize(buffer, options);
}

export type CalculateObjectSizeOptions = Pick<
SerializationOptions,
SerializeOptions,
'serializeFunctions' | 'ignoreUndefined'
>;

Expand Down Expand Up @@ -245,7 +248,7 @@ export function deserializeStream(
numberOfDocuments: number,
documents: Document[],
docStartIndex: number,
options: DeserializationOptions
options: DeserializeOptions
): number {
const internalOptions = Object.assign(
{ allowObjectSmallerThanBufferSize: true, index: 0 },
Expand Down
27 changes: 14 additions & 13 deletions src/long.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Timestamp } from './timestamp';
import type { EJSONOptions } from './extended_json';
import { isObjectLike } from './parser/utils';

Expand Down Expand Up @@ -297,7 +298,7 @@ export class Long {
}

/** Returns the sum of this and the specified Long. */
add(addend: string | number | Long): Long {
add(addend: string | number | Long | Timestamp): Long {
if (!Long.isLong(addend)) addend = Long.fromValue(addend);

// Divide each number into 4 chunks of 16 bits, and then sum the chunks.
Expand Down Expand Up @@ -334,7 +335,7 @@ export class Long {
* Returns the sum of this and the specified Long.
* @returns Sum
*/
and(other: string | number | Long): Long {
and(other: string | number | Long | Timestamp): Long {
if (!Long.isLong(other)) other = Long.fromValue(other);
return Long.fromBits(this.low & other.low, this.high & other.high, this.unsigned);
}
Expand All @@ -343,7 +344,7 @@ export class Long {
* Compares this Long's value with the specified's.
* @returns 0 if they are the same, 1 if the this is greater and -1 if the given one is greater
*/
compare(other: string | number | Long): 0 | 1 | -1 {
compare(other: string | number | Long | Timestamp): 0 | 1 | -1 {
if (!Long.isLong(other)) other = Long.fromValue(other);
if (this.eq(other)) return 0;
const thisNeg = this.isNegative(),
Expand All @@ -366,7 +367,7 @@ export class Long {
* Returns this Long divided by the specified. The result is signed if this Long is signed or unsigned if this Long is unsigned.
* @returns Quotient
*/
divide(divisor: string | number | Long): Long {
divide(divisor: string | number | Long | Timestamp): Long {
if (!Long.isLong(divisor)) divisor = Long.fromValue(divisor);
if (divisor.isZero()) throw Error('division by zero');

Expand Down Expand Up @@ -473,7 +474,7 @@ export class Long {
* Tests if this Long's value equals the specified's.
* @param other - Other value
*/
equals(other: string | number | Long): boolean {
equals(other: string | number | Long | Timestamp): boolean {
if (!Long.isLong(other)) other = Long.fromValue(other);
if (this.unsigned !== other.unsigned && this.high >>> 31 === 1 && other.high >>> 31 === 1)
return false;
Expand Down Expand Up @@ -516,15 +517,15 @@ export class Long {
}

/** Tests if this Long's value is greater than the specified's. */
greaterThan(other: string | number | Long): boolean {
greaterThan(other: string | number | Long | Timestamp): boolean {
return this.comp(other) > 0;
}

/** This is an alias of {@link Long.greaterThan} */
gt = Long.prototype.greaterThan;

/** Tests if this Long's value is greater than or equal the specified's. */
greaterThanOrEqual(other: string | number | Long): boolean {
greaterThanOrEqual(other: string | number | Long | Timestamp): boolean {
return this.comp(other) >= 0;
}

Expand Down Expand Up @@ -559,23 +560,23 @@ export class Long {
}

/** Tests if this Long's value is less than the specified's. */
lessThan(other: string | number | Long): boolean {
lessThan(other: string | number | Long | Timestamp): boolean {
return this.comp(other) < 0;
}

/** This is an alias of {@link Long#lessThan}. */
lt = Long.prototype.lessThan;

/** Tests if this Long's value is less than or equal the specified's. */
lessThanOrEqual(other: string | number | Long): boolean {
lessThanOrEqual(other: string | number | Long | Timestamp): boolean {
return this.comp(other) <= 0;
}

/** This is an alias of {@link Long.lessThanOrEqual} */
lte = Long.prototype.lessThanOrEqual;

/** Returns this Long modulo the specified. */
modulo(divisor: string | number | Long): Long {
modulo(divisor: string | number | Long | Timestamp): Long {
if (!Long.isLong(divisor)) divisor = Long.fromValue(divisor);

// use wasm support if present
Expand All @@ -602,7 +603,7 @@ export class Long {
* @param multiplier - Multiplier
* @returns Product
*/
multiply(multiplier: string | number | Long): Long {
multiply(multiplier: string | number | Long | Timestamp): Long {
if (this.isZero()) return Long.ZERO;
if (!Long.isLong(multiplier)) multiplier = Long.fromValue(multiplier);

Expand Down Expand Up @@ -683,7 +684,7 @@ export class Long {
}

/** Tests if this Long's value differs from the specified's. */
notEquals(other: string | number | Long): boolean {
notEquals(other: string | number | Long | Timestamp): boolean {
return !this.equals(other);
}

Expand Down Expand Up @@ -773,7 +774,7 @@ export class Long {
* @param subtrahend - Subtrahend
* @returns Difference
*/
subtract(subtrahend: string | number | Long): Long {
subtract(subtrahend: string | number | Long | Timestamp): Long {
if (!Long.isLong(subtrahend)) subtrahend = Long.fromValue(subtrahend);
return this.add(subtrahend.neg());
}
Expand Down
10 changes: 5 additions & 5 deletions src/parser/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { BSONSymbol } from '../symbol';
import { Timestamp } from '../timestamp';
import { validateUtf8 } from '../validate_utf8';

export interface DeserializationOptions {
export interface DeserializeOptions {
/** evaluate functions in the BSON document scoped to the object deserialized. */
evalFunctions?: boolean;
/** cache evaluated functions for reuse. */
Expand Down Expand Up @@ -49,7 +49,7 @@ const functionCache: { [hash: string]: Function } = {};

export function deserialize(
buffer: Buffer,
options: DeserializationOptions,
options: DeserializeOptions,
isArray?: boolean
): Document {
options = options == null ? {} : options;
Expand Down Expand Up @@ -93,7 +93,7 @@ export function deserialize(
function deserializeObject(
buffer: Buffer,
index: number,
options: DeserializationOptions,
options: DeserializeOptions,
isArray = false
) {
const evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
Expand Down Expand Up @@ -253,8 +253,8 @@ function deserializeObject(
arrayOptions = {};
for (const n in options) {
(arrayOptions as {
[key: string]: DeserializationOptions[keyof DeserializationOptions];
})[n] = options[n as keyof DeserializationOptions];
[key: string]: DeserializeOptions[keyof DeserializeOptions];
})[n] = options[n as keyof DeserializeOptions];
}
arrayOptions['raw'] = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { ObjectId } from '../objectid';
import type { BSONRegExp } from '../regexp';
import { isDate, normalizedFunctionString } from './utils';

export interface SerializationOptions {
export interface SerializeOptions {
/** the serializer will check if keys are valid. */
checkKeys?: boolean;
/** serialize the javascript functions **(default:false)**. */
Expand Down
24 changes: 6 additions & 18 deletions test/node/extended_json_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,16 +427,10 @@ describe('Extended JSON', function () {
// symbol: { $symbol: 'symbol' }, // removed because this type is deprecated. See comment above.
timestamp: { $timestamp: { t: 0, i: 0 } }
};
const ejsonSerializationOptions = { relaxed: false };
const resultOld = EJSON.serialize(
deserialized.usingOldDeserializer,
ejsonSerializationOptions
);
const ejsonSerializeOptions = { relaxed: false };
const resultOld = EJSON.serialize(deserialized.usingOldDeserializer, ejsonSerializeOptions);
expect(resultOld).to.deep.equal(ejsonExpected);
const resultNew = EJSON.serialize(
deserialized.usingNewDeserializer,
ejsonSerializationOptions
);
const resultNew = EJSON.serialize(deserialized.usingNewDeserializer, ejsonSerializeOptions);
expect(resultNew).to.deep.equal(ejsonExpected);
});

Expand Down Expand Up @@ -482,16 +476,10 @@ describe('Extended JSON', function () {
usingOldDeserializer: OldBSON.deserialize(expectedBufferMinKey, deserializationOptions),
usingNewDeserializer: BSON.deserialize(expectedBufferMinKey, deserializationOptions)
};
const ejsonSerializationOptions = { relaxed: false };
const resultOld = EJSON.serialize(
deserialized.usingOldDeserializer,
ejsonSerializationOptions
);
const ejsonSerializeOptions = { relaxed: false };
const resultOld = EJSON.serialize(deserialized.usingOldDeserializer, ejsonSerializeOptions);
expect(resultOld).to.deep.equal(ejsonExpected);
const resultNew = EJSON.serialize(
deserialized.usingNewDeserializer,
ejsonSerializationOptions
);
const resultNew = EJSON.serialize(deserialized.usingNewDeserializer, ejsonSerializeOptions);
expect(resultNew).to.deep.equal(ejsonExpected);
});
}
Expand Down

0 comments on commit c18ba71

Please sign in to comment.