File tree Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change @@ -56,8 +56,14 @@ export class ObjectId {
56
56
if ( typeof id === 'object' && id && 'id' in id ) {
57
57
if ( 'toHexString' in id && typeof id . toHexString === 'function' ) {
58
58
this [ kId ] = Buffer . from ( id . toHexString ( ) , 'hex' ) ;
59
+ } else if ( typeof id . id === 'string' ) {
60
+ this [ kId ] = Buffer . from ( id . id ) ;
59
61
} else {
60
- this [ kId ] = typeof id . id === 'string' ? Buffer . from ( id . id ) : id . id ;
62
+ try {
63
+ this [ kId ] = ensureBuffer ( id . id ) ;
64
+ } catch {
65
+ throw new BSONTypeError ( 'Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters' ) ;
66
+ }
61
67
}
62
68
} else if ( id == null || typeof id === 'number' ) {
63
69
// The most common use case (blank id, new objectId instance)
Original file line number Diff line number Diff line change @@ -48,6 +48,14 @@ describe('ObjectId', function () {
48
48
expect ( ( ) => new ObjectId ( objectIdLike ) ) . to . throw ( TypeError ) ;
49
49
} ) ;
50
50
51
+ it ( 'should throw error if object without string or buffer id is passed in' , function ( ) {
52
+ var objectIdLike = {
53
+ id : 5
54
+ } ;
55
+
56
+ expect ( ( ) => new ObjectId ( objectIdLike ) ) . to . throw ( TypeError ) ;
57
+ } ) ;
58
+
51
59
it ( 'should correctly create ObjectId from number' , function ( ) {
52
60
expect ( ( ) => new ObjectId ( 42 ) ) . to . not . throw ( ) ;
53
61
expect ( ( ) => new ObjectId ( 0x2a ) ) . to . not . throw ( ) ;
You can’t perform that action at this time.
0 commit comments