@benchdog-ts/core 0.0.0-stage → 0.3.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 +38 -2
- package/dist/approvals.d.ts +39 -0
- package/dist/audit.d.ts +49 -0
- package/dist/bucket.d.ts +4 -0
- package/dist/client.d.ts +69 -0
- package/dist/define.d.ts +41 -0
- package/dist/evaluate.d.ts +25 -0
- package/dist/expression.d.ts +23 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +798 -0
- package/dist/index.js.map +1 -0
- package/dist/payload.d.ts +18 -0
- package/dist/schedule.d.ts +27 -0
- package/dist/sinks.d.ts +52 -0
- package/dist/store.d.ts +85 -0
- package/dist/types.d.ts +109 -0
- package/dist/usage.d.ts +43 -0
- package/package.json +46 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Joe Gaudet
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @benchdog-ts/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The TypeScript core of [Benchdog](https://joegaudet.com/benchdog/), feature flags for Ember,
|
|
4
|
+
Rails, Django and Node. It runs in Node, browsers and edge runtimes. It passes every shared
|
|
5
|
+
vector in `spec/vectors`.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { MemoryStore, createBenchdog, defineFlags } from '@benchdog-ts/core'
|
|
9
|
+
|
|
10
|
+
const flags = defineFlags({
|
|
11
|
+
'new-checkout': { type: 'boolean' },
|
|
12
|
+
theme: { type: 'string', variations: ['classic', 'ocean'] },
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const benchdog = createBenchdog({
|
|
16
|
+
store: new MemoryStore(),
|
|
17
|
+
environment: 'production',
|
|
18
|
+
flags,
|
|
19
|
+
groups: { admins: actor => actor.attributes?.admin === true },
|
|
20
|
+
})
|
|
21
|
+
await benchdog.ready()
|
|
22
|
+
|
|
23
|
+
benchdog.enabled('new-checkout', { id: 'user:42' }) // boolean
|
|
24
|
+
benchdog.value('theme', { id: 'user:42' }, 'classic') // 'classic' | 'ocean'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
A wrong key, or a default of the wrong type, is a compile error.
|
|
28
|
+
|
|
29
|
+
## What is in it
|
|
30
|
+
|
|
31
|
+
- `evaluate(flagSet, key, context, defaultValue, options)`: one evaluation (SPEC section 5).
|
|
32
|
+
- `createBenchdog(options)`: a client that keeps a snapshot of one environment. A `ChangeFeed`
|
|
33
|
+
or `pollIntervalMs` loads it again after a change.
|
|
34
|
+
- `FlagStore` and `MemoryStore`: a store applies a change and appends its audit entry in one
|
|
35
|
+
step.
|
|
36
|
+
- `clientPayload(flagSet, context)`: the values of the flags with the `client` tag, for a
|
|
37
|
+
browser.
|
|
38
|
+
- `canonicalJson`, `hashAuditEntry`, `verifyAuditChain`: the audit hash chain (SPEC section 8).
|
|
39
|
+
- `bucket(text)`: Flipper's bucket function.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Change } from './store.js';
|
|
2
|
+
/** The state of an approval request. */
|
|
3
|
+
export type ApprovalState = 'pending' | 'approved' | 'rejected' | 'applied' | 'expired';
|
|
4
|
+
/** A change in a protected environment that waits for a second person (four eyes). */
|
|
5
|
+
export interface Approval {
|
|
6
|
+
id: string;
|
|
7
|
+
environment: string;
|
|
8
|
+
state: ApprovalState;
|
|
9
|
+
requestedBy: string;
|
|
10
|
+
requestedAt: string;
|
|
11
|
+
/** The reason of the person who asked. */
|
|
12
|
+
reason: string;
|
|
13
|
+
decidedBy: string | null;
|
|
14
|
+
decisionReason?: string;
|
|
15
|
+
change: Change;
|
|
16
|
+
}
|
|
17
|
+
/** Where approval requests are kept. */
|
|
18
|
+
export interface ApprovalStore {
|
|
19
|
+
create(approval: Approval): Promise<void>;
|
|
20
|
+
get(environment: string, id: string): Promise<Approval | undefined>;
|
|
21
|
+
/** The requests of an environment, oldest first. */
|
|
22
|
+
list(environment: string, state?: ApprovalState): Promise<Approval[]>;
|
|
23
|
+
/**
|
|
24
|
+
* Moves a request from `pending` to another state, in one step. Gives `undefined` when the
|
|
25
|
+
* request is not pending, so two people cannot decide the same request.
|
|
26
|
+
*/
|
|
27
|
+
decide(environment: string, id: string, next: Pick<Approval, 'state' | 'decidedBy' | 'decisionReason'>): Promise<Approval | undefined>;
|
|
28
|
+
/** Sets the state after a decision (for example `approved` to `applied`). */
|
|
29
|
+
setState(environment: string, id: string, state: ApprovalState): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
/** Approval requests in memory. */
|
|
32
|
+
export declare class MemoryApprovalStore implements ApprovalStore {
|
|
33
|
+
#private;
|
|
34
|
+
create(approval: Approval): Promise<void>;
|
|
35
|
+
get(environment: string, id: string): Promise<Approval | undefined>;
|
|
36
|
+
list(environment: string, state?: ApprovalState): Promise<Approval[]>;
|
|
37
|
+
decide(environment: string, id: string, next: Pick<Approval, 'state' | 'decidedBy' | 'decisionReason'>): Promise<Approval | undefined>;
|
|
38
|
+
setState(environment: string, id: string, state: ApprovalState): Promise<void>;
|
|
39
|
+
}
|
package/dist/audit.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Json } from './types.js';
|
|
2
|
+
/** The `action` of an audit entry (SPEC section 8). */
|
|
3
|
+
export type AuditAction = 'flag.create' | 'flag.update' | 'flag.delete' | 'flag.promote' | 'flag.kill' | 'segment.create' | 'segment.update' | 'segment.delete' | 'approval.request' | 'approval.approve' | 'approval.reject' | 'schedule.apply';
|
|
4
|
+
/** Where a change came from. */
|
|
5
|
+
export type AuditSource = 'dashboard' | 'api' | 'cli' | 'code' | 'worker' | 'external';
|
|
6
|
+
/** One entry of the audit hash chain (SPEC section 8). */
|
|
7
|
+
export interface AuditEntry {
|
|
8
|
+
seq: number;
|
|
9
|
+
at: string;
|
|
10
|
+
environment: string;
|
|
11
|
+
flag?: string;
|
|
12
|
+
segment?: string;
|
|
13
|
+
action: AuditAction;
|
|
14
|
+
actor: {
|
|
15
|
+
id: string;
|
|
16
|
+
name?: string;
|
|
17
|
+
};
|
|
18
|
+
source: AuditSource;
|
|
19
|
+
reason?: string;
|
|
20
|
+
before?: {
|
|
21
|
+
[key: string]: Json;
|
|
22
|
+
} | null;
|
|
23
|
+
after?: {
|
|
24
|
+
[key: string]: Json;
|
|
25
|
+
} | null;
|
|
26
|
+
prevHash: string;
|
|
27
|
+
hash: string;
|
|
28
|
+
}
|
|
29
|
+
/** The `prevHash` of the first entry. */
|
|
30
|
+
export declare const GENESIS_HASH: string;
|
|
31
|
+
/** The result of {@link verifyAuditChain}. */
|
|
32
|
+
export type ChainResult = {
|
|
33
|
+
valid: true;
|
|
34
|
+
} | {
|
|
35
|
+
valid: false;
|
|
36
|
+
seq: number;
|
|
37
|
+
error: 'SEQ_GAP' | 'PREV_HASH_MISMATCH' | 'HASH_MISMATCH';
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Canonical JSON (RFC 8785). JavaScript's `JSON.stringify` already gives the RFC's string
|
|
41
|
+
* escapes and number forms, so only the key order needs work.
|
|
42
|
+
*/
|
|
43
|
+
export declare function canonicalJson(value: unknown): string;
|
|
44
|
+
/** The SHA-256 hash of an entry without its `hash` field (SPEC section 8). */
|
|
45
|
+
export declare function hashAuditEntry(entry: Omit<AuditEntry, 'hash'> | AuditEntry): Promise<string>;
|
|
46
|
+
/** Adds `prevHash` and `hash` to an entry. */
|
|
47
|
+
export declare function sealAuditEntry(entry: Omit<AuditEntry, 'hash' | 'prevHash'>, prevHash: string): Promise<AuditEntry>;
|
|
48
|
+
/** Checks a chain in order (SPEC section 8). */
|
|
49
|
+
export declare function verifyAuditChain(entries: readonly AuditEntry[]): Promise<ChainResult>;
|
package/dist/bucket.d.ts
ADDED
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { BooleanKey, FlagDeclarations, ValueOf } from './define.js';
|
|
2
|
+
import type { ClientPayload } from './payload.js';
|
|
3
|
+
import type { ChangeFeed, FlagStore } from './store.js';
|
|
4
|
+
import type { UsageStore } from './usage.js';
|
|
5
|
+
import type { Actor, Context, EvaluationResult, FlagSet, GroupRegistry } from './types.js';
|
|
6
|
+
/** Options for {@link createBenchdog}. */
|
|
7
|
+
export interface BenchdogOptions<D extends FlagDeclarations> {
|
|
8
|
+
store: FlagStore;
|
|
9
|
+
environment: string;
|
|
10
|
+
/** The flag declarations from {@link defineFlags}. They give types and type checks. */
|
|
11
|
+
flags?: D;
|
|
12
|
+
groups?: GroupRegistry;
|
|
13
|
+
/** Loads the flag set again when another process reports a change. */
|
|
14
|
+
feed?: ChangeFeed;
|
|
15
|
+
/** Loads the flag set again at this interval, for hosts with no feed (serverless). */
|
|
16
|
+
pollIntervalMs?: number;
|
|
17
|
+
clock?: () => number;
|
|
18
|
+
random?: () => number;
|
|
19
|
+
/**
|
|
20
|
+
* Records when each flag was last evaluated, for the stale-flag report. The client counts in
|
|
21
|
+
* memory and writes to the store every `flushIntervalMs` (default 60 seconds), so evaluation
|
|
22
|
+
* does no I/O. Call `flushUsage()` before the process ends.
|
|
23
|
+
*/
|
|
24
|
+
usage?: {
|
|
25
|
+
store: UsageStore;
|
|
26
|
+
flushIntervalMs?: number;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** A Benchdog client: it keeps a snapshot of one environment and evaluates from it. */
|
|
30
|
+
export interface Benchdog<D extends FlagDeclarations> {
|
|
31
|
+
/** Resolves after the first load. */
|
|
32
|
+
ready(): Promise<void>;
|
|
33
|
+
/** Loads the flag set again now. */
|
|
34
|
+
refresh(): Promise<void>;
|
|
35
|
+
/** Resolves when no load is in progress. */
|
|
36
|
+
settled(): Promise<void>;
|
|
37
|
+
/** Is a boolean flag on for the actor? `false` before the first load. */
|
|
38
|
+
enabled(key: BooleanKey<D>, actor?: Actor, context?: Omit<Context, 'actor'>): boolean;
|
|
39
|
+
/** The value of a flag, or `defaultValue` on an error. */
|
|
40
|
+
value<K extends keyof D & string>(key: K, actor: Actor | undefined, defaultValue: ValueOf<D[K]>, context?: Omit<Context, 'actor'>): ValueOf<D[K]>;
|
|
41
|
+
/** The full result (SPEC section 5.2). */
|
|
42
|
+
details<K extends keyof D & string>(key: K, actor?: Actor, defaultValue?: ValueOf<D[K]>, context?: Omit<Context, 'actor'>): EvaluationResult<ValueOf<D[K]> | undefined>;
|
|
43
|
+
/**
|
|
44
|
+
* The names of the registered groups whose predicate is true for the actor. Each predicate runs
|
|
45
|
+
* once. Give the result as `memberOf` in the context, so that the next evaluations do not call
|
|
46
|
+
* the predicates again (SPEC section 5.5).
|
|
47
|
+
*/
|
|
48
|
+
groupsFor(actor: Actor): string[];
|
|
49
|
+
/** The client payload for an actor (SPEC section 9). */
|
|
50
|
+
payload(actor?: Actor): ClientPayload;
|
|
51
|
+
/** The current snapshot. */
|
|
52
|
+
readonly snapshot: FlagSet;
|
|
53
|
+
/** Calls `listener` after each load. Returns a function that removes it. */
|
|
54
|
+
onChange(listener: () => void): () => void;
|
|
55
|
+
/** Writes the usage counts to the usage store now. */
|
|
56
|
+
flushUsage(): Promise<void>;
|
|
57
|
+
/** Stops the feed subscription and the poll and flush timers. */
|
|
58
|
+
close(): void;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Creates a client for one environment:
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* const benchdog = createBenchdog({ store, environment: 'production', flags })
|
|
65
|
+
* await benchdog.ready()
|
|
66
|
+
* if (benchdog.enabled('new-checkout', { id: `user:${user.id}` })) { ... }
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare function createBenchdog<D extends FlagDeclarations = FlagDeclarations>(options: BenchdogOptions<D>): Benchdog<D>;
|
package/dist/define.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Json } from './types.js';
|
|
2
|
+
/** The declaration of one flag in code: its type, and for a string flag its variations. */
|
|
3
|
+
export type FlagDeclaration = {
|
|
4
|
+
type: 'boolean';
|
|
5
|
+
} | {
|
|
6
|
+
type: 'string';
|
|
7
|
+
variations?: readonly string[];
|
|
8
|
+
} | {
|
|
9
|
+
type: 'number';
|
|
10
|
+
} | {
|
|
11
|
+
type: 'object';
|
|
12
|
+
};
|
|
13
|
+
/** The flags that an app declares, by key. */
|
|
14
|
+
export type FlagDeclarations = Record<string, FlagDeclaration>;
|
|
15
|
+
/** The value type of a declared flag. */
|
|
16
|
+
export type ValueOf<D extends FlagDeclaration> = D extends {
|
|
17
|
+
type: 'boolean';
|
|
18
|
+
} ? boolean : D extends {
|
|
19
|
+
type: 'string';
|
|
20
|
+
variations: readonly (infer V extends string)[];
|
|
21
|
+
} ? V : D extends {
|
|
22
|
+
type: 'string';
|
|
23
|
+
} ? string : D extends {
|
|
24
|
+
type: 'number';
|
|
25
|
+
} ? number : {
|
|
26
|
+
[key: string]: Json;
|
|
27
|
+
};
|
|
28
|
+
/** The keys of the boolean flags. */
|
|
29
|
+
export type BooleanKey<D extends FlagDeclarations> = {
|
|
30
|
+
[K in keyof D & string]: D[K] extends {
|
|
31
|
+
type: 'boolean';
|
|
32
|
+
} ? K : never;
|
|
33
|
+
}[keyof D & string];
|
|
34
|
+
/**
|
|
35
|
+
* Declares the flags that an app uses, so that a wrong key or value type is a compile error:
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* const flags = defineFlags({ 'new-checkout': { type: 'boolean' } })
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function defineFlags<const D extends FlagDeclarations>(declarations: D): D;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Context, EvaluationResult, Flag, FlagSet, FlagType, GroupRegistry, Json, ScheduleStep } from './types.js';
|
|
2
|
+
/** Options for {@link evaluate}. */
|
|
3
|
+
export interface EvaluateOptions {
|
|
4
|
+
/** The type that the caller asks for. Another flag type gives `TYPE_MISMATCH`. */
|
|
5
|
+
expectedType?: FlagType | undefined;
|
|
6
|
+
/** The group predicates (SPEC section 5.5). */
|
|
7
|
+
groups?: GroupRegistry | undefined;
|
|
8
|
+
/** The clock, in milliseconds. `Date.now` if missing. */
|
|
9
|
+
clock?: () => number;
|
|
10
|
+
/** The random number source for the percentage-of-time gate. `Math.random` if missing. */
|
|
11
|
+
random?: () => number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The flag with each schedule step at or before `now` applied (SPEC section 4). The steps that
|
|
15
|
+
* are not due stay in `schedule`, in their order; with none left, `schedule` is removed.
|
|
16
|
+
*/
|
|
17
|
+
export declare function applySchedule(flag: Flag, now: number): {
|
|
18
|
+
flag: Flag;
|
|
19
|
+
applied: ScheduleStep[];
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Evaluates one flag for a context (SPEC section 5). It never throws: a problem gives
|
|
23
|
+
* `defaultValue` with the reason `ERROR`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function evaluate<T = Json>(flagSet: FlagSet, key: string, context: Context, defaultValue: T, options?: EvaluateOptions): EvaluationResult<T>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Actor, Expression, Json } from './types.js';
|
|
2
|
+
/** A point in time, as seconds since the Unix epoch (SPEC section 6.1). */
|
|
3
|
+
declare class Time {
|
|
4
|
+
readonly seconds: number;
|
|
5
|
+
constructor(seconds: number);
|
|
6
|
+
}
|
|
7
|
+
type Value = null | boolean | number | string | Time | Json[] | {
|
|
8
|
+
[key: string]: Json;
|
|
9
|
+
};
|
|
10
|
+
/** What an expression can read. */
|
|
11
|
+
export interface ExpressionScope {
|
|
12
|
+
flagKey: string;
|
|
13
|
+
actor: Actor | undefined;
|
|
14
|
+
now: number;
|
|
15
|
+
segments: Record<string, Expression>;
|
|
16
|
+
}
|
|
17
|
+
/** A result is truthy when it is not `null` and not `false` (SPEC section 6.1). */
|
|
18
|
+
export declare const truthy: (value: unknown) => boolean;
|
|
19
|
+
/** `String(x)` of SPEC section 6.5. */
|
|
20
|
+
export declare const stringOf: (value: Value) => string;
|
|
21
|
+
/** Evaluates an expression (SPEC section 6). */
|
|
22
|
+
export declare function evaluateExpression(expression: Expression, scope: ExpressionScope): unknown;
|
|
23
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The version of `spec/VERSION` that this package follows. The Ruby and Python cores have the
|
|
3
|
+
* same constant, and each test suite checks it against that file.
|
|
4
|
+
*/
|
|
5
|
+
export declare const SPEC_VERSION = "1";
|
|
6
|
+
export * from './types.js';
|
|
7
|
+
export { bucket, crc32 } from './bucket.js';
|
|
8
|
+
export { evaluate } from './evaluate.js';
|
|
9
|
+
export type { EvaluateOptions } from './evaluate.js';
|
|
10
|
+
export { GENESIS_HASH, canonicalJson, hashAuditEntry, sealAuditEntry, verifyAuditChain, } from './audit.js';
|
|
11
|
+
export type { AuditAction, AuditEntry, AuditSource, ChainResult } from './audit.js';
|
|
12
|
+
export { createBenchdog } from './client.js';
|
|
13
|
+
export type { Benchdog, BenchdogOptions } from './client.js';
|
|
14
|
+
export { defineFlags } from './define.js';
|
|
15
|
+
export type { BooleanKey, FlagDeclaration, FlagDeclarations, ValueOf } from './define.js';
|
|
16
|
+
export { CLIENT_TAG, clientPayload } from './payload.js';
|
|
17
|
+
export type { ClientPayload } from './payload.js';
|
|
18
|
+
export { ALL_CAPABILITIES, MemoryChangeFeed, MemoryStore, NotFoundError, applyChange, describeChange, nextAuditEntry, } from './store.js';
|
|
19
|
+
export type { AuditInput, Capability, Change, ChangeFeed, FlagStore } from './store.js';
|
|
20
|
+
export { applySchedule } from './evaluate.js';
|
|
21
|
+
export { SCHEDULER_ACTOR, applyDueSchedules, startScheduler } from './schedule.js';
|
|
22
|
+
export type { SchedulerOptions } from './schedule.js';
|
|
23
|
+
export { MemoryApprovalStore } from './approvals.js';
|
|
24
|
+
export type { Approval, ApprovalState, ApprovalStore } from './approvals.js';
|
|
25
|
+
export { MemoryUsageStore, mergeUsage, staleFlags } from './usage.js';
|
|
26
|
+
export type { FlagUsage, UsageEntry, UsageStore } from './usage.js';
|
|
27
|
+
export { auditCsv, describeEntry, httpSink, slackSink, teamsSink, verifyWebhookSignature, webhookSink, withAuditSinks, } from './sinks.js';
|
|
28
|
+
export type { AuditSink, SinkedStore } from './sinks.js';
|