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

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
@@ -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,27 @@ 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}$`);
29
+ /**
30
+ * A bundle identifier or application ID, as an artifact reports it: letters,
31
+ * digits, dots, dashes and underscores, which a scope key holds as they are.
32
+ */
33
+ const APP_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,254}$/;
34
+ /** A fingerprint hash as a Release Catalog scope key holds it. */
35
+ const FINGERPRINT_HASH = /^[A-Za-z0-9._~-]{1,255}$/;
36
+ /** How each kind of artifact can install. */
37
+ const DISTRIBUTIONS = {
38
+ apk: ["direct"],
39
+ app: ["direct"],
40
+ aab: ["store"],
41
+ ipa: ["direct", "store"]
42
+ };
21
43
  /** A native build or scope core cannot store or look up: the caller's input. */
22
44
  var NativeBuildInputError = class extends TypeError {
23
45
  name = "NativeBuildInputError";
@@ -39,24 +61,73 @@ const pick = (row) => ({
39
61
  build_number: row.build_number ?? null,
40
62
  submitted_to: row.submitted_to ?? null,
41
63
  submitted_at_ms: row.submitted_at_ms ?? null,
42
- submitted_version: row.submitted_version ?? null
64
+ submitted_version: row.submitted_version ?? null,
65
+ is_store_build: row.is_store_build === true,
66
+ is_sandbox: row.is_sandbox === true,
67
+ sandbox_url_scheme: row.sandbox_url_scheme ?? null,
68
+ export_method: row.export_method ?? null,
69
+ native_app_version: row.native_app_version ?? null
43
70
  });
44
- /** The store track each platform's builds are submitted to. */
45
- const SUBMISSION_DESTINATIONS = {
46
- android: "play-internal",
47
- ios: "testflight"
48
- };
71
+ /** The export methods an `.ipa` registers with. */
72
+ const EXPORT_METHODS = /* @__PURE__ */ new Set([
73
+ "app-store",
74
+ "ad-hoc",
75
+ "development",
76
+ "enterprise"
77
+ ]);
78
+ /**
79
+ * How many older builds of a scope one read takes, looking for one older than
80
+ * a bundle when a build made after the bundle is the newest.
81
+ */
82
+ const OLDER_BUILDS_PAGE = 20;
83
+ /**
84
+ * How many pages of older builds that look reads before it answers null: 100
85
+ * builds made after the bundle in one scope, where a deploy's new bundle has
86
+ * none or a few registered while it deploys.
87
+ */
88
+ const OLDER_BUILDS_PAGES = 5;
89
+ /** How many times a mark runs again after losing its scope's mark to another. */
90
+ const MARK_ATTEMPTS = 8;
49
91
  /** `native_builds.build_number`'s bound in core's schema. */
50
92
  const BUILD_NUMBER_MAX_LENGTH = 64;
51
93
  /** `native_builds.submitted_version`'s bound in core's schema. */
52
94
  const SUBMITTED_VERSION_MAX_LENGTH = 255;
95
+ const checkedScopeKey = (scopeKey) => {
96
+ 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.`);
97
+ return scopeKey;
98
+ };
99
+ /**
100
+ * How a registered artifact installs: an `.ipa` by its export method, and
101
+ * `unknown` for one whose export no profile confirmed, which no lookup asks
102
+ * for, so it is never reused as either.
103
+ */
104
+ const registeredDistribution = (kind, exportMethod) => kind !== "ipa" ? DISTRIBUTIONS[kind][0] : exportMethod === void 0 || exportMethod === null ? "unknown" : exportMethod === "app-store" ? "store" : "direct";
105
+ /**
106
+ * A sandbox build scope's key, which names no channel: the app ID, URL
107
+ * scheme, artifact kind and distribution in its place, then the fingerprint,
108
+ * or the app-version strategy and the exact app version. `v1:sandbox:` is no
109
+ * Release Catalog strategy, so it never meets a normal build's key.
110
+ */
111
+ const sandboxScopeKey = (scope, sandbox) => {
112
+ const { appId, urlScheme, artifactKind, distribution } = sandbox;
113
+ const problem = typeof appId !== "string" || !APP_ID.test(appId) ? "its app ID must be a bundle identifier or application ID, up to 255 characters" : typeof urlScheme !== "string" || !URL_SCHEME.test(urlScheme) ? `its URL scheme must be a URL scheme in lower case, up to ${SANDBOX_URL_SCHEME_MAX_LENGTH} characters` : !ARTIFACT_KINDS[scope.platform]?.includes(artifactKind) ? `its artifact kind must be one an ${scope.platform} build makes` : distribution !== "unknown" && !DISTRIBUTIONS[artifactKind].includes(distribution) ? `an .${artifactKind} doesn't install as ${String(distribution)}` : scope.fingerprintHash !== null && !FINGERPRINT_HASH.test(scope.fingerprintHash) ? "its fingerprint hash must be 1-255 URL-safe ASCII characters" : null;
114
+ if (problem !== null) throw new NativeBuildInputError(`Invalid sandbox build scope: ${problem}.`);
115
+ const target = scope.fingerprintHash === null ? `app-version:${encodeURIComponent(scope.appVersion)}` : `fingerprint:${scope.fingerprintHash}`;
116
+ return checkedScopeKey(`v1:sandbox:${scope.platform}:${appId}:${urlScheme}:${artifactKind}:${distribution}:${target}`);
117
+ };
53
118
  /**
54
119
  * A native build scope's key: the Release Catalog scope key of a
55
120
  * fingerprint, or of the app-version strategy followed by the exact app
56
121
  * version the build compiled in. One key serves both strategies, so one
57
- * index finds the newest build of either.
122
+ * index finds the newest build of either. A sandbox scope's key names no
123
+ * channel, so pull requests find a sandbox build on any.
58
124
  */
59
125
  const nativeBuildScopeKey = (scope) => {
126
+ const { sandbox } = scope;
127
+ if (sandbox !== void 0 && sandbox !== null) {
128
+ if (sandbox.distribution !== "direct" && sandbox.distribution !== "store") throw new NativeBuildInputError("Invalid sandbox build scope: its distribution must be direct or store.");
129
+ return sandboxScopeKey(scope, sandbox);
130
+ }
60
131
  let scopeKey;
61
132
  try {
62
133
  const channelKey = (0, _hot_updater_protocol.encodeChannelKey)(scope.channel);
@@ -73,32 +144,126 @@ const nativeBuildScopeKey = (scope) => {
73
144
  } catch (error) {
74
145
  throw new NativeBuildInputError(`Invalid native build scope: ${error instanceof Error ? error.message : String(error)}.`);
75
146
  }
76
- 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
- return scopeKey;
147
+ return checkedScopeKey(scopeKey);
148
+ };
149
+ /** The channel row a registration names, or the refusal of its channel. */
150
+ const channelIdOf = (channel) => {
151
+ try {
152
+ return `channel:${(0, _hot_updater_protocol.encodeChannelKey)(channel)}`;
153
+ } catch (error) {
154
+ throw new NativeBuildInputError(`Invalid native build scope: ${error instanceof Error ? error.message : String(error)}.`);
155
+ }
156
+ };
157
+ /**
158
+ * A registration's scope key, from the registration's own facts: a sandbox
159
+ * build's from what its artifact says, whatever `sandbox` it names.
160
+ */
161
+ const registrationScopeKey = (registration) => {
162
+ channelIdOf(registration.channel);
163
+ return registration.isSandbox === true ? sandboxScopeKey(registration, {
164
+ appId: registration.sandboxAppId,
165
+ urlScheme: registration.sandboxUrlScheme,
166
+ artifactKind: registration.artifact.kind,
167
+ distribution: registeredDistribution(registration.artifact.kind, registration.exportMethod)
168
+ }) : nativeBuildScopeKey({
169
+ ...registration,
170
+ sandbox: null
171
+ });
172
+ };
173
+ /**
174
+ * Refuses a patch from a sandbox build's built-in bundle: no deploy patches
175
+ * from one, so no store or test install of the app gets a patch made for a
176
+ * sandbox build. One unique read of the base.
177
+ */
178
+ const refuseSandboxPatchBase = async (db, baseBundleId) => {
179
+ 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.");
78
180
  };
79
181
  /** Whether `bundleId` is a native build's built-in bundle: one unique read. */
80
182
  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 }) => {
183
+ const problemOf = ({ id, minBundleId, platform, channel, fingerprintHash, appVersion, gitCommitHash, buildKey, buildNumber, isSandbox, sandboxUrlScheme, sandboxAppId, exportMethod, nativeAppVersion, artifact }) => {
82
184
  if (!(0, _hot_updater_plugin_core.isUUIDv7)(id)) return "id must be a UUIDv7";
83
185
  if (!(0, _hot_updater_plugin_core.isUUIDv7)(minBundleId)) return "minBundleId must be the UUIDv7 a release build compiles in";
84
186
  if (platform !== "ios" && platform !== "android") return "platform must be ios or android";
85
187
  if (channel.trim().length === 0) return "channel is required";
188
+ if (isSandbox !== void 0 && typeof isSandbox !== "boolean") return "isSandbox must be true or false";
189
+ if (sandboxUrlScheme !== void 0 && sandboxUrlScheme !== null) {
190
+ if (isSandbox !== true) return "sandboxUrlScheme is a sandbox build's, and this isn't one";
191
+ 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`;
192
+ }
193
+ if (sandboxAppId !== void 0 && sandboxAppId !== null) {
194
+ if (isSandbox !== true) return "sandboxAppId is a sandbox build's, and this isn't one";
195
+ if (typeof sandboxAppId !== "string" || !APP_ID.test(sandboxAppId)) return "sandboxAppId must be the artifact's bundle identifier or application ID, up to 255 characters";
196
+ }
197
+ if (isSandbox === true) {
198
+ if (sandboxUrlScheme === void 0 || sandboxUrlScheme === null) return "a sandbox build needs the sandboxUrlScheme its artifact registers";
199
+ if (sandboxAppId === void 0 || sandboxAppId === null) return "a sandbox build needs the sandboxAppId its artifact reports, which an older hot-updater CLI doesn't send. Upgrade the hot-updater CLI to the version that matches this server, and build again";
200
+ }
86
201
  if (gitCommitHash !== null && gitCommitHash.length > GIT_COMMIT_HASH_MAX_LENGTH) return `gitCommitHash must be at most ${GIT_COMMIT_HASH_MAX_LENGTH} characters`;
87
202
  if (fingerprintHash === null === (appVersion === null)) return "exactly one of fingerprintHash and appVersion is required";
88
203
  if (buildKey !== null && !BUILD_KEY.test(buildKey)) return "buildKey must be nbk1: and a SHA-256, or null";
89
204
  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`;
205
+ 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
206
  const kinds = ARTIFACT_KINDS[platform];
91
207
  if (!kinds.includes(artifact.kind)) return `artifact.kind of an ${platform} build must be ${kinds.join(" or ")}`;
208
+ if (exportMethod !== void 0 && exportMethod !== null) {
209
+ if (artifact.kind !== "ipa") return "exportMethod is an .ipa's, and this artifact isn't one";
210
+ if (!EXPORT_METHODS.has(exportMethod)) return `exportMethod must be ${[...EXPORT_METHODS].join(", ")}, or null`;
211
+ }
92
212
  if (!Number.isSafeInteger(artifact.byteSize) || artifact.byteSize < 0) return "artifact.byteSize must be a byte count";
93
213
  return null;
94
214
  };
215
+ /** The other install target of an iOS sandbox build's native code. */
216
+ const SIBLING_KINDS = {
217
+ app: "ipa",
218
+ ipa: "app"
219
+ };
220
+ /**
221
+ * Whether a build is one a person installs from its file: a Simulator `.app`,
222
+ * or an `.ipa` exported for direct install, that no store took and that isn't
223
+ * the store build. One rule for a sibling and the build it is found from, so
224
+ * a build that shows its sibling is shown by it.
225
+ */
226
+ const isInstallTarget = (build) => !build.is_store_build && build.submitted_to === null && (build.artifact_kind === "app" || build.artifact_kind === "ipa" && build.export_method !== null && build.export_method !== "app-store");
227
+ /**
228
+ * The scope key of the other install target of a sandbox build, from its own
229
+ * key's app ID and URL scheme, which hold no colon, and its row's fingerprint
230
+ * or app version, through `sandboxScopeKey`; null for a key it didn't make.
231
+ */
232
+ const siblingSandboxScopeKey = (build, artifactKind) => {
233
+ const [version, sandbox, platform, appId, urlScheme] = build.scope_key.split(":");
234
+ if (version !== "v1" || sandbox !== "sandbox" || platform !== build.platform || appId === void 0 || urlScheme === void 0) return null;
235
+ const target = build.fingerprint_hash === null ? {
236
+ platform: build.platform,
237
+ fingerprintHash: null,
238
+ appVersion: build.app_version ?? ""
239
+ } : {
240
+ platform: build.platform,
241
+ fingerprintHash: build.fingerprint_hash,
242
+ appVersion: null
243
+ };
244
+ try {
245
+ return sandboxScopeKey(target, {
246
+ appId,
247
+ urlScheme,
248
+ artifactKind: build.artifact_kind,
249
+ distribution: registeredDistribution(build.artifact_kind, build.export_method)
250
+ }) === build.scope_key ? sandboxScopeKey(target, {
251
+ appId,
252
+ urlScheme,
253
+ artifactKind,
254
+ distribution: "direct"
255
+ }) : null;
256
+ } catch {
257
+ return null;
258
+ }
259
+ };
95
260
  /** The native build registry: core's `native_builds` and the built-in bundle rows. */
96
261
  const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
97
262
  async registerNativeBuild(registration) {
98
263
  const problem = problemOf(registration);
99
264
  if (problem !== null) throw new NativeBuildInputError(`Invalid native build: ${problem}.`);
100
- const scopeKey = nativeBuildScopeKey(registration);
101
- const channelId = `channel:${(0, _hot_updater_protocol.encodeChannelKey)(registration.channel)}`;
265
+ const scopeKey = registrationScopeKey(registration);
266
+ const channelId = channelIdOf(registration.channel);
102
267
  const { embeddedBundle } = registration;
103
268
  const row = {
104
269
  id: registration.id,
@@ -117,7 +282,12 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
117
282
  build_number: registration.buildNumber ?? null,
118
283
  submitted_to: null,
119
284
  submitted_at_ms: null,
120
- submitted_version: null
285
+ submitted_version: null,
286
+ is_store_build: false,
287
+ is_sandbox: registration.isSandbox === true,
288
+ sandbox_url_scheme: registration.sandboxUrlScheme ?? null,
289
+ export_method: registration.exportMethod ?? null,
290
+ native_app_version: registration.nativeAppVersion ?? null
121
291
  };
122
292
  for (let attempt = 1;; attempt += 1) {
123
293
  await require_writes.insertChannel(db, {
@@ -151,25 +321,82 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
151
321
  const row = await db.findOne("native_builds", { id });
152
322
  return row === null ? null : pick(row);
153
323
  },
324
+ async findNativeBuildByBuiltInBundle(bundleId) {
325
+ const row = await db.findOne("native_builds", { min_bundle_id: bundleId });
326
+ return row === null ? null : pick(row);
327
+ },
154
328
  async findNativeBuild(scope, options) {
155
329
  const scopeKey = nativeBuildScopeKey(scope);
156
330
  const buildKey = options?.buildKey;
157
331
  if (buildKey !== void 0 && !BUILD_KEY.test(buildKey)) throw new NativeBuildInputError("Invalid native build key: it must be nbk1: and a SHA-256.");
158
- const { rows } = buildKey === void 0 ? await db.findMany("native_builds", {
332
+ const artifactKind = options?.artifactKind;
333
+ if (artifactKind !== void 0 && !ARTIFACT_KINDS[scope.platform].includes(artifactKind)) throw new NativeBuildInputError(`Invalid native build kind: an ${scope.platform} build is ${ARTIFACT_KINDS[scope.platform].join(" or ")}.`);
334
+ const ofKind = (row) => artifactKind === void 0 || row.artifact_kind === artifactKind;
335
+ if (options?.storeBuild) {
336
+ const marked = await db.findOne("native_builds", { store_build_scope_key: scopeKey });
337
+ return marked === null || !ofKind(marked) ? null : pick(marked);
338
+ }
339
+ const read = (limit, before) => {
340
+ const page = {
341
+ order: "desc",
342
+ limit,
343
+ ...before === void 0 ? {} : { range: { lt: before } }
344
+ };
345
+ return buildKey !== void 0 ? db.findMany("native_builds", {
346
+ index: "byBuildKey",
347
+ where: {
348
+ scope_key: scopeKey,
349
+ build_key: buildKey
350
+ },
351
+ ...page
352
+ }) : artifactKind !== void 0 ? db.findMany("native_builds", {
353
+ index: "byScopeKind",
354
+ where: {
355
+ scope_key: scopeKey,
356
+ artifact_kind: artifactKind
357
+ },
358
+ ...page
359
+ }) : db.findMany("native_builds", {
360
+ index: "byScope",
361
+ where: { scope_key: scopeKey },
362
+ ...page
363
+ });
364
+ };
365
+ const bundleId = options?.builtInOlderThan;
366
+ if (bundleId !== void 0 && scope.sandbox !== void 0 && scope.sandbox !== null) return null;
367
+ const { rows } = await read(1);
368
+ const newest = (rows[0] === void 0 ? void 0 : pick(rows[0])) ?? (artifactKind !== void 0 && buildKey === void 0 ? await db.findMany("native_builds", {
159
369
  index: "byScope",
160
370
  where: { scope_key: scopeKey },
161
371
  order: "desc",
162
372
  limit: 1
163
- }) : await db.findMany("native_builds", {
164
- index: "byBuildKey",
165
- where: {
166
- scope_key: scopeKey,
167
- build_key: buildKey
168
- },
373
+ }).then(({ rows: [row] }) => row?.artifact_kind === artifactKind ? pick(row) : void 0) : void 0);
374
+ if (newest === void 0 || !ofKind(newest)) return null;
375
+ if (bundleId === void 0 || newest.min_bundle_id < bundleId) return newest;
376
+ let before = newest.id;
377
+ for (let page = 0; page < OLDER_BUILDS_PAGES; page += 1) {
378
+ const { rows: older } = await read(OLDER_BUILDS_PAGE, before);
379
+ const usable = older.find(({ min_bundle_id }) => min_bundle_id < bundleId);
380
+ if (usable !== void 0) return ofKind(usable) ? pick(usable) : null;
381
+ const last = older.at(-1);
382
+ if (older.length < OLDER_BUILDS_PAGE || last === void 0) return null;
383
+ before = last.id;
384
+ }
385
+ return null;
386
+ },
387
+ async findNativeBuildSibling(build) {
388
+ const artifactKind = SIBLING_KINDS[build.artifact_kind];
389
+ if (build.platform !== "ios" || !build.is_sandbox || artifactKind === void 0 || !isInstallTarget(build)) return null;
390
+ const scopeKey = siblingSandboxScopeKey(build, artifactKind);
391
+ if (scopeKey === null) return null;
392
+ const { rows } = await db.findMany("native_builds", {
393
+ index: "byScope",
394
+ where: { scope_key: scopeKey },
169
395
  order: "desc",
170
396
  limit: 1
171
397
  });
172
- return rows[0] === void 0 ? null : pick(rows[0]);
398
+ const sibling = rows[0] === void 0 ? null : pick(rows[0]);
399
+ return sibling !== null && isInstallTarget(sibling) ? sibling : null;
173
400
  },
174
401
  async recordNativeBuildSubmission(id, { destination, version }) {
175
402
  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 +404,8 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
177
404
  const row = await tx.findOne("native_builds", { id });
178
405
  if (row === null) return null;
179
406
  const build = pick(row);
180
- if (SUBMISSION_DESTINATIONS[build.platform] !== destination) throw new NativeBuildInputError(`Invalid native build submission: an ${build.platform} build is submitted to ${SUBMISSION_DESTINATIONS[build.platform]}, not ${destination}.`);
407
+ 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}.`);
408
+ 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
409
  const set = {
182
410
  submitted_to: destination,
183
411
  submitted_at_ms: now(),
@@ -190,6 +418,46 @@ const createNativeBuildApi = (db, { now = Date.now } = {}) => ({
190
418
  };
191
419
  });
192
420
  },
421
+ async markNativeBuild(id, { storeBuild }) {
422
+ for (let attempt = 1;; attempt += 1) try {
423
+ return await db.transaction(async (tx) => {
424
+ const row = await tx.findOne("native_builds", { id });
425
+ if (row === null) return null;
426
+ const build = pick(row);
427
+ if (!storeBuild) {
428
+ if (build.is_store_build) tx.update("native_builds", row, { is_store_build: false });
429
+ return {
430
+ nativeBuild: {
431
+ ...build,
432
+ is_store_build: false
433
+ },
434
+ previous: null
435
+ };
436
+ }
437
+ if (build.is_sandbox) throw new NativeBuildInputError("A sandbox build can't be the store build.");
438
+ if (build.is_store_build) return {
439
+ nativeBuild: build,
440
+ previous: null
441
+ };
442
+ const holder = await tx.findOne("native_builds", { store_build_scope_key: build.scope_key });
443
+ if (holder !== null) tx.update("native_builds", holder, { is_store_build: false });
444
+ tx.update("native_builds", row, { is_store_build: true });
445
+ return {
446
+ nativeBuild: {
447
+ ...build,
448
+ is_store_build: true
449
+ },
450
+ previous: holder === null ? null : {
451
+ ...pick(holder),
452
+ is_store_build: false
453
+ }
454
+ };
455
+ });
456
+ } catch (error) {
457
+ if (attempt < MARK_ATTEMPTS && error instanceof _hot_updater_plugin_core.DatabaseConstraintError && error.reason === "unique") continue;
458
+ throw error;
459
+ }
460
+ },
193
461
  async deleteNativeBuild(id) {
194
462
  return db.transaction(async (tx) => {
195
463
  const row = await tx.findOne("native_builds", { id });
@@ -230,3 +498,4 @@ exports.NativeBuildInputError = NativeBuildInputError;
230
498
  exports.createNativeBuildApi = createNativeBuildApi;
231
499
  exports.isBuiltInBundle = isBuiltInBundle;
232
500
  exports.nativeBuildScopeKey = nativeBuildScopeKey;
501
+ exports.refuseSandboxPatchBase = refuseSandboxPatchBase;