@volter/teams 0.1.38 → 0.1.40
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/channel.mjs +3 -2
- package/machine-core.mjs +29 -4
- package/package.json +1 -1
- package/server/machine-link.mjs +13 -1
package/channel.mjs
CHANGED
|
@@ -15,8 +15,9 @@
|
|
|
15
15
|
import { EventEmitter } from 'node:events';
|
|
16
16
|
|
|
17
17
|
const PROTOCOL = 'supercode-machine/1';
|
|
18
|
-
/** The door vocabulary version. It grows; the framing under PROTOCOL never changes.
|
|
19
|
-
|
|
18
|
+
/** The door vocabulary version. It grows; the framing under PROTOCOL never changes. 3: a `mail` turn carries a
|
|
19
|
+
* `turn_key`, and the machine types each key once however often it is opened. */
|
|
20
|
+
export const NODE_PROTOCOL = 3;
|
|
20
21
|
const MAX_FRAME = 8 * 1024 * 1024;
|
|
21
22
|
|
|
22
23
|
/** Reads u32-length-prefixed frames from a socket. */
|
package/machine-core.mjs
CHANGED
|
@@ -89,6 +89,19 @@ export class MachineCore extends EventEmitter {
|
|
|
89
89
|
ensureSocketDirectory(this.socketPath);
|
|
90
90
|
await clearStaleSocket(this.socketPath);
|
|
91
91
|
await this.beforeListen();
|
|
92
|
+
try { await this.#listen(); } catch (error) {
|
|
93
|
+
// What beforeListen started (a product's children) goes with the start that failed, or it outlives this daemon.
|
|
94
|
+
await this.afterStop().catch(() => {});
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
if (this.link) {
|
|
98
|
+
await this.refreshLinkConfiguration().catch((error) => this.emit('link', { event: 'configuration-failed', error: error.message }));
|
|
99
|
+
this.#connectLink();
|
|
100
|
+
}
|
|
101
|
+
return this.socketPath;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async #listen() {
|
|
92
105
|
this.server = createServer((socket) => this.#accept(socket));
|
|
93
106
|
// A Unix socket is bound under a name of this daemon's own and then renamed into place: Node unlinks the name it
|
|
94
107
|
// bound when the server closes, and that name must never be the shared path, which a later daemon may serve from a
|
|
@@ -109,15 +122,25 @@ export class MachineCore extends EventEmitter {
|
|
|
109
122
|
this.address = this.socketPath;
|
|
110
123
|
writeJson(this.p.machine, { pid: process.pid, id: this.id, socket: this.socketPath, startedAt: Date.now(), version: MACHINE_VERSION, protocol: NODE_PROTOCOL, floor: this.floor, server: this.link?.server ?? null });
|
|
111
124
|
this.emit('listening', this.socketPath);
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
125
|
+
// The socket check at start has a gap (a daemon restarting has no socket for a moment), so two daemons can each
|
|
126
|
+
// start; the later rename wins the path and the other serves nobody while its children (a mail watcher that types
|
|
127
|
+
// waiting turns among them) run on beside the winner's. A daemon whose path now names another daemon's socket
|
|
128
|
+
// says so once; a daemon run on its own stops on it.
|
|
129
|
+
if (!isNamedPipe(this.socketPath)) {
|
|
130
|
+
this.socketWatch = setInterval(() => {
|
|
131
|
+
let inode = null;
|
|
132
|
+
try { inode = statSync(this.socketPath).ino; } catch { return; }
|
|
133
|
+
if (inode === this.socketInode) return;
|
|
134
|
+
clearInterval(this.socketWatch); this.socketWatch = null;
|
|
135
|
+
this.emit('superseded', this.socketPath);
|
|
136
|
+
}, SOCKET_WATCH_MS);
|
|
137
|
+
this.socketWatch.unref?.();
|
|
115
138
|
}
|
|
116
|
-
return this.socketPath;
|
|
117
139
|
}
|
|
118
140
|
|
|
119
141
|
async stop() {
|
|
120
142
|
this.stopping = true;
|
|
143
|
+
if (this.socketWatch) { clearInterval(this.socketWatch); this.socketWatch = null; }
|
|
121
144
|
if (this.linkTimer) { clearTimeout(this.linkTimer); this.linkTimer = null; }
|
|
122
145
|
// A close frame, so the server sees a machine that left rather than one that vanished.
|
|
123
146
|
try { this.linkSocket?.close(1000, 'machine stopped'); } catch { this.linkSocket?.terminate(); }
|
|
@@ -408,6 +431,8 @@ export class MachineCore extends EventEmitter {
|
|
|
408
431
|
|
|
409
432
|
}
|
|
410
433
|
|
|
434
|
+
const SOCKET_WATCH_MS = 5_000;
|
|
435
|
+
|
|
411
436
|
async function clearStaleSocket(path) {
|
|
412
437
|
// A named pipe has no file to leave behind: only a live daemon holds its name.
|
|
413
438
|
if (!isNamedPipe(path) && !existsSync(path)) return;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@volter/teams",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.40",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Volter Teams, the neutral team plane: machine identity and daemon core, the local door and server link, admission from machine grants, product doors, and the Teams server's neutral routes (company RFC 0008).",
|
|
6
6
|
"license": "MIT",
|
package/server/machine-link.mjs
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
// A delegated caller (./delegated.mjs) holds no Teams credential: its verified token, when it names exactly a surface
|
|
15
15
|
// the machine's custodian published to that workplace (./publications.mjs), becomes one machine grant on that target
|
|
16
16
|
// and the caps the token names, expiring with the token; its channel closes then, or when the surface is unpublished.
|
|
17
|
+
import { randomUUID } from 'node:crypto';
|
|
17
18
|
import { Channel } from '../channel.mjs';
|
|
18
19
|
import { bearer } from './auth.mjs';
|
|
19
20
|
import { delegatedAccessOf, delegatedPrincipal, delegatedSessionOf, looksDelegated, surfaceCaps } from './delegated.mjs';
|
|
@@ -306,6 +307,8 @@ async function acceptLink(api, teamId, machineId, duplex, closeSocket) {
|
|
|
306
307
|
}
|
|
307
308
|
|
|
308
309
|
const LINK_WAIT_MS = 10_000;
|
|
310
|
+
/** How long a typed line is retried on a machine that is linked but too loaded to open its stream. */
|
|
311
|
+
const TURN_RETRY_MS = 120_000;
|
|
309
312
|
|
|
310
313
|
/** The machine's live link, waiting up to `ms` for one that is relinking. */
|
|
311
314
|
async function linkWithin(api, linkKey, ms) {
|
|
@@ -339,15 +342,24 @@ async function acceptChannel(api, auth, machineId, duplex, closeSocket) {
|
|
|
339
342
|
entry.claim = claim;
|
|
340
343
|
// A machine relinks within seconds of this server restarting (a deploy drops every link): a stream waits for its
|
|
341
344
|
// link that long, and retries once on a link that closed under it, rather than refusing a message mid-relink.
|
|
345
|
+
// A line typed into a session is a person's words: a machine too loaded to answer the open in time is waited on,
|
|
346
|
+
// not refused, and every attempt carries one key so the machine types the line once however many reach it (an
|
|
347
|
+
// open that timed out here can still be accepted there). A machine below vocabulary 3 does not know the key and
|
|
348
|
+
// keeps the single attempt.
|
|
349
|
+
const turn = stream.name === 'mail' && stream.params?.request?.op === 'turn';
|
|
350
|
+
const params = turn ? { ...stream.params, request: { turn_key: `tk_${randomUUID()}`, ...stream.params.request } } : (stream.params ?? {});
|
|
351
|
+
const turnDeadline = Date.now() + TURN_RETRY_MS;
|
|
342
352
|
let upstream;
|
|
343
353
|
for (let attempt = 0; ; attempt += 1) {
|
|
344
354
|
const link = await linkWithin(api, key(auth.team_id, machineId), LINK_WAIT_MS);
|
|
345
355
|
if (!link) return refuse('machine_offline: the machine is not linked to this server');
|
|
346
356
|
try {
|
|
347
|
-
upstream = await link.channel.open(stream.name, { ...
|
|
357
|
+
upstream = await link.channel.open(stream.name, { ...params, _admitted: claim });
|
|
348
358
|
break;
|
|
349
359
|
} catch (error) {
|
|
350
360
|
if (attempt === 0 && link.channel.closed) continue;
|
|
361
|
+
const keyed = turn && (link.channel.peer?.nv ?? 0) >= 3;
|
|
362
|
+
if (keyed && !stream.closed && Date.now() < turnDeadline && (link.channel.closed || /did not open within/.test(error.message))) continue;
|
|
351
363
|
return refuse(error.message.replace(/^[a-z]+ refused: /, ''));
|
|
352
364
|
}
|
|
353
365
|
}
|