forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontexts.ts
More file actions
302 lines (273 loc) · 10.7 KB
/
Copy pathcontexts.ts
File metadata and controls
302 lines (273 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import {PayloadData, base64urldecode, isNonEmptyJson} from "./payload";
import {SelfDescribingJson} from "./core";
import isEqual = require('lodash/isEqual');
import has = require('lodash/has');
import get = require('lodash/get');
/**
* Datatypes (some algebraic) for representing context types
*/
export type ContextGenerator = (payload: SelfDescribingJson, eventType: string, schema: string) => SelfDescribingJson;
export type ContextPrimitive = SelfDescribingJson | ContextGenerator;
export type ContextFilter = (payload: SelfDescribingJson, eventType: string, schema: string) => boolean;
export type FilterContextProvider = [ContextFilter, ContextPrimitive];
export interface RuleSet {
accept?: string[] | string;
reject?: string[] | string;
}
export type PathContextProvider = [RuleSet, ContextPrimitive];
export type ConditionalContextProvider = FilterContextProvider | PathContextProvider;
export function getSchemaParts(input: string): Array<string> | undefined {
let re = new RegExp('^iglu:([a-zA-Z0-9-_]+|\.)\/([a-zA-Z0-9-_]+|\.)\/([a-zA-Z0-9-_]+|\.)\/([0-9]+-[0-9]+-[0-9]|\.)$');
let matches = re.exec(input);
if (matches !== null) {
return matches.slice(1, 5);
}
return undefined;
}
export function isValidMatcher(input: any): boolean {
let schemaParts = getSchemaParts(input);
if (schemaParts) {
return schemaParts.length === 4;
}
return false;
}
export function isStringArray(input: any): boolean {
if (Array.isArray(input)) {
return input.every(function(i){ return typeof i === 'string' });
}
return false;
}
export function isValidRuleSetArg(input: any): boolean{
if (isStringArray(input)) {
input.every(function(i){ return isValidMatcher(i) });
} else if (typeof input === 'string') {
return isValidMatcher(input);
}
return false;
}
export function isSelfDescribingJson(input: any) : boolean {
if (isNonEmptyJson(input)) {
if ('schema' in input && 'data' in input) {
return (typeof(input.schema) === 'string' && typeof(input.data) === 'object');
}
}
return false;
}
export function isEventJson(input: any) : boolean {
if (isNonEmptyJson(input)) {
if ('e' in input) {
return (typeof(input.e) === 'string');
}
}
return false;
}
export function isObject(input: any) : boolean {
return input && typeof input === 'object' && input.constructor === Object;
}
export function isRuleSet(input: any) : boolean {
let methodCount = 0;
if (isObject(input)) {
if (has(input, 'accept')) {
if (isValidRuleSetArg(input['accept'])) {
methodCount += 1;
} else {
return false;
}
}
if (has(input, 'reject')) {
if (isValidRuleSetArg(input['reject'])) {
methodCount += 1;
} else {
return false;
}
}
return methodCount > 0; // if 'reject' or 'accept' exists, we have a valid ruleset
}
return false;
}
export function isContextGenerator(input: any) : boolean {
if (typeof(input) === 'function') {
return input.length === 3;
}
return false;
}
export function isContextFilter(input: any) : boolean {
if (typeof(input) === 'function') {
return input.length === 3;
}
return false;
}
export function isContextPrimitive(input: any) : boolean {
return (isContextGenerator(input) || isSelfDescribingJson(input));
}
export function isFilterContextProvider(input: any) : boolean {
if (Array.isArray(input)) {
if (input.length === 2) {
return isContextFilter(input[0]) && isContextPrimitive(input[1]);
}
}
return false;
}
export function isPathContextProvider(input: any) : boolean {
if (Array.isArray(input) && input.length === 2) {
return isRuleSet(input[0]) && isContextPrimitive(input[1]);
}
return false;
}
export function isConditionalContextProvider(input: any) : boolean {
return isFilterContextProvider(input) || isPathContextProvider(input);
}
export function matchSchemaAgainstRule(rule: string, schema: string) : boolean {
let ruleParts = getSchemaParts(rule);
let schemaParts = getSchemaParts(schema);
if (ruleParts === undefined || schemaParts === undefined ||
ruleParts.length !== 4 || schemaParts.length !== 4) {
return false;
}
let matchCount = 0;
for (let i = 0; i <= 3; i++) {
if (ruleParts[0] === schemaParts[0] || ruleParts[0] === '.') {
matchCount++;
} else {
return false;
}
}
return matchCount === 4;
}
export function matchSchemaAgainstRuleSet(ruleSet: RuleSet, schema: string) : boolean {
let matchCount = 0;
let acceptRules = get(ruleSet, 'accept');
if (Array.isArray(acceptRules)) {
if (!(ruleSet.accept as Array<string>).every((rule) => (matchSchemaAgainstRule(rule, schema)))) {
return false;
}
matchCount++;
} else if (typeof(acceptRules) === 'string') {
if (!matchSchemaAgainstRule(acceptRules, schema)) {
return false;
}
matchCount++;
}
let rejectRules = get(ruleSet, 'reject');
if (Array.isArray(rejectRules)) {
if (!(ruleSet.reject as Array<string>).every((rule) => (matchSchemaAgainstRule(rule, schema)))) {
return false;
}
matchCount++;
} else if (typeof(rejectRules) === 'string') {
if (!matchSchemaAgainstRule(rejectRules, schema)) {
return false;
}
matchCount++;
}
return matchCount > 0;
}
// Warning: below is a design decision, good to know for understanding this functionality.
// Returns the "useful" schema, basically what would someone want to use to discern events.
// The idea being that you can determine the event type from 'e', so getting the schema from
// 'ue_px.schema'/'ue_pr.schema' would be redundant - it'll return the unstructured event schema.
// Instead the schema nested inside the unstructured event is more useful!
function getUsefulSchema(sb: SelfDescribingJson): string {
if (typeof get(sb, 'ue_px.data.schema') === 'string') {
return get(sb, 'ue_px.data.schema');
} else if (typeof get(sb, 'ue_pr.data.schema') === 'string') {
return get(sb, 'ue_pr.data.schema');
} else if (typeof get(sb, 'schema') === 'string') {
return get(sb, 'schema');
}
return '';
}
function getDecodedEvent(sb: SelfDescribingJson): SelfDescribingJson {
let decodedEvent = Object.assign({}, sb);
if (has(decodedEvent, 'ue_px')) {
decodedEvent['ue_px'] = JSON.parse(base64urldecode(get(decodedEvent, ['ue_px'])));
}
return decodedEvent;
}
function getEventType(sb: {}): string {
return get(sb, 'e', '');
}
export function contextModule() {
let globalPrimitives : Array<ContextPrimitive> = [];
let conditionalProviders : Array<ConditionalContextProvider> = [];
function generateContext(contextPrimitive: ContextPrimitive,
event: SelfDescribingJson,
eventType: string,
eventSchema: string) : SelfDescribingJson | undefined
{
if (isSelfDescribingJson(contextPrimitive)) {
return <SelfDescribingJson> contextPrimitive;
} else if (isContextGenerator(contextPrimitive)) {
return (contextPrimitive as ContextGenerator)(event, eventType, eventSchema);
}
}
function assembleAllContexts(event: SelfDescribingJson) : Array<SelfDescribingJson> {
let eventSchema = getUsefulSchema(event);
let eventType = getEventType(event);
let contexts : Array<SelfDescribingJson> = [];
for (let context of globalPrimitives) {
let generatedContext = generateContext(context, event, eventType, eventSchema);
if (generatedContext) {
contexts = contexts.concat(generatedContext);
}
}
for (let provider of conditionalProviders) {
if (isFilterContextProvider(provider)) {
let filter : ContextFilter = (provider as FilterContextProvider)[0];
if (filter(event, eventType, eventSchema)) {
let generatedContext = generateContext((provider as FilterContextProvider)[1], event, eventType, eventSchema);
if (generatedContext) {
contexts = contexts.concat(generatedContext);
}
}
} else if (isPathContextProvider(provider)) {
if (matchSchemaAgainstRuleSet((provider as PathContextProvider)[0], eventSchema)) {
let generatedContext = generateContext((provider as PathContextProvider)[1], event, eventType, eventSchema);
if (generatedContext) {
contexts = contexts.concat(generatedContext);
}
}
}
}
return contexts;
}
return {
addGlobalContexts: function (contexts: Array<any>) {
let acceptedConditionalContexts : ConditionalContextProvider[] = [];
let acceptedContextPrimitives : ContextPrimitive[] = [];
for (let context of contexts) {
if (isContextPrimitive(context)) {
acceptedContextPrimitives = acceptedContextPrimitives.concat(context);
} else if (isConditionalContextProvider(context)) {
acceptedConditionalContexts = acceptedConditionalContexts.concat(context);
}
}
globalPrimitives = globalPrimitives.concat(acceptedContextPrimitives);
conditionalProviders = conditionalProviders.concat(acceptedConditionalContexts);
},
clearAllContexts: function () {
conditionalProviders = [];
globalPrimitives = [];
},
removeGlobalContexts: function (contexts: Array<any>) {
for (let context of contexts) {
if (isContextPrimitive(context)) {
globalPrimitives = globalPrimitives.filter(item => !isEqual(item, context));
} else if (isConditionalContextProvider(context)) {
conditionalProviders = conditionalProviders.filter(item => !isEqual(item, context));
} else {
// error message here?
}
}
},
getApplicableContexts: function (event: PayloadData) : Array<SelfDescribingJson> {
const builtEvent = event.build();
if (isEventJson(builtEvent)) {
const decodedEvent = getDecodedEvent(builtEvent as SelfDescribingJson);
return assembleAllContexts(decodedEvent);
} else {
return [];
}
}
};
}