- packages/openfeature-railyard/src/targeting.mjs: one dependency-free
evaluator (sync SHA-1, rollout hash = parseInt(sha1("<flag>.<id>")[0:15])
/ 0xfffffffffffffff, inside when <= rollout/100; ordered rules, all
conditions of a rule must match, first match wins with its own value).
Property operators follow the reference matcher of posthog-node (MIT):
exact/is_not (lists, true/false by truthiness), icontains, regex, gt/gte/
lt/lte (numeric, else text), is_set/is_not_set. Segments are named sets of
property conditions per environment (Flagsmith segment shape, BSD-3);
cohorts are product analytics cohort ids. Byte copies with .d.mts types in
apps/console/lib/flags and apps/product-analytics/src/flags (drift test).
- Console: rules and segments live in Railyard's store for both backends
(migration 0009: railyard.flag_targeting, railyard.flag_segments, bounded
checks, cascade, RLS, no anon), so the table fallback and the Flagsmith
backend have the same rule features. setFlag validates rules and segment
references; a used segment cannot be removed; branches copy rules and
segments. The read endpoint takes ?id, ?props (JSON, 2 KB) and ?cohorts,
answers each flag for that person and ships {on, value, rules} plus the
segments used, so SDKs evaluate locally; without a person a targeted flag
reads off (old clients stay safe). Flags screen: rules per flag (everyone,
a property, a segment; cohorts only with RAILYARD_REPLAY_LIVE) with a
share, and segments. POST /api/flags/segments.
- Product analytics /flags: evaluates targeted flags with the person's
stored properties (request person_properties override) and cohort
membership (through person overrides).
- OpenFeature providers and the client: evaluation context targetingKey +
attributes (+ cohorts) resolve per person; cached starts keep rules.
- Contract fixture: flags items carry targeting (additive, version 2).
Tests
- openfeature 16/16: SHA-1 vs node crypto, rollout shares within bounds
over 10 000 ids and monotonic growth, operators, rules, validation, read
answer, client and both providers vs the evaluator, cached start, drift.
- console 142/142 incl. both backends with the same 20 rule sets, segments,
isolation, read-endpoint context, 0009 migration on a scratch PG, and
10 000 ids x 20 rule sets: provider local == read endpoint (0 mismatches).
- product analytics test/qy 10/10 incl. 10 000 ids x 20 rule sets: provider
local == /flags from stored people and cohorts (0 mismatches), and the
unmodified posthog-node local evaluation agreeing on all 18 non-cohort
sets for 10 000 ids (0 mismatches, 0 undecided); a3 experiments unchanged.
- contract 6/6; console and PA tsc clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
58 lines
2.8 KiB
TypeScript
58 lines
2.8 KiB
TypeScript
// Types of targeting.mjs (Wave Q QY1.3). Byte copies sit next to the copies of
|
|
// targeting.mjs in the console and in product analytics.
|
|
|
|
export type Operator = "exact" | "is_not" | "icontains" | "not_icontains" | "regex" | "not_regex" | "gt" | "gte" | "lt" | "lte" | "is_set" | "is_not_set";
|
|
export type Scalar = string | number | boolean;
|
|
export type PropertyCondition = { type: "property"; key: string; operator: Operator; value?: Scalar | Scalar[] };
|
|
export type Condition = PropertyCondition | { type: "cohort"; id: string } | { type: "segment"; key: string };
|
|
export interface Rule {
|
|
conditions: Condition[];
|
|
rollout: number;
|
|
value?: string | null;
|
|
}
|
|
export interface Targeting {
|
|
rules: Rule[];
|
|
}
|
|
export interface Segment {
|
|
key: string;
|
|
name: string;
|
|
conditions: PropertyCondition[];
|
|
}
|
|
export type SegmentMap = Record<string, { conditions: Condition[] }>;
|
|
export interface TargetingContext {
|
|
id?: string | null;
|
|
properties?: Record<string, unknown>;
|
|
cohorts?: string[];
|
|
}
|
|
export interface Evaluation {
|
|
enabled: boolean;
|
|
value: string | null;
|
|
reason: "DISABLED" | "STATIC" | "TARGETING_MATCH" | "NO_MATCH";
|
|
rule: number | null;
|
|
}
|
|
export interface AnsweredFlag {
|
|
enabled: boolean;
|
|
value: string | null;
|
|
targeting?: { on: boolean; value: string | null; rules: Rule[] };
|
|
}
|
|
|
|
export declare const LONG_SCALE: number;
|
|
export declare const OPERATORS: readonly Operator[];
|
|
export declare const MAX_RULES: number;
|
|
export declare const MAX_CONDITIONS: number;
|
|
export declare const MAX_SEGMENTS: number;
|
|
export declare const MAX_TEXT: number;
|
|
export declare const MAX_LIST: number;
|
|
export declare const SEGMENT_KEY: RegExp;
|
|
export declare const PROPERTY_KEY: RegExp;
|
|
export declare function sha1Hex(text: string): string;
|
|
export declare function flagHash(key: string, id: string, salt?: string): number;
|
|
export declare function parseTargeting(raw: unknown, segments?: Set<string>): Targeting | null | string;
|
|
export declare function parseSegment(raw: unknown): Segment | string;
|
|
export declare function segmentRefs(targeting: Targeting | null | undefined): Set<string>;
|
|
export declare function matchProperty(cond: PropertyCondition, properties: Record<string, unknown> | null | undefined): boolean;
|
|
export declare function evaluateFlag(key: string, flag: { enabled: boolean; value: string | null; targeting?: Targeting | null }, ctx?: TargetingContext, segments?: SegmentMap): Evaluation;
|
|
export declare function answerFlag(key: string, flag: { enabled: boolean; value: string | null; targeting?: Targeting | null }, ctx?: TargetingContext, segments?: SegmentMap): AnsweredFlag;
|
|
export declare function evaluateAnswer(key: string, answered: AnsweredFlag | null | undefined, ctx?: TargetingContext, segments?: SegmentMap): Evaluation;
|
|
export declare function referencedSegments(flags: Record<string, AnsweredFlag>, segments: SegmentMap): SegmentMap;
|