@osdlc/wayfinder 0.7.4 → 0.8.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/CHANGELOG.md +214 -0
- package/CONTRIBUTING.md +6 -0
- package/README.md +295 -150
- package/SECURITY.md +19 -5
- package/dist/actions.d.ts +23 -0
- package/dist/actions.js +97 -0
- package/dist/app.d.ts +1 -13
- package/dist/app.js +1 -13
- package/dist/args.d.ts +11 -6
- package/dist/args.js +25 -7
- package/dist/cache.d.ts +2 -4
- package/dist/cache.js +5 -10
- package/dist/catalog.d.ts +30 -3
- package/dist/catalog.js +87 -11
- package/dist/clean-command.d.ts +65 -36
- package/dist/clean-command.js +372 -226
- package/dist/cli.d.ts +24 -57
- package/dist/cli.js +139 -568
- package/dist/commands/browse.d.ts +128 -0
- package/dist/commands/browse.js +151 -0
- package/dist/commands/index.d.ts +25 -0
- package/dist/commands/index.js +71 -0
- package/dist/commands/list.d.ts +208 -0
- package/dist/commands/list.js +127 -0
- package/dist/commands/query.d.ts +166 -0
- package/dist/commands/query.js +73 -0
- package/dist/commands/run.d.ts +114 -0
- package/dist/commands/run.js +139 -0
- package/dist/commands/shared.d.ts +152 -0
- package/dist/commands/shared.js +209 -0
- package/dist/config-command.d.ts +27 -43
- package/dist/config-command.js +129 -330
- package/dist/config-keys.d.ts +25 -0
- package/dist/config-keys.js +29 -0
- package/dist/config.d.ts +271 -257
- package/dist/config.js +610 -460
- package/dist/core/copy-text.d.ts +30 -0
- package/dist/core/copy-text.js +50 -0
- package/dist/core/filter.d.ts +55 -0
- package/dist/core/filter.js +59 -0
- package/dist/core/run.d.ts +22 -0
- package/dist/core/run.js +59 -0
- package/dist/core/saved-view.d.ts +59 -0
- package/dist/core/saved-view.js +119 -0
- package/dist/core/session.d.ts +262 -0
- package/dist/core/session.js +589 -0
- package/dist/core/skills.d.ts +53 -0
- package/dist/core/skills.js +54 -0
- package/dist/core/unknowns.d.ts +72 -0
- package/dist/core/unknowns.js +68 -0
- package/dist/devlink.d.ts +3 -0
- package/dist/devlink.js +5 -2
- package/dist/document-schemas.generated.d.ts +22 -0
- package/dist/document-schemas.generated.js +916 -0
- package/dist/documents.d.ts +432 -0
- package/dist/documents.js +163 -0
- package/dist/groups.d.ts +22 -26
- package/dist/groups.js +13 -29
- package/dist/guide.d.ts +26 -13
- package/dist/guide.js +119 -103
- package/dist/index.d.ts +112 -0
- package/dist/index.js +58 -0
- package/dist/keys.d.ts +51 -0
- package/dist/keys.js +273 -0
- package/dist/page.d.ts +18 -8
- package/dist/page.js +28 -17
- package/dist/providers-command.d.ts +81 -20
- package/dist/providers-command.js +33 -75
- package/dist/schema.d.ts +48 -0
- package/dist/schema.js +61 -0
- package/dist/serve/auth.d.ts +46 -0
- package/dist/serve/auth.js +82 -0
- package/dist/serve/catalog-share.d.ts +51 -0
- package/dist/serve/catalog-share.js +116 -0
- package/dist/serve/index.d.ts +98 -0
- package/dist/serve/index.js +251 -0
- package/dist/serve/routes.d.ts +60 -0
- package/dist/serve/routes.js +186 -0
- package/dist/serve/terminal-input.d.ts +50 -0
- package/dist/serve/terminal-input.js +108 -0
- package/dist/skills.d.ts +19 -0
- package/dist/skills.js +54 -0
- package/dist/terminal-text.d.ts +8 -0
- package/dist/terminal-text.js +19 -6
- package/dist/tool-info.d.ts +24 -0
- package/dist/tool-info.js +17 -0
- package/dist/trust-command.d.ts +15 -27
- package/dist/trust-command.js +110 -208
- package/dist/trust-store.d.ts +5 -10
- package/dist/trust-store.js +9 -42
- package/dist/tui/app.d.ts +180 -0
- package/dist/tui/app.js +1278 -0
- package/dist/tui/copy.d.ts +29 -0
- package/dist/tui/copy.js +39 -0
- package/dist/tui/index.d.ts +33 -0
- package/dist/tui/index.js +97 -0
- package/dist/tui/keys-input.d.ts +27 -0
- package/dist/tui/keys-input.js +87 -0
- package/dist/tui/settings.d.ts +94 -0
- package/dist/tui/settings.js +198 -0
- package/dist/tui/style.d.ts +62 -0
- package/dist/tui/style.js +77 -0
- package/dist/version.d.ts +6 -0
- package/dist/version.js +7 -0
- package/dist/vocabulary.d.ts +89 -0
- package/dist/vocabulary.js +153 -0
- package/dist/web/main.js +1965 -568
- package/dist/web/page.html +39 -33
- package/dist/web/styles.css +457 -329
- package/dist/xdg.d.ts +76 -4
- package/dist/xdg.js +77 -5
- package/package.json +28 -9
- package/schema/config.json +133 -0
- package/dist/http.d.ts +0 -22
- package/dist/http.js +0 -149
- package/dist/origin-owner.d.ts +0 -19
- package/dist/origin-owner.js +0 -85
- package/dist/serve.d.ts +0 -98
- package/dist/serve.js +0 -671
- package/dist/tui.d.ts +0 -177
- package/dist/tui.js +0 -840
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What Copy puts on the clipboard, in the terminal UI and the web view alike.
|
|
3
|
+
*
|
|
4
|
+
* A task is copied as its argv quoted for the platform's shell, never as
|
|
5
|
+
* `display`, so a name like `x; touch PWNED #` pastes as one quoted word.
|
|
6
|
+
* Text that holds a control character is not copied at all: an `ESC[201~`
|
|
7
|
+
* ends a terminal's bracketed paste, and a newline would run the line, so
|
|
8
|
+
* replacing them would still put text on the clipboard the person did not
|
|
9
|
+
* see.
|
|
10
|
+
*
|
|
11
|
+
* Browser-safe: the quoting comes from `@osdlc/command-seam/quote`.
|
|
12
|
+
*/
|
|
13
|
+
import type { Task } from '@osdlc/system-model/detect';
|
|
14
|
+
import type { SkillRow } from './skills.ts';
|
|
15
|
+
/** Why a copy was refused, as every surface says it. */
|
|
16
|
+
export declare const COPY_REFUSED = "not copied: it holds a control character";
|
|
17
|
+
/**
|
|
18
|
+
* The line to paste for `task`: its argv quoted for `platform`'s shell, after
|
|
19
|
+
* a change to the task's directory when it has its own. Null when the line
|
|
20
|
+
* holds a control character, so it is not copied.
|
|
21
|
+
*/
|
|
22
|
+
export declare function taskCopyText(task: Pick<Task, 'argv' | 'cwd'>, platform: NodeJS.Platform): string | null;
|
|
23
|
+
/** `/<name>` for `skill`, or null when it holds a control character. */
|
|
24
|
+
export declare function skillCopyText(skill: Pick<SkillRow, 'invoke'>): string | null;
|
|
25
|
+
/**
|
|
26
|
+
* The line `list` and `query` print for `task`: what Copy gives, which `run`
|
|
27
|
+
* accepts. When Copy refuses, the same quoted line with its controls
|
|
28
|
+
* replaced, or the `display` of a task with no argv.
|
|
29
|
+
*/
|
|
30
|
+
export declare function taskLine(task: Pick<Task, 'argv' | 'cwd' | 'display'>): string;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What Copy puts on the clipboard, in the terminal UI and the web view alike.
|
|
3
|
+
*
|
|
4
|
+
* A task is copied as its argv quoted for the platform's shell, never as
|
|
5
|
+
* `display`, so a name like `x; touch PWNED #` pastes as one quoted word.
|
|
6
|
+
* Text that holds a control character is not copied at all: an `ESC[201~`
|
|
7
|
+
* ends a terminal's bracketed paste, and a newline would run the line, so
|
|
8
|
+
* replacing them would still put text on the clipboard the person did not
|
|
9
|
+
* see.
|
|
10
|
+
*
|
|
11
|
+
* Browser-safe: the quoting comes from `@osdlc/command-seam/quote`.
|
|
12
|
+
*/
|
|
13
|
+
import { commandString } from '@osdlc/command-seam/quote';
|
|
14
|
+
import { hasControls, terminalSafe } from "../terminal-text.js";
|
|
15
|
+
/** Why a copy was refused, as every surface says it. */
|
|
16
|
+
export const COPY_REFUSED = 'not copied: it holds a control character';
|
|
17
|
+
/** `text`, or null when it holds a control character. */
|
|
18
|
+
function clipboardText(text) {
|
|
19
|
+
return hasControls(text) ? null : text;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The line to paste for `task`: its argv quoted for `platform`'s shell, after
|
|
23
|
+
* a change to the task's directory when it has its own. Null when the line
|
|
24
|
+
* holds a control character, so it is not copied.
|
|
25
|
+
*/
|
|
26
|
+
export function taskCopyText(task, platform) {
|
|
27
|
+
if (task.argv.length === 0)
|
|
28
|
+
return null;
|
|
29
|
+
return clipboardText(commandString(task.argv, { platform, ...(task.cwd === '.' ? {} : { cwd: task.cwd }) }));
|
|
30
|
+
}
|
|
31
|
+
/** `/<name>` for `skill`, or null when it holds a control character. */
|
|
32
|
+
export function skillCopyText(skill) {
|
|
33
|
+
return clipboardText(skill.invoke);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The line `list` and `query` print for `task`: what Copy gives, which `run`
|
|
37
|
+
* accepts. When Copy refuses, the same quoted line with its controls
|
|
38
|
+
* replaced, or the `display` of a task with no argv.
|
|
39
|
+
*/
|
|
40
|
+
export function taskLine(task) {
|
|
41
|
+
const copied = taskCopyText(task, process.platform);
|
|
42
|
+
if (copied !== null)
|
|
43
|
+
return copied;
|
|
44
|
+
if (task.argv.length === 0)
|
|
45
|
+
return terminalSafe(task.display);
|
|
46
|
+
return terminalSafe(commandString(task.argv, {
|
|
47
|
+
platform: process.platform,
|
|
48
|
+
...(task.cwd === '.' ? {} : { cwd: task.cwd }),
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one search and filter every surface uses: the CLI's `list` and
|
|
3
|
+
* `query`, the terminal UI's entry line and the web view's toolbar.
|
|
4
|
+
*
|
|
5
|
+
* Browser-safe: it imports only the matcher, so the page can use it.
|
|
6
|
+
*/
|
|
7
|
+
import { type Mode } from '@osdlc/system-model/match';
|
|
8
|
+
import type { Task } from '@osdlc/system-model/detect';
|
|
9
|
+
/**
|
|
10
|
+
* What to keep. Every field is optional; an empty filter keeps every task in
|
|
11
|
+
* catalog order.
|
|
12
|
+
*/
|
|
13
|
+
export interface TaskFilter {
|
|
14
|
+
/** The search text. Terms are ANDed; a leading `'` makes one term exact. */
|
|
15
|
+
query?: string | undefined;
|
|
16
|
+
/** `fuzzy` (the default) matches letters in order; `exact` matches substrings. */
|
|
17
|
+
mode?: Mode | undefined;
|
|
18
|
+
/** Only tasks that run until stopped: dev servers, watchers. */
|
|
19
|
+
servicesOnly?: boolean | undefined;
|
|
20
|
+
/** Only tasks a file declares, not ones inferred from a convention. */
|
|
21
|
+
declaredOnly?: boolean | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Only tasks declared at most this many directories below the root (see
|
|
24
|
+
* {@link sourceDepth}). The web view calls this Scope.
|
|
25
|
+
*/
|
|
26
|
+
maxDepth?: number | undefined;
|
|
27
|
+
/** Only these runners. */
|
|
28
|
+
runners?: readonly string[] | undefined;
|
|
29
|
+
}
|
|
30
|
+
/** One kept task, with its score and the offsets the chain matched at. */
|
|
31
|
+
export interface TaskHit {
|
|
32
|
+
task: Task;
|
|
33
|
+
/** 0 when there is no query. */
|
|
34
|
+
score: number;
|
|
35
|
+
/** Offsets into `task.path.join(' ')`, when the chain is what matched. */
|
|
36
|
+
labelHits: number[] | null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* How deep in the repository a task is declared: the number of directories
|
|
40
|
+
* above its source file. `justfile` at the root is 0;
|
|
41
|
+
* `apps/web/client/moon.yml` is 3.
|
|
42
|
+
*/
|
|
43
|
+
export declare function sourceDepth(task: Pick<Task, 'source'>): number;
|
|
44
|
+
/** Whether `task` passes every part of `filter` except the query. */
|
|
45
|
+
export declare function keepsTask(task: Task, filter: TaskFilter): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* The tasks of `reports` that pass `filter`. With a query they are ranked
|
|
48
|
+
* best first, and equal scores keep catalog order. Without one they stay in
|
|
49
|
+
* catalog order.
|
|
50
|
+
*/
|
|
51
|
+
export declare function filterTasks(reports: readonly {
|
|
52
|
+
tasks: readonly Task[];
|
|
53
|
+
}[], filter?: TaskFilter): TaskHit[];
|
|
54
|
+
/** Whether `filter` has any query terms. */
|
|
55
|
+
export declare function hasQuery(filter: TaskFilter): boolean;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one search and filter every surface uses: the CLI's `list` and
|
|
3
|
+
* `query`, the terminal UI's entry line and the web view's toolbar.
|
|
4
|
+
*
|
|
5
|
+
* Browser-safe: it imports only the matcher, so the page can use it.
|
|
6
|
+
*/
|
|
7
|
+
import { matchEntry, terms as splitTerms } from '@osdlc/system-model/match';
|
|
8
|
+
/**
|
|
9
|
+
* How deep in the repository a task is declared: the number of directories
|
|
10
|
+
* above its source file. `justfile` at the root is 0;
|
|
11
|
+
* `apps/web/client/moon.yml` is 3.
|
|
12
|
+
*/
|
|
13
|
+
export function sourceDepth(task) {
|
|
14
|
+
const at = task.source.lastIndexOf('/');
|
|
15
|
+
return at === -1 ? 0 : task.source.slice(0, at).split('/').length;
|
|
16
|
+
}
|
|
17
|
+
/** Whether `task` passes every part of `filter` except the query. */
|
|
18
|
+
export function keepsTask(task, filter) {
|
|
19
|
+
if (filter.servicesOnly === true && !task.persistent.value)
|
|
20
|
+
return false;
|
|
21
|
+
if (filter.declaredOnly === true && task.origin !== 'declared')
|
|
22
|
+
return false;
|
|
23
|
+
if (filter.maxDepth !== undefined && sourceDepth(task) > filter.maxDepth)
|
|
24
|
+
return false;
|
|
25
|
+
if (filter.runners !== undefined && !filter.runners.includes(task.runner))
|
|
26
|
+
return false;
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The tasks of `reports` that pass `filter`. With a query they are ranked
|
|
31
|
+
* best first, and equal scores keep catalog order. Without one they stay in
|
|
32
|
+
* catalog order.
|
|
33
|
+
*/
|
|
34
|
+
export function filterTasks(reports, filter = {}) {
|
|
35
|
+
const queryTerms = splitTerms(filter.query ?? '');
|
|
36
|
+
const mode = filter.mode ?? 'fuzzy';
|
|
37
|
+
const hits = [];
|
|
38
|
+
for (const report of reports) {
|
|
39
|
+
for (const task of report.tasks) {
|
|
40
|
+
if (!keepsTask(task, filter))
|
|
41
|
+
continue;
|
|
42
|
+
if (queryTerms.length === 0) {
|
|
43
|
+
hits.push({ task, score: 0, labelHits: null });
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const m = matchEntry(task, queryTerms, mode);
|
|
47
|
+
if (m !== null)
|
|
48
|
+
hits.push({ task, score: m.score, labelHits: m.labelHits });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Stable, so equal scores keep the catalog's own order.
|
|
52
|
+
if (queryTerms.length > 0)
|
|
53
|
+
hits.sort((a, b) => b.score - a.score);
|
|
54
|
+
return hits;
|
|
55
|
+
}
|
|
56
|
+
/** Whether `filter` has any query terms. */
|
|
57
|
+
export function hasQuery(filter) {
|
|
58
|
+
return splitTerms(filter.query ?? '').length > 0;
|
|
59
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handing the terminal to a task. The CLI's `run`, the terminal UI and the
|
|
3
|
+
* web view's server hand off the same way.
|
|
4
|
+
*/
|
|
5
|
+
import type { RunPlan } from './session.ts';
|
|
6
|
+
/** How a handed-off task ended. */
|
|
7
|
+
export interface HandOffResult {
|
|
8
|
+
/** The task's exit code, `128 + n` when signal `n` ended it, 1 when it never started. */
|
|
9
|
+
exit: number;
|
|
10
|
+
/** Why it never started: no such binary, or no such directory. */
|
|
11
|
+
error?: string | undefined;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Run `plan` with this process's terminal and wait for it. `argv` is spawned
|
|
15
|
+
* without a shell, with `plan.env` added to the environment.
|
|
16
|
+
*
|
|
17
|
+
* Ctrl-C reaches the task and this process alike, since both are in the
|
|
18
|
+
* terminal's foreground group. This process ignores it while the task runs,
|
|
19
|
+
* so it can report how the task ended, as a shell does. An argv the system
|
|
20
|
+
* cannot spawn, such as one holding a NUL, is `{ exit: 1, error }`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function handOff(plan: Pick<RunPlan, 'argv' | 'env' | 'cwd'>): HandOffResult;
|
package/dist/core/run.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handing the terminal to a task. The CLI's `run`, the terminal UI and the
|
|
3
|
+
* web view's server hand off the same way.
|
|
4
|
+
*/
|
|
5
|
+
import { constants } from 'node:os';
|
|
6
|
+
// Live-streaming exception to the command seam: the task gets this process's
|
|
7
|
+
// terminal (`stdio: "inherit"`), which only `streamRunner` does.
|
|
8
|
+
import { streamRunner } from '@osdlc/command-seam';
|
|
9
|
+
/**
|
|
10
|
+
* The signals this process ignores while the task runs. SIGINT and SIGQUIT
|
|
11
|
+
* come from the terminal to the whole foreground group, so they are the
|
|
12
|
+
* task's to handle. SIGTERM and SIGHUP may be sent to this process alone, by
|
|
13
|
+
* `kill` or a supervisor. The spawn is synchronous, so they cannot be passed
|
|
14
|
+
* on to the task; ignoring them keeps this process waiting for the task
|
|
15
|
+
* rather than leaving it running with no parent. To stop both, signal the
|
|
16
|
+
* process group (`kill -TERM -<pgid>`) or press Ctrl-C.
|
|
17
|
+
*/
|
|
18
|
+
const TERMINAL_SIGNALS = process.platform === 'win32' ? ['SIGINT'] : ['SIGINT', 'SIGQUIT', 'SIGTERM', 'SIGHUP'];
|
|
19
|
+
/**
|
|
20
|
+
* Run `plan` with this process's terminal and wait for it. `argv` is spawned
|
|
21
|
+
* without a shell, with `plan.env` added to the environment.
|
|
22
|
+
*
|
|
23
|
+
* Ctrl-C reaches the task and this process alike, since both are in the
|
|
24
|
+
* terminal's foreground group. This process ignores it while the task runs,
|
|
25
|
+
* so it can report how the task ended, as a shell does. An argv the system
|
|
26
|
+
* cannot spawn, such as one holding a NUL, is `{ exit: 1, error }`.
|
|
27
|
+
*/
|
|
28
|
+
export function handOff(plan) {
|
|
29
|
+
const ignore = () => { };
|
|
30
|
+
for (const signal of TERMINAL_SIGNALS)
|
|
31
|
+
process.on(signal, ignore);
|
|
32
|
+
try {
|
|
33
|
+
const [binary, ...rest] = plan.argv;
|
|
34
|
+
if (binary === undefined)
|
|
35
|
+
return { exit: 1, error: 'the task has no command' };
|
|
36
|
+
let res;
|
|
37
|
+
try {
|
|
38
|
+
res = streamRunner(binary).run(rest, { cwd: plan.cwd, env: plan.env, quiet: true });
|
|
39
|
+
}
|
|
40
|
+
catch (err) {
|
|
41
|
+
return { exit: 1, error: err instanceof Error ? err.message : String(err) };
|
|
42
|
+
}
|
|
43
|
+
if (res.reason === 'spawn')
|
|
44
|
+
return { exit: 1, error: res.error ?? 'could not start' };
|
|
45
|
+
if (res.reason === 'signal' && res.signal !== undefined) {
|
|
46
|
+
const number = constants.signals[res.signal];
|
|
47
|
+
return { exit: number === undefined ? 1 : 128 + number };
|
|
48
|
+
}
|
|
49
|
+
return { exit: res.exitCode };
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
// A signal that arrived during the run is dispatched on a later turn.
|
|
53
|
+
// Removing the listener before then would let it end this process.
|
|
54
|
+
setTimeout(() => {
|
|
55
|
+
for (const signal of TERMINAL_SIGNALS)
|
|
56
|
+
process.off(signal, ignore);
|
|
57
|
+
}, 50).unref();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The saved view: the last catalog a repository showed, in one file, so the
|
|
3
|
+
* terminal UI and the web view can draw it before reading anything else.
|
|
4
|
+
*
|
|
5
|
+
* Reading the stored probes (`readCachedCatalog`) detects the project and
|
|
6
|
+
* hashes every runner's files, which takes seconds on a large repository.
|
|
7
|
+
* This file needs neither. It is not checked against the files: a surface
|
|
8
|
+
* draws it, then probes, and a task from it is probed again before it runs
|
|
9
|
+
* (`Session.confirmRun`). It is used only when it was saved under the same
|
|
10
|
+
* {@link viewKey}, so a change of trust or settings never shows rows another
|
|
11
|
+
* state produced. Each key has its own file, so a `--no-exec` script does not
|
|
12
|
+
* replace the view a UI draws.
|
|
13
|
+
*
|
|
14
|
+
* Layout: `<cache>/views/v<VIEW_FORMAT_VERSION>/<basename>-<root hash>-<key hash>.json`.
|
|
15
|
+
*/
|
|
16
|
+
import type { Catalog } from '@osdlc/system-model/detect';
|
|
17
|
+
import type { CacheState } from './session.ts';
|
|
18
|
+
import type { SkillRow } from './skills.ts';
|
|
19
|
+
/**
|
|
20
|
+
* A view file larger than this is not read. The largest catalog measured, of
|
|
21
|
+
* a repository with 30,000 files and 2,000 tasks, is about 1.3 MB.
|
|
22
|
+
*/
|
|
23
|
+
export declare const MAX_VIEW_BYTES: number;
|
|
24
|
+
/** What the file holds. */
|
|
25
|
+
export interface SavedView {
|
|
26
|
+
version: number;
|
|
27
|
+
/** The {@link viewKey} it was saved under. */
|
|
28
|
+
key: string;
|
|
29
|
+
/** When it was saved, in ms since the epoch. */
|
|
30
|
+
savedAt: number;
|
|
31
|
+
catalog: Catalog;
|
|
32
|
+
cache: CacheState[];
|
|
33
|
+
skills: SkillRow[];
|
|
34
|
+
/** Each runner's trust scopes, as the last probe saw them. */
|
|
35
|
+
scopes: Record<string, string[]>;
|
|
36
|
+
}
|
|
37
|
+
/** The file for `root` under `store`, for the view saved under `key`. */
|
|
38
|
+
export declare function viewPath(store: string, root: string, key: string): string;
|
|
39
|
+
/** Everything that changes which rows a probe returns, as one string. */
|
|
40
|
+
export declare function viewKey(parts: Record<string, unknown>): string;
|
|
41
|
+
/**
|
|
42
|
+
* The view saved for `root` under `key`, or null. A file that is too large,
|
|
43
|
+
* not valid JSON, not a view, or not a plain file (a symlink is not
|
|
44
|
+
* followed) is deleted, so the next read goes to the store at once. Never
|
|
45
|
+
* throws.
|
|
46
|
+
*/
|
|
47
|
+
export declare function readView(store: string, root: string, key: string): Promise<SavedView | null>;
|
|
48
|
+
/**
|
|
49
|
+
* Settles when every view write this process started has ended. Call it
|
|
50
|
+
* before `process.exit`, which would drop a write in flight, and before a
|
|
51
|
+
* test removes the cache directory.
|
|
52
|
+
*/
|
|
53
|
+
export declare function flushViews(): Promise<void>;
|
|
54
|
+
/**
|
|
55
|
+
* Save the view, without waiting: the caller answers first. A failed write
|
|
56
|
+
* only makes the next first paint slower. The returned promise settles when
|
|
57
|
+
* the file is written, for a caller that must wait.
|
|
58
|
+
*/
|
|
59
|
+
export declare function writeView(store: string, view: Omit<SavedView, 'version'>): Promise<void>;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The saved view: the last catalog a repository showed, in one file, so the
|
|
3
|
+
* terminal UI and the web view can draw it before reading anything else.
|
|
4
|
+
*
|
|
5
|
+
* Reading the stored probes (`readCachedCatalog`) detects the project and
|
|
6
|
+
* hashes every runner's files, which takes seconds on a large repository.
|
|
7
|
+
* This file needs neither. It is not checked against the files: a surface
|
|
8
|
+
* draws it, then probes, and a task from it is probed again before it runs
|
|
9
|
+
* (`Session.confirmRun`). It is used only when it was saved under the same
|
|
10
|
+
* {@link viewKey}, so a change of trust or settings never shows rows another
|
|
11
|
+
* state produced. Each key has its own file, so a `--no-exec` script does not
|
|
12
|
+
* replace the view a UI draws.
|
|
13
|
+
*
|
|
14
|
+
* Layout: `<cache>/views/v<VIEW_FORMAT_VERSION>/<basename>-<root hash>-<key hash>.json`.
|
|
15
|
+
*/
|
|
16
|
+
import { createHash } from 'node:crypto';
|
|
17
|
+
import { lstat, readFile, rm } from 'node:fs/promises';
|
|
18
|
+
import { basename, join } from 'node:path';
|
|
19
|
+
import { atomicWriteAsync } from '@osdlc/etag-write';
|
|
20
|
+
import { z } from 'zod';
|
|
21
|
+
import { CacheStateZ, CatalogZ, SkillZ } from "../documents.js";
|
|
22
|
+
const VIEW_FORMAT_VERSION = 1;
|
|
23
|
+
/**
|
|
24
|
+
* A view file larger than this is not read. The largest catalog measured, of
|
|
25
|
+
* a repository with 30,000 files and 2,000 tasks, is about 1.3 MB.
|
|
26
|
+
*/
|
|
27
|
+
export const MAX_VIEW_BYTES = 32 * 1024 * 1024;
|
|
28
|
+
const SavedViewZ = z.object({
|
|
29
|
+
version: z.literal(VIEW_FORMAT_VERSION),
|
|
30
|
+
key: z.string(),
|
|
31
|
+
savedAt: z.number().finite(),
|
|
32
|
+
catalog: CatalogZ,
|
|
33
|
+
cache: z.array(CacheStateZ),
|
|
34
|
+
skills: z.array(SkillZ),
|
|
35
|
+
scopes: z.record(z.string(), z.array(z.string())),
|
|
36
|
+
});
|
|
37
|
+
const hash = (text) => createHash('sha256').update(text).digest('hex').slice(0, 16);
|
|
38
|
+
/** The file for `root` under `store`, for the view saved under `key`. */
|
|
39
|
+
export function viewPath(store, root, key) {
|
|
40
|
+
const name = (basename(root) || 'root').replace(/[^A-Za-z0-9._-]/g, '-');
|
|
41
|
+
return join(store, 'views', `v${VIEW_FORMAT_VERSION}`, `${name}-${hash(root)}-${hash(key)}.json`);
|
|
42
|
+
}
|
|
43
|
+
/** Everything that changes which rows a probe returns, as one string. */
|
|
44
|
+
export function viewKey(parts) {
|
|
45
|
+
return JSON.stringify(parts);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The view saved for `root` under `key`, or null. A file that is too large,
|
|
49
|
+
* not valid JSON, not a view, or not a plain file (a symlink is not
|
|
50
|
+
* followed) is deleted, so the next read goes to the store at once. Never
|
|
51
|
+
* throws.
|
|
52
|
+
*/
|
|
53
|
+
export async function readView(store, root, key) {
|
|
54
|
+
const file = viewPath(store, root, key);
|
|
55
|
+
// A write this process started is finished first, so a read after a
|
|
56
|
+
// catalog() call sees what that call showed.
|
|
57
|
+
await writing.get(file);
|
|
58
|
+
const discard = async () => {
|
|
59
|
+
await rm(file, { force: true }).catch(() => { });
|
|
60
|
+
return null;
|
|
61
|
+
};
|
|
62
|
+
try {
|
|
63
|
+
const info = await lstat(file).catch(() => null);
|
|
64
|
+
if (info === null)
|
|
65
|
+
return null;
|
|
66
|
+
if (!info.isFile() || info.size > MAX_VIEW_BYTES)
|
|
67
|
+
return discard();
|
|
68
|
+
let raw;
|
|
69
|
+
try {
|
|
70
|
+
raw = JSON.parse(await readFile(file, 'utf8'));
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
return discard();
|
|
74
|
+
}
|
|
75
|
+
const parsed = SavedViewZ.safeParse(raw);
|
|
76
|
+
if (!parsed.success)
|
|
77
|
+
return discard();
|
|
78
|
+
const view = parsed.data;
|
|
79
|
+
if (view.key !== key || view.catalog.root !== root)
|
|
80
|
+
return null;
|
|
81
|
+
return view;
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** The writes this process has in flight, by file. */
|
|
88
|
+
const writing = new Map();
|
|
89
|
+
/**
|
|
90
|
+
* Settles when every view write this process started has ended. Call it
|
|
91
|
+
* before `process.exit`, which would drop a write in flight, and before a
|
|
92
|
+
* test removes the cache directory.
|
|
93
|
+
*/
|
|
94
|
+
export async function flushViews() {
|
|
95
|
+
while (writing.size > 0)
|
|
96
|
+
await Promise.all(writing.values());
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Save the view, without waiting: the caller answers first. A failed write
|
|
100
|
+
* only makes the next first paint slower. The returned promise settles when
|
|
101
|
+
* the file is written, for a caller that must wait.
|
|
102
|
+
*/
|
|
103
|
+
export function writeView(store, view) {
|
|
104
|
+
const file = viewPath(store, view.catalog.root, view.key);
|
|
105
|
+
const previous = writing.get(file) ?? Promise.resolve();
|
|
106
|
+
const done = previous
|
|
107
|
+
.then(() => atomicWriteAsync(file, JSON.stringify({ version: VIEW_FORMAT_VERSION, ...view }), {
|
|
108
|
+
mode: 0o600,
|
|
109
|
+
}))
|
|
110
|
+
.catch(() => {
|
|
111
|
+
// Nothing to do: the stored probes are still there.
|
|
112
|
+
})
|
|
113
|
+
.finally(() => {
|
|
114
|
+
if (writing.get(file) === done)
|
|
115
|
+
writing.delete(file);
|
|
116
|
+
});
|
|
117
|
+
writing.set(file, done);
|
|
118
|
+
return done;
|
|
119
|
+
}
|