@x-tech-laboratories/x-app 0.119.0 → 0.120.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 +1 -1
- package/dist/app/server.d.ts +1 -1
- package/dist/app/server.js +14 -0
- package/dist/app/web/android.d.ts +1 -1
- package/dist/app/web/android.js +9 -0
- package/dist/sql.d.ts +5 -0
- package/dist/sql.js +21 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -45,7 +45,7 @@ an app moves over by changing the package name in its imports and its `x-libs-*`
|
|
|
45
45
|
| `grouping` | `group_items`: items sorted into groups by a key each has, such as a status, a year or the collections they are in, an item with several keys in each of their groups, the app's `order` of keys first and the rest by name, numbers by value, `descending` when asked, each group keeping the items' order and those with no key last; for `GroupSections`, `GroupedMediaGrid` or a table under headings. |
|
|
46
46
|
| `money` | `format_money` in a currency and locale to a set number of places, falling back to the number and code on a currency Intl refuses; `round_money` rounding halves away from zero, `money_step`, `sum_money`, `is_currency_code` and `currency_options` naming each currency for a select. |
|
|
47
47
|
| `i18n` | Words in the reader's language: `create_translator` looks each key up in catalogs in turn, so a missing translation falls back to the next, with `{name}` placeholders filled; `merge_messages`; `best_locale`, the available locale that best fits the reader's preferred ones, an exact tag before the same language; `text_direction` for the `dir` attribute. |
|
|
48
|
-
| `sql` | `SqlDatabase`, the part of a synchronous SQLite connection the apps use, met by node:sqlite and by `web/sqlite_wasm`; `in_transaction`, and `migrate`, each step (a script, its statements, or code given the database) once in its own transaction, through `user_version` or, with an `owner` such as a feature or plugin, its own row in `schema_versions
|
|
48
|
+
| `sql` | `SqlDatabase`, the part of a synchronous SQLite connection the apps use, met by node:sqlite and by `web/sqlite_wasm`; `in_transaction`, and `migrate`, each step (a script, its statements, or code given the database) once in its own transaction, through `user_version` or, with an `owner` such as a feature or plugin, its own row in `schema_versions`; `tidy_database` refreshes the query planner's statistics and, once a tenth of the file (or the `free_share` given) is empty pages, vacuums it smaller and empties the write-ahead log, leaving a rewrite another connection's write holds up for next time. The base app tidies its database on opening, after a restore and every `tidy_every_ms` (a day) on the server and the desktop daemon, and on opening and on coming back after a day on a phone. |
|
|
49
49
|
| `settings` | An app's settings feature: one synced `settings (key, value)` table of JSON values, the same table and first step (`settings_migration` under the owner `"settings"`) every app already ran. Each feature declares a `SettingSpec` per key (`default`, `user_only`, `local` for this device alone, `secret` for another service's key, or its own `hide` and `keep`), `settings_of` giving every key of a defaults table the same spec; `create_settings_store(tables, { app })` merges them, refusing a key declared twice, and gives the migration, the synced table entry with its `local_keys`, `defaults`, the `user_only` set, `is_user_only_call` for `page_only_calls` (a user-only write, an import holding one, an export with secrets), `is_device_only` for `register_sync_routes`, `value_of`, and `get_settings`, `set_setting`, `export_settings` and `import_settings` with their api specs. Secrets read as `hidden_secret` and are kept when it comes back; the settings file is the standard's `{ app, kind: "settings", version, exported_at, settings }`, an import ignoring unknown keys and those of one device. `read_setting`, `write_setting` and `read_stored_settings` reach rows directly. No `node:` or DOM imports. |
|
|
50
50
|
| `caller`, `node_caller_scope` | Who is making the call in hand: `as_caller("page" \| "program" \| "engine", work)` around a call, `current_caller()` read by a store, and `caller_of(request)` (a token is a program). With a `CallerScope` in use (`use_caller_scope`), the caller holds across the call's awaits; the base app's server and daemon use `node_caller_scope()`, Node's `AsyncLocalStorage`, and without one, as on a phone, it holds for the call's synchronous part. |
|
|
51
51
|
| `api_spec` | An app's calls described once for other programs: `ApiSpec` gives each method its words and a JSON Schema per argument, so a method without one fails to compile; `input_schema` takes the arguments as one object, `positional_args` puts named arguments back in the method's order from a spec or any `NamedParams`, `openapi_document` writes an OpenAPI 3.1 document with one POST per call, and `route_openapi_document` one for REST routes such as `server/mcp`'s `route_tools` serves, each under its own path and verb with its path, query and body arguments; `check_known_ids` refuses a call naming an id that does not exist, such as "no such track a1", before it acts, `unknown_ids` lists them, and `keep_known_ids` cuts a remembered list, such as picked columns, down to the ids that still exist. A spec marked `page_only: true` is a call only the user may make at the app's own page (a signed-in browser, or a desktop window through its daemon's socket), such as approving what an assistant proposed: `page_only_calls(api, { rule?, refusal? })` gives the `PageOnlyCalls` the other modules take (`is_page_only(method)` for the marks, `is_page_only_call(method, args)` adding the app's `rule(method, args)` such as a write to some settings, `is_page_only_request(method, body)` for arguments as a list or by name, unchecked so a call with wrong names is left to refuse them itself, and the `refusal` text, `default_page_only_refusal` unless set), and both OpenAPI documents mark such a call with `x-page-only: true` and a 403. |
|
package/dist/app/server.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { type FastifyInstance } from "fastify";
|
|
|
3
3
|
import { type BackupMethods } from "../backups.ts";
|
|
4
4
|
import { AccountStore } from "../server/account_store.ts";
|
|
5
5
|
import { EventStream } from "../server/events.ts";
|
|
6
|
-
import type
|
|
6
|
+
import { type SqlDatabase } from "../sql.ts";
|
|
7
7
|
import { type SyncPeer } from "../sync_peer.ts";
|
|
8
8
|
import type { App } from "./define.ts";
|
|
9
9
|
import { type StartServices } from "./services.ts";
|
package/dist/app/server.js
CHANGED
|
@@ -24,6 +24,7 @@ import { BAD_JSON, register_error_replies, register_lenient_json_body, send_text
|
|
|
24
24
|
import { sqlite_backups } from "../server/sqlite_backups.js";
|
|
25
25
|
import { register_sync_peer_routes } from "../server/sync_peer_routes.js";
|
|
26
26
|
import { register_web_folder } from "../server/web_folder.js";
|
|
27
|
+
import { tidy_database, tidy_every_ms } from "../sql.js";
|
|
27
28
|
import { file_sync_storage } from "../sync_file_storage.js";
|
|
28
29
|
import { create_sync_peer, sync_device_methods } from "../sync_peer.js";
|
|
29
30
|
import { declared_file_of, no_services, side_databases } from "./services.js";
|
|
@@ -48,6 +49,7 @@ export function open_app_database(app, path, backups_folder) {
|
|
|
48
49
|
open: () => {
|
|
49
50
|
database = new DatabaseSync(path);
|
|
50
51
|
start(database);
|
|
52
|
+
tidy_database(database);
|
|
51
53
|
restore_hooks.after?.();
|
|
52
54
|
},
|
|
53
55
|
// The restored data no longer matches other devices', so sync asks again which copy wins.
|
|
@@ -64,6 +66,17 @@ export function open_app_database(app, path, backups_folder) {
|
|
|
64
66
|
start(database);
|
|
65
67
|
adopt_schedule_file(app, database, join(dirname(path), "backup-schedule.json"));
|
|
66
68
|
const keeper = keep_backing_up(kept, () => app.read_backup_schedule(database));
|
|
69
|
+
// The file is tidied on opening and once a day, so space freed by pruning and clean-ups goes back to the disk.
|
|
70
|
+
tidy_database(database);
|
|
71
|
+
const tidy_timer = setInterval(() => {
|
|
72
|
+
try {
|
|
73
|
+
tidy_database(database);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
// Closed for a restore, which tidies the copy it opens.
|
|
77
|
+
}
|
|
78
|
+
}, tidy_every_ms);
|
|
79
|
+
tidy_timer.unref?.();
|
|
67
80
|
return {
|
|
68
81
|
database: () => database,
|
|
69
82
|
back_up: async () => void (await kept.create()),
|
|
@@ -71,6 +84,7 @@ export function open_app_database(app, path, backups_folder) {
|
|
|
71
84
|
on_restore: (hooks) => void (restore_hooks = hooks),
|
|
72
85
|
close: () => {
|
|
73
86
|
keeper.stop();
|
|
87
|
+
clearInterval(tidy_timer);
|
|
74
88
|
database.close();
|
|
75
89
|
},
|
|
76
90
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type BackupFolderPlugin } from "../../phone_backups.ts";
|
|
2
2
|
import { type FileStore } from "../../web/sqlite_wasm.ts";
|
|
3
|
-
import type
|
|
3
|
+
import { type SqlDatabase } from "../../sql.ts";
|
|
4
4
|
import type { App } from "../define.ts";
|
|
5
5
|
import type { PageApi } from "../protocol.ts";
|
|
6
6
|
export interface PhoneLifecycle {
|
package/dist/app/web/android.js
CHANGED
|
@@ -8,6 +8,7 @@ import { phone_certificates } from "../../phone_certificates.js";
|
|
|
8
8
|
import { create_sync_peer, sync_device_methods } from "../../sync_peer.js";
|
|
9
9
|
import { indexed_db_files, open_wasm_database } from "../../web/sqlite_wasm.js";
|
|
10
10
|
import { local_sync_storage } from "../../web/sync_storage.js";
|
|
11
|
+
import { tidy_database, tidy_every_ms } from "../../sql.js";
|
|
11
12
|
// An older version kept the phone's backup schedule in the page's storage; it moves into the setting once.
|
|
12
13
|
function adopt_stored_schedule(app, database, storage) {
|
|
13
14
|
const key = `${app.id}-backup-schedule`;
|
|
@@ -55,6 +56,9 @@ export async function android_api(app, options = {}) {
|
|
|
55
56
|
await kept.create();
|
|
56
57
|
app.open_store(database.connection);
|
|
57
58
|
adopt_stored_schedule(app, database.connection, options.storage ?? page_storage());
|
|
59
|
+
// Tidied on opening and on coming back after a day or more, so freed space leaves the phone's storage too.
|
|
60
|
+
let tidied_at = Date.now();
|
|
61
|
+
tidy_database(database.connection);
|
|
58
62
|
await database.save_now();
|
|
59
63
|
const keeper = keep_backing_up(kept, () => app.read_backup_schedule(database.connection));
|
|
60
64
|
const services = options.services ? await options.services({ database: () => database.connection }) : {};
|
|
@@ -80,6 +84,11 @@ export async function android_api(app, options = {}) {
|
|
|
80
84
|
// A phone may close the app at any moment once it is in the background, so the database is saved straight away.
|
|
81
85
|
lifecycle.addListener("pause", () => void database.save_now());
|
|
82
86
|
lifecycle.addListener("resume", () => {
|
|
87
|
+
if (Date.now() - tidied_at >= tidy_every_ms) {
|
|
88
|
+
tidied_at = Date.now();
|
|
89
|
+
if (tidy_database(database.connection))
|
|
90
|
+
void database.save_now();
|
|
91
|
+
}
|
|
83
92
|
void sync.sync_now();
|
|
84
93
|
services.changed?.();
|
|
85
94
|
});
|
package/dist/sql.d.ts
CHANGED
|
@@ -20,3 +20,8 @@ export interface MigrateOptions {
|
|
|
20
20
|
owner?: string;
|
|
21
21
|
}
|
|
22
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;
|
package/dist/sql.js
CHANGED
|
@@ -38,3 +38,24 @@ export function migrate(database, steps, options = {}) {
|
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-tech-laboratories/x-app",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.120.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": {
|