@chaincraft/core 1.1.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 CHANGED
@@ -1,11 +1,18 @@
1
1
  # @chaincraft/core
2
2
 
3
- TypeScript implementation of the Chaincraft wire protocol and object semantics.
4
- Release **[1.1.0](https://www.npmjs.com/package/@chaincraft/core)** on npm.
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,15 +20,67 @@ npm install @chaincraft/core
13
20
  npm install --global @chaincraft/core
14
21
  ```
15
22
 
16
- The node binary is `chaincraft-ts-node`. Default listen is `0.0.0.0:9735`.
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:
27
+
28
+ ```bash
29
+ npx chaincraft-ts-node
30
+ # or
31
+ node node_modules/@chaincraft/core/dist/node.js
32
+ ```
33
+
34
+ ## Browser
35
+
36
+ `@chaincraft/core/browser` does not import `node:net`. `Mesh` stays on the Node
37
+ entry. A page asks the PeerJS broker for an id and passes `peerOptions()` to
38
+ `Peer`. The default broker is `https://dechess-org-peerjs.vercel.app`.
39
+
40
+ ```js
41
+ import { fetchPeerId, peerOptions } from "@chaincraft/core/browser";
42
+
43
+ const id = await fetchPeerId();
44
+ const peer = new Peer(id, peerOptions());
45
+ ```
46
+
47
+ The data channel is the caller's. `EnvelopeRelay` is the in-memory stand-in
48
+ used by the conformance vectors.
49
+
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.
17
67
 
18
- The unscoped npm name `chaincraft` belongs to another project. This package is
19
- `@chaincraft/core`, alongside the `chaincraft` crate and the `chaincraft` Python
20
- package.
68
+ ```ts
69
+ import { BaseSharedObject, type JsonValue } from "@chaincraft/core";
21
70
 
22
- ## Hello World
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)
23
82
 
24
- Two TCP nodes in one process. A listens on `9735`, B on `9736`, B dials A and
83
+ Two nodes in one process. A listens on `9735`, B on `9736`, B dials A and
25
84
  gossips `app.hello`. Save as `hello.mjs` and run `node hello.mjs`.
26
85
 
27
86
  ```js
@@ -43,7 +102,24 @@ a.close();
43
102
  b.close();
44
103
  ```
45
104
 
46
- CLI, three languages on one loopback mesh:
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`.
47
123
 
48
124
  ```bash
49
125
  python3 -m chaincraft.node
@@ -51,14 +127,41 @@ chaincraft-node 127.0.0.1:9736
51
127
  chaincraft-ts-node 127.0.0.1:9737
52
128
  ```
53
129
 
54
- Then `{"cmd":"connect","addr":"127.0.0.1:9735"}` on the last two, and
55
- `{"cmd":"send","type":"app.hello","data":{"message":"Hello, Chaincraft!"}}` on
56
- Python.
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
57
156
 
58
- Further local nodes take unused ports and connect. Remote hosts dial this
59
- machine's unicast IPv4 or DNS name on 9735. `connect` allows loopback,
60
- RFC1918, public unicast IPv4, `localhost`, `.local`, and ordinary DNS names.
61
- Each mesh keeps at most 256 peers.
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` |
62
165
 
63
- Specification and the other implementations live in the
166
+ Specification and the Python and Rust implementations live in the
64
167
  [monorepo](https://github.com/jose-compu/chaincraft-monorepo).
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Browser entry for @chaincraft/core.
3
+ *
4
+ * No TCP mesh and no UDP socket. PeerJS options default to the public
5
+ * broker at DEFAULT_PEER_ORIGIN. Opening the data channel is the caller's
6
+ * `new Peer(id, peerOptions())`.
7
+ */
8
+ export { Envelope } from './envelope.ts';
9
+ export { BaseSharedObject } from './object.ts';
10
+ export { parseAddress } from './channel.ts';
11
+ export { DEFAULT_PEER_ORIGIN, PEER_KEY, EnvelopeRelay, fetchPeerId, isPeerId, parsePeerIdBody, peerOptions, type PeerOptions, type RelayItem, } from './peer.ts';
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Browser entry for @chaincraft/core.
3
+ *
4
+ * No TCP mesh and no UDP socket. PeerJS options default to the public
5
+ * broker at DEFAULT_PEER_ORIGIN. Opening the data channel is the caller's
6
+ * `new Peer(id, peerOptions())`.
7
+ */
8
+ export { Envelope } from "./envelope.js";
9
+ export { BaseSharedObject } from "./object.js";
10
+ export { parseAddress } from "./channel.js";
11
+ export { DEFAULT_PEER_ORIGIN, PEER_KEY, EnvelopeRelay, fetchPeerId, isPeerId, parsePeerIdBody, peerOptions, } from "./peer.js";
package/dist/channel.d.ts CHANGED
@@ -6,6 +6,7 @@ export interface ParsedAddress {
6
6
  canonical: string;
7
7
  cap: string;
8
8
  }
9
+ export declare function isPeerId(value: string): boolean;
9
10
  export declare function parseAddress(raw: string): ParsedAddress;
10
11
  export declare function isConnectAllowed(raw: string): boolean;
11
12
  export interface InboxItem {
package/dist/channel.js CHANGED
@@ -5,7 +5,10 @@ const FAIL = { ok: false, scheme: '', canonical: '', cap: '' };
5
5
  const AUTHORITY = new Set(['tcp', 'udp']);
6
6
  const COLON = ['mailto', 'webrtc', 'forum', 'loop', 'pipe'];
7
7
  const HEX = /^[0-9a-f]+$/;
8
- const HEX64 = /^[0-9a-f]{64}$/;
8
+ const PEER_ID = /^[A-Za-z0-9_-]{1,64}$/;
9
+ export function isPeerId(value) {
10
+ return PEER_ID.test(value);
11
+ }
9
12
  const LOCAL = /^[A-Za-z0-9._+-]{1,64}$/;
10
13
  const LABEL = /^[a-z0-9](?:[a-z0-9-]{0,30}[a-z0-9])?$/;
11
14
  const THREAD = /^[A-Za-z0-9._-]{1,64}$/;
@@ -58,7 +61,7 @@ function parseColon(scheme, rest) {
58
61
  return ok(scheme, `mailto:${rest}`);
59
62
  }
60
63
  if (scheme === 'webrtc') {
61
- if (!HEX64.test(rest))
64
+ if (!isPeerId(rest))
62
65
  return { ...FAIL };
63
66
  return ok(scheme, `webrtc:${rest}`);
64
67
  }
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 { isValidAddress } from "./discovery.js";
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' || !isValidAddress(addr))
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);
@@ -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.1.0";
10
+ export declare const VERSION = "1.3.0";
11
11
  export declare const WIRE_VERSION = 1;
12
- export declare const IMPL = "ts-chaincraft/1.1.0";
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.1.0';
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';
@@ -52,6 +52,8 @@ export const FEATURES = [
52
52
  'channel.udp',
53
53
  'channel.mailto',
54
54
  'channel.webrtc',
55
+ 'channel.peer',
56
+ 'channel.hybrid',
55
57
  'channel.forum',
56
58
  'channel.pipe',
57
59
  'app.pubsub',
@@ -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
@@ -29,6 +29,8 @@ export { AdapterBus, AdapterChannel, UdpLink, MAX_FORUM_BYTES, MAX_MAIL_BYTES, M
29
29
  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
+ 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';
32
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';
33
35
  export * as ecdsa from './ecdsa.ts';
34
36
  export * as vdf from './vdf.ts';
package/dist/index.js CHANGED
@@ -29,6 +29,8 @@ export { AdapterBus, AdapterChannel, UdpLink, MAX_FORUM_BYTES, MAX_MAIL_BYTES, M
29
29
  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
+ export { DEFAULT_PEER_ORIGIN, PEER_KEY, EnvelopeRelay, fetchPeerId, isPeerId, parsePeerIdBody, peerOptions, } from "./peer.js";
33
+ export { HOP_CAP, LinkMesh, helloCaps } from "./hybrid.js";
32
34
  export { APP_NAT, NatTraversal, PUNCH_HEX, bindingRequest, bindingSuccess, parseBindingResponse, } from "./nat.js";
33
35
  export * as ecdsa from "./ecdsa.js";
34
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
  }
package/dist/peer.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ /** PeerJS ids, broker options, and an in-memory cross-scheme relay.
2
+ *
3
+ * SPEC/features/channel-peer.md. Signaling is an HTTP GET. The relay does not
4
+ * open an ICE data channel. This module does not import node:net.
5
+ */
6
+ import type { JsonValue } from './canonical.ts';
7
+ import { isPeerId } from './channel.ts';
8
+ export declare const DEFAULT_PEER_ORIGIN = "https://dechess-org-peerjs.vercel.app";
9
+ export declare const PEER_KEY = "peerjs";
10
+ export interface PeerOptions {
11
+ host: string;
12
+ port: number;
13
+ path: string;
14
+ secure: boolean;
15
+ key: string;
16
+ }
17
+ export interface RelayItem {
18
+ id: string;
19
+ type: string;
20
+ from: string;
21
+ }
22
+ export { isPeerId };
23
+ export declare function parsePeerIdBody(text: string): string | undefined;
24
+ export declare function peerOptions(origin?: string): PeerOptions;
25
+ export declare function fetchPeerId(origin?: string): Promise<string>;
26
+ export declare class EnvelopeRelay {
27
+ private readonly order;
28
+ private readonly inbox;
29
+ private readonly seen;
30
+ join(addr: string): boolean;
31
+ publish(src: string, typeName: string, data: JsonValue): string | undefined;
32
+ take(addr: string): RelayItem[];
33
+ }
package/dist/peer.js ADDED
@@ -0,0 +1,92 @@
1
+ /** PeerJS ids, broker options, and an in-memory cross-scheme relay.
2
+ *
3
+ * SPEC/features/channel-peer.md. Signaling is an HTTP GET. The relay does not
4
+ * open an ICE data channel. This module does not import node:net.
5
+ */
6
+ import { isPeerId, parseAddress } from "./channel.js";
7
+ import { Envelope } from "./envelope.js";
8
+ import { ProtocolError } from "./errors.js";
9
+ export const DEFAULT_PEER_ORIGIN = 'https://dechess-org-peerjs.vercel.app';
10
+ export const PEER_KEY = 'peerjs';
11
+ const RELAY = new Set(['udp', 'mailto', 'webrtc', 'forum', 'pipe']);
12
+ export { isPeerId };
13
+ export function parsePeerIdBody(text) {
14
+ const ident = text.trim();
15
+ if (!isPeerId(ident))
16
+ return undefined;
17
+ return ident;
18
+ }
19
+ export function peerOptions(origin = DEFAULT_PEER_ORIGIN) {
20
+ const parsed = new URL(origin);
21
+ if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') {
22
+ throw new Error('peer origin must be http or https');
23
+ }
24
+ const secure = parsed.protocol === 'https:';
25
+ const port = parsed.port ? Number(parsed.port) : secure ? 443 : 80;
26
+ if (!parsed.hostname)
27
+ throw new Error('peer origin must be http or https');
28
+ return { host: parsed.hostname, port, path: '/', secure, key: PEER_KEY };
29
+ }
30
+ export async function fetchPeerId(origin = DEFAULT_PEER_ORIGIN) {
31
+ const response = await fetch(`${origin.replace(/\/$/, '')}/peerjs/id`, {
32
+ headers: { Accept: 'text/plain' },
33
+ });
34
+ if (!response.ok)
35
+ throw new Error(`peer id fetch failed: ${response.status}`);
36
+ const ident = parsePeerIdBody(await response.text());
37
+ if (ident === undefined)
38
+ throw new Error('invalid peer id');
39
+ return ident;
40
+ }
41
+ export class EnvelopeRelay {
42
+ order = [];
43
+ inbox = new Map();
44
+ seen = new Set();
45
+ join(addr) {
46
+ const parsed = parseAddress(addr);
47
+ if (!parsed.ok || !RELAY.has(parsed.scheme))
48
+ return false;
49
+ if (this.inbox.has(parsed.canonical))
50
+ return false;
51
+ this.order.push(parsed.canonical);
52
+ this.inbox.set(parsed.canonical, []);
53
+ return true;
54
+ }
55
+ publish(src, typeName, data) {
56
+ const parsed = parseAddress(src);
57
+ if (!parsed.ok || !this.inbox.has(parsed.canonical))
58
+ return undefined;
59
+ let env;
60
+ try {
61
+ env = Envelope.build(typeName, data, { ts: 0 });
62
+ }
63
+ catch (error) {
64
+ if (error instanceof ProtocolError)
65
+ return undefined;
66
+ throw error;
67
+ }
68
+ if (this.seen.has(env.id))
69
+ return env.id;
70
+ this.seen.add(env.id);
71
+ const item = { id: env.id, type: env.type, from: parsed.canonical };
72
+ for (const member of this.order) {
73
+ if (member === parsed.canonical)
74
+ continue;
75
+ const box = this.inbox.get(member);
76
+ if (box)
77
+ box.push({ ...item });
78
+ }
79
+ return env.id;
80
+ }
81
+ take(addr) {
82
+ const parsed = parseAddress(addr);
83
+ if (!parsed.ok)
84
+ return [];
85
+ const box = this.inbox.get(parsed.canonical);
86
+ if (!box)
87
+ return [];
88
+ const out = box.slice();
89
+ box.length = 0;
90
+ return out;
91
+ }
92
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chaincraft/core",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "A platform for decentralized protocols — wire, objects, and pedagogical consensus",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -23,6 +23,10 @@
23
23
  ".": {
24
24
  "types": "./dist/index.d.ts",
25
25
  "default": "./dist/index.js"
26
+ },
27
+ "./browser": {
28
+ "types": "./dist/browser.d.ts",
29
+ "default": "./dist/browser.js"
26
30
  }
27
31
  },
28
32
  "bin": {