sideora 0.0.1-alpha.0 → 0.1.0-alpha.2
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.md +9 -0
- package/README.md +57 -6
- package/assets/sounds/freesound_community-mail-40275.mp3 +0 -0
- package/assets/sounds/mixkit-clear-announce-tones-2861.wav +0 -0
- package/assets/sounds/mixkit-happy-bells-notification-937.wav +0 -0
- package/assets/sounds/mixkit-musical-alert-notification-2309.wav +0 -0
- package/assets/sounds/nice-chime.mp3 +0 -0
- package/bin/sideora.js +2430 -12
- package/extension/dist/assets/background.ts-NW8YqAlO.js +1 -0
- package/extension/dist/assets/fillCode-Ci8IkBvY.js +1 -0
- package/extension/dist/assets/index.html-DrrPha1M.js +2 -0
- package/extension/dist/assets/modulepreload-polyfill-DcjICKwh.js +1 -0
- package/extension/dist/assets/shell-82MNMljp.css +1 -0
- package/extension/dist/assets/shell-QYBN1bx1.js +1 -0
- package/extension/dist/icons/icon-128.png +0 -0
- package/extension/dist/icons/icon-16.png +0 -0
- package/extension/dist/icons/icon-32.png +0 -0
- package/extension/dist/icons/icon-48.png +0 -0
- package/extension/dist/manifest.json +40 -0
- package/extension/dist/service-worker-loader.js +1 -0
- package/extension/dist/src/newtab/index.html +14 -0
- package/extension/dist/src/panel/index.html +15 -0
- package/extension/dist-web/assets/abnfDiagram-WEYOIA3C-CoXOc872.js +1 -0
- package/extension/dist-web/assets/apple-touch-icon-DSgRQASq.png +0 -0
- package/extension/dist-web/assets/architecture-WOLXFQ4H-BkJ4AerO.js +1 -0
- package/extension/dist-web/assets/architectureDiagram-ZHVYKT53-aPkSDlxA.js +36 -0
- package/extension/dist-web/assets/blockDiagram-RJAE7QA3-CH1G4NBZ.js +140 -0
- package/extension/dist-web/assets/c4Diagram-XDPPPE54-Baum1zDu.js +38 -0
- package/extension/dist-web/assets/channel-PsQg-mnK.js +1 -0
- package/extension/dist-web/assets/chunk-2E7WHVMD-CKvLrWRa.js +88 -0
- package/extension/dist-web/assets/chunk-2Q5K7J3B-C1jixKkw.js +1 -0
- package/extension/dist-web/assets/chunk-4EMNKR2T-6fjnroCZ.js +1 -0
- package/extension/dist-web/assets/chunk-55XCU76K-Ygs3sNpi.js +1 -0
- package/extension/dist-web/assets/chunk-5VM5RSS4-ZNzvKenW.js +15 -0
- package/extension/dist-web/assets/chunk-7S6XSTFO-iX5xYzfP.js +2 -0
- package/extension/dist-web/assets/chunk-AQDTSKZB-D9zwhFI8.js +72 -0
- package/extension/dist-web/assets/chunk-DSNLSHJZ-siGS-4qu.js +129 -0
- package/extension/dist-web/assets/chunk-DUW6YSOI-Dgx8z5s3.js +1 -0
- package/extension/dist-web/assets/chunk-HAGFOTED-_3dtnWXn.js +62 -0
- package/extension/dist-web/assets/chunk-HNE4Q36P-BPzRHi72.js +1 -0
- package/extension/dist-web/assets/chunk-J5ZVWO5B-oDCd-gWO.js +1 -0
- package/extension/dist-web/assets/chunk-JWPE2WC7-DVXcaiue.js +1 -0
- package/extension/dist-web/assets/chunk-NGNAAXSQ-DI_UYJZ4.js +136 -0
- package/extension/dist-web/assets/chunk-NQOKRVIF-AXbmOBjs.js +213 -0
- package/extension/dist-web/assets/chunk-UHW7PFBP-B3xrZ8sD.js +1 -0
- package/extension/dist-web/assets/chunk-VQ5W62QI-D_k7Pa6b.js +1 -0
- package/extension/dist-web/assets/chunk-WQQL4CDK-BBIrWwoZ.js +10 -0
- package/extension/dist-web/assets/chunk-XXDRQBXY-i7MKP1NY.js +1 -0
- package/extension/dist-web/assets/chunk-Y2CYZVJY-DsF7k-Jl.js +1 -0
- package/extension/dist-web/assets/chunk-YZO67HFG-Z7qp5QAI.js +1 -0
- package/extension/dist-web/assets/chunk-ZAUX2LTX-CSt3c0Rb.js +1 -0
- package/extension/dist-web/assets/chunk-ZCHMQSOR-g8XRABGK.js +2 -0
- package/extension/dist-web/assets/classDiagram-v2-4MYWR3UA-BL706wzc.js +217 -0
- package/extension/dist-web/assets/cose-bilkent-JH36ORCC-C6J2IoHg.js +1 -0
- package/extension/dist-web/assets/cynefin-EF2NZ3EQ-DLhQOiAy.js +1 -0
- package/extension/dist-web/assets/cynefinDiagram-WF24UDRP-Cv9Lx6_l.js +62 -0
- package/extension/dist-web/assets/cytoscape.esm-nTrd5EVq.js +321 -0
- package/extension/dist-web/assets/d3-scale-CElPUUud.js +1 -0
- package/extension/dist-web/assets/d3-selection-COWvaQNI.js +1 -0
- package/extension/dist-web/assets/dagre-NK4GIZVX-ngM-w2EC.js +4 -0
- package/extension/dist-web/assets/diagram-CBK3MWTC-kp8sayNG.js +24 -0
- package/extension/dist-web/assets/diagram-I5XIIV2D-BZ_Dgs6K.js +24 -0
- package/extension/dist-web/assets/diagram-ODIL2UCW-DWhUyArG.js +30 -0
- package/extension/dist-web/assets/diagram-PFBEJUXE-CBAdEK7l.js +3 -0
- package/extension/dist-web/assets/diagram-RJA3I4VV-kOdIr_3K.js +41 -0
- package/extension/dist-web/assets/diagram-TYUNFSNR-BrWGa0x5.js +200 -0
- package/extension/dist-web/assets/dist-DdXDFcej.js +1 -0
- package/extension/dist-web/assets/docx-preview-D1zbEiLy.js +20 -0
- package/extension/dist-web/assets/earth-Ce3xs8Cb.webp +0 -0
- package/extension/dist-web/assets/ebnfDiagram-V4P3TTPT-0r3weWmj.js +1 -0
- package/extension/dist-web/assets/elk-QONTBCTQ-D3oElRZ3.js +27 -0
- package/extension/dist-web/assets/erDiagram-KYQIGQAA-B9oU7_j_.js +99 -0
- package/extension/dist-web/assets/eventmodeling-K75KTNOO-CMKgc1nP.js +1 -0
- package/extension/dist-web/assets/favicon-D_X6tUVf.svg +5 -0
- package/extension/dist-web/assets/flowDiagram-IUGLLWBU-yRwks0Mw.js +1 -0
- package/extension/dist-web/assets/ganttDiagram-R7GPNTJY-cJqD1qwQ.js +292 -0
- package/extension/dist-web/assets/gitGraph-VSP46ZUC-CuBQqkbr.js +1 -0
- package/extension/dist-web/assets/gitGraphDiagram-PDYL7XHY-D7bHnQXC.js +106 -0
- package/extension/dist-web/assets/graphlib-DS17s2tU.js +1 -0
- package/extension/dist-web/assets/home-CV7-8Trs.js +1 -0
- package/extension/dist-web/assets/index-CQvw7Wy_.js +7 -0
- package/extension/dist-web/assets/index-CVKnZ21i.css +1 -0
- package/extension/dist-web/assets/info-OHQRW6UA-B8kyzV4O.js +1 -0
- package/extension/dist-web/assets/infoDiagram-MVDRKAX5-CEm6lezX.js +2 -0
- package/extension/dist-web/assets/ishikawaDiagram-HPEHV2GE-Ce3uv2wr.js +70 -0
- package/extension/dist-web/assets/journeyDiagram-BOSHW7O7-QgdahHCP.js +139 -0
- package/extension/dist-web/assets/jszip.min-3XXygzvy.js +2 -0
- package/extension/dist-web/assets/kanban-definition-FNSA4HC5-DPuTtWgR.js +89 -0
- package/extension/dist-web/assets/katex-MEuSR36T.js +257 -0
- package/extension/dist-web/assets/mermaid-parser.core-h7U7YGn_.js +7 -0
- package/extension/dist-web/assets/mermaid.core-DkGhophj.js +44 -0
- package/extension/dist-web/assets/mindmap-definition-6OY57UG3-D7VQxbda.js +98 -0
- package/extension/dist-web/assets/mount-CBzzf6KQ.js +195 -0
- package/extension/dist-web/assets/mount-DtvYnQXt.css +1 -0
- package/extension/dist-web/assets/officeparser.browser.slim-kEjhTrQ1.js +1989 -0
- package/extension/dist-web/assets/packet-JDAUHWVQ-oafHgw6X.js +1 -0
- package/extension/dist-web/assets/pegDiagram-ECIUJNBX-C3zo1naj.js +1 -0
- package/extension/dist-web/assets/peoria-B5TBlKDG.jpg +0 -0
- package/extension/dist-web/assets/peoria-thumb-CN8upAdZ.jpg +0 -0
- package/extension/dist-web/assets/pie-XZMESJXO-DXzeBQqF.js +1 -0
- package/extension/dist-web/assets/pieDiagram-GNMPY74T-S5NzPthl.js +39 -0
- package/extension/dist-web/assets/quadrantDiagram-D57K4MC3-C9glHQmk.js +7 -0
- package/extension/dist-web/assets/radar-ABXABTNO-CWTNqtbu.js +1 -0
- package/extension/dist-web/assets/railroad-I3PHUGI6-BTpD5_Bu.js +1 -0
- package/extension/dist-web/assets/railroad-abnf-LNEFI6M7-bWyxoXQG.js +1 -0
- package/extension/dist-web/assets/railroad-ebnf-DWYD2UWJ-ri5733DQ.js +1 -0
- package/extension/dist-web/assets/railroad-peg-JR7BVNR7-CmGfTubP.js +1 -0
- package/extension/dist-web/assets/railroadDiagram-Y72NRBL7-DTfe6Dh1.js +1 -0
- package/extension/dist-web/assets/requirementDiagram-EQYSW5RA-DQDzcS3L.js +84 -0
- package/extension/dist-web/assets/rolldown-runtime-COiEfhsA.js +1 -0
- package/extension/dist-web/assets/rough.esm-Dy-Kn_BL.js +1 -0
- package/extension/dist-web/assets/sankeyDiagram-SY24JHO5-DyMKWxdB.js +40 -0
- package/extension/dist-web/assets/sequenceDiagram-BLEHNHQU-dIkB1mCn.js +169 -0
- package/extension/dist-web/assets/sizeCapture-INFHLROL-3uHXrFqA.js +1 -0
- package/extension/dist-web/assets/stateDiagram-v2-A6IESKWZ-Bj_vxEVF.js +291 -0
- package/extension/dist-web/assets/swimlanes-B3ENRKFQ-BSkWEwHR.js +1 -0
- package/extension/dist-web/assets/swimlanesDiagram-AYQRV4AK-BTzSDlSM.js +8 -0
- package/extension/dist-web/assets/timeline-definition-JXYKGLGB-TwfWS0pw.js +120 -0
- package/extension/dist-web/assets/treeView-D4JQ5SDB-BzA_8YYb.js +1 -0
- package/extension/dist-web/assets/treemap-SAJKECNS-BI9d6--2.js +1 -0
- package/extension/dist-web/assets/usecaseDiagram-WPOLBNNX-BOVbAUPk.js +328 -0
- package/extension/dist-web/assets/vennDiagram-Q3Y3IDCW-X2kYAWaj.js +34 -0
- package/extension/dist-web/assets/wardley-7MLQ67FV-CItxqJ8G.js +1 -0
- package/extension/dist-web/assets/wardleyDiagram-IVAIDKWA-BnUXvKD_.js +78 -0
- package/extension/dist-web/assets/xychartDiagram-4URNHDU4-ysvBXvyE.js +7 -0
- package/extension/dist-web/icon-192.png +0 -0
- package/extension/dist-web/icon-512.png +0 -0
- package/extension/dist-web/index.html +25 -0
- package/extension/dist-web/manifest.webmanifest +14 -0
- package/package.json +64 -12
- package/plugin/.claude-plugin/icon.png +0 -0
- package/plugin/.claude-plugin/plugin.json +6 -0
- package/plugin/.mcp.json +8 -0
- package/plugin/README.md +72 -0
- package/plugin/mcp-launcher.mjs +48 -0
- package/src/account-probe.js +45 -0
- package/src/accounts.js +687 -0
- package/src/ai.js +269 -0
- package/src/assistant-runs.js +32 -0
- package/src/attached-mail.js +76 -0
- package/src/auth-gate.js +70 -0
- package/src/backup-cli.js +158 -0
- package/src/backup.js +329 -0
- package/src/bimi.js +233 -0
- package/src/bridge.js +101 -0
- package/src/calendar-alerts.js +56 -0
- package/src/chats.js +27 -0
- package/src/compose-editor.js +58 -0
- package/src/config.js +420 -0
- package/src/connections.d.ts +24 -0
- package/src/connections.js +37 -0
- package/src/core/agent.js +92 -0
- package/src/core/assistant.d.ts +13 -0
- package/src/core/assistant.js +790 -0
- package/src/core/chats.js +323 -0
- package/src/core/markdown.d.ts +8 -0
- package/src/core/markdown.js +239 -0
- package/src/core/notes.js +445 -0
- package/src/core/otp.js +152 -0
- package/src/core/rules.js +196 -0
- package/src/core/runs.d.ts +9 -0
- package/src/core/runs.js +211 -0
- package/src/core/thread.js +526 -0
- package/src/core/write.js +247 -0
- package/src/core-db.js +131 -0
- package/src/core.js +174 -0
- package/src/credentials.js +320 -0
- package/src/daemon.js +564 -0
- package/src/events.js +213 -0
- package/src/extension-id.d.ts +2 -0
- package/src/extension-id.js +5 -0
- package/src/file-lock.js +27 -0
- package/src/files-index.js +154 -0
- package/src/google-contacts.js +315 -0
- package/src/google-signin.js +306 -0
- package/src/host/assistant-html.js +44 -0
- package/src/host/chats-files.js +137 -0
- package/src/host/file-prep.js +122 -0
- package/src/host/notes-files.js +158 -0
- package/src/host/office-text.js +224 -0
- package/src/host/office-worker.js +8 -0
- package/src/host/rules-file.js +20 -0
- package/src/host/secrets-file.js +91 -0
- package/src/host/systemd.js +90 -0
- package/src/hub.js +245 -0
- package/src/identity.js +316 -0
- package/src/labels.js +5 -0
- package/src/mail-port.d.ts +79 -0
- package/src/mail-port.js +148 -0
- package/src/mcp-server.js +747 -0
- package/src/microsoft-app.js +20 -0
- package/src/microsoft-signin.js +194 -0
- package/src/migrate.js +327 -0
- package/src/notes-html.js +450 -0
- package/src/notes.js +19 -0
- package/src/notify.js +105 -0
- package/src/onepassword.js +223 -0
- package/src/op-registry.d.ts +14 -0
- package/src/op-registry.js +150 -0
- package/src/ops.js +1251 -0
- package/src/otp.js +6 -0
- package/src/panel-ops.js +273 -0
- package/src/pictures.js +735 -0
- package/src/postinstall.js +25 -0
- package/src/relay-passkey.js +122 -0
- package/src/relay-request.js +155 -0
- package/src/relay-seal.js +56 -0
- package/src/relay-settings.js +139 -0
- package/src/relay.js +876 -0
- package/src/render.js +82 -0
- package/src/rules-store.js +8 -0
- package/src/safe-fetch.js +215 -0
- package/src/sender-auth.js +270 -0
- package/src/site-match.js +77 -0
- package/src/tui-agenda.js +266 -0
- package/src/tui.js +670 -0
- package/src/vmc.js +376 -0
- package/src/wake.js +96 -0
- package/src/web-access.js +185 -0
- package/src/web-server.js +241 -0
- package/src/web-service.js +152 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// The rules' file on this Mac (src/core/rules.js asks it): ~/.sideora/rules.yaml, or wherever a link there points.
|
|
2
|
+
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import { RULES_PATH, ensureDirs } from '../config.js';
|
|
5
|
+
|
|
6
|
+
export function rulesFile(at = RULES_PATH) {
|
|
7
|
+
return {
|
|
8
|
+
read() {
|
|
9
|
+
ensureDirs();
|
|
10
|
+
try { return fs.readFileSync(at, 'utf8'); } catch (e) { if (e.code === 'ENOENT') return null; throw e; }
|
|
11
|
+
},
|
|
12
|
+
create(text) { if (!fs.existsSync(at)) fs.writeFileSync(at, text); },
|
|
13
|
+
write(text) {
|
|
14
|
+
const file = fs.realpathSync(at); // a link into a dotfiles folder stays a link
|
|
15
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
16
|
+
fs.writeFileSync(tmp, text, { mode: fs.statSync(file).mode & 0o777 });
|
|
17
|
+
fs.renameSync(tmp, file);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// Secrets on Linux, where there is no Keychain: one file of the person's own, ~/.sideora/secrets.json, readable
|
|
2
|
+
// by them alone (mode 600; its folder made theirs alone as it is written), as an SSH key is. What config.js's keychain* functions keep on
|
|
3
|
+
// the Mac (the mail passwords, the cache's key, the key of the AI connections, the relay's secrets) is kept here,
|
|
4
|
+
// under the same service and account names. One change at a time across processes (src/file-lock.js); each write
|
|
5
|
+
// whole, made beside it then moved over it.
|
|
6
|
+
//
|
|
7
|
+
// A server with nobody at it has nothing to unlock a keyring with: this is the trust of the machine's own disk and
|
|
8
|
+
// account. Encrypting the disk (or the home folder) is what keeps it from someone who takes the disk away.
|
|
9
|
+
|
|
10
|
+
import crypto from 'node:crypto';
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { withFileLock } from '../file-lock.js';
|
|
14
|
+
|
|
15
|
+
export const secretsPath = (home) => path.join(home, 'secrets.json');
|
|
16
|
+
const key = (service, account) => `${service}\u0000${account}`;
|
|
17
|
+
|
|
18
|
+
const ownUid = typeof process.getuid === 'function' ? process.getuid() : null;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The secrets as kept: { doc, stat } (stat: the file as read, to tell a change since), or an empty store when there is
|
|
22
|
+
* none. Refused, never taken as "none": a file that is not a plain file of the person's own (a link, another owner),
|
|
23
|
+
* one others can read, one that does not hold secrets as they are kept.
|
|
24
|
+
*/
|
|
25
|
+
function load(file) {
|
|
26
|
+
let st;
|
|
27
|
+
try { st = fs.lstatSync(file); } catch (e) { if (e.code === 'ENOENT') return { doc: { secrets: {} }, stat: null }; throw e; }
|
|
28
|
+
if (!st.isFile()) throw new Error(`${file} is not a plain file (a link?): it is not read`);
|
|
29
|
+
if (ownUid !== null && st.uid !== ownUid) throw new Error(`${file} is not this user's: it is not read`);
|
|
30
|
+
if ((st.mode & 0o077) !== 0) throw new Error(`${file} can be read by others (mode ${(st.mode & 0o777).toString(8)}): run chmod 600 ${file}`);
|
|
31
|
+
let doc;
|
|
32
|
+
try { doc = JSON.parse(fs.readFileSync(file, 'utf8')); } catch { throw new Error(`${file} is not readable as the secrets it should hold: it is left as it is`); }
|
|
33
|
+
const ok = doc && typeof doc === 'object' && !Array.isArray(doc) && doc.secrets && typeof doc.secrets === 'object' && !Array.isArray(doc.secrets) && Object.values(doc.secrets).every((v) => typeof v === 'string');
|
|
34
|
+
if (!ok) throw new Error(`${file} does not hold secrets as they are kept: it is left as it is`);
|
|
35
|
+
return { doc, stat: st };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Written whole: made beside it (new, this user's alone), flushed, moved over it; its folder kept this user's alone. */
|
|
39
|
+
function save(file, doc) {
|
|
40
|
+
const dir = path.dirname(file);
|
|
41
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
42
|
+
try { const d = fs.statSync(dir); if (ownUid === null || d.uid === ownUid) fs.chmodSync(dir, 0o700); } catch { /* left as it is */ }
|
|
43
|
+
const tmp = `${file}.${process.pid}.${crypto.randomBytes(6).toString('hex')}.tmp`;
|
|
44
|
+
const fd = fs.openSync(tmp, 'wx', 0o600);
|
|
45
|
+
try { fs.writeSync(fd, JSON.stringify(doc, null, 2) + '\n'); fs.fsyncSync(fd); } finally { fs.closeSync(fd); }
|
|
46
|
+
fs.renameSync(tmp, file);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* One change, read and written under the lock; and, as the lock of a process held up long enough can be taken by
|
|
51
|
+
* another, written only over the file as it was read (else read again and made again): no update of another lost.
|
|
52
|
+
*/
|
|
53
|
+
function change(file, fn) {
|
|
54
|
+
for (let tries = 0; tries < 5; tries++) {
|
|
55
|
+
const done = withFileLock(file, () => {
|
|
56
|
+
const { doc, stat } = load(file);
|
|
57
|
+
const out = fn(doc);
|
|
58
|
+
if (out === NOTHING) return { out: false };
|
|
59
|
+
let now = null;
|
|
60
|
+
try { now = fs.lstatSync(file); } catch { now = null; }
|
|
61
|
+
const same = !stat ? !now : !!now && now.ino === stat.ino && now.mtimeMs === stat.mtimeMs && now.size === stat.size;
|
|
62
|
+
if (!same) return null; // (changed meanwhile, by a writer that took the lock: done again)
|
|
63
|
+
save(file, doc);
|
|
64
|
+
return { out };
|
|
65
|
+
});
|
|
66
|
+
if (done) return done.out;
|
|
67
|
+
}
|
|
68
|
+
throw new Error('the secrets file kept changing as it was written: try again');
|
|
69
|
+
}
|
|
70
|
+
const NOTHING = Symbol('nothing to write');
|
|
71
|
+
|
|
72
|
+
/** null when there is none; a file that cannot be read throws (it is not "none"). */
|
|
73
|
+
export function read(home, service, account) {
|
|
74
|
+
const v = load(secretsPath(home)).doc.secrets[key(service, account)];
|
|
75
|
+
return typeof v === 'string' ? v : null;
|
|
76
|
+
}
|
|
77
|
+
export function write(home, service, account, secret) {
|
|
78
|
+
change(secretsPath(home), (doc) => { doc.secrets[key(service, account)] = String(secret); return true; });
|
|
79
|
+
}
|
|
80
|
+
/** Gone, or false when there was none. */
|
|
81
|
+
export function remove(home, service, account) {
|
|
82
|
+
return change(secretsPath(home), (doc) => {
|
|
83
|
+
if (!Object.hasOwn(doc.secrets, key(service, account))) return NOTHING;
|
|
84
|
+
delete doc.secrets[key(service, account)];
|
|
85
|
+
return true;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/** Every secret's [service, account] (never its value). */
|
|
89
|
+
export function list(home) {
|
|
90
|
+
return Object.keys(load(secretsPath(home)).doc.secrets).map((k) => { const at = k.indexOf('\u0000'); return [k.slice(0, at), k.slice(at + 1)]; });
|
|
91
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// The background services on Linux: systemd user units (~/.config/systemd/user/sideora-<name>.service), as
|
|
2
|
+
// launchd's LaunchAgents are on the Mac (bin/sideora.js install-daemon, src/web-service.js). They run as the
|
|
3
|
+
// person; started when they log in, or at boot with lingering on (`loginctl enable-linger <user>`, once, as root:
|
|
4
|
+
// what an always-on machine with nobody logged in needs).
|
|
5
|
+
//
|
|
6
|
+
// unitText({ description, args, env, log, restart, restartSec }) → the unit, as text
|
|
7
|
+
// install(name, text) · uninstall(name) → whether it was there · restart(name) · stop(name) · start(name)
|
|
8
|
+
// state(name) → { installed, loaded, pid, lastExit? } (as web-service.js's webServiceState says it)
|
|
9
|
+
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import os from 'node:os';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { execFileSync } from 'node:child_process';
|
|
14
|
+
|
|
15
|
+
// (SIDEORA_SERVICES=launchd: launchd's ways on any machine: the tests, with a stand-in for launchctl)
|
|
16
|
+
export const isLinux = process.platform === 'linux' && process.env.SIDEORA_SERVICES !== 'launchd';
|
|
17
|
+
export const UNIT_DIR = path.join(process.env.XDG_CONFIG_HOME || path.join(os.homedir(), '.config'), 'systemd', 'user');
|
|
18
|
+
export const unitName = (name) => `sideora-${name}.service`;
|
|
19
|
+
export const unitPath = (name) => path.join(UNIT_DIR, unitName(name));
|
|
20
|
+
|
|
21
|
+
const systemctl = (args, opts = {}) => execFileSync('systemctl', ['--user', ...args], { encoding: 'utf8', timeout: 20_000, stdio: ['ignore', 'pipe', 'pipe'], ...opts });
|
|
22
|
+
|
|
23
|
+
/** No line break (it would start a directive of its own) and no NUL, in anything written into a unit: refused. */
|
|
24
|
+
const plain = (v) => { const s = String(v); if (/[\r\n\0]/.test(s)) throw new Error('a service cannot be set up with a line break in its paths or settings'); return s; };
|
|
25
|
+
/** A word of an ExecStart or Environment line: % and $ taken as themselves (doubled), in double quotes when it has to be. */
|
|
26
|
+
const quote = (w) => { const s = plain(w).replace(/%/g, '%%').replace(/\$/g, '$$$$'); return /^[\w@%+=:,./-]+$/.test(s) ? s : `"${s.replace(/[\\"]/g, (c) => `\\${c}`)}"`; };
|
|
27
|
+
/** A path of a directive's own (the log's): % taken as itself. */
|
|
28
|
+
const pathOf = (v) => plain(v).replace(/%/g, '%%');
|
|
29
|
+
/** A line's value (the description): a line break as a space. */
|
|
30
|
+
const line = (v) => String(v).replace(/[\r\n\0]+/g, ' ');
|
|
31
|
+
|
|
32
|
+
export function unitText({ description, args, env = {}, log, restart = 'always', restartSec = 5 }) {
|
|
33
|
+
if (!Array.isArray(args) || !args.length) throw new Error('a service needs its program');
|
|
34
|
+
return [
|
|
35
|
+
'[Unit]',
|
|
36
|
+
`Description=${line(description)}`,
|
|
37
|
+
'After=network-online.target',
|
|
38
|
+
'Wants=network-online.target',
|
|
39
|
+
'',
|
|
40
|
+
'[Service]',
|
|
41
|
+
`ExecStart=${args.map(quote).join(' ')}`,
|
|
42
|
+
`Restart=${restart}`,
|
|
43
|
+
`RestartSec=${restartSec}`,
|
|
44
|
+
...Object.entries(env).map(([k, v]) => `Environment=${quote(`${k}=${v}`)}`),
|
|
45
|
+
...(log ? [`StandardOutput=append:${pathOf(log)}`, `StandardError=append:${pathOf(log)}`] : []),
|
|
46
|
+
'',
|
|
47
|
+
'[Install]',
|
|
48
|
+
'WantedBy=default.target',
|
|
49
|
+
''
|
|
50
|
+
].join('\n');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Set it up and start it (set up already: as asked now, started again). */
|
|
54
|
+
export function install(name, text, { run = systemctl } = {}) {
|
|
55
|
+
fs.mkdirSync(UNIT_DIR, { recursive: true });
|
|
56
|
+
fs.writeFileSync(unitPath(name), text);
|
|
57
|
+
run(['daemon-reload']);
|
|
58
|
+
run(['enable', unitName(name)]);
|
|
59
|
+
run(['restart', unitName(name)]);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function uninstall(name, { run = systemctl } = {}) {
|
|
63
|
+
const was = fs.existsSync(unitPath(name));
|
|
64
|
+
try { run(['disable', '--now', unitName(name)]); } catch { /* not there */ }
|
|
65
|
+
if (was) fs.unlinkSync(unitPath(name));
|
|
66
|
+
try { run(['daemon-reload']); } catch { /* none to reload */ }
|
|
67
|
+
return was;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Stopped and started again. Throws when it is not set up. */
|
|
71
|
+
export function restart(name, { run = systemctl, noBlock = false } = {}) {
|
|
72
|
+
if (!fs.existsSync(unitPath(name))) throw new Error(`the ${name} is not set up as a service`);
|
|
73
|
+
// (`noBlock`: asked from inside the very service, which the restart ends: asked, not waited for)
|
|
74
|
+
run(['restart', ...(noBlock ? ['--no-block'] : []), unitName(name)]);
|
|
75
|
+
}
|
|
76
|
+
export const stop = (name, { run = systemctl } = {}) => run(['stop', unitName(name)]);
|
|
77
|
+
export const start = (name, { run = systemctl } = {}) => run(['start', unitName(name)]);
|
|
78
|
+
|
|
79
|
+
export function state(name, { run = systemctl } = {}) {
|
|
80
|
+
if (!fs.existsSync(unitPath(name))) return { installed: false };
|
|
81
|
+
try {
|
|
82
|
+
const out = run(['show', unitName(name), '-p', 'MainPID', '-p', 'ActiveState', '-p', 'LoadState', '-p', 'ExecMainStatus']);
|
|
83
|
+
const prop = (k) => new RegExp(`^${k}=(.*)$`, 'm').exec(out)?.[1] ?? '';
|
|
84
|
+
const pid = Number(prop('MainPID')) || null;
|
|
85
|
+
const exit = prop('ExecMainStatus');
|
|
86
|
+
return { installed: true, loaded: prop('LoadState') === 'loaded', pid: prop('ActiveState') === 'active' ? pid : null, ...(exit !== '' ? { lastExit: Number(exit) } : {}) };
|
|
87
|
+
} catch {
|
|
88
|
+
return { installed: true, loaded: false, pid: null };
|
|
89
|
+
}
|
|
90
|
+
}
|
package/src/hub.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// The hub: one process (the daemon) that answers the panel's ops for the others on this Mac, so that what they keep
|
|
2
|
+
// (notes, chats, rules, the cache, kept mail connections) is written by one process, not by each. Its socket,
|
|
3
|
+
// ~/.sideora/hub.sock, is this user's alone (made so as it is made): whoever may open it may do on this Mac
|
|
4
|
+
// what the side panel may, which is what the bridge asks on the panel's behalf.
|
|
5
|
+
//
|
|
6
|
+
// Wire: newline-delimited JSON, both ways.
|
|
7
|
+
// from the hub, first { hub: 'sideora', protocol: 1, pid }
|
|
8
|
+
// request { id, op, args }
|
|
9
|
+
// answer { id, ok: true, result } | { id, ok: false, error }
|
|
10
|
+
// let go of { cancel: id } the request's signal aborts (a model turn stopped); its answer still comes
|
|
11
|
+
// Requests are answered as they finish, several at once; one whose asker goes away is let go (its signal aborts).
|
|
12
|
+
// Served: what the registry gives the side panel, and `hello` (src/op-registry.js), less what the asker does itself
|
|
13
|
+
// (ASKER_DOES: what restarts the daemon, keeps a flow in its process, or needs the asker's session). The AI ops are the
|
|
14
|
+
// hub's alone (HUB_ONLY): it holds the connections and their keys, and a second holder would undo what it keeps, so
|
|
15
|
+
// with no hub they are refused, not done by the asker. An op asked and not answered within CALL_MS is an error, as is
|
|
16
|
+
// one cut off: neither is ever done a second time by the asker.
|
|
17
|
+
//
|
|
18
|
+
// A client (hubClient) that cannot reach the hub says so, and its caller does the op itself, as before there was one:
|
|
19
|
+
// an older daemon, or none running, changes nothing. Ask it: the bridge (src/bridge.js), and the web app's server and
|
|
20
|
+
// the relay (viaHub, in `sideora web`).
|
|
21
|
+
|
|
22
|
+
import fs from 'node:fs';
|
|
23
|
+
import net from 'node:net';
|
|
24
|
+
import path from 'node:path';
|
|
25
|
+
import { HOME_DIR } from './config.js';
|
|
26
|
+
import { PANEL_OPS } from './op-registry.js';
|
|
27
|
+
|
|
28
|
+
export const HUB_SOCK = path.join(HOME_DIR, 'hub.sock');
|
|
29
|
+
export const HUB_PROTOCOL = 1;
|
|
30
|
+
/** A request line at most this long (an attachment added to a draft comes as base64 in one request). */
|
|
31
|
+
export const MAX_LINE = 96 * 1024 * 1024;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Whole lines out of a stream of text chunks, each handed to `line` as it completes: the chunks are kept apart and
|
|
35
|
+
* joined only once a newline ends them, and only the newest is searched (a 40 MB line read a chunk at a time is
|
|
36
|
+
* not looked through again and again). `tooLong` is called, and nothing more read, when a line passes `max`.
|
|
37
|
+
*/
|
|
38
|
+
export function lineReader(line, { max = MAX_LINE, tooLong = () => {} } = {}) {
|
|
39
|
+
let parts = [];
|
|
40
|
+
let size = 0;
|
|
41
|
+
let stopped = false;
|
|
42
|
+
return (chunk) => {
|
|
43
|
+
if (stopped) return;
|
|
44
|
+
let start = 0;
|
|
45
|
+
let nl;
|
|
46
|
+
while ((nl = chunk.indexOf('\n', start)) >= 0) {
|
|
47
|
+
const piece = chunk.slice(start, nl);
|
|
48
|
+
size += piece.length;
|
|
49
|
+
if (size > max) { stopped = true; tooLong(); return; }
|
|
50
|
+
parts.push(piece);
|
|
51
|
+
const text = parts.join('');
|
|
52
|
+
parts = [];
|
|
53
|
+
size = 0;
|
|
54
|
+
start = nl + 1;
|
|
55
|
+
line(text);
|
|
56
|
+
if (stopped) return;
|
|
57
|
+
}
|
|
58
|
+
const rest = chunk.slice(start);
|
|
59
|
+
if (rest) { parts.push(rest); size += rest.length; }
|
|
60
|
+
if (size > max) { stopped = true; tooLong(); }
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The side panel's ops the asker does itself, not the hub: what restarts the daemon (an account added, changed or
|
|
66
|
+
* removed: run in the daemon, it would end the very process answering), what keeps a flow in the process that began it
|
|
67
|
+
* (Google's sign-in), and what needs the asker's own session (the 1Password CLI, as the person's app unlocked it).
|
|
68
|
+
*/
|
|
69
|
+
export const ASKER_DOES = new Set(['addAccount', 'editAccount', 'removeAccount', 'testAccount', 'setDefaultAccount', 'googleSignInStart', 'googleSignInStatus', 'googleSignInCancel', 'microsoftSignInStart', 'microsoftSignInStatus', 'microsoftSignInCancel', 'onePasswordItems', 'relaySettings', 'relayJoin', 'relayPairLink', 'relayPairStatus', 'relayPairCancel', 'relayForget', 'relayAdmin', 'relayMove', 'relayLeave', 'relayRequest', 'relayRequestConfirm', 'relayRequestResend', 'relayRequestCheck', 'relayRequestCancel']); // (the relay's too: its keys are in the Keychain, its connection the asking web app's)
|
|
70
|
+
/** What only the hub does: with no hub answering, the asker refuses it (NO_HUB) rather than doing it itself. */
|
|
71
|
+
export const HUB_ONLY = new Set(['aiMessage', 'aiChat', 'assistantStart', 'assistantWait', 'assistantAnswer', 'assistantStop']);
|
|
72
|
+
export const NO_HUB = 'the AI models and the assistant are reached through Sideora\'s background service, which did not answer: start it (sideora install-daemon), or restart it if it runs an older version (sideora restart-daemon), then try again';
|
|
73
|
+
export const hubServes = (op) => typeof op === 'string' && (op === 'hello' || (PANEL_OPS.has(op) && !ASKER_DOES.has(op)));
|
|
74
|
+
/** How long an op sent to the hub is waited for (the extension gives up on its own at 90 s). */
|
|
75
|
+
export const CALL_MS = 100_000;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The hub's server, on `sock`. `run(op, args, ctx)` answers one op as runOp does ({ ok, result | error }, never
|
|
79
|
+
* throwing). Returns the server; `close()` stops it, ends its connections and removes the socket file.
|
|
80
|
+
*/
|
|
81
|
+
export function startHub({ sock = HUB_SOCK, run, log = () => {}, serves = hubServes }) {
|
|
82
|
+
const conns = new Set();
|
|
83
|
+
const server = net.createServer((conn) => {
|
|
84
|
+
conns.add(conn);
|
|
85
|
+
conn.on('close', () => conns.delete(conn));
|
|
86
|
+
const inflight = new Map(); // this connection's requests under way: their AbortControllers, by id
|
|
87
|
+
const write = (msg) => {
|
|
88
|
+
if (conn.destroyed) return;
|
|
89
|
+
let text;
|
|
90
|
+
try { text = JSON.stringify(msg); } catch { text = JSON.stringify({ id: msg?.id ?? null, ok: false, error: 'the answer could not be written' }); }
|
|
91
|
+
conn.write(text + '\n');
|
|
92
|
+
};
|
|
93
|
+
const request = (text) => {
|
|
94
|
+
let msg;
|
|
95
|
+
try { msg = JSON.parse(text); } catch { return; }
|
|
96
|
+
if (msg && typeof msg === 'object' && 'cancel' in msg && !('op' in msg)) { inflight.get(msg.cancel)?.abort(); return; }
|
|
97
|
+
const id = typeof msg?.id === 'number' || typeof msg?.id === 'string' ? msg.id : null;
|
|
98
|
+
const op = msg?.op;
|
|
99
|
+
const args = msg?.args && typeof msg.args === 'object' && !Array.isArray(msg.args) ? msg.args : {};
|
|
100
|
+
if (!serves(op)) { write({ id, ok: false, error: typeof op === 'string' && op.length <= 64 ? `unknown op "${op}"` : 'unknown op' }); return; }
|
|
101
|
+
// (an id used twice while the first is under way is refused: it could not be let go of)
|
|
102
|
+
if (id !== null && inflight.has(id)) { write({ id, ok: false, error: 'that request id is in use' }); return; }
|
|
103
|
+
const ac = new AbortController();
|
|
104
|
+
const key = id !== null ? id : Symbol('no id'); // (one with no id cannot be let go of by a cancel, but goes with its connection)
|
|
105
|
+
inflight.set(key, ac);
|
|
106
|
+
Promise.resolve()
|
|
107
|
+
.then(() => run(op, args, { signal: ac.signal }))
|
|
108
|
+
.catch((e) => ({ ok: false, error: e?.message || String(e) }))
|
|
109
|
+
.then((r) => { if (inflight.get(key) === ac) inflight.delete(key); write({ id, ...r }); });
|
|
110
|
+
};
|
|
111
|
+
write({ hub: 'sideora', protocol: HUB_PROTOCOL, pid: process.pid });
|
|
112
|
+
conn.setEncoding('utf8');
|
|
113
|
+
const read = lineReader((text) => { if (text.trim()) request(text); }, { tooLong: () => conn.destroy() }); // (a line too long: not a client of ours)
|
|
114
|
+
conn.on('data', (chunk) => {
|
|
115
|
+
try { read(chunk); } catch { conn.destroy(); } // (whatever went wrong with this connection ends it, never the hub)
|
|
116
|
+
});
|
|
117
|
+
const gone = () => { for (const ac of inflight.values()) ac.abort(); inflight.clear(); };
|
|
118
|
+
conn.on('close', gone);
|
|
119
|
+
conn.on('error', gone);
|
|
120
|
+
});
|
|
121
|
+
server.on('error', (e) => log('hub error: ' + e.message));
|
|
122
|
+
let owned = false; // (the socket file is this hub's: made by it, so removed by it)
|
|
123
|
+
const bind = () => {
|
|
124
|
+
try { fs.unlinkSync(sock); } catch { /* none */ }
|
|
125
|
+
// made this user's alone as it is made, not a moment after (the bind is done within listen())
|
|
126
|
+
const mask = process.umask(0o177);
|
|
127
|
+
try {
|
|
128
|
+
server.listen(sock, () => {
|
|
129
|
+
owned = true;
|
|
130
|
+
try { fs.chmodSync(sock, 0o600); } catch { /* made so already */ }
|
|
131
|
+
log('hub listening at ' + sock);
|
|
132
|
+
});
|
|
133
|
+
} finally {
|
|
134
|
+
process.umask(mask);
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
// a hub already answering there (another daemon, started by hand) is left alone: its socket is not taken from it
|
|
138
|
+
if (fs.existsSync(sock)) {
|
|
139
|
+
const probe = net.createConnection(sock);
|
|
140
|
+
const done = (live) => { probe.destroy(); if (live) log('another hub answers at ' + sock + ': this one does not start'); else bind(); };
|
|
141
|
+
const timer = setTimeout(() => done(false), 500);
|
|
142
|
+
probe.once('data', (d) => { clearTimeout(timer); done(String(d).startsWith('{"hub":"sideora"')); });
|
|
143
|
+
probe.once('error', () => { clearTimeout(timer); done(false); });
|
|
144
|
+
} else {
|
|
145
|
+
bind();
|
|
146
|
+
}
|
|
147
|
+
const close = server.close.bind(server);
|
|
148
|
+
// (its clients' connections end with it: each then does what it asks itself, until a hub is back)
|
|
149
|
+
server.close = (cb) => {
|
|
150
|
+
if (owned) { try { fs.unlinkSync(sock); } catch { /* gone */ } }
|
|
151
|
+
for (const c of conns) c.destroy();
|
|
152
|
+
return server.listening ? close(cb) : (cb?.(), server);
|
|
153
|
+
};
|
|
154
|
+
return server;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* A connection to the hub, made on first use and again after it drops. `call(op, args)` answers as runOp does, or
|
|
159
|
+
* returns null when the hub cannot be reached (no daemon, an older one, the socket gone): the caller then does the op
|
|
160
|
+
* itself. A call already sent when the connection drops is answered with an error, never null: the hub may have done
|
|
161
|
+
* it (a mail sent), so it must not be done a second time here. `signal` (whoever asked let go: Stop): the hub is told to
|
|
162
|
+
* let go of it too, and the call answers { ok: false, error: 'cancelled' } at once.
|
|
163
|
+
*/
|
|
164
|
+
export function hubClient({ sock = HUB_SOCK, connectMs = 1000, callMs = CALL_MS } = {}) {
|
|
165
|
+
let conn = null; // { socket, ready: Promise<boolean>, waiting: Map }
|
|
166
|
+
let seq = 0;
|
|
167
|
+
|
|
168
|
+
const open = () => {
|
|
169
|
+
const waiting = new Map();
|
|
170
|
+
const socket = net.createConnection(sock);
|
|
171
|
+
socket.setEncoding('utf8');
|
|
172
|
+
let greeted;
|
|
173
|
+
const ready = new Promise((resolve) => {
|
|
174
|
+
const timer = setTimeout(() => resolve(false), connectMs);
|
|
175
|
+
greeted = (ok) => { clearTimeout(timer); resolve(ok); };
|
|
176
|
+
socket.once('close', () => greeted(false));
|
|
177
|
+
socket.once('error', () => greeted(false));
|
|
178
|
+
});
|
|
179
|
+
socket.on('data', lineReader((text) => {
|
|
180
|
+
let msg;
|
|
181
|
+
try { msg = JSON.parse(text); } catch { return; }
|
|
182
|
+
if (msg?.hub === 'sideora') { greeted(msg.protocol === HUB_PROTOCOL); return; }
|
|
183
|
+
const w = waiting.get(msg?.id);
|
|
184
|
+
if (w) { waiting.delete(msg.id); const { id, ...answer } = msg; w(answer); }
|
|
185
|
+
}, { max: Infinity }));
|
|
186
|
+
const drop = () => {
|
|
187
|
+
// (asked, and not answered: it may have been done, so it is not done again here)
|
|
188
|
+
for (const w of waiting.values()) w({ ok: false, error: 'Sideora\'s background service stopped while answering: check, then try again' });
|
|
189
|
+
waiting.clear();
|
|
190
|
+
if (conn?.socket === socket) conn = null;
|
|
191
|
+
};
|
|
192
|
+
socket.on('close', drop);
|
|
193
|
+
socket.on('error', drop);
|
|
194
|
+
return { socket, ready, waiting };
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
async call(op, args, { signal } = {}) {
|
|
199
|
+
if (signal?.aborted) return { ok: false, error: 'cancelled' };
|
|
200
|
+
// (a connection the hub has already ended is not asked: a new one is made, or none, and the op done here)
|
|
201
|
+
if (conn && (conn.socket.destroyed || !conn.socket.writable || conn.socket.readableEnded)) { conn.socket.destroy(); conn = null; }
|
|
202
|
+
if (!conn) conn = open();
|
|
203
|
+
const c = conn;
|
|
204
|
+
if (!(await c.ready)) { if (conn === c) { c.socket.destroy(); conn = null; } return null; }
|
|
205
|
+
if (signal?.aborted) return { ok: false, error: 'cancelled' };
|
|
206
|
+
const id = ++seq;
|
|
207
|
+
return new Promise((resolve) => {
|
|
208
|
+
const letGo = () => {
|
|
209
|
+
if (!c.waiting.delete(id)) return;
|
|
210
|
+
clearTimeout(timer);
|
|
211
|
+
if (!c.socket.destroyed) c.socket.write(JSON.stringify({ cancel: id }) + '\n');
|
|
212
|
+
resolve({ ok: false, error: 'cancelled' });
|
|
213
|
+
};
|
|
214
|
+
// asked, and not answered in time: an error, never done again here (the hub may yet do it)
|
|
215
|
+
const timer = setTimeout(() => { if (c.waiting.delete(id)) { signal?.removeEventListener('abort', letGo); resolve({ ok: false, error: 'Sideora\'s background service did not answer in time: check, then try again' }); } }, callMs);
|
|
216
|
+
// (an op an older hub does not know, refused before anything was done: done here, as with no hub)
|
|
217
|
+
c.waiting.set(id, (answer) => { clearTimeout(timer); signal?.removeEventListener('abort', letGo); resolve(answer?.ok === false && answer.error === `unknown op "${op}"` ? null : answer); });
|
|
218
|
+
signal?.addEventListener('abort', letGo, { once: true });
|
|
219
|
+
c.socket.write(JSON.stringify({ id, op, args: args ?? {} }) + '\n');
|
|
220
|
+
});
|
|
221
|
+
},
|
|
222
|
+
close() { conn?.socket.destroy(); conn = null; }
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* `ops` (src/panel-ops.js's OPS, or a server's own), each op the hub serves asked of it first: done by the hub when
|
|
228
|
+
* one answers, here when none does (as hubClient says), except what only the hub does (HUB_ONLY: refused with none).
|
|
229
|
+
* What the hub does not serve (what the asker does itself) is done here, as before. For the web app's server and the relay, which run their ops as the
|
|
230
|
+
* bridge does: what they keep is then written by the daemon, not by a second process.
|
|
231
|
+
*/
|
|
232
|
+
export function viaHub(ops, client = hubClient()) {
|
|
233
|
+
const out = {};
|
|
234
|
+
for (const [op, fn] of Object.entries(ops)) {
|
|
235
|
+
out[op] = hubServes(op)
|
|
236
|
+
? async (args, ctx) => {
|
|
237
|
+
const answer = await client.call(op, args, { signal: ctx?.signal });
|
|
238
|
+
if (answer === null) { if (HUB_ONLY.has(op)) throw new Error(NO_HUB); return fn(args, ctx); }
|
|
239
|
+
if (answer.ok) return answer.result;
|
|
240
|
+
throw new Error(answer.error);
|
|
241
|
+
}
|
|
242
|
+
: fn;
|
|
243
|
+
}
|
|
244
|
+
return out;
|
|
245
|
+
}
|