@nervur-org/dock 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/GETTING_STARTED.md +128 -0
- package/LICENSE +202 -0
- package/NOTICE +6 -0
- package/README.md +37 -0
- package/api/quo-api.md +196 -0
- package/api/route.ts +227 -0
- package/beings/GLOSSARY.md +118 -0
- package/beings/WORLDS.md +567 -0
- package/beings/avatar.ts +101 -0
- package/beings/carry.ts +159 -0
- package/beings/clock.ts +552 -0
- package/beings/courier.ts +107 -0
- package/beings/desk.ts +128 -0
- package/beings/doorbell.ts +129 -0
- package/beings/envoy.ts +218 -0
- package/beings/index.ts +16 -0
- package/beings/join.ts +51 -0
- package/beings/link.ts +104 -0
- package/beings/look.ts +139 -0
- package/beings/porter.ts +43 -0
- package/beings/presence.ts +39 -0
- package/beings/quo-app-bells.md +280 -0
- package/beings/quo-dock.md +1942 -0
- package/beings/setup.ts +45 -0
- package/beings/side.ts +60 -0
- package/beings/user.ts +459 -0
- package/beings/way.ts +65 -0
- package/cli/bundles.ts +20 -0
- package/cli/client.ts +73 -0
- package/cli/daemon.ts +581 -0
- package/cli/estate/Caddyfile +33 -0
- package/cli/estate/quo.service +35 -0
- package/cli/estate/systems.quo.plist +44 -0
- package/cli/estate.ts +48 -0
- package/cli/folder.ts +27 -0
- package/cli/oauth.ts +32 -0
- package/cli/pilot.ts +38 -0
- package/cli/quo-dock.md +1942 -0
- package/cli/quo.ts +203 -0
- package/cli/stand.ts +160 -0
- package/dist/api/route.d.ts +21 -0
- package/dist/api/route.js +193 -0
- package/dist/beings/avatar.d.ts +32 -0
- package/dist/beings/avatar.js +100 -0
- package/dist/beings/carry.d.ts +12 -0
- package/dist/beings/carry.js +174 -0
- package/dist/beings/clock.d.ts +161 -0
- package/dist/beings/clock.js +528 -0
- package/dist/beings/courier.d.ts +50 -0
- package/dist/beings/courier.js +96 -0
- package/dist/beings/desk.d.ts +53 -0
- package/dist/beings/desk.js +135 -0
- package/dist/beings/doorbell.d.ts +78 -0
- package/dist/beings/doorbell.js +133 -0
- package/dist/beings/envoy.d.ts +54 -0
- package/dist/beings/envoy.js +211 -0
- package/dist/beings/index.d.ts +14 -0
- package/dist/beings/index.js +16 -0
- package/dist/beings/join.d.ts +15 -0
- package/dist/beings/join.js +42 -0
- package/dist/beings/link.d.ts +14 -0
- package/dist/beings/link.js +91 -0
- package/dist/beings/look.d.ts +30 -0
- package/dist/beings/look.js +104 -0
- package/dist/beings/porter.d.ts +14 -0
- package/dist/beings/porter.js +41 -0
- package/dist/beings/presence.d.ts +8 -0
- package/dist/beings/presence.js +21 -0
- package/dist/beings/setup.d.ts +3 -0
- package/dist/beings/setup.js +39 -0
- package/dist/beings/side.d.ts +32 -0
- package/dist/beings/side.js +37 -0
- package/dist/beings/user.d.ts +315 -0
- package/dist/beings/user.js +462 -0
- package/dist/beings/way.d.ts +19 -0
- package/dist/beings/way.js +28 -0
- package/dist/cli/bundles.d.ts +5 -0
- package/dist/cli/bundles.js +17 -0
- package/dist/cli/client.d.ts +15 -0
- package/dist/cli/client.js +58 -0
- package/dist/cli/daemon.d.ts +50 -0
- package/dist/cli/daemon.js +584 -0
- package/dist/cli/estate/Caddyfile +33 -0
- package/dist/cli/estate/quo.service +35 -0
- package/dist/cli/estate/systems.quo.plist +44 -0
- package/dist/cli/estate.d.ts +5 -0
- package/dist/cli/estate.js +50 -0
- package/dist/cli/folder.d.ts +2 -0
- package/dist/cli/folder.js +25 -0
- package/dist/cli/oauth.d.ts +2 -0
- package/dist/cli/oauth.js +32 -0
- package/dist/cli/pilot.d.ts +5 -0
- package/dist/cli/pilot.js +22 -0
- package/dist/cli/quo.d.ts +2 -0
- package/dist/cli/quo.js +201 -0
- package/dist/cli/stand.d.ts +37 -0
- package/dist/cli/stand.js +110 -0
- package/dist/dock/agents.d.ts +102 -0
- package/dist/dock/agents.js +105 -0
- package/dist/dock/apns.d.ts +27 -0
- package/dist/dock/apns.js +98 -0
- package/dist/dock/console.d.ts +6 -0
- package/dist/dock/console.js +20 -0
- package/dist/dock/dialers.d.ts +56 -0
- package/dist/dock/dialers.js +75 -0
- package/dist/dock/faculty.d.ts +24 -0
- package/dist/dock/faculty.js +142 -0
- package/dist/dock/fcm.d.ts +13 -0
- package/dist/dock/fcm.js +77 -0
- package/dist/dock/grants.d.ts +41 -0
- package/dist/dock/grants.js +99 -0
- package/dist/dock/http2.d.ts +4 -0
- package/dist/dock/http2.js +68 -0
- package/dist/dock/index.d.ts +41 -0
- package/dist/dock/index.js +113 -0
- package/dist/dock/local.d.ts +13 -0
- package/dist/dock/local.js +44 -0
- package/dist/dock/location.d.ts +19 -0
- package/dist/dock/location.js +11 -0
- package/dist/dock/notifications.d.ts +38 -0
- package/dist/dock/notifications.js +60 -0
- package/dist/dock/push.d.ts +79 -0
- package/dist/dock/push.js +132 -0
- package/dist/dock/service.d.ts +4 -0
- package/dist/dock/service.js +107 -0
- package/dist/dock/timer.d.ts +52 -0
- package/dist/dock/timer.js +149 -0
- package/dist/dock/webpush.d.ts +12 -0
- package/dist/dock/webpush.js +144 -0
- package/dist/harbor/browser.d.ts +15 -0
- package/dist/harbor/browser.js +82 -0
- package/dist/harbor/capacitor.d.ts +28 -0
- package/dist/harbor/capacitor.js +218 -0
- package/dist/harbor/disk.d.ts +27 -0
- package/dist/harbor/disk.js +249 -0
- package/dist/harbor/edge/alarm.d.ts +10 -0
- package/dist/harbor/edge/alarm.js +38 -0
- package/dist/harbor/edge/edge.d.ts +54 -0
- package/dist/harbor/edge/edge.js +370 -0
- package/dist/harbor/edge/exercise.d.ts +7 -0
- package/dist/harbor/edge/exercise.js +232 -0
- package/dist/harbor/edge/given.d.ts +26 -0
- package/dist/harbor/edge/given.js +42 -0
- package/dist/harbor/edge/platform.d.ts +39 -0
- package/dist/harbor/edge/storage.d.ts +30 -0
- package/dist/harbor/edge/storage.js +180 -0
- package/dist/harbor/edge/worker.d.ts +17 -0
- package/dist/harbor/edge/worker.js +67 -0
- package/dist/harbor/files.d.ts +15 -0
- package/dist/harbor/files.js +220 -0
- package/dist/harbor/http.d.ts +19 -0
- package/dist/harbor/http.js +177 -0
- package/dist/harbor/idb.d.ts +15 -0
- package/dist/harbor/idb.js +0 -0
- package/dist/harbor/quo.d.ts +10 -0
- package/dist/harbor/quo.js +42 -0
- package/dist/harbor/resolve.d.ts +16 -0
- package/dist/harbor/resolve.js +17 -0
- package/dist/harbor/seal.d.ts +3 -0
- package/dist/harbor/seal.js +25 -0
- package/dist/harbor/sealed.d.ts +35 -0
- package/dist/harbor/sealed.js +162 -0
- package/dist/harbor/tauri.d.ts +16 -0
- package/dist/harbor/tauri.js +59 -0
- package/dist/harbor/ward.d.ts +9 -0
- package/dist/harbor/ward.js +15 -0
- package/dist/human/app.d.ts +32 -0
- package/dist/human/app.js +527 -0
- package/dist/human/apps.d.ts +10 -0
- package/dist/human/apps.js +29 -0
- package/dist/human/dom.d.ts +2 -0
- package/dist/human/dom.js +15 -0
- package/dist/human/door.d.ts +19 -0
- package/dist/human/door.js +92 -0
- package/dist/human/guest.d.ts +3 -0
- package/dist/human/guest.js +33 -0
- package/dist/human/html.d.ts +55 -0
- package/dist/human/html.js +451 -0
- package/dist/human/local.d.ts +7 -0
- package/dist/human/local.js +10 -0
- package/dist/human/mark.d.ts +8 -0
- package/dist/human/mark.js +47 -0
- package/dist/human/move.d.ts +17 -0
- package/dist/human/move.js +90 -0
- package/dist/human/screen.d.ts +19 -0
- package/dist/human/screen.js +119 -0
- package/dist/human/style.d.ts +2 -0
- package/dist/human/style.js +60 -0
- package/dist/human/tab.bundle.txt +4775 -0
- package/dist/human/tab.d.ts +15 -0
- package/dist/human/tab.js +409 -0
- package/dist/human/tree.d.ts +50 -0
- package/dist/human/tree.js +157 -0
- package/dist/human/wake.d.ts +34 -0
- package/dist/human/wake.js +179 -0
- package/dist/human/web.d.ts +33 -0
- package/dist/human/web.js +184 -0
- package/dist/human/worker.bundle.txt +3868 -0
- package/dist/human/worker.d.ts +1 -0
- package/dist/human/worker.js +81 -0
- package/dist/human/worlds.d.ts +24 -0
- package/dist/human/worlds.js +99 -0
- package/dist/mcp/agent.d.ts +10 -0
- package/dist/mcp/agent.js +78 -0
- package/dist/mcp/allow.d.ts +20 -0
- package/dist/mcp/allow.js +88 -0
- package/dist/mcp/http.d.ts +27 -0
- package/dist/mcp/http.js +92 -0
- package/dist/mcp/oauth.d.ts +91 -0
- package/dist/mcp/oauth.js +241 -0
- package/dist/mcp/route.d.ts +15 -0
- package/dist/mcp/route.js +42 -0
- package/dist/mcp/runner.d.ts +45 -0
- package/dist/mcp/runner.js +99 -0
- package/dist/mcp/server.d.ts +11 -0
- package/dist/mcp/server.js +102 -0
- package/dock/agents.ts +121 -0
- package/dock/apns.ts +138 -0
- package/dock/console.ts +23 -0
- package/dock/dialers.ts +85 -0
- package/dock/faculty.ts +148 -0
- package/dock/fcm.ts +103 -0
- package/dock/grants.ts +111 -0
- package/dock/http2.ts +70 -0
- package/dock/index.ts +133 -0
- package/dock/local.ts +48 -0
- package/dock/location.ts +22 -0
- package/dock/notifications.ts +67 -0
- package/dock/push.ts +161 -0
- package/dock/quo-app-bells.md +280 -0
- package/dock/quo-dock.md +1942 -0
- package/dock/service.ts +100 -0
- package/dock/timer.ts +174 -0
- package/dock/webpush.ts +182 -0
- package/harbor/browser.ts +97 -0
- package/harbor/capacitor.ts +223 -0
- package/harbor/disk.ts +237 -0
- package/harbor/edge/alarm.ts +48 -0
- package/harbor/edge/edge.ts +407 -0
- package/harbor/edge/exercise.ts +241 -0
- package/harbor/edge/given.ts +106 -0
- package/harbor/edge/platform.d.ts +39 -0
- package/harbor/edge/storage.ts +179 -0
- package/harbor/edge/text.d.ts +15 -0
- package/harbor/edge/worker.ts +70 -0
- package/harbor/edge/wrangler.toml +30 -0
- package/harbor/files.ts +221 -0
- package/harbor/http.ts +188 -0
- package/harbor/idb.ts +0 -0
- package/harbor/quo-harbor.md +850 -0
- package/harbor/quo.ts +44 -0
- package/harbor/resolve.ts +31 -0
- package/harbor/seal.ts +26 -0
- package/harbor/sealed.ts +183 -0
- package/harbor/tauri.ts +79 -0
- package/harbor/ward.ts +28 -0
- package/human/app.ts +580 -0
- package/human/apps.ts +47 -0
- package/human/dom.ts +21 -0
- package/human/door.ts +113 -0
- package/human/guest.ts +34 -0
- package/human/html.ts +490 -0
- package/human/local.ts +30 -0
- package/human/mark.ts +53 -0
- package/human/move.ts +122 -0
- package/human/quo-human.md +1042 -0
- package/human/screen.ts +136 -0
- package/human/style.ts +63 -0
- package/human/tab.bundle.txt +4775 -0
- package/human/tab.ts +446 -0
- package/human/tree.ts +217 -0
- package/human/wake.ts +191 -0
- package/human/web.ts +259 -0
- package/human/worker.bundle.txt +3868 -0
- package/human/worker.ts +124 -0
- package/human/worlds.ts +127 -0
- package/mcp/agent.ts +94 -0
- package/mcp/allow.ts +131 -0
- package/mcp/http.ts +92 -0
- package/mcp/oauth.ts +270 -0
- package/mcp/quo-mcp.md +343 -0
- package/mcp/route.ts +45 -0
- package/mcp/runner.ts +136 -0
- package/mcp/server.ts +101 -0
- package/package.json +121 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Being } from '@nervur-org/nervur';
|
|
2
|
+
import type { Asker, Blueprint, JsonObject, Reply, Stance } from '@nervur-org/nervur';
|
|
3
|
+
export declare class Desk extends Being {
|
|
4
|
+
static asks: {
|
|
5
|
+
hello: {
|
|
6
|
+
description: string;
|
|
7
|
+
input: {
|
|
8
|
+
type: string;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
send: {
|
|
12
|
+
description: string;
|
|
13
|
+
input: {
|
|
14
|
+
type: string;
|
|
15
|
+
properties: {
|
|
16
|
+
kind: {
|
|
17
|
+
type: string;
|
|
18
|
+
};
|
|
19
|
+
to: {
|
|
20
|
+
type: string;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
required: string[];
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
device: {
|
|
27
|
+
description: string;
|
|
28
|
+
input: {
|
|
29
|
+
type: string;
|
|
30
|
+
properties: {
|
|
31
|
+
proof: {
|
|
32
|
+
type: string;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
required: string[];
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
constructor(stance: Stance);
|
|
40
|
+
get ways(): {
|
|
41
|
+
id: string;
|
|
42
|
+
kind: string;
|
|
43
|
+
}[];
|
|
44
|
+
answer(asker: Asker, method?: string, args?: JsonObject): Promise<Reply>;
|
|
45
|
+
describe(asker: Asker): Blueprint;
|
|
46
|
+
hello(_args: JsonObject, asker: Asker): {
|
|
47
|
+
desk: boolean;
|
|
48
|
+
asker: string | null;
|
|
49
|
+
kinds: string[];
|
|
50
|
+
};
|
|
51
|
+
send(args: JsonObject): Promise<JsonObject>;
|
|
52
|
+
device(args: JsonObject): Promise<import("@nervur-org/nervur").Json>;
|
|
53
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The front desk. The one public being of a world, and public for the first
|
|
3
|
+
// hello only: everything after is behind an invitation. It holds a standing
|
|
4
|
+
// to every user being, placed by the root, and trades a proof for a device
|
|
5
|
+
// invitation that the user being mints herself. The desk never mints one.
|
|
6
|
+
//
|
|
7
|
+
// A proof is what a route produced after authenticating by its own means. A
|
|
8
|
+
// way in turns a proof of one kind into who it is for: which user, which
|
|
9
|
+
// client identity. The ways in are beings, and the desk reaches each of
|
|
10
|
+
// them as she reaches anything, at a standing under `way:<kind>`: the
|
|
11
|
+
// world's own, placed by the root, and the box's, lent by the dock ward and
|
|
12
|
+
// taken at her birth, so a box with no local side offers no such kind.
|
|
13
|
+
//
|
|
14
|
+
// A kind whose proof the world has to hand out first answers `send` beside
|
|
15
|
+
// `verify`, and a stranger asks for one with `send({ kind, to })`. The desk
|
|
16
|
+
// judges nothing here: she does not know what an address is, whether one was
|
|
17
|
+
// sent to before, or what a link says. She keeps the ways in named in one
|
|
18
|
+
// place, so a side that renders her renders every way into this world.
|
|
19
|
+
import { Being, isSilence, isWord, wordOf } from '@nervur-org/nervur';
|
|
20
|
+
import { join } from '../dock/faculty.js';
|
|
21
|
+
import { LOCAL } from '../dock/local.js';
|
|
22
|
+
import { WAY, kindOf, verified } from './way.js';
|
|
23
|
+
export class Desk extends Being {
|
|
24
|
+
static asks = {
|
|
25
|
+
hello: { description: 'how to get in', input: { type: 'object' } },
|
|
26
|
+
send: { description: 'send a way in to an address of yours', input: { type: 'object', properties: { kind: { type: 'string' }, to: { type: 'string' } }, required: ['kind', 'to'] } },
|
|
27
|
+
device: { description: 'trade a proof for a device invitation', input: { type: 'object', properties: { proof: { type: 'object' } }, required: ['proof'] } },
|
|
28
|
+
};
|
|
29
|
+
// The box's way in, taken at every birth as any being takes what her box
|
|
30
|
+
// lends. A box that lends none, a tab or the edge, leaves her holding
|
|
31
|
+
// nothing under that id and the kind is not offered.
|
|
32
|
+
constructor(stance) {
|
|
33
|
+
super(stance);
|
|
34
|
+
void join(this, LOCAL, undefined, WAY(LOCAL));
|
|
35
|
+
}
|
|
36
|
+
// The ids of the ways in she holds, in the order they were placed.
|
|
37
|
+
get ways() {
|
|
38
|
+
const out = [];
|
|
39
|
+
for (const id of Object.keys(this.cells.standings)) {
|
|
40
|
+
const kind = kindOf(id);
|
|
41
|
+
if (kind !== null && this.standings[id])
|
|
42
|
+
out.push({ id, kind });
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
// Before she is described, she learns what her ways in can be asked. A
|
|
47
|
+
// standing's blueprint is filled by an empty ask and by nothing else, and
|
|
48
|
+
// a way in placed on her a moment ago has none yet; each is asked once,
|
|
49
|
+
// and a way in that will not describe stays a kind she can only verify.
|
|
50
|
+
async answer(asker, method, args = {}) {
|
|
51
|
+
if (method === undefined)
|
|
52
|
+
for (const w of this.ways)
|
|
53
|
+
if (this.cells.standings[w.id]?.blueprint === null)
|
|
54
|
+
await this.standings[w.id]?.ask();
|
|
55
|
+
return await super.answer(asker, method, args);
|
|
56
|
+
}
|
|
57
|
+
// The kinds are the ways in she holds and change with them, so the form a
|
|
58
|
+
// stranger sees is shaped now rather than declared once: `send` is there
|
|
59
|
+
// only where a way in says it can be asked to send, and it names the kinds
|
|
60
|
+
// that can. A world with none shows no such form, and a link is the only
|
|
61
|
+
// way in, as it always was.
|
|
62
|
+
describe(asker) {
|
|
63
|
+
const bp = super.describe(asker);
|
|
64
|
+
const kinds = this.ways.filter((w) => this.cells.standings[w.id]?.blueprint?.asks.some((a) => a.name === 'send')).map((w) => w.kind);
|
|
65
|
+
const asks = bp.asks.flatMap((a) => {
|
|
66
|
+
if (a.name !== 'send')
|
|
67
|
+
return [a];
|
|
68
|
+
if (kinds.length === 0)
|
|
69
|
+
return [];
|
|
70
|
+
return [{ ...a, input: { type: 'object', properties: { kind: { type: 'string', enum: kinds, default: kinds[0] }, to: { type: 'string' } }, required: ['kind', 'to'] } }];
|
|
71
|
+
});
|
|
72
|
+
return { ...bp, asks };
|
|
73
|
+
}
|
|
74
|
+
// A stranger learns the proof kinds and nothing else. Who lives on this
|
|
75
|
+
// world is not the caller's to know: the way in names the user, so
|
|
76
|
+
// nobody at the door ever has to, and a world with a thousand of them
|
|
77
|
+
// would otherwise hand the whole list to anyone who knocked.
|
|
78
|
+
hello(_args, asker) {
|
|
79
|
+
return { desk: true, asker: asker.id ?? null, kinds: this.ways.map((w) => w.kind) };
|
|
80
|
+
}
|
|
81
|
+
// A stranger asks for a way in to an address that is hers. Nothing of this
|
|
82
|
+
// world changes here and nothing is minted: the way in sends, and the
|
|
83
|
+
// proof that comes back is what admits her.
|
|
84
|
+
async send(args) {
|
|
85
|
+
const { kind, to } = args;
|
|
86
|
+
if (typeof kind !== 'string' || typeof to !== 'string')
|
|
87
|
+
return { error: 'kind and to are words' };
|
|
88
|
+
const way = this.standings[WAY(kind)];
|
|
89
|
+
if (!way)
|
|
90
|
+
return { error: 'no such way in' };
|
|
91
|
+
const out = await way.ask('send', { to });
|
|
92
|
+
if (isSilence(out) || isWord(out))
|
|
93
|
+
return { error: 'no such way in' };
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
async device(args) {
|
|
97
|
+
const proof = args.proof;
|
|
98
|
+
if (!proof || typeof proof !== 'object' || Array.isArray(proof) || typeof proof.kind !== 'string')
|
|
99
|
+
return { error: 'proof has a kind' };
|
|
100
|
+
// A kind this world holds no way in for and a way in that says no are
|
|
101
|
+
// one refusal, because the difference between them is a list a stranger
|
|
102
|
+
// could walk. Only a way in that sends for itself is named at this door,
|
|
103
|
+
// in its own form's kinds; a way in that only verifies, as a device's
|
|
104
|
+
// local nonce does, is named nowhere, and two errors here would name it
|
|
105
|
+
// to anyone who guessed the word.
|
|
106
|
+
const way = this.standings[WAY(proof.kind)];
|
|
107
|
+
const said = way ? await way.ask('verify', { proof }) : null;
|
|
108
|
+
const who = said === null || isSilence(said) || isWord(said) ? null : verified(said);
|
|
109
|
+
if (!who)
|
|
110
|
+
return { error: 'refused' };
|
|
111
|
+
const id = `user:${who.user}`;
|
|
112
|
+
// A way in that brings its own people: the first proof for a user nobody
|
|
113
|
+
// has booted makes her, and the desk holds her standing because she is
|
|
114
|
+
// the desk's to make. She learns her name the way the root's setup names
|
|
115
|
+
// her, by the desk's first hello, since a being does not know her own
|
|
116
|
+
// key. A way in that does not say `make` reaches an existing household
|
|
117
|
+
// and nothing else, which is what the local kind on a device does.
|
|
118
|
+
if (!this.standings[id] && (who.make === true || typeof who.make === 'string')) {
|
|
119
|
+
if (!/^[\w.-]{1,80}$/.test(who.user) || who.user === 'desk')
|
|
120
|
+
return { error: 'a user is a word, and not desk' };
|
|
121
|
+
if ((await this.boot(typeof who.make === 'string' ? who.make : 'User', who.user, id)) === null)
|
|
122
|
+
return { error: 'no such user' };
|
|
123
|
+
await this.standings[id].ask('hello', { name: who.user });
|
|
124
|
+
}
|
|
125
|
+
const user = this.standings[id];
|
|
126
|
+
if (!user)
|
|
127
|
+
return { error: 'no such user' };
|
|
128
|
+
const inv = await user.ask('device', { client: who.client, ...(who.wake === true ? { wake: true } : {}), ...(who.reach === true ? { reach: true } : {}), ...(who.mint === true ? { mint: true } : {}), ...(who.revoke === true ? { revoke: true } : {}) });
|
|
129
|
+
if (isSilence(inv))
|
|
130
|
+
return { error: 'silence' };
|
|
131
|
+
if (isWord(inv))
|
|
132
|
+
return { error: wordOf(inv) };
|
|
133
|
+
return inv;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Being } from '@nervur-org/nervur';
|
|
2
|
+
import type { Asker, JsonObject, OccupantRecord } from '@nervur-org/nervur';
|
|
3
|
+
declare const isDevice: (occ: OccupantRecord | undefined) => boolean;
|
|
4
|
+
declare const isWorld: (occ: OccupantRecord | undefined) => boolean;
|
|
5
|
+
declare const isMaker: (occ: OccupantRecord | undefined) => occ is OccupantRecord;
|
|
6
|
+
export declare class Doorbell extends Being {
|
|
7
|
+
static cells: JsonObject;
|
|
8
|
+
static asks: {
|
|
9
|
+
claim: {
|
|
10
|
+
description: string;
|
|
11
|
+
input: {
|
|
12
|
+
type: string;
|
|
13
|
+
};
|
|
14
|
+
for: typeof isMaker;
|
|
15
|
+
};
|
|
16
|
+
keep: {
|
|
17
|
+
description: string;
|
|
18
|
+
input: {
|
|
19
|
+
type: string;
|
|
20
|
+
properties: {
|
|
21
|
+
token: {
|
|
22
|
+
type: string;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
required: string[];
|
|
26
|
+
};
|
|
27
|
+
for: typeof isDevice;
|
|
28
|
+
};
|
|
29
|
+
admit: {
|
|
30
|
+
description: string;
|
|
31
|
+
input: {
|
|
32
|
+
type: string;
|
|
33
|
+
};
|
|
34
|
+
for: typeof isDevice;
|
|
35
|
+
};
|
|
36
|
+
drop: {
|
|
37
|
+
description: string;
|
|
38
|
+
input: {
|
|
39
|
+
type: string;
|
|
40
|
+
properties: {
|
|
41
|
+
world: {
|
|
42
|
+
type: string;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
required: string[];
|
|
46
|
+
};
|
|
47
|
+
for: typeof isDevice;
|
|
48
|
+
};
|
|
49
|
+
allowed: {
|
|
50
|
+
description: string;
|
|
51
|
+
input: {
|
|
52
|
+
type: string;
|
|
53
|
+
};
|
|
54
|
+
for: typeof isDevice;
|
|
55
|
+
};
|
|
56
|
+
ring: {
|
|
57
|
+
description: string;
|
|
58
|
+
input: {
|
|
59
|
+
type: string;
|
|
60
|
+
properties: {
|
|
61
|
+
sealed: {
|
|
62
|
+
type: string;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
required: string[];
|
|
66
|
+
};
|
|
67
|
+
for: typeof isWorld;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
describe(asker: Parameters<Being['describe']>[0]): import("@nervur-org/nervur").Blueprint;
|
|
71
|
+
claim(_args: JsonObject, asker: Asker): Promise<JsonObject>;
|
|
72
|
+
keep(args: JsonObject): JsonObject;
|
|
73
|
+
admit(_args: JsonObject): Promise<JsonObject>;
|
|
74
|
+
drop(args: JsonObject): JsonObject;
|
|
75
|
+
allowed(_args: JsonObject): JsonObject;
|
|
76
|
+
ring(args: JsonObject): Promise<JsonObject>;
|
|
77
|
+
}
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// One device's doorbell. The platform token lives in her cells and leaves
|
|
3
|
+
// for nowhere: no world ever sees it, so no world can copy it, hand it on,
|
|
4
|
+
// or keep ringing after it is told to stop.
|
|
5
|
+
//
|
|
6
|
+
// Her occupants are the worlds that device allowed. `device` is the phone
|
|
7
|
+
// itself, and every other id is one world, invited by the phone and by
|
|
8
|
+
// nobody else. The pair, this device and that world, is a row here, held by
|
|
9
|
+
// neither the world that rings nor the publisher who holds the platform
|
|
10
|
+
// key. That row is the whole of the permission.
|
|
11
|
+
//
|
|
12
|
+
// She is made by the porter, who holds a standing to her and asks once for
|
|
13
|
+
// the device's way in. After a device has claimed it there is nothing the
|
|
14
|
+
// porter can do here at all, which is why `claim` closes itself.
|
|
15
|
+
//
|
|
16
|
+
// A ring carries a banner sealed to the device's banner key. She checks
|
|
17
|
+
// that it fits what the platform will carry and hands it on, and could not
|
|
18
|
+
// read it if she tried.
|
|
19
|
+
//
|
|
20
|
+
// She never learns that key, and there is nothing here for it to be kept
|
|
21
|
+
// in. The device hands its banner key to a world itself, beside the
|
|
22
|
+
// invitation to this doorbell, so a bell that held both halves of the
|
|
23
|
+
// relation is a bell that could read a banner, and this one cannot.
|
|
24
|
+
import { Being, isSilence, isWord, wordOf } from '@nervur-org/nervur';
|
|
25
|
+
const DEVICE = 'device';
|
|
26
|
+
// Her standing at the box's push faculty, under an id of her own. The ward
|
|
27
|
+
// knocks and takes it for her, and the invitation never reaches her.
|
|
28
|
+
const MINE = 'push';
|
|
29
|
+
// What a platform will carry. It is the faculty's number, because the
|
|
30
|
+
// faculty is the one that hands a ring to a platform.
|
|
31
|
+
import { SEAL } from '../dock/push.js';
|
|
32
|
+
const isDevice = (occ) => occ?.id === DEVICE;
|
|
33
|
+
const isWorld = (occ) => occ !== undefined && occ.id !== DEVICE;
|
|
34
|
+
const isMaker = (occ) => occ !== undefined;
|
|
35
|
+
export class Doorbell extends Being {
|
|
36
|
+
static cells = { token: null, admitted: 0, claimed: false };
|
|
37
|
+
static asks = {
|
|
38
|
+
claim: { description: 'the one way in for this doorbell, handed out once', input: { type: 'object' }, for: isMaker },
|
|
39
|
+
keep: { description: 'the device hands its platform token, for nobody else', input: { type: 'object', properties: { token: { type: 'object' } }, required: ['token'] }, for: isDevice },
|
|
40
|
+
admit: { description: 'the device allows one world to wake it', input: { type: 'object' }, for: isDevice },
|
|
41
|
+
drop: { description: 'the device disallows one world, and only that one', input: { type: 'object', properties: { world: { type: 'string' } }, required: ['world'] }, for: isDevice },
|
|
42
|
+
allowed: { description: 'which worlds this device allows', input: { type: 'object' }, for: isDevice },
|
|
43
|
+
ring: { description: 'wake this device: a sealed payload and nothing else', input: { type: 'object', properties: { sealed: { type: 'string' } }, required: ['sealed'] }, for: isWorld },
|
|
44
|
+
};
|
|
45
|
+
// Once a device holds this doorbell there is no way in left to hand out,
|
|
46
|
+
// so the ask goes from every blueprint rather than sitting there dead.
|
|
47
|
+
describe(asker) {
|
|
48
|
+
const out = super.describe(asker);
|
|
49
|
+
if (this.cells.claimed !== true)
|
|
50
|
+
return out;
|
|
51
|
+
return { ...out, asks: out.asks.filter((a) => a.name !== 'claim') };
|
|
52
|
+
}
|
|
53
|
+
// The way in, handed out once and then gone. Whoever made her asks for it
|
|
54
|
+
// and gives it to the phone; a second ask is refused, so there is no
|
|
55
|
+
// second key to this device anywhere.
|
|
56
|
+
async claim(_args, asker) {
|
|
57
|
+
if (this.cells.claimed === true)
|
|
58
|
+
return { error: 'this doorbell has a device' };
|
|
59
|
+
const inv = await this.invite(DEVICE);
|
|
60
|
+
if (inv === null)
|
|
61
|
+
return { error: 'no way in' };
|
|
62
|
+
this.cells.claimed = true;
|
|
63
|
+
// And she leaves. Whoever made this doorbell has nothing here once the
|
|
64
|
+
// way in is handed out, so she is not left standing in the list of who
|
|
65
|
+
// may wake this phone.
|
|
66
|
+
const maker = this.occupant(asker);
|
|
67
|
+
if (maker)
|
|
68
|
+
this.occupants.remove(maker.id);
|
|
69
|
+
return { invitation: inv };
|
|
70
|
+
}
|
|
71
|
+
// Written once and rewritten whenever the platform hands the phone a new
|
|
72
|
+
// one. No world is told anything, because no world was ever given it.
|
|
73
|
+
keep(args) {
|
|
74
|
+
const token = args.token;
|
|
75
|
+
if (token === null || typeof token !== 'object' || Array.isArray(token) || typeof token.kind !== 'string')
|
|
76
|
+
return { error: 'a token names its kind' };
|
|
77
|
+
this.cells.token = token;
|
|
78
|
+
return { kept: token.kind };
|
|
79
|
+
}
|
|
80
|
+
// The device allows a world by asking for a way in and handing it over.
|
|
81
|
+
// So a world can only ring a phone that chose it.
|
|
82
|
+
async admit(_args) {
|
|
83
|
+
const admitted = typeof this.cells.admitted === 'number' ? this.cells.admitted : 0;
|
|
84
|
+
const world = `world:${admitted + 1}`;
|
|
85
|
+
const inv = await this.invite(world);
|
|
86
|
+
if (inv === null)
|
|
87
|
+
return { error: 'no way in' };
|
|
88
|
+
this.cells.admitted = admitted + 1;
|
|
89
|
+
return { world, invitation: inv };
|
|
90
|
+
}
|
|
91
|
+
// Disallowing is removing one occupant. The heir is forgotten, that world
|
|
92
|
+
// hears `removed` once at the door, and her other worlds are untouched.
|
|
93
|
+
drop(args) {
|
|
94
|
+
const world = args.world;
|
|
95
|
+
if (typeof world !== 'string' || world === DEVICE)
|
|
96
|
+
return { error: 'a world is named, and the device is not one' };
|
|
97
|
+
if (!Object.hasOwn(this.cells.occupants, world))
|
|
98
|
+
return { error: 'no such world' };
|
|
99
|
+
this.occupants.remove(world);
|
|
100
|
+
return { dropped: world };
|
|
101
|
+
}
|
|
102
|
+
allowed(_args) {
|
|
103
|
+
return { worlds: Object.keys(this.cells.occupants).filter((id) => id !== DEVICE) };
|
|
104
|
+
}
|
|
105
|
+
// Who is ringing is the standing it arrived on, so the ask carries no
|
|
106
|
+
// world, no pk and no token, and there is nothing in it to forge.
|
|
107
|
+
async ring(args) {
|
|
108
|
+
const sealed = args.sealed;
|
|
109
|
+
if (typeof sealed !== 'string' || sealed.length === 0)
|
|
110
|
+
return { error: 'a ring carries one sealed payload' };
|
|
111
|
+
if (sealed.length > SEAL)
|
|
112
|
+
return { error: `a sealed payload is at most ${SEAL} bytes` };
|
|
113
|
+
const token = this.cells.token;
|
|
114
|
+
if (token === null)
|
|
115
|
+
return { error: 'this device has handed no token' };
|
|
116
|
+
const at = this.standings[MINE] ?? (await this.lend(MINE, MINE).then(() => this.standings[MINE]));
|
|
117
|
+
if (!at)
|
|
118
|
+
return { error: 'this box rings nothing' };
|
|
119
|
+
const out = await at.ask('ring', { token, sealed });
|
|
120
|
+
if (out === undefined || isSilence(out))
|
|
121
|
+
return { error: 'this box rings nothing' };
|
|
122
|
+
if (isWord(out))
|
|
123
|
+
return { error: wordOf(out) };
|
|
124
|
+
// What the faculty says is the answer, and a refusal is one of the things
|
|
125
|
+
// it says: a box holding no key for this kind, or a service that would
|
|
126
|
+
// not take the ring. Passing that on is the whole of the promise a bell
|
|
127
|
+
// makes, because a ring reported as rung and never sent is worse than one
|
|
128
|
+
// that plainly failed: the world stops trying and the human is never told.
|
|
129
|
+
if (out !== null && typeof out === 'object' && !Array.isArray(out) && typeof out.error === 'string')
|
|
130
|
+
return { error: out.error };
|
|
131
|
+
return { rang: true };
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Being } from '@nervur-org/nervur';
|
|
2
|
+
import type { Asker, AskSpec, JsonObject, OccupantRecord } from '@nervur-org/nervur';
|
|
3
|
+
export declare const owner: (_rec: OccupantRecord | undefined, asker: Asker) => boolean;
|
|
4
|
+
export declare const occupant: (rec: OccupantRecord | undefined) => boolean;
|
|
5
|
+
export declare const places: (rec: OccupantRecord | undefined, asker: Asker) => boolean;
|
|
6
|
+
export declare const CREDENTIAL: (properties: Record<string, JsonObject>, required: string[]) => AskSpec;
|
|
7
|
+
export type Delivery = {
|
|
8
|
+
id: string;
|
|
9
|
+
kind: string;
|
|
10
|
+
about: string | null;
|
|
11
|
+
data: JsonObject;
|
|
12
|
+
};
|
|
13
|
+
export type Wake = {
|
|
14
|
+
method: string;
|
|
15
|
+
args: JsonObject;
|
|
16
|
+
};
|
|
17
|
+
export type Heard = {
|
|
18
|
+
ok: boolean;
|
|
19
|
+
status: number;
|
|
20
|
+
body: JsonObject;
|
|
21
|
+
};
|
|
22
|
+
export declare class Envoy extends Being {
|
|
23
|
+
#private;
|
|
24
|
+
static cells: JsonObject;
|
|
25
|
+
static asks: Record<string, AskSpec>;
|
|
26
|
+
static IN: Record<string, AskSpec>;
|
|
27
|
+
protected check(c: JsonObject): string | null;
|
|
28
|
+
static at: string | null;
|
|
29
|
+
credential(args: JsonObject, asker: Asker): JsonObject;
|
|
30
|
+
forget(): JsonObject;
|
|
31
|
+
protected get held(): JsonObject | null;
|
|
32
|
+
protected out(path: string, init?: {
|
|
33
|
+
method?: string;
|
|
34
|
+
body?: JsonObject;
|
|
35
|
+
headers?: Record<string, string>;
|
|
36
|
+
}): Promise<Heard>;
|
|
37
|
+
protected refused(h: Heard): JsonObject;
|
|
38
|
+
protected remember(vendorId: string, by: string, more?: JsonObject): void;
|
|
39
|
+
protected recall(vendorId: string): (JsonObject & {
|
|
40
|
+
by: string;
|
|
41
|
+
}) | undefined;
|
|
42
|
+
deliver(args: JsonObject): Promise<JsonObject>;
|
|
43
|
+
protected verify(_body: string, _headers: Record<string, string>): Promise<boolean> | boolean;
|
|
44
|
+
protected decode(_body: string, _headers: Record<string, string>): Delivery | {
|
|
45
|
+
error: string;
|
|
46
|
+
};
|
|
47
|
+
protected landed(_query: string, _headers: Record<string, string>): JsonObject | Promise<JsonObject>;
|
|
48
|
+
protected hmac(secret: string, text: string): Promise<string>;
|
|
49
|
+
protected fresh(ts: string | number): boolean;
|
|
50
|
+
protected wake(_d: Delivery, _about: (JsonObject & {
|
|
51
|
+
by: string;
|
|
52
|
+
}) | undefined): Wake | null;
|
|
53
|
+
get unwoken(): JsonObject[];
|
|
54
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The envoy base: what every envoy is and no envoy has alone. An envoy is
|
|
3
|
+
// a being of the org's standing in for a ward she does not run, an account
|
|
4
|
+
// at a thing that speaks no Quo, and the trunk's envoy chapter says the
|
|
5
|
+
// seven things she is. This is those seven as one class an author extends,
|
|
6
|
+
// so that a vendor's envoy is her asks out, her signature and her decoding,
|
|
7
|
+
// and nothing else.
|
|
8
|
+
//
|
|
9
|
+
// Two gates, one credential, one way out, one way in, one memory, one way
|
|
10
|
+
// back. `credential` and `forget` are the owner's; `deliver`, for an envoy
|
|
11
|
+
// that is called in, is the owner's too, and the daemon asks it with the
|
|
12
|
+
// vendor's raw bytes and headers. What an occupant may ask is the class's
|
|
13
|
+
// asks out, gated by `occupant` or by her own reading of the notes.
|
|
14
|
+
//
|
|
15
|
+
// Nothing here knows a vendor's name. The signature is `verify`, the
|
|
16
|
+
// vocabulary is `decode`, and what a delivery wakes is `wake`, each a
|
|
17
|
+
// method the class fills; the base keeps the discipline around them: the
|
|
18
|
+
// bytes checked before anything is parsed, the id kept bounded so a retry
|
|
19
|
+
// lands once, `{ ok }` answered before the wake goes out, a wake that meets
|
|
20
|
+
// silence written down and never retried.
|
|
21
|
+
import { Being, isSilence, isWord, wordOf } from '@nervur-org/nervur';
|
|
22
|
+
// The two gates. The owner is the ward's ask pointer, `{ id: 'OWNER' }`,
|
|
23
|
+
// which is how the daemon asks `deliver` and how the root places a
|
|
24
|
+
// credential; an occupant is anyone holding a standing at her.
|
|
25
|
+
export const owner = (_rec, asker) => asker.id === 'OWNER';
|
|
26
|
+
export const occupant = (rec) => rec !== undefined;
|
|
27
|
+
// The third gate: who may place what the vendor gave this account. WORLDS
|
|
28
|
+
// has placing an envoy as an ask on home, shown by the gate to the org's
|
|
29
|
+
// presences and to its model, with the box never logged into for it, so
|
|
30
|
+
// this cannot be the owner alone. It is the owner or an occupant whose
|
|
31
|
+
// note says she may, placed with her standing the way every other note is,
|
|
32
|
+
// and it is never a default: an org that marked nobody has the root's
|
|
33
|
+
// shell and nothing else, which is where this began.
|
|
34
|
+
export const places = (rec, asker) => asker.id === 'OWNER' || rec?.notes.credential === true;
|
|
35
|
+
// A credential as a form, for a class that wants one. `at` is never among
|
|
36
|
+
// the fields and cannot be: it is where this box sends the org's key, so a
|
|
37
|
+
// form that set it would be a form that points the box at anywhere and
|
|
38
|
+
// makes it fetch there with the account's own secret. Where a vendor
|
|
39
|
+
// answers is the class's, since the class is the thing that speaks that
|
|
40
|
+
// vendor, and only the owner may name another.
|
|
41
|
+
export const CREDENTIAL = (properties, required) => ({
|
|
42
|
+
description: 'what the vendor gave this account: kept in her cells, and never shown again',
|
|
43
|
+
input: { type: 'object', properties, required },
|
|
44
|
+
for: places,
|
|
45
|
+
});
|
|
46
|
+
const SEEN = 200;
|
|
47
|
+
const FRESH = 5 * 60_000;
|
|
48
|
+
export class Envoy extends Being {
|
|
49
|
+
static cells = { credential: null, about: {}, seen: [], unwoken: [] };
|
|
50
|
+
static asks = {
|
|
51
|
+
credential: { description: 'what the vendor gave the account: kept in her cells, never shown', input: { type: 'object' }, for: places },
|
|
52
|
+
forget: { description: 'forget the credential', input: { type: 'object' }, for: places },
|
|
53
|
+
};
|
|
54
|
+
// The one ask in, for an envoy the vendor calls: a class that is called
|
|
55
|
+
// in spreads it into her asks, `{ ...Envoy.asks, ...Envoy.IN, ...hers }`,
|
|
56
|
+
// and a one-way envoy shows no deliver at all.
|
|
57
|
+
static IN = {
|
|
58
|
+
deliver: { description: 'the vendor delivers: its raw bytes and headers, from the daemon as the owner', input: { type: 'object', properties: { body: { type: 'string' }, headers: { type: 'object' } }, required: ['body', 'headers'] }, for: owner },
|
|
59
|
+
};
|
|
60
|
+
// 1. Her credential is in her cells, and she gets it herself. The owner
|
|
61
|
+
// places what the vendor gave, values only; `check` is the class's word
|
|
62
|
+
// on what it must hold, a reason or null.
|
|
63
|
+
check(c) {
|
|
64
|
+
for (const [k, v] of Object.entries(c))
|
|
65
|
+
if (typeof v !== 'string' || v === '')
|
|
66
|
+
return `${k} is a non-empty string`;
|
|
67
|
+
return Object.keys(c).length ? null : 'a credential has at least one field';
|
|
68
|
+
}
|
|
69
|
+
// Where the vendor answers, as the class that speaks that vendor knows
|
|
70
|
+
// it. A class that leaves it null has an owner who must name one once.
|
|
71
|
+
static at = null;
|
|
72
|
+
credential(args, asker) {
|
|
73
|
+
// `at` is the one field a form may not carry. From anyone but the
|
|
74
|
+
// owner it is dropped rather than refused, since a human pasting a key
|
|
75
|
+
// has said nothing about it either way, and what stands is the one
|
|
76
|
+
// already placed or the class's own.
|
|
77
|
+
const { at: named, ...rest } = args;
|
|
78
|
+
const at = asker.id === 'OWNER' && typeof named === 'string' ? named : (this.held?.at ?? this.constructor.at);
|
|
79
|
+
const c = { ...rest, ...(typeof at === 'string' ? { at } : {}) };
|
|
80
|
+
const why = this.check(c);
|
|
81
|
+
if (why)
|
|
82
|
+
return { error: why };
|
|
83
|
+
this.cells.credential = c;
|
|
84
|
+
return { kept: true };
|
|
85
|
+
}
|
|
86
|
+
forget() {
|
|
87
|
+
this.cells.credential = null;
|
|
88
|
+
return { kept: false };
|
|
89
|
+
}
|
|
90
|
+
get held() {
|
|
91
|
+
return this.cells.credential ?? null;
|
|
92
|
+
}
|
|
93
|
+
// 2. Her asks out are the vendor's surface, narrowed: one fetch, from
|
|
94
|
+
// `at` in her credential with `key` as the bearer, the answer as values.
|
|
95
|
+
// The class reshapes what it heard and passes nothing through raw.
|
|
96
|
+
async out(path, init = {}) {
|
|
97
|
+
const c = this.held;
|
|
98
|
+
if (!c || typeof c.at !== 'string')
|
|
99
|
+
return { ok: false, status: 0, body: { error: 'no credential: the owner has not placed one' } };
|
|
100
|
+
let r;
|
|
101
|
+
try {
|
|
102
|
+
r = await fetch(`${c.at}${path}`, {
|
|
103
|
+
method: init.method ?? (init.body ? 'POST' : 'GET'),
|
|
104
|
+
headers: { ...(typeof c.key === 'string' ? { authorization: `Bearer ${c.key}` } : {}), ...(init.body ? { 'content-type': 'application/json' } : {}), ...(init.headers ?? {}) },
|
|
105
|
+
...(init.body ? { body: JSON.stringify(init.body) } : {}),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return { ok: false, status: 0, body: { error: 'the vendor is unreachable' } };
|
|
110
|
+
}
|
|
111
|
+
const v = (await r.json().catch(() => ({})));
|
|
112
|
+
const body = v !== null && typeof v === 'object' && !Array.isArray(v) ? v : {};
|
|
113
|
+
return { ok: r.ok, status: r.status, body };
|
|
114
|
+
}
|
|
115
|
+
// The vendor's no, as an error object in her own words.
|
|
116
|
+
refused(h) {
|
|
117
|
+
return h.status === 0 ? h.body : { error: `the vendor refused: ${h.status}${typeof h.body.message === 'string' ? ` ${h.body.message}` : ''}` };
|
|
118
|
+
}
|
|
119
|
+
// 4. Her cells are what the vendor told her: the map from the vendor's id
|
|
120
|
+
// to whom it is about, written when she asked out, read when called in.
|
|
121
|
+
remember(vendorId, by, more = {}) {
|
|
122
|
+
this.cells.about[vendorId] = { by, ...more };
|
|
123
|
+
}
|
|
124
|
+
recall(vendorId) {
|
|
125
|
+
const m = this.cells.about;
|
|
126
|
+
return Object.hasOwn(m, vendorId) ? m[vendorId] : undefined;
|
|
127
|
+
}
|
|
128
|
+
// 3. `deliver` is her one ask in. The daemon asks it as the owner with
|
|
129
|
+
// the raw body and the headers; an empty body is a redirect landing,
|
|
130
|
+
// the query in the headers under `:query`. The signature is checked
|
|
131
|
+
// before anything is parsed; a delivery she has seen is `{ ok }` at once
|
|
132
|
+
// and wakes nobody twice; and 6, `{ ok }` goes back before the wake.
|
|
133
|
+
async deliver(args) {
|
|
134
|
+
const body = typeof args.body === 'string' ? args.body : '';
|
|
135
|
+
const headers = (args.headers && typeof args.headers === 'object' && !Array.isArray(args.headers) ? args.headers : {});
|
|
136
|
+
if (body === '')
|
|
137
|
+
return this.landed(headers[':query'] ?? '', headers);
|
|
138
|
+
if (!(await this.verify(body, headers)))
|
|
139
|
+
return { error: 'bad signature' };
|
|
140
|
+
const d = this.decode(body, headers);
|
|
141
|
+
if ('error' in d)
|
|
142
|
+
return d;
|
|
143
|
+
const seen = this.cells.seen;
|
|
144
|
+
const mark = `${d.kind}:${d.id}`;
|
|
145
|
+
if (seen.includes(mark))
|
|
146
|
+
return { ok: true };
|
|
147
|
+
seen.push(mark);
|
|
148
|
+
while (seen.length > SEEN)
|
|
149
|
+
seen.shift();
|
|
150
|
+
void this.#wake(d);
|
|
151
|
+
return { ok: true };
|
|
152
|
+
}
|
|
153
|
+
// The class's word on the vendor's signature, over the bytes as sent.
|
|
154
|
+
verify(_body, _headers) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
// The class's reading of the vendor's vocabulary.
|
|
158
|
+
decode(_body, _headers) {
|
|
159
|
+
return { error: 'not called in' };
|
|
160
|
+
}
|
|
161
|
+
// Where a redirect lands: an OAuth code in the query, for a class that
|
|
162
|
+
// begins its account that way. The base takes nothing from it.
|
|
163
|
+
landed(_query, _headers) {
|
|
164
|
+
return { ok: true };
|
|
165
|
+
}
|
|
166
|
+
// A signature over text with a shared secret, hex, the shape most
|
|
167
|
+
// vendors sign in: here once so a class writes the vendor's framing only.
|
|
168
|
+
async hmac(secret, text) {
|
|
169
|
+
const key = await crypto.subtle.importKey('raw', new TextEncoder().encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
|
|
170
|
+
return [...new Uint8Array(await crypto.subtle.sign('HMAC', key, new TextEncoder().encode(text)))].map((x) => x.toString(16).padStart(2, '0')).join('');
|
|
171
|
+
}
|
|
172
|
+
// Whether a vendor's timestamp is within five minutes of now, in seconds
|
|
173
|
+
// or in milliseconds, since vendors sign in either and a class should
|
|
174
|
+
// not have to know which.
|
|
175
|
+
fresh(ts) {
|
|
176
|
+
const t = Number(ts);
|
|
177
|
+
if (!Number.isFinite(t))
|
|
178
|
+
return false;
|
|
179
|
+
const ms = t < 1e11 ? t * 1000 : t;
|
|
180
|
+
return Math.abs(Date.now() - ms) <= FRESH;
|
|
181
|
+
}
|
|
182
|
+
// 5. Her way back is a standing placed like any other, `to:<id>` for the
|
|
183
|
+
// occupant a vendor id was remembered for, handed to her at the knock.
|
|
184
|
+
// A delivery is one ask on it, in her own words, said by `wake`; the
|
|
185
|
+
// being woken cannot tell it from a clock.
|
|
186
|
+
wake(_d, _about) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
async #wake(d) {
|
|
190
|
+
const about = d.about === null ? undefined : this.recall(d.about);
|
|
191
|
+
const w = this.wake(d, about);
|
|
192
|
+
if (!w)
|
|
193
|
+
return;
|
|
194
|
+
const unwoken = this.cells.unwoken;
|
|
195
|
+
// a wake the class shaped wrong is written down, never dropped on the floor
|
|
196
|
+
if (typeof w.method !== 'string' || !w.method || w.args === null || typeof w.args !== 'object')
|
|
197
|
+
return void unwoken.push({ id: d.id, kind: d.kind, why: 'a wake is { method, args }' });
|
|
198
|
+
const back = about ? this.standings[`to:${about.by}`] : undefined;
|
|
199
|
+
if (!back)
|
|
200
|
+
return void unwoken.push({ id: d.id, kind: d.kind, why: about ? `no way back to ${about.by}` : 'about nobody she remembers' });
|
|
201
|
+
const out = await back.ask(w.method, w.args);
|
|
202
|
+
if (isSilence(out) || out === undefined)
|
|
203
|
+
unwoken.push({ id: d.id, kind: d.kind, why: 'silence' });
|
|
204
|
+
else if (isWord(out))
|
|
205
|
+
unwoken.push({ id: d.id, kind: d.kind, why: wordOf(out) });
|
|
206
|
+
}
|
|
207
|
+
// What she could not wake, for the device's own code and a suite.
|
|
208
|
+
get unwoken() {
|
|
209
|
+
return this.cells.unwoken;
|
|
210
|
+
}
|
|
211
|
+
}
|