@serfab/cadre-core 1.19.0 → 1.20.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 (131) hide show
  1. package/dist/cadre-node.d.ts +118 -16
  2. package/dist/cadre-node.js +210 -31
  3. package/dist/cadre-node.js.map +1 -1
  4. package/dist/control-database.d.ts +2 -1
  5. package/dist/control-database.js +128 -126
  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-read-retry.d.ts +19 -16
  10. package/dist/control-read-retry.js +22 -54
  11. package/dist/control-read-retry.js.map +1 -1
  12. package/dist/control-retry.d.ts +50 -6
  13. package/dist/control-retry.js +53 -6
  14. package/dist/control-retry.js.map +1 -1
  15. package/dist/control-stream.js +2 -1
  16. package/dist/control-stream.js.map +1 -1
  17. package/dist/control-write-retry.d.ts +24 -16
  18. package/dist/control-write-retry.js +58 -30
  19. package/dist/control-write-retry.js.map +1 -1
  20. package/dist/describe-thrown.d.ts +12 -0
  21. package/dist/describe-thrown.js +59 -0
  22. package/dist/describe-thrown.js.map +1 -0
  23. package/dist/edge-node/config.d.ts +8 -2
  24. package/dist/edge-node/config.js +5 -1
  25. package/dist/edge-node/config.js.map +1 -1
  26. package/dist/edge-node/dissolve.d.ts +1 -1
  27. package/dist/edge-node/dissolve.js +1 -1
  28. package/dist/edge-node/dissolve.js.map +1 -1
  29. package/dist/edge-node/edge-join.js +4 -1
  30. package/dist/edge-node/edge-join.js.map +1 -1
  31. package/dist/edge-node/edge-node.d.ts +10 -0
  32. package/dist/edge-node/edge-node.js +54 -9
  33. package/dist/edge-node/edge-node.js.map +1 -1
  34. package/dist/edge-node/options.d.ts +11 -2
  35. package/dist/edge-node/options.js +11 -4
  36. package/dist/edge-node/options.js.map +1 -1
  37. package/dist/edge-node/platform.d.ts +15 -3
  38. package/dist/edge-node/strand-move.d.ts +0 -2
  39. package/dist/edge-node/strand-move.js +1 -4
  40. package/dist/edge-node/strand-move.js.map +1 -1
  41. package/dist/edge-node/strands.js +5 -3
  42. package/dist/edge-node/strands.js.map +1 -1
  43. package/dist/enrollment.js +2 -1
  44. package/dist/enrollment.js.map +1 -1
  45. package/dist/index.d.ts +6 -2
  46. package/dist/index.js +9 -1
  47. package/dist/index.js.map +1 -1
  48. package/dist/member-redemption.js +3 -9
  49. package/dist/member-redemption.js.map +1 -1
  50. package/dist/node-local-snapshot.js +2 -1
  51. package/dist/node-local-snapshot.js.map +1 -1
  52. package/dist/peer-dial.js +3 -2
  53. package/dist/peer-dial.js.map +1 -1
  54. package/dist/pending-join-runner.d.ts +29 -2
  55. package/dist/pending-join-runner.js +60 -20
  56. package/dist/pending-join-runner.js.map +1 -1
  57. package/dist/push-notifier-apns.js +8 -7
  58. package/dist/push-notifier-apns.js.map +1 -1
  59. package/dist/push-notifier-fcm.js +6 -5
  60. package/dist/push-notifier-fcm.js.map +1 -1
  61. package/dist/push-notifier-shared.d.ts +0 -2
  62. package/dist/push-notifier-shared.js +4 -4
  63. package/dist/push-notifier-shared.js.map +1 -1
  64. package/dist/relay-reservation.js +4 -3
  65. package/dist/relay-reservation.js.map +1 -1
  66. package/dist/strand-addr-protocol.js +2 -1
  67. package/dist/strand-addr-protocol.js.map +1 -1
  68. package/dist/strand-app-schema-report.d.ts +53 -0
  69. package/dist/strand-app-schema-report.js +77 -0
  70. package/dist/strand-app-schema-report.js.map +1 -0
  71. package/dist/strand-database.d.ts +60 -0
  72. package/dist/strand-database.js +93 -0
  73. package/dist/strand-database.js.map +1 -1
  74. package/dist/strand-first-sync-gate.js +4 -3
  75. package/dist/strand-first-sync-gate.js.map +1 -1
  76. package/dist/strand-formation-protocol.js +4 -3
  77. package/dist/strand-formation-protocol.js.map +1 -1
  78. package/dist/strand-instance-manager.d.ts +73 -2
  79. package/dist/strand-instance-manager.js +112 -9
  80. package/dist/strand-instance-manager.js.map +1 -1
  81. package/dist/strand-membership-reconciler.d.ts +13 -1
  82. package/dist/strand-membership-reconciler.js +25 -16
  83. package/dist/strand-membership-reconciler.js.map +1 -1
  84. package/dist/strand-membership-writer.d.ts +16 -2
  85. package/dist/strand-membership-writer.js +54 -8
  86. package/dist/strand-membership-writer.js.map +1 -1
  87. package/dist/strand-move.js +4 -6
  88. package/dist/strand-move.js.map +1 -1
  89. package/dist/strand-offline.d.ts +102 -0
  90. package/dist/strand-offline.js +97 -0
  91. package/dist/strand-offline.js.map +1 -0
  92. package/dist/types.d.ts +37 -0
  93. package/dist/types.js.map +1 -1
  94. package/package.json +8 -8
  95. package/src/cadre-node.ts +230 -31
  96. package/src/control-database.ts +5594 -5592
  97. package/src/control-protocol-guard.ts +3 -2
  98. package/src/control-read-retry.ts +22 -58
  99. package/src/control-retry.ts +54 -6
  100. package/src/control-stream.ts +2 -1
  101. package/src/control-write-retry.ts +58 -30
  102. package/src/describe-thrown.ts +60 -0
  103. package/src/edge-node/config.ts +11 -2
  104. package/src/edge-node/dissolve.ts +2 -1
  105. package/src/edge-node/edge-join.ts +2 -1
  106. package/src/edge-node/edge-node.ts +57 -7
  107. package/src/edge-node/options.ts +20 -4
  108. package/src/edge-node/platform.ts +15 -3
  109. package/src/edge-node/strand-move.ts +1 -5
  110. package/src/edge-node/strands.ts +74 -72
  111. package/src/enrollment.ts +2 -1
  112. package/src/index.ts +27 -1
  113. package/src/member-redemption.ts +3 -9
  114. package/src/node-local-snapshot.ts +2 -1
  115. package/src/peer-dial.ts +605 -604
  116. package/src/pending-join-runner.ts +68 -22
  117. package/src/push-notifier-apns.ts +8 -7
  118. package/src/push-notifier-fcm.ts +6 -5
  119. package/src/push-notifier-shared.ts +5 -5
  120. package/src/relay-reservation.ts +1212 -1211
  121. package/src/strand-addr-protocol.ts +2 -1
  122. package/src/strand-app-schema-report.ts +115 -0
  123. package/src/strand-database.ts +123 -1
  124. package/src/strand-first-sync-gate.ts +600 -599
  125. package/src/strand-formation-protocol.ts +4 -3
  126. package/src/strand-instance-manager.ts +137 -10
  127. package/src/strand-membership-reconciler.ts +942 -928
  128. package/src/strand-membership-writer.ts +61 -14
  129. package/src/strand-move.ts +594 -597
  130. package/src/strand-offline.ts +178 -0
  131. package/src/types.ts +36 -0
@@ -3,6 +3,7 @@ import { toString as uint8ArrayToString } from 'uint8arrays';
3
3
  import { TransactionActiveError } from '@quereus/quereus';
4
4
  import type { Database, SqlValue } from '@quereus/quereus';
5
5
  import { digest, sign, verify, generatePrivateKey, getPublicKey, randomBytes } from '@optimystic/quereus-plugin-crypto';
6
+ import { offlineControllerOf } from '@serfab/quereus-plugin-sereus';
6
7
  import type { SAppConfig } from './types.js';
7
8
  import type { Ed25519KeyPair } from './ed25519-key.js';
8
9
  import { canonicalDatetime } from './canonical-datetime.js';
@@ -146,6 +147,31 @@ export class StrandTransactionBusyError extends Error {
146
147
  }
147
148
  }
148
149
 
150
+ /**
151
+ * A membership write refused because the strand's offline queue holds transactions its members
152
+ * have not taken yet (docs/strands.md → "Offline mode"). Every `Strand` table is network-acknowledged, and a
153
+ * network-acknowledged commit made while transactions are queued waits for them to settle while
154
+ * holding Quereus's execution lock, so every other statement on the strand database, the app's
155
+ * included, would wait with it for up to the plugin's `networkCommitTimeoutMs`. Nothing was
156
+ * written. Raised whether or not the write would have joined an open transaction: joining would
157
+ * make the app's own commit wait the same way.
158
+ */
159
+ export class StrandOfflineQueueBusyError extends Error {
160
+ /** The queued transactions the write would have waited behind, in queue order. */
161
+ readonly transactionIds: readonly string[];
162
+
163
+ constructor(transactionIds: readonly string[]) {
164
+ super(
165
+ `The strand's offline queue holds ${transactionIds.length} transaction(s) its members have not taken yet `
166
+ + `(${transactionIds.join(', ')}). A membership write is acknowledged by the strand's members and would `
167
+ + 'wait behind them, holding the strand database, so nothing was written; await '
168
+ + 'strand.database.offline.whenDrained() and retry.',
169
+ );
170
+ this.name = 'StrandOfflineQueueBusyError';
171
+ this.transactionIds = transactionIds;
172
+ }
173
+ }
174
+
149
175
  /** Options every transactional membership writer accepts. */
150
176
  export interface StrandWriteOptions {
151
177
  /**
@@ -207,6 +233,8 @@ function combineStatements(...fragments: StrandStatements[]): StrandStatements {
207
233
  * autocommit statement is merely in flight, which would make a background writer refuse
208
234
  * spuriously and a composing caller run its statements as separate autocommits.
209
235
  *
236
+ * - The strand's offline queue holds transactions: {@link StrandOfflineQueueBusyError}, before
237
+ * anything runs, every writer and every option alike (see {@link refuseBehindOfflineQueue}).
210
238
  * - No transaction was open: the batch commits or fails as a unit, leaving nothing open.
211
239
  * - One was already open, so the batch refuses with `TransactionActiveError` having run
212
240
  * nothing. With `joinOpenTransaction: false` that becomes a
@@ -219,6 +247,7 @@ async function execStrandTransaction(
219
247
  statements: StrandStatements,
220
248
  options?: StrandWriteOptions,
221
249
  ): Promise<void> {
250
+ refuseBehindOfflineQueue(db);
222
251
  try {
223
252
  await db.exec(statements.sql, statements.params, { transaction: true });
224
253
  return;
@@ -244,6 +273,24 @@ async function execStrandTransaction(
244
273
  await db.exec(statements.sql, statements.params);
245
274
  }
246
275
 
276
+ /**
277
+ * Throw {@link StrandOfflineQueueBusyError} while the strand's offline queue lists any
278
+ * transaction. `entries()` also lists the journaled ones a restore has not put back yet, and a
279
+ * network-acknowledged commit waits behind those too.
280
+ *
281
+ * NOTE: racy by nature: an app commit queued between this check and the batch's `exec` makes the
282
+ * membership commit wait behind it, for up to `networkCommitTimeoutMs`, holding the execution
283
+ * lock. That is upstream's bounded wait in the rare case. If a frozen strand database is ever
284
+ * traced to a membership write that slipped past this check, hold the app's writes during
285
+ * membership commits instead.
286
+ */
287
+ function refuseBehindOfflineQueue(db: Database): void {
288
+ const queued = offlineControllerOf(db)?.entries() ?? [];
289
+ if (queued.length > 0) {
290
+ throw new StrandOfflineQueueBusyError(queued.map((entry) => entry.transactionId));
291
+ }
292
+ }
293
+
247
294
  /**
248
295
  * The `Strand.Revocation` tombstone insert that retires a deleted row's StampId.
249
296
  *
@@ -736,12 +783,12 @@ export async function issueInvite(db: Database, params: IssueInviteParams): Prom
736
783
  const managerSignature = signStrandPayload(payload, managerKeyPair.privateKeyB64);
737
784
  const inviteSignature = signStrandPayload(payload, invitePrivateKey);
738
785
 
739
- await db.exec(
740
- `insert into Strand.Invite (Key, Expiration)
741
- with context ManagerKey = ?, ManagerSignature = ?, InviteSignature = ?
742
- values (?, ?)`,
743
- [managerKeyPair.publicKeyB64, managerSignature, inviteSignature, inviteKey, expirationColumn],
744
- );
786
+ await execStrandTransaction(db, {
787
+ sql: `insert into Strand.Invite (Key, Expiration)
788
+ with context ManagerKey = :managerKey, ManagerSignature = :managerSignature, InviteSignature = :inviteSignature
789
+ values (:inviteKey, :expiration);`,
790
+ params: { managerKey: managerKeyPair.publicKeyB64, managerSignature, inviteSignature, inviteKey, expiration: expirationColumn },
791
+ });
745
792
  log('Issued invite %s (expires=%s)', inviteKey, expirationColumn ?? 'never');
746
793
 
747
794
  return { inviteKey, invitePrivateKey };
@@ -937,8 +984,8 @@ export interface CancelInviteParams {
937
984
  * `digest('Strand.CancelledInvite', 'cancel', InviteKey)`. The digest binds the exact
938
985
  * invite key, so an approval minted for one invitation cannot cancel another.
939
986
  *
940
- * One row, one statement — no transaction needed. `CancelledInvite.Authorized` is a
941
- * deferred (subquery-bearing) check, so it fires at this statement's own auto-commit.
987
+ * One row, one statement. `CancelledInvite.Authorized` is a deferred (subquery-bearing)
988
+ * check, so it fires at the batch's commit, or at the caller's when it joins an open one.
942
989
  *
943
990
  * Insert-if-absent, matching {@link registerMemberPeer}'s and {@link revokeMember}'s
944
991
  * restart-safe shape: a repeat cancellation logs and returns rather than throwing on the
@@ -973,12 +1020,12 @@ export async function cancelInvite(db: Database, params: CancelInviteParams): Pr
973
1020
  ['Strand.CancelledInvite', 'cancel', inviteKey],
974
1021
  managerKeyPair.privateKeyB64,
975
1022
  );
976
- await db.exec(
977
- `insert into Strand.CancelledInvite (InviteKey)
978
- with context ManagerKey = ?, ManagerSignature = ?
979
- values (?)`,
980
- [managerKeyPair.publicKeyB64, signature, inviteKey],
981
- );
1023
+ await execStrandTransaction(db, {
1024
+ sql: `insert into Strand.CancelledInvite (InviteKey)
1025
+ with context ManagerKey = :managerKey, ManagerSignature = :managerSignature
1026
+ values (:inviteKey);`,
1027
+ params: { managerKey: managerKeyPair.publicKeyB64, managerSignature: signature, inviteKey },
1028
+ });
982
1029
  log('Cancelled invite %s by manager %s', inviteKey, managerKeyPair.publicKeyB64);
983
1030
  }
984
1031