@zigordev/flags 0.0.0-stage → 1.25.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 +94 -2
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -3
package/README.md
CHANGED
|
@@ -1,3 +1,95 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zigordev/flags
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Feature flags for the apps of the platform. A flag lets unfinished work ship switched off, and lets it be switched on later by a one-line change. Code asks for a flag through OpenFeature, and the answer comes from the app's own `flags.json`.
|
|
4
|
+
|
|
5
|
+
## What it is
|
|
6
|
+
|
|
7
|
+
| Export | What it is |
|
|
8
|
+
| --------------------------- | ---------------------------------------------------------------------------------------------------------------- |
|
|
9
|
+
| `startFlags({ app, file })` | Reads the app's flags file, and gives an OpenFeature client to ask with, the health part `flags`, and a `stop()` |
|
|
10
|
+
| `flagsSettings` | The one setting an app reads, `FLAGS_FILE`, as a Zod field for its config schema |
|
|
11
|
+
|
|
12
|
+
OpenFeature is the open standard for asking whether a feature is on: code asks a client, and a provider behind it finds the answer. flagd is OpenFeature's own flag format and rules. Here its provider reads the file inside the app, so no flag server runs.
|
|
13
|
+
|
|
14
|
+
## Why we have it
|
|
15
|
+
|
|
16
|
+
Every change merges and deploys by itself once `check` is green, so a feature that is not finished has to ship switched off. The core rules say so: unfinished work ships behind a flag that carries a removal date.
|
|
17
|
+
|
|
18
|
+
[Record 0046](../../docs/adr/0046-each-app-reads-its-own-flags-in-the-app.md) holds how: each app reads its own `flags.json`, because the host reads only a product's compose files and its list of secrets. [Record 0030](../../docs/adr/0030-no-console-for-feature-flags-for-now.md) keeps a flag change a pull request, which takes effect with the deploy.
|
|
19
|
+
|
|
20
|
+
Without flags, a half-built feature would wait on a branch for days, against the rule that a branch lives for hours. Without removal dates, flags would pile up, and nobody would know which ones still matter.
|
|
21
|
+
|
|
22
|
+
## How it works here
|
|
23
|
+
|
|
24
|
+
```mermaid
|
|
25
|
+
flowchart LR
|
|
26
|
+
file["apps/notes-api/flags.json"] --> image["The app's image"]
|
|
27
|
+
image --> provider["flagd's provider, inside the app"]
|
|
28
|
+
code["client.getBooleanValue('new-search', false)"] --> provider
|
|
29
|
+
provider --> answer["true or false"]
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
- **The file.** Each app has a `flags.json` beside its `package.json`, listed in its `files`, so its image carries it. The file follows flagd's format: each flag has a state, its variants, the default variant and, here, a removal date.
|
|
33
|
+
- **The answer.** The provider evaluates each flag in the app, with no network call. A flag can answer differently for different callers through flagd's targeting rules, from the context the code passes.
|
|
34
|
+
- **The default.** Code always passes a default. A flag that is switched off, missing or broken answers that default, and the details say why.
|
|
35
|
+
- **The start.** `startFlags` refuses a file it cannot read or parse, so the app stops as it starts, and the deploy's health gate keeps the old version.
|
|
36
|
+
- **The removal date.** The `flags` rule of `zigordev check` reads every `flags.json` of the repo, in `pnpm check` and in the pipeline. It fails on a flag without `metadata.removeBy` and on a flag whose date has passed, and names the file and the line.
|
|
37
|
+
- **The health answer.** The part `flags` is up once the file is read. It is not required, because every flag has a default.
|
|
38
|
+
|
|
39
|
+
Nothing differs between the laptop and production: the same file is read in both.
|
|
40
|
+
|
|
41
|
+
## How to use it
|
|
42
|
+
|
|
43
|
+
Add the package and the libraries it shares with the app:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pnpm add @zigordev/flags @openfeature/server-sdk @grpc/grpc-js zod
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Write the app's `flags.json`. A new flag starts switched off:
|
|
50
|
+
|
|
51
|
+
```json
|
|
52
|
+
{
|
|
53
|
+
"$schema": "https://flagd.dev/schema/v0/flags.json",
|
|
54
|
+
"flags": {
|
|
55
|
+
"new-search": {
|
|
56
|
+
"state": "ENABLED",
|
|
57
|
+
"variants": { "on": true, "off": false },
|
|
58
|
+
"defaultVariant": "off",
|
|
59
|
+
"metadata": { "removeBy": "2027-03-31" }
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Read the file's path in the config, with the app's own file as the default, and start the flags with the app:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { fileURLToPath } from 'node:url';
|
|
69
|
+
import { addHealthComponent } from '@zigordev/api';
|
|
70
|
+
import { flagsSettings, startFlags } from '@zigordev/flags';
|
|
71
|
+
|
|
72
|
+
const schema = z.object({
|
|
73
|
+
...service,
|
|
74
|
+
FLAGS_FILE: flagsSettings.FLAGS_FILE.default(
|
|
75
|
+
fileURLToPath(new URL('../flags.json', import.meta.url))
|
|
76
|
+
),
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const flags = await startFlags({ app: config.OTEL_SERVICE_NAME, file: config.FLAGS_FILE });
|
|
80
|
+
addHealthComponent(app, flags.health);
|
|
81
|
+
app.addHook('onClose', () => flags.stop());
|
|
82
|
+
|
|
83
|
+
const results = (await flags.client.getBooleanValue('new-search', false))
|
|
84
|
+
? await searchEverywhere(query)
|
|
85
|
+
: await searchTitles(query);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
To switch the feature on, a pull request sets `"defaultVariant": "on"`. To finish, a pull request removes the flag and the code that asks for it, before its date. A test sets a flag by pointing `FLAGS_FILE` at a file of its own.
|
|
89
|
+
|
|
90
|
+
## How to check that it works
|
|
91
|
+
|
|
92
|
+
- `pnpm exec vitest run --project flags` runs the package's tests on real files: a flag on, a flag switched off, a missing flag, targeting, the health part and a broken file.
|
|
93
|
+
- `pnpm exec zigordev check flags` lists every flag without a removal date or past it.
|
|
94
|
+
- When a flag always answers its default, the details from `getBooleanDetails` say why: `FLAG_NOT_FOUND` for a name that is not in the file, or the flag is `DISABLED`.
|
|
95
|
+
- When the app does not start and says `The flags file ... cannot be read`, its image lacks the file: the app's `package.json` must list `flags.json` in `files`, and its Dockerfile copies it.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Client } from '@openfeature/server-sdk';
|
|
2
|
+
import type { HealthComponent } from '@zigordev/otel';
|
|
3
|
+
import * as z from 'zod';
|
|
4
|
+
export declare const flagsSettings: {
|
|
5
|
+
FLAGS_FILE: z.ZodString;
|
|
6
|
+
};
|
|
7
|
+
export interface FlagsOptions {
|
|
8
|
+
readonly app: string;
|
|
9
|
+
readonly file: string;
|
|
10
|
+
}
|
|
11
|
+
export interface Flags {
|
|
12
|
+
readonly client: Client;
|
|
13
|
+
readonly health: HealthComponent;
|
|
14
|
+
stop(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export declare function startFlags(options: FlagsOptions): Promise<Flags>;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,MAAM,EAA+B,MAAM,yBAAyB,CAAC;AACnF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB,eAAO,MAAM,aAAa;IACxB,UAAU;CACX,CAAC;AAEF,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,KAAK;IACpB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;IACjC,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACvB;AAgBD,wBAAsB,UAAU,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,CAmBtE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { FlagdProvider } from '@openfeature/flagd-provider';
|
|
3
|
+
import { OpenFeature, ProviderStatus } from '@openfeature/server-sdk';
|
|
4
|
+
import * as z from 'zod';
|
|
5
|
+
export const flagsSettings = {
|
|
6
|
+
FLAGS_FILE: z.string().min(1),
|
|
7
|
+
};
|
|
8
|
+
async function readFlagsFile(file) {
|
|
9
|
+
let text;
|
|
10
|
+
try {
|
|
11
|
+
text = await readFile(file, 'utf8');
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
throw new Error(`The flags file ${file} cannot be read`, { cause: error });
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
JSON.parse(text);
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
throw new Error(`The flags file ${file} is not JSON`, { cause: error });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export async function startFlags(options) {
|
|
24
|
+
await readFlagsFile(options.file);
|
|
25
|
+
const provider = new FlagdProvider({
|
|
26
|
+
resolverType: 'in-process',
|
|
27
|
+
offlineFlagSourcePath: options.file,
|
|
28
|
+
});
|
|
29
|
+
await OpenFeature.setProviderAndWait(options.app, provider);
|
|
30
|
+
const client = OpenFeature.getClient(options.app);
|
|
31
|
+
return {
|
|
32
|
+
client,
|
|
33
|
+
health: {
|
|
34
|
+
name: 'flags',
|
|
35
|
+
required: false,
|
|
36
|
+
check: async () => (client.providerStatus === ProviderStatus.READY ? 'up' : 'down'),
|
|
37
|
+
},
|
|
38
|
+
stop: async () => {
|
|
39
|
+
await provider.onClose();
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5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YACN,IAAI,EAAE,OAAO;YACb,QAAQ,EAAE,KAAK;YACf,KAAK,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,cAAc,KAAK,cAAc,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC;SACpF;QACD,IAAI,EAAE,KAAK,IAAI,EAAE;YACf,MAAM,QAAQ,CAAC,OAAO,EAAE,CAAC;QAC3B,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zigordev/flags",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.25.0",
|
|
4
|
+
"description": "Feature flags for zigordev apps: OpenFeature, reading the app's own flags.json in the app through flagd's provider",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=26"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@openfeature/flagd-provider": "0.18.0"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"@grpc/grpc-js": "^1.14.0",
|
|
24
|
+
"@openfeature/server-sdk": "^1.23.0",
|
|
25
|
+
"@zigordev/otel": "^1.25.0",
|
|
26
|
+
"zod": "^4.6.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@grpc/grpc-js": "1.14.7",
|
|
30
|
+
"@openfeature/server-sdk": "1.23.0",
|
|
31
|
+
"@types/node": "26.6.6",
|
|
32
|
+
"@zigordev/dev": "1.25.0",
|
|
33
|
+
"@zigordev/otel": "1.25.0",
|
|
34
|
+
"typescript": "7.0.2",
|
|
35
|
+
"vitest": "5.1.1",
|
|
36
|
+
"zod": "4.6.7"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/zigordev/toolkit.git",
|
|
44
|
+
"directory": "packages/flags"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsc -p tsconfig.build.json",
|
|
48
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
49
|
+
}
|
|
6
50
|
}
|