@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,74 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
const or = (read , otherwise ) => {
|
|
14
|
+
try {
|
|
15
|
+
return read();
|
|
16
|
+
} catch {
|
|
17
|
+
return otherwise;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const thisHost = {
|
|
22
|
+
platform: process.platform,
|
|
23
|
+
file: (path) => or(() => readFileSync(path, 'utf8'), null),
|
|
24
|
+
entries: (path) => or(() => readdirSync(path), []),
|
|
25
|
+
device: (path) => or(() => statSync(path), null),
|
|
26
|
+
ran: (program, args) =>
|
|
27
|
+
or(() => execFileSync(program, args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }), null),
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const PRICE =
|
|
31
|
+
"Node cannot lock the organism's pages in memory, so this host may page what it holds, keys and plaintext among it, to its swap";
|
|
32
|
+
|
|
33
|
+
const numbersOf = (dev ) =>
|
|
34
|
+
`${(Math.floor(dev / 256) & 0xfff) | (Math.floor(dev / 2 ** 32) & ~0xfff)}:${(dev & 0xff) | (Math.floor(dev / 4096) & ~0xff)}`;
|
|
35
|
+
|
|
36
|
+
const blockOf = (host , numbers , depth = 0) => {
|
|
37
|
+
const at = `/sys/dev/block/${numbers}`;
|
|
38
|
+
if ((host.file(`${at}/dm/uuid`) ?? '').startsWith('CRYPT-')) return 'sealed';
|
|
39
|
+
if (/^DEVNAME=zram/mu.test(host.file(`${at}/uevent`) ?? '')) return 'memory';
|
|
40
|
+
if (depth === 8) return 'plain';
|
|
41
|
+
const under = host.entries(`${at}/slaves`).map((name) => (host.file(`/sys/class/block/${name}/dev`) ?? '').trim());
|
|
42
|
+
return under.some((one) => one !== '' && blockOf(host, one, depth + 1) === 'sealed') ? 'sealed' : 'plain';
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const swapSaid = (host = thisHost) => {
|
|
46
|
+
if (host.platform === 'darwin') {
|
|
47
|
+
const usage = host.ran('/usr/sbin/sysctl', ['-n', 'vm.swapusage']);
|
|
48
|
+
if (usage === null) return `${PRICE}; this Mac did not say whether it seals it.`;
|
|
49
|
+
return usage.includes('(encrypted)')
|
|
50
|
+
? `${PRICE}, which this Mac seals, so none of it reaches the disk in the clear.`
|
|
51
|
+
: `${PRICE}, which this Mac does not seal: turn on its encrypted swap, or bear that price.`;
|
|
52
|
+
}
|
|
53
|
+
if (host.platform === 'linux') {
|
|
54
|
+
const swaps = host.file('/proc/swaps');
|
|
55
|
+
if (swaps === null) return `${PRICE}; this machine did not say what swap it holds.`;
|
|
56
|
+
const each = swaps
|
|
57
|
+
.split('\n')
|
|
58
|
+
.slice(1)
|
|
59
|
+
.map((line) => line.trim().split(/\s+/u))
|
|
60
|
+
.filter(([path]) => path !== undefined && path !== '')
|
|
61
|
+
.map(([path = '', kind]) => {
|
|
62
|
+
const stat = host.device(path);
|
|
63
|
+
const kept = stat === null ? 'plain' : blockOf(host, numbersOf(kind === 'partition' ? stat.rdev : stat.dev));
|
|
64
|
+
return { path, kept };
|
|
65
|
+
});
|
|
66
|
+
if (each.every(({ kept }) => kept === 'memory'))
|
|
67
|
+
return `${PRICE}, and this machine holds no swap on a disk, so none of it reaches one.`;
|
|
68
|
+
const plain = each.filter(({ kept }) => kept === 'plain').map(({ path }) => path);
|
|
69
|
+
return plain.length === 0
|
|
70
|
+
? `${PRICE}, which this machine seals, so none of it reaches the disk in the clear.`
|
|
71
|
+
: `${PRICE}, and this machine's swap at ${plain.join(', ')} is not sealed: encrypt it or turn it off, or bear that price.`;
|
|
72
|
+
}
|
|
73
|
+
return `${PRICE}: seal this machine's swap or turn it off, or bear that price.`;
|
|
74
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type Watchdog = {
|
|
2
|
+
readonly socket: string;
|
|
3
|
+
readonly every: number;
|
|
4
|
+
};
|
|
5
|
+
export declare const watchdogOf: (env: NodeJS.ProcessEnv, pid: number) => Watchdog | null;
|
|
6
|
+
export declare const toldAlive: (watchdog: Watchdog, path: string, told?: (failed: Error | null) => void) => void;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import { execFile } from 'node:child_process';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const SET = ['NOTIFY_SOCKET', 'WATCHDOG_USEC', 'WATCHDOG_PID'] ;
|
|
7
|
+
|
|
8
|
+
const BEATS = 4;
|
|
9
|
+
const USEC = 1000;
|
|
10
|
+
|
|
11
|
+
export const watchdogOf = (env , pid ) => {
|
|
12
|
+
const [socket, span, owner] = SET.map((name) => env[name]);
|
|
13
|
+
for (const name of SET) Reflect.deleteProperty(env, name);
|
|
14
|
+
const usec = Number(span);
|
|
15
|
+
if (socket === undefined || socket === '' || !Number.isSafeInteger(usec) || usec <= 0) return null;
|
|
16
|
+
if (owner !== undefined && Number(owner) !== pid) return null;
|
|
17
|
+
return { socket, every: Math.max(1, Math.floor(usec / USEC / BEATS)) };
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const toldAlive = (
|
|
21
|
+
watchdog ,
|
|
22
|
+
path ,
|
|
23
|
+
told = () => undefined,
|
|
24
|
+
) => {
|
|
25
|
+
execFile(
|
|
26
|
+
'systemd-notify',
|
|
27
|
+
['WATCHDOG=1'],
|
|
28
|
+
{ env: { NOTIFY_SOCKET: watchdog.socket, PATH: path }, timeout: watchdog.every },
|
|
29
|
+
(failed) => told(failed),
|
|
30
|
+
);
|
|
31
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
type Source<Held> = {
|
|
2
|
+
readonly key: string;
|
|
3
|
+
count: number;
|
|
4
|
+
readonly silent: Set<Held>;
|
|
5
|
+
};
|
|
6
|
+
export type Crowd<Held> = {
|
|
7
|
+
readonly held: Map<Held, Source<Held>>;
|
|
8
|
+
readonly silent: Set<Held>;
|
|
9
|
+
readonly carried: Set<Held>;
|
|
10
|
+
readonly sources: Map<string, Source<Held>>;
|
|
11
|
+
readonly drop: (one: Held) => void;
|
|
12
|
+
};
|
|
13
|
+
export declare const crowdOf: <Held>(drop: (one: Held) => void) => Crowd<Held>;
|
|
14
|
+
export declare const left: <Held>(crowd: Crowd<Held>, one: Held) => void;
|
|
15
|
+
export declare const carried: <Held>(crowd: Crowd<Held>, one: Held) => void;
|
|
16
|
+
export declare const working: <Held>(crowd: Crowd<Held>, one: Held, inFlight: boolean) => void;
|
|
17
|
+
export declare const admitted: <Held>(crowd: Crowd<Held>, one: Held, source: string, most: number, share: number, silent?: boolean) => boolean;
|
|
18
|
+
export {};
|
package/src/tcp/crowd.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
export const crowdOf = (drop ) => ({
|
|
18
|
+
held: new Map(),
|
|
19
|
+
silent: new Set(),
|
|
20
|
+
carried: new Set(),
|
|
21
|
+
sources: new Map(),
|
|
22
|
+
drop,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export const left = (crowd , one ) => {
|
|
26
|
+
const source = crowd.held.get(one);
|
|
27
|
+
if (source === undefined) return;
|
|
28
|
+
crowd.held.delete(one);
|
|
29
|
+
crowd.silent.delete(one);
|
|
30
|
+
crowd.carried.delete(one);
|
|
31
|
+
source.silent.delete(one);
|
|
32
|
+
source.count -= 1;
|
|
33
|
+
if (source.count === 0) crowd.sources.delete(source.key);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const carried = (crowd , one ) => {
|
|
37
|
+
if (!crowd.held.has(one)) return;
|
|
38
|
+
crowd.carried.add(one);
|
|
39
|
+
crowd.silent.delete(one);
|
|
40
|
+
crowd.held.get(one)?.silent.delete(one);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const working = (crowd , one , inFlight ) => {
|
|
44
|
+
const source = crowd.held.get(one);
|
|
45
|
+
if (source === undefined || crowd.carried.has(one)) return;
|
|
46
|
+
crowd.silent.delete(one);
|
|
47
|
+
source.silent.delete(one);
|
|
48
|
+
if (inFlight) return;
|
|
49
|
+
crowd.silent.add(one);
|
|
50
|
+
source.silent.add(one);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const replaced = (crowd , silent ) => {
|
|
54
|
+
const [oldest] = silent;
|
|
55
|
+
if (oldest === undefined) return false;
|
|
56
|
+
left(crowd, oldest);
|
|
57
|
+
crowd.drop(oldest);
|
|
58
|
+
return true;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const admitted = (
|
|
62
|
+
crowd ,
|
|
63
|
+
one ,
|
|
64
|
+
source ,
|
|
65
|
+
most ,
|
|
66
|
+
share ,
|
|
67
|
+
silent = true,
|
|
68
|
+
) => {
|
|
69
|
+
const before = crowd.sources.get(source);
|
|
70
|
+
if (before !== undefined && before.count >= share && !replaced(crowd, before.silent)) return false;
|
|
71
|
+
if (crowd.held.size >= most && !replaced(crowd, crowd.silent)) return false;
|
|
72
|
+
const standing = crowd.sources.get(source) ?? {
|
|
73
|
+
key: source,
|
|
74
|
+
count: 0,
|
|
75
|
+
silent: new Set (),
|
|
76
|
+
};
|
|
77
|
+
crowd.sources.set(source, standing);
|
|
78
|
+
standing.count += 1;
|
|
79
|
+
crowd.held.set(one, standing);
|
|
80
|
+
if (silent) {
|
|
81
|
+
crowd.silent.add(one);
|
|
82
|
+
standing.silent.add(one);
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Socket } from 'node:net';
|
|
2
|
+
import { type CarriersSeam } from '@nervur.org/genome/seams';
|
|
3
|
+
type Answer = {
|
|
4
|
+
readonly reply: Uint8Array | null;
|
|
5
|
+
} | {
|
|
6
|
+
readonly why: string;
|
|
7
|
+
};
|
|
8
|
+
type Line = {
|
|
9
|
+
readonly socket: Socket;
|
|
10
|
+
readonly asks: Map<number, (answer: Answer) => void>;
|
|
11
|
+
heard: number;
|
|
12
|
+
silent: number;
|
|
13
|
+
};
|
|
14
|
+
export type Dialing = {
|
|
15
|
+
readonly lines: Map<string, Line>;
|
|
16
|
+
next: number;
|
|
17
|
+
redialed: number;
|
|
18
|
+
readonly counted: () => void;
|
|
19
|
+
};
|
|
20
|
+
export declare const dialingOf: (counted?: () => void) => Dialing;
|
|
21
|
+
export declare const SILENT = 3;
|
|
22
|
+
export declare const sendOf: (dialing: Dialing) => CarriersSeam['send'];
|
|
23
|
+
export {};
|
package/src/tcp/dial.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import { connect, } from 'node:net';
|
|
3
|
+
import { BOX_SIZE, } from '@nervur.org/genome/seams';
|
|
4
|
+
import { unhex } from '@nervur.org/genome/standard';
|
|
5
|
+
import { addressOf, frameOf, reader, WARD } from './frame.js';
|
|
6
|
+
import { ringPast } from '../door/longest.js';
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export const dialingOf = (counted = () => undefined) => ({
|
|
25
|
+
lines: new Map(),
|
|
26
|
+
next: 1,
|
|
27
|
+
redialed: 0,
|
|
28
|
+
counted,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export const SILENT = 3;
|
|
32
|
+
|
|
33
|
+
const lineTo = (dialing , address , host , port ) => {
|
|
34
|
+
const held = dialing.lines.get(address);
|
|
35
|
+
if (held !== undefined && !held.socket.destroyed) return held;
|
|
36
|
+
const socket = connect({ host, port });
|
|
37
|
+
const line = { socket, asks: new Map(), heard: 0, silent: 0 };
|
|
38
|
+
const feed = reader(
|
|
39
|
+
(frame) => {
|
|
40
|
+
line.heard += 1;
|
|
41
|
+
line.silent = 0;
|
|
42
|
+
if (frame.kind === 'ask') return;
|
|
43
|
+
const answer = line.asks.get(frame.id);
|
|
44
|
+
line.asks.delete(frame.id);
|
|
45
|
+
answer?.(frame.kind === 'reply' && frame.box.length <= BOX_SIZE ? { reply: frame.box } : { reply: null });
|
|
46
|
+
},
|
|
47
|
+
() => socket.destroy(),
|
|
48
|
+
);
|
|
49
|
+
socket.on('data', (chunk) => feed(new Uint8Array(chunk)));
|
|
50
|
+
let failed = 'closed';
|
|
51
|
+
socket.on('error', (error) => {
|
|
52
|
+
failed = error.message;
|
|
53
|
+
socket.destroy();
|
|
54
|
+
});
|
|
55
|
+
socket.on('close', () => {
|
|
56
|
+
if (dialing.lines.get(address) === line) dialing.lines.delete(address);
|
|
57
|
+
for (const answer of line.asks.values()) answer({ why: `the line to ${address} ${failed}` });
|
|
58
|
+
line.asks.clear();
|
|
59
|
+
});
|
|
60
|
+
dialing.lines.set(address, line);
|
|
61
|
+
return line;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const passed = (dialing , line , heard ) => {
|
|
65
|
+
if (line.heard !== heard || line.socket.destroyed) return;
|
|
66
|
+
line.silent += 1;
|
|
67
|
+
if (line.silent < SILENT) return;
|
|
68
|
+
dialing.redialed += 1;
|
|
69
|
+
line.socket.destroy();
|
|
70
|
+
dialing.counted();
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const wardOf = (ward ) => {
|
|
74
|
+
const read = ward === undefined || ward.length !== WARD * 2 ? null : unhex(ward);
|
|
75
|
+
return read === null || 'why' in read ? null : read.bytes;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const sendOf =
|
|
79
|
+
(dialing ) =>
|
|
80
|
+
(address, bytes, wait, wardText) => {
|
|
81
|
+
const at = addressOf(address);
|
|
82
|
+
if (at === null) return Promise.resolve({ why: `${address} is no tcp address` });
|
|
83
|
+
const ward = wardOf(wardText);
|
|
84
|
+
if (ward === null) return Promise.resolve({ why: `an ask over tcp names its ward, ${WARD * 2} hex` });
|
|
85
|
+
if (bytes.length > BOX_SIZE) return Promise.resolve({ why: `${bytes.length} bytes is more than a box holds` });
|
|
86
|
+
const line = lineTo(dialing, address, at.host, at.port);
|
|
87
|
+
const id = dialing.next;
|
|
88
|
+
dialing.next = (dialing.next + 1) % 2 ** 32;
|
|
89
|
+
const heard = line.heard;
|
|
90
|
+
return new Promise((done) => {
|
|
91
|
+
const stop = ringPast(wait, () => {
|
|
92
|
+
line.asks.delete(id);
|
|
93
|
+
done({ why: `no reply from ${address} within ${wait} ms` });
|
|
94
|
+
passed(dialing, line, heard);
|
|
95
|
+
});
|
|
96
|
+
line.asks.set(id, (answer) => {
|
|
97
|
+
stop();
|
|
98
|
+
done(answer);
|
|
99
|
+
});
|
|
100
|
+
line.socket.write(frameOf({ kind: 'ask', id, ward, box: bytes }));
|
|
101
|
+
});
|
|
102
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare const WARD = 64;
|
|
2
|
+
export type Frame = {
|
|
3
|
+
readonly kind: 'ask';
|
|
4
|
+
readonly id: number;
|
|
5
|
+
readonly ward: Uint8Array;
|
|
6
|
+
readonly box: Uint8Array;
|
|
7
|
+
} | {
|
|
8
|
+
readonly kind: 'reply';
|
|
9
|
+
readonly id: number;
|
|
10
|
+
readonly box: Uint8Array;
|
|
11
|
+
} | {
|
|
12
|
+
readonly kind: 'nothing';
|
|
13
|
+
readonly id: number;
|
|
14
|
+
};
|
|
15
|
+
export declare const frameOf: (frame: Frame) => Uint8Array;
|
|
16
|
+
export declare const bodyOf: (body: Uint8Array, cut?: boolean) => Frame | null;
|
|
17
|
+
export declare const DROPPED: number;
|
|
18
|
+
export declare const reader: (frame: (frame: Frame) => void, broken: () => void, dropped?: number) => ((chunk: Uint8Array) => void);
|
|
19
|
+
export declare const addressOf: (address: string) => {
|
|
20
|
+
readonly host: string;
|
|
21
|
+
readonly port: number;
|
|
22
|
+
} | null;
|
|
23
|
+
export declare const addressAt: (host: string, port: number) => string | null;
|
package/src/tcp/frame.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import { BOX_SIZE } from '@nervur.org/genome/seams';
|
|
3
|
+
|
|
4
|
+
export const WARD = 64;
|
|
5
|
+
|
|
6
|
+
const HEAD = 5;
|
|
7
|
+
const LARGEST = HEAD + WARD + BOX_SIZE;
|
|
8
|
+
const KINDS = ['ask', 'reply', 'nothing'] ;
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
const joined = (a , b ) => {
|
|
21
|
+
const bytes = new Uint8Array(a.length + b.length);
|
|
22
|
+
bytes.set(a);
|
|
23
|
+
bytes.set(b, a.length);
|
|
24
|
+
return bytes;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const frameOf = (frame ) => {
|
|
28
|
+
const rest =
|
|
29
|
+
frame.kind === 'ask' ? joined(frame.ward, frame.box) : frame.kind === 'reply' ? frame.box : new Uint8Array(0);
|
|
30
|
+
const bytes = new Uint8Array(4 + HEAD + rest.length);
|
|
31
|
+
const view = new DataView(bytes.buffer);
|
|
32
|
+
view.setUint32(0, HEAD + rest.length);
|
|
33
|
+
bytes[4] = KINDS.indexOf(frame.kind);
|
|
34
|
+
view.setUint32(5, frame.id);
|
|
35
|
+
bytes.set(rest, 4 + HEAD);
|
|
36
|
+
return bytes;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const bodyOf = (body , cut = false) => {
|
|
40
|
+
const kind = KINDS[body[0] ?? -1];
|
|
41
|
+
if (cut && kind !== 'ask') return null;
|
|
42
|
+
const id = new DataView(body.buffer, body.byteOffset).getUint32(1);
|
|
43
|
+
const rest = body.subarray(HEAD);
|
|
44
|
+
if (kind === 'ask') return rest.length < WARD ? null : { kind, id, ward: rest.slice(0, WARD), box: rest.slice(WARD) };
|
|
45
|
+
if (kind === 'reply') return { kind, id, box: rest.slice() };
|
|
46
|
+
if (kind === 'nothing') return rest.length === 0 ? { kind, id } : null;
|
|
47
|
+
return null;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const held = (
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
) => {
|
|
55
|
+
const chunks = [];
|
|
56
|
+
let first = 0;
|
|
57
|
+
let offset = 0;
|
|
58
|
+
let size = 0;
|
|
59
|
+
const next = () => chunks[first] ?? new Uint8Array(0);
|
|
60
|
+
const read = (count ) => {
|
|
61
|
+
const part = next().subarray(offset, offset + count);
|
|
62
|
+
offset += part.length;
|
|
63
|
+
if (offset === next().length) {
|
|
64
|
+
first += 1;
|
|
65
|
+
offset = 0;
|
|
66
|
+
}
|
|
67
|
+
return part;
|
|
68
|
+
};
|
|
69
|
+
const gathered = (count ) => {
|
|
70
|
+
const bytes = new Uint8Array(count);
|
|
71
|
+
for (let filled = 0; filled < count;) {
|
|
72
|
+
const part = read(count - filled);
|
|
73
|
+
bytes.set(part, filled);
|
|
74
|
+
filled += part.length;
|
|
75
|
+
}
|
|
76
|
+
return bytes;
|
|
77
|
+
};
|
|
78
|
+
return {
|
|
79
|
+
add: (chunk) => {
|
|
80
|
+
chunks.push(chunk);
|
|
81
|
+
size += chunk.length;
|
|
82
|
+
},
|
|
83
|
+
size: () => size,
|
|
84
|
+
take: (count) => {
|
|
85
|
+
size -= count;
|
|
86
|
+
const bytes = next().length - offset >= count ? read(count) : gathered(count);
|
|
87
|
+
if (first * 2 >= chunks.length) {
|
|
88
|
+
chunks.splice(0, first);
|
|
89
|
+
first = 0;
|
|
90
|
+
}
|
|
91
|
+
return bytes;
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const DROPPED = 64 * BOX_SIZE;
|
|
97
|
+
|
|
98
|
+
export const reader = (
|
|
99
|
+
frame ,
|
|
100
|
+
broken ,
|
|
101
|
+
dropped = DROPPED,
|
|
102
|
+
) => {
|
|
103
|
+
const bytes = held();
|
|
104
|
+
let length = null;
|
|
105
|
+
let over = false;
|
|
106
|
+
let dropping = 0;
|
|
107
|
+
let room = dropped;
|
|
108
|
+
return (chunk) => {
|
|
109
|
+
if (over) return;
|
|
110
|
+
bytes.add(chunk);
|
|
111
|
+
for (;;) {
|
|
112
|
+
if (dropping > 0) {
|
|
113
|
+
const count = Math.min(dropping, bytes.size());
|
|
114
|
+
if (count > 0) bytes.take(count);
|
|
115
|
+
dropping -= count;
|
|
116
|
+
if (dropping > 0) return;
|
|
117
|
+
}
|
|
118
|
+
if (length === null) {
|
|
119
|
+
if (bytes.size() < 4) return;
|
|
120
|
+
const head = bytes.take(4);
|
|
121
|
+
length = new DataView(head.buffer, head.byteOffset).getUint32(0);
|
|
122
|
+
}
|
|
123
|
+
const past = length - (LARGEST + 1);
|
|
124
|
+
if (length > LARGEST && past <= room) {
|
|
125
|
+
if (bytes.size() < LARGEST + 1) return;
|
|
126
|
+
const cut = bodyOf(bytes.take(LARGEST + 1), true);
|
|
127
|
+
if (cut !== null) {
|
|
128
|
+
room -= past;
|
|
129
|
+
dropping = past;
|
|
130
|
+
length = null;
|
|
131
|
+
frame(cut);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (length >= HEAD && length <= LARGEST && bytes.size() < length) return;
|
|
136
|
+
const read = length < HEAD || length > LARGEST ? null : bodyOf(bytes.take(length));
|
|
137
|
+
length = null;
|
|
138
|
+
if (read === null) {
|
|
139
|
+
over = true;
|
|
140
|
+
broken();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
frame(read);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
export const addressOf = (address ) => {
|
|
149
|
+
const found = /^tcp:\/\/(\[[0-9A-Fa-f:.]+\]|[^\s/?#@[\]:]+):(\d{1,5})$/i.exec(address);
|
|
150
|
+
if (found?.[1] === undefined || found[2] === undefined) return null;
|
|
151
|
+
const port = Number(found[2]);
|
|
152
|
+
if (port < 1 || port > 65_535) return null;
|
|
153
|
+
return { host: found[1].replace(/^\[|\]$/g, ''), port };
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export const addressAt = (host , port ) => {
|
|
157
|
+
const address = `tcp://${host.includes(':') ? `[${host}]` : host}:${port}`;
|
|
158
|
+
return addressOf(address) === null ? null : address;
|
|
159
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type Server, type Socket } from 'node:net';
|
|
2
|
+
import { type CarriersSeam, type Exchange } from '@nervur.org/genome/seams';
|
|
3
|
+
import { type Crowd } from './crowd.js';
|
|
4
|
+
export type Bounds = {
|
|
5
|
+
readonly connections: number;
|
|
6
|
+
readonly 'connections.share': number;
|
|
7
|
+
readonly 'source.v6': number;
|
|
8
|
+
readonly flight: number;
|
|
9
|
+
readonly idle: number;
|
|
10
|
+
readonly wait: number;
|
|
11
|
+
readonly dropped: number;
|
|
12
|
+
};
|
|
13
|
+
export declare const shareOf: (total: number) => number;
|
|
14
|
+
export declare const carriedOf: (memory: number) => number;
|
|
15
|
+
export declare const BOUNDS: Bounds;
|
|
16
|
+
type Held = {
|
|
17
|
+
readonly socket: Socket;
|
|
18
|
+
readonly id: number;
|
|
19
|
+
readonly flight: Set<Exchange>;
|
|
20
|
+
readonly timer: ReturnType<typeof setTimeout>;
|
|
21
|
+
};
|
|
22
|
+
export declare const LONGEST_SPAN: number;
|
|
23
|
+
export declare const moved: <Named extends {
|
|
24
|
+
readonly idle: number;
|
|
25
|
+
readonly wait: number;
|
|
26
|
+
}>(bounds: Named, words: {
|
|
27
|
+
readonly [name: string]: number;
|
|
28
|
+
}) => Named;
|
|
29
|
+
export type Listening = {
|
|
30
|
+
readonly defaults: Bounds;
|
|
31
|
+
bounds: Bounds;
|
|
32
|
+
readonly waiting: Map<Exchange, Held>;
|
|
33
|
+
readonly servers: Server[];
|
|
34
|
+
readonly sockets: Set<Socket>;
|
|
35
|
+
readonly crowd: Crowd<Socket>;
|
|
36
|
+
next: Exchange;
|
|
37
|
+
accepted: number;
|
|
38
|
+
unreplied: number;
|
|
39
|
+
readonly counted: () => void;
|
|
40
|
+
};
|
|
41
|
+
export declare const listeningOf: (bounds?: Bounds, counted?: () => void) => Listening;
|
|
42
|
+
export declare const boundedBy: (listening: Listening, words: {
|
|
43
|
+
readonly [name: string]: number;
|
|
44
|
+
}) => void;
|
|
45
|
+
export declare const hostsOf: (host: string) => readonly string[];
|
|
46
|
+
export declare const whyNot: (failed: NodeJS.ErrnoException, port: number) => string;
|
|
47
|
+
export declare const listenOf: (listening: Listening, announced: string | null) => CarriersSeam['listen'];
|
|
48
|
+
export declare const openedOf: (listening: Listening) => NonNullable<CarriersSeam['opened']>;
|
|
49
|
+
export declare const replyOf: (listening: Listening) => CarriersSeam['reply'];
|
|
50
|
+
export {};
|