Skip to content

Commit 1be51f5

Browse files
committed
feat(NODE-4892)!: error on bson types not from this version
1 parent 95d5edf commit 1be51f5

24 files changed

+292
-606
lines changed

src/binary.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export class Binary {
3131
get _bsontype(): 'Binary' {
3232
return 'Binary';
3333
}
34+
/** @internal */
35+
get [Symbol.for('@@mdb.bson.version')](): 5 {
36+
return 5;
37+
}
3438

3539
/**
3640
* Binary default subtype
@@ -305,6 +309,11 @@ const UUID_BYTE_LENGTH = 16;
305309
* @public
306310
*/
307311
export class UUID extends Binary {
312+
/** @internal */
313+
get [Symbol.for('@@mdb.bson.version')](): 5 {
314+
return 5;
315+
}
316+
308317
static cacheHexString: boolean;
309318

310319
/** UUID hexString cache @internal */

src/code.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ export class Code {
1515
get _bsontype(): 'Code' {
1616
return 'Code';
1717
}
18+
/** @internal */
19+
get [Symbol.for('@@mdb.bson.version')](): 5 {
20+
return 5;
21+
}
1822

1923
code: string;
2024

src/db_ref.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export class DBRef {
3232
get _bsontype(): 'DBRef' {
3333
return 'DBRef';
3434
}
35+
/** @internal */
36+
get [Symbol.for('@@mdb.bson.version')](): 5 {
37+
return 5;
38+
}
3539

3640
collection!: string;
3741
oid!: ObjectId;

src/decimal128.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ export class Decimal128 {
130130
get _bsontype(): 'Decimal128' {
131131
return 'Decimal128';
132132
}
133+
/** @internal */
134+
get [Symbol.for('@@mdb.bson.version')](): 5 {
135+
return 5;
136+
}
133137

134138
readonly bytes!: Uint8Array;
135139

src/double.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export class Double {
1414
get _bsontype(): 'Double' {
1515
return 'Double';
1616
}
17+
/** @internal */
18+
get [Symbol.for('@@mdb.bson.version')](): 5 {
19+
return 5;
20+
}
1721

1822
value!: number;
1923
/**

src/extended_json.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,9 @@ const BSON_TYPE_MAPPINGS = {
275275
),
276276
MaxKey: () => new MaxKey(),
277277
MinKey: () => new MinKey(),
278-
ObjectID: (o: ObjectId) => new ObjectId(o),
279-
// The _bsontype for ObjectId is spelled with a capital "D", to the mapping above will be used (most of the time)
280-
// specifically BSON versions 4.0.0 and 4.0.1 the _bsontype was changed to "ObjectId" so we keep this mapping to support
281-
// those version of BSON
282278
ObjectId: (o: ObjectId) => new ObjectId(o),
283279
BSONRegExp: (o: BSONRegExp) => new BSONRegExp(o.pattern, o.options),
284-
Symbol: (o: BSONSymbol) => new BSONSymbol(o.value),
280+
BSONSymbol: (o: BSONSymbol) => new BSONSymbol(o.value),
285281
Timestamp: (o: Timestamp) => Timestamp.fromBits(o.low, o.high)
286282
} as const;
287283

@@ -312,6 +308,13 @@ function serializeDocument(doc: any, options: EJSONSerializeOptions) {
312308
}
313309
}
314310
return _doc;
311+
} else if (
312+
doc != null &&
313+
typeof doc === 'object' &&
314+
typeof doc._bsontype === 'string' &&
315+
doc[Symbol.for('@@mdb.bson.version')] == null
316+
) {
317+
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
315318
} else if (isBSONType(doc)) {
316319
// the "document" is really just a BSON type object
317320
// eslint-disable-next-line @typescript-eslint/no-explicit-any

src/int_32.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ export class Int32 {
1414
get _bsontype(): 'Int32' {
1515
return 'Int32';
1616
}
17+
/** @internal */
18+
get [Symbol.for('@@mdb.bson.version')](): 5 {
19+
return 5;
20+
}
1721

1822
value!: number;
1923
/**

src/long.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ export class Long {
102102
get _bsontype(): 'Long' {
103103
return 'Long';
104104
}
105+
/** @internal */
106+
get [Symbol.for('@@mdb.bson.version')](): 5 {
107+
return 5;
108+
}
105109

106110
/** An indicator used to reliably determine if an object is a Long or not. */
107111
get __isLong__(): boolean {

src/max_key.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export class MaxKey {
1212
get _bsontype(): 'MaxKey' {
1313
return 'MaxKey';
1414
}
15+
/** @internal */
16+
get [Symbol.for('@@mdb.bson.version')](): 5 {
17+
return 5;
18+
}
1519

1620
/** @internal */
1721
toExtendedJSON(): MaxKeyExtended {

src/min_key.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export class MinKey {
1212
get _bsontype(): 'MinKey' {
1313
return 'MinKey';
1414
}
15+
/** @internal */
16+
get [Symbol.for('@@mdb.bson.version')](): 5 {
17+
return 5;
18+
}
1519

1620
/** @internal */
1721
toExtendedJSON(): MinKeyExtended {

0 commit comments

Comments
 (0)