create-expo-updates-server 0.1.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 (117) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -0
  3. package/bin/create-expo-updates-server.mjs +5 -0
  4. package/dist/template/.env.docker.example +15 -0
  5. package/dist/template/.env.example +35 -0
  6. package/dist/template/Dockerfile +42 -0
  7. package/dist/template/LICENSE +21 -0
  8. package/dist/template/README.md +665 -0
  9. package/dist/template/app/api/admin/access-tokens/[id]/route.ts +25 -0
  10. package/dist/template/app/api/admin/access-tokens/route.test.ts +73 -0
  11. package/dist/template/app/api/admin/access-tokens/route.ts +90 -0
  12. package/dist/template/app/api/admin/embedded-updates/[embeddedUpdateId]/route.ts +25 -0
  13. package/dist/template/app/api/admin/embedded-updates/route.test.ts +102 -0
  14. package/dist/template/app/api/admin/embedded-updates/route.ts +47 -0
  15. package/dist/template/app/api/admin/emergency-redirects/[id]/route.ts +62 -0
  16. package/dist/template/app/api/admin/emergency-redirects/route.test.ts +84 -0
  17. package/dist/template/app/api/admin/emergency-redirects/route.ts +56 -0
  18. package/dist/template/app/api/admin/login/route.test.ts +55 -0
  19. package/dist/template/app/api/admin/login/route.ts +52 -0
  20. package/dist/template/app/api/admin/logout/route.ts +20 -0
  21. package/dist/template/app/api/admin/session/route.ts +15 -0
  22. package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +33 -0
  23. package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +29 -0
  24. package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +29 -0
  25. package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +96 -0
  26. package/dist/template/app/api/admin/updates/route.test.ts +142 -0
  27. package/dist/template/app/api/admin/updates/route.ts +32 -0
  28. package/dist/template/app/api/assets/route.ts +6 -0
  29. package/dist/template/app/api/health/route.test.ts +26 -0
  30. package/dist/template/app/api/health/route.ts +42 -0
  31. package/dist/template/app/api/manifest/route.ts +6 -0
  32. package/dist/template/app/api/update-complete/route.ts +6 -0
  33. package/dist/template/app/favicon.ico +0 -0
  34. package/dist/template/app/globals.css +26 -0
  35. package/dist/template/app/layout.tsx +34 -0
  36. package/dist/template/app/login/page.tsx +77 -0
  37. package/dist/template/app/page.tsx +12 -0
  38. package/dist/template/app/updates/[encodedKey]/page.tsx +482 -0
  39. package/dist/template/app/updates/embedded/page.tsx +164 -0
  40. package/dist/template/app/updates/emergency-redirects/page.tsx +430 -0
  41. package/dist/template/app/updates/layout.tsx +14 -0
  42. package/dist/template/app/updates/page.tsx +281 -0
  43. package/dist/template/docker-compose.yml +100 -0
  44. package/dist/template/docs/expo-app-setup.md +497 -0
  45. package/dist/template/docs/getting-started.md +502 -0
  46. package/dist/template/docs/getting-started.ru.md +623 -0
  47. package/dist/template/docs/index.md +57 -0
  48. package/dist/template/docs/migrations/schema.sql +363 -0
  49. package/dist/template/docs/npm-installer.md +83 -0
  50. package/dist/template/docs/operations-guide.md +270 -0
  51. package/dist/template/docs/ota-emergency-recovery.md +109 -0
  52. package/dist/template/docs/supabase-setup.md +133 -0
  53. package/dist/template/eslint.config.mjs +30 -0
  54. package/dist/template/gitignore.template +58 -0
  55. package/dist/template/next.config.ts +7 -0
  56. package/dist/template/package-lock.json +12156 -0
  57. package/dist/template/package.json +50 -0
  58. package/dist/template/postcss.config.mjs +7 -0
  59. package/dist/template/public/file.svg +1 -0
  60. package/dist/template/public/globe.svg +1 -0
  61. package/dist/template/public/next.svg +1 -0
  62. package/dist/template/public/vercel.svg +1 -0
  63. package/dist/template/public/window.svg +1 -0
  64. package/dist/template/scripts/configure-expo-app.mjs +145 -0
  65. package/dist/template/scripts/create-access-token.mjs +135 -0
  66. package/dist/template/scripts/create-admin.mjs +169 -0
  67. package/dist/template/scripts/import-emergency-redirects.mjs +126 -0
  68. package/dist/template/scripts/lib/embedded-update-registrar.test.ts +64 -0
  69. package/dist/template/scripts/lib/expo-app-configurator.mjs +666 -0
  70. package/dist/template/scripts/lib/expo-app-configurator.test.ts +463 -0
  71. package/dist/template/scripts/lib/expo-app-prompts.mjs +139 -0
  72. package/dist/template/scripts/lib/expo-app-prompts.test.ts +97 -0
  73. package/dist/template/scripts/lib/expo-export-command.test.ts +31 -0
  74. package/dist/template/scripts/lib/ota-publish-retry.test.ts +33 -0
  75. package/dist/template/scripts/manage-emergency-redirect.mjs +483 -0
  76. package/dist/template/scripts/sync-docker-database-password.sh +16 -0
  77. package/dist/template/src/client/admin-api.ts +174 -0
  78. package/dist/template/src/client/admin-types.ts +93 -0
  79. package/dist/template/src/client/logout-button.tsx +31 -0
  80. package/dist/template/src/server/controllers/assets.ts +216 -0
  81. package/dist/template/src/server/controllers/manifest.test.ts +403 -0
  82. package/dist/template/src/server/controllers/manifest.ts +626 -0
  83. package/dist/template/src/server/controllers/update-complete.ts +36 -0
  84. package/dist/template/src/server/lib/admin-access-tokens.ts +169 -0
  85. package/dist/template/src/server/lib/admin-api.ts +101 -0
  86. package/dist/template/src/server/lib/admin-auth.ts +208 -0
  87. package/dist/template/src/server/lib/admin-embedded-updates.ts +150 -0
  88. package/dist/template/src/server/lib/admin-updates.ts +1282 -0
  89. package/dist/template/src/server/lib/common.js +24 -0
  90. package/dist/template/src/server/lib/emergency-channel-redirects.ts +310 -0
  91. package/dist/template/src/server/lib/env.js +8 -0
  92. package/dist/template/src/server/lib/manifest-helpers.js +91 -0
  93. package/dist/template/src/server/lib/response.js +11 -0
  94. package/dist/template/src/server/lib/signing.js +55 -0
  95. package/dist/template/src/server/lib/supabase.js +763 -0
  96. package/dist/template/src/server/manifest/manifest-repository.ts +269 -0
  97. package/dist/template/src/server/manifest/manifest-request.ts +21 -0
  98. package/dist/template/src/server/manifest/manifest-response.ts +235 -0
  99. package/dist/template/src/server/manifest/manifest-values.ts +62 -0
  100. package/dist/template/templates/expo-app/.env.ota.example +18 -0
  101. package/dist/template/templates/expo-app/ci_scripts/ci_post_xcodebuild.sh +6 -0
  102. package/dist/template/templates/expo-app/ci_scripts/register-embedded-update.sh +36 -0
  103. package/dist/template/templates/expo-app/plugins/with-ota-embedded-registration.js +33 -0
  104. package/dist/template/templates/expo-app/scripts/ota-publish/api/database-client.mjs +87 -0
  105. package/dist/template/templates/expo-app/scripts/ota-publish/api/storage-client.mjs +57 -0
  106. package/dist/template/templates/expo-app/scripts/ota-publish/index.mjs +182 -0
  107. package/dist/template/templates/expo-app/scripts/ota-publish/lib/common.mjs +84 -0
  108. package/dist/template/templates/expo-app/scripts/ota-publish/lib/expo-export-command.mjs +24 -0
  109. package/dist/template/templates/expo-app/scripts/ota-publish/lib/retry.mjs +25 -0
  110. package/dist/template/templates/expo-app/scripts/ota-publish/shared/config.mjs +48 -0
  111. package/dist/template/templates/expo-app/scripts/ota-register-embedded/android-register-embedded-update.sh +18 -0
  112. package/dist/template/templates/expo-app/scripts/ota-register-embedded/index.mjs +275 -0
  113. package/dist/template/tsconfig.json +36 -0
  114. package/dist/template/vitest.config.mts +20 -0
  115. package/dist/template-manifest.json +564 -0
  116. package/lib/cli.mjs +549 -0
  117. package/package.json +41 -0
@@ -0,0 +1,174 @@
1
+ import {
2
+ AdminSessionResponse,
3
+ EmbeddedUpdatesListResponse,
4
+ EmergencyRedirectInput,
5
+ EmergencyRedirectItem,
6
+ EmergencyRedirectsListResponse,
7
+ UpdateDetail,
8
+ UpdatesListResponse,
9
+ } from "./admin-types";
10
+
11
+ async function parseJson<T>(response: Response): Promise<T> {
12
+ const data = await response.json().catch(() => ({}));
13
+ if (!response.ok) {
14
+ const message = (data as { error?: string })?.error || "Request failed";
15
+ throw new Error(message);
16
+ }
17
+ return data as T;
18
+ }
19
+
20
+ export async function adminLogin(username: string, password: string) {
21
+ const response = await fetch("/api/admin/login", {
22
+ method: "POST",
23
+ headers: { "Content-Type": "application/json" },
24
+ body: JSON.stringify({ username, password }),
25
+ });
26
+
27
+ return parseJson<{ ok: boolean }>(response);
28
+ }
29
+
30
+ export async function adminLogout() {
31
+ const response = await fetch("/api/admin/logout", { method: "POST" });
32
+ return parseJson<{ ok: boolean }>(response);
33
+ }
34
+
35
+ export async function getAdminSession() {
36
+ const response = await fetch("/api/admin/session", { cache: "no-store" });
37
+ return parseJson<AdminSessionResponse>(response);
38
+ }
39
+
40
+ export async function getUpdates(params: {
41
+ page: number;
42
+ pageSize: number;
43
+ sortBy: string;
44
+ order: "asc" | "desc";
45
+ }) {
46
+ const query = new URLSearchParams({
47
+ page: String(params.page),
48
+ pageSize: String(params.pageSize),
49
+ sortBy: params.sortBy,
50
+ order: params.order,
51
+ });
52
+
53
+ const response = await fetch(`/api/admin/updates?${query.toString()}`, {
54
+ cache: "no-store",
55
+ });
56
+
57
+ return parseJson<UpdatesListResponse>(response);
58
+ }
59
+
60
+ export async function getUpdate(encodedKey: string) {
61
+ const response = await fetch(`/api/admin/updates/${encodedKey}`, {
62
+ cache: "no-store",
63
+ });
64
+
65
+ return parseJson<UpdateDetail>(response);
66
+ }
67
+
68
+ export async function deleteUpdate(encodedKey: string) {
69
+ const response = await fetch(`/api/admin/updates/${encodedKey}`, {
70
+ method: "DELETE",
71
+ });
72
+
73
+ return parseJson<{ ok: boolean; removedFiles: number }>(response);
74
+ }
75
+
76
+ export async function updateJsonFile(
77
+ encodedKey: string,
78
+ fileName:
79
+ | "update-info.json"
80
+ | "update-meta.json"
81
+ | "metadata.json"
82
+ | "channel-latest.json",
83
+ content: Record<string, unknown>,
84
+ ) {
85
+ const response = await fetch(`/api/admin/updates/${encodedKey}`, {
86
+ method: "PATCH",
87
+ headers: { "Content-Type": "application/json" },
88
+ body: JSON.stringify({ fileName, content }),
89
+ });
90
+
91
+ return parseJson<{ ok: boolean; path: string }>(response);
92
+ }
93
+
94
+ export async function rollbackUpdate(encodedKey: string) {
95
+ const response = await fetch(`/api/admin/updates/${encodedKey}/rollback`, {
96
+ method: "POST",
97
+ });
98
+
99
+ return parseJson<{ ok: boolean }>(response);
100
+ }
101
+
102
+ export async function promoteUpdate(encodedKey: string) {
103
+ const response = await fetch(`/api/admin/updates/${encodedKey}/promote`, {
104
+ method: "POST",
105
+ });
106
+
107
+ return parseJson<{ ok: boolean }>(response);
108
+ }
109
+
110
+ export async function setUpdateDisabled(encodedKey: string, disabled: boolean) {
111
+ const response = await fetch(`/api/admin/updates/${encodedKey}/deactivate`, {
112
+ method: "POST",
113
+ headers: { "Content-Type": "application/json" },
114
+ body: JSON.stringify({ disabled }),
115
+ });
116
+
117
+ return parseJson<{ ok: boolean; fallbackApplied?: boolean }>(response);
118
+ }
119
+
120
+ export async function getEmbeddedUpdates() {
121
+ const response = await fetch("/api/admin/embedded-updates", {
122
+ cache: "no-store",
123
+ });
124
+
125
+ return parseJson<EmbeddedUpdatesListResponse>(response);
126
+ }
127
+
128
+ export async function deleteEmbeddedUpdate(embeddedUpdateId: string) {
129
+ const response = await fetch(
130
+ `/api/admin/embedded-updates/${embeddedUpdateId}`,
131
+ {
132
+ method: "DELETE",
133
+ },
134
+ );
135
+
136
+ return parseJson<{ ok: boolean; deletedId: string }>(response);
137
+ }
138
+
139
+ export async function getEmergencyRedirects() {
140
+ const response = await fetch("/api/admin/emergency-redirects", {
141
+ cache: "no-store",
142
+ });
143
+ return parseJson<EmergencyRedirectsListResponse>(response);
144
+ }
145
+
146
+ export async function createEmergencyRedirect(
147
+ input: EmergencyRedirectInput,
148
+ ) {
149
+ const response = await fetch("/api/admin/emergency-redirects", {
150
+ method: "POST",
151
+ headers: { "Content-Type": "application/json" },
152
+ body: JSON.stringify(input),
153
+ });
154
+ return parseJson<EmergencyRedirectItem>(response);
155
+ }
156
+
157
+ export async function updateEmergencyRedirect(
158
+ id: string,
159
+ patch: Partial<EmergencyRedirectInput>,
160
+ ) {
161
+ const response = await fetch(`/api/admin/emergency-redirects/${id}`, {
162
+ method: "PATCH",
163
+ headers: { "Content-Type": "application/json" },
164
+ body: JSON.stringify(patch),
165
+ });
166
+ return parseJson<EmergencyRedirectItem>(response);
167
+ }
168
+
169
+ export async function deleteEmergencyRedirect(id: string) {
170
+ const response = await fetch(`/api/admin/emergency-redirects/${id}`, {
171
+ method: "DELETE",
172
+ });
173
+ return parseJson<{ ok: boolean }>(response);
174
+ }
@@ -0,0 +1,93 @@
1
+ export type UpdateItem = {
2
+ key: string;
3
+ encodedKey: string;
4
+ comment: string | null;
5
+ runtimeVersion: string;
6
+ platform: "ios" | "android";
7
+ createdAtPath: string;
8
+ channel: string;
9
+ stage: string;
10
+ updateId: string;
11
+ createdAt: string;
12
+ status: "active" | "disabled";
13
+ isLatest: boolean;
14
+ isRollbackActive: boolean;
15
+ autoDeliveryEnabled: boolean;
16
+ isIgnoredByRollback: boolean;
17
+ assetsCount: number;
18
+ };
19
+
20
+ export type UpdatesListResponse = {
21
+ items: UpdateItem[];
22
+ total: number;
23
+ page: number;
24
+ pageSize: number;
25
+ totalPages: number;
26
+ rollbackLockByScope: Array<{
27
+ scopeKey: string;
28
+ runtimeVersion: string;
29
+ channel: string;
30
+ platform: "ios" | "android";
31
+ rollbackActive: boolean;
32
+ autoDeliveryEnabled: boolean;
33
+ activeUpdateId: string | null;
34
+ latestUpdateId: string | null;
35
+ }>;
36
+ };
37
+
38
+ export type UpdateDetail = UpdateItem & {
39
+ updateInfo: Record<string, unknown>;
40
+ updateMeta: {
41
+ isActive: boolean;
42
+ updatedAt: string | null;
43
+ disabledAt: string | null;
44
+ };
45
+ metadata: Record<string, unknown> | null;
46
+ channelLatest: Record<string, unknown> | null;
47
+ };
48
+
49
+ export type EmbeddedUpdateItem = {
50
+ embeddedUpdateId: string;
51
+ createdAt: string;
52
+ channel: string;
53
+ platform: "ios" | "android";
54
+ isEmbedded: boolean;
55
+ insertedAt: string;
56
+ modifiedAt: string;
57
+ };
58
+
59
+ export type EmbeddedUpdatesListResponse = {
60
+ items: EmbeddedUpdateItem[];
61
+ };
62
+
63
+ export type AdminRole = "admin" | "operator" | "viewer";
64
+
65
+ export type AdminSessionResponse = {
66
+ authenticated: true;
67
+ username: string;
68
+ role: AdminRole;
69
+ };
70
+
71
+ export type EmergencyRedirectItem = {
72
+ id: string;
73
+ name: string;
74
+ enabled: boolean;
75
+ embeddedUpdateId: string;
76
+ runtimeVersion: string;
77
+ platform: "ios" | "android";
78
+ fromChannel: string;
79
+ toChannel: string;
80
+ targetMode: "pinned" | "follow";
81
+ expectedUpdateId: string | null;
82
+ createdAt: string;
83
+ modifiedAt: string;
84
+ };
85
+
86
+ export type EmergencyRedirectInput = Omit<
87
+ EmergencyRedirectItem,
88
+ "id" | "createdAt" | "modifiedAt"
89
+ >;
90
+
91
+ export type EmergencyRedirectsListResponse = {
92
+ items: EmergencyRedirectItem[];
93
+ };
@@ -0,0 +1,31 @@
1
+ "use client";
2
+
3
+ import { adminLogout } from "@/src/client/admin-api";
4
+ import { useRouter } from "next/navigation";
5
+ import { useState } from "react";
6
+
7
+ export default function LogoutButton() {
8
+ const [isLoading, setIsLoading] = useState(false);
9
+ const router = useRouter();
10
+
11
+ const handleLogout = async () => {
12
+ setIsLoading(true);
13
+ try {
14
+ await adminLogout();
15
+ router.replace("/login");
16
+ } finally {
17
+ setIsLoading(false);
18
+ }
19
+ };
20
+
21
+ return (
22
+ <button
23
+ type="button"
24
+ onClick={handleLogout}
25
+ disabled={isLoading}
26
+ className="rounded-md border border-zinc-300 px-4 py-4 text-sm hover:bg-zinc-100 disabled:cursor-not-allowed disabled:opacity-60 dark:border-zinc-700 dark:hover:bg-zinc-900"
27
+ >
28
+ {isLoading ? "Выход..." : "Выйти"}
29
+ </button>
30
+ );
31
+ }
@@ -0,0 +1,216 @@
1
+ import path from "path";
2
+ import { supabase, SUPABASE_BUCKET } from "../lib/supabase.js";
3
+ import { toPosixPath } from "../lib/manifest-helpers.js";
4
+ import { Buffer } from "buffer";
5
+ import { NextRequest } from "next/server.js";
6
+
7
+ type OtaUpdateRow = {
8
+ update_id: string;
9
+ runtime_version: string;
10
+ channel: string;
11
+ platform: "ios" | "android";
12
+ storage_bucket: string;
13
+ storage_base_path: string;
14
+ manifest: Record<string, unknown>;
15
+ };
16
+
17
+ function buildAssetRequestContext({
18
+ runtimeVersion,
19
+ platform,
20
+ updateId,
21
+ channel,
22
+ }) {
23
+ return `channel=${channel || "unknown"} runtime=${runtimeVersion || "unknown"} platform=${platform || "unknown"} updateId=${updateId || "unknown"}`;
24
+ }
25
+
26
+ function normalizeContentType(
27
+ contentType: string | undefined,
28
+ isLaunchAsset: boolean,
29
+ ) {
30
+ if (isLaunchAsset) {
31
+ return "application/javascript";
32
+ }
33
+
34
+ if (contentType && contentType !== "application/octet-stream") {
35
+ return contentType;
36
+ }
37
+
38
+ return contentType || "application/octet-stream";
39
+ }
40
+
41
+ async function getUpdateById(updateId: string): Promise<OtaUpdateRow | null> {
42
+ const { data, error } = await supabase
43
+ .from("ota_updates")
44
+ .select("*")
45
+ .eq("update_id", updateId)
46
+ .maybeSingle();
47
+
48
+ if (error) {
49
+ throw new Error(`Failed to load update row: ${error.message}`);
50
+ }
51
+
52
+ return (data as OtaUpdateRow | null) || null;
53
+ }
54
+
55
+ const assetsController = async (req: NextRequest) => {
56
+ if (req.method !== "GET") {
57
+ return Response.json({ error: "Method not allowed." }, { status: 405 });
58
+ }
59
+
60
+ const searchParams = req.nextUrl.searchParams;
61
+
62
+ const runtimeVersion = searchParams.get("runtimeVersion");
63
+ const platform = searchParams.get("platform");
64
+ const channel = searchParams.get("channel");
65
+ const updateId = searchParams.get("updateId") || searchParams.get("buildId");
66
+ const assetPath = searchParams.get("assetPath");
67
+
68
+ if (platform !== "ios" && platform !== "android") {
69
+ return Response.json(
70
+ {
71
+ error: 'No platform provided. Expected "ios" or "android".',
72
+ },
73
+ { status: 400 },
74
+ );
75
+ }
76
+
77
+ if (!runtimeVersion || typeof runtimeVersion !== "string") {
78
+ return Response.json(
79
+ { error: "No runtimeVersion provided." },
80
+ { status: 400 },
81
+ );
82
+ }
83
+
84
+ if (!updateId || typeof updateId !== "string") {
85
+ return Response.json({ error: "No updateId provided." }, { status: 400 });
86
+ }
87
+
88
+ if (!assetPath || typeof assetPath !== "string") {
89
+ return Response.json({ error: "No assetPath provided." }, { status: 400 });
90
+ }
91
+
92
+ try {
93
+ const updateRow = await getUpdateById(updateId);
94
+ if (!updateRow) {
95
+ return Response.json(
96
+ { error: `Update not found: ${updateId}` },
97
+ { status: 404 },
98
+ );
99
+ }
100
+
101
+ if (
102
+ updateRow.platform !== platform ||
103
+ updateRow.runtime_version !== runtimeVersion ||
104
+ (channel && updateRow.channel !== channel)
105
+ ) {
106
+ return Response.json(
107
+ { error: "Update does not match requested runtime/platform/channel" },
108
+ { status: 404 },
109
+ );
110
+ }
111
+
112
+ const requestContext = buildAssetRequestContext({
113
+ runtimeVersion,
114
+ platform,
115
+ updateId,
116
+ channel: updateRow.channel,
117
+ });
118
+
119
+ const manifest =
120
+ updateRow.manifest &&
121
+ typeof updateRow.manifest === "object" &&
122
+ !Array.isArray(updateRow.manifest)
123
+ ? updateRow.manifest
124
+ : {};
125
+
126
+ const normalizedAssetPath = toPosixPath(assetPath);
127
+ const launchAsset =
128
+ manifest.launchAsset && typeof manifest.launchAsset === "object"
129
+ ? (manifest.launchAsset as Record<string, unknown>)
130
+ : null;
131
+ const launchAssetPath = toPosixPath(String(launchAsset?.path || ""));
132
+
133
+ const assets = Array.isArray(manifest.assets)
134
+ ? (manifest.assets as Array<Record<string, unknown>>)
135
+ : [];
136
+
137
+ const isLaunchAsset = normalizedAssetPath === launchAssetPath;
138
+ const assetMetadata = isLaunchAsset
139
+ ? launchAsset
140
+ : assets.find(
141
+ (asset) =>
142
+ toPosixPath(String(asset.path || "")) === normalizedAssetPath,
143
+ ) || null;
144
+
145
+ if (!assetMetadata) {
146
+ return Response.json(
147
+ {
148
+ error: `Asset metadata not found for path: ${normalizedAssetPath}`,
149
+ },
150
+ { status: 404 },
151
+ );
152
+ }
153
+
154
+ const storagePath = toPosixPath(
155
+ path.posix.join(updateRow.storage_base_path, normalizedAssetPath),
156
+ );
157
+ const storageBucket = updateRow.storage_bucket || SUPABASE_BUCKET;
158
+
159
+ console.log(
160
+ `[${new Date().toLocaleTimeString()}] 📥 /api/assets ${requestContext} assetPath=${normalizedAssetPath} launch=${isLaunchAsset}`,
161
+ );
162
+
163
+ const { data, error } = await supabase.storage
164
+ .from(storageBucket)
165
+ .download(storagePath);
166
+
167
+ if (error || !data) {
168
+ return Response.json(
169
+ {
170
+ error: `Asset not found: ${storagePath}`,
171
+ details: error?.message,
172
+ },
173
+ { status: 404 },
174
+ );
175
+ }
176
+
177
+ const contentType = normalizeContentType(
178
+ typeof assetMetadata.contentType === "string"
179
+ ? assetMetadata.contentType
180
+ : undefined,
181
+ isLaunchAsset,
182
+ );
183
+
184
+ const arrayBuffer = await data.arrayBuffer();
185
+ const buffer = Buffer.from(arrayBuffer);
186
+
187
+ console.log(
188
+ `[${new Date().toLocaleTimeString()}] 📦 Asset sent ${requestContext}`,
189
+ JSON.stringify(
190
+ {
191
+ assetPath: normalizedAssetPath,
192
+ contentType,
193
+ bytes: buffer.length,
194
+ },
195
+ null,
196
+ 2,
197
+ ),
198
+ );
199
+
200
+ return new Response(new Uint8Array(buffer), {
201
+ status: 200,
202
+ headers: {
203
+ "Content-Type": contentType,
204
+ "Content-Length": buffer.length.toString(),
205
+ },
206
+ });
207
+ } catch (error: unknown) {
208
+ console.error(
209
+ `[${new Date().toLocaleTimeString()}] ❌ Asset error runtime=${runtimeVersion || "unknown"} platform=${platform || "unknown"} updateId=${updateId || "unknown"}:`,
210
+ error,
211
+ );
212
+ return Response.json({ error: (error as Error).message }, { status: 500 });
213
+ }
214
+ };
215
+
216
+ export default assetsController;