@nervur.org/installer-node 0.0.0-stage → 0.37.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 +105 -0
- package/README.md +159 -2
- package/SECURITY.md +8 -0
- package/bin/nervur.js +5 -0
- package/cargo/emscripten-module.wasm +0 -0
- package/cargo/manifest.json +1 -0
- package/cargo/sqlite3.wasm +0 -0
- package/cargo/table-i-i.wasm +0 -0
- package/cargo/table-i-ii.wasm +0 -0
- package/cargo/table-i-iii.wasm +0 -0
- package/cargo/table-i-iiii.wasm +0 -0
- package/cargo/table-i-iiiii.wasm +0 -0
- package/cargo/table-i-iiij.wasm +0 -0
- package/cargo/table-i-ij.wasm +0 -0
- package/cargo/table-v-ii.wasm +0 -0
- package/cargo/table-v-iii.wasm +0 -0
- package/cargo/worker.js +30808 -0
- package/organs/console/node/guard.d.ts +10 -0
- package/organs/console/node/guard.js +52 -0
- package/organs/console/node/index.d.ts +4 -0
- package/organs/console/node/index.js +5 -0
- package/organs/console/node/line.d.ts +16 -0
- package/organs/console/node/line.js +150 -0
- package/organs/console/node/main.js +51 -0
- package/organs/console/node/program.d.ts +1 -0
- package/organs/console/node/program.js +5 -0
- package/organs/console/node/proof.d.ts +15 -0
- package/organs/console/node/proof.js +84 -0
- package/organs/console/node/socket.d.ts +15 -0
- package/organs/console/node/socket.js +99 -0
- package/organs/disk/index.d.ts +1 -0
- package/organs/disk/index.js +2 -0
- package/organs/disk/node/files.js +162 -0
- package/organs/disk/node/main.js +20 -0
- package/organs/disk/node/program.d.ts +1 -0
- package/organs/disk/node/program.js +5 -0
- package/organs/disk/organ.d.ts +5 -0
- package/organs/disk/organ.js +69 -0
- package/organs/network/node/fill.d.ts +21 -0
- package/organs/network/node/fill.js +195 -0
- package/organs/network/node/flows.js +66 -0
- package/organs/network/node/half.js +260 -0
- package/organs/network/node/lines.d.ts +29 -0
- package/organs/network/node/lines.js +236 -0
- package/organs/network/node/link.d.ts +24 -0
- package/organs/network/node/link.js +215 -0
- package/organs/network/node/main.js +41 -0
- package/organs/network/node/moved.d.ts +2 -0
- package/organs/network/node/moved.js +20 -0
- package/organs/network/node/near.d.ts +63 -0
- package/organs/network/node/near.js +263 -0
- package/organs/network/node/program.d.ts +1 -0
- package/organs/network/node/program.js +5 -0
- package/organs/network/node/schemes.d.ts +1 -0
- package/organs/network/node/schemes.js +2 -0
- package/organs/network/node/sent.d.ts +13 -0
- package/organs/network/node/sent.js +101 -0
- package/organs/network/node/served.d.ts +19 -0
- package/organs/network/node/served.js +237 -0
- package/organs/screen/node/client.js +3 -0
- package/organs/screen/node/follow.js +266 -0
- package/organs/screen/node/frames.js +26 -0
- package/organs/screen/node/held.js +27 -0
- package/organs/screen/node/http.js +179 -0
- package/organs/screen/node/lay.js +116 -0
- package/organs/screen/node/leave.js +27 -0
- package/organs/screen/node/linked.js +69 -0
- package/organs/screen/node/listen.js +61 -0
- package/organs/screen/node/look.js +121 -0
- package/organs/screen/node/loop.js +154 -0
- package/organs/screen/node/main.js +74 -0
- package/organs/screen/node/mirror.js +112 -0
- package/organs/screen/node/notice.js +44 -0
- package/organs/screen/node/presser.js +11 -0
- package/organs/screen/node/presses.js +108 -0
- package/organs/screen/node/program.d.ts +1 -0
- package/organs/screen/node/program.js +5 -0
- package/organs/screen/node/shown.js +205 -0
- package/organs/screen/node/standing.js +29 -0
- package/organs/screen/node/state.js +80 -0
- package/organs/screen/node/tell.js +47 -0
- package/organs/screen/node/uid.js +65 -0
- package/organs/screen/node/worn.js +23 -0
- package/organs/shell/index.d.ts +1 -0
- package/organs/shell/index.js +2 -0
- package/organs/shell/node/main.js +26 -0
- package/organs/shell/node/output.js +70 -0
- package/organs/shell/node/pressure.js +14 -0
- package/organs/shell/node/program.d.ts +1 -0
- package/organs/shell/node/program.js +5 -0
- package/organs/shell/node/runs.js +220 -0
- package/organs/shell/node/told.js +70 -0
- package/organs/shell/organ.d.ts +5 -0
- package/organs/shell/organ.js +59 -0
- package/package.json +43 -4
- package/src/command/act.d.ts +31 -0
- package/src/command/act.js +261 -0
- package/src/command/answers.d.ts +3 -0
- package/src/command/answers.js +232 -0
- package/src/command/args.d.ts +14 -0
- package/src/command/args.js +220 -0
- package/src/command/arrive.d.ts +2 -0
- package/src/command/arrive.js +21 -0
- package/src/command/asked.d.ts +18 -0
- package/src/command/asked.js +49 -0
- package/src/command/backup.d.ts +18 -0
- package/src/command/backup.js +261 -0
- package/src/command/before.d.ts +40 -0
- package/src/command/before.js +249 -0
- package/src/command/born.d.ts +27 -0
- package/src/command/born.js +103 -0
- package/src/command/bridge.d.ts +19 -0
- package/src/command/bridge.js +152 -0
- package/src/command/chain.d.ts +9 -0
- package/src/command/chain.js +48 -0
- package/src/command/channel.d.ts +29 -0
- package/src/command/channel.js +263 -0
- package/src/command/choices.d.ts +35 -0
- package/src/command/choices.js +173 -0
- package/src/command/claim.d.ts +12 -0
- package/src/command/claim.js +46 -0
- package/src/command/commands.d.ts +16 -0
- package/src/command/commands.js +257 -0
- package/src/command/ends.d.ts +16 -0
- package/src/command/ends.js +78 -0
- package/src/command/follow.d.ts +12 -0
- package/src/command/follow.js +66 -0
- package/src/command/given.d.ts +39 -0
- package/src/command/given.js +53 -0
- package/src/command/held.d.ts +15 -0
- package/src/command/held.js +56 -0
- package/src/command/index.d.ts +1 -0
- package/src/command/index.js +203 -0
- package/src/command/jailer.d.ts +28 -0
- package/src/command/jailer.js +135 -0
- package/src/command/laid.d.ts +6 -0
- package/src/command/laid.js +31 -0
- package/src/command/left.d.ts +9 -0
- package/src/command/left.js +34 -0
- package/src/command/line.d.ts +7 -0
- package/src/command/line.js +39 -0
- package/src/command/link.d.ts +30 -0
- package/src/command/link.js +123 -0
- package/src/command/lobes.d.ts +1 -0
- package/src/command/lobes.js +10 -0
- package/src/command/locks.d.ts +17 -0
- package/src/command/locks.js +76 -0
- package/src/command/mcp/calls.d.ts +27 -0
- package/src/command/mcp/calls.js +155 -0
- package/src/command/mcp/notices.d.ts +22 -0
- package/src/command/mcp/notices.js +203 -0
- package/src/command/mcp/resources.d.ts +31 -0
- package/src/command/mcp/resources.js +108 -0
- package/src/command/mcp/server.d.ts +19 -0
- package/src/command/mcp/server.js +149 -0
- package/src/command/mcp/tools.d.ts +39 -0
- package/src/command/mcp/tools.js +131 -0
- package/src/command/mcp.d.ts +8 -0
- package/src/command/mcp.js +39 -0
- package/src/command/noticed.d.ts +11 -0
- package/src/command/noticed.js +55 -0
- package/src/command/older.d.ts +11 -0
- package/src/command/older.js +45 -0
- package/src/command/open.d.ts +24 -0
- package/src/command/open.js +107 -0
- package/src/command/passphrase.d.ts +6 -0
- package/src/command/passphrase.js +82 -0
- package/src/command/reclaim.d.ts +7 -0
- package/src/command/reclaim.js +47 -0
- package/src/command/refused.d.ts +4 -0
- package/src/command/refused.js +27 -0
- package/src/command/release.d.ts +27 -0
- package/src/command/release.js +153 -0
- package/src/command/repo.d.ts +31 -0
- package/src/command/repo.js +133 -0
- package/src/command/rewrap.d.ts +16 -0
- package/src/command/rewrap.js +175 -0
- package/src/command/runtime.d.ts +25 -0
- package/src/command/runtime.js +175 -0
- package/src/command/salvage.d.ts +4 -0
- package/src/command/salvage.js +42 -0
- package/src/command/screen.d.ts +14 -0
- package/src/command/screen.js +62 -0
- package/src/command/spawn.d.ts +23 -0
- package/src/command/spawn.js +213 -0
- package/src/command/start.d.ts +34 -0
- package/src/command/start.js +246 -0
- package/src/command/state.d.ts +5 -0
- package/src/command/state.js +39 -0
- package/src/command/token.d.ts +1 -0
- package/src/command/token.js +9 -0
- package/src/command/trial.d.ts +18 -0
- package/src/command/trial.js +108 -0
- package/src/command/undone.d.ts +3 -0
- package/src/command/undone.js +21 -0
- package/src/command/vouch.d.ts +1 -0
- package/src/command/vouch.js +15 -0
- package/src/command/windows-processes.d.ts +2 -0
- package/src/command/windows-processes.js +22 -0
- package/src/command/wipe.d.ts +9 -0
- package/src/command/wipe.js +105 -0
- package/src/command/words.d.ts +5 -0
- package/src/command/words.js +53 -0
- package/src/door/claim.d.ts +5 -0
- package/src/door/claim.js +21 -0
- package/src/door/claimed.d.ts +7 -0
- package/src/door/claimed.js +110 -0
- package/src/door/configuration.d.ts +40 -0
- package/src/door/configuration.js +206 -0
- package/src/door/design.d.ts +13 -0
- package/src/door/design.js +71 -0
- package/src/door/digests.d.ts +7 -0
- package/src/door/digests.js +36 -0
- package/src/door/distribution.d.ts +25 -0
- package/src/door/distribution.js +88 -0
- package/src/door/door.d.ts +41 -0
- package/src/door/door.js +219 -0
- package/src/door/dressed.d.ts +34 -0
- package/src/door/dressed.js +182 -0
- package/src/door/faculties.d.ts +29 -0
- package/src/door/faculties.js +88 -0
- package/src/door/longest.d.ts +6 -0
- package/src/door/longest.js +25 -0
- package/src/fills/account-only.d.ts +3 -0
- package/src/fills/account-only.js +24 -0
- package/src/fills/boot-key.d.ts +21 -0
- package/src/fills/boot-key.js +70 -0
- package/src/fills/boot-move.d.ts +25 -0
- package/src/fills/boot-move.js +86 -0
- package/src/fills/bridge.d.ts +13 -0
- package/src/fills/bridge.js +86 -0
- package/src/fills/chance.d.ts +2 -0
- package/src/fills/chance.js +7 -0
- package/src/fills/crypto.d.ts +2 -0
- package/src/fills/crypto.js +94 -0
- package/src/fills/flushed.d.ts +9 -0
- package/src/fills/flushed.js +80 -0
- package/src/fills/hardware.d.ts +18 -0
- package/src/fills/hardware.js +70 -0
- package/src/fills/held.d.ts +31 -0
- package/src/fills/held.js +171 -0
- package/src/fills/keepers.d.ts +23 -0
- package/src/fills/keepers.js +151 -0
- package/src/fills/keyring.d.ts +19 -0
- package/src/fills/keyring.js +131 -0
- package/src/fills/pages.d.ts +3 -0
- package/src/fills/pages.js +260 -0
- package/src/fills/reserve.d.ts +11 -0
- package/src/fills/reserve.js +72 -0
- package/src/fills/time.d.ts +2 -0
- package/src/fills/time.js +29 -0
- package/src/fills/unlocker.d.ts +6 -0
- package/src/fills/unlocker.js +26 -0
- package/src/fills/vitals.d.ts +8 -0
- package/src/fills/vitals.js +100 -0
- package/src/fills/windows-start.d.ts +2 -0
- package/src/fills/windows-start.js +21 -0
- package/src/halves/build.d.ts +10 -0
- package/src/halves/build.js +219 -0
- package/src/halves/end.d.ts +17 -0
- package/src/halves/end.js +157 -0
- package/src/halves/genome.d.ts +3 -0
- package/src/halves/genome.js +21 -0
- package/src/halves/handler.d.ts +8 -0
- package/src/halves/handler.js +40 -0
- package/src/halves/index.d.ts +5 -0
- package/src/halves/index.js +6 -0
- package/src/halves/laid.d.ts +12 -0
- package/src/halves/laid.js +103 -0
- package/src/halves/lent.d.ts +6 -0
- package/src/halves/lent.js +147 -0
- package/src/halves/native.d.ts +12 -0
- package/src/halves/native.js +53 -0
- package/src/halves/statements.d.ts +23 -0
- package/src/halves/statements.js +165 -0
- package/src/halves/tokens.d.ts +22 -0
- package/src/halves/tokens.js +135 -0
- package/src/link/link.d.ts +4 -0
- package/src/link/link.js +4 -0
- package/src/link/placeholder.d.ts +1 -0
- package/src/link/placeholder.js +21 -0
- package/src/link/steps.d.ts +52 -0
- package/src/link/steps.js +118 -0
- package/src/lobes/calls.d.ts +76 -0
- package/src/lobes/calls.js +61 -0
- package/src/lobes/channel.d.ts +76 -0
- package/src/lobes/channel.js +202 -0
- package/src/lobes/index.d.ts +1 -0
- package/src/lobes/index.js +2 -0
- package/src/lobes/lobes.d.ts +15 -0
- package/src/lobes/lobes.js +244 -0
- package/src/lobes/main.js +107 -0
- package/src/lobes/thread.d.ts +22 -0
- package/src/lobes/thread.js +270 -0
- package/src/organism/act.d.ts +8 -0
- package/src/organism/act.js +63 -0
- package/src/organism/beat.d.ts +5 -0
- package/src/organism/beat.js +45 -0
- package/src/organism/compiled.d.ts +2 -0
- package/src/organism/compiled.js +24 -0
- package/src/organism/folder.d.ts +88 -0
- package/src/organism/folder.js +259 -0
- package/src/organism/main.d.ts +1 -0
- package/src/organism/main.js +11 -0
- package/src/organism/organism.d.ts +1 -0
- package/src/organism/organism.js +220 -0
- package/src/organism/said.d.ts +12 -0
- package/src/organism/said.js +30 -0
- package/src/organism/told.d.ts +5 -0
- package/src/organism/told.js +110 -0
- package/src/organism/unlock.d.ts +8 -0
- package/src/organism/unlock.js +95 -0
- package/src/organism/upgrade.d.ts +24 -0
- package/src/organism/upgrade.js +145 -0
- package/src/organism/via.d.ts +7 -0
- package/src/organism/via.js +20 -0
- package/src/service/body.d.ts +8 -0
- package/src/service/body.js +34 -0
- package/src/service/handler.d.ts +20 -0
- package/src/service/handler.js +128 -0
- package/src/service/index.d.ts +4 -0
- package/src/service/index.js +16 -0
- package/src/service/manager.d.ts +18 -0
- package/src/service/manager.js +185 -0
- package/src/service/service.d.ts +24 -0
- package/src/service/service.js +200 -0
- package/src/service/swap.d.ts +12 -0
- package/src/service/swap.js +74 -0
- package/src/service/watchdog.d.ts +6 -0
- package/src/service/watchdog.js +31 -0
- package/src/tcp/crowd.d.ts +18 -0
- package/src/tcp/crowd.js +85 -0
- package/src/tcp/dial.d.ts +23 -0
- package/src/tcp/dial.js +102 -0
- package/src/tcp/frame.d.ts +23 -0
- package/src/tcp/frame.js +159 -0
- package/src/tcp/listen.d.ts +50 -0
- package/src/tcp/listen.js +207 -0
- package/src/tcp/source.d.ts +2 -0
- package/src/tcp/source.js +26 -0
- package/src/version/index.d.ts +1 -0
- package/src/version/index.js +2 -0
- package/src/version/version.d.ts +1 -0
- package/src/version/version.js +2 -0
- package/src/work/first.d.ts +11 -0
- package/src/work/first.js +91 -0
- package/src/work/index.d.ts +2 -0
- package/src/work/index.js +15 -0
- package/src/work/work.d.ts +20 -0
- package/src/work/work.js +136 -0
- package/src/ws/dial.d.ts +7 -0
- package/src/ws/dial.js +252 -0
- package/src/ws/dialing.d.ts +50 -0
- package/src/ws/dialing.js +72 -0
- package/src/ws/frame.d.ts +32 -0
- package/src/ws/frame.js +124 -0
- package/src/ws/handshake.d.ts +4 -0
- package/src/ws/handshake.js +27 -0
- package/src/ws/line.d.ts +45 -0
- package/src/ws/line.js +250 -0
- package/src/ws/listen.d.ts +42 -0
- package/src/ws/listen.js +272 -0
- package/src/ws/rfc6455.d.ts +18 -0
- package/src/ws/rfc6455.js +187 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
type Why = {
|
|
2
|
+
readonly why: string;
|
|
3
|
+
};
|
|
4
|
+
export type Ran = (file: string, args: readonly string[], input: string) => Promise<{
|
|
5
|
+
readonly code: number;
|
|
6
|
+
readonly out: string;
|
|
7
|
+
readonly err: string;
|
|
8
|
+
} | Why>;
|
|
9
|
+
export declare const ranHere: Ran;
|
|
10
|
+
export type Keyring = {
|
|
11
|
+
keep(name: string, key: Uint8Array): Promise<null | Why>;
|
|
12
|
+
kept(name: string): Promise<{
|
|
13
|
+
readonly key: Uint8Array;
|
|
14
|
+
} | null | Why>;
|
|
15
|
+
forget(name: string): Promise<null | Why>;
|
|
16
|
+
};
|
|
17
|
+
export declare const keyringOf: (platform: NodeJS.Platform, ran?: Ran, service?: string) => Keyring | Why;
|
|
18
|
+
export declare const keyringPrice: (platform: NodeJS.Platform) => string;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import { spawn } from 'node:child_process';
|
|
3
|
+
import { hex, unhex } from '@nervur.org/genome/standard';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const KEY = 32;
|
|
8
|
+
const RAN_WITHIN = 30_000;
|
|
9
|
+
const NOT_FOUND = 44;
|
|
10
|
+
const SERVICE = 'nervur boot key';
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
export const ranHere = (file, args, input) =>
|
|
19
|
+
new Promise((done) => {
|
|
20
|
+
const child = spawn(file, [...args], { stdio: ['pipe', 'pipe', 'pipe'] });
|
|
21
|
+
let out = '';
|
|
22
|
+
let err = '';
|
|
23
|
+
const timer = setTimeout(() => {
|
|
24
|
+
child.kill('SIGKILL');
|
|
25
|
+
done({ why: `${file} did not answer within ${RAN_WITHIN / 1000} s` });
|
|
26
|
+
}, RAN_WITHIN);
|
|
27
|
+
child.stdout.setEncoding('utf8').on('data', (chunk ) => (out += chunk));
|
|
28
|
+
child.stderr.setEncoding('utf8').on('data', (chunk ) => (err += chunk));
|
|
29
|
+
child.stdin.on('error', () => undefined);
|
|
30
|
+
child.on('error', (failed) => {
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
done({ why: `${file} cannot run here: ${failed.message}` });
|
|
33
|
+
});
|
|
34
|
+
child.on('close', (code) => {
|
|
35
|
+
clearTimeout(timer);
|
|
36
|
+
done({ code: code ?? -1, out, err });
|
|
37
|
+
});
|
|
38
|
+
child.stdin.end(input);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
const keyOf = (text ) => {
|
|
48
|
+
const read = text.length === KEY * 2 ? unhex(text) : null;
|
|
49
|
+
return read === null || 'why' in read ? null : { key: read.bytes };
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const quoted = (word ) => `"${word.replaceAll('\\', '\\\\').replaceAll('"', '\\"')}"`;
|
|
53
|
+
|
|
54
|
+
const macKeyring = (ran , service ) => {
|
|
55
|
+
const standing = async () => {
|
|
56
|
+
const named = await ran('/usr/bin/security', ['default-keychain', '-d', 'user'], '');
|
|
57
|
+
if ('why' in named) return named;
|
|
58
|
+
const path = named.out.trim().replace(/^"|"$/gu, '');
|
|
59
|
+
const shown =
|
|
60
|
+
named.code === 0 && path !== '' ? await ran('/usr/bin/security', ['show-keychain-info', path], '') : null;
|
|
61
|
+
if (shown !== null && 'why' in shown) return shown;
|
|
62
|
+
return shown?.code === 0
|
|
63
|
+
? null
|
|
64
|
+
: { why: 'this account has no login keychain to keep the boot key in: choose --boot present, remote or file' };
|
|
65
|
+
};
|
|
66
|
+
const kept = async (name) => {
|
|
67
|
+
const none = await standing();
|
|
68
|
+
if (none !== null) return none;
|
|
69
|
+
const done = await ran('/usr/bin/security', ['find-generic-password', '-a', name, '-s', service, '-w'], '');
|
|
70
|
+
if ('why' in done) return done;
|
|
71
|
+
if (done.code === NOT_FOUND) return null;
|
|
72
|
+
if (done.code !== 0) return { why: `the keychain refused: ${done.err.trim()}` };
|
|
73
|
+
return keyOf(done.out.trim()) ?? { why: 'the keychain holds no boot key under this organism' };
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
keep: async (name, key) => {
|
|
77
|
+
const none = await standing();
|
|
78
|
+
if (none !== null) return none;
|
|
79
|
+
const line = `add-generic-password -U -a ${quoted(name)} -s ${quoted(service)} -w ${hex(key)}\n`;
|
|
80
|
+
const done = await ran('/usr/bin/security', ['-i'], line);
|
|
81
|
+
if ('why' in done) return done;
|
|
82
|
+
const back = await kept(name);
|
|
83
|
+
if (back === null || 'why' in back) return { why: `the keychain did not keep the key: ${done.err.trim()}` };
|
|
84
|
+
return hex(back.key) === hex(key) ? null : { why: 'the keychain kept another key' };
|
|
85
|
+
},
|
|
86
|
+
kept,
|
|
87
|
+
forget: async (name) => {
|
|
88
|
+
const done = await ran('/usr/bin/security', ['delete-generic-password', '-a', name, '-s', service], '');
|
|
89
|
+
if ('why' in done) return done;
|
|
90
|
+
return done.code === 0 || done.code === NOT_FOUND ? null : { why: `the keychain refused: ${done.err.trim()}` };
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const linuxKeyring = (ran , service ) => {
|
|
96
|
+
const attributes = (name ) => ['service', service, 'account', name];
|
|
97
|
+
const kept = async (name) => {
|
|
98
|
+
const done = await ran('secret-tool', ['lookup', ...attributes(name)], '');
|
|
99
|
+
if ('why' in done) return done;
|
|
100
|
+
if (done.code !== 0)
|
|
101
|
+
return done.err.trim() === '' ? null : { why: `the Secret Service refused: ${done.err.trim()}` };
|
|
102
|
+
return keyOf(done.out.trim()) ?? { why: 'the Secret Service holds no boot key under this organism' };
|
|
103
|
+
};
|
|
104
|
+
return {
|
|
105
|
+
keep: async (name, key) => {
|
|
106
|
+
const done = await ran('secret-tool', ['store', `--label=${service}`, ...attributes(name)], hex(key));
|
|
107
|
+
if ('why' in done) return done;
|
|
108
|
+
return done.code === 0 ? null : { why: `the Secret Service refused: ${done.err.trim()}` };
|
|
109
|
+
},
|
|
110
|
+
kept,
|
|
111
|
+
forget: async (name) => {
|
|
112
|
+
const done = await ran('secret-tool', ['clear', ...attributes(name)], '');
|
|
113
|
+
if ('why' in done) return done;
|
|
114
|
+
return done.code === 0 ? null : { why: `the Secret Service refused: ${done.err.trim()}` };
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export const keyringOf = (platform , ran = ranHere, service = SERVICE) => {
|
|
120
|
+
if (platform === 'darwin') return macKeyring(ran, service);
|
|
121
|
+
if (platform === 'linux') return linuxKeyring(ran, service);
|
|
122
|
+
return {
|
|
123
|
+
why:
|
|
124
|
+
platform === 'win32'
|
|
125
|
+
? 'Windows has no keyring mode, since the installer drives no Credential Manager: choose --boot present, remote or file, boot.key held to this account alone by its access list'
|
|
126
|
+
: `this host offers no keyring the installer can drive: ${platform} has neither the macOS keychain nor a Secret Service; choose another --boot`,
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export const keyringPrice = (platform ) =>
|
|
131
|
+
`The boot key stands in this account's ${platform === 'darwin' ? 'login keychain' : 'Secret Service keyring'}, opened by your login: any program running as this account while it is open reads it, and it checks no fingerprint. It is lost with that keyring, so keep a backup named to a key of yours.`;
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import {
|
|
4
|
+
closeSync,
|
|
5
|
+
existsSync,
|
|
6
|
+
readdirSync,
|
|
7
|
+
fstatSync,
|
|
8
|
+
fsync,
|
|
9
|
+
ftruncateSync,
|
|
10
|
+
linkSync,
|
|
11
|
+
openSync,
|
|
12
|
+
readFileSync,
|
|
13
|
+
readSync,
|
|
14
|
+
renameSync,
|
|
15
|
+
rmSync,
|
|
16
|
+
statSync,
|
|
17
|
+
writeFileSync,
|
|
18
|
+
} from 'node:fs';
|
|
19
|
+
import { join } from 'node:path';
|
|
20
|
+
import { folderFlushed, foldersLaid } from './flushed.js';
|
|
21
|
+
import { NO_RESERVE, withRoom, writtenWhole, } from './reserve.js';
|
|
22
|
+
import { windowsStart } from './windows-start.js';
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
const messageOf = (thrown ) => (thrown instanceof Error ? thrown.message : String(thrown));
|
|
26
|
+
|
|
27
|
+
const signalled = (pid ) => {
|
|
28
|
+
try {
|
|
29
|
+
process.kill(pid, 0);
|
|
30
|
+
return true;
|
|
31
|
+
} catch (thrown) {
|
|
32
|
+
return thrown instanceof Error && 'code' in thrown && thrown.code === 'EPERM';
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const codeOf = (thrown ) => (thrown instanceof Error && 'code' in thrown ? thrown.code : undefined);
|
|
37
|
+
|
|
38
|
+
const flushFolder = folderFlushed;
|
|
39
|
+
|
|
40
|
+
let boot ;
|
|
41
|
+
const startOf = (pid ) => {
|
|
42
|
+
if (process.platform === 'win32') return windowsStart(pid);
|
|
43
|
+
if (process.platform === 'linux') {
|
|
44
|
+
try {
|
|
45
|
+
boot ??= readFileSync('/proc/sys/kernel/random/boot_id', 'utf8').trim();
|
|
46
|
+
const stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
|
|
47
|
+
const start = stat.slice(stat.lastIndexOf(')') + 2).split(' ')[19];
|
|
48
|
+
return start === undefined ? undefined : `${boot} ${start}`;
|
|
49
|
+
} catch (thrown) {
|
|
50
|
+
return codeOf(thrown) === 'ENOENT' || codeOf(thrown) === 'ESRCH' ? null : undefined;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const ran = spawnSync('/bin/ps', ['-o', 'lstart=', '-p', String(pid)], {
|
|
54
|
+
encoding: 'utf8',
|
|
55
|
+
env: { LC_ALL: 'C', TZ: 'UTC' },
|
|
56
|
+
});
|
|
57
|
+
if (ran.error !== undefined) return undefined;
|
|
58
|
+
const said = ran.stdout.trim().replaceAll(/\s+/gu, ' ');
|
|
59
|
+
return ran.status === 0 && said !== '' ? said : ran.status === 1 && said === '' ? null : undefined;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
let own ;
|
|
63
|
+
let known = false;
|
|
64
|
+
const ownStart = () => {
|
|
65
|
+
if (!known) {
|
|
66
|
+
own = startOf(process.pid);
|
|
67
|
+
known = true;
|
|
68
|
+
}
|
|
69
|
+
return own ?? undefined;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
const lookAt = (at ) => {
|
|
75
|
+
let descriptor ;
|
|
76
|
+
try {
|
|
77
|
+
descriptor = openSync(at, 'r');
|
|
78
|
+
} catch (thrown) {
|
|
79
|
+
if (codeOf(thrown) === 'ENOENT') return null;
|
|
80
|
+
throw thrown;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
const [id, ...start] = readFileSync(descriptor, 'utf8').split(' ');
|
|
84
|
+
const pid = Number(id);
|
|
85
|
+
return {
|
|
86
|
+
inode: fstatSync(descriptor).ino,
|
|
87
|
+
holder:
|
|
88
|
+
Number.isSafeInteger(pid) && pid > 0 ? { pid, start: start.length === 0 ? undefined : start.join(' ') } : null,
|
|
89
|
+
};
|
|
90
|
+
} finally {
|
|
91
|
+
closeSync(descriptor);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const alive = ({ pid, start } ) => {
|
|
96
|
+
if (start === undefined) return pid === process.pid || signalled(pid);
|
|
97
|
+
if (pid === process.pid) return start === ownStart();
|
|
98
|
+
const now = startOf(pid);
|
|
99
|
+
return now === undefined ? signalled(pid) : now === start;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const left = (look ) => look.holder === null || !alive(look.holder);
|
|
103
|
+
|
|
104
|
+
const beside = (at ) => {
|
|
105
|
+
const mine = `${at}.${process.pid}.new`;
|
|
106
|
+
const start = ownStart();
|
|
107
|
+
try {
|
|
108
|
+
writeFileSync(mine, start === undefined ? String(process.pid) : `${process.pid} ${start}`, { mode: 0o600 });
|
|
109
|
+
} catch (thrown) {
|
|
110
|
+
rmSync(mine, { force: true });
|
|
111
|
+
throw thrown;
|
|
112
|
+
}
|
|
113
|
+
return mine;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const linked = (mine , at ) => {
|
|
117
|
+
try {
|
|
118
|
+
linkSync(mine, at);
|
|
119
|
+
return true;
|
|
120
|
+
} catch (thrown) {
|
|
121
|
+
if (codeOf(thrown) === 'EEXIST') return false;
|
|
122
|
+
throw thrown;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const claimed = (claim ) => {
|
|
127
|
+
const mine = beside(claim);
|
|
128
|
+
try {
|
|
129
|
+
for (let tries = 0; tries < 2; tries += 1) {
|
|
130
|
+
if (linked(mine, claim)) return true;
|
|
131
|
+
const standing = lookAt(claim);
|
|
132
|
+
if (standing !== null && !left(standing)) return false;
|
|
133
|
+
rmSync(claim, { force: true });
|
|
134
|
+
}
|
|
135
|
+
return false;
|
|
136
|
+
} finally {
|
|
137
|
+
rmSync(mine, { force: true });
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const takenOver = (lock , mine ) => {
|
|
142
|
+
const seen = lookAt(lock);
|
|
143
|
+
if (seen === null) return linked(mine, lock);
|
|
144
|
+
if (!left(seen)) return false;
|
|
145
|
+
const claim = `${lock}.${seen.inode}`;
|
|
146
|
+
if (!claimed(claim)) return false;
|
|
147
|
+
try {
|
|
148
|
+
const now = lookAt(lock);
|
|
149
|
+
if (now === null || now.inode !== seen.inode || !left(now)) return false;
|
|
150
|
+
renameSync(mine, lock);
|
|
151
|
+
return true;
|
|
152
|
+
} finally {
|
|
153
|
+
rmSync(claim, { force: true });
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const locked = (lock ) => {
|
|
158
|
+
const mine = beside(lock);
|
|
159
|
+
try {
|
|
160
|
+
const inode = statSync(mine).ino;
|
|
161
|
+
return linked(mine, lock) || takenOver(lock, mine) ? inode : null;
|
|
162
|
+
} finally {
|
|
163
|
+
rmSync(mine, { force: true });
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const fileOf = (fd , release , room ) => {
|
|
168
|
+
let flushed = Promise.resolve(null);
|
|
169
|
+
return {
|
|
170
|
+
read: (offset, length) => {
|
|
171
|
+
const bytes = new Uint8Array(length);
|
|
172
|
+
const read = readSync(fd, bytes, 0, length, offset);
|
|
173
|
+
return bytes.slice(0, read);
|
|
174
|
+
},
|
|
175
|
+
write: (offset, bytes) => withRoom(room, () => writtenWhole(fd, offset, bytes)),
|
|
176
|
+
size: () => fstatSync(fd).size,
|
|
177
|
+
truncate: (size) =>
|
|
178
|
+
withRoom(room, () => {
|
|
179
|
+
try {
|
|
180
|
+
ftruncateSync(fd, size);
|
|
181
|
+
return null;
|
|
182
|
+
} catch (thrown) {
|
|
183
|
+
return { why: messageOf(thrown) };
|
|
184
|
+
}
|
|
185
|
+
}),
|
|
186
|
+
flush: () => {
|
|
187
|
+
flushed = flushed.then(
|
|
188
|
+
() =>
|
|
189
|
+
new Promise((done) => {
|
|
190
|
+
fsync(fd, (failed) => {
|
|
191
|
+
if (failed?.code === 'ENOSPC') room.release();
|
|
192
|
+
done(failed === null ? null : { why: failed.message });
|
|
193
|
+
});
|
|
194
|
+
}),
|
|
195
|
+
);
|
|
196
|
+
return flushed;
|
|
197
|
+
},
|
|
198
|
+
close: () => {
|
|
199
|
+
closeSync(fd);
|
|
200
|
+
release();
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const listed = (home , prefix ) =>
|
|
206
|
+
existsSync(home) ? readdirSync(home).filter((name) => name.startsWith(prefix) && !name.endsWith('.lock')) : [];
|
|
207
|
+
|
|
208
|
+
const laidHome = (home ) => {
|
|
209
|
+
try {
|
|
210
|
+
foldersLaid(home, 0o700);
|
|
211
|
+
return null;
|
|
212
|
+
} catch (thrown) {
|
|
213
|
+
return { why: messageOf(thrown) };
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
export const nodePages = (home , room = NO_RESERVE) => {
|
|
218
|
+
const held = new Set ();
|
|
219
|
+
const lockOf = (name ) => join(home, `${name}.lock`);
|
|
220
|
+
const opened = (name) => {
|
|
221
|
+
let lock = null;
|
|
222
|
+
const release = () => {
|
|
223
|
+
held.delete(name);
|
|
224
|
+
if (lookAt(lockOf(name))?.inode === lock) rmSync(lockOf(name), { force: true });
|
|
225
|
+
};
|
|
226
|
+
try {
|
|
227
|
+
lock = held.has(name) ? null : locked(lockOf(name));
|
|
228
|
+
if (lock === null) return { why: `${name} is held by another opener` };
|
|
229
|
+
held.add(name);
|
|
230
|
+
const made = !existsSync(join(home, name));
|
|
231
|
+
const fd = openSync(join(home, name), made ? 'w+' : 'r+', 0o600);
|
|
232
|
+
if (made) flushFolder(home);
|
|
233
|
+
return { file: fileOf(fd, release, room) };
|
|
234
|
+
} catch (thrown) {
|
|
235
|
+
if (lock !== null) release();
|
|
236
|
+
return { why: messageOf(thrown) };
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
return {
|
|
240
|
+
ready: () => {
|
|
241
|
+
const laid = laidHome(home);
|
|
242
|
+
if (laid === null) room.lay();
|
|
243
|
+
return Promise.resolve(laid);
|
|
244
|
+
},
|
|
245
|
+
open: (name) => withRoom(room, () => opened(name)),
|
|
246
|
+
exists: (name) => existsSync(join(home, name)),
|
|
247
|
+
list: (prefix) => listed(home, prefix),
|
|
248
|
+
remove: (name) => {
|
|
249
|
+
if (held.has(name)) return { why: `${name} is held` };
|
|
250
|
+
try {
|
|
251
|
+
if (!existsSync(join(home, name))) return null;
|
|
252
|
+
rmSync(join(home, name));
|
|
253
|
+
flushFolder(home);
|
|
254
|
+
return null;
|
|
255
|
+
} catch (thrown) {
|
|
256
|
+
return { why: messageOf(thrown) };
|
|
257
|
+
}
|
|
258
|
+
},
|
|
259
|
+
};
|
|
260
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const RESERVE = 67108864;
|
|
2
|
+
export type Reserve = {
|
|
3
|
+
readonly lay: () => void;
|
|
4
|
+
readonly release: () => boolean;
|
|
5
|
+
};
|
|
6
|
+
export declare const NO_RESERVE: Reserve;
|
|
7
|
+
export declare const reserveOf: (home: string, size: number) => Reserve;
|
|
8
|
+
export declare const withRoom: <T>(room: Reserve, act: () => T) => T;
|
|
9
|
+
export declare const writtenWhole: (fd: number, offset: number, bytes: Uint8Array) => null | {
|
|
10
|
+
readonly why: string;
|
|
11
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import { randomBytes } from 'node:crypto';
|
|
3
|
+
import { closeSync, existsSync, openSync, rmSync, statSync, statfsSync, writeSync } from 'node:fs';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { folderFlushed } from './flushed.js';
|
|
6
|
+
|
|
7
|
+
export const RESERVE = 67_108_864;
|
|
8
|
+
|
|
9
|
+
const BLOCK = 1_048_576;
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
export const NO_RESERVE = { lay: () => undefined, release: () => false };
|
|
19
|
+
|
|
20
|
+
const sizeAt = (file ) => (existsSync(file) ? statSync(file).size : null);
|
|
21
|
+
|
|
22
|
+
const written = (file , size ) => {
|
|
23
|
+
const block = randomBytes(BLOCK);
|
|
24
|
+
const fd = openSync(file, 'w', 0o600);
|
|
25
|
+
try {
|
|
26
|
+
for (let at = 0; at < size; at += BLOCK) writeSync(fd, block, 0, Math.min(BLOCK, size - at), at);
|
|
27
|
+
} finally {
|
|
28
|
+
closeSync(fd);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const reserveOf = (home , size ) => {
|
|
33
|
+
const file = join(home, 'reserve');
|
|
34
|
+
return {
|
|
35
|
+
lay: () => {
|
|
36
|
+
if (sizeAt(file) === size) return;
|
|
37
|
+
try {
|
|
38
|
+
rmSync(file, { force: true });
|
|
39
|
+
if (size === 0) return;
|
|
40
|
+
const { bavail, bsize } = statfsSync(home);
|
|
41
|
+
if (bavail * bsize >= 2 * size) written(file, size);
|
|
42
|
+
} catch {
|
|
43
|
+
rmSync(file, { force: true });
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
release: () => {
|
|
47
|
+
if (!existsSync(file)) return false;
|
|
48
|
+
rmSync(file, { force: true });
|
|
49
|
+
folderFlushed(home);
|
|
50
|
+
return true;
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const withRoom = (room , act ) => {
|
|
56
|
+
const first = act();
|
|
57
|
+
const short = typeof first === 'object' && first !== null && 'why' in first && String(first.why).startsWith('ENOSPC');
|
|
58
|
+
return short && room.release() ? act() : first;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const writtenWhole = (fd , offset , bytes ) => {
|
|
62
|
+
try {
|
|
63
|
+
for (let at = 0; at < bytes.length;) {
|
|
64
|
+
const wrote = writeSync(fd, bytes, at, bytes.length - at, offset + at);
|
|
65
|
+
if (wrote === 0) return { why: 'ENOSPC: the device took none of a write' };
|
|
66
|
+
at += wrote;
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
} catch (thrown) {
|
|
70
|
+
return { why: thrown instanceof Error ? thrown.message : String(thrown) };
|
|
71
|
+
}
|
|
72
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
const LONGEST = 2_147_483_647;
|
|
5
|
+
|
|
6
|
+
export const nodeTime = () => {
|
|
7
|
+
let timer = null;
|
|
8
|
+
const disarm = () => {
|
|
9
|
+
if (timer !== null) clearTimeout(timer);
|
|
10
|
+
timer = null;
|
|
11
|
+
};
|
|
12
|
+
const arm = (at , ring ) => {
|
|
13
|
+
disarm();
|
|
14
|
+
const step = () => {
|
|
15
|
+
const left = at - performance.now();
|
|
16
|
+
timer = setTimeout(
|
|
17
|
+
() => {
|
|
18
|
+
timer = null;
|
|
19
|
+
if (performance.now() >= at) ring();
|
|
20
|
+
else step();
|
|
21
|
+
},
|
|
22
|
+
Math.min(LONGEST, Math.max(0, Math.ceil(left))),
|
|
23
|
+
);
|
|
24
|
+
timer.unref();
|
|
25
|
+
};
|
|
26
|
+
step();
|
|
27
|
+
};
|
|
28
|
+
return { wall: () => Date.now(), steady: () => performance.now(), arm, disarm };
|
|
29
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type ScryptOptions } from 'node:crypto';
|
|
2
|
+
import type { UnlockerSeam } from '@nervur.org/genome/seams';
|
|
3
|
+
export declare const COST: ScryptOptions;
|
|
4
|
+
export declare const saltOf: () => Uint8Array;
|
|
5
|
+
export declare const bootKeyOf: (passphrase: string, salt: Uint8Array, cost?: ScryptOptions) => Promise<Uint8Array>;
|
|
6
|
+
export declare const presentUnlocker: (give: () => Promise<Uint8Array | null>) => UnlockerSeam;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import { randomBytes, scrypt, } from 'node:crypto';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
const KEY = 32;
|
|
6
|
+
const SALT = 16;
|
|
7
|
+
|
|
8
|
+
export const COST = { N: 2 ** 17, r: 8, p: 1, maxmem: 256 * 1024 * 1024 };
|
|
9
|
+
|
|
10
|
+
export const saltOf = () => new Uint8Array(randomBytes(SALT));
|
|
11
|
+
|
|
12
|
+
export const bootKeyOf = (passphrase , salt , cost = COST) =>
|
|
13
|
+
new Promise((done, failed) => {
|
|
14
|
+
scrypt(passphrase.normalize('NFC'), salt, KEY, cost, (error, key) => {
|
|
15
|
+
if (error === null) done(new Uint8Array(key));
|
|
16
|
+
else failed(error);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export const presentUnlocker = (give ) => ({
|
|
21
|
+
open: async () => {
|
|
22
|
+
const key = await give();
|
|
23
|
+
return key === null ? null : { key };
|
|
24
|
+
},
|
|
25
|
+
admit: () => Promise.resolve(null),
|
|
26
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Vitals, VitalsSeam } from '@nervur.org/genome/seams';
|
|
2
|
+
export type Host = {
|
|
3
|
+
readonly disk: (home: string) => Vitals['disk'];
|
|
4
|
+
readonly report: () => Promise<string | null>;
|
|
5
|
+
};
|
|
6
|
+
export declare const outputOf: (file: string, args: readonly string[], span: number) => Promise<string | null>;
|
|
7
|
+
export declare const memoryOfVmStat: (report: string) => number | null;
|
|
8
|
+
export declare const nodeVitals: (home: string, host?: Host) => Promise<VitalsSeam>;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import { execFile } from 'node:child_process';
|
|
3
|
+
import { statfsSync } from 'node:fs';
|
|
4
|
+
import { availableParallelism, freemem, loadavg, totalmem } from 'node:os';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const EVERY = 10_000;
|
|
8
|
+
const SPAN = 2_000;
|
|
9
|
+
const FRESH = 1_000;
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
export const outputOf = (file , args , span ) =>
|
|
17
|
+
new Promise((done) => {
|
|
18
|
+
execFile(file, args, { encoding: 'utf8', timeout: span, killSignal: 'SIGKILL' }, (error, out) => {
|
|
19
|
+
done(error === null ? out : null);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const pages = (report , name ) => {
|
|
24
|
+
const found = new RegExp(`^${name}:\\s+(\\d+)\\.`, 'm').exec(report);
|
|
25
|
+
return found?.[1] === undefined ? null : Number(found[1]);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const memoryOfVmStat = (report ) => {
|
|
29
|
+
const size = /page size of (\d+) bytes/.exec(report)?.[1];
|
|
30
|
+
const counted = ['Pages active', 'Pages wired down', 'Pages occupied by compressor'].map((name) =>
|
|
31
|
+
pages(report, name),
|
|
32
|
+
);
|
|
33
|
+
if (size === undefined || counted.some((count) => count === null)) return null;
|
|
34
|
+
return counted.reduce ((sum, count) => sum + (count ?? 0), 0) * Number(size);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const diskOf = (home ) => {
|
|
38
|
+
const disk = statfsSync(home);
|
|
39
|
+
const total = disk.blocks * disk.bsize;
|
|
40
|
+
return { total, used: total - disk.bavail * disk.bsize };
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const nodeHost = {
|
|
44
|
+
disk: diskOf,
|
|
45
|
+
report: () => (process.platform === 'darwin' ? outputOf('vm_stat', [], SPAN) : Promise.resolve(null)),
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const reading = (held , disk ) => {
|
|
49
|
+
const total = totalmem();
|
|
50
|
+
return {
|
|
51
|
+
memory: { used: held === null ? total - freemem() : Math.min(total, held), total },
|
|
52
|
+
disk,
|
|
53
|
+
load: Math.min(1, (loadavg()[0] ?? 0) / availableParallelism()),
|
|
54
|
+
battery: null,
|
|
55
|
+
temperature: null,
|
|
56
|
+
sleep: null,
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const nodeVitals = async (home , host = nodeHost) => {
|
|
61
|
+
let held = null;
|
|
62
|
+
let heldAt = 0;
|
|
63
|
+
let running = null;
|
|
64
|
+
const refresh = () => {
|
|
65
|
+
running ??= host
|
|
66
|
+
.report()
|
|
67
|
+
.catch(() => null)
|
|
68
|
+
.then((text) => {
|
|
69
|
+
if (text !== null) held = memoryOfVmStat(text) ?? held;
|
|
70
|
+
heldAt = Date.now();
|
|
71
|
+
running = null;
|
|
72
|
+
});
|
|
73
|
+
return running;
|
|
74
|
+
};
|
|
75
|
+
let disk = null;
|
|
76
|
+
const read = () => {
|
|
77
|
+
if (running === null && Date.now() - heldAt >= FRESH) void refresh();
|
|
78
|
+
try {
|
|
79
|
+
disk = host.disk(home);
|
|
80
|
+
} catch {
|
|
81
|
+
}
|
|
82
|
+
return reading(held, disk);
|
|
83
|
+
};
|
|
84
|
+
await refresh();
|
|
85
|
+
read();
|
|
86
|
+
let timer = null;
|
|
87
|
+
return {
|
|
88
|
+
read,
|
|
89
|
+
watch: (changed) => {
|
|
90
|
+
if (timer !== null) clearInterval(timer);
|
|
91
|
+
const ticking = setInterval(() => {
|
|
92
|
+
void refresh().then(() => {
|
|
93
|
+
if (timer === ticking) changed();
|
|
94
|
+
});
|
|
95
|
+
}, EVERY);
|
|
96
|
+
ticking.unref();
|
|
97
|
+
timer = ticking;
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
};
|