@terreno/api 0.31.0 → 56.0.0-beta.2

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 (232) 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 +121 -26
  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 +47 -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 +83 -27
  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 +54 -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 +899 -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/api.errors.test.ts +32 -23
  66. package/src/api.test.ts +12 -0
  67. package/src/api.ts +87 -296
  68. package/src/auth.test.ts +202 -0
  69. package/src/auth.ts +134 -23
  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/configuration.test.ts +59 -38
  74. package/src/configurationPlugin.test.ts +72 -42
  75. package/src/docLoader.ts +3 -1
  76. package/src/errors.ts +33 -0
  77. package/src/expressServer.test.ts +7 -1
  78. package/src/index.ts +42 -0
  79. package/src/middleware.test.ts +56 -1
  80. package/src/middleware.ts +15 -0
  81. package/src/openApiCompat.ts +99 -45
  82. package/src/permissions.test.ts +101 -17
  83. package/src/permissions.ts +49 -0
  84. package/src/plugins.test.ts +64 -0
  85. package/src/plugins.ts +56 -0
  86. package/src/realtime/changeStreamWatcher.ts +552 -114
  87. package/src/realtime/realtime.test.ts +157 -0
  88. package/src/realtime/realtimeApp.ts +82 -14
  89. package/src/realtime/realtimeAppSetup.test.ts +25 -27
  90. package/src/realtime/sessionRevalidation.test.ts +423 -0
  91. package/src/realtime/sessionRevalidation.ts +273 -0
  92. package/src/realtime/socketAuth.test.ts +355 -0
  93. package/src/realtime/socketAuth.ts +228 -0
  94. package/src/realtime/socketUser.ts +69 -0
  95. package/src/realtime/types.ts +54 -0
  96. package/src/secretProviders.test.ts +7 -1
  97. package/src/secretProviders.ts +15 -3
  98. package/src/secretProvidersGcpClient.test.ts +66 -0
  99. package/src/sync/ensureSyncIndexes.test.ts +144 -0
  100. package/src/sync/executors.test.ts +882 -0
  101. package/src/sync/executors.ts +705 -0
  102. package/src/sync/integration.test.ts +1160 -0
  103. package/src/sync/loadHarness.ts +618 -0
  104. package/src/sync/models.ts +641 -0
  105. package/src/sync/mutationHandler.test.ts +1252 -0
  106. package/src/sync/mutationHandler.ts +850 -0
  107. package/src/sync/registry.ts +232 -0
  108. package/src/sync/routes.ts +812 -0
  109. package/src/sync/scripts/compactTombstones.test.ts +249 -0
  110. package/src/sync/scripts/compactTombstones.ts +145 -0
  111. package/src/sync/serialize.ts +48 -0
  112. package/src/sync/sessionRevalidationIntegration.test.ts +281 -0
  113. package/src/sync/socketHandlers.ts +320 -0
  114. package/src/sync/streams.ts +188 -0
  115. package/src/sync/sync.test.ts +582 -0
  116. package/src/sync/syncApp.ts +44 -0
  117. package/src/sync/syncBatch.ts +219 -0
  118. package/src/sync/syncFrontier.test.ts +494 -0
  119. package/src/sync/syncPhaseC.test.ts +718 -0
  120. package/src/sync/syncRoutes.test.ts +865 -0
  121. package/src/sync/syncSeqPlugin.ts +730 -0
  122. package/src/sync/syncSocket.test.ts +1440 -0
  123. package/src/sync/types.ts +270 -0
  124. package/src/terrenoApp.ts +37 -17
  125. package/src/tests/bunSetup.ts +8 -0
  126. package/tsconfig.json +4 -4
  127. package/tsconfig.typedoc.json +7 -1
  128. package/dist/__tests__/openApiCompat.test.js +0 -317
  129. package/dist/__tests__/versionCheckPlugin.test.d.ts +0 -1
  130. package/dist/__tests__/versionCheckPlugin.test.js +0 -597
  131. package/dist/actions.openApi.test.d.ts +0 -1
  132. package/dist/actions.openApi.test.js +0 -253
  133. package/dist/actions.test.d.ts +0 -1
  134. package/dist/actions.test.js +0 -1087
  135. package/dist/api.arrayOperations.test.d.ts +0 -1
  136. package/dist/api.arrayOperations.test.js +0 -878
  137. package/dist/api.asyncHandler.test.d.ts +0 -1
  138. package/dist/api.asyncHandler.test.js +0 -236
  139. package/dist/api.coverage.test.d.ts +0 -1
  140. package/dist/api.coverage.test.js +0 -192
  141. package/dist/api.errorPaths.test.d.ts +0 -1
  142. package/dist/api.errorPaths.test.js +0 -887
  143. package/dist/api.errors.test.d.ts +0 -1
  144. package/dist/api.errors.test.js +0 -185
  145. package/dist/api.hooks.test.d.ts +0 -1
  146. package/dist/api.hooks.test.js +0 -950
  147. package/dist/api.query.test.d.ts +0 -1
  148. package/dist/api.query.test.js +0 -948
  149. package/dist/api.test.d.ts +0 -1
  150. package/dist/api.test.js +0 -2820
  151. package/dist/auth.compile.test.d.ts +0 -1
  152. package/dist/auth.compile.test.js +0 -201
  153. package/dist/auth.coverage.test.d.ts +0 -1
  154. package/dist/auth.coverage.test.js +0 -202
  155. package/dist/auth.test.d.ts +0 -1
  156. package/dist/auth.test.js +0 -2286
  157. package/dist/betterAuth.test.d.ts +0 -1
  158. package/dist/betterAuth.test.js +0 -181
  159. package/dist/betterAuthApp.test.d.ts +0 -1
  160. package/dist/betterAuthApp.test.js +0 -242
  161. package/dist/betterAuthSetup.test.d.ts +0 -1
  162. package/dist/betterAuthSetup.test.js +0 -762
  163. package/dist/config.test.d.ts +0 -1
  164. package/dist/config.test.js +0 -353
  165. package/dist/configuration.test.d.ts +0 -1
  166. package/dist/configuration.test.js +0 -980
  167. package/dist/configurationPlugin.test.d.ts +0 -1
  168. package/dist/configurationPlugin.test.js +0 -827
  169. package/dist/consentApp.test.d.ts +0 -1
  170. package/dist/consentApp.test.js +0 -1411
  171. package/dist/docLoader.test.d.ts +0 -1
  172. package/dist/docLoader.test.js +0 -137
  173. package/dist/envConfigurationPlugin.test.d.ts +0 -1
  174. package/dist/envConfigurationPlugin.test.js +0 -369
  175. package/dist/errors.test.d.ts +0 -1
  176. package/dist/errors.test.js +0 -607
  177. package/dist/expressServer.test.d.ts +0 -1
  178. package/dist/expressServer.test.js +0 -1249
  179. package/dist/githubAuth.test.d.ts +0 -1
  180. package/dist/githubAuth.test.js +0 -1142
  181. package/dist/httpClient.test.d.ts +0 -1
  182. package/dist/httpClient.test.js +0 -1471
  183. package/dist/logger.test.d.ts +0 -1
  184. package/dist/logger.test.js +0 -685
  185. package/dist/middleware.test.d.ts +0 -1
  186. package/dist/middleware.test.js +0 -241
  187. package/dist/notifiers/googleChatNotifier.test.d.ts +0 -1
  188. package/dist/notifiers/googleChatNotifier.test.js +0 -298
  189. package/dist/notifiers/slackNotifier.test.d.ts +0 -1
  190. package/dist/notifiers/slackNotifier.test.js +0 -297
  191. package/dist/notifiers/zoomNotifier.test.d.ts +0 -1
  192. package/dist/notifiers/zoomNotifier.test.js +0 -373
  193. package/dist/openApi.test.d.ts +0 -1
  194. package/dist/openApi.test.js +0 -620
  195. package/dist/openApiBuilder.test.d.ts +0 -1
  196. package/dist/openApiBuilder.test.js +0 -599
  197. package/dist/openApiEtag.test.d.ts +0 -1
  198. package/dist/openApiEtag.test.js +0 -86
  199. package/dist/openApiValidator.test.d.ts +0 -1
  200. package/dist/openApiValidator.test.js +0 -795
  201. package/dist/permissions.middleware.test.d.ts +0 -1
  202. package/dist/permissions.middleware.test.js +0 -308
  203. package/dist/permissions.test.d.ts +0 -1
  204. package/dist/permissions.test.js +0 -439
  205. package/dist/plugins.test.d.ts +0 -1
  206. package/dist/plugins.test.js +0 -902
  207. package/dist/populate.test.d.ts +0 -1
  208. package/dist/populate.test.js +0 -370
  209. package/dist/realtime/realtime.test.d.ts +0 -10
  210. package/dist/realtime/realtime.test.js +0 -4213
  211. package/dist/realtime/realtimeAppSetup.test.d.ts +0 -1
  212. package/dist/realtime/realtimeAppSetup.test.js +0 -213
  213. package/dist/requestContext.test.d.ts +0 -1
  214. package/dist/requestContext.test.js +0 -412
  215. package/dist/scriptRunner.test.d.ts +0 -1
  216. package/dist/scriptRunner.test.js +0 -386
  217. package/dist/secretProviders.test.d.ts +0 -1
  218. package/dist/secretProviders.test.js +0 -726
  219. package/dist/syncConsents.test.d.ts +0 -1
  220. package/dist/syncConsents.test.js +0 -522
  221. package/dist/terrenoApp.test.d.ts +0 -1
  222. package/dist/terrenoApp.test.js +0 -489
  223. package/dist/tests/bunSetup.d.ts +0 -1
  224. package/dist/tests/fixtures/compileAuthEntry.d.ts +0 -1
  225. package/dist/tests/fixtures/compileAuthEntry.js +0 -37
  226. package/dist/tests/testEnv.d.ts +0 -0
  227. package/dist/tests/testEnv.js +0 -6
  228. package/dist/transformers.test.d.ts +0 -1
  229. package/dist/transformers.test.js +0 -423
  230. package/dist/utils.test.d.ts +0 -1
  231. package/dist/utils.test.js +0 -242
  232. /package/dist/{__tests__/openApiCompat.test.d.ts → sync/loadHarness.d.ts} +0 -0
@@ -0,0 +1,47 @@
1
+ import { type SyncRegistryEntry } from "../registry";
2
+ /**
3
+ * C7 tombstone retention maintenance.
4
+ *
5
+ * Tombstones (soft-deleted documents kept so offline clients learn of deletions) and
6
+ * `SyncScopeMove` markers accumulate forever otherwise. This script hard-deletes both
7
+ * once they are older than the model's `retentionDays` (default 90). It is paired with
8
+ * the client-side rule that a cursor older than the snapshot response's
9
+ * `oldestRetainedSeq` triggers a full re-bootstrap of that stream — so compacting
10
+ * tombstones a client has already seen is safe, and a client that missed them recovers
11
+ * by re-bootstrapping rather than silently keeping stale data.
12
+ *
13
+ * Run as a periodic maintenance job (cron), NOT on the request path. Requires an active
14
+ * Mongoose connection.
15
+ */
16
+ /** Default retention window when a model does not override `sync.retentionDays`. */
17
+ export declare const DEFAULT_TOMBSTONE_RETENTION_DAYS = 90;
18
+ export interface CompactTombstonesResult {
19
+ /** Per-collection counts of hard-deleted tombstones and scope-move markers. */
20
+ byCollection: Record<string, {
21
+ tombstones: number;
22
+ markers: number;
23
+ }>;
24
+ /** Total tombstones hard-deleted. */
25
+ totalTombstones: number;
26
+ /** Total scope-move markers hard-deleted. */
27
+ totalMarkers: number;
28
+ }
29
+ /**
30
+ * Compact tombstones and scope-move markers for one registry entry: hard-delete
31
+ * soft-deleted documents whose `updated` (falling back to `created`) predates the
32
+ * retention window, plus scope-move markers older than the window.
33
+ *
34
+ * Task 9.15: the rows are READ before they are deleted so the highest reaped seq per
35
+ * stream can be recorded as that stream's `compactedThroughSeq` watermark. That watermark
36
+ * is the snapshot's `oldestRetainedSeq`, and it is the only signal a client has that its
37
+ * cursor sits below a deletion it can no longer be told about. Deleting without raising it
38
+ * silently strands the client on stale data. This is also why `SyncScopeMove` no longer
39
+ * carries a TTL index: an expiry that fires outside this function reaps rows with no
40
+ * watermark update and ignores the model's `retentionDays`.
41
+ */
42
+ export declare const compactEntryTombstones: (entry: SyncRegistryEntry) => Promise<{
43
+ tombstones: number;
44
+ markers: number;
45
+ }>;
46
+ /** Compact tombstones across every registered sync model. */
47
+ export declare const compactTombstones: () => Promise<CompactTombstonesResult>;
@@ -0,0 +1,270 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ 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);
13
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ 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;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __values = (this && this.__values) || function(o) {
39
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
40
+ if (m) return m.call(o);
41
+ if (o && typeof o.length === "number") return {
42
+ next: function () {
43
+ if (o && i >= o.length) o = void 0;
44
+ return { value: o && o[i++], done: !o };
45
+ }
46
+ };
47
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
48
+ };
49
+ var __read = (this && this.__read) || function (o, n) {
50
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
51
+ if (!m) return o;
52
+ var i = m.call(o), r, ar = [], e;
53
+ try {
54
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
+ }
56
+ catch (error) { e = { error: error }; }
57
+ finally {
58
+ try {
59
+ if (r && !r.done && (m = i["return"])) m.call(i);
60
+ }
61
+ finally { if (e) throw e.error; }
62
+ }
63
+ return ar;
64
+ };
65
+ var __importDefault = (this && this.__importDefault) || function (mod) {
66
+ return (mod && mod.__esModule) ? mod : { "default": mod };
67
+ };
68
+ Object.defineProperty(exports, "__esModule", { value: true });
69
+ exports.compactTombstones = exports.compactEntryTombstones = exports.DEFAULT_TOMBSTONE_RETENTION_DAYS = void 0;
70
+ // noExplicitAny: operates generically across registered sync models
71
+ // biome-ignore-all lint/suspicious/noExplicitAny: operates generically across registered sync models
72
+ var luxon_1 = require("luxon");
73
+ var mongoose_1 = __importDefault(require("mongoose"));
74
+ var logger_1 = require("../../logger");
75
+ var models_1 = require("../models");
76
+ var registry_1 = require("../registry");
77
+ var streams_1 = require("../streams");
78
+ /**
79
+ * C7 tombstone retention maintenance.
80
+ *
81
+ * Tombstones (soft-deleted documents kept so offline clients learn of deletions) and
82
+ * `SyncScopeMove` markers accumulate forever otherwise. This script hard-deletes both
83
+ * once they are older than the model's `retentionDays` (default 90). It is paired with
84
+ * the client-side rule that a cursor older than the snapshot response's
85
+ * `oldestRetainedSeq` triggers a full re-bootstrap of that stream — so compacting
86
+ * tombstones a client has already seen is safe, and a client that missed them recovers
87
+ * by re-bootstrapping rather than silently keeping stale data.
88
+ *
89
+ * Run as a periodic maintenance job (cron), NOT on the request path. Requires an active
90
+ * Mongoose connection.
91
+ */
92
+ /** Default retention window when a model does not override `sync.retentionDays`. */
93
+ exports.DEFAULT_TOMBSTONE_RETENTION_DAYS = 90;
94
+ var retentionCutoff = function (entry) {
95
+ var _a;
96
+ var days = (_a = entry.config.retentionDays) !== null && _a !== void 0 ? _a : exports.DEFAULT_TOMBSTONE_RETENTION_DAYS;
97
+ return luxon_1.DateTime.now().minus({ days: days }).toJSDate();
98
+ };
99
+ /**
100
+ * Compact tombstones and scope-move markers for one registry entry: hard-delete
101
+ * soft-deleted documents whose `updated` (falling back to `created`) predates the
102
+ * retention window, plus scope-move markers older than the window.
103
+ *
104
+ * Task 9.15: the rows are READ before they are deleted so the highest reaped seq per
105
+ * stream can be recorded as that stream's `compactedThroughSeq` watermark. That watermark
106
+ * is the snapshot's `oldestRetainedSeq`, and it is the only signal a client has that its
107
+ * cursor sits below a deletion it can no longer be told about. Deleting without raising it
108
+ * silently strands the client on stale data. This is also why `SyncScopeMove` no longer
109
+ * carries a TTL index: an expiry that fires outside this function reaps rows with no
110
+ * watermark update and ignores the model's `retentionDays`.
111
+ */
112
+ var compactEntryTombstones = function (entry) { return __awaiter(void 0, void 0, void 0, function () {
113
+ var cutoff, model, scopeField, tombstoneFilter, markerFilter, watermarks, raiseWatermark, isCustomScope, projection, doomedTombstones, doomedTombstones_1, doomedTombstones_1_1, row, doomedMarkers, doomedMarkers_1, doomedMarkers_1_1, marker, tombstoneResult, markerResult, watermarks_1, watermarks_1_1, _a, stream, seq, e_1_1;
114
+ var _b, e_2, _c, e_3, _d, e_1, _e;
115
+ var _f, _g;
116
+ return __generator(this, function (_h) {
117
+ switch (_h.label) {
118
+ case 0:
119
+ cutoff = retentionCutoff(entry);
120
+ model = mongoose_1.default.model(entry.modelName);
121
+ scopeField = (0, streams_1.getScopeField)(entry.config.scope);
122
+ tombstoneFilter = {
123
+ $or: [{ updated: { $lt: cutoff } }, { created: { $lt: cutoff }, updated: { $exists: false } }],
124
+ deleted: true,
125
+ };
126
+ markerFilter = { collectionTag: entry.collectionTag, created: { $lt: cutoff } };
127
+ watermarks = new Map();
128
+ raiseWatermark = function (stream, seq) {
129
+ var _a;
130
+ if (typeof seq !== "number" || seq <= 0) {
131
+ return;
132
+ }
133
+ if (seq > ((_a = watermarks.get(stream)) !== null && _a !== void 0 ? _a : 0)) {
134
+ watermarks.set(stream, seq);
135
+ }
136
+ };
137
+ isCustomScope = typeof entry.config.scope === "function";
138
+ projection = scopeField
139
+ ? (_b = { _id: 1, _syncSeq: 1 }, _b[scopeField] = 1, _b) : isCustomScope
140
+ ? undefined
141
+ : { _id: 1, _syncSeq: 1 };
142
+ return [4 /*yield*/, model.collection
143
+ .find(tombstoneFilter, projection ? { projection: projection } : {})
144
+ .toArray()];
145
+ case 1:
146
+ doomedTombstones = _h.sent();
147
+ try {
148
+ for (doomedTombstones_1 = __values(doomedTombstones), doomedTombstones_1_1 = doomedTombstones_1.next(); !doomedTombstones_1_1.done; doomedTombstones_1_1 = doomedTombstones_1.next()) {
149
+ row = doomedTombstones_1_1.value;
150
+ raiseWatermark((0, streams_1.resolveStreamForDoc)({
151
+ collectionTag: entry.collectionTag,
152
+ doc: row,
153
+ scope: entry.config.scope,
154
+ }), row._syncSeq);
155
+ }
156
+ }
157
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
158
+ finally {
159
+ try {
160
+ if (doomedTombstones_1_1 && !doomedTombstones_1_1.done && (_c = doomedTombstones_1.return)) _c.call(doomedTombstones_1);
161
+ }
162
+ finally { if (e_2) throw e_2.error; }
163
+ }
164
+ return [4 /*yield*/, models_1.SyncScopeMove.collection
165
+ .find(markerFilter, { projection: { _id: 1, fromStream: 1, seq: 1 } })
166
+ .toArray()];
167
+ case 2:
168
+ doomedMarkers = _h.sent();
169
+ try {
170
+ for (doomedMarkers_1 = __values(doomedMarkers), doomedMarkers_1_1 = doomedMarkers_1.next(); !doomedMarkers_1_1.done; doomedMarkers_1_1 = doomedMarkers_1.next()) {
171
+ marker = doomedMarkers_1_1.value;
172
+ raiseWatermark(String(marker.fromStream), marker.seq);
173
+ }
174
+ }
175
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
176
+ finally {
177
+ try {
178
+ if (doomedMarkers_1_1 && !doomedMarkers_1_1.done && (_d = doomedMarkers_1.return)) _d.call(doomedMarkers_1);
179
+ }
180
+ finally { if (e_3) throw e_3.error; }
181
+ }
182
+ return [4 /*yield*/, model.collection.deleteMany(tombstoneFilter)];
183
+ case 3:
184
+ tombstoneResult = _h.sent();
185
+ return [4 /*yield*/, models_1.SyncScopeMove.collection.deleteMany(markerFilter)];
186
+ case 4:
187
+ markerResult = _h.sent();
188
+ _h.label = 5;
189
+ case 5:
190
+ _h.trys.push([5, 10, 11, 12]);
191
+ watermarks_1 = __values(watermarks), watermarks_1_1 = watermarks_1.next();
192
+ _h.label = 6;
193
+ case 6:
194
+ if (!!watermarks_1_1.done) return [3 /*break*/, 9];
195
+ _a = __read(watermarks_1_1.value, 2), stream = _a[0], seq = _a[1];
196
+ return [4 /*yield*/, (0, models_1.recordCompactedThroughSeq)({ seq: seq, stream: stream })];
197
+ case 7:
198
+ _h.sent();
199
+ _h.label = 8;
200
+ case 8:
201
+ watermarks_1_1 = watermarks_1.next();
202
+ return [3 /*break*/, 6];
203
+ case 9: return [3 /*break*/, 12];
204
+ case 10:
205
+ e_1_1 = _h.sent();
206
+ e_1 = { error: e_1_1 };
207
+ return [3 /*break*/, 12];
208
+ case 11:
209
+ try {
210
+ if (watermarks_1_1 && !watermarks_1_1.done && (_e = watermarks_1.return)) _e.call(watermarks_1);
211
+ }
212
+ finally { if (e_1) throw e_1.error; }
213
+ return [7 /*endfinally*/];
214
+ case 12: return [2 /*return*/, {
215
+ markers: (_f = markerResult.deletedCount) !== null && _f !== void 0 ? _f : 0,
216
+ tombstones: (_g = tombstoneResult.deletedCount) !== null && _g !== void 0 ? _g : 0,
217
+ }];
218
+ }
219
+ });
220
+ }); };
221
+ exports.compactEntryTombstones = compactEntryTombstones;
222
+ /** Compact tombstones across every registered sync model. */
223
+ var compactTombstones = function () { return __awaiter(void 0, void 0, void 0, function () {
224
+ var byCollection, totalTombstones, totalMarkers, _a, _b, entry, counts, e_4_1;
225
+ var e_4, _c;
226
+ return __generator(this, function (_d) {
227
+ switch (_d.label) {
228
+ case 0:
229
+ byCollection = {};
230
+ totalTombstones = 0;
231
+ totalMarkers = 0;
232
+ _d.label = 1;
233
+ case 1:
234
+ _d.trys.push([1, 6, 7, 8]);
235
+ _a = __values((0, registry_1.getSyncRegistry)()), _b = _a.next();
236
+ _d.label = 2;
237
+ case 2:
238
+ if (!!_b.done) return [3 /*break*/, 5];
239
+ entry = _b.value;
240
+ return [4 /*yield*/, (0, exports.compactEntryTombstones)(entry)];
241
+ case 3:
242
+ counts = _d.sent();
243
+ byCollection[entry.collectionTag] = counts;
244
+ totalTombstones += counts.tombstones;
245
+ totalMarkers += counts.markers;
246
+ logger_1.logger.info("[sync] Compacted tombstones", {
247
+ collection: entry.collectionTag,
248
+ markers: counts.markers,
249
+ tombstones: counts.tombstones,
250
+ });
251
+ _d.label = 4;
252
+ case 4:
253
+ _b = _a.next();
254
+ return [3 /*break*/, 2];
255
+ case 5: return [3 /*break*/, 8];
256
+ case 6:
257
+ e_4_1 = _d.sent();
258
+ e_4 = { error: e_4_1 };
259
+ return [3 /*break*/, 8];
260
+ case 7:
261
+ try {
262
+ if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
263
+ }
264
+ finally { if (e_4) throw e_4.error; }
265
+ return [7 /*endfinally*/];
266
+ case 8: return [2 /*return*/, { byCollection: byCollection, totalMarkers: totalMarkers, totalTombstones: totalTombstones }];
267
+ }
268
+ });
269
+ }); };
270
+ exports.compactTombstones = compactTombstones;
@@ -0,0 +1,20 @@
1
+ import type express from "express";
2
+ import type { SyncRegistryEntry } from "./registry";
3
+ import type { SyncMutationOperation } from "./types";
4
+ /**
5
+ * Shared serializer for sync payloads (snapshot entities, conflict server docs, and
6
+ * change-stream deltas), applying the fallback chain:
7
+ *
8
+ * sync `responseHandler` > modelRouter `responseHandler` > `toJSON()` > raw object.
9
+ *
10
+ * Lives in its own module so both the HTTP routes/mutation handler and the realtime
11
+ * change-stream watcher can import it without deepening the routes -> mutationHandler
12
+ * import cycle. Accepts hydrated Mongoose documents and the raw BSON objects change
13
+ * streams deliver (which have neither `toObject` nor `toJSON`).
14
+ */
15
+ export declare const serializeSyncPayload: ({ entry, doc, method, req, }: {
16
+ entry: SyncRegistryEntry;
17
+ doc: Record<string, unknown>;
18
+ method?: SyncMutationOperation;
19
+ req: express.Request;
20
+ }) => Promise<unknown>;
@@ -1,37 +1,4 @@
1
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
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
36
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
37
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -69,39 +36,39 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
69
36
  }
70
37
  };
71
38
  Object.defineProperty(exports, "__esModule", { value: true });
72
- var test_1 = require("@terreno/test");
73
- var useFixtureCache = process.env.TERRENO_TEST_USE_FIXTURE_CACHE === "true";
74
- if (useFixtureCache) {
75
- (0, test_1.registerBackendPreload)({
76
- connectMongoInBeforeAll: true,
77
- loadTestDataFromCache: function () { return __awaiter(void 0, void 0, void 0, function () {
78
- var loadTestDataFromCache;
79
- return __generator(this, function (_a) {
80
- switch (_a.label) {
81
- case 0: return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require("./mongoTestSetup")); })];
82
- case 1:
83
- loadTestDataFromCache = (_a.sent()).loadTestDataFromCache;
84
- return [4 /*yield*/, loadTestDataFromCache()];
85
- case 2:
86
- _a.sent();
87
- return [2 /*return*/];
88
- }
89
- });
90
- }); },
91
- mongo: {
92
- baseDatabaseName: "terrenoTest_base",
93
- useReplSet: true,
94
- },
95
- testEnv: {
96
- tokenIssuer: "terreno-api.test",
97
- },
98
- useTransactions: true,
99
- });
100
- }
101
- else {
102
- (0, test_1.registerSimpleMongoPreload)({
103
- testEnv: {
104
- tokenIssuer: "terreno-api.test",
105
- },
39
+ exports.serializeSyncPayload = void 0;
40
+ /**
41
+ * Shared serializer for sync payloads (snapshot entities, conflict server docs, and
42
+ * change-stream deltas), applying the fallback chain:
43
+ *
44
+ * sync `responseHandler` > modelRouter `responseHandler` > `toJSON()` > raw object.
45
+ *
46
+ * Lives in its own module so both the HTTP routes/mutation handler and the realtime
47
+ * change-stream watcher can import it without deepening the routes -> mutationHandler
48
+ * import cycle. Accepts hydrated Mongoose documents and the raw BSON objects change
49
+ * streams deliver (which have neither `toObject` nor `toJSON`).
50
+ */
51
+ var serializeSyncPayload = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
52
+ var plain;
53
+ var entry = _b.entry, doc = _b.doc, _c = _b.method, method = _c === void 0 ? "update" : _c, req = _b.req;
54
+ return __generator(this, function (_d) {
55
+ plain = typeof doc.toObject === "function"
56
+ ? doc.toObject()
57
+ : doc;
58
+ if (entry.config.responseHandler) {
59
+ return [2 /*return*/, entry.config.responseHandler(plain, method)];
60
+ }
61
+ if (entry.options.responseHandler) {
62
+ // C8: sync serializes a SINGLE entity (snapshot row, conflict doc, or delta), so the
63
+ // modelRouter responseHandler must run with single-entity `"read"` semantics — not
64
+ // the hardcoded `"list"`, which can trigger list-only shaping (field trimming, array
65
+ // wrapping) that corrupts a single-doc payload.
66
+ return [2 /*return*/, entry.options.responseHandler(doc, "read", req, entry.options)];
67
+ }
68
+ if (typeof doc.toJSON === "function") {
69
+ return [2 /*return*/, doc.toJSON()];
70
+ }
71
+ return [2 /*return*/, plain];
106
72
  });
107
- }
73
+ }); };
74
+ exports.serializeSyncPayload = serializeSyncPayload;
@@ -0,0 +1,54 @@
1
+ import type { Server } from "socket.io";
2
+ import { type SocketWithDecodedToken } from "../realtime/socketUser";
3
+ import type { SyncAppOptions } from "./routes";
4
+ /**
5
+ * Socket handlers for the SyncDB local-first protocol:
6
+ *
7
+ * - `sync:subscribe {collections}` / `sync:unsubscribe {collections}` — resolves the
8
+ * caller's streams from the sync registry scope config and joins/leaves `sync:{stream}`
9
+ * rooms. Owner scopes always use the socket's own userId (never a client-supplied one);
10
+ * tenant and custom scopes resolve stream values via `SyncAppOptions.getUserScopes`.
11
+ * Sync deltas fan out through these dedicated `sync:{stream}` rooms rather than the
12
+ * legacy realtime rooms so the two event families never overlap.
13
+ * - `sync:mutate` — applies a mutation through `applySyncMutation` and replies with
14
+ * `sync:ack {mutationId, id, seq}` or `sync:nack {mutationId, code, ...}`; when the
15
+ * client supplied a Socket.io ack callback it also receives `{ack}` / `{nack}`.
16
+ * - `sync:error {collection, message}` — emitted for per-collection subscribe failures
17
+ * (unknown collection, permission denied, missing scope resolver, cap exceeded).
18
+ *
19
+ * ## Wiring
20
+ *
21
+ * `RealtimeApp` installs these handlers on every connection with the `SyncAppOptions`
22
+ * attached to the same Express application, so multiple app instances stay isolated.
23
+ */
24
+ /** Maximum distinct collection subscriptions per socket (DoS protection). */
25
+ export declare const MAX_SYNC_COLLECTION_SUBSCRIPTIONS = 50;
26
+ /**
27
+ * C8: maximum entries accepted in a single `sync:subscribe`/`sync:unsubscribe`
28
+ * `collections` array — checked BEFORE iterating so an oversized array is rejected
29
+ * with no per-entry work.
30
+ */
31
+ export declare const MAX_SYNC_SUBSCRIBE_ARRAY_LENGTH = 100;
32
+ /** The Socket.io room a sync stream fans out through. */
33
+ export declare const syncRoomForStream: (stream: string) => string;
34
+ /**
35
+ * Minimal shape this module requires from a Socket.io socket. Matches
36
+ * `RealtimeSocketLike` structurally so tests can drive handlers with a mock socket.
37
+ */
38
+ export interface SyncSocketLike extends SocketWithDecodedToken {
39
+ id: string;
40
+ join: (room: string) => Promise<void> | void;
41
+ leave: (room: string) => Promise<void> | void;
42
+ emit: (event: string, payload: unknown) => void;
43
+ on: (event: string, handler: (...args: any[]) => any) => void;
44
+ }
45
+ /**
46
+ * Install the sync socket handlers on a single socket. Wired into RealtimeApp's
47
+ * connection handler alongside `installRealtimeSocketHandlers`.
48
+ *
49
+ * The `_io` server parameter is accepted for signature symmetry with future fan-out
50
+ * needs; the current handlers only act on the connecting socket.
51
+ */
52
+ export declare const installSyncSocketHandlers: (_io: Server | null, socket: SyncSocketLike, options?: SyncAppOptions, handlerOptions?: {
53
+ logInfo?: (msg: string) => void;
54
+ }) => void;