nervur 0.20.0 → 0.20.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +229 -20
- package/dist/being/being.d.ts +1 -0
- package/dist/being/being.js +5 -1
- package/dist/being/faculty.d.ts +1 -0
- package/dist/being/faculty.js +13 -7
- package/dist/being/index.d.ts +1 -0
- package/dist/being/index.js +1 -0
- package/dist/being/kind.d.ts +4 -0
- package/dist/being/kind.js +28 -0
- package/dist/being/types.d.ts +3 -4
- package/dist/browser/index.d.ts +35 -0
- package/dist/browser/index.js +188 -0
- package/dist/browser/worker.d.ts +12 -0
- package/dist/browser/worker.js +40 -0
- package/dist/browser.d.ts +1 -0
- package/dist/browser.js +7 -0
- package/dist/cli/command.d.ts +13 -0
- package/dist/cli/command.js +237 -0
- package/dist/cli/harbor.d.ts +19 -0
- package/dist/cli/harbor.js +99 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +14 -0
- package/dist/contract/index.d.ts +16 -2
- package/dist/contract/index.js +13 -3
- package/dist/edge/index.d.ts +57 -0
- package/dist/edge/index.js +306 -0
- package/dist/edge.d.ts +1 -0
- package/dist/edge.js +8 -0
- package/dist/folder/index.d.ts +9 -4
- package/dist/folder/index.js +89 -84
- package/dist/harbor/carrying.d.ts +17 -0
- package/dist/harbor/carrying.js +38 -0
- package/dist/harbor/catalogue.d.ts +27 -0
- package/dist/harbor/catalogue.js +101 -0
- package/dist/harbor/dock.d.ts +32 -3
- package/dist/harbor/dock.js +98 -12
- package/dist/harbor/harbor.d.ts +27 -8
- package/dist/harbor/harbor.js +217 -26
- package/dist/harbor/index.d.ts +8 -2
- package/dist/harbor/index.js +8 -1
- package/dist/harbor/package.d.ts +12 -0
- package/dist/harbor/package.js +130 -0
- package/dist/harbor/probe.d.ts +18 -0
- package/dist/harbor/probe.js +15 -0
- package/dist/harbor/registry.d.ts +6 -3
- package/dist/harbor/registry.js +83 -7
- package/dist/harbor/relay.d.ts +15 -0
- package/dist/harbor/relay.js +387 -0
- package/dist/harbor/root-line.d.ts +9 -0
- package/dist/harbor/root-line.js +38 -0
- package/dist/harbor/terrain.d.ts +14 -1
- package/dist/harbor/terrain.js +19 -0
- package/dist/http/index.d.ts +4 -0
- package/dist/http/index.js +156 -0
- package/dist/http/websocket.d.ts +29 -0
- package/dist/http/websocket.js +133 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +12 -6
- package/dist/line/answer.d.ts +8 -0
- package/dist/line/answer.js +60 -0
- package/dist/line/frame.d.ts +34 -0
- package/dist/line/frame.js +81 -0
- package/dist/line/ground.d.ts +3 -0
- package/dist/line/ground.js +9 -0
- package/dist/line/index.d.ts +5 -0
- package/dist/line/index.js +9 -0
- package/dist/line/push.d.ts +7 -0
- package/dist/line/push.js +92 -0
- package/dist/line/web.d.ts +30 -0
- package/dist/line/web.js +181 -0
- package/dist/node/index.d.ts +32 -0
- package/dist/node/index.js +164 -0
- package/dist/node.d.ts +1 -0
- package/dist/node.js +7 -0
- package/dist/pointer/bodies.d.ts +11 -3
- package/dist/pointer/bodies.js +47 -18
- package/dist/pointer/index.d.ts +1 -1
- package/dist/pointer/index.js +1 -1
- package/dist/pointer/world.d.ts +12 -5
- package/dist/pointer/world.js +29 -14
- package/dist/quo/address.d.ts +19 -0
- package/dist/quo/address.js +84 -0
- package/dist/quo/door.d.ts +1 -1
- package/dist/quo/door.js +6 -4
- package/dist/quo/index.d.ts +1 -0
- package/dist/quo/index.js +1 -0
- package/dist/quo/invitation.d.ts +1 -0
- package/dist/quo/invitation.js +9 -5
- package/dist/quo/keys.js +1 -1
- package/dist/tcp/frame.d.ts +25 -0
- package/dist/tcp/frame.js +64 -0
- package/dist/tcp/index.d.ts +24 -0
- package/dist/tcp/index.js +260 -0
- package/dist/ward/index.d.ts +3 -2
- package/dist/ward/index.js +2 -1
- package/dist/ward/pilot.d.ts +8 -0
- package/dist/ward/pilot.js +50 -0
- package/dist/ward/stance.d.ts +4 -4
- package/dist/ward/stance.js +10 -7
- package/dist/ward/ward-being.d.ts +4 -1
- package/dist/ward/ward-being.js +8 -2
- package/dist/ward/ward.d.ts +11 -2
- package/dist/ward/ward.js +41 -5
- package/package.json +28 -2
- package/src/being/being.ts +5 -1
- package/src/being/faculty.ts +14 -7
- package/src/being/index.ts +1 -0
- package/src/being/kind.ts +30 -0
- package/src/being/types.ts +8 -6
- package/src/browser/index.ts +207 -0
- package/src/browser/worker.ts +66 -0
- package/src/browser.ts +9 -0
- package/src/cli/command.ts +224 -0
- package/src/cli/harbor.ts +103 -0
- package/src/cli/main.ts +13 -0
- package/src/contract/index.ts +42 -10
- package/src/edge/index.ts +359 -0
- package/src/edge.ts +10 -0
- package/src/folder/index.ts +87 -84
- package/src/harbor/carrying.ts +56 -0
- package/src/harbor/catalogue.ts +125 -0
- package/src/harbor/dock.ts +122 -15
- package/src/harbor/harbor.ts +240 -36
- package/src/harbor/index.ts +10 -3
- package/src/harbor/package.ts +147 -0
- package/src/harbor/probe.ts +34 -0
- package/src/harbor/registry.ts +73 -9
- package/src/harbor/relay.ts +399 -0
- package/src/harbor/root-line.ts +39 -0
- package/src/harbor/terrain.ts +31 -3
- package/src/http/index.ts +150 -0
- package/src/http/websocket.ts +124 -0
- package/src/index.ts +39 -6
- package/src/line/answer.ts +59 -0
- package/src/line/frame.ts +88 -0
- package/src/line/ground.ts +19 -0
- package/src/line/index.ts +9 -0
- package/src/line/push.ts +112 -0
- package/src/line/web.ts +227 -0
- package/src/node/index.ts +170 -0
- package/src/node.ts +9 -0
- package/src/pointer/bodies.ts +45 -16
- package/src/pointer/index.ts +1 -1
- package/src/pointer/world.ts +38 -19
- package/src/quo/address.ts +87 -0
- package/src/quo/door.ts +6 -4
- package/src/quo/index.ts +1 -0
- package/src/quo/invitation.ts +10 -6
- package/src/quo/keys.ts +1 -1
- package/src/stand/main.ts +1 -0
- package/src/stand/stand.ts +87 -6
- package/src/tcp/index.ts +284 -0
- package/src/ward/index.ts +3 -2
- package/src/ward/pilot.ts +50 -0
- package/src/ward/stance.ts +14 -8
- package/src/ward/ward-being.ts +9 -3
- package/src/ward/ward.ts +53 -6
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { Custody, Memory } from '../contract/index.js';
|
|
10
|
+
import { decrypt, encrypt, hkdf, isHex, unhex, utf8 } from '../crypto/index.js';
|
|
11
|
+
import { addGround, CarrierFaculty, Harbor, UNDIALED } from '../harbor/index.js';
|
|
12
|
+
import { answerHeld, answerPost, askFrame, FrameReader, TCP, TCP_OPENING, WEB_OPENING, webFaculty, webPush } from '../line/index.js';
|
|
13
|
+
import { CryptoEntropy, HeldLoader, PointerTerrain } from '../pointer/index.js';
|
|
14
|
+
import { tcpAddress, tcpAt } from '../quo/index.js';
|
|
15
|
+
import { ownerAsk } from '../ward/index.js';
|
|
16
|
+
export const EDGE = 'edge';
|
|
17
|
+
const ENTRY = 'm/';
|
|
18
|
+
const SEED = 'c/seed';
|
|
19
|
+
// The most keys one storage call takes.
|
|
20
|
+
const BATCH = 128;
|
|
21
|
+
const SEAL_INFO = utf8('nervur edge custody');
|
|
22
|
+
const MAX_ID = 0xffff_ffff;
|
|
23
|
+
const chunks = (items) => Array.from({ length: Math.ceil(items.length / BATCH) }, (_, i) => items.slice(i * BATCH, (i + 1) * BATCH));
|
|
24
|
+
const bytesOf = (value) => new Uint8Array(value instanceof ArrayBuffer ? value : value);
|
|
25
|
+
// A body keeps its keys under `root`, so one object may hold more than one
|
|
26
|
+
// keeping.
|
|
27
|
+
export class EdgeMemory extends Memory {
|
|
28
|
+
#storage;
|
|
29
|
+
#entry;
|
|
30
|
+
constructor(storage, root = '') {
|
|
31
|
+
super();
|
|
32
|
+
this.#storage = storage;
|
|
33
|
+
this.#entry = `${root}${ENTRY}`;
|
|
34
|
+
}
|
|
35
|
+
async read(place) {
|
|
36
|
+
const prefix = `${this.#entry}${place}/`;
|
|
37
|
+
const listed = await this.#storage.list({ prefix });
|
|
38
|
+
return new Map([...listed].map(([key, value]) => [key.slice(prefix.length), bytesOf(value).slice()]));
|
|
39
|
+
}
|
|
40
|
+
write(place, entries) {
|
|
41
|
+
const put = [];
|
|
42
|
+
const gone = [];
|
|
43
|
+
for (const [name, bytes] of entries) {
|
|
44
|
+
if (bytes === null)
|
|
45
|
+
gone.push(`${this.#entry}${place}/${name}`);
|
|
46
|
+
else
|
|
47
|
+
put.push([`${this.#entry}${place}/${name}`, bytes.slice()]);
|
|
48
|
+
}
|
|
49
|
+
return this.#storage.transaction(async (txn) => {
|
|
50
|
+
for (const batch of chunks(put))
|
|
51
|
+
await txn.put(Object.fromEntries(batch));
|
|
52
|
+
for (const batch of chunks(gone))
|
|
53
|
+
await txn.delete(batch);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
async places() {
|
|
57
|
+
const entry = this.#entry;
|
|
58
|
+
const keys = [...(await this.#storage.list({ prefix: entry })).keys()];
|
|
59
|
+
return [...new Set(keys.map((key) => key.slice(entry.length, key.indexOf('/', entry.length))))];
|
|
60
|
+
}
|
|
61
|
+
async forget(place) {
|
|
62
|
+
const keys = [...(await this.#storage.list({ prefix: `${this.#entry}${place}/` })).keys()];
|
|
63
|
+
await this.#storage.transaction(async (txn) => {
|
|
64
|
+
for (const batch of chunks(keys))
|
|
65
|
+
await txn.delete(batch);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// The seed, drawn once and kept sealed under a key the secret gives, so
|
|
70
|
+
// the storage alone opens nothing. A harbor with no secret does not stand.
|
|
71
|
+
export class EdgeCustody extends Custody {
|
|
72
|
+
#storage;
|
|
73
|
+
#secret;
|
|
74
|
+
#entropy;
|
|
75
|
+
#key;
|
|
76
|
+
constructor(storage, secret, entropy = new CryptoEntropy(), root = '') {
|
|
77
|
+
super();
|
|
78
|
+
this.#storage = storage;
|
|
79
|
+
this.#secret = secret;
|
|
80
|
+
this.#entropy = entropy;
|
|
81
|
+
this.#key = `${root}${SEED}`;
|
|
82
|
+
}
|
|
83
|
+
async seed() {
|
|
84
|
+
if (!this.#secret)
|
|
85
|
+
throw new Error('the worker holds no NERVUR_SECRET');
|
|
86
|
+
const key = await hkdf(utf8(this.#secret), SEAL_INFO, 32);
|
|
87
|
+
return this.#storage.transaction(async (txn) => {
|
|
88
|
+
const kept = (await txn.get(this.#key));
|
|
89
|
+
if (kept) {
|
|
90
|
+
const seed = await decrypt(key, bytesOf(kept.nonce), new Uint8Array(0), bytesOf(kept.sealed));
|
|
91
|
+
if (seed === null)
|
|
92
|
+
throw new Error('the seed does not open under this NERVUR_SECRET');
|
|
93
|
+
return seed;
|
|
94
|
+
}
|
|
95
|
+
const seed = this.#entropy.draw(32);
|
|
96
|
+
const nonce = this.#entropy.draw(12);
|
|
97
|
+
await txn.put({ [this.#key]: { nonce, sealed: await encrypt(key, nonce, new Uint8Array(0), seed) } });
|
|
98
|
+
return seed.slice();
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
class EdgeLine {
|
|
103
|
+
#socket;
|
|
104
|
+
#writer;
|
|
105
|
+
#waiting = new Map();
|
|
106
|
+
#next = 1;
|
|
107
|
+
#closed = false;
|
|
108
|
+
constructor(socket, onClose) {
|
|
109
|
+
this.#socket = socket;
|
|
110
|
+
this.#writer = socket.writable.getWriter();
|
|
111
|
+
const reader = new FrameReader();
|
|
112
|
+
void (async () => {
|
|
113
|
+
try {
|
|
114
|
+
for await (const chunk of socket.readable) {
|
|
115
|
+
for (const frame of reader.push(new Uint8Array(chunk))) {
|
|
116
|
+
if (frame.kind === 'ask')
|
|
117
|
+
continue;
|
|
118
|
+
const done = this.#waiting.get(frame.id);
|
|
119
|
+
this.#waiting.delete(frame.id);
|
|
120
|
+
done?.(frame.kind === 'reply' ? frame.box : null);
|
|
121
|
+
}
|
|
122
|
+
if (reader.bad)
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
// A line that broke answers nothing more.
|
|
128
|
+
}
|
|
129
|
+
this.#closed = true;
|
|
130
|
+
onClose();
|
|
131
|
+
for (const done of this.#waiting.values())
|
|
132
|
+
done(null);
|
|
133
|
+
this.#waiting.clear();
|
|
134
|
+
await socket.close().catch(() => undefined);
|
|
135
|
+
})();
|
|
136
|
+
}
|
|
137
|
+
ask(pk, box) {
|
|
138
|
+
if (this.#closed)
|
|
139
|
+
return Promise.resolve(null);
|
|
140
|
+
const id = this.#next;
|
|
141
|
+
this.#next = this.#next === MAX_ID ? 1 : this.#next + 1;
|
|
142
|
+
return new Promise((resolve) => {
|
|
143
|
+
this.#waiting.set(id, resolve);
|
|
144
|
+
this.#writer.write(askFrame(id, pk, box)).catch(() => resolve(null));
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
close() {
|
|
148
|
+
this.#closed = true;
|
|
149
|
+
return this.#socket.close().catch(() => undefined);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const sockets = 'cloudflare:sockets';
|
|
153
|
+
const connector = async () => {
|
|
154
|
+
try {
|
|
155
|
+
return (await import(__rewriteRelativeImportExtension(/* @vite-ignore */ sockets))).connect;
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
// Quo over TCP from a worker: it dials, and takes no inbound TCP.
|
|
162
|
+
export const edgeTcp = () => class EdgeTcp extends CarrierFaculty {
|
|
163
|
+
static kind = TCP;
|
|
164
|
+
static asks = {
|
|
165
|
+
listen: ownerAsk('an edge takes no inbound TCP'),
|
|
166
|
+
deaf: ownerAsk('listen nowhere'),
|
|
167
|
+
};
|
|
168
|
+
#lines = new Map();
|
|
169
|
+
accepts(address) {
|
|
170
|
+
return tcpAddress(address) !== null;
|
|
171
|
+
}
|
|
172
|
+
async dial(address, pk, bytes) {
|
|
173
|
+
const at = tcpAddress(address);
|
|
174
|
+
if (at === null || !isHex(pk, 64))
|
|
175
|
+
return UNDIALED;
|
|
176
|
+
const line = await this.#line(at);
|
|
177
|
+
return line ? line.ask(unhex(pk), bytes) : UNDIALED;
|
|
178
|
+
}
|
|
179
|
+
#line({ host, port }) {
|
|
180
|
+
const at = tcpAt(host, port);
|
|
181
|
+
let line = this.#lines.get(at);
|
|
182
|
+
if (!line) {
|
|
183
|
+
line = (async () => {
|
|
184
|
+
const connect = await connector();
|
|
185
|
+
if (!connect)
|
|
186
|
+
return null;
|
|
187
|
+
try {
|
|
188
|
+
const socket = connect({ hostname: host, port });
|
|
189
|
+
await socket.opened;
|
|
190
|
+
return new EdgeLine(socket, () => this.#lines.delete(at));
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
})();
|
|
196
|
+
this.#lines.set(at, line);
|
|
197
|
+
void line.then((open) => open ?? this.#lines.delete(at));
|
|
198
|
+
}
|
|
199
|
+
return line;
|
|
200
|
+
}
|
|
201
|
+
async sleep() {
|
|
202
|
+
const lines = [...this.#lines.values()];
|
|
203
|
+
this.#lines.clear();
|
|
204
|
+
for (const line of await Promise.all(lines))
|
|
205
|
+
await line?.close();
|
|
206
|
+
}
|
|
207
|
+
listen() {
|
|
208
|
+
return { error: 'an edge takes no inbound TCP' };
|
|
209
|
+
}
|
|
210
|
+
deaf() {
|
|
211
|
+
return { deaf: true };
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
// Where the edge's web carrier listens: the object's own requests, at the
|
|
215
|
+
// host and path its cells name. A port of 0 is the edge's own https.
|
|
216
|
+
const edgeServe = (ground, listen) => {
|
|
217
|
+
const terrain = ground.terrain;
|
|
218
|
+
if (!(terrain instanceof EdgeTerrain))
|
|
219
|
+
return Promise.reject(new Error('this harbor stands on no edge'));
|
|
220
|
+
terrain.listening = listen;
|
|
221
|
+
const authority = listen.port === 0 ? listen.host : `${tcpAt(listen.host, listen.port).slice('tcp://'.length)}`;
|
|
222
|
+
const [post, held] = listen.port === 0 ? ['https', 'wss'] : ['http', 'ws'];
|
|
223
|
+
return Promise.resolve({
|
|
224
|
+
port: listen.port,
|
|
225
|
+
at: [`${post}://${authority}${listen.path}`, `${held}://${authority}${listen.path}`],
|
|
226
|
+
close: () => {
|
|
227
|
+
if (terrain.listening === listen)
|
|
228
|
+
terrain.listening = undefined;
|
|
229
|
+
return Promise.resolve();
|
|
230
|
+
},
|
|
231
|
+
});
|
|
232
|
+
};
|
|
233
|
+
export class EdgeTerrain extends PointerTerrain {
|
|
234
|
+
given;
|
|
235
|
+
// Where the web carrier listens now, as the object answers requests.
|
|
236
|
+
listening;
|
|
237
|
+
#harbor;
|
|
238
|
+
constructor(parts) {
|
|
239
|
+
const entropy = new CryptoEntropy();
|
|
240
|
+
const { storage } = parts.given.state;
|
|
241
|
+
super({
|
|
242
|
+
entropy,
|
|
243
|
+
loader: new HeldLoader(parts.modules ?? []),
|
|
244
|
+
custody: new EdgeCustody(storage, parts.given.env.NERVUR_SECRET, entropy),
|
|
245
|
+
memory: new EdgeMemory(storage),
|
|
246
|
+
faculties: [edgeTcp, webFaculty(edgeServe), webPush],
|
|
247
|
+
opens: [TCP_OPENING, WEB_OPENING],
|
|
248
|
+
});
|
|
249
|
+
this.given = parts.given;
|
|
250
|
+
}
|
|
251
|
+
stand(harbor) {
|
|
252
|
+
this.#harbor = harbor;
|
|
253
|
+
return Promise.resolve();
|
|
254
|
+
}
|
|
255
|
+
// One request to the object: a post, or a held line opened, where the
|
|
256
|
+
// web carrier listens.
|
|
257
|
+
answer(request) {
|
|
258
|
+
const listen = this.listening;
|
|
259
|
+
const harbor = this.#harbor;
|
|
260
|
+
if (!listen || !harbor)
|
|
261
|
+
return Promise.resolve(new Response(null, { status: 404 }));
|
|
262
|
+
if (request.headers.get('upgrade')?.toLowerCase() !== 'websocket')
|
|
263
|
+
return answerPost(harbor, listen, request);
|
|
264
|
+
const offered = (request.headers.get('sec-websocket-protocol') ?? '').split(',').map((p) => p.trim());
|
|
265
|
+
if (new URL(request.url).pathname !== listen.path || !offered.includes('quo'))
|
|
266
|
+
return Promise.resolve(new Response(null, { status: 400 }));
|
|
267
|
+
const Pair = globalThis.WebSocketPair;
|
|
268
|
+
const pair = new Pair();
|
|
269
|
+
const [client, server] = [pair[0], pair[1]];
|
|
270
|
+
server.accept();
|
|
271
|
+
const side = { send: (body) => server.send(new Uint8Array(body)), close: () => server.close() };
|
|
272
|
+
server.addEventListener('message', (event) => answerHeld(harbor, side, typeof event.data === 'string' ? event.data : new Uint8Array(event.data)));
|
|
273
|
+
return Promise.resolve(new Response(null, { status: 101, webSocket: client, headers: { 'sec-websocket-protocol': 'quo' } }));
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const isGiven = (value) => {
|
|
277
|
+
const given = value;
|
|
278
|
+
return typeof given?.state?.storage?.transaction === 'function' && typeof given.env === 'object';
|
|
279
|
+
};
|
|
280
|
+
// A harbor handed a Durable Object's state and environment.
|
|
281
|
+
export const edgeGround = {
|
|
282
|
+
name: EDGE,
|
|
283
|
+
fits: ({ given }) => isGiven(given),
|
|
284
|
+
terrain: ({ given, modules }) => new EdgeTerrain({ given: given, ...(modules ? { modules } : {}) }),
|
|
285
|
+
};
|
|
286
|
+
export const harborObject = (modules, stood) => class HarborObject {
|
|
287
|
+
#opening;
|
|
288
|
+
// The object's harbor, once it stands.
|
|
289
|
+
get harbor() {
|
|
290
|
+
return this.#opening;
|
|
291
|
+
}
|
|
292
|
+
constructor(state, env) {
|
|
293
|
+
addGround(edgeGround);
|
|
294
|
+
// Opened inside the object's block, so no request reaches it half
|
|
295
|
+
// open and its storage answers the opening.
|
|
296
|
+
this.#opening = state.blockConcurrencyWhile(async () => {
|
|
297
|
+
const harbor = await Harbor.open({ modules, given: { state, env } });
|
|
298
|
+
await stood?.(harbor);
|
|
299
|
+
return harbor;
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
async fetch(request) {
|
|
303
|
+
const harbor = await this.#opening;
|
|
304
|
+
return harbor.terrain.answer(request);
|
|
305
|
+
}
|
|
306
|
+
};
|
package/dist/edge.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './index.ts';
|
package/dist/edge.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// `nervur` as a Workers bundle reads it: the main entry, with the edge
|
|
3
|
+
// ground added, so `Harbor.open({ given: { state, env } })` in a Durable
|
|
4
|
+
// Object keeps a harbor in its storage.
|
|
5
|
+
import { edgeGround } from './edge/index.js';
|
|
6
|
+
import { addGround } from './harbor/index.js';
|
|
7
|
+
export * from './index.js';
|
|
8
|
+
addGround(edgeGround);
|
package/dist/folder/index.d.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
import { Custody, Memory, type Entropy
|
|
2
|
-
export
|
|
1
|
+
import { Custody, Memory, type Entropy } from '../contract/index.ts';
|
|
2
|
+
export type Keep = Record<string, string | null>;
|
|
3
3
|
export declare class FolderMemory extends Memory {
|
|
4
|
+
#private;
|
|
4
5
|
readonly root: string;
|
|
5
6
|
constructor(root: string);
|
|
6
|
-
|
|
7
|
+
read(place: string): Promise<Map<string, Uint8Array>>;
|
|
8
|
+
write(place: string, entries: ReadonlyMap<string, Uint8Array | null>): Promise<void>;
|
|
9
|
+
places(): Promise<string[]>;
|
|
10
|
+
forget(place: string): Promise<void>;
|
|
11
|
+
protected apply(dir: string, keep?: Keep): Promise<void>;
|
|
7
12
|
}
|
|
8
13
|
export declare class FolderCustody extends Custody {
|
|
9
14
|
#private;
|
|
10
15
|
readonly root: string;
|
|
11
16
|
constructor(root: string, entropy: Entropy);
|
|
12
|
-
seed(
|
|
17
|
+
seed(): Promise<Uint8Array>;
|
|
13
18
|
}
|
package/dist/folder/index.js
CHANGED
|
@@ -1,102 +1,99 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
-
// A harbor on a disk, for local proof: `nervur/folder`,
|
|
2
|
+
// A harbor on a disk, for local proof: `nervur/folder`, an entry that
|
|
3
3
|
// names a platform.
|
|
4
4
|
//
|
|
5
|
-
// <root
|
|
6
|
-
// <root>/<
|
|
7
|
-
// <root>/<
|
|
5
|
+
// <root>/seed the harbor's seed, as hex
|
|
6
|
+
// <root>/<place>/<entry> one sealed entry of the harbor's memory
|
|
7
|
+
// <root>/<place>/keep a keep of that place, until it is applied
|
|
8
8
|
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
9
|
+
// Places and entries are the hex names the kit chose, so no name reaches
|
|
10
|
+
// outside its folder, and the disk shows no ward's name. A file is written
|
|
11
|
+
// whole, beside itself and then renamed over.
|
|
12
|
+
//
|
|
13
|
+
// A keep is whole, by its own file: the whole keep is written under `keep`
|
|
14
|
+
// and renamed over, then its entries are written and removed one by one,
|
|
15
|
+
// then `keep` goes. A read applies a `keep` it finds first, and the same
|
|
16
|
+
// bytes written twice leave the same place, so a keep that a crash cut
|
|
17
|
+
// short finishes at the next read. A place read before its `keep` landed
|
|
18
|
+
// has none of it. `keep` is no hex name, so it is never an entry.
|
|
19
|
+
//
|
|
20
|
+
// The seed is a plain file: custody that seals it is a terrain's.
|
|
13
21
|
import { mkdir, readdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
14
22
|
import { join } from 'node:path';
|
|
15
23
|
import { hex, isHex, unhex } from '../crypto/index.js';
|
|
16
24
|
import { Custody, Memory } from '../contract/index.js';
|
|
17
|
-
import { HEAD_ROW } from '../ward/index.js';
|
|
18
|
-
const HEAD = 'ward.json';
|
|
19
|
-
const BEING = 'being.json';
|
|
20
|
-
export const folderName = (name) => encodeURIComponent(name).replaceAll('.', '%2E').replaceAll('*', '%2A') || '%';
|
|
21
|
-
const nameOf = (folder) => (folder === '%' ? '' : decodeURIComponent(folder));
|
|
22
25
|
const missing = (e) => e.code === 'ENOENT';
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (missing(e))
|
|
29
|
-
return undefined;
|
|
30
|
-
throw e;
|
|
31
|
-
}
|
|
26
|
+
const KEEP = 'keep';
|
|
27
|
+
const named = (name) => {
|
|
28
|
+
if (!isHex(name) || name === '')
|
|
29
|
+
throw new Error(`${name} is not a name the kit chose`);
|
|
30
|
+
return name;
|
|
32
31
|
};
|
|
33
|
-
const writeWhole = async (path,
|
|
34
|
-
await writeFile(`${path}.next`,
|
|
32
|
+
const writeWhole = async (path, bytes) => {
|
|
33
|
+
await writeFile(`${path}.next`, bytes, { mode: 0o600 });
|
|
35
34
|
await rename(`${path}.next`, path);
|
|
36
35
|
};
|
|
37
|
-
class FolderRows {
|
|
38
|
-
rows;
|
|
39
|
-
#dir;
|
|
40
|
-
#told = new Set();
|
|
41
|
-
#line = Promise.resolve(true);
|
|
42
|
-
constructor(dir, rows) {
|
|
43
|
-
this.#dir = dir;
|
|
44
|
-
this.rows = rows;
|
|
45
|
-
}
|
|
46
|
-
told(row) {
|
|
47
|
-
this.#told.add(row);
|
|
48
|
-
}
|
|
49
|
-
// Every row told since the last keep, written in order, one keep at a time.
|
|
50
|
-
kept() {
|
|
51
|
-
const rows = [...this.#told];
|
|
52
|
-
this.#told.clear();
|
|
53
|
-
this.#line = this.#line.then(() => this.#write(rows)).catch(() => false);
|
|
54
|
-
return this.#line;
|
|
55
|
-
}
|
|
56
|
-
async #write(told) {
|
|
57
|
-
await mkdir(this.#dir, { recursive: true, mode: 0o700 });
|
|
58
|
-
const beings = (this.rows.beings ?? {});
|
|
59
|
-
for (const row of told) {
|
|
60
|
-
if (row === HEAD_ROW) {
|
|
61
|
-
await writeWhole(join(this.#dir, HEAD), JSON.stringify(this.rows.ward ?? null));
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
const dir = join(this.#dir, folderName(row));
|
|
65
|
-
if (!Object.hasOwn(beings, row)) {
|
|
66
|
-
await rm(dir, { recursive: true, force: true });
|
|
67
|
-
continue;
|
|
68
|
-
}
|
|
69
|
-
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
70
|
-
await writeWhole(join(dir, BEING), JSON.stringify(beings[row]));
|
|
71
|
-
}
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
36
|
export class FolderMemory extends Memory {
|
|
76
37
|
root;
|
|
38
|
+
#line = Promise.resolve();
|
|
77
39
|
constructor(root) {
|
|
78
40
|
super();
|
|
79
41
|
this.root = root;
|
|
80
42
|
}
|
|
81
|
-
// The
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (!entry.isDirectory())
|
|
43
|
+
// The entries as the disk holds them now, a keep left unapplied first.
|
|
44
|
+
// A file that is not an entry, one still being written among them, is
|
|
45
|
+
// not read.
|
|
46
|
+
async read(place) {
|
|
47
|
+
const dir = join(this.root, named(place));
|
|
48
|
+
await this.apply(dir);
|
|
49
|
+
const out = new Map();
|
|
50
|
+
const files = await readdir(dir, { withFileTypes: true }).catch((e) => (missing(e) ? [] : Promise.reject(e)));
|
|
51
|
+
for (const file of files) {
|
|
52
|
+
if (!file.isFile() || !isHex(file.name) || file.name === '')
|
|
92
53
|
continue;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
54
|
+
out.set(file.name, new Uint8Array(await readFile(join(dir, file.name))));
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
// One keep at a time, written whole under `keep` before any entry moves.
|
|
59
|
+
async write(place, entries) {
|
|
60
|
+
const dir = join(this.root, named(place));
|
|
61
|
+
const keep = {};
|
|
62
|
+
for (const [name, bytes] of entries)
|
|
63
|
+
keep[named(name)] = bytes === null ? null : hex(bytes);
|
|
64
|
+
const kept = this.#line.then(async () => {
|
|
65
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
66
|
+
await writeWhole(join(dir, KEEP), JSON.stringify(keep));
|
|
67
|
+
await this.apply(dir, keep);
|
|
68
|
+
});
|
|
69
|
+
this.#line = kept.catch(() => undefined);
|
|
70
|
+
await kept;
|
|
71
|
+
}
|
|
72
|
+
// Every folder of the root that is a place.
|
|
73
|
+
async places() {
|
|
74
|
+
const files = await readdir(this.root, { withFileTypes: true }).catch((e) => (missing(e) ? [] : Promise.reject(e)));
|
|
75
|
+
return files.filter((f) => f.isDirectory() && isHex(f.name) && f.name !== '').map((f) => f.name);
|
|
76
|
+
}
|
|
77
|
+
forget(place) {
|
|
78
|
+
return rm(join(this.root, named(place)), { recursive: true, force: true });
|
|
79
|
+
}
|
|
80
|
+
// The keep a folder holds, applied and then gone. Writing the same bytes
|
|
81
|
+
// again leaves the same place, so applying twice is applying once.
|
|
82
|
+
async apply(dir, keep) {
|
|
83
|
+
const found = keep ?? (await readFile(join(dir, KEEP), 'utf8').then((text) => JSON.parse(text), () => null));
|
|
84
|
+
if (found === null)
|
|
85
|
+
return;
|
|
86
|
+
for (const [name, written] of Object.entries(found)) {
|
|
87
|
+
const path = join(dir, named(name));
|
|
88
|
+
if (written === null)
|
|
89
|
+
await rm(path, { force: true });
|
|
90
|
+
else
|
|
91
|
+
await writeWhole(path, unhex(written));
|
|
96
92
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
93
|
+
await rm(join(dir, KEEP), { force: true });
|
|
94
|
+
const left = await readdir(dir).catch((e) => (missing(e) ? [] : Promise.reject(e)));
|
|
95
|
+
if (left.length === 0)
|
|
96
|
+
await rm(dir, { recursive: true, force: true });
|
|
100
97
|
}
|
|
101
98
|
}
|
|
102
99
|
export class FolderCustody extends Custody {
|
|
@@ -107,9 +104,10 @@ export class FolderCustody extends Custody {
|
|
|
107
104
|
this.root = root;
|
|
108
105
|
this.#entropy = entropy;
|
|
109
106
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
107
|
+
// The seed the folder holds, or a drawn one written there the first time.
|
|
108
|
+
// Two first asks at once keep the one written first.
|
|
109
|
+
async seed() {
|
|
110
|
+
const path = join(this.root, 'seed');
|
|
113
111
|
const kept = await readFile(path, 'utf8').catch((e) => (missing(e) ? undefined : Promise.reject(e)));
|
|
114
112
|
if (kept !== undefined) {
|
|
115
113
|
const text = kept.trim();
|
|
@@ -118,8 +116,15 @@ export class FolderCustody extends Custody {
|
|
|
118
116
|
return unhex(text);
|
|
119
117
|
}
|
|
120
118
|
const seed = this.#entropy.draw(32);
|
|
121
|
-
await mkdir(
|
|
122
|
-
|
|
119
|
+
await mkdir(this.root, { recursive: true, mode: 0o700 });
|
|
120
|
+
try {
|
|
121
|
+
await writeFile(path, `${hex(seed)}\n`, { mode: 0o600, flag: 'wx' });
|
|
122
|
+
}
|
|
123
|
+
catch (e) {
|
|
124
|
+
if (e.code === 'EEXIST')
|
|
125
|
+
return this.seed();
|
|
126
|
+
throw e;
|
|
127
|
+
}
|
|
123
128
|
return seed;
|
|
124
129
|
}
|
|
125
130
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Faculty, type BeingClass } from '../being/index.ts';
|
|
2
|
+
export interface Ground {
|
|
3
|
+
arrive(pk: string, bytes: Uint8Array): Promise<Uint8Array | null>;
|
|
4
|
+
}
|
|
5
|
+
export type GroundFaculty = (ground: Ground) => BeingClass;
|
|
6
|
+
export declare const UNDIALED: unique symbol;
|
|
7
|
+
export type Dialed = Uint8Array | null | typeof UNDIALED;
|
|
8
|
+
export declare abstract class CarrierFaculty extends Faculty {
|
|
9
|
+
static readonly kind: string;
|
|
10
|
+
abstract accepts(address: string): boolean;
|
|
11
|
+
abstract dial(address: string, pk: string, bytes: Uint8Array): Promise<Dialed>;
|
|
12
|
+
listening(): string[];
|
|
13
|
+
relays(_pk: string): boolean;
|
|
14
|
+
relay(_pk: string, _bytes: Uint8Array): Promise<Uint8Array | null>;
|
|
15
|
+
awake(): Promise<void>;
|
|
16
|
+
sleep(): Promise<void>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// A carrier is a faculty. It speaks the schemes of the addresses it dials,
|
|
3
|
+
// dials an address when the harbor carries a box there, and listens where
|
|
4
|
+
// its cells say, handing each box it hears to the harbor. The harbor keeps
|
|
5
|
+
// every route and walks it in order, handing each address to the carrier
|
|
6
|
+
// that speaks its scheme.
|
|
7
|
+
//
|
|
8
|
+
// A ground ships its carriers as classes the terrain names, each made for
|
|
9
|
+
// one harbor, so the class holds the harbor it hands boxes to. They are
|
|
10
|
+
// the kit's, of kinds under `org.nervur.`, and the dock opens them as
|
|
11
|
+
// faculties, at genesis where the terrain says, and on the root's `open`.
|
|
12
|
+
import { Faculty } from '../being/index.js';
|
|
13
|
+
// Beside a reply and nothing, a dial answers `undialed` when the address
|
|
14
|
+
// could not be reached at all, so the harbor tries the next one. A box a
|
|
15
|
+
// line took may have been heard, so after it no other address is tried.
|
|
16
|
+
export const UNDIALED = Symbol('undialed');
|
|
17
|
+
export class CarrierFaculty extends Faculty {
|
|
18
|
+
static kind = 'org.nervur.carrier';
|
|
19
|
+
// The addresses it listens at now.
|
|
20
|
+
listening() {
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
// Whether it answers for a ward pk no door of this harbor holds, as a
|
|
24
|
+
// relay does, and its answer.
|
|
25
|
+
relays(_pk) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
relay(_pk, _bytes) {
|
|
29
|
+
return Promise.resolve(null);
|
|
30
|
+
}
|
|
31
|
+
// Once the harbor has unpacked, and once before it closes.
|
|
32
|
+
awake() {
|
|
33
|
+
return Promise.resolve();
|
|
34
|
+
}
|
|
35
|
+
sleep() {
|
|
36
|
+
return Promise.resolve();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Faculty, type AskSpec, type BeingClass, type JsonObject, type Stance } from '../being/index.ts';
|
|
2
|
+
import type { Loaded } from '../contract/index.ts';
|
|
3
|
+
export declare const CATALOGUE = "catalogue";
|
|
4
|
+
export type ModuleRow = {
|
|
5
|
+
version: string;
|
|
6
|
+
from: string;
|
|
7
|
+
kinds: string[];
|
|
8
|
+
};
|
|
9
|
+
export interface Cataloguing {
|
|
10
|
+
load(from: string): Promise<Loaded | null>;
|
|
11
|
+
check(modules: readonly Loaded[]): string | null;
|
|
12
|
+
run(modules: readonly Loaded[]): string | null;
|
|
13
|
+
stands(kinds: readonly string[]): boolean;
|
|
14
|
+
kindsOf(module: string): string[];
|
|
15
|
+
}
|
|
16
|
+
export declare class Catalogue extends Faculty {
|
|
17
|
+
#private;
|
|
18
|
+
static readonly kind: string;
|
|
19
|
+
static cells: JsonObject;
|
|
20
|
+
static asks: Record<string, AskSpec>;
|
|
21
|
+
constructor(stance: Stance, harbor: Cataloguing);
|
|
22
|
+
wake(): Promise<void>;
|
|
23
|
+
add(args: JsonObject): Promise<JsonObject>;
|
|
24
|
+
remove(args: JsonObject): JsonObject;
|
|
25
|
+
modules(): JsonObject;
|
|
26
|
+
}
|
|
27
|
+
export declare const catalogueFor: (harbor: Cataloguing) => BeingClass;
|