-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathKeyBuilder.ts
More file actions
81 lines (61 loc) · 1.92 KB
/
KeyBuilder.ts
File metadata and controls
81 lines (61 loc) · 1.92 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
import { ISettings } from '../types';
import { hash } from '../utils/murmur3/murmur3';
const everythingAtTheEnd = /[^.]+$/;
const DEFAULT_PREFIX = 'SPLITIO';
export function validatePrefix(prefix: unknown) {
return prefix ? prefix + '.SPLITIO' : 'SPLITIO';
}
export class KeyBuilder {
readonly prefix: string;
constructor(prefix: string = DEFAULT_PREFIX) {
this.prefix = prefix;
}
buildTrafficTypeKey(trafficType: string) {
return `${this.prefix}.trafficType.${trafficType}`;
}
buildFlagSetKey(flagSet: string) {
return `${this.prefix}.flagSet.${flagSet}`;
}
buildSplitKey(splitName: string) {
return `${this.prefix}.split.${splitName}`;
}
buildSplitsTillKey() {
return `${this.prefix}.splits.till`;
}
buildSplitKeyPrefix() {
return `${this.prefix}.split.`;
}
buildRBSegmentKey(splitName: string) {
return `${this.prefix}.rbsegment.${splitName}`;
}
buildRBSegmentsTillKey() {
return `${this.prefix}.rbsegments.till`;
}
buildRBSegmentKeyPrefix() {
return `${this.prefix}.rbsegment.`;
}
buildSegmentNameKey(segmentName: string) {
return `${this.prefix}.segment.${segmentName}`;
}
buildSegmentTillKey(segmentName: string) {
return `${this.prefix}.segment.${segmentName}.till`;
}
extractKey(builtKey: string) {
const s = builtKey.match(everythingAtTheEnd);
if (s && s.length) {
return s[0];
} else {
throw new Error('Invalid latency key provided');
}
}
buildHashKey() {
return `${this.prefix}.hash`;
}
}
/**
* Generates a murmur32 hash based on the authorization key, the feature flags filter query, and version of SplitChanges API.
* The hash is in hexadecimal format (8 characters max, 32 bits).
*/
export function getStorageHash(settings: ISettings) {
return hash(`${settings.core.authorizationKey}::${settings.sync.__splitFiltersValidation.queryString}::${settings.sync.flagSpecVersion}`).toString(16);
}