@fireweaveai/server-sdk-cjs 0.0.0-reserve.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/LICENSE +21 -0
- package/README.md +55 -0
- package/dist/application/client.d.ts +77 -0
- package/dist/application/client.js +144 -0
- package/dist/application/mode.d.ts +70 -0
- package/dist/application/mode.js +74 -0
- package/dist/application/ports.d.ts +94 -0
- package/dist/application/ports.js +2 -0
- package/dist/application/runtime.d.ts +101 -0
- package/dist/application/runtime.js +346 -0
- package/dist/domain/context.d.ts +67 -0
- package/dist/domain/context.js +90 -0
- package/dist/domain/errors.d.ts +45 -0
- package/dist/domain/errors.js +99 -0
- package/dist/domain/target.d.ts +2 -0
- package/dist/domain/target.js +2 -0
- package/dist/domain/types.d.ts +107 -0
- package/dist/domain/types.js +2 -0
- package/dist/domain/validation.d.ts +124 -0
- package/dist/domain/validation.js +355 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +52 -0
- package/dist/infrastructure/adapters/inmemory.d.ts +77 -0
- package/dist/infrastructure/adapters/inmemory.js +151 -0
- package/dist/infrastructure/adapters/local.d.ts +108 -0
- package/dist/infrastructure/adapters/local.js +105 -0
- package/dist/infrastructure/adapters/remote.d.ts +74 -0
- package/dist/infrastructure/adapters/remote.js +373 -0
- package/dist/infrastructure/hosts.d.ts +18 -0
- package/dist/infrastructure/hosts.js +72 -0
- package/dist/register-browser.d.ts +1 -0
- package/dist/register-browser.js +7 -0
- package/dist/register.d.ts +1 -0
- package/dist/register.js +13 -0
- package/dist/start/browser.d.ts +12 -0
- package/dist/start/browser.js +36 -0
- package/dist/start/build-info.d.ts +2 -0
- package/dist/start/build-info.js +9 -0
- package/dist/start/control-points.d.ts +20 -0
- package/dist/start/control-points.js +59 -0
- package/dist/start/env.d.ts +11 -0
- package/dist/start/env.js +64 -0
- package/dist/start/fw.d.ts +29 -0
- package/dist/start/fw.js +76 -0
- package/dist/start/index.d.ts +23 -0
- package/dist/start/index.js +28 -0
- package/dist/start/instance.d.ts +17 -0
- package/dist/start/instance.js +56 -0
- package/dist/start/names.d.ts +22 -0
- package/dist/start/names.js +25 -0
- package/dist/start/resolve.d.ts +40 -0
- package/dist/start/resolve.js +139 -0
- package/dist/start/state.d.ts +96 -0
- package/dist/start/state.js +303 -0
- package/package.json +58 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { FireweaveErrorKind, OpenFeatureErrorCode } from './errors.js';
|
|
2
|
+
/** JSON-compatible value (spec/decision.schema.json `value`). */
|
|
3
|
+
export type JsonValue = string | number | boolean | null | JsonValue[] | {
|
|
4
|
+
[key: string]: JsonValue;
|
|
5
|
+
};
|
|
6
|
+
export type FlagValueType = 'boolean' | 'string' | 'number' | 'object';
|
|
7
|
+
export type DecisionReason = 'STATIC' | 'DEFAULT' | 'TARGETING_MATCH' | 'SPLIT' | 'CACHED' | 'STALE' | 'DISABLED' | 'UNKNOWN' | 'ERROR';
|
|
8
|
+
/** Canonical decision (spec/decision.schema.json). */
|
|
9
|
+
export interface Decision {
|
|
10
|
+
controlPointKey: string;
|
|
11
|
+
value: JsonValue;
|
|
12
|
+
reason: DecisionReason;
|
|
13
|
+
variant?: string;
|
|
14
|
+
errorCode?: OpenFeatureErrorCode;
|
|
15
|
+
errorKind?: FireweaveErrorKind;
|
|
16
|
+
errorMessage?: string;
|
|
17
|
+
/** fireweave.* flag metadata surfaced to OpenFeature controlPointMetadata. */
|
|
18
|
+
metadata: Record<string, string | number | boolean>;
|
|
19
|
+
}
|
|
20
|
+
/** Canonical evaluation context after validation/canonicalization. */
|
|
21
|
+
export interface CanonicalContext {
|
|
22
|
+
targetingKey?: string;
|
|
23
|
+
/** All non-reserved attributes (targetingKey excluded). */
|
|
24
|
+
attributes: Record<string, JsonValue>;
|
|
25
|
+
/**
|
|
26
|
+
* Group memberships extracted from `fireweave.groups` (canonical, ruling 13)
|
|
27
|
+
* or the plain `groups` alias, when shaped as Record<string,string>.
|
|
28
|
+
*/
|
|
29
|
+
groups?: Record<string, string>;
|
|
30
|
+
/**
|
|
31
|
+
* Group properties extracted from `fireweave.groupProperties` (canonical)
|
|
32
|
+
* or the plain `groupProperties` alias.
|
|
33
|
+
*/
|
|
34
|
+
groupProperties?: Record<string, JsonValue>;
|
|
35
|
+
}
|
|
36
|
+
export type LifecycleState = 'UNINITIALIZED' | 'INITIALIZING' | 'READY' | 'STALE' | 'ERROR' | 'FATAL' | 'SHUTDOWN';
|
|
37
|
+
export type SignalKind = 'health' | 'error' | 'metric' | 'outcome';
|
|
38
|
+
/** Canonical signal envelope (spec/signal.schema.json). */
|
|
39
|
+
export interface Signal {
|
|
40
|
+
kind: SignalKind;
|
|
41
|
+
name: string;
|
|
42
|
+
status?: string;
|
|
43
|
+
errorKind?: FireweaveErrorKind;
|
|
44
|
+
message?: string;
|
|
45
|
+
value?: number | boolean | string;
|
|
46
|
+
timestamp?: string;
|
|
47
|
+
targetingKey?: string;
|
|
48
|
+
rolloutId?: string;
|
|
49
|
+
changeId?: string;
|
|
50
|
+
stampId?: string;
|
|
51
|
+
controlPointKey?: string;
|
|
52
|
+
variant?: string;
|
|
53
|
+
traceId?: string;
|
|
54
|
+
role?: 'adoption' | 'guard' | 'quality' | 'custom';
|
|
55
|
+
direction?: 'up-good' | 'up-bad' | 'neutral';
|
|
56
|
+
attributes?: Record<string, string | number | boolean | null>;
|
|
57
|
+
}
|
|
58
|
+
/** Release / rollout context (spec/release-context.schema.json; ruling 15: rolloutId + stampIds required). */
|
|
59
|
+
export interface ReleaseContext {
|
|
60
|
+
stampIds: string[];
|
|
61
|
+
rolloutId: string;
|
|
62
|
+
changeId?: string;
|
|
63
|
+
surfaces?: Array<{
|
|
64
|
+
surfaceId: string;
|
|
65
|
+
kind?: 'ts-server' | 'node-server' | 'python' | 'go' | 'java' | 'web' | 'other';
|
|
66
|
+
}>;
|
|
67
|
+
metadata?: Record<string, string | number | boolean>;
|
|
68
|
+
}
|
|
69
|
+
export type ReleaseStatus = 'set' | 'in_progress' | 'completed' | 'failed';
|
|
70
|
+
export interface ReleaseState {
|
|
71
|
+
context: ReleaseContext;
|
|
72
|
+
status: ReleaseStatus;
|
|
73
|
+
reason?: string;
|
|
74
|
+
}
|
|
75
|
+
/** Exposure event (extensions fixtures shape). */
|
|
76
|
+
export interface Exposure {
|
|
77
|
+
targetingKey: string;
|
|
78
|
+
controlPointKey: string;
|
|
79
|
+
value: JsonValue;
|
|
80
|
+
variant?: string;
|
|
81
|
+
rolloutId?: string;
|
|
82
|
+
changeId?: string;
|
|
83
|
+
stampId?: string;
|
|
84
|
+
}
|
|
85
|
+
/** Capability matrix (spec/capabilities.schema.json). */
|
|
86
|
+
export interface Capabilities {
|
|
87
|
+
static: {
|
|
88
|
+
language: 'node';
|
|
89
|
+
sdkVersion?: string;
|
|
90
|
+
specVersion?: '0.1.0';
|
|
91
|
+
openFeature: {
|
|
92
|
+
specFloor: '0.8.0';
|
|
93
|
+
providerName: 'fireweave';
|
|
94
|
+
serverOnly?: boolean;
|
|
95
|
+
};
|
|
96
|
+
features: Record<string, boolean>;
|
|
97
|
+
};
|
|
98
|
+
runtime: {
|
|
99
|
+
backend: 'fireweave' | 'inmemory' | 'none' | 'other';
|
|
100
|
+
lifecycle: LifecycleState;
|
|
101
|
+
features?: Record<string, boolean>;
|
|
102
|
+
limits?: {
|
|
103
|
+
intSafeMaxAbs?: number;
|
|
104
|
+
shutdownTimeoutMsDefault?: number;
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fireweave SDK validation — pure, total functions per spec/control-points.md
|
|
3
|
+
* "Validation, before any I/O" and spec/modes.md "Initialisation validation".
|
|
4
|
+
*
|
|
5
|
+
* Every read-path validator here (`validateControlPointKey`,
|
|
6
|
+
* `validateDefaultValue`, `validateContext`, `validateTargetingKey`) returns
|
|
7
|
+
* `Validated<T>` instead of throwing. `FireweaveRuntime.evaluate` runs them,
|
|
8
|
+
* in the fixed order the spec names — key, default-vs-type, context,
|
|
9
|
+
* lifecycle — and degrades to the caller's default on the first failure; it
|
|
10
|
+
* NEVER throws (spec/control-points.md "Return discipline — never throw into
|
|
11
|
+
* a read path").
|
|
12
|
+
*
|
|
13
|
+
* `validateInitOptions` is the one named exception (spec/modes.md
|
|
14
|
+
* "Initialisation validation"): its failures are converted to a THROW by
|
|
15
|
+
* `initFireweave` (src/init.ts). The validator itself still returns
|
|
16
|
+
* `Validated<T>` — the entry point does the throwing, not this module.
|
|
17
|
+
*
|
|
18
|
+
* Collected into one module so it can move wholesale into `domain/` (plan
|
|
19
|
+
* Phase 1.4) without touching its exports or call sites again. Everything
|
|
20
|
+
* below is pure (no I/O, no ambient state) and total (every branch returns a
|
|
21
|
+
* `Validated<T>`) — `conformance/` can exercise all four read-path rules
|
|
22
|
+
* offline, with no backend.
|
|
23
|
+
*
|
|
24
|
+
* `validateInitOptions` is deliberately generic rather than importing
|
|
25
|
+
* `InitFireweaveOptions` from `init.ts`: init.ts is the "mode" entry point
|
|
26
|
+
* (Phase 1.4 assigns it to `application/`), and this module is destined for
|
|
27
|
+
* `domain/`, which must not depend on outer layers. It also does not
|
|
28
|
+
* validate "apiUrl fails the host allowlist" (spec/modes.md row 3) — that
|
|
29
|
+
* check (`assertHostAllowed`) lives in `hosts.ts`, which Phase 1.4 assigns to
|
|
30
|
+
* `infrastructure/`; it stays reachable today via `FireweaveRuntime.initialize`
|
|
31
|
+
* (runtime.ts) / `FireweaveRemoteAdapter.initialize` (adapters/remote.ts),
|
|
32
|
+
* unchanged by this module.
|
|
33
|
+
*/
|
|
34
|
+
import { FireweaveError } from './errors.js';
|
|
35
|
+
import { type ContextPolicy } from './context.js';
|
|
36
|
+
import type { CanonicalContext, JsonValue } from './types.js';
|
|
37
|
+
/** Result of a pure validator: success carries the validated value, failure the canonical error. */
|
|
38
|
+
export type Validated<T> = {
|
|
39
|
+
ok: true;
|
|
40
|
+
value: T;
|
|
41
|
+
} | {
|
|
42
|
+
ok: false;
|
|
43
|
+
error: FireweaveError;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* key — non-empty, ≤256 characters, no control characters
|
|
47
|
+
* (spec/control-points.md rule 1, the first check in the fixed order).
|
|
48
|
+
*
|
|
49
|
+
* No taxonomy kind names "malformed key" explicitly (the return-discipline
|
|
50
|
+
* table's closest row is "key unknown to the backend" → ControlPointNotFound): a key
|
|
51
|
+
* that can never identify a flag is treated the same as one the backend
|
|
52
|
+
* doesn't recognise, so this maps to ControlPointNotFound too.
|
|
53
|
+
*
|
|
54
|
+
* Controller-ruled interim mapping (Task 3 review): the 15-kind taxonomy in
|
|
55
|
+
* errors.schema.json is frozen at exactly 15 entries, `InvalidContext` is
|
|
56
|
+
* textually scoped to the evaluation *context* (not the key), and the
|
|
57
|
+
* schema already maps another non-literal case — quota-limited responses —
|
|
58
|
+
* onto `ControlPointNotFound` rather than adding a kind for it. `ControlPointNotFound` is
|
|
59
|
+
* therefore the least-wrong existing kind, not a literal fit. Other-language
|
|
60
|
+
* implementations copying node as the reference SHOULD use the same mapping
|
|
61
|
+
* rather than each independently guessing a different one.
|
|
62
|
+
*/
|
|
63
|
+
export declare function validateControlPointKey(key: string): Validated<string>;
|
|
64
|
+
export type ExpectedFlagType = 'boolean' | 'string' | 'number' | 'object';
|
|
65
|
+
/**
|
|
66
|
+
* Whether `value` matches the shape `expected` names. Shared by
|
|
67
|
+
* {@link validateDefaultValue} (the caller's default, before any I/O) and
|
|
68
|
+
* `FireweaveRuntime.evaluate`'s post-resolve check (the backend's resolved
|
|
69
|
+
* value, after I/O) — same predicate, two different inputs.
|
|
70
|
+
*/
|
|
71
|
+
export declare function matchesExpectedType(value: JsonValue, expected: ExpectedFlagType): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* default vs type — e.g. `getBooleanValue` with a non-boolean default is
|
|
74
|
+
* TypeMismatch (spec/control-points.md rule 2, checked before any I/O).
|
|
75
|
+
*/
|
|
76
|
+
export declare function validateDefaultValue(expectedType: ExpectedFlagType, defaultValue: JsonValue): Validated<JsonValue>;
|
|
77
|
+
/**
|
|
78
|
+
* targetingKey: "An SDK MUST NOT invent one: a missing targeting key is
|
|
79
|
+
* InvalidContext where the evaluation needs it, never a generated anonymous
|
|
80
|
+
* id" (spec/control-points.md "Context"). `required` is call-site policy —
|
|
81
|
+
* the remote adapter always requires one for `resolve`/`registerTarget`
|
|
82
|
+
* (spec/remote-protocol.md "Two identity paths"); the generic context
|
|
83
|
+
* pipeline ({@link validateContext}) only does when
|
|
84
|
+
* `ContextPolicy.requireTargetingKey` opts in.
|
|
85
|
+
*/
|
|
86
|
+
export declare function validateTargetingKey(targetingKey: string | undefined, required: boolean): Validated<string | undefined>;
|
|
87
|
+
/**
|
|
88
|
+
* context — depth, key count, value size, reserved keys
|
|
89
|
+
* (evaluation-context.schema.json) (spec/control-points.md rule 3). Also
|
|
90
|
+
* enforces `policy.requireTargetingKey` via {@link validateTargetingKey}, and
|
|
91
|
+
* — on success — extracts `groups`/`groupProperties` the same way the former
|
|
92
|
+
* `canonicalizeContext` did.
|
|
93
|
+
*
|
|
94
|
+
* `merged` is the already-merged (global → client → invocation) raw input;
|
|
95
|
+
* merging itself (`mergeContexts`) lives in context.ts and carries no
|
|
96
|
+
* validation concerns of its own.
|
|
97
|
+
*/
|
|
98
|
+
export declare function validateContext(merged: {
|
|
99
|
+
targetingKey?: string;
|
|
100
|
+
attributes: Record<string, unknown>;
|
|
101
|
+
}, policy: ContextPolicy): Validated<CanonicalContext>;
|
|
102
|
+
/**
|
|
103
|
+
* Throwing wrapper around {@link validateContext} kept for the pre-existing
|
|
104
|
+
* public surface (`FireweaveRuntime.resolveContext`, direct callers of the
|
|
105
|
+
* package export). Read paths do not use this — they call
|
|
106
|
+
* {@link validateContext} directly so a failure degrades instead of throwing.
|
|
107
|
+
*/
|
|
108
|
+
export declare function canonicalizeContext(merged: {
|
|
109
|
+
targetingKey?: string;
|
|
110
|
+
attributes: Record<string, unknown>;
|
|
111
|
+
}, policy: ContextPolicy): CanonicalContext;
|
|
112
|
+
/**
|
|
113
|
+
* Initialisation-validation table (spec/modes.md), rows 1, 2 and 4:
|
|
114
|
+
* - `mode` absent or unrecognised
|
|
115
|
+
* - `mode: 'remote'` with `apiKey` or `apiUrl` missing/blank
|
|
116
|
+
* - `mode: 'local'` with credentials supplied (a config half-migrated from
|
|
117
|
+
* remote to local reads as neither, silently — reject it instead)
|
|
118
|
+
*
|
|
119
|
+
* Row 3 ("apiUrl fails the host allowlist") is intentionally NOT checked
|
|
120
|
+
* here — see the module doc comment.
|
|
121
|
+
*/
|
|
122
|
+
export declare function validateInitOptions<T extends {
|
|
123
|
+
readonly mode?: unknown;
|
|
124
|
+
}>(options: T): Validated<T>;
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateControlPointKey = validateControlPointKey;
|
|
4
|
+
exports.matchesExpectedType = matchesExpectedType;
|
|
5
|
+
exports.validateDefaultValue = validateDefaultValue;
|
|
6
|
+
exports.validateTargetingKey = validateTargetingKey;
|
|
7
|
+
exports.validateContext = validateContext;
|
|
8
|
+
exports.canonicalizeContext = canonicalizeContext;
|
|
9
|
+
exports.validateInitOptions = validateInitOptions;
|
|
10
|
+
/**
|
|
11
|
+
* Fireweave SDK validation — pure, total functions per spec/control-points.md
|
|
12
|
+
* "Validation, before any I/O" and spec/modes.md "Initialisation validation".
|
|
13
|
+
*
|
|
14
|
+
* Every read-path validator here (`validateControlPointKey`,
|
|
15
|
+
* `validateDefaultValue`, `validateContext`, `validateTargetingKey`) returns
|
|
16
|
+
* `Validated<T>` instead of throwing. `FireweaveRuntime.evaluate` runs them,
|
|
17
|
+
* in the fixed order the spec names — key, default-vs-type, context,
|
|
18
|
+
* lifecycle — and degrades to the caller's default on the first failure; it
|
|
19
|
+
* NEVER throws (spec/control-points.md "Return discipline — never throw into
|
|
20
|
+
* a read path").
|
|
21
|
+
*
|
|
22
|
+
* `validateInitOptions` is the one named exception (spec/modes.md
|
|
23
|
+
* "Initialisation validation"): its failures are converted to a THROW by
|
|
24
|
+
* `initFireweave` (src/init.ts). The validator itself still returns
|
|
25
|
+
* `Validated<T>` — the entry point does the throwing, not this module.
|
|
26
|
+
*
|
|
27
|
+
* Collected into one module so it can move wholesale into `domain/` (plan
|
|
28
|
+
* Phase 1.4) without touching its exports or call sites again. Everything
|
|
29
|
+
* below is pure (no I/O, no ambient state) and total (every branch returns a
|
|
30
|
+
* `Validated<T>`) — `conformance/` can exercise all four read-path rules
|
|
31
|
+
* offline, with no backend.
|
|
32
|
+
*
|
|
33
|
+
* `validateInitOptions` is deliberately generic rather than importing
|
|
34
|
+
* `InitFireweaveOptions` from `init.ts`: init.ts is the "mode" entry point
|
|
35
|
+
* (Phase 1.4 assigns it to `application/`), and this module is destined for
|
|
36
|
+
* `domain/`, which must not depend on outer layers. It also does not
|
|
37
|
+
* validate "apiUrl fails the host allowlist" (spec/modes.md row 3) — that
|
|
38
|
+
* check (`assertHostAllowed`) lives in `hosts.ts`, which Phase 1.4 assigns to
|
|
39
|
+
* `infrastructure/`; it stays reachable today via `FireweaveRuntime.initialize`
|
|
40
|
+
* (runtime.ts) / `FireweaveRemoteAdapter.initialize` (adapters/remote.ts),
|
|
41
|
+
* unchanged by this module.
|
|
42
|
+
*/
|
|
43
|
+
const errors_js_1 = require("./errors.js");
|
|
44
|
+
const context_js_1 = require("./context.js");
|
|
45
|
+
const ok = (value) => ({ ok: true, value });
|
|
46
|
+
const fail = (error) => ({ ok: false, error });
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
// Rule 1 — validateControlPointKey (spec/control-points.md "Validation,
|
|
49
|
+
// before any I/O": "key — non-empty, ≤256 characters, no control characters")
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
/** C0 + C1 control characters (U+0000-U+001F, U+007F-U+009F). */
|
|
52
|
+
const CONTROL_CHARACTERS = /[\u0000-\u001f\u007f-\u009f]/;
|
|
53
|
+
const MAX_CONTROL_POINT_KEY_LENGTH = 256;
|
|
54
|
+
/**
|
|
55
|
+
* key — non-empty, ≤256 characters, no control characters
|
|
56
|
+
* (spec/control-points.md rule 1, the first check in the fixed order).
|
|
57
|
+
*
|
|
58
|
+
* No taxonomy kind names "malformed key" explicitly (the return-discipline
|
|
59
|
+
* table's closest row is "key unknown to the backend" → ControlPointNotFound): a key
|
|
60
|
+
* that can never identify a flag is treated the same as one the backend
|
|
61
|
+
* doesn't recognise, so this maps to ControlPointNotFound too.
|
|
62
|
+
*
|
|
63
|
+
* Controller-ruled interim mapping (Task 3 review): the 15-kind taxonomy in
|
|
64
|
+
* errors.schema.json is frozen at exactly 15 entries, `InvalidContext` is
|
|
65
|
+
* textually scoped to the evaluation *context* (not the key), and the
|
|
66
|
+
* schema already maps another non-literal case — quota-limited responses —
|
|
67
|
+
* onto `ControlPointNotFound` rather than adding a kind for it. `ControlPointNotFound` is
|
|
68
|
+
* therefore the least-wrong existing kind, not a literal fit. Other-language
|
|
69
|
+
* implementations copying node as the reference SHOULD use the same mapping
|
|
70
|
+
* rather than each independently guessing a different one.
|
|
71
|
+
*/
|
|
72
|
+
function validateControlPointKey(key) {
|
|
73
|
+
if (typeof key !== 'string' || key.length === 0) {
|
|
74
|
+
return fail(new errors_js_1.FireweaveError('ControlPointNotFound', { message: 'control point key must be a non-empty string' }));
|
|
75
|
+
}
|
|
76
|
+
if (key.length > MAX_CONTROL_POINT_KEY_LENGTH) {
|
|
77
|
+
return fail(new errors_js_1.FireweaveError('ControlPointNotFound', { message: 'control point key exceeds maximum length' }));
|
|
78
|
+
}
|
|
79
|
+
if (CONTROL_CHARACTERS.test(key)) {
|
|
80
|
+
return fail(new errors_js_1.FireweaveError('ControlPointNotFound', { message: 'control point key contains control characters' }));
|
|
81
|
+
}
|
|
82
|
+
return ok(key);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Whether `value` matches the shape `expected` names. Shared by
|
|
86
|
+
* {@link validateDefaultValue} (the caller's default, before any I/O) and
|
|
87
|
+
* `FireweaveRuntime.evaluate`'s post-resolve check (the backend's resolved
|
|
88
|
+
* value, after I/O) — same predicate, two different inputs.
|
|
89
|
+
*/
|
|
90
|
+
function matchesExpectedType(value, expected) {
|
|
91
|
+
switch (expected) {
|
|
92
|
+
case 'boolean':
|
|
93
|
+
return typeof value === 'boolean';
|
|
94
|
+
case 'string':
|
|
95
|
+
return typeof value === 'string';
|
|
96
|
+
case 'number':
|
|
97
|
+
return typeof value === 'number';
|
|
98
|
+
case 'object':
|
|
99
|
+
return value !== null && typeof value === 'object';
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* default vs type — e.g. `getBooleanValue` with a non-boolean default is
|
|
104
|
+
* TypeMismatch (spec/control-points.md rule 2, checked before any I/O).
|
|
105
|
+
*/
|
|
106
|
+
function validateDefaultValue(expectedType, defaultValue) {
|
|
107
|
+
if (!matchesExpectedType(defaultValue, expectedType)) {
|
|
108
|
+
return fail(new errors_js_1.FireweaveError('TypeMismatch'));
|
|
109
|
+
}
|
|
110
|
+
return ok(defaultValue);
|
|
111
|
+
}
|
|
112
|
+
// ---------------------------------------------------------------------------
|
|
113
|
+
// validateTargetingKey (spec/control-points.md "Context": targetingKey)
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
/**
|
|
116
|
+
* targetingKey: "An SDK MUST NOT invent one: a missing targeting key is
|
|
117
|
+
* InvalidContext where the evaluation needs it, never a generated anonymous
|
|
118
|
+
* id" (spec/control-points.md "Context"). `required` is call-site policy —
|
|
119
|
+
* the remote adapter always requires one for `resolve`/`registerTarget`
|
|
120
|
+
* (spec/remote-protocol.md "Two identity paths"); the generic context
|
|
121
|
+
* pipeline ({@link validateContext}) only does when
|
|
122
|
+
* `ContextPolicy.requireTargetingKey` opts in.
|
|
123
|
+
*/
|
|
124
|
+
function validateTargetingKey(targetingKey, required) {
|
|
125
|
+
if (required && (targetingKey === undefined || targetingKey === '')) {
|
|
126
|
+
return fail(new errors_js_1.FireweaveError('InvalidContext', {
|
|
127
|
+
message: 'targeting key missing',
|
|
128
|
+
openFeatureErrorCode: 'TARGETING_KEY_MISSING',
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
return ok(targetingKey);
|
|
132
|
+
}
|
|
133
|
+
// ---------------------------------------------------------------------------
|
|
134
|
+
// Rule 3 — validateContext (spec/control-points.md rule 3: "context — depth,
|
|
135
|
+
// key count, value size, reserved keys (evaluation-context.schema.json)")
|
|
136
|
+
// ---------------------------------------------------------------------------
|
|
137
|
+
/**
|
|
138
|
+
* UTF-8 byte length. `TextEncoder` rather than `Buffer.byteLength`: `Buffer`
|
|
139
|
+
* is a Node global that Deno only exposes under npm compatibility, and this
|
|
140
|
+
* bound is enforced on every evaluation (ADR-0008 runtime portability).
|
|
141
|
+
*/
|
|
142
|
+
const utf8Encoder = new TextEncoder();
|
|
143
|
+
const byteLength = (s) => utf8Encoder.encode(s).length;
|
|
144
|
+
/**
|
|
145
|
+
* Sentinel returned (never thrown) when `deepCopyJson` walks back into an
|
|
146
|
+
* object already on the current recursion path — an actual reference cycle,
|
|
147
|
+
* not merely the same object shared by two sibling branches (which is legal
|
|
148
|
+
* and handled by backtracking `seen.delete(...)` below). Untyped caller
|
|
149
|
+
* input is `unknown`, so a circular attribute value is reachable from a
|
|
150
|
+
* caller today; without this, recursion never terminates and crashes the
|
|
151
|
+
* whole process with `RangeError: Maximum call stack size exceeded` —
|
|
152
|
+
* exactly the kind of throw a "before any I/O" validator must not allow
|
|
153
|
+
* (spec/control-points.md "Return discipline — never throw into a read
|
|
154
|
+
* path"). Module-local: never returned across this file's public surface,
|
|
155
|
+
* only used to fail `validateContext` closed as `InvalidContext`.
|
|
156
|
+
*/
|
|
157
|
+
const CYCLIC = Symbol('fireweave.cyclicJson');
|
|
158
|
+
function deepCopyJson(value, seen = new WeakSet()) {
|
|
159
|
+
// Structured clone of JSON-compatible data; drops functions/undefined like JSON round-trip.
|
|
160
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean' || typeof value === 'number') {
|
|
161
|
+
return value;
|
|
162
|
+
}
|
|
163
|
+
if (Array.isArray(value)) {
|
|
164
|
+
if (seen.has(value))
|
|
165
|
+
return CYCLIC;
|
|
166
|
+
seen.add(value);
|
|
167
|
+
const out = [];
|
|
168
|
+
for (const v of value) {
|
|
169
|
+
const copied = deepCopyJson(v, seen);
|
|
170
|
+
if (copied === CYCLIC)
|
|
171
|
+
return CYCLIC;
|
|
172
|
+
out.push(copied);
|
|
173
|
+
}
|
|
174
|
+
seen.delete(value);
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
if (typeof value === 'object') {
|
|
178
|
+
if (seen.has(value))
|
|
179
|
+
return CYCLIC;
|
|
180
|
+
seen.add(value);
|
|
181
|
+
const out = {};
|
|
182
|
+
for (const [k, v] of Object.entries(value)) {
|
|
183
|
+
if (v !== undefined) {
|
|
184
|
+
const copied = deepCopyJson(v, seen);
|
|
185
|
+
if (copied === CYCLIC)
|
|
186
|
+
return CYCLIC;
|
|
187
|
+
out[k] = copied;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
seen.delete(value);
|
|
191
|
+
return out;
|
|
192
|
+
}
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Cycle-safe defensively: `nestingDepth` only ever runs on `deepCopyJson`'s
|
|
197
|
+
* own output today, which is guaranteed acyclic by construction (it builds
|
|
198
|
+
* fresh containers, never back-references). Guarded anyway per the same
|
|
199
|
+
* "must be genuinely total" reasoning as `deepCopyJson`, so a future caller
|
|
200
|
+
* (e.g. a Task 4 reuse) can't reintroduce the same crash by feeding this
|
|
201
|
+
* function un-copied input. A detected cycle returns `Infinity`, which
|
|
202
|
+
* always exceeds `limits.maxNestingDepth` and fails closed through the
|
|
203
|
+
* existing bound check in {@link validateContext} — no new failure path.
|
|
204
|
+
*/
|
|
205
|
+
function nestingDepth(value, seen = new WeakSet()) {
|
|
206
|
+
if (value === null || typeof value !== 'object')
|
|
207
|
+
return 0;
|
|
208
|
+
if (seen.has(value))
|
|
209
|
+
return Infinity;
|
|
210
|
+
seen.add(value);
|
|
211
|
+
let max = 0;
|
|
212
|
+
const children = Array.isArray(value) ? value : Object.values(value);
|
|
213
|
+
for (const child of children) {
|
|
214
|
+
const d = nestingDepth(child, seen);
|
|
215
|
+
if (d > max)
|
|
216
|
+
max = d;
|
|
217
|
+
}
|
|
218
|
+
seen.delete(value);
|
|
219
|
+
return max + 1;
|
|
220
|
+
}
|
|
221
|
+
const invalidContext = (message) => new errors_js_1.FireweaveError('InvalidContext', { message });
|
|
222
|
+
/**
|
|
223
|
+
* context — depth, key count, value size, reserved keys
|
|
224
|
+
* (evaluation-context.schema.json) (spec/control-points.md rule 3). Also
|
|
225
|
+
* enforces `policy.requireTargetingKey` via {@link validateTargetingKey}, and
|
|
226
|
+
* — on success — extracts `groups`/`groupProperties` the same way the former
|
|
227
|
+
* `canonicalizeContext` did.
|
|
228
|
+
*
|
|
229
|
+
* `merged` is the already-merged (global → client → invocation) raw input;
|
|
230
|
+
* merging itself (`mergeContexts`) lives in context.ts and carries no
|
|
231
|
+
* validation concerns of its own.
|
|
232
|
+
*/
|
|
233
|
+
function validateContext(merged, policy) {
|
|
234
|
+
const { limits } = policy;
|
|
235
|
+
const copied = deepCopyJson(merged.attributes);
|
|
236
|
+
if (copied === CYCLIC) {
|
|
237
|
+
return fail(invalidContext('context contains a circular reference'));
|
|
238
|
+
}
|
|
239
|
+
const attributes = copied;
|
|
240
|
+
const keys = Object.keys(attributes);
|
|
241
|
+
if (keys.length > limits.maxAttributeCount) {
|
|
242
|
+
return fail(invalidContext('context exceeds maximum attribute count'));
|
|
243
|
+
}
|
|
244
|
+
for (const key of keys) {
|
|
245
|
+
if (byteLength(key) > limits.maxKeyBytes) {
|
|
246
|
+
return fail(invalidContext('context key exceeds maximum size'));
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
for (const key of keys) {
|
|
250
|
+
const value = attributes[key];
|
|
251
|
+
if (byteLength(JSON.stringify(value ?? null)) > limits.maxValueBytes) {
|
|
252
|
+
return fail(invalidContext('context value exceeds maximum size'));
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
for (const key of keys) {
|
|
256
|
+
const value = attributes[key];
|
|
257
|
+
if (value !== undefined && nestingDepth(value) > limits.maxNestingDepth) {
|
|
258
|
+
return fail(invalidContext('context exceeds maximum nesting depth'));
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const serialized = JSON.stringify({ targetingKey: merged.targetingKey, attributes });
|
|
262
|
+
if (byteLength(serialized) > limits.maxSerializedContextBytes) {
|
|
263
|
+
return fail(invalidContext('serialized context exceeds maximum size'));
|
|
264
|
+
}
|
|
265
|
+
for (const key of keys) {
|
|
266
|
+
if (policy.reservedAttributeKeys.includes(key)) {
|
|
267
|
+
return fail(invalidContext('invalid evaluation context'));
|
|
268
|
+
}
|
|
269
|
+
// Ruling 13: exactly fireweave.groups + fireweave.groupProperties are
|
|
270
|
+
// permitted in the reserved namespace; every other fireweave.* key is rejected.
|
|
271
|
+
if (key.startsWith('fireweave.') && !context_js_1.ALLOWED_FIREWEAVE_CONTEXT_KEYS.includes(key)) {
|
|
272
|
+
return fail(invalidContext('invalid evaluation context'));
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
const targetingKeyResult = validateTargetingKey(merged.targetingKey, policy.requireTargetingKey);
|
|
276
|
+
if (!targetingKeyResult.ok)
|
|
277
|
+
return targetingKeyResult;
|
|
278
|
+
const canonical = { attributes };
|
|
279
|
+
if (merged.targetingKey !== undefined)
|
|
280
|
+
canonical.targetingKey = merged.targetingKey;
|
|
281
|
+
// Group memberships: canonical carrier `fireweave.groups` (rulings 13–14),
|
|
282
|
+
// with plain `groups` retained as a documented alias.
|
|
283
|
+
const groups = attributes['fireweave.groups'] ?? attributes['groups'];
|
|
284
|
+
if (groups !== null && groups !== undefined && typeof groups === 'object' && !Array.isArray(groups)) {
|
|
285
|
+
const g = {};
|
|
286
|
+
let allStrings = true;
|
|
287
|
+
for (const [k, v] of Object.entries(groups)) {
|
|
288
|
+
if (typeof v === 'string')
|
|
289
|
+
g[k] = v;
|
|
290
|
+
else
|
|
291
|
+
allStrings = false;
|
|
292
|
+
}
|
|
293
|
+
if (allStrings)
|
|
294
|
+
canonical.groups = g;
|
|
295
|
+
}
|
|
296
|
+
// Group properties: canonical carrier `fireweave.groupProperties`,
|
|
297
|
+
// plain `groupProperties` retained as alias.
|
|
298
|
+
const groupProps = attributes['fireweave.groupProperties'] ?? attributes['groupProperties'];
|
|
299
|
+
if (groupProps !== null &&
|
|
300
|
+
groupProps !== undefined &&
|
|
301
|
+
typeof groupProps === 'object' &&
|
|
302
|
+
!Array.isArray(groupProps)) {
|
|
303
|
+
canonical.groupProperties = groupProps;
|
|
304
|
+
}
|
|
305
|
+
return ok(canonical);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Throwing wrapper around {@link validateContext} kept for the pre-existing
|
|
309
|
+
* public surface (`FireweaveRuntime.resolveContext`, direct callers of the
|
|
310
|
+
* package export). Read paths do not use this — they call
|
|
311
|
+
* {@link validateContext} directly so a failure degrades instead of throwing.
|
|
312
|
+
*/
|
|
313
|
+
function canonicalizeContext(merged, policy) {
|
|
314
|
+
const result = validateContext(merged, policy);
|
|
315
|
+
if (!result.ok)
|
|
316
|
+
throw result.error;
|
|
317
|
+
return result.value;
|
|
318
|
+
}
|
|
319
|
+
// ---------------------------------------------------------------------------
|
|
320
|
+
// validateInitOptions (spec/modes.md "Initialisation validation")
|
|
321
|
+
// ---------------------------------------------------------------------------
|
|
322
|
+
/** "missing" and "blank" collapse to one check: not a non-empty string. */
|
|
323
|
+
const isBlank = (value) => typeof value !== 'string' || value.trim().length === 0;
|
|
324
|
+
const configError = (message) => new errors_js_1.FireweaveError('Configuration', { message });
|
|
325
|
+
/**
|
|
326
|
+
* Initialisation-validation table (spec/modes.md), rows 1, 2 and 4:
|
|
327
|
+
* - `mode` absent or unrecognised
|
|
328
|
+
* - `mode: 'remote'` with `apiKey` or `apiUrl` missing/blank
|
|
329
|
+
* - `mode: 'local'` with credentials supplied (a config half-migrated from
|
|
330
|
+
* remote to local reads as neither, silently — reject it instead)
|
|
331
|
+
*
|
|
332
|
+
* Row 3 ("apiUrl fails the host allowlist") is intentionally NOT checked
|
|
333
|
+
* here — see the module doc comment.
|
|
334
|
+
*/
|
|
335
|
+
function validateInitOptions(options) {
|
|
336
|
+
const mode = options?.mode;
|
|
337
|
+
if (mode !== 'local' && mode !== 'remote') {
|
|
338
|
+
return fail(configError('mode is required and must be "local" or "remote"'));
|
|
339
|
+
}
|
|
340
|
+
// Runtime-only guard: a config object half-migrated from remote to local
|
|
341
|
+
// can carry apiKey/apiUrl even though the local-mode type declares
|
|
342
|
+
// neither — TypeScript's excess-property check only fires on a fresh
|
|
343
|
+
// object literal, not on a variable assembled elsewhere and passed in.
|
|
344
|
+
const credentials = options;
|
|
345
|
+
if (mode === 'remote') {
|
|
346
|
+
if (isBlank(credentials.apiKey) || isBlank(credentials.apiUrl)) {
|
|
347
|
+
return fail(configError('mode "remote" requires apiKey and apiUrl'));
|
|
348
|
+
}
|
|
349
|
+
return ok(options);
|
|
350
|
+
}
|
|
351
|
+
if (!isBlank(credentials.apiKey) || !isBlank(credentials.apiUrl)) {
|
|
352
|
+
return fail(configError('mode "local" must not be combined with apiKey/apiUrl — the caller means one or the other'));
|
|
353
|
+
}
|
|
354
|
+
return ok(options);
|
|
355
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fireweaveai/server-sdk — Fireweave release-engineering SDK for server runtimes
|
|
3
|
+
* (Node, Bun, Deno).
|
|
4
|
+
*
|
|
5
|
+
* Exactly two v1 capabilities (spec/control-points.md): control points and
|
|
6
|
+
* target registration.
|
|
7
|
+
*
|
|
8
|
+
* Production default: {@link FireweaveRemoteAdapter} (Fireweave project key →
|
|
9
|
+
* fw-server). {@link InMemoryAdapter} for offline work and tests. The backend
|
|
10
|
+
* fw-server forwards to is fw-server's concern — no vendor SDK, key, or host
|
|
11
|
+
* appears in the application process (ADR-0005, ADR-0006).
|
|
12
|
+
*/
|
|
13
|
+
export { FireweaveError, ERROR_TAXONOMY, redactSecrets, isFireweaveError } from './domain/errors.js';
|
|
14
|
+
export type { FireweaveErrorKind, OpenFeatureErrorCode, ErrorKindSpec } from './domain/errors.js';
|
|
15
|
+
export type { JsonValue, Decision, DecisionReason, CanonicalContext, LifecycleState, Signal, SignalKind, ReleaseContext, ReleaseState, ReleaseStatus, Exposure, Capabilities, FlagValueType, } from './domain/types.js';
|
|
16
|
+
export { DEFAULT_CONTEXT_LIMITS, DEFAULT_RESERVED_ATTRIBUTE_KEYS, ALLOWED_FIREWEAVE_CONTEXT_KEYS, mergeContexts, normalizeContextInput, resolvedContextView, } from './domain/context.js';
|
|
17
|
+
export type { ContextInput, ContextLimits, ContextPolicy } from './domain/context.js';
|
|
18
|
+
export { canonicalizeContext, validateControlPointKey, validateDefaultValue, validateContext, validateTargetingKey, validateInitOptions, } from './domain/validation.js';
|
|
19
|
+
export type { Validated } from './domain/validation.js';
|
|
20
|
+
export type { BackendAdapter, AdapterResolution, AdapterRuntimeFeatures, RegisterTargetOptions, RegisterTargetResult, ResolveOptions, } from './application/ports.js';
|
|
21
|
+
export type { TargetKind } from './domain/target.js';
|
|
22
|
+
export { InMemoryAdapter } from './infrastructure/adapters/inmemory.js';
|
|
23
|
+
export type { InMemoryAdapterOptions, InMemoryFlagDefinition, InMemoryFault } from './infrastructure/adapters/inmemory.js';
|
|
24
|
+
export { FireweaveRemoteAdapter } from './infrastructure/adapters/remote.js';
|
|
25
|
+
export type { FireweaveRemoteAdapterOptions } from './infrastructure/adapters/remote.js';
|
|
26
|
+
export { FireweaveLocalAdapter } from './infrastructure/adapters/local.js';
|
|
27
|
+
export type { FireweaveLocalAdapterOptions } from './infrastructure/adapters/local.js';
|
|
28
|
+
export { DEFAULT_ALLOWED_HOSTS, assertHostAllowed, isLoopbackHostname } from './infrastructure/hosts.js';
|
|
29
|
+
export { FireweaveRuntime, stableStringify, DEFAULT_SHUTDOWN_TIMEOUT_MS } from './application/runtime.js';
|
|
30
|
+
export type { FireweaveRuntimeConfig, EvaluateOptions, ExpectedFlagType } from './application/runtime.js';
|
|
31
|
+
export { FireweaveClient } from './application/client.js';
|
|
32
|
+
export type { ControlPointsApi, FireweaveClientOptions } from './application/client.js';
|
|
33
|
+
export { initFireweave } from './application/mode.js';
|
|
34
|
+
export type { InitFireweaveOptions, InitFireweaveLocalOptions, InitFireweaveRemoteOptions, } from './application/mode.js';
|