@@ -4,6 +4,7 @@ import { Code } from './code';
4
4
import { DBRef , isDBRefLike } from './db_ref' ;
5
5
import { Decimal128 } from './decimal128' ;
6
6
import { Double } from './double' ;
7
+ import { BSONError , BSONTypeError } from './error' ;
7
8
import { Int32 } from './int_32' ;
8
9
import { Long } from './long' ;
9
10
import { MaxKey } from './max_key' ;
@@ -185,7 +186,7 @@ function serializeValue(value: any, options: EJSONSerializeOptions): any {
185
186
circularPart . length + ( alreadySeen . length + current . length ) / 2 - 1
186
187
) ;
187
188
188
- throw new TypeError (
189
+ throw new BSONTypeError (
189
190
'Converting circular structure to EJSON:\n' +
190
191
` ${ leadingPart } ${ alreadySeen } ${ circularPart } ${ current } \n` +
191
192
` ${ leadingSpace } \\${ dashes } /`
@@ -274,7 +275,7 @@ const BSON_TYPE_MAPPINGS = {
274
275
275
276
// eslint-disable-next-line @typescript-eslint/no-explicit-any
276
277
function serializeDocument ( doc : any , options : EJSONSerializeOptions ) {
277
- if ( doc == null || typeof doc !== 'object' ) throw new Error ( 'not an object instance' ) ;
278
+ if ( doc == null || typeof doc !== 'object' ) throw new BSONError ( 'not an object instance' ) ;
278
279
279
280
const bsontype : BSONType [ '_bsontype' ] = doc . _bsontype ;
280
281
if ( typeof bsontype === 'undefined' ) {
@@ -300,7 +301,7 @@ function serializeDocument(doc: any, options: EJSONSerializeOptions) {
300
301
// Copy the object into this library's version of that type.
301
302
const mapper = BSON_TYPE_MAPPINGS [ doc . _bsontype ] ;
302
303
if ( ! mapper ) {
303
- throw new TypeError ( 'Unrecognized or invalid _bsontype: ' + doc . _bsontype ) ;
304
+ throw new BSONTypeError ( 'Unrecognized or invalid _bsontype: ' + doc . _bsontype ) ;
304
305
}
305
306
outDoc = mapper ( outDoc ) ;
306
307
}
@@ -319,7 +320,7 @@ function serializeDocument(doc: any, options: EJSONSerializeOptions) {
319
320
320
321
return outDoc . toExtendedJSON ( options ) ;
321
322
} else {
322
- throw new Error ( '_bsontype must be a string, but was: ' + typeof bsontype ) ;
323
+ throw new BSONError ( '_bsontype must be a string, but was: ' + typeof bsontype ) ;
323
324
}
324
325
}
325
326
@@ -366,7 +367,14 @@ export namespace EJSON {
366
367
if ( typeof finalOptions . relaxed === 'boolean' ) finalOptions . strict = ! finalOptions . relaxed ;
367
368
if ( typeof finalOptions . strict === 'boolean' ) finalOptions . relaxed = ! finalOptions . strict ;
368
369
369
- return JSON . parse ( text , ( _key , value ) => deserializeValue ( value , finalOptions ) ) ;
370
+ return JSON . parse ( text , ( key , value ) => {
371
+ if ( key . indexOf ( '\x00' ) !== - 1 ) {
372
+ throw new BSONError (
373
+ `BSON Document field names cannot contain null bytes, found: ${ JSON . stringify ( key ) } `
374
+ ) ;
375
+ }
376
+ return deserializeValue ( value , finalOptions ) ;
377
+ } ) ;
370
378
}
371
379
372
380
export type JSONPrimitive = string | number | boolean | null ;
0 commit comments