versimo 0.0.0-stage → 0.2.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/CHANGELOG.md +23 -0
- package/LICENSE +21 -0
- package/README.md +107 -2
- package/dist/bucket.d.ts +13 -0
- package/dist/bucket.js +60 -0
- package/dist/client.d.ts +24 -0
- package/dist/client.js +85 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/provider.d.ts +28 -0
- package/dist/provider.js +51 -0
- package/dist/resolve.d.ts +5 -0
- package/dist/resolve.js +22 -0
- package/dist/semver.d.ts +11 -0
- package/dist/semver.js +56 -0
- package/dist/transitions.d.ts +14 -0
- package/dist/transitions.js +49 -0
- package/dist/types.d.ts +47 -0
- package/dist/types.js +8 -0
- package/dist/validate.d.ts +6 -0
- package/dist/validate.js +77 -0
- package/docs/api.md +147 -0
- package/docs/concepts.md +63 -0
- package/docs/porting.md +111 -0
- package/docs/record-format.md +61 -0
- package/docs/usage.md +178 -0
- package/package.json +49 -4
- package/schema/examples/billing.json +15 -0
- package/schema/module-record.schema.json +49 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes are listed here. This project follows [semver](https://semver.org).
|
|
4
|
+
|
|
5
|
+
## 0.2.0
|
|
6
|
+
|
|
7
|
+
- Versimo for Python, Go, Kotlin and Swift, in `packages/`
|
|
8
|
+
- Shared language-neutral test vectors in `spec/vectors.json`, generated from the TypeScript code with `npm run spec:generate`
|
|
9
|
+
- `bucketFor` works on engines without `TextEncoder`, such as older React Native
|
|
10
|
+
- Docs for React Native and for porting to other languages
|
|
11
|
+
- A release workflow that publishes to npm with provenance
|
|
12
|
+
|
|
13
|
+
## 0.1.0
|
|
14
|
+
|
|
15
|
+
First release.
|
|
16
|
+
|
|
17
|
+
- Module record format and JSON Schema (2020-12)
|
|
18
|
+
- `resolveVersion` and `resolve` with deterministic bucketing
|
|
19
|
+
- `draftNext`, `promote`, `rollback` and `setRollout`, all pure, all logging history
|
|
20
|
+
- `validateRecord`, hand-written
|
|
21
|
+
- `createClient` with an in-memory cache, a TTL and stale-on-error
|
|
22
|
+
- `VersimoProvider`, OpenFeature compatible by structure
|
|
23
|
+
- Semver helpers
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lucasia Ltd
|
|
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,108 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Versimo
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A release model with no staging.
|
|
4
|
+
|
|
5
|
+
Every module of your product (billing, sign-in, search) has three versions: **last**, **current** and **next**. Next is user acceptance testing on live, behind a flag that reaches a percentage of your users. When next is good, you promote it. Next becomes current and current becomes last. If current fails its health checks, users go back to last on their own, and the fix ships as a semver patch.
|
|
6
|
+
|
|
7
|
+
Versimo is the small, dependency-free library that answers one question: which version of this module should this user get?
|
|
8
|
+
|
|
9
|
+
- Ports for TypeScript, Python, Go, Kotlin and Swift, all with zero runtime dependencies
|
|
10
|
+
- Node 20 and later, modern browsers and React Native
|
|
11
|
+
- Deterministic bucketing, so a user stays on the same version as the rollout grows
|
|
12
|
+
- Pure functions for promote, rollback, rollout and draft
|
|
13
|
+
- A hand-written record validator and a JSON Schema
|
|
14
|
+
- An OpenFeature compatible provider, with no OpenFeature import
|
|
15
|
+
|
|
16
|
+
Site: [versimo.dev](https://versimo.dev)
|
|
17
|
+
|
|
18
|
+
## Languages
|
|
19
|
+
|
|
20
|
+
Every port gives the same answer for the same record and user. They are checked against the same [test vectors](spec/vectors.json).
|
|
21
|
+
|
|
22
|
+
| Language | Package | Install | Docs |
|
|
23
|
+
| --- | --- | --- | --- |
|
|
24
|
+
| TypeScript and JavaScript, including React Native | Versimo for TypeScript | `npm install versimo` | This page |
|
|
25
|
+
| Python 3.10 and later | Versimo for Python | `pip install "git+https://github.com/luca-sia/versimo#subdirectory=packages/python"` | [packages/python](packages/python/README.md) |
|
|
26
|
+
| Go 1.22 and later | Versimo for Go | `go get github.com/luca-sia/versimo/packages/go` | [packages/go](packages/go/README.md) |
|
|
27
|
+
| Kotlin and Android | Versimo for Kotlin | Build from source with `gradle publishToMavenLocal`. Not on Maven Central or JitPack yet | [packages/kotlin](packages/kotlin/README.md) |
|
|
28
|
+
| Swift and iOS | Versimo for Swift | SwiftPM URL `https://github.com/luca-sia/versimo` | [packages/swift](packages/swift/README.md) |
|
|
29
|
+
|
|
30
|
+
Writing another port? See [docs/porting.md](docs/porting.md).
|
|
31
|
+
|
|
32
|
+
## Install
|
|
33
|
+
|
|
34
|
+
The rest of this page uses TypeScript.
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
npm install versimo
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
The package is not on npm yet. Until it is published, install it from GitHub:
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
npm install github:luca-sia/versimo
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The install builds the package on your machine, so it needs a working `npm` and Node 20 or later.
|
|
47
|
+
|
|
48
|
+
## Quick start
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { createClient } from 'versimo';
|
|
52
|
+
|
|
53
|
+
const versimo = createClient({
|
|
54
|
+
source: 'https://config.example.com/versimo', // fetches /versimo/billing.json
|
|
55
|
+
ttlMs: 30_000,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const version = await versimo.version('billing', { userId: 'user-42' });
|
|
59
|
+
// "2.4.0" for most users, "2.5.0" for the share of users on next
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Load the version your code asks for. How you do that is up to you. It might be a dynamic import, a feature branch of a service, or a route to another deployment.
|
|
63
|
+
|
|
64
|
+
You can also work with a record directly:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { resolveVersion, draftNext, setRollout, promote } from 'versimo';
|
|
68
|
+
|
|
69
|
+
let record = await versimo.record('billing');
|
|
70
|
+
record = draftNext(record, '2.6.0', { by: 'alex' });
|
|
71
|
+
record = setRollout(record, 10, { by: 'alex' });
|
|
72
|
+
record = promote(record, { by: 'alex' });
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
These functions never change the record you pass in. They return a new one with a history entry. Saving it is your job.
|
|
76
|
+
|
|
77
|
+
## The record
|
|
78
|
+
|
|
79
|
+
```json
|
|
80
|
+
{
|
|
81
|
+
"module": "billing",
|
|
82
|
+
"last": "2.3.1",
|
|
83
|
+
"current": "2.4.0",
|
|
84
|
+
"next": "2.5.0",
|
|
85
|
+
"rollout": 10,
|
|
86
|
+
"healthy": true,
|
|
87
|
+
"updatedAt": "2026-03-12T09:30:00Z"
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
The full format is in [docs/record-format.md](docs/record-format.md). The JSON Schema is at [schema/module-record.schema.json](schema/module-record.schema.json).
|
|
92
|
+
|
|
93
|
+
## Docs
|
|
94
|
+
|
|
95
|
+
- [Concepts](docs/concepts.md): the model in plain words
|
|
96
|
+
- [Usage](docs/usage.md): server, browser, React, React Native, OpenFeature and PostHog
|
|
97
|
+
- [Porting](docs/porting.md): the algorithm and the test vectors, for new languages
|
|
98
|
+
- [Record format](docs/record-format.md)
|
|
99
|
+
- [API reference](docs/api.md)
|
|
100
|
+
- [Contributing](CONTRIBUTING.md) and [Security](SECURITY.md)
|
|
101
|
+
|
|
102
|
+
## Licence
|
|
103
|
+
|
|
104
|
+
MIT. Copyright 2026 Lucasia Ltd.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
Thinks with Lucasia.ai
|
package/dist/bucket.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UTF-8 bytes of a string. Uses `TextEncoder` when the runtime has it.
|
|
3
|
+
* Older React Native engines do not, so there is a small fallback.
|
|
4
|
+
* A lone surrogate becomes U+FFFD, as `TextEncoder` does.
|
|
5
|
+
*/
|
|
6
|
+
export declare function utf8Bytes(text: string): Uint8Array;
|
|
7
|
+
/**
|
|
8
|
+
* Maps a module and user to a stable bucket from 0 to 99.
|
|
9
|
+
* FNV-1a over the UTF-8 bytes of `module`, a NUL byte and `userId`,
|
|
10
|
+
* then the murmur3 32-bit finaliser, then modulo 100.
|
|
11
|
+
* The steps are simple on purpose so other languages can port them.
|
|
12
|
+
*/
|
|
13
|
+
export declare function bucketFor(module: string, userId: string): number;
|
package/dist/bucket.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
let encoder;
|
|
2
|
+
/**
|
|
3
|
+
* UTF-8 bytes of a string. Uses `TextEncoder` when the runtime has it.
|
|
4
|
+
* Older React Native engines do not, so there is a small fallback.
|
|
5
|
+
* A lone surrogate becomes U+FFFD, as `TextEncoder` does.
|
|
6
|
+
*/
|
|
7
|
+
export function utf8Bytes(text) {
|
|
8
|
+
if (typeof TextEncoder !== 'undefined')
|
|
9
|
+
return (encoder ??= new TextEncoder()).encode(text);
|
|
10
|
+
const out = [];
|
|
11
|
+
for (let i = 0; i < text.length; i++) {
|
|
12
|
+
let cp = text.charCodeAt(i);
|
|
13
|
+
if (cp >= 0xd800 && cp <= 0xdbff) {
|
|
14
|
+
const low = text.charCodeAt(i + 1);
|
|
15
|
+
if (low >= 0xdc00 && low <= 0xdfff) {
|
|
16
|
+
cp = 0x10000 + ((cp - 0xd800) << 10) + (low - 0xdc00);
|
|
17
|
+
i++;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
cp = 0xfffd;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
else if (cp >= 0xdc00 && cp <= 0xdfff) {
|
|
24
|
+
cp = 0xfffd;
|
|
25
|
+
}
|
|
26
|
+
if (cp < 0x80) {
|
|
27
|
+
out.push(cp);
|
|
28
|
+
}
|
|
29
|
+
else if (cp < 0x800) {
|
|
30
|
+
out.push(0xc0 | (cp >> 6), 0x80 | (cp & 0x3f));
|
|
31
|
+
}
|
|
32
|
+
else if (cp < 0x10000) {
|
|
33
|
+
out.push(0xe0 | (cp >> 12), 0x80 | ((cp >> 6) & 0x3f), 0x80 | (cp & 0x3f));
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
out.push(0xf0 | (cp >> 18), 0x80 | ((cp >> 12) & 0x3f), 0x80 | ((cp >> 6) & 0x3f), 0x80 | (cp & 0x3f));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return Uint8Array.from(out);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Maps a module and user to a stable bucket from 0 to 99.
|
|
43
|
+
* FNV-1a over the UTF-8 bytes of `module`, a NUL byte and `userId`,
|
|
44
|
+
* then the murmur3 32-bit finaliser, then modulo 100.
|
|
45
|
+
* The steps are simple on purpose so other languages can port them.
|
|
46
|
+
*/
|
|
47
|
+
export function bucketFor(module, userId) {
|
|
48
|
+
const bytes = utf8Bytes(`${module}\u0000${userId}`);
|
|
49
|
+
let h = 0x811c9dc5;
|
|
50
|
+
for (const byte of bytes) {
|
|
51
|
+
h ^= byte;
|
|
52
|
+
h = Math.imul(h, 0x01000193);
|
|
53
|
+
}
|
|
54
|
+
h ^= h >>> 16;
|
|
55
|
+
h = Math.imul(h, 0x85ebca6b);
|
|
56
|
+
h ^= h >>> 13;
|
|
57
|
+
h = Math.imul(h, 0xc2b2ae35);
|
|
58
|
+
h ^= h >>> 16;
|
|
59
|
+
return (h >>> 0) % 100;
|
|
60
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type Context, type ModuleRecord, type Resolution } from './types.js';
|
|
2
|
+
export type SourceFn = (module: string) => ModuleRecord | null | undefined | Promise<ModuleRecord | null | undefined>;
|
|
3
|
+
export interface ClientOptions {
|
|
4
|
+
/**
|
|
5
|
+
* A function returning the record for a module, or a URL.
|
|
6
|
+
* A URL containing `{module}` has it replaced. Any other URL is a base
|
|
7
|
+
* and the client fetches `<base>/<module>.json`.
|
|
8
|
+
*/
|
|
9
|
+
source: SourceFn | string;
|
|
10
|
+
/** How long a record is cached, in milliseconds. Default 30000. */
|
|
11
|
+
ttlMs?: number;
|
|
12
|
+
/** Clock for the cache, in milliseconds. Mainly for tests. */
|
|
13
|
+
now?: () => number;
|
|
14
|
+
/** Fetch implementation for URL sources. Defaults to the global `fetch`. */
|
|
15
|
+
fetch?: typeof fetch;
|
|
16
|
+
}
|
|
17
|
+
export interface VersimoClient {
|
|
18
|
+
record(module: string): Promise<ModuleRecord>;
|
|
19
|
+
resolve(module: string, ctx?: Context): Promise<Resolution>;
|
|
20
|
+
version(module: string, ctx?: Context): Promise<string>;
|
|
21
|
+
/** Drops one cached record, or all of them. */
|
|
22
|
+
clear(module?: string): void;
|
|
23
|
+
}
|
|
24
|
+
export declare function createClient(options: ClientOptions): VersimoClient;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { resolve } from './resolve.js';
|
|
2
|
+
import { validateRecord } from './validate.js';
|
|
3
|
+
import { VersimoError } from './types.js';
|
|
4
|
+
const SLUG = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
5
|
+
function urlSource(url, fetchImpl) {
|
|
6
|
+
return async (module) => {
|
|
7
|
+
const f = fetchImpl ?? globalThis.fetch;
|
|
8
|
+
if (!f)
|
|
9
|
+
throw new VersimoError('SOURCE_ERROR', 'No fetch available. Pass the `fetch` option.');
|
|
10
|
+
const target = url.includes('{module}')
|
|
11
|
+
? url.replace('{module}', module)
|
|
12
|
+
: `${url.replace(/\/+$/, '')}/${module}.json`;
|
|
13
|
+
let res;
|
|
14
|
+
try {
|
|
15
|
+
res = await f(target);
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
throw new VersimoError('SOURCE_ERROR', `Could not fetch ${target}: ${err.message}`);
|
|
19
|
+
}
|
|
20
|
+
if (res.status === 404)
|
|
21
|
+
return null;
|
|
22
|
+
if (!res.ok)
|
|
23
|
+
throw new VersimoError('SOURCE_ERROR', `Fetching ${target} returned ${res.status}`);
|
|
24
|
+
return (await res.json());
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export function createClient(options) {
|
|
28
|
+
const ttlMs = options.ttlMs ?? 30_000;
|
|
29
|
+
const now = options.now ?? Date.now;
|
|
30
|
+
const source = typeof options.source === 'string' ? urlSource(options.source, options.fetch) : options.source;
|
|
31
|
+
const cache = new Map();
|
|
32
|
+
const inflight = new Map();
|
|
33
|
+
async function load(module) {
|
|
34
|
+
const raw = await source(module);
|
|
35
|
+
if (raw === null || raw === undefined)
|
|
36
|
+
throw new VersimoError('NOT_FOUND', `No record for module "${module}"`);
|
|
37
|
+
const checked = validateRecord(raw);
|
|
38
|
+
if (!checked.ok) {
|
|
39
|
+
throw new VersimoError('INVALID_RECORD', `Invalid record for "${module}": ${checked.errors.join('; ')}`);
|
|
40
|
+
}
|
|
41
|
+
return raw;
|
|
42
|
+
}
|
|
43
|
+
async function record(module) {
|
|
44
|
+
if (!SLUG.test(module))
|
|
45
|
+
throw new VersimoError('INVALID_INPUT', `Invalid module slug: ${JSON.stringify(module)}`);
|
|
46
|
+
const hit = cache.get(module);
|
|
47
|
+
if (hit && hit.expires > now())
|
|
48
|
+
return hit.record;
|
|
49
|
+
const pending = inflight.get(module);
|
|
50
|
+
if (pending)
|
|
51
|
+
return pending;
|
|
52
|
+
const p = load(module)
|
|
53
|
+
.then((rec) => {
|
|
54
|
+
cache.set(module, { record: rec, expires: now() + ttlMs });
|
|
55
|
+
return rec;
|
|
56
|
+
})
|
|
57
|
+
.catch((err) => {
|
|
58
|
+
const e = err instanceof VersimoError ? err : new VersimoError('SOURCE_ERROR', err.message);
|
|
59
|
+
// Serve the stale record when the source is down. A missing or invalid record is not hidden.
|
|
60
|
+
if (hit && e.code === 'SOURCE_ERROR') {
|
|
61
|
+
cache.set(module, { record: hit.record, expires: now() + ttlMs });
|
|
62
|
+
return hit.record;
|
|
63
|
+
}
|
|
64
|
+
throw e;
|
|
65
|
+
})
|
|
66
|
+
.finally(() => inflight.delete(module));
|
|
67
|
+
inflight.set(module, p);
|
|
68
|
+
return p;
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
record,
|
|
72
|
+
async resolve(module, ctx) {
|
|
73
|
+
return resolve(await record(module), ctx);
|
|
74
|
+
},
|
|
75
|
+
async version(module, ctx) {
|
|
76
|
+
return (await this.resolve(module, ctx)).version;
|
|
77
|
+
},
|
|
78
|
+
clear(module) {
|
|
79
|
+
if (module === undefined)
|
|
80
|
+
cache.clear();
|
|
81
|
+
else
|
|
82
|
+
cache.delete(module);
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { bucketFor } from './bucket.js';
|
|
2
|
+
export { resolve, resolveVersion } from './resolve.js';
|
|
3
|
+
export { draftNext, promote, rollback, setRollout } from './transitions.js';
|
|
4
|
+
export { validateRecord, type ValidationResult } from './validate.js';
|
|
5
|
+
export { compareSemver, isSemver, parseSemver, SEMVER_PATTERN, type ParsedSemver } from './semver.js';
|
|
6
|
+
export { createClient, type ClientOptions, type SourceFn, type VersimoClient } from './client.js';
|
|
7
|
+
export { VersimoProvider, type EvaluationContext, type ResolutionDetails } from './provider.js';
|
|
8
|
+
export { VersimoError, type Action, type Channel, type Context, type ErrorCode, type HistoryEntry, type ModuleRecord, type Resolution, type ResolutionReason, type TransitionOptions, } from './types.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { bucketFor } from './bucket.js';
|
|
2
|
+
export { resolve, resolveVersion } from './resolve.js';
|
|
3
|
+
export { draftNext, promote, rollback, setRollout } from './transitions.js';
|
|
4
|
+
export { validateRecord } from './validate.js';
|
|
5
|
+
export { compareSemver, isSemver, parseSemver, SEMVER_PATTERN } from './semver.js';
|
|
6
|
+
export { createClient } from './client.js';
|
|
7
|
+
export { VersimoProvider } from './provider.js';
|
|
8
|
+
export { VersimoError, } from './types.js';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { VersimoClient } from './client.js';
|
|
2
|
+
/** Structural copies of the OpenFeature types, so this package imports nothing. */
|
|
3
|
+
export interface EvaluationContext {
|
|
4
|
+
targetingKey?: string;
|
|
5
|
+
[key: string]: unknown;
|
|
6
|
+
}
|
|
7
|
+
export interface ResolutionDetails<T> {
|
|
8
|
+
value: T;
|
|
9
|
+
variant?: string;
|
|
10
|
+
reason?: string;
|
|
11
|
+
errorCode?: string;
|
|
12
|
+
errorMessage?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The flag key is the module slug. The value is the version string.
|
|
16
|
+
* The targeting key is the user id. The variant is `next`, `current` or `last`.
|
|
17
|
+
*/
|
|
18
|
+
export declare class VersimoProvider {
|
|
19
|
+
private readonly client;
|
|
20
|
+
readonly metadata: {
|
|
21
|
+
readonly name: "versimo";
|
|
22
|
+
};
|
|
23
|
+
constructor(client: VersimoClient);
|
|
24
|
+
resolveStringEvaluation(flagKey: string, defaultValue: string, context?: EvaluationContext): Promise<ResolutionDetails<string>>;
|
|
25
|
+
resolveBooleanEvaluation(_flagKey: string, defaultValue: boolean): Promise<ResolutionDetails<boolean>>;
|
|
26
|
+
resolveNumberEvaluation(_flagKey: string, defaultValue: number): Promise<ResolutionDetails<number>>;
|
|
27
|
+
resolveObjectEvaluation<T>(_flagKey: string, defaultValue: T): Promise<ResolutionDetails<T>>;
|
|
28
|
+
}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { VersimoError } from './types.js';
|
|
2
|
+
const REASONS = {
|
|
3
|
+
ROLLOUT: 'SPLIT',
|
|
4
|
+
DEFAULT: 'STATIC',
|
|
5
|
+
NO_USER: 'STATIC',
|
|
6
|
+
UNHEALTHY: 'DISABLED',
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* The flag key is the module slug. The value is the version string.
|
|
10
|
+
* The targeting key is the user id. The variant is `next`, `current` or `last`.
|
|
11
|
+
*/
|
|
12
|
+
export class VersimoProvider {
|
|
13
|
+
client;
|
|
14
|
+
metadata = { name: 'versimo' };
|
|
15
|
+
constructor(client) {
|
|
16
|
+
this.client = client;
|
|
17
|
+
}
|
|
18
|
+
async resolveStringEvaluation(flagKey, defaultValue, context = {}) {
|
|
19
|
+
try {
|
|
20
|
+
const userId = typeof context.targetingKey === 'string' ? context.targetingKey : undefined;
|
|
21
|
+
const r = await this.client.resolve(flagKey, { userId });
|
|
22
|
+
return { value: r.version, variant: r.channel, reason: REASONS[r.reason] };
|
|
23
|
+
}
|
|
24
|
+
catch (err) {
|
|
25
|
+
const code = err instanceof VersimoError ? err.code : 'SOURCE_ERROR';
|
|
26
|
+
return {
|
|
27
|
+
value: defaultValue,
|
|
28
|
+
reason: 'ERROR',
|
|
29
|
+
errorCode: code === 'NOT_FOUND' ? 'FLAG_NOT_FOUND' : code === 'INVALID_INPUT' ? 'INVALID_CONTEXT' : 'GENERAL',
|
|
30
|
+
errorMessage: err.message,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async resolveBooleanEvaluation(_flagKey, defaultValue) {
|
|
35
|
+
return mismatch(defaultValue);
|
|
36
|
+
}
|
|
37
|
+
async resolveNumberEvaluation(_flagKey, defaultValue) {
|
|
38
|
+
return mismatch(defaultValue);
|
|
39
|
+
}
|
|
40
|
+
async resolveObjectEvaluation(_flagKey, defaultValue) {
|
|
41
|
+
return mismatch(defaultValue);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function mismatch(value) {
|
|
45
|
+
return {
|
|
46
|
+
value,
|
|
47
|
+
reason: 'ERROR',
|
|
48
|
+
errorCode: 'TYPE_MISMATCH',
|
|
49
|
+
errorMessage: 'Versimo flags are version strings. Use resolveStringEvaluation.',
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Context, ModuleRecord, Resolution } from './types.js';
|
|
2
|
+
/** Like `resolveVersion`, but says which channel was chosen and why. */
|
|
3
|
+
export declare function resolve(record: ModuleRecord, ctx?: Context): Resolution;
|
|
4
|
+
/** The version of the module this user should get. */
|
|
5
|
+
export declare function resolveVersion(record: ModuleRecord, ctx?: Context): string;
|
package/dist/resolve.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { bucketFor } from './bucket.js';
|
|
2
|
+
/** Like `resolveVersion`, but says which channel was chosen and why. */
|
|
3
|
+
export function resolve(record, ctx = {}) {
|
|
4
|
+
if (!record.healthy) {
|
|
5
|
+
if (record.last !== null) {
|
|
6
|
+
return { version: record.last, channel: 'last', reason: 'UNHEALTHY', bucket: null };
|
|
7
|
+
}
|
|
8
|
+
return { version: record.current, channel: 'current', reason: 'UNHEALTHY', bucket: null };
|
|
9
|
+
}
|
|
10
|
+
if (!ctx.userId) {
|
|
11
|
+
return { version: record.current, channel: 'current', reason: 'NO_USER', bucket: null };
|
|
12
|
+
}
|
|
13
|
+
const bucket = bucketFor(record.module, ctx.userId);
|
|
14
|
+
if (record.next !== null && bucket < record.rollout) {
|
|
15
|
+
return { version: record.next, channel: 'next', reason: 'ROLLOUT', bucket };
|
|
16
|
+
}
|
|
17
|
+
return { version: record.current, channel: 'current', reason: 'DEFAULT', bucket };
|
|
18
|
+
}
|
|
19
|
+
/** The version of the module this user should get. */
|
|
20
|
+
export function resolveVersion(record, ctx = {}) {
|
|
21
|
+
return resolve(record, ctx).version;
|
|
22
|
+
}
|
package/dist/semver.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const SEMVER_PATTERN: string;
|
|
2
|
+
export interface ParsedSemver {
|
|
3
|
+
major: number;
|
|
4
|
+
minor: number;
|
|
5
|
+
patch: number;
|
|
6
|
+
prerelease: string[];
|
|
7
|
+
}
|
|
8
|
+
export declare function isSemver(value: unknown): value is string;
|
|
9
|
+
export declare function parseSemver(value: string): ParsedSemver;
|
|
10
|
+
/** Returns -1, 0 or 1. Build metadata is ignored, as the semver spec says. */
|
|
11
|
+
export declare function compareSemver(a: string, b: string): -1 | 0 | 1;
|
package/dist/semver.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const IDENT = '(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)';
|
|
2
|
+
export const SEMVER_PATTERN = `^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)` +
|
|
3
|
+
`(?:-(${IDENT}(?:\\.${IDENT})*))?` +
|
|
4
|
+
`(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$`;
|
|
5
|
+
const SEMVER = new RegExp(SEMVER_PATTERN);
|
|
6
|
+
export function isSemver(value) {
|
|
7
|
+
return typeof value === 'string' && SEMVER.test(value);
|
|
8
|
+
}
|
|
9
|
+
export function parseSemver(value) {
|
|
10
|
+
const m = SEMVER.exec(value);
|
|
11
|
+
if (!m)
|
|
12
|
+
throw new TypeError(`Invalid semver: ${JSON.stringify(value)}`);
|
|
13
|
+
return {
|
|
14
|
+
major: Number(m[1]),
|
|
15
|
+
minor: Number(m[2]),
|
|
16
|
+
patch: Number(m[3]),
|
|
17
|
+
prerelease: m[4] ? m[4].split('.') : [],
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
const isNumeric = (s) => /^\d+$/.test(s);
|
|
21
|
+
/** Returns -1, 0 or 1. Build metadata is ignored, as the semver spec says. */
|
|
22
|
+
export function compareSemver(a, b) {
|
|
23
|
+
const x = parseSemver(a);
|
|
24
|
+
const y = parseSemver(b);
|
|
25
|
+
for (const key of ['major', 'minor', 'patch']) {
|
|
26
|
+
if (x[key] !== y[key])
|
|
27
|
+
return x[key] < y[key] ? -1 : 1;
|
|
28
|
+
}
|
|
29
|
+
if (x.prerelease.length === 0 && y.prerelease.length === 0)
|
|
30
|
+
return 0;
|
|
31
|
+
if (x.prerelease.length === 0)
|
|
32
|
+
return 1;
|
|
33
|
+
if (y.prerelease.length === 0)
|
|
34
|
+
return -1;
|
|
35
|
+
const len = Math.max(x.prerelease.length, y.prerelease.length);
|
|
36
|
+
for (let i = 0; i < len; i++) {
|
|
37
|
+
const p = x.prerelease[i];
|
|
38
|
+
const q = y.prerelease[i];
|
|
39
|
+
if (p === undefined)
|
|
40
|
+
return -1;
|
|
41
|
+
if (q === undefined)
|
|
42
|
+
return 1;
|
|
43
|
+
if (p === q)
|
|
44
|
+
continue;
|
|
45
|
+
const pn = isNumeric(p);
|
|
46
|
+
const qn = isNumeric(q);
|
|
47
|
+
if (pn && qn)
|
|
48
|
+
return Number(p) < Number(q) ? -1 : 1;
|
|
49
|
+
if (pn)
|
|
50
|
+
return -1;
|
|
51
|
+
if (qn)
|
|
52
|
+
return 1;
|
|
53
|
+
return p < q ? -1 : 1;
|
|
54
|
+
}
|
|
55
|
+
return 0;
|
|
56
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ModuleRecord, type TransitionOptions } from './types.js';
|
|
2
|
+
/** Sets a new next version at 0% rollout. It replaces any next already drafted. */
|
|
3
|
+
export declare function draftNext(record: ModuleRecord, version: string, opts?: TransitionOptions & {
|
|
4
|
+
patch?: boolean;
|
|
5
|
+
}): ModuleRecord;
|
|
6
|
+
/** Moves next to current and current to last. Rollout resets to 0. */
|
|
7
|
+
export declare function promote(record: ModuleRecord, opts?: TransitionOptions): ModuleRecord;
|
|
8
|
+
/**
|
|
9
|
+
* Puts last back as current. The failed version and any drafted next are dropped,
|
|
10
|
+
* so there is nothing left to roll back to until the next promote.
|
|
11
|
+
*/
|
|
12
|
+
export declare function rollback(record: ModuleRecord, opts?: TransitionOptions): ModuleRecord;
|
|
13
|
+
/** Sets the share of users on next, an integer from 0 to 100. */
|
|
14
|
+
export declare function setRollout(record: ModuleRecord, n: number, opts?: TransitionOptions): ModuleRecord;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { compareSemver, isSemver } from './semver.js';
|
|
2
|
+
import { VersimoError, } from './types.js';
|
|
3
|
+
function stamp(opts) {
|
|
4
|
+
const d = new Date(opts.now ?? Date.now());
|
|
5
|
+
if (Number.isNaN(d.getTime()))
|
|
6
|
+
throw new VersimoError('INVALID_INPUT', 'Invalid `now` option');
|
|
7
|
+
return d.toISOString();
|
|
8
|
+
}
|
|
9
|
+
function apply(record, changes, action, from, to, opts) {
|
|
10
|
+
const at = stamp(opts);
|
|
11
|
+
const entry = { at, action, from, to };
|
|
12
|
+
if (opts.by !== undefined)
|
|
13
|
+
entry.by = opts.by;
|
|
14
|
+
return { ...record, ...changes, updatedAt: at, history: [...(record.history ?? []), entry] };
|
|
15
|
+
}
|
|
16
|
+
/** Sets a new next version at 0% rollout. It replaces any next already drafted. */
|
|
17
|
+
export function draftNext(record, version, opts = {}) {
|
|
18
|
+
if (!isSemver(version))
|
|
19
|
+
throw new VersimoError('INVALID_INPUT', `Invalid semver: ${version}`);
|
|
20
|
+
if (compareSemver(version, record.current) <= 0) {
|
|
21
|
+
throw new VersimoError('INVALID_INPUT', `Next ${version} must be greater than current ${record.current}`);
|
|
22
|
+
}
|
|
23
|
+
return apply(record, { next: version, rollout: 0 }, opts.patch ? 'patch' : 'draft', record.next ?? record.current, version, opts);
|
|
24
|
+
}
|
|
25
|
+
/** Moves next to current and current to last. Rollout resets to 0. */
|
|
26
|
+
export function promote(record, opts = {}) {
|
|
27
|
+
if (record.next === null)
|
|
28
|
+
throw new VersimoError('INVALID_INPUT', 'Nothing to promote: next is empty');
|
|
29
|
+
return apply(record, { last: record.current, current: record.next, next: null, rollout: 0, healthy: true }, 'promote', record.current, record.next, opts);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Puts last back as current. The failed version and any drafted next are dropped,
|
|
33
|
+
* so there is nothing left to roll back to until the next promote.
|
|
34
|
+
*/
|
|
35
|
+
export function rollback(record, opts = {}) {
|
|
36
|
+
if (record.last === null)
|
|
37
|
+
throw new VersimoError('INVALID_INPUT', 'Nothing to roll back to: last is empty');
|
|
38
|
+
return apply(record, { last: null, current: record.last, next: null, rollout: 0, healthy: true }, 'rollback', record.current, record.last, opts);
|
|
39
|
+
}
|
|
40
|
+
/** Sets the share of users on next, an integer from 0 to 100. */
|
|
41
|
+
export function setRollout(record, n, opts = {}) {
|
|
42
|
+
if (!Number.isInteger(n) || n < 0 || n > 100) {
|
|
43
|
+
throw new VersimoError('INVALID_INPUT', 'Rollout must be an integer from 0 to 100');
|
|
44
|
+
}
|
|
45
|
+
if (n > 0 && record.next === null) {
|
|
46
|
+
throw new VersimoError('INVALID_INPUT', 'Cannot roll out: next is empty');
|
|
47
|
+
}
|
|
48
|
+
return apply(record, { rollout: n }, 'rollout', String(record.rollout), String(n), opts);
|
|
49
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export type Action = 'draft' | 'promote' | 'rollback' | 'rollout' | 'patch';
|
|
2
|
+
export interface HistoryEntry {
|
|
3
|
+
/** ISO 8601 date-time. */
|
|
4
|
+
at: string;
|
|
5
|
+
action: Action;
|
|
6
|
+
from: string | null;
|
|
7
|
+
to: string | null;
|
|
8
|
+
by?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface ModuleRecord {
|
|
11
|
+
$schema?: string;
|
|
12
|
+
/** Lower-case slug, for example `billing` or `sign-in`. */
|
|
13
|
+
module: string;
|
|
14
|
+
last: string | null;
|
|
15
|
+
current: string;
|
|
16
|
+
next: string | null;
|
|
17
|
+
/** Share of users on `next`, an integer from 0 to 100. */
|
|
18
|
+
rollout: number;
|
|
19
|
+
/** Health of `current`. When false, users fall back to `last`. */
|
|
20
|
+
healthy: boolean;
|
|
21
|
+
/** ISO 8601 date-time. */
|
|
22
|
+
updatedAt: string;
|
|
23
|
+
history?: HistoryEntry[];
|
|
24
|
+
}
|
|
25
|
+
export interface Context {
|
|
26
|
+
userId?: string;
|
|
27
|
+
}
|
|
28
|
+
export type Channel = 'next' | 'current' | 'last';
|
|
29
|
+
export type ResolutionReason = 'ROLLOUT' | 'DEFAULT' | 'NO_USER' | 'UNHEALTHY';
|
|
30
|
+
export interface Resolution {
|
|
31
|
+
version: string;
|
|
32
|
+
channel: Channel;
|
|
33
|
+
reason: ResolutionReason;
|
|
34
|
+
/** The user's bucket from 0 to 99, or null when there was no user id. */
|
|
35
|
+
bucket: number | null;
|
|
36
|
+
}
|
|
37
|
+
export interface TransitionOptions {
|
|
38
|
+
/** Who made the change. Recorded in history. */
|
|
39
|
+
by?: string;
|
|
40
|
+
/** Time of the change. Defaults to now. */
|
|
41
|
+
now?: Date | string | number;
|
|
42
|
+
}
|
|
43
|
+
export type ErrorCode = 'INVALID_INPUT' | 'INVALID_RECORD' | 'NOT_FOUND' | 'SOURCE_ERROR';
|
|
44
|
+
export declare class VersimoError extends Error {
|
|
45
|
+
readonly code: ErrorCode;
|
|
46
|
+
constructor(code: ErrorCode, message: string);
|
|
47
|
+
}
|