Skip to content

Commit fa387f9

Browse files
committed
Added the ability to exclude properties based on data exclusions :D.
1 parent 8a6ed55 commit fa387f9

File tree

6 files changed

+73
-17
lines changed

6 files changed

+73
-17
lines changed

src/Utils-spec.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,29 @@ describe('Utils', () => {
2323
expect(Utils.stringify([{one: afoo, two: afoo}])).toBe('[{"one":{"a":"foo"}}]');
2424
});
2525

26+
it('should exclude properties', () => {
27+
var user:any = {
28+
id:1,
29+
name: 'Blake',
30+
password: '123456',
31+
passwordResetToken: 'a reset token',
32+
myPasswordValue: '123456',
33+
myPassword: '123456',
34+
customValue: 'Password',
35+
value: {
36+
Password: '123456'
37+
}
38+
};
39+
40+
expect(Utils.stringify(user)).toBe(JSON.stringify(user));
41+
expect(Utils.stringify(user, ['pAssword'])).toBe('{"id":1,"name":"Blake","passwordResetToken":"a reset token","myPasswordValue":"123456","myPassword":"123456","customValue":"Password","value":{}}');
42+
expect(Utils.stringify(user, ['*password'])).toBe('{"id":1,"name":"Blake","myPasswordValue":"123456","myPassword":"123456","customValue":"Password","value":{}}');
43+
expect(Utils.stringify(user, ['password*'])).toBe('{"id":1,"name":"Blake","passwordResetToken":"a reset token","myPasswordValue":"123456","customValue":"Password","value":{}}');
44+
expect(Utils.stringify(user, ['*password*'])).toBe('{"id":1,"name":"Blake","customValue":"Password","value":{}}');
45+
});
46+
2647
it('should stringify array', () => {
27-
var error:IEvent = {
48+
var error = {
2849
"type": "error",
2950
"data": {
3051
"@error": {
@@ -52,7 +73,8 @@ describe('Utils', () => {
5273
"column": 10
5374
}]
5475
}, "@submission_method": "onerror"
55-
}, "tags": []
76+
},
77+
"tags": []
5678
};
5779

5880
expect(Utils.stringify(error)).toBe(JSON.stringify(error));

src/Utils.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,46 @@ export class Utils {
109109
return Math.floor(Math.random() * 9007199254740992);
110110
}
111111

112-
public static stringify(data:any): string {
113-
function stringifyImpl(data:any): string {
112+
public static stringify(data:any, exclusions?:string[]): string {
113+
function checkForMatch(pattern:string, value:string): boolean {
114+
if (!pattern || !value || typeof value !== 'string') {
115+
return false;
116+
}
117+
118+
var startsWithWildcard:boolean = pattern[0] === '*';
119+
if (startsWithWildcard) {
120+
pattern = pattern.slice(1);
121+
}
122+
123+
var endsWithWildcard:boolean = pattern[pattern.length - 1] === '*';
124+
if (endsWithWildcard) {
125+
pattern = pattern.substring(0, pattern.length - 1);
126+
}
127+
128+
pattern = pattern.toLowerCase();
129+
value = value.toLowerCase();
130+
131+
if (startsWithWildcard && endsWithWildcard)
132+
return value.indexOf(pattern) !== -1;
133+
134+
if (startsWithWildcard)
135+
return value.lastIndexOf(pattern, 0) !== -1;
136+
137+
if (endsWithWildcard)
138+
return value.lastIndexOf(pattern) === (value.length - pattern.length);
139+
140+
return value === pattern;
141+
}
142+
143+
function stringifyImpl(data:any, exclusions:string[]): string {
114144
var cache:string[] = [];
115145
return JSON.stringify(data, function(key:string, value:any) {
146+
for (var index = 0; index < (exclusions || []).length; index++) {
147+
if (checkForMatch(exclusions[index], key)){
148+
return;
149+
}
150+
}
151+
116152
if (typeof value === 'object' && !!value) {
117153
if (cache.indexOf(value) !== -1) {
118154
// Circular reference found, discard key
@@ -129,12 +165,12 @@ export class Utils {
129165
if (toString.call(data) === '[object Array]') {
130166
var result = [];
131167
for (var index = 0; index < data.length; index++) {
132-
result[index] = JSON.parse(stringifyImpl(data[index]));
168+
result[index] = JSON.parse(stringifyImpl(data[index], exclusions || []));
133169
}
134170

135171
return JSON.stringify(result);
136172
}
137173

138-
return stringifyImpl(data);
174+
return stringifyImpl(data, exclusions || []);
139175
}
140176
}

src/configuration/Configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class Configuration implements IConfigurationSettings {
6161
* Contains a dictionary of custom settings that can be used to control
6262
* the client and will be automatically updated from the server.
6363
*/
64-
public settings:Object;
64+
public settings:Object = {};
6565

6666
public storage:IStorage<Object>;
6767

src/configuration/SettingsManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class SettingsManager {
66
private static _configPath:string = 'ex-server-settings.json';
77

88
public static applySavedServerSettings(config:Configuration):void {
9-
Utils.merge(config.settings, this.getSavedServerSettings(config));
9+
config.settings = Utils.merge(config.settings, this.getSavedServerSettings(config));
1010
// TODO: Fire on changed event.
1111
}
1212

@@ -37,7 +37,7 @@ export class SettingsManager {
3737
}
3838

3939
var savedServerSettings = SettingsManager.getSavedServerSettings(config);
40-
Utils.merge(config.settings, savedServerSettings);
40+
config.settings = Utils.merge(config.settings, savedServerSettings);
4141

4242
// TODO: Store snapshot of settings after reading from config and attributes and use that to revert to defaults.
4343
// Remove any existing server settings that are not in the new server settings.

src/submission/DefaultSubmissionClient.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,13 @@ export class DefaultSubmissionClient extends SubmissionClientBase {
3939
function parseResponseHeaders(headerStr) {
4040
var headers = {};
4141
var headerPairs = (headerStr || '').split('\u000d\u000a');
42-
for (var index = 0; index < headerPairs.length; index++) {
42+
for (var index:number = 0; index < headerPairs.length; index++) {
4343
var headerPair = headerPairs[index];
4444
// Can't use split() here because it does the wrong thing
4545
// if the header value has the string ": " in it.
46-
var index = headerPair.indexOf('\u003a\u0020');
47-
if (index > 0) {
48-
var key = headerPair.substring(0, index);
49-
var val = headerPair.substring(index + 2);
50-
headers[key] = val;
46+
var separator = headerPair.indexOf('\u003a\u0020');
47+
if (separator > 0) {
48+
headers[headerPair.substring(0, separator)] = headerPair.substring(separator + 2);
5149
}
5250
}
5351

src/submission/SubmissionClientBase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Utils } from '../Utils';
1010

1111
export class SubmissionClientBase implements ISubmissionClient {
1212
public postEvents(events:IEvent[], config:Configuration, callback:(response:SubmissionResponse) => void):void {
13-
return this.sendRequest(config, 'POST', '/api/v2/events', Utils.stringify(events), (status:number, message:string, data?:string, headers?:Object) => {
13+
return this.sendRequest(config, 'POST', '/api/v2/events', Utils.stringify(events, config.dataExclusions), (status:number, message:string, data?:string, headers?:Object) => {
1414

1515
var settingsVersion = (headers && parseInt(headers['X-Exceptionless-ConfigVersion'])) || -1;
1616
SettingsManager.checkVersion(settingsVersion, config);
@@ -21,7 +21,7 @@ export class SubmissionClientBase implements ISubmissionClient {
2121

2222
public postUserDescription(referenceId:string, description:IUserDescription, config:Configuration, callback:(response:SubmissionResponse) => void):void {
2323
var path = `/api/v2/events/by-ref/${encodeURIComponent(referenceId)}/user-description`;
24-
return this.sendRequest(config, 'POST', path, Utils.stringify(description), (status:number, message:string) => {
24+
return this.sendRequest(config, 'POST', path, Utils.stringify(description, config.dataExclusions), (status:number, message:string) => {
2525
callback(new SubmissionResponse(status, message));
2626
});
2727
}

0 commit comments

Comments
 (0)