@temps-sdk/node-sdk 0.0.7 → 0.0.8-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analytics/identify.d.ts +72 -0
- package/dist/analytics/identify.js +103 -0
- package/dist/analytics/index.d.ts +6 -0
- package/dist/analytics/index.js +6 -0
- package/dist/client/client/client.gen.js +2 -0
- package/dist/client/client/index.js +2 -1
- package/dist/client/client/types.gen.js +2 -1
- package/dist/client/client/utils.gen.js +2 -0
- package/dist/client/client.gen.js +2 -1
- package/dist/client/core/auth.gen.js +2 -1
- package/dist/client/core/bodySerializer.gen.js +2 -1
- package/dist/client/core/params.gen.js +2 -1
- package/dist/client/core/pathSerializer.gen.js +2 -1
- package/dist/client/core/serverSentEvents.gen.js +2 -1
- package/dist/client/core/types.gen.js +2 -1
- package/dist/client/core/utils.gen.js +2 -1
- package/dist/client/index.js +2 -0
- package/dist/client/sdk.gen.d.ts +20 -0
- package/dist/client/sdk.gen.js +80 -2
- package/dist/client/types.gen.d.ts +661 -8
- package/dist/client/types.gen.js +2 -1
- package/dist/errors/client.js +2 -0
- package/dist/errors/hub.js +2 -0
- package/dist/errors/index.js +2 -0
- package/dist/errors/scope.js +2 -0
- package/dist/errors/stacktrace.js +2 -0
- package/dist/errors/transaction.js +2 -0
- package/dist/errors/transport.js +2 -0
- package/dist/errors/types.js +2 -0
- package/dist/flags/client.d.ts +0 -11
- package/dist/flags/client.js +23 -1
- package/dist/flags/evaluate.d.ts +12 -21
- package/dist/flags/evaluate.js +252 -22
- package/dist/flags/index.js +2 -0
- package/dist/flags/sha256.d.ts +4 -0
- package/dist/flags/sha256.js +81 -0
- package/dist/flags/types.d.ts +57 -14
- package/dist/flags/types.js +2 -5
- package/dist/index.d.ts +86 -0
- package/dist/index.js +9201 -552
- package/package.json +8 -3
package/dist/client/types.gen.js
CHANGED
package/dist/errors/client.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
1
3
|
import { Hub } from './hub.js';
|
|
2
4
|
import { createTransportFromDsn } from './transport.js';
|
|
3
5
|
import { exceptionFromError, extractErrorMessage, isError } from './stacktrace.js';
|
package/dist/errors/hub.js
CHANGED
package/dist/errors/index.js
CHANGED
package/dist/errors/scope.js
CHANGED
package/dist/errors/transport.js
CHANGED
package/dist/errors/types.js
CHANGED
package/dist/flags/client.d.ts
CHANGED
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* In-memory feature-flag client for services running on Temps.
|
|
3
|
-
*
|
|
4
|
-
* Reads are synchronous and cost no network I/O: the whole environment's flag
|
|
5
|
-
* set lives in memory and is refreshed in the background. That is the point —
|
|
6
|
-
* a per-check HTTP round trip (~200-500ms) is unusable in a request handler.
|
|
7
|
-
*
|
|
8
|
-
* Zero configuration on Temps: `TEMPS_API_URL` and `TEMPS_API_TOKEN` are
|
|
9
|
-
* injected into every deployment, and the token pins the project (and usually
|
|
10
|
-
* the environment), so `new FlagsClient()` already knows what to fetch.
|
|
11
|
-
*/
|
|
12
1
|
import type { EvalContext, Evaluation, FlagsClientOptions } from './types';
|
|
13
2
|
export declare class FlagsClient {
|
|
14
3
|
private readonly apiUrl;
|
package/dist/flags/client.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
1
3
|
/**
|
|
2
4
|
* In-memory feature-flag client for services running on Temps.
|
|
3
5
|
*
|
|
@@ -30,6 +32,25 @@ function envVar(name) {
|
|
|
30
32
|
const global = globalThis;
|
|
31
33
|
return global.process?.env?.[name];
|
|
32
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Parse `TEMPS_FLAGS_REFRESH_INTERVAL_MS`, falling back to `fallback` for an
|
|
37
|
+
* unset, non-numeric, or negative value.
|
|
38
|
+
*
|
|
39
|
+
* A malformed value is surfaced via `console.warn` rather than silently
|
|
40
|
+
* ignored — an operator who set this and sees no effect needs to know the
|
|
41
|
+
* value they set was rejected, not that the feature doesn't exist.
|
|
42
|
+
*/
|
|
43
|
+
function refreshIntervalFromEnv(fallback) {
|
|
44
|
+
const raw = envVar('TEMPS_FLAGS_REFRESH_INTERVAL_MS');
|
|
45
|
+
if (raw === undefined)
|
|
46
|
+
return fallback;
|
|
47
|
+
const parsed = Number(raw);
|
|
48
|
+
if (!Number.isFinite(parsed) || parsed < 0) {
|
|
49
|
+
console.warn(`[temps-flags] TEMPS_FLAGS_REFRESH_INTERVAL_MS=${JSON.stringify(raw)} is not a valid non-negative number. Using the default of ${fallback}ms instead.`);
|
|
50
|
+
return fallback;
|
|
51
|
+
}
|
|
52
|
+
return parsed;
|
|
53
|
+
}
|
|
33
54
|
export class FlagsClient {
|
|
34
55
|
apiUrl;
|
|
35
56
|
apiToken;
|
|
@@ -61,7 +82,8 @@ export class FlagsClient {
|
|
|
61
82
|
this.apiUrl = (options.apiUrl ?? envVar('TEMPS_API_URL') ?? '').replace(/\/+$/, '');
|
|
62
83
|
this.apiToken = options.apiToken ?? envVar('TEMPS_API_TOKEN') ?? '';
|
|
63
84
|
this.environmentId = options.environmentId;
|
|
64
|
-
this.refreshIntervalMs =
|
|
85
|
+
this.refreshIntervalMs =
|
|
86
|
+
options.refreshIntervalMs ?? refreshIntervalFromEnv(DEFAULT_REFRESH_INTERVAL_MS);
|
|
65
87
|
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
66
88
|
this.reportExposure = options.reportExposure ?? true;
|
|
67
89
|
this.onError =
|
package/dist/flags/evaluate.d.ts
CHANGED
|
@@ -1,21 +1,10 @@
|
|
|
1
|
+
import type { EvalContext, Evaluation, FlagSnapshot, FlagValueType } from './types';
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* `crates/temps-flags/src/eval.rs`. If you change the resolution order here,
|
|
6
|
-
* change it there in the same commit, or the same flag will resolve
|
|
7
|
-
* differently in the app and on the server.
|
|
8
|
-
*
|
|
9
|
-
* Resolution order:
|
|
10
|
-
*
|
|
11
|
-
* 1. flag missing or archived -> caller's fallback FLAG_NOT_FOUND
|
|
12
|
-
* 2. enabled === false -> default_value DISABLED
|
|
13
|
-
* 3. -- reserved for rules -- RULE_MATCH{i}
|
|
14
|
-
* | PERCENTAGE_ROLLOUT{i}
|
|
15
|
-
* 4. environment value present -> that value ENVIRONMENT_VALUE
|
|
16
|
-
* 5. otherwise -> default_value DEFAULT
|
|
3
|
+
* Longest string a `matches` condition is tested against. JavaScript regular
|
|
4
|
+
* expressions backtrack (the server's do not), so the SDK bounds the input a
|
|
5
|
+
* pattern can be run on. Longer values never match.
|
|
17
6
|
*/
|
|
18
|
-
|
|
7
|
+
export declare const MAX_MATCHED_LENGTH = 1024;
|
|
19
8
|
/**
|
|
20
9
|
* Whether a concrete JSON value is admissible for a declared flag type.
|
|
21
10
|
*
|
|
@@ -24,9 +13,11 @@ import type { EvalContext, Evaluation, FlagSnapshot, FlagValueType } from './typ
|
|
|
24
13
|
*/
|
|
25
14
|
export declare function valueMatchesType(valueType: FlagValueType, value: unknown): boolean;
|
|
26
15
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* `_context` is accepted and ignored: targeting has not shipped. The parameter
|
|
30
|
-
* is the expensive half of the contract; the code that reads it is cheap.
|
|
16
|
+
* Stable bucket in [0, 100]. Identical to `bucket()` on the server: the first
|
|
17
|
+
* 32 bits of SHA-256 over `key:salt:subject`.
|
|
31
18
|
*/
|
|
32
|
-
export declare function
|
|
19
|
+
export declare function bucket(flagKey: string, salt: string, subject: string): number;
|
|
20
|
+
/** The split position, independent of the rollout bucket. */
|
|
21
|
+
export declare function splitBucket(flagKey: string, salt: string, subject: string): number;
|
|
22
|
+
/** Evaluate one flag. Always returns a usable value. */
|
|
23
|
+
export declare function evaluate<T>(flag: FlagSnapshot | undefined, context: EvalContext, fallback: T): Evaluation<T>;
|
package/dist/flags/evaluate.js
CHANGED
|
@@ -1,20 +1,37 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
1
3
|
/**
|
|
2
4
|
* Flag evaluation. Pure, synchronous, total — no I/O, never throws.
|
|
3
5
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* A deliberate mirror of `evaluate()` in `crates/temps-flags/src/eval.rs`. If
|
|
7
|
+
* you change the resolution order or a rule semantic here, change it there in
|
|
8
|
+
* the same commit, or the same flag resolves differently in the app and on
|
|
9
|
+
* the server. `evaluate.test.ts` pins the shared bucketing vectors.
|
|
8
10
|
*
|
|
9
11
|
* Resolution order:
|
|
10
12
|
*
|
|
11
13
|
* 1. flag missing or archived -> caller's fallback FLAG_NOT_FOUND
|
|
12
14
|
* 2. enabled === false -> default_value DISABLED
|
|
13
|
-
* 3.
|
|
15
|
+
* 3. first matching rule -> its serve RULE_MATCH{i}
|
|
14
16
|
* | PERCENTAGE_ROLLOUT{i}
|
|
15
17
|
* 4. environment value present -> that value ENVIRONMENT_VALUE
|
|
16
18
|
* 5. otherwise -> default_value DEFAULT
|
|
19
|
+
*
|
|
20
|
+
* Rule semantics (same as the server):
|
|
21
|
+
* - A condition on an attribute the context lacks is false, except
|
|
22
|
+
* `not_exists`. Targeting fails closed.
|
|
23
|
+
* - A rule needing a bucketing subject (rollout < 100, or a split) is skipped
|
|
24
|
+
* when the context has no `key`.
|
|
25
|
+
* - A rule that cannot be evaluated (undeclared variant, bad pattern, split
|
|
26
|
+
* not summing to 100) makes the flag serve its default with reason `ERROR`.
|
|
27
|
+
*/
|
|
28
|
+
import { sha256Prefix32 } from './sha256';
|
|
29
|
+
/**
|
|
30
|
+
* Longest string a `matches` condition is tested against. JavaScript regular
|
|
31
|
+
* expressions backtrack (the server's do not), so the SDK bounds the input a
|
|
32
|
+
* pattern can be run on. Longer values never match.
|
|
17
33
|
*/
|
|
34
|
+
export const MAX_MATCHED_LENGTH = 1024;
|
|
18
35
|
/**
|
|
19
36
|
* Whether a concrete JSON value is admissible for a declared flag type.
|
|
20
37
|
*
|
|
@@ -35,41 +52,254 @@ export function valueMatchesType(valueType, value) {
|
|
|
35
52
|
return false;
|
|
36
53
|
}
|
|
37
54
|
}
|
|
38
|
-
|
|
39
|
-
|
|
55
|
+
/**
|
|
56
|
+
* Stable bucket in [0, 100]. Identical to `bucket()` on the server: the first
|
|
57
|
+
* 32 bits of SHA-256 over `key:salt:subject`.
|
|
58
|
+
*/
|
|
59
|
+
export function bucket(flagKey, salt, subject) {
|
|
60
|
+
return (sha256Prefix32(`${flagKey}:${salt}:${subject}`) / 0xffffffff) * 100;
|
|
61
|
+
}
|
|
62
|
+
/** The split position, independent of the rollout bucket. */
|
|
63
|
+
export function splitBucket(flagKey, salt, subject) {
|
|
64
|
+
return bucket(`${flagKey}#split`, salt, subject);
|
|
65
|
+
}
|
|
66
|
+
function outcome(value, reason, variant = null) {
|
|
67
|
+
return { value: value, variant, reason };
|
|
68
|
+
}
|
|
69
|
+
function degraded(flag, fallback) {
|
|
70
|
+
if (valueMatchesType(flag.value_type, flag.default_value)) {
|
|
71
|
+
return outcome(flag.default_value, { kind: 'ERROR' });
|
|
72
|
+
}
|
|
73
|
+
return outcome(fallback, { kind: 'ERROR' });
|
|
40
74
|
}
|
|
41
75
|
/**
|
|
42
76
|
* Serve `value` if it matches the flag's declared type, otherwise degrade to
|
|
43
77
|
* the flag default. An operator typing the wrong thing into the flag UI must
|
|
44
78
|
* not break the running app.
|
|
45
79
|
*/
|
|
46
|
-
function sanitized(flag, value, reason, fallback) {
|
|
80
|
+
function sanitized(flag, value, reason, fallback, variant = null) {
|
|
47
81
|
if (valueMatchesType(flag.value_type, value)) {
|
|
48
|
-
return outcome(value, reason);
|
|
82
|
+
return outcome(value, reason, variant);
|
|
49
83
|
}
|
|
50
|
-
|
|
51
|
-
|
|
84
|
+
return degraded(flag, fallback);
|
|
85
|
+
}
|
|
86
|
+
class Malformed extends Error {
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The finite decimal numbers the server's evaluator (Rust's `f64` parser)
|
|
90
|
+
* accepts. `Number()` alone also reads `0x10`, `0b1` and `0o7`, which the
|
|
91
|
+
* server treats as plain strings, so the two evaluators would disagree.
|
|
92
|
+
*/
|
|
93
|
+
const DECIMAL = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/;
|
|
94
|
+
function asNumber(value) {
|
|
95
|
+
if (typeof value === 'number')
|
|
96
|
+
return Number.isFinite(value) ? value : null;
|
|
97
|
+
if (typeof value === 'string') {
|
|
98
|
+
const trimmed = value.trim();
|
|
99
|
+
if (!DECIMAL.test(trimmed))
|
|
100
|
+
return null;
|
|
101
|
+
const parsed = Number(trimmed);
|
|
102
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
52
103
|
}
|
|
53
|
-
return
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
function looselyEquals(a, b) {
|
|
107
|
+
if (typeof a === 'boolean' || typeof b === 'boolean')
|
|
108
|
+
return a === b;
|
|
109
|
+
if (typeof a === typeof b)
|
|
110
|
+
return a === b;
|
|
111
|
+
const x = asNumber(a);
|
|
112
|
+
const y = asNumber(b);
|
|
113
|
+
return x !== null && y !== null && x === y;
|
|
114
|
+
}
|
|
115
|
+
const patternCache = new Map();
|
|
116
|
+
const PATTERN_CACHE_LIMIT = 256;
|
|
117
|
+
function compiledPattern(source) {
|
|
118
|
+
let compiled = patternCache.get(source);
|
|
119
|
+
if (compiled === undefined) {
|
|
120
|
+
try {
|
|
121
|
+
// Compile the pattern alone first, as the server does: `pro)|(?:team`
|
|
122
|
+
// only balances against the wrapper and would escape its anchors.
|
|
123
|
+
compiled =
|
|
124
|
+
source.length > 256 ? null : (new RegExp(source, 'u'), new RegExp(`^(?:${source})$`, 'u'));
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
compiled = null;
|
|
128
|
+
}
|
|
129
|
+
if (patternCache.size >= PATTERN_CACHE_LIMIT)
|
|
130
|
+
patternCache.clear();
|
|
131
|
+
patternCache.set(source, compiled);
|
|
132
|
+
}
|
|
133
|
+
if (compiled === null)
|
|
134
|
+
throw new Malformed();
|
|
135
|
+
return compiled;
|
|
136
|
+
}
|
|
137
|
+
function single(condition) {
|
|
138
|
+
const value = condition.value;
|
|
139
|
+
if (value === undefined || value === null || Array.isArray(value))
|
|
140
|
+
throw new Malformed();
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
function list(condition) {
|
|
144
|
+
if (!Array.isArray(condition.value))
|
|
145
|
+
throw new Malformed();
|
|
146
|
+
return condition.value;
|
|
147
|
+
}
|
|
148
|
+
function conditionHolds(condition, attributes) {
|
|
149
|
+
const has = Object.prototype.hasOwnProperty.call(attributes, condition.attribute);
|
|
150
|
+
const actual = has ? attributes[condition.attribute] : undefined;
|
|
151
|
+
if (actual === undefined || actual === null) {
|
|
152
|
+
return condition.operator === 'not_exists';
|
|
153
|
+
}
|
|
154
|
+
const text = typeof actual === 'string' ? actual : null;
|
|
155
|
+
switch (condition.operator) {
|
|
156
|
+
case 'exists':
|
|
157
|
+
return true;
|
|
158
|
+
case 'not_exists':
|
|
159
|
+
return false;
|
|
160
|
+
case 'eq':
|
|
161
|
+
return looselyEquals(actual, single(condition));
|
|
162
|
+
case 'neq':
|
|
163
|
+
return !looselyEquals(actual, single(condition));
|
|
164
|
+
case 'in':
|
|
165
|
+
return list(condition).some((v) => looselyEquals(actual, v));
|
|
166
|
+
case 'not_in':
|
|
167
|
+
return !list(condition).some((v) => looselyEquals(actual, v));
|
|
168
|
+
case 'contains':
|
|
169
|
+
case 'not_contains': {
|
|
170
|
+
const needle = single(condition);
|
|
171
|
+
if (typeof needle !== 'string')
|
|
172
|
+
throw new Malformed();
|
|
173
|
+
if (text === null)
|
|
174
|
+
return false;
|
|
175
|
+
return condition.operator === 'contains' ? text.includes(needle) : !text.includes(needle);
|
|
176
|
+
}
|
|
177
|
+
case 'matches': {
|
|
178
|
+
const source = single(condition);
|
|
179
|
+
if (typeof source !== 'string')
|
|
180
|
+
throw new Malformed();
|
|
181
|
+
const pattern = compiledPattern(source);
|
|
182
|
+
return text !== null && text.length <= MAX_MATCHED_LENGTH && pattern.test(text);
|
|
183
|
+
}
|
|
184
|
+
case 'gt':
|
|
185
|
+
case 'gte':
|
|
186
|
+
case 'lt':
|
|
187
|
+
case 'lte': {
|
|
188
|
+
const operand = asNumber(single(condition));
|
|
189
|
+
if (operand === null)
|
|
190
|
+
throw new Malformed();
|
|
191
|
+
const value = asNumber(actual);
|
|
192
|
+
if (value === null)
|
|
193
|
+
return false;
|
|
194
|
+
if (condition.operator === 'gt')
|
|
195
|
+
return value > operand;
|
|
196
|
+
if (condition.operator === 'gte')
|
|
197
|
+
return value >= operand;
|
|
198
|
+
if (condition.operator === 'lt')
|
|
199
|
+
return value < operand;
|
|
200
|
+
return value <= operand;
|
|
201
|
+
}
|
|
202
|
+
default:
|
|
203
|
+
throw new Malformed();
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function findVariant(flag, key) {
|
|
207
|
+
const variant = (flag.variants ?? []).find((v) => v.key === key);
|
|
208
|
+
if (!variant)
|
|
209
|
+
throw new Malformed();
|
|
210
|
+
return variant;
|
|
54
211
|
}
|
|
55
212
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* is the expensive half of the contract; the code that reads it is cheap.
|
|
213
|
+
* A split is evaluable when it is non-empty, its weights sum to exactly 100 and
|
|
214
|
+
* every arm names a declared variant. Checked before the subject, as the Rust
|
|
215
|
+
* evaluator does, so a broken split is an `ERROR` whoever is asking.
|
|
60
216
|
*/
|
|
61
|
-
|
|
217
|
+
function checkSplit(flag, split) {
|
|
218
|
+
const total = split.reduce((sum, arm) => sum + arm.weight, 0);
|
|
219
|
+
if (split.length === 0 || total !== 100)
|
|
220
|
+
throw new Malformed();
|
|
221
|
+
for (const arm of split)
|
|
222
|
+
findVariant(flag, arm.variant);
|
|
223
|
+
}
|
|
224
|
+
/** Called after `checkSplit`. */
|
|
225
|
+
function pickSplit(flag, split, subject) {
|
|
226
|
+
const position = splitBucket(flag.key, flag.salt ?? '', subject);
|
|
227
|
+
let cumulative = 0;
|
|
228
|
+
for (const arm of split) {
|
|
229
|
+
cumulative += arm.weight;
|
|
230
|
+
if (position < cumulative)
|
|
231
|
+
return arm.variant;
|
|
232
|
+
}
|
|
233
|
+
const last = [...split].reverse().find((arm) => arm.weight > 0);
|
|
234
|
+
if (!last)
|
|
235
|
+
throw new Malformed();
|
|
236
|
+
return last.variant;
|
|
237
|
+
}
|
|
238
|
+
/** The rule's outcome, `null` when it does not match. Throws `Malformed`. */
|
|
239
|
+
function evaluateRule(flag, rule, index, context, fallback) {
|
|
240
|
+
const attributes = context.attributes ?? {};
|
|
241
|
+
for (const condition of rule.conditions ?? []) {
|
|
242
|
+
if (!conditionHolds(condition, attributes))
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
// Shape first, subject second, as on the server: a malformed split is an
|
|
246
|
+
// ERROR even for callers a partial rollout or a missing key would skip.
|
|
247
|
+
if (rule.serve?.kind === 'split')
|
|
248
|
+
checkSplit(flag, rule.serve.split ?? []);
|
|
249
|
+
const subject = context.key ? context.key : null;
|
|
250
|
+
let bucketed = false;
|
|
251
|
+
const percentage = rule.rollout_percentage;
|
|
252
|
+
if (percentage !== undefined && percentage !== null) {
|
|
253
|
+
if (!Number.isFinite(percentage) || percentage < 0 || percentage > 100)
|
|
254
|
+
throw new Malformed();
|
|
255
|
+
if (percentage < 100) {
|
|
256
|
+
if (subject === null)
|
|
257
|
+
return null;
|
|
258
|
+
if (bucket(flag.key, flag.salt ?? '', subject) >= percentage)
|
|
259
|
+
return null;
|
|
260
|
+
bucketed = true;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const reason = (byBucket) => byBucket ? { kind: 'PERCENTAGE_ROLLOUT', index } : { kind: 'RULE_MATCH', index };
|
|
264
|
+
const serve = rule.serve;
|
|
265
|
+
switch (serve?.kind) {
|
|
266
|
+
case 'value':
|
|
267
|
+
return sanitized(flag, serve.value, reason(bucketed), fallback);
|
|
268
|
+
case 'variant': {
|
|
269
|
+
const variant = findVariant(flag, serve.variant);
|
|
270
|
+
return sanitized(flag, variant.value, reason(bucketed), fallback, variant.key);
|
|
271
|
+
}
|
|
272
|
+
case 'split': {
|
|
273
|
+
if (subject === null)
|
|
274
|
+
return null;
|
|
275
|
+
const variant = findVariant(flag, pickSplit(flag, serve.split ?? [], subject));
|
|
276
|
+
return sanitized(flag, variant.value, reason(true), fallback, variant.key);
|
|
277
|
+
}
|
|
278
|
+
default:
|
|
279
|
+
throw new Malformed();
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/** Evaluate one flag. Always returns a usable value. */
|
|
283
|
+
export function evaluate(flag, context, fallback) {
|
|
62
284
|
if (!flag) {
|
|
63
285
|
return outcome(fallback, { kind: 'FLAG_NOT_FOUND' });
|
|
64
286
|
}
|
|
65
287
|
if (!flag.enabled) {
|
|
66
288
|
return sanitized(flag, flag.default_value, { kind: 'DISABLED' }, fallback);
|
|
67
289
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
290
|
+
const rules = flag.rules ?? [];
|
|
291
|
+
for (let index = 0; index < rules.length; index++) {
|
|
292
|
+
try {
|
|
293
|
+
const result = evaluateRule(flag, rules[index], index, context ?? {}, fallback);
|
|
294
|
+
if (result)
|
|
295
|
+
return result;
|
|
296
|
+
}
|
|
297
|
+
catch (error) {
|
|
298
|
+
if (error instanceof Malformed)
|
|
299
|
+
return degraded(flag, fallback);
|
|
300
|
+
throw error;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
73
303
|
if (flag.environment_value !== null && flag.environment_value !== undefined) {
|
|
74
304
|
return sanitized(flag, flag.environment_value, { kind: 'ENVIRONMENT_VALUE' }, fallback);
|
|
75
305
|
}
|
package/dist/flags/index.js
CHANGED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
3
|
+
/**
|
|
4
|
+
* Synchronous SHA-256 over a UTF-8 string, returning the first four bytes of
|
|
5
|
+
* the digest as an unsigned 32-bit integer.
|
|
6
|
+
*
|
|
7
|
+
* Bucketing must be synchronous (flag reads are) and this package also runs
|
|
8
|
+
* in browsers, so neither `node:crypto` nor the async WebCrypto digest fits.
|
|
9
|
+
* Only the leading 32 bits are needed, which is all `bucket()` reads.
|
|
10
|
+
*/
|
|
11
|
+
const K = new Uint32Array([
|
|
12
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
13
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
14
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
15
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
16
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
17
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
18
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
19
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
|
|
20
|
+
]);
|
|
21
|
+
const rotr = (x, n) => (x >>> n) | (x << (32 - n));
|
|
22
|
+
/** Full SHA-256 digest of `input` (UTF-8). */
|
|
23
|
+
export function sha256(input) {
|
|
24
|
+
const data = new TextEncoder().encode(input);
|
|
25
|
+
const bitLength = data.length * 8;
|
|
26
|
+
const paddedLength = (((data.length + 9 + 63) >> 6) << 6);
|
|
27
|
+
const block = new Uint8Array(paddedLength);
|
|
28
|
+
block.set(data);
|
|
29
|
+
block[data.length] = 0x80;
|
|
30
|
+
const view = new DataView(block.buffer);
|
|
31
|
+
view.setUint32(paddedLength - 8, Math.floor(bitLength / 0x100000000));
|
|
32
|
+
view.setUint32(paddedLength - 4, bitLength >>> 0);
|
|
33
|
+
const h = new Uint32Array([
|
|
34
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
35
|
+
]);
|
|
36
|
+
const w = new Uint32Array(64);
|
|
37
|
+
for (let offset = 0; offset < paddedLength; offset += 64) {
|
|
38
|
+
for (let i = 0; i < 16; i++)
|
|
39
|
+
w[i] = view.getUint32(offset + i * 4);
|
|
40
|
+
for (let i = 16; i < 64; i++) {
|
|
41
|
+
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >>> 3);
|
|
42
|
+
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >>> 10);
|
|
43
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
|
|
44
|
+
}
|
|
45
|
+
let [a, b, c, d, e, f, g, hh] = h;
|
|
46
|
+
for (let i = 0; i < 64; i++) {
|
|
47
|
+
const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
48
|
+
const ch = (e & f) ^ (~e & g);
|
|
49
|
+
const t1 = (hh + S1 + ch + K[i] + w[i]) >>> 0;
|
|
50
|
+
const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
|
51
|
+
const maj = (a & b) ^ (a & c) ^ (b & c);
|
|
52
|
+
const t2 = (S0 + maj) >>> 0;
|
|
53
|
+
hh = g;
|
|
54
|
+
g = f;
|
|
55
|
+
f = e;
|
|
56
|
+
e = (d + t1) >>> 0;
|
|
57
|
+
d = c;
|
|
58
|
+
c = b;
|
|
59
|
+
b = a;
|
|
60
|
+
a = (t1 + t2) >>> 0;
|
|
61
|
+
}
|
|
62
|
+
h[0] = (h[0] + a) >>> 0;
|
|
63
|
+
h[1] = (h[1] + b) >>> 0;
|
|
64
|
+
h[2] = (h[2] + c) >>> 0;
|
|
65
|
+
h[3] = (h[3] + d) >>> 0;
|
|
66
|
+
h[4] = (h[4] + e) >>> 0;
|
|
67
|
+
h[5] = (h[5] + f) >>> 0;
|
|
68
|
+
h[6] = (h[6] + g) >>> 0;
|
|
69
|
+
h[7] = (h[7] + hh) >>> 0;
|
|
70
|
+
}
|
|
71
|
+
const out = new Uint8Array(32);
|
|
72
|
+
const outView = new DataView(out.buffer);
|
|
73
|
+
for (let i = 0; i < 8; i++)
|
|
74
|
+
outView.setUint32(i * 4, h[i]);
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
/** The first four digest bytes as a big-endian unsigned integer. */
|
|
78
|
+
export function sha256Prefix32(input) {
|
|
79
|
+
const digest = sha256(input);
|
|
80
|
+
return ((digest[0] << 24) | (digest[1] << 16) | (digest[2] << 8) | digest[3]) >>> 0;
|
|
81
|
+
}
|