@serfab/cadre-core 1.19.1 → 1.21.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 (103) hide show
  1. package/dist/cadre-node.d.ts +53 -14
  2. package/dist/cadre-node.js +97 -30
  3. package/dist/cadre-node.js.map +1 -1
  4. package/dist/control-database.d.ts +2 -1
  5. package/dist/control-database.js +146 -135
  6. package/dist/control-database.js.map +1 -1
  7. package/dist/control-protocol-guard.js +3 -2
  8. package/dist/control-protocol-guard.js.map +1 -1
  9. package/dist/control-stream.js +2 -1
  10. package/dist/control-stream.js.map +1 -1
  11. package/dist/describe-thrown.d.ts +12 -0
  12. package/dist/describe-thrown.js +59 -0
  13. package/dist/describe-thrown.js.map +1 -0
  14. package/dist/edge-node/dissolve.d.ts +1 -1
  15. package/dist/edge-node/dissolve.js +1 -1
  16. package/dist/edge-node/dissolve.js.map +1 -1
  17. package/dist/edge-node/edge-join.js +2 -1
  18. package/dist/edge-node/edge-join.js.map +1 -1
  19. package/dist/edge-node/edge-node.js +2 -1
  20. package/dist/edge-node/edge-node.js.map +1 -1
  21. package/dist/edge-node/strand-move.d.ts +0 -2
  22. package/dist/edge-node/strand-move.js +1 -4
  23. package/dist/edge-node/strand-move.js.map +1 -1
  24. package/dist/edge-node/strands.js +2 -1
  25. package/dist/edge-node/strands.js.map +1 -1
  26. package/dist/enrollment.js +2 -1
  27. package/dist/enrollment.js.map +1 -1
  28. package/dist/index.d.ts +3 -1
  29. package/dist/index.js +3 -0
  30. package/dist/index.js.map +1 -1
  31. package/dist/member-redemption.js +3 -9
  32. package/dist/member-redemption.js.map +1 -1
  33. package/dist/node-local-snapshot.js +2 -1
  34. package/dist/node-local-snapshot.js.map +1 -1
  35. package/dist/peer-dial.js +3 -2
  36. package/dist/peer-dial.js.map +1 -1
  37. package/dist/pending-join-runner.d.ts +29 -2
  38. package/dist/pending-join-runner.js +60 -20
  39. package/dist/pending-join-runner.js.map +1 -1
  40. package/dist/push-notifier-apns.js +8 -7
  41. package/dist/push-notifier-apns.js.map +1 -1
  42. package/dist/push-notifier-fcm.js +6 -5
  43. package/dist/push-notifier-fcm.js.map +1 -1
  44. package/dist/push-notifier-shared.d.ts +0 -2
  45. package/dist/push-notifier-shared.js +4 -4
  46. package/dist/push-notifier-shared.js.map +1 -1
  47. package/dist/relay-reservation.js +24 -4
  48. package/dist/relay-reservation.js.map +1 -1
  49. package/dist/strand-addr-protocol.js +2 -1
  50. package/dist/strand-addr-protocol.js.map +1 -1
  51. package/dist/strand-app-schema-report.d.ts +56 -0
  52. package/dist/strand-app-schema-report.js +83 -0
  53. package/dist/strand-app-schema-report.js.map +1 -0
  54. package/dist/strand-database.d.ts +9 -5
  55. package/dist/strand-database.js +12 -6
  56. package/dist/strand-database.js.map +1 -1
  57. package/dist/strand-first-sync-gate.js +4 -3
  58. package/dist/strand-first-sync-gate.js.map +1 -1
  59. package/dist/strand-formation-protocol.js +4 -3
  60. package/dist/strand-formation-protocol.js.map +1 -1
  61. package/dist/strand-instance-manager.d.ts +8 -2
  62. package/dist/strand-instance-manager.js +8 -6
  63. package/dist/strand-instance-manager.js.map +1 -1
  64. package/dist/strand-membership-reconciler.js +9 -11
  65. package/dist/strand-membership-reconciler.js.map +1 -1
  66. package/dist/strand-move.js +4 -6
  67. package/dist/strand-move.js.map +1 -1
  68. package/dist/strand-offline.d.ts +10 -39
  69. package/dist/strand-offline.js +9 -54
  70. package/dist/strand-offline.js.map +1 -1
  71. package/dist/types.d.ts +8 -5
  72. package/dist/types.js.map +1 -1
  73. package/package.json +8 -8
  74. package/src/cadre-node.ts +11528 -11456
  75. package/src/control-database.ts +5603 -5592
  76. package/src/control-protocol-guard.ts +3 -2
  77. package/src/control-stream.ts +2 -1
  78. package/src/describe-thrown.ts +60 -0
  79. package/src/edge-node/dissolve.ts +2 -1
  80. package/src/edge-node/edge-join.ts +1 -1
  81. package/src/edge-node/edge-node.ts +2 -1
  82. package/src/edge-node/strand-move.ts +1 -5
  83. package/src/edge-node/strands.ts +74 -73
  84. package/src/enrollment.ts +2 -1
  85. package/src/index.ts +924 -917
  86. package/src/member-redemption.ts +3 -9
  87. package/src/node-local-snapshot.ts +2 -1
  88. package/src/peer-dial.ts +605 -604
  89. package/src/pending-join-runner.ts +68 -22
  90. package/src/push-notifier-apns.ts +8 -7
  91. package/src/push-notifier-fcm.ts +6 -5
  92. package/src/push-notifier-shared.ts +5 -5
  93. package/src/relay-reservation.ts +1239 -1211
  94. package/src/strand-addr-protocol.ts +2 -1
  95. package/src/strand-app-schema-report.ts +122 -0
  96. package/src/strand-database.ts +474 -466
  97. package/src/strand-first-sync-gate.ts +600 -599
  98. package/src/strand-formation-protocol.ts +4 -3
  99. package/src/strand-instance-manager.ts +2219 -2210
  100. package/src/strand-membership-reconciler.ts +942 -945
  101. package/src/strand-move.ts +594 -597
  102. package/src/strand-offline.ts +11 -80
  103. package/src/types.ts +2963 -2960
@@ -37,6 +37,7 @@ import { decodeLengthPrefixedFrame } from './seed-bootstrap.js';
37
37
  import { orderSignalingFirst } from './peer-record.js';
38
38
  import { type ControlStream, withDeadline, exchangeFrame, readStreamToEnd, replyAndClose } from './control-stream.js';
39
39
  import { relayedRequestBudgetMs } from './link-budget.js';
40
+ import { toError } from './describe-thrown.js';
40
41
 
41
42
  const log = debug('sereus:cadre:strand-addr');
42
43
 
@@ -417,7 +418,7 @@ async function dialOneSibling(
417
418
  );
418
419
  return siblingAnswer(response);
419
420
  } catch (err) {
420
- const error = err instanceof Error ? err : new Error(String(err));
421
+ const error = toError(err);
421
422
  // Matched by name, as libp2p's own error classes are compared across package copies.
422
423
  if (error.name === 'UnsupportedProtocolError') {
423
424
  log('Strand-addr refused by %s via %s: it does not serve %s', peer.peerId, describeTarget(target), protocolId);
@@ -0,0 +1,122 @@
1
+ import debug from 'debug';
2
+ import { toError } from './describe-thrown.js';
3
+ import type { AppSchemaOutcome, AppSchemaWait, SereusPluginResult } from '@serfab/quereus-plugin-sereus';
4
+
5
+ const log = debug('sereus:cadre:strand-app-schema');
6
+
7
+ /**
8
+ * `StrandDatabase.appSchemaState()`: whether the database runs the `App` schema its launch was
9
+ * given. `'waiting-for-queue'` while queued offline transactions refuse the apply (the database runs
10
+ * the earlier schema); `'waiting-for-cohort'` while the strand's members cannot be reached; `'failed'`
11
+ * when a later retry failed for another reason. docs/strands.md → "An app schema upgrade waits for the
12
+ * queue or the cohort" says what the database runs in the cohort case, and when `'applied'` is not yet true.
13
+ */
14
+ export type StrandAppSchemaState = 'applied' | 'waiting-for-queue' | 'waiting-for-cohort' | 'failed';
15
+
16
+ /** `strand:app-schema-deferred`: why the `App` schema apply was held back. */
17
+ export interface StrandAppSchemaDeferred {
18
+ readonly strandId: string;
19
+ /** `'queue'`: queued offline transactions refused it. `'cohort'`: the strand's members could not be reached. */
20
+ readonly waitingFor: AppSchemaWait;
21
+ /** The queued transactions that refused it; empty when {@link waitingFor} is `'cohort'`. */
22
+ readonly transactionIds: readonly string[];
23
+ }
24
+
25
+ /**
26
+ * Where a strand database reports its `App` schema apply. Retained with the launch config and handed
27
+ * to every database built for the launch, so the reports outlive hibernation and every other rebuild.
28
+ */
29
+ export interface StrandAppSchemaListeners {
30
+ onAppSchemaDeferred?: (event: StrandAppSchemaDeferred) => void;
31
+ onAppSchemaApplied?: (event: { strandId: string }) => void;
32
+ onAppSchemaFailed?: (event: { strandId: string; error: Error }) => void;
33
+ }
34
+
35
+ const WAITING_STATE: Record<AppSchemaWait, StrandAppSchemaState> = {
36
+ queue: 'waiting-for-queue',
37
+ cohort: 'waiting-for-cohort'
38
+ };
39
+
40
+ /**
41
+ * What one strand database reports about its `App` schema apply. Built once `connectToStrand` has
42
+ * returned and released first when the database closes, so nothing from a closing connection reaches
43
+ * the app. Events begin at publication, when the app gets the database: a retry that landed before
44
+ * then is not announced, and one that failed before then is announced at publication.
45
+ */
46
+ export class StrandAppSchemaReport {
47
+ private state: StrandAppSchemaState;
48
+ private error: Error | undefined;
49
+ private wasPublished = false;
50
+ private released = false;
51
+
52
+ constructor(
53
+ private readonly strandId: string,
54
+ private readonly listeners: StrandAppSchemaListeners,
55
+ private readonly result: Pick<SereusPluginResult, 'appSchema' | 'appSchemaWaitingFor' | 'appSchemaDeferredBy'>
56
+ ) {
57
+ // `appSchema` is absent without an `App` schema (a storage replica), which has nothing to defer.
58
+ const waitingFor = result.appSchemaWaitingFor;
59
+ this.state = waitingFor ? WAITING_STATE[waitingFor] : 'applied';
60
+ if (waitingFor) {
61
+ void result.appSchema?.then((outcome) => this.settle(outcome)).catch((error: unknown) => {
62
+ log('strand %s: reporting the deferred app schema apply failed: %O', strandId, error);
63
+ });
64
+ }
65
+ }
66
+
67
+ appSchemaState(): StrandAppSchemaState {
68
+ return this.state;
69
+ }
70
+
71
+ /** The database was handed to the app: report an apply still waiting or already failed. */
72
+ published(): void {
73
+ if (this.released || this.wasPublished) {
74
+ return;
75
+ }
76
+ this.wasPublished = true;
77
+ const waitingFor = this.result.appSchemaWaitingFor;
78
+ if (waitingFor && this.state === WAITING_STATE[waitingFor]) {
79
+ const event = { strandId: this.strandId, waitingFor, transactionIds: this.result.appSchemaDeferredBy ?? [] };
80
+ this.forward('deferred', () => this.listeners.onAppSchemaDeferred?.(event));
81
+ } else if (this.error) {
82
+ this.reportFailed(this.error);
83
+ }
84
+ }
85
+
86
+ release(): void {
87
+ this.released = true;
88
+ }
89
+
90
+ /** A cancelled retry (the database closed first) reports nothing. */
91
+ private settle(outcome: AppSchemaOutcome): void {
92
+ if (this.released || outcome.state === 'cancelled') {
93
+ return;
94
+ }
95
+ if (outcome.state === 'applied') {
96
+ this.state = 'applied';
97
+ if (this.wasPublished) {
98
+ this.forward('applied', () => this.listeners.onAppSchemaApplied?.({ strandId: this.strandId }));
99
+ }
100
+ return;
101
+ }
102
+ this.state = 'failed';
103
+ this.error = toError(outcome.error);
104
+ log('strand %s: the deferred app schema apply failed; the database keeps the earlier schema: %O', this.strandId, outcome.error);
105
+ if (this.wasPublished) {
106
+ this.reportFailed(this.error);
107
+ }
108
+ }
109
+
110
+ private reportFailed(error: Error): void {
111
+ this.forward('failed', () => this.listeners.onAppSchemaFailed?.({ strandId: this.strandId, error }));
112
+ }
113
+
114
+ /** A listener's throw is logged, so it cannot stop {@link published}'s caller from finishing publication. */
115
+ private forward(what: string, deliver: () => void): void {
116
+ try {
117
+ deliver();
118
+ } catch (error) {
119
+ log('strand %s: an app schema %s listener threw: %O', this.strandId, what, error);
120
+ }
121
+ }
122
+ }