@x-tech-laboratories/x-app 0.133.0 → 0.134.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/README.md +8 -6
- package/dist/config_watch.d.ts +1 -15
- package/dist/config_watch.js +2 -75
- package/dist/cron.d.ts +1 -34
- package/dist/cron.js +2 -187
- package/dist/log.d.ts +1 -30
- package/dist/log.js +2 -85
- package/dist/plugin_folder.d.ts +1 -24
- package/dist/plugin_folder.js +2 -107
- package/dist/plugins.d.ts +1 -138
- package/dist/plugins.js +2 -346
- package/dist/python_text.d.ts +1 -19
- package/dist/python_text.js +2 -363
- package/dist/server/agents/agent.d.ts +1 -27
- package/dist/server/agents/agent.js +2 -16
- package/dist/server/agents/connection.d.ts +1 -63
- package/dist/server/agents/connection.js +2 -266
- package/dist/server/agents/hub.d.ts +1 -110
- package/dist/server/agents/hub.js +2 -327
- package/dist/sql.d.ts +1 -27
- package/dist/sql.js +2 -61
- package/package.json +2 -4
|
@@ -1,327 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// that key, so a leaked token cannot stand in for it. Given a key of its own, the server signs its challenge to every
|
|
4
|
-
// agent, and every message to an agent that checked that signature.
|
|
5
|
-
import { EventEmitter } from "node:events";
|
|
6
|
-
import { agent_hello_text, fingerprint_of, is_nonce, new_nonce, parse_public_key, public_key_line, server_challenge_text, sign_text, verify_challenge, verify_text, } from "../../agent/keys.js";
|
|
7
|
-
import { clip_hello, encode_message, is_agent_message, key_host_header, parse_agent_message, } from "../../agent/protocol.js";
|
|
8
|
-
import { AgentError, ended_stream } from "./agent.js";
|
|
9
|
-
import { AgentConnection } from "./connection.js";
|
|
10
|
-
// What every refused admission answers, so a caller learns nothing of which hosts exist.
|
|
11
|
-
export const admission_refused = "This agent cannot log in here.";
|
|
12
|
-
// What an upgrade is answered when too many sockets are already waiting to log in.
|
|
13
|
-
export const too_many_pending_logins = "Too many agents are logging in; try again shortly.";
|
|
14
|
-
// A message before login may be at most this size, room for a hello listing every run it may carry.
|
|
15
|
-
export const pre_login_message_limit_in_bytes = 256 * 1024;
|
|
16
|
-
// Close codes the hub hangs up with.
|
|
17
|
-
export const close_codes = {
|
|
18
|
-
protocol: 4002,
|
|
19
|
-
replaced: 4003,
|
|
20
|
-
revoked: 4004,
|
|
21
|
-
no_hello: 4005,
|
|
22
|
-
bad_key: 4006,
|
|
23
|
-
// The first message was not a hello.
|
|
24
|
-
not_hello: 4007,
|
|
25
|
-
// A message before login was larger than a hello can be.
|
|
26
|
-
too_large: 1009,
|
|
27
|
-
// A hello asked for signed messages from a server with no key, or with a malformed nonce.
|
|
28
|
-
bad_agent_nonce: 4010,
|
|
29
|
-
};
|
|
30
|
-
const default_connection = {
|
|
31
|
-
window_in_bytes: 1024 * 1024,
|
|
32
|
-
request_timeout_in_milliseconds: 60_000,
|
|
33
|
-
heartbeat_in_milliseconds: 15_000,
|
|
34
|
-
};
|
|
35
|
-
// ── Hub ──────────────────────────────────────────────────────────────────────
|
|
36
|
-
export class AgentHub extends EventEmitter {
|
|
37
|
-
app;
|
|
38
|
-
local;
|
|
39
|
-
tokens;
|
|
40
|
-
on_message;
|
|
41
|
-
hello_timeout;
|
|
42
|
-
most_pending_logins;
|
|
43
|
-
most_pending_logins_per_address;
|
|
44
|
-
// How many sockets are waiting to log in, in all and by address.
|
|
45
|
-
pending_logins = 0;
|
|
46
|
-
pending_logins_by_address = new Map();
|
|
47
|
-
connection_options;
|
|
48
|
-
connections = new Map();
|
|
49
|
-
logins = new WeakMap();
|
|
50
|
-
// Each host's declared public key, with the line it was read from.
|
|
51
|
-
agent_keys = new Map();
|
|
52
|
-
server_key;
|
|
53
|
-
constructor(options) {
|
|
54
|
-
super();
|
|
55
|
-
this.app = options.app;
|
|
56
|
-
this.local = options.local === undefined ? null : { ...options.local };
|
|
57
|
-
this.tokens = options.tokens ?? null;
|
|
58
|
-
this.on_message = options.on_message ?? null;
|
|
59
|
-
this.hello_timeout = options.hello_timeout_in_milliseconds ?? 10_000;
|
|
60
|
-
this.most_pending_logins = options.most_pending_logins ?? 64;
|
|
61
|
-
this.most_pending_logins_per_address = options.most_pending_logins_per_address ?? 8;
|
|
62
|
-
this.connection_options = { ...default_connection, ...options.connection };
|
|
63
|
-
this.server_key = options.server_key ? { key: options.server_key, fingerprint: fingerprint_of(options.server_key) } : null;
|
|
64
|
-
}
|
|
65
|
-
// The OpenSSH public line of the server's key, for each agent's `server_keys`, or null when it has none.
|
|
66
|
-
server_public_line(comment = "") {
|
|
67
|
-
return this.server_key === null ? null : public_key_line(this.server_key.key, comment);
|
|
68
|
-
}
|
|
69
|
-
// The SHA256 fingerprint of the server's key, or null when it has none.
|
|
70
|
-
server_key_fingerprint() {
|
|
71
|
-
return this.server_key?.fingerprint ?? null;
|
|
72
|
-
}
|
|
73
|
-
// The host the built-in agent answers for, which follows the config as it names this server's own host.
|
|
74
|
-
set_local_host(host) {
|
|
75
|
-
if (this.local !== null)
|
|
76
|
-
this.local.host = host;
|
|
77
|
-
}
|
|
78
|
-
// ── Enrolment ────────────────────────────────────────────────────────────
|
|
79
|
-
// Takes each host's declared OpenSSH public key line, and hangs up on an agent whose host's key is new, changed or
|
|
80
|
-
// gone, so it logs in again the way the config now asks.
|
|
81
|
-
set_agent_keys(lines) {
|
|
82
|
-
const next = new Map([...lines].map(([host, line]) => [host, { line, key: parse_public_key(line) }]));
|
|
83
|
-
for (const host of new Set([...this.agent_keys.keys(), ...next.keys()])) {
|
|
84
|
-
if (this.agent_keys.get(host)?.line === next.get(host)?.line)
|
|
85
|
-
continue;
|
|
86
|
-
this.disconnect(host, close_codes.revoked, "The host's declared agent key changed.");
|
|
87
|
-
}
|
|
88
|
-
this.agent_keys = next;
|
|
89
|
-
}
|
|
90
|
-
has_agent_key(host) {
|
|
91
|
-
return this.agent_keys.has(host);
|
|
92
|
-
}
|
|
93
|
-
// The hosts with a declared key, in order.
|
|
94
|
-
declared_hosts() {
|
|
95
|
-
return [...this.agent_keys.keys()].sort();
|
|
96
|
-
}
|
|
97
|
-
// Hangs up on a host's agent that logged in with a token, as when the app replaces or revokes that token.
|
|
98
|
-
disconnect_token_login(host, reason) {
|
|
99
|
-
const connection = this.connections.get(host);
|
|
100
|
-
if (connection !== undefined && this.logins.get(connection) === "token")
|
|
101
|
-
connection.close(close_codes.revoked, reason);
|
|
102
|
-
}
|
|
103
|
-
// The host an Authorization header's bearer token belongs to.
|
|
104
|
-
authenticate(authorization) {
|
|
105
|
-
const match = /^Bearer\s+(\S+)$/.exec(authorization ?? "");
|
|
106
|
-
return match === null || this.tokens === null ? null : this.tokens.host_for(match[1]);
|
|
107
|
-
}
|
|
108
|
-
// Who an agent's upgrade request says it is: the host it names to log in with that host's key, or a token's host.
|
|
109
|
-
admit(headers) {
|
|
110
|
-
const header = key_host_header(this.app.name);
|
|
111
|
-
const named = headers[header];
|
|
112
|
-
if (typeof named === "string" && named !== "") {
|
|
113
|
-
if (named === this.local?.host)
|
|
114
|
-
return refused("The key login named the server's own host, which needs no agent.");
|
|
115
|
-
if (!this.agent_keys.has(named))
|
|
116
|
-
return refused("The key login named a host with no declared agent key.");
|
|
117
|
-
return { host: named, login: "key" };
|
|
118
|
-
}
|
|
119
|
-
if (this.tokens === null)
|
|
120
|
-
return refused(`The upgrade named no host in the ${header} header.`);
|
|
121
|
-
const host = this.authenticate(headers.authorization);
|
|
122
|
-
if (host === null)
|
|
123
|
-
return refused("The agent token is not enrolled.");
|
|
124
|
-
if (this.agent_keys.has(host))
|
|
125
|
-
return refused(`The token belongs to ${host}, which has a declared agent key and logs in with that key.`);
|
|
126
|
-
return { host, login: "token" };
|
|
127
|
-
}
|
|
128
|
-
// Holds a place for a socket that has not logged in yet, or gives null when every place, or every one for its address, is taken; the release may be called more than once.
|
|
129
|
-
hold_pending_login(address) {
|
|
130
|
-
const from_address = this.pending_logins_by_address.get(address) ?? 0;
|
|
131
|
-
if (this.pending_logins >= this.most_pending_logins || from_address >= this.most_pending_logins_per_address)
|
|
132
|
-
return null;
|
|
133
|
-
this.pending_logins++;
|
|
134
|
-
this.pending_logins_by_address.set(address, from_address + 1);
|
|
135
|
-
let held = true;
|
|
136
|
-
return () => {
|
|
137
|
-
if (!held)
|
|
138
|
-
return;
|
|
139
|
-
held = false;
|
|
140
|
-
this.pending_logins--;
|
|
141
|
-
const left = (this.pending_logins_by_address.get(address) ?? 1) - 1;
|
|
142
|
-
if (left > 0)
|
|
143
|
-
this.pending_logins_by_address.set(address, left);
|
|
144
|
-
else
|
|
145
|
-
this.pending_logins_by_address.delete(address);
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
// ── Connections ──────────────────────────────────────────────────────────
|
|
149
|
-
// Takes a socket whose upgrade named the host and waits for its hello, closing it on anything else; a key login is sent a nonce first, which its hello must carry signed, and a server with a key sends every login its signed challenge.
|
|
150
|
-
accept(socket, host, login, hooks = {}) {
|
|
151
|
-
let connection = null;
|
|
152
|
-
let turned_away = false;
|
|
153
|
-
const turn_away = (code, reason) => {
|
|
154
|
-
turned_away = true;
|
|
155
|
-
socket.close(code, reason);
|
|
156
|
-
};
|
|
157
|
-
const hello_timer = setTimeout(() => socket.close(close_codes.no_hello, "No hello came."), this.hello_timeout);
|
|
158
|
-
const nonce = login === "key" || this.server_key !== null ? new_nonce() : null;
|
|
159
|
-
if (nonce !== null)
|
|
160
|
-
socket.send(encode_message(this.challenge(host, nonce), this.app));
|
|
161
|
-
socket.on("message", (raw, is_binary) => {
|
|
162
|
-
const data = as_buffer(raw);
|
|
163
|
-
if (connection !== null) {
|
|
164
|
-
connection.receive(data, is_binary);
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
if (turned_away)
|
|
168
|
-
return;
|
|
169
|
-
clearTimeout(hello_timer);
|
|
170
|
-
if (data.length > pre_login_message_limit_in_bytes) {
|
|
171
|
-
turn_away(close_codes.too_large, "The message before login was too large.");
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
const parsed = is_binary ? null : parse_agent_message(data.toString("utf8"), this.app);
|
|
175
|
-
if (parsed === null || !is_agent_message(parsed) || parsed.type !== "hello") {
|
|
176
|
-
turn_away(close_codes.not_hello, "The first message was not a hello.");
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
const hello = clip_hello(parsed);
|
|
180
|
-
if (hello.protocol !== this.app.protocol_version) {
|
|
181
|
-
turn_away(close_codes.protocol, `This server speaks agent protocol ${this.app.protocol_version}, not ${hello.protocol}.`);
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
const { signature, agent_nonce, ...greeting } = hello;
|
|
185
|
-
let signing = null;
|
|
186
|
-
if (agent_nonce !== undefined) {
|
|
187
|
-
if (this.server_key === null || nonce === null || !is_nonce(agent_nonce)) {
|
|
188
|
-
turn_away(close_codes.bad_agent_nonce, "The hello asked for signed messages this server cannot give.");
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
signing = { server_key: this.server_key.key, server_nonce: nonce, agent_nonce };
|
|
192
|
-
}
|
|
193
|
-
if (login === "key" && !this.proves_key(host, nonce, signature, signing)) {
|
|
194
|
-
turn_away(close_codes.bad_key, `The agent's key is not the one declared for ${host}.`);
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
this.connections.get(host)?.close(close_codes.replaced, "A newer connection from the same host replaced this one.");
|
|
198
|
-
const current = new AgentConnection(socket, this.app, host, greeting, this.connection_options, signing);
|
|
199
|
-
current.on_message = (message) => this.on_message?.(host, message, current);
|
|
200
|
-
connection = current;
|
|
201
|
-
this.connections.set(host, current);
|
|
202
|
-
this.logins.set(current, login);
|
|
203
|
-
hooks.on_login?.();
|
|
204
|
-
this.emit("connect", this.status_of(current));
|
|
205
|
-
});
|
|
206
|
-
socket.on("error", () => { });
|
|
207
|
-
socket.on("close", () => {
|
|
208
|
-
clearTimeout(hello_timer);
|
|
209
|
-
if (connection === null)
|
|
210
|
-
return;
|
|
211
|
-
connection.closed(`The agent on ${host} disconnected.`);
|
|
212
|
-
if (this.logins.get(connection) === "token")
|
|
213
|
-
this.tokens?.seen?.(host);
|
|
214
|
-
if (this.connections.get(host) !== connection)
|
|
215
|
-
return;
|
|
216
|
-
this.connections.delete(host);
|
|
217
|
-
this.emit("disconnect", host);
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
// The challenge for a login, signed with the server's key when it has one.
|
|
221
|
-
challenge(host, nonce) {
|
|
222
|
-
if (this.server_key === null)
|
|
223
|
-
return { type: "challenge", nonce };
|
|
224
|
-
const { key, fingerprint } = this.server_key;
|
|
225
|
-
return { type: "challenge", nonce, server_key: fingerprint, server_signature: sign_text(key, server_challenge_text(this.app.name, host, nonce, fingerprint)) };
|
|
226
|
-
}
|
|
227
|
-
// Whether the hello's signature proves the host's declared key: over the bound hello text for an agent that checked the server, otherwise over the plain challenge.
|
|
228
|
-
proves_key(host, nonce, signature, signing) {
|
|
229
|
-
const declared = this.agent_keys.get(host);
|
|
230
|
-
if (declared === undefined || typeof signature !== "string")
|
|
231
|
-
return false;
|
|
232
|
-
if (signing === null)
|
|
233
|
-
return verify_challenge(declared.key, this.app.name, host, nonce, signature);
|
|
234
|
-
return verify_text(declared.key, agent_hello_text(this.app.name, host, nonce, signing.agent_nonce, this.server_key.fingerprint), signature);
|
|
235
|
-
}
|
|
236
|
-
disconnect(host, code = 1000, reason = "The server hung up.") {
|
|
237
|
-
this.connections.get(host)?.close(code, reason);
|
|
238
|
-
}
|
|
239
|
-
close() {
|
|
240
|
-
for (const host of this.connections.keys())
|
|
241
|
-
this.disconnect(host, 1001, "The server is stopping.");
|
|
242
|
-
}
|
|
243
|
-
is_connected(host) {
|
|
244
|
-
return this.local?.host === host || this.connections.get(host)?.is_open === true;
|
|
245
|
-
}
|
|
246
|
-
status(host) {
|
|
247
|
-
if (this.local?.host === host)
|
|
248
|
-
return local_status(host);
|
|
249
|
-
const connection = this.connections.get(host);
|
|
250
|
-
return connection?.is_open ? this.status_of(connection) : null;
|
|
251
|
-
}
|
|
252
|
-
// The built-in agent, then every connected one.
|
|
253
|
-
statuses() {
|
|
254
|
-
const remote = [...this.connections.values()].filter((connection) => connection.is_open).map((connection) => this.status_of(connection));
|
|
255
|
-
return this.local === null ? remote : [local_status(this.local.host), ...remote];
|
|
256
|
-
}
|
|
257
|
-
// The host's agent, or null when it is not connected.
|
|
258
|
-
agent(host) {
|
|
259
|
-
if (this.local?.host === host)
|
|
260
|
-
return this.local.agent;
|
|
261
|
-
const connection = this.connections.get(host);
|
|
262
|
-
return connection?.is_open ? connection : null;
|
|
263
|
-
}
|
|
264
|
-
// ── Commands ─────────────────────────────────────────────────────────────
|
|
265
|
-
// A run on a host with no agent answers with exit code -1 rather than rejecting, as a command that failed does.
|
|
266
|
-
run(host, task, options) {
|
|
267
|
-
const agent = this.agent(host);
|
|
268
|
-
return agent === null ? Promise.resolve({ exit_code: -1, stdout: "", stderr: not_connected(host) }) : agent.run(task, options);
|
|
269
|
-
}
|
|
270
|
-
stream(host, task, options = {}) {
|
|
271
|
-
return this.agent(host)?.stream(task, options) ?? ended_stream(not_connected(host));
|
|
272
|
-
}
|
|
273
|
-
start(host, run_id, task, options = {}) {
|
|
274
|
-
return this.require(host).then((agent) => agent.start(run_id, task, options));
|
|
275
|
-
}
|
|
276
|
-
poll(host, run_id, offset) {
|
|
277
|
-
return this.require(host).then((agent) => agent.poll(run_id, offset));
|
|
278
|
-
}
|
|
279
|
-
attach(host, run_id, offset) {
|
|
280
|
-
return this.agent(host)?.attach(run_id, offset) ?? ended_stream(not_connected(host));
|
|
281
|
-
}
|
|
282
|
-
stop(host, run_id) {
|
|
283
|
-
return this.require(host).then((agent) => agent.stop(run_id));
|
|
284
|
-
}
|
|
285
|
-
forget(host, run_id) {
|
|
286
|
-
return this.require(host).then((agent) => agent.forget(run_id));
|
|
287
|
-
}
|
|
288
|
-
runs(host) {
|
|
289
|
-
return this.require(host).then((agent) => agent.runs());
|
|
290
|
-
}
|
|
291
|
-
request(host, type, body = {}) {
|
|
292
|
-
return this.require(host).then((agent) => agent.request(type, body));
|
|
293
|
-
}
|
|
294
|
-
async require(host) {
|
|
295
|
-
const agent = this.agent(host);
|
|
296
|
-
if (agent === null)
|
|
297
|
-
throw new AgentError(not_connected(host), true);
|
|
298
|
-
return agent;
|
|
299
|
-
}
|
|
300
|
-
status_of(connection) {
|
|
301
|
-
return {
|
|
302
|
-
host: connection.host,
|
|
303
|
-
reported_host: connection.hello.host,
|
|
304
|
-
agent_version: connection.hello.agent_version,
|
|
305
|
-
connected_epoch: connection.connected_epoch,
|
|
306
|
-
built_in: false,
|
|
307
|
-
login: this.logins.get(connection) ?? null,
|
|
308
|
-
signed_messages: connection.signs_messages,
|
|
309
|
-
runs: connection.hello.runs,
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
314
|
-
function refused(reason) {
|
|
315
|
-
return { error: admission_refused, reason };
|
|
316
|
-
}
|
|
317
|
-
function not_connected(host) {
|
|
318
|
-
return `No agent is connected for ${host}.`;
|
|
319
|
-
}
|
|
320
|
-
function local_status(host) {
|
|
321
|
-
return { host, reported_host: host, agent_version: "built in", connected_epoch: 0, built_in: true, login: null, signed_messages: false, runs: [] };
|
|
322
|
-
}
|
|
323
|
-
function as_buffer(raw) {
|
|
324
|
-
if (Array.isArray(raw))
|
|
325
|
-
return Buffer.concat(raw);
|
|
326
|
-
return Buffer.isBuffer(raw) ? raw : Buffer.from(raw);
|
|
327
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, so an engine on a host can hold agent connections without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/server/agents/hub";
|
package/dist/sql.d.ts
CHANGED
|
@@ -1,27 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
export interface SqlStatement {
|
|
3
|
-
run: (...params: SqlValue[]) => {
|
|
4
|
-
changes: number | bigint;
|
|
5
|
-
lastInsertRowid: number | bigint;
|
|
6
|
-
};
|
|
7
|
-
get: (...params: SqlValue[]) => unknown;
|
|
8
|
-
all: (...params: SqlValue[]) => unknown[];
|
|
9
|
-
setReturnArrays?: (on: boolean) => void;
|
|
10
|
-
setReadBigInts?: (on: boolean) => void;
|
|
11
|
-
}
|
|
12
|
-
export interface SqlDatabase {
|
|
13
|
-
exec: (sql: string) => void;
|
|
14
|
-
prepare: (sql: string) => SqlStatement;
|
|
15
|
-
close: () => void;
|
|
16
|
-
}
|
|
17
|
-
export declare function in_transaction<Result>(database: SqlDatabase, work: () => Result): Result;
|
|
18
|
-
export type MigrationStep = string | readonly string[] | ((database: SqlDatabase) => void);
|
|
19
|
-
export interface MigrateOptions {
|
|
20
|
-
owner?: string;
|
|
21
|
-
}
|
|
22
|
-
export declare function migrate(database: SqlDatabase, steps: readonly MigrationStep[], options?: MigrateOptions): void;
|
|
23
|
-
export declare const tidy_every_ms: number;
|
|
24
|
-
export interface TidyOptions {
|
|
25
|
-
free_share?: number;
|
|
26
|
-
}
|
|
27
|
-
export declare function tidy_database(database: SqlDatabase, options?: TidyOptions): boolean;
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/sql";
|
package/dist/sql.js
CHANGED
|
@@ -1,61 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// ── Transactions ─────────────────────────────────────────────────────────────
|
|
4
|
-
// Runs the work as one transaction, rolled back if it throws so the connection is never left mid-transaction.
|
|
5
|
-
export function in_transaction(database, work) {
|
|
6
|
-
database.exec("BEGIN");
|
|
7
|
-
try {
|
|
8
|
-
const result = work();
|
|
9
|
-
database.exec("COMMIT");
|
|
10
|
-
return result;
|
|
11
|
-
}
|
|
12
|
-
catch (error) {
|
|
13
|
-
database.exec("ROLLBACK");
|
|
14
|
-
throw error;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
function run_step(database, step) {
|
|
18
|
-
if (typeof step === "function")
|
|
19
|
-
return step(database);
|
|
20
|
-
for (const statement of typeof step === "string" ? [step] : step)
|
|
21
|
-
database.exec(statement);
|
|
22
|
-
}
|
|
23
|
-
// Brings the database up to the last step, each step once and in its own transaction; without an owner the version is SQLite's user_version.
|
|
24
|
-
export function migrate(database, steps, options = {}) {
|
|
25
|
-
const { owner } = options;
|
|
26
|
-
if (owner !== undefined)
|
|
27
|
-
database.exec("CREATE TABLE IF NOT EXISTS schema_versions (owner TEXT PRIMARY KEY, version INTEGER NOT NULL)");
|
|
28
|
-
const read_version = () => owner === undefined
|
|
29
|
-
? database.prepare("PRAGMA user_version").get().user_version
|
|
30
|
-
: (database.prepare("SELECT version FROM schema_versions WHERE owner = ?").get(owner)?.version ?? 0);
|
|
31
|
-
for (let version = read_version(); version < steps.length; version++) {
|
|
32
|
-
in_transaction(database, () => {
|
|
33
|
-
run_step(database, steps[version]);
|
|
34
|
-
if (owner === undefined)
|
|
35
|
-
database.exec(`PRAGMA user_version = ${version + 1}`);
|
|
36
|
-
else
|
|
37
|
-
database.prepare("INSERT INTO schema_versions (owner, version) VALUES (?, ?) ON CONFLICT (owner) DO UPDATE SET version = excluded.version").run(owner, version + 1);
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
// ── Upkeep ───────────────────────────────────────────────────────────────────
|
|
42
|
-
// How often an app keeps its open database tidy.
|
|
43
|
-
export const tidy_every_ms = 24 * 60 * 60 * 1000;
|
|
44
|
-
// Refreshes the query planner's statistics, and rewrites the file without its empty pages once enough of it is empty;
|
|
45
|
-
// says whether it rewrote it. A rewrite held up by another connection's write is left for next time.
|
|
46
|
-
export function tidy_database(database, options = {}) {
|
|
47
|
-
database.exec("PRAGMA optimize");
|
|
48
|
-
const { page_count } = database.prepare("PRAGMA page_count").get();
|
|
49
|
-
const { freelist_count } = database.prepare("PRAGMA freelist_count").get();
|
|
50
|
-
if (page_count === 0 || freelist_count / page_count < (options.free_share ?? 0.1))
|
|
51
|
-
return false;
|
|
52
|
-
try {
|
|
53
|
-
database.exec("VACUUM");
|
|
54
|
-
}
|
|
55
|
-
catch {
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
// The rewrite passes through the write-ahead log, which is emptied so it does not keep the old size on disk.
|
|
59
|
-
database.exec("PRAGMA wal_checkpoint(TRUNCATE)");
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, which a host agent installs without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/sql";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-tech-laboratories/x-app",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.134.0",
|
|
4
4
|
"description": "Code the X-apps share: agents and their long runs, passwords and sign-in, the Fastify server pieces, durations, and the web page's helpers and widgets",
|
|
5
5
|
"license": "GPL-3.0-or-later",
|
|
6
6
|
"repository": {
|
|
@@ -243,7 +243,7 @@
|
|
|
243
243
|
"@tiptap/pm": "^3.33.0",
|
|
244
244
|
"@tiptap/react": "^3.33.0",
|
|
245
245
|
"@tiptap/starter-kit": "^3.33.0",
|
|
246
|
-
"@x-tech-laboratories/x-app-agent": "0.
|
|
246
|
+
"@x-tech-laboratories/x-app-agent": "0.134.0",
|
|
247
247
|
"bonjour-service": "^1.4.4",
|
|
248
248
|
"dayjs": "^1.11.23",
|
|
249
249
|
"diff": "^9.0.0",
|
|
@@ -255,7 +255,6 @@
|
|
|
255
255
|
"fflate": "^0.8.3",
|
|
256
256
|
"force-graph": "^1.52.0",
|
|
257
257
|
"frimousse": "^0.4.0",
|
|
258
|
-
"hookable": "^6.1.2",
|
|
259
258
|
"lucide-react": "^1.67.0",
|
|
260
259
|
"marked": "^18.1.3",
|
|
261
260
|
"parse-diff": "^0.12.0",
|
|
@@ -264,7 +263,6 @@
|
|
|
264
263
|
"react-grid-layout": "^2.3.0",
|
|
265
264
|
"rrule": "^2.8.1",
|
|
266
265
|
"selfsigned": "^5.5.0",
|
|
267
|
-
"semver": "^7.8.6",
|
|
268
266
|
"spdx-satisfies": "^6.0.1",
|
|
269
267
|
"tweetnacl": "^1.0.3"
|
|
270
268
|
},
|