Skip to content

Commit eaa2418

Browse files
committed
Added better support for checking for exclusions and patterns.
1 parent ac556aa commit eaa2418

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

src/Utils.ts

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ export class Utils {
3232
return hash;
3333
}
3434

35-
public static getCookies(cookies:string): Object {
35+
public static getCookies(cookies:string, exclusions?:string[]): Object {
3636
let result:Object = {};
3737

3838
let parts:string[] = (cookies || '').split('; ');
3939
for (let index = 0; index < parts.length; index++) {
4040
let cookie:string[] = parts[index].split('=');
41-
result[cookie[0]] = cookie[1];
41+
if (!Utils.isMatch(cookie[0], exclusions)) {
42+
result[cookie[0]] = cookie[1];
43+
}
4244
}
4345

4446
return result;
@@ -84,7 +86,7 @@ export class Utils {
8486
return null;
8587
}
8688

87-
public static parseQueryString(query:string) {
89+
public static parseQueryString(query:string, exclusions?:string[]) {
8890
if (!query || query.length === 0) {
8991
return null;
9092
}
@@ -97,7 +99,9 @@ export class Utils {
9799
let result:Object = {};
98100
for (let index = 0; index < pairs.length; index++) {
99101
let pair = pairs[index].split('=');
100-
result[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
102+
if (!Utils.isMatch(pair[0], exclusions)) {
103+
result[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
104+
}
101105
}
102106

103107
return result;
@@ -108,20 +112,23 @@ export class Utils {
108112
}
109113

110114
/**
111-
* Stringifys an object with optional exclusions and max depth.
112-
* @param data The data object to add.
113-
* @param exclusions Any property names that should be excluded.
114-
* @param maxDepth The max depth of the object to include.
115+
* Checks to see if a value matches a pattern.
116+
* @param input the value to check against the @pattern.
117+
* @param pattern The pattern to check, supports wild cards (*).
115118
*/
116-
public static stringify(data:any, exclusions?:string[], maxDepth?:number): string {
117-
function checkForMatch(pattern:string, value:string): boolean {
118-
if (!pattern || !value || typeof value !== 'string') {
119+
public static isMatch(input:string, patterns:string[]):boolean {
120+
if (!input || typeof input !== 'string') {
121+
return false;
122+
}
123+
124+
let trim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
125+
return (patterns || []).some(pattern => {
126+
if (!pattern) {
119127
return false;
120128
}
121129

122-
let trim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
123130
pattern = pattern.toLowerCase().replace(trim, '');
124-
value = value.toLowerCase().replace(trim, '');
131+
input = input.toLowerCase().replace(trim, '');
125132

126133
if (pattern.length <= 0) {
127134
return false;
@@ -138,27 +145,33 @@ export class Utils {
138145
}
139146

140147
if (startsWithWildcard && endsWithWildcard) {
141-
return value.indexOf(pattern) !== -1;
148+
return input.indexOf(pattern) !== -1;
142149
}
143150

144151
if (startsWithWildcard) {
145-
return value.lastIndexOf(pattern) === (value.length - pattern.length);
152+
return input.lastIndexOf(pattern) === (input.length - pattern.length);
146153
}
147154

148155
if (endsWithWildcard) {
149-
return value.indexOf(pattern) === 0;
156+
return input.indexOf(pattern) === 0;
150157
}
151158

152-
return value === pattern;
153-
}
159+
return input === pattern;
160+
});
161+
}
154162

163+
/**
164+
* Stringifys an object with optional exclusions and max depth.
165+
* @param data The data object to add.
166+
* @param exclusions Any property names that should be excluded.
167+
* @param maxDepth The max depth of the object to include.
168+
*/
169+
public static stringify(data:any, exclusions?:string[], maxDepth?:number): string {
155170
function stringifyImpl(obj:any, excludedKeys:string[]): string {
156171
let cache:string[] = [];
157172
return JSON.stringify(obj, function(key:string, value:any) {
158-
for (let index = 0; index < (excludedKeys || []).length; index++) {
159-
if (checkForMatch(excludedKeys[index], key)) {
160-
return;
161-
}
173+
if (Utils.isMatch(key, excludedKeys)) {
174+
return;
162175
}
163176

164177
if (typeof value === 'object' && !!value) {
@@ -177,12 +190,12 @@ export class Utils {
177190
if (({}).toString.call(data) === '[object Array]') {
178191
let result = [];
179192
for (let index = 0; index < data.length; index++) {
180-
result[index] = JSON.parse(stringifyImpl(data[index], exclusions || []));
193+
result[index] = JSON.parse(stringifyImpl(data[index], exclusions));
181194
}
182195

183196
return JSON.stringify(result);
184197
}
185198

186-
return stringifyImpl(data, exclusions || []);
199+
return stringifyImpl(data, exclusions);
187200
}
188201
}

src/services/DefaultRequestInfoCollector.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ export class DefaultRequestInfoCollector implements IRequestInfoCollector {
99
return null;
1010
}
1111

12+
let exclusions = context.client.config.dataExclusions;
1213
let requestInfo:IRequestInfo = {
1314
user_agent: navigator.userAgent,
1415
is_secure: location.protocol === 'https:',
1516
host: location.hostname,
1617
port: location.port && location.port !== '' ? parseInt(location.port, 10) : 80,
1718
path: location.pathname,
1819
// client_ip_address: 'TODO',
19-
cookies: Utils.getCookies(document.cookie),
20-
query_string: Utils.parseQueryString(location.search.substring(1))
20+
cookies: Utils.getCookies(document.cookie, exclusions),
21+
query_string: Utils.parseQueryString(location.search.substring(1), exclusions)
2122
};
2223

2324
if (document.referrer && document.referrer !== '') {

src/services/NodeRequestInfoCollector.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ export class NodeRequestInfoCollector implements IRequestInfoCollector {
1010
return null;
1111
}
1212

13-
let request = context.contextData[REQUEST_KEY];
13+
let exclusions = context.client.config.dataExclusions;
14+
1415
// TODO: include referrer
16+
let request = context.contextData[REQUEST_KEY];
1517
let requestInfo:IRequestInfo = {
1618
client_ip_address: request.ip,
1719
user_agent: request.headers['user-agent'],
1820
is_secure: request.secure,
1921
http_method: request.method,
2022
host: request.hostname || request.host,
2123
path: request.path,
22-
post_data: request.body,
23-
cookies: Utils.getCookies((request || {}).headers.cookie),
24-
query_string: request.params
24+
post_data: JSON.parse(Utils.stringify(request.body, exclusions)),
25+
cookies: Utils.getCookies((request || {}).headers.cookie, exclusions),
26+
query_string: JSON.parse(Utils.stringify(request.params, exclusions))
2527
};
2628

2729
let host = request.headers.host;

0 commit comments

Comments
 (0)