@mistriwork/mistri 1.0.0-rc.82 → 1.0.0-rc.83
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 +18 -0
- package/README.md +12 -3
- package/dist/adapters/integrations/integrationStore.js +17 -7
- package/dist/docs/build-and-deploy.md +1 -0
- package/dist/docs/check-codes.md +1 -1
- package/dist/docs/cli.md +7 -4
- package/dist/docs/status.md +3 -3
- package/dist/embed/react/headless.js +1 -1
- package/dist/embed/react/index.js +1 -1
- package/dist/server/app/appPreferences.js +8 -3
- package/dist/server/auth/authStoreReads.js +28 -2
- package/dist/server/auth/signInState.d.ts +8 -3
- package/dist/server/auth/signInState.js +39 -12
- package/dist/server/build/check.js +17 -3
- package/dist/server/build/checkDeploy.js +19 -12
- package/dist/server/cli/apps.js +8 -1
- package/dist/server/cli/devSupervisor.d.ts +2 -1
- package/dist/server/cli/devSupervisor.js +12 -1
- package/dist/server/cli/mistriMain.js +126 -2
- package/dist/server/cli/primaryCounters.d.ts +30 -0
- package/dist/server/cli/primaryCounters.js +83 -0
- package/dist/server/cli/usage.js +13 -4
- package/dist/server/cli/workers.d.ts +70 -0
- package/dist/server/cli/workers.js +585 -0
- package/dist/server/forms/formKey.js +3 -3
- package/dist/server/forms/publicTicket.js +2 -2
- package/dist/server/processBus.d.ts +54 -0
- package/dist/server/processBus.js +147 -0
- package/dist/server/processKey.d.ts +2 -0
- package/dist/server/processKey.js +26 -0
- package/package.json +4 -4
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The process bus: what one process tells the others serving the same start, such as "forget this cached thing".
|
|
3
|
+
*
|
|
4
|
+
* A start with `--workers` (cli/workers.ts) is several processes behind one port, each with its own memory, so a cache
|
|
5
|
+
* one of them drops on a change the others would go on serving until it expires. `publish` runs this process's own
|
|
6
|
+
* subscribers at once and, in a worker, sends the message to the primary, which hands it to every other worker; one
|
|
7
|
+
* process alone has nothing to send, so the bus is a loopback there and costs a function call.
|
|
8
|
+
*
|
|
9
|
+
* Messages are hints to forget, never the state itself: a worker that missed one (it was starting, or restarting) has
|
|
10
|
+
* an empty cache anyway, and replicas, which share no bus, keep the time-to-live each cache already has as their bound.
|
|
11
|
+
* A payload is whatever the structured clone of an IPC message carries; keep it small (a key, a hash).
|
|
12
|
+
*
|
|
13
|
+
* One exchange waits for an answer: `ask`, a worker's question to its primary (the counters a start's workers share,
|
|
14
|
+
* cli/primaryCounters.ts), which the primary answers with `answer`. A question carries a deadline, because a worker
|
|
15
|
+
* must not hang a request on a primary that is stuck; what a late answer means is the asker's to decide.
|
|
16
|
+
*
|
|
17
|
+
* Topics in use:
|
|
18
|
+
* - `cache.evict` — a key of the Registry's in-process cache store (engine connections.ts `cache()`).
|
|
19
|
+
* - `preferences.evict` — an account's recent-picks entry (app/appPreferences.ts).
|
|
20
|
+
* - `session.evict` — a short digest of an auth store's URL (never the URL, which carries credentials) and the
|
|
21
|
+
* publishing pid, for the in-process session cache (auth/authStoreReads.ts): a change to the store in one worker
|
|
22
|
+
* moves that store's epoch on in the others, so none answers from what it kept before a sign-out or revocation.
|
|
23
|
+
*/
|
|
24
|
+
import { type Worker } from 'node:cluster';
|
|
25
|
+
type Listener = (payload: unknown) => void;
|
|
26
|
+
/** The IPC message a worker sends the primary and the primary relays: one shape, so a stray message is never read as one. */
|
|
27
|
+
type BusMessage = {
|
|
28
|
+
mistriBus: {
|
|
29
|
+
topic: string;
|
|
30
|
+
payload: unknown;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
/** A question the primary did not answer in time, or could not be sent: the asker decides what that means. */
|
|
34
|
+
export declare class NoAnswer extends Error {
|
|
35
|
+
}
|
|
36
|
+
export declare function busMessage(message: unknown): BusMessage['mistriBus'] | undefined;
|
|
37
|
+
/** Tells this process's subscribers to `topic`, then every other worker's. */
|
|
38
|
+
export declare function publish(topic: string, payload: unknown): void;
|
|
39
|
+
/** Runs `listener` for every `topic` message, this process's own included. Returns the unsubscribe. */
|
|
40
|
+
export declare function subscribe(topic: string, listener: Listener): () => void;
|
|
41
|
+
/** In the primary: relays one worker's bus message to every other live worker. False when `message` is not one. */
|
|
42
|
+
export declare function relay(message: unknown, from: Worker, workers: Iterable<Worker | undefined>): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* In a worker: asks the primary `op` with `args` and resolves with its answer. Rejects with NoAnswer when this is not
|
|
45
|
+
* a worker, the question cannot be sent, or no answer comes within `timeoutMs`; with the primary's error when it
|
|
46
|
+
* answered with one.
|
|
47
|
+
*/
|
|
48
|
+
export declare function ask(op: string, args: unknown[], timeoutMs: number): Promise<unknown>;
|
|
49
|
+
/**
|
|
50
|
+
* In the primary: answers one worker's question with `handler`. False when `message` is not a question. A handler's
|
|
51
|
+
* throw is answered as an error, so the worker's request fails rather than waits for its deadline.
|
|
52
|
+
*/
|
|
53
|
+
export declare function answer(message: unknown, from: Worker, handler: (op: string, args: unknown[]) => unknown): boolean;
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The process bus: what one process tells the others serving the same start, such as "forget this cached thing".
|
|
3
|
+
*
|
|
4
|
+
* A start with `--workers` (cli/workers.ts) is several processes behind one port, each with its own memory, so a cache
|
|
5
|
+
* one of them drops on a change the others would go on serving until it expires. `publish` runs this process's own
|
|
6
|
+
* subscribers at once and, in a worker, sends the message to the primary, which hands it to every other worker; one
|
|
7
|
+
* process alone has nothing to send, so the bus is a loopback there and costs a function call.
|
|
8
|
+
*
|
|
9
|
+
* Messages are hints to forget, never the state itself: a worker that missed one (it was starting, or restarting) has
|
|
10
|
+
* an empty cache anyway, and replicas, which share no bus, keep the time-to-live each cache already has as their bound.
|
|
11
|
+
* A payload is whatever the structured clone of an IPC message carries; keep it small (a key, a hash).
|
|
12
|
+
*
|
|
13
|
+
* One exchange waits for an answer: `ask`, a worker's question to its primary (the counters a start's workers share,
|
|
14
|
+
* cli/primaryCounters.ts), which the primary answers with `answer`. A question carries a deadline, because a worker
|
|
15
|
+
* must not hang a request on a primary that is stuck; what a late answer means is the asker's to decide.
|
|
16
|
+
*
|
|
17
|
+
* Topics in use:
|
|
18
|
+
* - `cache.evict` — a key of the Registry's in-process cache store (engine connections.ts `cache()`).
|
|
19
|
+
* - `preferences.evict` — an account's recent-picks entry (app/appPreferences.ts).
|
|
20
|
+
* - `session.evict` — a short digest of an auth store's URL (never the URL, which carries credentials) and the
|
|
21
|
+
* publishing pid, for the in-process session cache (auth/authStoreReads.ts): a change to the store in one worker
|
|
22
|
+
* moves that store's epoch on in the others, so none answers from what it kept before a sign-out or revocation.
|
|
23
|
+
*/
|
|
24
|
+
import cluster, {} from 'node:cluster';
|
|
25
|
+
const listeners = new Map();
|
|
26
|
+
let wired = false;
|
|
27
|
+
let asked = 0;
|
|
28
|
+
const waiting = new Map();
|
|
29
|
+
/** A question the primary did not answer in time, or could not be sent: the asker decides what that means. */
|
|
30
|
+
export class NoAnswer extends Error {
|
|
31
|
+
}
|
|
32
|
+
export function busMessage(message) {
|
|
33
|
+
if (typeof message !== 'object' || message === null || !('mistriBus' in message))
|
|
34
|
+
return undefined;
|
|
35
|
+
const said = message.mistriBus;
|
|
36
|
+
return typeof said === 'object' && said !== null && typeof said.topic === 'string' ? said : undefined;
|
|
37
|
+
}
|
|
38
|
+
function deliver(topic, payload) {
|
|
39
|
+
for (const listener of listeners.get(topic) ?? []) {
|
|
40
|
+
// One subscriber's mistake does not keep the message from the rest, nor reach the publisher.
|
|
41
|
+
try {
|
|
42
|
+
listener(payload);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
console.error(`mistri: a ${topic} subscriber failed: ${error.message}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/* In a worker, what the primary relays from the others. Wired at the first subscribe, and the channel left unref'd as
|
|
50
|
+
the worker found it: a listener must not keep alive a worker that is otherwise done. */
|
|
51
|
+
function wire() {
|
|
52
|
+
if (wired || !cluster.isWorker)
|
|
53
|
+
return;
|
|
54
|
+
wired = true;
|
|
55
|
+
process.on('message', (message) => {
|
|
56
|
+
const said = busMessage(message);
|
|
57
|
+
if (said)
|
|
58
|
+
return deliver(said.topic, said.payload);
|
|
59
|
+
const answer = answerMessage(message);
|
|
60
|
+
if (answer)
|
|
61
|
+
waiting.get(answer.id)?.settle(answer);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function answerMessage(message) {
|
|
65
|
+
if (typeof message !== 'object' || message === null || !('mistriAnswer' in message))
|
|
66
|
+
return undefined;
|
|
67
|
+
const said = message.mistriAnswer;
|
|
68
|
+
return typeof said === 'object' && said !== null && typeof said.id === 'number' ? said : undefined;
|
|
69
|
+
}
|
|
70
|
+
/** Tells this process's subscribers to `topic`, then every other worker's. */
|
|
71
|
+
export function publish(topic, payload) {
|
|
72
|
+
deliver(topic, payload);
|
|
73
|
+
if (cluster.isWorker && process.connected)
|
|
74
|
+
process.send?.({ mistriBus: { topic, payload } }, undefined, undefined, () => { });
|
|
75
|
+
}
|
|
76
|
+
/** Runs `listener` for every `topic` message, this process's own included. Returns the unsubscribe. */
|
|
77
|
+
export function subscribe(topic, listener) {
|
|
78
|
+
wire();
|
|
79
|
+
let set = listeners.get(topic);
|
|
80
|
+
if (!set)
|
|
81
|
+
listeners.set(topic, (set = new Set()));
|
|
82
|
+
set.add(listener);
|
|
83
|
+
return () => {
|
|
84
|
+
set.delete(listener);
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/** In the primary: relays one worker's bus message to every other live worker. False when `message` is not one. */
|
|
88
|
+
export function relay(message, from, workers) {
|
|
89
|
+
const said = busMessage(message);
|
|
90
|
+
if (!said)
|
|
91
|
+
return false;
|
|
92
|
+
for (const worker of workers)
|
|
93
|
+
if (worker && worker.id !== from.id && worker.isConnected())
|
|
94
|
+
worker.send({ mistriBus: said }, undefined, () => { });
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* In a worker: asks the primary `op` with `args` and resolves with its answer. Rejects with NoAnswer when this is not
|
|
99
|
+
* a worker, the question cannot be sent, or no answer comes within `timeoutMs`; with the primary's error when it
|
|
100
|
+
* answered with one.
|
|
101
|
+
*/
|
|
102
|
+
export function ask(op, args, timeoutMs) {
|
|
103
|
+
if (!cluster.isWorker || !process.connected || !process.send)
|
|
104
|
+
return Promise.reject(new NoAnswer('no primary to ask.'));
|
|
105
|
+
wire();
|
|
106
|
+
const id = ++asked;
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
const settle = (answer) => {
|
|
109
|
+
if (!waiting.delete(id))
|
|
110
|
+
return;
|
|
111
|
+
clearTimeout(timer);
|
|
112
|
+
if (answer instanceof NoAnswer)
|
|
113
|
+
reject(answer);
|
|
114
|
+
else if (answer.error !== undefined)
|
|
115
|
+
reject(new Error(answer.error));
|
|
116
|
+
else
|
|
117
|
+
resolve(answer.value);
|
|
118
|
+
};
|
|
119
|
+
// Unref'd: a question in flight belongs to a request, which keeps the worker alive itself.
|
|
120
|
+
const timer = setTimeout(() => settle(new NoAnswer(`the primary did not answer ${op} within ${timeoutMs} ms.`)), timeoutMs);
|
|
121
|
+
timer.unref();
|
|
122
|
+
waiting.set(id, { settle });
|
|
123
|
+
process.send({ mistriAsk: { id, op, args } }, undefined, undefined, (error) => {
|
|
124
|
+
if (error)
|
|
125
|
+
settle(new NoAnswer(`the primary could not be asked ${op}: ${error.message}`));
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* In the primary: answers one worker's question with `handler`. False when `message` is not a question. A handler's
|
|
131
|
+
* throw is answered as an error, so the worker's request fails rather than waits for its deadline.
|
|
132
|
+
*/
|
|
133
|
+
export function answer(message, from, handler) {
|
|
134
|
+
if (typeof message !== 'object' || message === null || !('mistriAsk' in message))
|
|
135
|
+
return false;
|
|
136
|
+
const said = message.mistriAsk;
|
|
137
|
+
if (typeof said !== 'object' || said === null || typeof said.id !== 'number' || typeof said.op !== 'string')
|
|
138
|
+
return false;
|
|
139
|
+
const reply = (body) => {
|
|
140
|
+
if (from.isConnected())
|
|
141
|
+
from.send({ mistriAnswer: body }, undefined, () => { });
|
|
142
|
+
};
|
|
143
|
+
Promise.resolve()
|
|
144
|
+
.then(() => handler(said.op, Array.isArray(said.args) ? said.args : []))
|
|
145
|
+
.then((value) => reply({ id: said.id, value }), (error) => reply({ id: said.id, error: error instanceof Error ? error.message : String(error) }));
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keys this server makes for itself when the deployment names none: the public door's ticket key (forms/publicTicket.ts)
|
|
3
|
+
* and the seal for what it writes and later reads back (forms/formKey.ts).
|
|
4
|
+
*
|
|
5
|
+
* One process makes them at random at start, so a restart forgets them, which those files say is the cost of not naming
|
|
6
|
+
* MISTRI_FORM_KEY. A start with `--workers` (cli/workers.ts) is several processes behind one port, and a ticket signed by
|
|
7
|
+
* one worker is checked by whichever the next request reaches: the primary makes one random secret for the start and
|
|
8
|
+
* hands it to each worker in its environment, and every key here is derived from it, so the workers agree with each
|
|
9
|
+
* other as one process agrees with itself. Read once and taken out of the environment, where App code could see it.
|
|
10
|
+
*/
|
|
11
|
+
import { hkdfSync, randomBytes } from 'node:crypto';
|
|
12
|
+
/** The variable the primary hands its secret down in; never set by a person. */
|
|
13
|
+
const WORKER_SECRET_ENV = 'MISTRI_WORKER_SECRET';
|
|
14
|
+
const handed = process.env[WORKER_SECRET_ENV];
|
|
15
|
+
delete process.env[WORKER_SECRET_ENV];
|
|
16
|
+
const secret = handed && handed.length >= 32 ? Buffer.from(handed, 'utf8') : undefined;
|
|
17
|
+
const made = new Map();
|
|
18
|
+
/** This start's own 32-byte key for `purpose`: the same in every worker of one `--workers` start, new at each start. */
|
|
19
|
+
export function processKey(purpose) {
|
|
20
|
+
let key = made.get(purpose);
|
|
21
|
+
if (!key) {
|
|
22
|
+
key = secret ? Buffer.from(hkdfSync('sha256', secret, 'mistri.process-key', purpose, 32)) : randomBytes(32);
|
|
23
|
+
made.set(purpose, key);
|
|
24
|
+
}
|
|
25
|
+
return key;
|
|
26
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mistriwork/mistri",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.83",
|
|
4
4
|
"description": "The App framework: a few lines of TypeScript, a complete App, served from your own database.",
|
|
5
5
|
"homepage": "https://mistriwork.dev",
|
|
6
6
|
"bugs": {
|
|
@@ -114,9 +114,9 @@
|
|
|
114
114
|
"./package.json": "./package.json"
|
|
115
115
|
},
|
|
116
116
|
"dependencies": {
|
|
117
|
-
"@mistriwork/engine": "1.0.0-rc.
|
|
118
|
-
"@mistriwork/kit": "1.0.0-rc.
|
|
119
|
-
"@mistriwork/ui": "1.0.0-rc.
|
|
117
|
+
"@mistriwork/engine": "1.0.0-rc.83",
|
|
118
|
+
"@mistriwork/kit": "1.0.0-rc.83",
|
|
119
|
+
"@mistriwork/ui": "1.0.0-rc.83",
|
|
120
120
|
"intl-messageformat": "12.1.2",
|
|
121
121
|
"marked": "^18.1.0",
|
|
122
122
|
"sanitize-html": "^2.18.0",
|