snowplow-tracker-core
Advanced tools
Comparing version 0.7.0-alpha.3 to 0.7.0-alpha.5
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function base64urldecode(data) { | ||
if (!data) { | ||
return data; | ||
} | ||
var padding = 4 - data.length % 4; | ||
switch (padding) { | ||
case 2: | ||
data += "=="; | ||
break; | ||
case 3: | ||
data += "="; | ||
break; | ||
} | ||
var b64Data = data.replace(/-/g, '+').replace(/_/g, '/'); | ||
return base64decode(b64Data); | ||
} | ||
exports.base64urldecode = base64urldecode; | ||
function base64encode(data) { | ||
@@ -4,0 +21,0 @@ var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; |
@@ -27,2 +27,19 @@ /* | ||
export function base64urldecode(data: string): string { | ||
if (!data) { | ||
return data; | ||
} | ||
var padding = 4 - data.length % 4; | ||
switch (padding) { | ||
case 2: | ||
data += "=="; | ||
break; | ||
case 3: | ||
data += "="; | ||
break; | ||
} | ||
var b64Data = data.replace(/-/g, '+').replace(/_/g, '/'); | ||
return base64decode(b64Data); | ||
} | ||
/** | ||
@@ -29,0 +46,0 @@ * Encode string as base64. |
@@ -12,3 +12,3 @@ "use strict"; | ||
var payload_1 = require("./payload"); | ||
var base64url_1 = require("base64url"); | ||
var base64_1 = require("./base64"); | ||
var isEqual = require("lodash/isEqual"); | ||
@@ -18,2 +18,5 @@ var has = require("lodash/has"); | ||
var isPlainObject = require("lodash/isPlainObject"); | ||
var every = require("lodash/every"); | ||
var compact = require("lodash/compact"); | ||
var map = require("lodash/map"); | ||
function getSchemaParts(input) { | ||
@@ -116,2 +119,5 @@ var re = new RegExp('^iglu:([a-zA-Z0-9-_]+|\.)\/([a-zA-Z0-9-_]+|\.)\/([a-zA-Z0-9-_]+|\.)\/([0-9]+-[0-9]+-[0-9]|\.)$'); | ||
if (input.length === 2) { | ||
if (Array.isArray(input[1])) { | ||
return isContextFilter(input[0]) && every(input[1], isContextPrimitive); | ||
} | ||
return isContextFilter(input[0]) && isContextPrimitive(input[1]); | ||
@@ -125,2 +131,5 @@ } | ||
if (Array.isArray(input) && input.length === 2) { | ||
if (Array.isArray(input[1])) { | ||
return isRuleSet(input[0]) && every(input[1], isContextPrimitive); | ||
} | ||
return isRuleSet(input[0]) && isContextPrimitive(input[1]); | ||
@@ -200,3 +209,3 @@ } | ||
if (has(decodedEvent, 'ue_px')) { | ||
decodedEvent['ue_px'] = JSON.parse(base64url_1.base64url.base64urldecode(get(decodedEvent, ['ue_px']))); | ||
decodedEvent['ue_px'] = JSON.parse(base64_1.base64urldecode(get(decodedEvent, ['ue_px']))); | ||
} | ||
@@ -208,13 +217,88 @@ return decodedEvent; | ||
} | ||
function buildGenerator(generator, event, eventType, eventSchema) { | ||
var contextGeneratorResult = undefined; | ||
try { | ||
contextGeneratorResult = generator(event, eventType, eventSchema); | ||
if (isSelfDescribingJson(contextGeneratorResult)) { | ||
return contextGeneratorResult; | ||
} | ||
else if (Array.isArray(contextGeneratorResult) && every(contextGeneratorResult, isSelfDescribingJson)) { | ||
return contextGeneratorResult; | ||
} | ||
else { | ||
return undefined; | ||
} | ||
} | ||
catch (error) { | ||
contextGeneratorResult = undefined; | ||
} | ||
return contextGeneratorResult; | ||
} | ||
function normalizeToArray(input) { | ||
if (Array.isArray(input)) { | ||
return input; | ||
} | ||
return Array.of(input); | ||
} | ||
function generatePrimitives(contextPrimitives, event, eventType, eventSchema) { | ||
var normalizedInputs = normalizeToArray(contextPrimitives); | ||
var partialEvaluate = function (primitive) { | ||
var result = evaluatePrimitive(primitive, event, eventType, eventSchema); | ||
if (result && result.length !== 0) { | ||
return result; | ||
} | ||
}; | ||
var generatedContexts = map(normalizedInputs, partialEvaluate); | ||
return [].concat.apply([], compact(generatedContexts)); | ||
} | ||
function evaluatePrimitive(contextPrimitive, event, eventType, eventSchema) { | ||
if (isSelfDescribingJson(contextPrimitive)) { | ||
return [contextPrimitive]; | ||
} | ||
else if (isContextGenerator(contextPrimitive)) { | ||
var generatorOutput = buildGenerator(contextPrimitive, event, eventType, eventSchema); | ||
if (isSelfDescribingJson(generatorOutput)) { | ||
return [generatorOutput]; | ||
} | ||
else if (Array.isArray(generatorOutput)) { | ||
return generatorOutput; | ||
} | ||
} | ||
return undefined; | ||
} | ||
function evaluateProvider(provider, event, eventType, eventSchema) { | ||
if (isFilterContextProvider(provider)) { | ||
var filter = provider[0]; | ||
var filterResult = false; | ||
try { | ||
filterResult = filter(event, eventType, eventSchema); | ||
} | ||
catch (error) { | ||
filterResult = false; | ||
} | ||
if (filterResult === true) { | ||
return generatePrimitives(provider[1], event, eventType, eventSchema); | ||
} | ||
} | ||
else if (isPathContextProvider(provider)) { | ||
if (matchSchemaAgainstRuleSet(provider[0], eventSchema)) { | ||
return generatePrimitives(provider[1], event, eventType, eventSchema); | ||
} | ||
} | ||
return []; | ||
} | ||
function generateConditionals(providers, event, eventType, eventSchema) { | ||
var normalizedInput = normalizeToArray(providers); | ||
var partialEvaluate = function (provider) { | ||
var result = evaluateProvider(provider, event, eventType, eventSchema); | ||
if (result && result.length !== 0) { | ||
return result; | ||
} | ||
}; | ||
var generatedContexts = map(normalizedInput, partialEvaluate); | ||
return [].concat.apply([], compact(generatedContexts)); | ||
} | ||
function contextModule() { | ||
var globalPrimitives = []; | ||
var conditionalProviders = []; | ||
function generateContext(contextPrimitive, event, eventType, eventSchema) { | ||
if (isSelfDescribingJson(contextPrimitive)) { | ||
return contextPrimitive; | ||
} | ||
else if (isContextGenerator(contextPrimitive)) { | ||
return contextPrimitive(event, eventType, eventSchema); | ||
} | ||
} | ||
function assembleAllContexts(event) { | ||
@@ -224,29 +308,6 @@ var eventSchema = getUsefulSchema(event); | ||
var contexts = []; | ||
for (var _i = 0, globalPrimitives_1 = globalPrimitives; _i < globalPrimitives_1.length; _i++) { | ||
var context = globalPrimitives_1[_i]; | ||
var generatedContext = generateContext(context, event, eventType, eventSchema); | ||
if (generatedContext) { | ||
contexts = contexts.concat(generatedContext); | ||
} | ||
} | ||
for (var _a = 0, conditionalProviders_1 = conditionalProviders; _a < conditionalProviders_1.length; _a++) { | ||
var provider = conditionalProviders_1[_a]; | ||
if (isFilterContextProvider(provider)) { | ||
var filter = provider[0]; | ||
if (filter(event, eventType, eventSchema)) { | ||
var generatedContext = generateContext(provider[1], event, eventType, eventSchema); | ||
if (generatedContext) { | ||
contexts = contexts.concat(generatedContext); | ||
} | ||
} | ||
} | ||
else if (isPathContextProvider(provider)) { | ||
if (matchSchemaAgainstRuleSet(provider[0], eventSchema)) { | ||
var generatedContext = generateContext(provider[1], event, eventType, eventSchema); | ||
if (generatedContext) { | ||
contexts = contexts.concat(generatedContext); | ||
} | ||
} | ||
} | ||
} | ||
var generatedPrimitives = generatePrimitives(globalPrimitives, event, eventType, eventSchema); | ||
contexts.push.apply(contexts, generatedPrimitives); | ||
var generatedConditionals = generateConditionals(conditionalProviders, event, eventType, eventSchema); | ||
contexts.push.apply(contexts, generatedConditionals); | ||
return contexts; | ||
@@ -260,7 +321,7 @@ } | ||
var context = contexts_1[_i]; | ||
if (isContextPrimitive(context)) { | ||
acceptedContextPrimitives = acceptedContextPrimitives.concat(context); | ||
if (isConditionalContextProvider(context)) { | ||
acceptedConditionalContexts.push(context); | ||
} | ||
else if (isConditionalContextProvider(context)) { | ||
acceptedConditionalContexts = acceptedConditionalContexts.concat(context); | ||
else if (isContextPrimitive(context)) { | ||
acceptedContextPrimitives.push(context); | ||
} | ||
@@ -277,8 +338,8 @@ } | ||
var _loop_1 = function (context) { | ||
if (isContextPrimitive(context)) { | ||
if (isConditionalContextProvider(context)) { | ||
conditionalProviders = conditionalProviders.filter(function (item) { return !isEqual(item, context); }); | ||
} | ||
else if (isContextPrimitive(context)) { | ||
globalPrimitives = globalPrimitives.filter(function (item) { return !isEqual(item, context); }); | ||
} | ||
else if (isConditionalContextProvider(context)) { | ||
conditionalProviders = conditionalProviders.filter(function (item) { return !isEqual(item, context); }); | ||
} | ||
else { | ||
@@ -285,0 +346,0 @@ } |
import { PayloadData, isNonEmptyJson } from "./payload"; | ||
import { SelfDescribingJson } from "./core"; | ||
import { base64url } from "base64url"; | ||
import { base64urldecode } from "./base64"; | ||
import isEqual = require('lodash/isEqual'); | ||
@@ -8,2 +8,5 @@ import has = require('lodash/has'); | ||
import isPlainObject = require('lodash/isPlainObject'); | ||
import every = require('lodash/every'); | ||
import compact = require('lodash/compact'); | ||
import map = require('lodash/map'); | ||
@@ -16,3 +19,3 @@ /** | ||
export type ContextFilter = (payload: SelfDescribingJson, eventType: string, schema: string) => boolean; | ||
export type FilterContextProvider = [ContextFilter, ContextPrimitive]; | ||
export type FilterContextProvider = [ContextFilter, Array<ContextPrimitive> | ContextPrimitive]; | ||
export interface RuleSet { | ||
@@ -22,3 +25,3 @@ accept?: string[] | string; | ||
} | ||
export type PathContextProvider = [RuleSet, ContextPrimitive]; | ||
export type PathContextProvider = [RuleSet, Array<ContextPrimitive> | ContextPrimitive]; | ||
export type ConditionalContextProvider = FilterContextProvider | PathContextProvider; | ||
@@ -122,2 +125,5 @@ | ||
if (input.length === 2) { | ||
if (Array.isArray(input[1])) { | ||
return isContextFilter(input[0]) && every(input[1], isContextPrimitive); | ||
} | ||
return isContextFilter(input[0]) && isContextPrimitive(input[1]); | ||
@@ -131,2 +137,5 @@ } | ||
if (Array.isArray(input) && input.length === 2) { | ||
if (Array.isArray(input[1])) { | ||
return isRuleSet(input[0]) && every(input[1], isContextPrimitive); | ||
} | ||
return isRuleSet(input[0]) && isContextPrimitive(input[1]); | ||
@@ -208,3 +217,3 @@ } | ||
if (has(decodedEvent, 'ue_px')) { | ||
decodedEvent['ue_px'] = JSON.parse(base64url.base64urldecode(get(decodedEvent, ['ue_px']))); | ||
decodedEvent['ue_px'] = JSON.parse(base64urldecode(get(decodedEvent, ['ue_px']))); | ||
} | ||
@@ -218,2 +227,101 @@ return decodedEvent; | ||
function buildGenerator(generator: ContextGenerator, | ||
event: SelfDescribingJson, | ||
eventType: string, | ||
eventSchema: string) : SelfDescribingJson | Array<SelfDescribingJson> | undefined { | ||
let contextGeneratorResult : SelfDescribingJson | Array<SelfDescribingJson> | undefined = undefined; | ||
try { | ||
// try to evaluate context generator | ||
contextGeneratorResult = generator(event, eventType, eventSchema); | ||
// determine if the produced result is a valid SDJ | ||
if (isSelfDescribingJson(contextGeneratorResult)) { | ||
return contextGeneratorResult; | ||
} else if (Array.isArray(contextGeneratorResult) && every(contextGeneratorResult, isSelfDescribingJson)) { | ||
return contextGeneratorResult; | ||
} else { | ||
return undefined; | ||
} | ||
} catch (error) { | ||
contextGeneratorResult = undefined; | ||
} | ||
return contextGeneratorResult; | ||
} | ||
function normalizeToArray(input: any) : Array<any> { | ||
if (Array.isArray(input)) { | ||
return input; | ||
} | ||
return Array.of(input); | ||
} | ||
function generatePrimitives(contextPrimitives: Array<ContextPrimitive> | ContextPrimitive, | ||
event: SelfDescribingJson, | ||
eventType: string, | ||
eventSchema: string) : Array<SelfDescribingJson> { | ||
let normalizedInputs : Array<ContextPrimitive> = normalizeToArray(contextPrimitives); | ||
let partialEvaluate = (primitive) => { | ||
let result = evaluatePrimitive(primitive, event, eventType, eventSchema); | ||
if (result && result.length !== 0) { | ||
return result; | ||
} | ||
}; | ||
let generatedContexts = map(normalizedInputs, partialEvaluate); | ||
return [].concat(...compact(generatedContexts)); | ||
} | ||
function evaluatePrimitive(contextPrimitive: ContextPrimitive, | ||
event: SelfDescribingJson, | ||
eventType: string, | ||
eventSchema: string) : Array<SelfDescribingJson> | undefined { | ||
if (isSelfDescribingJson(contextPrimitive)) { | ||
return [contextPrimitive as SelfDescribingJson]; | ||
} else if (isContextGenerator(contextPrimitive)) { | ||
let generatorOutput = buildGenerator(contextPrimitive as ContextGenerator, event, eventType, eventSchema); | ||
if (isSelfDescribingJson(generatorOutput)) { | ||
return [generatorOutput as SelfDescribingJson]; | ||
} else if (Array.isArray(generatorOutput)) { | ||
return generatorOutput; | ||
} | ||
} | ||
return undefined; | ||
} | ||
function evaluateProvider(provider: ConditionalContextProvider, | ||
event: SelfDescribingJson, | ||
eventType: string, | ||
eventSchema: string): Array<SelfDescribingJson> { | ||
if (isFilterContextProvider(provider)) { | ||
let filter : ContextFilter = (provider as FilterContextProvider)[0]; | ||
let filterResult = false; | ||
try { | ||
filterResult = filter(event, eventType, eventSchema); | ||
} catch(error) { | ||
filterResult = false; | ||
} | ||
if (filterResult === true) { | ||
return generatePrimitives((provider as FilterContextProvider)[1], event, eventType, eventSchema); | ||
} | ||
} else if (isPathContextProvider(provider)) { | ||
if (matchSchemaAgainstRuleSet((provider as PathContextProvider)[0], eventSchema)) { | ||
return generatePrimitives((provider as PathContextProvider)[1], event, eventType, eventSchema); | ||
} | ||
} | ||
return []; | ||
} | ||
function generateConditionals(providers: Array<ConditionalContextProvider> | ConditionalContextProvider, | ||
event: SelfDescribingJson, | ||
eventType: string, | ||
eventSchema: string) : Array<SelfDescribingJson> { | ||
let normalizedInput : Array<ConditionalContextProvider> = normalizeToArray(providers); | ||
let partialEvaluate = (provider) => { | ||
let result = evaluateProvider(provider, event, eventType, eventSchema); | ||
if (result && result.length !== 0) { | ||
return result; | ||
} | ||
}; | ||
let generatedContexts = map(normalizedInput, partialEvaluate); | ||
return [].concat(...compact(generatedContexts)); | ||
} | ||
export function contextModule() { | ||
@@ -223,14 +331,2 @@ let globalPrimitives : Array<ContextPrimitive> = []; | ||
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> { | ||
@@ -240,27 +336,8 @@ let eventSchema = getUsefulSchema(event); | ||
let contexts : Array<SelfDescribingJson> = []; | ||
for (let context of globalPrimitives) { | ||
let generatedContext = generateContext(context, event, eventType, eventSchema); | ||
if (generatedContext) { | ||
contexts = contexts.concat(generatedContext); | ||
} | ||
} | ||
let generatedPrimitives = generatePrimitives(globalPrimitives, event, eventType, eventSchema); | ||
contexts.push(...generatedPrimitives); | ||
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); | ||
} | ||
} | ||
} | ||
} | ||
let generatedConditionals = generateConditionals(conditionalProviders, event, eventType, eventSchema); | ||
contexts.push(...generatedConditionals); | ||
return contexts; | ||
@@ -274,6 +351,6 @@ } | ||
for (let context of contexts) { | ||
if (isContextPrimitive(context)) { | ||
acceptedContextPrimitives = acceptedContextPrimitives.concat(context); | ||
} else if (isConditionalContextProvider(context)) { | ||
acceptedConditionalContexts = acceptedConditionalContexts.concat(context); | ||
if (isConditionalContextProvider(context)) { | ||
acceptedConditionalContexts.push(context); | ||
} else if (isContextPrimitive(context)) { | ||
acceptedContextPrimitives.push(context); | ||
} | ||
@@ -292,6 +369,6 @@ } | ||
for (let context of contexts) { | ||
if (isContextPrimitive(context)) { | ||
if (isConditionalContextProvider(context)) { | ||
conditionalProviders = conditionalProviders.filter(item => !isEqual(item, context)); | ||
} else if (isContextPrimitive(context)) { | ||
globalPrimitives = globalPrimitives.filter(item => !isEqual(item, context)); | ||
} else if (isConditionalContextProvider(context)) { | ||
conditionalProviders = conditionalProviders.filter(item => !isEqual(item, context)); | ||
} else { | ||
@@ -298,0 +375,0 @@ // error message here? |
@@ -52,8 +52,10 @@ "use strict"; | ||
var globalContexts = getAllContexts(sb); | ||
var returnedContexts = []; | ||
if (contexts && contexts.length) { | ||
return contexts.concat(globalContexts); | ||
returnedContexts.push.apply(returnedContexts, contexts); | ||
} | ||
else { | ||
return globalContexts; | ||
if (globalContexts && globalContexts.length) { | ||
returnedContexts.push.apply(returnedContexts, globalContexts); | ||
} | ||
return returnedContexts; | ||
} | ||
@@ -60,0 +62,0 @@ function track(sb, context, tstamp) { |
@@ -141,9 +141,13 @@ /* | ||
*/ | ||
function attachGlobalContexts(sb: PayloadData, contexts?: Array<SelfDescribingJson>): Array<SelfDescribingJson> | undefined{ | ||
var globalContexts = getAllContexts(sb); | ||
function attachGlobalContexts(sb: PayloadData, | ||
contexts?: Array<SelfDescribingJson>): Array<SelfDescribingJson> { | ||
let globalContexts : Array<SelfDescribingJson> = getAllContexts(sb); | ||
let returnedContexts : Array<SelfDescribingJson> = []; | ||
if (contexts && contexts.length) { | ||
return contexts.concat(globalContexts); | ||
} else { | ||
return globalContexts; | ||
returnedContexts.push(...contexts); | ||
} | ||
if (globalContexts && globalContexts.length) { | ||
returnedContexts.push(...globalContexts); | ||
} | ||
return returnedContexts; | ||
} | ||
@@ -150,0 +154,0 @@ |
declare module 'snowplow-tracker/lib/base64' { | ||
export function base64urldecode(data: string): string; | ||
export function base64encode(data: string): string; | ||
@@ -24,3 +25,3 @@ export function base64decode(encodedData: string): string; | ||
export type ContextFilter = (payload: SelfDescribingJson, eventType: string, schema: string) => boolean; | ||
export type FilterContextProvider = [ContextFilter, ContextPrimitive]; | ||
export type FilterContextProvider = [ContextFilter, Array<ContextPrimitive> | ContextPrimitive]; | ||
export interface RuleSet { | ||
@@ -30,3 +31,3 @@ accept?: string[] | string; | ||
} | ||
export type PathContextProvider = [RuleSet, ContextPrimitive]; | ||
export type PathContextProvider = [RuleSet, Array<ContextPrimitive> | ContextPrimitive]; | ||
export type ConditionalContextProvider = FilterContextProvider | PathContextProvider; | ||
@@ -33,0 +34,0 @@ export function getSchemaParts(input: string): Array<string> | undefined; |
{ | ||
"name": "snowplow-tracker-core", | ||
"version": "0.7.0-alpha.3", | ||
"version": "0.7.0-alpha.5", | ||
"devDependencies": { | ||
@@ -15,3 +15,2 @@ "@types/es6-shim": "0.31.34", | ||
"@types/uuid": "^2.0.29", | ||
"base64url": "^3.0.0", | ||
"lodash": "^4.17.11", | ||
@@ -18,0 +17,0 @@ "uuid": "2.0.3" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
144025
3
29
2547
- Removedbase64url@^3.0.0
- Removedbase64url@3.0.1(transitive)