@hot-updater/server 1.2.0-rc.4 → 1.2.0-rc.40
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.
- package/dist/core/api.cjs +2 -0
- package/dist/core/api.d.cts +29 -0
- package/dist/core/api.d.mts +29 -0
- package/dist/core/api.mjs +2 -0
- package/dist/core/iosDevices.cjs +309 -0
- package/dist/core/iosDevices.mjs +306 -0
- package/dist/core/nativeBuildDestinations.cjs +13 -0
- package/dist/core/nativeBuildDestinations.mjs +13 -0
- package/dist/core/nativeBuilds.cjs +142 -24
- package/dist/core/nativeBuilds.mjs +142 -25
- package/dist/core/operations.cjs +6 -2
- package/dist/core/operations.mjs +7 -3
- package/dist/core/reads.cjs +9 -1
- package/dist/core/reads.mjs +9 -1
- package/dist/core/writes.cjs +4 -1
- package/dist/core/writes.mjs +4 -1
- package/dist/createHotUpdaterCore.d.cts +2 -4
- package/dist/createHotUpdaterCore.d.mts +2 -4
- package/dist/handlerAdminRoutes.cjs +149 -7
- package/dist/handlerAdminRoutes.mjs +149 -7
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/package.json +7 -14
|
@@ -0,0 +1,306 @@
|
|
|
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
|
+
/** How many registry rows one read takes while finding its account. */
|
|
22
|
+
const ACCOUNT_SCAN_PAGE = 200;
|
|
23
|
+
/** The one row of `ios_device_account`. */
|
|
24
|
+
const ACCOUNT_ROW = "registry";
|
|
25
|
+
/** `ios_devices.apple_error`'s bound. */
|
|
26
|
+
const ERROR_MAX_LENGTH = 512;
|
|
27
|
+
const STATUSES = [
|
|
28
|
+
"pending",
|
|
29
|
+
"approved",
|
|
30
|
+
"registered"
|
|
31
|
+
];
|
|
32
|
+
const OUTCOMES = [
|
|
33
|
+
"registered",
|
|
34
|
+
"refused",
|
|
35
|
+
"disabled",
|
|
36
|
+
"missing"
|
|
37
|
+
];
|
|
38
|
+
/** The `apple_error` of a device Apple has disabled. */
|
|
39
|
+
const IOS_DEVICE_DISABLED_ERROR = "Disabled in the Apple account.";
|
|
40
|
+
/** The `apple_error` of a registered device Apple no longer lists. */
|
|
41
|
+
const IOS_DEVICE_MISSING_ERROR = "Gone from the Apple account. Approve it again to register it.";
|
|
42
|
+
/** A device or registry request core cannot store: the caller's input. */
|
|
43
|
+
var IosDeviceInputError = class extends TypeError {
|
|
44
|
+
name = "IosDeviceInputError";
|
|
45
|
+
};
|
|
46
|
+
const pick = (row) => ({
|
|
47
|
+
id: row.id,
|
|
48
|
+
udid: row.udid,
|
|
49
|
+
name: row.name,
|
|
50
|
+
status: row.status,
|
|
51
|
+
requested_by: row.requested_by ?? null,
|
|
52
|
+
created_at_ms: row.created_at_ms,
|
|
53
|
+
approved_by: row.approved_by ?? null,
|
|
54
|
+
approved_at_ms: row.approved_at_ms ?? null,
|
|
55
|
+
registered_at_ms: row.registered_at_ms ?? null,
|
|
56
|
+
apple_issuer_id: row.apple_issuer_id ?? null,
|
|
57
|
+
apple_error: row.apple_error ?? null,
|
|
58
|
+
build_id: row.build_id ?? null
|
|
59
|
+
});
|
|
60
|
+
const udidOf = (udid) => {
|
|
61
|
+
const key = typeof udid === "string" ? normalizeIosUdid(udid) : null;
|
|
62
|
+
if (key === null) throw new IosDeviceInputError("Invalid iOS device: udid must be 40 hex digits, or 8 and 16 joined by a hyphen.");
|
|
63
|
+
return key;
|
|
64
|
+
};
|
|
65
|
+
const nameOf = (name) => {
|
|
66
|
+
const trimmed = typeof name === "string" ? name.trim() : "";
|
|
67
|
+
if (trimmed.length === 0 || trimmed.length > NAME_MAX_LENGTH) throw new IosDeviceInputError(`Invalid iOS device: name must be 1 to ${NAME_MAX_LENGTH} characters.`);
|
|
68
|
+
return trimmed;
|
|
69
|
+
};
|
|
70
|
+
/** An email, lower-cased, or null for the CLI. */
|
|
71
|
+
const personOf = (value, field) => {
|
|
72
|
+
if (value === null) return null;
|
|
73
|
+
const email = typeof value === "string" ? value.trim().toLowerCase() : "";
|
|
74
|
+
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.`);
|
|
75
|
+
return email;
|
|
76
|
+
};
|
|
77
|
+
/** Apple's words, on one line and within the column's bound. */
|
|
78
|
+
const errorText = (text) => {
|
|
79
|
+
const line = text.replace(/\s+/gu, " ").trim();
|
|
80
|
+
return line.length > ERROR_MAX_LENGTH ? `${line.slice(0, 511)}…` : line;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* The iOS device registry: the iPhones and iPads a team asked to install ad hoc
|
|
84
|
+
* builds on. The Console writes requests and approvals, and the CLI what Apple
|
|
85
|
+
* said, each over the row it read.
|
|
86
|
+
*/
|
|
87
|
+
const createIosDeviceApi = (db, { now = Date.now } = {}) => {
|
|
88
|
+
const findIosDevice = async (udid) => {
|
|
89
|
+
const key = normalizeIosUdid(udid);
|
|
90
|
+
if (key === null) return null;
|
|
91
|
+
const row = await db.findOne("ios_devices", { udid: key });
|
|
92
|
+
return row === null ? null : pick(row);
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* The issuer of the registry's earliest registration among its rows, by
|
|
96
|
+
* when and then by ID, or null when no row names one: read only while the
|
|
97
|
+
* registry has no account yet, a page at a time.
|
|
98
|
+
*/
|
|
99
|
+
const accountOfRows = async () => {
|
|
100
|
+
let earliest = null;
|
|
101
|
+
for (let after;;) {
|
|
102
|
+
const { rows } = await db.findMany("ios_devices", {
|
|
103
|
+
index: "all",
|
|
104
|
+
where: {},
|
|
105
|
+
order: "asc",
|
|
106
|
+
limit: ACCOUNT_SCAN_PAGE,
|
|
107
|
+
...after === void 0 ? {} : { range: { gt: after } }
|
|
108
|
+
});
|
|
109
|
+
for (const row of rows.map(pick)) {
|
|
110
|
+
if (row.apple_issuer_id === null) continue;
|
|
111
|
+
if (earliest === null || (row.registered_at_ms ?? 0) < (earliest.registered_at_ms ?? 0)) earliest = row;
|
|
112
|
+
}
|
|
113
|
+
if (rows.length < ACCOUNT_SCAN_PAGE) return earliest?.apple_issuer_id ?? null;
|
|
114
|
+
after = rows.at(-1).id;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
return {
|
|
118
|
+
async listIosDevices(input) {
|
|
119
|
+
if (input.status !== void 0 && !STATUSES.includes(input.status)) throw new IosDeviceInputError(`Invalid iOS device status: ${String(input.status)}. Expected ${STATUSES.join(", ")}.`);
|
|
120
|
+
const page = {
|
|
121
|
+
order: input.order ?? "asc",
|
|
122
|
+
limit: input.limit,
|
|
123
|
+
...after(input)
|
|
124
|
+
};
|
|
125
|
+
const { rows } = input.status === void 0 ? await db.findMany("ios_devices", {
|
|
126
|
+
index: "all",
|
|
127
|
+
where: {},
|
|
128
|
+
...page
|
|
129
|
+
}) : await db.findMany("ios_devices", {
|
|
130
|
+
index: "byStatus",
|
|
131
|
+
where: { status: input.status },
|
|
132
|
+
...page
|
|
133
|
+
});
|
|
134
|
+
return rows.map(pick);
|
|
135
|
+
},
|
|
136
|
+
findIosDevice,
|
|
137
|
+
async requestIosDevice(input) {
|
|
138
|
+
const udid = udidOf(input.udid);
|
|
139
|
+
const name = nameOf(input.name);
|
|
140
|
+
const requestedBy = personOf(input.requestedBy, "requestedBy");
|
|
141
|
+
const existing = await db.findOne("ios_devices", { udid });
|
|
142
|
+
if (existing !== null) return {
|
|
143
|
+
device: pick(existing),
|
|
144
|
+
created: false
|
|
145
|
+
};
|
|
146
|
+
const device = {
|
|
147
|
+
id: createUUIDv7(),
|
|
148
|
+
udid,
|
|
149
|
+
name,
|
|
150
|
+
status: "pending",
|
|
151
|
+
requested_by: requestedBy,
|
|
152
|
+
created_at_ms: now(),
|
|
153
|
+
approved_by: null,
|
|
154
|
+
approved_at_ms: null,
|
|
155
|
+
registered_at_ms: null,
|
|
156
|
+
apple_issuer_id: null,
|
|
157
|
+
apple_error: null,
|
|
158
|
+
build_id: null
|
|
159
|
+
};
|
|
160
|
+
try {
|
|
161
|
+
await db.transaction(async (tx) => {
|
|
162
|
+
tx.create("ios_devices", device);
|
|
163
|
+
});
|
|
164
|
+
} catch (error) {
|
|
165
|
+
if (error instanceof DatabaseConstraintError && error.reason === "unique") {
|
|
166
|
+
const winner = await db.findOne("ios_devices", { udid });
|
|
167
|
+
if (winner !== null) return {
|
|
168
|
+
device: pick(winner),
|
|
169
|
+
created: false
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
device,
|
|
176
|
+
created: true
|
|
177
|
+
};
|
|
178
|
+
},
|
|
179
|
+
async approveIosDevice(id, input) {
|
|
180
|
+
const name = input.name === void 0 ? void 0 : nameOf(input.name);
|
|
181
|
+
const approvedBy = personOf(input.approvedBy, "approvedBy");
|
|
182
|
+
return db.transaction(async (tx) => {
|
|
183
|
+
const row = await tx.findOne("ios_devices", { id });
|
|
184
|
+
if (row === null) return null;
|
|
185
|
+
const device = pick(row);
|
|
186
|
+
const set = {
|
|
187
|
+
...name === void 0 || name === device.name ? {} : { name },
|
|
188
|
+
...device.status === "pending" ? {
|
|
189
|
+
status: "approved",
|
|
190
|
+
approved_by: approvedBy,
|
|
191
|
+
approved_at_ms: now()
|
|
192
|
+
} : {}
|
|
193
|
+
};
|
|
194
|
+
if (Object.keys(set).length === 0) return device;
|
|
195
|
+
tx.update("ios_devices", row, set);
|
|
196
|
+
return {
|
|
197
|
+
...device,
|
|
198
|
+
...set
|
|
199
|
+
};
|
|
200
|
+
});
|
|
201
|
+
},
|
|
202
|
+
async renameIosDevice(id, input) {
|
|
203
|
+
const name = nameOf(input);
|
|
204
|
+
return db.transaction(async (tx) => {
|
|
205
|
+
const row = await tx.findOne("ios_devices", { id });
|
|
206
|
+
if (row === null) return null;
|
|
207
|
+
const device = pick(row);
|
|
208
|
+
if (device.name === name) return device;
|
|
209
|
+
tx.update("ios_devices", row, { name });
|
|
210
|
+
return {
|
|
211
|
+
...device,
|
|
212
|
+
name
|
|
213
|
+
};
|
|
214
|
+
});
|
|
215
|
+
},
|
|
216
|
+
async recordIosDeviceOutcome({ id, seen, issuerId, outcome, reason }) {
|
|
217
|
+
if (!OUTCOMES.includes(outcome)) throw new IosDeviceInputError(`Invalid iOS device outcome: ${String(outcome)}. Expected ${OUTCOMES.join(", ")}.`);
|
|
218
|
+
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.`);
|
|
219
|
+
return db.transaction(async (tx) => {
|
|
220
|
+
const row = await tx.findOne("ios_devices", { id });
|
|
221
|
+
if (row === null) return null;
|
|
222
|
+
const device = pick(row);
|
|
223
|
+
if (device.status !== seen.status || device.approved_at_ms !== seen.approvedAtMs || device.apple_issuer_id !== null && device.apple_issuer_id !== issuerId) return null;
|
|
224
|
+
let set;
|
|
225
|
+
switch (outcome) {
|
|
226
|
+
case "registered":
|
|
227
|
+
if (device.status === "pending") return null;
|
|
228
|
+
set = {
|
|
229
|
+
status: "registered",
|
|
230
|
+
registered_at_ms: device.registered_at_ms ?? now(),
|
|
231
|
+
apple_issuer_id: issuerId,
|
|
232
|
+
apple_error: null
|
|
233
|
+
};
|
|
234
|
+
break;
|
|
235
|
+
case "refused":
|
|
236
|
+
if (device.status !== "approved") return null;
|
|
237
|
+
set = { apple_error: errorText(reason === void 0 || reason.trim() === "" ? "Apple refused it." : reason) };
|
|
238
|
+
break;
|
|
239
|
+
case "disabled":
|
|
240
|
+
if (device.status === "pending") return null;
|
|
241
|
+
set = { apple_error: IOS_DEVICE_DISABLED_ERROR };
|
|
242
|
+
break;
|
|
243
|
+
case "missing":
|
|
244
|
+
if (device.status !== "registered") return null;
|
|
245
|
+
set = {
|
|
246
|
+
status: "pending",
|
|
247
|
+
approved_by: null,
|
|
248
|
+
approved_at_ms: null,
|
|
249
|
+
apple_error: IOS_DEVICE_MISSING_ERROR
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
if (Object.entries(set).some(([column, value]) => device[column] !== (value ?? null))) tx.update("ios_devices", row, set);
|
|
253
|
+
return {
|
|
254
|
+
...device,
|
|
255
|
+
...set
|
|
256
|
+
};
|
|
257
|
+
});
|
|
258
|
+
},
|
|
259
|
+
async markIosDevicesInBuild({ buildId, ids }) {
|
|
260
|
+
for (const id of new Set(ids)) await db.transaction(async (tx) => {
|
|
261
|
+
const row = await tx.findOne("ios_devices", { id });
|
|
262
|
+
if (row === null || typeof row.build_id === "string" && row.build_id >= buildId) return;
|
|
263
|
+
tx.update("ios_devices", row, { build_id: buildId });
|
|
264
|
+
});
|
|
265
|
+
},
|
|
266
|
+
async claimIosDeviceAccount(issuerId) {
|
|
267
|
+
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.`);
|
|
268
|
+
const answer = (held) => ({
|
|
269
|
+
issuerId: held,
|
|
270
|
+
claimed: held === issuerId
|
|
271
|
+
});
|
|
272
|
+
const existing = await db.findOne("ios_device_account", { id: ACCOUNT_ROW });
|
|
273
|
+
if (existing !== null) return answer(existing.apple_issuer_id);
|
|
274
|
+
const holder = await accountOfRows() ?? issuerId;
|
|
275
|
+
try {
|
|
276
|
+
await db.transaction(async (tx) => {
|
|
277
|
+
tx.create("ios_device_account", {
|
|
278
|
+
id: ACCOUNT_ROW,
|
|
279
|
+
apple_issuer_id: holder,
|
|
280
|
+
claimed_at_ms: now()
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
return answer(holder);
|
|
284
|
+
} catch (error) {
|
|
285
|
+
if (error instanceof DatabaseConstraintError && (error.reason === "exists" || error.reason === "unique")) {
|
|
286
|
+
const winner = await db.findOne("ios_device_account", { id: ACCOUNT_ROW });
|
|
287
|
+
if (winner !== null) return answer(winner.apple_issuer_id);
|
|
288
|
+
}
|
|
289
|
+
throw error;
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
async deleteIosDevice(id, only) {
|
|
293
|
+
const requestedBy = only?.requestedBy === void 0 ? void 0 : personOf(only.requestedBy, "requestedBy");
|
|
294
|
+
return db.transaction(async (tx) => {
|
|
295
|
+
const row = await tx.findOne("ios_devices", { id });
|
|
296
|
+
if (row === null) return null;
|
|
297
|
+
const device = pick(row);
|
|
298
|
+
if (only?.status !== void 0 && device.status !== only.status || requestedBy !== void 0 && device.requested_by !== requestedBy) return null;
|
|
299
|
+
await tx.delete("ios_devices", row);
|
|
300
|
+
return device;
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
};
|
|
305
|
+
//#endregion
|
|
306
|
+
export { IOS_DEVICE_DISABLED_ERROR, IOS_DEVICE_MISSING_ERROR, IosDeviceInputError, createIosDeviceApi };
|
|
@@ -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 };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const require_reads = require("./reads.cjs");
|
|
2
|
+
const require_nativeBuildDestinations = require("./nativeBuildDestinations.cjs");
|
|
2
3
|
const require_writes = require("./writes.cjs");
|
|
3
4
|
let _hot_updater_plugin_core = require("@hot-updater/plugin-core");
|
|
4
5
|
let _hot_updater_protocol = require("@hot-updater/protocol");
|
|
@@ -18,6 +19,13 @@ const GIT_COMMIT_HASH_MAX_LENGTH = 255;
|
|
|
18
19
|
* and looks up nothing else.
|
|
19
20
|
*/
|
|
20
21
|
const BUILD_KEY = /^nbk1:[0-9a-f]{64}$/;
|
|
22
|
+
/** `native_builds.sandbox_url_scheme`'s bound in core's schema. */
|
|
23
|
+
const SANDBOX_URL_SCHEME_MAX_LENGTH = 255;
|
|
24
|
+
/**
|
|
25
|
+
* A URL scheme as RFC 3986 spells it, in lower case: what a sandbox build's
|
|
26
|
+
* artifact registers, as `@hot-updater/protocol/native-artifact` reads it.
|
|
27
|
+
*/
|
|
28
|
+
const URL_SCHEME = new RegExp(`^[a-z][a-z0-9+.-]{0,254}$`);
|
|
21
29
|
/** A native build or scope core cannot store or look up: the caller's input. */
|
|
22
30
|
var NativeBuildInputError = class extends TypeError {
|
|
23
31
|
name = "NativeBuildInputError";
|
|
@@ -39,13 +47,33 @@ const pick = (row) => ({
|
|
|
39
47
|
build_number: row.build_number ?? null,
|
|
40
48
|
submitted_to: row.submitted_to ?? null,
|
|
41
49
|
submitted_at_ms: row.submitted_at_ms ?? null,
|
|
42
|
-
submitted_version: row.submitted_version ?? null
|
|
50
|
+
submitted_version: row.submitted_version ?? null,
|
|
51
|
+
is_store_build: row.is_store_build === true,
|
|
52
|
+
is_sandbox: row.is_sandbox === true,
|
|
53
|
+
sandbox_url_scheme: row.sandbox_url_scheme ?? null,
|
|
54
|
+
export_method: row.export_method ?? null,
|
|
55
|
+
native_app_version: row.native_app_version ?? null
|
|
43
56
|
});
|
|
44
|
-
/** The
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
57
|
+
/** The export methods an `.ipa` registers with. */
|
|
58
|
+
const EXPORT_METHODS = /* @__PURE__ */ new Set([
|
|
59
|
+
"app-store",
|
|
60
|
+
"ad-hoc",
|
|
61
|
+
"development",
|
|
62
|
+
"enterprise"
|
|
63
|
+
]);
|
|
64
|
+
/**
|
|
65
|
+
* The channels only sandbox builds register on: a registration's `sandbox`
|
|
66
|
+
* must agree with it. The registry's convention, as
|
|
67
|
+
* `@hot-updater/protocol/native-artifact` checks it for a build's artifact.
|
|
68
|
+
*/
|
|
69
|
+
const SANDBOX_CHANNEL_PREFIX = "sandbox-";
|
|
70
|
+
/**
|
|
71
|
+
* How many older builds of a scope one read takes, looking for one older than
|
|
72
|
+
* a bundle when a build made after the bundle is the newest.
|
|
73
|
+
*/
|
|
74
|
+
const OLDER_BUILDS_PAGE = 20;
|
|
75
|
+
/** How many times a mark runs again after losing its scope's mark to another. */
|
|
76
|
+
const MARK_ATTEMPTS = 8;
|
|
49
77
|
/** `native_builds.build_number`'s bound in core's schema. */
|
|
50
78
|
const BUILD_NUMBER_MAX_LENGTH = 64;
|
|
51
79
|
/** `native_builds.submitted_version`'s bound in core's schema. */
|
|
@@ -76,19 +104,40 @@ const nativeBuildScopeKey = (scope) => {
|
|
|
76
104
|
if (scopeKey.length > SCOPE_KEY_MAX_LENGTH) throw new NativeBuildInputError(`Invalid native build scope: its key is longer than ${SCOPE_KEY_MAX_LENGTH} characters; shorten the channel or app version.`);
|
|
77
105
|
return scopeKey;
|
|
78
106
|
};
|
|
107
|
+
/**
|
|
108
|
+
* Refuses a patch from a sandbox build's built-in bundle: no deploy patches
|
|
109
|
+
* from one, so no store or test install of the app gets a patch made for a
|
|
110
|
+
* sandbox build. One unique read of the base.
|
|
111
|
+
*/
|
|
112
|
+
const refuseSandboxPatchBase = async (db, baseBundleId) => {
|
|
113
|
+
if ((await db.findOne("native_builds", { min_bundle_id: baseBundleId }))?.is_sandbox === true) throw new NativeBuildInputError("A sandbox build's built-in bundle is no patch base.");
|
|
114
|
+
};
|
|
79
115
|
/** Whether `bundleId` is a native build's built-in bundle: one unique read. */
|
|
80
116
|
const isBuiltInBundle = async (db, bundleId) => await db.findOne("native_builds", { min_bundle_id: bundleId }) !== null;
|
|
81
|
-
const problemOf = ({ id, minBundleId, platform, channel, fingerprintHash, appVersion, gitCommitHash, buildKey, buildNumber, artifact }) => {
|
|
117
|
+
const problemOf = ({ id, minBundleId, platform, channel, fingerprintHash, appVersion, gitCommitHash, buildKey, buildNumber, isSandbox, sandboxUrlScheme, exportMethod, nativeAppVersion, artifact }) => {
|
|
82
118
|
if (!(0, _hot_updater_plugin_core.isUUIDv7)(id)) return "id must be a UUIDv7";
|
|
83
119
|
if (!(0, _hot_updater_plugin_core.isUUIDv7)(minBundleId)) return "minBundleId must be the UUIDv7 a release build compiles in";
|
|
84
120
|
if (platform !== "ios" && platform !== "android") return "platform must be ios or android";
|
|
85
121
|
if (channel.trim().length === 0) return "channel is required";
|
|
122
|
+
if (isSandbox !== void 0 && typeof isSandbox !== "boolean") return "isSandbox must be true or false";
|
|
123
|
+
const reserved = channel.startsWith(SANDBOX_CHANNEL_PREFIX);
|
|
124
|
+
if (isSandbox === true && !reserved) return `a sandbox build registers on a ${SANDBOX_CHANNEL_PREFIX}* channel, not ${channel}`;
|
|
125
|
+
if (isSandbox !== true && reserved) return `channel ${channel} is reserved for sandbox builds`;
|
|
126
|
+
if (sandboxUrlScheme !== void 0 && sandboxUrlScheme !== null) {
|
|
127
|
+
if (isSandbox !== true) return "sandboxUrlScheme is a sandbox build's, and this isn't one";
|
|
128
|
+
if (typeof sandboxUrlScheme !== "string" || !URL_SCHEME.test(sandboxUrlScheme)) return `sandboxUrlScheme must be a URL scheme in lower case, up to ${SANDBOX_URL_SCHEME_MAX_LENGTH} characters, or null`;
|
|
129
|
+
}
|
|
86
130
|
if (gitCommitHash !== null && gitCommitHash.length > GIT_COMMIT_HASH_MAX_LENGTH) return `gitCommitHash must be at most ${GIT_COMMIT_HASH_MAX_LENGTH} characters`;
|
|
87
131
|
if (fingerprintHash === null === (appVersion === null)) return "exactly one of fingerprintHash and appVersion is required";
|
|
88
132
|
if (buildKey !== null && !BUILD_KEY.test(buildKey)) return "buildKey must be nbk1: and a SHA-256, or null";
|
|
89
133
|
if (buildNumber !== void 0 && buildNumber !== null && (buildNumber.trim().length === 0 || buildNumber.length > BUILD_NUMBER_MAX_LENGTH)) return `buildNumber must be the artifact's build number, up to ${BUILD_NUMBER_MAX_LENGTH} characters, or null`;
|
|
134
|
+
if (nativeAppVersion !== void 0 && nativeAppVersion !== null && (typeof nativeAppVersion !== "string" || nativeAppVersion.trim().length === 0)) return "nativeAppVersion must be the artifact's app version, or null";
|
|
90
135
|
const kinds = ARTIFACT_KINDS[platform];
|
|
91
136
|
if (!kinds.includes(artifact.kind)) return `artifact.kind of an ${platform} build must be ${kinds.join(" or ")}`;
|
|
137
|
+
if (exportMethod !== void 0 && exportMethod !== null) {
|
|
138
|
+
if (artifact.kind !== "ipa") return "exportMethod is an .ipa's, and this artifact isn't one";
|
|
139
|
+
if (!EXPORT_METHODS.has(exportMethod)) return `exportMethod must be ${[...EXPORT_METHODS].join(", ")}, or null`;
|
|
140
|
+
}
|
|
92
141
|
if (!Number.isSafeInteger(artifact.byteSize) || artifact.byteSize < 0) return "artifact.byteSize must be a byte count";
|
|
93
142
|
return null;
|
|
94
143
|
};
|
|
@@ -117,7 +166,12 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
|
|
|
117
166
|
build_number: registration.buildNumber ?? null,
|
|
118
167
|
submitted_to: null,
|
|
119
168
|
submitted_at_ms: null,
|
|
120
|
-
submitted_version: null
|
|
169
|
+
submitted_version: null,
|
|
170
|
+
is_store_build: false,
|
|
171
|
+
is_sandbox: registration.isSandbox === true,
|
|
172
|
+
sandbox_url_scheme: registration.sandboxUrlScheme ?? null,
|
|
173
|
+
export_method: registration.exportMethod ?? null,
|
|
174
|
+
native_app_version: registration.nativeAppVersion ?? null
|
|
121
175
|
};
|
|
122
176
|
for (let attempt = 1;; attempt += 1) {
|
|
123
177
|
await require_writes.insertChannel(db, {
|
|
@@ -155,21 +209,43 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
|
|
|
155
209
|
const scopeKey = nativeBuildScopeKey(scope);
|
|
156
210
|
const buildKey = options?.buildKey;
|
|
157
211
|
if (buildKey !== void 0 && !BUILD_KEY.test(buildKey)) throw new NativeBuildInputError("Invalid native build key: it must be nbk1: and a SHA-256.");
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
212
|
+
if (options?.storeBuild) {
|
|
213
|
+
const marked = await db.findOne("native_builds", { store_build_scope_key: scopeKey });
|
|
214
|
+
return marked === null ? null : pick(marked);
|
|
215
|
+
}
|
|
216
|
+
const read = (limit, before) => {
|
|
217
|
+
const page = {
|
|
218
|
+
order: "desc",
|
|
219
|
+
limit,
|
|
220
|
+
...before === void 0 ? {} : { range: { lt: before } }
|
|
221
|
+
};
|
|
222
|
+
return buildKey === void 0 ? db.findMany("native_builds", {
|
|
223
|
+
index: "byScope",
|
|
224
|
+
where: { scope_key: scopeKey },
|
|
225
|
+
...page
|
|
226
|
+
}) : db.findMany("native_builds", {
|
|
227
|
+
index: "byBuildKey",
|
|
228
|
+
where: {
|
|
229
|
+
scope_key: scopeKey,
|
|
230
|
+
build_key: buildKey
|
|
231
|
+
},
|
|
232
|
+
...page
|
|
233
|
+
});
|
|
234
|
+
};
|
|
235
|
+
const bundleId = options?.builtInOlderThan;
|
|
236
|
+
if (bundleId !== void 0 && scope.channel.startsWith(SANDBOX_CHANNEL_PREFIX)) return null;
|
|
237
|
+
const { rows } = await read(1);
|
|
238
|
+
const newest = rows[0];
|
|
239
|
+
if (newest === void 0) return null;
|
|
240
|
+
if (bundleId === void 0 || newest.min_bundle_id < bundleId) return pick(newest);
|
|
241
|
+
for (let before = newest.id;;) {
|
|
242
|
+
const { rows: older } = await read(OLDER_BUILDS_PAGE, before);
|
|
243
|
+
const usable = older.find(({ min_bundle_id }) => min_bundle_id < bundleId);
|
|
244
|
+
if (usable !== void 0) return pick(usable);
|
|
245
|
+
const last = older.at(-1);
|
|
246
|
+
if (older.length < OLDER_BUILDS_PAGE || last === void 0) return null;
|
|
247
|
+
before = last.id;
|
|
248
|
+
}
|
|
173
249
|
},
|
|
174
250
|
async recordNativeBuildSubmission(id, { destination, version }) {
|
|
175
251
|
if (version.trim().length === 0 || version.length > SUBMITTED_VERSION_MAX_LENGTH) throw new NativeBuildInputError(`Invalid native build submission: version must be the store's label of the build, up to ${SUBMITTED_VERSION_MAX_LENGTH} characters.`);
|
|
@@ -177,7 +253,8 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
|
|
|
177
253
|
const row = await tx.findOne("native_builds", { id });
|
|
178
254
|
if (row === null) return null;
|
|
179
255
|
const build = pick(row);
|
|
180
|
-
if (
|
|
256
|
+
if (!require_nativeBuildDestinations.NATIVE_BUILD_SUBMISSION_DESTINATIONS[build.platform].includes(destination)) throw new NativeBuildInputError(`Invalid native build submission: an ${build.platform} build is submitted to ${require_nativeBuildDestinations.NATIVE_BUILD_SUBMISSION_DESTINATIONS[build.platform].join(" or ")}, not ${destination}.`);
|
|
257
|
+
if (build.is_sandbox && destination !== "testflight" && destination !== "play-internal") throw new NativeBuildInputError("A sandbox build can submit only to TestFlight or Play internal testing.");
|
|
181
258
|
const set = {
|
|
182
259
|
submitted_to: destination,
|
|
183
260
|
submitted_at_ms: now(),
|
|
@@ -190,6 +267,46 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
|
|
|
190
267
|
};
|
|
191
268
|
});
|
|
192
269
|
},
|
|
270
|
+
async markNativeBuild(id, { storeBuild }) {
|
|
271
|
+
for (let attempt = 1;; attempt += 1) try {
|
|
272
|
+
return await db.transaction(async (tx) => {
|
|
273
|
+
const row = await tx.findOne("native_builds", { id });
|
|
274
|
+
if (row === null) return null;
|
|
275
|
+
const build = pick(row);
|
|
276
|
+
if (!storeBuild) {
|
|
277
|
+
if (build.is_store_build) tx.update("native_builds", row, { is_store_build: false });
|
|
278
|
+
return {
|
|
279
|
+
nativeBuild: {
|
|
280
|
+
...build,
|
|
281
|
+
is_store_build: false
|
|
282
|
+
},
|
|
283
|
+
previous: null
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
if (build.is_sandbox) throw new NativeBuildInputError("A sandbox build can't be the store build.");
|
|
287
|
+
if (build.is_store_build) return {
|
|
288
|
+
nativeBuild: build,
|
|
289
|
+
previous: null
|
|
290
|
+
};
|
|
291
|
+
const holder = await tx.findOne("native_builds", { store_build_scope_key: build.scope_key });
|
|
292
|
+
if (holder !== null) tx.update("native_builds", holder, { is_store_build: false });
|
|
293
|
+
tx.update("native_builds", row, { is_store_build: true });
|
|
294
|
+
return {
|
|
295
|
+
nativeBuild: {
|
|
296
|
+
...build,
|
|
297
|
+
is_store_build: true
|
|
298
|
+
},
|
|
299
|
+
previous: holder === null ? null : {
|
|
300
|
+
...pick(holder),
|
|
301
|
+
is_store_build: false
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
});
|
|
305
|
+
} catch (error) {
|
|
306
|
+
if (attempt < MARK_ATTEMPTS && error instanceof _hot_updater_plugin_core.DatabaseConstraintError && error.reason === "unique") continue;
|
|
307
|
+
throw error;
|
|
308
|
+
}
|
|
309
|
+
},
|
|
193
310
|
async deleteNativeBuild(id) {
|
|
194
311
|
return db.transaction(async (tx) => {
|
|
195
312
|
const row = await tx.findOne("native_builds", { id });
|
|
@@ -230,3 +347,4 @@ exports.NativeBuildInputError = NativeBuildInputError;
|
|
|
230
347
|
exports.createNativeBuildApi = createNativeBuildApi;
|
|
231
348
|
exports.isBuiltInBundle = isBuiltInBundle;
|
|
232
349
|
exports.nativeBuildScopeKey = nativeBuildScopeKey;
|
|
350
|
+
exports.refuseSandboxPatchBase = refuseSandboxPatchBase;
|