@fnndsc/calypso 0.29.14 → 0.30.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/dist/calypso.js +19 -8
- package/dist/calypso.js.map +1 -1
- package/dist/daemon/attachFile.d.ts +26 -19
- package/dist/daemon/attachFile.js +42 -51
- package/dist/daemon/attachFile.js.map +1 -1
- package/dist/daemon/attachOrigin.d.ts +69 -0
- package/dist/daemon/attachOrigin.js +125 -0
- package/dist/daemon/attachOrigin.js.map +1 -0
- package/dist/daemon/berth.d.ts +265 -26
- package/dist/daemon/berth.js +521 -158
- package/dist/daemon/berth.js.map +1 -1
- package/dist/daemon/claims.js +19 -9
- package/dist/daemon/claims.js.map +1 -1
- package/dist/daemon/commandLane.d.ts +317 -0
- package/dist/daemon/commandLane.js +409 -0
- package/dist/daemon/commandLane.js.map +1 -0
- package/dist/daemon/console.d.ts +0 -2
- package/dist/daemon/console.js +7 -10
- package/dist/daemon/console.js.map +1 -1
- package/dist/daemon/consoleSession.js +4 -1
- package/dist/daemon/consoleSession.js.map +1 -1
- package/dist/daemon/elevation.d.ts +0 -3
- package/dist/daemon/elevation.js +1 -3
- package/dist/daemon/elevation.js.map +1 -1
- package/dist/daemon/face.d.ts +18 -4
- package/dist/daemon/face.js +77 -32
- package/dist/daemon/face.js.map +1 -1
- package/dist/daemon/hostControl.d.ts +114 -1
- package/dist/daemon/hostControl.js +104 -4
- package/dist/daemon/hostControl.js.map +1 -1
- package/dist/daemon/hostPty.d.ts +14 -0
- package/dist/daemon/hostPty.js +19 -1
- package/dist/daemon/hostPty.js.map +1 -1
- package/dist/daemon/hostPtyRelay.d.ts +118 -0
- package/dist/daemon/hostPtyRelay.js +186 -0
- package/dist/daemon/hostPtyRelay.js.map +1 -0
- package/dist/daemon/httpRoutes.d.ts +257 -0
- package/dist/daemon/httpRoutes.js +537 -0
- package/dist/daemon/httpRoutes.js.map +1 -0
- package/dist/daemon/instance.d.ts +37 -0
- package/dist/daemon/instance.js +59 -0
- package/dist/daemon/instance.js.map +1 -0
- package/dist/daemon/launch.d.ts +17 -0
- package/dist/daemon/launch.js +198 -194
- package/dist/daemon/launch.js.map +1 -1
- package/dist/daemon/launchBanner.d.ts +76 -0
- package/dist/daemon/launchBanner.js +80 -0
- package/dist/daemon/launchBanner.js.map +1 -0
- package/dist/daemon/rival.d.ts +65 -0
- package/dist/daemon/rival.js +67 -0
- package/dist/daemon/rival.js.map +1 -0
- package/dist/daemon/server.d.ts +104 -190
- package/dist/daemon/server.js +358 -866
- package/dist/daemon/server.js.map +1 -1
- package/dist/daemon/settings.d.ts +42 -0
- package/dist/daemon/settings.js +43 -0
- package/dist/daemon/settings.js.map +1 -0
- package/dist/daemon/surface.d.ts +103 -0
- package/dist/daemon/surface.js +71 -0
- package/dist/daemon/surface.js.map +1 -0
- package/dist/daemon/surfaceReplies.d.ts +109 -0
- package/dist/daemon/surfaceReplies.js +108 -0
- package/dist/daemon/surfaceReplies.js.map +1 -0
- package/dist/daemon/terminals.js +5 -2
- package/dist/daemon/terminals.js.map +1 -1
- package/dist/daemon/token.d.ts +41 -5
- package/dist/daemon/token.js +43 -12
- package/dist/daemon/token.js.map +1 -1
- package/dist/daemon/uploadHold.d.ts +85 -0
- package/dist/daemon/uploadHold.js +205 -0
- package/dist/daemon/uploadHold.js.map +1 -0
- package/dist/daemon/vfsHeaders.d.ts +11 -0
- package/dist/daemon/vfsHeaders.js +60 -0
- package/dist/daemon/vfsHeaders.js.map +1 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.js +3 -4
- package/dist/index.js.map +1 -1
- package/dist/notes.json +1 -1
- package/package.json +9 -4
- package/dist/daemon/discovery.d.ts +0 -23
- package/dist/daemon/discovery.js +0 -57
- package/dist/daemon/discovery.js.map +0 -1
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The `!` commands a daemon runs on host ptys of its own, relayed to the terminal surface that typed them.
|
|
3
|
+
*
|
|
4
|
+
* Under host control, a surface that is a terminal (it declared `tty`) has its `!` run on a pseudo-terminal of the
|
|
5
|
+
* daemon's, sized and typed as that terminal; the output goes raw to that surface alone (`hostPtyStart`,
|
|
6
|
+
* `hostPtyData`, `hostPtyExit`) and its keys and sizes come back (`hostPtyInput`, `hostPtyResize`). This module owns
|
|
7
|
+
* the programs running so, by id, and the spawn that starts them; it stops them when their surface goes and when
|
|
8
|
+
* the daemon stops.
|
|
9
|
+
*
|
|
10
|
+
* Key components:
|
|
11
|
+
* - HostPtyRelay: the running host ptys, their start, input, size, refusal and hang-up.
|
|
12
|
+
*
|
|
13
|
+
* Typical usage:
|
|
14
|
+
* ```ts
|
|
15
|
+
* const relay: HostPtyRelay = new HostPtyRelay(send, log);
|
|
16
|
+
* relay.spawn_set(await ptySpawn_load());
|
|
17
|
+
* const code: number | null = await relay.pty_run(origin, lineId, 'nvim notes.txt', signal);
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Dependencies: hostPty.ts (the program on its pty), node-pty's spawn as terminals.ts loads it, surface.ts.
|
|
21
|
+
*
|
|
22
|
+
* @module
|
|
23
|
+
*/
|
|
24
|
+
import type { ClientMessage, ServerMessage } from '@fnndsc/menu';
|
|
25
|
+
import { WebSocket } from 'ws';
|
|
26
|
+
import type { Surface } from './surface.js';
|
|
27
|
+
import type { PtySpawn } from './terminals.js';
|
|
28
|
+
/**
|
|
29
|
+
* The `!` commands running on the daemon's host ptys, by id.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const relay: HostPtyRelay = new HostPtyRelay((socket, message) => { socket.send(JSON.stringify(message)); }, console.error);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare class HostPtyRelay {
|
|
37
|
+
private readonly send;
|
|
38
|
+
private readonly log;
|
|
39
|
+
/** The `!` commands running on host ptys, by id, with the surface they draw on. */
|
|
40
|
+
private readonly ptys;
|
|
41
|
+
/** Starts host ptys for a terminal surface's `!` under host control; null when node-pty is not built here. */
|
|
42
|
+
private ptySpawn;
|
|
43
|
+
/**
|
|
44
|
+
* @param send - Sends a message down a surface's socket, when it is open.
|
|
45
|
+
* @param log - Writes a line of the daemon's own log.
|
|
46
|
+
*/
|
|
47
|
+
constructor(send: (socket: WebSocket, message: ServerMessage) => void, log: (line: string) => void);
|
|
48
|
+
/**
|
|
49
|
+
* Lets a terminal surface's `!` run on a pty of the daemon's own.
|
|
50
|
+
*
|
|
51
|
+
* @param spawn - node-pty's spawn, or null to run every `!` on pipes.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* relay.spawn_set(await ptySpawn_load());
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
spawn_set(spawn: PtySpawn | null): void;
|
|
59
|
+
/**
|
|
60
|
+
* Runs a host-shell command on a pty for the surface running the line, when it is a terminal. The caller has
|
|
61
|
+
* already decided the host runs `!` (the `shell` tier).
|
|
62
|
+
*
|
|
63
|
+
* @param origin - The surface of the running line, or null outside any.
|
|
64
|
+
* @param lineId - The surface's id for the running line, or null.
|
|
65
|
+
* @param command - The shell command without the leading `!`.
|
|
66
|
+
* @param signal - Stops the program when the line is cancelled.
|
|
67
|
+
* @returns The exit status, or null when the surface is not a terminal or
|
|
68
|
+
* this daemon has no ptys (the command then runs on pipes).
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const code: number | null = await relay.pty_run(origin, lineId, 'nvim notes.txt', signal);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
pty_run(origin: Surface | null, lineId: string | null, command: string, signal?: AbortSignal): Promise<number> | null;
|
|
76
|
+
/**
|
|
77
|
+
* Returns the held PTY for `ptyId` if it belongs to `attached`.
|
|
78
|
+
*
|
|
79
|
+
* @param attached - Surface that sent the message.
|
|
80
|
+
* @param ptyId - PTY correlation id.
|
|
81
|
+
* @returns The held PTY, or undefined when absent or owned by another surface.
|
|
82
|
+
*/
|
|
83
|
+
private ownedPty_get;
|
|
84
|
+
/**
|
|
85
|
+
* Takes a surface's `hostPty*` message: its keys, its refusal, its size. A message about a pty that is not the
|
|
86
|
+
* surface's own does nothing.
|
|
87
|
+
*
|
|
88
|
+
* @param attached - The surface that sent it.
|
|
89
|
+
* @param message - The message.
|
|
90
|
+
* @returns False when the message is not a `hostPty*` one.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* if (relay.message_take(attached, value)) return;
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
message_take(attached: Surface, message: ClientMessage): boolean;
|
|
98
|
+
/**
|
|
99
|
+
* A program drawing on a terminal that went away hears the hang-up.
|
|
100
|
+
*
|
|
101
|
+
* @param surface - The surface that went away, or null when it never attached.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* relay.surface_hangup(surface);
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
surface_hangup(surface: Surface | null): void;
|
|
109
|
+
/**
|
|
110
|
+
* Stops every program on a host pty: they go with the daemon.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* relay.ptys_stop();
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
ptys_stop(): void;
|
|
118
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The `!` commands a daemon runs on host ptys of its own, relayed to the terminal surface that typed them.
|
|
3
|
+
*
|
|
4
|
+
* Under host control, a surface that is a terminal (it declared `tty`) has its `!` run on a pseudo-terminal of the
|
|
5
|
+
* daemon's, sized and typed as that terminal; the output goes raw to that surface alone (`hostPtyStart`,
|
|
6
|
+
* `hostPtyData`, `hostPtyExit`) and its keys and sizes come back (`hostPtyInput`, `hostPtyResize`). This module owns
|
|
7
|
+
* the programs running so, by id, and the spawn that starts them; it stops them when their surface goes and when
|
|
8
|
+
* the daemon stops.
|
|
9
|
+
*
|
|
10
|
+
* Key components:
|
|
11
|
+
* - HostPtyRelay: the running host ptys, their start, input, size, refusal and hang-up.
|
|
12
|
+
*
|
|
13
|
+
* Typical usage:
|
|
14
|
+
* ```ts
|
|
15
|
+
* const relay: HostPtyRelay = new HostPtyRelay(send, log);
|
|
16
|
+
* relay.spawn_set(await ptySpawn_load());
|
|
17
|
+
* const code: number | null = await relay.pty_run(origin, lineId, 'nvim notes.txt', signal);
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Dependencies: hostPty.ts (the program on its pty), node-pty's spawn as terminals.ts loads it, surface.ts.
|
|
21
|
+
*
|
|
22
|
+
* @module
|
|
23
|
+
*/
|
|
24
|
+
import { randomBytes } from 'node:crypto';
|
|
25
|
+
import { WebSocket } from 'ws';
|
|
26
|
+
import { hostPty_start } from './hostPty.js';
|
|
27
|
+
/**
|
|
28
|
+
* The `!` commands running on the daemon's host ptys, by id.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const relay: HostPtyRelay = new HostPtyRelay((socket, message) => { socket.send(JSON.stringify(message)); }, console.error);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export class HostPtyRelay {
|
|
36
|
+
send;
|
|
37
|
+
log;
|
|
38
|
+
/** The `!` commands running on host ptys, by id, with the surface they draw on. */
|
|
39
|
+
ptys = new Map();
|
|
40
|
+
/** Starts host ptys for a terminal surface's `!` under host control; null when node-pty is not built here. */
|
|
41
|
+
ptySpawn = null;
|
|
42
|
+
/**
|
|
43
|
+
* @param send - Sends a message down a surface's socket, when it is open.
|
|
44
|
+
* @param log - Writes a line of the daemon's own log.
|
|
45
|
+
*/
|
|
46
|
+
constructor(send, log) {
|
|
47
|
+
this.send = send;
|
|
48
|
+
this.log = log;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Lets a terminal surface's `!` run on a pty of the daemon's own.
|
|
52
|
+
*
|
|
53
|
+
* @param spawn - node-pty's spawn, or null to run every `!` on pipes.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* relay.spawn_set(await ptySpawn_load());
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
spawn_set(spawn) {
|
|
61
|
+
this.ptySpawn = spawn;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Runs a host-shell command on a pty for the surface running the line, when it is a terminal. The caller has
|
|
65
|
+
* already decided the host runs `!` (the `shell` tier).
|
|
66
|
+
*
|
|
67
|
+
* @param origin - The surface of the running line, or null outside any.
|
|
68
|
+
* @param lineId - The surface's id for the running line, or null.
|
|
69
|
+
* @param command - The shell command without the leading `!`.
|
|
70
|
+
* @param signal - Stops the program when the line is cancelled.
|
|
71
|
+
* @returns The exit status, or null when the surface is not a terminal or
|
|
72
|
+
* this daemon has no ptys (the command then runs on pipes).
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const code: number | null = await relay.pty_run(origin, lineId, 'nvim notes.txt', signal);
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
pty_run(origin, lineId, command, signal) {
|
|
80
|
+
if (origin === null || origin.capabilities.tty === undefined || this.ptySpawn === null)
|
|
81
|
+
return null;
|
|
82
|
+
if (origin.socket.readyState !== WebSocket.OPEN)
|
|
83
|
+
return null;
|
|
84
|
+
const ptyId = randomBytes(8).toString('hex');
|
|
85
|
+
// The surface takes its terminal over to the program before any byte arrives, for its own line's `!` only.
|
|
86
|
+
this.send(origin.socket, { type: 'hostPtyStart', ptyId, command, ...(lineId === null ? {} : { lineId }) });
|
|
87
|
+
let run;
|
|
88
|
+
try {
|
|
89
|
+
run = hostPty_start(this.ptySpawn, command, {
|
|
90
|
+
terminal: origin.capabilities.tty,
|
|
91
|
+
cwd: process.cwd(),
|
|
92
|
+
env: process.env,
|
|
93
|
+
data: (data) => { this.send(origin.socket, { type: 'hostPtyData', ptyId, data }); },
|
|
94
|
+
...(signal !== undefined ? { signal } : {}),
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
this.send(origin.socket, { type: 'hostPtyExit', ptyId, code: 1 });
|
|
99
|
+
return Promise.reject(error instanceof Error ? error : new Error(String(error)));
|
|
100
|
+
}
|
|
101
|
+
this.ptys.set(ptyId, { surface: origin, run });
|
|
102
|
+
return run.done.then((code) => {
|
|
103
|
+
this.ptys.delete(ptyId);
|
|
104
|
+
this.send(origin.socket, { type: 'hostPtyExit', ptyId, code });
|
|
105
|
+
return code;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Returns the held PTY for `ptyId` if it belongs to `attached`.
|
|
110
|
+
*
|
|
111
|
+
* @param attached - Surface that sent the message.
|
|
112
|
+
* @param ptyId - PTY correlation id.
|
|
113
|
+
* @returns The held PTY, or undefined when absent or owned by another surface.
|
|
114
|
+
*/
|
|
115
|
+
ownedPty_get(attached, ptyId) {
|
|
116
|
+
if (ptyId === undefined)
|
|
117
|
+
return undefined;
|
|
118
|
+
const held = this.ptys.get(ptyId);
|
|
119
|
+
return held !== undefined && held.surface === attached ? held : undefined;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Takes a surface's `hostPty*` message: its keys, its refusal, its size. A message about a pty that is not the
|
|
123
|
+
* surface's own does nothing.
|
|
124
|
+
*
|
|
125
|
+
* @param attached - The surface that sent it.
|
|
126
|
+
* @param message - The message.
|
|
127
|
+
* @returns False when the message is not a `hostPty*` one.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* if (relay.message_take(attached, value)) return;
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
message_take(attached, message) {
|
|
135
|
+
if (message.type === 'hostPtyInput') {
|
|
136
|
+
this.ownedPty_get(attached, message.ptyId)?.run.write(message.data);
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
if (message.type === 'hostPtyRefuse') {
|
|
140
|
+
// The surface would not hand its terminal over: its line did not ask for this program. It is stopped, and
|
|
141
|
+
// the line settles on its exit.
|
|
142
|
+
const held = this.ownedPty_get(attached, message.ptyId);
|
|
143
|
+
if (held !== undefined) {
|
|
144
|
+
this.log(`[!] host pty refused by its surface: ${message.reason}`);
|
|
145
|
+
held.run.stop();
|
|
146
|
+
}
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
if (message.type === 'hostPtyResize') {
|
|
150
|
+
// The size a later `!` starts at, and the running one's, when it is this surface's.
|
|
151
|
+
if (attached.capabilities.tty !== undefined)
|
|
152
|
+
attached.capabilities.tty = { ...attached.capabilities.tty, cols: message.cols, rows: message.rows };
|
|
153
|
+
this.ownedPty_get(attached, message.ptyId)?.run.resize(message.cols, message.rows);
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* A program drawing on a terminal that went away hears the hang-up.
|
|
160
|
+
*
|
|
161
|
+
* @param surface - The surface that went away, or null when it never attached.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* relay.surface_hangup(surface);
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
surface_hangup(surface) {
|
|
169
|
+
for (const held of this.ptys.values())
|
|
170
|
+
if (held.surface === surface)
|
|
171
|
+
held.run.stop();
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Stops every program on a host pty: they go with the daemon.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* relay.ptys_stop();
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
ptys_stop() {
|
|
182
|
+
for (const held of this.ptys.values())
|
|
183
|
+
held.run.stop();
|
|
184
|
+
}
|
|
185
|
+
}
|
|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I,KAAK,eAAe,EAAE,CAAC;YACrC,oFAAoF;YACpF,IAAI,QAAQ,CAAC,YAAY,CAAC,GAAG,KAAK,SAAS;gBAAE,QAAQ,CAAC,YAAY,CAAC,GAAG,GAAG,EAAE,GAAG,QAAQ,CAAC,YAAY,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC;YAClJ,IAAI,CAAC,YAAY,CAAC,QAAQ,EAAE,OAAO,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;YACnF,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;;;;;;;OASG;IACI,cAAc,CAAC,OAAuB;QAC3C,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;YAAE,IAAI,IAAI,CAAC,OAAO,KAAK,OAAO;gBAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;IACvF,CAAC;IAED;;;;;;;OAOG;IACI,SAAS;QACd,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;YAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;IACzD,CAAC;CACF"}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The daemon's plain HTTP side: the file route, the session's facts, and the web surface it serves.
|
|
3
|
+
*
|
|
4
|
+
* One loopback port carries both the wire (WebSocket upgrades) and plain requests. The plain requests are routed
|
|
5
|
+
* here: `/vfs` serves one ChRIS file's bytes, streamed, and takes an upload; `/instance` proves which daemon
|
|
6
|
+
* answers; `/backend` and `/joblets` say what the session runs over and what runs in it; anything else is the web
|
|
7
|
+
* root's, or not found. Every route but `/instance` and the web root is gated by the attach token, compared in
|
|
8
|
+
* constant time, and a wrong one is not found. The downloads being served (for their progress and stop) and the
|
|
9
|
+
* bytes held for uploads in flight are this module's.
|
|
10
|
+
*
|
|
11
|
+
* Key components:
|
|
12
|
+
* - DaemonHttp: the routes, the download ledger and the upload hold.
|
|
13
|
+
* - VFS_POST_LIMIT: the largest file `POST /vfs` takes.
|
|
14
|
+
*
|
|
15
|
+
* Typical usage:
|
|
16
|
+
* ```ts
|
|
17
|
+
* const http: DaemonHttp = new DaemonHttp(engine, token, { webRoot, backend, uploadHeld }, log);
|
|
18
|
+
* createServer((request, response): void => { http.request_route(request, response); });
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* Dependencies: the engine's file seams (engine.ts), downloads.ts, uploadHold.ts, vfsHeaders.ts, vfsBreak.ts,
|
|
22
|
+
* static.ts, instance.ts, token.ts.
|
|
23
|
+
*
|
|
24
|
+
* @module
|
|
25
|
+
*/
|
|
26
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
27
|
+
import type { HostedEngine } from './engine.js';
|
|
28
|
+
import { type InstanceFacts } from './instance.js';
|
|
29
|
+
/**
|
|
30
|
+
* The largest file `POST /vfs` will take, in bytes.
|
|
31
|
+
*
|
|
32
|
+
* A cap rather than a stream to the store: the route exists so an operator
|
|
33
|
+
* can put a document, a manifest or an image into the folder they are
|
|
34
|
+
* looking at, and a surface able to post unbounded bytes to a loopback port
|
|
35
|
+
* is a surface able to fill the machine.
|
|
36
|
+
*/
|
|
37
|
+
export declare const VFS_POST_LIMIT: number;
|
|
38
|
+
/**
|
|
39
|
+
* What the HTTP side serves beyond the engine and the token.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const options: DaemonHttpOptions = { webRoot: '/opt/argus/dist', backend: 'chris', uploadHeld: 512 * 1024 * 1024 };
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export interface DaemonHttpOptions {
|
|
47
|
+
/** The built web surface, served for any other path; undefined, other paths are not found. */
|
|
48
|
+
webRoot: string | undefined;
|
|
49
|
+
/** The session's backend id, for `/backend`. */
|
|
50
|
+
backend: string | undefined;
|
|
51
|
+
/** The most bytes of uploads held at once. */
|
|
52
|
+
uploadHeld: number;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The daemon's plain HTTP routes.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* const http: DaemonHttp = new DaemonHttp(engine, token, { webRoot, backend: undefined, uploadHeld }, console.error);
|
|
60
|
+
* http.instance_set({ probeKey, identity, pid: process.pid, booted });
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare class DaemonHttp {
|
|
64
|
+
private readonly engine;
|
|
65
|
+
private readonly token;
|
|
66
|
+
private readonly log;
|
|
67
|
+
/** The downloads this daemon is serving, for their progress (downloads.ts). */
|
|
68
|
+
private readonly downloads;
|
|
69
|
+
/** The bytes this daemon holds for uploads in flight, under its ceiling (uploadHold.ts). */
|
|
70
|
+
private readonly uploads;
|
|
71
|
+
/** The facts this daemon proves itself by at `/instance`; null until its record is written. */
|
|
72
|
+
private instance;
|
|
73
|
+
/** The built web surface, when one is served. */
|
|
74
|
+
private readonly webRoot;
|
|
75
|
+
/** The session's backend id. */
|
|
76
|
+
private readonly backend;
|
|
77
|
+
/**
|
|
78
|
+
* @param engine - The engine whose files the routes read and write.
|
|
79
|
+
* @param token - The attach token every gated route asks for.
|
|
80
|
+
* @param options - The web root, the backend id and the upload ceiling.
|
|
81
|
+
* @param log - Writes a line of the daemon's own log.
|
|
82
|
+
*/
|
|
83
|
+
constructor(engine: HostedEngine, token: string, options: DaemonHttpOptions, log: (line: string) => void);
|
|
84
|
+
/**
|
|
85
|
+
* Sets the facts this daemon proves itself by at `/instance`, the ones its berth record holds.
|
|
86
|
+
*
|
|
87
|
+
* @param facts - The launch's probe key, identity, pid and boot time.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* http.instance_set({ probeKey, identity, pid: process.pid, booted });
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
instance_set(facts: InstanceFacts): void;
|
|
95
|
+
/**
|
|
96
|
+
* Answers one plain request by its path.
|
|
97
|
+
*
|
|
98
|
+
* @param request - The incoming HTTP request.
|
|
99
|
+
* @param response - The response to write.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* createServer((request, response): void => { http.request_route(request, response); });
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
request_route(request: IncomingMessage, response: ServerResponse): void;
|
|
107
|
+
/**
|
|
108
|
+
* Says which backend the session runs over, before a surface builds:
|
|
109
|
+
* `{"backend": "chris"}`. A surface chooses its composition from it, and
|
|
110
|
+
* the attach answer says the same once it attaches. The attach token gates
|
|
111
|
+
* it as it gates `/vfs` (a wrong one is not found).
|
|
112
|
+
*
|
|
113
|
+
* @param request - The incoming HTTP request.
|
|
114
|
+
* @param response - The response to write.
|
|
115
|
+
*/
|
|
116
|
+
private backend_serve;
|
|
117
|
+
/**
|
|
118
|
+
* Says which joblets run now (`line &`): `{"joblets": [{number, line,
|
|
119
|
+
* waiting}]}`. A host about to restart the session (porter, for a new
|
|
120
|
+
* release) asks first, since a restart ends them (#1273). Gated by the
|
|
121
|
+
* attach token, as `/backend` is.
|
|
122
|
+
*
|
|
123
|
+
* @param request - The incoming HTTP request.
|
|
124
|
+
* @param response - The response to write.
|
|
125
|
+
*/
|
|
126
|
+
private joblets_serve;
|
|
127
|
+
/**
|
|
128
|
+
* Writes a plain-text `/vfs` refusal response.
|
|
129
|
+
*
|
|
130
|
+
* @param response - HTTP response to write.
|
|
131
|
+
* @param status - HTTP status code.
|
|
132
|
+
* @param message - Plain-text refusal body.
|
|
133
|
+
*/
|
|
134
|
+
private vfs_refuse;
|
|
135
|
+
/**
|
|
136
|
+
* Answers a `/vfs?progress=...` or `/vfs?cancel=...` download status/stop request.
|
|
137
|
+
*
|
|
138
|
+
* @param asked - Requested download id.
|
|
139
|
+
* @param stopping - True when the request cancels the download.
|
|
140
|
+
* @param response - HTTP response to write.
|
|
141
|
+
*/
|
|
142
|
+
private vfsDownloadQuery_answer;
|
|
143
|
+
/**
|
|
144
|
+
* Reads a file whole through `read` and writes the HTTP response or refusal.
|
|
145
|
+
*
|
|
146
|
+
* @param read - Engine file reader.
|
|
147
|
+
* @param filePath - Path of the file to read.
|
|
148
|
+
* @param request - Incoming HTTP request.
|
|
149
|
+
* @param query - Parsed URL query parameters.
|
|
150
|
+
* @param response - HTTP response to write.
|
|
151
|
+
*/
|
|
152
|
+
private vfsBuffered_serve;
|
|
153
|
+
/**
|
|
154
|
+
* Serves a validated `GET`/`HEAD` file request via streaming or buffered read.
|
|
155
|
+
*
|
|
156
|
+
* @param request - Incoming HTTP request.
|
|
157
|
+
* @param query - Parsed URL query parameters.
|
|
158
|
+
* @param response - HTTP response to write.
|
|
159
|
+
* @param refuse - Refusal writer.
|
|
160
|
+
*/
|
|
161
|
+
private vfsFile_serve;
|
|
162
|
+
/**
|
|
163
|
+
* Serves one ChRIS file's bytes over HTTP: `GET /vfs?path=...&token=...`.
|
|
164
|
+
*
|
|
165
|
+
* The route exists so browser surfaces can render file content natively
|
|
166
|
+
* (an image in a panel) instead of through the terminal stream. It is
|
|
167
|
+
* gated by the same attach token as the wire (compared in constant time)
|
|
168
|
+
* and requires the hosted engine to provide `file_read`; refusals and
|
|
169
|
+
* failures answer 404 so the response does not confirm what exists.
|
|
170
|
+
*
|
|
171
|
+
* With `download=1` the same bytes come down as an attachment named for
|
|
172
|
+
* the file, which is how a browser is told to save rather than show: a
|
|
173
|
+
* surface's DOWNLOAD is this URL behind an anchor, so a file reaches the
|
|
174
|
+
* operator's disk the way every other download does.
|
|
175
|
+
*
|
|
176
|
+
* @param request - The incoming HTTP request.
|
|
177
|
+
* @param response - The response to write.
|
|
178
|
+
*/
|
|
179
|
+
private vfs_serve;
|
|
180
|
+
/**
|
|
181
|
+
* Serves one file's bytes as they arrive: the length in the header when
|
|
182
|
+
* the store gave one (so a browser draws its own progress), the body
|
|
183
|
+
* piped, nothing held whole. A 3.7 GB DICOM used to be read into one
|
|
184
|
+
* buffer behind a 30 s deadline and answered 404. A failure to open the
|
|
185
|
+
* file answers with the kernel's status and reason. The status waits for
|
|
186
|
+
* CUBE's first byte, so a file whose bytes never start is a 502 that says
|
|
187
|
+
* why. A break after bytes have gone cuts the socket, which the porter and
|
|
188
|
+
* the browser see as a body that stopped rather than a short file. Either
|
|
189
|
+
* break is one line in the daemon's log (vfsBreak.ts): the path, the bytes
|
|
190
|
+
* sent of the size CUBE stated, the time taken and the cause.
|
|
191
|
+
*
|
|
192
|
+
* A download with an id (`dl`) is counted for its progress; its probe
|
|
193
|
+
* (HEAD, `probe=1`) opens the file and answers the headers alone, holding the
|
|
194
|
+
* stream for the save that follows (downloads.ts).
|
|
195
|
+
*
|
|
196
|
+
* @param open - The engine's stream seam.
|
|
197
|
+
* @param filePath - The file asked for.
|
|
198
|
+
* @param query - The request's query (`download=1` names an attachment).
|
|
199
|
+
* @param response - The response to write.
|
|
200
|
+
* @param refuse - Answers a refusal before any byte is sent.
|
|
201
|
+
*/
|
|
202
|
+
private vfs_stream;
|
|
203
|
+
/**
|
|
204
|
+
* Answers a download's probe with the file's headers alone, its stream parked for the save that follows.
|
|
205
|
+
*
|
|
206
|
+
* @param dl - The download's id.
|
|
207
|
+
* @param opened - The opened file.
|
|
208
|
+
* @param headers - The file's headers.
|
|
209
|
+
* @param response - The response to write.
|
|
210
|
+
* @param refuse - Answers a refusal before any byte is sent.
|
|
211
|
+
*/
|
|
212
|
+
private vfsProbe_answer;
|
|
213
|
+
/**
|
|
214
|
+
* Answers a file the store would not open: with the kernel's status and reason when it gave them, else not found;
|
|
215
|
+
* a download with an id is marked failed.
|
|
216
|
+
*
|
|
217
|
+
* @param error - Why it would not open.
|
|
218
|
+
* @param dl - The download's id, or null.
|
|
219
|
+
* @param refuse - Answers a refusal before any byte is sent.
|
|
220
|
+
*/
|
|
221
|
+
private openFailure_answer;
|
|
222
|
+
/**
|
|
223
|
+
* Pipes an opened file's bytes to the response: the status waits for the first byte, and a break is logged and
|
|
224
|
+
* cuts the socket.
|
|
225
|
+
*
|
|
226
|
+
* @param opened - The opened file.
|
|
227
|
+
* @param headers - The file's headers, sent with its first byte.
|
|
228
|
+
* @param filePath - The file asked for.
|
|
229
|
+
* @param dl - The download's id, or null.
|
|
230
|
+
* @param startedAt - When the request was taken, from `Date.now()`.
|
|
231
|
+
* @param response - The response to write.
|
|
232
|
+
* @param refuse - Answers a refusal before any byte is sent.
|
|
233
|
+
*/
|
|
234
|
+
private vfsBody_pipe;
|
|
235
|
+
/**
|
|
236
|
+
* Takes one file's bytes from a surface: `POST /vfs?path=...&token=...`.
|
|
237
|
+
*
|
|
238
|
+
* A browser cannot reach the machine the daemon runs on, so `upload` is
|
|
239
|
+
* not a verb it can speak: the bytes travel here and the ENGINE puts them
|
|
240
|
+
* in the store, which keeps the write on the kernel's own path rather
|
|
241
|
+
* than letting a surface talk to CUBE.
|
|
242
|
+
*
|
|
243
|
+
* Gated by the same attach token as the read, and capped: a surface that
|
|
244
|
+
* can post unbounded bytes to a loopback port is a surface that can fill
|
|
245
|
+
* the machine.
|
|
246
|
+
*
|
|
247
|
+
* One request is capped, and so is what all requests in flight hold
|
|
248
|
+
* together (`uploadHold.ts`): the body's bytes are reserved before they
|
|
249
|
+
* are read and stay reserved until the store has them or has refused.
|
|
250
|
+
*
|
|
251
|
+
* @param request - The incoming request, whose body is the file.
|
|
252
|
+
* @param response - The response to write.
|
|
253
|
+
* @param query - The parsed query string (path and token).
|
|
254
|
+
* @param refuse - How this route says no.
|
|
255
|
+
*/
|
|
256
|
+
private vfs_receive;
|
|
257
|
+
}
|