@@ -10,12 +10,19 @@ function isPrimitveType(obj: any) {
1010
1111export class Redactor {
1212 constructor ( private fieldNames : string [ ] = [ ] ) { }
13-
1413 addFieldName ( fieldName : string ) {
1514 this . fieldNames . push ( fieldName ) ;
1615 }
1716
18- sanitize ( obj ?: any , dropFields : string [ ] = [ ] ) : any {
17+ sanitize (
18+ obj ?: any ,
19+ dropFields : string [ ] = [ ] ,
20+ seen : Set < any > = new Set ( )
21+ ) : any {
22+ if ( seen . has ( obj ) ) {
23+ return `circular ref` ;
24+ }
25+ seen . add ( obj ) ;
1926 if ( obj === undefined ) {
2027 return undefined ;
2128 }
@@ -24,24 +31,25 @@ export class Redactor {
2431 return obj ;
2532 }
2633 if ( Array . isArray ( obj ) ) {
27- return obj . map ( ( e ) => this . sanitize ( e , dropFields ) ) ;
34+ return obj . map ( ( e ) => this . sanitize ( e , dropFields , seen ) ) ;
2835 }
36+
2937 //make a copy of the object
30- obj = Object . assign ( { } , obj ) ;
31- for ( const key in obj ) {
38+ const copyObj = Object . assign ( { } , obj ) ;
39+ for ( const key in copyObj ) {
3240 if ( dropFields . includes ( key ) ) {
33- delete obj [ key ] ;
41+ delete copyObj [ key ] ;
3442 } else if (
3543 isPrimitveType ( obj [ key ] ) &&
3644 this . fieldNames . includes ( key )
3745 ) {
38- obj [ key ] = "***REDACTED***" ;
46+ copyObj [ key ] = "***REDACTED***" ;
3947 } else {
40- obj [ key ] = this . sanitize ( obj [ key ] , dropFields ) ;
48+ copyObj [ key ] = this . sanitize ( obj [ key ] , dropFields , seen ) ;
4149 }
4250 }
4351
44- return obj ;
52+ return copyObj ;
4553 }
4654}
4755
0 commit comments