@temps-sdk/node-sdk 0.0.5 → 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 +89 -0
- package/dist/flags/client.js +279 -0
- package/dist/flags/evaluate.d.ts +23 -0
- package/dist/flags/evaluate.js +307 -0
- package/dist/flags/index.d.ts +3 -0
- package/dist/flags/index.js +4 -0
- package/dist/flags/sha256.d.ts +4 -0
- package/dist/flags/sha256.js +81 -0
- package/dist/flags/types.d.ts +147 -0
- package/dist/flags/types.js +3 -0
- package/dist/index.d.ts +88 -0
- package/dist/index.js +9201 -548
- package/package.json +8 -3
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: 2024-2026 Temps Contributors
|
|
2
|
+
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
3
|
+
/**
|
|
4
|
+
* Flag evaluation. Pure, synchronous, total — no I/O, never throws.
|
|
5
|
+
*
|
|
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.
|
|
10
|
+
*
|
|
11
|
+
* Resolution order:
|
|
12
|
+
*
|
|
13
|
+
* 1. flag missing or archived -> caller's fallback FLAG_NOT_FOUND
|
|
14
|
+
* 2. enabled === false -> default_value DISABLED
|
|
15
|
+
* 3. first matching rule -> its serve RULE_MATCH{i}
|
|
16
|
+
* | PERCENTAGE_ROLLOUT{i}
|
|
17
|
+
* 4. environment value present -> that value ENVIRONMENT_VALUE
|
|
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.
|
|
33
|
+
*/
|
|
34
|
+
export const MAX_MATCHED_LENGTH = 1024;
|
|
35
|
+
/**
|
|
36
|
+
* Whether a concrete JSON value is admissible for a declared flag type.
|
|
37
|
+
*
|
|
38
|
+
* `json` accepts anything except `null`: a flag must always resolve to
|
|
39
|
+
* something usable, and `null` is indistinguishable from "unset".
|
|
40
|
+
*/
|
|
41
|
+
export function valueMatchesType(valueType, value) {
|
|
42
|
+
switch (valueType) {
|
|
43
|
+
case 'bool':
|
|
44
|
+
return typeof value === 'boolean';
|
|
45
|
+
case 'string':
|
|
46
|
+
return typeof value === 'string';
|
|
47
|
+
case 'number':
|
|
48
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
49
|
+
case 'json':
|
|
50
|
+
return value !== null && value !== undefined;
|
|
51
|
+
default:
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
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' });
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Serve `value` if it matches the flag's declared type, otherwise degrade to
|
|
77
|
+
* the flag default. An operator typing the wrong thing into the flag UI must
|
|
78
|
+
* not break the running app.
|
|
79
|
+
*/
|
|
80
|
+
function sanitized(flag, value, reason, fallback, variant = null) {
|
|
81
|
+
if (valueMatchesType(flag.value_type, value)) {
|
|
82
|
+
return outcome(value, reason, variant);
|
|
83
|
+
}
|
|
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;
|
|
103
|
+
}
|
|
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;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
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.
|
|
216
|
+
*/
|
|
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) {
|
|
284
|
+
if (!flag) {
|
|
285
|
+
return outcome(fallback, { kind: 'FLAG_NOT_FOUND' });
|
|
286
|
+
}
|
|
287
|
+
if (!flag.enabled) {
|
|
288
|
+
return sanitized(flag, flag.default_value, { kind: 'DISABLED' }, fallback);
|
|
289
|
+
}
|
|
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
|
+
}
|
|
303
|
+
if (flag.environment_value !== null && flag.environment_value !== undefined) {
|
|
304
|
+
return sanitized(flag, flag.environment_value, { kind: 'ENVIRONMENT_VALUE' }, fallback);
|
|
305
|
+
}
|
|
306
|
+
return sanitized(flag, flag.default_value, { kind: 'DEFAULT' }, fallback);
|
|
307
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feature-flag types. These mirror `crates/temps-flags/src/eval.rs` exactly —
|
|
3
|
+
* the SDK evaluates locally, so the two implementations must agree or the same
|
|
4
|
+
* flag resolves differently on the server and in the app.
|
|
5
|
+
*/
|
|
6
|
+
export type FlagValueType = 'bool' | 'string' | 'number' | 'json';
|
|
7
|
+
/**
|
|
8
|
+
* Why an evaluation produced the value it did.
|
|
9
|
+
*
|
|
10
|
+
* `RULE_MATCH` means targeting rule `index` matched on its conditions;
|
|
11
|
+
* `PERCENTAGE_ROLLOUT` means the subject's bucket decided it (a rollout
|
|
12
|
+
* percentage or a variant split).
|
|
13
|
+
*/
|
|
14
|
+
export type EvalReason = {
|
|
15
|
+
kind: 'FLAG_NOT_FOUND';
|
|
16
|
+
} | {
|
|
17
|
+
kind: 'DISABLED';
|
|
18
|
+
} | {
|
|
19
|
+
kind: 'RULE_MATCH';
|
|
20
|
+
index: number;
|
|
21
|
+
} | {
|
|
22
|
+
kind: 'PERCENTAGE_ROLLOUT';
|
|
23
|
+
index: number;
|
|
24
|
+
} | {
|
|
25
|
+
kind: 'ENVIRONMENT_VALUE';
|
|
26
|
+
} | {
|
|
27
|
+
kind: 'DEFAULT';
|
|
28
|
+
} | {
|
|
29
|
+
kind: 'ERROR';
|
|
30
|
+
};
|
|
31
|
+
export type AttributeValue = string | number | boolean;
|
|
32
|
+
/** A named value a flag can serve. */
|
|
33
|
+
export interface FlagVariant {
|
|
34
|
+
key: string;
|
|
35
|
+
value: unknown;
|
|
36
|
+
description?: string | null;
|
|
37
|
+
}
|
|
38
|
+
export type ConditionOperator = 'eq' | 'neq' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'matches' | 'gt' | 'gte' | 'lt' | 'lte' | 'exists' | 'not_exists';
|
|
39
|
+
export interface RuleCondition {
|
|
40
|
+
attribute: string;
|
|
41
|
+
operator: ConditionOperator;
|
|
42
|
+
/** Absent for `exists`/`not_exists`; a list for `in`/`not_in`. */
|
|
43
|
+
value?: AttributeValue | AttributeValue[] | null;
|
|
44
|
+
}
|
|
45
|
+
export interface VariantWeight {
|
|
46
|
+
variant: string;
|
|
47
|
+
weight: number;
|
|
48
|
+
}
|
|
49
|
+
export type RuleServe = {
|
|
50
|
+
kind: 'value';
|
|
51
|
+
value: unknown;
|
|
52
|
+
} | {
|
|
53
|
+
kind: 'variant';
|
|
54
|
+
variant: string;
|
|
55
|
+
} | {
|
|
56
|
+
kind: 'split';
|
|
57
|
+
split: VariantWeight[];
|
|
58
|
+
};
|
|
59
|
+
/** `when <all conditions> [within rollout_percentage] serve <serve>`. */
|
|
60
|
+
export interface FlagRule {
|
|
61
|
+
description?: string | null;
|
|
62
|
+
conditions?: RuleCondition[];
|
|
63
|
+
/** 0-100; absent means every matching subject. */
|
|
64
|
+
rollout_percentage?: number | null;
|
|
65
|
+
serve: RuleServe;
|
|
66
|
+
}
|
|
67
|
+
/** One flag, already resolved down to a single environment. */
|
|
68
|
+
export interface FlagSnapshot {
|
|
69
|
+
key: string;
|
|
70
|
+
value_type: FlagValueType;
|
|
71
|
+
default_value: unknown;
|
|
72
|
+
/** `false` means the kill switch is engaged for this environment. */
|
|
73
|
+
enabled: boolean;
|
|
74
|
+
/** `null` means "inherit `default_value`". */
|
|
75
|
+
environment_value?: unknown | null;
|
|
76
|
+
/** Bucketing salt for rollouts and splits. Server-side only. */
|
|
77
|
+
salt?: string;
|
|
78
|
+
variants?: FlagVariant[];
|
|
79
|
+
/** Ordered targeting rules; first match wins. */
|
|
80
|
+
rules?: FlagRule[];
|
|
81
|
+
}
|
|
82
|
+
export interface FlagSnapshotResponse {
|
|
83
|
+
environment_id: number;
|
|
84
|
+
flags: FlagSnapshot[];
|
|
85
|
+
}
|
|
86
|
+
/** Everything you know about the subject being evaluated. */
|
|
87
|
+
export interface EvalContext {
|
|
88
|
+
/**
|
|
89
|
+
* Stable bucketing subject (user id, account id, device id). Rules with a
|
|
90
|
+
* rollout below 100% or a variant split are skipped without one; a 100%
|
|
91
|
+
* rollout serving a value or a variant matches without it.
|
|
92
|
+
*/
|
|
93
|
+
key?: string;
|
|
94
|
+
/** Targeting attributes, e.g. `{ plan: 'enterprise', country: 'DE' }`. */
|
|
95
|
+
attributes?: Record<string, AttributeValue>;
|
|
96
|
+
}
|
|
97
|
+
export interface Evaluation<T> {
|
|
98
|
+
value: T;
|
|
99
|
+
/** The variant a targeting rule served, or `null` for plain values. */
|
|
100
|
+
variant: string | null;
|
|
101
|
+
reason: EvalReason;
|
|
102
|
+
}
|
|
103
|
+
export interface FlagsClientOptions {
|
|
104
|
+
/**
|
|
105
|
+
* Control-plane API base URL. Defaults to `TEMPS_API_URL`, which Temps
|
|
106
|
+
* injects into every deployment.
|
|
107
|
+
*/
|
|
108
|
+
apiUrl?: string;
|
|
109
|
+
/**
|
|
110
|
+
* Deployment token. Defaults to `TEMPS_API_TOKEN`, injected alongside
|
|
111
|
+
* `TEMPS_API_URL`.
|
|
112
|
+
*/
|
|
113
|
+
apiToken?: string;
|
|
114
|
+
/**
|
|
115
|
+
* Only needed when the token is project-wide rather than scoped to one
|
|
116
|
+
* environment.
|
|
117
|
+
*/
|
|
118
|
+
environmentId?: number;
|
|
119
|
+
/**
|
|
120
|
+
* Background refresh interval in milliseconds. Default 30s. Unchanged flags
|
|
121
|
+
* cost a 304, not a payload.
|
|
122
|
+
*
|
|
123
|
+
* Falls back to `TEMPS_FLAGS_REFRESH_INTERVAL_MS` when unset, so a
|
|
124
|
+
* deployment can tighten (or loosen) polling without a code change or
|
|
125
|
+
* redeploy of the SDK itself — just the env var.
|
|
126
|
+
*/
|
|
127
|
+
refreshIntervalMs?: number;
|
|
128
|
+
/** Per-request timeout in milliseconds. Default 5s. */
|
|
129
|
+
timeoutMs?: number;
|
|
130
|
+
/**
|
|
131
|
+
* Report which flag keys were actually evaluated, on the refresh interval.
|
|
132
|
+
* Defaults to `true`.
|
|
133
|
+
*
|
|
134
|
+
* This is what makes "last evaluated" in the console meaningful — the server
|
|
135
|
+
* hands out the whole flag set and evaluation happens locally, so without it
|
|
136
|
+
* the control plane cannot tell a flag live code still reads from one nothing
|
|
137
|
+
* has called in a year. Only keys that exist in the snapshot are reported,
|
|
138
|
+
* and only on the interval, never per call.
|
|
139
|
+
*/
|
|
140
|
+
reportExposure?: boolean;
|
|
141
|
+
/**
|
|
142
|
+
* Called when a background refresh fails. Defaults to a `console.warn`.
|
|
143
|
+
* Failures never throw and never clear the cache — the last good snapshot
|
|
144
|
+
* keeps serving.
|
|
145
|
+
*/
|
|
146
|
+
onError?: (error: Error) => void;
|
|
147
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,10 @@ import * as sdk from './client/sdk.gen';
|
|
|
3
3
|
export * from './client/types.gen';
|
|
4
4
|
export * from './client/sdk.gen';
|
|
5
5
|
export * as ErrorTracking from './errors';
|
|
6
|
+
export * as Flags from './flags';
|
|
7
|
+
export { FlagsClient, flags } from './flags';
|
|
8
|
+
export * as VisitorAnalytics from './analytics';
|
|
9
|
+
export { analytics } from './analytics';
|
|
6
10
|
export interface TempsClientConfig {
|
|
7
11
|
baseUrl: string;
|
|
8
12
|
apiKey?: string;
|
|
@@ -1574,6 +1578,48 @@ declare class Email {
|
|
|
1574
1578
|
request: Request;
|
|
1575
1579
|
response: Response;
|
|
1576
1580
|
}>;
|
|
1581
|
+
listAuthorizedProjects: (options: Parameters<typeof sdk.listEmailDomainProjects>[0]) => Promise<{
|
|
1582
|
+
data: import(".").AuthorizedEmailDomainProjectResponse[];
|
|
1583
|
+
request: Request;
|
|
1584
|
+
response: Response;
|
|
1585
|
+
}> | Promise<({
|
|
1586
|
+
data: import(".").AuthorizedEmailDomainProjectResponse[];
|
|
1587
|
+
error: undefined;
|
|
1588
|
+
} | {
|
|
1589
|
+
data: undefined;
|
|
1590
|
+
error: unknown;
|
|
1591
|
+
}) & {
|
|
1592
|
+
request: Request;
|
|
1593
|
+
response: Response;
|
|
1594
|
+
}>;
|
|
1595
|
+
authorizeProject: (options: Parameters<typeof sdk.authorizeEmailDomainProject>[0]) => Promise<{
|
|
1596
|
+
data: void;
|
|
1597
|
+
request: Request;
|
|
1598
|
+
response: Response;
|
|
1599
|
+
}> | Promise<({
|
|
1600
|
+
data: void;
|
|
1601
|
+
error: undefined;
|
|
1602
|
+
} | {
|
|
1603
|
+
data: undefined;
|
|
1604
|
+
error: unknown;
|
|
1605
|
+
}) & {
|
|
1606
|
+
request: Request;
|
|
1607
|
+
response: Response;
|
|
1608
|
+
}>;
|
|
1609
|
+
revokeProject: (options: Parameters<typeof sdk.revokeEmailDomainProject>[0]) => Promise<{
|
|
1610
|
+
data: void;
|
|
1611
|
+
request: Request;
|
|
1612
|
+
response: Response;
|
|
1613
|
+
}> | Promise<({
|
|
1614
|
+
data: void;
|
|
1615
|
+
error: undefined;
|
|
1616
|
+
} | {
|
|
1617
|
+
data: undefined;
|
|
1618
|
+
error: unknown;
|
|
1619
|
+
}) & {
|
|
1620
|
+
request: Request;
|
|
1621
|
+
response: Response;
|
|
1622
|
+
}>;
|
|
1577
1623
|
send: (options: Parameters<typeof sdk.sendEmail>[0]) => Promise<{
|
|
1578
1624
|
data: import(".").SendEmailResponseBody;
|
|
1579
1625
|
request: Request;
|
|
@@ -3176,6 +3222,48 @@ declare class Projects {
|
|
|
3176
3222
|
request: Request;
|
|
3177
3223
|
response: Response;
|
|
3178
3224
|
}>;
|
|
3225
|
+
getServiceTemplate: (options: Parameters<typeof sdk.getProjectServiceTemplate>[0]) => Promise<{
|
|
3226
|
+
data: import(".").ServiceTemplateInstanceResponse;
|
|
3227
|
+
request: Request;
|
|
3228
|
+
response: Response;
|
|
3229
|
+
}> | Promise<({
|
|
3230
|
+
data: import(".").ServiceTemplateInstanceResponse;
|
|
3231
|
+
error: undefined;
|
|
3232
|
+
} | {
|
|
3233
|
+
data: undefined;
|
|
3234
|
+
error: unknown;
|
|
3235
|
+
}) & {
|
|
3236
|
+
request: Request;
|
|
3237
|
+
response: Response;
|
|
3238
|
+
}>;
|
|
3239
|
+
updateServiceRuntime: (options: Parameters<typeof sdk.updateServiceTemplateRuntime>[0]) => Promise<{
|
|
3240
|
+
data: import(".").ProjectResponse;
|
|
3241
|
+
request: Request;
|
|
3242
|
+
response: Response;
|
|
3243
|
+
}> | Promise<({
|
|
3244
|
+
data: import(".").ProjectResponse;
|
|
3245
|
+
error: undefined;
|
|
3246
|
+
} | {
|
|
3247
|
+
data: undefined;
|
|
3248
|
+
error: unknown;
|
|
3249
|
+
}) & {
|
|
3250
|
+
request: Request;
|
|
3251
|
+
response: Response;
|
|
3252
|
+
}>;
|
|
3253
|
+
upgradeServiceTemplate: (options: Parameters<typeof sdk.upgradeProjectServiceTemplate>[0]) => Promise<{
|
|
3254
|
+
data: import(".").ProjectResponse;
|
|
3255
|
+
request: Request;
|
|
3256
|
+
response: Response;
|
|
3257
|
+
}> | Promise<({
|
|
3258
|
+
data: import(".").ProjectResponse;
|
|
3259
|
+
error: undefined;
|
|
3260
|
+
} | {
|
|
3261
|
+
data: undefined;
|
|
3262
|
+
error: unknown;
|
|
3263
|
+
}) & {
|
|
3264
|
+
request: Request;
|
|
3265
|
+
response: Response;
|
|
3266
|
+
}>;
|
|
3179
3267
|
getDeployments: (options: Parameters<typeof sdk.getProjectDeployments>[0]) => Promise<{
|
|
3180
3268
|
data: import(".").DeploymentListResponse;
|
|
3181
3269
|
request: Request;
|