@serfab/cadre-client 0.0.0-stage → 2.1.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 (116) hide show
  1. package/ADOPTING.md +228 -0
  2. package/README.md +130 -3
  3. package/dist/address.d.ts +45 -0
  4. package/dist/address.js +109 -0
  5. package/dist/address.js.map +1 -0
  6. package/dist/app-link/index.d.ts +16 -0
  7. package/dist/app-link/index.js +17 -0
  8. package/dist/app-link/index.js.map +1 -0
  9. package/dist/app-link/join-response.d.ts +31 -0
  10. package/dist/app-link/join-response.js +87 -0
  11. package/dist/app-link/join-response.js.map +1 -0
  12. package/dist/app-link/known-apps.d.ts +32 -0
  13. package/dist/app-link/known-apps.js +51 -0
  14. package/dist/app-link/known-apps.js.map +1 -0
  15. package/dist/app-link/link-request-store.d.ts +50 -0
  16. package/dist/app-link/link-request-store.js +101 -0
  17. package/dist/app-link/link-request-store.js.map +1 -0
  18. package/dist/app-link/links.d.ts +47 -0
  19. package/dist/app-link/links.js +76 -0
  20. package/dist/app-link/links.js.map +1 -0
  21. package/dist/app-link/recommend-links.d.ts +30 -0
  22. package/dist/app-link/recommend-links.js +33 -0
  23. package/dist/app-link/recommend-links.js.map +1 -0
  24. package/dist/app-link/recommendation-store.d.ts +54 -0
  25. package/dist/app-link/recommendation-store.js +99 -0
  26. package/dist/app-link/recommendation-store.js.map +1 -0
  27. package/dist/bootstrap.d.ts +106 -0
  28. package/dist/bootstrap.js +106 -0
  29. package/dist/bootstrap.js.map +1 -0
  30. package/dist/inbox.d.ts +10 -0
  31. package/dist/inbox.js +28 -0
  32. package/dist/inbox.js.map +1 -0
  33. package/dist/index.d.ts +15 -0
  34. package/dist/index.js +16 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/invitation-text.d.ts +81 -0
  37. package/dist/invitation-text.js +80 -0
  38. package/dist/invitation-text.js.map +1 -0
  39. package/dist/lifecycle/background-runner.d.ts +94 -0
  40. package/dist/lifecycle/background-runner.js +278 -0
  41. package/dist/lifecycle/background-runner.js.map +1 -0
  42. package/dist/lifecycle/index.d.ts +9 -0
  43. package/dist/lifecycle/index.js +10 -0
  44. package/dist/lifecycle/index.js.map +1 -0
  45. package/dist/loader.d.ts +47 -0
  46. package/dist/loader.js +93 -0
  47. package/dist/loader.js.map +1 -0
  48. package/dist/model.d.ts +309 -0
  49. package/dist/model.js +22 -0
  50. package/dist/model.js.map +1 -0
  51. package/dist/node-code-text.d.ts +10 -0
  52. package/dist/node-code-text.js +30 -0
  53. package/dist/node-code-text.js.map +1 -0
  54. package/dist/result.d.ts +79 -0
  55. package/dist/result.js +84 -0
  56. package/dist/result.js.map +1 -0
  57. package/dist/sereus/client.d.ts +49 -0
  58. package/dist/sereus/client.js +420 -0
  59. package/dist/sereus/client.js.map +1 -0
  60. package/dist/sereus/edge-host.d.ts +22 -0
  61. package/dist/sereus/edge-host.js +61 -0
  62. package/dist/sereus/edge-host.js.map +1 -0
  63. package/dist/sereus/host.d.ts +57 -0
  64. package/dist/sereus/host.js +2 -0
  65. package/dist/sereus/host.js.map +1 -0
  66. package/dist/sereus/index.d.ts +8 -0
  67. package/dist/sereus/index.js +9 -0
  68. package/dist/sereus/index.js.map +1 -0
  69. package/dist/sereus/invitations.d.ts +16 -0
  70. package/dist/sereus/invitations.js +333 -0
  71. package/dist/sereus/invitations.js.map +1 -0
  72. package/dist/sereus/node-code.d.ts +52 -0
  73. package/dist/sereus/node-code.js +51 -0
  74. package/dist/sereus/node-code.js.map +1 -0
  75. package/dist/store.d.ts +27 -0
  76. package/dist/store.js +51 -0
  77. package/dist/store.js.map +1 -0
  78. package/dist/stores.d.ts +38 -0
  79. package/dist/stores.js +48 -0
  80. package/dist/stores.js.map +1 -0
  81. package/examples/README.md +20 -0
  82. package/examples/nativescript/cadrons-page.xml +24 -0
  83. package/examples/nativescript/cadrons-view-model.ts +68 -0
  84. package/examples/react-native/CadronList.tsx +204 -0
  85. package/examples/react-native/InvitationPanel.tsx +191 -0
  86. package/examples/react-native/useStore.ts +11 -0
  87. package/examples/svelte/CadronList.svelte +86 -0
  88. package/examples/svelte/InvitationPanel.svelte +97 -0
  89. package/package.json +65 -3
  90. package/src/address.ts +125 -0
  91. package/src/app-link/index.ts +45 -0
  92. package/src/app-link/join-response.ts +109 -0
  93. package/src/app-link/known-apps.ts +70 -0
  94. package/src/app-link/link-request-store.ts +129 -0
  95. package/src/app-link/links.ts +90 -0
  96. package/src/app-link/recommend-links.ts +49 -0
  97. package/src/app-link/recommendation-store.ts +132 -0
  98. package/src/bootstrap.ts +184 -0
  99. package/src/globals.d.ts +26 -0
  100. package/src/inbox.ts +36 -0
  101. package/src/index.ts +15 -0
  102. package/src/invitation-text.ts +156 -0
  103. package/src/lifecycle/background-runner.ts +363 -0
  104. package/src/lifecycle/index.ts +17 -0
  105. package/src/loader.ts +103 -0
  106. package/src/model.ts +327 -0
  107. package/src/node-code-text.ts +25 -0
  108. package/src/result.ts +131 -0
  109. package/src/sereus/client.ts +454 -0
  110. package/src/sereus/edge-host.ts +81 -0
  111. package/src/sereus/host.ts +56 -0
  112. package/src/sereus/index.ts +8 -0
  113. package/src/sereus/invitations.ts +359 -0
  114. package/src/sereus/node-code.ts +80 -0
  115. package/src/store.ts +61 -0
  116. package/src/stores.ts +62 -0
@@ -0,0 +1,99 @@
1
+ /** How long a pre-approval waits for the recommended app's request: enough to install it from the store and open it. */
2
+ export const RECOMMENDATION_TTL_MS = 60 * 60_000;
3
+ /**
4
+ * Create one per app, over `PhoneNode.recommendationSlot()`: its calls run one at a time, so two
5
+ * deliveries of the same request (the launch URL and a `url` event) cannot both take the
6
+ * pre-approval.
7
+ */
8
+ export class AppRecommendationStore {
9
+ slot;
10
+ now;
11
+ turn = Promise.resolve();
12
+ constructor(slot, now = Date.now) {
13
+ this.slot = slot;
14
+ this.now = now;
15
+ }
16
+ /** Record a pre-approval, replacing any for the same scheme. */
17
+ approve(entry) {
18
+ return this.inTurn(async () => {
19
+ const approved = { ...entry, approvedAtMs: this.now() };
20
+ const others = (await this.readLive(entry.partyId)).filter((live) => live.scheme !== entry.scheme);
21
+ await this.write([approved, ...others]);
22
+ return approved;
23
+ });
24
+ }
25
+ /** The live pre-approvals for `partyId`, most recently approved first, for the owner app's list. A slot read that fails throws. */
26
+ live(partyId) {
27
+ return this.inTurn(() => this.readLive(partyId));
28
+ }
29
+ /** Drop the pre-approval for `scheme`, if any. */
30
+ withdraw(scheme) {
31
+ return this.inTurn(async () => {
32
+ // Other parties' entries stay: withdraw names no party, and they are never returned for this one.
33
+ await this.write((await this.readUnexpired()).filter((entry) => entry.scheme !== scheme));
34
+ });
35
+ }
36
+ /**
37
+ * The live pre-approval for `scheme` in `partyId`, removed as it is returned; null when there is
38
+ * none. Removed even when the caller then fails to mint, so it allows one answer at most.
39
+ */
40
+ take(scheme, partyId) {
41
+ return this.inTurn(async () => {
42
+ const live = await this.readLive(partyId);
43
+ const taken = live.find((entry) => entry.scheme === scheme);
44
+ if (!taken)
45
+ return null;
46
+ await this.write(live.filter((entry) => entry !== taken));
47
+ return taken;
48
+ });
49
+ }
50
+ inTurn(op) {
51
+ const result = this.turn.then(op);
52
+ this.turn = result.catch(() => undefined);
53
+ return result;
54
+ }
55
+ /** The unexpired entries for `partyId`: what every write keeps, so expired and other parties' entries are pruned by it. */
56
+ async readLive(partyId) {
57
+ return (await this.readUnexpired()).filter((entry) => entry.partyId === partyId);
58
+ }
59
+ async readUnexpired() {
60
+ const text = await this.slot.load();
61
+ if (text === undefined)
62
+ return [];
63
+ const now = this.now();
64
+ // NOTE: an entry dated ahead of `now` (the clock set back after approving) stays live until the
65
+ // clock passes its date plus the hour, longer than the hour the user agreed to. If clock changes
66
+ // ever matter here, treat an entry more than a minute in the future as expired.
67
+ return parseRecommendations(text).filter((entry) => now - entry.approvedAtMs <= RECOMMENDATION_TTL_MS);
68
+ }
69
+ write(entries) {
70
+ return this.slot.save(JSON.stringify(entries));
71
+ }
72
+ }
73
+ /** The saved list, or an empty one, logged, when it is not one: a record that cannot be read allows nothing. */
74
+ function parseRecommendations(text) {
75
+ let parsed;
76
+ try {
77
+ parsed = JSON.parse(text);
78
+ }
79
+ catch (err) {
80
+ console.warn('[cadre-client/app-link] the app recommendation record is not JSON; reading it as none:', err);
81
+ return [];
82
+ }
83
+ const entries = Array.isArray(parsed) ? parsed.map(parseRecommendation) : null;
84
+ if (!entries?.every((entry) => entry !== null)) {
85
+ console.warn('[cadre-client/app-link] the app recommendation record has the wrong shape; reading it as none:', text);
86
+ return [];
87
+ }
88
+ return entries;
89
+ }
90
+ /** Only the named fields are read, so a stored entry carrying a field this shape does not have parses without it. */
91
+ function parseRecommendation(value) {
92
+ const { scheme, appName, partyId, approvedAtMs } = (typeof value === 'object' && value !== null ? value : {});
93
+ if (typeof scheme !== 'string' || typeof appName !== 'string' || typeof partyId !== 'string'
94
+ || typeof approvedAtMs !== 'number' || !Number.isFinite(approvedAtMs)) {
95
+ return null;
96
+ }
97
+ return { scheme, appName, partyId, approvedAtMs };
98
+ }
99
+ //# sourceMappingURL=recommendation-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"recommendation-store.js","sourceRoot":"","sources":["../../src/app-link/recommendation-store.ts"],"names":[],"mappings":"AAgBA,wHAAwH;AACxH,MAAM,CAAC,MAAM,qBAAqB,GAAG,EAAE,GAAG,MAAM,CAAC;AAajD;;;;GAIG;AACH,MAAM,OAAO,sBAAsB;IAIhB;IACA;IAJV,IAAI,GAAqB,OAAO,CAAC,OAAO,EAAE,CAAC;IAEnD,YACkB,IAAiB,EACjB,MAAoB,IAAI,CAAC,GAAG;QAD5B,SAAI,GAAJ,IAAI,CAAa;QACjB,QAAG,GAAH,GAAG,CAAyB;IAC3C,CAAC;IAEJ,gEAAgE;IAChE,OAAO,CAAC,KAA8C;QACrD,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,IAAI,EAAE;YAC7B,MAAM,QAAQ,GAAsB,EAAE,GAAG,KAAK,EAAE,YAAY,EAAE,IAAI,CAA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AI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC3B,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACd,OAAO,CAAC,IAAI,CAAC,wFAAwF,EAAE,GAAG,CAAC,CAAC;QAC5G,OAAO,EAAE,CAAC;IACX,CAAC;IACD,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IAC/E,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,KAAK,EAA8B,EAAE,CAAC,KAAK,KAAK,IAAI,CAAC,EAAE,CAAC;QAC5E,OAAO,CAAC,IAAI,CAAC,gGAAgG,EAAE,IAAI,CAAC,CAAC;QACrH,OAAO,EAAE,CAAC;IACX,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC;AAED,qHAAqH;AACrH,SAAS,mBAAmB,CAAC,KAAc;IAC1C,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,GAAG,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAA4B,CAAC;IACzI,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,OAAO,KAAK,QAAQ;WACxF,OAAO,YAAY,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC;QACxE,OAAO,IAAI,CAAC;IACb,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,CAAC;AACnD,CAAC"}
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Which addresses an invitation names: the few that will reach one of the person's cadrons
3
+ * (the machines of their cadre) from anywhere, in the order a joiner should try them.
4
+ *
5
+ * WORKAROUND(sereus: ranked open invitations, curated published records). cadre-core's
6
+ * `createOpenInvitation` names each other machine's first four addresses in the order the
7
+ * machine published them, and a machine publishes every address libp2p reports, loopback,
8
+ * private and plain TCP first. A cadre-host machine on a VPS so ends up named only by
9
+ * addresses no phone can dial, its public WebSocket address cut. This plans the list itself from the
10
+ * machines' full records, and the app puts it in the invitation before encoding it (the list is
11
+ * not signed; the responder checks only the token).
12
+ *
13
+ * Pure: the evidence comes in (`BootstrapInput`), the plan goes out. `/sereus` gathers it.
14
+ */
15
+ import { type CadronAddressKind } from './address.js';
16
+ /** Where an address of a cadron came from, best evidence first. */
17
+ export type AddressSource =
18
+ /** A connection this device opened to it, with no relay hop, is open now: it answered. */
19
+ 'connection'
20
+ /** The cadron's own signed, recently published record. */
21
+ | 'record'
22
+ /** What this device was handed when it claimed the cadron, kept until its record arrives. */
23
+ | 'claim';
24
+ export interface CadronCandidate {
25
+ /** The cadron's control peer id: what an invitation's addresses end in (`/p2p/<peerId>`). */
26
+ peerId: string;
27
+ /** It declares itself always on (its signed record); unknown counts as not. */
28
+ alwaysOn: boolean;
29
+ /** When its record was last published (ms), or null when none has reached this device yet. */
30
+ lastPublishedAt: number | null;
31
+ addrs: ReadonlyArray<{
32
+ addr: string;
33
+ source: AddressSource;
34
+ }>;
35
+ }
36
+ /** How invitations name the person's cadrons. Kept per device for now. */
37
+ export interface CadronPreferences {
38
+ /**
39
+ * Cadrons to name first, in this order (control peer ids).
40
+ * WORKAROUND(sereus: a party-level bootstrap preference): kept on this device, so each of the
41
+ * person's devices has its own.
42
+ */
43
+ preferred: string[];
44
+ /** The person's own names for their cadrons, by peer id, for screens and the readable invitation. */
45
+ names: Record<string, string>;
46
+ /** How many addresses an invitation names in all (1–8). Fewer: a shorter link, a sparser QR. */
47
+ maxAddrs: number;
48
+ /**
49
+ * This device's own addresses (a phone: its relay addresses): `fallback`, only when no cadron
50
+ * reaches from anywhere; `always`, in the last slot when one is left; `never`.
51
+ */
52
+ thisDevice: 'fallback' | 'always' | 'never';
53
+ /** Name private-network addresses too (a joiner on the same network). Off by default. */
54
+ allowPrivate: boolean;
55
+ }
56
+ export declare const DEFAULT_CADRON_PREFERENCES: CadronPreferences;
57
+ export declare const MAX_BOOTSTRAP_ADDRS = 8;
58
+ /**
59
+ * The stored form's version (`format` beside the fields). The app keeps the text opaque; a later
60
+ * format is read and migrated here, and a field this version does not know is dropped.
61
+ */
62
+ export declare const CADRON_PREFERENCES_FORMAT = 1;
63
+ /** Preferences as stored, made whole and in range (missing fields defaulted, ids deduplicated). */
64
+ export declare function normalizePreferences(raw: Partial<CadronPreferences> | null | undefined): CadronPreferences;
65
+ export interface BootstrapInput {
66
+ cadrons: readonly CadronCandidate[];
67
+ thisDevice: {
68
+ peerId: string;
69
+ addrs: readonly string[];
70
+ };
71
+ prefs: CadronPreferences;
72
+ }
73
+ export interface BootstrapEntry {
74
+ addr: string;
75
+ peerId: string;
76
+ /** This device rather than one of its cadrons. */
77
+ thisDevice: boolean;
78
+ kind: CadronAddressKind;
79
+ source: AddressSource | 'this-device';
80
+ preferred: boolean;
81
+ alwaysOn: boolean;
82
+ }
83
+ export type BootstrapWarning =
84
+ /** No cadron has an address that reaches from anywhere: only this device (if anything) answers. */
85
+ 'no-cadron-reachable-from-anywhere'
86
+ /** A named cadron's address comes from its claim: its signed record has not reached this device yet. */
87
+ | 'cadron-record-not-yet-published'
88
+ /** Nothing at all reaches from anywhere: the invitation works only on the inviter's own network, if at all. */
89
+ | 'unreachable';
90
+ export interface BootstrapPlan {
91
+ entries: BootstrapEntry[];
92
+ /**
93
+ * Usable addresses the plan left out, best first: cadrons' addresses past the cap, then this
94
+ * device's when it was not wanted. What a person can add to one invitation by hand.
95
+ */
96
+ others: BootstrapEntry[];
97
+ warnings: BootstrapWarning[];
98
+ }
99
+ /**
100
+ * The plan: preferred cadrons first (in the person's order), then other always-on cadrons, then
101
+ * the rest, each group newest record first; each cadron's addresses best first (reach, then
102
+ * evidence), loopback and undialable ones never; at most `maxAddrs` in all, one address per
103
+ * cadron before any cadron gets a second, so a second cadron backs up the first. This device's
104
+ * addresses go in as `prefs.thisDevice` says.
105
+ */
106
+ export declare function planBootstrap(input: BootstrapInput): BootstrapPlan;
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Which addresses an invitation names: the few that will reach one of the person's cadrons
3
+ * (the machines of their cadre) from anywhere, in the order a joiner should try them.
4
+ *
5
+ * WORKAROUND(sereus: ranked open invitations, curated published records). cadre-core's
6
+ * `createOpenInvitation` names each other machine's first four addresses in the order the
7
+ * machine published them, and a machine publishes every address libp2p reports, loopback,
8
+ * private and plain TCP first. A cadre-host machine on a VPS so ends up named only by
9
+ * addresses no phone can dial, its public WebSocket address cut. This plans the list itself from the
10
+ * machines' full records, and the app puts it in the invitation before encoding it (the list is
11
+ * not signed; the responder checks only the token).
12
+ *
13
+ * Pure: the evidence comes in (`BootstrapInput`), the plan goes out. `/sereus` gathers it.
14
+ */
15
+ import { addressKind, ADDRESS_KIND_RANK, reachesAnywhere } from './address.js';
16
+ export const DEFAULT_CADRON_PREFERENCES = {
17
+ preferred: [], names: {}, maxAddrs: 2, thisDevice: 'fallback', allowPrivate: false,
18
+ };
19
+ export const MAX_BOOTSTRAP_ADDRS = 8;
20
+ /**
21
+ * The stored form's version (`format` beside the fields). The app keeps the text opaque; a later
22
+ * format is read and migrated here, and a field this version does not know is dropped.
23
+ */
24
+ export const CADRON_PREFERENCES_FORMAT = 1;
25
+ /** Preferences as stored, made whole and in range (missing fields defaulted, ids deduplicated). */
26
+ export function normalizePreferences(raw) {
27
+ const p = { ...DEFAULT_CADRON_PREFERENCES, ...(raw ?? {}) };
28
+ const maxAddrs = Number.isInteger(p.maxAddrs) ? Math.min(MAX_BOOTSTRAP_ADDRS, Math.max(1, p.maxAddrs)) : DEFAULT_CADRON_PREFERENCES.maxAddrs;
29
+ return {
30
+ preferred: [...new Set((Array.isArray(p.preferred) ? p.preferred : []).filter(id => typeof id === 'string' && id))],
31
+ names: typeof p.names === 'object' && p.names ? { ...p.names } : {},
32
+ maxAddrs,
33
+ thisDevice: p.thisDevice === 'always' || p.thisDevice === 'never' ? p.thisDevice : 'fallback',
34
+ allowPrivate: p.allowPrivate === true,
35
+ };
36
+ }
37
+ const SOURCE_RANK = { connection: 0, record: 1, claim: 2 };
38
+ /**
39
+ * The plan: preferred cadrons first (in the person's order), then other always-on cadrons, then
40
+ * the rest, each group newest record first; each cadron's addresses best first (reach, then
41
+ * evidence), loopback and undialable ones never; at most `maxAddrs` in all, one address per
42
+ * cadron before any cadron gets a second, so a second cadron backs up the first. This device's
43
+ * addresses go in as `prefs.thisDevice` says.
44
+ */
45
+ export function planBootstrap(input) {
46
+ const { prefs } = input;
47
+ const usable = (kind) => kind !== null && kind !== 'loopback' && (reachesAnywhere(kind) || (prefs.allowPrivate && kind === 'private'));
48
+ const preferredAt = (peerId) => {
49
+ const i = prefs.preferred.indexOf(peerId);
50
+ return i < 0 ? Infinity : i;
51
+ };
52
+ const cadrons = input.cadrons
53
+ .filter(c => c.peerId !== input.thisDevice.peerId)
54
+ .map(c => {
55
+ // One entry per address, its best evidence kept.
56
+ const best = new Map();
57
+ for (const { addr, source } of c.addrs) {
58
+ const had = best.get(addr);
59
+ if (had === undefined || SOURCE_RANK[source] < SOURCE_RANK[had])
60
+ best.set(addr, source);
61
+ }
62
+ const addrs = [...best]
63
+ .map(([addr, source]) => ({ addr, source, kind: addressKind(addr) }))
64
+ .filter((a) => usable(a.kind))
65
+ .sort((a, b) => ADDRESS_KIND_RANK[a.kind] - ADDRESS_KIND_RANK[b.kind] || SOURCE_RANK[a.source] - SOURCE_RANK[b.source]);
66
+ return { ...c, addrs, preferred: preferredAt(c.peerId) !== Infinity };
67
+ })
68
+ .filter(c => c.addrs.length > 0)
69
+ .sort((a, b) => preferredAt(a.peerId) - preferredAt(b.peerId)
70
+ || Number(b.alwaysOn) - Number(a.alwaysOn)
71
+ || (b.lastPublishedAt ?? 0) - (a.lastPublishedAt ?? 0));
72
+ const own = input.thisDevice.addrs
73
+ .map(addr => ({ addr, kind: addressKind(addr) }))
74
+ .filter((a) => usable(a.kind))
75
+ .sort((a, b) => ADDRESS_KIND_RANK[a.kind] - ADDRESS_KIND_RANK[b.kind]);
76
+ const ownEntry = (a) => ({
77
+ addr: a.addr, peerId: input.thisDevice.peerId, thisDevice: true, kind: a.kind, source: 'this-device', preferred: false, alwaysOn: false,
78
+ });
79
+ const anywhereCadron = cadrons.some(c => c.addrs.some(a => reachesAnywhere(a.kind)));
80
+ const wantOwn = prefs.thisDevice === 'always' || (prefs.thisDevice === 'fallback' && !anywhereCadron);
81
+ const cadronSlots = wantOwn && own.length > 0 && cadrons.length > 0 ? Math.max(1, prefs.maxAddrs - 1) : prefs.maxAddrs;
82
+ // Round robin: each cadron's best address, then each one's second, until the slots are filled.
83
+ const entries = [];
84
+ const others = [];
85
+ const rounds = Math.max(0, ...cadrons.map(c => c.addrs.length));
86
+ for (let round = 0; round < rounds; round++) {
87
+ for (const c of cadrons) {
88
+ const a = c.addrs[round];
89
+ if (!a)
90
+ continue;
91
+ const entry = { addr: a.addr, peerId: c.peerId, thisDevice: false, kind: a.kind, source: a.source, preferred: c.preferred, alwaysOn: c.alwaysOn };
92
+ (entries.length < cadronSlots ? entries : others).push(entry);
93
+ }
94
+ }
95
+ for (const a of own)
96
+ (wantOwn && entries.length < prefs.maxAddrs ? entries : others).push(ownEntry(a));
97
+ const warnings = [];
98
+ if (!anywhereCadron)
99
+ warnings.push('no-cadron-reachable-from-anywhere');
100
+ if (entries.some(e => e.source === 'claim'))
101
+ warnings.push('cadron-record-not-yet-published');
102
+ if (!entries.some(e => reachesAnywhere(e.kind)))
103
+ warnings.push('unreachable');
104
+ return { entries, others, warnings };
105
+ }
106
+ //# sourceMappingURL=bootstrap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bootstrap.js","sourceRoot":"","sources":["../src/bootstrap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,O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;IAEvG,MAAM,QAAQ,GAAuB,EAAE,CAAC;IACxC,IAAI,CAAC,cAAc;QAAE,QAAQ,CAAC,IAAI,CAAC,mCAAmC,CAAC,CAAC;IACxE,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,KAAK,OAAO,CAAC;QAAE,QAAQ,CAAC,IAAI,CAAC,iCAAiC,CAAC,CAAC;IAC9F,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,eAAe,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QAAE,QAAQ,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IAC9E,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC;AACtC,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { type Readable } from './store.js';
2
+ export interface NodeCodeInbox {
3
+ /** The code waiting, or null; subscribe to show the prompt when one arrives. Never log its value. */
4
+ pending: Readable<string | null>;
5
+ /** A link or text that may hold a node code: kept (replacing any waiting one) if it does. Returns whether it did. */
6
+ offer(textOrUrl: string | null | undefined): boolean;
7
+ /** The waiting code, removed; null when none. */
8
+ take(): string | null;
9
+ }
10
+ export declare function nodeCodeInbox(): NodeCodeInbox;
package/dist/inbox.js ADDED
@@ -0,0 +1,28 @@
1
+ /**
2
+ * One node code waiting to be reviewed. A code can arrive at any moment: a link opened while the
3
+ * app starts, a scan, a paste, while another claim is still running. The app hands each arrival to
4
+ * `offer`; the cadre screen takes it when it can show the approval prompt. One slot, the latest
5
+ * arrival wins: a person acts on the code they just opened. Listening for the links themselves is
6
+ * the platform's (on React Native, the app's linking handler); this only holds what arrived.
7
+ */
8
+ import { nodeCodeFromUrl } from './node-code-text.js';
9
+ import { writable } from './store.js';
10
+ export function nodeCodeInbox() {
11
+ const pending = writable(null);
12
+ return {
13
+ pending,
14
+ offer(textOrUrl) {
15
+ const code = nodeCodeFromUrl(textOrUrl);
16
+ if (!code)
17
+ return false;
18
+ pending.set(code);
19
+ return true;
20
+ },
21
+ take() {
22
+ const code = pending.get();
23
+ pending.set(null);
24
+ return code;
25
+ },
26
+ };
27
+ }
28
+ //# sourceMappingURL=inbox.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inbox.js","sourceRoot":"","sources":["../src/inbox.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAiB,MAAM,YAAY,CAAC;AAWrD,MAAM,UAAU,aAAa;IAC5B,MAAM,OAAO,GAAG,QAAQ,CAAgB,IAAI,CAAC,CAAC;IAC9C,OAAO;QACN,OAAO;QACP,KAAK,CAAC,SAAS;YACd,MAAM,IAAI,GAAG,eAAe,CAAC,SAAS,CAAC,CAAC;YACxC,IAAI,CAAC,IAAI;gBAAE,OAAO,KAAK,CAAC;YACxB,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAClB,OAAO,IAAI,CAAC;QACb,CAAC;QACD,IAAI;YACH,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;YAC3B,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAClB,OAAO,IAAI,CAAC;QACb,CAAC;KACD,CAAC;AACH,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * `@serfab/cadre-client`: the stack-free half. Types, results, node-code text, address
3
+ * judgement and stores — nothing here loads cadre-core, so a screen or a mock can import
4
+ * it freely. The implementation over a running node is `@serfab/cadre-client/sereus`.
5
+ */
6
+ export * from './result.js';
7
+ export * from './model.js';
8
+ export * from './node-code-text.js';
9
+ export * from './inbox.js';
10
+ export * from './address.js';
11
+ export * from './bootstrap.js';
12
+ export * from './invitation-text.js';
13
+ export * from './store.js';
14
+ export * from './loader.js';
15
+ export * from './stores.js';
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * `@serfab/cadre-client`: the stack-free half. Types, results, node-code text, address
3
+ * judgement and stores — nothing here loads cadre-core, so a screen or a mock can import
4
+ * it freely. The implementation over a running node is `@serfab/cadre-client/sereus`.
5
+ */
6
+ export * from './result.js';
7
+ export * from './model.js';
8
+ export * from './node-code-text.js';
9
+ export * from './inbox.js';
10
+ export * from './address.js';
11
+ export * from './bootstrap.js';
12
+ export * from './invitation-text.js';
13
+ export * from './store.js';
14
+ export * from './loader.js';
15
+ export * from './stores.js';
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,81 @@
1
+ /**
2
+ * An invitation in words: what a person sees when they ask what is in the link they are about to
3
+ * hand out (or were handed). Pure: `/sereus` decodes the invitation, this describes it.
4
+ *
5
+ * Besides the addresses it names, it can say which machine each belongs to in the words the
6
+ * person's cadre screen uses (name, status dot, whether it is theirs), and which other addresses
7
+ * of theirs it could name instead, for a screen that lets the person change one invitation's list
8
+ * (`InvitationsModel.rebootstrap`).
9
+ */
10
+ import { type CadronAddressKind } from './address.js';
11
+ import { type CadreMachine, type CadronPresence } from './model.js';
12
+ export interface InvitationAddressText {
13
+ /** The cadron's name the person gave it, else its host name, else its short peer id. */
14
+ label: string;
15
+ peerId: string;
16
+ /** This device's own address rather than one of its cadrons'. */
17
+ thisDevice: boolean;
18
+ /** Null for an address no phone can dial (plain TCP, unparsable). */
19
+ kind: CadronAddressKind | null;
20
+ reachesAnywhere: boolean;
21
+ /** The host a joiner dials: the cadron's own, or for a relayed address the relay's. */
22
+ host: string;
23
+ port: number | null;
24
+ /** `ws`, `wss`, `tcp`, or `relay` for an address reached through a relay. */
25
+ via: 'ws' | 'wss' | 'tcp' | 'relay';
26
+ addr: string;
27
+ /** The invitation names it; false for an address it could name instead (`others`). */
28
+ included: boolean;
29
+ }
30
+ /** A machine an invitation names, or could: the addresses grouped by whose they are. */
31
+ export interface InvitationCadronText {
32
+ peerId: string;
33
+ /** As on each address: the person's name for it, else its host name, else its short peer id. */
34
+ label: string;
35
+ thisDevice: boolean;
36
+ /**
37
+ * One of the person's own cadrons (on their cadron list and not untrusted), or this device.
38
+ * False: a machine that is not theirs, or one since removed. Null when the list was not given.
39
+ */
40
+ yours: boolean | null;
41
+ /** As on the cadre screen (`cadronPresence`); null when the machine is not on the list. */
42
+ presence: CadronPresence | null;
43
+ alwaysOn: boolean | null;
44
+ /** The ones the invitation names first, in its order, then the ones it could. */
45
+ addresses: InvitationAddressText[];
46
+ }
47
+ export interface InvitationText {
48
+ expiresAt: string;
49
+ /** Length of the text shared (the link), in characters. */
50
+ linkLength: number;
51
+ /** The smallest QR version (1–40) holding the link at error correction M, or null past 40. */
52
+ qrVersion: number | null;
53
+ /** The addresses it names, in its order. */
54
+ addresses: InvitationAddressText[];
55
+ /** The same, and the `others`, grouped by machine: the ones it names first, in its order. */
56
+ cadrons: InvitationCadronText[];
57
+ /** The person's cap on addresses per invitation, when known. */
58
+ maxAddrs: number | null;
59
+ /** Usable addresses of the person's cadrons (not this device) it leaves out. 0: it names all there are. */
60
+ cadronAddrsLeftOut: number;
61
+ }
62
+ export interface DescribeOptions {
63
+ /** The text actually shared (the link), for its length. */
64
+ link: string;
65
+ /** The person's own names for their cadrons. */
66
+ names?: Record<string, string>;
67
+ thisDevicePeerId?: string | null;
68
+ /** The person's cadron list (`CadreStatus.machines`): whose each address is, and how each is. */
69
+ machines?: readonly CadreMachine[];
70
+ /** Other usable addresses it could name (`BootstrapPlan` entries and others), best first. */
71
+ others?: readonly string[];
72
+ maxAddrs?: number | null;
73
+ now?: number;
74
+ }
75
+ export declare function qrVersionFor(length: number): number | null;
76
+ /** The peer an address ends in (its last `/p2p/`), or '' when it names none or does not parse. */
77
+ export declare function destinationPeerId(addr: string): string;
78
+ export declare function describeInvitation(invitation: {
79
+ expiration: Date | string;
80
+ bootstrap: readonly string[];
81
+ }, opts: DescribeOptions): InvitationText;
@@ -0,0 +1,80 @@
1
+ /**
2
+ * An invitation in words: what a person sees when they ask what is in the link they are about to
3
+ * hand out (or were handed). Pure: `/sereus` decodes the invitation, this describes it.
4
+ *
5
+ * Besides the addresses it names, it can say which machine each belongs to in the words the
6
+ * person's cadre screen uses (name, status dot, whether it is theirs), and which other addresses
7
+ * of theirs it could name instead, for a screen that lets the person change one invitation's list
8
+ * (`InvitationsModel.rebootstrap`).
9
+ */
10
+ import { addressComponents, addressKind, reachesAnywhere, suggestedName } from './address.js';
11
+ import { cadronPresence, shortPeerId } from './model.js';
12
+ /** QR byte-mode capacity at error correction level M, versions 1–40. */
13
+ const QR_CAPACITY_M = [14, 26, 42, 62, 84, 106, 122, 152, 180, 213, 251, 287, 331, 362, 412, 450, 504, 560, 624, 666, 711, 779,
14
+ 857, 911, 997, 1059, 1125, 1190, 1264, 1370, 1452, 1538, 1628, 1722, 1809, 1911, 1989, 2099, 2213, 2331];
15
+ export function qrVersionFor(length) {
16
+ const i = QR_CAPACITY_M.findIndex(cap => cap >= length);
17
+ return i < 0 ? null : i + 1;
18
+ }
19
+ /** The peer an address ends in (its last `/p2p/`), or '' when it names none or does not parse. */
20
+ export function destinationPeerId(addr) {
21
+ const peers = (addressComponents(addr) ?? []).filter(c => c.name === 'p2p');
22
+ return peers[peers.length - 1]?.value ?? '';
23
+ }
24
+ function hostAndPort(addr) {
25
+ const components = addressComponents(addr) ?? [];
26
+ const relayed = components.some(c => c.name === 'p2p-circuit');
27
+ const tcp = components.find(c => c.name === 'tcp');
28
+ const port = tcp?.value ? Number(tcp.value) : NaN;
29
+ const names = new Set(components.map(c => c.name));
30
+ const via = relayed ? 'relay' : names.has('wss') || names.has('tls') ? 'wss' : names.has('ws') ? 'ws' : 'tcp';
31
+ return { host: components[0]?.value ?? '', port: Number.isFinite(port) ? port : null, via };
32
+ }
33
+ export function describeInvitation(invitation, opts) {
34
+ const names = opts.names ?? {};
35
+ const machines = new Map((opts.machines ?? []).map(m => [m.peerId, m]));
36
+ const included = new Set(invitation.bootstrap);
37
+ const others = (opts.others ?? []).filter(addr => !included.has(addr));
38
+ const all = [...invitation.bootstrap, ...others];
39
+ // A machine's host name: from its list entry, else from the addresses here.
40
+ const label = (peerId) => names[peerId]?.trim()
41
+ || suggestedName(machines.get(peerId) ?? { addresses: all.filter(a => destinationPeerId(a) === peerId) })
42
+ || shortPeerId(peerId);
43
+ const isThisDevice = (peerId) => !!opts.thisDevicePeerId && peerId === opts.thisDevicePeerId;
44
+ const text = (addr, inInvitation) => {
45
+ const peerId = destinationPeerId(addr);
46
+ const kind = addressKind(addr);
47
+ return {
48
+ label: label(peerId), peerId, thisDevice: isThisDevice(peerId), kind, reachesAnywhere: reachesAnywhere(kind),
49
+ ...hostAndPort(addr), addr, included: inInvitation,
50
+ };
51
+ };
52
+ const addresses = invitation.bootstrap.map(addr => text(addr, true));
53
+ const extra = others.map(addr => text(addr, false));
54
+ const cadrons = new Map();
55
+ for (const a of [...addresses, ...extra]) {
56
+ let c = cadrons.get(a.peerId);
57
+ if (!c) {
58
+ const m = machines.get(a.peerId);
59
+ c = {
60
+ peerId: a.peerId, label: a.label, thisDevice: a.thisDevice,
61
+ yours: a.thisDevice ? true : opts.machines ? !!m && m.trusted !== false : null,
62
+ presence: m ? cadronPresence(m, opts.now) : null,
63
+ alwaysOn: m?.alwaysOn ?? null,
64
+ addresses: [],
65
+ };
66
+ cadrons.set(a.peerId, c);
67
+ }
68
+ c.addresses.push(a);
69
+ }
70
+ return {
71
+ expiresAt: new Date(invitation.expiration).toISOString(),
72
+ linkLength: opts.link.length,
73
+ qrVersion: qrVersionFor(opts.link.length),
74
+ addresses,
75
+ cadrons: [...cadrons.values()],
76
+ maxAddrs: opts.maxAddrs ?? null,
77
+ cadronAddrsLeftOut: extra.filter(a => !a.thisDevice).length,
78
+ };
79
+ }
80
+ //# sourceMappingURL=invitation-text.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"invitation-text.js","sourceRoot":"","sources":["../src/invitation-text.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,eAAe,EAAE,aAAa,EAA0B,MAAM,cAAc,CAAC;AACtH,OAAO,EAAE,cAAc,EAAE,WAAW,EAA0C,MAAM,YAAY,CAAC;AAqEjG,wEAAwE;AACxE,MAAM,aAAa,GAAG,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG;IAC7H,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AAE1G,MAAM,UAAU,YAAY,CAAC,MAAc;IAC1C,MAAM,CAAC,GAAG,aAAa,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,MAAM,CAAC,CAAC;IACxD,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAC7B,CAAC;AA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