11const querystring = require ( 'querystring' ) ;
2- const { sha256 , accessNestedObjectValueByStringPath } = require ( '../pxutil' ) ;
2+ const { accessNestedObjectValueByStringPath } = require ( '../pxutil' ) ;
33
4- const SENT_THROUGH = { BODY : 'body' , QUERY_PARAM : 'query-param' , HEADER : 'header' } ;
5- const ENCODING_TYPE = { URL_ENCODE : 'url-encode ' , CLEAR_TEXT : 'clear-text ' , BASE64 : 'base64' , CUSTOM : 'custom ' } ;
4+ const SentThrough = { BODY : 'body' , QUERY_PARAM : 'query-param' , HEADER : 'header' } ;
5+ const ContentType = { JSON : 'application/json ' , URL_ENCODED : 'application/x-www-form-urlencoded ' , MULTIPART_FORM : 'multipart/form-data ' } ;
66
77class FieldExtractor {
8- constructor ( sentThrough , contentType , encoding , fieldsToExtract ) {
8+ constructor ( sentThrough , fieldsToExtract ) {
99 this . containerName = this . _getContainerName ( sentThrough ) ;
10- this . contentType = contentType ;
11- this . _decode = this . _getDecodeFunction ( encoding ) ;
1210 this . fieldsToExtract = fieldsToExtract instanceof Array ? fieldsToExtract : [ fieldsToExtract ] ;
1311 }
1412
@@ -19,37 +17,32 @@ class FieldExtractor {
1917 }
2018 const fields = { } ;
2119 this . fieldsToExtract . forEach ( ( desiredField ) => {
22- const value = accessNestedObjectValueByStringPath ( desiredField . origRequestFieldName , container ) ;
20+ let value = accessNestedObjectValueByStringPath ( desiredField . oldFieldName , container ) || container [ desiredField . oldFieldName ] ;
2321 if ( ! value || typeof value !== 'string' ) {
2422 return ;
2523 }
26- fields [ desiredField . resultActivityFieldName ] = sha256 ( value ) ;
24+ value = desiredField . shouldNormalize ? this . _normalizeField ( value ) : value ;
25+ fields [ desiredField . newFieldName ] = value ;
2726 } ) ;
2827 return fields ;
2928 }
3029
30+ _normalizeField ( fieldValue ) {
31+ return fieldValue . toLowerCase ( ) ;
32+ }
33+
3134 _getContainerName ( sentThrough ) {
3235 switch ( sentThrough ) {
33- case SENT_THROUGH . QUERY_PARAM :
36+ case SentThrough . QUERY_PARAM :
3437 return 'query' ;
35- case SENT_THROUGH . HEADER :
38+ case SentThrough . HEADER :
3639 return 'headers' ;
37- case SENT_THROUGH . BODY :
40+ case SentThrough . BODY :
3841 default :
3942 return 'body' ;
4043 }
4144 }
4245
43- _getDecodeFunction ( encoding ) {
44- switch ( encoding ) {
45- case ENCODING_TYPE . URL_ENCODE :
46- return ( string ) => querystring . parse ( string ) ;
47- case ENCODING_TYPE . CLEAR_TEXT :
48- default :
49- return ( string ) => JSON . parse ( string ) ;
50- }
51- }
52-
5346 _getContainer ( request ) {
5447 const container = request [ this . containerName ] ;
5548 if ( ! container ) {
@@ -59,7 +52,14 @@ class FieldExtractor {
5952 if ( typeof container === 'object' ) {
6053 return container ;
6154 }
62- return this . _decode ( container ) ;
55+
56+ const contentType = request . headers [ 'content-type' ] ;
57+ if ( contentType . includes ( ContentType . JSON ) ) {
58+ return JSON . parse ( container ) ;
59+ } else if ( contentType . includes ( ContentType . URL_ENCODED ) ) {
60+ return querystring . parse ( container ) ;
61+ }
62+ return null ;
6363 }
6464}
6565
0 commit comments