@traffical/core 0.1.2
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/dist/dedup/decision-dedup.d.ts +74 -0
- package/dist/dedup/decision-dedup.d.ts.map +1 -0
- package/dist/dedup/decision-dedup.js +132 -0
- package/dist/dedup/decision-dedup.js.map +1 -0
- package/dist/dedup/index.d.ts +5 -0
- package/dist/dedup/index.d.ts.map +1 -0
- package/dist/dedup/index.js +5 -0
- package/dist/dedup/index.js.map +1 -0
- package/dist/edge/client.d.ts +109 -0
- package/dist/edge/client.d.ts.map +1 -0
- package/dist/edge/client.js +154 -0
- package/dist/edge/client.js.map +1 -0
- package/dist/edge/index.d.ts +7 -0
- package/dist/edge/index.d.ts.map +1 -0
- package/dist/edge/index.js +7 -0
- package/dist/edge/index.js.map +1 -0
- package/dist/hashing/bucket.d.ts +56 -0
- package/dist/hashing/bucket.d.ts.map +1 -0
- package/dist/hashing/bucket.js +89 -0
- package/dist/hashing/bucket.js.map +1 -0
- package/dist/hashing/fnv1a.d.ts +17 -0
- package/dist/hashing/fnv1a.d.ts.map +1 -0
- package/dist/hashing/fnv1a.js +27 -0
- package/dist/hashing/fnv1a.js.map +1 -0
- package/dist/hashing/index.d.ts +8 -0
- package/dist/hashing/index.d.ts.map +1 -0
- package/dist/hashing/index.js +8 -0
- package/dist/hashing/index.js.map +1 -0
- package/dist/ids/index.d.ts +83 -0
- package/dist/ids/index.d.ts.map +1 -0
- package/dist/ids/index.js +165 -0
- package/dist/ids/index.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/resolution/conditions.d.ts +81 -0
- package/dist/resolution/conditions.d.ts.map +1 -0
- package/dist/resolution/conditions.js +197 -0
- package/dist/resolution/conditions.js.map +1 -0
- package/dist/resolution/engine.d.ts +54 -0
- package/dist/resolution/engine.d.ts.map +1 -0
- package/dist/resolution/engine.js +382 -0
- package/dist/resolution/engine.js.map +1 -0
- package/dist/resolution/index.d.ts +8 -0
- package/dist/resolution/index.d.ts.map +1 -0
- package/dist/resolution/index.js +10 -0
- package/dist/resolution/index.js.map +1 -0
- package/dist/types/index.d.ts +440 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +8 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +51 -0
- package/src/dedup/decision-dedup.ts +175 -0
- package/src/dedup/index.ts +6 -0
- package/src/edge/client.ts +256 -0
- package/src/edge/index.ts +16 -0
- package/src/hashing/bucket.ts +115 -0
- package/src/hashing/fnv1a.test.ts +87 -0
- package/src/hashing/fnv1a.ts +31 -0
- package/src/hashing/index.ts +15 -0
- package/src/ids/index.ts +221 -0
- package/src/index.ts +136 -0
- package/src/resolution/conditions.ts +253 -0
- package/src/resolution/engine.test.ts +242 -0
- package/src/resolution/engine.ts +480 -0
- package/src/resolution/index.ts +32 -0
- package/src/types/index.ts +508 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bucket Computation
|
|
3
|
+
*
|
|
4
|
+
* Deterministic bucket assignment for traffic splitting.
|
|
5
|
+
* The bucket is computed as: hash(unitKeyValue + ":" + layerId) % bucketCount
|
|
6
|
+
*
|
|
7
|
+
* This ensures:
|
|
8
|
+
* - Same user always gets same bucket for a given layer
|
|
9
|
+
* - Different layers can have independent bucketing (orthogonality)
|
|
10
|
+
* - Deterministic results across SDK and server
|
|
11
|
+
*/
|
|
12
|
+
import { fnv1a } from "./fnv1a.js";
|
|
13
|
+
/**
|
|
14
|
+
* Computes the bucket for a given unit and layer.
|
|
15
|
+
*
|
|
16
|
+
* @param unitKeyValue - The value of the unit key (e.g., userId value)
|
|
17
|
+
* @param layerId - The layer ID for orthogonal bucketing
|
|
18
|
+
* @param bucketCount - Total number of buckets (e.g., 1000)
|
|
19
|
+
* @returns Bucket number in range [0, bucketCount - 1]
|
|
20
|
+
*/
|
|
21
|
+
export function computeBucket(unitKeyValue, layerId, bucketCount) {
|
|
22
|
+
// Concatenate unit key and layer ID with separator
|
|
23
|
+
const input = `${unitKeyValue}:${layerId}`;
|
|
24
|
+
// Use FNV-1a for consistent hashing
|
|
25
|
+
const hash = fnv1a(input);
|
|
26
|
+
// Map to bucket range
|
|
27
|
+
return hash % bucketCount;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Checks if a bucket falls within a range.
|
|
31
|
+
*
|
|
32
|
+
* @param bucket - The computed bucket
|
|
33
|
+
* @param range - [start, end] inclusive range
|
|
34
|
+
* @returns True if bucket is in range
|
|
35
|
+
*/
|
|
36
|
+
export function isInBucketRange(bucket, range) {
|
|
37
|
+
return bucket >= range[0] && bucket <= range[1];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Finds which allocation matches a given bucket.
|
|
41
|
+
*
|
|
42
|
+
* @param bucket - The computed bucket
|
|
43
|
+
* @param allocations - Array of allocations with bucket ranges
|
|
44
|
+
* @returns The matching allocation, or null if none match
|
|
45
|
+
*/
|
|
46
|
+
export function findMatchingAllocation(bucket, allocations) {
|
|
47
|
+
for (const allocation of allocations) {
|
|
48
|
+
if (isInBucketRange(bucket, allocation.bucketRange)) {
|
|
49
|
+
return allocation;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Converts a percentage to a bucket range.
|
|
56
|
+
*
|
|
57
|
+
* @param percentage - Traffic percentage (0-100)
|
|
58
|
+
* @param bucketCount - Total buckets
|
|
59
|
+
* @param startBucket - Starting bucket (default 0)
|
|
60
|
+
* @returns [start, end] bucket range
|
|
61
|
+
*/
|
|
62
|
+
export function percentageToBucketRange(percentage, bucketCount, startBucket = 0) {
|
|
63
|
+
const bucketsNeeded = Math.floor((percentage / 100) * bucketCount);
|
|
64
|
+
const endBucket = Math.min(startBucket + bucketsNeeded - 1, bucketCount - 1);
|
|
65
|
+
return [startBucket, endBucket];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Creates non-overlapping bucket ranges for multiple variants.
|
|
69
|
+
*
|
|
70
|
+
* @param percentages - Array of percentages that should sum to <= 100
|
|
71
|
+
* @param bucketCount - Total buckets
|
|
72
|
+
* @returns Array of [start, end] bucket ranges
|
|
73
|
+
*/
|
|
74
|
+
export function createBucketRanges(percentages, bucketCount) {
|
|
75
|
+
const ranges = [];
|
|
76
|
+
let currentBucket = 0;
|
|
77
|
+
for (const percentage of percentages) {
|
|
78
|
+
if (percentage <= 0)
|
|
79
|
+
continue;
|
|
80
|
+
const bucketsNeeded = Math.floor((percentage / 100) * bucketCount);
|
|
81
|
+
if (bucketsNeeded > 0) {
|
|
82
|
+
const endBucket = currentBucket + bucketsNeeded - 1;
|
|
83
|
+
ranges.push([currentBucket, endBucket]);
|
|
84
|
+
currentBucket = endBucket + 1;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return ranges;
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=bucket.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bucket.js","sourceRoot":"","sources":["../../src/hashing/bucket.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAC3B,YAAoB,EACpB,OAAe,EACf,WAAmB;IAEnB,mDAAmD;IACnD,MAAM,KAAK,GAAG,GAAG,YAAY,IAAI,OAAO,EAAE,CAAC;IAE3C,oCAAoC;IACpC,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;IAE1B,sBAAsB;IACtB,OAAO,IAAI,GAAG,WAAW,CAAC;AAC5B,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAC7B,MAAc,EACd,KAAuB;IAEvB,OAAO,MAAM,IAAI,KAAK,CAAC,CAAC,CAAC,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SAAS,GAAG,CAAC,CAAC;QAChC,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FNV-1a Hash Function
|
|
3
|
+
*
|
|
4
|
+
* A simple, fast hash function with good distribution properties.
|
|
5
|
+
* Used by Google's experimentation system for bucket assignment.
|
|
6
|
+
*
|
|
7
|
+
* This implementation produces consistent results across all platforms
|
|
8
|
+
* and is the canonical hash for Traffical SDKs.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Computes the FNV-1a hash of a string.
|
|
12
|
+
*
|
|
13
|
+
* @param input - The string to hash
|
|
14
|
+
* @returns Unsigned 32-bit integer hash
|
|
15
|
+
*/
|
|
16
|
+
export declare function fnv1a(input: string): number;
|
|
17
|
+
//# sourceMappingURL=fnv1a.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fnv1a.d.ts","sourceRoot":"","sources":["../../src/hashing/fnv1a.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAKH;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAU3C"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FNV-1a Hash Function
|
|
3
|
+
*
|
|
4
|
+
* A simple, fast hash function with good distribution properties.
|
|
5
|
+
* Used by Google's experimentation system for bucket assignment.
|
|
6
|
+
*
|
|
7
|
+
* This implementation produces consistent results across all platforms
|
|
8
|
+
* and is the canonical hash for Traffical SDKs.
|
|
9
|
+
*/
|
|
10
|
+
const FNV_OFFSET_BASIS = 2166136261;
|
|
11
|
+
const FNV_PRIME = 16777619;
|
|
12
|
+
/**
|
|
13
|
+
* Computes the FNV-1a hash of a string.
|
|
14
|
+
*
|
|
15
|
+
* @param input - The string to hash
|
|
16
|
+
* @returns Unsigned 32-bit integer hash
|
|
17
|
+
*/
|
|
18
|
+
export function fnv1a(input) {
|
|
19
|
+
let hash = FNV_OFFSET_BASIS;
|
|
20
|
+
for (let i = 0; i < input.length; i++) {
|
|
21
|
+
hash ^= input.charCodeAt(i);
|
|
22
|
+
hash = Math.imul(hash, FNV_PRIME);
|
|
23
|
+
}
|
|
24
|
+
// Convert to unsigned 32-bit integer
|
|
25
|
+
return hash >>> 0;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=fnv1a.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fnv1a.js","sourceRoot":"","sources":["../../src/hashing/fnv1a.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,gBAAgB,GAAG,UAAU,CAAC;AACpC,MAAM,SAAS,GAAG,QAAQ,CAAC;AAE3B;;;;;GAKG;AACH,MAAM,UAAU,KAAK,CAAC,KAAa;IACjC,IAAI,IAAI,GAAG,gBAAgB,CAAC;IAE5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACtC,IAAI,IAAI,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QAC5B,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;IACpC,CAAC;IAED,qCAAqC;IACrC,OAAO,IAAI,KAAK,CAAC,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hashing Module
|
|
3
|
+
*
|
|
4
|
+
* Exports all hashing-related functions for deterministic bucket assignment.
|
|
5
|
+
*/
|
|
6
|
+
export { fnv1a } from "./fnv1a.js";
|
|
7
|
+
export { computeBucket, isInBucketRange, findMatchingAllocation, percentageToBucketRange, createBucketRanges, } from "./bucket.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hashing/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EACL,aAAa,EACb,eAAe,EACf,sBAAsB,EACtB,uBAAuB,EACvB,kBAAkB,GACnB,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hashing Module
|
|
3
|
+
*
|
|
4
|
+
* Exports all hashing-related functions for deterministic bucket assignment.
|
|
5
|
+
*/
|
|
6
|
+
export { fnv1a } from "./fnv1a.js";
|
|
7
|
+
export { computeBucket, isInBucketRange, findMatchingAllocation, percentageToBucketRange, createBucketRanges, } from "./bucket.js";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hashing/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EACL,aAAa,EACb,eAAe,EACf,sBAAsB,EACtB,uBAAuB,EACvB,kBAAkB,GACnB,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ID Generation Utilities
|
|
3
|
+
*
|
|
4
|
+
* Provides consistent ID generation with type prefixes for all entities and events.
|
|
5
|
+
*
|
|
6
|
+
* Entity IDs: 8-character NanoID with prefix (e.g., "proj_hVF1cCoC")
|
|
7
|
+
* - Compact and URL-friendly
|
|
8
|
+
* - 64^8 = 281 trillion combinations
|
|
9
|
+
* - With DB constraints, collisions are handled via retry
|
|
10
|
+
*
|
|
11
|
+
* Event IDs: ULID with prefix (e.g., "dec_01JHFK1WWMMG7M0XPEBTYXZEBW")
|
|
12
|
+
* - Lexicographically sortable (time-ordered)
|
|
13
|
+
* - Contains millisecond timestamp for analytics
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Entity ID prefixes for each entity type.
|
|
17
|
+
*/
|
|
18
|
+
export type EntityIdPrefix = "org" | "proj" | "env" | "ns" | "lay" | "pol" | "alloc" | "param" | "dom" | "ovr" | "ak";
|
|
19
|
+
/**
|
|
20
|
+
* Event ID prefixes for each event type.
|
|
21
|
+
*/
|
|
22
|
+
export type EventIdPrefix = "dec" | "exp" | "trk";
|
|
23
|
+
/**
|
|
24
|
+
* Generates a prefixed 8-char NanoID for the specified entity type.
|
|
25
|
+
*
|
|
26
|
+
* @param prefix - The entity type prefix
|
|
27
|
+
* @returns A prefixed NanoID string (e.g., "proj_hVF1cCoC")
|
|
28
|
+
*/
|
|
29
|
+
export declare function generateEntityId(prefix: EntityIdPrefix): string;
|
|
30
|
+
/**
|
|
31
|
+
* Generates a prefixed ULID for the specified event type.
|
|
32
|
+
* Events use ULID for time-sortability in analytics.
|
|
33
|
+
*
|
|
34
|
+
* @param prefix - The event type prefix
|
|
35
|
+
* @returns A prefixed ULID string (e.g., "dec_01JHFK1WWMMG7M0XPEBTYXZEBW")
|
|
36
|
+
*/
|
|
37
|
+
export declare function generateEventId(prefix: EventIdPrefix): string;
|
|
38
|
+
/**
|
|
39
|
+
* Generates a plain 8-char NanoID without prefix.
|
|
40
|
+
* Used for internal IDs that don't need type identification.
|
|
41
|
+
*/
|
|
42
|
+
export declare function generateShortId(): string;
|
|
43
|
+
/** Generates an Organization ID with "org_" prefix */
|
|
44
|
+
export declare function generateOrgId(): string;
|
|
45
|
+
/** Generates a Project ID with "proj_" prefix */
|
|
46
|
+
export declare function generateProjectId(): string;
|
|
47
|
+
/** Generates an Environment ID with "env_" prefix */
|
|
48
|
+
export declare function generateEnvironmentId(): string;
|
|
49
|
+
/** Generates a Namespace ID with "ns_" prefix */
|
|
50
|
+
export declare function generateNamespaceId(): string;
|
|
51
|
+
/** Generates a Layer ID with "lay_" prefix */
|
|
52
|
+
export declare function generateLayerId(): string;
|
|
53
|
+
/** Generates a Policy ID with "pol_" prefix */
|
|
54
|
+
export declare function generatePolicyId(): string;
|
|
55
|
+
/** Generates an Allocation ID with "alloc_" prefix */
|
|
56
|
+
export declare function generateAllocationId(): string;
|
|
57
|
+
/** Generates a Parameter ID with "param_" prefix */
|
|
58
|
+
export declare function generateParameterId(): string;
|
|
59
|
+
/** Generates a DOM Binding ID with "dom_" prefix */
|
|
60
|
+
export declare function generateDomBindingId(): string;
|
|
61
|
+
/** Generates an Environment Override ID with "ovr_" prefix */
|
|
62
|
+
export declare function generateOverrideId(): string;
|
|
63
|
+
/** Generates an API Key ID with "ak_" prefix */
|
|
64
|
+
export declare function generateApiKeyId(): string;
|
|
65
|
+
/** Generates a Decision event ID with "dec_" prefix (ULID) */
|
|
66
|
+
export declare function generateDecisionId(): string;
|
|
67
|
+
/** Generates an Exposure event ID with "exp_" prefix (ULID) */
|
|
68
|
+
export declare function generateExposureId(): string;
|
|
69
|
+
/** Generates a Track event ID with "trk_" prefix (ULID) */
|
|
70
|
+
export declare function generateTrackEventId(): string;
|
|
71
|
+
/**
|
|
72
|
+
* Extracts the timestamp from a ULID-based ID.
|
|
73
|
+
* Only works for event IDs (ULID format).
|
|
74
|
+
*
|
|
75
|
+
* @param id - A prefixed ULID (e.g., "dec_01JHFK1WWMMG7M0XPEBTYXZEBW")
|
|
76
|
+
* @returns The timestamp as a Date, or null if invalid
|
|
77
|
+
*/
|
|
78
|
+
export declare function getIdTimestamp(id: string): Date | null;
|
|
79
|
+
/**
|
|
80
|
+
* @deprecated Use getIdTimestamp instead
|
|
81
|
+
*/
|
|
82
|
+
export declare function getEventIdTimestamp(eventId: string): Date | null;
|
|
83
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ids/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AA8BH;;GAEG;AACH,MAAM,MAAM,cAAc,GACtB,KAAK,G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EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CA2BtD;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAEhE"}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ID Generation Utilities
|
|
3
|
+
*
|
|
4
|
+
* Provides consistent ID generation with type prefixes for all entities and events.
|
|
5
|
+
*
|
|
6
|
+
* Entity IDs: 8-character NanoID with prefix (e.g., "proj_hVF1cCoC")
|
|
7
|
+
* - Compact and URL-friendly
|
|
8
|
+
* - 64^8 = 281 trillion combinations
|
|
9
|
+
* - With DB constraints, collisions are handled via retry
|
|
10
|
+
*
|
|
11
|
+
* Event IDs: ULID with prefix (e.g., "dec_01JHFK1WWMMG7M0XPEBTYXZEBW")
|
|
12
|
+
* - Lexicographically sortable (time-ordered)
|
|
13
|
+
* - Contains millisecond timestamp for analytics
|
|
14
|
+
*/
|
|
15
|
+
import { customAlphabet } from "nanoid";
|
|
16
|
+
import { ulid } from "ulid";
|
|
17
|
+
// =============================================================================
|
|
18
|
+
// NanoID Configuration
|
|
19
|
+
// =============================================================================
|
|
20
|
+
/**
|
|
21
|
+
* URL-safe alphabet for NanoID (64 characters).
|
|
22
|
+
* Includes: 0-9, A-Z, a-z (no special chars to avoid URL encoding issues)
|
|
23
|
+
*/
|
|
24
|
+
const NANOID_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
25
|
+
/**
|
|
26
|
+
* Default length for entity IDs (without prefix).
|
|
27
|
+
* 64^8 = 281,474,976,710,656 (~281 trillion) combinations.
|
|
28
|
+
*/
|
|
29
|
+
const ENTITY_ID_LENGTH = 8;
|
|
30
|
+
/**
|
|
31
|
+
* NanoID generator with custom alphabet.
|
|
32
|
+
*/
|
|
33
|
+
const nanoid = customAlphabet(NANOID_ALPHABET, ENTITY_ID_LENGTH);
|
|
34
|
+
// =============================================================================
|
|
35
|
+
// Generic ID Generation
|
|
36
|
+
// =============================================================================
|
|
37
|
+
/**
|
|
38
|
+
* Generates a prefixed 8-char NanoID for the specified entity type.
|
|
39
|
+
*
|
|
40
|
+
* @param prefix - The entity type prefix
|
|
41
|
+
* @returns A prefixed NanoID string (e.g., "proj_hVF1cCoC")
|
|
42
|
+
*/
|
|
43
|
+
export function generateEntityId(prefix) {
|
|
44
|
+
return `${prefix}_${nanoid()}`;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Generates a prefixed ULID for the specified event type.
|
|
48
|
+
* Events use ULID for time-sortability in analytics.
|
|
49
|
+
*
|
|
50
|
+
* @param prefix - The event type prefix
|
|
51
|
+
* @returns A prefixed ULID string (e.g., "dec_01JHFK1WWMMG7M0XPEBTYXZEBW")
|
|
52
|
+
*/
|
|
53
|
+
export function generateEventId(prefix) {
|
|
54
|
+
return `${prefix}_${ulid()}`;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Generates a plain 8-char NanoID without prefix.
|
|
58
|
+
* Used for internal IDs that don't need type identification.
|
|
59
|
+
*/
|
|
60
|
+
export function generateShortId() {
|
|
61
|
+
return nanoid();
|
|
62
|
+
}
|
|
63
|
+
// =============================================================================
|
|
64
|
+
// Entity ID Convenience Functions
|
|
65
|
+
// =============================================================================
|
|
66
|
+
/** Generates an Organization ID with "org_" prefix */
|
|
67
|
+
export function generateOrgId() {
|
|
68
|
+
return generateEntityId("org");
|
|
69
|
+
}
|
|
70
|
+
/** Generates a Project ID with "proj_" prefix */
|
|
71
|
+
export function generateProjectId() {
|
|
72
|
+
return generateEntityId("proj");
|
|
73
|
+
}
|
|
74
|
+
/** Generates an Environment ID with "env_" prefix */
|
|
75
|
+
export function generateEnvironmentId() {
|
|
76
|
+
return generateEntityId("env");
|
|
77
|
+
}
|
|
78
|
+
/** Generates a Namespace ID with "ns_" prefix */
|
|
79
|
+
export function generateNamespaceId() {
|
|
80
|
+
return generateEntityId("ns");
|
|
81
|
+
}
|
|
82
|
+
/** Generates a Layer ID with "lay_" prefix */
|
|
83
|
+
export function generateLayerId() {
|
|
84
|
+
return generateEntityId("lay");
|
|
85
|
+
}
|
|
86
|
+
/** Generates a Policy ID with "pol_" prefix */
|
|
87
|
+
export function generatePolicyId() {
|
|
88
|
+
return generateEntityId("pol");
|
|
89
|
+
}
|
|
90
|
+
/** Generates an Allocation ID with "alloc_" prefix */
|
|
91
|
+
export function generateAllocationId() {
|
|
92
|
+
return generateEntityId("alloc");
|
|
93
|
+
}
|
|
94
|
+
/** Generates a Parameter ID with "param_" prefix */
|
|
95
|
+
export function generateParameterId() {
|
|
96
|
+
return generateEntityId("param");
|
|
97
|
+
}
|
|
98
|
+
/** Generates a DOM Binding ID with "dom_" prefix */
|
|
99
|
+
export function generateDomBindingId() {
|
|
100
|
+
return generateEntityId("dom");
|
|
101
|
+
}
|
|
102
|
+
/** Generates an Environment Override ID with "ovr_" prefix */
|
|
103
|
+
export function generateOverrideId() {
|
|
104
|
+
return generateEntityId("ovr");
|
|
105
|
+
}
|
|
106
|
+
/** Generates an API Key ID with "ak_" prefix */
|
|
107
|
+
export function generateApiKeyId() {
|
|
108
|
+
return generateEntityId("ak");
|
|
109
|
+
}
|
|
110
|
+
// =============================================================================
|
|
111
|
+
// Event ID Convenience Functions (Keep ULID for time-sortability)
|
|
112
|
+
// =============================================================================
|
|
113
|
+
/** Generates a Decision event ID with "dec_" prefix (ULID) */
|
|
114
|
+
export function generateDecisionId() {
|
|
115
|
+
return generateEventId("dec");
|
|
116
|
+
}
|
|
117
|
+
/** Generates an Exposure event ID with "exp_" prefix (ULID) */
|
|
118
|
+
export function generateExposureId() {
|
|
119
|
+
return generateEventId("exp");
|
|
120
|
+
}
|
|
121
|
+
/** Generates a Track event ID with "trk_" prefix (ULID) */
|
|
122
|
+
export function generateTrackEventId() {
|
|
123
|
+
return generateEventId("trk");
|
|
124
|
+
}
|
|
125
|
+
// =============================================================================
|
|
126
|
+
// Utilities
|
|
127
|
+
// =============================================================================
|
|
128
|
+
/**
|
|
129
|
+
* Extracts the timestamp from a ULID-based ID.
|
|
130
|
+
* Only works for event IDs (ULID format).
|
|
131
|
+
*
|
|
132
|
+
* @param id - A prefixed ULID (e.g., "dec_01JHFK1WWMMG7M0XPEBTYXZEBW")
|
|
133
|
+
* @returns The timestamp as a Date, or null if invalid
|
|
134
|
+
*/
|
|
135
|
+
export function getIdTimestamp(id) {
|
|
136
|
+
// Extract the ULID part (after the prefix and underscore)
|
|
137
|
+
const parts = id.split("_");
|
|
138
|
+
if (parts.length < 2) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
const ulidPart = parts[1];
|
|
142
|
+
if (!ulidPart || ulidPart.length !== 26) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
// ULID timestamp is encoded in the first 10 characters (Crockford's Base32)
|
|
146
|
+
const ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
147
|
+
const TIME_LEN = 10;
|
|
148
|
+
let time = 0;
|
|
149
|
+
for (let i = 0; i < TIME_LEN; i++) {
|
|
150
|
+
const char = ulidPart.charAt(i).toUpperCase();
|
|
151
|
+
const index = ENCODING.indexOf(char);
|
|
152
|
+
if (index === -1) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
time = time * 32 + index;
|
|
156
|
+
}
|
|
157
|
+
return new Date(time);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* @deprecated Use getIdTimestamp instead
|
|
161
|
+
*/
|
|
162
|
+
export function getEventIdTimestamp(eventId) {
|
|
163
|
+
return getIdTimestamp(eventId);
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/ids/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,QAAQ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,KAAK,EAAE,EAAE,CAAC;QACxC,OAAO,IAAI,CAAC;IACd,CAAC;IAED,4EAA4E;IAC5E,MAAM,QAAQ,GAAG,kCAAkC,CAAC;IACpD,MAAM,QAAQ,GAAG,EAAE,CAAC;IAEpB,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,EAAE,CAAC,EAAE,EAAE,CAAC;QAClC,MAAM,IAAI,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;QAC9C,MAAM,KAAK,GAAG,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACrC,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;YACjB,OAAO,IAAI,CAAC;QACd,CAAC;QACD,IAAI,GAAG,IAAI,GAAG,EAAE,GAAG,KAAK,CAAC;IAC3B,CAAC;IAED,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC;AACxB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAAe;IACjD,OAAO,cAAc,CAAC,OAAO,CAAC,CAAC;AACjC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @traffical/core
|
|
3
|
+
*
|
|
4
|
+
* Pure TypeScript core for Traffical SDK.
|
|
5
|
+
* This package performs no I/O and can be used in any JavaScript environment.
|
|
6
|
+
*
|
|
7
|
+
* Key features:
|
|
8
|
+
* - Deterministic parameter resolution
|
|
9
|
+
* - FNV-1a hashing for bucket assignment
|
|
10
|
+
* - Condition evaluation for targeting
|
|
11
|
+
* - Defaults-based graceful degradation
|
|
12
|
+
*/
|
|
13
|
+
export type { Timestamp, Id, ParameterType, ParameterValue, Context, ConfigBundle, BundleHashingConfig, BundleParameter, BundleDOMBinding, BundleLayer, BundlePolicy, BundleAllocation, BundleCondition, PolicyState, PolicyKind, ConditionOperator, EntityConfig, EntityWeights, BundleEntityPolicyState, ParameterDefaults, DecisionResult, DecisionMetadata, LayerResolution, BaseEventFields, ExposureEvent, TrackEvent, TrackAttribution, DecisionEvent, TrackableEvent, TrafficalClientOptions, GetParamsOptions, DecideOptions, TrackOptions, } from "./types/index.js";
|
|
14
|
+
export { fnv1a, computeBucket, isInBucketRange, findMatchingAllocation, percentageToBucketRange, createBucketRanges, } from "./hashing/index.js";
|
|
15
|
+
export { resolveParameters, decide, getUnitKeyValue, evaluateCondition, evaluateConditions, eq, neq, inValues, notIn, gt, gte, lt, lte, contains, startsWith, endsWith, regex, exists, notExists, } from "./resolution/index.js";
|
|
16
|
+
export { DecisionDeduplicator, type DecisionDeduplicatorOptions, } from "./dedup/index.js";
|
|
17
|
+
export { generateEventId, generateDecisionId, generateExposureId, generateTrackEventId, generateEntityId, generateShortId, generateOrgId, generateProjectId, generateEnvironmentId, generateNamespaceId, generateLayerId, generatePolicyId, generateAllocationId, generateParameterId, generateDomBindingId, generateOverrideId, generateApiKeyId, getIdTimestamp, getEventIdTimestamp, // deprecated
|
|
18
|
+
type EventIdPrefix, type EntityIdPrefix, } from "./ids/index.js";
|
|
19
|
+
export { EdgeClient, createEdgeDecideRequest, type EdgeClientConfig, type EdgeDecideRequest, type EdgeDecideResponse, type EdgeBatchDecideRequest, type EdgeBatchDecideResponse, } from "./edge/index.js";
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,YAAY,EAEV,SAAS,EACT,EAAE,EACF,aAAa,EACb,cAAc,EACd,OAAO,EAEP,YAAY,EACZ,mBAAmB,EACnB,eAAe,EACf,gBAAgB,EAChB,WAAW,EACX,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,WAAW,EACX,UAAU,EACV,iBAAiB,EAEjB,YAA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|
|
|
1
|
+
/**
|
|
2
|
+
* @traffical/core
|
|
3
|
+
*
|
|
4
|
+
* Pure TypeScript core for Traffical SDK.
|
|
5
|
+
* This package performs no I/O and can be used in any JavaScript environment.
|
|
6
|
+
*
|
|
7
|
+
* Key features:
|
|
8
|
+
* - Deterministic parameter resolution
|
|
9
|
+
* - FNV-1a hashing for bucket assignment
|
|
10
|
+
* - Condition evaluation for targeting
|
|
11
|
+
* - Defaults-based graceful degradation
|
|
12
|
+
*/
|
|
13
|
+
// Hashing
|
|
14
|
+
export { fnv1a, computeBucket, isInBucketRange, findMatchingAllocation, percentageToBucketRange, createBucketRanges, } from "./hashing/index.js";
|
|
15
|
+
// Resolution
|
|
16
|
+
export { resolveParameters, decide, getUnitKeyValue, evaluateCondition, evaluateConditions,
|
|
17
|
+
// Condition builders
|
|
18
|
+
eq, neq, inValues, notIn, gt, gte, lt, lte, contains, startsWith, endsWith, regex, exists, notExists, } from "./resolution/index.js";
|
|
19
|
+
// Deduplication
|
|
20
|
+
export { DecisionDeduplicator, } from "./dedup/index.js";
|
|
21
|
+
// ID Generation
|
|
22
|
+
export {
|
|
23
|
+
// Event IDs (ULID - time-sortable)
|
|
24
|
+
generateEventId, generateDecisionId, generateExposureId, generateTrackEventId,
|
|
25
|
+
// Entity IDs (8-char NanoID)
|
|
26
|
+
generateEntityId, generateShortId, generateOrgId, generateProjectId, generateEnvironmentId, generateNamespaceId, generateLayerId, generatePolicyId, generateAllocationId, generateParameterId, generateDomBindingId, generateOverrideId, generateApiKeyId,
|
|
27
|
+
// Utilities
|
|
28
|
+
getIdTimestamp, getEventIdTimestamp, // deprecated
|
|
29
|
+
} from "./ids/index.js";
|
|
30
|
+
// Edge Client (for per-entity policies)
|
|
31
|
+
export { EdgeClient, createEdgeDecideRequest, } from "./edge/index.js";
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AA6CH,UAAU;AACV,OAAO,EACL,KAAK,EACL,aAAa,EACb,eAAe,EACf,sBAAsB,EACtB,uBAAuB,EACvB,kBAAkB,GACnB,MAAM,oBAAoB,CAAC;AAE5B,aAAa;AACb,OAAO,EACL,iBAAiB,EACjB,MAAM,EACN,eAAe,EACf,iBAAiB,EACjB,kBAAkB;AAClB,qBAAqB;AACrB,EAAE,EACF,GAAG,EACH,QAAQ,EACR,KAAK,EACL,EAAE,EACF,GAAG,EACH,EAAE,EACF,GAAG,EACH,QAAQ,EACR,UAAU,EACV,QAAQ,EACR,KAAK,EACL,MAAM,EACN,SAAS,GACV,MAAM,uBAAuB,CAAC;AAE/B,gBAAgB;AAChB,OAAO,EACL,oBAAoB,GAErB,MAAM,kBAAkB,CAAC;AAE1B,gBAAgB;AAChB,OAAO;AACL,mCAAmC;AACnC,eAAe,EACf,kBAAkB,EAClB,kBAAkB,EAClB,oBAAoB;AACpB,6BAA6B;AAC7B,gBAAgB,EAChB,eAAe,EACf,aAAa,EACb,iBAAiB,EACjB,qBAAqB,EACrB,mBAAmB,EACnB,eAAe,EACf,gBAAgB,EAChB,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,kBAAkB,EAClB,gBAAgB;AAChB,YAAY;AACZ,cAAc,EACd,mBAAmB,EAAE,aAAa;EAInC,MAAM,gBAAgB,CAAC;AAExB,wCAAwC;AACxC,OAAO,EACL,UAAU,EACV,uBAAuB,GAMxB,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Condition Evaluation
|
|
3
|
+
*
|
|
4
|
+
* Evaluates context predicates to determine policy eligibility.
|
|
5
|
+
* Conditions are AND-ed together: all must match for a policy to apply.
|
|
6
|
+
*/
|
|
7
|
+
import type { Context, BundleCondition } from "../types/index.js";
|
|
8
|
+
/**
|
|
9
|
+
* Evaluates a single condition against a context.
|
|
10
|
+
*
|
|
11
|
+
* @param condition - The condition to evaluate
|
|
12
|
+
* @param context - The context to evaluate against
|
|
13
|
+
* @returns True if the condition matches
|
|
14
|
+
*/
|
|
15
|
+
export declare function evaluateCondition(condition: BundleCondition, context: Context): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Evaluates all conditions against a context.
|
|
18
|
+
* All conditions must match (AND logic).
|
|
19
|
+
*
|
|
20
|
+
* @param conditions - Array of conditions
|
|
21
|
+
* @param context - The context to evaluate against
|
|
22
|
+
* @returns True if all conditions match (or if there are no conditions)
|
|
23
|
+
*/
|
|
24
|
+
export declare function evaluateConditions(conditions: BundleCondition[], context: Context): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Creates an equality condition.
|
|
27
|
+
*/
|
|
28
|
+
export declare function eq(field: string, value: unknown): BundleCondition;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a not-equal condition.
|
|
31
|
+
*/
|
|
32
|
+
export declare function neq(field: string, value: unknown): BundleCondition;
|
|
33
|
+
/**
|
|
34
|
+
* Creates an "in" condition.
|
|
35
|
+
*/
|
|
36
|
+
export declare function inValues(field: string, values: unknown[]): BundleCondition;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a "not in" condition.
|
|
39
|
+
*/
|
|
40
|
+
export declare function notIn(field: string, values: unknown[]): BundleCondition;
|
|
41
|
+
/**
|
|
42
|
+
* Creates a greater-than condition.
|
|
43
|
+
*/
|
|
44
|
+
export declare function gt(field: string, value: number): BundleCondition;
|
|
45
|
+
/**
|
|
46
|
+
* Creates a greater-than-or-equal condition.
|
|
47
|
+
*/
|
|
48
|
+
export declare function gte(field: string, value: number): BundleCondition;
|
|
49
|
+
/**
|
|
50
|
+
* Creates a less-than condition.
|
|
51
|
+
*/
|
|
52
|
+
export declare function lt(field: string, value: number): BundleCondition;
|
|
53
|
+
/**
|
|
54
|
+
* Creates a less-than-or-equal condition.
|
|
55
|
+
*/
|
|
56
|
+
export declare function lte(field: string, value: number): BundleCondition;
|
|
57
|
+
/**
|
|
58
|
+
* Creates a string contains condition.
|
|
59
|
+
*/
|
|
60
|
+
export declare function contains(field: string, value: string): BundleCondition;
|
|
61
|
+
/**
|
|
62
|
+
* Creates a string starts-with condition.
|
|
63
|
+
*/
|
|
64
|
+
export declare function startsWith(field: string, value: string): BundleCondition;
|
|
65
|
+
/**
|
|
66
|
+
* Creates a string ends-with condition.
|
|
67
|
+
*/
|
|
68
|
+
export declare function endsWith(field: string, value: string): BundleCondition;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a regex match condition.
|
|
71
|
+
*/
|
|
72
|
+
export declare function regex(field: string, pattern: string): BundleCondition;
|
|
73
|
+
/**
|
|
74
|
+
* Creates an exists condition.
|
|
75
|
+
*/
|
|
76
|
+
export declare function exists(field: string): BundleCondition;
|
|
77
|
+
/**
|
|
78
|
+
* Creates a not-exists condition.
|
|
79
|
+
*/
|
|
80
|
+
export declare function notExists(field: string): BundleCondition;
|
|
81
|
+
//# sourceMappingURL=conditions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conditions.d.ts","sourceRoot":"","sources":["../../src/resolution/conditions.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAElE;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,eAAe,EAC1B,OAAO,EAAE,OAAO,GACf,OAAO,CAmFT;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,eAAe,EAAE,EAC7B,OAAO,EAAE,OAAO,GACf,OAAO,CAQT;AAgCD;;GAEG;AACH,wBAAgB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,eAAe,CAEjE;AAED;;GAEG;AACH,wBAAgB,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,eAAe,CAElE;AAED;;GA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