avo-inspector 2.2.0 → 2.2.2-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -1
- package/README.md +95 -0
- package/bin/avo-inspector.js +97 -0
- package/dist/AvoBatcher.d.ts +4 -2
- package/dist/AvoEncryption.d.ts +33 -0
- package/dist/AvoInspector.d.ts +31 -3
- package/dist/AvoNetworkCallsHandler.d.ts +28 -3
- package/dist/AvoSchemaParser.d.ts +12 -1
- package/dist/browser.js.LICENSE.txt +5 -0
- package/dist/eventSpec/AvoEventSpecCache.d.ts +73 -0
- package/dist/eventSpec/AvoEventSpecFetchTypes.d.ts +109 -0
- package/dist/eventSpec/AvoEventSpecFetcher.d.ts +54 -0
- package/dist/eventSpec/EventValidator.d.ts +90 -0
- package/dist/eventSpec/cache.d.ts +73 -0
- package/dist/eventSpec/index.d.ts +4 -0
- package/dist/eventSpec/types.d.ts +74 -0
- package/dist/index.js +2 -1
- package/dist/index.js.LICENSE.txt +5 -0
- package/dist-browser/AvoAnonymousId.d.ts +26 -0
- package/dist-browser/AvoBatcher.d.ts +26 -0
- package/dist-browser/AvoDeduplicator.d.ts +20 -0
- package/dist-browser/AvoEncryption.d.ts +33 -0
- package/dist-browser/AvoGuid.d.ts +3 -0
- package/dist-browser/AvoInspector.d.ts +78 -0
- package/dist-browser/AvoInspectorEnv.d.ts +7 -0
- package/dist-browser/AvoNetworkCallsHandler.d.ts +74 -0
- package/dist-browser/AvoSchemaParser.d.ts +20 -0
- package/dist-browser/AvoStorage.d.ts +22 -0
- package/dist-browser/__mocks__/xhr.d.ts +12 -0
- package/dist-browser/__mocks__/xhrError.d.ts +12 -0
- package/dist-browser/__tests__/AnonymousId_test.d.ts +1 -0
- package/dist-browser/__tests__/AvoEncryption_test.d.ts +1 -0
- package/dist-browser/__tests__/AvoInspectorEventSpec_test.d.ts +1 -0
- package/dist-browser/__tests__/AvoInspector_test.d.ts +1 -0
- package/dist-browser/__tests__/BatcherPublic_test.d.ts +1 -0
- package/dist-browser/__tests__/Batcher_test.d.ts +1 -0
- package/dist-browser/__tests__/Deduplicator_test.d.ts +1 -0
- package/dist-browser/__tests__/EncryptedPropertyTracking_test.d.ts +1 -0
- package/dist-browser/__tests__/EventSpecCache_test.d.ts +1 -0
- package/dist-browser/__tests__/EventSpecFetcher_test.d.ts +1 -0
- package/dist-browser/__tests__/EventValidator_test.d.ts +1 -0
- package/dist-browser/__tests__/NetworkCallsHandler_test.d.ts +1 -0
- package/dist-browser/__tests__/Parsing_test.d.ts +1 -0
- package/dist-browser/__tests__/Storage_test.d.ts +1 -0
- package/dist-browser/__tests__/ValidationIntegration_test.d.ts +1 -0
- package/dist-browser/__tests__/constants.d.ts +36 -0
- package/dist-browser/__tests__/helpers/encryptionHelpers.d.ts +13 -0
- package/dist-browser/__tests__/helpers/mockFactories.d.ts +49 -0
- package/dist-browser/eventSpec/AvoEventSpecCache.d.ts +73 -0
- package/dist-browser/eventSpec/AvoEventSpecFetchTypes.d.ts +109 -0
- package/dist-browser/eventSpec/AvoEventSpecFetcher.d.ts +55 -0
- package/dist-browser/eventSpec/EventValidator.d.ts +90 -0
- package/dist-browser/eventSpec/index.d.ts +4 -0
- package/dist-browser/index.d.ts +2 -0
- package/dist-browser/utils.d.ts +3 -0
- package/dist-native/AvoBatcher.d.ts +24 -0
- package/dist-native/AvoBatcher.js +82 -0
- package/dist-native/AvoDeduplicator.d.ts +36 -0
- package/dist-native/AvoDeduplicator.js +142 -0
- package/dist-native/AvoGuid.d.ts +3 -0
- package/dist-native/AvoGuid.js +14 -0
- package/dist-native/AvoInspector.d.ts +52 -0
- package/dist-native/AvoInspector.js +220 -0
- package/dist-native/AvoInspectorEnv.d.ts +7 -0
- package/dist-native/AvoInspectorEnv.js +8 -0
- package/dist-native/AvoInstallationId.d.ts +5 -0
- package/dist-native/AvoInstallationId.js +36 -0
- package/dist-native/AvoNetworkCallsHandler.d.ts +48 -0
- package/dist-native/AvoNetworkCallsHandler.js +142 -0
- package/dist-native/AvoSchemaParser.d.ts +11 -0
- package/dist-native/AvoSchemaParser.js +95 -0
- package/dist-native/AvoSessionTracker.d.ts +15 -0
- package/dist-native/AvoSessionTracker.js +104 -0
- package/dist-native/AvoStorage.d.ts +22 -0
- package/dist-native/AvoStorage.js +336 -0
- package/dist-native/__mocks__/xhr.d.ts +12 -0
- package/dist-native/__mocks__/xhr.js +14 -0
- package/dist-native/__mocks__/xhrError.d.ts +12 -0
- package/dist-native/__mocks__/xhrError.js +14 -0
- package/dist-native/__tests__/AvoInspector.d.ts +1 -0
- package/dist-native/__tests__/AvoInspector.js +207 -0
- package/dist-native/__tests__/Batcher.d.ts +1 -0
- package/dist-native/__tests__/Batcher.js +280 -0
- package/dist-native/__tests__/BatcherPublic.d.ts +1 -0
- package/dist-native/__tests__/BatcherPublic.js +82 -0
- package/dist-native/__tests__/Deduplicator.d.ts +1 -0
- package/dist-native/__tests__/Deduplicator.js +111 -0
- package/dist-native/__tests__/InstallationId.d.ts +1 -0
- package/dist-native/__tests__/InstallationId.js +40 -0
- package/dist-native/__tests__/NetworkCallsHandler.d.ts +1 -0
- package/dist-native/__tests__/NetworkCallsHandler.js +132 -0
- package/dist-native/__tests__/Parsing.d.ts +1 -0
- package/dist-native/__tests__/Parsing.js +115 -0
- package/dist-native/__tests__/Sessions.d.ts +1 -0
- package/dist-native/__tests__/Sessions.js +179 -0
- package/dist-native/__tests__/Storage.d.ts +1 -0
- package/dist-native/__tests__/Storage.js +173 -0
- package/dist-native/__tests__/constants.d.ts +36 -0
- package/dist-native/__tests__/constants.js +47 -0
- package/dist-native/index.d.ts +2 -0
- package/dist-native/index.js +7 -0
- package/dist-native/utils.d.ts +3 -0
- package/dist-native/utils.js +42 -0
- package/jest.config.js +6 -0
- package/package.json +8 -2
- package/webpack.browserConfig.js +1 -1
- package/webpack.config.js +1 -1
- package/.claude/settings.local.json +0 -9
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is generated. Internal development changes should be made in the generator
|
|
3
|
+
* and the file should be re-generated. External contributions are welcome to submit
|
|
4
|
+
* changes directly to this file, and we'll apply them to the generator internally.
|
|
5
|
+
*/
|
|
6
|
+
import type { EventSpecResponse, FetchEventSpecParams } from "./AvoEventSpecFetchTypes";
|
|
7
|
+
/**
|
|
8
|
+
* EventSpecFetcher handles fetching event specifications from the Avo API.
|
|
9
|
+
*
|
|
10
|
+
* Endpoint: GET /getEventSpec
|
|
11
|
+
* Base URL: https://us-central1-avo-web-app.cloudfunctions.net
|
|
12
|
+
*/
|
|
13
|
+
export declare class AvoEventSpecFetcher {
|
|
14
|
+
/** Base URL for the event spec API */
|
|
15
|
+
private readonly baseUrl;
|
|
16
|
+
/** Network timeout in milliseconds */
|
|
17
|
+
private readonly timeout;
|
|
18
|
+
/** In-flight requests to prevent duplicate fetches */
|
|
19
|
+
private inFlightRequests;
|
|
20
|
+
/** Whether to log debug information */
|
|
21
|
+
private readonly shouldLog;
|
|
22
|
+
/** Environment name */
|
|
23
|
+
private readonly env;
|
|
24
|
+
constructor(timeout: number | undefined, shouldLog: boolean | undefined, env: string, baseUrl?: string);
|
|
25
|
+
/** Generates a unique key for tracking in-flight requests. */
|
|
26
|
+
private generateRequestKey;
|
|
27
|
+
/**
|
|
28
|
+
* Fetches an event specification from the API.
|
|
29
|
+
*
|
|
30
|
+
* Returns null if:
|
|
31
|
+
* - The network request fails
|
|
32
|
+
* - The response has an invalid status code (non-200)
|
|
33
|
+
* - The response is invalid or malformed
|
|
34
|
+
* - The request times out
|
|
35
|
+
*
|
|
36
|
+
* This method gracefully degrades - failures do not throw errors.
|
|
37
|
+
* When null is returned, validation should be skipped for that event.
|
|
38
|
+
*/
|
|
39
|
+
fetch(params: FetchEventSpecParams): Promise<EventSpecResponse | null>;
|
|
40
|
+
/** Internal fetch implementation. */
|
|
41
|
+
private fetchInternal;
|
|
42
|
+
/** Builds the complete URL with query parameters. */
|
|
43
|
+
private buildUrl;
|
|
44
|
+
/**
|
|
45
|
+
* Makes an HTTP GET request using XMLHttpRequest.
|
|
46
|
+
* Returns the parsed JSON response or null on failure.
|
|
47
|
+
*/
|
|
48
|
+
private makeRequest;
|
|
49
|
+
/**
|
|
50
|
+
* Basic shape check - ensures response has the minimum expected structure.
|
|
51
|
+
* Does not perform deep validation of all fields.
|
|
52
|
+
*/
|
|
53
|
+
private hasExpectedShape;
|
|
54
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EventValidator - Client-side validation of tracking events against the Avo Tracking Plan.
|
|
3
|
+
*
|
|
4
|
+
* This module handles:
|
|
5
|
+
* 1. Matching: Identifying the most likely Event and Variant from specs
|
|
6
|
+
* 2. Validation: Checking runtime properties against the matched spec rules
|
|
7
|
+
*/
|
|
8
|
+
import type { EventSpecResponse, EventSpec, EventSpecMetadata, VariantSpec } from "./AvoEventSpecFetchTypes";
|
|
9
|
+
/**
|
|
10
|
+
* Validation issue codes for client-side validation.
|
|
11
|
+
*/
|
|
12
|
+
export type ValidationIssueCode = "RequiredMissing" | "ValueBelowMin" | "ValueAboveMax" | "RegexMismatch" | "NotInAllowedValues" | "UnexpectedProperty" | "UnexpectedEvent" | "TypeMismatch";
|
|
13
|
+
/**
|
|
14
|
+
* Represents a validation issue found during event validation.
|
|
15
|
+
*
|
|
16
|
+
* Note: We do NOT include the received value to avoid sending user data to the backend.
|
|
17
|
+
* The backend can correlate issues with encrypted values via propertyName matching
|
|
18
|
+
* against eventProperties.encryptedPropertyValue in the payload.
|
|
19
|
+
*/
|
|
20
|
+
export interface ValidationIssue {
|
|
21
|
+
/** The type of validation issue */
|
|
22
|
+
code: ValidationIssueCode;
|
|
23
|
+
/** Property ID (preferred over name) */
|
|
24
|
+
propertyId?: string;
|
|
25
|
+
/** Property name - used to correlate with encryptedPropertyValue in eventProperties */
|
|
26
|
+
propertyName?: string;
|
|
27
|
+
/** Expected value for the validation rule (from spec, not user data) */
|
|
28
|
+
expected?: string | number | boolean;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Result of the validation process.
|
|
32
|
+
*/
|
|
33
|
+
export interface ValidationResult {
|
|
34
|
+
/** ID of the matched event, null if no match found */
|
|
35
|
+
eventId: string | null;
|
|
36
|
+
/** ID of the matched variant, null if base event or no match */
|
|
37
|
+
variantId: string | null;
|
|
38
|
+
/** Event spec metadata from the response */
|
|
39
|
+
eventSpecMetadata: EventSpecMetadata;
|
|
40
|
+
/** List of all validation issues found */
|
|
41
|
+
validationErrors: ValidationIssue[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Runtime property value - can be any JSON-compatible type
|
|
45
|
+
*/
|
|
46
|
+
export type RuntimePropertyValue = string | number | boolean | null | undefined | object | Array<any>;
|
|
47
|
+
/**
|
|
48
|
+
* Runtime properties map
|
|
49
|
+
*/
|
|
50
|
+
export type RuntimeProperties = Record<string, RuntimePropertyValue>;
|
|
51
|
+
/**
|
|
52
|
+
* Internal match result for scoring
|
|
53
|
+
*/
|
|
54
|
+
interface MatchCandidate {
|
|
55
|
+
event: EventSpec;
|
|
56
|
+
variant: VariantSpec | null;
|
|
57
|
+
score: number;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Validates a runtime event against the EventSpecResponse.
|
|
61
|
+
*
|
|
62
|
+
* @param eventName - The event name observed at runtime
|
|
63
|
+
* @param properties - The properties observed at runtime
|
|
64
|
+
* @param specResponse - The EventSpecResponse from the backend
|
|
65
|
+
* @returns ValidationResult with match info and any validation errors
|
|
66
|
+
*/
|
|
67
|
+
export declare function validateEvent(eventName: string, properties: RuntimeProperties, specResponse: EventSpecResponse): ValidationResult;
|
|
68
|
+
/**
|
|
69
|
+
* Finds the closest matching Event and Variant for the runtime data.
|
|
70
|
+
*
|
|
71
|
+
* @param eventName - Runtime event name
|
|
72
|
+
* @param properties - Runtime properties
|
|
73
|
+
* @param events - Array of EventSpecs to match against
|
|
74
|
+
* @returns The best matching event and variant, or null if no match
|
|
75
|
+
*/
|
|
76
|
+
export declare function findClosestMatch(eventName: string, properties: RuntimeProperties, events: EventSpec[]): MatchCandidate | null;
|
|
77
|
+
/**
|
|
78
|
+
* Validates runtime properties against the matched event and variant specs.
|
|
79
|
+
*
|
|
80
|
+
* @param properties - Runtime properties
|
|
81
|
+
* @param event - Matched EventSpec
|
|
82
|
+
* @param variant - Matched VariantSpec (or null for base event)
|
|
83
|
+
* @returns Array of validation issues found
|
|
84
|
+
*/
|
|
85
|
+
export declare function validateProperties(properties: RuntimeProperties, event: EventSpec, variant: VariantSpec | null): ValidationIssue[];
|
|
86
|
+
/**
|
|
87
|
+
* Quick check if an event exists in the spec (without full validation).
|
|
88
|
+
*/
|
|
89
|
+
export declare function eventExistsInSpec(eventName: string, specResponse: EventSpecResponse): boolean;
|
|
90
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { EventSpec } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* EventSpecCache implements a dual-condition cache with LRU eviction.
|
|
4
|
+
*
|
|
5
|
+
* Cache Policy:
|
|
6
|
+
* - Entries expire after 5 minutes OR 50 events, whichever comes first
|
|
7
|
+
* - When 50 events are processed, the oldest cached entry is evicted
|
|
8
|
+
* - Each cache entry tracks: spec, timestamp, and eventCount
|
|
9
|
+
*
|
|
10
|
+
* Cache Key Format: ${schemaId}:${sourceId}:${eventName}:${branchId}
|
|
11
|
+
*/
|
|
12
|
+
export declare class EventSpecCache {
|
|
13
|
+
/** Cache storage: key -> CacheEntry */
|
|
14
|
+
private cache;
|
|
15
|
+
/** Time-to-live in milliseconds (5 minutes) */
|
|
16
|
+
private readonly TTL_MS;
|
|
17
|
+
/** Maximum event count before rotating cache (50 events) */
|
|
18
|
+
private readonly MAX_EVENT_COUNT;
|
|
19
|
+
/** Global event counter to track when to rotate cache */
|
|
20
|
+
private globalEventCount;
|
|
21
|
+
/** Whether to log debug information */
|
|
22
|
+
private readonly shouldLog;
|
|
23
|
+
constructor(shouldLog?: boolean);
|
|
24
|
+
/**
|
|
25
|
+
* Generates a cache key from the provided parameters.
|
|
26
|
+
*/
|
|
27
|
+
private generateKey;
|
|
28
|
+
/**
|
|
29
|
+
* Retrieves an event spec from the cache if it exists and is valid.
|
|
30
|
+
* Returns null if the entry is missing, expired, or has exceeded event count.
|
|
31
|
+
*/
|
|
32
|
+
get(schemaId: string, sourceId: string, eventName: string, branchId: string): EventSpec | null;
|
|
33
|
+
/**
|
|
34
|
+
* Stores an event spec in the cache.
|
|
35
|
+
*/
|
|
36
|
+
set(schemaId: string, sourceId: string, eventName: string, branchId: string, spec: EventSpec): void;
|
|
37
|
+
/**
|
|
38
|
+
* Increments the event count for all cached entries and triggers
|
|
39
|
+
* eviction if the global event count reaches MAX_EVENT_COUNT.
|
|
40
|
+
*/
|
|
41
|
+
incrementEventCount(): void;
|
|
42
|
+
/**
|
|
43
|
+
* Determines if a cache entry should be evicted based on:
|
|
44
|
+
* - Age (older than 5 minutes)
|
|
45
|
+
* - Event count (50 or more events processed)
|
|
46
|
+
*/
|
|
47
|
+
private shouldEvict;
|
|
48
|
+
/**
|
|
49
|
+
* Evicts the oldest cache entry based on timestamp.
|
|
50
|
+
* This implements the LRU (Least Recently Used) eviction policy.
|
|
51
|
+
*/
|
|
52
|
+
private evictOldest;
|
|
53
|
+
/**
|
|
54
|
+
* Clears all cached entries. Useful for testing.
|
|
55
|
+
*/
|
|
56
|
+
clear(): void;
|
|
57
|
+
/**
|
|
58
|
+
* Returns the current size of the cache.
|
|
59
|
+
*/
|
|
60
|
+
size(): number;
|
|
61
|
+
/**
|
|
62
|
+
* Returns cache statistics for debugging.
|
|
63
|
+
*/
|
|
64
|
+
getStats(): {
|
|
65
|
+
size: number;
|
|
66
|
+
globalEventCount: number;
|
|
67
|
+
entries: Array<{
|
|
68
|
+
key: string;
|
|
69
|
+
age: number;
|
|
70
|
+
eventCount: number;
|
|
71
|
+
}>;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents the specification for a single property in an event.
|
|
3
|
+
* Supports recursive nesting for object types.
|
|
4
|
+
*/
|
|
5
|
+
export interface PropertySpec {
|
|
6
|
+
/** Type - can be a primitive type string OR a nested object structure (recursive) */
|
|
7
|
+
t: string | Record<string, PropertySpec>;
|
|
8
|
+
/** Required flag */
|
|
9
|
+
r: boolean;
|
|
10
|
+
/** Is list (array) flag */
|
|
11
|
+
l?: boolean;
|
|
12
|
+
/** Minimum value (for numeric types) */
|
|
13
|
+
min?: number;
|
|
14
|
+
/** Maximum value (for numeric types) */
|
|
15
|
+
max?: number;
|
|
16
|
+
/** Enum values (allowed values for the property) */
|
|
17
|
+
v?: string[];
|
|
18
|
+
/** Regex pattern for string validation */
|
|
19
|
+
rx?: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Represents a variant of an event with additional or modified properties.
|
|
23
|
+
*/
|
|
24
|
+
export interface EventVariant {
|
|
25
|
+
/** Unique identifier for this variant */
|
|
26
|
+
variantId: string;
|
|
27
|
+
/** Suffix to append to the base event name */
|
|
28
|
+
nameSuffix: string;
|
|
29
|
+
/** Event ID for this variant */
|
|
30
|
+
eventId: string;
|
|
31
|
+
/** Properties specific to this variant */
|
|
32
|
+
props: Record<string, PropertySpec>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Represents the complete specification for an event, including base event
|
|
36
|
+
* and optional variants.
|
|
37
|
+
*/
|
|
38
|
+
export interface EventSpec {
|
|
39
|
+
/** Base event specification */
|
|
40
|
+
baseEvent: {
|
|
41
|
+
/** Name of the event */
|
|
42
|
+
name: string;
|
|
43
|
+
/** Unique identifier for the event */
|
|
44
|
+
id: string;
|
|
45
|
+
/** Properties defined for the base event */
|
|
46
|
+
props: Record<string, PropertySpec>;
|
|
47
|
+
};
|
|
48
|
+
/** Optional variants of this event with additional/modified properties */
|
|
49
|
+
variants?: EventVariant[];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Cache entry for storing event specs with metadata.
|
|
53
|
+
*/
|
|
54
|
+
export interface EventSpecCacheEntry {
|
|
55
|
+
/** The cached event specification */
|
|
56
|
+
spec: EventSpec;
|
|
57
|
+
/** Timestamp when this entry was cached */
|
|
58
|
+
timestamp: number;
|
|
59
|
+
/** Number of events processed since this entry was cached */
|
|
60
|
+
eventCount: number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Parameters for fetching event specifications from the API.
|
|
64
|
+
*/
|
|
65
|
+
export interface FetchEventSpecParams {
|
|
66
|
+
/** The workspace/schema ID */
|
|
67
|
+
schemaId: string;
|
|
68
|
+
/** The source ID */
|
|
69
|
+
sourceId: string;
|
|
70
|
+
/** The name of the event */
|
|
71
|
+
eventName: string;
|
|
72
|
+
/** The branch ID (defaults to "main") */
|
|
73
|
+
branchId?: string;
|
|
74
|
+
}
|