@otea/cli 0.0.0-stage → 0.1.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/LICENSE +661 -0
- package/README.es.md +151 -0
- package/README.md +252 -2
- package/THIRD_PARTY_NOTICES.md +114 -0
- package/bin/otea.mjs +28 -0
- package/dist/app/agent-detail.js +26 -0
- package/dist/app/agent-draft.js +429 -0
- package/dist/app/agent-prompts.js +52 -0
- package/dist/app/agents.js +77 -0
- package/dist/app/asking.js +43 -0
- package/dist/app/board-archive.js +243 -0
- package/dist/app/board-migration.js +272 -0
- package/dist/app/boards.js +870 -0
- package/dist/app/branch-owner.js +48 -0
- package/dist/app/brief.js +53 -0
- package/dist/app/choices.js +61 -0
- package/dist/app/collections.js +101 -0
- package/dist/app/context.js +1 -0
- package/dist/app/crop.js +56 -0
- package/dist/app/devices.js +62 -0
- package/dist/app/draft-request.js +82 -0
- package/dist/app/events.js +12 -0
- package/dist/app/evidence.js +287 -0
- package/dist/app/gates.js +610 -0
- package/dist/app/guide.js +37 -0
- package/dist/app/home.js +145 -0
- package/dist/app/init-boards.js +36 -0
- package/dist/app/intake.js +25 -0
- package/dist/app/item-search.js +141 -0
- package/dist/app/items.js +318 -0
- package/dist/app/keep-waiting.js +44 -0
- package/dist/app/land-order.js +107 -0
- package/dist/app/language.js +52 -0
- package/dist/app/launcher.js +56 -0
- package/dist/app/lease-take.js +54 -0
- package/dist/app/leases.js +86 -0
- package/dist/app/machine.js +39 -0
- package/dist/app/merged.js +89 -0
- package/dist/app/migrate.js +171 -0
- package/dist/app/next.js +1637 -0
- package/dist/app/notes.js +319 -0
- package/dist/app/notify-targets.js +113 -0
- package/dist/app/opened.js +25 -0
- package/dist/app/owner-only.js +34 -0
- package/dist/app/performance.js +28 -0
- package/dist/app/phone.js +97 -0
- package/dist/app/presence.js +596 -0
- package/dist/app/preset-views.js +25 -0
- package/dist/app/presets.js +129 -0
- package/dist/app/priority.js +156 -0
- package/dist/app/profile.js +86 -0
- package/dist/app/project.js +247 -0
- package/dist/app/proofs.js +179 -0
- package/dist/app/questions.js +215 -0
- package/dist/app/reader-filter.js +58 -0
- package/dist/app/relations.js +164 -0
- package/dist/app/relay.js +98 -0
- package/dist/app/repos.js +176 -0
- package/dist/app/restart.js +92 -0
- package/dist/app/set-aside.js +64 -0
- package/dist/app/system-locale.js +6 -0
- package/dist/app/tailscale-cli.js +77 -0
- package/dist/app/tailscale-testing.js +41 -0
- package/dist/app/tailscale.js +177 -0
- package/dist/app/tips.js +30 -0
- package/dist/app/topics-everywhere.js +53 -0
- package/dist/app/topics.js +453 -0
- package/dist/app/undo.js +52 -0
- package/dist/app/validate.js +131 -0
- package/dist/app/watch.js +48 -0
- package/dist/cli/command.js +44 -0
- package/dist/cli/commands.js +3303 -0
- package/dist/cli/exit-codes.js +70 -0
- package/dist/cli/forge.js +176 -0
- package/dist/cli/format.js +340 -0
- package/dist/cli/gh.js +17 -0
- package/dist/cli/gitrepo.js +489 -0
- package/dist/cli/help.js +101 -0
- package/dist/cli/hooks.js +232 -0
- package/dist/cli/main.js +37 -0
- package/dist/cli/merged-outside.js +58 -0
- package/dist/cli/names.js +154 -0
- package/dist/cli/opener.js +21 -0
- package/dist/cli/owner.js +154 -0
- package/dist/cli/phone.js +221 -0
- package/dist/cli/ports.js +24 -0
- package/dist/cli/probe.js +18 -0
- package/dist/cli/run.js +246 -0
- package/dist/cli/serve-file.js +21 -0
- package/dist/cli/stats.js +55 -0
- package/dist/cli/version.js +3 -0
- package/dist/core/agent-guide.js +31 -0
- package/dist/core/agent-prompts.js +139 -0
- package/dist/core/authority.js +94 -0
- package/dist/core/board/analysis.js +86 -0
- package/dist/core/board/choices.js +100 -0
- package/dist/core/board/compile.js +362 -0
- package/dist/core/board/engine.js +523 -0
- package/dist/core/board/hints.js +46 -0
- package/dist/core/board/history.js +146 -0
- package/dist/core/board/ids.js +87 -0
- package/dist/core/board/joins.js +30 -0
- package/dist/core/board/mentions.js +68 -0
- package/dist/core/board/merged.js +18 -0
- package/dist/core/board/model.js +135 -0
- package/dist/core/board/next.js +1116 -0
- package/dist/core/board/notes.js +298 -0
- package/dist/core/board/performance.js +218 -0
- package/dist/core/board/plan.js +299 -0
- package/dist/core/board/problems.js +232 -0
- package/dist/core/board/proofs.js +84 -0
- package/dist/core/board/questions.js +114 -0
- package/dist/core/board/reading.js +220 -0
- package/dist/core/board/readings.js +534 -0
- package/dist/core/board/schema.js +335 -0
- package/dist/core/board/shape-words.js +179 -0
- package/dist/core/board/template-words.js +23 -0
- package/dist/core/board/templates-catalogue.js +965 -0
- package/dist/core/board/templates-coverage.js +261 -0
- package/dist/core/board/templates-flows.js +779 -0
- package/dist/core/board/templates.js +586 -0
- package/dist/core/brief.js +93 -0
- package/dist/core/diagram.js +80 -0
- package/dist/core/errors.js +12 -0
- package/dist/core/evidence.js +33 -0
- package/dist/core/fields.js +129 -0
- package/dist/core/hierarchy.js +30 -0
- package/dist/core/icons.js +181 -0
- package/dist/core/image-crop.js +166 -0
- package/dist/core/item.js +49 -0
- package/dist/core/lease.js +31 -0
- package/dist/core/modules.js +318 -0
- package/dist/core/notify/planner.js +117 -0
- package/dist/core/notify/signals.js +66 -0
- package/dist/core/notify/text.js +60 -0
- package/dist/core/path-key.js +22 -0
- package/dist/core/placement.js +161 -0
- package/dist/core/preset-labels.js +66 -0
- package/dist/core/preset-notes.js +109 -0
- package/dist/core/preset.js +541 -0
- package/dist/core/profile.js +55 -0
- package/dist/core/project-id.js +20 -0
- package/dist/core/refusal.js +1 -0
- package/dist/core/relations.js +208 -0
- package/dist/core/repo.js +54 -0
- package/dist/core/samples.js +161 -0
- package/dist/core/search-words.js +68 -0
- package/dist/core/test-report.js +120 -0
- package/dist/core/time.js +8 -0
- package/dist/core/validation.js +60 -0
- package/dist/core/views.js +171 -0
- package/dist/core/workflow.js +51 -0
- package/dist/mcp/commands.js +101 -0
- package/dist/mcp/server.js +239 -0
- package/dist/mcp/words.js +95 -0
- package/dist/server/boards.js +232 -0
- package/dist/server/changes.js +42 -0
- package/dist/server/diff-tip.js +83 -0
- package/dist/server/drift.js +87 -0
- package/dist/server/errors.js +87 -0
- package/dist/server/evidence.js +82 -0
- package/dist/server/hosts.js +6 -0
- package/dist/server/hub.js +98 -0
- package/dist/server/lan-access.js +62 -0
- package/dist/server/lan.js +164 -0
- package/dist/server/listen.js +70 -0
- package/dist/server/notifier.js +156 -0
- package/dist/server/notify-api.js +151 -0
- package/dist/server/pairing.js +124 -0
- package/dist/server/projects.js +191 -0
- package/dist/server/push.js +199 -0
- package/dist/server/server.js +960 -0
- package/dist/server/session.js +43 -0
- package/dist/server/stream.js +36 -0
- package/dist/server/test-push.js +38 -0
- package/dist/server/token.js +37 -0
- package/dist/server/transports.js +54 -0
- package/dist/server/ui.js +44 -0
- package/dist/server/writes.js +361 -0
- package/dist/server/wrong-address.js +36 -0
- package/dist/store/boards.js +111 -0
- package/dist/store/database.js +45 -0
- package/dist/store/machine.js +77 -0
- package/dist/store/migrations.js +493 -0
- package/dist/store/queries.js +585 -0
- package/dist/store/registry.js +72 -0
- package/package.json +73 -4
- package/presets/architecture/playbook.md +69 -0
- package/presets/architecture/preset.yaml +80 -0
- package/presets/architecture/samples.yaml +23 -0
- package/presets/bug-triage/playbook.md +129 -0
- package/presets/bug-triage/preset.yaml +127 -0
- package/presets/bug-triage/samples/wall-slide-after.png +0 -0
- package/presets/bug-triage/samples/wall-slide-before.png +0 -0
- package/presets/bug-triage/samples.yaml +72 -0
- package/presets/change/playbook.md +115 -0
- package/presets/change/preset.yaml +141 -0
- package/presets/change/samples.yaml +62 -0
- package/presets/review/playbook.md +51 -0
- package/presets/review/preset.yaml +62 -0
- package/presets/review/samples.yaml +27 -0
- package/presets/tasks/playbook.md +78 -0
- package/presets/tasks/preset.yaml +61 -0
- package/presets/tasks/samples.yaml +46 -0
- package/schemas/board.schema.json +764 -0
- package/ui/dist/THIRD_PARTY_NOTICES.md +114 -0
- package/ui/dist/assets/dm-mono-latin-400-normal--0xN8mdc.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-400-normal-4GdczIuU.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-500-normal-CN8Miw6E.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-500-normal-DRMDZjhP.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-400-normal-1aZr6b2b.woff +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-400-normal-C2zvOubV.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-500-normal-BtRyHRi6.woff2 +0 -0
- package/ui/dist/assets/dm-mono-latin-ext-500-normal-Dw3M13d8.woff +0 -0
- package/ui/dist/assets/index-C3OzJope.js +103 -0
- package/ui/dist/assets/index-COdEjckQ.css +1 -0
- package/ui/dist/assets/newsreader-latin-ext-opsz-italic-CSPGnFdO.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-ext-opsz-normal-BQn1nviT.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-opsz-italic-kJKFiXvB.woff2 +0 -0
- package/ui/dist/assets/newsreader-latin-opsz-normal-s-izfB6B.woff2 +0 -0
- package/ui/dist/assets/newsreader-vietnamese-opsz-italic-DTr-ymY4.woff2 +0 -0
- package/ui/dist/assets/newsreader-vietnamese-opsz-normal-BjhtXyW2.woff2 +0 -0
- package/ui/dist/assets/pdf-YwugTw7Y.js +54 -0
- package/ui/dist/assets/pdf-engine-CkBopwck.js +1 -0
- package/ui/dist/assets/pdf-rELvHkft.js +56 -0
- package/ui/dist/assets/pdf.worker.min-B5Yc7dP4.mjs +27 -0
- package/ui/dist/assets/pdf.worker.min-B7U8ptvh.js +1 -0
- package/ui/dist/assets/pdf.worker.min-CIqJB0yW.mjs +28 -0
- package/ui/dist/assets/pdf.worker.min-UqOn5EDC.js +1 -0
- package/ui/dist/assets/schibsted-grotesk-latin-ext-wght-normal-hsMS0n0O.woff2 +0 -0
- package/ui/dist/assets/schibsted-grotesk-latin-wght-normal-Bb8VGrTG.woff2 +0 -0
- package/ui/dist/icons/apple-touch-icon.png +0 -0
- package/ui/dist/icons/otea-192.png +0 -0
- package/ui/dist/icons/otea-512.png +0 -0
- package/ui/dist/icons/otea-badge.png +0 -0
- package/ui/dist/icons/otea-maskable-512.png +0 -0
- package/ui/dist/index.html +25 -0
- package/ui/dist/manifest.webmanifest +24 -0
- package/ui/dist/otea-idle.svg +5 -0
- package/ui/dist/otea-working.svg +5 -0
- package/ui/dist/otea-yours-working.svg +5 -0
- package/ui/dist/otea-yours.svg +5 -0
- package/ui/dist/sw.js +1 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs';
|
|
2
|
+
import { Readable } from 'node:stream';
|
|
3
|
+
import { evidenceToServe } from '../app/evidence.js';
|
|
4
|
+
// Evidence is whatever an agent attached, served from the app's own origin, so nothing in it may
|
|
5
|
+
// run there. Images and videos (the kinds exclude SVG) are shown inline with their type; diffs, diagrams and
|
|
6
|
+
// logs as plain text; anything else only as a download. nosniff keeps the browser to the declared
|
|
7
|
+
// type, and the sandbox policy denies scripts and the origin to a file opened on its own.
|
|
8
|
+
function presentation({ kind, contentType }) {
|
|
9
|
+
if (kind === 'image' || kind === 'video' || kind === 'audio')
|
|
10
|
+
return { type: contentType, inline: true };
|
|
11
|
+
if (kind === 'diff' || kind === 'log' || kind === 'diagram') {
|
|
12
|
+
return { type: 'text/plain; charset=utf-8', inline: true };
|
|
13
|
+
}
|
|
14
|
+
return { type: contentType, inline: false };
|
|
15
|
+
}
|
|
16
|
+
function disposition(inline, filename) {
|
|
17
|
+
const ascii = filename.replace(/[^\x20-\x7e]|["\\]/g, '_');
|
|
18
|
+
const encoded = encodeURIComponent(filename).replace(/['()*]/g, (character) => `%${character.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
19
|
+
return `${inline ? 'inline' : 'attachment'}; filename="${ascii}"; filename*=UTF-8''${encoded}`;
|
|
20
|
+
}
|
|
21
|
+
const RANGE = /^bytes=(\d*)-(\d*)$/;
|
|
22
|
+
// One byte range (a video player seeking), inclusive. undefined: serve the whole file (no range, or
|
|
23
|
+
// one this does not read, such as several ranges); 'unsatisfiable': past the end.
|
|
24
|
+
function rangeOf(header, size) {
|
|
25
|
+
const match = header === undefined ? null : RANGE.exec(header.trim());
|
|
26
|
+
if (!match)
|
|
27
|
+
return undefined;
|
|
28
|
+
const [, from = '', to = ''] = match;
|
|
29
|
+
if (from === '' && to === '')
|
|
30
|
+
return undefined;
|
|
31
|
+
if (from === '') {
|
|
32
|
+
const suffix = Number(to);
|
|
33
|
+
return suffix === 0 || size === 0
|
|
34
|
+
? 'unsatisfiable'
|
|
35
|
+
: { start: Math.max(0, size - suffix), end: size - 1 };
|
|
36
|
+
}
|
|
37
|
+
const start = Number(from);
|
|
38
|
+
if (start >= size)
|
|
39
|
+
return 'unsatisfiable';
|
|
40
|
+
const end = to === '' ? size - 1 : Math.min(Number(to), size - 1);
|
|
41
|
+
return end < start ? undefined : { start, end };
|
|
42
|
+
}
|
|
43
|
+
// If-None-Match lists the tags of the copies a browser keeps, or * for any.
|
|
44
|
+
function matches(header, etag) {
|
|
45
|
+
if (header === undefined)
|
|
46
|
+
return false;
|
|
47
|
+
return header.split(',').some((tag) => {
|
|
48
|
+
const trimmed = tag.trim();
|
|
49
|
+
return trimmed === '*' || trimmed.replace(/^W\//, '') === etag;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
export function serveEvidence(c, project, id) {
|
|
53
|
+
const { evidence, path } = evidenceToServe(project, id);
|
|
54
|
+
const { type, inline } = presentation(evidence);
|
|
55
|
+
const etag = `"${project.identity.id}:${evidence.sha256}"`;
|
|
56
|
+
const headers = {
|
|
57
|
+
'content-type': type,
|
|
58
|
+
'content-disposition': disposition(inline, evidence.filename),
|
|
59
|
+
'accept-ranges': 'bytes',
|
|
60
|
+
'x-content-type-options': 'nosniff',
|
|
61
|
+
'content-security-policy': 'sandbox',
|
|
62
|
+
// The URL names an entry by its number, which another project served later on this host and
|
|
63
|
+
// port reuses for another file. So the browser asks every time, and the tag that lets it keep
|
|
64
|
+
// its copy names this project and the file's content.
|
|
65
|
+
'cache-control': 'private, no-cache',
|
|
66
|
+
etag,
|
|
67
|
+
};
|
|
68
|
+
if (matches(c.req.header('if-none-match'), etag))
|
|
69
|
+
return c.body(null, 304, headers);
|
|
70
|
+
const range = rangeOf(c.req.header('range'), evidence.size);
|
|
71
|
+
if (range === 'unsatisfiable') {
|
|
72
|
+
return c.body(null, 416, { ...headers, 'content-range': `bytes */${evidence.size}` });
|
|
73
|
+
}
|
|
74
|
+
const span = range ?? { start: 0, end: evidence.size - 1 };
|
|
75
|
+
const stream = createReadStream(path, evidence.size === 0 ? {} : span);
|
|
76
|
+
const body = Readable.toWeb(stream);
|
|
77
|
+
const length = { 'content-length': String(evidence.size === 0 ? 0 : span.end - span.start + 1) };
|
|
78
|
+
if (!range)
|
|
79
|
+
return c.body(body, 200, { ...headers, ...length });
|
|
80
|
+
const contentRange = `bytes ${span.start}-${span.end}/${evidence.size}`;
|
|
81
|
+
return c.body(body, 206, { ...headers, ...length, 'content-range': contentRange });
|
|
82
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const DEFAULT_PORT = 4400;
|
|
2
|
+
// The API: the computer's at /api, a project's at /p/<id>/api.
|
|
3
|
+
export const isApiPath = (path) => /^(?:\/p\/[^/]+)?\/api(?:\/|$)/.test(path);
|
|
4
|
+
export function isAllowedHost(host, port) {
|
|
5
|
+
return host === `127.0.0.1:${port}` || host === `localhost:${port}`;
|
|
6
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { kindOf, listProjects, withRegistry } from '../app/home.js';
|
|
3
|
+
import { forgetLocalProject, localProjects } from '../app/machine.js';
|
|
4
|
+
import { databasePath, openProject, openRegistered, oteaDir, } from '../app/project.js';
|
|
5
|
+
// Projects already open, as a test serves them: the caller owns their databases.
|
|
6
|
+
export function openProjects(...projects) {
|
|
7
|
+
const served = (project) => ({
|
|
8
|
+
id: project.identity.id,
|
|
9
|
+
name: project.identity.name,
|
|
10
|
+
storage: project.storage,
|
|
11
|
+
paths: [{ path: project.root, kind: kindOf(project.root), missing: false }],
|
|
12
|
+
project,
|
|
13
|
+
});
|
|
14
|
+
return {
|
|
15
|
+
list: () => projects.map(served),
|
|
16
|
+
get: (id) => projects.find((project) => project.identity.id === id),
|
|
17
|
+
close: () => { },
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
const failure = (error) => (error instanceof Error ? error.message : String(error));
|
|
21
|
+
const byName = (a, b) => a.name.localeCompare(b.name) || a.id.localeCompare(b.id);
|
|
22
|
+
// The registry and the local projects in machine.db, read again on every call (another process
|
|
23
|
+
// registers a project, or `otea serve` adds a local one, while this one runs).
|
|
24
|
+
export function machineProjects(env, machine) {
|
|
25
|
+
const opened = new Map();
|
|
26
|
+
const keep = (id, open) => {
|
|
27
|
+
const held = opened.get(id);
|
|
28
|
+
if (held)
|
|
29
|
+
return held;
|
|
30
|
+
const project = open();
|
|
31
|
+
opened.set(id, project);
|
|
32
|
+
return project;
|
|
33
|
+
};
|
|
34
|
+
const registered = (listed) => {
|
|
35
|
+
const { id, name, storage, paths } = listed;
|
|
36
|
+
const base = { id, name, storage, paths };
|
|
37
|
+
if (storage === 'common' && !existsSync(databasePath(listed.dataDir))) {
|
|
38
|
+
return { ...base, unavailable: `The data of ${name} is missing (${listed.dataDir})` };
|
|
39
|
+
}
|
|
40
|
+
const root = paths.find((path) => !path.missing)?.path ?? paths[0]?.path ?? listed.dataDir;
|
|
41
|
+
try {
|
|
42
|
+
return { ...base, project: keep(id, () => openRegistered(listed, root)) };
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
return { ...base, unavailable: failure(error) };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const local = (id, root) => {
|
|
49
|
+
// Gone (moved to common storage, or its folder deleted): no longer served.
|
|
50
|
+
if (!existsSync(databasePath(oteaDir(root)))) {
|
|
51
|
+
forgetLocalProject(machine, id);
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const project = keep(id, () => openProject(root, { ...env, OTEA_PROJECT: root }));
|
|
56
|
+
if (project.identity.id !== id)
|
|
57
|
+
return undefined;
|
|
58
|
+
const { name } = project.identity;
|
|
59
|
+
return {
|
|
60
|
+
id,
|
|
61
|
+
name,
|
|
62
|
+
storage: 'local',
|
|
63
|
+
paths: [{ path: root, kind: kindOf(root), missing: false }],
|
|
64
|
+
project,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
return {
|
|
69
|
+
id,
|
|
70
|
+
name: id,
|
|
71
|
+
storage: 'local',
|
|
72
|
+
paths: [{ path: root, kind: 'folder', missing: true }],
|
|
73
|
+
unavailable: failure(error),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const list = () => {
|
|
78
|
+
const listed = withRegistry(env, listProjects) ?? [];
|
|
79
|
+
const served = listed.map(registered);
|
|
80
|
+
for (const { id, root } of localProjects(machine)) {
|
|
81
|
+
if (served.some((each) => each.id === id))
|
|
82
|
+
continue;
|
|
83
|
+
const one = local(id, root);
|
|
84
|
+
if (one)
|
|
85
|
+
served.push(one);
|
|
86
|
+
}
|
|
87
|
+
return served.sort(byName);
|
|
88
|
+
};
|
|
89
|
+
return {
|
|
90
|
+
list,
|
|
91
|
+
get: (id) => opened.get(id) ?? list().find((each) => each.id === id)?.project,
|
|
92
|
+
close: () => {
|
|
93
|
+
for (const project of opened.values())
|
|
94
|
+
project.db.close();
|
|
95
|
+
opened.clear();
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { RejectedRequest } from './errors.js';
|
|
2
|
+
import { lanTransport } from './transports.js';
|
|
3
|
+
function unavailable(message) {
|
|
4
|
+
return new RejectedRequest(409, 'transport_unavailable', message);
|
|
5
|
+
}
|
|
6
|
+
export function lanAccess(fetch, port, network) {
|
|
7
|
+
let opened;
|
|
8
|
+
// One change at a time: an open and a close sent together run in order.
|
|
9
|
+
let queue = Promise.resolve();
|
|
10
|
+
const serially = (change) => {
|
|
11
|
+
const next = queue.then(change);
|
|
12
|
+
queue = next.catch(() => undefined);
|
|
13
|
+
return next;
|
|
14
|
+
};
|
|
15
|
+
const stopAll = (stops) => Promise.all(stops.map((stop) => stop())).then(() => { });
|
|
16
|
+
const open = async () => {
|
|
17
|
+
if (opened)
|
|
18
|
+
return;
|
|
19
|
+
if (!network)
|
|
20
|
+
throw unavailable('This server cannot open the local network; restart otea serve');
|
|
21
|
+
const { addresses, names } = await network.hosts();
|
|
22
|
+
const stops = [];
|
|
23
|
+
for (const address of addresses) {
|
|
24
|
+
try {
|
|
25
|
+
stops.push(await network.listen(fetch, address, port()));
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
await stopAll(stops);
|
|
29
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
30
|
+
throw unavailable(`Could not listen on ${address}:${port()} (${reason})`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
opened = { transport: lanTransport([...addresses, ...names]), stops };
|
|
34
|
+
};
|
|
35
|
+
const close = async () => {
|
|
36
|
+
const was = opened;
|
|
37
|
+
opened = undefined;
|
|
38
|
+
if (was)
|
|
39
|
+
await stopAll(was.stops);
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
isOpen: () => opened !== undefined,
|
|
43
|
+
transport: () => opened?.transport,
|
|
44
|
+
open: () => serially(open),
|
|
45
|
+
close: () => serially(close),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
// A door opened by the caller, who already listens on every interface (tests, and servers given
|
|
49
|
+
// their addresses up front): open from the start, nothing to stop.
|
|
50
|
+
export function openLan(hosts) {
|
|
51
|
+
let transport = lanTransport([...hosts.addresses, ...hosts.names]);
|
|
52
|
+
return {
|
|
53
|
+
isOpen: () => transport !== undefined,
|
|
54
|
+
transport: () => transport,
|
|
55
|
+
open: async () => {
|
|
56
|
+
transport ??= lanTransport([...hosts.addresses, ...hosts.names]);
|
|
57
|
+
},
|
|
58
|
+
close: async () => {
|
|
59
|
+
transport = undefined;
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { randomInt } from 'node:crypto';
|
|
2
|
+
import { lookup } from 'node:dns/promises';
|
|
3
|
+
import { hostname, networkInterfaces } from 'node:os';
|
|
4
|
+
import { sameToken } from './token.js';
|
|
5
|
+
// LAN access (Settings › Phone, otea phone lan --open, otea serve --lan): the server also listens
|
|
6
|
+
// on this computer's private addresses, and a request that does not come from this computer gets past the door only with a device token, earned once with a pairing
|
|
7
|
+
// code shown on the desk. Everything here is the policy; server.ts applies it.
|
|
8
|
+
function octets(ip) {
|
|
9
|
+
const parts = ip.split('.');
|
|
10
|
+
if (parts.length !== 4)
|
|
11
|
+
return undefined;
|
|
12
|
+
const numbers = parts.map((part) => (/^\d{1,3}$/.test(part) ? Number(part) : Number.NaN));
|
|
13
|
+
return numbers.every((n) => n >= 0 && n <= 255) ? numbers : undefined;
|
|
14
|
+
}
|
|
15
|
+
// Node reports IPv4 peers on a dual-stack socket as IPv4-mapped IPv6 addresses.
|
|
16
|
+
export function plainAddress(address) {
|
|
17
|
+
return address.startsWith('::ffff:') && octets(address.slice(7)) ? address.slice(7) : address;
|
|
18
|
+
}
|
|
19
|
+
export function isLoopback(address) {
|
|
20
|
+
const ip = plainAddress(address);
|
|
21
|
+
return ip === '::1' || octets(ip)?.[0] === 127;
|
|
22
|
+
}
|
|
23
|
+
// The networks a phone on the same network (or on a Tailscale tailnet) reaches the desk from:
|
|
24
|
+
// private ranges, CGNAT (Tailscale's 100.64.0.0/10) and link-local. Nothing public.
|
|
25
|
+
export function isPrivateAddress(address) {
|
|
26
|
+
const ip = octets(plainAddress(address));
|
|
27
|
+
if (!ip)
|
|
28
|
+
return false;
|
|
29
|
+
const [a = -1, b = -1] = ip;
|
|
30
|
+
return (a === 10 ||
|
|
31
|
+
(a === 172 && b >= 16 && b <= 31) ||
|
|
32
|
+
(a === 192 && b === 168) ||
|
|
33
|
+
(a === 100 && b >= 64 && b <= 127) ||
|
|
34
|
+
(a === 169 && b === 254));
|
|
35
|
+
}
|
|
36
|
+
// Virtual adapters (VirtualBox, VMware, Hyper-V and WSL, Docker) no phone can reach.
|
|
37
|
+
const VIRTUAL = /vethernet|virtual|vbox|vmware|docker|wsl|hyper-v|^br-|^virbr|^veth|^vmnet/i;
|
|
38
|
+
// Wi-Fi and wired networks first, virtual adapters last (VirtualBox's host-only 192.168.56.0/24
|
|
39
|
+
// often hides behind a plain name); then home ranges before office ones, Tailscale after them.
|
|
40
|
+
export function rankAddress(name, address) {
|
|
41
|
+
const [a = 0, b = 0, c = 0] = octets(address) ?? [];
|
|
42
|
+
const virtual = VIRTUAL.test(name) || (a === 192 && b === 168 && c === 56);
|
|
43
|
+
const wireless = /wi-?fi|wlan|wireless/i.test(name);
|
|
44
|
+
const range = a === 192 ? 0 : a === 10 ? 1 : a === 100 ? 2 : a === 172 ? 3 : 4;
|
|
45
|
+
return (virtual ? 20 : wireless ? 0 : 10) + range;
|
|
46
|
+
}
|
|
47
|
+
// This computer's addresses on its networks, the hosts a paired device may name, best guess first.
|
|
48
|
+
export function lanAddresses() {
|
|
49
|
+
return Object.entries(networkInterfaces())
|
|
50
|
+
.flatMap(([name, infos = []]) => infos
|
|
51
|
+
.filter((info) => info.family === 'IPv4' && !info.internal && isPrivateAddress(info.address))
|
|
52
|
+
.map((info) => ({ address: info.address, rank: rankAddress(name, info.address) })))
|
|
53
|
+
.sort((x, y) => x.rank - y.rank)
|
|
54
|
+
.map(({ address }) => address);
|
|
55
|
+
}
|
|
56
|
+
// This computer's mDNS name, `<hostname>.local`, lower case; none for a hostname that is not one
|
|
57
|
+
// DNS label (macOS already ends it in .local).
|
|
58
|
+
export function localNameOf(host) {
|
|
59
|
+
const label = host.toLowerCase().replace(/\.local$/, '');
|
|
60
|
+
return /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/.test(label) ? `${label}.local` : undefined;
|
|
61
|
+
}
|
|
62
|
+
const resolveIPv4 = async (name) => (await lookup(name, { all: true, family: 4 })).map(({ address }) => address);
|
|
63
|
+
// The .local name a phone can use instead of an IP that DHCP may change: kept only when it resolves
|
|
64
|
+
// (here, through the system's mDNS) to one of the addresses LAN access answers on. Gives up quietly
|
|
65
|
+
// after `timeoutMs`.
|
|
66
|
+
export async function localName(addresses, { host = hostname(), resolve = resolveIPv4, timeoutMs = 1500 } = {}) {
|
|
67
|
+
const name = localNameOf(host);
|
|
68
|
+
if (!name || addresses.length === 0)
|
|
69
|
+
return undefined;
|
|
70
|
+
const timeout = new Promise((done) => setTimeout(() => done([]), timeoutMs).unref());
|
|
71
|
+
const resolved = await Promise.race([resolve(name).catch(() => []), timeout]);
|
|
72
|
+
return resolved.some((address) => addresses.includes(address)) ? name : undefined;
|
|
73
|
+
}
|
|
74
|
+
export const CODE_TTL_MS = 10 * 60 * 1000;
|
|
75
|
+
// How recently a code was issued, or read by a phone, for the pairing dialog to keep it when it
|
|
76
|
+
// opens: another tab or otea serve just showed it, or a phone is pairing with it.
|
|
77
|
+
export const CODE_KEPT_MS = 60 * 1000;
|
|
78
|
+
// Wrong codes the current one survives; then it is thrown away and the desk shows a new one.
|
|
79
|
+
export const MAX_WRONG_CODES = 5;
|
|
80
|
+
// Pairing attempts one address may make per window, right or wrong.
|
|
81
|
+
export const MAX_ATTEMPTS = 10;
|
|
82
|
+
export const ATTEMPT_WINDOW_MS = 60 * 1000;
|
|
83
|
+
// No 0/O or 1/I: the code is read off a screen and typed on a phone.
|
|
84
|
+
const ALPHABET = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
|
|
85
|
+
const CODE_LENGTH = 8;
|
|
86
|
+
export function normalizeCode(code) {
|
|
87
|
+
return code.toUpperCase().replace(/[\s-]/g, '');
|
|
88
|
+
}
|
|
89
|
+
export function formatCode(code) {
|
|
90
|
+
return `${code.slice(0, 4)}-${code.slice(4)}`;
|
|
91
|
+
}
|
|
92
|
+
// One code at a time: issuing a new one replaces the old, a right code is used up at once, and
|
|
93
|
+
// MAX_WRONG_CODES wrong ones burn it.
|
|
94
|
+
export class PairingCodes {
|
|
95
|
+
#current;
|
|
96
|
+
#wrong = 0;
|
|
97
|
+
// When the current code was issued or last read right by a phone.
|
|
98
|
+
#usedAt = 0;
|
|
99
|
+
#now;
|
|
100
|
+
constructor(now = Date.now) {
|
|
101
|
+
this.#now = now;
|
|
102
|
+
}
|
|
103
|
+
issue() {
|
|
104
|
+
let code = '';
|
|
105
|
+
for (let index = 0; index < CODE_LENGTH; index += 1)
|
|
106
|
+
code += ALPHABET[randomInt(ALPHABET.length)];
|
|
107
|
+
this.#usedAt = this.#now();
|
|
108
|
+
this.#current = { code, expiresAt: this.#usedAt + CODE_TTL_MS };
|
|
109
|
+
this.#wrong = 0;
|
|
110
|
+
return this.#current;
|
|
111
|
+
}
|
|
112
|
+
// The code the pairing dialog opens on, with its whole lifetime ahead: a new one, unless the
|
|
113
|
+
// current one was issued or read by a phone in the last CODE_KEPT_MS.
|
|
114
|
+
fresh() {
|
|
115
|
+
const current = this.current();
|
|
116
|
+
return current && this.#now() - this.#usedAt < CODE_KEPT_MS ? current : this.issue();
|
|
117
|
+
}
|
|
118
|
+
// The code the desk shows, while it is still good.
|
|
119
|
+
current() {
|
|
120
|
+
const current = this.#current;
|
|
121
|
+
return current && current.expiresAt > this.#now() ? current : undefined;
|
|
122
|
+
}
|
|
123
|
+
// Checks a code without using it up (the phone shows what it is about to pair with).
|
|
124
|
+
check(given) {
|
|
125
|
+
const current = this.#current;
|
|
126
|
+
if (!current || !sameToken(normalizeCode(given), current.code)) {
|
|
127
|
+
this.#wrong += 1;
|
|
128
|
+
if (this.#wrong >= MAX_WRONG_CODES)
|
|
129
|
+
this.#current = undefined;
|
|
130
|
+
return 'wrong';
|
|
131
|
+
}
|
|
132
|
+
const now = this.#now();
|
|
133
|
+
if (current.expiresAt <= now)
|
|
134
|
+
return 'expired';
|
|
135
|
+
this.#usedAt = now;
|
|
136
|
+
return 'ok';
|
|
137
|
+
}
|
|
138
|
+
redeem(given) {
|
|
139
|
+
const result = this.check(given);
|
|
140
|
+
if (result !== 'wrong')
|
|
141
|
+
this.#current = undefined;
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// A fixed window per address: at most MAX_ATTEMPTS pairing attempts a minute.
|
|
146
|
+
export class AttemptLimiter {
|
|
147
|
+
#windows = new Map();
|
|
148
|
+
#now;
|
|
149
|
+
constructor(now = Date.now) {
|
|
150
|
+
this.#now = now;
|
|
151
|
+
}
|
|
152
|
+
// Counts one attempt and says whether it may go ahead.
|
|
153
|
+
allow(address) {
|
|
154
|
+
const now = this.#now();
|
|
155
|
+
for (const [key, window] of this.#windows) {
|
|
156
|
+
if (now - window.start >= ATTEMPT_WINDOW_MS)
|
|
157
|
+
this.#windows.delete(key);
|
|
158
|
+
}
|
|
159
|
+
const window = this.#windows.get(address) ?? { start: now, count: 0 };
|
|
160
|
+
window.count += 1;
|
|
161
|
+
this.#windows.set(address, window);
|
|
162
|
+
return window.count <= MAX_ATTEMPTS;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { createAdaptorServer } from '@hono/node-server';
|
|
2
|
+
import { DEFAULT_PORT } from './hosts.js';
|
|
3
|
+
import { lanAddresses, localName } from './lan.js';
|
|
4
|
+
export { DEFAULT_PORT } from './hosts.js';
|
|
5
|
+
const HOST = '127.0.0.1';
|
|
6
|
+
// Ports above 1024 that browsers (and fetch) refuse to connect to, per the Fetch spec's bad-port list.
|
|
7
|
+
const UNSAFE_PORTS = new Set([
|
|
8
|
+
1719, 1720, 1723, 2049, 3659, 4045, 4190, 5060, 5061, 6000, 6566, 6665, 6666, 6667, 6668, 6669,
|
|
9
|
+
6679, 6697, 10080,
|
|
10
|
+
]);
|
|
11
|
+
export function isUnsafePort(port) {
|
|
12
|
+
return UNSAFE_PORTS.has(port);
|
|
13
|
+
}
|
|
14
|
+
export class PortInUseError extends Error {
|
|
15
|
+
constructor(port) {
|
|
16
|
+
super(`Port ${port} is already in use; stop the other process or pass --port <n>`);
|
|
17
|
+
this.name = 'PortInUseError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function shut(http) {
|
|
21
|
+
return new Promise((resolve) => {
|
|
22
|
+
http.close(() => resolve());
|
|
23
|
+
http.closeAllConnections();
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
async function stop(http, server) {
|
|
27
|
+
await server.lan.close();
|
|
28
|
+
server.close();
|
|
29
|
+
await shut(http);
|
|
30
|
+
}
|
|
31
|
+
// The real local network: this computer's private addresses (and its .local name when it
|
|
32
|
+
// resolves to one), and a listener on one of them beside the one on 127.0.0.1.
|
|
33
|
+
export const localNetwork = {
|
|
34
|
+
async hosts() {
|
|
35
|
+
const addresses = lanAddresses();
|
|
36
|
+
const name = await localName(addresses);
|
|
37
|
+
return { addresses, names: name ? [name] : [] };
|
|
38
|
+
},
|
|
39
|
+
listen(fetch, address, port) {
|
|
40
|
+
const http = createAdaptorServer({ fetch });
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
http.once('error', reject);
|
|
43
|
+
http.listen(port, address, () => {
|
|
44
|
+
http.off('error', reject);
|
|
45
|
+
resolve(() => shut(http));
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
export function listen(server, port = DEFAULT_PORT) {
|
|
51
|
+
const http = createAdaptorServer({ fetch: server.app.fetch });
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
http.once('error', (error) => {
|
|
54
|
+
server.close();
|
|
55
|
+
reject(error.code === 'EADDRINUSE' ? new PortInUseError(port) : error);
|
|
56
|
+
});
|
|
57
|
+
const bind = () => http.listen(port, HOST, onListening);
|
|
58
|
+
const onListening = () => {
|
|
59
|
+
const { port: bound } = http.address();
|
|
60
|
+
// A random port the browser would refuse: ask the system for another one.
|
|
61
|
+
if (port === 0 && isUnsafePort(bound)) {
|
|
62
|
+
http.close(bind);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
server.listeningOn(bound);
|
|
66
|
+
resolve({ url: `http://${HOST}:${bound}`, port: bound, close: () => stop(http, server) });
|
|
67
|
+
};
|
|
68
|
+
bind();
|
|
69
|
+
});
|
|
70
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { listItems } from '../app/items.js';
|
|
2
|
+
import { boardRoundOf, liveBoards } from '../app/next.js';
|
|
3
|
+
import { notesSummary } from '../app/notes.js';
|
|
4
|
+
import { DESKTOP, desktopChosen, desktopTarget, dropEndpoint, listTargets, } from '../app/notify-targets.js';
|
|
5
|
+
import { setAsideOf } from '../app/set-aside.js';
|
|
6
|
+
import { localMinuteOfDay, Planner } from '../core/notify/planner.js';
|
|
7
|
+
import { freshSignals, signalsOf } from '../core/notify/signals.js';
|
|
8
|
+
import { noticeText } from '../core/notify/text.js';
|
|
9
|
+
import { selectEvents } from '../store/queries.js';
|
|
10
|
+
import { outcomeLine } from './test-push.js';
|
|
11
|
+
const TICK_MS = 5000;
|
|
12
|
+
function factsOf(project, now) {
|
|
13
|
+
const boards = liveBoards(project);
|
|
14
|
+
return listItems(project, {}, () => new Date(now).toISOString()).map((item) => {
|
|
15
|
+
const round = boardRoundOf(project, item, boards);
|
|
16
|
+
const notes = notesSummary(project, item, boards);
|
|
17
|
+
const setAside = item.turn === 'agent' ? setAsideOf(selectEvents(project.db, item.id)) : undefined;
|
|
18
|
+
return {
|
|
19
|
+
...item,
|
|
20
|
+
...(round === undefined ? {} : { round }),
|
|
21
|
+
...(notes === undefined ? {} : { notes }),
|
|
22
|
+
...(setAside === undefined ? {} : { setAside }),
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export function createNotifier(project, feed, options) {
|
|
27
|
+
const targetsDb = options.targets;
|
|
28
|
+
// A notice names its project and opens it: one phone and one desktop app hear every project.
|
|
29
|
+
// A burst says it in its own words ("3 cosas te tocan en tienda-web").
|
|
30
|
+
const { id, name } = project.identity;
|
|
31
|
+
const words = (notice, lang) => {
|
|
32
|
+
const text = noticeText(notice, lang, { id, name });
|
|
33
|
+
return {
|
|
34
|
+
...text,
|
|
35
|
+
title: notice.kind === 'group' ? text.title : `${name} · ${text.title}`,
|
|
36
|
+
url: `/p/${id}${text.url}`,
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
const now = options.now ?? Date.now;
|
|
40
|
+
const minuteOfDay = options.minuteOfDay ?? localMinuteOfDay;
|
|
41
|
+
const planners = new Map();
|
|
42
|
+
const desktop = new Map();
|
|
43
|
+
const sending = new Set();
|
|
44
|
+
let seen;
|
|
45
|
+
let stopFeed;
|
|
46
|
+
let timer;
|
|
47
|
+
const push = (target, text) => {
|
|
48
|
+
const subscription = target.push;
|
|
49
|
+
if (!subscription)
|
|
50
|
+
return;
|
|
51
|
+
const sent = options.sender
|
|
52
|
+
.send(subscription, JSON.stringify(text))
|
|
53
|
+
.then((outcome) => {
|
|
54
|
+
options.log?.(`Otea ${outcomeLine(`push to device ${target.device ?? target.id}`, outcome)}`);
|
|
55
|
+
if (outcome.result !== 'gone')
|
|
56
|
+
return;
|
|
57
|
+
dropEndpoint(targetsDb, subscription.endpoint);
|
|
58
|
+
refresh();
|
|
59
|
+
})
|
|
60
|
+
.catch((error) => {
|
|
61
|
+
options.log?.(`Otea push to device ${target.device ?? target.id}: ${String(error)}`);
|
|
62
|
+
})
|
|
63
|
+
.finally(() => sending.delete(sent));
|
|
64
|
+
sending.add(sent);
|
|
65
|
+
};
|
|
66
|
+
const listeners = () => {
|
|
67
|
+
const targets = listTargets(targetsDb);
|
|
68
|
+
if (desktop.size > 0)
|
|
69
|
+
targets.push(desktopTarget(targetsDb));
|
|
70
|
+
return targets;
|
|
71
|
+
};
|
|
72
|
+
const scan = () => {
|
|
73
|
+
const at = now();
|
|
74
|
+
let signals;
|
|
75
|
+
try {
|
|
76
|
+
signals = signalsOf(factsOf(project, at), at);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const fresh = freshSignals(seen, signals);
|
|
82
|
+
seen = new Set(signals.map(({ key }) => key));
|
|
83
|
+
const targets = listeners();
|
|
84
|
+
for (const id of planners.keys()) {
|
|
85
|
+
if (!targets.some((target) => target.id === id))
|
|
86
|
+
planners.delete(id);
|
|
87
|
+
}
|
|
88
|
+
for (const target of targets) {
|
|
89
|
+
let planner = planners.get(target.id);
|
|
90
|
+
if (!planner) {
|
|
91
|
+
planner = new Planner(minuteOfDay, id);
|
|
92
|
+
planners.set(target.id, planner);
|
|
93
|
+
}
|
|
94
|
+
planner.offer(fresh, target.prefs, at);
|
|
95
|
+
for (const notice of planner.take(at, seen, target.prefs)) {
|
|
96
|
+
if (target.id !== DESKTOP) {
|
|
97
|
+
push(target, words(notice, target.lang));
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
// The language chosen in Preferencias, once it was; else the system's.
|
|
101
|
+
const chose = desktopChosen(targetsDb);
|
|
102
|
+
for (const [listener, { lang }] of desktop) {
|
|
103
|
+
listener(words(notice, chose ? target.lang : lang));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
const stop = () => {
|
|
109
|
+
stopFeed?.();
|
|
110
|
+
stopFeed = undefined;
|
|
111
|
+
clearInterval(timer);
|
|
112
|
+
timer = undefined;
|
|
113
|
+
seen = undefined;
|
|
114
|
+
planners.clear();
|
|
115
|
+
};
|
|
116
|
+
const refresh = () => {
|
|
117
|
+
let wanted = desktop.size > 0;
|
|
118
|
+
try {
|
|
119
|
+
wanted ||= listTargets(targetsDb).length > 0;
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
wanted = false;
|
|
123
|
+
}
|
|
124
|
+
if (wanted && !stopFeed) {
|
|
125
|
+
stopFeed = feed.subscribe(scan);
|
|
126
|
+
timer = setInterval(scan, options.tickMs ?? TICK_MS);
|
|
127
|
+
timer.unref?.();
|
|
128
|
+
scan();
|
|
129
|
+
}
|
|
130
|
+
else if (!wanted && stopFeed)
|
|
131
|
+
stop();
|
|
132
|
+
};
|
|
133
|
+
return {
|
|
134
|
+
scan,
|
|
135
|
+
refresh,
|
|
136
|
+
listenDesktop(listener, lang, end) {
|
|
137
|
+
desktop.set(listener, end ? { lang, end } : { lang });
|
|
138
|
+
refresh();
|
|
139
|
+
return () => {
|
|
140
|
+
desktop.delete(listener);
|
|
141
|
+
refresh();
|
|
142
|
+
};
|
|
143
|
+
},
|
|
144
|
+
async settled() {
|
|
145
|
+
while (sending.size > 0)
|
|
146
|
+
await Promise.all(sending);
|
|
147
|
+
},
|
|
148
|
+
close() {
|
|
149
|
+
const open = [...desktop.values()];
|
|
150
|
+
desktop.clear();
|
|
151
|
+
for (const { end } of open)
|
|
152
|
+
end?.();
|
|
153
|
+
stop();
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
}
|