@terreno/api 0.31.0 → 56.0.0-beta.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 (250) hide show
  1. package/CHANGELOG.md +122 -2
  2. package/LICENSE +202 -21
  3. package/dist/api.d.ts +12 -3
  4. package/dist/api.js +110 -361
  5. package/dist/auth.d.ts +26 -0
  6. package/dist/auth.js +100 -1
  7. package/dist/betterAuth.d.ts +5 -0
  8. package/dist/betterAuthSetup.d.ts +1 -15
  9. package/dist/betterAuthSetup.js +29 -8
  10. package/dist/configurationPlugin.js +2 -2
  11. package/dist/docLoader.js +12 -9
  12. package/dist/errors.d.ts +13 -0
  13. package/dist/errors.js +29 -1
  14. package/dist/expressServer.js +5 -11
  15. package/dist/githubAuth.js +4 -1
  16. package/dist/index.d.ts +17 -1
  17. package/dist/index.js +29 -1
  18. package/dist/middleware.d.ts +7 -0
  19. package/dist/middleware.js +16 -1
  20. package/dist/openApiCompat.d.ts +10 -1
  21. package/dist/openApiCompat.js +115 -47
  22. package/dist/permissions.d.ts +27 -0
  23. package/dist/permissions.js +42 -1
  24. package/dist/plugins.d.ts +15 -3
  25. package/dist/plugins.js +36 -6
  26. package/dist/realtime/changeStreamWatcher.d.ts +100 -3
  27. package/dist/realtime/changeStreamWatcher.js +542 -137
  28. package/dist/realtime/realtimeApp.d.ts +3 -1
  29. package/dist/realtime/realtimeApp.js +75 -27
  30. package/dist/realtime/sessionRevalidation.d.ts +90 -0
  31. package/dist/realtime/sessionRevalidation.js +438 -0
  32. package/dist/realtime/socketAuth.d.ts +97 -0
  33. package/dist/realtime/socketAuth.js +272 -0
  34. package/dist/realtime/socketUser.d.ts +54 -0
  35. package/dist/realtime/socketUser.js +75 -5
  36. package/dist/realtime/types.d.ts +40 -0
  37. package/dist/secretProviders.d.ts +23 -0
  38. package/dist/secretProviders.js +9 -3
  39. package/dist/sync/executors.d.ts +198 -0
  40. package/dist/sync/executors.js +670 -0
  41. package/dist/sync/loadHarness.js +807 -0
  42. package/dist/sync/models.d.ts +216 -0
  43. package/dist/sync/models.js +644 -0
  44. package/dist/sync/mutationHandler.d.ts +97 -0
  45. package/dist/sync/mutationHandler.js +886 -0
  46. package/dist/sync/registry.d.ts +73 -0
  47. package/dist/sync/registry.js +223 -0
  48. package/dist/sync/routes.d.ts +62 -0
  49. package/dist/sync/routes.js +936 -0
  50. package/dist/sync/scripts/compactTombstones.d.ts +47 -0
  51. package/dist/sync/scripts/compactTombstones.js +270 -0
  52. package/dist/sync/serialize.d.ts +20 -0
  53. package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
  54. package/dist/sync/socketHandlers.d.ts +54 -0
  55. package/dist/sync/socketHandlers.js +470 -0
  56. package/dist/sync/streams.d.ts +85 -0
  57. package/dist/sync/streams.js +221 -0
  58. package/dist/sync/syncApp.d.ts +29 -0
  59. package/dist/sync/syncApp.js +44 -0
  60. package/dist/sync/syncBatch.d.ts +71 -0
  61. package/dist/sync/syncBatch.js +252 -0
  62. package/dist/sync/syncSeqPlugin.d.ts +8 -0
  63. package/dist/sync/syncSeqPlugin.js +903 -0
  64. package/dist/sync/types.d.ts +254 -0
  65. package/dist/sync/types.js +9 -0
  66. package/dist/terrenoApp.js +90 -27
  67. package/dist/tests/models.d.ts +11 -27
  68. package/dist/tests/models.js +3 -5
  69. package/dist/tests/testHelper.d.ts +3 -9
  70. package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
  71. package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
  72. package/package.json +10 -8
  73. package/src/__tests__/openApiCompat.test.ts +20 -0
  74. package/src/actions.test.ts +12 -19
  75. package/src/actions.ts +2 -2
  76. package/src/api.coverage.test.ts +9 -7
  77. package/src/api.errorPaths.test.ts +1 -47
  78. package/src/api.test.ts +12 -0
  79. package/src/api.ts +98 -317
  80. package/src/auth.test.ts +128 -176
  81. package/src/auth.ts +109 -0
  82. package/src/betterAuth.ts +6 -0
  83. package/src/betterAuthSetup.test.ts +34 -0
  84. package/src/betterAuthSetup.ts +27 -8
  85. package/src/configurationPlugin.ts +2 -2
  86. package/src/consentApp.test.ts +12 -12
  87. package/src/docLoader.ts +3 -1
  88. package/src/errors.ts +33 -0
  89. package/src/expressServer.test.ts +7 -1
  90. package/src/expressServer.ts +5 -11
  91. package/src/githubAuth.ts +3 -1
  92. package/src/index.ts +42 -0
  93. package/src/middleware.test.ts +40 -1
  94. package/src/middleware.ts +15 -0
  95. package/src/models/versionConfig.ts +1 -1
  96. package/src/openApiBuilder.test.ts +9 -24
  97. package/src/openApiCompat.ts +99 -45
  98. package/src/openApiValidator.test.ts +0 -41
  99. package/src/permissions.test.ts +75 -1
  100. package/src/permissions.ts +49 -0
  101. package/src/plugins.test.ts +54 -14
  102. package/src/plugins.ts +47 -9
  103. package/src/populate.test.ts +16 -25
  104. package/src/realtime/changeStreamWatcher.ts +552 -114
  105. package/src/realtime/realtime.test.ts +157 -239
  106. package/src/realtime/realtimeApp.ts +80 -19
  107. package/src/realtime/sessionRevalidation.test.ts +423 -0
  108. package/src/realtime/sessionRevalidation.ts +273 -0
  109. package/src/realtime/socketAuth.test.ts +355 -0
  110. package/src/realtime/socketAuth.ts +228 -0
  111. package/src/realtime/socketUser.ts +69 -0
  112. package/src/realtime/types.ts +40 -0
  113. package/src/secretProviders.test.ts +7 -1
  114. package/src/secretProviders.ts +15 -3
  115. package/src/sync/ensureSyncIndexes.test.ts +144 -0
  116. package/src/sync/executors.test.ts +882 -0
  117. package/src/sync/executors.ts +703 -0
  118. package/src/sync/integration.test.ts +1160 -0
  119. package/src/sync/loadHarness.ts +618 -0
  120. package/src/sync/models.ts +640 -0
  121. package/src/sync/mutationHandler.test.ts +1252 -0
  122. package/src/sync/mutationHandler.ts +850 -0
  123. package/src/sync/registry.ts +232 -0
  124. package/src/sync/routes.ts +812 -0
  125. package/src/sync/scripts/compactTombstones.test.ts +249 -0
  126. package/src/sync/scripts/compactTombstones.ts +145 -0
  127. package/src/sync/serialize.ts +48 -0
  128. package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
  129. package/src/sync/socketHandlers.ts +320 -0
  130. package/src/sync/streams.ts +188 -0
  131. package/src/sync/sync.test.ts +582 -0
  132. package/src/sync/syncApp.ts +44 -0
  133. package/src/sync/syncBatch.ts +219 -0
  134. package/src/sync/syncFrontier.test.ts +494 -0
  135. package/src/sync/syncPhaseC.test.ts +718 -0
  136. package/src/sync/syncRoutes.test.ts +865 -0
  137. package/src/sync/syncSeqPlugin.ts +738 -0
  138. package/src/sync/syncSocket.test.ts +1440 -0
  139. package/src/sync/types.ts +270 -0
  140. package/src/terrenoApp.ts +37 -17
  141. package/src/tests/bunSetup.ts +8 -0
  142. package/src/tests/models.ts +6 -8
  143. package/tsconfig.json +4 -4
  144. package/tsconfig.typedoc.json +7 -1
  145. package/dist/__tests__/openApiCompat.test.js +0 -317
  146. package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
  147. package/dist/__tests__/versionCheckPlugin.test.js +0 -597
  148. package/dist/actions.openApi.test.d.ts +0 -1
  149. package/dist/actions.openApi.test.js +0 -253
  150. package/dist/actions.test.d.ts +0 -1
  151. package/dist/actions.test.js +0 -1087
  152. package/dist/api.arrayOperations.test.d.ts +0 -1
  153. package/dist/api.arrayOperations.test.js +0 -878
  154. package/dist/api.asyncHandler.test.d.ts +0 -1
  155. package/dist/api.asyncHandler.test.js +0 -236
  156. package/dist/api.coverage.test.d.ts +0 -1
  157. package/dist/api.coverage.test.js +0 -192
  158. package/dist/api.errorPaths.test.d.ts +0 -1
  159. package/dist/api.errorPaths.test.js +0 -887
  160. package/dist/api.errors.test.d.ts +0 -1
  161. package/dist/api.errors.test.js +0 -185
  162. package/dist/api.hooks.test.d.ts +0 -1
  163. package/dist/api.hooks.test.js +0 -950
  164. package/dist/api.query.test.d.ts +0 -1
  165. package/dist/api.query.test.js +0 -948
  166. package/dist/api.test.d.ts +0 -1
  167. package/dist/api.test.js +0 -2820
  168. package/dist/auth.compile.test.d.ts +0 -1
  169. package/dist/auth.compile.test.js +0 -201
  170. package/dist/auth.coverage.test.d.ts +0 -1
  171. package/dist/auth.coverage.test.js +0 -202
  172. package/dist/auth.test.d.ts +0 -1
  173. package/dist/auth.test.js +0 -2286
  174. package/dist/betterAuth.test.d.ts +0 -1
  175. package/dist/betterAuth.test.js +0 -181
  176. package/dist/betterAuthApp.test.d.ts +0 -1
  177. package/dist/betterAuthApp.test.js +0 -242
  178. package/dist/betterAuthSetup.test.d.ts +0 -1
  179. package/dist/betterAuthSetup.test.js +0 -762
  180. package/dist/config.test.d.ts +0 -1
  181. package/dist/config.test.js +0 -353
  182. package/dist/configuration.test.d.ts +0 -1
  183. package/dist/configuration.test.js +0 -980
  184. package/dist/configurationPlugin.test.d.ts +0 -1
  185. package/dist/configurationPlugin.test.js +0 -827
  186. package/dist/consentApp.test.d.ts +0 -1
  187. package/dist/consentApp.test.js +0 -1411
  188. package/dist/docLoader.test.d.ts +0 -1
  189. package/dist/docLoader.test.js +0 -137
  190. package/dist/envConfigurationPlugin.test.d.ts +0 -1
  191. package/dist/envConfigurationPlugin.test.js +0 -369
  192. package/dist/errors.test.d.ts +0 -1
  193. package/dist/errors.test.js +0 -607
  194. package/dist/expressServer.test.d.ts +0 -1
  195. package/dist/expressServer.test.js +0 -1249
  196. package/dist/githubAuth.test.d.ts +0 -1
  197. package/dist/githubAuth.test.js +0 -1142
  198. package/dist/httpClient.test.d.ts +0 -1
  199. package/dist/httpClient.test.js +0 -1471
  200. package/dist/logger.test.d.ts +0 -1
  201. package/dist/logger.test.js +0 -685
  202. package/dist/middleware.test.d.ts +0 -1
  203. package/dist/middleware.test.js +0 -241
  204. package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
  205. package/dist/notifiers/googleChatNotifier.test.js +0 -298
  206. package/dist/notifiers/slackNotifier.test.d.ts +0 -1
  207. package/dist/notifiers/slackNotifier.test.js +0 -297
  208. package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
  209. package/dist/notifiers/zoomNotifier.test.js +0 -373
  210. package/dist/openApi.test.d.ts +0 -1
  211. package/dist/openApi.test.js +0 -620
  212. package/dist/openApiBuilder.test.d.ts +0 -1
  213. package/dist/openApiBuilder.test.js +0 -599
  214. package/dist/openApiEtag.test.d.ts +0 -1
  215. package/dist/openApiEtag.test.js +0 -86
  216. package/dist/openApiValidator.test.d.ts +0 -1
  217. package/dist/openApiValidator.test.js +0 -795
  218. package/dist/permissions.middleware.test.d.ts +0 -1
  219. package/dist/permissions.middleware.test.js +0 -308
  220. package/dist/permissions.test.d.ts +0 -1
  221. package/dist/permissions.test.js +0 -439
  222. package/dist/plugins.test.d.ts +0 -1
  223. package/dist/plugins.test.js +0 -902
  224. package/dist/populate.test.d.ts +0 -1
  225. package/dist/populate.test.js +0 -370
  226. package/dist/realtime/realtime.test.d.ts +0 -10
  227. package/dist/realtime/realtime.test.js +0 -4213
  228. package/dist/realtime/realtimeAppSetup.test.d.ts +0 -1
  229. package/dist/realtime/realtimeAppSetup.test.js +0 -213
  230. package/dist/requestContext.test.d.ts +0 -1
  231. package/dist/requestContext.test.js +0 -412
  232. package/dist/scriptRunner.test.d.ts +0 -1
  233. package/dist/scriptRunner.test.js +0 -386
  234. package/dist/secretProviders.test.d.ts +0 -1
  235. package/dist/secretProviders.test.js +0 -726
  236. package/dist/syncConsents.test.d.ts +0 -1
  237. package/dist/syncConsents.test.js +0 -522
  238. package/dist/terrenoApp.test.d.ts +0 -1
  239. package/dist/terrenoApp.test.js +0 -489
  240. package/dist/tests/bunSetup.d.ts +0 -1
  241. package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
  242. package/dist/tests/fixtures/compileAuthEntry.js +0 -37
  243. package/dist/tests/testEnv.d.ts +0 -0
  244. package/dist/tests/testEnv.js +0 -6
  245. package/dist/transformers.test.d.ts +0 -1
  246. package/dist/transformers.test.js +0 -423
  247. package/dist/utils.test.d.ts +0 -1
  248. package/dist/utils.test.js +0 -242
  249. package/src/realtime/realtimeAppSetup.test.ts +0 -161
  250. /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
@@ -0,0 +1,644 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
+ return new (P || (P = Promise))(function (resolve, reject) {
38
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
39
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
40
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
41
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
42
+ });
43
+ };
44
+ var __generator = (this && this.__generator) || function (thisArg, body) {
45
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
46
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
47
+ function verb(n) { return function (v) { return step([n, v]); }; }
48
+ function step(op) {
49
+ if (f) throw new TypeError("Generator is already executing.");
50
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
51
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
52
+ if (y = 0, t) op = [op[0] & 2, t.value];
53
+ switch (op[0]) {
54
+ case 0: case 1: t = op; break;
55
+ case 4: _.label++; return { value: op[1], done: false };
56
+ case 5: _.label++; y = op[1]; op = [0]; continue;
57
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
58
+ default:
59
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
60
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
61
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
62
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
63
+ if (t[2]) _.ops.pop();
64
+ _.trys.pop(); continue;
65
+ }
66
+ op = body.call(thisArg, _);
67
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
68
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
69
+ }
70
+ };
71
+ var __values = (this && this.__values) || function(o) {
72
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
73
+ if (m) return m.call(o);
74
+ if (o && typeof o.length === "number") return {
75
+ next: function () {
76
+ if (o && i >= o.length) o = void 0;
77
+ return { value: o && o[i++], done: !o };
78
+ }
79
+ };
80
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
81
+ };
82
+ var __importDefault = (this && this.__importDefault) || function (mod) {
83
+ return (mod && mod.__esModule) ? mod : { "default": mod };
84
+ };
85
+ var _a, _b, _c, _d;
86
+ Object.defineProperty(exports, "__esModule", { value: true });
87
+ exports.getOrCreateSyncKeyMaterial = exports.ensureSyncModelIndexes = exports.SyncKey = exports.SyncMutation = exports.SYNC_MUTATION_LEASE_MS = exports.SyncScopeMove = exports.getCompactedThroughSeq = exports.recordCompactedThroughSeq = exports.computeStableFrontier = exports.releaseSyncSeqs = exports.confirmSyncSeqs = exports.claimSyncSeqs = exports.SyncCounter = exports.PENDING_CLAIM_LEASE_MS = void 0;
88
+ var node_crypto_1 = __importDefault(require("node:crypto"));
89
+ var mongoose_1 = __importStar(require("mongoose"));
90
+ var logger_1 = require("../logger");
91
+ var plugins_1 = require("../plugins");
92
+ /**
93
+ * C1: a `pending` claim older than this is treated as abandoned (the writer
94
+ * crashed between claiming and confirming) and excluded from the frontier — a
95
+ * crashed writer must never freeze the frontier forever. Default 60s.
96
+ */
97
+ exports.PENDING_CLAIM_LEASE_MS = 60 * 1000;
98
+ var syncPendingClaimSchema = new mongoose_1.Schema({
99
+ claimedAt: {
100
+ default: function () { return new Date(); },
101
+ description: "When this seq was claimed; a claim older than PENDING_CLAIM_LEASE_MS is stale",
102
+ type: Date,
103
+ },
104
+ seq: {
105
+ description: "A claimed-but-uncommitted sequence number",
106
+ required: true,
107
+ type: Number,
108
+ },
109
+ }, { _id: false, strict: "throw" });
110
+ var syncCounterSchema = new mongoose_1.Schema({
111
+ compactedThroughSeq: {
112
+ default: 0,
113
+ description: "C7 retention watermark: the highest seq whose tombstone/scope-move row has been " +
114
+ "hard-deleted by compactTombstones; served as the snapshot's oldestRetainedSeq",
115
+ type: Number,
116
+ },
117
+ pending: {
118
+ default: [],
119
+ description: "C1 in-flight registry: seqs claimed but not yet confirmed committed; the stable " +
120
+ "frontier is min(pending.seq) - 1 (or the head when empty)",
121
+ type: [syncPendingClaimSchema],
122
+ },
123
+ seq: {
124
+ default: 0,
125
+ description: "The last sequence number claimed for this stream",
126
+ type: Number,
127
+ },
128
+ stream: {
129
+ description: "Stream key this counter belongs to (e.g. 'todos|owner:123')",
130
+ required: true,
131
+ type: String,
132
+ unique: true,
133
+ },
134
+ },
135
+ // Consumer apps run checkModelsStrict() at startup; framework models must comply.
136
+ { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
137
+ exports.SyncCounter = (_a = mongoose_1.default.models.SyncCounter) !== null && _a !== void 0 ? _a : mongoose_1.default.model("SyncCounter", syncCounterSchema);
138
+ /**
139
+ * C1: atomically claim `count` sequence numbers for a stream AND register them on
140
+ * the in-flight registry so the stable frontier can exclude them until committed.
141
+ *
142
+ * Two modes:
143
+ * - **No caller session (the hot path):** one `findOneAndUpdate` does `$inc: {seq}`
144
+ * plus `$push` of a `pending` entry per claimed seq. The write commits separately,
145
+ * so `confirmSyncSeqs` must `$pull` the entries once it lands. Until then the
146
+ * frontier holds below the lowest pending seq.
147
+ * - **Caller session present:** the `$inc` and the document write commit atomically
148
+ * in the caller's transaction, so there is no window where a claimed seq is
149
+ * uncommitted — the pending registry is skipped (`registered: false`, nothing to
150
+ * confirm). Frontier logic treats a session-backed claim as already committed.
151
+ *
152
+ * Retries once on the upsert race (two concurrent first claims for a new stream).
153
+ */
154
+ var claimSyncSeqs = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
155
+ var materialize, doc, claim, error_1;
156
+ var stream = _b.stream, _c = _b.count, count = _c === void 0 ? 1 : _c, session = _b.session;
157
+ return __generator(this, function (_d) {
158
+ switch (_d.label) {
159
+ case 0:
160
+ materialize = function (lastSeq) {
161
+ var seqs = [];
162
+ for (var s = lastSeq - count + 1; s <= lastSeq; s++) {
163
+ seqs.push(s);
164
+ }
165
+ return seqs;
166
+ };
167
+ if (!session) return [3 /*break*/, 2];
168
+ return [4 /*yield*/, exports.SyncCounter.findOneAndUpdate({ stream: stream }, { $inc: { seq: count } }, { new: true, session: session, upsert: true })];
169
+ case 1:
170
+ doc = _d.sent();
171
+ return [2 /*return*/, { lastSeq: doc.seq, registered: false, seqs: materialize(doc.seq) }];
172
+ case 2:
173
+ claim = function () { return __awaiter(void 0, void 0, void 0, function () {
174
+ var now, doc;
175
+ return __generator(this, function (_a) {
176
+ switch (_a.label) {
177
+ case 0:
178
+ now = new Date();
179
+ return [4 /*yield*/, exports.SyncCounter.findOneAndUpdate({ stream: stream }, [
180
+ { $set: { _syncOldSeq: { $ifNull: ["$seq", 0] } } },
181
+ { $set: { seq: { $add: ["$_syncOldSeq", count] } } },
182
+ {
183
+ $set: {
184
+ pending: {
185
+ $concatArrays: [
186
+ { $ifNull: ["$pending", []] },
187
+ {
188
+ $map: {
189
+ as: "s",
190
+ in: { claimedAt: now, seq: "$$s" },
191
+ input: { $range: [{ $add: ["$_syncOldSeq", 1] }, { $add: ["$seq", 1] }] },
192
+ },
193
+ },
194
+ ],
195
+ },
196
+ },
197
+ },
198
+ { $unset: "_syncOldSeq" },
199
+ ], { new: true, upsert: true })];
200
+ case 1:
201
+ doc = _a.sent();
202
+ return [2 /*return*/, { lastSeq: doc.seq, registered: true, seqs: materialize(doc.seq) }];
203
+ }
204
+ });
205
+ }); };
206
+ _d.label = 3;
207
+ case 3:
208
+ _d.trys.push([3, 5, , 6]);
209
+ return [4 /*yield*/, claim()];
210
+ case 4: return [2 /*return*/, _d.sent()];
211
+ case 5:
212
+ error_1 = _d.sent();
213
+ // E11000: two concurrent upserts raced to create the counter; the loser retries
214
+ // against the now-existing document.
215
+ if (error_1.code === 11000) {
216
+ return [2 /*return*/, claim()];
217
+ }
218
+ throw error_1;
219
+ case 6: return [2 /*return*/];
220
+ }
221
+ });
222
+ }); };
223
+ exports.claimSyncSeqs = claimSyncSeqs;
224
+ /** Remove the given pending claims from a stream's in-flight registry. */
225
+ var pullPendingClaims = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
226
+ var result;
227
+ var _c;
228
+ var stream = _b.stream, seqs = _b.seqs, session = _b.session;
229
+ return __generator(this, function (_d) {
230
+ switch (_d.label) {
231
+ case 0: return [4 /*yield*/, exports.SyncCounter.updateOne({ stream: stream }, { $pull: { pending: { seq: { $in: seqs } } } }, session ? { session: session } : {})];
232
+ case 1:
233
+ result = _d.sent();
234
+ return [2 /*return*/, { cleared: ((_c = result.modifiedCount) !== null && _c !== void 0 ? _c : 0) > 0 }];
235
+ }
236
+ });
237
+ }); };
238
+ /**
239
+ * C1: confirm that the writes owning `seqs` on `stream` have committed, clearing
240
+ * their pending registry entries so the stable frontier can advance past them.
241
+ * A no-op when `seqs` is empty (a session-backed claim registered nothing). Runs
242
+ * after the document write commits (`post("save")` / query-write post hook); a
243
+ * `$pull` failure is logged by the caller and left to age out via the lease.
244
+ *
245
+ * Returns whether anything was actually cleared so callers can detect a reaped
246
+ * lease — see {@link SyncSeqConfirmResult}.
247
+ */
248
+ var confirmSyncSeqs = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
249
+ var stream = _b.stream, seqs = _b.seqs, session = _b.session;
250
+ return __generator(this, function (_c) {
251
+ if (seqs.length === 0) {
252
+ return [2 /*return*/, { cleared: true }];
253
+ }
254
+ return [2 /*return*/, pullPendingClaims({ seqs: seqs, session: session, stream: stream })];
255
+ });
256
+ }); };
257
+ exports.confirmSyncSeqs = confirmSyncSeqs;
258
+ /**
259
+ * Cancel claimed-but-never-used seqs (Task 9.13): the owning write failed, so the seqs
260
+ * will never be stamped on any document and holding the frontier below them for the
261
+ * full lease would stall every catch-up cursor on the stream for no reason. Unlike
262
+ * `confirmSyncSeqs` this is only safe when the write is known NOT to have committed
263
+ * (e.g. a Mongoose `VersionError`, where the conditional update matched no document).
264
+ */
265
+ var releaseSyncSeqs = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
266
+ var stream = _b.stream, seqs = _b.seqs, session = _b.session;
267
+ return __generator(this, function (_c) {
268
+ switch (_c.label) {
269
+ case 0:
270
+ if (seqs.length === 0) {
271
+ return [2 /*return*/];
272
+ }
273
+ return [4 /*yield*/, pullPendingClaims({ seqs: seqs, session: session, stream: stream })];
274
+ case 1:
275
+ _c.sent();
276
+ return [2 /*return*/];
277
+ }
278
+ });
279
+ }); };
280
+ exports.releaseSyncSeqs = releaseSyncSeqs;
281
+ /**
282
+ * C1: the stable frontier for a stream — the highest seq below which no claim is
283
+ * uncommitted. A cursor (snapshot or delta) may advance to seq N only when N is at
284
+ * or below this value, so no committed document is ever permanently skipped.
285
+ *
286
+ * frontier = `min(live pending.seq) - 1`, or the head `seq` when no live pending
287
+ * entries remain. A pending entry older than {@link PENDING_CLAIM_LEASE_MS} is
288
+ * considered abandoned (crashed writer): it is excluded from the min AND `$pull`ed
289
+ * opportunistically so a crash cannot freeze the frontier forever.
290
+ */
291
+ var computeStableFrontier = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
292
+ var counter, pending, cutoff, staleSeqs, minLiveSeq, pending_1, pending_1_1, claim, claimedAtMs;
293
+ var e_1, _c;
294
+ var _d;
295
+ var stream = _b.stream;
296
+ return __generator(this, function (_e) {
297
+ switch (_e.label) {
298
+ case 0: return [4 /*yield*/, exports.SyncCounter.findOne({ stream: stream })];
299
+ case 1:
300
+ counter = _e.sent();
301
+ if (!counter) {
302
+ return [2 /*return*/, 0];
303
+ }
304
+ pending = (_d = counter.pending) !== null && _d !== void 0 ? _d : [];
305
+ if (pending.length === 0) {
306
+ return [2 /*return*/, counter.seq];
307
+ }
308
+ cutoff = Date.now() - exports.PENDING_CLAIM_LEASE_MS;
309
+ staleSeqs = [];
310
+ minLiveSeq = Number.POSITIVE_INFINITY;
311
+ try {
312
+ for (pending_1 = __values(pending), pending_1_1 = pending_1.next(); !pending_1_1.done; pending_1_1 = pending_1.next()) {
313
+ claim = pending_1_1.value;
314
+ claimedAtMs = new Date(claim.claimedAt).getTime();
315
+ if (claimedAtMs < cutoff) {
316
+ staleSeqs.push(claim.seq);
317
+ continue;
318
+ }
319
+ if (claim.seq < minLiveSeq) {
320
+ minLiveSeq = claim.seq;
321
+ }
322
+ }
323
+ }
324
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
325
+ finally {
326
+ try {
327
+ if (pending_1_1 && !pending_1_1.done && (_c = pending_1.return)) _c.call(pending_1);
328
+ }
329
+ finally { if (e_1) throw e_1.error; }
330
+ }
331
+ if (!(staleSeqs.length > 0)) return [3 /*break*/, 3];
332
+ // Opportunistic cleanup: a crashed writer's claim must not freeze the frontier.
333
+ return [4 /*yield*/, exports.SyncCounter.updateOne({ stream: stream }, { $pull: { pending: { seq: { $in: staleSeqs } } } }).catch(function () { })];
334
+ case 2:
335
+ // Opportunistic cleanup: a crashed writer's claim must not freeze the frontier.
336
+ _e.sent();
337
+ _e.label = 3;
338
+ case 3:
339
+ // All pending entries were stale → frontier is the head; otherwise one below the
340
+ // lowest live (uncommitted) claim.
341
+ return [2 /*return*/, minLiveSeq === Number.POSITIVE_INFINITY ? counter.seq : minLiveSeq - 1];
342
+ }
343
+ });
344
+ }); };
345
+ exports.computeStableFrontier = computeStableFrontier;
346
+ /**
347
+ * C7: raise a stream's retention watermark to `seq` (never lowers it — `$max`), called by
348
+ * `compactTombstones` after it hard-deletes rows. Served to clients as the snapshot's
349
+ * `oldestRetainedSeq`, which is the only signal that a stale cursor must re-bootstrap.
350
+ * Does not create the counter: a stream with compacted rows always already has one.
351
+ */
352
+ var recordCompactedThroughSeq = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
353
+ var stream = _b.stream, seq = _b.seq;
354
+ return __generator(this, function (_c) {
355
+ switch (_c.label) {
356
+ case 0:
357
+ if (seq <= 0) {
358
+ return [2 /*return*/];
359
+ }
360
+ return [4 /*yield*/, exports.SyncCounter.updateOne({ stream: stream }, { $max: { compactedThroughSeq: seq } })];
361
+ case 1:
362
+ _c.sent();
363
+ return [2 /*return*/];
364
+ }
365
+ });
366
+ }); };
367
+ exports.recordCompactedThroughSeq = recordCompactedThroughSeq;
368
+ /**
369
+ * C7: the stream's retention watermark — the highest seq whose tombstone/marker has been
370
+ * compacted away. 0 when nothing has been compacted (no retention gap to enforce).
371
+ */
372
+ var getCompactedThroughSeq = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
373
+ var counter;
374
+ var _c;
375
+ var stream = _b.stream;
376
+ return __generator(this, function (_d) {
377
+ switch (_d.label) {
378
+ case 0: return [4 /*yield*/, (0, plugins_1.findOneOrNoneFor)(exports.SyncCounter, { stream: stream })];
379
+ case 1:
380
+ counter = _d.sent();
381
+ return [2 /*return*/, (_c = counter === null || counter === void 0 ? void 0 : counter.compactedThroughSeq) !== null && _c !== void 0 ? _c : 0];
382
+ }
383
+ });
384
+ }); };
385
+ exports.getCompactedThroughSeq = getCompactedThroughSeq;
386
+ var syncScopeMoveSchema = new mongoose_1.Schema({
387
+ collectionTag: {
388
+ description: "The collection tag of the moved document",
389
+ required: true,
390
+ type: String,
391
+ },
392
+ created: {
393
+ default: function () { return new Date(); },
394
+ description: "When the move was recorded; reaped by compactTombstones once older than the " +
395
+ "owning model's retentionDays (deliberately NOT TTL-indexed — see the model doc)",
396
+ type: Date,
397
+ },
398
+ entityId: {
399
+ description: "The _id of the document that moved between streams",
400
+ required: true,
401
+ type: String,
402
+ },
403
+ fromStream: {
404
+ description: "The stream the document left (the one to tombstone)",
405
+ required: true,
406
+ type: String,
407
+ },
408
+ seq: {
409
+ description: "Seq claimed from the OLD stream's counter; orders the tombstone in that stream",
410
+ required: true,
411
+ type: Number,
412
+ },
413
+ toStream: {
414
+ description: "The stream the document moved into",
415
+ required: true,
416
+ type: String,
417
+ },
418
+ },
419
+ // Consumer apps run checkModelsStrict() at startup; framework models must comply.
420
+ { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
421
+ // Old-stream snapshot catch-up pages markers by {fromStream, seq}.
422
+ syncScopeMoveSchema.index({ fromStream: 1, seq: 1 });
423
+ syncScopeMoveSchema.index({ collectionTag: 1, entityId: 1 });
424
+ // `compactTombstones` reaps markers by {collectionTag, created}.
425
+ syncScopeMoveSchema.index({ collectionTag: 1, created: 1 });
426
+ /**
427
+ * Name of the TTL index earlier versions built on `created`. Task 9.15 removed it:
428
+ * a fixed 90-day TTL ignored per-model `retentionDays`, so markers for a
429
+ * longer-retention model vanished with no watermark update and old-stream clients
430
+ * silently never learned about the move. `compactTombstones` is now the sole reaper
431
+ * (it raises `compactedThroughSeq` as it deletes), so the stale index is dropped at
432
+ * startup to stop it from reaping behind the watermark's back.
433
+ */
434
+ var LEGACY_SCOPE_MOVE_TTL_INDEX_NAME = "created_1";
435
+ /**
436
+ * C4: durable marker written in the same op-scope as a scope move, replacing the
437
+ * racy `_syncPrevStream` post-image read. The old stream tombstones the document
438
+ * from this marker (change-stream fan-out + snapshot catch-up), so a racing second
439
+ * write that overwrites `_syncPrevStream` can no longer erase the tombstone.
440
+ *
441
+ * Retention: reaped only by `compactTombstones`, which honors the owning model's
442
+ * `retentionDays` and records a `compactedThroughSeq` watermark as it deletes.
443
+ */
444
+ exports.SyncScopeMove = (_b = mongoose_1.default.models.SyncScopeMove) !== null && _b !== void 0 ? _b : mongoose_1.default.model("SyncScopeMove", syncScopeMoveSchema);
445
+ /** Ledger rows are only needed while a client could still replay a mutation. */
446
+ var SYNC_MUTATION_TTL_SECONDS = 30 * 24 * 60 * 60;
447
+ /**
448
+ * C5 (FIX 6): a `pending` row older than this may be taken over by a fresh
449
+ * delivery — the original claimant is assumed to have crashed between the
450
+ * claim and finalizing the ledger row.
451
+ */
452
+ exports.SYNC_MUTATION_LEASE_MS = 60 * 1000;
453
+ var syncMutationSchema = new mongoose_1.Schema({
454
+ claimedAt: {
455
+ default: function () { return new Date(); },
456
+ description: "When this delivery claimed the mutation (lease); a pending row older than " +
457
+ "SYNC_MUTATION_LEASE_MS may be taken over by a fresh delivery via findOneAndUpdate",
458
+ type: Date,
459
+ },
460
+ created: {
461
+ default: function () { return new Date(); },
462
+ description: "When the mutation was first claimed; TTL-indexed so rows expire after 30 days",
463
+ type: Date,
464
+ },
465
+ error: {
466
+ description: "Error message recorded for rejected mutations",
467
+ type: String,
468
+ },
469
+ mutationId: {
470
+ description: "Client-generated idempotency key; unique so concurrent deliveries claim exactly once",
471
+ required: true,
472
+ type: String,
473
+ unique: true,
474
+ },
475
+ nackCode: {
476
+ description: "Nack code recorded for rejected mutations (conflict/unauthorized/validation/error)",
477
+ type: String,
478
+ },
479
+ resultId: {
480
+ description: "The affected document id, recorded when the mutation is applied or conflicts; a " +
481
+ "duplicate delivery of a conflict re-serializes the live document from this id",
482
+ type: String,
483
+ },
484
+ resultSeq: {
485
+ description: "The document's _syncSeq after apply (applied) or at conflict time (conflicted)",
486
+ type: Number,
487
+ },
488
+ status: {
489
+ description: "Lifecycle status: pending while applying, then applied, conflicted, or failed",
490
+ enum: ["pending", "applied", "conflicted", "failed"],
491
+ required: true,
492
+ type: String,
493
+ },
494
+ userId: {
495
+ description: "The user who submitted the mutation; replays from other users are rejected",
496
+ required: true,
497
+ type: String,
498
+ },
499
+ },
500
+ // Consumer apps run checkModelsStrict() at startup; framework models must comply.
501
+ { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
502
+ syncMutationSchema.index({ created: 1 }, { expireAfterSeconds: SYNC_MUTATION_TTL_SECONDS });
503
+ /**
504
+ * Idempotency ledger for the sync mutation channel. A row is inserted with status
505
+ * `pending` before a mutation is applied (the atomic claim on the unique mutationId);
506
+ * the outcome is recorded on the same row so duplicate deliveries — socket retries or
507
+ * the HTTP fallback racing a socket send — read back the recorded outcome instead of
508
+ * re-applying.
509
+ *
510
+ * Task 9.21: a row NEVER stores document data. A conflict records only
511
+ * `{resultId, resultSeq}`; a duplicate delivery re-serializes the live document from
512
+ * `resultId` through the collection's sync `responseHandler`, so PHI is not retained in
513
+ * `syncmutations` for the 30-day TTL and the client also sees current (not stale) state.
514
+ */
515
+ exports.SyncMutation = (_c = mongoose_1.default.models.SyncMutation) !== null && _c !== void 0 ? _c : mongoose_1.default.model("SyncMutation", syncMutationSchema);
516
+ var syncKeySchema = new mongoose_1.Schema({
517
+ created: {
518
+ default: function () { return new Date(); },
519
+ description: "When this key material was generated",
520
+ type: Date,
521
+ },
522
+ keyMaterial: {
523
+ description: "Server-generated random key material (32 bytes, base64) for HKDF derivation",
524
+ required: true,
525
+ type: String,
526
+ },
527
+ userId: {
528
+ description: "The user this key material belongs to",
529
+ required: true,
530
+ type: String,
531
+ unique: true,
532
+ },
533
+ },
534
+ // Consumer apps run checkModelsStrict() at startup; framework models must comply.
535
+ { strict: "throw", toJSON: { virtuals: true }, toObject: { virtuals: true } });
536
+ exports.SyncKey = (_d = mongoose_1.default.models.SyncKey) !== null && _d !== void 0 ? _d : mongoose_1.default.model("SyncKey", syncKeySchema);
537
+ /**
538
+ * Every model backing the SyncDB protocol. Their indexes are correctness-critical, not
539
+ * just performance: without the unique `SyncMutation.mutationId` index a duplicate
540
+ * delivery double-applies a mutation (INV-3), and without the unique `SyncCounter.stream`
541
+ * index the counter upsert race mints duplicate seqs. Schema-level `unique: true` only
542
+ * builds them when Mongoose `autoIndex` is on — commonly disabled in production — so
543
+ * startup builds them explicitly.
544
+ */
545
+ // noExplicitAny: a heterogeneous list of models is only used for ensureIndexes
546
+ // biome-ignore lint/suspicious/noExplicitAny: a heterogeneous list of models is only used for ensureIndexes
547
+ var SYNC_BOOKKEEPING_MODELS = [exports.SyncCounter, exports.SyncMutation, exports.SyncScopeMove, exports.SyncKey];
548
+ /**
549
+ * Best-effort removal of the pre-Task-9.15 `SyncScopeMove` TTL index. Failure is only
550
+ * logged: a leftover TTL index degrades retention accounting but must never block
551
+ * startup, and the index is absent on fresh databases (the common case).
552
+ */
553
+ var dropLegacyScopeMoveTtlIndex = function () { return __awaiter(void 0, void 0, void 0, function () {
554
+ var indexes, legacy, error_2;
555
+ return __generator(this, function (_a) {
556
+ switch (_a.label) {
557
+ case 0:
558
+ _a.trys.push([0, 3, , 4]);
559
+ return [4 /*yield*/, exports.SyncScopeMove.collection.indexes()];
560
+ case 1:
561
+ indexes = _a.sent();
562
+ legacy = indexes.find(function (index) {
563
+ return index.name === LEGACY_SCOPE_MOVE_TTL_INDEX_NAME && index.expireAfterSeconds !== undefined;
564
+ });
565
+ if (!legacy) {
566
+ return [2 /*return*/];
567
+ }
568
+ return [4 /*yield*/, exports.SyncScopeMove.collection.dropIndex(LEGACY_SCOPE_MOVE_TTL_INDEX_NAME)];
569
+ case 2:
570
+ _a.sent();
571
+ logger_1.logger.info("[sync] Dropped the legacy SyncScopeMove TTL index; compactTombstones is now the " +
572
+ "sole reaper of scope-move markers (it honors per-model retentionDays).");
573
+ return [3 /*break*/, 4];
574
+ case 3:
575
+ error_2 = _a.sent();
576
+ logger_1.logger.warn("[sync] Could not drop the legacy SyncScopeMove TTL index", {
577
+ error: String(error_2),
578
+ });
579
+ return [3 /*break*/, 4];
580
+ case 4: return [2 /*return*/];
581
+ }
582
+ });
583
+ }); };
584
+ /**
585
+ * Build the indexes for every sync bookkeeping model, throwing on the first failure so
586
+ * server startup fails loudly. Kicked off by `SyncApp.register` and awaited through
587
+ * `ensureSyncIndexes()`; idempotent, so calling it repeatedly is safe.
588
+ */
589
+ var ensureSyncModelIndexes = function () { return __awaiter(void 0, void 0, void 0, function () {
590
+ return __generator(this, function (_a) {
591
+ switch (_a.label) {
592
+ case 0: return [4 /*yield*/, dropLegacyScopeMoveTtlIndex()];
593
+ case 1:
594
+ _a.sent();
595
+ return [4 /*yield*/, Promise.all(SYNC_BOOKKEEPING_MODELS.map(function (model) { return __awaiter(void 0, void 0, void 0, function () {
596
+ var error_3;
597
+ return __generator(this, function (_a) {
598
+ switch (_a.label) {
599
+ case 0:
600
+ _a.trys.push([0, 2, , 3]);
601
+ return [4 /*yield*/, model.ensureIndexes()];
602
+ case 1:
603
+ _a.sent();
604
+ return [3 /*break*/, 3];
605
+ case 2:
606
+ error_3 = _a.sent();
607
+ logger_1.logger.error("[sync] Failed to ensure sync indexes for ".concat(model.modelName), {
608
+ error: String(error_3),
609
+ });
610
+ throw new Error("Failed to ensure sync indexes for ".concat(model.modelName, ": ").concat(String(error_3), ". ") +
611
+ "The sync protocol depends on these indexes (unique mutationId for mutation " +
612
+ "idempotency, unique stream for seq allocation); fix the DB and restart.");
613
+ case 3: return [2 /*return*/];
614
+ }
615
+ });
616
+ }); }))];
617
+ case 2:
618
+ _a.sent();
619
+ return [2 /*return*/];
620
+ }
621
+ });
622
+ }); };
623
+ exports.ensureSyncModelIndexes = ensureSyncModelIndexes;
624
+ /**
625
+ * Return the user's key material, generating it on first call. Race-safe: concurrent
626
+ * first calls converge on the single persisted value via `$setOnInsert` upsert — a
627
+ * caller must never receive bytes that were not persisted, or its encrypted store
628
+ * would be undecryptable by any other session.
629
+ */
630
+ var getOrCreateSyncKeyMaterial = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
631
+ var candidate, doc;
632
+ var userId = _b.userId;
633
+ return __generator(this, function (_c) {
634
+ switch (_c.label) {
635
+ case 0:
636
+ candidate = node_crypto_1.default.randomBytes(32).toString("base64");
637
+ return [4 /*yield*/, exports.SyncKey.findOneAndUpdate({ userId: userId }, { $setOnInsert: { created: new Date(), keyMaterial: candidate, userId: userId } }, { new: true, upsert: true })];
638
+ case 1:
639
+ doc = _c.sent();
640
+ return [2 /*return*/, doc.keyMaterial];
641
+ }
642
+ });
643
+ }); };
644
+ exports.getOrCreateSyncKeyMaterial = getOrCreateSyncKeyMaterial;