@sylphx/sdk 0.30.0 → 0.32.0
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/README.md +123 -0
- package/dist/access.d.ts +126 -20
- package/dist/access.js +111 -15
- package/dist/access.js.map +1 -1
- package/dist/ai.d.ts +1 -2
- package/dist/ai.js +2 -3
- package/dist/ai.js.map +1 -1
- package/dist/artifacts.d.ts +1 -1
- package/dist/artifacts.js +2 -2
- package/dist/assets.d.ts +447 -0
- package/dist/assets.js +266 -0
- package/dist/assets.js.map +1 -0
- package/dist/auth/client.d.ts +239 -0
- package/dist/auth/client.js +337 -0
- package/dist/auth/client.js.map +1 -0
- package/dist/auth/verify.d.ts +92 -0
- package/dist/auth/verify.js +207 -0
- package/dist/auth/verify.js.map +1 -0
- package/dist/auth.d.ts +455 -7
- package/dist/auth.js +416 -5
- package/dist/auth.js.map +1 -1
- package/dist/billing.d.ts +113 -17
- package/dist/billing.js +45 -5
- package/dist/billing.js.map +1 -1
- package/dist/broker.d.ts +5 -4
- package/dist/broker.js +3 -3
- package/dist/broker.js.map +1 -1
- package/dist/build.d.ts +4 -4
- package/dist/build.js +3 -3
- package/dist/common.d.ts +1 -1
- package/dist/common.js +1 -1
- package/dist/config/flags.d.ts +140 -0
- package/dist/config/flags.js +462 -0
- package/dist/config/flags.js.map +1 -0
- package/dist/config.d.ts +380 -15
- package/dist/config.js +283 -5
- package/dist/config.js.map +1 -1
- package/dist/connections.d.ts +6 -6
- package/dist/connections.js +3 -3
- package/dist/data.d.ts +844 -8
- package/dist/data.js +779 -5
- package/dist/data.js.map +1 -1
- package/dist/entitlement.d.ts +18 -9
- package/dist/entitlement.js +4 -4
- package/dist/entitlement.js.map +1 -1
- package/dist/events/realtime.d.ts +69 -0
- package/dist/events/realtime.js +107 -0
- package/dist/events/realtime.js.map +1 -0
- package/dist/events/webhooks.d.ts +40 -0
- package/dist/events/webhooks.js +110 -0
- package/dist/events/webhooks.js.map +1 -0
- package/dist/events.d.ts +369 -22
- package/dist/events.js +311 -8
- package/dist/events.js.map +1 -1
- package/dist/hosting.d.ts +13 -14
- package/dist/hosting.js +3 -4
- package/dist/hosting.js.map +1 -1
- package/dist/ids.d.ts +837 -0
- package/dist/ids.js +287 -0
- package/dist/ids.js.map +1 -0
- package/dist/index.d.ts +12 -1
- package/dist/index.js +16 -2
- package/dist/index.js.map +1 -1
- package/dist/keys.d.ts +2 -2
- package/dist/keys.js +3 -3
- package/dist/localization.d.ts +663 -0
- package/dist/localization.js +491 -0
- package/dist/localization.js.map +1 -0
- package/dist/methods.js +1933 -133
- package/dist/methods.js.map +1 -1
- package/dist/money.d.ts +1406 -0
- package/dist/money.js +1090 -0
- package/dist/money.js.map +1 -0
- package/dist/network.d.ts +4 -5
- package/dist/network.js +3 -4
- package/dist/network.js.map +1 -1
- package/dist/notify.d.ts +718 -127
- package/dist/notify.js +493 -74
- package/dist/notify.js.map +1 -1
- package/dist/observability.d.ts +184 -7
- package/dist/observability.js +156 -8
- package/dist/observability.js.map +1 -1
- package/dist/release.d.ts +14 -15
- package/dist/release.js +3 -4
- package/dist/release.js.map +1 -1
- package/dist/runners.d.ts +2 -3
- package/dist/runners.js +3 -4
- package/dist/runners.js.map +1 -1
- package/dist/runtime.d.ts +42 -5
- package/dist/runtime.js +101 -18
- package/dist/runtime.js.map +1 -1
- package/dist/sandboxes.d.ts +64 -20
- package/dist/sandboxes.js +9 -4
- package/dist/sandboxes.js.map +1 -1
- package/dist/secrets.d.ts +6 -6
- package/dist/secrets.js +4 -4
- package/dist/workflows.d.ts +850 -12
- package/dist/workflows.js +672 -3
- package/dist/workflows.js.map +1 -1
- package/package.json +4 -4
package/dist/build.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// @generated by sylphx-gen 0.1.0 from contracts@
|
|
1
|
+
// @generated by sylphx-gen 0.1.0 from contracts@c8aac0afaf7591d0d5efa1ba6a8540d723d41a97ce7ad5d68601380ef3ab4783. Do not edit.
|
|
2
2
|
// `sylphx.build.v1`.
|
|
3
3
|
import * as runtime from './runtime.js';
|
|
4
4
|
import './common.js';
|
|
@@ -83,8 +83,8 @@ runtime.registerSchemas({
|
|
|
83
83
|
});
|
|
84
84
|
/**
|
|
85
85
|
* BuildService is Sylphx Build: given a repository and a commit, an image with
|
|
86
|
-
* an SBOM and signed SLSA provenance, all written to
|
|
87
|
-
* digest
|
|
86
|
+
* an SBOM and signed SLSA provenance, all written to Sylphx Artifacts by
|
|
87
|
+
* digest.
|
|
88
88
|
*
|
|
89
89
|
* Namespace of Sylphx Build on `Sylphx`.
|
|
90
90
|
*/
|
package/dist/common.d.ts
CHANGED
|
@@ -150,7 +150,7 @@ export type ConditionStatus = 'true' | 'false' | 'unknown' | (string & {});
|
|
|
150
150
|
* codes it may return in `(sylphx.common.v1.method).errors`, without the
|
|
151
151
|
* `ERROR_CODE_` prefix. Adding a code is additive; removing one is breaking.
|
|
152
152
|
*/
|
|
153
|
-
export type ErrorCode = 'UNKNOWN_FIELD' | 'INVALID_FIELD' | 'PAGE_TOKEN_MISMATCH' | 'IDEMPOTENCY_KEY_REUSED' | 'IDEMPOTENCY_IN_PROGRESS' | 'ETAG_MISMATCH' | 'RESOURCE_NOT_FOUND' | 'RESOURCE_ALREADY_EXISTS' | 'UNAUTHENTICATED' | 'PERMISSION_DENIED' | 'DELETION_PROTECTED' | 'RECONCILE_FAILED' | 'PLAN_LIMIT_REACHED' | 'SPEND_LIMIT_REACHED' | 'RATE_LIMITED' | 'INVALID_STATE' | 'INTERNAL' | 'UNAVAILABLE' | 'UNIMPLEMENTED' | 'NOT_YET_PROPAGATED' | 'DEADLINE_EXCEEDED' | 'NOT_FOUND' | 'CONTRACT_SKEW' | 'NO_CAPACITY' | 'SHAPE_NOT_OFFERED' | 'STALE_GENERATION' | 'RESOURCE_IN_USE' | 'CONTROL_HELD' | 'CONTROL_HELD_BY_HUMAN' | (string & {});
|
|
153
|
+
export type ErrorCode = 'UNKNOWN_FIELD' | 'INVALID_FIELD' | 'PAGE_TOKEN_MISMATCH' | 'IDEMPOTENCY_KEY_REUSED' | 'IDEMPOTENCY_IN_PROGRESS' | 'ETAG_MISMATCH' | 'RESOURCE_NOT_FOUND' | 'RESOURCE_ALREADY_EXISTS' | 'UNAUTHENTICATED' | 'PERMISSION_DENIED' | 'DELETION_PROTECTED' | 'RECONCILE_FAILED' | 'PLAN_LIMIT_REACHED' | 'SPEND_LIMIT_REACHED' | 'RATE_LIMITED' | 'INVALID_STATE' | 'INTERNAL' | 'UNAVAILABLE' | 'UNIMPLEMENTED' | 'NOT_YET_PROPAGATED' | 'DEADLINE_EXCEEDED' | 'NOT_FOUND' | 'CONTRACT_SKEW' | 'NO_CAPACITY' | 'SHAPE_NOT_OFFERED' | 'STALE_GENERATION' | 'RESOURCE_IN_USE' | 'CONTROL_HELD' | 'CONTROL_HELD_BY_HUMAN' | 'INVALID_ID' | 'ETAG_CONFLICT' | 'VALIDATE_ONLY_NOT_SUPPORTED' | 'ID_MOVED' | (string & {});
|
|
154
154
|
/**
|
|
155
155
|
* ErrorEffect says what a failed call committed (standards/dx.md "retry and
|
|
156
156
|
* effect status").
|
package/dist/common.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// @generated by sylphx-gen 0.1.0 from contracts@
|
|
1
|
+
// @generated by sylphx-gen 0.1.0 from contracts@c8aac0afaf7591d0d5efa1ba6a8540d723d41a97ce7ad5d68601380ef3ab4783. Do not edit.
|
|
2
2
|
// `sylphx.common.v1`.
|
|
3
3
|
import * as runtime from './runtime.js';
|
|
4
4
|
runtime.registerSchemas({
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sylphx Config feature flags in an app: typed values, targeting rules,
|
|
3
|
+
* percentage rollouts and segments, evaluated the same way the platform
|
|
4
|
+
* evaluates them (the shared vectors in `test/fixtures/evaluate.golden.json`).
|
|
5
|
+
*
|
|
6
|
+
* Two ways to evaluate:
|
|
7
|
+
*
|
|
8
|
+
* - `local` (servers, secret key with `config:read`): download the
|
|
9
|
+
* environment's ruleset (`config_flags:snapshot`), evaluate every call in
|
|
10
|
+
* process, and poll for changes with the ruleset's etag. No call per
|
|
11
|
+
* evaluation.
|
|
12
|
+
* - `remote` (browsers and apps, publishable key with `config:evaluate`):
|
|
13
|
+
* ask `config_flags:evaluate` for one context's values, cached for five
|
|
14
|
+
* minutes. Rules never reach the device, and only flags marked
|
|
15
|
+
* `clientVisible` answer.
|
|
16
|
+
*
|
|
17
|
+
* import { Sylphx } from '@sylphx/sdk'
|
|
18
|
+
* import { Flags } from '@sylphx/sdk/config/flags'
|
|
19
|
+
* const flags = new Flags({ client: new Sylphx(), parent: 'orgs/…/projects/…/envs/…' })
|
|
20
|
+
* if (await flags.isEnabled('new-checkout', { userId: user.id, attributes: { plan } })) { … }
|
|
21
|
+
*
|
|
22
|
+
* OpenFeature: `SylphxServerProvider` (for `@openfeature/server-sdk`) and
|
|
23
|
+
* `SylphxWebProvider` (for `@openfeature/web-sdk`) implement the provider
|
|
24
|
+
* interfaces structurally, so this package keeps zero dependencies.
|
|
25
|
+
*/
|
|
26
|
+
import type { Sylphx } from '../index.js';
|
|
27
|
+
import type { ConfigFlagSpec, ConfigSegmentSpec, EvaluationReason } from '../config.js';
|
|
28
|
+
/** Who and what an evaluation is for. */
|
|
29
|
+
export interface FlagContext {
|
|
30
|
+
/** The signed-in user; the default bucketing subject. */
|
|
31
|
+
userId?: string;
|
|
32
|
+
/** A stable device or visitor id; the subject when there is no user. */
|
|
33
|
+
anonymousId?: string;
|
|
34
|
+
/** Anything rules compare: `plan`, `country`, `app_version`, … */
|
|
35
|
+
attributes?: Record<string, unknown>;
|
|
36
|
+
}
|
|
37
|
+
/** One flag's value for one context, and why. */
|
|
38
|
+
export interface FlagEvaluation {
|
|
39
|
+
value: unknown;
|
|
40
|
+
reason: EvaluationReason;
|
|
41
|
+
ruleIndex?: number;
|
|
42
|
+
}
|
|
43
|
+
/** FNV-1a 32-bit over UTF-8 bytes. */
|
|
44
|
+
export declare function fnv1a32(input: string): number;
|
|
45
|
+
/** The bucket of `subject` for `flag`: FNV-1a 32 of `{flag}:{subject}` mod `buckets`. */
|
|
46
|
+
export declare function bucket(flag: string, subject: string, buckets?: number): number;
|
|
47
|
+
/** Whether `ctx` is in `segment`. */
|
|
48
|
+
export declare function inSegment(segment: ConfigSegmentSpec, ctx: FlagContext): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Evaluates flag `flag` for `ctx`: disabled → the default; else the first rule
|
|
51
|
+
* whose conditions hold and whose percentage admits the subject; else the
|
|
52
|
+
* default.
|
|
53
|
+
*/
|
|
54
|
+
export declare function evaluate(flag: string, spec: ConfigFlagSpec, ctx: FlagContext, segments?: ReadonlyMap<string, ConfigSegmentSpec>): FlagEvaluation;
|
|
55
|
+
export interface FlagsOptions {
|
|
56
|
+
/** The Sylphx client: a secret key for `local`, a publishable key for `remote`. */
|
|
57
|
+
client: Sylphx;
|
|
58
|
+
/** The environment: `orgs/{org}/projects/{project}/envs/{env}`. */
|
|
59
|
+
parent: string;
|
|
60
|
+
/** `local` evaluates from the downloaded ruleset; `remote` asks the API. Default `remote`. */
|
|
61
|
+
mode?: 'local' | 'remote';
|
|
62
|
+
/** How long `remote` answers are reused per context. Default 5 minutes. */
|
|
63
|
+
cacheTtlMs?: number;
|
|
64
|
+
/** The clock (tests). */
|
|
65
|
+
now?: () => number;
|
|
66
|
+
}
|
|
67
|
+
/** Evaluates an environment's flags, locally or through the API. */
|
|
68
|
+
export declare class Flags {
|
|
69
|
+
readonly mode: 'local' | 'remote';
|
|
70
|
+
private readonly client;
|
|
71
|
+
private readonly parent;
|
|
72
|
+
private readonly ttl;
|
|
73
|
+
private readonly now;
|
|
74
|
+
private ruleset;
|
|
75
|
+
private refreshing;
|
|
76
|
+
private readonly cache;
|
|
77
|
+
constructor(options: FlagsOptions);
|
|
78
|
+
/** Whether a boolean flag is on for `ctx`; false when it is missing. */
|
|
79
|
+
isEnabled(flag: string, ctx?: FlagContext): Promise<boolean>;
|
|
80
|
+
/** A flag's value for `ctx`, or `fallback` when the flag is missing. */
|
|
81
|
+
getValue<T>(flag: string, ctx: FlagContext, fallback: T): Promise<T>;
|
|
82
|
+
/** A flag's evaluation for `ctx`. */
|
|
83
|
+
evaluate(flag: string, ctx?: FlagContext): Promise<FlagEvaluation>;
|
|
84
|
+
/** Every flag's evaluation for `ctx`, by id. */
|
|
85
|
+
evaluateAll(ctx?: FlagContext): Promise<Map<string, FlagEvaluation>>;
|
|
86
|
+
/** Forgets cached answers and the ruleset; the next call fetches. */
|
|
87
|
+
reset(): void;
|
|
88
|
+
/** The ruleset, polled with its etag once its poll interval has passed. */
|
|
89
|
+
private current;
|
|
90
|
+
private fetchRuleset;
|
|
91
|
+
}
|
|
92
|
+
/** OpenFeature's evaluation context: `targetingKey` is the subject. */
|
|
93
|
+
export interface OpenFeatureContext {
|
|
94
|
+
targetingKey?: string;
|
|
95
|
+
[key: string]: unknown;
|
|
96
|
+
}
|
|
97
|
+
/** OpenFeature's resolution details. */
|
|
98
|
+
export interface ResolutionDetails<T> {
|
|
99
|
+
value: T;
|
|
100
|
+
reason?: string;
|
|
101
|
+
variant?: string;
|
|
102
|
+
errorCode?: 'FLAG_NOT_FOUND' | 'TYPE_MISMATCH' | 'GENERAL';
|
|
103
|
+
errorMessage?: string;
|
|
104
|
+
}
|
|
105
|
+
/** OpenFeature context → a flag context: `targetingKey` is the user. */
|
|
106
|
+
export declare function fromOpenFeature(context?: OpenFeatureContext): FlagContext;
|
|
107
|
+
/** An OpenFeature server provider (`OpenFeature.setProvider(new SylphxServerProvider(flags))`). */
|
|
108
|
+
export declare class SylphxServerProvider {
|
|
109
|
+
readonly flags: Flags;
|
|
110
|
+
readonly metadata: {
|
|
111
|
+
readonly name: "sylphx";
|
|
112
|
+
};
|
|
113
|
+
readonly runsOn: "server";
|
|
114
|
+
constructor(flags: Flags);
|
|
115
|
+
private run;
|
|
116
|
+
resolveBooleanEvaluation(flag: string, fallback: boolean, context?: OpenFeatureContext): Promise<ResolutionDetails<boolean>>;
|
|
117
|
+
resolveStringEvaluation(flag: string, fallback: string, context?: OpenFeatureContext): Promise<ResolutionDetails<string>>;
|
|
118
|
+
resolveNumberEvaluation(flag: string, fallback: number, context?: OpenFeatureContext): Promise<ResolutionDetails<number>>;
|
|
119
|
+
resolveObjectEvaluation<T>(flag: string, fallback: T, context?: OpenFeatureContext): Promise<ResolutionDetails<T>>;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* An OpenFeature web provider: one `:evaluate` per context (on `initialize`
|
|
123
|
+
* and `onContextChange`), then synchronous answers, as the web SDK requires.
|
|
124
|
+
*/
|
|
125
|
+
export declare class SylphxWebProvider {
|
|
126
|
+
readonly flags: Flags;
|
|
127
|
+
readonly metadata: {
|
|
128
|
+
readonly name: "sylphx";
|
|
129
|
+
};
|
|
130
|
+
readonly runsOn: "client";
|
|
131
|
+
private values;
|
|
132
|
+
constructor(flags: Flags);
|
|
133
|
+
initialize(context?: OpenFeatureContext): Promise<void>;
|
|
134
|
+
onContextChange(_old: OpenFeatureContext, next: OpenFeatureContext): Promise<void>;
|
|
135
|
+
private run;
|
|
136
|
+
resolveBooleanEvaluation(flag: string, fallback: boolean): ResolutionDetails<boolean>;
|
|
137
|
+
resolveStringEvaluation(flag: string, fallback: string): ResolutionDetails<string>;
|
|
138
|
+
resolveNumberEvaluation(flag: string, fallback: number): ResolutionDetails<number>;
|
|
139
|
+
resolveObjectEvaluation<T>(flag: string, fallback: T): ResolutionDetails<T>;
|
|
140
|
+
}
|
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
// @generated by sylphx-gen 0.1.0 from contracts@c8aac0afaf7591d0d5efa1ba6a8540d723d41a97ce7ad5d68601380ef3ab4783. Do not edit.
|
|
2
|
+
/**
|
|
3
|
+
* Sylphx Config feature flags in an app: typed values, targeting rules,
|
|
4
|
+
* percentage rollouts and segments, evaluated the same way the platform
|
|
5
|
+
* evaluates them (the shared vectors in `test/fixtures/evaluate.golden.json`).
|
|
6
|
+
*
|
|
7
|
+
* Two ways to evaluate:
|
|
8
|
+
*
|
|
9
|
+
* - `local` (servers, secret key with `config:read`): download the
|
|
10
|
+
* environment's ruleset (`config_flags:snapshot`), evaluate every call in
|
|
11
|
+
* process, and poll for changes with the ruleset's etag. No call per
|
|
12
|
+
* evaluation.
|
|
13
|
+
* - `remote` (browsers and apps, publishable key with `config:evaluate`):
|
|
14
|
+
* ask `config_flags:evaluate` for one context's values, cached for five
|
|
15
|
+
* minutes. Rules never reach the device, and only flags marked
|
|
16
|
+
* `clientVisible` answer.
|
|
17
|
+
*
|
|
18
|
+
* import { Sylphx } from '@sylphx/sdk'
|
|
19
|
+
* import { Flags } from '@sylphx/sdk/config/flags'
|
|
20
|
+
* const flags = new Flags({ client: new Sylphx(), parent: 'orgs/…/projects/…/envs/…' })
|
|
21
|
+
* if (await flags.isEnabled('new-checkout', { userId: user.id, attributes: { plan } })) { … }
|
|
22
|
+
*
|
|
23
|
+
* OpenFeature: `SylphxServerProvider` (for `@openfeature/server-sdk`) and
|
|
24
|
+
* `SylphxWebProvider` (for `@openfeature/web-sdk`) implement the provider
|
|
25
|
+
* interfaces structurally, so this package keeps zero dependencies.
|
|
26
|
+
*/
|
|
27
|
+
// ── the engine (crates/sylphx-bc-tenancy/src/flags.rs, same vectors) ──────
|
|
28
|
+
const encoder = new TextEncoder();
|
|
29
|
+
/** FNV-1a 32-bit over UTF-8 bytes. */
|
|
30
|
+
export function fnv1a32(input) {
|
|
31
|
+
let hash = 0x811c9dc5;
|
|
32
|
+
for (const byte of encoder.encode(input)) {
|
|
33
|
+
hash ^= byte;
|
|
34
|
+
hash = Math.imul(hash, 0x01000193) >>> 0;
|
|
35
|
+
}
|
|
36
|
+
return hash >>> 0;
|
|
37
|
+
}
|
|
38
|
+
/** The bucket of `subject` for `flag`: FNV-1a 32 of `{flag}:{subject}` mod `buckets`. */
|
|
39
|
+
export function bucket(flag, subject, buckets = 100) {
|
|
40
|
+
return fnv1a32(`${flag}:${subject}`) % (buckets || 100);
|
|
41
|
+
}
|
|
42
|
+
const nonEmpty = (s) => (s ? s : undefined);
|
|
43
|
+
function attribute(ctx, name) {
|
|
44
|
+
if (name === 'user_id')
|
|
45
|
+
return nonEmpty(ctx.userId);
|
|
46
|
+
if (name === 'anonymous_id')
|
|
47
|
+
return nonEmpty(ctx.anonymousId);
|
|
48
|
+
const attrs = ctx.attributes ?? {};
|
|
49
|
+
if (Object.hasOwn(attrs, name))
|
|
50
|
+
return attrs[name] ?? undefined;
|
|
51
|
+
let cur = attrs;
|
|
52
|
+
for (const part of name.split('.')) {
|
|
53
|
+
if (cur === null || typeof cur !== 'object' || Array.isArray(cur))
|
|
54
|
+
return undefined;
|
|
55
|
+
cur = cur[part];
|
|
56
|
+
}
|
|
57
|
+
return cur ?? undefined;
|
|
58
|
+
}
|
|
59
|
+
function subjectOf(bucketAttribute, ctx) {
|
|
60
|
+
if (!bucketAttribute)
|
|
61
|
+
return nonEmpty(ctx.userId) ?? nonEmpty(ctx.anonymousId);
|
|
62
|
+
const v = attribute(ctx, bucketAttribute);
|
|
63
|
+
if (typeof v === 'string')
|
|
64
|
+
return v || undefined;
|
|
65
|
+
if (typeof v === 'number' || typeof v === 'boolean')
|
|
66
|
+
return String(v);
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
function jsonEqual(a, b) {
|
|
70
|
+
if (a === b)
|
|
71
|
+
return true;
|
|
72
|
+
if (typeof a !== typeof b || a === null || b === null || typeof a !== 'object')
|
|
73
|
+
return false;
|
|
74
|
+
if (Array.isArray(a) !== Array.isArray(b))
|
|
75
|
+
return false;
|
|
76
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
77
|
+
return a.length === b.length && a.every((x, i) => jsonEqual(x, b[i]));
|
|
78
|
+
}
|
|
79
|
+
const ka = Object.keys(a);
|
|
80
|
+
const kb = Object.keys(b);
|
|
81
|
+
return (ka.length === kb.length &&
|
|
82
|
+
ka.every((k) => jsonEqual(a[k], b[k])));
|
|
83
|
+
}
|
|
84
|
+
function asNumber(v) {
|
|
85
|
+
if (typeof v === 'number')
|
|
86
|
+
return Number.isFinite(v) ? v : undefined;
|
|
87
|
+
if (typeof v === 'string' && v.trim() !== '') {
|
|
88
|
+
const n = Number(v.trim());
|
|
89
|
+
return Number.isFinite(n) ? n : undefined;
|
|
90
|
+
}
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
function compare(a, b) {
|
|
94
|
+
const x = asNumber(a);
|
|
95
|
+
const y = asNumber(b);
|
|
96
|
+
if (x !== undefined && y !== undefined)
|
|
97
|
+
return x < y ? -1 : x > y ? 1 : 0;
|
|
98
|
+
if (typeof a === 'string' && typeof b === 'string')
|
|
99
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
function parseSemver(v) {
|
|
103
|
+
if (typeof v !== 'string')
|
|
104
|
+
return undefined;
|
|
105
|
+
const m = /^v?(\d+)\.(\d+)\.(\d+)(?:-(.+))?$/.exec(v);
|
|
106
|
+
if (!m)
|
|
107
|
+
return undefined;
|
|
108
|
+
const [maj, min, pat] = [m[1], m[2], m[3]].map((p) => Number(p));
|
|
109
|
+
return { core: [maj, min, pat], pre: m[4] ? m[4].split('.') : [] };
|
|
110
|
+
}
|
|
111
|
+
function semverCompare(a, b) {
|
|
112
|
+
const x = parseSemver(a);
|
|
113
|
+
const y = parseSemver(b);
|
|
114
|
+
if (!x || !y)
|
|
115
|
+
return undefined;
|
|
116
|
+
for (let i = 0; i < 3; i++) {
|
|
117
|
+
const d = (x.core[i] ?? 0) - (y.core[i] ?? 0);
|
|
118
|
+
if (d !== 0)
|
|
119
|
+
return Math.sign(d);
|
|
120
|
+
}
|
|
121
|
+
if (x.pre.length === 0 || y.pre.length === 0) {
|
|
122
|
+
return x.pre.length === y.pre.length ? 0 : x.pre.length === 0 ? 1 : -1;
|
|
123
|
+
}
|
|
124
|
+
for (let i = 0; i < Math.min(x.pre.length, y.pre.length); i++) {
|
|
125
|
+
const p = x.pre[i];
|
|
126
|
+
const q = y.pre[i];
|
|
127
|
+
const pn = /^\d+$/.test(p);
|
|
128
|
+
const qn = /^\d+$/.test(q);
|
|
129
|
+
const d = pn && qn ? Math.sign(Number(p) - Number(q)) : pn ? -1 : qn ? 1 : p < q ? -1 : p > q ? 1 : 0;
|
|
130
|
+
if (d !== 0)
|
|
131
|
+
return d;
|
|
132
|
+
}
|
|
133
|
+
return Math.sign(x.pre.length - y.pre.length);
|
|
134
|
+
}
|
|
135
|
+
function numbersEqual(a, b) {
|
|
136
|
+
return typeof a === 'number' && typeof b === 'number' ? a === b : jsonEqual(a, b);
|
|
137
|
+
}
|
|
138
|
+
function lowerPair(a, b) {
|
|
139
|
+
return typeof a === 'string' && typeof b === 'string' ? [a.toLowerCase(), b.toLowerCase()] : undefined;
|
|
140
|
+
}
|
|
141
|
+
function contains(actual, expected) {
|
|
142
|
+
if (typeof actual === 'string') {
|
|
143
|
+
const p = lowerPair(actual, expected);
|
|
144
|
+
return !!p && p[0].includes(p[1]);
|
|
145
|
+
}
|
|
146
|
+
return Array.isArray(actual) && actual.some((x) => numbersEqual(x, expected));
|
|
147
|
+
}
|
|
148
|
+
const isIn = (actual, list) => Array.isArray(list) && list.some((x) => numbersEqual(actual, x));
|
|
149
|
+
function matches(actual, pattern) {
|
|
150
|
+
if (typeof actual !== 'string' || typeof pattern !== 'string')
|
|
151
|
+
return false;
|
|
152
|
+
try {
|
|
153
|
+
return new RegExp(pattern, 'i').test(actual);
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/** Whether `ctx` is in `segment`. */
|
|
160
|
+
export function inSegment(segment, ctx) {
|
|
161
|
+
const subjects = [nonEmpty(ctx.userId), nonEmpty(ctx.anonymousId)].filter((s) => !!s);
|
|
162
|
+
if (subjects.some((s) => segment.excluded?.includes(s)))
|
|
163
|
+
return false;
|
|
164
|
+
if (subjects.some((s) => segment.included?.includes(s)))
|
|
165
|
+
return true;
|
|
166
|
+
const conditions = segment.conditions ?? [];
|
|
167
|
+
return conditions.length > 0 && conditions.every((c) => holds(c, ctx, undefined));
|
|
168
|
+
}
|
|
169
|
+
function holds(c, ctx, segments) {
|
|
170
|
+
if (c.operator === 'in_segment' || c.operator === 'not_in_segment') {
|
|
171
|
+
const seg = segments && typeof c.value === 'string' ? segments.get(c.value) : undefined;
|
|
172
|
+
const member = !!seg && inSegment(seg, ctx);
|
|
173
|
+
return c.operator === 'in_segment' ? member : !!segments && !member;
|
|
174
|
+
}
|
|
175
|
+
const actual = attribute(ctx, c.attribute ?? '');
|
|
176
|
+
if (actual === undefined || actual === null) {
|
|
177
|
+
return c.operator === 'not_contains' || c.operator === 'not_in';
|
|
178
|
+
}
|
|
179
|
+
const e = c.value;
|
|
180
|
+
switch (c.operator) {
|
|
181
|
+
case 'equals':
|
|
182
|
+
return numbersEqual(actual, e);
|
|
183
|
+
case 'not_equals':
|
|
184
|
+
return !numbersEqual(actual, e);
|
|
185
|
+
case 'greater_than':
|
|
186
|
+
return compare(actual, e) === 1;
|
|
187
|
+
case 'greater_or_equal':
|
|
188
|
+
return (compare(actual, e) ?? -1) >= 0;
|
|
189
|
+
case 'less_than':
|
|
190
|
+
return compare(actual, e) === -1;
|
|
191
|
+
case 'less_or_equal':
|
|
192
|
+
return (compare(actual, e) ?? 1) <= 0;
|
|
193
|
+
case 'contains':
|
|
194
|
+
return contains(actual, e);
|
|
195
|
+
case 'not_contains':
|
|
196
|
+
return !contains(actual, e);
|
|
197
|
+
case 'starts_with': {
|
|
198
|
+
const p = lowerPair(actual, e);
|
|
199
|
+
return !!p && p[0].startsWith(p[1]);
|
|
200
|
+
}
|
|
201
|
+
case 'ends_with': {
|
|
202
|
+
const p = lowerPair(actual, e);
|
|
203
|
+
return !!p && p[0].endsWith(p[1]);
|
|
204
|
+
}
|
|
205
|
+
case 'in':
|
|
206
|
+
return isIn(actual, e);
|
|
207
|
+
case 'not_in':
|
|
208
|
+
return !isIn(actual, e);
|
|
209
|
+
case 'matches':
|
|
210
|
+
return matches(actual, e);
|
|
211
|
+
case 'semver_greater_than':
|
|
212
|
+
return semverCompare(actual, e) === 1;
|
|
213
|
+
case 'semver_greater_or_equal':
|
|
214
|
+
return (semverCompare(actual, e) ?? -1) >= 0;
|
|
215
|
+
case 'semver_less_than':
|
|
216
|
+
return semverCompare(actual, e) === -1;
|
|
217
|
+
case 'semver_less_or_equal':
|
|
218
|
+
return (semverCompare(actual, e) ?? 1) <= 0;
|
|
219
|
+
default:
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Evaluates flag `flag` for `ctx`: disabled → the default; else the first rule
|
|
225
|
+
* whose conditions hold and whose percentage admits the subject; else the
|
|
226
|
+
* default.
|
|
227
|
+
*/
|
|
228
|
+
export function evaluate(flag, spec, ctx, segments = new Map()) {
|
|
229
|
+
const fallback = (reason) => ({ value: spec.defaultValue ?? null, reason });
|
|
230
|
+
if (spec.enabled === false)
|
|
231
|
+
return fallback('disabled');
|
|
232
|
+
const rules = spec.rules ?? [];
|
|
233
|
+
for (const [i, rule] of rules.entries()) {
|
|
234
|
+
if (!(rule.conditions ?? []).every((c) => holds(c, ctx, segments)))
|
|
235
|
+
continue;
|
|
236
|
+
const percentage = rule.percentage ?? 100;
|
|
237
|
+
if (percentage >= 100)
|
|
238
|
+
return { value: rule.value ?? null, reason: 'targeting_match', ruleIndex: i };
|
|
239
|
+
const subject = subjectOf(rule.bucketAttribute, ctx);
|
|
240
|
+
if (subject === undefined || percentage <= 0)
|
|
241
|
+
continue;
|
|
242
|
+
if (bucket(flag, subject) < percentage)
|
|
243
|
+
return { value: rule.value ?? null, reason: 'split', ruleIndex: i };
|
|
244
|
+
}
|
|
245
|
+
return fallback('default');
|
|
246
|
+
}
|
|
247
|
+
function durationMs(d, fallback) {
|
|
248
|
+
const m = d ? /^(\d+(?:\.\d+)?)s$/.exec(d) : null;
|
|
249
|
+
return m ? Number(m[1]) * 1000 : fallback;
|
|
250
|
+
}
|
|
251
|
+
const idOf = (name) => (name ?? '').slice((name ?? '').lastIndexOf('/') + 1);
|
|
252
|
+
const NOT_FOUND = { value: null, reason: 'not_found' };
|
|
253
|
+
/** Evaluates an environment's flags, locally or through the API. */
|
|
254
|
+
export class Flags {
|
|
255
|
+
mode;
|
|
256
|
+
client;
|
|
257
|
+
parent;
|
|
258
|
+
ttl;
|
|
259
|
+
now;
|
|
260
|
+
ruleset;
|
|
261
|
+
refreshing;
|
|
262
|
+
cache = new Map();
|
|
263
|
+
constructor(options) {
|
|
264
|
+
this.client = options.client;
|
|
265
|
+
this.parent = options.parent;
|
|
266
|
+
this.mode = options.mode ?? 'remote';
|
|
267
|
+
this.ttl = options.cacheTtlMs ?? 5 * 60 * 1000;
|
|
268
|
+
this.now = options.now ?? Date.now;
|
|
269
|
+
}
|
|
270
|
+
/** Whether a boolean flag is on for `ctx`; false when it is missing. */
|
|
271
|
+
async isEnabled(flag, ctx = {}) {
|
|
272
|
+
return (await this.evaluate(flag, ctx)).value === true;
|
|
273
|
+
}
|
|
274
|
+
/** A flag's value for `ctx`, or `fallback` when the flag is missing. */
|
|
275
|
+
async getValue(flag, ctx, fallback) {
|
|
276
|
+
const e = await this.evaluate(flag, ctx);
|
|
277
|
+
return e.reason === 'not_found' || e.value === null || e.value === undefined ? fallback : e.value;
|
|
278
|
+
}
|
|
279
|
+
/** A flag's evaluation for `ctx`. */
|
|
280
|
+
async evaluate(flag, ctx = {}) {
|
|
281
|
+
if (this.mode === 'local') {
|
|
282
|
+
const r = await this.current();
|
|
283
|
+
const spec = r.flags.get(flag);
|
|
284
|
+
return spec ? evaluate(flag, spec, ctx, r.segments) : NOT_FOUND;
|
|
285
|
+
}
|
|
286
|
+
return (await this.evaluateAll(ctx)).get(flag) ?? NOT_FOUND;
|
|
287
|
+
}
|
|
288
|
+
/** Every flag's evaluation for `ctx`, by id. */
|
|
289
|
+
async evaluateAll(ctx = {}) {
|
|
290
|
+
if (this.mode === 'local') {
|
|
291
|
+
const r = await this.current();
|
|
292
|
+
return new Map([...r.flags].map(([id, spec]) => [id, evaluate(id, spec, ctx, r.segments)]));
|
|
293
|
+
}
|
|
294
|
+
const key = JSON.stringify([ctx.userId ?? '', ctx.anonymousId ?? '', ctx.attributes ?? {}]);
|
|
295
|
+
const hit = this.cache.get(key);
|
|
296
|
+
if (hit && this.now() - hit.at < this.ttl)
|
|
297
|
+
return hit.values;
|
|
298
|
+
const res = await this.client.config.configFlags.evaluate({
|
|
299
|
+
parent: this.parent,
|
|
300
|
+
context: {
|
|
301
|
+
...(ctx.userId ? { userId: ctx.userId } : {}),
|
|
302
|
+
...(ctx.anonymousId ? { anonymousId: ctx.anonymousId } : {}),
|
|
303
|
+
...(ctx.attributes ? { attributes: ctx.attributes } : {}),
|
|
304
|
+
},
|
|
305
|
+
});
|
|
306
|
+
const values = new Map();
|
|
307
|
+
for (const e of res.evaluations ?? []) {
|
|
308
|
+
values.set(e.configFlag ?? '', {
|
|
309
|
+
value: e.value ?? null,
|
|
310
|
+
reason: e.reason ?? 'default',
|
|
311
|
+
...(e.ruleIndex !== undefined ? { ruleIndex: e.ruleIndex } : {}),
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
if (this.cache.size > 1000)
|
|
315
|
+
this.cache.clear();
|
|
316
|
+
this.cache.set(key, { at: this.now(), values });
|
|
317
|
+
return values;
|
|
318
|
+
}
|
|
319
|
+
/** Forgets cached answers and the ruleset; the next call fetches. */
|
|
320
|
+
reset() {
|
|
321
|
+
this.cache.clear();
|
|
322
|
+
this.ruleset = undefined;
|
|
323
|
+
}
|
|
324
|
+
/** The ruleset, polled with its etag once its poll interval has passed. */
|
|
325
|
+
async current() {
|
|
326
|
+
const r = this.ruleset;
|
|
327
|
+
if (r && this.now() - r.fetchedAt < r.pollMs)
|
|
328
|
+
return r;
|
|
329
|
+
this.refreshing ??= this.fetchRuleset(r).finally(() => {
|
|
330
|
+
this.refreshing = undefined;
|
|
331
|
+
});
|
|
332
|
+
try {
|
|
333
|
+
return await this.refreshing;
|
|
334
|
+
}
|
|
335
|
+
catch (error) {
|
|
336
|
+
// A Cell keeps its last good ruleset while the API is away.
|
|
337
|
+
if (r)
|
|
338
|
+
return r;
|
|
339
|
+
throw error;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
async fetchRuleset(held) {
|
|
343
|
+
const snap = await this.client.config.configFlags.snapshot({
|
|
344
|
+
parent: this.parent,
|
|
345
|
+
...(held ? { ifNoneMatch: held.etag } : {}),
|
|
346
|
+
});
|
|
347
|
+
const pollMs = durationMs(snap.pollInterval, 30_000);
|
|
348
|
+
if (held && snap.notModified) {
|
|
349
|
+
this.ruleset = { ...held, fetchedAt: this.now(), pollMs };
|
|
350
|
+
return this.ruleset;
|
|
351
|
+
}
|
|
352
|
+
const flags = new Map();
|
|
353
|
+
for (const f of snap.configFlags ?? [])
|
|
354
|
+
if (f.spec)
|
|
355
|
+
flags.set(idOf(f.name), f.spec);
|
|
356
|
+
const segments = new Map();
|
|
357
|
+
for (const s of snap.configSegments ?? [])
|
|
358
|
+
if (s.spec)
|
|
359
|
+
segments.set(idOf(s.name), s.spec);
|
|
360
|
+
this.ruleset = { etag: snap.etag ?? '', flags, segments, fetchedAt: this.now(), pollMs };
|
|
361
|
+
return this.ruleset;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
/** OpenFeature context → a flag context: `targetingKey` is the user. */
|
|
365
|
+
export function fromOpenFeature(context = {}) {
|
|
366
|
+
const { targetingKey, anonymousId, ...attributes } = context;
|
|
367
|
+
return {
|
|
368
|
+
...(typeof targetingKey === 'string' && targetingKey ? { userId: targetingKey } : {}),
|
|
369
|
+
...(typeof anonymousId === 'string' && anonymousId ? { anonymousId } : {}),
|
|
370
|
+
attributes,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
const REASONS = {
|
|
374
|
+
targeting_match: 'TARGETING_MATCH',
|
|
375
|
+
split: 'SPLIT',
|
|
376
|
+
default: 'DEFAULT',
|
|
377
|
+
disabled: 'DISABLED',
|
|
378
|
+
};
|
|
379
|
+
function resolve(e, kind, fallback) {
|
|
380
|
+
if (e.reason === 'not_found') {
|
|
381
|
+
return { value: fallback, reason: 'ERROR', errorCode: 'FLAG_NOT_FOUND', errorMessage: 'No flag has this id.' };
|
|
382
|
+
}
|
|
383
|
+
const v = e.value;
|
|
384
|
+
const fits = kind === 'object' ? v !== null && typeof v === 'object' : typeof v === kind;
|
|
385
|
+
if (!fits) {
|
|
386
|
+
return {
|
|
387
|
+
value: fallback,
|
|
388
|
+
reason: 'ERROR',
|
|
389
|
+
errorCode: 'TYPE_MISMATCH',
|
|
390
|
+
errorMessage: `The flag's value is not a ${kind}.`,
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
return {
|
|
394
|
+
value: v,
|
|
395
|
+
reason: REASONS[e.reason] ?? 'UNKNOWN',
|
|
396
|
+
...(e.ruleIndex !== undefined ? { variant: `rule-${e.ruleIndex}` } : {}),
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
/** An OpenFeature server provider (`OpenFeature.setProvider(new SylphxServerProvider(flags))`). */
|
|
400
|
+
export class SylphxServerProvider {
|
|
401
|
+
flags;
|
|
402
|
+
metadata = { name: 'sylphx' };
|
|
403
|
+
runsOn = 'server';
|
|
404
|
+
constructor(flags) {
|
|
405
|
+
this.flags = flags;
|
|
406
|
+
}
|
|
407
|
+
async run(flag, kind, fallback, context) {
|
|
408
|
+
try {
|
|
409
|
+
return resolve(await this.flags.evaluate(flag, fromOpenFeature(context)), kind, fallback);
|
|
410
|
+
}
|
|
411
|
+
catch (error) {
|
|
412
|
+
return { value: fallback, reason: 'ERROR', errorCode: 'GENERAL', errorMessage: String(error) };
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
resolveBooleanEvaluation(flag, fallback, context) {
|
|
416
|
+
return this.run(flag, 'boolean', fallback, context);
|
|
417
|
+
}
|
|
418
|
+
resolveStringEvaluation(flag, fallback, context) {
|
|
419
|
+
return this.run(flag, 'string', fallback, context);
|
|
420
|
+
}
|
|
421
|
+
resolveNumberEvaluation(flag, fallback, context) {
|
|
422
|
+
return this.run(flag, 'number', fallback, context);
|
|
423
|
+
}
|
|
424
|
+
resolveObjectEvaluation(flag, fallback, context) {
|
|
425
|
+
return this.run(flag, 'object', fallback, context);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* An OpenFeature web provider: one `:evaluate` per context (on `initialize`
|
|
430
|
+
* and `onContextChange`), then synchronous answers, as the web SDK requires.
|
|
431
|
+
*/
|
|
432
|
+
export class SylphxWebProvider {
|
|
433
|
+
flags;
|
|
434
|
+
metadata = { name: 'sylphx' };
|
|
435
|
+
runsOn = 'client';
|
|
436
|
+
values = new Map();
|
|
437
|
+
constructor(flags) {
|
|
438
|
+
this.flags = flags;
|
|
439
|
+
}
|
|
440
|
+
async initialize(context) {
|
|
441
|
+
this.values = await this.flags.evaluateAll(fromOpenFeature(context));
|
|
442
|
+
}
|
|
443
|
+
async onContextChange(_old, next) {
|
|
444
|
+
await this.initialize(next);
|
|
445
|
+
}
|
|
446
|
+
run(flag, kind, fallback) {
|
|
447
|
+
return resolve(this.values.get(flag) ?? NOT_FOUND, kind, fallback);
|
|
448
|
+
}
|
|
449
|
+
resolveBooleanEvaluation(flag, fallback) {
|
|
450
|
+
return this.run(flag, 'boolean', fallback);
|
|
451
|
+
}
|
|
452
|
+
resolveStringEvaluation(flag, fallback) {
|
|
453
|
+
return this.run(flag, 'string', fallback);
|
|
454
|
+
}
|
|
455
|
+
resolveNumberEvaluation(flag, fallback) {
|
|
456
|
+
return this.run(flag, 'number', fallback);
|
|
457
|
+
}
|
|
458
|
+
resolveObjectEvaluation(flag, fallback) {
|
|
459
|
+
return this.run(flag, 'object', fallback);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
//# sourceMappingURL=flags.js.map
|