@@ -162,10 +162,18 @@ type SemanticTypeMap = {
162
162
[ typeName : string ] : SemanticTypeFunction | boolean ;
163
163
} ;
164
164
165
- export type SchemaParseOptions = {
166
- semanticTypes ? : boolean | SemanticTypeMap ;
167
- storeValues ? : boolean ;
165
+ type AllSchemaParseOptions = {
166
+ semanticTypes : boolean | SemanticTypeMap ;
167
+ storeValues : boolean ;
168
168
signal ?: AbortSignal ;
169
+ storedValuesLengthLimit : number ;
170
+ } ;
171
+ export type SchemaParseOptions = Partial < AllSchemaParseOptions > ;
172
+
173
+ const defaultSchemaParseOptions : AllSchemaParseOptions = {
174
+ semanticTypes : false ,
175
+ storeValues : true ,
176
+ storedValuesLengthLimit : 10000
169
177
} ;
170
178
171
179
/**
@@ -331,25 +339,25 @@ function simplifiedSchema(fields: SchemaAnalysisFieldsMap): SimplifiedSchema {
331
339
332
340
function cropString ( value : string , limit : number ) {
333
341
if ( limit < 1 ) return '' ;
334
- return value . charCodeAt ( limit - 1 ) === value . codePointAt ( 10000 - 1 )
342
+ return value . charCodeAt ( limit - 1 ) === value . codePointAt ( limit - 1 )
335
343
? value . slice ( 0 , limit )
336
344
: value . slice ( 0 , limit - 1 ) ;
337
345
}
338
346
339
- function getCappedValue ( bsonType : SchemaBSONType , value : BSONValue ) {
347
+ function getCappedValue ( bsonType : SchemaBSONType , value : BSONValue , limit : number ) {
340
348
if ( bsonType === 'String' ) {
341
- return cropString ( value as string , 10000 ) ;
349
+ return cropString ( value as string , limit ) ;
342
350
}
343
351
if ( bsonType === 'Binary' ) {
344
352
value = value as Binary ;
345
- return value . buffer . length > 10000
346
- ? new Binary ( value . buffer . slice ( 0 , 10000 ) , value . sub_type )
353
+ return value . buffer . length > limit
354
+ ? new Binary ( value . buffer . slice ( 0 , limit ) , value . sub_type )
347
355
: value ;
348
356
}
349
357
if ( bsonType === 'Code' ) {
350
358
value = value as Code ;
351
- return ( value . code . length >= 10000 )
352
- ? new Code ( cropString ( value . code , 10000 ) , value . scope )
359
+ return ( value . code . length >= limit )
360
+ ? new Code ( cropString ( value . code , limit ) , value . scope )
353
361
: value ;
354
362
}
355
363
return value ;
@@ -459,7 +467,7 @@ function finalizeSchema(schemaAnalysis: SchemaAnalysisRoot): SchemaField[] {
459
467
460
468
export class SchemaAnalyzer {
461
469
semanticTypes : SemanticTypeMap ;
462
- options : SchemaParseOptions ;
470
+ options : AllSchemaParseOptions ;
463
471
documentsAnalyzed = 0 ;
464
472
schemaAnalysisRoot : SchemaAnalysisRoot = {
465
473
fields : Object . create ( null ) ,
@@ -474,7 +482,7 @@ export class SchemaAnalyzer {
474
482
475
483
constructor ( options ?: SchemaParseOptions ) {
476
484
// Set default options.
477
- this . options = { semanticTypes : false , storeValues : true , ...options } ;
485
+ this . options = { ... defaultSchemaParseOptions , ...options } ;
478
486
479
487
this . semanticTypes = {
480
488
...semanticTypes
@@ -555,7 +563,7 @@ export class SchemaAnalyzer {
555
563
}
556
564
557
565
type . values . pushSome (
558
- getCappedValue ( type . bsonType , value )
566
+ getCappedValue ( type . bsonType , value , this . options . storedValuesLengthLimit )
559
567
) ;
560
568
}
561
569
} ;
0 commit comments