@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
package/src/ws/line.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
|
|
3
|
+
import { BOX_SIZE, } from '@nervur.org/genome/seams';
|
|
4
|
+
import { hex } from '@nervur.org/genome/standard';
|
|
5
|
+
import {
|
|
6
|
+
beatMessage,
|
|
7
|
+
BOUNDED,
|
|
8
|
+
frameOfMessage,
|
|
9
|
+
greetingOf,
|
|
10
|
+
isGreeting,
|
|
11
|
+
LARGEST,
|
|
12
|
+
messageOf,
|
|
13
|
+
|
|
14
|
+
} from './frame.js';
|
|
15
|
+
import { encode, OPCODE, reader } from './rfc6455.js';
|
|
16
|
+
|
|
17
|
+
import { ringPast } from '../door/longest.js';
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
export const REPLACED = 4000;
|
|
22
|
+
export const REPLACED_WHY = 'replaced';
|
|
23
|
+
|
|
24
|
+
export const BOUNDED_WHY = 'put out at a bound';
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
export const exchangesOf = (
|
|
51
|
+
flight ,
|
|
52
|
+
wait ,
|
|
53
|
+
counted = () => undefined,
|
|
54
|
+
quiet = () => undefined,
|
|
55
|
+
) => ({
|
|
56
|
+
held: new Map(),
|
|
57
|
+
flight,
|
|
58
|
+
wait,
|
|
59
|
+
next: 1,
|
|
60
|
+
unreplied: 0,
|
|
61
|
+
counted,
|
|
62
|
+
quiet,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const freed = (exchanges , exchange ) => {
|
|
66
|
+
const held = exchanges.held.get(exchange);
|
|
67
|
+
if (held === undefined) return undefined;
|
|
68
|
+
exchanges.held.delete(exchange);
|
|
69
|
+
held.line.flight.delete(exchange);
|
|
70
|
+
clearTimeout(held.timer);
|
|
71
|
+
if (held.line.flight.size === 0) exchanges.quiet(held.line);
|
|
72
|
+
return held;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const write = (line , opcode , payload ) => {
|
|
76
|
+
if (!line.socket.destroyed) line.socket.write(encode(opcode, payload, line.masked));
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const send = (line , frame ) => write(line, OPCODE.binary, messageOf(frame));
|
|
80
|
+
|
|
81
|
+
const settled = (line ) => {
|
|
82
|
+
if (!line.ending || line.asks.size > 0 || line.flight.size > 0) return;
|
|
83
|
+
line.ending = false;
|
|
84
|
+
const code = new Uint8Array(2);
|
|
85
|
+
new DataView(code.buffer).setUint16(0, REPLACED);
|
|
86
|
+
write(line, OPCODE.close, code);
|
|
87
|
+
line.socket.end();
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const arrival = (
|
|
91
|
+
line ,
|
|
92
|
+
exchanges ,
|
|
93
|
+
frame ,
|
|
94
|
+
arrived ,
|
|
95
|
+
) => {
|
|
96
|
+
if (line.socket.destroyed) return;
|
|
97
|
+
if (line.flight.size >= exchanges.flight) {
|
|
98
|
+
line.socket.destroy();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const exchange = exchanges.next++;
|
|
102
|
+
const timer = setTimeout(() => {
|
|
103
|
+
if (freed(exchanges, exchange) === undefined) return;
|
|
104
|
+
exchanges.unreplied += 1;
|
|
105
|
+
exchanges.counted();
|
|
106
|
+
settled(line);
|
|
107
|
+
}, exchanges.wait);
|
|
108
|
+
line.flight.add(exchange);
|
|
109
|
+
exchanges.held.set(exchange, { line, id: frame.id, timer });
|
|
110
|
+
arrived(exchange, frame.box, hex(frame.ward));
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const answered = (line , frame ) => {
|
|
114
|
+
const answer = line.asks.get(frame.id);
|
|
115
|
+
line.asks.delete(frame.id);
|
|
116
|
+
answer?.(frame.kind === 'reply' && frame.box.length <= BOX_SIZE ? { reply: frame.box } : { reply: null });
|
|
117
|
+
settled(line);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const read = (
|
|
121
|
+
line ,
|
|
122
|
+
message ,
|
|
123
|
+
asked ,
|
|
124
|
+
greeted ,
|
|
125
|
+
) => {
|
|
126
|
+
const greeting = isGreeting(message) ? greetingOf(message) : undefined;
|
|
127
|
+
const frame = greeting === undefined ? frameOfMessage(message) : null;
|
|
128
|
+
if (greeting === null || (greeting === undefined && frame === null)) line.socket.destroy();
|
|
129
|
+
else if (greeting !== undefined) greeted(line, greeting);
|
|
130
|
+
else if (frame?.kind === 'ask') asked(frame);
|
|
131
|
+
else if (frame !== null) answered(line, frame);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const closedBy = (payload ) => {
|
|
135
|
+
const code = payload.length >= 2 ? new DataView(payload.buffer, payload.byteOffset).getUint16(0) : 0;
|
|
136
|
+
return code === REPLACED ? REPLACED_WHY : code === BOUNDED ? BOUNDED_WHY : null;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const fresh = (socket , masked ) => ({
|
|
140
|
+
socket,
|
|
141
|
+
masked,
|
|
142
|
+
asks: new Map(),
|
|
143
|
+
flight: new Set(),
|
|
144
|
+
ending: false,
|
|
145
|
+
next: 1,
|
|
146
|
+
silent: 0,
|
|
147
|
+
pinged: false,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
export const lineOf = (
|
|
151
|
+
socket ,
|
|
152
|
+
masked ,
|
|
153
|
+
exchanges ,
|
|
154
|
+
arrived ,
|
|
155
|
+
head ,
|
|
156
|
+
closed ,
|
|
157
|
+
greeted ,
|
|
158
|
+
) => {
|
|
159
|
+
const line = fresh(socket, masked);
|
|
160
|
+
const heard = () => {
|
|
161
|
+
line.silent = 0;
|
|
162
|
+
};
|
|
163
|
+
const feed = reader(!masked, LARGEST, {
|
|
164
|
+
binary: (message) => {
|
|
165
|
+
heard();
|
|
166
|
+
read(line, message, (frame) => arrival(line, exchanges, frame, arrived), greeted);
|
|
167
|
+
},
|
|
168
|
+
ping: (payload) => {
|
|
169
|
+
heard();
|
|
170
|
+
line.pinged = true;
|
|
171
|
+
write(line, OPCODE.pong, payload);
|
|
172
|
+
},
|
|
173
|
+
pong: heard,
|
|
174
|
+
close: (payload) => {
|
|
175
|
+
failed = closedBy(payload) ?? failed;
|
|
176
|
+
write(line, OPCODE.close, new Uint8Array(0));
|
|
177
|
+
socket.end();
|
|
178
|
+
},
|
|
179
|
+
broken: () => socket.destroy(),
|
|
180
|
+
});
|
|
181
|
+
let failed = 'closed';
|
|
182
|
+
socket.on('data', (chunk) => feed(new Uint8Array(chunk)));
|
|
183
|
+
socket.on('end', () => socket.end());
|
|
184
|
+
socket.on('error', (error) => {
|
|
185
|
+
failed = error.message;
|
|
186
|
+
socket.destroy();
|
|
187
|
+
});
|
|
188
|
+
socket.on('close', () => {
|
|
189
|
+
for (const exchange of line.flight) freed(exchanges, exchange);
|
|
190
|
+
for (const answer of line.asks.values())
|
|
191
|
+
answer(failed === BOUNDED_WHY ? { reply: null } : { why: `the line ${failed}` });
|
|
192
|
+
line.asks.clear();
|
|
193
|
+
closed(line, failed);
|
|
194
|
+
});
|
|
195
|
+
feed(head);
|
|
196
|
+
return line;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
export const replaced = (line ) => {
|
|
200
|
+
line.ending = true;
|
|
201
|
+
settled(line);
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
export const ask = (line , ward , bytes , wait ) => {
|
|
205
|
+
if (line.socket.destroyed) return Promise.resolve({ why: 'the line is closed' });
|
|
206
|
+
const id = line.next;
|
|
207
|
+
line.next = (line.next + 1) % 2 ** 32;
|
|
208
|
+
return new Promise((done) => {
|
|
209
|
+
const stop = ringPast(wait, () => {
|
|
210
|
+
line.asks.delete(id);
|
|
211
|
+
done({ why: `no reply within ${wait} ms` });
|
|
212
|
+
settled(line);
|
|
213
|
+
});
|
|
214
|
+
line.asks.set(id, (answer) => {
|
|
215
|
+
stop();
|
|
216
|
+
done(answer);
|
|
217
|
+
});
|
|
218
|
+
send(line, { kind: 'ask', id, ward, box: bytes });
|
|
219
|
+
});
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
export const replyOf =
|
|
223
|
+
(exchanges ) =>
|
|
224
|
+
(exchange , bytes ) => {
|
|
225
|
+
const held = freed(exchanges, exchange);
|
|
226
|
+
if (held === undefined) return;
|
|
227
|
+
send(held.line, bytes === null ? { kind: 'nothing', id: held.id } : { kind: 'reply', id: held.id, box: bytes });
|
|
228
|
+
settled(held.line);
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
export const greet = (line , message ) => write(line, OPCODE.binary, message);
|
|
232
|
+
|
|
233
|
+
export const ping = (line ) => {
|
|
234
|
+
line.silent += 1;
|
|
235
|
+
write(line, OPCODE.ping, new Uint8Array(0));
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
export const BEATS = 24;
|
|
239
|
+
export const beatenOf = (line , idle ) => {
|
|
240
|
+
const span = Math.ceil(idle / BEATS);
|
|
241
|
+
return setInterval(() => {
|
|
242
|
+
if (line.pinged) return;
|
|
243
|
+
if (line.silent >= BEATS) {
|
|
244
|
+
line.socket.destroy();
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
ping(line);
|
|
248
|
+
greet(line, beatMessage(span));
|
|
249
|
+
}, span);
|
|
250
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type Server } from 'node:http';
|
|
2
|
+
import { Socket } from 'node:net';
|
|
3
|
+
import { type Challenge, type Exchange } from '@nervur.org/genome/seams';
|
|
4
|
+
import { replyOf, type Answer, type Exchanges, type Line } from './line.js';
|
|
5
|
+
import { type Crowd } from '../tcp/crowd.js';
|
|
6
|
+
export type Bounds = {
|
|
7
|
+
readonly lines: number;
|
|
8
|
+
readonly 'lines.share': number;
|
|
9
|
+
readonly handshakes: number;
|
|
10
|
+
readonly 'handshakes.share': number;
|
|
11
|
+
readonly 'source.v6': number;
|
|
12
|
+
readonly flight: number;
|
|
13
|
+
readonly idle: number;
|
|
14
|
+
readonly wait: number;
|
|
15
|
+
};
|
|
16
|
+
export declare const BOUNDS: Bounds;
|
|
17
|
+
export type Listening = {
|
|
18
|
+
readonly defaults: Bounds;
|
|
19
|
+
bounds: Bounds;
|
|
20
|
+
readonly exchanges: Exchanges;
|
|
21
|
+
readonly named: Map<Line, readonly string[]>;
|
|
22
|
+
readonly challenges: Map<Line, Challenge>;
|
|
23
|
+
readonly wards: Map<string, Line>;
|
|
24
|
+
readonly servers: Server[];
|
|
25
|
+
readonly crowd: Crowd<Socket>;
|
|
26
|
+
second: number;
|
|
27
|
+
taken: number;
|
|
28
|
+
readonly bySource: Map<string, number>;
|
|
29
|
+
accepted: number;
|
|
30
|
+
};
|
|
31
|
+
export declare const listeningOf: (bounds?: Bounds, counted?: () => void) => Listening;
|
|
32
|
+
export declare const boundedBy: (listening: Listening, words: {
|
|
33
|
+
readonly [name: string]: number;
|
|
34
|
+
}) => void;
|
|
35
|
+
export declare const listenOf: (listening: Listening, announced: string | null) => (endpoint: string, arrived: (exchange: Exchange, bytes: Uint8Array, ward: string, source: string) => void, declared: (wards: readonly string[], up: boolean) => void) => Promise<{
|
|
36
|
+
readonly addresses: readonly string[];
|
|
37
|
+
} | {
|
|
38
|
+
readonly why: string;
|
|
39
|
+
}>;
|
|
40
|
+
export declare const replyOn: (listening: Listening) => ReturnType<typeof replyOf>;
|
|
41
|
+
export declare const openedOn: (listening: Listening) => (exchange: Exchange) => void;
|
|
42
|
+
export declare const sendOf: (listening: Listening) => (ward: string, bytes: Uint8Array, wait: number) => Promise<Answer>;
|
package/src/ws/listen.js
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
// SPDX-License-Identifier: FSL-1.1-ALv2
|
|
2
|
+
import { randomBytes } from 'node:crypto';
|
|
3
|
+
import { createServer, } from 'node:http';
|
|
4
|
+
import { Socket } from 'node:net';
|
|
5
|
+
import { BOX_SIZE, CHALLENGE_BYTES, } from '@nervur.org/genome/seams';
|
|
6
|
+
import { addressOf, BOUNDED, challengeMessage, PROTOCOL, provenOf, wardOf, } from './frame.js';
|
|
7
|
+
import {
|
|
8
|
+
ask,
|
|
9
|
+
beatenOf,
|
|
10
|
+
exchangesOf,
|
|
11
|
+
greet,
|
|
12
|
+
lineOf,
|
|
13
|
+
replaced,
|
|
14
|
+
replyOf,
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
} from './line.js';
|
|
19
|
+
import { admitted, carried, crowdOf, left, working, } from '../tcp/crowd.js';
|
|
20
|
+
import { BOUNDS as TCP, hostsOf, moved, shareOf, whyNot } from '../tcp/listen.js';
|
|
21
|
+
import { sourceOf } from '../tcp/source.js';
|
|
22
|
+
import { nodeCrypto } from '../fills/crypto.js';
|
|
23
|
+
import { putOut, refused, switching } from './handshake.js';
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
const LINES = TCP.connections * 16;
|
|
37
|
+
const HANDSHAKES = Math.ceil(LINES / 60);
|
|
38
|
+
export const BOUNDS = {
|
|
39
|
+
lines: LINES,
|
|
40
|
+
'lines.share': shareOf(LINES),
|
|
41
|
+
handshakes: HANDSHAKES,
|
|
42
|
+
'handshakes.share': shareOf(HANDSHAKES),
|
|
43
|
+
'source.v6': TCP['source.v6'],
|
|
44
|
+
flight: TCP.flight,
|
|
45
|
+
idle: 600_000,
|
|
46
|
+
wait: TCP.wait,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
export const listeningOf = (bounds = BOUNDS, counted = () => undefined) => {
|
|
68
|
+
const crowd = crowdOf ((socket) => socket.destroy());
|
|
69
|
+
return {
|
|
70
|
+
defaults: bounds,
|
|
71
|
+
bounds,
|
|
72
|
+
exchanges: exchangesOf(bounds.flight, bounds.wait, counted, (line) => working(crowd, line.socket, false)),
|
|
73
|
+
named: new Map(),
|
|
74
|
+
challenges: new Map(),
|
|
75
|
+
wards: new Map(),
|
|
76
|
+
servers: [],
|
|
77
|
+
crowd,
|
|
78
|
+
second: 0,
|
|
79
|
+
taken: 0,
|
|
80
|
+
bySource: new Map(),
|
|
81
|
+
accepted: 0,
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const boundedBy = (listening , words ) => {
|
|
86
|
+
const { lines, handshakes } = words;
|
|
87
|
+
const shares = {
|
|
88
|
+
...(lines === undefined ? {} : { 'lines.share': shareOf(lines) }),
|
|
89
|
+
...(handshakes === undefined ? {} : { 'handshakes.share': shareOf(handshakes) }),
|
|
90
|
+
};
|
|
91
|
+
listening.bounds = moved(listening.defaults, { ...shares, ...words });
|
|
92
|
+
listening.exchanges.flight = listening.bounds.flight;
|
|
93
|
+
listening.exchanges.wait = listening.bounds.wait;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const rolled = (listening ) => {
|
|
97
|
+
const second = Math.floor(Date.now() / 1000);
|
|
98
|
+
if (second === listening.second) return;
|
|
99
|
+
listening.second = second;
|
|
100
|
+
listening.taken = 0;
|
|
101
|
+
listening.bySource.clear();
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const floodedBySource = (listening , source ) => {
|
|
105
|
+
rolled(listening);
|
|
106
|
+
const taken = (listening.bySource.get(source) ?? 0) + 1;
|
|
107
|
+
listening.bySource.set(source, taken);
|
|
108
|
+
return taken > listening.bounds['handshakes.share'];
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const floodedAtFull = (listening ) => {
|
|
112
|
+
if (listening.crowd.held.size < listening.bounds.lines) return false;
|
|
113
|
+
rolled(listening);
|
|
114
|
+
listening.taken += 1;
|
|
115
|
+
return listening.taken > listening.bounds.handshakes;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const boundedAt = (listening , socket , source , bySource ) =>
|
|
119
|
+
bySource ||
|
|
120
|
+
floodedAtFull(listening) ||
|
|
121
|
+
!admitted(listening.crowd, socket, source, listening.bounds.lines, listening.bounds['lines.share']);
|
|
122
|
+
|
|
123
|
+
const wrong = (request , path ) => {
|
|
124
|
+
const url = new URL(request.url ?? '/', 'http://line');
|
|
125
|
+
const offered = (request.headers['sec-websocket-protocol'] ?? '').split(',').map((one) => one.trim());
|
|
126
|
+
if (request.method !== 'GET' || request.headers.upgrade?.toLowerCase() !== 'websocket') return '400 Bad Request';
|
|
127
|
+
if (request.headers['sec-websocket-version'] !== '13') return '426 Upgrade Required';
|
|
128
|
+
if (url.pathname !== path) return '404 Not Found';
|
|
129
|
+
if (!offered.includes(PROTOCOL)) return '400 Bad Request';
|
|
130
|
+
return (request.headers['sec-websocket-key'] ?? '').length === 24 ? null : '400 Bad Request';
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const gone = (listening , declared , line ) => {
|
|
134
|
+
const wards = (listening.named.get(line) ?? []).filter((ward) => listening.wards.get(ward) === line);
|
|
135
|
+
listening.named.delete(line);
|
|
136
|
+
listening.challenges.delete(line);
|
|
137
|
+
left(listening.crowd, line.socket);
|
|
138
|
+
for (const ward of wards) listening.wards.delete(ward);
|
|
139
|
+
if (wards.length > 0) declared(wards, false);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const answered = (
|
|
143
|
+
listening ,
|
|
144
|
+
declared ,
|
|
145
|
+
line ,
|
|
146
|
+
greeting ,
|
|
147
|
+
) => {
|
|
148
|
+
const challenge = listening.challenges.get(line);
|
|
149
|
+
if (challenge === undefined || greeting.kind !== 'proof') return;
|
|
150
|
+
listening.challenges.delete(line);
|
|
151
|
+
const wards = provenOf(nodeCrypto, challenge, greeting.proven);
|
|
152
|
+
listening.named.set(line, wards);
|
|
153
|
+
const olders = new Set ();
|
|
154
|
+
for (const ward of wards) {
|
|
155
|
+
const older = listening.wards.get(ward);
|
|
156
|
+
listening.wards.set(ward, line);
|
|
157
|
+
if (older !== undefined && older !== line) olders.add(older);
|
|
158
|
+
}
|
|
159
|
+
for (const older of olders)
|
|
160
|
+
if (!(listening.named.get(older) ?? []).some((ward) => listening.wards.get(ward) === older)) replaced(older);
|
|
161
|
+
if (wards.length > 0) declared(wards, true);
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const upgraded = (
|
|
165
|
+
listening ,
|
|
166
|
+
arrived ,
|
|
167
|
+
declared ,
|
|
168
|
+
shaken
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
,
|
|
174
|
+
identity ,
|
|
175
|
+
) => {
|
|
176
|
+
const { request, socket, head, source } = shaken;
|
|
177
|
+
socket.write(switching(request.headers['sec-websocket-key'] ?? '', PROTOCOL));
|
|
178
|
+
socket.setNoDelay(true);
|
|
179
|
+
socket.setTimeout(listening.bounds.idle, () => socket.destroy());
|
|
180
|
+
const heard = (exchange , bytes , ward ) => {
|
|
181
|
+
working(listening.crowd, socket, true);
|
|
182
|
+
const holder = listening.wards.get(ward);
|
|
183
|
+
const key = wardOf(ward);
|
|
184
|
+
if (holder === undefined || holder === line || key === null) return arrived(exchange, bytes, ward, source);
|
|
185
|
+
void ask(holder, key, bytes, listening.bounds.wait).then((answer) =>
|
|
186
|
+
replyOn(listening)(exchange, 'why' in answer ? null : answer.reply),
|
|
187
|
+
);
|
|
188
|
+
};
|
|
189
|
+
const line = lineOf(
|
|
190
|
+
socket,
|
|
191
|
+
false,
|
|
192
|
+
listening.exchanges,
|
|
193
|
+
heard,
|
|
194
|
+
head,
|
|
195
|
+
(closed) => {
|
|
196
|
+
clearInterval(beaten);
|
|
197
|
+
gone(listening, declared, closed);
|
|
198
|
+
},
|
|
199
|
+
(greeted, greeting) => answered(listening, declared, greeted, greeting),
|
|
200
|
+
);
|
|
201
|
+
listening.named.set(line, []);
|
|
202
|
+
const challenge = { nonce: new Uint8Array(randomBytes(CHALLENGE_BYTES)), identity };
|
|
203
|
+
listening.challenges.set(line, challenge);
|
|
204
|
+
greet(line, challengeMessage(challenge));
|
|
205
|
+
const beaten = beatenOf(line, listening.bounds.idle);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
export const listenOf =
|
|
209
|
+
(listening , announced ) =>
|
|
210
|
+
(
|
|
211
|
+
endpoint ,
|
|
212
|
+
arrived ,
|
|
213
|
+
declared ,
|
|
214
|
+
) => {
|
|
215
|
+
const url = addressOf(endpoint);
|
|
216
|
+
if (url === null || url.protocol !== 'ws:' || url.search !== '')
|
|
217
|
+
return Promise.resolve({ why: `${endpoint} is no ws endpoint` });
|
|
218
|
+
const identity = new Uint8Array(randomBytes(CHALLENGE_BYTES));
|
|
219
|
+
const server = createServer((_request, response) => {
|
|
220
|
+
response.writeHead(426, { 'Content-Length': 0 }).end();
|
|
221
|
+
});
|
|
222
|
+
server.on('upgrade', (request, socket, head) => {
|
|
223
|
+
if (!(socket instanceof Socket)) {
|
|
224
|
+
socket.destroy();
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
listening.accepted += 1;
|
|
228
|
+
const source = sourceOf(socket.remoteAddress, listening.accepted, listening.bounds['source.v6']);
|
|
229
|
+
const bySource = floodedBySource(listening, source);
|
|
230
|
+
const why = wrong(request, url.pathname);
|
|
231
|
+
if (why !== null) refused(socket, why);
|
|
232
|
+
else if (boundedAt(listening, socket, source, bySource))
|
|
233
|
+
putOut(socket, request.headers['sec-websocket-key'] ?? '', PROTOCOL, BOUNDED);
|
|
234
|
+
else upgraded(listening, arrived, declared, { request, socket, head, source }, identity);
|
|
235
|
+
});
|
|
236
|
+
listening.servers.push(server);
|
|
237
|
+
const port = Number(url.port === '' ? 80 : url.port);
|
|
238
|
+
const host = url.hostname.replace(/^\[|\]$/g, '');
|
|
239
|
+
return new Promise((done) => {
|
|
240
|
+
server.once('error', (failed ) => done({ why: whyNot(failed, port) }));
|
|
241
|
+
server.listen(port, host === '::' ? undefined : host, () => {
|
|
242
|
+
const where = server.address();
|
|
243
|
+
const at = typeof where === 'object' && where !== null ? where.port : 0;
|
|
244
|
+
const hosts = announced === null ? hostsOf(host) : [announced];
|
|
245
|
+
done({
|
|
246
|
+
addresses: hosts.map((one) => `ws://${one.includes(':') ? `[${one}]` : one}:${at}${url.pathname}`),
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
export const replyOn = (listening ) => replyOf(listening.exchanges);
|
|
253
|
+
export const openedOn =
|
|
254
|
+
(listening ) =>
|
|
255
|
+
(exchange ) => {
|
|
256
|
+
const line = listening.exchanges.held.get(exchange)?.line;
|
|
257
|
+
if (line !== undefined) carried(listening.crowd, line.socket);
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
export const sendOf =
|
|
261
|
+
(listening ) =>
|
|
262
|
+
(ward , bytes , wait ) => {
|
|
263
|
+
const line = listening.wards.get(ward);
|
|
264
|
+
const key = wardOf(ward);
|
|
265
|
+
if (line === undefined || key === null)
|
|
266
|
+
return Promise.resolve({ why: `no line names the ward ${ward.slice(0, 8)}` });
|
|
267
|
+
if (bytes.length > BOX_SIZE) return Promise.resolve({ why: `${bytes.length} bytes is more than a box holds` });
|
|
268
|
+
return ask(line, key, bytes, wait).then((answer) => {
|
|
269
|
+
if (!('why' in answer)) carried(listening.crowd, line.socket);
|
|
270
|
+
return answer;
|
|
271
|
+
});
|
|
272
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const OPCODE: {
|
|
2
|
+
readonly continuation: 0;
|
|
3
|
+
readonly text: 1;
|
|
4
|
+
readonly binary: 2;
|
|
5
|
+
readonly close: 8;
|
|
6
|
+
readonly ping: 9;
|
|
7
|
+
readonly pong: 10;
|
|
8
|
+
};
|
|
9
|
+
export declare const GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
|
|
10
|
+
export declare const encode: (opcode: number, payload: Uint8Array, masked: boolean) => Uint8Array;
|
|
11
|
+
export type Heard = {
|
|
12
|
+
readonly binary: (message: Uint8Array) => void;
|
|
13
|
+
readonly ping: (payload: Uint8Array) => void;
|
|
14
|
+
readonly pong: () => void;
|
|
15
|
+
readonly close: (payload: Uint8Array) => void;
|
|
16
|
+
readonly broken: () => void;
|
|
17
|
+
};
|
|
18
|
+
export declare const reader: (masked: boolean, largest: number, heard: Heard, dropped?: number) => ((chunk: Uint8Array) => void);
|