@hot-updater/plugin-core 1.0.0-rc.15 → 1.0.0-rc.17

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 (151) hide show
  1. package/dist/coreApi.d.cts +131 -0
  2. package/dist/coreApi.d.mts +131 -0
  3. package/dist/database/adapter.cjs +46 -0
  4. package/dist/database/adapter.d.cts +183 -0
  5. package/dist/database/adapter.d.mts +183 -0
  6. package/dist/database/adapter.mjs +38 -0
  7. package/dist/{insightsDistinctSummary.mjs → database/distinct.cjs} +25 -11
  8. package/dist/database/distinct.d.cts +16 -0
  9. package/dist/database/distinct.d.mts +16 -0
  10. package/dist/{insightsDistinctSummary.cjs → database/distinct.mjs} +21 -15
  11. package/dist/database/memoryAdapter.cjs +115 -0
  12. package/dist/database/memoryAdapter.d.cts +16 -0
  13. package/dist/database/memoryAdapter.d.mts +16 -0
  14. package/dist/database/memoryAdapter.mjs +115 -0
  15. package/dist/database/values.cjs +110 -0
  16. package/dist/database/values.d.cts +30 -0
  17. package/dist/database/values.d.mts +30 -0
  18. package/dist/database/values.mjs +100 -0
  19. package/dist/database/verifyAdapter.cjs +136 -0
  20. package/dist/database/verifyAdapter.d.cts +29 -0
  21. package/dist/database/verifyAdapter.d.mts +29 -0
  22. package/dist/database/verifyAdapter.mjs +135 -0
  23. package/dist/databaseErrors.cjs +30 -0
  24. package/dist/databaseErrors.d.cts +21 -0
  25. package/dist/databaseErrors.d.mts +21 -0
  26. package/dist/databaseErrors.mjs +28 -0
  27. package/dist/databaseJsonValue.cjs +0 -2
  28. package/dist/databaseJsonValue.d.cts +3 -3
  29. package/dist/databaseJsonValue.d.mts +3 -3
  30. package/dist/databaseJsonValue.mjs +1 -2
  31. package/dist/databaseMetadata.cjs +2 -2
  32. package/dist/databaseMetadata.mjs +1 -1
  33. package/dist/index.cjs +9 -39
  34. package/dist/index.d.cts +13 -19
  35. package/dist/index.d.mts +13 -19
  36. package/dist/index.mjs +7 -14
  37. package/dist/internal.cjs +37 -20
  38. package/dist/internal.d.cts +11 -12
  39. package/dist/internal.d.mts +11 -12
  40. package/dist/internal.mjs +8 -6
  41. package/dist/releaseCatalogCompiler.cjs +1 -1
  42. package/dist/releaseCatalogCompiler.mjs +2 -2
  43. package/dist/releaseCatalogMutation.cjs +0 -292
  44. package/dist/releaseCatalogMutation.d.cts +4 -45
  45. package/dist/releaseCatalogMutation.d.mts +4 -45
  46. package/dist/releaseCatalogMutation.mjs +1 -288
  47. package/dist/releaseManagement.cjs +0 -221
  48. package/dist/releaseManagement.d.cts +4 -28
  49. package/dist/releaseManagement.d.mts +4 -28
  50. package/dist/releaseManagement.mjs +1 -216
  51. package/dist/types/databaseConfig.cjs +5 -0
  52. package/dist/types/databaseConfig.d.cts +61 -0
  53. package/dist/types/databaseConfig.d.mts +61 -0
  54. package/dist/types/databaseConfig.mjs +5 -0
  55. package/dist/types/databaseOperations.d.cts +5 -174
  56. package/dist/types/databaseOperations.d.mts +5 -174
  57. package/dist/types/databaseRows.d.cts +1 -53
  58. package/dist/types/databaseRows.d.mts +1 -53
  59. package/dist/types/index.cjs +2 -0
  60. package/dist/types/index.d.cts +8 -74
  61. package/dist/types/index.d.mts +8 -74
  62. package/dist/types/index.mjs +2 -1
  63. package/dist/types/internal.d.cts +4 -8
  64. package/dist/types/internal.d.mts +4 -8
  65. package/dist/types/models.d.cts +14 -0
  66. package/dist/types/models.d.mts +14 -0
  67. package/dist/types/public.d.cts +4 -3
  68. package/dist/types/public.d.mts +4 -3
  69. package/package.json +1 -1
  70. package/dist/calculatePagination.cjs +0 -25
  71. package/dist/calculatePagination.d.cts +0 -12
  72. package/dist/calculatePagination.d.mts +0 -12
  73. package/dist/calculatePagination.mjs +0 -25
  74. package/dist/createDatabasePlugin.cjs +0 -875
  75. package/dist/createDatabasePlugin.d.cts +0 -28
  76. package/dist/createDatabasePlugin.d.mts +0 -28
  77. package/dist/createDatabasePlugin.mjs +0 -871
  78. package/dist/databaseClient.cjs +0 -117
  79. package/dist/databaseClient.d.cts +0 -32
  80. package/dist/databaseClient.d.mts +0 -32
  81. package/dist/databaseClient.mjs +0 -114
  82. package/dist/databaseClientReads.cjs +0 -115
  83. package/dist/databaseClientReads.mjs +0 -113
  84. package/dist/databaseClientUpdates.cjs +0 -37
  85. package/dist/databaseClientUpdates.d.cts +0 -11
  86. package/dist/databaseClientUpdates.d.mts +0 -11
  87. package/dist/databaseClientUpdates.mjs +0 -35
  88. package/dist/databasePluginCrud.cjs +0 -87
  89. package/dist/databasePluginCrud.d.cts +0 -4
  90. package/dist/databasePluginCrud.d.mts +0 -4
  91. package/dist/databasePluginCrud.mjs +0 -86
  92. package/dist/databasePluginCrudValidation.d.cts +0 -6
  93. package/dist/databasePluginCrudValidation.d.mts +0 -6
  94. package/dist/databasePluginCrudValidationErrors.cjs +0 -11
  95. package/dist/databasePluginCrudValidationErrors.d.cts +0 -9
  96. package/dist/databasePluginCrudValidationErrors.d.mts +0 -9
  97. package/dist/databasePluginCrudValidationErrors.mjs +0 -11
  98. package/dist/databasePluginCrudValidationFields.cjs +0 -284
  99. package/dist/databasePluginCrudValidationFields.d.cts +0 -1
  100. package/dist/databasePluginCrudValidationFields.d.mts +0 -1
  101. package/dist/databasePluginCrudValidationFields.mjs +0 -272
  102. package/dist/databasePluginCrudValidationMutations.cjs +0 -88
  103. package/dist/databasePluginCrudValidationMutations.d.cts +0 -1
  104. package/dist/databasePluginCrudValidationMutations.d.mts +0 -1
  105. package/dist/databasePluginCrudValidationMutations.mjs +0 -82
  106. package/dist/databasePluginCrudValidationQueries.cjs +0 -103
  107. package/dist/databasePluginCrudValidationQueries.d.cts +0 -2
  108. package/dist/databasePluginCrudValidationQueries.d.mts +0 -2
  109. package/dist/databasePluginCrudValidationQueries.mjs +0 -97
  110. package/dist/databasePluginCrudValidationRows.cjs +0 -55
  111. package/dist/databasePluginCrudValidationRows.d.cts +0 -1
  112. package/dist/databasePluginCrudValidationRows.d.mts +0 -1
  113. package/dist/databasePluginCrudValidationRows.mjs +0 -53
  114. package/dist/databasePluginTransaction.cjs +0 -7
  115. package/dist/databasePluginTransaction.mjs +0 -7
  116. package/dist/insightsContract.cjs +0 -192
  117. package/dist/insightsContract.d.cts +0 -9
  118. package/dist/insightsContract.d.mts +0 -9
  119. package/dist/insightsContract.mjs +0 -189
  120. package/dist/insightsDistinctSummary.d.cts +0 -12
  121. package/dist/insightsDistinctSummary.d.mts +0 -12
  122. package/dist/insightsLatestQueries.cjs +0 -53
  123. package/dist/insightsLatestQueries.d.cts +0 -13
  124. package/dist/insightsLatestQueries.d.mts +0 -13
  125. package/dist/insightsLatestQueries.mjs +0 -52
  126. package/dist/insightsOverview.cjs +0 -139
  127. package/dist/insightsOverview.d.cts +0 -40
  128. package/dist/insightsOverview.d.mts +0 -40
  129. package/dist/insightsOverview.mjs +0 -135
  130. package/dist/paginateBundles.cjs +0 -53
  131. package/dist/paginateBundles.d.cts +0 -13
  132. package/dist/paginateBundles.d.mts +0 -13
  133. package/dist/paginateBundles.mjs +0 -53
  134. package/dist/queryBundles.cjs +0 -34
  135. package/dist/queryBundles.d.cts +0 -9
  136. package/dist/queryBundles.d.mts +0 -9
  137. package/dist/queryBundles.mjs +0 -32
  138. package/dist/requestBundleCache.cjs +0 -22
  139. package/dist/requestBundleCache.d.cts +0 -8
  140. package/dist/requestBundleCache.d.mts +0 -8
  141. package/dist/requestBundleCache.mjs +0 -22
  142. package/dist/types/database.d.cts +0 -5
  143. package/dist/types/database.d.mts +0 -5
  144. package/dist/types/databaseFields.cjs +0 -85
  145. package/dist/types/databaseFields.d.cts +0 -12
  146. package/dist/types/databaseFields.d.mts +0 -12
  147. package/dist/types/databaseFields.mjs +0 -85
  148. package/dist/types/databasePlugin.d.cts +0 -388
  149. package/dist/types/databasePlugin.d.mts +0 -388
  150. package/dist/types/databaseQuery.d.cts +0 -46
  151. package/dist/types/databaseQuery.d.mts +0 -46
@@ -1,10 +1,4 @@
1
- import { createUUIDv7 } from "./uuidv7.mjs";
2
- import { releaseRowToRelease } from "./databaseRows.mjs";
3
- import { compileReleaseCatalog } from "./releaseCatalogCompiler.mjs";
4
- import { createReleaseCatalogScopeKey, encodeChannelKey } from "@hot-updater/core";
5
1
  //#region src/releaseCatalogMutation.ts
6
- const RELEASE_PAGE_SIZE = 1e3;
7
- const DEFAULT_MAX_ATTEMPTS = 3;
8
2
  var ReleaseCatalogMutationError = class extends Error {
9
3
  code;
10
4
  name = "ReleaseCatalogMutationError";
@@ -13,286 +7,5 @@ var ReleaseCatalogMutationError = class extends Error {
13
7
  this.code = code;
14
8
  }
15
9
  };
16
- const readScopeReleases = async (database, scopeKey) => {
17
- const releases = [];
18
- let afterReleaseId;
19
- for (;;) {
20
- const page = await database.models.releases.findManyByScope({
21
- scopeKey,
22
- ...afterReleaseId === void 0 ? {} : { afterReleaseId },
23
- limit: RELEASE_PAGE_SIZE,
24
- consistency: "strong"
25
- });
26
- for (const row of page) {
27
- if (row.scope_key !== scopeKey) throw new ReleaseCatalogMutationError("INVALID_SCOPE", `Release "${row.id}" was returned for the wrong catalog scope`);
28
- if (releases.at(-1)?.id === row.id) throw new ReleaseCatalogMutationError("INVALID_SCOPE", `Release "${row.id}" was returned more than once`);
29
- releases.push(row);
30
- }
31
- if (page.length < RELEASE_PAGE_SIZE) return releases;
32
- const next = page.at(-1)?.id;
33
- if (next === void 0 || next === afterReleaseId) throw new ReleaseCatalogMutationError("INVALID_SCOPE", "Release scope paging did not advance");
34
- afterReleaseId = next;
35
- }
36
- };
37
- const validateScope = (scope) => {
38
- const channelKey = encodeChannelKey(scope.channelName);
39
- if (scope.strategy === "FINGERPRINT" !== (scope.fingerprintHash !== null)) throw new ReleaseCatalogMutationError("INVALID_SCOPE", "Release catalog strategy and fingerprint are inconsistent");
40
- const expectedScopeKey = scope.strategy === "APP_VERSION" ? createReleaseCatalogScopeKey({
41
- channelKey,
42
- platform: scope.platform,
43
- strategy: "APP_VERSION"
44
- }) : createReleaseCatalogScopeKey({
45
- channelKey,
46
- fingerprintHash: scope.fingerprintHash ?? "",
47
- platform: scope.platform,
48
- strategy: "FINGERPRINT"
49
- });
50
- if (scope.scopeKey !== expectedScopeKey) throw new ReleaseCatalogMutationError("INVALID_SCOPE", "Release catalog scope metadata is inconsistent");
51
- };
52
- const applyMutation = (rows, scope, mutation) => {
53
- const currentIndex = mutation.operation === "insert" ? -1 : rows.findIndex(({ id }) => id === mutation.id);
54
- const current = currentIndex < 0 ? void 0 : rows[currentIndex];
55
- if (mutation.operation === "insert") {
56
- const latestId = rows.at(-1)?.id;
57
- if (mutation.row.scope_key !== scope.scopeKey || mutation.row.channel_id !== scope.channelId || mutation.row.platform !== scope.platform || mutation.row.strategy !== scope.strategy || mutation.row.fingerprint_hash !== scope.fingerprintHash || mutation.row.revision !== 1) throw new ReleaseCatalogMutationError("INVALID_SCOPE", "Inserted Release does not match its catalog scope");
58
- if (latestId !== void 0 && mutation.row.id <= latestId) throw new ReleaseCatalogMutationError("NON_MONOTONIC_RELEASE_ID", `Release "${mutation.row.id}" must sort after "${latestId}"`);
59
- return {
60
- change: {
61
- model: "releases",
62
- operation: "insert",
63
- row: mutation.row
64
- },
65
- expectedRevision: null,
66
- nextRows: [...rows, mutation.row],
67
- release: mutation.row
68
- };
69
- }
70
- if (current === void 0) throw new ReleaseCatalogMutationError("RELEASE_NOT_FOUND", `Release "${mutation.id}" was not found in scope "${scope.scopeKey}"`);
71
- if (mutation.operation === "delete") return {
72
- change: {
73
- model: "releases",
74
- operation: "delete",
75
- where: { id: mutation.id }
76
- },
77
- expectedRevision: current.revision,
78
- nextRows: rows.filter(({ id }) => id !== mutation.id),
79
- release: null
80
- };
81
- const release = {
82
- ...current,
83
- ...mutation.update,
84
- revision: current.revision + 1
85
- };
86
- return {
87
- change: {
88
- model: "releases",
89
- operation: "update",
90
- where: { id: mutation.id },
91
- update: {
92
- ...mutation.update,
93
- revision: release.revision
94
- }
95
- },
96
- expectedRevision: current.revision,
97
- nextRows: rows.map((row) => row.id === release.id ? release : row),
98
- release
99
- };
100
- };
101
- const catalogIdentity = (current, rows) => {
102
- if (current !== null) return current.catalog_id;
103
- if (rows.length > 0) throw new ReleaseCatalogMutationError("CATALOG_IDENTITY_MISSING", "Catalog history is missing. Restore its catalog row from backup before rebuilding or deploying.");
104
- return createUUIDv7();
105
- };
106
- const readCatalogScope = async (database, scopeKey) => {
107
- let currentCatalog = await database.models.releaseCatalogs.findByScopeKey(scopeKey);
108
- let rows = await readScopeReleases(database, scopeKey);
109
- if (currentCatalog === null && rows.length > 0) {
110
- currentCatalog = await database.models.releaseCatalogs.findByScopeKey(scopeKey);
111
- if (currentCatalog !== null) rows = await readScopeReleases(database, scopeKey);
112
- }
113
- return {
114
- currentCatalog,
115
- rows
116
- };
117
- };
118
- const prepareReleaseCatalogMutation = async (database, mutationInput) => {
119
- const { rows, currentCatalog } = await readCatalogScope(database, mutationInput.scope.scopeKey);
120
- const applied = applyMutation(rows, mutationInput.scope, mutationInput.mutation);
121
- const compilation = await compileReleaseCatalog({
122
- strategy: mutationInput.scope.strategy,
123
- releases: applied.nextRows.map(releaseRowToRelease)
124
- });
125
- const generation = (currentCatalog?.generation ?? 0) + 1;
126
- if (!Number.isSafeInteger(generation)) throw new ReleaseCatalogMutationError("CATALOG_GENERATION_EXHAUSTED", `Catalog "${mutationInput.scope.scopeKey}" exhausted its generation counter`);
127
- return {
128
- applied,
129
- catalog: {
130
- scope_key: mutationInput.scope.scopeKey,
131
- catalog_id: catalogIdentity(currentCatalog, rows),
132
- strategy: mutationInput.scope.strategy,
133
- channel_id: mutationInput.scope.channelId,
134
- channel_key: encodeChannelKey(mutationInput.scope.channelName),
135
- platform: mutationInput.scope.platform,
136
- fingerprint_hash: mutationInput.scope.fingerprintHash,
137
- generation,
138
- payload: compilation.canonicalPayload,
139
- catalog_hash: compilation.catalogHash,
140
- byte_size: compilation.byteSize,
141
- is_tombstone: compilation.payload.releaseDescriptors.length === 0,
142
- updated_at_ms: mutationInput.updatedAtMs ?? Date.now()
143
- },
144
- compilation,
145
- currentCatalog,
146
- mutationInput
147
- };
148
- };
149
- async function preflightReleaseCatalogMutation(input) {
150
- validateScope(input.scope);
151
- const prepared = await prepareReleaseCatalogMutation(input.database, input);
152
- return {
153
- catalog: prepared.catalog,
154
- currentCatalog: prepared.currentCatalog,
155
- diagnostics: prepared.compilation.diagnostics,
156
- expectedReleaseRevision: prepared.applied.expectedRevision,
157
- release: prepared.applied.release
158
- };
159
- }
160
- async function commitReleaseCatalogMutations(input) {
161
- if (input.mutations.length === 0) return [];
162
- const scopeKeys = /* @__PURE__ */ new Set();
163
- for (const mutation of input.mutations) {
164
- validateScope(mutation.scope);
165
- if (scopeKeys.has(mutation.scope.scopeKey)) throw new ReleaseCatalogMutationError("INVALID_SCOPE", `A batch cannot mutate catalog scope "${mutation.scope.scopeKey}" more than once`);
166
- scopeKeys.add(mutation.scope.scopeKey);
167
- }
168
- const maxAttempts = input.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;
169
- if (!Number.isSafeInteger(maxAttempts) || maxAttempts <= 0) throw new RangeError("maxAttempts must be a positive safe integer");
170
- for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
171
- const prepared = await Promise.all(input.mutations.map((mutationInput) => prepareReleaseCatalogMutation(input.database, mutationInput)));
172
- const result = await input.database.commit({
173
- expectations: prepared.flatMap(({ applied, currentCatalog, mutationInput }) => [{
174
- model: "releases",
175
- id: mutationInput.mutation.operation === "insert" ? mutationInput.mutation.row.id : mutationInput.mutation.id,
176
- revision: applied.expectedRevision
177
- }, {
178
- model: "releaseCatalogs",
179
- scopeKey: mutationInput.scope.scopeKey,
180
- generation: currentCatalog?.generation ?? null
181
- }]),
182
- changes: prepared.flatMap(({ applied, catalog, mutationInput }) => [
183
- ...mutationInput.companionChanges ?? [],
184
- applied.change,
185
- {
186
- model: "releaseCatalogs",
187
- operation: "put",
188
- row: catalog
189
- },
190
- ...mutationInput.trailingChanges ?? []
191
- ])
192
- });
193
- if (result.committed) return prepared.map(({ applied, catalog }) => ({
194
- attempts: attempt,
195
- catalog,
196
- release: applied.release
197
- }));
198
- if (result.conflict.reason !== "version_conflict") throw new ReleaseCatalogMutationError("INVALID_SCOPE", `Release mutation failed: ${result.conflict.reason}`);
199
- }
200
- throw new ReleaseCatalogMutationError("VERSION_CONFLICT", `Release catalog mutation batch conflicted ${maxAttempts} times`);
201
- }
202
- async function commitReleaseCatalogMutation(input) {
203
- const [result] = await commitReleaseCatalogMutations({
204
- database: input.database,
205
- maxAttempts: input.maxAttempts,
206
- mutations: [input]
207
- });
208
- return result;
209
- }
210
- const catalogProjectionMatches = (current, scope, compilation) => current.strategy === scope.strategy && current.channel_id === scope.channelId && current.channel_key === encodeChannelKey(scope.channelName) && current.platform === scope.platform && current.fingerprint_hash === scope.fingerprintHash && current.payload === compilation.canonicalPayload && current.catalog_hash === compilation.catalogHash && current.byte_size === compilation.byteSize && current.is_tombstone === (compilation.payload.releaseDescriptors.length === 0);
211
- async function preflightReleaseCatalogRebuild(input) {
212
- validateScope(input.scope);
213
- const { rows, currentCatalog } = await readCatalogScope(input.database, input.scope.scopeKey);
214
- const compilation = await compileReleaseCatalog({
215
- releases: rows.map(releaseRowToRelease),
216
- strategy: input.scope.strategy
217
- });
218
- const changed = currentCatalog === null || !catalogProjectionMatches(currentCatalog, input.scope, compilation);
219
- const generation = changed ? (currentCatalog?.generation ?? 0) + 1 : currentCatalog.generation;
220
- if (!Number.isSafeInteger(generation)) throw new ReleaseCatalogMutationError("CATALOG_GENERATION_EXHAUSTED", `Catalog "${input.scope.scopeKey}" exhausted its generation counter`);
221
- return {
222
- changed,
223
- currentCatalog,
224
- diagnostics: compilation.diagnostics,
225
- projectedCatalog: {
226
- catalog_id: catalogIdentity(currentCatalog, rows),
227
- byte_size: compilation.byteSize,
228
- catalog_hash: compilation.catalogHash,
229
- channel_id: input.scope.channelId,
230
- channel_key: encodeChannelKey(input.scope.channelName),
231
- fingerprint_hash: input.scope.fingerprintHash,
232
- generation,
233
- is_tombstone: compilation.payload.releaseDescriptors.length === 0,
234
- payload: compilation.canonicalPayload,
235
- platform: input.scope.platform,
236
- scope_key: input.scope.scopeKey,
237
- strategy: input.scope.strategy,
238
- updated_at_ms: changed || currentCatalog === null ? input.updatedAtMs ?? Date.now() : currentCatalog.updated_at_ms
239
- }
240
- };
241
- }
242
- async function rebuildReleaseCatalog(input) {
243
- validateScope(input.scope);
244
- const maxAttempts = input.maxAttempts ?? DEFAULT_MAX_ATTEMPTS;
245
- if (!Number.isSafeInteger(maxAttempts) || maxAttempts <= 0) throw new RangeError("maxAttempts must be a positive safe integer");
246
- for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
247
- const { rows, currentCatalog } = await readCatalogScope(input.database, input.scope.scopeKey);
248
- const compilation = await compileReleaseCatalog({
249
- releases: rows.map(releaseRowToRelease),
250
- strategy: input.scope.strategy
251
- });
252
- if (currentCatalog !== null && catalogProjectionMatches(currentCatalog, input.scope, compilation)) return {
253
- attempts: attempt,
254
- catalog: currentCatalog,
255
- changed: false,
256
- diagnostics: compilation.diagnostics
257
- };
258
- const generation = (currentCatalog?.generation ?? 0) + 1;
259
- if (!Number.isSafeInteger(generation)) throw new ReleaseCatalogMutationError("CATALOG_GENERATION_EXHAUSTED", `Catalog "${input.scope.scopeKey}" exhausted its generation counter`);
260
- const catalog = {
261
- catalog_id: catalogIdentity(currentCatalog, rows),
262
- byte_size: compilation.byteSize,
263
- catalog_hash: compilation.catalogHash,
264
- channel_id: input.scope.channelId,
265
- channel_key: encodeChannelKey(input.scope.channelName),
266
- fingerprint_hash: input.scope.fingerprintHash,
267
- generation,
268
- is_tombstone: compilation.payload.releaseDescriptors.length === 0,
269
- payload: compilation.canonicalPayload,
270
- platform: input.scope.platform,
271
- scope_key: input.scope.scopeKey,
272
- strategy: input.scope.strategy,
273
- updated_at_ms: input.updatedAtMs ?? Date.now()
274
- };
275
- const result = await input.database.commit({
276
- changes: [{
277
- model: "releaseCatalogs",
278
- operation: "put",
279
- row: catalog
280
- }],
281
- expectations: [{
282
- generation: currentCatalog?.generation ?? null,
283
- model: "releaseCatalogs",
284
- scopeKey: input.scope.scopeKey
285
- }]
286
- });
287
- if (result.committed) return {
288
- attempts: attempt,
289
- catalog,
290
- changed: true,
291
- diagnostics: compilation.diagnostics
292
- };
293
- if (result.conflict.reason !== "version_conflict") throw new ReleaseCatalogMutationError("INVALID_SCOPE", `Release catalog rebuild failed: ${result.conflict.reason}`);
294
- }
295
- throw new ReleaseCatalogMutationError("VERSION_CONFLICT", `Release catalog rebuild conflicted ${maxAttempts} times`);
296
- }
297
10
  //#endregion
298
- export { ReleaseCatalogMutationError, commitReleaseCatalogMutation, commitReleaseCatalogMutations, preflightReleaseCatalogMutation, preflightReleaseCatalogRebuild, rebuildReleaseCatalog };
11
+ export { ReleaseCatalogMutationError };
@@ -1,8 +1,4 @@
1
- const require_uuidv7 = require("./uuidv7.cjs");
2
- const require_releaseCatalogMutation = require("./releaseCatalogMutation.cjs");
3
- let _hot_updater_core = require("@hot-updater/core");
4
1
  //#region src/releaseManagement.ts
5
- const RELEASE_PAGE_SIZE = 1e3;
6
2
  var ReleaseManagementError = class extends Error {
7
3
  code;
8
4
  name = "ReleaseManagementError";
@@ -11,222 +7,5 @@ var ReleaseManagementError = class extends Error {
11
7
  this.code = code;
12
8
  }
13
9
  };
14
- const readReleaseScope = async (database, scopeKey) => {
15
- const rows = [];
16
- let afterReleaseId;
17
- for (;;) {
18
- const page = await database.models.releases.findManyByScope({
19
- scopeKey,
20
- ...afterReleaseId === void 0 ? {} : { afterReleaseId },
21
- consistency: "strong",
22
- limit: RELEASE_PAGE_SIZE
23
- });
24
- rows.push(...page);
25
- if (page.length < RELEASE_PAGE_SIZE) return rows;
26
- const nextCursor = page.at(-1)?.id;
27
- if (nextCursor === void 0 || nextCursor === afterReleaseId) throw new require_releaseCatalogMutation.ReleaseCatalogMutationError("INVALID_SCOPE", `Release paging did not advance for scope "${scopeKey}".`);
28
- afterReleaseId = nextCursor;
29
- }
30
- };
31
- const createForwardReleaseId = (rows, candidates, nowMs) => {
32
- const floor = [...candidates, rows.at(-1)?.id ?? null].filter((value) => value !== null && (0, _hot_updater_core.isUUIDv7)(value)).sort().at(-1);
33
- return require_uuidv7.createUUIDv7After(floor ?? null, nowMs);
34
- };
35
- const prepareTargetScope = async ({ channelName, database, fingerprintHash, platform, strategy }) => {
36
- const channelKey = (0, _hot_updater_core.encodeChannelKey)(channelName);
37
- const channel = (await database.models.channels.list({})).channels.find(({ name }) => name === channelName) ?? (await database.models.channels.insert({
38
- row: {
39
- id: `channel:${channelKey}`,
40
- name: channelName
41
- },
42
- onConflict: "returnExisting"
43
- })).row;
44
- const scopeKey = strategy === "APP_VERSION" ? (0, _hot_updater_core.createReleaseCatalogScopeKey)({
45
- channelKey,
46
- platform,
47
- strategy
48
- }) : (0, _hot_updater_core.createReleaseCatalogScopeKey)({
49
- channelKey,
50
- fingerprintHash: fingerprintHash ?? "",
51
- platform,
52
- strategy
53
- });
54
- return { scope: {
55
- channelId: channel.id,
56
- channelName,
57
- fingerprintHash,
58
- platform,
59
- scopeKey,
60
- strategy
61
- } };
62
- };
63
- const loadReleaseTarget = async (input) => {
64
- const release = await input.database.models.releases.findById(input.releaseId);
65
- if (release === null) throw new ReleaseManagementError("RELEASE_NOT_FOUND", `Release "${input.releaseId}" was not found.`);
66
- if (input.expectedRevision !== void 0 && input.expectedRevision !== release.revision) throw new ReleaseManagementError("VERSION_CONFLICT", `Release "${release.id}" is revision ${release.revision}; expected ${input.expectedRevision}.`);
67
- const catalog = await input.database.models.releaseCatalogs.findByScopeKey(release.scope_key);
68
- if (catalog === null) throw new require_releaseCatalogMutation.ReleaseCatalogMutationError("INVALID_SCOPE", `Release "${release.id}" has no catalog projection.`);
69
- return {
70
- release,
71
- scope: {
72
- channelId: release.channel_id,
73
- channelName: (0, _hot_updater_core.decodeChannelKey)(catalog.channel_key),
74
- fingerprintHash: release.fingerprint_hash,
75
- platform: release.platform,
76
- scopeKey: release.scope_key,
77
- strategy: release.strategy
78
- }
79
- };
80
- };
81
- const releasePolicyUpdate = (release, patch, updatedAtMs) => {
82
- if (patch.fingerprintHash !== void 0 && patch.fingerprintHash !== release.fingerprint_hash) throw new ReleaseManagementError("SCOPE_MOVE_UNSUPPORTED", "Changing a fingerprint target requires an atomic Release scope move.");
83
- return {
84
- ...patch.enabled === void 0 ? {} : { enabled: patch.enabled },
85
- ...patch.message === void 0 ? {} : { message: patch.message },
86
- ...patch.rolloutCohortCount === void 0 ? {} : { rollout_cohort_count: patch.rolloutCohortCount },
87
- ...patch.shouldForceUpdate === void 0 ? {} : { should_force_update: patch.shouldForceUpdate },
88
- ...patch.targetAppVersion === void 0 ? {} : { target_app_version: patch.targetAppVersion },
89
- ...patch.targetCohorts === void 0 ? {} : { target_cohorts: [...patch.targetCohorts] },
90
- updated_at_ms: updatedAtMs
91
- };
92
- };
93
- async function updateReleasePolicy(input) {
94
- const { release, scope } = await loadReleaseTarget(input);
95
- const updatedAtMs = input.updatedAtMs ?? Date.now();
96
- return require_releaseCatalogMutation.commitReleaseCatalogMutation({
97
- database: input.database,
98
- mutation: {
99
- id: release.id,
100
- operation: "update",
101
- update: releasePolicyUpdate(release, input.patch, updatedAtMs)
102
- },
103
- scope,
104
- updatedAtMs
105
- });
106
- }
107
- async function preflightReleasePolicy(input) {
108
- const { release, scope } = await loadReleaseTarget(input);
109
- const updatedAtMs = input.updatedAtMs ?? Date.now();
110
- return require_releaseCatalogMutation.preflightReleaseCatalogMutation({
111
- database: input.database,
112
- mutation: {
113
- id: release.id,
114
- operation: "update",
115
- update: releasePolicyUpdate(release, input.patch, updatedAtMs)
116
- },
117
- scope,
118
- updatedAtMs
119
- });
120
- }
121
- async function deleteRelease(input) {
122
- const { release, scope } = await loadReleaseTarget(input);
123
- if (release.enabled) throw new ReleaseManagementError("ENABLED_RELEASE", `Disable Release "${release.id}" before hard deletion.`);
124
- return require_releaseCatalogMutation.commitReleaseCatalogMutation({
125
- database: input.database,
126
- mutation: {
127
- id: release.id,
128
- operation: "delete"
129
- },
130
- scope,
131
- updatedAtMs: input.updatedAtMs
132
- });
133
- }
134
- async function promoteRelease(input) {
135
- const { release: source, scope: sourceScope } = await loadReleaseTarget(input);
136
- if (source.kind !== "BUNDLE" || source.bundle_id === null) throw new ReleaseManagementError("TARGET_RELEASE_INVALID", "Only a Bundle Release can be promoted.");
137
- const targetChannel = input.targetChannel.trim();
138
- if (targetChannel.length === 0) throw new ReleaseManagementError("TARGET_RELEASE_INVALID", "Promotion requires a target channel.");
139
- const { scope: targetScope } = await prepareTargetScope({
140
- channelName: targetChannel,
141
- database: input.database,
142
- fingerprintHash: source.fingerprint_hash,
143
- platform: source.platform,
144
- strategy: source.strategy
145
- });
146
- if (targetScope.scopeKey === sourceScope.scopeKey) throw new ReleaseManagementError("TARGET_RELEASE_INVALID", "Source and target Release scopes are the same.");
147
- const targetRows = await readReleaseScope(input.database, targetScope.scopeKey);
148
- const updatedAtMs = input.updatedAtMs ?? Date.now();
149
- const targetMutation = {
150
- mutation: {
151
- operation: "insert",
152
- row: {
153
- ...source,
154
- id: createForwardReleaseId(targetRows, [source.id, source.bundle_id], updatedAtMs),
155
- revision: 1,
156
- scope_key: targetScope.scopeKey,
157
- channel_id: targetScope.channelId,
158
- enabled: true,
159
- rollout_cohort_count: 1e3,
160
- target_cohorts: [],
161
- operation: "PROMOTE",
162
- source_release_id: source.id,
163
- created_at_ms: updatedAtMs,
164
- updated_at_ms: updatedAtMs
165
- }
166
- },
167
- scope: targetScope,
168
- updatedAtMs
169
- };
170
- const sourceMutation = input.action === "move" ? {
171
- mutation: {
172
- id: source.id,
173
- operation: "update",
174
- update: {
175
- enabled: false,
176
- updated_at_ms: updatedAtMs
177
- }
178
- },
179
- scope: sourceScope,
180
- updatedAtMs
181
- } : null;
182
- const results = await require_releaseCatalogMutation.commitReleaseCatalogMutations({
183
- database: input.database,
184
- mutations: sourceMutation === null ? [targetMutation] : [sourceMutation, targetMutation]
185
- });
186
- return {
187
- source: sourceMutation === null ? null : results[0],
188
- target: results.at(-1)
189
- };
190
- }
191
- const prepareRollbackRelease = async (input) => {
192
- const { release: source, scope } = await loadReleaseTarget(input);
193
- if (!source.enabled) throw new ReleaseManagementError("TARGET_RELEASE_INVALID", "Rollback requires an enabled Release.");
194
- const updatedAtMs = input.updatedAtMs ?? Date.now();
195
- return {
196
- mutation: {
197
- id: source.id,
198
- operation: "update",
199
- update: {
200
- enabled: false,
201
- updated_at_ms: updatedAtMs
202
- }
203
- },
204
- scope,
205
- updatedAtMs
206
- };
207
- };
208
- async function rollbackReleases(input) {
209
- const mutations = await Promise.all(input.rollbacks.map((rollback) => prepareRollbackRelease({
210
- ...rollback,
211
- database: input.database
212
- })));
213
- return require_releaseCatalogMutation.commitReleaseCatalogMutations({
214
- database: input.database,
215
- mutations
216
- });
217
- }
218
- async function rollbackRelease(input) {
219
- const [result] = await rollbackReleases({
220
- database: input.database,
221
- rollbacks: [input]
222
- });
223
- return result;
224
- }
225
10
  //#endregion
226
11
  exports.ReleaseManagementError = ReleaseManagementError;
227
- exports.deleteRelease = deleteRelease;
228
- exports.preflightReleasePolicy = preflightReleasePolicy;
229
- exports.promoteRelease = promoteRelease;
230
- exports.rollbackRelease = rollbackRelease;
231
- exports.rollbackReleases = rollbackReleases;
232
- exports.updateReleasePolicy = updateReleasePolicy;
@@ -1,7 +1,6 @@
1
- import { BundleRepository } from "./types/databasePlugin.cjs";
2
- import "./types/index.cjs";
3
- import { ReleaseCatalogMutationPreflight, ReleaseCatalogMutationResult } from "./releaseCatalogMutation.cjs";
1
+ import { ReleaseCatalogMutationResult } from "./releaseCatalogMutation.cjs";
4
2
  //#region src/releaseManagement.d.ts
3
+ /** The release fields a policy change may set. */
5
4
  interface ReleasePolicyPatch {
6
5
  readonly enabled?: boolean;
7
6
  readonly fingerprintHash?: string;
@@ -11,38 +10,15 @@ interface ReleasePolicyPatch {
11
10
  readonly targetAppVersion?: string;
12
11
  readonly targetCohorts?: readonly string[];
13
12
  }
14
- interface ReleaseMutationTarget {
15
- readonly database: BundleRepository;
16
- readonly expectedRevision?: number;
17
- readonly releaseId: string;
18
- readonly updatedAtMs?: number;
19
- }
20
13
  declare class ReleaseManagementError extends Error {
21
14
  readonly code: "ENABLED_RELEASE" | "RELEASE_NOT_FOUND" | "SCOPE_MOVE_UNSUPPORTED" | "TARGET_RELEASE_INVALID" | "VERSION_CONFLICT";
22
15
  readonly name = "ReleaseManagementError";
23
16
  constructor(code: "ENABLED_RELEASE" | "RELEASE_NOT_FOUND" | "SCOPE_MOVE_UNSUPPORTED" | "TARGET_RELEASE_INVALID" | "VERSION_CONFLICT", message: string);
24
17
  }
25
- declare function updateReleasePolicy(input: ReleaseMutationTarget & {
26
- readonly patch: ReleasePolicyPatch;
27
- }): Promise<ReleaseCatalogMutationResult>;
28
- declare function preflightReleasePolicy(input: ReleaseMutationTarget & {
29
- readonly patch: ReleasePolicyPatch;
30
- }): Promise<ReleaseCatalogMutationPreflight>;
31
- declare function deleteRelease(input: ReleaseMutationTarget): Promise<ReleaseCatalogMutationResult>;
32
- interface PromoteReleaseInput extends ReleaseMutationTarget {
33
- readonly action?: "copy" | "move";
34
- readonly targetChannel: string;
35
- }
36
18
  interface PromoteReleaseResult {
19
+ /** The source release after a move, or null for a copy. */
37
20
  readonly source: ReleaseCatalogMutationResult | null;
38
21
  readonly target: ReleaseCatalogMutationResult;
39
22
  }
40
- declare function promoteRelease(input: PromoteReleaseInput): Promise<PromoteReleaseResult>;
41
- type RollbackReleaseInput = ReleaseMutationTarget;
42
- declare function rollbackReleases(input: {
43
- readonly database: BundleRepository;
44
- readonly rollbacks: readonly Omit<RollbackReleaseInput, "database">[];
45
- }): Promise<readonly ReleaseCatalogMutationResult[]>;
46
- declare function rollbackRelease(input: RollbackReleaseInput): Promise<ReleaseCatalogMutationResult>;
47
23
  //#endregion
48
- export { PromoteReleaseInput, PromoteReleaseResult, ReleaseManagementError, ReleaseMutationTarget, ReleasePolicyPatch, RollbackReleaseInput, deleteRelease, preflightReleasePolicy, promoteRelease, rollbackRelease, rollbackReleases, updateReleasePolicy };
24
+ export { PromoteReleaseResult, ReleaseManagementError, ReleasePolicyPatch };
@@ -1,7 +1,6 @@
1
- import { BundleRepository } from "./types/databasePlugin.mjs";
2
- import "./types/index.mjs";
3
- import { ReleaseCatalogMutationPreflight, ReleaseCatalogMutationResult } from "./releaseCatalogMutation.mjs";
1
+ import { ReleaseCatalogMutationResult } from "./releaseCatalogMutation.mjs";
4
2
  //#region src/releaseManagement.d.ts
3
+ /** The release fields a policy change may set. */
5
4
  interface ReleasePolicyPatch {
6
5
  readonly enabled?: boolean;
7
6
  readonly fingerprintHash?: string;
@@ -11,38 +10,15 @@ interface ReleasePolicyPatch {
11
10
  readonly targetAppVersion?: string;
12
11
  readonly targetCohorts?: readonly string[];
13
12
  }
14
- interface ReleaseMutationTarget {
15
- readonly database: BundleRepository;
16
- readonly expectedRevision?: number;
17
- readonly releaseId: string;
18
- readonly updatedAtMs?: number;
19
- }
20
13
  declare class ReleaseManagementError extends Error {
21
14
  readonly code: "ENABLED_RELEASE" | "RELEASE_NOT_FOUND" | "SCOPE_MOVE_UNSUPPORTED" | "TARGET_RELEASE_INVALID" | "VERSION_CONFLICT";
22
15
  readonly name = "ReleaseManagementError";
23
16
  constructor(code: "ENABLED_RELEASE" | "RELEASE_NOT_FOUND" | "SCOPE_MOVE_UNSUPPORTED" | "TARGET_RELEASE_INVALID" | "VERSION_CONFLICT", message: string);
24
17
  }
25
- declare function updateReleasePolicy(input: ReleaseMutationTarget & {
26
- readonly patch: ReleasePolicyPatch;
27
- }): Promise<ReleaseCatalogMutationResult>;
28
- declare function preflightReleasePolicy(input: ReleaseMutationTarget & {
29
- readonly patch: ReleasePolicyPatch;
30
- }): Promise<ReleaseCatalogMutationPreflight>;
31
- declare function deleteRelease(input: ReleaseMutationTarget): Promise<ReleaseCatalogMutationResult>;
32
- interface PromoteReleaseInput extends ReleaseMutationTarget {
33
- readonly action?: "copy" | "move";
34
- readonly targetChannel: string;
35
- }
36
18
  interface PromoteReleaseResult {
19
+ /** The source release after a move, or null for a copy. */
37
20
  readonly source: ReleaseCatalogMutationResult | null;
38
21
  readonly target: ReleaseCatalogMutationResult;
39
22
  }
40
- declare function promoteRelease(input: PromoteReleaseInput): Promise<PromoteReleaseResult>;
41
- type RollbackReleaseInput = ReleaseMutationTarget;
42
- declare function rollbackReleases(input: {
43
- readonly database: BundleRepository;
44
- readonly rollbacks: readonly Omit<RollbackReleaseInput, "database">[];
45
- }): Promise<readonly ReleaseCatalogMutationResult[]>;
46
- declare function rollbackRelease(input: RollbackReleaseInput): Promise<ReleaseCatalogMutationResult>;
47
23
  //#endregion
48
- export { PromoteReleaseInput, PromoteReleaseResult, ReleaseManagementError, ReleaseMutationTarget, ReleasePolicyPatch, RollbackReleaseInput, deleteRelease, preflightReleasePolicy, promoteRelease, rollbackRelease, rollbackReleases, updateReleasePolicy };
24
+ export { PromoteReleaseResult, ReleaseManagementError, ReleasePolicyPatch };