@osdlc/wayfinder 0.7.4 → 0.9.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 +240 -0
- package/CONTRIBUTING.md +6 -0
- package/README.md +310 -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 +45 -4
- package/dist/catalog.js +134 -13
- 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 +153 -0
- package/dist/commands/index.d.ts +25 -0
- package/dist/commands/index.js +71 -0
- package/dist/commands/list.d.ts +224 -0
- package/dist/commands/list.js +178 -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 +272 -257
- package/dist/config.js +611 -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 +282 -0
- package/dist/core/session.js +602 -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 +508 -0
- package/dist/documents.js +181 -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 +118 -0
- package/dist/index.js +61 -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 +63 -0
- package/dist/serve/catalog-share.js +155 -0
- package/dist/serve/index.d.ts +100 -0
- package/dist/serve/index.js +259 -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 +193 -0
- package/dist/tui/app.js +1400 -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 +91 -0
- package/dist/vocabulary.js +155 -0
- package/dist/web/main.js +2232 -582
- package/dist/web/page.html +41 -33
- package/dist/web/styles.css +513 -329
- package/dist/xdg.d.ts +76 -4
- package/dist/xdg.js +77 -5
- package/package.json +27 -8
- 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
|
@@ -3,30 +3,12 @@
|
|
|
3
3
|
* with its roles, its runner kind, its binary and whether that binary is on
|
|
4
4
|
* an absolute PATH entry. Reads files only; runs nothing.
|
|
5
5
|
*/
|
|
6
|
-
import { EXIT } from '@osdlc/cli-tool';
|
|
7
6
|
import { which } from '@osdlc/command-seam';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import { appTrustStore } from "./trust-store.js";
|
|
7
|
+
import { kit, openSession } from "./commands/shared.js";
|
|
8
|
+
import { ProvidersOutput } from "./documents.js";
|
|
9
|
+
import { terminalSafe } from "./terminal-text.js";
|
|
10
|
+
import { toolSummary } from "./tool-info.js";
|
|
13
11
|
const ROLE_ORDER = ['runner', 'packageManager', 'toolManager', 'hookManager'];
|
|
14
|
-
function usage() {
|
|
15
|
-
return `${BIN} providers — the tools ${APP} knows, built-in and your own
|
|
16
|
-
|
|
17
|
-
usage: ${BIN} providers [options]
|
|
18
|
-
|
|
19
|
-
--config MODULE also load the tools a module exports; repeatable
|
|
20
|
-
--json the same rows as a JSON array
|
|
21
|
-
-C, --dir PATH the project whose config to read (default: cwd)
|
|
22
|
-
-h, --help this text
|
|
23
|
-
|
|
24
|
-
A custom tool comes from --config, from "tools" in the user config, or from
|
|
25
|
-
"tools" in the project file when the repository is trusted and execution is
|
|
26
|
-
allowed. kind is the runner
|
|
27
|
-
role's: parse reads files, exec runs the repository's tools and needs trust.
|
|
28
|
-
`;
|
|
29
|
-
}
|
|
30
12
|
/** Found on an absolute PATH entry, as detection finds a binary. */
|
|
31
13
|
function onPath(binary, env) {
|
|
32
14
|
const lookup = { absolutePathOnly: true, env: { PATH: env['PATH'], PATHEXT: env['PATHEXT'] } };
|
|
@@ -36,6 +18,7 @@ function onPath(binary, env) {
|
|
|
36
18
|
export function providerRows(tools, custom, env) {
|
|
37
19
|
return tools.map((t) => {
|
|
38
20
|
const from = custom.find((c) => c.def === t);
|
|
21
|
+
const info = toolSummary(t);
|
|
39
22
|
return {
|
|
40
23
|
id: t.id,
|
|
41
24
|
roles: ROLE_ORDER.filter((r) => t.roles[r] !== undefined),
|
|
@@ -44,6 +27,10 @@ export function providerRows(tools, custom, env) {
|
|
|
44
27
|
onPath: t.binary === undefined ? null : onPath(t.binary, env),
|
|
45
28
|
source: from === undefined ? 'built-in' : 'custom',
|
|
46
29
|
...(from !== undefined ? { module: from.module } : {}),
|
|
30
|
+
homepage: info.homepage,
|
|
31
|
+
description: info.description,
|
|
32
|
+
ecosystem: info.ecosystem,
|
|
33
|
+
language: info.language,
|
|
47
34
|
};
|
|
48
35
|
});
|
|
49
36
|
}
|
|
@@ -55,9 +42,11 @@ function table(rows) {
|
|
|
55
42
|
r.kind ?? '-',
|
|
56
43
|
r.binary ?? '-',
|
|
57
44
|
r.onPath === null ? '-' : r.onPath ? 'yes' : 'no',
|
|
45
|
+
r.language,
|
|
58
46
|
r.source === 'custom' && r.module !== undefined ? `custom ${r.module}` : r.source,
|
|
47
|
+
r.homepage ?? '-',
|
|
59
48
|
].map((cell) => terminalSafe(cell)));
|
|
60
|
-
const head = ['tool', 'roles', 'kind', 'binary', 'on PATH', 'source'];
|
|
49
|
+
const head = ['tool', 'roles', 'kind', 'binary', 'on PATH', 'language', 'source', 'homepage'];
|
|
61
50
|
const widths = head.map((h, i) => Math.max(h.length, ...cells.map((c) => c[i].length)));
|
|
62
51
|
const fmt = (c) => c
|
|
63
52
|
.map((v, i) => (i === c.length - 1 ? v : v.padEnd(widths[i])))
|
|
@@ -65,56 +54,25 @@ function table(rows) {
|
|
|
65
54
|
.trimEnd();
|
|
66
55
|
return [fmt(head), ...cells.map(fmt)].join('\n') + '\n';
|
|
67
56
|
}
|
|
68
|
-
/**
|
|
69
|
-
export
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
io.stderr(`${APP}: unknown option: ${arg}\n\n${usage()}`);
|
|
92
|
-
return EXIT.usage;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
let cfg;
|
|
96
|
-
try {
|
|
97
|
-
cfg = await loadConfig(root, env, home);
|
|
98
|
-
}
|
|
99
|
-
catch (err) {
|
|
100
|
-
if (err instanceof ConfigError) {
|
|
101
|
-
io.stderr(`${APP}: ${err.message}\n`);
|
|
102
|
-
return EXIT.error;
|
|
103
|
-
}
|
|
104
|
-
throw err;
|
|
105
|
-
}
|
|
106
|
-
for (const warning of cfg.warnings)
|
|
107
|
-
io.stderr(`warning: ${warning}\n`);
|
|
108
|
-
// The same gate `way` applies: project modules need trust and execution allowed.
|
|
109
|
-
const gate = await policyExecGate(appTrustStore({ env, home }), trustPolicyFromConfig(cfg.effective), { root, configured: io.configuredAllowExec(cfg) }, trustPolicyOptions(home));
|
|
110
|
-
const built = await io.load(cfg, modules, gate.trusted && gate.allowExec);
|
|
111
|
-
if ('error' in built) {
|
|
112
|
-
io.stderr(`${APP}: ${built.error}\n`);
|
|
113
|
-
return EXIT.error;
|
|
114
|
-
}
|
|
115
|
-
for (const warning of built.warnings)
|
|
116
|
-
io.stderr(`warning: ${warning}\n`);
|
|
117
|
-
const rows = providerRows(built.project.tools, built.tools, env);
|
|
118
|
-
io.stdout(json ? `${jsonText(rows, 2)}\n` : table(rows));
|
|
119
|
-
return EXIT.ok;
|
|
57
|
+
/** Make the `providers` command. */
|
|
58
|
+
export function providersCommand(home) {
|
|
59
|
+
return kit.command({
|
|
60
|
+
summary: 'the registered tools, built-in and your own',
|
|
61
|
+
description: 'Every tool wayfinder knows, with its roles, its runner kind, its binary and whether that ' +
|
|
62
|
+
'binary is on an absolute PATH entry. Reads files only; runs nothing. A custom tool comes ' +
|
|
63
|
+
'from --config, from "tools" in the user config, or from "tools" in the project file when ' +
|
|
64
|
+
"the repository is trusted and execution is allowed. kind is the runner role's: parse " +
|
|
65
|
+
"reads files, exec runs the repository's tools and needs trust. Each tool has its " +
|
|
66
|
+
'language and homepage; --json adds its description.',
|
|
67
|
+
output: ProvidersOutput,
|
|
68
|
+
kind: 'providers',
|
|
69
|
+
readOnly: true,
|
|
70
|
+
async run({ globals }, ctx) {
|
|
71
|
+
const session = await openSession(globals, ctx, home);
|
|
72
|
+
return { providers: providerRows(session.project.tools, session.tools, ctx.env) };
|
|
73
|
+
},
|
|
74
|
+
text(output) {
|
|
75
|
+
return table(output.providers);
|
|
76
|
+
},
|
|
77
|
+
});
|
|
120
78
|
}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@osdlc/wayfinder/schema`: JSON Schemas (draft 2020-12) for every document
|
|
3
|
+
* `way` prints with `--json`, keyed by each document's `kind`. Every document
|
|
4
|
+
* is an object with `schemaVersion` and `kind` first. `way schema` prints the
|
|
5
|
+
* same schemas with each command's input.
|
|
6
|
+
*
|
|
7
|
+
* The task, report and catalog shapes are `@osdlc/system-model/schema`'s,
|
|
8
|
+
* re-exported here. Browser-safe: no Node built-in.
|
|
9
|
+
*/
|
|
10
|
+
import { z } from 'zod';
|
|
11
|
+
import { GENERATED_VERSIONS } from './document-schemas.generated.ts';
|
|
12
|
+
export { CATALOG_SCHEMA_VERSION, catalogSchema, paramSchema, runnerReportSchema, taskSchema, toolInfoSchema, } from '@osdlc/system-model/schema';
|
|
13
|
+
/** A JSON Schema document, as a plain object. */
|
|
14
|
+
export type JsonSchema = Readonly<Record<string, unknown>>;
|
|
15
|
+
/**
|
|
16
|
+
* The `kind` of each document `way` prints: `catalog` (`list`), `query`,
|
|
17
|
+
* `run`, `providers`, `config-<name>` for each `config` subcommand, `trust`,
|
|
18
|
+
* `guide`, `clean`, `help`, `schema`, `error` for a failure and `version`
|
|
19
|
+
* for `--version`.
|
|
20
|
+
*/
|
|
21
|
+
export type DocumentKind = keyof typeof GENERATED_VERSIONS;
|
|
22
|
+
/** Every {@link DocumentKind}. */
|
|
23
|
+
export declare const DOCUMENT_KINDS: readonly DocumentKind[];
|
|
24
|
+
/**
|
|
25
|
+
* The `schemaVersion` of each document, as each command declares it. Raised
|
|
26
|
+
* when a document changes incompatibly.
|
|
27
|
+
*/
|
|
28
|
+
export declare const DOCUMENT_VERSIONS: Readonly<Record<DocumentKind, number>>;
|
|
29
|
+
/**
|
|
30
|
+
* The Zod schema of a whole document: `schemaVersion` and `kind`, then the
|
|
31
|
+
* command's output. The same envelope `@osdlc/cli-tool` prints. A document
|
|
32
|
+
* wayfinder does not declare itself is read from its JSON Schema.
|
|
33
|
+
*/
|
|
34
|
+
export declare function documentZod(kind: DocumentKind): z.ZodObject;
|
|
35
|
+
/** `way list --json`: the catalog with its groups, cache verdicts and unknowns. */
|
|
36
|
+
export declare const catalogDocumentSchema: JsonSchema;
|
|
37
|
+
/** `way query --json`: the matching tasks, best first. */
|
|
38
|
+
export declare const queryDocumentSchema: JsonSchema;
|
|
39
|
+
/** `way providers --json`: every registered tool. */
|
|
40
|
+
export declare const providersDocumentSchema: JsonSchema;
|
|
41
|
+
/** `way run --json`: what ran, or with `--dry-run` what would. */
|
|
42
|
+
export declare const runDocumentSchema: JsonSchema;
|
|
43
|
+
/** `way trust --json`: what a grant, a removal, `--show` or `--list` found. */
|
|
44
|
+
export declare const trustDocumentSchema: JsonSchema;
|
|
45
|
+
/** `way clean --json`: what was removed, or with `--dry-run` what would be. */
|
|
46
|
+
export declare const cleanDocumentSchema: JsonSchema;
|
|
47
|
+
/** Every document schema, by `kind`. */
|
|
48
|
+
export declare const DOCUMENT_SCHEMAS: Readonly<Record<DocumentKind, JsonSchema>>;
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@osdlc/wayfinder/schema`: JSON Schemas (draft 2020-12) for every document
|
|
3
|
+
* `way` prints with `--json`, keyed by each document's `kind`. Every document
|
|
4
|
+
* is an object with `schemaVersion` and `kind` first. `way schema` prints the
|
|
5
|
+
* same schemas with each command's input.
|
|
6
|
+
*
|
|
7
|
+
* The task, report and catalog shapes are `@osdlc/system-model/schema`'s,
|
|
8
|
+
* re-exported here. Browser-safe: no Node built-in.
|
|
9
|
+
*/
|
|
10
|
+
import { z } from 'zod';
|
|
11
|
+
import { GENERATED_SCHEMAS, GENERATED_VERSIONS } from "./document-schemas.generated.js";
|
|
12
|
+
import { CatalogOutput, ProvidersOutput, QueryOutput, RunOutput } from "./documents.js";
|
|
13
|
+
export { CATALOG_SCHEMA_VERSION, catalogSchema, paramSchema, runnerReportSchema, taskSchema, toolInfoSchema, } from '@osdlc/system-model/schema';
|
|
14
|
+
/** Every {@link DocumentKind}. */
|
|
15
|
+
export const DOCUMENT_KINDS = Object.keys(GENERATED_VERSIONS);
|
|
16
|
+
/**
|
|
17
|
+
* The `schemaVersion` of each document, as each command declares it. Raised
|
|
18
|
+
* when a document changes incompatibly.
|
|
19
|
+
*/
|
|
20
|
+
export const DOCUMENT_VERSIONS = GENERATED_VERSIONS;
|
|
21
|
+
/** The documents wayfinder declares itself, as Zod. */
|
|
22
|
+
const OUTPUTS = {
|
|
23
|
+
catalog: CatalogOutput,
|
|
24
|
+
query: QueryOutput,
|
|
25
|
+
providers: ProvidersOutput,
|
|
26
|
+
run: RunOutput,
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The Zod schema of a whole document: `schemaVersion` and `kind`, then the
|
|
30
|
+
* command's output. The same envelope `@osdlc/cli-tool` prints. A document
|
|
31
|
+
* wayfinder does not declare itself is read from its JSON Schema.
|
|
32
|
+
*/
|
|
33
|
+
export function documentZod(kind) {
|
|
34
|
+
const output = OUTPUTS[kind];
|
|
35
|
+
if (output === undefined) {
|
|
36
|
+
return z.fromJSONSchema(GENERATED_SCHEMAS[kind]);
|
|
37
|
+
}
|
|
38
|
+
return z
|
|
39
|
+
.object({ schemaVersion: z.literal(DOCUMENT_VERSIONS[kind]), kind: z.literal(kind) })
|
|
40
|
+
.extend(output.shape);
|
|
41
|
+
}
|
|
42
|
+
function jsonSchema(kind) {
|
|
43
|
+
const generated = GENERATED_SCHEMAS[kind];
|
|
44
|
+
if (generated !== undefined)
|
|
45
|
+
return generated;
|
|
46
|
+
return z.toJSONSchema(documentZod(kind), { io: 'input', unrepresentable: 'any' });
|
|
47
|
+
}
|
|
48
|
+
/** `way list --json`: the catalog with its groups, cache verdicts and unknowns. */
|
|
49
|
+
export const catalogDocumentSchema = jsonSchema('catalog');
|
|
50
|
+
/** `way query --json`: the matching tasks, best first. */
|
|
51
|
+
export const queryDocumentSchema = jsonSchema('query');
|
|
52
|
+
/** `way providers --json`: every registered tool. */
|
|
53
|
+
export const providersDocumentSchema = jsonSchema('providers');
|
|
54
|
+
/** `way run --json`: what ran, or with `--dry-run` what would. */
|
|
55
|
+
export const runDocumentSchema = jsonSchema('run');
|
|
56
|
+
/** `way trust --json`: what a grant, a removal, `--show` or `--list` found. */
|
|
57
|
+
export const trustDocumentSchema = jsonSchema('trust');
|
|
58
|
+
/** `way clean --json`: what was removed, or with `--dry-run` what would be. */
|
|
59
|
+
export const cleanDocumentSchema = jsonSchema('clean');
|
|
60
|
+
/** Every document schema, by `kind`. */
|
|
61
|
+
export const DOCUMENT_SCHEMAS = Object.fromEntries(DOCUMENT_KINDS.map((kind) => [kind, jsonSchema(kind)]));
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signing a browser in to the web view.
|
|
3
|
+
*
|
|
4
|
+
* The command tree is served by cli-tool's HTTP adapter, which owns the
|
|
5
|
+
* bearer token and the Host and Origin checks. What it leaves to the app is
|
|
6
|
+
* how its own page gets that token.
|
|
7
|
+
*
|
|
8
|
+
* Other local users can reach 127.0.0.1 and can read a process's argv while
|
|
9
|
+
* it runs. So the bearer token never appears in a URL, an argv or the page
|
|
10
|
+
* itself. The URL the server prints or opens carries a boot token instead.
|
|
11
|
+
* The page sends it once, in a JSON body to `POST /session`, and gets the
|
|
12
|
+
* bearer token back. The boot token works once and expires a minute after
|
|
13
|
+
* the URL is shown, so it is only usable between opening the browser and the
|
|
14
|
+
* page loading.
|
|
15
|
+
*
|
|
16
|
+
* The boot token is in the browser opener's argv while it runs, so another
|
|
17
|
+
* local user could redeem it first. The right boot token sent a second time
|
|
18
|
+
* is refused, and the server warns in its terminal (see `onReplay`). Sessions
|
|
19
|
+
* already signed in keep working; a new link comes from {@link SignIn.renew}.
|
|
20
|
+
*
|
|
21
|
+
* A JSON body from another origin needs a CORS preflight, which the server
|
|
22
|
+
* never answers, so a website cannot redeem the boot token either.
|
|
23
|
+
*/
|
|
24
|
+
/** The one-shot boot token. */
|
|
25
|
+
export interface SignIn {
|
|
26
|
+
/** Put this in the URL the person opens. The latest one {@link renew} made. */
|
|
27
|
+
readonly bootToken: string;
|
|
28
|
+
/**
|
|
29
|
+
* Start the expiry timer. Call it when the URL is shown: the first probe
|
|
30
|
+
* runs before that, and on a cold tree it can use up much of the minute.
|
|
31
|
+
*/
|
|
32
|
+
arm(): void;
|
|
33
|
+
/** True once, for the first unexpired `candidate` that matches. */
|
|
34
|
+
redeem(candidate: unknown): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Replace the boot token with a new one, armed, and return it. The old one
|
|
37
|
+
* stops working. Bearer tokens already handed out keep working.
|
|
38
|
+
*/
|
|
39
|
+
renew(): string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Make a boot token. `now` is injectable for tests. `onReplay` is called when
|
|
43
|
+
* the right boot token arrives after it was redeemed: a second tab, or
|
|
44
|
+
* another program that read the link.
|
|
45
|
+
*/
|
|
46
|
+
export declare function createSignIn(now?: () => number, onReplay?: () => void): SignIn;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signing a browser in to the web view.
|
|
3
|
+
*
|
|
4
|
+
* The command tree is served by cli-tool's HTTP adapter, which owns the
|
|
5
|
+
* bearer token and the Host and Origin checks. What it leaves to the app is
|
|
6
|
+
* how its own page gets that token.
|
|
7
|
+
*
|
|
8
|
+
* Other local users can reach 127.0.0.1 and can read a process's argv while
|
|
9
|
+
* it runs. So the bearer token never appears in a URL, an argv or the page
|
|
10
|
+
* itself. The URL the server prints or opens carries a boot token instead.
|
|
11
|
+
* The page sends it once, in a JSON body to `POST /session`, and gets the
|
|
12
|
+
* bearer token back. The boot token works once and expires a minute after
|
|
13
|
+
* the URL is shown, so it is only usable between opening the browser and the
|
|
14
|
+
* page loading.
|
|
15
|
+
*
|
|
16
|
+
* The boot token is in the browser opener's argv while it runs, so another
|
|
17
|
+
* local user could redeem it first. The right boot token sent a second time
|
|
18
|
+
* is refused, and the server warns in its terminal (see `onReplay`). Sessions
|
|
19
|
+
* already signed in keep working; a new link comes from {@link SignIn.renew}.
|
|
20
|
+
*
|
|
21
|
+
* A JSON body from another origin needs a CORS preflight, which the server
|
|
22
|
+
* never answers, so a website cannot redeem the boot token either.
|
|
23
|
+
*/
|
|
24
|
+
/** How long the boot token stays usable once the URL is shown. */
|
|
25
|
+
const BOOT_TTL_MS = 60_000;
|
|
26
|
+
/** 128 bits, hex, from a secure source. */
|
|
27
|
+
function secret() {
|
|
28
|
+
const bytes = globalThis.crypto.getRandomValues(new Uint8Array(16));
|
|
29
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
30
|
+
}
|
|
31
|
+
/** Compared without an early exit, so a near miss takes as long as a far one. */
|
|
32
|
+
function tokenEquals(a, b) {
|
|
33
|
+
if (a.length !== b.length)
|
|
34
|
+
return false;
|
|
35
|
+
let diff = 0;
|
|
36
|
+
for (let i = 0; i < a.length; i++)
|
|
37
|
+
diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
38
|
+
return diff === 0;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Make a boot token. `now` is injectable for tests. `onReplay` is called when
|
|
42
|
+
* the right boot token arrives after it was redeemed: a second tab, or
|
|
43
|
+
* another program that read the link.
|
|
44
|
+
*/
|
|
45
|
+
export function createSignIn(now = Date.now, onReplay) {
|
|
46
|
+
let bootToken = secret();
|
|
47
|
+
let live = true;
|
|
48
|
+
let redeemed = false;
|
|
49
|
+
let expiresAt = Number.POSITIVE_INFINITY;
|
|
50
|
+
return {
|
|
51
|
+
get bootToken() {
|
|
52
|
+
return bootToken;
|
|
53
|
+
},
|
|
54
|
+
arm() {
|
|
55
|
+
expiresAt = now() + BOOT_TTL_MS;
|
|
56
|
+
},
|
|
57
|
+
redeem(candidate) {
|
|
58
|
+
if (typeof candidate !== 'string')
|
|
59
|
+
return false;
|
|
60
|
+
if (redeemed && tokenEquals(candidate, bootToken))
|
|
61
|
+
onReplay?.();
|
|
62
|
+
if (!live)
|
|
63
|
+
return false;
|
|
64
|
+
if (now() > expiresAt) {
|
|
65
|
+
live = false;
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
if (!tokenEquals(candidate, bootToken))
|
|
69
|
+
return false;
|
|
70
|
+
live = false;
|
|
71
|
+
redeemed = true;
|
|
72
|
+
return true;
|
|
73
|
+
},
|
|
74
|
+
renew() {
|
|
75
|
+
bootToken = secret();
|
|
76
|
+
live = true;
|
|
77
|
+
redeemed = false;
|
|
78
|
+
expiresAt = now() + BOOT_TTL_MS;
|
|
79
|
+
return bootToken;
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The catalog the web view's server holds between requests.
|
|
3
|
+
*
|
|
4
|
+
* Each request opens its own session, as a CLI run does. Without this, the
|
|
5
|
+
* page's first `list --cached` would read the store again (seconds on a large
|
|
6
|
+
* repository) right after `serve` read it, and every poll would start its
|
|
7
|
+
* own probe. With it:
|
|
8
|
+
*
|
|
9
|
+
* - `list --cached` answers with the newest catalog the server holds, marked
|
|
10
|
+
* `source: "cached"`.
|
|
11
|
+
* - `list --join` (the page's first read and its polls) joins a probe
|
|
12
|
+
* already running. A plain `list` (Refresh) always starts a probe, so it
|
|
13
|
+
* never answers with one that began before an edit.
|
|
14
|
+
*
|
|
15
|
+
* A shared probe runs on a signal this module owns. A request that stops
|
|
16
|
+
* waiting (a closed tab) leaves on its own; the probe stops only when nobody
|
|
17
|
+
* waits for it, or when the server stops. A failed probe fails every waiter
|
|
18
|
+
* and is forgotten, so the next request starts again.
|
|
19
|
+
*
|
|
20
|
+
* An entry is used only by a request whose session has the same view key
|
|
21
|
+
* (trust and settings), so a trust grant or a saved setting is never answered
|
|
22
|
+
* with rows from before it. Only unfiltered listings are shared.
|
|
23
|
+
*
|
|
24
|
+
* A shared probe's progress lines (one per runner as it answers) go to every
|
|
25
|
+
* waiter that asks for them. One that joins late gets the lines sent so far
|
|
26
|
+
* first, so each waiter sees every runner.
|
|
27
|
+
*/
|
|
28
|
+
import type { CatalogBody } from '../commands/list.ts';
|
|
29
|
+
import type { CatalogProgressBody } from '../documents.ts';
|
|
30
|
+
/** Sends one progress line of a shared probe. */
|
|
31
|
+
export type ProgressSink = (line: CatalogProgressBody) => void;
|
|
32
|
+
/** See the module comment. One per command tree; `serve` turns it on. */
|
|
33
|
+
export interface CatalogShare {
|
|
34
|
+
/** True once `serve` has started, until it stops. The command line never shares. */
|
|
35
|
+
readonly active: boolean;
|
|
36
|
+
/** Aborted when the server stops. Sessions that run shared probes use it. */
|
|
37
|
+
readonly signal: AbortSignal;
|
|
38
|
+
activate(): void;
|
|
39
|
+
/** Stop every shared probe and share nothing more. */
|
|
40
|
+
stop(): void;
|
|
41
|
+
/** Keep `body` as the newest catalog for `key`. */
|
|
42
|
+
remember(key: string, body: CatalogBody): void;
|
|
43
|
+
/** The newest catalog for `key`, marked as cached, or null. */
|
|
44
|
+
cached(key: string): CatalogBody | null;
|
|
45
|
+
/** True while a probe for `key` runs. */
|
|
46
|
+
probing(key: string): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* A fresh catalog for `key`. With `join`, the probe already running for it
|
|
49
|
+
* when there is one; otherwise `probe` starts now. `probe` gets the
|
|
50
|
+
* share's signal, not the caller's. `signal` is the caller's own: when it
|
|
51
|
+
* aborts, this call rejects and the probe goes on for the other waiters.
|
|
52
|
+
* `probe` sends its progress lines through its second argument; `onEvent`
|
|
53
|
+
* gets them, the ones sent before this call first.
|
|
54
|
+
*/
|
|
55
|
+
fresh(key: string, probe: (signal: AbortSignal, emit: ProgressSink) => Promise<CatalogBody>, opts: {
|
|
56
|
+
join: boolean;
|
|
57
|
+
signal?: AbortSignal | undefined;
|
|
58
|
+
onEvent?: ProgressSink | undefined;
|
|
59
|
+
}): Promise<CatalogBody>;
|
|
60
|
+
/** How many callers wait on the running probe for `key`. For tests. */
|
|
61
|
+
waiting(key: string): number;
|
|
62
|
+
}
|
|
63
|
+
export declare function createCatalogShare(): CatalogShare;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The catalog the web view's server holds between requests.
|
|
3
|
+
*
|
|
4
|
+
* Each request opens its own session, as a CLI run does. Without this, the
|
|
5
|
+
* page's first `list --cached` would read the store again (seconds on a large
|
|
6
|
+
* repository) right after `serve` read it, and every poll would start its
|
|
7
|
+
* own probe. With it:
|
|
8
|
+
*
|
|
9
|
+
* - `list --cached` answers with the newest catalog the server holds, marked
|
|
10
|
+
* `source: "cached"`.
|
|
11
|
+
* - `list --join` (the page's first read and its polls) joins a probe
|
|
12
|
+
* already running. A plain `list` (Refresh) always starts a probe, so it
|
|
13
|
+
* never answers with one that began before an edit.
|
|
14
|
+
*
|
|
15
|
+
* A shared probe runs on a signal this module owns. A request that stops
|
|
16
|
+
* waiting (a closed tab) leaves on its own; the probe stops only when nobody
|
|
17
|
+
* waits for it, or when the server stops. A failed probe fails every waiter
|
|
18
|
+
* and is forgotten, so the next request starts again.
|
|
19
|
+
*
|
|
20
|
+
* An entry is used only by a request whose session has the same view key
|
|
21
|
+
* (trust and settings), so a trust grant or a saved setting is never answered
|
|
22
|
+
* with rows from before it. Only unfiltered listings are shared.
|
|
23
|
+
*
|
|
24
|
+
* A shared probe's progress lines (one per runner as it answers) go to every
|
|
25
|
+
* waiter that asks for them. One that joins late gets the lines sent so far
|
|
26
|
+
* first, so each waiter sees every runner.
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* Give `line` to one waiter's listener. False when it throws: that waiter
|
|
30
|
+
* gets no more lines, and the probe and the other waiters go on.
|
|
31
|
+
*/
|
|
32
|
+
function tell(listener, line) {
|
|
33
|
+
try {
|
|
34
|
+
listener(line);
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export function createCatalogShare() {
|
|
42
|
+
let active = false;
|
|
43
|
+
const stopper = new AbortController();
|
|
44
|
+
let latest = null;
|
|
45
|
+
let running = null;
|
|
46
|
+
let seq = 0;
|
|
47
|
+
const start = (key, probe) => {
|
|
48
|
+
const controller = new AbortController();
|
|
49
|
+
const onStop = () => controller.abort(stopper.signal.reason);
|
|
50
|
+
stopper.signal.addEventListener('abort', onStop, { once: true });
|
|
51
|
+
const events = [];
|
|
52
|
+
const listeners = new Set();
|
|
53
|
+
const emit = (line) => {
|
|
54
|
+
if (run.settled)
|
|
55
|
+
return;
|
|
56
|
+
events.push(line);
|
|
57
|
+
for (const listener of listeners) {
|
|
58
|
+
if (!tell(listener, line))
|
|
59
|
+
listeners.delete(listener);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const run = {
|
|
63
|
+
key,
|
|
64
|
+
seq: ++seq,
|
|
65
|
+
controller,
|
|
66
|
+
promise: Promise.resolve().then(() => probe(controller.signal, emit)),
|
|
67
|
+
waiters: 0,
|
|
68
|
+
settled: false,
|
|
69
|
+
events,
|
|
70
|
+
listeners,
|
|
71
|
+
};
|
|
72
|
+
running = run;
|
|
73
|
+
const done = () => {
|
|
74
|
+
run.settled = true;
|
|
75
|
+
stopper.signal.removeEventListener('abort', onStop);
|
|
76
|
+
if (running === run)
|
|
77
|
+
running = null;
|
|
78
|
+
};
|
|
79
|
+
run.promise.then((body) => {
|
|
80
|
+
done();
|
|
81
|
+
if (!controller.signal.aborted && (latest === null || latest.seq < run.seq)) {
|
|
82
|
+
latest = { key, seq: run.seq, body };
|
|
83
|
+
}
|
|
84
|
+
}, done);
|
|
85
|
+
return run;
|
|
86
|
+
};
|
|
87
|
+
const wait = (run, signal, onEvent) => new Promise((resolve, reject) => {
|
|
88
|
+
let left = false;
|
|
89
|
+
run.waiters++;
|
|
90
|
+
const leave = () => {
|
|
91
|
+
if (left)
|
|
92
|
+
return;
|
|
93
|
+
left = true;
|
|
94
|
+
run.waiters--;
|
|
95
|
+
if (onEvent !== undefined)
|
|
96
|
+
run.listeners.delete(onEvent);
|
|
97
|
+
signal?.removeEventListener('abort', onAbort);
|
|
98
|
+
if (run.waiters === 0 && !run.settled)
|
|
99
|
+
run.controller.abort(new Error('nobody is waiting'));
|
|
100
|
+
};
|
|
101
|
+
const onAbort = () => {
|
|
102
|
+
leave();
|
|
103
|
+
reject(signal?.reason ?? new Error('aborted'));
|
|
104
|
+
};
|
|
105
|
+
if (signal?.aborted === true)
|
|
106
|
+
return onAbort();
|
|
107
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
108
|
+
// After `leave` is set up, so a listener that throws cannot leak this waiter.
|
|
109
|
+
if (onEvent !== undefined && run.events.every((line) => tell(onEvent, line))) {
|
|
110
|
+
run.listeners.add(onEvent);
|
|
111
|
+
}
|
|
112
|
+
run.promise.then((body) => {
|
|
113
|
+
leave();
|
|
114
|
+
resolve(body);
|
|
115
|
+
}, (err) => {
|
|
116
|
+
leave();
|
|
117
|
+
reject(err);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
return {
|
|
121
|
+
get active() {
|
|
122
|
+
return active && !stopper.signal.aborted;
|
|
123
|
+
},
|
|
124
|
+
signal: stopper.signal,
|
|
125
|
+
activate() {
|
|
126
|
+
active = true;
|
|
127
|
+
},
|
|
128
|
+
stop() {
|
|
129
|
+
active = false;
|
|
130
|
+
stopper.abort(new Error('the server stopped'));
|
|
131
|
+
},
|
|
132
|
+
remember(key, body) {
|
|
133
|
+
latest = { key, seq: latest?.seq ?? 0, body };
|
|
134
|
+
},
|
|
135
|
+
cached(key) {
|
|
136
|
+
if (latest === null || latest.key !== key)
|
|
137
|
+
return null;
|
|
138
|
+
const body = latest.body;
|
|
139
|
+
return body.source === 'cached'
|
|
140
|
+
? body
|
|
141
|
+
: { ...body, source: 'cached', storedAt: body.generatedAt };
|
|
142
|
+
},
|
|
143
|
+
probing(key) {
|
|
144
|
+
return running !== null && running.key === key && !running.settled;
|
|
145
|
+
},
|
|
146
|
+
fresh(key, probe, opts) {
|
|
147
|
+
const joinable = running !== null && running.key === key && !running.settled;
|
|
148
|
+
const run = opts.join && joinable ? running : start(key, probe);
|
|
149
|
+
return wait(run, opts.signal, opts.onEvent);
|
|
150
|
+
},
|
|
151
|
+
waiting(key) {
|
|
152
|
+
return running !== null && running.key === key ? running.waiters : 0;
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
}
|