create-expo-updates-server 0.1.4 → 0.1.6

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 (67) hide show
  1. package/dist/template/.env.docker.example +1 -0
  2. package/dist/template/.env.example +2 -0
  3. package/dist/template/README.md +4 -1
  4. package/dist/template/app/api/admin/distribution-control/route.test.ts +153 -0
  5. package/dist/template/app/api/admin/distribution-control/route.ts +111 -0
  6. package/dist/template/app/api/admin/guard-actions/[id]/route.test.ts +60 -0
  7. package/dist/template/app/api/admin/guard-actions/[id]/route.ts +32 -0
  8. package/dist/template/app/api/admin/guard-actions/route.test.ts +123 -0
  9. package/dist/template/app/api/admin/guard-actions/route.ts +66 -0
  10. package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +4 -1
  11. package/dist/template/app/api/admin/updates/[encodedKey]/policy/route.test.ts +171 -0
  12. package/dist/template/app/api/admin/updates/[encodedKey]/policy/route.ts +75 -0
  13. package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +4 -1
  14. package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +4 -1
  15. package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +4 -1
  16. package/dist/template/app/updates/[encodedKey]/page.tsx +74 -6
  17. package/dist/template/app/updates/page.tsx +22 -1
  18. package/dist/template/docs/getting-started.md +1 -0
  19. package/dist/template/docs/getting-started.ru.md +2 -1
  20. package/dist/template/docs/migrations/2026-09-01-ota-update-policy.sql +108 -0
  21. package/dist/template/docs/migrations/2026-09-01-p1-guard-actions.sql +19 -0
  22. package/dist/template/docs/migrations/2026-09-01-p2-simplify-ota-guard-policy.sql +69 -0
  23. package/dist/template/docs/migrations/2026-09-01-policy-publication-correction.sql +44 -0
  24. package/dist/template/docs/migrations/2026-09-02-ota-distribution-control.sql +377 -0
  25. package/dist/template/docs/migrations/schema.sql +539 -2
  26. package/dist/template/docs/operations-guide.md +52 -1
  27. package/dist/template/docs/ota-emergency-recovery.md +22 -0
  28. package/dist/template/docs/ota-update-policy.md +89 -0
  29. package/dist/template/docs/supabase-setup.md +2 -1
  30. package/dist/template/package-lock.json +2 -2
  31. package/dist/template/package.json +1 -1
  32. package/dist/template/src/client/admin-api.ts +80 -1
  33. package/dist/template/src/client/admin-types.ts +50 -2
  34. package/dist/template/src/client/distribution-control.tsx +231 -0
  35. package/dist/template/src/client/guard-action-combobox.test.ts +26 -0
  36. package/dist/template/src/client/guard-action-combobox.tsx +267 -0
  37. package/dist/template/src/client/guard-action-options.test.ts +62 -0
  38. package/dist/template/src/client/guard-action-options.ts +70 -0
  39. package/dist/template/src/client/update-policy-editor.test.ts +36 -0
  40. package/dist/template/src/client/update-policy-editor.tsx +436 -0
  41. package/dist/template/src/server/controllers/assets.test.ts +87 -0
  42. package/dist/template/src/server/controllers/assets.ts +26 -0
  43. package/dist/template/src/server/controllers/manifest.test.ts +413 -2
  44. package/dist/template/src/server/controllers/manifest.ts +103 -38
  45. package/dist/template/src/server/lib/admin-distribution-guard.test.ts +61 -0
  46. package/dist/template/src/server/lib/admin-update-policy.test.ts +176 -0
  47. package/dist/template/src/server/lib/admin-updates.test.ts +72 -0
  48. package/dist/template/src/server/lib/admin-updates.ts +169 -24
  49. package/dist/template/src/server/lib/distribution-control-migration.test.ts +94 -0
  50. package/dist/template/src/server/lib/distribution-control.test.ts +181 -0
  51. package/dist/template/src/server/lib/distribution-control.ts +259 -0
  52. package/dist/template/src/server/lib/guard-actions-migration.test.ts +56 -0
  53. package/dist/template/src/server/lib/guard-actions.test.ts +142 -0
  54. package/dist/template/src/server/lib/guard-actions.ts +106 -0
  55. package/dist/template/src/server/lib/ota-policy-migrations.test.ts +64 -0
  56. package/dist/template/src/server/lib/simple-policy-migration.test.ts +97 -0
  57. package/dist/template/src/server/lib/supabase.js +95 -8
  58. package/dist/template/src/server/lib/supabase.test.ts +194 -0
  59. package/dist/template/src/server/lib/update-policy.test.ts +98 -0
  60. package/dist/template/src/server/lib/update-policy.ts +171 -0
  61. package/dist/template/src/server/manifest/manifest-repository.ts +10 -1
  62. package/dist/template/src/server/manifest/manifest-response.ts +0 -16
  63. package/dist/template/src/server/manifest/manifest-values.test.ts +54 -0
  64. package/dist/template/src/server/manifest/manifest-values.ts +145 -0
  65. package/dist/template/tsconfig.json +4 -1
  66. package/dist/template-manifest.json +239 -59
  67. package/package.json +1 -1
@@ -0,0 +1,436 @@
1
+ "use client";
2
+
3
+ import {
4
+ createGuardAction,
5
+ deleteGuardAction,
6
+ getGuardActions,
7
+ getUpdatePolicy,
8
+ putUpdatePolicy,
9
+ } from "@/src/client/admin-api";
10
+ import {
11
+ GuardActionItem,
12
+ UpdatePolicy,
13
+ UpdatePolicyGuard,
14
+ } from "@/src/client/admin-types";
15
+ import GuardActionCombobox from "@/src/client/guard-action-combobox";
16
+ import {
17
+ beginCatalogAttempt,
18
+ GuardActionOption,
19
+ isCurrentCatalogAttempt,
20
+ mergeGuardActionOptions,
21
+ normalizeCreatableGuardAction,
22
+ } from "@/src/client/guard-action-options";
23
+ import { FormEvent, useEffect, useRef, useState } from "react";
24
+
25
+ function payloadText(guard: UpdatePolicyGuard | null): string {
26
+ return guard?.payload === undefined
27
+ ? ""
28
+ : JSON.stringify(guard.payload, null, 2);
29
+ }
30
+
31
+ export default function UpdatePolicyEditor({
32
+ encodedKey,
33
+ onSaved,
34
+ }: {
35
+ encodedKey: string;
36
+ onSaved: () => Promise<void>;
37
+ }) {
38
+ const [policy, setPolicy] = useState<UpdatePolicy | null>(null);
39
+ const [payload, setPayload] = useState("");
40
+ const [error, setError] = useState("");
41
+ const [message, setMessage] = useState("");
42
+ const [saving, setSaving] = useState(false);
43
+ const [guardActions, setGuardActions] = useState<GuardActionItem[]>([]);
44
+ const [canWriteGuardActions, setCanWriteGuardActions] = useState(false);
45
+ const [catalogError, setCatalogError] = useState("");
46
+ const [catalogCreateValue, setCatalogCreateValue] = useState("");
47
+ const [catalogCreateError, setCatalogCreateError] = useState("");
48
+ const [catalogCreatePending, setCatalogCreatePending] = useState(false);
49
+ const [actionError, setActionError] = useState("");
50
+ const actionAttempt = useRef({ current: 0 });
51
+
52
+ const loadGuardActions = async () => {
53
+ try {
54
+ const result = await getGuardActions();
55
+ setGuardActions(result.items);
56
+ setCanWriteGuardActions(result.canWrite);
57
+ setCatalogError("");
58
+ } catch (loadError: unknown) {
59
+ setCatalogError(
60
+ (loadError as Error).message || "Failed to load action catalog",
61
+ );
62
+ }
63
+ };
64
+
65
+ useEffect(() => {
66
+ void getUpdatePolicy(encodedKey)
67
+ .then((loaded) => {
68
+ beginCatalogAttempt(actionAttempt.current);
69
+ setActionError("");
70
+ setPolicy(loaded);
71
+ setPayload(payloadText(loaded.guard));
72
+ })
73
+ .catch((loadError: unknown) =>
74
+ setError((loadError as Error).message || "Failed to load policy"),
75
+ );
76
+ }, [encodedKey]);
77
+
78
+ useEffect(() => {
79
+ void loadGuardActions();
80
+ }, []);
81
+
82
+ if (!policy) {
83
+ return (
84
+ <section className="rounded-xl border border-zinc-200 bg-white p-4 text-sm dark:border-zinc-800 dark:bg-zinc-900">
85
+ {error || "Loading update policy…"}
86
+ </section>
87
+ );
88
+ }
89
+
90
+ const readOnly = !policy.editable;
91
+ const editingDisabled = readOnly || saving;
92
+ const guardActionOptions = mergeGuardActionOptions(
93
+ guardActions,
94
+ policy.guard ? [policy.guard.action] : [],
95
+ );
96
+
97
+ const addGuardAction = (item: GuardActionItem) => {
98
+ setGuardActions((current) => {
99
+ const withoutDuplicate = current.filter(
100
+ (candidate) => candidate.actionKey !== item.actionKey,
101
+ );
102
+ return [...withoutDuplicate, item].sort((left, right) =>
103
+ left.actionKey.localeCompare(right.actionKey, "en"),
104
+ );
105
+ });
106
+ };
107
+
108
+ const changeGuardAction = (action: string) => {
109
+ beginCatalogAttempt(actionAttempt.current);
110
+ setActionError("");
111
+ setPolicy((current) =>
112
+ current?.guard
113
+ ? { ...current, guard: { ...current.guard, action } }
114
+ : current,
115
+ );
116
+ };
117
+
118
+ const persistSelectedAction = async (action: string) => {
119
+ const attempt = beginCatalogAttempt(actionAttempt.current);
120
+ try {
121
+ addGuardAction(await createGuardAction(action));
122
+ if (isCurrentCatalogAttempt(actionAttempt.current, attempt)) {
123
+ setActionError("");
124
+ }
125
+ } catch (createError: unknown) {
126
+ if (isCurrentCatalogAttempt(actionAttempt.current, attempt)) {
127
+ setActionError(
128
+ (createError as Error).message ||
129
+ "Action remains selected but was not saved to the catalog.",
130
+ );
131
+ }
132
+ }
133
+ };
134
+
135
+ const createCatalogOnlyAction = async (event: FormEvent) => {
136
+ event.preventDefault();
137
+ let action: string;
138
+ try {
139
+ action = normalizeCreatableGuardAction(catalogCreateValue);
140
+ } catch (validationError: unknown) {
141
+ setCatalogCreateError((validationError as Error).message);
142
+ return;
143
+ }
144
+
145
+ setCatalogCreatePending(true);
146
+ setCatalogCreateError("");
147
+ try {
148
+ addGuardAction(await createGuardAction(action));
149
+ setCatalogCreateValue("");
150
+ } catch (createError: unknown) {
151
+ setCatalogCreateError(
152
+ (createError as Error).message ||
153
+ "Failed to add action to the catalog.",
154
+ );
155
+ } finally {
156
+ setCatalogCreatePending(false);
157
+ }
158
+ };
159
+
160
+ const removeGuardAction = async (option: GuardActionOption) => {
161
+ if (
162
+ !option.id ||
163
+ !window.confirm(
164
+ `Delete "${option.actionKey}" from the action catalog? Existing policies are unaffected.`,
165
+ )
166
+ ) {
167
+ return;
168
+ }
169
+ try {
170
+ await deleteGuardAction(option.id);
171
+ setGuardActions((current) =>
172
+ current.filter((item) => item.id !== option.id),
173
+ );
174
+ setCatalogError("");
175
+ } catch (deleteError: unknown) {
176
+ setCatalogError(
177
+ (deleteError as Error).message ||
178
+ "Failed to delete action from catalog",
179
+ );
180
+ }
181
+ };
182
+
183
+ const save = async () => {
184
+ setError("");
185
+ setMessage("");
186
+ setSaving(true);
187
+ try {
188
+ let guard: UpdatePolicyGuard | null = null;
189
+ if (policy.guard) {
190
+ if (!policy.guard.action) {
191
+ throw new Error("Select or create a Guard action.");
192
+ }
193
+ const trimmedPayload = payload.trim();
194
+ guard = {
195
+ action: policy.guard.action,
196
+ ...(trimmedPayload
197
+ ? { payload: JSON.parse(trimmedPayload) }
198
+ : {}),
199
+ };
200
+ }
201
+ const saved = await putUpdatePolicy(
202
+ encodedKey,
203
+ { delivery: policy.delivery, guard },
204
+ policy.policyVersion,
205
+ );
206
+ setPolicy(saved);
207
+ setPayload(payloadText(saved.guard));
208
+ setMessage(`Policy saved as version ${saved.policyVersion}.`);
209
+ await onSaved();
210
+ } catch (saveError: unknown) {
211
+ setError(
212
+ saveError instanceof SyntaxError
213
+ ? "Guard payload must be valid JSON."
214
+ : (saveError as Error).message || "Failed to save policy",
215
+ );
216
+ } finally {
217
+ setSaving(false);
218
+ }
219
+ };
220
+
221
+ return (
222
+ <section className="space-y-4 rounded-xl border border-zinc-200 bg-white p-4 dark:border-zinc-800 dark:bg-zinc-900">
223
+ <div className="flex flex-wrap items-center justify-between gap-3">
224
+ <div>
225
+ <h2 className="font-semibold">Update policy</h2>
226
+ <p className="text-xs text-zinc-500">
227
+ Version {policy.policyVersion}
228
+ {policy.publishedAt
229
+ ? ` · published ${new Date(policy.publishedAt).toLocaleString()}`
230
+ : " · draft"}
231
+ </p>
232
+ </div>
233
+ <label className="text-sm">
234
+ Delivery{" "}
235
+ <select
236
+ value={policy.delivery}
237
+ disabled={editingDisabled}
238
+ onChange={(event) =>
239
+ setPolicy({
240
+ ...policy,
241
+ delivery: event.target.value as UpdatePolicy["delivery"],
242
+ })
243
+ }
244
+ className="rounded-md border border-zinc-300 bg-transparent px-2 py-2 dark:border-zinc-700"
245
+ >
246
+ <option value="manual">Manual</option>
247
+ <option value="background">Background</option>
248
+ </select>
249
+ </label>
250
+ </div>
251
+
252
+ {readOnly ? (
253
+ <div className="rounded-md border border-zinc-300 bg-zinc-50 p-3 text-sm dark:border-zinc-700 dark:bg-zinc-950">
254
+ Published policies are read-only. Publish a new update ID to change
255
+ policy.
256
+ </div>
257
+ ) : null}
258
+ {policy.guard && policy.delivery === "manual" ? (
259
+ <div className="rounded-md border border-blue-300 bg-blue-50 p-3 text-sm text-blue-900 dark:border-blue-800 dark:bg-blue-950/30 dark:text-blue-200">
260
+ <strong>Note:</strong> a Guard is usually paired with Background
261
+ delivery so the candidate can fetch silently. Activation is still
262
+ allowed.
263
+ </div>
264
+ ) : null}
265
+ {error ? <p className="text-sm text-red-600">{error}</p> : null}
266
+ {message ? <p className="text-sm text-emerald-600">{message}</p> : null}
267
+ {catalogError ? (
268
+ <div
269
+ role="status"
270
+ className="flex items-center gap-2 text-sm text-amber-700 dark:text-amber-300"
271
+ >
272
+ <span>Action catalog: {catalogError}</span>
273
+ <button
274
+ type="button"
275
+ onClick={() => void loadGuardActions()}
276
+ className="underline"
277
+ >
278
+ Retry
279
+ </button>
280
+ </div>
281
+ ) : null}
282
+ {canWriteGuardActions ? (
283
+ <form
284
+ onSubmit={(event) => void createCatalogOnlyAction(event)}
285
+ className="flex flex-wrap items-end gap-2 rounded-md border border-zinc-200 p-3 dark:border-zinc-800"
286
+ >
287
+ <label className="min-w-56 flex-1 text-xs text-zinc-500">
288
+ Add action to catalog
289
+ <input
290
+ value={catalogCreateValue}
291
+ maxLength={100}
292
+ disabled={catalogCreatePending}
293
+ onChange={(event) => {
294
+ setCatalogCreateValue(event.target.value);
295
+ setCatalogCreateError("");
296
+ }}
297
+ aria-describedby="guard-action-catalog-create-help"
298
+ className="mt-1 w-full rounded border border-zinc-300 bg-transparent p-2 text-sm dark:border-zinc-700"
299
+ placeholder="Reusable action name"
300
+ />
301
+ </label>
302
+ <button
303
+ type="submit"
304
+ disabled={catalogCreatePending}
305
+ className="rounded border border-zinc-300 px-3 py-2 text-sm disabled:opacity-40 dark:border-zinc-700"
306
+ >
307
+ {catalogCreatePending ? "Adding…" : "Add to catalog"}
308
+ </button>
309
+ <p
310
+ id="guard-action-catalog-create-help"
311
+ className="w-full text-xs text-zinc-500"
312
+ >
313
+ Adds a reusable catalog item without changing this policy.
314
+ </p>
315
+ {catalogCreateError ? (
316
+ <div
317
+ role="status"
318
+ className="flex w-full items-center gap-2 text-xs text-amber-700 dark:text-amber-300"
319
+ >
320
+ <span>{catalogCreateError}</span>
321
+ <button type="submit" className="underline">
322
+ Retry
323
+ </button>
324
+ </div>
325
+ ) : null}
326
+ </form>
327
+ ) : null}
328
+ <div className="space-y-2 rounded-md border border-zinc-200 p-3 dark:border-zinc-800">
329
+ <h3 className="text-sm font-medium">Action catalog</h3>
330
+ {guardActions.length > 0 ? (
331
+ <ul
332
+ aria-label="Guard action catalog entries"
333
+ className="max-h-40 space-y-1 overflow-auto"
334
+ >
335
+ {guardActions.map((item) => (
336
+ <li
337
+ key={item.id}
338
+ className="flex items-center justify-between gap-2 rounded bg-zinc-50 px-2 py-1 text-sm dark:bg-zinc-950"
339
+ >
340
+ <span className="min-w-0 truncate" title={item.actionKey}>
341
+ {item.actionKey}
342
+ </span>
343
+ {canWriteGuardActions ? (
344
+ <button
345
+ type="button"
346
+ aria-label={`Delete ${item.actionKey} from action catalog`}
347
+ onClick={() =>
348
+ void removeGuardAction({
349
+ id: item.id,
350
+ actionKey: item.actionKey,
351
+ persisted: true,
352
+ })
353
+ }
354
+ className="rounded px-2 py-1 text-xs text-red-600"
355
+ >
356
+ Delete
357
+ </button>
358
+ ) : null}
359
+ </li>
360
+ ))}
361
+ </ul>
362
+ ) : (
363
+ <p className="text-xs text-zinc-500">No catalog actions found.</p>
364
+ )}
365
+ </div>
366
+
367
+ <div className="space-y-3 rounded-lg border border-zinc-300 p-3 dark:border-zinc-700">
368
+ <label className="flex items-center gap-2 text-sm font-medium">
369
+ <input
370
+ type="checkbox"
371
+ checked={policy.guard !== null}
372
+ disabled={editingDisabled}
373
+ onChange={(event) => {
374
+ beginCatalogAttempt(actionAttempt.current);
375
+ setActionError("");
376
+ setPolicy({
377
+ ...policy,
378
+ guard: event.target.checked
379
+ ? { action: guardActions[0]?.actionKey || "" }
380
+ : null,
381
+ });
382
+ if (!event.target.checked) setPayload("");
383
+ }}
384
+ />
385
+ Guard enabled
386
+ </label>
387
+ <p className="text-xs text-zinc-500">
388
+ When enabled, this Guard applies unconditionally whenever this OTA is
389
+ selected for a client.
390
+ </p>
391
+ {policy.guard ? (
392
+ <>
393
+ <GuardActionCombobox
394
+ value={policy.guard.action}
395
+ options={guardActionOptions}
396
+ selectionDisabled={editingDisabled}
397
+ canMutateCatalog={canWriteGuardActions}
398
+ mutationError={actionError}
399
+ onChange={changeGuardAction}
400
+ onCreate={persistSelectedAction}
401
+ onDelete={removeGuardAction}
402
+ onRetry={
403
+ actionError
404
+ ? () => persistSelectedAction(policy.guard?.action || "")
405
+ : undefined
406
+ }
407
+ />
408
+ <label className="block text-xs text-zinc-500">
409
+ Optional custom JSON payload
410
+ <textarea
411
+ value={payload}
412
+ disabled={editingDisabled}
413
+ onChange={(event) => setPayload(event.target.value)}
414
+ className="mt-1 h-24 w-full rounded border border-zinc-300 bg-transparent p-2 font-mono text-xs dark:border-zinc-700"
415
+ placeholder='{"message":"Optional"}'
416
+ />
417
+ </label>
418
+ </>
419
+ ) : (
420
+ <p className="text-sm text-zinc-500">
421
+ No Guard will be emitted in the update manifest.
422
+ </p>
423
+ )}
424
+ </div>
425
+
426
+ <button
427
+ type="button"
428
+ disabled={editingDisabled}
429
+ onClick={() => void save()}
430
+ className="rounded bg-blue-600 px-4 py-2 text-sm text-white disabled:opacity-40"
431
+ >
432
+ Save policy
433
+ </button>
434
+ </section>
435
+ );
436
+ }
@@ -0,0 +1,87 @@
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { NextRequest } from "next/server";
3
+
4
+ const state = vi.hoisted(() => ({
5
+ blocked: false,
6
+ readError: null as Error | null,
7
+ tableReads: 0,
8
+ downloads: 0,
9
+ }));
10
+
11
+ vi.mock("../lib/distribution-control", () => ({
12
+ isOtaDistributionBlocked: vi.fn(async () => {
13
+ if (state.readError) throw state.readError;
14
+ return state.blocked;
15
+ }),
16
+ }));
17
+
18
+ vi.mock("../lib/supabase.js", () => ({
19
+ SUPABASE_BUCKET: "updates",
20
+ supabase: {
21
+ from: () => {
22
+ state.tableReads += 1;
23
+ return {
24
+ select: () => ({
25
+ eq: () => ({
26
+ maybeSingle: async () => ({ data: null, error: null }),
27
+ }),
28
+ }),
29
+ };
30
+ },
31
+ storage: {
32
+ from: () => ({
33
+ download: async () => {
34
+ state.downloads += 1;
35
+ return { data: null, error: { message: "missing" } };
36
+ },
37
+ }),
38
+ },
39
+ },
40
+ }));
41
+
42
+ async function requestAsset() {
43
+ const { default: assetsController } = await import("./assets");
44
+ return assetsController(
45
+ new NextRequest(
46
+ "https://updates.example.com/api/assets?runtimeVersion=1&platform=ios&channel=production&updateId=11111111-1111-4111-8111-111111111111&assetPath=bundle.js",
47
+ ),
48
+ );
49
+ }
50
+
51
+ describe("GET /api/assets distribution gate", () => {
52
+ beforeEach(() => {
53
+ vi.resetModules();
54
+ state.blocked = false;
55
+ state.readError = null;
56
+ state.tableReads = 0;
57
+ state.downloads = 0;
58
+ });
59
+
60
+ it("returns no-store 503 before update lookup when globally blocked", async () => {
61
+ state.blocked = true;
62
+
63
+ const response = await requestAsset();
64
+
65
+ expect(response.status).toBe(503);
66
+ expect(response.headers.get("cache-control")).toBe("no-store");
67
+ expect(state.tableReads).toBe(0);
68
+ expect(state.downloads).toBe(0);
69
+ });
70
+
71
+ it("fails closed before lookup when global state cannot be read", async () => {
72
+ state.readError = new Error("database unavailable");
73
+
74
+ const response = await requestAsset();
75
+
76
+ expect(response.status).toBe(503);
77
+ expect(response.headers.get("cache-control")).toBe("no-store");
78
+ expect(state.tableReads).toBe(0);
79
+ });
80
+
81
+ it("preserves the existing lookup path while distribution is active", async () => {
82
+ const response = await requestAsset();
83
+
84
+ expect(response.status).toBe(404);
85
+ expect(state.tableReads).toBe(1);
86
+ });
87
+ });
@@ -3,6 +3,7 @@ import { supabase, SUPABASE_BUCKET } from "../lib/supabase.js";
3
3
  import { toPosixPath } from "../lib/manifest-helpers.js";
4
4
  import { Buffer } from "buffer";
5
5
  import { NextRequest } from "next/server.js";
6
+ import { isOtaDistributionBlocked } from "../lib/distribution-control";
6
7
 
7
8
  type OtaUpdateRow = {
8
9
  update_id: string;
@@ -57,6 +58,30 @@ const assetsController = async (req: NextRequest) => {
57
58
  return Response.json({ error: "Method not allowed." }, { status: 405 });
58
59
  }
59
60
 
61
+ try {
62
+ if (await isOtaDistributionBlocked()) {
63
+ return Response.json(
64
+ { error: "OTA distribution is temporarily unavailable." },
65
+ {
66
+ status: 503,
67
+ headers: { "Cache-Control": "no-store" },
68
+ },
69
+ );
70
+ }
71
+ } catch (error: unknown) {
72
+ console.error(
73
+ "Failed to read OTA distribution control; failing asset request closed:",
74
+ error,
75
+ );
76
+ return Response.json(
77
+ { error: "OTA distribution is temporarily unavailable." },
78
+ {
79
+ status: 503,
80
+ headers: { "Cache-Control": "no-store" },
81
+ },
82
+ );
83
+ }
84
+
60
85
  const searchParams = req.nextUrl.searchParams;
61
86
 
62
87
  const runtimeVersion = searchParams.get("runtimeVersion");
@@ -202,6 +227,7 @@ const assetsController = async (req: NextRequest) => {
202
227
  headers: {
203
228
  "Content-Type": contentType,
204
229
  "Content-Length": buffer.length.toString(),
230
+ "Cache-Control": "private, no-store",
205
231
  },
206
232
  });
207
233
  } catch (error: unknown) {