|
| 1 | +import SplitIO from '../../types/splitio'; |
| 2 | +import { IRBSegmentsCacheSync, ISegmentsCacheSync, ISplitsCacheSync } from './types'; |
| 3 | +import { ILogger } from '../logger/types'; |
| 4 | +import { isObject } from '../utils/lang'; |
| 5 | +import { isConsumerMode } from '../utils/settingsValidation/mode'; |
| 6 | +import { RolloutPlan } from './types'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Validates if the given rollout plan is valid. |
| 10 | + */ |
| 11 | +export function validateRolloutPlan(log: ILogger, settings: SplitIO.ISettings): RolloutPlan | undefined { |
| 12 | + const { mode, initialRolloutPlan } = settings; |
| 13 | + |
| 14 | + if (isConsumerMode(mode)) { |
| 15 | + log.warn('storage: initial rollout plan is ignored in consumer mode'); |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + if (isObject(initialRolloutPlan) && isObject((initialRolloutPlan as any).splitChanges)) return initialRolloutPlan as RolloutPlan; |
| 20 | + |
| 21 | + log.error('storage: invalid rollout plan provided'); |
| 22 | + return; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Sets the given synchronous storage with the provided rollout plan snapshot. |
| 27 | + * If `matchingKey` is provided, the storage is handled as a client-side storage (segments and largeSegments are instances of MySegmentsCache). |
| 28 | + * Otherwise, the storage is handled as a server-side storage (segments is an instance of SegmentsCache). |
| 29 | + */ |
| 30 | +export function setRolloutPlan(log: ILogger, rolloutPlan: RolloutPlan, storage: { splits?: ISplitsCacheSync, rbSegments?: IRBSegmentsCacheSync, segments: ISegmentsCacheSync, largeSegments?: ISegmentsCacheSync }, matchingKey?: string) { |
| 31 | + const { splits, rbSegments, segments, largeSegments } = storage; |
| 32 | + const { splitChanges: { ff, rbs } } = rolloutPlan; |
| 33 | + |
| 34 | + log.debug(`storage: set feature flags and segments${matchingKey ? ` for key ${matchingKey}` : ''}`); |
| 35 | + |
| 36 | + if (splits && ff) { |
| 37 | + splits.clear(); |
| 38 | + splits.update(ff.d, [], ff.t); |
| 39 | + } |
| 40 | + |
| 41 | + if (rbSegments && rbs) { |
| 42 | + rbSegments.clear(); |
| 43 | + rbSegments.update(rbs.d, [], rbs.t); |
| 44 | + } |
| 45 | + |
| 46 | + const segmentChanges = rolloutPlan.segmentChanges; |
| 47 | + if (matchingKey) { // add memberships data (client-side) |
| 48 | + let memberships = rolloutPlan.memberships && rolloutPlan.memberships[matchingKey]; |
| 49 | + if (!memberships && segmentChanges) { |
| 50 | + memberships = { |
| 51 | + ms: { |
| 52 | + k: segmentChanges.filter(segment => { |
| 53 | + return segment.added.indexOf(matchingKey) > -1; |
| 54 | + }).map(segment => ({ n: segment.name })) |
| 55 | + } |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + if (memberships) { |
| 60 | + if (memberships.ms) segments.resetSegments(memberships.ms!); |
| 61 | + if (memberships.ls && largeSegments) largeSegments.resetSegments(memberships.ls!); |
| 62 | + } |
| 63 | + } else { // add segments data (server-side) |
| 64 | + if (segmentChanges) { |
| 65 | + segments.clear(); |
| 66 | + segmentChanges.forEach(segment => { |
| 67 | + segments.update(segment.name, segment.added, segment.removed, segment.till); |
| 68 | + }); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments