@serve.zone/witness 0.0.0-stage → 1.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.
Files changed (46) hide show
  1. package/.smartconfig.json +72 -0
  2. package/changelog.md +13 -0
  3. package/cli.js +4 -0
  4. package/dist_ts/00_commitinfo_data.d.ts +8 -0
  5. package/dist_ts/00_commitinfo_data.js +9 -0
  6. package/dist_ts/classes.models.d.ts +60 -0
  7. package/dist_ts/classes.models.js +158 -0
  8. package/dist_ts/classes.refusal.d.ts +16 -0
  9. package/dist_ts/classes.refusal.js +21 -0
  10. package/dist_ts/classes.register.d.ts +70 -0
  11. package/dist_ts/classes.register.js +306 -0
  12. package/dist_ts/classes.server.d.ts +38 -0
  13. package/dist_ts/classes.server.js +86 -0
  14. package/dist_ts/classes.storage.d.ts +45 -0
  15. package/dist_ts/classes.storage.js +110 -0
  16. package/dist_ts/classes.witness.d.ts +29 -0
  17. package/dist_ts/classes.witness.js +82 -0
  18. package/dist_ts/index.d.ts +7 -0
  19. package/dist_ts/index.js +8 -0
  20. package/dist_ts/plugins.d.ts +13 -0
  21. package/dist_ts/plugins.js +19 -0
  22. package/dist_ts/witness.cli.d.ts +16 -0
  23. package/dist_ts/witness.cli.js +145 -0
  24. package/dist_ts/witness.config.d.ts +41 -0
  25. package/dist_ts/witness.config.js +71 -0
  26. package/dist_ts/witness.enroll.d.ts +26 -0
  27. package/dist_ts/witness.enroll.js +74 -0
  28. package/dist_ts/witness.log.d.ts +28 -0
  29. package/dist_ts/witness.log.js +34 -0
  30. package/license.md +21 -0
  31. package/package.json +45 -3
  32. package/readme.md +96 -0
  33. package/ts/00_commitinfo_data.ts +8 -0
  34. package/ts/classes.models.ts +100 -0
  35. package/ts/classes.refusal.ts +28 -0
  36. package/ts/classes.register.ts +389 -0
  37. package/ts/classes.server.ts +129 -0
  38. package/ts/classes.storage.ts +123 -0
  39. package/ts/classes.witness.ts +99 -0
  40. package/ts/index.ts +23 -0
  41. package/ts/plugins.ts +38 -0
  42. package/ts/witness.cli.ts +168 -0
  43. package/ts/witness.config.ts +107 -0
  44. package/ts/witness.enroll.ts +113 -0
  45. package/ts/witness.log.ts +58 -0
  46. package/README.md +0 -3
package/license.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Task Venture Capital GmbH
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/package.json CHANGED
@@ -1,6 +1,48 @@
1
1
  {
2
2
  "name": "@serve.zone/witness",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.2.0",
4
+ "private": false,
5
+ "description": "the serve.zone witness: a compare-and-set register that decides which site's controller is active",
6
+ "main": "dist_ts/index.js",
7
+ "typings": "dist_ts/index.d.ts",
8
+ "type": "module",
9
+ "author": "Task Venture Capital GmbH",
10
+ "license": "MIT",
11
+ "bin": {
12
+ "witness": "cli.js"
13
+ },
14
+ "devDependencies": {
15
+ "@git.zone/tsrepo": "^2.1.0",
16
+ "@types/node": "26.6.6"
17
+ },
18
+ "dependencies": {
19
+ "@api.global/typedrequest": "^9.2.2",
20
+ "@api.global/typedserver": "^16.0.2",
21
+ "@lossless.org/client": "^2.10.0",
22
+ "@lossless.org/nosqldb": "^11.0.0",
23
+ "@serve.zone/interfaces": "^32.124.0"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://code.foss.global/serve.zone/witness.git"
28
+ },
29
+ "bugs": {
30
+ "url": "https://code.foss.global/serve.zone/witness/issues"
31
+ },
32
+ "homepage": "https://code.foss.global/serve.zone/witness#readme",
33
+ "files": [
34
+ "ts/**/*",
35
+ "dist_ts/**/*",
36
+ "cli.js",
37
+ ".smartconfig.json",
38
+ "readme.md",
39
+ "changelog.md",
40
+ "license.md"
41
+ ],
42
+ "scripts": {
43
+ "test": "(tsrepo test test/ --verbose --logfile --timeout 120)",
44
+ "build": "(tsrepo build tsfolders)",
45
+ "build:binary": "(tsrepo bundle && tsrepo sea build)",
46
+ "start": "(node ./cli.js serve)"
47
+ }
6
48
  }
package/readme.md ADDED
@@ -0,0 +1,96 @@
1
+ # @serve.zone/witness
2
+
3
+ `@serve.zone/witness` is the serve.zone witness (AGL-1195): a small service at a third site that keeps one compare-and-set record per controller. The record decides which controller site is active in which epoch and whether the active has a standby in sync with it. Cloudly is its only caller and speaks the witness contract of `@serve.zone/interfaces` (`requests.witness`, `data.IWitnessRecord`) with TypedRequest over HTTPS.
4
+
5
+ ## Issue Reporting and Security
6
+
7
+ For reporting bugs, issues, or security vulnerabilities, please visit [community.foss.global/](https://community.foss.global/). This is the central community hub for all issue reporting. Developers who sign and comply with our contribution agreement and go through identification can also get a [code.foss.global/](https://code.foss.global/) account to submit Pull Requests directly.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ pnpm add @serve.zone/witness
13
+ ```
14
+
15
+ The package installs the `witness` command. It runs on Node.js.
16
+
17
+ ## Running the witness
18
+
19
+ `witness serve` starts the HTTPS endpoint and the embedded storage engine. It stops on `SIGINT` or `SIGTERM` (exit 0) and when the storage engine exits on its own (exit 1), so a supervisor restarts it.
20
+
21
+ | Variable | Used by | Meaning |
22
+ | --- | --- | --- |
23
+ | `WITNESS_DATA_DIR` | every command | Normalized absolute directory of the witness state |
24
+ | `WITNESS_TLS_CERT_PATH` | `serve` | PEM certificate chain of the witness's `https` origin |
25
+ | `WITNESS_TLS_KEY_PATH` | `serve` | PEM private key of that certificate |
26
+ | `WITNESS_LISTEN_HOST` | `serve` | Interface address to bind; default `0.0.0.0` |
27
+ | `WITNESS_PORT` | `serve` | TCP port; default `443` |
28
+
29
+ Below `WITNESS_DATA_DIR` the witness keeps `nosqldb/`, the NoSQLDB storage root, and `run/nosqldb.sock`, the engine's socket in a directory of mode 0700, so only the witness's own user reaches the engine. The witness reads its certificate at start; a renewed certificate takes effect at the next start.
30
+
31
+ ### Pairing codes
32
+
33
+ A controller pairs with the witness by redeeming a one-time code the witness operator makes on the witness host for exactly one controller id:
34
+
35
+ ```bash
36
+ witness pairing-code --controller <controllerId> [--lifetime-minutes <1-60>]
37
+ ```
38
+
39
+ The command connects to the engine of the running `witness serve`, stores only the SHA-256 of a new code, and prints the code once as a JSON line on stdout: `{"controllerId":…,"pairingCode":…,"expiresAt":…}`. `@lossless.org/client` 2.10.0 also prints its own `Connected to database` and `disconnected from database` lines on stdout around it; the answer is the line that is JSON. A code lives 10 minutes unless `--lifetime-minutes` says otherwise, pairs once, and pairs only its controller id. Pass the code to the controller's operator over a channel you trust; whoever holds it can pair that controller id until it expires.
40
+
41
+ ## The contract it serves
42
+
43
+ The witness answers the five `requests.witness` requests at `data.witnessContract.typedRequestPath` (`/typedrequest`) and nothing else: no static content, health route, sitemap, feed or WebSocket. The interfaces readme ("Witness") is the reference for the requests, the record and the refusals; this package never restates the contract, it imports it.
44
+
45
+ - `pairWitnessController` consumes the code and creates the record in one transaction, so a controller whose answer was lost reads the record with its own bearer: the record is there exactly when the code was consumed.
46
+ - `enrollWitnessMember` and `compareAndSetWitnessRecord` are compare-and-sets on `{controllerId, revision}` (`atomicFindOneAndUpdate`), each raising the revision by one. Of two changes naming one revision, at most one is stored; the other is refused `revision-stale`.
47
+ - `getWitnessRecord` answers the record to a member.
48
+ - `getWitnessHealth` answers `{ ready, witnessVersion, interfacesVersion }` without authentication and names no controller. `ready` is true while the embedded storage engine runs and answers a bounded read; it does not probe a write. A write the storage cannot make is answered on that write, as `witness-unavailable` or `outcome-unknown`.
49
+
50
+ Every request passes its gates in the contract's order: the contract's validator (`request-invalid`), then the pairing code or the authentication, then `revision-stale`, then the first refusal of `data.judgeWitnessChange` or `data.judgeWitnessMemberEnrollment`. A refusal is a `TypedResponseError` whose `errorData` is `data.createWitnessRefusalErrorData(code)`.
51
+
52
+ ### Credentials
53
+
54
+ The witness never holds a bearer. Each site creates its own and hands over only `bearerSha256`; the witness compares the SHA-256 of a presented bearer with the stored one in constant time. An unknown controller, a non-member site and a bad bearer all answer the same `credential-invalid` with the same text; the witness log records which case it was. No answer, refusal or log line carries a bearer or a pairing code.
55
+
56
+ ### Durability
57
+
58
+ Storage is a `LocalNoSqlDb` from `@lossless.org/nosqldb`, embedded in the witness process, with the models of `@lossless.org/client/nosqldb`. NoSQLDB acknowledges a write on file storage only after its write-ahead log is fsynced, and the witness answers a change only after that acknowledgement. NoSQLDB's own conditions apply: XFS, or ext4 with `data=ordered`, and a device that does not lose acknowledged bytes (see the NoSQLDB readme, "Storage Engine Reliability"). A change whose storage call fails is refused `outcome-unknown`: it may have been stored, and the caller reads the record before it acts.
59
+
60
+ ## Programmatic use
61
+
62
+ ```typescript
63
+ import { Witness, readWitnessServeConfig } from '@serve.zone/witness';
64
+
65
+ const witness = new Witness(readWitnessServeConfig(process.env));
66
+ await witness.start();
67
+ const { pairingCode } = await witness.issuePairingCode('controller-0123456789abcdef');
68
+ await witness.stop();
69
+ ```
70
+
71
+ `Witness` options extend the serve configuration with `logSink` (structured log entries; default one JSON line each on stdout and stderr), `clock` (the witness clock pairing codes expire against) and `onStorageFailure`. `runCli(argv, io)` runs the CLI and resolves with its exit code.
72
+
73
+ ## Not yet
74
+
75
+ Forced promotion and member removal come later as additive contract requests (Linus ruling 73 Q2, Q9). A single executable build via `tsrepo sea` is not set up yet.
76
+
77
+ ## License and Legal Information
78
+
79
+ This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the [repository license file](./license.md).
80
+
81
+ **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
82
+
83
+ ### Trademarks
84
+
85
+ This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
86
+
87
+ Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
88
+
89
+ ### Company Information
90
+
91
+ Task Venture Capital GmbH<br>
92
+ Registered at District Court Bremen HRB 35230 HB, Germany
93
+
94
+ For any legal inquiries or further information, please contact us via email at hello@task.vc.
95
+
96
+ By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * autocreated commitinfo by @push.rocks/commitinfo
3
+ */
4
+ export const commitinfo = {
5
+ name: '@serve.zone/witness',
6
+ version: '1.2.0',
7
+ description: 'the serve.zone witness: a compare-and-set register that decides which site\'s controller is active'
8
+ }
@@ -0,0 +1,100 @@
1
+ import * as plugins from './plugins.js';
2
+
3
+ /**
4
+ * How a member authenticates: the SHA-256 of the bearer that site created for itself, as 64
5
+ * lowercase hex characters. The witness never holds the bearer.
6
+ */
7
+ export interface IWitnessMemberHash {
8
+ /** The member's site origin. */
9
+ origin: string;
10
+ bearerSha256: string;
11
+ }
12
+
13
+ /**
14
+ * The stored witness record of one controller: the contract's `IWitnessRecord` without
15
+ * `memberOrigins`, which is derived from `members`, so the members and their hashes cannot disagree.
16
+ */
17
+ export interface IWitnessRecordDocument {
18
+ controllerId: string;
19
+ epoch: number;
20
+ activeOrigin: string;
21
+ inSyncStandbyOrigin: string | null;
22
+ revision: number;
23
+ /** Every member, ordered by origin as `IWitnessRecord.memberOrigins` is. */
24
+ members: IWitnessMemberHash[];
25
+ }
26
+
27
+ /** A one-time pairing code, stored as its hash only. */
28
+ export interface IWitnessPairingCodeDocument {
29
+ /** SHA-256 of the code's UTF-8, 64 lowercase hex characters. */
30
+ codeSha256: string;
31
+ /** The one controller id the code pairs. */
32
+ controllerId: string;
33
+ /** Witness clock, milliseconds since the epoch. */
34
+ issuedAt: number;
35
+ /** The code is refused `pairing-code-expired` from this witness-clock time on. */
36
+ expiresAt: number;
37
+ /** When the code was redeemed, or `null` while it is unused. */
38
+ consumedAt: number | null;
39
+ }
40
+
41
+ @plugins.smartdata.managed({ collectionName: 'witnessrecords' })
42
+ export class WitnessRecordDoc
43
+ extends plugins.smartdata.SmartDataDbDoc<WitnessRecordDoc, IWitnessRecordDocument>
44
+ implements IWitnessRecordDocument {
45
+ @plugins.smartdata.unI()
46
+ public controllerId!: string;
47
+
48
+ @plugins.smartdata.svDb()
49
+ public epoch!: number;
50
+
51
+ @plugins.smartdata.svDb()
52
+ public activeOrigin!: string;
53
+
54
+ @plugins.smartdata.svDb()
55
+ public inSyncStandbyOrigin!: string | null;
56
+
57
+ @plugins.smartdata.svDb()
58
+ public revision!: number;
59
+
60
+ @plugins.smartdata.svDb()
61
+ public members!: IWitnessMemberHash[];
62
+ }
63
+
64
+ @plugins.smartdata.managed({ collectionName: 'witnesspairingcodes' })
65
+ export class WitnessPairingCodeDoc
66
+ extends plugins.smartdata.SmartDataDbDoc<WitnessPairingCodeDoc, IWitnessPairingCodeDocument>
67
+ implements IWitnessPairingCodeDocument {
68
+ @plugins.smartdata.unI()
69
+ public codeSha256!: string;
70
+
71
+ @plugins.smartdata.svDb()
72
+ public controllerId!: string;
73
+
74
+ @plugins.smartdata.svDb()
75
+ public issuedAt!: number;
76
+
77
+ @plugins.smartdata.svDb()
78
+ public expiresAt!: number;
79
+
80
+ @plugins.smartdata.svDb()
81
+ public consumedAt!: number | null;
82
+ }
83
+
84
+ /** The witness models, bound to one database. */
85
+ export interface IWitnessModels {
86
+ Record: typeof WitnessRecordDoc;
87
+ PairingCode: typeof WitnessPairingCodeDoc;
88
+ }
89
+
90
+ /** Binds the models to `dbArg`, so several witnesses (a restart in a test) use their own database. */
91
+ export const bindWitnessModels = (dbArg: plugins.smartdata.SmartdataDb): IWitnessModels => {
92
+ const manager: plugins.smartdata.IManager = { db: dbArg };
93
+ return {
94
+ Record: plugins.smartdata.bindDocToManager(manager, WitnessRecordDoc),
95
+ PairingCode: plugins.smartdata.bindDocToManager(manager, WitnessPairingCodeDoc),
96
+ };
97
+ };
98
+
99
+ /** Every witness model, for preparation at start. */
100
+ export const listWitnessModels = (modelsArg: IWitnessModels) => [modelsArg.Record, modelsArg.PairingCode] as const;
@@ -0,0 +1,28 @@
1
+ import * as plugins from './plugins.js';
2
+ import type { TWitnessLogValue } from './witness.log.js';
3
+
4
+ type TWitnessRefusal = plugins.servezoneInterfaces.data.TWitnessRefusal;
5
+
6
+ /**
7
+ * A witness refusal on its way to the caller. The message is the wire text: the code and a detail
8
+ * that never carries a bearer or a pairing code. `logFields` stay in the witness log; they name what
9
+ * the caller must not learn, such as which case of `credential-invalid` it was (ruling 73 Q8).
10
+ */
11
+ export class WitnessRefusalError extends Error {
12
+ constructor(
13
+ public readonly code: TWitnessRefusal,
14
+ detailArg: string,
15
+ public readonly logFields: Readonly<Record<string, TWitnessLogValue>> = {},
16
+ ) {
17
+ super(`${code}: ${detailArg}`);
18
+ this.name = 'WitnessRefusalError';
19
+ }
20
+
21
+ /** The refusal as the contract answers it: a `TypedResponseError` with `IWitnessRefusalErrorData`. */
22
+ public toTypedResponseError(): plugins.typedrequest.TypedResponseError {
23
+ return new plugins.typedrequest.TypedResponseError(
24
+ this.message,
25
+ plugins.servezoneInterfaces.data.createWitnessRefusalErrorData(this.code),
26
+ );
27
+ }
28
+ }