@serfab/cadre-core 1.14.0 → 1.15.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.
Files changed (171) hide show
  1. package/README.md +293 -274
  2. package/dist/cadre-invite-protocol.d.ts +64 -29
  3. package/dist/cadre-invite-protocol.js +207 -38
  4. package/dist/cadre-invite-protocol.js.map +1 -1
  5. package/dist/cadre-join-request.d.ts +42 -0
  6. package/dist/cadre-join-request.js +144 -0
  7. package/dist/cadre-join-request.js.map +1 -0
  8. package/dist/cadre-node.d.ts +375 -79
  9. package/dist/cadre-node.js +758 -186
  10. package/dist/cadre-node.js.map +1 -1
  11. package/dist/control-database.d.ts +92 -12
  12. package/dist/control-database.js +323 -142
  13. package/dist/control-database.js.map +1 -1
  14. package/dist/control-schema.d.ts +1 -1
  15. package/dist/control-schema.js +19 -3
  16. package/dist/control-schema.js.map +1 -1
  17. package/dist/ed25519-key.d.ts +7 -9
  18. package/dist/ed25519-key.js +20 -10
  19. package/dist/ed25519-key.js.map +1 -1
  20. package/dist/fetch-impl.d.ts +12 -0
  21. package/dist/fetch-impl.js +23 -0
  22. package/dist/fetch-impl.js.map +1 -0
  23. package/dist/file-durable-slot.js +2 -1
  24. package/dist/file-durable-slot.js.map +1 -1
  25. package/dist/file-safe-name.d.ts +25 -0
  26. package/dist/file-safe-name.js +29 -0
  27. package/dist/file-safe-name.js.map +1 -0
  28. package/dist/formation-approval.js +5 -11
  29. package/dist/formation-approval.js.map +1 -1
  30. package/dist/fs-atomic.d.ts +0 -19
  31. package/dist/fs-atomic.js +3 -24
  32. package/dist/fs-atomic.js.map +1 -1
  33. package/dist/index.d.ts +12 -5
  34. package/dist/index.js +31 -6
  35. package/dist/index.js.map +1 -1
  36. package/dist/invitation-bootstrap.d.ts +50 -12
  37. package/dist/invitation-bootstrap.js +76 -13
  38. package/dist/invitation-bootstrap.js.map +1 -1
  39. package/dist/joined-strand-store.d.ts +7 -0
  40. package/dist/joined-strand-store.js +9 -0
  41. package/dist/joined-strand-store.js.map +1 -1
  42. package/dist/key-store-file.js +2 -1
  43. package/dist/key-store-file.js.map +1 -1
  44. package/dist/link-budget.d.ts +9 -4
  45. package/dist/link-budget.js +9 -4
  46. package/dist/link-budget.js.map +1 -1
  47. package/dist/machine-removal.d.ts +78 -0
  48. package/dist/machine-removal.js +166 -0
  49. package/dist/machine-removal.js.map +1 -0
  50. package/dist/membership-connection-gater.d.ts +6 -5
  51. package/dist/membership-connection-gater.js +6 -5
  52. package/dist/membership-connection-gater.js.map +1 -1
  53. package/dist/node-claim-payload.d.ts +55 -0
  54. package/dist/node-claim-payload.js +108 -4
  55. package/dist/node-claim-payload.js.map +1 -1
  56. package/dist/node-default-transports.d.ts +27 -0
  57. package/dist/node-default-transports.js +35 -0
  58. package/dist/node-default-transports.js.map +1 -0
  59. package/dist/node-transports.d.ts +30 -0
  60. package/dist/node-transports.js +40 -0
  61. package/dist/node-transports.js.map +1 -0
  62. package/dist/peer-dial.d.ts +124 -27
  63. package/dist/peer-dial.js +254 -30
  64. package/dist/peer-dial.js.map +1 -1
  65. package/dist/peer-join-backfill.js +6 -0
  66. package/dist/peer-join-backfill.js.map +1 -1
  67. package/dist/peer-record.d.ts +6 -45
  68. package/dist/peer-record.js +8 -50
  69. package/dist/peer-record.js.map +1 -1
  70. package/dist/primary-lan-address.d.ts +8 -0
  71. package/dist/primary-lan-address.js +46 -0
  72. package/dist/primary-lan-address.js.map +1 -0
  73. package/dist/provider/client.d.ts +131 -0
  74. package/dist/provider/client.js +226 -0
  75. package/dist/provider/client.js.map +1 -0
  76. package/dist/provider/descriptor.d.ts +41 -0
  77. package/dist/provider/descriptor.js +103 -0
  78. package/dist/provider/descriptor.js.map +1 -0
  79. package/dist/provider/index.d.ts +9 -0
  80. package/dist/provider/index.js +10 -0
  81. package/dist/provider/index.js.map +1 -0
  82. package/dist/provider/oauth.d.ts +125 -0
  83. package/dist/provider/oauth.js +199 -0
  84. package/dist/provider/oauth.js.map +1 -0
  85. package/dist/provider/rent-node.d.ts +101 -0
  86. package/dist/provider/rent-node.js +220 -0
  87. package/dist/provider/rent-node.js.map +1 -0
  88. package/dist/relay-addrs.d.ts +9 -4
  89. package/dist/relay-addrs.js +9 -4
  90. package/dist/relay-addrs.js.map +1 -1
  91. package/dist/relay-reservation.d.ts +51 -16
  92. package/dist/relay-reservation.js +107 -24
  93. package/dist/relay-reservation.js.map +1 -1
  94. package/dist/seed-bootstrap.d.ts +22 -5
  95. package/dist/seed-bootstrap.js +48 -25
  96. package/dist/seed-bootstrap.js.map +1 -1
  97. package/dist/strand-commit-durability.d.ts +54 -0
  98. package/dist/strand-commit-durability.js +74 -0
  99. package/dist/strand-commit-durability.js.map +1 -0
  100. package/dist/strand-database.d.ts +23 -8
  101. package/dist/strand-database.js +41 -13
  102. package/dist/strand-database.js.map +1 -1
  103. package/dist/strand-first-sync-gate.d.ts +7 -1
  104. package/dist/strand-first-sync-gate.js +7 -1
  105. package/dist/strand-first-sync-gate.js.map +1 -1
  106. package/dist/strand-formation-protocol.js +4 -1
  107. package/dist/strand-formation-protocol.js.map +1 -1
  108. package/dist/strand-fret-addrs.d.ts +19 -1
  109. package/dist/strand-fret-addrs.js +27 -9
  110. package/dist/strand-fret-addrs.js.map +1 -1
  111. package/dist/strand-instance-manager.d.ts +81 -19
  112. package/dist/strand-instance-manager.js +216 -105
  113. package/dist/strand-instance-manager.js.map +1 -1
  114. package/dist/strand-move.d.ts +124 -0
  115. package/dist/strand-move.js +438 -0
  116. package/dist/strand-move.js.map +1 -0
  117. package/dist/strand-network-config.js +7 -6
  118. package/dist/strand-network-config.js.map +1 -1
  119. package/dist/strand-peer-redial.d.ts +113 -0
  120. package/dist/strand-peer-redial.js +236 -0
  121. package/dist/strand-peer-redial.js.map +1 -0
  122. package/dist/strand-wake-protocol.js +10 -7
  123. package/dist/strand-wake-protocol.js.map +1 -1
  124. package/dist/types.d.ts +212 -4
  125. package/dist/types.js +10 -0
  126. package/dist/types.js.map +1 -1
  127. package/package.json +18 -10
  128. package/src/cadre-invite-protocol.ts +233 -45
  129. package/src/cadre-join-request.ts +167 -0
  130. package/src/cadre-node.ts +10003 -9381
  131. package/src/control-database.ts +4663 -4451
  132. package/src/control-schema.ts +19 -3
  133. package/src/ed25519-key.ts +20 -10
  134. package/src/fetch-impl.ts +23 -0
  135. package/src/file-durable-slot.ts +64 -63
  136. package/src/file-safe-name.ts +32 -0
  137. package/src/formation-approval.ts +604 -610
  138. package/src/fs-atomic.ts +92 -117
  139. package/src/index.ts +789 -722
  140. package/src/invitation-bootstrap.ts +91 -13
  141. package/src/joined-strand-store.ts +508 -498
  142. package/src/key-store-file.ts +142 -141
  143. package/src/link-budget.ts +9 -4
  144. package/src/machine-removal.ts +216 -0
  145. package/src/membership-connection-gater.ts +638 -637
  146. package/src/node-claim-payload.ts +138 -4
  147. package/src/node-default-transports.ts +38 -0
  148. package/src/node-transports.ts +49 -0
  149. package/src/peer-dial.ts +604 -336
  150. package/src/peer-join-backfill.ts +6 -0
  151. package/src/peer-record.ts +159 -202
  152. package/src/primary-lan-address.ts +47 -0
  153. package/src/provider/client.ts +326 -0
  154. package/src/provider/descriptor.ts +129 -0
  155. package/src/provider/index.ts +59 -0
  156. package/src/provider/oauth.ts +285 -0
  157. package/src/provider/rent-node.ts +275 -0
  158. package/src/relay-addrs.ts +9 -4
  159. package/src/relay-reservation.ts +1211 -1121
  160. package/src/seed-bootstrap.ts +1502 -1471
  161. package/src/strand-commit-durability.ts +116 -0
  162. package/src/strand-database.ts +46 -14
  163. package/src/strand-first-sync-gate.ts +7 -1
  164. package/src/strand-formation-protocol.ts +1039 -1036
  165. package/src/strand-fret-addrs.ts +179 -145
  166. package/src/strand-instance-manager.ts +2026 -1883
  167. package/src/strand-move.ts +585 -0
  168. package/src/strand-network-config.ts +7 -6
  169. package/src/strand-peer-redial.ts +307 -0
  170. package/src/strand-wake-protocol.ts +10 -7
  171. package/src/types.ts +220 -4
@@ -13,7 +13,20 @@
13
13
  * `@libp2p/peer-id` and `uint8arrays`, so it loads on React Native and in the browser.
14
14
  */
15
15
  import { peerIdFromString } from '@libp2p/peer-id';
16
- import { multiaddr, CODE_P2P, CODE_P2P_CIRCUIT } from '@multiformats/multiaddr';
16
+ import {
17
+ multiaddr,
18
+ CODE_DNS,
19
+ CODE_DNS4,
20
+ CODE_DNS6,
21
+ CODE_DNSADDR,
22
+ CODE_IP4,
23
+ CODE_IP6,
24
+ CODE_IP6ZONE,
25
+ CODE_P2P,
26
+ CODE_P2P_CIRCUIT,
27
+ CODE_WS,
28
+ CODE_WSS,
29
+ } from '@multiformats/multiaddr';
17
30
  import { fromString as uint8ArrayFromString, toString as uint8ArrayToString } from 'uint8arrays';
18
31
  import { canonicalJson } from './canonical-json.js';
19
32
  import { parseClaimSecret } from './claim-proof.js';
@@ -67,6 +80,124 @@ export function decodeNodeClaimPayload(text: string): NodeClaimPayload {
67
80
  return requireNodeClaimPayload({ peerId, multiaddrs, secret });
68
81
  }
69
82
 
83
+ /** How {@link selectNodeClaimAddresses} narrows a node's addresses. */
84
+ export interface NodeClaimAddressOptions {
85
+ /**
86
+ * The machine's LAN address, the one a phone at home reaches it on. A string keeps only
87
+ * the private addresses on that IP (dropping Docker bridges, VPN and other interfaces no
88
+ * phone can reach); `null` drops every private address (a machine reached only by its
89
+ * public name); absent keeps them all, for a caller that cannot tell.
90
+ */
91
+ lan?: string | null;
92
+ /** Keep addresses a phone cannot dial: plain TCP. Default false, since phones dial WebSocket and relay only. */
93
+ includeTcp?: boolean;
94
+ }
95
+
96
+ /**
97
+ * The addresses a node code should carry, from the ones a node knows it has (`/status`,
98
+ * a NAT layer's public addresses). A phone dials them in turn, each on its own timeout, so
99
+ * every address it cannot reach only delays a failing claim, and makes the QR code bigger.
100
+ *
101
+ * - Each gains `/p2p/<peerId>` when it lacks a destination; loopback, unspecified and
102
+ * unparsable ones are dropped, and so is plain TCP unless `includeTcp`.
103
+ * - Public names (`/dns*`) come first: they work away from home. Public IP addresses are
104
+ * kept only when there is no public name, since they reach the same machine.
105
+ * - Private addresses follow, narrowed by {@link NodeClaimAddressOptions.lan}; relay
106
+ * addresses last.
107
+ *
108
+ * Pure and dependency-light, like the codec beside it, so it loads on React Native too.
109
+ * Finding the LAN address is the caller's (Node: `@serfab/cadre-core/primary-lan-address`).
110
+ */
111
+ export function selectNodeClaimAddresses(reported: readonly string[], peerId: string, options: NodeClaimAddressOptions = {}): string[] {
112
+ const names: string[] = [];
113
+ const publicIps: string[] = [];
114
+ const privates: string[] = [];
115
+ const relayed: string[] = [];
116
+ for (const raw of reported) {
117
+ const classified = classifyClaimAddress(raw.trim(), peerId, options);
118
+ if (!classified) continue;
119
+ ({ name: names, public: publicIps, private: privates, relay: relayed })[classified.kind].push(classified.addr);
120
+ }
121
+ return [...new Set([...names, ...(names.length > 0 ? [] : publicIps), ...privates, ...relayed])];
122
+ }
123
+
124
+ /**
125
+ * `addr` as a node code carries it: with `/p2p/<peerId>` appended unless it already names a
126
+ * destination. A relay address that ends at its hop (`…/p2p/<relay>/p2p-circuit`) names none,
127
+ * so it gains the node's id too. Throws when `addr` does not parse. For callers that take
128
+ * exact addresses from an operator; {@link selectNodeClaimAddresses} applies it itself.
129
+ */
130
+ export function withClaimDestination(addr: string, peerId: string): string {
131
+ const trimmed = addr.trim();
132
+ return destinationPeerIdOf(trimmed) === null ? `${trimmed}/p2p/${peerId}` : trimmed;
133
+ }
134
+
135
+ /**
136
+ * Where a phone could dial an address from: a relayed address (`/p2p-circuit`), a public name,
137
+ * a public IP, a private (LAN, VPN, link-local) IP, or this machine's own loopback.
138
+ */
139
+ export type DialAddressKind = 'relay' | 'name' | 'public' | 'private' | 'loopback';
140
+
141
+ /**
142
+ * Classify `addr` for a phone dialing it, or undefined when no phone can dial it at all: it
143
+ * does not parse, names the unspecified address, or is plain TCP without `includeTcp` (phones
144
+ * dial WebSocket and relay only).
145
+ */
146
+ export function classifyDialAddress(addr: string, options: { includeTcp?: boolean } = {}): DialAddressKind | undefined {
147
+ const components = componentsOf(addr);
148
+ return components ? classifyComponents(components, options.includeTcp ?? false)?.kind : undefined;
149
+ }
150
+
151
+ type AddressComponents = ReturnType<ReturnType<typeof multiaddr>['getComponents']>;
152
+
153
+ function componentsOf(addr: string): AddressComponents | undefined {
154
+ try {
155
+ return multiaddr(addr).getComponents();
156
+ } catch {
157
+ return undefined;
158
+ }
159
+ }
160
+
161
+ /** {@link classifyDialAddress}, keeping the IP for a caller that narrows private addresses by it. */
162
+ function classifyComponents(components: AddressComponents, includeTcp: boolean): { kind: DialAddressKind; ip?: string } | undefined {
163
+ if (components.some((c) => c.code === CODE_P2P_CIRCUIT)) return { kind: 'relay' };
164
+ if (!includeTcp && !components.some((c) => c.code === CODE_WS || c.code === CODE_WSS)) return undefined;
165
+ const host = components[0];
166
+ if (!host?.value) return undefined;
167
+ if (host.code === CODE_DNS || host.code === CODE_DNS4 || host.code === CODE_DNS6 || host.code === CODE_DNSADDR) {
168
+ return { kind: host.value === 'localhost' ? 'loopback' : 'name' };
169
+ }
170
+ if (host.code !== CODE_IP4 && host.code !== CODE_IP6 && host.code !== CODE_IP6ZONE) return undefined;
171
+ const ip = host.code === CODE_IP6ZONE ? components[1]?.value ?? '' : host.value;
172
+ if (ip === '0.0.0.0' || ip === '::') return undefined;
173
+ if (ip.startsWith('127.') || ip === '::1') return { kind: 'loopback', ip };
174
+ if (host.code !== CODE_IP6ZONE && !isPrivateIp(ip)) return { kind: 'public', ip };
175
+ return { kind: 'private', ip };
176
+ }
177
+
178
+ /** One address's place in {@link selectNodeClaimAddresses}, or undefined to drop it. */
179
+ function classifyClaimAddress(addr: string, peerId: string, options: NodeClaimAddressOptions): { kind: Exclude<DialAddressKind, 'loopback'>; addr: string } | undefined {
180
+ const components = componentsOf(addr);
181
+ if (!components) return undefined;
182
+ const classified = classifyComponents(components, options.includeTcp ?? false);
183
+ if (!classified || classified.kind === 'loopback') return undefined;
184
+ if (classified.kind === 'private' && (options.lan === null || (options.lan !== undefined && classified.ip !== options.lan))) {
185
+ return undefined;
186
+ }
187
+ return { kind: classified.kind, addr: destinationIn(components) === null ? `${addr}/p2p/${peerId}` : addr };
188
+ }
189
+
190
+ /** RFC 1918, carrier-grade NAT (100.64/10), link-local, and IPv6 unique-local / link-local. */
191
+ function isPrivateIp(ip: string): boolean {
192
+ if (ip.includes(':')) return /^(f[cd]|fe[89ab])/i.test(ip);
193
+ const [a, b] = ip.split('.').map(Number) as [number, number];
194
+ return a === 10
195
+ || (a === 172 && b >= 16 && b <= 31)
196
+ || (a === 192 && b === 168)
197
+ || (a === 169 && b === 254)
198
+ || (a === 100 && b >= 64 && b <= 127);
199
+ }
200
+
70
201
  /** The shape rules both directions apply. Returns the payload with its fields trimmed. */
71
202
  function requireNodeClaimPayload(payload: NodeClaimPayload): NodeClaimPayload {
72
203
  const peerId = parsePeerId(payload.peerId);
@@ -112,10 +243,13 @@ function requireAddressOf(addr: string, peerId: ReturnType<typeof peerIdFromStri
112
243
  * The peer an address reaches: its last `/p2p/` component. A relayed address
113
244
  * (`…/p2p/<relay>/p2p-circuit/p2p/<destination>`) names its destination after the
114
245
  * circuit hop; one that ends at the hop names nobody. Same rule as
115
- * `groupAddrsByPeerId` in `peer-addr-book.ts`.
246
+ * `groupAddrsByPeerId` in `peer-addr-book.ts`. Throws when `addr` does not parse.
116
247
  */
117
- function destinationPeerIdOf(addr: string): string | null {
118
- const components = multiaddr(addr).getComponents();
248
+ export function destinationPeerIdOf(addr: string): string | null {
249
+ return destinationIn(multiaddr(addr).getComponents());
250
+ }
251
+
252
+ function destinationIn(components: AddressComponents): string | null {
119
253
  for (let i = components.length - 1; i >= 0; i--) {
120
254
  const component = components[i]!;
121
255
  if (component.code === CODE_P2P_CIRCUIT) return null;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Node-only: `@optimystic/db-p2p`'s default transport list, for `NetworkConfig.defaultTransports`.
3
+ *
4
+ * A node with configured relays is built from an explicit transport list, so that libp2p's relay
5
+ * discovery can be switched off in it (`node-transports.ts`). A Node host that supplies no
6
+ * `network.transports` passes this function so that list still matches what db-p2p would have
7
+ * built. A subpath module (`@serfab/cadre-core/node-default-transports`), like `push-node`,
8
+ * because it names `@libp2p/tcp`: some bundlers (the NativeScript reference app's webpack) take
9
+ * its Node implementation rather than its browser stub, and that needs `net`, `os` and `path`.
10
+ */
11
+
12
+ import { tcp } from '@libp2p/tcp';
13
+ import { webSockets } from '@libp2p/websockets';
14
+ import { circuitRelayTransport } from '@libp2p/circuit-relay-v2';
15
+ import type { Libp2pTransports } from '@optimystic/db-p2p';
16
+ import type { ListenTransportOptions } from './relay-addrs.js';
17
+
18
+ /**
19
+ * TCP, WebSocket exactly when the node's listen entries switch it on, and the circuit-relay
20
+ * transport.
21
+ *
22
+ * Passing an explicit list makes db-p2p ignore `wsPort` for transports, which is why WebSocket is
23
+ * decided here from the same switch. Its other use of `wsPort`, a default WebSocket listen
24
+ * address, never applies: a node with configured relays always has explicit listen entries.
25
+ *
26
+ * NOTE: mirrors `createLibp2pNode`'s default list in
27
+ * `../optimystic/packages/db-p2p/src/libp2p-node.ts` (cadre never sets its `disableTcp`). That file
28
+ * is the source of truth; if its defaults change, this list has to follow. If db-p2p ever takes an
29
+ * option to build its default list with relay discovery off, use that and drop this module and
30
+ * `NetworkConfig.defaultTransports`.
31
+ */
32
+ export function nodeDefaultTransports({ wsPort }: ListenTransportOptions): Libp2pTransports {
33
+ return [
34
+ tcp(),
35
+ ...(wsPort === undefined ? [] : [webSockets()]),
36
+ circuitRelayTransport()
37
+ ];
38
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * The `transports` option for a cadre-built libp2p node, control and strand alike.
3
+ *
4
+ * A node whose bare `/p2p-circuit` search entries cadre generated for configured relays gets its
5
+ * transports through {@link withoutRelayDiscovery}, so libp2p's relay discovery cannot hand those
6
+ * slots to some other relay-capable peer before the per-relay supervisors fill them (see
7
+ * `relay-reservation.ts`, "WHY LIBP2P'S RELAY DISCOVERY IS SWITCHED OFF"). Every other node gets
8
+ * the embedder's list as it was, or no option at all, and `@optimystic/db-p2p` builds its own
9
+ * default list.
10
+ *
11
+ * The wrapper needs a list to wrap. React Native and browser embedders always supply one
12
+ * (`network.transports`); a Node host supplies the default list through
13
+ * `network.defaultTransports` instead, since db-p2p builds its default internally and it names
14
+ * `@libp2p/tcp`, which this module's graph must not reach (`node-default-transports.ts`).
15
+ */
16
+
17
+ import debug from 'debug';
18
+ import type { Libp2pTransports } from '@optimystic/db-p2p';
19
+ import { withoutRelayDiscovery } from './relay-reservation.js';
20
+ import type { ListenTransportOptions } from './relay-addrs.js';
21
+ import type { NetworkConfig } from './types.js';
22
+
23
+ const log = debug('sereus:cadre:node-transports');
24
+
25
+ /**
26
+ * The `transports` option to spread into a node's `createLibp2pNode` options.
27
+ *
28
+ * @param listenTransports what the node's resolved listen entries imply (`resolveTransportOptions`
29
+ * in `relay-addrs.ts`), handed to `network.defaultTransports`.
30
+ * @param relaySlotsSupervised whether cadre generated this node's bare `/p2p-circuit` entries for
31
+ * configured relays and runs a supervisor over them — `network.relayAddrs` on the control node,
32
+ * a non-empty relay list from `strandNodeAddrs` on a strand node.
33
+ */
34
+ export function nodeTransportsOption(
35
+ network: NetworkConfig | undefined,
36
+ listenTransports: ListenTransportOptions,
37
+ relaySlotsSupervised: boolean
38
+ ): { transports?: Libp2pTransports } {
39
+ if (!relaySlotsSupervised) {
40
+ return network?.transports ? { transports: network.transports } : {};
41
+ }
42
+ const transports = network?.transports ?? network?.defaultTransports?.(listenTransports);
43
+ if (transports === undefined) {
44
+ log('Relays are configured but no transport list was supplied (network.transports or ' +
45
+ 'network.defaultTransports); libp2p relay discovery stays on and can take their slots');
46
+ return {};
47
+ }
48
+ return { transports: withoutRelayDiscovery(transports) };
49
+ }