Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | 5x 5x 47x 47x 47x 47x 47x 47x 47x 9x 1x 210x 210x 1x 209x 209x 10x 208x 4x 4x 4x 4x 4x 4x 18x 18x 18x 16x 2x 2x 2x 1506x 2x 1x 1x 1504x 18x 1504x 745x 745x 759x 37x 736x 38x 714x 6x 9x 708x 711x 207x 506x 100x 406x 207x 207x 207x 199x 199x 15x 24x 184x 8x 12x 176x 10x 10x 166x 166x 177x 165x 228x 67x 161x 72x 72x 132x 132x 70x 2x 178x 178x 178x 49x 129x 369x 369x 13x 13x 13x 356x 1x 1x 1x 129x 573x 563x 10x 10x 10x 242x 5x 237x | import type {
Feature,
Segment,
DatafileContent,
SegmentKey,
FeatureKey,
Context,
Traffic,
Allocation,
GroupSegment,
Condition,
Force,
} from "@featurevisor/types";
import { conditionIsMatched } from "./conditions";
import { Logger } from "./logger";
export type GetRegex = (regexString: string, regexFlags: string) => RegExp;
export interface DatafileReaderOptions {
datafile: DatafileContent;
logger: Logger;
}
export interface ForceResult {
force?: Force;
forceIndex?: number;
}
export class DatafileReader {
private schemaVersion: string;
private revision: string;
private segments: Record<SegmentKey, Segment>;
private features: Record<FeatureKey, Feature>;
private logger: Logger;
// done to avoid creating new RegExp objects for the same regex string and flags.
// kept here to avoid memory leaks.
// if datafile is reset, this cache will be cleared.
private regexCache: Record<string, RegExp>;
constructor(options: DatafileReaderOptions) {
const { datafile, logger } = options;
this.logger = logger;
this.schemaVersion = datafile.schemaVersion;
this.revision = datafile.revision;
this.segments = datafile.segments;
this.features = datafile.features;
this.regexCache = {};
}
getRevision(): string {
return this.revision;
}
getSchemaVersion(): string {
return this.schemaVersion;
}
getSegment(segmentKey: SegmentKey): Segment | undefined {
const segment = this.segments[segmentKey];
if (!segment) {
return undefined;
}
segment.conditions = this.parseConditionsIfStringified(segment.conditions);
return segment;
}
getFeatureKeys(): string[] {
return Object.keys(this.features);
}
getFeature(featureKey: FeatureKey): Feature | undefined {
return this.features[featureKey];
}
getVariableKeys(featureKey: FeatureKey): string[] {
const feature = this.getFeature(featureKey);
Iif (!feature) {
return [];
}
return Object.keys(feature.variablesSchema || {});
}
hasVariations(featureKey: FeatureKey): boolean {
const feature = this.getFeature(featureKey);
Iif (!feature) {
return false;
}
return Array.isArray(feature.variations) && feature.variations.length > 0;
}
getRegex(regexString: string, regexFlags?: string): RegExp {
const flags = regexFlags || "";
const cacheKey = `${regexString}-${flags}`;
if (this.regexCache[cacheKey]) {
return this.regexCache[cacheKey];
}
const regex = new RegExp(regexString, flags);
this.regexCache[cacheKey] = regex;
return regex;
}
allConditionsAreMatched(conditions: Condition[] | Condition, context: Context): boolean {
if (typeof conditions === "string") {
if (conditions === "*") {
return true;
}
return false;
}
const getRegex = (regexString: string, regexFlags: string) =>
this.getRegex(regexString, regexFlags);
if ("attribute" in conditions) {
try {
return conditionIsMatched(conditions, context, getRegex);
} catch (e) {
this.logger.warn(e.message, {
error: e,
details: {
condition: conditions,
context,
},
});
return false;
}
}
if ("and" in conditions && Array.isArray(conditions.and)) {
return conditions.and.every((c) => this.allConditionsAreMatched(c, context));
}
if ("or" in conditions && Array.isArray(conditions.or)) {
return conditions.or.some((c) => this.allConditionsAreMatched(c, context));
}
if ("not" in conditions && Array.isArray(conditions.not)) {
return conditions.not.every(
() =>
this.allConditionsAreMatched(
{
and: conditions.not,
},
context,
) === false,
);
}
if (Array.isArray(conditions)) {
return conditions.every((c) => this.allConditionsAreMatched(c, context));
}
return false;
}
segmentIsMatched(segment: Segment, context: Context): boolean {
return this.allConditionsAreMatched(segment.conditions as Condition | Condition[], context);
}
allSegmentsAreMatched(
groupSegments: GroupSegment | GroupSegment[] | "*",
context: Context,
): boolean {
if (groupSegments === "*") {
return true;
}
if (typeof groupSegments === "string") {
const segment = this.getSegment(groupSegments);
if (segment) {
return this.segmentIsMatched(segment, context);
}
return false;
}
if (typeof groupSegments === "object") {
if ("and" in groupSegments && Array.isArray(groupSegments.and)) {
return groupSegments.and.every((groupSegment) =>
this.allSegmentsAreMatched(groupSegment, context),
);
}
if ("or" in groupSegments && Array.isArray(groupSegments.or)) {
return groupSegments.or.some((groupSegment) =>
this.allSegmentsAreMatched(groupSegment, context),
);
}
if ("not" in groupSegments && Array.isArray(groupSegments.not)) {
return groupSegments.not.every(
(groupSegment) => this.allSegmentsAreMatched(groupSegment, context) === false,
);
}
}
if (Array.isArray(groupSegments)) {
return groupSegments.every((groupSegment) =>
this.allSegmentsAreMatched(groupSegment, context),
);
}
return false;
}
getMatchedTraffic(traffic: Traffic[], context: Context): Traffic | undefined {
return traffic.find((t) => {
if (!this.allSegmentsAreMatched(this.parseSegmentsIfStringified(t.segments), context)) {
return false;
}
return true;
});
}
getMatchedAllocation(traffic: Traffic, bucketValue: number): Allocation | undefined {
Iif (!traffic.allocation) {
return undefined;
}
for (const allocation of traffic.allocation) {
const [start, end] = allocation.range;
if (allocation.range && start <= bucketValue && end >= bucketValue) {
return allocation;
}
}
return undefined;
}
getMatchedForce(featureKey: FeatureKey | Feature, context: Context): ForceResult {
const result: ForceResult = {
force: undefined,
forceIndex: undefined,
};
const feature = typeof featureKey === "string" ? this.getFeature(featureKey) : featureKey;
if (!feature || !feature.force) {
return result;
}
for (let i = 0; i < feature.force.length; i++) {
const currentForce = feature.force[i];
if (
currentForce.conditions &&
this.allConditionsAreMatched(
this.parseConditionsIfStringified(currentForce.conditions),
context,
)
) {
result.force = currentForce;
result.forceIndex = i;
break;
}
if (
currentForce.segments &&
this.allSegmentsAreMatched(this.parseSegmentsIfStringified(currentForce.segments), context)
) {
result.force = currentForce;
result.forceIndex = i;
break;
}
}
return result;
}
parseConditionsIfStringified(conditions: Condition | Condition[]): Condition | Condition[] {
if (typeof conditions !== "string") {
// already parsed
return conditions;
}
Iif (conditions === "*") {
// everyone
return conditions;
}
try {
return JSON.parse(conditions);
} catch (e) {
this.logger.error("Error parsing conditions", {
error: e,
details: {
conditions,
},
});
return conditions;
}
}
parseSegmentsIfStringified(
segments: GroupSegment | GroupSegment[],
): GroupSegment | GroupSegment[] {
if (typeof segments === "string" && (segments.startsWith("{") || segments.startsWith("["))) {
return JSON.parse(segments);
}
return segments;
}
}
|