@hot-updater/server 1.2.0-rc.6 → 1.2.0-rc.60

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 (53) hide show
  1. package/dist/adapters/drizzle.cjs +8 -4
  2. package/dist/adapters/drizzle.d.cts +3 -2
  3. package/dist/adapters/drizzle.d.mts +3 -2
  4. package/dist/adapters/drizzle.mjs +8 -4
  5. package/dist/adapters/drizzleExecutor.cjs +4 -2
  6. package/dist/adapters/drizzleExecutor.mjs +4 -2
  7. package/dist/adapters/kysely.cjs +3 -2
  8. package/dist/adapters/kysely.mjs +3 -2
  9. package/dist/adapters/mongodb.cjs +1 -1
  10. package/dist/adapters/mongodb.mjs +1 -1
  11. package/dist/adapters/mongodbAdapter.cjs +15 -3
  12. package/dist/adapters/mongodbAdapter.mjs +15 -3
  13. package/dist/adapters/prisma.cjs +9 -13
  14. package/dist/adapters/prisma.mjs +10 -14
  15. package/dist/adapters/prismaExecutor.cjs +25 -20
  16. package/dist/adapters/prismaExecutor.mjs +25 -20
  17. package/dist/core/api.cjs +2 -0
  18. package/dist/core/api.d.cts +48 -1
  19. package/dist/core/api.d.mts +48 -1
  20. package/dist/core/api.mjs +2 -0
  21. package/dist/core/iosDevices.cjs +306 -0
  22. package/dist/core/iosDevices.mjs +303 -0
  23. package/dist/core/labels.cjs +325 -0
  24. package/dist/core/labels.mjs +318 -0
  25. package/dist/core/nativeBuildDestinations.cjs +13 -0
  26. package/dist/core/nativeBuildDestinations.mjs +13 -0
  27. package/dist/core/nativeBuilds.cjs +291 -22
  28. package/dist/core/nativeBuilds.mjs +291 -23
  29. package/dist/core/operations.cjs +45 -24
  30. package/dist/core/operations.mjs +48 -27
  31. package/dist/core/reads.cjs +69 -5
  32. package/dist/core/reads.mjs +70 -6
  33. package/dist/core/releases.cjs +138 -38
  34. package/dist/core/releases.mjs +138 -41
  35. package/dist/core/writes.cjs +14 -2
  36. package/dist/core/writes.mjs +14 -2
  37. package/dist/createHotUpdaterCore.d.cts +2 -4
  38. package/dist/createHotUpdaterCore.d.mts +2 -4
  39. package/dist/db/engineDrizzleSchema.cjs +21 -5
  40. package/dist/db/engineDrizzleSchema.mjs +21 -6
  41. package/dist/db/enginePrismaSchema.cjs +6 -14
  42. package/dist/db/enginePrismaSchema.mjs +7 -15
  43. package/dist/db/releaseCatalog.cjs +13 -4
  44. package/dist/db/releaseCatalog.mjs +12 -5
  45. package/dist/handler.cjs +2 -1
  46. package/dist/handler.mjs +2 -1
  47. package/dist/handlerAdminRoutes.cjs +288 -16
  48. package/dist/handlerAdminRoutes.mjs +289 -17
  49. package/dist/handlerReleaseCatalogRoutes.cjs +43 -27
  50. package/dist/handlerReleaseCatalogRoutes.mjs +43 -27
  51. package/dist/package.cjs +1 -1
  52. package/dist/package.mjs +1 -1
  53. package/package.json +7 -14
@@ -0,0 +1,325 @@
1
+ const require_reads = require("./reads.cjs");
2
+ let _hot_updater_plugin_core = require("@hot-updater/plugin-core");
3
+ //#region src/core/labels.ts
4
+ /**
5
+ * The counter of a channel and platform's release labels: one
6
+ * `label_sequences` row, which every writer of the pair reads and bumps.
7
+ */
8
+ const labelSequenceId = (channelId, platform) => `releases/${platform}/${channelId}`;
9
+ /** The pairs a channel's counters belong to, which deleting it removes. */
10
+ const LABEL_PLATFORMS = ["ios", "android"];
11
+ /**
12
+ * A channel and platform whose releases written before labels have none
13
+ * yet, or that has no counter: the write stops before taking a label, the
14
+ * core labels the pair with `fillLabels`, and the write runs again.
15
+ */
16
+ var NeedsLabelFill = class extends Error {
17
+ channelId;
18
+ platform;
19
+ constructor(channelId, platform) {
20
+ super(`Release labels of ${platform} ${channelId} need a fill first.`);
21
+ this.channelId = channelId;
22
+ this.platform = platform;
23
+ }
24
+ };
25
+ const createLabelAllocator = (tx, used, caughtUp) => {
26
+ const sequences = /* @__PURE__ */ new Map();
27
+ return {
28
+ async next(channelId, platform) {
29
+ const id = labelSequenceId(channelId, platform);
30
+ let sequence = sequences.get(id);
31
+ if (sequence === void 0) {
32
+ const row = await tx.findOne("label_sequences", { id });
33
+ if (row === null || !row.is_filled) {
34
+ const channel = row === null ? await tx.findOne("channels", { id: channelId }) : null;
35
+ if (channel === null || (channel._refs_releases_channel_id ?? 0) > 0) throw new NeedsLabelFill(channelId, platform);
36
+ }
37
+ const read = row?.last_label ?? 0;
38
+ sequence = {
39
+ row,
40
+ read,
41
+ last: read
42
+ };
43
+ sequences.set(id, sequence);
44
+ used.set(id, {
45
+ channelId,
46
+ platform,
47
+ read
48
+ });
49
+ }
50
+ sequence.last += 1;
51
+ return sequence.last;
52
+ },
53
+ caughtUp(channelId, platform) {
54
+ caughtUp.set(labelSequenceId(channelId, platform), {
55
+ channelId,
56
+ platform
57
+ });
58
+ },
59
+ flush() {
60
+ for (const [id, { row, read, last }] of sequences) if (row === null) tx.create("label_sequences", {
61
+ id,
62
+ last_label: last,
63
+ is_filled: true
64
+ });
65
+ else if (last !== read) tx.update("label_sequences", row, { last_label: last });
66
+ }
67
+ };
68
+ };
69
+ const FILL_BATCH = 25;
70
+ const PAGE = 500;
71
+ const PAUSE_MS = 1e3;
72
+ const MAX_PAUSE_MS = 32e3;
73
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
74
+ /**
75
+ * Labels a channel and platform's releases that have none, in ID order,
76
+ * which is the order they were created in, and marks its counter filled:
77
+ * each batch reads the counter and its rows guarded, so a concurrent fill or
78
+ * write serializes on the counter and skips what another labeled. With
79
+ * `dryRun`, it only counts them. Answers how many it labeled.
80
+ */
81
+ const fillLabels = async (db, { channelId, platform }, { dryRun = false } = {}) => {
82
+ const id = labelSequenceId(channelId, platform);
83
+ let labeled = 0;
84
+ let batchSize = FILL_BATCH;
85
+ let paused = 0;
86
+ const label = async (ids) => {
87
+ for (let at = 0; at < ids.length;) {
88
+ const batch = ids.slice(at, at + batchSize);
89
+ try {
90
+ labeled += await db.transaction(async (tx) => {
91
+ const counter = await tx.findOne("label_sequences", { id });
92
+ let last = counter?.last_label ?? 0;
93
+ let count = 0;
94
+ for (const releaseId of batch) {
95
+ const row = await tx.findOne("releases", { id: releaseId });
96
+ if (row === null || typeof row.label === "number") continue;
97
+ last += 1;
98
+ count += 1;
99
+ tx.update("releases", row, { label: last });
100
+ }
101
+ if (counter === null) tx.create("label_sequences", {
102
+ id,
103
+ last_label: last,
104
+ is_filled: false
105
+ });
106
+ else if (last !== counter.last_label) tx.update("label_sequences", counter, { last_label: last });
107
+ return count;
108
+ });
109
+ at += batch.length;
110
+ paused = 0;
111
+ } catch (error) {
112
+ if (batchSize > 1 && error instanceof _hot_updater_plugin_core.DatabaseConstraintError && error.reason === "too_large") {
113
+ batchSize = Math.max(1, Math.floor(batchSize / 2));
114
+ continue;
115
+ }
116
+ if (!(0, _hot_updater_plugin_core.isDatabaseBusyError)(error) || paused >= MAX_PAUSE_MS) throw error;
117
+ batchSize = Math.max(1, Math.floor(batchSize / 2));
118
+ paused = paused === 0 ? PAUSE_MS : paused * 2;
119
+ await sleep(paused);
120
+ }
121
+ }
122
+ };
123
+ let cursor;
124
+ do {
125
+ const page = await db.findMany("releases", {
126
+ index: "byChannelPlatform",
127
+ where: {
128
+ channel_id: channelId,
129
+ platform
130
+ },
131
+ limit: PAGE,
132
+ ...cursor === void 0 ? {} : { cursor }
133
+ });
134
+ const unlabeled = page.rows.filter((row) => typeof row.label !== "number").map((row) => row.id);
135
+ if (dryRun) labeled += unlabeled.length;
136
+ else await label(unlabeled);
137
+ cursor = page.next;
138
+ } while (cursor !== void 0);
139
+ if (dryRun) return labeled;
140
+ await db.transaction(async (tx) => {
141
+ const counter = await tx.findOne("label_sequences", { id });
142
+ if (counter === null) tx.create("label_sequences", {
143
+ id,
144
+ last_label: 0,
145
+ is_filled: true
146
+ });
147
+ else if (!counter.is_filled) tx.update("label_sequences", counter, { is_filled: true });
148
+ });
149
+ return labeled;
150
+ };
151
+ /**
152
+ * Reads what a pair's fill writes, its counter and its label index, without
153
+ * writing: a database without release labels' schema, such as one a 1.2
154
+ * prerelease migrated, fails here as the fill would. The schema is the
155
+ * database's, so one pair tells. Two reads.
156
+ */
157
+ const probeLabelSchema = async (db, { channelId, platform }) => {
158
+ await db.findOne("label_sequences", { id: labelSequenceId(channelId, platform) });
159
+ await db.findMany("releases", {
160
+ index: "byLabel",
161
+ where: {
162
+ channel_id: channelId,
163
+ platform,
164
+ label: 0
165
+ },
166
+ limit: 1
167
+ });
168
+ };
169
+ const REPAIRS = 4;
170
+ const RACES = 32;
171
+ /**
172
+ * Runs a release write that takes labels, in one transaction with the
173
+ * counters it bumps. A pair that still needs its fill is filled, and the
174
+ * write runs again; so is a pair whose catch-up made the write too large for
175
+ * the store. A label another row holds, such as an index item a release
176
+ * deleted by a server of an earlier release left on a key-value store, is
177
+ * skipped: its counters move past it and the write runs again.
178
+ */
179
+ const writeWithLabels = async (db, write) => {
180
+ const filled = /* @__PURE__ */ new Set();
181
+ const fill = async (pairs) => {
182
+ for (const pair of pairs) {
183
+ filled.add(labelSequenceId(pair.channelId, pair.platform));
184
+ await fillLabels(db, pair);
185
+ }
186
+ };
187
+ for (let repairs = 0, races = 0;;) {
188
+ const used = /* @__PURE__ */ new Map();
189
+ const caughtUp = /* @__PURE__ */ new Map();
190
+ try {
191
+ return await db.transaction(async (tx) => {
192
+ used.clear();
193
+ caughtUp.clear();
194
+ const labels = createLabelAllocator(tx, used, caughtUp);
195
+ const result = await write(tx, labels);
196
+ labels.flush();
197
+ return result;
198
+ });
199
+ } catch (error) {
200
+ const needs = error instanceof NeedsLabelFill ? [error] : error instanceof _hot_updater_plugin_core.DatabaseConstraintError && error.reason === "too_large" ? [...caughtUp.values()] : [];
201
+ if (needs.length > 0) {
202
+ const fresh = needs.some(({ channelId, platform }) => !filled.has(labelSequenceId(channelId, platform)));
203
+ if (fresh || repairs < REPAIRS) {
204
+ if (!fresh) repairs += 1;
205
+ await fill(needs);
206
+ continue;
207
+ }
208
+ }
209
+ if (error instanceof _hot_updater_plugin_core.DatabaseConstraintError && error.reason === "unique" && error.model === "releases" && used.size > 0) {
210
+ if (await db.transaction(async (tx) => {
211
+ const moves = [];
212
+ for (const [id, { channelId, read }] of used) {
213
+ const counter = await tx.findOne("label_sequences", { id });
214
+ if (counter !== null) {
215
+ if (counter.last_label !== read) return false;
216
+ moves.push(() => tx.update("label_sequences", counter, { last_label: read + 1 }));
217
+ continue;
218
+ }
219
+ const channel = await tx.findOne("channels", { id: channelId });
220
+ if (channel === null || (channel._refs_releases_channel_id ?? 0) > 0) return false;
221
+ moves.push(() => tx.create("label_sequences", {
222
+ id,
223
+ last_label: read + 1,
224
+ is_filled: true
225
+ }));
226
+ }
227
+ for (const move of moves) move();
228
+ return true;
229
+ }) ? (repairs += 1) <= REPAIRS : (races += 1) <= RACES) continue;
230
+ }
231
+ throw error;
232
+ }
233
+ }
234
+ };
235
+ /**
236
+ * Labels a scope's releases a server of an earlier release wrote after its
237
+ * newest labeled one, oldest first, before the write takes its own label:
238
+ * `latest` is the scope's newest release, so a labeled one costs no read.
239
+ * Answers the labels it gave, by release ID.
240
+ */
241
+ const catchUpLabels = async (tx, labels, latest) => {
242
+ const given = /* @__PURE__ */ new Map();
243
+ if (latest === null || typeof latest.label === "number") return given;
244
+ const unlabeled = [];
245
+ let cursor;
246
+ scan: do {
247
+ const page = await tx.findMany("releases", {
248
+ index: "byScope",
249
+ where: { scope_key: latest.scope_key },
250
+ order: "desc",
251
+ limit: FILL_BATCH,
252
+ ...cursor === void 0 ? {} : { cursor }
253
+ });
254
+ for (const row of page.rows) {
255
+ if (typeof row.label === "number") break scan;
256
+ unlabeled.push(row);
257
+ }
258
+ cursor = page.next;
259
+ } while (cursor !== void 0);
260
+ labels.caughtUp(latest.channel_id, latest.platform);
261
+ for (const row of unlabeled.reverse()) {
262
+ const { id, channel_id, platform } = require_reads.toReleaseRow(row);
263
+ const label = await labels.next(channel_id, platform);
264
+ tx.update("releases", row, { label });
265
+ given.set(id, label);
266
+ }
267
+ return given;
268
+ };
269
+ /**
270
+ * The channels and platforms whose releases need labels: a scope whose
271
+ * newest release has none, or a pair without a filled counter. Releases a
272
+ * server of an earlier release wrote in a filled pair come after its
273
+ * labeled ones in their scope, so the newest release of each scope tells.
274
+ * Two reads per scope, one per pair.
275
+ */
276
+ const pendingLabelPairs = async (db) => {
277
+ const pairs = /* @__PURE__ */ new Map();
278
+ for (let last = "";;) {
279
+ const { rows } = await db.findMany("releases", {
280
+ index: "scopes",
281
+ where: {},
282
+ limit: 1,
283
+ range: { gt: last }
284
+ });
285
+ if (rows[0] === void 0) break;
286
+ last = rows[0].scope_key;
287
+ const { rows: newest } = await db.findMany("releases", {
288
+ index: "byScope",
289
+ where: { scope_key: last },
290
+ order: "desc",
291
+ limit: 1
292
+ });
293
+ if (newest[0] === void 0) continue;
294
+ const { channel_id: channelId, platform, label } = require_reads.toReleaseRow(newest[0]);
295
+ const id = labelSequenceId(channelId, platform);
296
+ const pair = pairs.get(id) ?? {
297
+ channelId,
298
+ platform,
299
+ pending: false
300
+ };
301
+ pair.pending ||= label === null;
302
+ pairs.set(id, pair);
303
+ }
304
+ const pending = [];
305
+ for (const [id, { channelId, platform, pending: unlabeled }] of pairs) {
306
+ if (!unlabeled) {
307
+ const counter = await db.findOne("label_sequences", { id });
308
+ if (counter !== null && counter.is_filled) continue;
309
+ }
310
+ pending.push({
311
+ channelId,
312
+ platform
313
+ });
314
+ }
315
+ return pending;
316
+ };
317
+ //#endregion
318
+ exports.LABEL_PLATFORMS = LABEL_PLATFORMS;
319
+ exports.NeedsLabelFill = NeedsLabelFill;
320
+ exports.catchUpLabels = catchUpLabels;
321
+ exports.fillLabels = fillLabels;
322
+ exports.labelSequenceId = labelSequenceId;
323
+ exports.pendingLabelPairs = pendingLabelPairs;
324
+ exports.probeLabelSchema = probeLabelSchema;
325
+ exports.writeWithLabels = writeWithLabels;
@@ -0,0 +1,318 @@
1
+ import { toReleaseRow } from "./reads.mjs";
2
+ import { DatabaseConstraintError, isDatabaseBusyError } from "@hot-updater/plugin-core";
3
+ //#region src/core/labels.ts
4
+ /**
5
+ * The counter of a channel and platform's release labels: one
6
+ * `label_sequences` row, which every writer of the pair reads and bumps.
7
+ */
8
+ const labelSequenceId = (channelId, platform) => `releases/${platform}/${channelId}`;
9
+ /** The pairs a channel's counters belong to, which deleting it removes. */
10
+ const LABEL_PLATFORMS = ["ios", "android"];
11
+ /**
12
+ * A channel and platform whose releases written before labels have none
13
+ * yet, or that has no counter: the write stops before taking a label, the
14
+ * core labels the pair with `fillLabels`, and the write runs again.
15
+ */
16
+ var NeedsLabelFill = class extends Error {
17
+ channelId;
18
+ platform;
19
+ constructor(channelId, platform) {
20
+ super(`Release labels of ${platform} ${channelId} need a fill first.`);
21
+ this.channelId = channelId;
22
+ this.platform = platform;
23
+ }
24
+ };
25
+ const createLabelAllocator = (tx, used, caughtUp) => {
26
+ const sequences = /* @__PURE__ */ new Map();
27
+ return {
28
+ async next(channelId, platform) {
29
+ const id = labelSequenceId(channelId, platform);
30
+ let sequence = sequences.get(id);
31
+ if (sequence === void 0) {
32
+ const row = await tx.findOne("label_sequences", { id });
33
+ if (row === null || !row.is_filled) {
34
+ const channel = row === null ? await tx.findOne("channels", { id: channelId }) : null;
35
+ if (channel === null || (channel._refs_releases_channel_id ?? 0) > 0) throw new NeedsLabelFill(channelId, platform);
36
+ }
37
+ const read = row?.last_label ?? 0;
38
+ sequence = {
39
+ row,
40
+ read,
41
+ last: read
42
+ };
43
+ sequences.set(id, sequence);
44
+ used.set(id, {
45
+ channelId,
46
+ platform,
47
+ read
48
+ });
49
+ }
50
+ sequence.last += 1;
51
+ return sequence.last;
52
+ },
53
+ caughtUp(channelId, platform) {
54
+ caughtUp.set(labelSequenceId(channelId, platform), {
55
+ channelId,
56
+ platform
57
+ });
58
+ },
59
+ flush() {
60
+ for (const [id, { row, read, last }] of sequences) if (row === null) tx.create("label_sequences", {
61
+ id,
62
+ last_label: last,
63
+ is_filled: true
64
+ });
65
+ else if (last !== read) tx.update("label_sequences", row, { last_label: last });
66
+ }
67
+ };
68
+ };
69
+ const FILL_BATCH = 25;
70
+ const PAGE = 500;
71
+ const PAUSE_MS = 1e3;
72
+ const MAX_PAUSE_MS = 32e3;
73
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
74
+ /**
75
+ * Labels a channel and platform's releases that have none, in ID order,
76
+ * which is the order they were created in, and marks its counter filled:
77
+ * each batch reads the counter and its rows guarded, so a concurrent fill or
78
+ * write serializes on the counter and skips what another labeled. With
79
+ * `dryRun`, it only counts them. Answers how many it labeled.
80
+ */
81
+ const fillLabels = async (db, { channelId, platform }, { dryRun = false } = {}) => {
82
+ const id = labelSequenceId(channelId, platform);
83
+ let labeled = 0;
84
+ let batchSize = FILL_BATCH;
85
+ let paused = 0;
86
+ const label = async (ids) => {
87
+ for (let at = 0; at < ids.length;) {
88
+ const batch = ids.slice(at, at + batchSize);
89
+ try {
90
+ labeled += await db.transaction(async (tx) => {
91
+ const counter = await tx.findOne("label_sequences", { id });
92
+ let last = counter?.last_label ?? 0;
93
+ let count = 0;
94
+ for (const releaseId of batch) {
95
+ const row = await tx.findOne("releases", { id: releaseId });
96
+ if (row === null || typeof row.label === "number") continue;
97
+ last += 1;
98
+ count += 1;
99
+ tx.update("releases", row, { label: last });
100
+ }
101
+ if (counter === null) tx.create("label_sequences", {
102
+ id,
103
+ last_label: last,
104
+ is_filled: false
105
+ });
106
+ else if (last !== counter.last_label) tx.update("label_sequences", counter, { last_label: last });
107
+ return count;
108
+ });
109
+ at += batch.length;
110
+ paused = 0;
111
+ } catch (error) {
112
+ if (batchSize > 1 && error instanceof DatabaseConstraintError && error.reason === "too_large") {
113
+ batchSize = Math.max(1, Math.floor(batchSize / 2));
114
+ continue;
115
+ }
116
+ if (!isDatabaseBusyError(error) || paused >= MAX_PAUSE_MS) throw error;
117
+ batchSize = Math.max(1, Math.floor(batchSize / 2));
118
+ paused = paused === 0 ? PAUSE_MS : paused * 2;
119
+ await sleep(paused);
120
+ }
121
+ }
122
+ };
123
+ let cursor;
124
+ do {
125
+ const page = await db.findMany("releases", {
126
+ index: "byChannelPlatform",
127
+ where: {
128
+ channel_id: channelId,
129
+ platform
130
+ },
131
+ limit: PAGE,
132
+ ...cursor === void 0 ? {} : { cursor }
133
+ });
134
+ const unlabeled = page.rows.filter((row) => typeof row.label !== "number").map((row) => row.id);
135
+ if (dryRun) labeled += unlabeled.length;
136
+ else await label(unlabeled);
137
+ cursor = page.next;
138
+ } while (cursor !== void 0);
139
+ if (dryRun) return labeled;
140
+ await db.transaction(async (tx) => {
141
+ const counter = await tx.findOne("label_sequences", { id });
142
+ if (counter === null) tx.create("label_sequences", {
143
+ id,
144
+ last_label: 0,
145
+ is_filled: true
146
+ });
147
+ else if (!counter.is_filled) tx.update("label_sequences", counter, { is_filled: true });
148
+ });
149
+ return labeled;
150
+ };
151
+ /**
152
+ * Reads what a pair's fill writes, its counter and its label index, without
153
+ * writing: a database without release labels' schema, such as one a 1.2
154
+ * prerelease migrated, fails here as the fill would. The schema is the
155
+ * database's, so one pair tells. Two reads.
156
+ */
157
+ const probeLabelSchema = async (db, { channelId, platform }) => {
158
+ await db.findOne("label_sequences", { id: labelSequenceId(channelId, platform) });
159
+ await db.findMany("releases", {
160
+ index: "byLabel",
161
+ where: {
162
+ channel_id: channelId,
163
+ platform,
164
+ label: 0
165
+ },
166
+ limit: 1
167
+ });
168
+ };
169
+ const REPAIRS = 4;
170
+ const RACES = 32;
171
+ /**
172
+ * Runs a release write that takes labels, in one transaction with the
173
+ * counters it bumps. A pair that still needs its fill is filled, and the
174
+ * write runs again; so is a pair whose catch-up made the write too large for
175
+ * the store. A label another row holds, such as an index item a release
176
+ * deleted by a server of an earlier release left on a key-value store, is
177
+ * skipped: its counters move past it and the write runs again.
178
+ */
179
+ const writeWithLabels = async (db, write) => {
180
+ const filled = /* @__PURE__ */ new Set();
181
+ const fill = async (pairs) => {
182
+ for (const pair of pairs) {
183
+ filled.add(labelSequenceId(pair.channelId, pair.platform));
184
+ await fillLabels(db, pair);
185
+ }
186
+ };
187
+ for (let repairs = 0, races = 0;;) {
188
+ const used = /* @__PURE__ */ new Map();
189
+ const caughtUp = /* @__PURE__ */ new Map();
190
+ try {
191
+ return await db.transaction(async (tx) => {
192
+ used.clear();
193
+ caughtUp.clear();
194
+ const labels = createLabelAllocator(tx, used, caughtUp);
195
+ const result = await write(tx, labels);
196
+ labels.flush();
197
+ return result;
198
+ });
199
+ } catch (error) {
200
+ const needs = error instanceof NeedsLabelFill ? [error] : error instanceof DatabaseConstraintError && error.reason === "too_large" ? [...caughtUp.values()] : [];
201
+ if (needs.length > 0) {
202
+ const fresh = needs.some(({ channelId, platform }) => !filled.has(labelSequenceId(channelId, platform)));
203
+ if (fresh || repairs < REPAIRS) {
204
+ if (!fresh) repairs += 1;
205
+ await fill(needs);
206
+ continue;
207
+ }
208
+ }
209
+ if (error instanceof DatabaseConstraintError && error.reason === "unique" && error.model === "releases" && used.size > 0) {
210
+ if (await db.transaction(async (tx) => {
211
+ const moves = [];
212
+ for (const [id, { channelId, read }] of used) {
213
+ const counter = await tx.findOne("label_sequences", { id });
214
+ if (counter !== null) {
215
+ if (counter.last_label !== read) return false;
216
+ moves.push(() => tx.update("label_sequences", counter, { last_label: read + 1 }));
217
+ continue;
218
+ }
219
+ const channel = await tx.findOne("channels", { id: channelId });
220
+ if (channel === null || (channel._refs_releases_channel_id ?? 0) > 0) return false;
221
+ moves.push(() => tx.create("label_sequences", {
222
+ id,
223
+ last_label: read + 1,
224
+ is_filled: true
225
+ }));
226
+ }
227
+ for (const move of moves) move();
228
+ return true;
229
+ }) ? (repairs += 1) <= REPAIRS : (races += 1) <= RACES) continue;
230
+ }
231
+ throw error;
232
+ }
233
+ }
234
+ };
235
+ /**
236
+ * Labels a scope's releases a server of an earlier release wrote after its
237
+ * newest labeled one, oldest first, before the write takes its own label:
238
+ * `latest` is the scope's newest release, so a labeled one costs no read.
239
+ * Answers the labels it gave, by release ID.
240
+ */
241
+ const catchUpLabels = async (tx, labels, latest) => {
242
+ const given = /* @__PURE__ */ new Map();
243
+ if (latest === null || typeof latest.label === "number") return given;
244
+ const unlabeled = [];
245
+ let cursor;
246
+ scan: do {
247
+ const page = await tx.findMany("releases", {
248
+ index: "byScope",
249
+ where: { scope_key: latest.scope_key },
250
+ order: "desc",
251
+ limit: FILL_BATCH,
252
+ ...cursor === void 0 ? {} : { cursor }
253
+ });
254
+ for (const row of page.rows) {
255
+ if (typeof row.label === "number") break scan;
256
+ unlabeled.push(row);
257
+ }
258
+ cursor = page.next;
259
+ } while (cursor !== void 0);
260
+ labels.caughtUp(latest.channel_id, latest.platform);
261
+ for (const row of unlabeled.reverse()) {
262
+ const { id, channel_id, platform } = toReleaseRow(row);
263
+ const label = await labels.next(channel_id, platform);
264
+ tx.update("releases", row, { label });
265
+ given.set(id, label);
266
+ }
267
+ return given;
268
+ };
269
+ /**
270
+ * The channels and platforms whose releases need labels: a scope whose
271
+ * newest release has none, or a pair without a filled counter. Releases a
272
+ * server of an earlier release wrote in a filled pair come after its
273
+ * labeled ones in their scope, so the newest release of each scope tells.
274
+ * Two reads per scope, one per pair.
275
+ */
276
+ const pendingLabelPairs = async (db) => {
277
+ const pairs = /* @__PURE__ */ new Map();
278
+ for (let last = "";;) {
279
+ const { rows } = await db.findMany("releases", {
280
+ index: "scopes",
281
+ where: {},
282
+ limit: 1,
283
+ range: { gt: last }
284
+ });
285
+ if (rows[0] === void 0) break;
286
+ last = rows[0].scope_key;
287
+ const { rows: newest } = await db.findMany("releases", {
288
+ index: "byScope",
289
+ where: { scope_key: last },
290
+ order: "desc",
291
+ limit: 1
292
+ });
293
+ if (newest[0] === void 0) continue;
294
+ const { channel_id: channelId, platform, label } = toReleaseRow(newest[0]);
295
+ const id = labelSequenceId(channelId, platform);
296
+ const pair = pairs.get(id) ?? {
297
+ channelId,
298
+ platform,
299
+ pending: false
300
+ };
301
+ pair.pending ||= label === null;
302
+ pairs.set(id, pair);
303
+ }
304
+ const pending = [];
305
+ for (const [id, { channelId, platform, pending: unlabeled }] of pairs) {
306
+ if (!unlabeled) {
307
+ const counter = await db.findOne("label_sequences", { id });
308
+ if (counter !== null && counter.is_filled) continue;
309
+ }
310
+ pending.push({
311
+ channelId,
312
+ platform
313
+ });
314
+ }
315
+ return pending;
316
+ };
317
+ //#endregion
318
+ export { LABEL_PLATFORMS, NeedsLabelFill, catchUpLabels, fillLabels, labelSequenceId, pendingLabelPairs, probeLabelSchema, writeWithLabels };
@@ -0,0 +1,13 @@
1
+ //#region src/core/nativeBuildDestinations.ts
2
+ /** Store destinations accepted for each native platform. */
3
+ const NATIVE_BUILD_SUBMISSION_DESTINATIONS = {
4
+ android: [
5
+ "play-internal",
6
+ "play-closed",
7
+ "play-open",
8
+ "play-production"
9
+ ],
10
+ ios: ["testflight"]
11
+ };
12
+ //#endregion
13
+ exports.NATIVE_BUILD_SUBMISSION_DESTINATIONS = NATIVE_BUILD_SUBMISSION_DESTINATIONS;
@@ -0,0 +1,13 @@
1
+ //#region src/core/nativeBuildDestinations.ts
2
+ /** Store destinations accepted for each native platform. */
3
+ const NATIVE_BUILD_SUBMISSION_DESTINATIONS = {
4
+ android: [
5
+ "play-internal",
6
+ "play-closed",
7
+ "play-open",
8
+ "play-production"
9
+ ],
10
+ ios: ["testflight"]
11
+ };
12
+ //#endregion
13
+ export { NATIVE_BUILD_SUBMISSION_DESTINATIONS };