@hot-updater/server 1.2.0-rc.5 → 1.2.0-rc.51

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 (51) 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 +317 -0
  24. package/dist/core/labels.mjs +310 -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 +121 -38
  34. package/dist/core/releases.mjs +121 -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/handlerAdminRoutes.cjs +288 -16
  46. package/dist/handlerAdminRoutes.mjs +289 -17
  47. package/dist/handlerReleaseCatalogRoutes.cjs +43 -27
  48. package/dist/handlerReleaseCatalogRoutes.mjs +43 -27
  49. package/dist/package.cjs +1 -1
  50. package/dist/package.mjs +1 -1
  51. package/package.json +7 -14
@@ -0,0 +1,306 @@
1
+ const require_reads = require("./reads.cjs");
2
+ let _hot_updater_plugin_core = require("@hot-updater/plugin-core");
3
+ //#region src/core/iosDevices.ts
4
+ /**
5
+ * A device's UDID: 40 hex digits, or 8 and 16 joined by a hyphen. The same
6
+ * rule as `normalizeIosUdid` of `@hot-updater/protocol/native-artifact`,
7
+ * implemented here so the server's runtime never loads the artifact readers.
8
+ */
9
+ const UDID = /^(?:[0-9a-f]{40}|[0-9a-f]{8}-[0-9a-f]{16})$/iu;
10
+ /** The form the registry stores and compares a UDID in, or null for text that isn't one. */
11
+ const normalizeIosUdid = (value) => {
12
+ const trimmed = value.trim();
13
+ return UDID.test(trimmed) ? trimmed.toLowerCase() : null;
14
+ };
15
+ /** `ios_devices.name`'s bound in core's schema. */
16
+ const NAME_MAX_LENGTH = 100;
17
+ /** `ios_devices.requested_by` and `approved_by`'s bound: an email address. */
18
+ const EMAIL_MAX_LENGTH = 320;
19
+ /** `ios_devices.apple_issuer_id`'s bound. */
20
+ const ISSUER_MAX_LENGTH = 64;
21
+ /** The one row of `ios_device_account`. */
22
+ const ACCOUNT_ROW = "registry";
23
+ /** `ios_devices.apple_error`'s bound. */
24
+ const ERROR_MAX_LENGTH = 512;
25
+ const STATUSES = [
26
+ "pending",
27
+ "approved",
28
+ "registered"
29
+ ];
30
+ const OUTCOMES = [
31
+ "registered",
32
+ "refused",
33
+ "disabled",
34
+ "missing"
35
+ ];
36
+ /** The `apple_error` of a device Apple has disabled. */
37
+ const IOS_DEVICE_DISABLED_ERROR = "Disabled in the Apple account.";
38
+ /** The `apple_error` of a registered device Apple no longer lists. */
39
+ const IOS_DEVICE_MISSING_ERROR = "Gone from the Apple account. Approve it again to register it.";
40
+ /** A device or registry request core cannot store: the caller's input. */
41
+ var IosDeviceInputError = class extends TypeError {
42
+ name = "IosDeviceInputError";
43
+ };
44
+ const pick = (row) => ({
45
+ id: row.id,
46
+ udid: row.udid,
47
+ name: row.name,
48
+ status: row.status,
49
+ requested_by: row.requested_by ?? null,
50
+ created_at_ms: row.created_at_ms,
51
+ approved_by: row.approved_by ?? null,
52
+ approved_at_ms: row.approved_at_ms ?? null,
53
+ registered_at_ms: row.registered_at_ms ?? null,
54
+ apple_issuer_id: row.apple_issuer_id ?? null,
55
+ apple_error: row.apple_error ?? null,
56
+ build_id: row.build_id ?? null
57
+ });
58
+ const udidOf = (udid) => {
59
+ const key = typeof udid === "string" ? normalizeIosUdid(udid) : null;
60
+ if (key === null) throw new IosDeviceInputError("Invalid iOS device: udid must be 40 hex digits, or 8 and 16 joined by a hyphen.");
61
+ return key;
62
+ };
63
+ const nameOf = (name) => {
64
+ const trimmed = typeof name === "string" ? name.trim() : "";
65
+ if (trimmed.length === 0 || trimmed.length > NAME_MAX_LENGTH) throw new IosDeviceInputError(`Invalid iOS device: name must be 1 to ${NAME_MAX_LENGTH} characters.`);
66
+ return trimmed;
67
+ };
68
+ /** An email, lower-cased, or null for the CLI. */
69
+ const personOf = (value, field) => {
70
+ if (value === null) return null;
71
+ const email = typeof value === "string" ? value.trim().toLowerCase() : "";
72
+ if (email.length === 0 || email.length > EMAIL_MAX_LENGTH) throw new IosDeviceInputError(`Invalid iOS device: ${field} must be an email of at most ${EMAIL_MAX_LENGTH} characters, or null.`);
73
+ return email;
74
+ };
75
+ /** Apple's words, on one line and within the column's bound. */
76
+ const errorText = (text) => {
77
+ const line = text.replace(/\s+/gu, " ").trim();
78
+ return line.length > ERROR_MAX_LENGTH ? `${line.slice(0, 511)}…` : line;
79
+ };
80
+ /**
81
+ * The iOS device registry: the iPhones and iPads a team asked to install ad hoc
82
+ * builds on. The Console writes requests and approvals, and the CLI what Apple
83
+ * said, each over the row it read.
84
+ */
85
+ const createIosDeviceApi = (db, { now = Date.now } = {}) => {
86
+ const findIosDevice = async (udid) => {
87
+ const key = normalizeIosUdid(udid);
88
+ if (key === null) return null;
89
+ const row = await db.findOne("ios_devices", { udid: key });
90
+ return row === null ? null : pick(row);
91
+ };
92
+ /**
93
+ * The issuer of the registry's earliest registration among its rows, by
94
+ * when and then by ID, or null when no row names one: read only while the
95
+ * registry has no account yet, in one read of its first registration. A
96
+ * row names an issuer exactly when it has a registration time: both are
97
+ * set by its first registration and never cleared.
98
+ */
99
+ const accountOfRows = async () => {
100
+ const { rows } = await db.findMany("ios_devices", {
101
+ index: "byRegistration",
102
+ where: {},
103
+ order: "asc",
104
+ limit: 1,
105
+ range: { gte: 0 }
106
+ });
107
+ return (rows[0] === void 0 ? null : pick(rows[0]))?.apple_issuer_id ?? null;
108
+ };
109
+ return {
110
+ async listIosDevices(input) {
111
+ if (input.status !== void 0 && !STATUSES.includes(input.status)) throw new IosDeviceInputError(`Invalid iOS device status: ${String(input.status)}. Expected ${STATUSES.join(", ")}.`);
112
+ const page = {
113
+ order: input.order ?? "asc",
114
+ limit: input.limit,
115
+ ...require_reads.after(input)
116
+ };
117
+ const { rows } = input.status === void 0 ? await db.findMany("ios_devices", {
118
+ index: "all",
119
+ where: {},
120
+ ...page
121
+ }) : await db.findMany("ios_devices", {
122
+ index: "byStatus",
123
+ where: { status: input.status },
124
+ ...page
125
+ });
126
+ return rows.map(pick);
127
+ },
128
+ findIosDevice,
129
+ async requestIosDevice(input) {
130
+ const udid = udidOf(input.udid);
131
+ const name = nameOf(input.name);
132
+ const requestedBy = personOf(input.requestedBy, "requestedBy");
133
+ const existing = await db.findOne("ios_devices", { udid });
134
+ if (existing !== null) return {
135
+ device: pick(existing),
136
+ created: false
137
+ };
138
+ const device = {
139
+ id: (0, _hot_updater_plugin_core.createUUIDv7)(),
140
+ udid,
141
+ name,
142
+ status: "pending",
143
+ requested_by: requestedBy,
144
+ created_at_ms: now(),
145
+ approved_by: null,
146
+ approved_at_ms: null,
147
+ registered_at_ms: null,
148
+ apple_issuer_id: null,
149
+ apple_error: null,
150
+ build_id: null
151
+ };
152
+ try {
153
+ await db.transaction(async (tx) => {
154
+ tx.create("ios_devices", device);
155
+ });
156
+ } catch (error) {
157
+ if (error instanceof _hot_updater_plugin_core.DatabaseConstraintError && error.reason === "unique") {
158
+ const winner = await db.findOne("ios_devices", { udid });
159
+ if (winner !== null) return {
160
+ device: pick(winner),
161
+ created: false
162
+ };
163
+ }
164
+ throw error;
165
+ }
166
+ return {
167
+ device,
168
+ created: true
169
+ };
170
+ },
171
+ async approveIosDevice(id, input) {
172
+ const name = input.name === void 0 ? void 0 : nameOf(input.name);
173
+ const approvedBy = personOf(input.approvedBy, "approvedBy");
174
+ return db.transaction(async (tx) => {
175
+ const row = await tx.findOne("ios_devices", { id });
176
+ if (row === null) return null;
177
+ const device = pick(row);
178
+ const set = {
179
+ ...name === void 0 || name === device.name ? {} : { name },
180
+ ...device.status === "pending" ? {
181
+ status: "approved",
182
+ approved_by: approvedBy,
183
+ approved_at_ms: now()
184
+ } : {}
185
+ };
186
+ if (Object.keys(set).length === 0) return device;
187
+ tx.update("ios_devices", row, set);
188
+ return {
189
+ ...device,
190
+ ...set
191
+ };
192
+ });
193
+ },
194
+ async renameIosDevice(id, input) {
195
+ const name = nameOf(input);
196
+ return db.transaction(async (tx) => {
197
+ const row = await tx.findOne("ios_devices", { id });
198
+ if (row === null) return null;
199
+ const device = pick(row);
200
+ if (device.name === name) return device;
201
+ tx.update("ios_devices", row, { name });
202
+ return {
203
+ ...device,
204
+ name
205
+ };
206
+ });
207
+ },
208
+ async recordIosDeviceOutcome({ id, seen, issuerId, outcome, reason }) {
209
+ if (!OUTCOMES.includes(outcome)) throw new IosDeviceInputError(`Invalid iOS device outcome: ${String(outcome)}. Expected ${OUTCOMES.join(", ")}.`);
210
+ if (typeof issuerId !== "string" || issuerId.trim().length === 0 || issuerId.length > ISSUER_MAX_LENGTH) throw new IosDeviceInputError(`Invalid iOS device outcome: issuerId must be an App Store Connect issuer of at most ${ISSUER_MAX_LENGTH} characters.`);
211
+ return db.transaction(async (tx) => {
212
+ const row = await tx.findOne("ios_devices", { id });
213
+ if (row === null) return null;
214
+ const device = pick(row);
215
+ if (device.status !== seen.status || device.approved_at_ms !== seen.approvedAtMs || device.apple_issuer_id !== null && device.apple_issuer_id !== issuerId) return null;
216
+ let set;
217
+ switch (outcome) {
218
+ case "registered":
219
+ if (device.status === "pending") return null;
220
+ set = {
221
+ status: "registered",
222
+ registered_at_ms: device.registered_at_ms ?? now(),
223
+ apple_issuer_id: issuerId,
224
+ apple_error: null
225
+ };
226
+ break;
227
+ case "refused":
228
+ if (device.status !== "approved") return null;
229
+ set = { apple_error: errorText(reason === void 0 || reason.trim() === "" ? "Apple refused it." : reason) };
230
+ break;
231
+ case "disabled":
232
+ if (device.status === "pending") return null;
233
+ set = { apple_error: IOS_DEVICE_DISABLED_ERROR };
234
+ break;
235
+ case "missing":
236
+ if (device.status !== "registered") return null;
237
+ set = {
238
+ status: "pending",
239
+ approved_by: null,
240
+ approved_at_ms: null,
241
+ apple_error: IOS_DEVICE_MISSING_ERROR
242
+ };
243
+ }
244
+ if (Object.entries(set).some(([column, value]) => device[column] !== (value ?? null))) tx.update("ios_devices", row, set);
245
+ return {
246
+ ...device,
247
+ ...set
248
+ };
249
+ });
250
+ },
251
+ async markIosDevicesInBuild({ buildId, ids }) {
252
+ for (const id of new Set(ids)) await db.transaction(async (tx) => {
253
+ const row = await tx.findOne("ios_devices", { id });
254
+ if (row === null || typeof row.build_id === "string" && row.build_id >= buildId) return;
255
+ tx.update("ios_devices", row, { build_id: buildId });
256
+ });
257
+ },
258
+ async findIosDeviceAccount() {
259
+ const existing = await db.findOne("ios_device_account", { id: ACCOUNT_ROW });
260
+ const issuerId = existing === null ? await accountOfRows() : existing.apple_issuer_id;
261
+ return issuerId === null ? null : { issuerId };
262
+ },
263
+ async claimIosDeviceAccount(issuerId) {
264
+ if (typeof issuerId !== "string" || issuerId.trim().length === 0 || issuerId.length > ISSUER_MAX_LENGTH) throw new IosDeviceInputError(`Invalid iOS device account: issuerId must be an App Store Connect issuer of at most ${ISSUER_MAX_LENGTH} characters.`);
265
+ const answer = (held) => ({
266
+ issuerId: held,
267
+ claimed: held === issuerId
268
+ });
269
+ const existing = await db.findOne("ios_device_account", { id: ACCOUNT_ROW });
270
+ if (existing !== null) return answer(existing.apple_issuer_id);
271
+ const holder = await accountOfRows() ?? issuerId;
272
+ try {
273
+ await db.transaction(async (tx) => {
274
+ tx.create("ios_device_account", {
275
+ id: ACCOUNT_ROW,
276
+ apple_issuer_id: holder,
277
+ claimed_at_ms: now()
278
+ });
279
+ });
280
+ return answer(holder);
281
+ } catch (error) {
282
+ if (error instanceof _hot_updater_plugin_core.DatabaseConstraintError && (error.reason === "exists" || error.reason === "unique")) {
283
+ const winner = await db.findOne("ios_device_account", { id: ACCOUNT_ROW });
284
+ if (winner !== null) return answer(winner.apple_issuer_id);
285
+ }
286
+ throw error;
287
+ }
288
+ },
289
+ async deleteIosDevice(id, only) {
290
+ const requestedBy = only?.requestedBy === void 0 ? void 0 : personOf(only.requestedBy, "requestedBy");
291
+ return db.transaction(async (tx) => {
292
+ const row = await tx.findOne("ios_devices", { id });
293
+ if (row === null) return null;
294
+ const device = pick(row);
295
+ if (only?.status !== void 0 && device.status !== only.status || requestedBy !== void 0 && device.requested_by !== requestedBy) return null;
296
+ await tx.delete("ios_devices", row);
297
+ return device;
298
+ });
299
+ }
300
+ };
301
+ };
302
+ //#endregion
303
+ exports.IOS_DEVICE_DISABLED_ERROR = IOS_DEVICE_DISABLED_ERROR;
304
+ exports.IOS_DEVICE_MISSING_ERROR = IOS_DEVICE_MISSING_ERROR;
305
+ exports.IosDeviceInputError = IosDeviceInputError;
306
+ exports.createIosDeviceApi = createIosDeviceApi;
@@ -0,0 +1,303 @@
1
+ import { after } from "./reads.mjs";
2
+ import { DatabaseConstraintError, createUUIDv7 } from "@hot-updater/plugin-core";
3
+ //#region src/core/iosDevices.ts
4
+ /**
5
+ * A device's UDID: 40 hex digits, or 8 and 16 joined by a hyphen. The same
6
+ * rule as `normalizeIosUdid` of `@hot-updater/protocol/native-artifact`,
7
+ * implemented here so the server's runtime never loads the artifact readers.
8
+ */
9
+ const UDID = /^(?:[0-9a-f]{40}|[0-9a-f]{8}-[0-9a-f]{16})$/iu;
10
+ /** The form the registry stores and compares a UDID in, or null for text that isn't one. */
11
+ const normalizeIosUdid = (value) => {
12
+ const trimmed = value.trim();
13
+ return UDID.test(trimmed) ? trimmed.toLowerCase() : null;
14
+ };
15
+ /** `ios_devices.name`'s bound in core's schema. */
16
+ const NAME_MAX_LENGTH = 100;
17
+ /** `ios_devices.requested_by` and `approved_by`'s bound: an email address. */
18
+ const EMAIL_MAX_LENGTH = 320;
19
+ /** `ios_devices.apple_issuer_id`'s bound. */
20
+ const ISSUER_MAX_LENGTH = 64;
21
+ /** The one row of `ios_device_account`. */
22
+ const ACCOUNT_ROW = "registry";
23
+ /** `ios_devices.apple_error`'s bound. */
24
+ const ERROR_MAX_LENGTH = 512;
25
+ const STATUSES = [
26
+ "pending",
27
+ "approved",
28
+ "registered"
29
+ ];
30
+ const OUTCOMES = [
31
+ "registered",
32
+ "refused",
33
+ "disabled",
34
+ "missing"
35
+ ];
36
+ /** The `apple_error` of a device Apple has disabled. */
37
+ const IOS_DEVICE_DISABLED_ERROR = "Disabled in the Apple account.";
38
+ /** The `apple_error` of a registered device Apple no longer lists. */
39
+ const IOS_DEVICE_MISSING_ERROR = "Gone from the Apple account. Approve it again to register it.";
40
+ /** A device or registry request core cannot store: the caller's input. */
41
+ var IosDeviceInputError = class extends TypeError {
42
+ name = "IosDeviceInputError";
43
+ };
44
+ const pick = (row) => ({
45
+ id: row.id,
46
+ udid: row.udid,
47
+ name: row.name,
48
+ status: row.status,
49
+ requested_by: row.requested_by ?? null,
50
+ created_at_ms: row.created_at_ms,
51
+ approved_by: row.approved_by ?? null,
52
+ approved_at_ms: row.approved_at_ms ?? null,
53
+ registered_at_ms: row.registered_at_ms ?? null,
54
+ apple_issuer_id: row.apple_issuer_id ?? null,
55
+ apple_error: row.apple_error ?? null,
56
+ build_id: row.build_id ?? null
57
+ });
58
+ const udidOf = (udid) => {
59
+ const key = typeof udid === "string" ? normalizeIosUdid(udid) : null;
60
+ if (key === null) throw new IosDeviceInputError("Invalid iOS device: udid must be 40 hex digits, or 8 and 16 joined by a hyphen.");
61
+ return key;
62
+ };
63
+ const nameOf = (name) => {
64
+ const trimmed = typeof name === "string" ? name.trim() : "";
65
+ if (trimmed.length === 0 || trimmed.length > NAME_MAX_LENGTH) throw new IosDeviceInputError(`Invalid iOS device: name must be 1 to ${NAME_MAX_LENGTH} characters.`);
66
+ return trimmed;
67
+ };
68
+ /** An email, lower-cased, or null for the CLI. */
69
+ const personOf = (value, field) => {
70
+ if (value === null) return null;
71
+ const email = typeof value === "string" ? value.trim().toLowerCase() : "";
72
+ if (email.length === 0 || email.length > EMAIL_MAX_LENGTH) throw new IosDeviceInputError(`Invalid iOS device: ${field} must be an email of at most ${EMAIL_MAX_LENGTH} characters, or null.`);
73
+ return email;
74
+ };
75
+ /** Apple's words, on one line and within the column's bound. */
76
+ const errorText = (text) => {
77
+ const line = text.replace(/\s+/gu, " ").trim();
78
+ return line.length > ERROR_MAX_LENGTH ? `${line.slice(0, 511)}…` : line;
79
+ };
80
+ /**
81
+ * The iOS device registry: the iPhones and iPads a team asked to install ad hoc
82
+ * builds on. The Console writes requests and approvals, and the CLI what Apple
83
+ * said, each over the row it read.
84
+ */
85
+ const createIosDeviceApi = (db, { now = Date.now } = {}) => {
86
+ const findIosDevice = async (udid) => {
87
+ const key = normalizeIosUdid(udid);
88
+ if (key === null) return null;
89
+ const row = await db.findOne("ios_devices", { udid: key });
90
+ return row === null ? null : pick(row);
91
+ };
92
+ /**
93
+ * The issuer of the registry's earliest registration among its rows, by
94
+ * when and then by ID, or null when no row names one: read only while the
95
+ * registry has no account yet, in one read of its first registration. A
96
+ * row names an issuer exactly when it has a registration time: both are
97
+ * set by its first registration and never cleared.
98
+ */
99
+ const accountOfRows = async () => {
100
+ const { rows } = await db.findMany("ios_devices", {
101
+ index: "byRegistration",
102
+ where: {},
103
+ order: "asc",
104
+ limit: 1,
105
+ range: { gte: 0 }
106
+ });
107
+ return (rows[0] === void 0 ? null : pick(rows[0]))?.apple_issuer_id ?? null;
108
+ };
109
+ return {
110
+ async listIosDevices(input) {
111
+ if (input.status !== void 0 && !STATUSES.includes(input.status)) throw new IosDeviceInputError(`Invalid iOS device status: ${String(input.status)}. Expected ${STATUSES.join(", ")}.`);
112
+ const page = {
113
+ order: input.order ?? "asc",
114
+ limit: input.limit,
115
+ ...after(input)
116
+ };
117
+ const { rows } = input.status === void 0 ? await db.findMany("ios_devices", {
118
+ index: "all",
119
+ where: {},
120
+ ...page
121
+ }) : await db.findMany("ios_devices", {
122
+ index: "byStatus",
123
+ where: { status: input.status },
124
+ ...page
125
+ });
126
+ return rows.map(pick);
127
+ },
128
+ findIosDevice,
129
+ async requestIosDevice(input) {
130
+ const udid = udidOf(input.udid);
131
+ const name = nameOf(input.name);
132
+ const requestedBy = personOf(input.requestedBy, "requestedBy");
133
+ const existing = await db.findOne("ios_devices", { udid });
134
+ if (existing !== null) return {
135
+ device: pick(existing),
136
+ created: false
137
+ };
138
+ const device = {
139
+ id: createUUIDv7(),
140
+ udid,
141
+ name,
142
+ status: "pending",
143
+ requested_by: requestedBy,
144
+ created_at_ms: now(),
145
+ approved_by: null,
146
+ approved_at_ms: null,
147
+ registered_at_ms: null,
148
+ apple_issuer_id: null,
149
+ apple_error: null,
150
+ build_id: null
151
+ };
152
+ try {
153
+ await db.transaction(async (tx) => {
154
+ tx.create("ios_devices", device);
155
+ });
156
+ } catch (error) {
157
+ if (error instanceof DatabaseConstraintError && error.reason === "unique") {
158
+ const winner = await db.findOne("ios_devices", { udid });
159
+ if (winner !== null) return {
160
+ device: pick(winner),
161
+ created: false
162
+ };
163
+ }
164
+ throw error;
165
+ }
166
+ return {
167
+ device,
168
+ created: true
169
+ };
170
+ },
171
+ async approveIosDevice(id, input) {
172
+ const name = input.name === void 0 ? void 0 : nameOf(input.name);
173
+ const approvedBy = personOf(input.approvedBy, "approvedBy");
174
+ return db.transaction(async (tx) => {
175
+ const row = await tx.findOne("ios_devices", { id });
176
+ if (row === null) return null;
177
+ const device = pick(row);
178
+ const set = {
179
+ ...name === void 0 || name === device.name ? {} : { name },
180
+ ...device.status === "pending" ? {
181
+ status: "approved",
182
+ approved_by: approvedBy,
183
+ approved_at_ms: now()
184
+ } : {}
185
+ };
186
+ if (Object.keys(set).length === 0) return device;
187
+ tx.update("ios_devices", row, set);
188
+ return {
189
+ ...device,
190
+ ...set
191
+ };
192
+ });
193
+ },
194
+ async renameIosDevice(id, input) {
195
+ const name = nameOf(input);
196
+ return db.transaction(async (tx) => {
197
+ const row = await tx.findOne("ios_devices", { id });
198
+ if (row === null) return null;
199
+ const device = pick(row);
200
+ if (device.name === name) return device;
201
+ tx.update("ios_devices", row, { name });
202
+ return {
203
+ ...device,
204
+ name
205
+ };
206
+ });
207
+ },
208
+ async recordIosDeviceOutcome({ id, seen, issuerId, outcome, reason }) {
209
+ if (!OUTCOMES.includes(outcome)) throw new IosDeviceInputError(`Invalid iOS device outcome: ${String(outcome)}. Expected ${OUTCOMES.join(", ")}.`);
210
+ if (typeof issuerId !== "string" || issuerId.trim().length === 0 || issuerId.length > ISSUER_MAX_LENGTH) throw new IosDeviceInputError(`Invalid iOS device outcome: issuerId must be an App Store Connect issuer of at most ${ISSUER_MAX_LENGTH} characters.`);
211
+ return db.transaction(async (tx) => {
212
+ const row = await tx.findOne("ios_devices", { id });
213
+ if (row === null) return null;
214
+ const device = pick(row);
215
+ if (device.status !== seen.status || device.approved_at_ms !== seen.approvedAtMs || device.apple_issuer_id !== null && device.apple_issuer_id !== issuerId) return null;
216
+ let set;
217
+ switch (outcome) {
218
+ case "registered":
219
+ if (device.status === "pending") return null;
220
+ set = {
221
+ status: "registered",
222
+ registered_at_ms: device.registered_at_ms ?? now(),
223
+ apple_issuer_id: issuerId,
224
+ apple_error: null
225
+ };
226
+ break;
227
+ case "refused":
228
+ if (device.status !== "approved") return null;
229
+ set = { apple_error: errorText(reason === void 0 || reason.trim() === "" ? "Apple refused it." : reason) };
230
+ break;
231
+ case "disabled":
232
+ if (device.status === "pending") return null;
233
+ set = { apple_error: IOS_DEVICE_DISABLED_ERROR };
234
+ break;
235
+ case "missing":
236
+ if (device.status !== "registered") return null;
237
+ set = {
238
+ status: "pending",
239
+ approved_by: null,
240
+ approved_at_ms: null,
241
+ apple_error: IOS_DEVICE_MISSING_ERROR
242
+ };
243
+ }
244
+ if (Object.entries(set).some(([column, value]) => device[column] !== (value ?? null))) tx.update("ios_devices", row, set);
245
+ return {
246
+ ...device,
247
+ ...set
248
+ };
249
+ });
250
+ },
251
+ async markIosDevicesInBuild({ buildId, ids }) {
252
+ for (const id of new Set(ids)) await db.transaction(async (tx) => {
253
+ const row = await tx.findOne("ios_devices", { id });
254
+ if (row === null || typeof row.build_id === "string" && row.build_id >= buildId) return;
255
+ tx.update("ios_devices", row, { build_id: buildId });
256
+ });
257
+ },
258
+ async findIosDeviceAccount() {
259
+ const existing = await db.findOne("ios_device_account", { id: ACCOUNT_ROW });
260
+ const issuerId = existing === null ? await accountOfRows() : existing.apple_issuer_id;
261
+ return issuerId === null ? null : { issuerId };
262
+ },
263
+ async claimIosDeviceAccount(issuerId) {
264
+ if (typeof issuerId !== "string" || issuerId.trim().length === 0 || issuerId.length > ISSUER_MAX_LENGTH) throw new IosDeviceInputError(`Invalid iOS device account: issuerId must be an App Store Connect issuer of at most ${ISSUER_MAX_LENGTH} characters.`);
265
+ const answer = (held) => ({
266
+ issuerId: held,
267
+ claimed: held === issuerId
268
+ });
269
+ const existing = await db.findOne("ios_device_account", { id: ACCOUNT_ROW });
270
+ if (existing !== null) return answer(existing.apple_issuer_id);
271
+ const holder = await accountOfRows() ?? issuerId;
272
+ try {
273
+ await db.transaction(async (tx) => {
274
+ tx.create("ios_device_account", {
275
+ id: ACCOUNT_ROW,
276
+ apple_issuer_id: holder,
277
+ claimed_at_ms: now()
278
+ });
279
+ });
280
+ return answer(holder);
281
+ } catch (error) {
282
+ if (error instanceof DatabaseConstraintError && (error.reason === "exists" || error.reason === "unique")) {
283
+ const winner = await db.findOne("ios_device_account", { id: ACCOUNT_ROW });
284
+ if (winner !== null) return answer(winner.apple_issuer_id);
285
+ }
286
+ throw error;
287
+ }
288
+ },
289
+ async deleteIosDevice(id, only) {
290
+ const requestedBy = only?.requestedBy === void 0 ? void 0 : personOf(only.requestedBy, "requestedBy");
291
+ return db.transaction(async (tx) => {
292
+ const row = await tx.findOne("ios_devices", { id });
293
+ if (row === null) return null;
294
+ const device = pick(row);
295
+ if (only?.status !== void 0 && device.status !== only.status || requestedBy !== void 0 && device.requested_by !== requestedBy) return null;
296
+ await tx.delete("ios_devices", row);
297
+ return device;
298
+ });
299
+ }
300
+ };
301
+ };
302
+ //#endregion
303
+ export { IOS_DEVICE_DISABLED_ERROR, IOS_DEVICE_MISSING_ERROR, IosDeviceInputError, createIosDeviceApi };