@@ -50,15 +50,14 @@ enum TypeOrder {
5050 BSON_TIMESTAMP = 5 ,
5151 STRING = 6 ,
5252 BLOB = 7 ,
53- BSON_BINARY = 8 ,
54- REF = 9 ,
55- BSON_OBJECT_ID = 10 ,
56- GEO_POINT = 11 ,
57- REGEX = 12 ,
58- ARRAY = 13 ,
59- VECTOR = 14 ,
60- OBJECT = 15 ,
61- MAX_KEY = 16 ,
53+ REF = 8 ,
54+ BSON_OBJECT_ID = 9 ,
55+ GEO_POINT = 10 ,
56+ REGEX = 11 ,
57+ ARRAY = 12 ,
58+ VECTOR = 13 ,
59+ OBJECT = 14 ,
60+ MAX_KEY = 15 ,
6261}
6362
6463/*!
@@ -95,9 +94,8 @@ function typeOrder(val: api.IValue): TypeOrder {
9594 case 'bsonObjectIdValue' :
9695 return TypeOrder . BSON_OBJECT_ID ;
9796 case 'bytesValue' :
98- return TypeOrder . BLOB ;
9997 case 'bsonBinaryValue' :
100- return TypeOrder . BSON_BINARY ;
98+ return TypeOrder . BLOB ;
10199 case 'referenceValue' :
102100 return TypeOrder . REF ;
103101 case 'mapValue' :
@@ -274,26 +272,45 @@ function compareBsonTimestamps(left: api.IValue, right: api.IValue): number {
274272 * @private
275273 * @internal
276274 */
277- function compareBsonBinaryData ( left : api . IValue , right : api . IValue ) : number {
278- const leftBytes =
279- left . mapValue ! . fields ?. [ RESERVED_BSON_BINARY_KEY ] ?. bytesValue ;
280- const rightBytes =
281- right . mapValue ! . fields ?. [ RESERVED_BSON_BINARY_KEY ] ?. bytesValue ;
282- if ( ! rightBytes || ! leftBytes ) {
283- throw new Error ( 'Received incorrect bytesValue for BsonBinaryData' ) ;
275+ function getSubtype ( value : api . IValue ) : number {
276+ if ( value . bytesValue !== undefined ) {
277+ return 0 ;
284278 }
285- return Buffer . compare ( Buffer . from ( leftBytes ) , Buffer . from ( rightBytes ) ) ;
279+ const bsonBinaryFields = value . mapValue ?. fields ?. [ RESERVED_BSON_BINARY_KEY ] ;
280+ if ( bsonBinaryFields && bsonBinaryFields . bytesValue ) {
281+ return bsonBinaryFields . bytesValue [ 0 ] ;
282+ }
283+ throw new Error ( 'Cannot get subtype for non-blob value: ' + JSON . stringify ( value ) ) ;
284+ }
285+
286+ function getData ( value : api . IValue ) : Uint8Array {
287+ if ( value . bytesValue !== undefined ) {
288+ return value . bytesValue || new Uint8Array ( ) ;
289+ }
290+ const bsonBinaryFields = value . mapValue ?. fields ?. [ RESERVED_BSON_BINARY_KEY ] ;
291+ if ( bsonBinaryFields && bsonBinaryFields . bytesValue ) {
292+ return bsonBinaryFields . bytesValue . slice ( 1 ) ;
293+ }
294+ throw new Error ( 'Cannot get data for non-blob value: ' + JSON . stringify ( value ) ) ;
286295}
287296
288297/*!
289298 * @private
290299 * @internal
291300 */
292- function compareBlobs ( left : Uint8Array , right : Uint8Array ) : number {
293- if ( ! ( left instanceof Buffer ) || ! ( right instanceof Buffer ) ) {
301+ function compareBlobs ( left : api . IValue , right : api . IValue ) : number {
302+ if ( left . bytesValue !== undefined && ! ( left . bytesValue instanceof Buffer ) ) {
294303 throw new Error ( 'Blobs can only be compared if they are Buffers.' ) ;
295304 }
296- return Buffer . compare ( left , right ) ;
305+ if ( right . bytesValue !== undefined && ! ( right . bytesValue instanceof Buffer ) ) {
306+ throw new Error ( 'Blobs can only be compared if they are Buffers.' ) ;
307+ }
308+ const leftSubtype = getSubtype ( left ) ;
309+ const rightSubtype = getSubtype ( right ) ;
310+ if ( leftSubtype !== rightSubtype ) {
311+ return primitiveComparator ( leftSubtype , rightSubtype ) ;
312+ }
313+ return Buffer . compare ( Buffer . from ( getData ( left ) ) , Buffer . from ( getData ( right ) ) ) ;
297314}
298315
299316/*!
@@ -526,9 +543,7 @@ export function compare(left: api.IValue, right: api.IValue): number {
526543 case TypeOrder . BSON_TIMESTAMP :
527544 return compareBsonTimestamps ( left , right ) ;
528545 case TypeOrder . BLOB :
529- return compareBlobs ( left . bytesValue ! , right . bytesValue ! ) ;
530- case TypeOrder . BSON_BINARY :
531- return compareBsonBinaryData ( left , right ) ;
546+ return compareBlobs ( left , right ) ;
532547 case TypeOrder . REF :
533548 return compareReferenceProtos ( left , right ) ;
534549 case TypeOrder . GEO_POINT :
0 commit comments