@terreno/api 0.30.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 (226) hide show
  1. package/dist/api.d.ts +10 -0
  2. package/dist/api.js +110 -361
  3. package/dist/auth.d.ts +26 -0
  4. package/dist/auth.js +100 -1
  5. package/dist/betterAuth.d.ts +5 -0
  6. package/dist/betterAuthSetup.js +23 -4
  7. package/dist/docLoader.js +12 -9
  8. package/dist/errors.d.ts +13 -0
  9. package/dist/errors.js +29 -1
  10. package/dist/index.d.ts +17 -1
  11. package/dist/index.js +29 -1
  12. package/dist/middleware.d.ts +7 -0
  13. package/dist/middleware.js +16 -1
  14. package/dist/openApiCompat.d.ts +10 -1
  15. package/dist/openApiCompat.js +115 -47
  16. package/dist/permissions.d.ts +27 -0
  17. package/dist/permissions.js +42 -1
  18. package/dist/plugins.d.ts +13 -0
  19. package/dist/plugins.js +35 -1
  20. package/dist/realtime/changeStreamWatcher.d.ts +100 -3
  21. package/dist/realtime/changeStreamWatcher.js +542 -137
  22. package/dist/realtime/realtimeApp.d.ts +2 -0
  23. package/dist/realtime/realtimeApp.js +73 -21
  24. package/dist/realtime/sessionRevalidation.d.ts +90 -0
  25. package/dist/realtime/sessionRevalidation.js +438 -0
  26. package/dist/realtime/socketAuth.d.ts +97 -0
  27. package/dist/realtime/socketAuth.js +272 -0
  28. package/dist/realtime/socketUser.d.ts +54 -0
  29. package/dist/realtime/socketUser.js +75 -5
  30. package/dist/realtime/types.d.ts +40 -0
  31. package/dist/secretProviders.d.ts +23 -0
  32. package/dist/secretProviders.js +9 -3
  33. package/dist/sync/executors.d.ts +198 -0
  34. package/dist/sync/executors.js +670 -0
  35. package/dist/sync/loadHarness.js +807 -0
  36. package/dist/sync/models.d.ts +216 -0
  37. package/dist/sync/models.js +644 -0
  38. package/dist/sync/mutationHandler.d.ts +97 -0
  39. package/dist/sync/mutationHandler.js +886 -0
  40. package/dist/sync/registry.d.ts +73 -0
  41. package/dist/sync/registry.js +223 -0
  42. package/dist/sync/routes.d.ts +62 -0
  43. package/dist/sync/routes.js +936 -0
  44. package/dist/sync/scripts/compactTombstones.d.ts +47 -0
  45. package/dist/sync/scripts/compactTombstones.js +270 -0
  46. package/dist/sync/serialize.d.ts +20 -0
  47. package/dist/{tests/bunSetup.js → sync/serialize.js} +35 -68
  48. package/dist/sync/socketHandlers.d.ts +54 -0
  49. package/dist/sync/socketHandlers.js +470 -0
  50. package/dist/sync/streams.d.ts +85 -0
  51. package/dist/sync/streams.js +221 -0
  52. package/dist/sync/syncApp.d.ts +29 -0
  53. package/dist/sync/syncApp.js +44 -0
  54. package/dist/sync/syncBatch.d.ts +71 -0
  55. package/dist/sync/syncBatch.js +252 -0
  56. package/dist/sync/syncSeqPlugin.d.ts +8 -0
  57. package/dist/sync/syncSeqPlugin.js +903 -0
  58. package/dist/sync/types.d.ts +254 -0
  59. package/dist/sync/types.js +9 -0
  60. package/dist/terrenoApp.js +90 -27
  61. package/dist/vendor/wesleytodd-openapi/index.d.ts +1 -1
  62. package/dist/vendor/wesleytodd-openapi/lib/ui.d.ts +2 -1
  63. package/package.json +7 -6
  64. package/src/__tests__/openApiCompat.test.ts +20 -0
  65. package/src/actions.openApi.test.ts +7 -10
  66. package/src/api.test.ts +12 -0
  67. package/src/api.ts +87 -295
  68. package/src/auth.test.ts +128 -176
  69. package/src/auth.ts +109 -0
  70. package/src/betterAuth.ts +6 -0
  71. package/src/betterAuthSetup.test.ts +34 -0
  72. package/src/betterAuthSetup.ts +20 -1
  73. package/src/docLoader.ts +3 -1
  74. package/src/errors.ts +33 -0
  75. package/src/expressServer.test.ts +7 -1
  76. package/src/index.ts +42 -0
  77. package/src/middleware.test.ts +40 -1
  78. package/src/middleware.ts +15 -0
  79. package/src/openApiCompat.ts +99 -45
  80. package/src/permissions.test.ts +75 -1
  81. package/src/permissions.ts +49 -0
  82. package/src/plugins.test.ts +54 -0
  83. package/src/plugins.ts +43 -0
  84. package/src/realtime/changeStreamWatcher.ts +552 -114
  85. package/src/realtime/realtime.test.ts +157 -0
  86. package/src/realtime/realtimeApp.ts +75 -11
  87. package/src/realtime/sessionRevalidation.test.ts +423 -0
  88. package/src/realtime/sessionRevalidation.ts +273 -0
  89. package/src/realtime/socketAuth.test.ts +355 -0
  90. package/src/realtime/socketAuth.ts +228 -0
  91. package/src/realtime/socketUser.ts +69 -0
  92. package/src/realtime/types.ts +40 -0
  93. package/src/secretProviders.test.ts +7 -1
  94. package/src/secretProviders.ts +15 -3
  95. package/src/sync/ensureSyncIndexes.test.ts +144 -0
  96. package/src/sync/executors.test.ts +882 -0
  97. package/src/sync/executors.ts +703 -0
  98. package/src/sync/integration.test.ts +1160 -0
  99. package/src/sync/loadHarness.ts +618 -0
  100. package/src/sync/models.ts +640 -0
  101. package/src/sync/mutationHandler.test.ts +1252 -0
  102. package/src/sync/mutationHandler.ts +850 -0
  103. package/src/sync/registry.ts +232 -0
  104. package/src/sync/routes.ts +812 -0
  105. package/src/sync/scripts/compactTombstones.test.ts +249 -0
  106. package/src/sync/scripts/compactTombstones.ts +145 -0
  107. package/src/sync/serialize.ts +48 -0
  108. package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
  109. package/src/sync/socketHandlers.ts +320 -0
  110. package/src/sync/streams.ts +188 -0
  111. package/src/sync/sync.test.ts +582 -0
  112. package/src/sync/syncApp.ts +44 -0
  113. package/src/sync/syncBatch.ts +219 -0
  114. package/src/sync/syncFrontier.test.ts +494 -0
  115. package/src/sync/syncPhaseC.test.ts +718 -0
  116. package/src/sync/syncRoutes.test.ts +865 -0
  117. package/src/sync/syncSeqPlugin.ts +738 -0
  118. package/src/sync/syncSocket.test.ts +1440 -0
  119. package/src/sync/types.ts +270 -0
  120. package/src/terrenoApp.ts +37 -17
  121. package/src/tests/bunSetup.ts +8 -0
  122. package/tsconfig.json +4 -4
  123. package/tsconfig.typedoc.json +7 -1
  124. package/dist/__tests__/openApiCompat.test.js +0 -317
  125. package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
  126. package/dist/__tests__/versionCheckPlugin.test.js +0 -597
  127. package/dist/actions.openApi.test.d.ts +0 -1
  128. package/dist/actions.openApi.test.js +0 -255
  129. package/dist/actions.test.d.ts +0 -1
  130. package/dist/actions.test.js +0 -1089
  131. package/dist/api.arrayOperations.test.d.ts +0 -1
  132. package/dist/api.arrayOperations.test.js +0 -878
  133. package/dist/api.asyncHandler.test.d.ts +0 -1
  134. package/dist/api.asyncHandler.test.js +0 -236
  135. package/dist/api.coverage.test.d.ts +0 -1
  136. package/dist/api.coverage.test.js +0 -194
  137. package/dist/api.errorPaths.test.d.ts +0 -1
  138. package/dist/api.errorPaths.test.js +0 -801
  139. package/dist/api.errors.test.d.ts +0 -1
  140. package/dist/api.errors.test.js +0 -185
  141. package/dist/api.hooks.test.d.ts +0 -1
  142. package/dist/api.hooks.test.js +0 -950
  143. package/dist/api.query.test.d.ts +0 -1
  144. package/dist/api.query.test.js +0 -948
  145. package/dist/api.test.d.ts +0 -1
  146. package/dist/api.test.js +0 -2820
  147. package/dist/auth.compile.test.d.ts +0 -1
  148. package/dist/auth.compile.test.js +0 -201
  149. package/dist/auth.coverage.test.d.ts +0 -1
  150. package/dist/auth.coverage.test.js +0 -202
  151. package/dist/auth.test.d.ts +0 -1
  152. package/dist/auth.test.js +0 -2286
  153. package/dist/betterAuth.test.d.ts +0 -1
  154. package/dist/betterAuth.test.js +0 -181
  155. package/dist/betterAuthApp.test.d.ts +0 -1
  156. package/dist/betterAuthApp.test.js +0 -242
  157. package/dist/betterAuthSetup.test.d.ts +0 -1
  158. package/dist/betterAuthSetup.test.js +0 -762
  159. package/dist/config.test.d.ts +0 -1
  160. package/dist/config.test.js +0 -353
  161. package/dist/configuration.test.d.ts +0 -1
  162. package/dist/configuration.test.js +0 -980
  163. package/dist/configurationPlugin.test.d.ts +0 -1
  164. package/dist/configurationPlugin.test.js +0 -827
  165. package/dist/consentApp.test.d.ts +0 -1
  166. package/dist/consentApp.test.js +0 -1415
  167. package/dist/docLoader.test.d.ts +0 -1
  168. package/dist/docLoader.test.js +0 -137
  169. package/dist/envConfigurationPlugin.test.d.ts +0 -1
  170. package/dist/envConfigurationPlugin.test.js +0 -369
  171. package/dist/errors.test.d.ts +0 -1
  172. package/dist/errors.test.js +0 -607
  173. package/dist/expressServer.test.d.ts +0 -1
  174. package/dist/expressServer.test.js +0 -1249
  175. package/dist/githubAuth.test.d.ts +0 -1
  176. package/dist/githubAuth.test.js +0 -1142
  177. package/dist/httpClient.test.d.ts +0 -1
  178. package/dist/httpClient.test.js +0 -1471
  179. package/dist/logger.test.d.ts +0 -1
  180. package/dist/logger.test.js +0 -685
  181. package/dist/middleware.test.d.ts +0 -1
  182. package/dist/middleware.test.js +0 -241
  183. package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
  184. package/dist/notifiers/googleChatNotifier.test.js +0 -298
  185. package/dist/notifiers/slackNotifier.test.d.ts +0 -1
  186. package/dist/notifiers/slackNotifier.test.js +0 -297
  187. package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
  188. package/dist/notifiers/zoomNotifier.test.js +0 -373
  189. package/dist/openApi.test.d.ts +0 -1
  190. package/dist/openApi.test.js +0 -620
  191. package/dist/openApiBuilder.test.d.ts +0 -1
  192. package/dist/openApiBuilder.test.js +0 -601
  193. package/dist/openApiEtag.test.d.ts +0 -1
  194. package/dist/openApiEtag.test.js +0 -86
  195. package/dist/openApiValidator.test.d.ts +0 -1
  196. package/dist/openApiValidator.test.js +0 -761
  197. package/dist/permissions.middleware.test.d.ts +0 -1
  198. package/dist/permissions.middleware.test.js +0 -308
  199. package/dist/permissions.test.d.ts +0 -1
  200. package/dist/permissions.test.js +0 -439
  201. package/dist/plugins.test.d.ts +0 -1
  202. package/dist/plugins.test.js +0 -890
  203. package/dist/populate.test.d.ts +0 -1
  204. package/dist/populate.test.js +0 -373
  205. package/dist/realtime/realtime.test.d.ts +0 -10
  206. package/dist/realtime/realtime.test.js +0 -3982
  207. package/dist/requestContext.test.d.ts +0 -1
  208. package/dist/requestContext.test.js +0 -412
  209. package/dist/scriptRunner.test.d.ts +0 -1
  210. package/dist/scriptRunner.test.js +0 -386
  211. package/dist/secretProviders.test.d.ts +0 -1
  212. package/dist/secretProviders.test.js +0 -726
  213. package/dist/syncConsents.test.d.ts +0 -1
  214. package/dist/syncConsents.test.js +0 -522
  215. package/dist/terrenoApp.test.d.ts +0 -1
  216. package/dist/terrenoApp.test.js +0 -489
  217. package/dist/tests/bunSetup.d.ts +0 -1
  218. package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
  219. package/dist/tests/fixtures/compileAuthEntry.js +0 -37
  220. package/dist/tests/testEnv.d.ts +0 -0
  221. package/dist/tests/testEnv.js +0 -6
  222. package/dist/transformers.test.d.ts +0 -1
  223. package/dist/transformers.test.js +0 -423
  224. package/dist/utils.test.d.ts +0 -1
  225. package/dist/utils.test.js +0 -242
  226. /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
@@ -1,4 +1,6 @@
1
1
  "use strict";
2
+ // noExplicitAny: change stream and socket handlers use dynamic document shapes
3
+ // biome-ignore-all lint/suspicious/noExplicitAny: change stream and socket handlers use dynamic document shapes
2
4
  var __assign = (this && this.__assign) || function () {
3
5
  __assign = Object.assign || function(t) {
4
6
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -119,19 +121,96 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
119
121
  return (mod && mod.__esModule) ? mod : { "default": mod };
120
122
  };
121
123
  Object.defineProperty(exports, "__esModule", { value: true });
122
- exports.stopChangeStreamWatcher = exports.startChangeStreamWatcher = exports.emitToDocumentAndQueryRooms = exports.emitToAuthorizedRoom = exports.serializeDoc = exports.ensureApiId = exports.resolveRooms = exports.mapOperationType = void 0;
124
+ exports.stopChangeStreamWatcher = exports.startChangeStreamWatcher = exports.emitSyncDeltaForChange = exports.emitToDocumentAndQueryRooms = exports.emitToAuthorizedRoom = exports.emitPayloadToAuthorizedRoom = exports.serializeDoc = exports.ensureApiId = exports.resolveRooms = exports.mapOperationType = exports.DEFAULT_IGNORED_COLLECTIONS = exports.SYNC_RESYNC_EVENT = exports.CHANGE_STREAM_RESTART_MAX_DELAY_MS = exports.CHANGE_STREAM_RESTART_BASE_DELAY_MS = void 0;
123
125
  var Sentry = __importStar(require("@sentry/bun"));
124
126
  var luxon_1 = require("luxon");
125
127
  var mongoose_1 = __importDefault(require("mongoose"));
126
128
  var errors_1 = require("../errors");
127
129
  var logger_1 = require("../logger");
128
130
  var permissions_1 = require("../permissions");
131
+ var models_1 = require("../sync/models");
132
+ var registry_1 = require("../sync/registry");
133
+ var serialize_1 = require("../sync/serialize");
134
+ var socketHandlers_1 = require("../sync/socketHandlers");
135
+ var streams_1 = require("../sync/streams");
129
136
  var queryMatcher_1 = require("./queryMatcher");
130
137
  var queryStore_1 = require("./queryStore");
131
- var registry_1 = require("./registry");
138
+ var registry_2 = require("./registry");
132
139
  var socketUser_1 = require("./socketUser");
133
140
  var changeWatcher = null;
134
- var DEFAULT_IGNORED_COLLECTIONS = ["socketio", "sessions"];
141
+ /**
142
+ * Task 9.16: restart state for the single process-wide watcher.
143
+ *
144
+ * The watcher used to only LOG on `error`/`close`: a replica-set election, a network blip,
145
+ * or a dropped cursor permanently killed every realtime event and every `sync:delta` until
146
+ * the process was restarted — online clients silently stopped receiving data while looking
147
+ * perfectly connected. The stream is now re-created with backoff, resuming from the last
148
+ * observed resume token so no events in the gap are missed.
149
+ */
150
+ /** Resume token of the last change seen, replayed via `resumeAfter` on restart. */
151
+ var lastResumeToken;
152
+ /** Bumped on every open/stop so callbacks from a replaced stream are ignored. */
153
+ var watcherGeneration = 0;
154
+ /** True between `stopChangeStreamWatcher()` and the next `startChangeStreamWatcher()`. */
155
+ var watcherStopped = true;
156
+ /** Consecutive failed opens, driving the backoff delay. Reset on a successful change. */
157
+ var restartAttempts = 0;
158
+ var restartTimer = null;
159
+ /** First restart delay; doubles per consecutive attempt up to the max. */
160
+ exports.CHANGE_STREAM_RESTART_BASE_DELAY_MS = 250;
161
+ /** Ceiling for the restart backoff — a long outage retries once every 30s, forever. */
162
+ exports.CHANGE_STREAM_RESTART_MAX_DELAY_MS = 30000;
163
+ /**
164
+ * Socket event telling clients their cached cursors may have gaps and they must
165
+ * re-bootstrap. Emitted when the change stream's oplog history is gone
166
+ * (`ChangeStreamHistoryLost`), which is the one failure a resume token cannot repair.
167
+ * Payload: {@link SyncResyncHint}.
168
+ */
169
+ exports.SYNC_RESYNC_EVENT = "sync:resync-required";
170
+ /** MongoDB error code / codeName for an unresumable change stream (oplog rolled past). */
171
+ var CHANGE_STREAM_HISTORY_LOST_CODE = 286;
172
+ /** True when the error means the resume token can no longer be used. */
173
+ var isHistoryLostError = function (error) {
174
+ var _a = (error !== null && error !== void 0 ? error : {}), code = _a.code, codeName = _a.codeName, message = _a.message;
175
+ return (code === CHANGE_STREAM_HISTORY_LOST_CODE ||
176
+ codeName === "ChangeStreamHistoryLost" ||
177
+ Boolean(message === null || message === void 0 ? void 0 : message.includes("ChangeStreamHistoryLost")) ||
178
+ Boolean(message === null || message === void 0 ? void 0 : message.includes("resume point may no longer be in the oplog")));
179
+ };
180
+ /**
181
+ * C8: per-entity serialized dispatch. The change-stream `"change"` handler is async and
182
+ * multiple events can be in flight at once; without ordering, two events for the SAME
183
+ * document could emit their deltas out of order (a client would then see LWW-by-seq
184
+ * violated within an entity). This keeps a promise chain PER entity key
185
+ * (`{collection}:{docId}`) so same-entity deltas dispatch strictly in change-stream
186
+ * order, while different entities still run concurrently. Chains self-clean when idle.
187
+ */
188
+ var entityDispatchChains = new Map();
189
+ var serializePerEntity = function (key, task) {
190
+ var _a;
191
+ var prior = (_a = entityDispatchChains.get(key)) !== null && _a !== void 0 ? _a : Promise.resolve();
192
+ var next = prior.then(task, task);
193
+ entityDispatchChains.set(key, next);
194
+ void next.finally(function () {
195
+ // Drop the chain once this was the last queued task, so the map does not grow.
196
+ if (entityDispatchChains.get(key) === next) {
197
+ entityDispatchChains.delete(key);
198
+ }
199
+ });
200
+ };
201
+ /**
202
+ * C7: the sync bookkeeping collections must never drive fan-out — their own change
203
+ * events are internal (counters/ledger/markers/keys), and processing them would emit
204
+ * spurious deltas or reprocess scope-move markers. Exported for testing.
205
+ */
206
+ exports.DEFAULT_IGNORED_COLLECTIONS = [
207
+ "socketio",
208
+ "sessions",
209
+ "synccounters",
210
+ "syncmutations",
211
+ "syncscopemoves",
212
+ "synckeys",
213
+ ];
135
214
  /**
136
215
  * Map MongoDB change stream operation types to our method names.
137
216
  *
@@ -303,45 +382,51 @@ var serializeDoc = function (entry, doc, method, user) { return __awaiter(void 0
303
382
  });
304
383
  }); };
305
384
  exports.serializeDoc = serializeDoc;
306
- var emitToAuthorizedRoom = function (io, room, event, entry, fullDocument, logDebug) { return __awaiter(void 0, void 0, void 0, function () {
307
- var sockets, sockets_1, sockets_1_1, socket, user, permissionDocument, canRead, data, error_3, e_2_1;
308
- var e_2, _a;
309
- return __generator(this, function (_b) {
310
- switch (_b.label) {
385
+ /**
386
+ * Generalized authorized-room emitter: iterates the sockets in `room`, runs the per-socket
387
+ * read-permission check against `fullDocument`, builds the payload per socket (so
388
+ * responseHandlers can tailor output to the receiving user), and emits `eventName`.
389
+ *
390
+ * `emitToAuthorizedRoom` (legacy "sync" events) and the sync layer's `sync:delta`
391
+ * emission both delegate here.
392
+ */
393
+ var emitPayloadToAuthorizedRoom = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
394
+ var sockets, sockets_1, sockets_1_1, socket, user, permissionDocument, canRead, _c, _d, _e, error_3, e_2_1;
395
+ var e_2, _f;
396
+ var io = _b.io, room = _b.room, eventName = _b.eventName, entry = _b.entry, fullDocument = _b.fullDocument, buildPayload = _b.buildPayload, logDebug = _b.logDebug;
397
+ return __generator(this, function (_g) {
398
+ switch (_g.label) {
311
399
  case 0:
312
400
  sockets = getSocketsInRoom(io, room);
313
- _b.label = 1;
401
+ _g.label = 1;
314
402
  case 1:
315
- _b.trys.push([1, 9, 10, 11]);
403
+ _g.trys.push([1, 9, 10, 11]);
316
404
  sockets_1 = __values(sockets), sockets_1_1 = sockets_1.next();
317
- _b.label = 2;
405
+ _g.label = 2;
318
406
  case 2:
319
407
  if (!!sockets_1_1.done) return [3 /*break*/, 8];
320
408
  socket = sockets_1_1.value;
321
409
  user = (0, socketUser_1.getSocketUser)(socket);
322
- _b.label = 3;
410
+ _g.label = 3;
323
411
  case 3:
324
- _b.trys.push([3, 6, , 7]);
412
+ _g.trys.push([3, 6, , 7]);
325
413
  permissionDocument = fullDocument !== null && fullDocument !== void 0 ? fullDocument : {};
326
414
  return [4 /*yield*/, canReadDocument(entry, user, permissionDocument)];
327
415
  case 4:
328
- canRead = _b.sent();
416
+ canRead = _g.sent();
329
417
  if (!canRead) {
330
418
  logDebug("[realtime] Skipped ".concat(room, " for ").concat(socket.id, ": read permission denied"));
331
419
  return [3 /*break*/, 7];
332
420
  }
333
- if (!fullDocument) {
334
- socket.emit("sync", event);
335
- return [3 /*break*/, 7];
336
- }
337
- return [4 /*yield*/, (0, exports.serializeDoc)(entry, fullDocument, event.method, user)];
421
+ _d = (_c = socket).emit;
422
+ _e = [eventName];
423
+ return [4 /*yield*/, buildPayload(user)];
338
424
  case 5:
339
- data = _b.sent();
340
- socket.emit("sync", __assign(__assign({}, event), { data: data }));
425
+ _d.apply(_c, _e.concat([_g.sent()]));
341
426
  return [3 /*break*/, 7];
342
427
  case 6:
343
- error_3 = _b.sent();
344
- logger_1.logger.error("[realtime] Failed to emit ".concat(entry.modelName, "/").concat(event.method, " to socket ").concat(socket.id, ": ").concat(error_3));
428
+ error_3 = _g.sent();
429
+ logger_1.logger.error("[realtime] Failed to emit ".concat(eventName, " for ").concat(entry.modelName, " to socket ").concat(socket.id, ": ").concat(error_3));
345
430
  Sentry.captureException(error_3);
346
431
  return [3 /*break*/, 7];
347
432
  case 7:
@@ -349,12 +434,12 @@ var emitToAuthorizedRoom = function (io, room, event, entry, fullDocument, logDe
349
434
  return [3 /*break*/, 2];
350
435
  case 8: return [3 /*break*/, 11];
351
436
  case 9:
352
- e_2_1 = _b.sent();
437
+ e_2_1 = _g.sent();
353
438
  e_2 = { error: e_2_1 };
354
439
  return [3 /*break*/, 11];
355
440
  case 10:
356
441
  try {
357
- if (sockets_1_1 && !sockets_1_1.done && (_a = sockets_1.return)) _a.call(sockets_1);
442
+ if (sockets_1_1 && !sockets_1_1.done && (_f = sockets_1.return)) _f.call(sockets_1);
358
443
  }
359
444
  finally { if (e_2) throw e_2.error; }
360
445
  return [7 /*endfinally*/];
@@ -362,6 +447,34 @@ var emitToAuthorizedRoom = function (io, room, event, entry, fullDocument, logDe
362
447
  }
363
448
  });
364
449
  }); };
450
+ exports.emitPayloadToAuthorizedRoom = emitPayloadToAuthorizedRoom;
451
+ var emitToAuthorizedRoom = function (io, room, event, entry, fullDocument, logDebug) { return __awaiter(void 0, void 0, void 0, function () {
452
+ return __generator(this, function (_a) {
453
+ return [2 /*return*/, (0, exports.emitPayloadToAuthorizedRoom)({
454
+ buildPayload: function (user) { return __awaiter(void 0, void 0, void 0, function () {
455
+ var data;
456
+ return __generator(this, function (_a) {
457
+ switch (_a.label) {
458
+ case 0:
459
+ if (!fullDocument) {
460
+ return [2 /*return*/, event];
461
+ }
462
+ return [4 /*yield*/, (0, exports.serializeDoc)(entry, fullDocument, event.method, user)];
463
+ case 1:
464
+ data = _a.sent();
465
+ return [2 /*return*/, __assign(__assign({}, event), { data: data })];
466
+ }
467
+ });
468
+ }); },
469
+ entry: entry,
470
+ eventName: "sync",
471
+ fullDocument: fullDocument,
472
+ io: io,
473
+ logDebug: logDebug,
474
+ room: room,
475
+ })];
476
+ });
477
+ }); };
365
478
  exports.emitToAuthorizedRoom = emitToAuthorizedRoom;
366
479
  /**
367
480
  * Emit a sync event to document-specific and query rooms.
@@ -504,13 +617,323 @@ var emitToDocumentAndQueryRooms = function (io, collection, event, fullDocument,
504
617
  });
505
618
  }); };
506
619
  exports.emitToDocumentAndQueryRooms = emitToDocumentAndQueryRooms;
620
+ /**
621
+ * Emit legacy realtime "sync" events for a change on a realtime-registered collection.
622
+ * Extracted verbatim from the change handler so sync-delta emission (which is keyed off
623
+ * the independent sync registry) runs even when the realtime path skips the change.
624
+ */
625
+ var processRealtimeChange = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
626
+ var method, fullDocument, isHardDelete, rooms, collectionTag, collection, event, rooms_1, rooms_1_1, room, e_4_1, metadata;
627
+ var e_4, _c;
628
+ var _d;
629
+ var io = _b.io, entry = _b.entry, change = _b.change, docId = _b.docId, logDebug = _b.logDebug, logInfo = _b.logInfo;
630
+ return __generator(this, function (_e) {
631
+ switch (_e.label) {
632
+ case 0:
633
+ method = (0, exports.mapOperationType)(change.operationType, change, entry.config.methods);
634
+ if (!method) {
635
+ return [2 /*return*/];
636
+ }
637
+ // Check if this method is enabled for this model
638
+ if (!entry.config.methods.includes(method)) {
639
+ logDebug("[realtime] Method ".concat(method, " not enabled for ").concat(entry.modelName));
640
+ return [2 /*return*/];
641
+ }
642
+ fullDocument = change.operationType === "delete" ? undefined : change.fullDocument;
643
+ isHardDelete = method === "delete" && !fullDocument;
644
+ if (isHardDelete) {
645
+ // Hard delete: no fullDocument, so we can't resolve owner/custom rooms.
646
+ // For owner strategy we cannot safely fan out to a model room without
647
+ // leaking deletes across users — admins still receive the event via the
648
+ // admin room and any document-specific subscribers via document rooms.
649
+ if (entry.config.roomStrategy === "owner") {
650
+ rooms = ["admin"];
651
+ }
652
+ else if (entry.config.roomStrategy === "broadcast") {
653
+ rooms = ["authenticated"];
654
+ }
655
+ else {
656
+ collectionTag = getCollectionTag(entry.routePath);
657
+ rooms = ["model:".concat(collectionTag)];
658
+ }
659
+ }
660
+ else {
661
+ rooms = (0, exports.resolveRooms)(entry, fullDocument !== null && fullDocument !== void 0 ? fullDocument : {}, method);
662
+ }
663
+ collection = getCollectionTag(entry.routePath);
664
+ event = __assign({ collection: collection, id: docId, method: method, model: entry.modelName, timestamp: luxon_1.DateTime.now().toMillis() }, (change.operationType === "update" && ((_d = change.updateDescription) === null || _d === void 0 ? void 0 : _d.updatedFields)
665
+ ? { updatedFields: Object.keys(change.updateDescription.updatedFields) }
666
+ : {}));
667
+ _e.label = 1;
668
+ case 1:
669
+ _e.trys.push([1, 6, 7, 8]);
670
+ rooms_1 = __values(rooms), rooms_1_1 = rooms_1.next();
671
+ _e.label = 2;
672
+ case 2:
673
+ if (!!rooms_1_1.done) return [3 /*break*/, 5];
674
+ room = rooms_1_1.value;
675
+ return [4 /*yield*/, (0, exports.emitToAuthorizedRoom)(io, room, event, entry, fullDocument, logDebug)];
676
+ case 3:
677
+ _e.sent();
678
+ _e.label = 4;
679
+ case 4:
680
+ rooms_1_1 = rooms_1.next();
681
+ return [3 /*break*/, 2];
682
+ case 5: return [3 /*break*/, 8];
683
+ case 6:
684
+ e_4_1 = _e.sent();
685
+ e_4 = { error: e_4_1 };
686
+ return [3 /*break*/, 8];
687
+ case 7:
688
+ try {
689
+ if (rooms_1_1 && !rooms_1_1.done && (_c = rooms_1.return)) _c.call(rooms_1);
690
+ }
691
+ finally { if (e_4) throw e_4.error; }
692
+ return [7 /*endfinally*/];
693
+ case 8:
694
+ // Emit to document-specific and query rooms
695
+ return [4 /*yield*/, (0, exports.emitToDocumentAndQueryRooms)(io, collection, event, fullDocument, logDebug, entry)];
696
+ case 9:
697
+ // Emit to document-specific and query rooms
698
+ _e.sent();
699
+ logDebug("[realtime] Emitted ".concat(method, " for ").concat(entry.modelName, "/").concat(docId, " to rooms: ").concat(rooms.join(", ")));
700
+ metadata = {
701
+ collection: event.collection,
702
+ id: event.id,
703
+ method: event.method,
704
+ model: event.model,
705
+ timestamp: event.timestamp,
706
+ };
707
+ if (event.updatedFields) {
708
+ metadata.updatedFields = event.updatedFields;
709
+ }
710
+ logInfo("[realtime] sync event: ".concat(JSON.stringify(metadata)));
711
+ return [2 /*return*/];
712
+ }
713
+ });
714
+ }); };
715
+ /**
716
+ * Emit `sync:delta` events for a change on a sync-registered collection.
717
+ *
718
+ * Deltas fan out to the dedicated `sync:{stream}` rooms joined via `sync:subscribe`
719
+ * (independent of the legacy realtime rooms), with the same per-socket read-permission
720
+ * checks as legacy events. `seq` and `stream` come from the post-image: the `syncPlugin`
721
+ * stamps `_syncSeq` on every synced write and the watcher runs `updateLookup`.
722
+ *
723
+ * Soft deletes (an update setting `deleted: true`, reclassified by `mapOperationType`)
724
+ * produce a `method: "delete"` delta with `deleted: true` and the tombstone data intact.
725
+ *
726
+ * Scope moves (C4): the old-stream tombstone is derived from durable `SyncScopeMove`
727
+ * markers (written by `syncPlugin` in the same op-scope as the move), NOT from the racy
728
+ * `_syncPrevStream` post-image — a second write racing this event's processing can reset
729
+ * `_syncPrevStream`, but it cannot erase the durable marker. For each marker on this
730
+ * document, a data-less tombstone is emitted to the marker's `fromStream` (carrying the
731
+ * marker's old-stream seq), and the new-stream delta is reported as `method: "create"`.
732
+ * Client tombstone application is idempotent by seq, so re-emitting a marker on a later
733
+ * change of the same doc is harmless.
734
+ *
735
+ * Every emitted delta carries `frontierSeq` (the emitting stream's stable frontier at
736
+ * emit time, C1); the client advances its cursor to `min(delta.seq, delta.frontierSeq)`
737
+ * so a delta observed out of commit order never advances a cursor past an uncommitted hole.
738
+ *
739
+ * A model with both `realtime` and `sync` configs emits both the legacy "sync" event and
740
+ * "sync:delta" — distinct event names, no interference (documented as transitional).
741
+ *
742
+ * Exported for testing.
743
+ */
744
+ var emitSyncDeltaForChange = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
745
+ var fullDocument, mapped, method, deleted, seq, stream, markers, movedAway, _loop_1, markers_1, markers_1_1, marker, e_5_1, frontierSeq, delta;
746
+ var e_5, _c;
747
+ var io = _b.io, entry = _b.entry, change = _b.change, docId = _b.docId, logDebug = _b.logDebug;
748
+ return __generator(this, function (_d) {
749
+ switch (_d.label) {
750
+ case 0:
751
+ if (change.operationType === "delete") {
752
+ // Hard deletes are blocked on synced models by syncPlugin; a direct collection-level
753
+ // delete has no post-image, so neither stream nor seq can be resolved. Clients
754
+ // reconcile via snapshot instead.
755
+ logDebug("[sync] Skipping hard delete for synced collection ".concat(entry.collectionTag, "/").concat(docId, ": ") +
756
+ "no post-image to resolve stream/seq");
757
+ return [2 /*return*/];
758
+ }
759
+ fullDocument = change.fullDocument;
760
+ if (!fullDocument) {
761
+ logDebug("[sync] Skipping ".concat(entry.collectionTag, "/").concat(docId, ": change has no fullDocument"));
762
+ return [2 /*return*/];
763
+ }
764
+ mapped = (0, exports.mapOperationType)(change.operationType, change);
765
+ if (!mapped) {
766
+ return [2 /*return*/];
767
+ }
768
+ method = mapped;
769
+ deleted = fullDocument.deleted === true;
770
+ seq = typeof fullDocument._syncSeq === "number" ? fullDocument._syncSeq : 0;
771
+ stream = (0, streams_1.resolveStreamForDoc)({
772
+ collectionTag: entry.collectionTag,
773
+ doc: fullDocument,
774
+ scope: entry.config.scope,
775
+ });
776
+ return [4 /*yield*/, models_1.SyncScopeMove.find({
777
+ collectionTag: entry.collectionTag,
778
+ entityId: docId,
779
+ }).lean()];
780
+ case 1:
781
+ markers = _d.sent();
782
+ movedAway = false;
783
+ _loop_1 = function (marker) {
784
+ var markerFrontier, tombstone;
785
+ return __generator(this, function (_e) {
786
+ switch (_e.label) {
787
+ case 0:
788
+ if (marker.fromStream === stream) {
789
+ return [2 /*return*/, "continue"];
790
+ }
791
+ movedAway = true;
792
+ return [4 /*yield*/, (0, models_1.computeStableFrontier)({ stream: marker.fromStream })];
793
+ case 1:
794
+ markerFrontier = _e.sent();
795
+ tombstone = {
796
+ collection: entry.collectionTag,
797
+ deleted: true,
798
+ frontierSeq: markerFrontier,
799
+ id: docId,
800
+ method: "delete",
801
+ seq: marker.seq,
802
+ stream: marker.fromStream,
803
+ };
804
+ return [4 /*yield*/, (0, exports.emitPayloadToAuthorizedRoom)({
805
+ buildPayload: function () { return tombstone; },
806
+ entry: entry,
807
+ eventName: "sync:delta",
808
+ fullDocument: fullDocument,
809
+ io: io,
810
+ logDebug: logDebug,
811
+ room: (0, socketHandlers_1.syncRoomForStream)(marker.fromStream),
812
+ })];
813
+ case 2:
814
+ _e.sent();
815
+ logDebug("[sync] Emitted scope-move tombstone for ".concat(entry.collectionTag, "/").concat(docId, " to ").concat(marker.fromStream, " seq=").concat(marker.seq));
816
+ return [2 /*return*/];
817
+ }
818
+ });
819
+ };
820
+ _d.label = 2;
821
+ case 2:
822
+ _d.trys.push([2, 7, 8, 9]);
823
+ markers_1 = __values(markers), markers_1_1 = markers_1.next();
824
+ _d.label = 3;
825
+ case 3:
826
+ if (!!markers_1_1.done) return [3 /*break*/, 6];
827
+ marker = markers_1_1.value;
828
+ return [5 /*yield**/, _loop_1(marker)];
829
+ case 4:
830
+ _d.sent();
831
+ _d.label = 5;
832
+ case 5:
833
+ markers_1_1 = markers_1.next();
834
+ return [3 /*break*/, 3];
835
+ case 6: return [3 /*break*/, 9];
836
+ case 7:
837
+ e_5_1 = _d.sent();
838
+ e_5 = { error: e_5_1 };
839
+ return [3 /*break*/, 9];
840
+ case 8:
841
+ try {
842
+ if (markers_1_1 && !markers_1_1.done && (_c = markers_1.return)) _c.call(markers_1);
843
+ }
844
+ finally { if (e_5) throw e_5.error; }
845
+ return [7 /*endfinally*/];
846
+ case 9:
847
+ // The new stream sees a moved-away doc for the first time: report it as a create
848
+ // (unless the same write also soft-deleted the doc, in which case the tombstone wins).
849
+ if (movedAway && method !== "delete") {
850
+ method = "create";
851
+ }
852
+ return [4 /*yield*/, (0, models_1.computeStableFrontier)({ stream: stream })];
853
+ case 10:
854
+ frontierSeq = _d.sent();
855
+ delta = __assign({ collection: entry.collectionTag, frontierSeq: frontierSeq, id: docId, method: method, seq: seq, stream: stream }, (deleted ? { deleted: true } : {}));
856
+ return [4 /*yield*/, (0, exports.emitPayloadToAuthorizedRoom)({
857
+ // C7: tombstone deltas carry no data (only id/seq/deleted); live deltas serialize.
858
+ buildPayload: deleted
859
+ ? function () { return delta; }
860
+ : function (user) { return __awaiter(void 0, void 0, void 0, function () {
861
+ var syntheticReq, data, _a;
862
+ return __generator(this, function (_b) {
863
+ switch (_b.label) {
864
+ case 0:
865
+ syntheticReq = { params: {}, query: {}, user: user };
866
+ _a = exports.ensureApiId;
867
+ return [4 /*yield*/, (0, serialize_1.serializeSyncPayload)({ doc: fullDocument, entry: entry, method: method, req: syntheticReq })];
868
+ case 1:
869
+ data = _a.apply(void 0, [_b.sent()]);
870
+ return [2 /*return*/, __assign(__assign({}, delta), { data: data })];
871
+ }
872
+ });
873
+ }); },
874
+ entry: entry,
875
+ eventName: "sync:delta",
876
+ fullDocument: fullDocument,
877
+ io: io,
878
+ logDebug: logDebug,
879
+ room: (0, socketHandlers_1.syncRoomForStream)(stream),
880
+ })];
881
+ case 11:
882
+ _d.sent();
883
+ logDebug("[sync] Emitted sync:delta ".concat(method, " for ").concat(entry.collectionTag, "/").concat(docId, " seq=").concat(seq, " stream=").concat(stream, " frontier=").concat(frontierSeq));
884
+ return [2 /*return*/];
885
+ }
886
+ });
887
+ }); };
888
+ exports.emitSyncDeltaForChange = emitSyncDeltaForChange;
507
889
  /**
508
890
  * Start watching MongoDB change streams and emitting real-time events.
891
+ *
892
+ * Task 9.16: the stream is supervised — an `error`/`close`/`end` schedules a re-open with
893
+ * exponential backoff, resuming from the last seen token. Call
894
+ * {@link stopChangeStreamWatcher} to end supervision.
509
895
  */
510
896
  var startChangeStreamWatcher = function (io, config, debug) {
511
- var _a, _b, _c, _d;
512
897
  if (config === void 0) { config = {}; }
513
898
  if (debug === void 0) { debug = false; }
899
+ watcherStopped = false;
900
+ restartAttempts = 0;
901
+ lastResumeToken = undefined;
902
+ openChangeStream({ config: config, debug: debug, io: io });
903
+ };
904
+ exports.startChangeStreamWatcher = startChangeStreamWatcher;
905
+ /**
906
+ * Task 9.16: schedule a re-open after a stream failure. Silently returns when the watcher
907
+ * was stopped, when a newer stream is already running (a failing stream can fire several
908
+ * terminal events), or when a restart is already pending.
909
+ */
910
+ var scheduleChangeStreamRestart = function (_a) {
911
+ var _b;
912
+ var config = _a.config, debug = _a.debug, generation = _a.generation, io = _a.io, reason = _a.reason;
913
+ if (watcherStopped || generation !== watcherGeneration || restartTimer) {
914
+ return;
915
+ }
916
+ restartAttempts += 1;
917
+ var delayMs = Math.min(exports.CHANGE_STREAM_RESTART_BASE_DELAY_MS * Math.pow(2, (restartAttempts - 1)), exports.CHANGE_STREAM_RESTART_MAX_DELAY_MS);
918
+ logger_1.logger.warn("[realtime] Change stream ".concat(reason, "; reopening in ").concat(delayMs, "ms (attempt ").concat(restartAttempts, ")"), { resumable: lastResumeToken !== undefined });
919
+ restartTimer = setTimeout(function () {
920
+ restartTimer = null;
921
+ if (watcherStopped) {
922
+ return;
923
+ }
924
+ openChangeStream({ config: config, debug: debug, io: io, isReopen: true });
925
+ }, delayMs);
926
+ // Never hold the process open just to retry a watcher.
927
+ (_b = restartTimer.unref) === null || _b === void 0 ? void 0 : _b.call(restartTimer);
928
+ };
929
+ /**
930
+ * Open (or re-open) the change stream and wire its listeners. Throws only for a
931
+ * configuration-level failure on the FIRST open (no connection, `watch` returned nothing),
932
+ * which must fail startup loudly; a failure while reopening is retried with backoff.
933
+ */
934
+ var openChangeStream = function (_a) {
935
+ var _b, _c, _d, _e;
936
+ var config = _a.config, debug = _a.debug, io = _a.io, _f = _a.isReopen, isReopen = _f === void 0 ? false : _f;
514
937
  var logInfo = function (message) {
515
938
  if (debug) {
516
939
  logger_1.logger.info(message);
@@ -521,10 +944,11 @@ var startChangeStreamWatcher = function (io, config, debug) {
521
944
  logger_1.logger.debug(message);
522
945
  }
523
946
  };
947
+ var generation = ++watcherGeneration;
524
948
  try {
525
949
  logInfo("[realtime] Initializing change stream watcher...");
526
- var ignored = new Set(__spreadArray(__spreadArray([], __read(DEFAULT_IGNORED_COLLECTIONS), false), __read(((_a = config.ignoredCollections) !== null && _a !== void 0 ? _a : [])), false));
527
- var ignoredOps_1 = new Set((_b = config.ignoredOperations) !== null && _b !== void 0 ? _b : []);
950
+ var ignored = new Set(__spreadArray(__spreadArray([], __read(exports.DEFAULT_IGNORED_COLLECTIONS), false), __read(((_b = config.ignoredCollections) !== null && _b !== void 0 ? _b : [])), false));
951
+ var ignoredOps_1 = new Set((_c = config.ignoredOperations) !== null && _c !== void 0 ? _c : []);
528
952
  // Build the change stream pipeline
529
953
  var pipeline = [
530
954
  {
@@ -550,26 +974,29 @@ var startChangeStreamWatcher = function (io, config, debug) {
550
974
  title: "MongoDB connection not available for change stream",
551
975
  });
552
976
  }
553
- var options = {
554
- batchSize: (_c = config.batchSize) !== null && _c !== void 0 ? _c : 50,
555
- fullDocument: (_d = config.fullDocument) !== null && _d !== void 0 ? _d : "updateLookup",
556
- fullDocumentBeforeChange: "off",
977
+ var options = __assign({ batchSize: (_d = config.batchSize) !== null && _d !== void 0 ? _d : 50, fullDocument: (_e = config.fullDocument) !== null && _e !== void 0 ? _e : "updateLookup", fullDocumentBeforeChange: "off",
557
978
  // How long the cursor waits for new events before yielding control.
558
979
  // Lower values give more responsive updates at the cost of more frequent driver round-trips.
559
- maxAwaitTimeMS: 1000,
560
- };
561
- changeWatcher = nativeDb.watch(pipeline, options);
562
- if (!changeWatcher) {
980
+ maxAwaitTimeMS: 1000 }, (lastResumeToken !== undefined ? { resumeAfter: lastResumeToken } : {}));
981
+ var stream = nativeDb.watch(pipeline, options);
982
+ if (!stream) {
563
983
  throw new errors_1.APIError({ status: 500, title: "Failed to create change stream watcher" });
564
984
  }
565
- changeWatcher.on("change", function (rawChange) { return __awaiter(void 0, void 0, void 0, function () {
566
- var change, collectionName, docId, entry, method, fullDocument, isHardDelete, rooms, collectionTag, collection, event_1, rooms_1, rooms_1_1, room, e_4_1, metadata, error_4;
567
- var e_4, _a;
568
- var _b, _c, _d, _e;
569
- return __generator(this, function (_f) {
570
- switch (_f.label) {
985
+ changeWatcher = stream;
986
+ stream.on("change", function (rawChange) { return __awaiter(void 0, void 0, void 0, function () {
987
+ var token, change_1, collectionName, docId_1, entry, syncEntry_1, error_4;
988
+ var _a, _b, _c;
989
+ return __generator(this, function (_d) {
990
+ switch (_d.label) {
571
991
  case 0:
572
- _f.trys.push([0, 10, , 11]);
992
+ _d.trys.push([0, 3, , 4]);
993
+ token = rawChange._id;
994
+ if (token !== undefined) {
995
+ lastResumeToken = token;
996
+ }
997
+ // A stream that is delivering events has recovered; drop the accumulated backoff so
998
+ // the next unrelated failure retries promptly instead of at the previous ceiling.
999
+ restartAttempts = 0;
573
1000
  // The pipeline restricts operationType to a subset that always has ns/documentKey;
574
1001
  // narrow once here so downstream code doesn't need repeated casts.
575
1002
  if (rawChange.operationType !== "insert" &&
@@ -578,137 +1005,115 @@ var startChangeStreamWatcher = function (io, config, debug) {
578
1005
  rawChange.operationType !== "delete") {
579
1006
  return [2 /*return*/];
580
1007
  }
581
- change = rawChange;
582
- collectionName = (_b = change.ns) === null || _b === void 0 ? void 0 : _b.coll;
583
- docId = (_d = (_c = change.documentKey) === null || _c === void 0 ? void 0 : _c._id) === null || _d === void 0 ? void 0 : _d.toString();
584
- if (!collectionName || !docId) {
1008
+ change_1 = rawChange;
1009
+ collectionName = (_a = change_1.ns) === null || _a === void 0 ? void 0 : _a.coll;
1010
+ docId_1 = (_c = (_b = change_1.documentKey) === null || _b === void 0 ? void 0 : _b._id) === null || _c === void 0 ? void 0 : _c.toString();
1011
+ if (!collectionName || !docId_1) {
585
1012
  return [2 /*return*/];
586
1013
  }
587
1014
  // Check if this operation type is ignored
588
- if (ignoredOps_1.has(change.operationType)) {
1015
+ if (ignoredOps_1.has(change_1.operationType)) {
589
1016
  return [2 /*return*/];
590
1017
  }
591
- entry = (0, registry_1.findRegistryEntryByCollection)(collectionName);
592
- if (!entry) {
593
- // Not a registered realtime model — skip
1018
+ entry = (0, registry_2.findRegistryEntryByCollection)(collectionName);
1019
+ syncEntry_1 = (0, registry_1.findSyncEntryByCollectionName)(collectionName);
1020
+ if (!entry && !syncEntry_1) {
1021
+ // Not a registered realtime or sync model — skip
594
1022
  logDebug("[realtime] No registry entry for collection: ".concat(collectionName));
595
1023
  return [2 /*return*/];
596
1024
  }
597
- method = (0, exports.mapOperationType)(change.operationType, change, entry.config.methods);
598
- if (!method) {
599
- return [2 /*return*/];
600
- }
601
- // Check if this method is enabled for this model
602
- if (!entry.config.methods.includes(method)) {
603
- logDebug("[realtime] Method ".concat(method, " not enabled for ").concat(entry.modelName));
604
- return [2 /*return*/];
605
- }
606
- fullDocument = change.operationType === "delete" ? undefined : change.fullDocument;
607
- isHardDelete = method === "delete" && !fullDocument;
608
- rooms = void 0;
609
- if (isHardDelete) {
610
- // Hard delete: no fullDocument, so we can't resolve owner/custom rooms.
611
- // For owner strategy we cannot safely fan out to a model room without
612
- // leaking deletes across users — admins still receive the event via the
613
- // admin room and any document-specific subscribers via document rooms.
614
- if (entry.config.roomStrategy === "owner") {
615
- rooms = ["admin"];
616
- }
617
- else if (entry.config.roomStrategy === "broadcast") {
618
- rooms = ["authenticated"];
619
- }
620
- else {
621
- collectionTag = getCollectionTag(entry.routePath);
622
- rooms = ["model:".concat(collectionTag)];
623
- }
624
- }
625
- else {
626
- rooms = (0, exports.resolveRooms)(entry, fullDocument !== null && fullDocument !== void 0 ? fullDocument : {}, method);
627
- }
628
- collection = getCollectionTag(entry.routePath);
629
- event_1 = __assign({ collection: collection, id: docId, method: method, model: entry.modelName, timestamp: luxon_1.DateTime.now().toMillis() }, (change.operationType === "update" && ((_e = change.updateDescription) === null || _e === void 0 ? void 0 : _e.updatedFields)
630
- ? { updatedFields: Object.keys(change.updateDescription.updatedFields) }
631
- : {}));
632
- _f.label = 1;
1025
+ if (!entry) return [3 /*break*/, 2];
1026
+ return [4 /*yield*/, processRealtimeChange({ change: change_1, docId: docId_1, entry: entry, io: io, logDebug: logDebug, logInfo: logInfo })];
633
1027
  case 1:
634
- _f.trys.push([1, 6, 7, 8]);
635
- rooms_1 = __values(rooms), rooms_1_1 = rooms_1.next();
636
- _f.label = 2;
1028
+ _d.sent();
1029
+ _d.label = 2;
637
1030
  case 2:
638
- if (!!rooms_1_1.done) return [3 /*break*/, 5];
639
- room = rooms_1_1.value;
640
- return [4 /*yield*/, (0, exports.emitToAuthorizedRoom)(io, room, event_1, entry, fullDocument, logDebug)];
641
- case 3:
642
- _f.sent();
643
- _f.label = 4;
644
- case 4:
645
- rooms_1_1 = rooms_1.next();
646
- return [3 /*break*/, 2];
647
- case 5: return [3 /*break*/, 8];
648
- case 6:
649
- e_4_1 = _f.sent();
650
- e_4 = { error: e_4_1 };
651
- return [3 /*break*/, 8];
652
- case 7:
653
- try {
654
- if (rooms_1_1 && !rooms_1_1.done && (_a = rooms_1.return)) _a.call(rooms_1);
1031
+ if (syncEntry_1) {
1032
+ // C8: serialize per entity so same-doc deltas emit in change-stream order;
1033
+ // different docs still dispatch concurrently. See serializePerEntity + the
1034
+ // per-entity LWW-by-seq contract documented in sync/types.ts (SyncDelta).
1035
+ serializePerEntity("".concat(collectionName, ":").concat(docId_1), function () { return __awaiter(void 0, void 0, void 0, function () {
1036
+ var error_5;
1037
+ return __generator(this, function (_a) {
1038
+ switch (_a.label) {
1039
+ case 0:
1040
+ _a.trys.push([0, 2, , 3]);
1041
+ return [4 /*yield*/, (0, exports.emitSyncDeltaForChange)({ change: change_1, docId: docId_1, entry: syncEntry_1, io: io, logDebug: logDebug })];
1042
+ case 1:
1043
+ _a.sent();
1044
+ return [3 /*break*/, 3];
1045
+ case 2:
1046
+ error_5 = _a.sent();
1047
+ logger_1.logger.error("[sync] Error emitting sync delta: ".concat(error_5));
1048
+ Sentry.captureException(error_5);
1049
+ return [3 /*break*/, 3];
1050
+ case 3: return [2 /*return*/];
1051
+ }
1052
+ });
1053
+ }); });
655
1054
  }
656
- finally { if (e_4) throw e_4.error; }
657
- return [7 /*endfinally*/];
658
- case 8:
659
- // Emit to document-specific and query rooms
660
- return [4 /*yield*/, (0, exports.emitToDocumentAndQueryRooms)(io, collection, event_1, fullDocument, logDebug, entry)];
661
- case 9:
662
- // Emit to document-specific and query rooms
663
- _f.sent();
664
- logDebug("[realtime] Emitted ".concat(method, " for ").concat(entry.modelName, "/").concat(docId, " to rooms: ").concat(rooms.join(", ")));
665
- metadata = {
666
- collection: event_1.collection,
667
- id: event_1.id,
668
- method: event_1.method,
669
- model: event_1.model,
670
- timestamp: event_1.timestamp,
671
- };
672
- if (event_1.updatedFields) {
673
- metadata.updatedFields = event_1.updatedFields;
674
- }
675
- logInfo("[realtime] sync event: ".concat(JSON.stringify(metadata)));
676
- return [3 /*break*/, 11];
677
- case 10:
678
- error_4 = _f.sent();
1055
+ return [3 /*break*/, 4];
1056
+ case 3:
1057
+ error_4 = _d.sent();
679
1058
  logger_1.logger.error("[realtime] Error processing change event: ".concat(error_4));
680
1059
  Sentry.captureException(error_4);
681
- return [3 /*break*/, 11];
682
- case 11: return [2 /*return*/];
1060
+ return [3 /*break*/, 4];
1061
+ case 4: return [2 /*return*/];
683
1062
  }
684
1063
  });
685
1064
  }); });
686
- changeWatcher.on("error", function (err) {
1065
+ stream.on("error", function (err) {
687
1066
  Sentry.captureException(err);
688
1067
  logger_1.logger.error("[realtime] Change stream error: ".concat((err === null || err === void 0 ? void 0 : err.message) || err));
1068
+ if (isHistoryLostError(err)) {
1069
+ // The oplog no longer contains the resume point, so the gap can never be replayed.
1070
+ // Reopen from NOW and tell clients to re-bootstrap: their cursors may sit below
1071
+ // changes (including deletions) they will otherwise never hear about.
1072
+ lastResumeToken = undefined;
1073
+ logger_1.logger.error("[sync] Change stream history lost; reopening without a resume token and asking " +
1074
+ "clients to resync");
1075
+ var hint = { reason: "history_lost" };
1076
+ io.emit(exports.SYNC_RESYNC_EVENT, hint);
1077
+ }
1078
+ scheduleChangeStreamRestart({ config: config, debug: debug, generation: generation, io: io, reason: "errored" });
689
1079
  });
690
- changeWatcher.on("close", function () {
1080
+ stream.on("close", function () {
691
1081
  logger_1.logger.warn("[realtime] Change stream closed");
1082
+ scheduleChangeStreamRestart({ config: config, debug: debug, generation: generation, io: io, reason: "closed" });
692
1083
  });
693
- changeWatcher.on("end", function () {
1084
+ stream.on("end", function () {
694
1085
  logger_1.logger.warn("[realtime] Change stream ended");
1086
+ scheduleChangeStreamRestart({ config: config, debug: debug, generation: generation, io: io, reason: "ended" });
695
1087
  });
696
1088
  logInfo("[realtime] Change stream watcher initialized successfully");
697
1089
  }
698
1090
  catch (error) {
699
1091
  logger_1.logger.error("[realtime] Failed to initialize change stream watcher: ".concat(error));
700
1092
  Sentry.captureException(error);
1093
+ // A first open must fail startup loudly; a failed REOPEN (the DB is down mid-flight)
1094
+ // has no caller to throw to, so keep retrying with backoff instead.
1095
+ if (isReopen && !watcherStopped) {
1096
+ scheduleChangeStreamRestart({ config: config, debug: debug, generation: generation, io: io, reason: "failed to open" });
1097
+ return;
1098
+ }
701
1099
  throw error;
702
1100
  }
703
1101
  };
704
- exports.startChangeStreamWatcher = startChangeStreamWatcher;
705
1102
  /**
706
- * Stop the change stream watcher.
1103
+ * Stop the change stream watcher and end Task 9.16's restart supervision (a pending
1104
+ * reopen is cancelled, and terminal events from the closing stream are ignored).
707
1105
  */
708
1106
  var stopChangeStreamWatcher = function () { return __awaiter(void 0, void 0, void 0, function () {
709
1107
  return __generator(this, function (_a) {
710
1108
  switch (_a.label) {
711
1109
  case 0:
1110
+ watcherStopped = true;
1111
+ if (restartTimer) {
1112
+ clearTimeout(restartTimer);
1113
+ restartTimer = null;
1114
+ }
1115
+ restartAttempts = 0;
1116
+ lastResumeToken = undefined;
712
1117
  if (!changeWatcher) return [3 /*break*/, 2];
713
1118
  return [4 /*yield*/, changeWatcher.close()];
714
1119
  case 1: