@chaincraft/core 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +104 -17
- package/dist/cli.js +2 -2
- package/dist/features.d.ts +2 -2
- package/dist/features.js +2 -1
- package/dist/hybrid.d.ts +20 -0
- package/dist/hybrid.js +105 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/mesh.d.ts +7 -0
- package/dist/mesh.js +99 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
# @chaincraft/core
|
|
2
2
|
|
|
3
|
-
TypeScript implementation of the Chaincraft wire protocol
|
|
4
|
-
|
|
3
|
+
TypeScript implementation of the Chaincraft wire protocol, object semantics, and
|
|
4
|
+
Level 2 stack (consensus, ledgers, BitTorrent, channels). Same observable
|
|
5
|
+
behaviour as the Python `chaincraft` package and the Rust `chaincraft` crate.
|
|
6
|
+
Release **[1.3.0](https://www.npmjs.com/package/@chaincraft/core)** on npm.
|
|
5
7
|
|
|
6
8
|
Open-source toolkit for peer-to-peer protocols like blockchains, chat rooms,
|
|
7
9
|
BitTorrent swarms, and content-addressed stores.
|
|
8
10
|
|
|
11
|
+
Node **20+**, ESM only (`"type": "module"`). The default entry and `Mesh` use
|
|
12
|
+
`node:net`; browser consumers can use the `@chaincraft/core/browser` entry.
|
|
13
|
+
Raw UDP is the one registry exemption (browsers have no UDP socket). The
|
|
14
|
+
unscoped npm name `chaincraft` belongs to another project.
|
|
15
|
+
|
|
9
16
|
## Install
|
|
10
17
|
|
|
11
18
|
```bash
|
|
@@ -13,11 +20,16 @@ npm install @chaincraft/core
|
|
|
13
20
|
npm install --global @chaincraft/core
|
|
14
21
|
```
|
|
15
22
|
|
|
16
|
-
|
|
23
|
+
Global binaries: `chaincraft-ts-node` (mesh CLI) and `chaincraft-ts-bt-node`
|
|
24
|
+
(BitTorrent client on the same mesh). Default listen is `0.0.0.0:9735`.
|
|
25
|
+
|
|
26
|
+
Without a global install:
|
|
17
27
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
28
|
+
```bash
|
|
29
|
+
npx chaincraft-ts-node
|
|
30
|
+
# or
|
|
31
|
+
node node_modules/@chaincraft/core/dist/node.js
|
|
32
|
+
```
|
|
21
33
|
|
|
22
34
|
## Browser
|
|
23
35
|
|
|
@@ -35,9 +47,40 @@ const peer = new Peer(id, peerOptions());
|
|
|
35
47
|
The data channel is the caller's. `EnvelopeRelay` is the in-memory stand-in
|
|
36
48
|
used by the conformance vectors.
|
|
37
49
|
|
|
38
|
-
##
|
|
50
|
+
## Envelope
|
|
51
|
+
|
|
52
|
+
`id` is SHA-256 of the canonical payload. Keys are sorted. There is no floating
|
|
53
|
+
point on the wire.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { Envelope, encodeFrame } from "@chaincraft/core";
|
|
57
|
+
|
|
58
|
+
const env = Envelope.build("app.hello", { message: "Hello, Chaincraft!" });
|
|
59
|
+
const frame = encodeFrame(env.toBytes());
|
|
60
|
+
console.log(env.id, frame.length);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Object
|
|
64
|
+
|
|
65
|
+
A prototype implements `receive`. Attach it to `Mesh.onApp`. Reject with
|
|
66
|
+
`isValid`. Consensus objects override `addMessage` and the snapshot.
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { BaseSharedObject, type JsonValue } from "@chaincraft/core";
|
|
70
|
+
|
|
71
|
+
class Hello extends BaseSharedObject {
|
|
72
|
+
lines: string[] = [];
|
|
73
|
+
receive(message: JsonValue): void {
|
|
74
|
+
const text = (message as { message?: string }).message ?? "";
|
|
75
|
+
this.lines.push(text);
|
|
76
|
+
console.log("Received:", text);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Hello World (TCP)
|
|
39
82
|
|
|
40
|
-
Two
|
|
83
|
+
Two nodes in one process. A listens on `9735`, B on `9736`, B dials A and
|
|
41
84
|
gossips `app.hello`. Save as `hello.mjs` and run `node hello.mjs`.
|
|
42
85
|
|
|
43
86
|
```js
|
|
@@ -59,7 +102,24 @@ a.close();
|
|
|
59
102
|
b.close();
|
|
60
103
|
```
|
|
61
104
|
|
|
62
|
-
CLI
|
|
105
|
+
## CLI
|
|
106
|
+
|
|
107
|
+
JSON lines on stdin. Events on stdout.
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
chaincraft-ts-node
|
|
111
|
+
chaincraft-ts-node 127.0.0.1:9737
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
| `cmd` | Fields | Effect |
|
|
115
|
+
|---|---|---|
|
|
116
|
+
| `connect` | `addr` | Dial `host:port`, complete `ctl.hello` |
|
|
117
|
+
| `send` | `type`, `data` | Gossip an application envelope |
|
|
118
|
+
| `ids` | — | Payload ids this process has recorded |
|
|
119
|
+
| `quit` | — | Stop |
|
|
120
|
+
|
|
121
|
+
Three languages on one loopback mesh: Python holds `9735`, Rust `9736`,
|
|
122
|
+
TypeScript `9737`.
|
|
63
123
|
|
|
64
124
|
```bash
|
|
65
125
|
python3 -m chaincraft.node
|
|
@@ -67,14 +127,41 @@ chaincraft-node 127.0.0.1:9736
|
|
|
67
127
|
chaincraft-ts-node 127.0.0.1:9737
|
|
68
128
|
```
|
|
69
129
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
130
|
+
On terminals 2 and 3:
|
|
131
|
+
|
|
132
|
+
```json
|
|
133
|
+
{"cmd":"connect","addr":"127.0.0.1:9735"}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
On terminal 1:
|
|
137
|
+
|
|
138
|
+
```json
|
|
139
|
+
{"cmd":"send","type":"app.hello","data":{"message":"Hello, Chaincraft!"}}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
`sent.id` is the same digest on every language. `{"cmd":"ids"}` lists it.
|
|
143
|
+
BitTorrent is a separate process: `chaincraft-ts-bt-node`.
|
|
144
|
+
|
|
145
|
+
## Network
|
|
146
|
+
|
|
147
|
+
Further local nodes take unused ports and `connect`. Remote hosts dial this
|
|
148
|
+
machine's unicast IPv4 or DNS name on 9735. Default bind is `0.0.0.0:9735`;
|
|
149
|
+
loopback Hello World still uses `127.0.0.1:9735`.
|
|
150
|
+
|
|
151
|
+
`connect` allows loopback, RFC1918, public unicast IPv4, `localhost`, `.local`,
|
|
152
|
+
and ordinary DNS names. Unspecified (`0.0.0.0`), link-local, multicast, and
|
|
153
|
+
reserved addresses are refused. Each mesh keeps at most 256 peers.
|
|
154
|
+
|
|
155
|
+
## Library
|
|
73
156
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
157
|
+
| Area | Entry |
|
|
158
|
+
|---|---|
|
|
159
|
+
| Wire | `Envelope`, `encodeFrame`, `canonicalJson`, `sign` / `verify` |
|
|
160
|
+
| Objects | `BaseSharedObject`, `ObjectRegistry`, `StateMemento` |
|
|
161
|
+
| Mesh | `Mesh`, `parseListen` |
|
|
162
|
+
| Gossip apps | `PubSub`, `Stream`, `BitTorrentClient` |
|
|
163
|
+
| Chain | `ProofOfWork`, `ProofOfStake`, `PBFT`, `BalanceLedger`, `UtxoLedger`, `Mempool` |
|
|
164
|
+
| Discovery | `AddressBook`, `NatTraversal`, `parseAddress` |
|
|
78
165
|
|
|
79
|
-
Specification and the
|
|
166
|
+
Specification and the Python and Rust implementations live in the
|
|
80
167
|
[monorepo](https://github.com/jose-compu/chaincraft-monorepo).
|
package/dist/cli.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** Node JSON-lines command surface — SPEC/features/cli-node.md. */
|
|
2
2
|
import { parseJson, payloadDigest } from "./canonical.js";
|
|
3
3
|
import { CanonicalError } from "./errors.js";
|
|
4
|
-
import {
|
|
4
|
+
import { parseAddress } from "./channel.js";
|
|
5
5
|
const TYPE_RE = /^[a-z0-9._-]{1,64}$/;
|
|
6
6
|
function asRecord(value) {
|
|
7
7
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
@@ -62,7 +62,7 @@ export class NodeSession {
|
|
|
62
62
|
}
|
|
63
63
|
connect(command) {
|
|
64
64
|
const addr = command['addr'];
|
|
65
|
-
if (typeof addr !== 'string' || !
|
|
65
|
+
if (typeof addr !== 'string' || !parseAddress(addr).ok)
|
|
66
66
|
return [error('invalid address')];
|
|
67
67
|
if (!this.peers.includes(addr))
|
|
68
68
|
this.peers.push(addr);
|
package/dist/features.d.ts
CHANGED
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
*
|
|
8
8
|
* Add an ID here only when the feature is implemented and its vectors pass.
|
|
9
9
|
*/
|
|
10
|
-
export declare const VERSION = "1.
|
|
10
|
+
export declare const VERSION = "1.3.0";
|
|
11
11
|
export declare const WIRE_VERSION = 1;
|
|
12
|
-
export declare const IMPL = "ts-chaincraft/1.
|
|
12
|
+
export declare const IMPL = "ts-chaincraft/1.3.0";
|
|
13
13
|
export declare const LANGUAGE = "typescript";
|
|
14
14
|
export declare const FEATURES: readonly string[];
|
|
15
15
|
export interface Manifest {
|
package/dist/features.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*
|
|
8
8
|
* Add an ID here only when the feature is implemented and its vectors pass.
|
|
9
9
|
*/
|
|
10
|
-
export const VERSION = '1.
|
|
10
|
+
export const VERSION = '1.3.0';
|
|
11
11
|
export const WIRE_VERSION = 1;
|
|
12
12
|
export const IMPL = `ts-chaincraft/${VERSION}`;
|
|
13
13
|
export const LANGUAGE = 'typescript';
|
|
@@ -53,6 +53,7 @@ export const FEATURES = [
|
|
|
53
53
|
'channel.mailto',
|
|
54
54
|
'channel.webrtc',
|
|
55
55
|
'channel.peer',
|
|
56
|
+
'channel.hybrid',
|
|
56
57
|
'channel.forum',
|
|
57
58
|
'channel.pipe',
|
|
58
59
|
'app.pubsub',
|
package/dist/hybrid.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** One mesh, one channel per peer — SPEC/features/channel-hybrid.md. */
|
|
2
|
+
import type { JsonValue } from './canonical.ts';
|
|
3
|
+
export declare const HOP_CAP: Readonly<Record<string, number>>;
|
|
4
|
+
export interface HybridItem {
|
|
5
|
+
id: string;
|
|
6
|
+
type: string;
|
|
7
|
+
from: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function helloCaps(udp: boolean): string[];
|
|
10
|
+
export declare class LinkMesh {
|
|
11
|
+
private readonly order;
|
|
12
|
+
private readonly scheme;
|
|
13
|
+
private readonly inbox;
|
|
14
|
+
private readonly seen;
|
|
15
|
+
join(addr: string): boolean;
|
|
16
|
+
gossip(src: string, typeName: string, data: JsonValue): string | undefined;
|
|
17
|
+
direct(src: string, dst: string, typeName: string, data: JsonValue): string | undefined;
|
|
18
|
+
take(addr: string): HybridItem[];
|
|
19
|
+
private send;
|
|
20
|
+
}
|
package/dist/hybrid.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/** One mesh, one channel per peer — SPEC/features/channel-hybrid.md. */
|
|
2
|
+
import { parseAddress } from "./channel.js";
|
|
3
|
+
import { Envelope } from "./envelope.js";
|
|
4
|
+
import { ProtocolError } from "./errors.js";
|
|
5
|
+
export const HOP_CAP = {
|
|
6
|
+
tcp: 262144,
|
|
7
|
+
udp: 65507,
|
|
8
|
+
webrtc: 65507,
|
|
9
|
+
pipe: 65507,
|
|
10
|
+
mailto: 4096,
|
|
11
|
+
forum: 4096,
|
|
12
|
+
loop: 64,
|
|
13
|
+
};
|
|
14
|
+
export function helloCaps(udp) {
|
|
15
|
+
const caps = ['digest-sync', 'gossip'];
|
|
16
|
+
if (udp)
|
|
17
|
+
caps.push('udp');
|
|
18
|
+
return caps.sort();
|
|
19
|
+
}
|
|
20
|
+
export class LinkMesh {
|
|
21
|
+
order = [];
|
|
22
|
+
scheme = new Map();
|
|
23
|
+
inbox = new Map();
|
|
24
|
+
seen = new Set();
|
|
25
|
+
join(addr) {
|
|
26
|
+
const parsed = parseAddress(addr);
|
|
27
|
+
if (!parsed.ok || !(parsed.scheme in HOP_CAP))
|
|
28
|
+
return false;
|
|
29
|
+
if (this.inbox.has(parsed.canonical))
|
|
30
|
+
return false;
|
|
31
|
+
this.order.push(parsed.canonical);
|
|
32
|
+
this.scheme.set(parsed.canonical, parsed.scheme);
|
|
33
|
+
this.inbox.set(parsed.canonical, []);
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
gossip(src, typeName, data) {
|
|
37
|
+
return this.send(src, undefined, typeName, data, true);
|
|
38
|
+
}
|
|
39
|
+
direct(src, dst, typeName, data) {
|
|
40
|
+
return this.send(src, dst, typeName, data, false);
|
|
41
|
+
}
|
|
42
|
+
take(addr) {
|
|
43
|
+
const canonical = canonicalOf(addr);
|
|
44
|
+
if (canonical === undefined)
|
|
45
|
+
return [];
|
|
46
|
+
const box = this.inbox.get(canonical);
|
|
47
|
+
if (box === undefined)
|
|
48
|
+
return [];
|
|
49
|
+
const out = box.slice();
|
|
50
|
+
box.length = 0;
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
send(src, dst, typeName, data, gossip) {
|
|
54
|
+
const source = canonicalOf(src);
|
|
55
|
+
if (source === undefined || !this.inbox.has(source))
|
|
56
|
+
return undefined;
|
|
57
|
+
let target;
|
|
58
|
+
if (!gossip) {
|
|
59
|
+
target = dst === undefined ? undefined : canonicalOf(dst);
|
|
60
|
+
if (target === undefined || !this.inbox.has(target))
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
let env;
|
|
64
|
+
try {
|
|
65
|
+
env = Envelope.build(typeName, data, { ts: 0 });
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
if (error instanceof ProtocolError)
|
|
69
|
+
return undefined;
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
if (gossip && env.isControl())
|
|
73
|
+
return undefined;
|
|
74
|
+
if (!gossip && !env.isControl())
|
|
75
|
+
return undefined;
|
|
76
|
+
if (this.seen.has(env.id))
|
|
77
|
+
return env.id;
|
|
78
|
+
this.seen.add(env.id);
|
|
79
|
+
const size = env.toBytes().length;
|
|
80
|
+
const item = { id: env.id, type: env.type, from: source };
|
|
81
|
+
if (gossip) {
|
|
82
|
+
for (const member of this.order) {
|
|
83
|
+
if (member === source)
|
|
84
|
+
continue;
|
|
85
|
+
const scheme = this.scheme.get(member) ?? '';
|
|
86
|
+
const cap = HOP_CAP[scheme] ?? 0;
|
|
87
|
+
if (size <= cap)
|
|
88
|
+
this.inbox.get(member)?.push({ ...item });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
else if (target !== undefined) {
|
|
92
|
+
const scheme = this.scheme.get(target) ?? '';
|
|
93
|
+
const cap = HOP_CAP[scheme] ?? 0;
|
|
94
|
+
if (size <= cap)
|
|
95
|
+
this.inbox.get(target)?.push({ ...item });
|
|
96
|
+
}
|
|
97
|
+
return env.id;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function canonicalOf(addr) {
|
|
101
|
+
const parsed = parseAddress(addr);
|
|
102
|
+
if (!parsed.ok)
|
|
103
|
+
return undefined;
|
|
104
|
+
return parsed.canonical;
|
|
105
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -30,6 +30,7 @@ export { NodeSession } from './cli.ts';
|
|
|
30
30
|
export { BitTorrentClient, Swarm, WIRE_TYPE, MAX_NAME_LEN, MAX_PEER_ID_LEN, infoHash, pieceHash, splitPayload, } from './bittorrent.ts';
|
|
31
31
|
export { Mesh } from './mesh.ts';
|
|
32
32
|
export { DEFAULT_PEER_ORIGIN, PEER_KEY, EnvelopeRelay, fetchPeerId, isPeerId, parsePeerIdBody, peerOptions, type PeerOptions, type RelayItem, } from './peer.ts';
|
|
33
|
+
export { HOP_CAP, LinkMesh, helloCaps, type HybridItem } from './hybrid.ts';
|
|
33
34
|
export { type CodecResult as NatCodecResult, type HandleOut as NatHandle, type NatMessage, type ParsedBinding, type Punch as NatPunch, type RelayOut as NatRelay, APP_NAT, NatTraversal, PUNCH_HEX, bindingRequest, bindingSuccess, parseBindingResponse, } from './nat.ts';
|
|
34
35
|
export * as ecdsa from './ecdsa.ts';
|
|
35
36
|
export * as vdf from './vdf.ts';
|
package/dist/index.js
CHANGED
|
@@ -30,6 +30,7 @@ export { NodeSession } from "./cli.js";
|
|
|
30
30
|
export { BitTorrentClient, Swarm, WIRE_TYPE, MAX_NAME_LEN, MAX_PEER_ID_LEN, infoHash, pieceHash, splitPayload, } from "./bittorrent.js";
|
|
31
31
|
export { Mesh } from "./mesh.js";
|
|
32
32
|
export { DEFAULT_PEER_ORIGIN, PEER_KEY, EnvelopeRelay, fetchPeerId, isPeerId, parsePeerIdBody, peerOptions, } from "./peer.js";
|
|
33
|
+
export { HOP_CAP, LinkMesh, helloCaps } from "./hybrid.js";
|
|
33
34
|
export { APP_NAT, NatTraversal, PUNCH_HEX, bindingRequest, bindingSuccess, parseBindingResponse, } from "./nat.js";
|
|
34
35
|
export * as ecdsa from "./ecdsa.js";
|
|
35
36
|
export * as vdf from "./vdf.js";
|
package/dist/mesh.d.ts
CHANGED
|
@@ -33,12 +33,15 @@ export declare class Mesh {
|
|
|
33
33
|
data: JsonValue;
|
|
34
34
|
}>;
|
|
35
35
|
listenAddr: string;
|
|
36
|
+
private udp;
|
|
37
|
+
private readonly udpPeers;
|
|
36
38
|
onApp: AppHandler;
|
|
37
39
|
onLink: LinkHandler;
|
|
38
40
|
private readonly onEvent;
|
|
39
41
|
private server;
|
|
40
42
|
constructor(impl?: string, onEvent?: (event: MeshEvent) => void);
|
|
41
43
|
start(host?: string, port?: number): Promise<string>;
|
|
44
|
+
bindUdp(): Promise<string>;
|
|
42
45
|
connect(addr: string): Promise<void>;
|
|
43
46
|
broadcast(typeName: string, data: JsonValue): string;
|
|
44
47
|
send(addr: string, typeName: string, data: JsonValue): string;
|
|
@@ -48,5 +51,9 @@ export declare class Mesh {
|
|
|
48
51
|
private addPeer;
|
|
49
52
|
private write;
|
|
50
53
|
private handle;
|
|
54
|
+
private connectUdp;
|
|
55
|
+
private sendUdp;
|
|
56
|
+
private udpLoop;
|
|
57
|
+
private handleDatagram;
|
|
51
58
|
}
|
|
52
59
|
export {};
|
package/dist/mesh.js
CHANGED
|
@@ -12,7 +12,10 @@ import { isConnectAllowed, isResolvedIpAllowed } from "./discovery.js";
|
|
|
12
12
|
import { Envelope } from "./envelope.js";
|
|
13
13
|
import { ProtocolError } from "./errors.js";
|
|
14
14
|
import { IMPL } from "./features.js";
|
|
15
|
-
import { FrameReader, encodeFrame } from "./framing.js";
|
|
15
|
+
import { FrameReader, MAX_UDP_BYTES, encodeFrame } from "./framing.js";
|
|
16
|
+
import { helloCaps } from "./hybrid.js";
|
|
17
|
+
import { UdpLink } from "./adapters.js";
|
|
18
|
+
import { parseAddress } from "./channel.js";
|
|
16
19
|
export const MAX_PEERS = 256;
|
|
17
20
|
export const MAX_RECEIVED = 4096;
|
|
18
21
|
export const CONNECT_TIMEOUT_MS = 5000;
|
|
@@ -36,6 +39,8 @@ export class Mesh {
|
|
|
36
39
|
peers = [];
|
|
37
40
|
received = [];
|
|
38
41
|
listenAddr = '';
|
|
42
|
+
udp;
|
|
43
|
+
udpPeers = [];
|
|
39
44
|
onApp = () => undefined;
|
|
40
45
|
onLink = () => undefined;
|
|
41
46
|
onEvent;
|
|
@@ -60,7 +65,22 @@ export class Mesh {
|
|
|
60
65
|
});
|
|
61
66
|
});
|
|
62
67
|
}
|
|
68
|
+
async bindUdp() {
|
|
69
|
+
if (this.udp === undefined) {
|
|
70
|
+
this.udp = await UdpLink.bind();
|
|
71
|
+
void this.udpLoop();
|
|
72
|
+
}
|
|
73
|
+
return this.udp.hostPort;
|
|
74
|
+
}
|
|
63
75
|
async connect(addr) {
|
|
76
|
+
const parsed = parseAddress(addr);
|
|
77
|
+
if (parsed.ok && parsed.scheme === 'udp') {
|
|
78
|
+
await this.connectUdp(parsed.canonical.slice('udp://'.length));
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (!parsed.ok || parsed.scheme !== 'tcp')
|
|
82
|
+
throw new Error('address not allowed');
|
|
83
|
+
addr = parsed.canonical.slice('tcp://'.length);
|
|
64
84
|
if (!isConnectAllowed(addr))
|
|
65
85
|
throw new Error('address not allowed');
|
|
66
86
|
const [host, port] = splitAddr(addr);
|
|
@@ -93,6 +113,7 @@ export class Mesh {
|
|
|
93
113
|
for (const peer of this.peers) {
|
|
94
114
|
this.write(peer, env);
|
|
95
115
|
}
|
|
116
|
+
this.sendUdp(env);
|
|
96
117
|
this.onApp(typeName, data);
|
|
97
118
|
}
|
|
98
119
|
return env.id;
|
|
@@ -112,6 +133,8 @@ export class Mesh {
|
|
|
112
133
|
}
|
|
113
134
|
close() {
|
|
114
135
|
this.server?.close();
|
|
136
|
+
void this.udp?.close();
|
|
137
|
+
this.udp = undefined;
|
|
115
138
|
for (const peer of this.peers) {
|
|
116
139
|
peer.socket.destroy();
|
|
117
140
|
}
|
|
@@ -130,7 +153,7 @@ export class Mesh {
|
|
|
130
153
|
const remotePort = socket.remotePort ?? 0;
|
|
131
154
|
const peer = { socket, addr: addr ?? `${remote}:${remotePort}` };
|
|
132
155
|
this.peers.push(peer);
|
|
133
|
-
this.write(peer, control.hello(this.impl, { listen: this.listenAddr }));
|
|
156
|
+
this.write(peer, control.hello(this.impl, { listen: this.listenAddr, caps: helloCaps(this.udp !== undefined) }));
|
|
134
157
|
const reader = new FrameReader(NODE_MAX_FRAME);
|
|
135
158
|
socket.on('data', (chunk) => {
|
|
136
159
|
try {
|
|
@@ -184,6 +207,80 @@ export class Mesh {
|
|
|
184
207
|
// drop a broken peer; close will remove it
|
|
185
208
|
}
|
|
186
209
|
}
|
|
210
|
+
this.sendUdp(env);
|
|
211
|
+
}
|
|
212
|
+
this.onApp(env.type, env.data);
|
|
213
|
+
}
|
|
214
|
+
async connectUdp(hostPort) {
|
|
215
|
+
if (!isConnectAllowed(hostPort))
|
|
216
|
+
throw new Error('address not allowed');
|
|
217
|
+
await this.bindUdp();
|
|
218
|
+
if (!this.udpPeers.includes(hostPort)) {
|
|
219
|
+
if (this.peers.length + this.udpPeers.length >= MAX_PEERS) {
|
|
220
|
+
throw new Error('too many peers');
|
|
221
|
+
}
|
|
222
|
+
this.udpPeers.push(hostPort);
|
|
223
|
+
}
|
|
224
|
+
const env = control.hello(this.impl, this.listenAddr ? { caps: helloCaps(true), listen: this.listenAddr } : { caps: helloCaps(true) });
|
|
225
|
+
await this.udp?.send(hostPort, env.toBytes());
|
|
226
|
+
}
|
|
227
|
+
sendUdp(env, skip = '') {
|
|
228
|
+
if (this.udp === undefined)
|
|
229
|
+
return;
|
|
230
|
+
const body = env.toBytes();
|
|
231
|
+
if (body.length > MAX_UDP_BYTES)
|
|
232
|
+
return;
|
|
233
|
+
for (const hostPort of this.udpPeers) {
|
|
234
|
+
if (hostPort === skip)
|
|
235
|
+
continue;
|
|
236
|
+
void this.udp.send(hostPort, body).catch(() => undefined);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
async udpLoop() {
|
|
240
|
+
while (this.udp !== undefined) {
|
|
241
|
+
try {
|
|
242
|
+
const message = await this.udp.recv();
|
|
243
|
+
this.handleDatagram(message.from, message.body);
|
|
244
|
+
}
|
|
245
|
+
catch (error) {
|
|
246
|
+
if (String(error).includes('udp timeout'))
|
|
247
|
+
continue;
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
handleDatagram(src, body) {
|
|
253
|
+
if (body.length > MAX_UDP_BYTES)
|
|
254
|
+
return;
|
|
255
|
+
const env = Envelope.parse(body);
|
|
256
|
+
if (!this.udpPeers.includes(src) && this.peers.length + this.udpPeers.length < MAX_PEERS) {
|
|
257
|
+
this.udpPeers.push(src);
|
|
258
|
+
}
|
|
259
|
+
if (env.type.startsWith('ctl.')) {
|
|
260
|
+
if (env.type === control.HELLO) {
|
|
261
|
+
const hello = control.parseHello(env);
|
|
262
|
+
this.onEvent({ event: 'hello', peer: hello.impl, addr: src });
|
|
263
|
+
}
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
if (env.isControl()) {
|
|
267
|
+
this.onLink(src, env.type, env.data);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (!this.dedup.shouldProcess(env))
|
|
271
|
+
return;
|
|
272
|
+
this.record(env);
|
|
273
|
+
this.onEvent({ event: 'received', id: env.id, type: env.type, data: env.data });
|
|
274
|
+
if (this.dedup.shouldRelay(env)) {
|
|
275
|
+
for (const other of this.peers) {
|
|
276
|
+
try {
|
|
277
|
+
this.write(other, env);
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
// a broken TCP peer is dropped by its close handler
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
this.sendUdp(env, src);
|
|
187
284
|
}
|
|
188
285
|
this.onApp(env.type, env.data);
|
|
189
286
|
}
|