-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathcontroller-fns.ts
More file actions
143 lines (127 loc) · 3.9 KB
/
controller-fns.ts
File metadata and controls
143 lines (127 loc) · 3.9 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
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
import { evaluate as evalFlag } from './evaluate';
import { internalReportValue } from './lib/report-value';
import type {
BundledDefinitions,
ControllerInterface,
Datafile,
EvaluationResult,
Packed,
} from './types';
import { ErrorCode, ResolutionReason } from './types';
export type ControllerInstance = {
controller: ControllerInterface;
initialized: boolean;
initPromise: Promise<void> | null;
};
export const controllerInstanceMap = new Map<number, ControllerInstance>();
function getInstance(id: number): ControllerInstance {
const instance = controllerInstanceMap.get(id);
if (!instance) {
throw new Error(
`@vercel/flags-core: Client instance ${id} not found. It may have been shut down.`,
);
}
return instance;
}
export function initialize(id: number): Promise<void> {
return getInstance(id).controller.initialize();
}
export function shutdown(id: number): void | Promise<void> {
return getInstance(id).controller.shutdown();
}
export function getDatafile(id: number) {
return getInstance(id).controller.getDatafile();
}
export function getFallbackDatafile(id: number): Promise<BundledDefinitions> {
const ds = getInstance(id).controller;
if (ds.getFallbackDatafile) return ds.getFallbackDatafile();
throw new Error('flags: This data source does not support fallbacks');
}
export async function evaluate<T, E = Record<string, unknown>>(
id: number,
flagKey: string,
defaultValue?: T,
entities?: E,
): Promise<EvaluationResult<T>> {
const controller = getInstance(id).controller;
let datafile: Datafile;
try {
datafile = await controller.read();
} catch (error) {
// All data sources failed. Fall back to defaultValue if provided.
if (defaultValue !== undefined) {
return {
value: defaultValue,
reason: ResolutionReason.ERROR,
errorMessage:
error instanceof Error ? error.message : 'Failed to read datafile',
};
}
throw error;
}
const flagDefinition = datafile.definitions[flagKey] as Packed.FlagDefinition;
if (flagDefinition === undefined) {
if (datafile.projectId) {
internalReportValue(flagKey, defaultValue, {
originProjectId: datafile.projectId,
originProvider: 'vercel',
reason: ResolutionReason.ERROR,
});
}
return {
value: defaultValue,
reason: ResolutionReason.ERROR,
errorCode: ErrorCode.FLAG_NOT_FOUND,
errorMessage: `@vercel/flags-core: Definition not found for flag "${flagKey}"`,
metrics: {
evaluationMs: 0,
readMs: datafile.metrics.readMs,
source: datafile.metrics.source,
cacheStatus: datafile.metrics.cacheStatus,
connectionState: datafile.metrics.connectionState,
mode: datafile.metrics.mode,
},
};
}
const evalStartTime = Date.now();
const result = evalFlag<T>({
defaultValue,
definition: flagDefinition,
environment: datafile.environment,
entities: entities ?? {},
segments: datafile.segments,
});
const evaluationDurationMs = Date.now() - evalStartTime;
if (datafile.projectId) {
internalReportValue(flagKey, result.value, {
originProjectId: datafile.projectId,
originProvider: 'vercel',
reason: result.reason,
outcomeType:
result.reason !== ResolutionReason.ERROR
? result.outcomeType
: undefined,
});
}
if (
result.reason !== ResolutionReason.ERROR &&
flagDefinition.id &&
result.variantId
) {
controller.trackEvaluation({
flagId: flagDefinition.id,
variantId: result.variantId,
reason: result.reason,
});
}
return Object.assign(result, {
metrics: {
evaluationMs: evaluationDurationMs,
readMs: datafile.metrics.readMs,
source: datafile.metrics.source,
cacheStatus: datafile.metrics.cacheStatus,
connectionState: datafile.metrics.connectionState,
mode: datafile.metrics.mode,
},
});
}