create-expo-updates-server 0.3.0 → 0.6.0

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 (191) hide show
  1. package/README.md +4 -2
  2. package/dist/template/Dockerfile +4 -6
  3. package/dist/template/README.md +5 -10
  4. package/dist/template/app/api/admin/device-favorites/route.test.ts +125 -0
  5. package/dist/template/app/api/admin/device-favorites/route.ts +91 -0
  6. package/dist/template/app/api/admin/distribution-history/restore/route.ts +39 -0
  7. package/dist/template/app/api/admin/distribution-history/route.test.ts +55 -0
  8. package/dist/template/app/api/admin/distribution-history/route.ts +24 -0
  9. package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +2 -1
  10. package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +2 -1
  11. package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +2 -1
  12. package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +2 -1
  13. package/dist/template/app/api/admin/updates/route.test.ts +21 -0
  14. package/dist/template/app/api/admin/updates/route.ts +12 -1
  15. package/dist/template/app/globals.css +65 -68
  16. package/dist/template/app/login/page.tsx +66 -42
  17. package/dist/template/app/updates/[encodedKey]/page.tsx +382 -248
  18. package/dist/template/app/updates/devices/page.tsx +187 -145
  19. package/dist/template/app/updates/embedded/page.tsx +80 -34
  20. package/dist/template/app/updates/emergency-redirects/page.tsx +138 -71
  21. package/dist/template/app/updates/layout.tsx +2 -1
  22. package/dist/template/app/updates/page.tsx +385 -274
  23. package/dist/template/app/updates/releases.test.tsx +156 -0
  24. package/dist/template/components/admin/admin-role.ts +5 -0
  25. package/dist/template/components/admin/admin-shell.tsx +166 -95
  26. package/dist/template/components/admin/confirm-action.tsx +123 -0
  27. package/dist/template/components/admin/language-switcher.tsx +27 -16
  28. package/dist/template/components/admin/release-badges.test.tsx +50 -0
  29. package/dist/template/components/admin/release-badges.tsx +101 -0
  30. package/dist/template/components/ui/alert.tsx +10 -3
  31. package/dist/template/components/ui/avatar.tsx +108 -0
  32. package/dist/template/components/ui/badge.tsx +2 -2
  33. package/dist/template/components/ui/breadcrumb.tsx +108 -0
  34. package/dist/template/components/ui/button.tsx +5 -5
  35. package/dist/template/components/ui/card.tsx +2 -2
  36. package/dist/template/components/ui/chart.tsx +1 -1
  37. package/dist/template/components/ui/collapsible.tsx +7 -0
  38. package/dist/template/components/ui/dialog.tsx +13 -4
  39. package/dist/template/components/ui/empty.tsx +103 -0
  40. package/dist/template/components/ui/field.tsx +248 -0
  41. package/dist/template/components/ui/input-group.tsx +170 -0
  42. package/dist/template/components/ui/input.tsx +1 -1
  43. package/dist/template/components/ui/native-select.tsx +1 -1
  44. package/dist/template/components/ui/radio-group.tsx +41 -0
  45. package/dist/template/components/ui/sheet.tsx +6 -2
  46. package/dist/template/components/ui/sidebar.tsx +62 -17
  47. package/dist/template/components/ui/status-tones.ts +15 -0
  48. package/dist/template/components/ui/table.tsx +1 -1
  49. package/dist/template/components/ui/textarea.tsx +1 -1
  50. package/dist/template/docs/admin-ui.md +22 -24
  51. package/dist/template/docs/device-targeting.md +6 -12
  52. package/dist/template/docs/doctor.md +2 -2
  53. package/dist/template/docs/expo-app-setup.md +9 -27
  54. package/dist/template/docs/getting-started.md +3 -4
  55. package/dist/template/docs/getting-started.ru.md +11 -544
  56. package/dist/template/docs/index.md +1 -1
  57. package/dist/template/docs/installation-telemetry.md +6 -12
  58. package/dist/template/docs/migrations/2026-10-16-manifest-identity.sql +5 -0
  59. package/dist/template/docs/migrations/2026-11-25-device-favorites.sql +53 -0
  60. package/dist/template/docs/migrations/2026-11-25-download-on-postpone.sql +5 -0
  61. package/dist/template/docs/migrations/2026-11-25-public-distribution-history.sql +283 -0
  62. package/dist/template/docs/migrations/schema.sql +349 -0
  63. package/dist/template/docs/npm-installer.md +3 -3
  64. package/dist/template/docs/operations-guide.md +9 -7
  65. package/dist/template/docs/ota-client-lifecycle.md +30 -68
  66. package/dist/template/docs/ota-emergency-recovery.md +34 -57
  67. package/dist/template/docs/ota-update-policy.md +9 -20
  68. package/dist/template/docs/ru/admin-ui.md +33 -0
  69. package/dist/template/docs/ru/device-targeting.md +59 -0
  70. package/dist/template/docs/ru/doctor.md +98 -0
  71. package/dist/template/docs/ru/expo-app-setup.md +497 -0
  72. package/dist/template/docs/ru/getting-started.md +391 -0
  73. package/dist/template/docs/ru/index.md +50 -0
  74. package/dist/template/docs/ru/installation-telemetry.md +125 -0
  75. package/dist/template/docs/ru/npm-installer.md +71 -0
  76. package/dist/template/docs/ru/operations-guide.md +322 -0
  77. package/dist/template/docs/ru/ota-client-lifecycle.md +162 -0
  78. package/dist/template/docs/ru/ota-emergency-recovery.md +108 -0
  79. package/dist/template/docs/ru/ota-update-policy.md +69 -0
  80. package/dist/template/docs/ru/publishing-npm-installer.md +49 -0
  81. package/dist/template/docs/ru/supabase-setup.md +120 -0
  82. package/dist/template/docs/uk/admin-ui.md +33 -0
  83. package/dist/template/docs/uk/device-targeting.md +59 -0
  84. package/dist/template/docs/uk/doctor.md +98 -0
  85. package/dist/template/docs/uk/expo-app-setup.md +497 -0
  86. package/dist/template/docs/uk/getting-started.md +391 -0
  87. package/dist/template/docs/uk/index.md +50 -0
  88. package/dist/template/docs/uk/installation-telemetry.md +125 -0
  89. package/dist/template/docs/uk/npm-installer.md +71 -0
  90. package/dist/template/docs/uk/operations-guide.md +322 -0
  91. package/dist/template/docs/uk/ota-client-lifecycle.md +162 -0
  92. package/dist/template/docs/uk/ota-emergency-recovery.md +108 -0
  93. package/dist/template/docs/uk/ota-update-policy.md +69 -0
  94. package/dist/template/docs/uk/publishing-npm-installer.md +49 -0
  95. package/dist/template/docs/uk/supabase-setup.md +120 -0
  96. package/dist/template/lib/audience-ids.ts +4 -0
  97. package/dist/template/package-lock.json +46 -0
  98. package/dist/template/package.json +4 -1
  99. package/dist/template/scripts/configure-expo-app.mjs +3 -3
  100. package/dist/template/scripts/create-access-token.mjs +1 -1
  101. package/dist/template/scripts/lib/doctor.mjs +10 -7
  102. package/dist/template/scripts/lib/doctor.test.mjs +7 -5
  103. package/dist/template/scripts/lib/embedded-update-registrar.test.ts +22 -7
  104. package/dist/template/scripts/lib/expo-app-configurator.mjs +25 -22
  105. package/dist/template/scripts/lib/expo-app-configurator.test.ts +37 -2
  106. package/dist/template/scripts/lib/ota-client-configurator.mjs +152 -55
  107. package/dist/template/scripts/lib/ota-client-configurator.test.ts +60 -20
  108. package/dist/template/scripts/lib/ota-client-package.mjs +25 -0
  109. package/dist/template/scripts/lib/ota-root-hook-patcher.mjs +90 -32
  110. package/dist/template/scripts/lib/ota-root-hook-patcher.test.ts +49 -6
  111. package/dist/template/scripts/tests/device-analytics-migrations.mjs +12 -1
  112. package/dist/template/scripts/tests/device-targeting-activation.mjs +2 -3
  113. package/dist/template/scripts/tests/e2e/client-lifecycle.mjs +161 -0
  114. package/dist/template/scripts/tests/e2e/client-lifecycle.scenario.jsx +465 -0
  115. package/dist/template/scripts/tests/e2e/client-vitest.mts +18 -0
  116. package/dist/template/scripts/tests/e2e/console.mjs +86 -0
  117. package/dist/template/scripts/tests/e2e/delivery.mjs +296 -0
  118. package/dist/template/scripts/tests/e2e/device-favorites.mjs +309 -0
  119. package/dist/template/scripts/tests/e2e/installations.mjs +132 -0
  120. package/dist/template/scripts/tests/e2e/policy.mjs +253 -0
  121. package/dist/template/scripts/tests/e2e/registrar-token.mjs +59 -0
  122. package/dist/template/scripts/tests/e2e/release-matrix.mjs +307 -0
  123. package/dist/template/scripts/tests/e2e/releases.mjs +162 -0
  124. package/dist/template/scripts/tests/e2e/run.mjs +224 -0
  125. package/dist/template/scripts/tests/manifest-identity.sql +7 -5
  126. package/dist/template/scripts/tests/public-distribution-history.mjs +102 -0
  127. package/dist/template/src/client/admin-api.ts +45 -10
  128. package/dist/template/src/client/admin-types.ts +35 -1
  129. package/dist/template/src/client/devices/analytics.tsx +169 -219
  130. package/dist/template/src/client/devices/cohorts.tsx +181 -0
  131. package/dist/template/src/client/devices/device-name.tsx +37 -21
  132. package/dist/template/src/client/devices/favorite-targets.tsx +381 -0
  133. package/dist/template/src/client/devices/registry.tsx +265 -233
  134. package/dist/template/src/client/devices/use-devices-report.test.tsx +98 -0
  135. package/dist/template/src/client/devices/use-devices-report.ts +53 -82
  136. package/dist/template/src/client/devices/version-adoption-chart.tsx +48 -38
  137. package/dist/template/src/client/distribution-control.tsx +98 -53
  138. package/dist/template/src/client/distribution-history.tsx +353 -0
  139. package/dist/template/src/client/guard-action-catalog.tsx +183 -0
  140. package/dist/template/src/client/guard-action-combobox.test.ts +16 -22
  141. package/dist/template/src/client/guard-action-combobox.tsx +134 -234
  142. package/dist/template/src/client/guard-action-options.ts +4 -6
  143. package/dist/template/src/client/i18n/en.json +335 -2
  144. package/dist/template/src/client/i18n/language-switching.test.tsx +233 -0
  145. package/dist/template/src/client/i18n/messages.test.ts +102 -19
  146. package/dist/template/src/client/i18n/messages.ts +31 -16
  147. package/dist/template/src/client/i18n/provider.tsx +6 -3
  148. package/dist/template/src/client/i18n/ru.json +619 -0
  149. package/dist/template/src/client/i18n/uk.json +619 -0
  150. package/dist/template/src/client/i18n/use-formatters.ts +5 -0
  151. package/dist/template/src/client/logout-button.tsx +3 -3
  152. package/dist/template/src/client/policy-draft.ts +138 -0
  153. package/dist/template/src/client/update-audience-editor.tsx +597 -199
  154. package/dist/template/src/client/update-policy-editor.test.ts +101 -29
  155. package/dist/template/src/client/update-policy-editor.tsx +466 -307
  156. package/dist/template/src/client/use-filtered-results.ts +55 -0
  157. package/dist/template/src/server/controllers/manifest.test.ts +23 -3
  158. package/dist/template/src/server/controllers/manifest.ts +15 -1
  159. package/dist/template/src/server/devices/audience.ts +9 -5
  160. package/dist/template/src/server/lib/admin-audience-activation.test.ts +37 -90
  161. package/dist/template/src/server/lib/admin-release-history.test.ts +138 -0
  162. package/dist/template/src/server/lib/admin-updates.ts +160 -534
  163. package/dist/template/src/server/lib/distribution-history.ts +97 -0
  164. package/dist/template/src/server/lib/supabase.js +24 -0
  165. package/dist/template/src/server/lib/supabase.test.ts +38 -1
  166. package/dist/template/src/server/lib/update-policy.test.ts +20 -0
  167. package/dist/template/src/server/lib/update-policy.ts +23 -10
  168. package/dist/template/src/server/manifest/manifest-repository.ts +11 -3
  169. package/dist/template/src/server/manifest/manifest-values.test.ts +2 -0
  170. package/dist/template/src/server/manifest/manifest-values.ts +21 -13
  171. package/dist/template/templates/expo-app/scripts/ota-publish/api/database-client.mjs +7 -30
  172. package/dist/template/templates/expo-app/scripts/ota-register-embedded/index.mjs +2 -1
  173. package/dist/template/tsconfig.json +2 -2
  174. package/dist/template-manifest.json +571 -271
  175. package/package.json +1 -1
  176. package/dist/template/scripts/lib/device-identity.test.ts +0 -29
  177. package/dist/template/scripts/lib/ota-client.test.ts +0 -55
  178. package/dist/template/scripts/lib/ota-update-controller.test.ts +0 -232
  179. package/dist/template/scripts/lib/ota-updates-ui.test.tsx +0 -154
  180. package/dist/template/scripts/tests/ota-client-types.mjs +0 -69
  181. package/dist/template/templates/expo-app/ota-client/README.md +0 -73
  182. package/dist/template/templates/expo-app/ota-client/controller.ts +0 -107
  183. package/dist/template/templates/expo-app/ota-client/device-identity.ts +0 -48
  184. package/dist/template/templates/expo-app/ota-client/docs/update-lifecycle.md +0 -61
  185. package/dist/template/templates/expo-app/ota-client/index.ts +0 -16
  186. package/dist/template/templates/expo-app/ota-client/model/hooks/useOtaUpdates.ts +0 -97
  187. package/dist/template/templates/expo-app/ota-client/model/update-controller.ts +0 -293
  188. package/dist/template/templates/expo-app/ota-client/runtime.ts +0 -153
  189. package/dist/template/templates/expo-app/ota-client/types.ts +0 -85
  190. package/dist/template/templates/expo-app/ota-client/ui/ota-update-gate.tsx +0 -25
  191. package/dist/template/templates/expo-app/ota-client/ui/ota-updates.tsx +0 -196
package/README.md CHANGED
@@ -4,7 +4,7 @@ Create a standalone, self-hosted Expo Updates Server project from the
4
4
  versioned template bundled in this npm package.
5
5
 
6
6
  ```bash
7
- npx create-expo-updates-server@latest my-server
7
+ npx create-expo-updates-server@0.6.0 my-server
8
8
  cd my-server
9
9
  ```
10
10
 
@@ -13,7 +13,7 @@ To scaffold into an empty current directory instead:
13
13
  ```bash
14
14
  mkdir my-server
15
15
  cd my-server
16
- npx create-expo-updates-server@latest .
16
+ npx create-expo-updates-server@0.6.0 .
17
17
  ```
18
18
 
19
19
  The initializer performs no network requests, dependency installation, Git
@@ -29,6 +29,8 @@ Successful output provides numbered Docker and npm paths. When `.` is used,
29
29
  those steps start directly with configuration or installation instead of
30
30
  asking you to change into the directory again.
31
31
 
32
+ After setting up the server, run `npm ci` and `npm run configure-app` in the server directory. The configurator installs `expo-updates-server-client@0.1.0` and its Expo-compatible native dependencies in your application, initializes the client and mounts its update component in supported roots. Follow the [Expo application setup guide](https://github.com/tonichiga/expo-updates-server/blob/main/docs/expo-app-setup.md).
33
+
32
34
  See the
33
35
  [installer documentation](https://github.com/tonichiga/expo-updates-server/blob/main/docs/npm-installer.md)
34
36
  for Docker and npm setup instructions.
@@ -2,28 +2,27 @@ FROM node:24-alpine AS dependencies
2
2
 
3
3
  WORKDIR /app
4
4
  COPY package.json package-lock.json ./
5
- RUN npm ci
5
+ RUN npm ci --no-audit --no-fund && npm cache clean --force
6
6
 
7
7
  FROM node:24-alpine AS production-dependencies
8
8
 
9
9
  WORKDIR /app
10
10
  COPY package.json package-lock.json ./
11
- RUN npm ci --omit=dev
11
+ RUN npm ci --omit=dev --no-audit --no-fund && npm cache clean --force
12
12
 
13
13
  FROM production-dependencies AS storage-init
14
14
  COPY scripts/ensure-storage-bucket.mjs ./scripts/ensure-storage-bucket.mjs
15
15
  USER node
16
16
  ENTRYPOINT ["node", "/app/scripts/ensure-storage-bucket.mjs"]
17
17
 
18
- FROM node:24-alpine AS builder
18
+ FROM dependencies AS builder
19
19
 
20
20
  WORKDIR /app
21
21
  ENV NEXT_TELEMETRY_DISABLED=1
22
- COPY --from=dependencies /app/node_modules ./node_modules
23
22
  COPY . .
24
23
  RUN npm run build
25
24
 
26
- FROM node:24-alpine AS runner
25
+ FROM production-dependencies AS runner
27
26
 
28
27
  WORKDIR /app
29
28
  ENV NODE_ENV=production
@@ -37,7 +36,6 @@ RUN addgroup --system --gid 1001 nodejs \
37
36
  COPY --from=builder --chown=nextjs:nodejs /app/public ./public
38
37
  COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
39
38
  COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
40
- COPY --from=production-dependencies --chown=nextjs:nodejs /app/node_modules ./node_modules
41
39
  COPY --from=builder --chown=nextjs:nodejs /app/scripts ./scripts
42
40
  COPY --from=builder --chown=nextjs:nodejs /app/package.json ./package.json
43
41
 
@@ -6,8 +6,7 @@ panel, device targeting and installation analytics.
6
6
  - Publish inactive updates, review their audience, then activate them.
7
7
  - Track running app versions by runtime, channel and platform.
8
8
  - Test a release on selected devices before making it available to everyone.
9
- - Integrate identity, telemetry and manual/automatic updates through a generated
10
- `useOtaUpdates` hook, including background delivery and required-update policies.
9
+ - Integrate identity, telemetry and manual/automatic updates through the `expo-updates-server-client` package, including background delivery and required-update policies.
11
10
  - Run PostgreSQL and S3-compatible storage locally with Docker, or use Supabase.
12
11
 
13
12
  ## Start here
@@ -15,7 +14,7 @@ panel, device targeting and installation analytics.
15
14
  Create a server project:
16
15
 
17
16
  ```bash
18
- npx create-expo-updates-server@latest my-server
17
+ npx create-expo-updates-server@0.6.0 my-server
19
18
  cd my-server
20
19
  ```
21
20
 
@@ -24,7 +23,7 @@ or generate credentials/signing keys. It contains the template from its publishe
24
23
  version, not unpublished changes on GitHub or in a local checkout.
25
24
 
26
25
  Continue with the **[Quick Start](docs/getting-started.md)**
27
- ([Русский](docs/getting-started.ru.md)). It covers one complete path:
26
+ ([Русский](docs/ru/getting-started.md) · [Українська](docs/uk/getting-started.md)). It covers one complete path:
28
27
 
29
28
  1. Prepare Docker, server settings and a signing key pair.
30
29
  2. Start the server and create an administrator.
@@ -40,13 +39,9 @@ checkout. For initializer options and filesystem behavior, see
40
39
  ## Application integration
41
40
 
42
41
  Run `npm ci` in the **server directory** before `npm run configure-app`.
43
- The configurator supports static `app.json` projects and installs the client,
44
- root hook, publisher and embedded-build registration scripts. See
45
- [Expo application setup](docs/expo-app-setup.md) for supported roots and flags.
42
+ The configurator supports static `app.json` projects. It installs `expo-updates-server-client@0.1.0` and its native dependencies with `expo install`, writes the public settings to `ota-client/config.json`, and initializes the client before loading your original entry. In supported roots it imports and mounts `<OtaUpdates />` beside navigation. It also installs publisher and embedded-build registration scripts. See [Expo application setup](docs/expo-app-setup.md).
46
43
 
47
- If generation cannot be used, follow [manual client integration](docs/ota-client-lifecycle.md#manual-integration).
48
- This fallback wires the client; publisher/build registration still require
49
- separate setup. Expo Go cannot validate the release OTA lifecycle.
44
+ For a custom root or dynamic app configuration, follow [manual client integration](docs/ota-client-lifecycle.md#manual-integration). The guide covers initialization, the built-in component, and the headless hook. Configure publishing and build registration separately for a manual setup. Test the OTA lifecycle in a native release build.
50
45
 
51
46
  ## Check your setup
52
47
 
@@ -0,0 +1,125 @@
1
+ import { NextRequest, NextResponse } from "next/server";
2
+ import { beforeEach, describe, expect, it, vi } from "vitest";
3
+
4
+ const auth = vi.hoisted(() => vi.fn());
5
+ const db = vi.hoisted(() => ({ from: vi.fn(), rpc: vi.fn() }));
6
+ vi.mock("@/src/server/lib/admin-api", () => ({ requireAdminSession: auth }));
7
+ vi.mock("@/src/server/lib/supabase.js", () => ({ supabase: db }));
8
+ import { DELETE, GET, PUT } from "./route";
9
+
10
+ const user = { id: "admin-a", role: "admin" };
11
+ const target = {
12
+ platform: "ios",
13
+ target_kind: "device",
14
+ target_id: "a".repeat(64),
15
+ };
16
+ const payload = { ...target, name: "Personal phone", user_id: "admin-b" };
17
+ const request = (method = "GET", body?: unknown) =>
18
+ new NextRequest(
19
+ "http://test/api/admin/device-favorites?platform=ios&user_id=admin-b",
20
+ {
21
+ method,
22
+ body: body === undefined ? undefined : JSON.stringify(body),
23
+ },
24
+ );
25
+
26
+ describe("personal audience favorites", () => {
27
+ const state = {
28
+ select: vi.fn().mockReturnThis(),
29
+ eq: vi.fn().mockReturnThis(),
30
+ limit: vi.fn().mockReturnThis(),
31
+ maybeSingle: vi.fn(),
32
+ };
33
+ const favorites = {
34
+ delete: vi.fn().mockReturnThis(),
35
+ eq: vi.fn().mockReturnThis(),
36
+ upsert: vi.fn().mockReturnThis(),
37
+ execute: vi.fn(),
38
+ };
39
+ beforeEach(() => {
40
+ vi.clearAllMocks();
41
+ auth.mockResolvedValue(user);
42
+ db.from.mockImplementation((name) =>
43
+ name === "ota_device_state" ? state : favorites,
44
+ );
45
+ state.maybeSingle.mockResolvedValue({
46
+ data: { device_id: target.target_id },
47
+ });
48
+ favorites.execute.mockResolvedValue({ error: null });
49
+ db.rpc.mockResolvedValue({ data: [] });
50
+ });
51
+
52
+ it("derives owner from the session on every access", async () => {
53
+ const list = await GET(request());
54
+ expect(list.headers.get("cache-control")).toContain("no-store");
55
+ expect(db.rpc).toHaveBeenCalledWith("ota_list_device_favorites", {
56
+ p_user: user.id,
57
+ p_platform: "ios",
58
+ });
59
+ expect((await PUT(request("PUT", payload))).status).toBe(200);
60
+ expect(favorites.upsert).toHaveBeenCalledWith(
61
+ { ...payload, user_id: user.id },
62
+ { onConflict: "user_id,platform,target_kind,target_id" },
63
+ );
64
+ expect((await DELETE(request("DELETE", payload))).status).toBe(200);
65
+ expect(favorites.eq.mock.calls).toEqual([
66
+ ["user_id", user.id],
67
+ ["platform", "ios"],
68
+ ["target_kind", "device"],
69
+ ["target_id", target.target_id],
70
+ ]);
71
+ });
72
+
73
+ it.each([
74
+ { platform: "macos" },
75
+ { target_kind: "other" },
76
+ { target_kind: "installation" },
77
+ { target_id: "" },
78
+ { name: "" },
79
+ { name: " padded " },
80
+ { name: "abc\n123" },
81
+ { name: "x".repeat(81) },
82
+ ])("rejects invalid input before querying targets: %j", async (invalid) => {
83
+ expect((await PUT(request("PUT", { ...payload, ...invalid }))).status).toBe(
84
+ 400,
85
+ );
86
+ expect(db.from).not.toHaveBeenCalled();
87
+ });
88
+
89
+ it("lets registration disappear without losing the ability to remove a favorite", async () => {
90
+ state.maybeSingle.mockResolvedValueOnce({ data: null });
91
+ expect((await PUT(request("PUT", payload))).status).toBe(404);
92
+ expect(favorites.upsert).not.toHaveBeenCalled();
93
+ expect((await DELETE(request("DELETE", target))).status).toBe(200);
94
+ expect(state.maybeSingle).toHaveBeenCalledTimes(1);
95
+ });
96
+
97
+ it("distinguishes storage errors from missing targets without leaking database details", async () => {
98
+ const internal = { error: { message: "internal detail" } };
99
+ state.maybeSingle.mockResolvedValueOnce(internal);
100
+ db.rpc.mockResolvedValueOnce(internal);
101
+ favorites.execute.mockResolvedValueOnce(internal);
102
+ for (const res of [
103
+ await GET(request()),
104
+ await PUT(request("PUT", payload)),
105
+ await DELETE(request("DELETE", target)),
106
+ ]) {
107
+ expect(res.status).toBe(503);
108
+ expect(await res.text()).not.toContain("internal detail");
109
+ }
110
+ });
111
+ it("requires a signed-in account and rejects viewer mutations before parsing", async () => {
112
+ auth.mockResolvedValueOnce(NextResponse.json({}, { status: 401 }));
113
+ expect((await GET(request())).status).toBe(401);
114
+ auth.mockResolvedValue(NextResponse.json({}, { status: 403 }));
115
+ expect((await PUT(request("PUT", payload))).status).toBe(403);
116
+ expect((await DELETE(request("DELETE", target))).status).toBe(403);
117
+ expect(
118
+ auth.mock.calls
119
+ .slice(1)
120
+ .every(([, roles]) => roles.join() === "admin,operator"),
121
+ ).toBe(true);
122
+ expect(db.from).not.toHaveBeenCalled();
123
+ expect(db.rpc).not.toHaveBeenCalled();
124
+ });
125
+ });
@@ -0,0 +1,91 @@
1
+ import { NextRequest, NextResponse } from "next/server";
2
+ import { requireAdminSession } from "@/src/server/lib/admin-api";
3
+ import {
4
+ deviceIdPattern,
5
+ installationIdPattern,
6
+ } from "@/src/server/devices/audience";
7
+ import { supabase } from "@/src/server/lib/supabase.js";
8
+
9
+ const table = "ota_device_favorites";
10
+ const error = (message: string, status: number) =>
11
+ NextResponse.json({ error: message }, { status });
12
+ const validPlatform = (value: unknown) =>
13
+ value === "android" || value === "ios";
14
+
15
+ export async function GET(request: NextRequest) {
16
+ const user = await requireAdminSession(request);
17
+ if (user instanceof NextResponse) return user;
18
+ const platform = request.nextUrl.searchParams.get("platform");
19
+ if (!validPlatform(platform)) return error("Invalid platform", 400);
20
+ const result = await supabase.rpc("ota_list_device_favorites", {
21
+ p_user: user.id,
22
+ p_platform: platform,
23
+ });
24
+ if (result.error) return error("Favorites unavailable", 503);
25
+ return NextResponse.json(
26
+ { favorites: result.data },
27
+ { headers: { "Cache-Control": "private, no-store" } },
28
+ );
29
+ }
30
+
31
+ async function mutate(request: NextRequest, remove: boolean) {
32
+ const user = await requireAdminSession(request, ["admin", "operator"]);
33
+ if (user instanceof NextResponse) return user;
34
+ const body = await request.json().catch(() => null);
35
+ const { platform, target_kind: kind, target_id: id, name } = body ?? {};
36
+ if (
37
+ !validPlatform(platform) ||
38
+ (kind !== "device" && kind !== "installation") ||
39
+ typeof id !== "string" ||
40
+ !(kind === "device" ? deviceIdPattern : installationIdPattern).test(id)
41
+ )
42
+ return error("Invalid favorite target", 400);
43
+ if (remove) {
44
+ const result = await supabase
45
+ .from(table)
46
+ .delete()
47
+ .eq("user_id", user.id)
48
+ .eq("platform", platform)
49
+ .eq("target_kind", kind)
50
+ .eq("target_id", id)
51
+ .execute();
52
+ if (result.error) return error("Could not remove favorite", 503);
53
+ } else {
54
+ if (
55
+ typeof name !== "string" ||
56
+ !name.trim() ||
57
+ name !== name.trim() ||
58
+ name.length > 80 ||
59
+ /[\x00-\x1f\x7f]/.test(name)
60
+ )
61
+ return error("Invalid favorite name", 400);
62
+ const column = kind === "device" ? "device_id" : "installation_id";
63
+ const state = await supabase
64
+ .from("ota_device_state")
65
+ .select(column)
66
+ .eq("platform", platform)
67
+ .eq(column, id)
68
+ .limit(1)
69
+ .maybeSingle();
70
+ if (state.error) return error("Favorites unavailable", 503);
71
+ if (!state.data) return error("Target is no longer in the registry", 404);
72
+ const result = await supabase
73
+ .from(table)
74
+ .upsert(
75
+ {
76
+ user_id: user.id,
77
+ platform,
78
+ target_kind: kind,
79
+ target_id: id,
80
+ name,
81
+ },
82
+ { onConflict: "user_id,platform,target_kind,target_id" },
83
+ )
84
+ .execute();
85
+ if (result.error) return error("Could not save favorite", 503);
86
+ }
87
+ return NextResponse.json({ ok: true });
88
+ }
89
+
90
+ export const PUT = (request: NextRequest) => mutate(request, false);
91
+ export const DELETE = (request: NextRequest) => mutate(request, true);
@@ -0,0 +1,39 @@
1
+ import { NextRequest, NextResponse } from "next/server";
2
+ import { requireAdminAccess } from "@/src/server/lib/admin-api";
3
+ import {
4
+ changeDistribution,
5
+ DistributionHistoryConflictError,
6
+ } from "@/src/server/lib/distribution-history";
7
+ import { OtaDistributionBlockedError } from "@/src/server/lib/distribution-control";
8
+
9
+ export async function POST(request: NextRequest) {
10
+ const principal = await requireAdminAccess(request, "updates:write");
11
+ if (principal instanceof NextResponse) return principal;
12
+ const body = await request.json().catch(() => null);
13
+ if (
14
+ typeof body?.updateId !== "string" ||
15
+ !/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(body.updateId) ||
16
+ typeof body.expectedRevision !== "string" ||
17
+ !/^\d{1,20}$/.test(body.expectedRevision)
18
+ ) {
19
+ return NextResponse.json(
20
+ { error: "A release ID and expected revision are required." },
21
+ { status: 400 },
22
+ );
23
+ }
24
+ try {
25
+ await changeDistribution(body.updateId, "restore", body.expectedRevision);
26
+ return NextResponse.json({ ok: true });
27
+ } catch (error) {
28
+ if (
29
+ error instanceof DistributionHistoryConflictError ||
30
+ error instanceof OtaDistributionBlockedError
31
+ )
32
+ return NextResponse.json({ error: error.message }, { status: 409 });
33
+ console.error("Failed to restore public release", error);
34
+ return NextResponse.json(
35
+ { error: "Failed to restore public release." },
36
+ { status: 500 },
37
+ );
38
+ }
39
+ }
@@ -0,0 +1,55 @@
1
+ import { NextRequest, NextResponse } from "next/server";
2
+ import { beforeEach, expect, it, vi } from "vitest";
3
+
4
+ const access = vi.hoisted(() => vi.fn());
5
+ const scopes = vi.hoisted(() => vi.fn());
6
+ const change = vi.hoisted(() => vi.fn());
7
+ vi.mock("@/src/server/lib/admin-api", () => ({ requireAdminAccess: access }));
8
+ vi.mock("@/src/server/lib/distribution-history", async (original) => ({
9
+ ...(await original<typeof import("@/src/server/lib/distribution-history")>()),
10
+ getDistributionHistory: scopes, changeDistribution: change,
11
+ }));
12
+
13
+ import { DistributionHistoryConflictError } from "@/src/server/lib/distribution-history";
14
+ import { GET } from "./route";
15
+ import { POST } from "./restore/route";
16
+
17
+ const id = "11111111-1111-4111-8111-111111111111";
18
+ const url = "http://localhost/api/admin/distribution-history";
19
+ const post = (body: unknown) => new NextRequest(url + "/restore", { method: "POST", body: JSON.stringify(body) });
20
+
21
+ beforeEach(() => {
22
+ vi.resetAllMocks();
23
+ access.mockResolvedValue({ type: "session", role: "operator" });
24
+ scopes.mockResolvedValue([]);
25
+ });
26
+
27
+ it.each([
28
+ [{ type: "session", role: "viewer" }, false],
29
+ [{ type: "session", role: "operator" }, true],
30
+ [{ type: "access-token", scopes: ["updates:read"] }, false],
31
+ ])("reads history with the principal's write capability", async (principal, canWrite) => {
32
+ access.mockResolvedValue(principal);
33
+ const req = new NextRequest(url);
34
+ expect(await (await GET(req)).json()).toEqual({ canWrite, scopes: [] });
35
+ expect(access).toHaveBeenCalledWith(req, "updates:read");
36
+ });
37
+
38
+ it("requires write permission before parsing or applying a restore", async () => {
39
+ access.mockResolvedValue(NextResponse.json({}, { status: 403 }));
40
+ const req = post({ updateId: id, expectedRevision: "0" });
41
+ expect((await POST(req)).status).toBe(403);
42
+ expect(access).toHaveBeenCalledWith(req, "updates:write");
43
+ expect(change).not.toHaveBeenCalled();
44
+ });
45
+
46
+ it.each([{ updateId: id }, { updateId: "invalid", expectedRevision: "17" }])("rejects an invalid restore", async (body) => {
47
+ expect((await POST(post(body))).status).toBe(400);
48
+ expect(change).not.toHaveBeenCalled();
49
+ });
50
+
51
+ it("passes the observed revision to the database and signals a conflict on stale history", async () => {
52
+ change.mockRejectedValue(new DistributionHistoryConflictError("Refresh."));
53
+ expect((await POST(post({ updateId: id, expectedRevision: "17" }))).status).toBe(409);
54
+ expect(change).toHaveBeenCalledWith(id, "restore", "17");
55
+ });
@@ -0,0 +1,24 @@
1
+ import { NextRequest, NextResponse } from "next/server";
2
+ import { requireAdminAccess } from "@/src/server/lib/admin-api";
3
+ import { getDistributionHistory } from "@/src/server/lib/distribution-history";
4
+
5
+ export async function GET(request: NextRequest) {
6
+ const principal = await requireAdminAccess(request, "updates:read");
7
+ if (principal instanceof NextResponse) return principal;
8
+ const canWrite =
9
+ principal.type === "session"
10
+ ? principal.role !== "viewer"
11
+ : principal.scopes.includes("updates:write");
12
+ try {
13
+ return NextResponse.json({
14
+ canWrite,
15
+ scopes: await getDistributionHistory(),
16
+ });
17
+ } catch (error) {
18
+ console.error("Failed to read public distribution history", error);
19
+ return NextResponse.json(
20
+ { error: "Failed to read distribution history." },
21
+ { status: 500 },
22
+ );
23
+ }
24
+ }
@@ -1,3 +1,4 @@
1
+ import { DistributionHistoryConflictError } from "@/src/server/lib/distribution-history";
1
2
  import { requireAdminAccess } from "@/src/server/lib/admin-api";
2
3
  import {
3
4
  decodeUpdateKey,
@@ -29,7 +30,7 @@ export async function POST(request: NextRequest, context: RouteContext) {
29
30
  return NextResponse.json(
30
31
  { error: (error as Error).message || "Failed to deactivate update" },
31
32
  {
32
- status: error instanceof OtaDistributionBlockedError ? 409 : 500,
33
+ status: error instanceof OtaDistributionBlockedError || error instanceof DistributionHistoryConflictError ? 409 : 500,
33
34
  },
34
35
  );
35
36
  }
@@ -1,3 +1,4 @@
1
+ import { DistributionHistoryConflictError } from "@/src/server/lib/distribution-history";
1
2
  import { requireAdminAccess } from "@/src/server/lib/admin-api";
2
3
  import {
3
4
  decodeUpdateKey,
@@ -25,7 +26,7 @@ export async function POST(request: NextRequest, context: RouteContext) {
25
26
  return NextResponse.json(
26
27
  { error: (error as Error).message || "Failed to promote latest update" },
27
28
  {
28
- status: error instanceof OtaDistributionBlockedError ? 409 : 500,
29
+ status: error instanceof OtaDistributionBlockedError || error instanceof DistributionHistoryConflictError ? 409 : 500,
29
30
  },
30
31
  );
31
32
  }
@@ -1,3 +1,4 @@
1
+ import { DistributionHistoryConflictError } from "@/src/server/lib/distribution-history";
1
2
  import { requireAdminAccess } from "@/src/server/lib/admin-api";
2
3
  import {
3
4
  decodeUpdateKey,
@@ -25,7 +26,7 @@ export async function POST(request: NextRequest, context: RouteContext) {
25
26
  return NextResponse.json(
26
27
  { error: (error as Error).message || "Failed to rollback update" },
27
28
  {
28
- status: error instanceof OtaDistributionBlockedError ? 409 : 500,
29
+ status: error instanceof OtaDistributionBlockedError || error instanceof DistributionHistoryConflictError ? 409 : 500,
29
30
  },
30
31
  );
31
32
  }
@@ -1,3 +1,4 @@
1
+ import { DistributionHistoryConflictError } from "@/src/server/lib/distribution-history";
1
2
  import { requireAdminAccess } from "@/src/server/lib/admin-api";
2
3
  import {
3
4
  decodeUpdateKey,
@@ -92,7 +93,7 @@ export async function PATCH(request: NextRequest, context: RouteContext) {
92
93
  return NextResponse.json(
93
94
  { error: (error as Error).message || "Failed to update JSON" },
94
95
  {
95
- status: error instanceof OtaDistributionBlockedError ? 409 : 500,
96
+ status: error instanceof OtaDistributionBlockedError || error instanceof DistributionHistoryConflictError ? 409 : 500,
96
97
  },
97
98
  );
98
99
  }
@@ -96,6 +96,27 @@ describe("GET /api/admin/updates authorization", () => {
96
96
  expect(getUpdatesPage).toHaveBeenCalledOnce();
97
97
  });
98
98
 
99
+ it("forwards search and release filters to the history query", async () => {
100
+ sessionAuth.result = { id: "viewer", username: "reader", role: "viewer" };
101
+ const { GET } = await import("./route");
102
+ const response = await GET(
103
+ new NextRequest(
104
+ "https://updates.example.com/api/admin/updates?q=2.4&channel=beta&runtime=2&platform=ios&audience=devices&status=draft",
105
+ ),
106
+ );
107
+ expect(response.status).toBe(200);
108
+ expect(getUpdatesPage).toHaveBeenCalledWith(
109
+ expect.objectContaining({
110
+ q: "2.4",
111
+ channel: "beta",
112
+ runtime: "2",
113
+ platform: "ios",
114
+ audience: "devices",
115
+ status: "draft",
116
+ }),
117
+ );
118
+ });
119
+
99
120
  it("returns the app version associated with each update", async () => {
100
121
  tokenAuth.result = {
101
122
  id: "token-id",
@@ -21,7 +21,18 @@ export async function GET(request: NextRequest) {
21
21
  const sortBy = normalizeSortBy(searchParams.get("sortBy"));
22
22
  const order = normalizeSortOrder(searchParams.get("order"));
23
23
 
24
- const result = await getUpdatesPage({ page, pageSize, sortBy, order });
24
+ const result = await getUpdatesPage({
25
+ page,
26
+ pageSize,
27
+ sortBy,
28
+ order,
29
+ q: searchParams.get("q") ?? "",
30
+ platform: searchParams.get("platform") ?? "",
31
+ channel: searchParams.get("channel") ?? "",
32
+ runtime: searchParams.get("runtime") ?? "",
33
+ status: searchParams.get("status") ?? "",
34
+ audience: searchParams.get("audience") ?? "",
35
+ });
25
36
  return NextResponse.json(result);
26
37
  } catch (error: unknown) {
27
38
  return NextResponse.json(