@@ -475,7 +475,12 @@ export function stringifyWithMaxLen(
475475 currentLength += String ( value ) . length ;
476476 } else if ( typeof value === 'boolean' ) {
477477 currentLength += value ? 4 : 5 ;
478- } else if ( 'buffer' in value && isUint8Array ( value . buffer ) ) {
478+ } else if (
479+ value != null &&
480+ typeof value === 'object' &&
481+ 'buffer' in value &&
482+ isUint8Array ( value . buffer )
483+ ) {
479484 // Handle binData
480485 currentLength += ( value . buffer . byteLength + value . buffer . byteLength * 0.5 ) | 0 ;
481486 } else if ( value != null && typeof value === 'object' && '_bsontype' in value ) {
@@ -490,7 +495,7 @@ export function stringifyWithMaxLen(
490495 if ( v . scope == null ) {
491496 currentLength += v . code . length + 10 + 2 ;
492497 } else {
493- // Ignoring actual scope object
498+ // Ignoring actual scope object, so this undercounts
494499 currentLength += v . code . length + 10 + 11 ;
495500 }
496501 } else if ( v . _bsontype === 'Decimal128' ) {
@@ -531,6 +536,7 @@ export function stringifyWithMaxLen(
531536 ) {
532537 currentLength += 19 + String ( v . t ) . length + 5 + String ( v . i ) . length + 2 ;
533538 } else if ( v . _bsontype === 'DBRef' ) {
539+ // TODO: Handle fields property; currently undercounts
534540 // '{"$ref":"<collection>","$id":<stringified oid>}' or '{"$ref":"<collection>","$id":<stringified oid>,"$db":"test"}'
535541 currentLength += 9 ;
536542 // account for collection
@@ -547,6 +553,8 @@ export function stringifyWithMaxLen(
547553 if ( 'oid' in v ) {
548554 currentLength += 35 ;
549555 }
556+ } else {
557+ // Unknown BSON type, handle same as plain objects
550558 }
551559 }
552560
0 commit comments