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.
- package/LICENSE +21 -0
- package/README.md +19 -0
- package/bin/create-expo-updates-server.mjs +5 -0
- package/dist/template/.env.docker.example +15 -0
- package/dist/template/.env.example +35 -0
- package/dist/template/Dockerfile +42 -0
- package/dist/template/LICENSE +21 -0
- package/dist/template/README.md +665 -0
- package/dist/template/app/api/admin/access-tokens/[id]/route.ts +25 -0
- package/dist/template/app/api/admin/access-tokens/route.test.ts +73 -0
- package/dist/template/app/api/admin/access-tokens/route.ts +90 -0
- package/dist/template/app/api/admin/embedded-updates/[embeddedUpdateId]/route.ts +25 -0
- package/dist/template/app/api/admin/embedded-updates/route.test.ts +102 -0
- package/dist/template/app/api/admin/embedded-updates/route.ts +47 -0
- package/dist/template/app/api/admin/emergency-redirects/[id]/route.ts +62 -0
- package/dist/template/app/api/admin/emergency-redirects/route.test.ts +84 -0
- package/dist/template/app/api/admin/emergency-redirects/route.ts +56 -0
- package/dist/template/app/api/admin/login/route.test.ts +55 -0
- package/dist/template/app/api/admin/login/route.ts +52 -0
- package/dist/template/app/api/admin/logout/route.ts +20 -0
- package/dist/template/app/api/admin/session/route.ts +15 -0
- package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +33 -0
- package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +29 -0
- package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +29 -0
- package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +96 -0
- package/dist/template/app/api/admin/updates/route.test.ts +142 -0
- package/dist/template/app/api/admin/updates/route.ts +32 -0
- package/dist/template/app/api/assets/route.ts +6 -0
- package/dist/template/app/api/health/route.test.ts +26 -0
- package/dist/template/app/api/health/route.ts +42 -0
- package/dist/template/app/api/manifest/route.ts +6 -0
- package/dist/template/app/api/update-complete/route.ts +6 -0
- package/dist/template/app/favicon.ico +0 -0
- package/dist/template/app/globals.css +26 -0
- package/dist/template/app/layout.tsx +34 -0
- package/dist/template/app/login/page.tsx +77 -0
- package/dist/template/app/page.tsx +12 -0
- package/dist/template/app/updates/[encodedKey]/page.tsx +482 -0
- package/dist/template/app/updates/embedded/page.tsx +164 -0
- package/dist/template/app/updates/emergency-redirects/page.tsx +430 -0
- package/dist/template/app/updates/layout.tsx +14 -0
- package/dist/template/app/updates/page.tsx +281 -0
- package/dist/template/docker-compose.yml +100 -0
- package/dist/template/docs/expo-app-setup.md +497 -0
- package/dist/template/docs/getting-started.md +502 -0
- package/dist/template/docs/getting-started.ru.md +623 -0
- package/dist/template/docs/index.md +57 -0
- package/dist/template/docs/migrations/schema.sql +363 -0
- package/dist/template/docs/npm-installer.md +83 -0
- package/dist/template/docs/operations-guide.md +270 -0
- package/dist/template/docs/ota-emergency-recovery.md +109 -0
- package/dist/template/docs/supabase-setup.md +133 -0
- package/dist/template/eslint.config.mjs +30 -0
- package/dist/template/gitignore.template +58 -0
- package/dist/template/next.config.ts +7 -0
- package/dist/template/package-lock.json +12156 -0
- package/dist/template/package.json +50 -0
- package/dist/template/postcss.config.mjs +7 -0
- package/dist/template/public/file.svg +1 -0
- package/dist/template/public/globe.svg +1 -0
- package/dist/template/public/next.svg +1 -0
- package/dist/template/public/vercel.svg +1 -0
- package/dist/template/public/window.svg +1 -0
- package/dist/template/scripts/configure-expo-app.mjs +145 -0
- package/dist/template/scripts/create-access-token.mjs +135 -0
- package/dist/template/scripts/create-admin.mjs +169 -0
- package/dist/template/scripts/import-emergency-redirects.mjs +126 -0
- package/dist/template/scripts/lib/embedded-update-registrar.test.ts +64 -0
- package/dist/template/scripts/lib/expo-app-configurator.mjs +666 -0
- package/dist/template/scripts/lib/expo-app-configurator.test.ts +463 -0
- package/dist/template/scripts/lib/expo-app-prompts.mjs +139 -0
- package/dist/template/scripts/lib/expo-app-prompts.test.ts +97 -0
- package/dist/template/scripts/lib/expo-export-command.test.ts +31 -0
- package/dist/template/scripts/lib/ota-publish-retry.test.ts +33 -0
- package/dist/template/scripts/manage-emergency-redirect.mjs +483 -0
- package/dist/template/scripts/sync-docker-database-password.sh +16 -0
- package/dist/template/src/client/admin-api.ts +174 -0
- package/dist/template/src/client/admin-types.ts +93 -0
- package/dist/template/src/client/logout-button.tsx +31 -0
- package/dist/template/src/server/controllers/assets.ts +216 -0
- package/dist/template/src/server/controllers/manifest.test.ts +403 -0
- package/dist/template/src/server/controllers/manifest.ts +626 -0
- package/dist/template/src/server/controllers/update-complete.ts +36 -0
- package/dist/template/src/server/lib/admin-access-tokens.ts +169 -0
- package/dist/template/src/server/lib/admin-api.ts +101 -0
- package/dist/template/src/server/lib/admin-auth.ts +208 -0
- package/dist/template/src/server/lib/admin-embedded-updates.ts +150 -0
- package/dist/template/src/server/lib/admin-updates.ts +1282 -0
- package/dist/template/src/server/lib/common.js +24 -0
- package/dist/template/src/server/lib/emergency-channel-redirects.ts +310 -0
- package/dist/template/src/server/lib/env.js +8 -0
- package/dist/template/src/server/lib/manifest-helpers.js +91 -0
- package/dist/template/src/server/lib/response.js +11 -0
- package/dist/template/src/server/lib/signing.js +55 -0
- package/dist/template/src/server/lib/supabase.js +763 -0
- package/dist/template/src/server/manifest/manifest-repository.ts +269 -0
- package/dist/template/src/server/manifest/manifest-request.ts +21 -0
- package/dist/template/src/server/manifest/manifest-response.ts +235 -0
- package/dist/template/src/server/manifest/manifest-values.ts +62 -0
- package/dist/template/templates/expo-app/.env.ota.example +18 -0
- package/dist/template/templates/expo-app/ci_scripts/ci_post_xcodebuild.sh +6 -0
- package/dist/template/templates/expo-app/ci_scripts/register-embedded-update.sh +36 -0
- package/dist/template/templates/expo-app/plugins/with-ota-embedded-registration.js +33 -0
- package/dist/template/templates/expo-app/scripts/ota-publish/api/database-client.mjs +87 -0
- package/dist/template/templates/expo-app/scripts/ota-publish/api/storage-client.mjs +57 -0
- package/dist/template/templates/expo-app/scripts/ota-publish/index.mjs +182 -0
- package/dist/template/templates/expo-app/scripts/ota-publish/lib/common.mjs +84 -0
- package/dist/template/templates/expo-app/scripts/ota-publish/lib/expo-export-command.mjs +24 -0
- package/dist/template/templates/expo-app/scripts/ota-publish/lib/retry.mjs +25 -0
- package/dist/template/templates/expo-app/scripts/ota-publish/shared/config.mjs +48 -0
- package/dist/template/templates/expo-app/scripts/ota-register-embedded/android-register-embedded-update.sh +18 -0
- package/dist/template/templates/expo-app/scripts/ota-register-embedded/index.mjs +275 -0
- package/dist/template/tsconfig.json +36 -0
- package/dist/template/vitest.config.mts +20 -0
- package/dist/template-manifest.json +564 -0
- package/lib/cli.mjs +549 -0
- package/package.json +41 -0
|
@@ -0,0 +1,1282 @@
|
|
|
1
|
+
import { supabase, SUPABASE_BUCKET } from "./supabase.js";
|
|
2
|
+
import { toPosixPath } from "./manifest-helpers.js";
|
|
3
|
+
|
|
4
|
+
let hasServedManifestLogTable: boolean | null = null;
|
|
5
|
+
|
|
6
|
+
function isMissingServedManifestLogTableError(error: unknown): boolean {
|
|
7
|
+
if (!error || typeof error !== "object") {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const message =
|
|
12
|
+
"message" in error && typeof error.message === "string"
|
|
13
|
+
? error.message.toLowerCase()
|
|
14
|
+
: "";
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
message.includes("ota_served_manifest_log") &&
|
|
18
|
+
message.includes("does not exist")
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Records served_manifest_id → update_id mapping so manifest controller can
|
|
24
|
+
* resolve what real update a device is running from its currentUpdateId.
|
|
25
|
+
* Fire-and-forget: errors are logged but never thrown.
|
|
26
|
+
*/
|
|
27
|
+
function logServedManifestMapping({
|
|
28
|
+
servedManifestId,
|
|
29
|
+
updateId,
|
|
30
|
+
runtimeVersion,
|
|
31
|
+
channel,
|
|
32
|
+
platform,
|
|
33
|
+
reason,
|
|
34
|
+
}: {
|
|
35
|
+
servedManifestId: string;
|
|
36
|
+
updateId: string;
|
|
37
|
+
runtimeVersion: string;
|
|
38
|
+
channel: string;
|
|
39
|
+
platform: string;
|
|
40
|
+
reason: string;
|
|
41
|
+
}): void {
|
|
42
|
+
if (hasServedManifestLogTable === false) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
supabase
|
|
47
|
+
.from("ota_served_manifest_log")
|
|
48
|
+
.upsert(
|
|
49
|
+
{
|
|
50
|
+
served_manifest_id: servedManifestId,
|
|
51
|
+
update_id: updateId,
|
|
52
|
+
runtime_version: runtimeVersion,
|
|
53
|
+
channel,
|
|
54
|
+
platform,
|
|
55
|
+
reason,
|
|
56
|
+
},
|
|
57
|
+
{ onConflict: "served_manifest_id" },
|
|
58
|
+
)
|
|
59
|
+
.then(({ error }) => {
|
|
60
|
+
if (isMissingServedManifestLogTableError(error)) {
|
|
61
|
+
hasServedManifestLogTable = false;
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!error && hasServedManifestLogTable === null) {
|
|
66
|
+
hasServedManifestLogTable = true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (
|
|
70
|
+
error &&
|
|
71
|
+
!error.message?.includes("duplicate") &&
|
|
72
|
+
!error.message?.includes("unique")
|
|
73
|
+
) {
|
|
74
|
+
console.warn(
|
|
75
|
+
`[admin-updates] Failed to log served manifest mapping: ${error.message}`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
.catch(() => {
|
|
80
|
+
/* ignore */
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
type UpdateStatus = "active" | "disabled";
|
|
85
|
+
|
|
86
|
+
type UpdateMetaState = {
|
|
87
|
+
isActive: boolean;
|
|
88
|
+
updatedAt: string | null;
|
|
89
|
+
disabledAt: string | null;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
type OtaUpdateRow = {
|
|
93
|
+
id: string;
|
|
94
|
+
update_id: string;
|
|
95
|
+
build_id: string;
|
|
96
|
+
comment: string | null;
|
|
97
|
+
runtime_version: string;
|
|
98
|
+
channel: string;
|
|
99
|
+
platform: "ios" | "android";
|
|
100
|
+
created_at: string;
|
|
101
|
+
created_at_path: string;
|
|
102
|
+
storage_bucket: string;
|
|
103
|
+
storage_base_path: string;
|
|
104
|
+
is_active: boolean;
|
|
105
|
+
updated_at: string | null;
|
|
106
|
+
disabled_at: string | null;
|
|
107
|
+
assets_count: number;
|
|
108
|
+
launch_asset_path: string | null;
|
|
109
|
+
rolled_back_from_update_id: string | null;
|
|
110
|
+
manifest: Record<string, unknown>;
|
|
111
|
+
inserted_at: string;
|
|
112
|
+
modified_at: string;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
type OtaChannelRow = {
|
|
116
|
+
id: number;
|
|
117
|
+
runtime_version: string;
|
|
118
|
+
channel: string;
|
|
119
|
+
platform: "ios" | "android";
|
|
120
|
+
latest_update_id: string | null;
|
|
121
|
+
latest_created_at: string | null;
|
|
122
|
+
latest_created_at_path: string | null;
|
|
123
|
+
active_update_id: string | null;
|
|
124
|
+
active_changed_at: string | null;
|
|
125
|
+
served_manifest_id: string;
|
|
126
|
+
served_manifest_changed_at: string;
|
|
127
|
+
inserted_at: string;
|
|
128
|
+
modified_at: string;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export type UpdateRecord = {
|
|
132
|
+
key: string;
|
|
133
|
+
encodedKey: string;
|
|
134
|
+
comment: string | null;
|
|
135
|
+
runtimeVersion: string;
|
|
136
|
+
platform: "ios" | "android";
|
|
137
|
+
createdAtPath: string;
|
|
138
|
+
channel: string;
|
|
139
|
+
stage: string;
|
|
140
|
+
updateId: string;
|
|
141
|
+
prefix: string;
|
|
142
|
+
updateInfoPath: string;
|
|
143
|
+
updateMetaPath: string;
|
|
144
|
+
metadataPath: string;
|
|
145
|
+
channelLatestPath: string;
|
|
146
|
+
createdAt: string | null;
|
|
147
|
+
status: UpdateStatus;
|
|
148
|
+
isLatest: boolean;
|
|
149
|
+
isRollbackActive: boolean;
|
|
150
|
+
autoDeliveryEnabled: boolean;
|
|
151
|
+
isIgnoredByRollback: boolean;
|
|
152
|
+
launchAssetPath: string | null;
|
|
153
|
+
assetsCount: number;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export type UpdateDetail = UpdateRecord & {
|
|
157
|
+
updateInfo: Record<string, unknown>;
|
|
158
|
+
updateMeta: UpdateMetaState;
|
|
159
|
+
metadata: Record<string, unknown> | null;
|
|
160
|
+
channelLatest: Record<string, unknown> | null;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
type SortField =
|
|
164
|
+
| "createdAt"
|
|
165
|
+
| "runtimeVersion"
|
|
166
|
+
| "platform"
|
|
167
|
+
| "channel"
|
|
168
|
+
| "stage"
|
|
169
|
+
| "status"
|
|
170
|
+
| "updateId";
|
|
171
|
+
|
|
172
|
+
type SortOrder = "asc" | "desc";
|
|
173
|
+
|
|
174
|
+
type ListOptions = {
|
|
175
|
+
page: number;
|
|
176
|
+
pageSize: number;
|
|
177
|
+
sortBy: SortField;
|
|
178
|
+
order: SortOrder;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
type Scope = {
|
|
182
|
+
runtimeVersion: string;
|
|
183
|
+
channel: string;
|
|
184
|
+
platform: "ios" | "android";
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
type RollbackOptions = {
|
|
188
|
+
reason?: string;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
function parseDate(value: string | null): number {
|
|
192
|
+
if (!value) {
|
|
193
|
+
return 0;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const parsed = Date.parse(value);
|
|
197
|
+
return Number.isNaN(parsed) ? 0 : parsed;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function ensureUuid(value: string): boolean {
|
|
201
|
+
return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(
|
|
202
|
+
value,
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function extractUpdateIdFromKey(key: string): string {
|
|
207
|
+
if (ensureUuid(key)) {
|
|
208
|
+
return key;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const tail = key.split("/").filter(Boolean).at(-1) || "";
|
|
212
|
+
if (!ensureUuid(tail)) {
|
|
213
|
+
throw new Error("Invalid update key format.");
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return tail;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function makeUpdateKey(row: OtaUpdateRow): string {
|
|
220
|
+
return `${row.runtime_version}/${row.platform}/${row.channel}/${row.update_id}`;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function mapUpdateMeta(row: OtaUpdateRow): UpdateMetaState {
|
|
224
|
+
return {
|
|
225
|
+
isActive: row.is_active,
|
|
226
|
+
updatedAt: row.updated_at,
|
|
227
|
+
disabledAt: row.disabled_at,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function channelLatestToJson(
|
|
232
|
+
channel: OtaChannelRow | null,
|
|
233
|
+
newestUpdateId?: string | null,
|
|
234
|
+
newestCreatedAt?: string | null,
|
|
235
|
+
): Record<string, unknown> | null {
|
|
236
|
+
if (!channel) {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return {
|
|
241
|
+
runtimeVersion: channel.runtime_version,
|
|
242
|
+
channel: channel.channel,
|
|
243
|
+
platform: channel.platform,
|
|
244
|
+
latestUpdateId: channel.latest_update_id,
|
|
245
|
+
latestCreatedAt: channel.latest_created_at,
|
|
246
|
+
latestCreatedAtPath: channel.latest_created_at_path,
|
|
247
|
+
activeUpdateId: channel.active_update_id,
|
|
248
|
+
activeChangedAt: channel.active_changed_at,
|
|
249
|
+
newestUpdateId: newestUpdateId || null,
|
|
250
|
+
newestCreatedAt: newestCreatedAt || null,
|
|
251
|
+
servedManifestId: channel.served_manifest_id,
|
|
252
|
+
servedManifestChangedAt: channel.served_manifest_changed_at,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
async function getNewestUpdateByScope(
|
|
257
|
+
scope: Scope,
|
|
258
|
+
): Promise<OtaUpdateRow | null> {
|
|
259
|
+
const { data, error } = await supabase
|
|
260
|
+
.from("ota_updates")
|
|
261
|
+
.select("*")
|
|
262
|
+
.eq("runtime_version", scope.runtimeVersion)
|
|
263
|
+
.eq("channel", scope.channel)
|
|
264
|
+
.eq("platform", scope.platform)
|
|
265
|
+
.order("created_at", { ascending: false })
|
|
266
|
+
.limit(1)
|
|
267
|
+
.maybeSingle();
|
|
268
|
+
|
|
269
|
+
if (error) {
|
|
270
|
+
throw new Error(`Failed to load newest update by scope: ${error.message}`);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return (data as OtaUpdateRow | null) || null;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function mapRowToRecord(
|
|
277
|
+
row: OtaUpdateRow,
|
|
278
|
+
channel: OtaChannelRow | null,
|
|
279
|
+
): UpdateRecord {
|
|
280
|
+
const key = makeUpdateKey(row);
|
|
281
|
+
const isLatest = channel?.latest_update_id === row.update_id;
|
|
282
|
+
const rollbackActiveAtScope =
|
|
283
|
+
Boolean(channel?.active_update_id) &&
|
|
284
|
+
channel?.active_update_id !== channel?.latest_update_id;
|
|
285
|
+
const isRollbackActive =
|
|
286
|
+
rollbackActiveAtScope && channel?.active_update_id === row.update_id;
|
|
287
|
+
const isIgnoredByRollback =
|
|
288
|
+
rollbackActiveAtScope && channel?.active_update_id !== row.update_id;
|
|
289
|
+
|
|
290
|
+
const status: UpdateStatus = row.is_active ? "active" : "disabled";
|
|
291
|
+
|
|
292
|
+
return {
|
|
293
|
+
key,
|
|
294
|
+
encodedKey: encodeUpdateKey(row.update_id),
|
|
295
|
+
comment: row.comment,
|
|
296
|
+
runtimeVersion: row.runtime_version,
|
|
297
|
+
platform: row.platform,
|
|
298
|
+
createdAtPath: row.created_at_path,
|
|
299
|
+
channel: row.channel,
|
|
300
|
+
stage: "-",
|
|
301
|
+
updateId: row.update_id,
|
|
302
|
+
prefix: row.storage_base_path,
|
|
303
|
+
updateInfoPath: `${row.storage_base_path}/update-info.json`,
|
|
304
|
+
updateMetaPath: `${row.storage_base_path}/update-meta.json`,
|
|
305
|
+
metadataPath: `${row.storage_base_path}/metadata.json`,
|
|
306
|
+
channelLatestPath: `${row.runtime_version}/${row.platform}/channels/${row.channel}/latest.json`,
|
|
307
|
+
createdAt: row.created_at,
|
|
308
|
+
status,
|
|
309
|
+
isLatest,
|
|
310
|
+
isRollbackActive,
|
|
311
|
+
autoDeliveryEnabled: !rollbackActiveAtScope,
|
|
312
|
+
isIgnoredByRollback,
|
|
313
|
+
launchAssetPath: row.launch_asset_path,
|
|
314
|
+
assetsCount: row.assets_count,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function compareValues(a: string | number, b: string | number): number {
|
|
319
|
+
if (a < b) {
|
|
320
|
+
return -1;
|
|
321
|
+
}
|
|
322
|
+
if (a > b) {
|
|
323
|
+
return 1;
|
|
324
|
+
}
|
|
325
|
+
return 0;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function sortUpdates(
|
|
329
|
+
items: UpdateRecord[],
|
|
330
|
+
sortBy: SortField,
|
|
331
|
+
order: SortOrder,
|
|
332
|
+
): UpdateRecord[] {
|
|
333
|
+
const multiplier = order === "asc" ? 1 : -1;
|
|
334
|
+
|
|
335
|
+
return [...items].sort((a, b) => {
|
|
336
|
+
let valueA: string | number;
|
|
337
|
+
let valueB: string | number;
|
|
338
|
+
|
|
339
|
+
if (sortBy === "createdAt") {
|
|
340
|
+
valueA = parseDate(a.createdAt);
|
|
341
|
+
valueB = parseDate(b.createdAt);
|
|
342
|
+
} else {
|
|
343
|
+
valueA = a[sortBy] as string | number;
|
|
344
|
+
valueB = b[sortBy] as string | number;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const result = compareValues(valueA, valueB) * multiplier;
|
|
348
|
+
if (result !== 0) {
|
|
349
|
+
return result;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return compareValues(a.key, b.key) * multiplier;
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
async function listPageInBucket(
|
|
357
|
+
bucket: string,
|
|
358
|
+
prefix: string,
|
|
359
|
+
offset: number,
|
|
360
|
+
limit = 1000,
|
|
361
|
+
) {
|
|
362
|
+
const { data, error } = await supabase.storage
|
|
363
|
+
.from(bucket)
|
|
364
|
+
.list(prefix, { limit, offset, sortBy: { column: "name", order: "asc" } });
|
|
365
|
+
|
|
366
|
+
if (error) {
|
|
367
|
+
throw new Error(
|
|
368
|
+
`Failed to list ${bucket}:${prefix || "<root>"}: ${error.message}`,
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
return data || [];
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
async function listAllEntriesInBucket(
|
|
376
|
+
bucket: string,
|
|
377
|
+
prefix: string,
|
|
378
|
+
): Promise<Array<{ name: string; id?: string | null }>> {
|
|
379
|
+
const merged: Array<{ name: string; id?: string | null }> = [];
|
|
380
|
+
const limit = 1000;
|
|
381
|
+
let offset = 0;
|
|
382
|
+
|
|
383
|
+
for (;;) {
|
|
384
|
+
const items = await listPageInBucket(bucket, prefix, offset, limit);
|
|
385
|
+
merged.push(...items);
|
|
386
|
+
if (items.length < limit) {
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
offset += limit;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return merged;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function isLikelyFolder(entry: { name: string; id?: string | null }): boolean {
|
|
396
|
+
return entry.id === null || entry.id === undefined;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
async function listAllFilesRecursive(
|
|
400
|
+
bucket: string,
|
|
401
|
+
prefix: string,
|
|
402
|
+
): Promise<string[]> {
|
|
403
|
+
const entries = await listAllEntriesInBucket(bucket, prefix);
|
|
404
|
+
const files: string[] = [];
|
|
405
|
+
|
|
406
|
+
for (const entry of entries) {
|
|
407
|
+
const childPath = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
408
|
+
if (isLikelyFolder(entry)) {
|
|
409
|
+
const nestedFiles = await listAllFilesRecursive(bucket, childPath);
|
|
410
|
+
files.push(...nestedFiles);
|
|
411
|
+
} else {
|
|
412
|
+
files.push(childPath);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
return files;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
async function getChannelRow(scope: Scope): Promise<OtaChannelRow | null> {
|
|
420
|
+
const { data, error } = await supabase
|
|
421
|
+
.from("ota_update_channels")
|
|
422
|
+
.select("*")
|
|
423
|
+
.eq("runtime_version", scope.runtimeVersion)
|
|
424
|
+
.eq("channel", scope.channel)
|
|
425
|
+
.eq("platform", scope.platform)
|
|
426
|
+
.maybeSingle();
|
|
427
|
+
|
|
428
|
+
if (error) {
|
|
429
|
+
throw new Error(`Failed to load ota_update_channels row: ${error.message}`);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return (data as OtaChannelRow | null) || null;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
async function upsertChannelRow(
|
|
436
|
+
scope: Scope,
|
|
437
|
+
payload: Partial<OtaChannelRow>,
|
|
438
|
+
): Promise<OtaChannelRow> {
|
|
439
|
+
const { data, error } = await supabase
|
|
440
|
+
.from("ota_update_channels")
|
|
441
|
+
.upsert(
|
|
442
|
+
{
|
|
443
|
+
runtime_version: scope.runtimeVersion,
|
|
444
|
+
channel: scope.channel,
|
|
445
|
+
platform: scope.platform,
|
|
446
|
+
...payload,
|
|
447
|
+
},
|
|
448
|
+
{
|
|
449
|
+
onConflict: "runtime_version,channel,platform",
|
|
450
|
+
},
|
|
451
|
+
)
|
|
452
|
+
.select("*")
|
|
453
|
+
.single();
|
|
454
|
+
|
|
455
|
+
if (error || !data) {
|
|
456
|
+
throw new Error(
|
|
457
|
+
`Failed to upsert ota_update_channels row: ${error?.message}`,
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
return data as OtaChannelRow;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
async function clearRollbackForOtherRuntimes(scope: Scope) {
|
|
465
|
+
const { error } = await supabase
|
|
466
|
+
.from("ota_update_channels")
|
|
467
|
+
.update({
|
|
468
|
+
active_update_id: null,
|
|
469
|
+
active_changed_at: new Date().toISOString(),
|
|
470
|
+
})
|
|
471
|
+
.eq("channel", scope.channel)
|
|
472
|
+
.eq("platform", scope.platform)
|
|
473
|
+
.neq("runtime_version", scope.runtimeVersion)
|
|
474
|
+
.not("active_update_id", "is", null)
|
|
475
|
+
.execute();
|
|
476
|
+
|
|
477
|
+
if (error) {
|
|
478
|
+
throw new Error(
|
|
479
|
+
`Failed to clear rollback for other runtimes: ${error.message}`,
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
async function getLatestRuntimeForChannelPlatform(params: {
|
|
485
|
+
channel: string;
|
|
486
|
+
platform: "ios" | "android";
|
|
487
|
+
}): Promise<string | null> {
|
|
488
|
+
const { data, error } = await supabase
|
|
489
|
+
.from("ota_update_channels")
|
|
490
|
+
.select("runtime_version, latest_created_at")
|
|
491
|
+
.eq("channel", params.channel)
|
|
492
|
+
.eq("platform", params.platform)
|
|
493
|
+
.not("latest_update_id", "is", null)
|
|
494
|
+
.order("latest_created_at", { ascending: false, nullsFirst: false })
|
|
495
|
+
.limit(1)
|
|
496
|
+
.maybeSingle();
|
|
497
|
+
|
|
498
|
+
if (error) {
|
|
499
|
+
throw new Error(
|
|
500
|
+
`Failed to resolve latest runtime for channel/platform: ${error.message}`,
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
return data?.runtime_version || null;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
async function getUpdateRowById(updateId: string): Promise<OtaUpdateRow> {
|
|
508
|
+
const { data, error } = await supabase
|
|
509
|
+
.from("ota_updates")
|
|
510
|
+
.select("*")
|
|
511
|
+
.eq("update_id", updateId)
|
|
512
|
+
.single();
|
|
513
|
+
|
|
514
|
+
if (error || !data) {
|
|
515
|
+
throw new Error(`Update not found: ${updateId}`);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
return data as OtaUpdateRow;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
async function getUpdateRowsByScope(scope: Scope): Promise<OtaUpdateRow[]> {
|
|
522
|
+
const { data, error } = await supabase
|
|
523
|
+
.from("ota_updates")
|
|
524
|
+
.select("*")
|
|
525
|
+
.eq("runtime_version", scope.runtimeVersion)
|
|
526
|
+
.eq("channel", scope.channel)
|
|
527
|
+
.eq("platform", scope.platform)
|
|
528
|
+
.order("created_at", { ascending: false })
|
|
529
|
+
.execute();
|
|
530
|
+
|
|
531
|
+
if (error) {
|
|
532
|
+
throw new Error(`Failed to load updates by scope: ${error.message}`);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
return (data || []) as OtaUpdateRow[];
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function makeScopeFromRow(row: OtaUpdateRow): Scope {
|
|
539
|
+
return {
|
|
540
|
+
runtimeVersion: row.runtime_version,
|
|
541
|
+
channel: row.channel,
|
|
542
|
+
platform: row.platform,
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
async function recalculateChannelPointers(scope: Scope) {
|
|
547
|
+
const rows = await getUpdateRowsByScope(scope);
|
|
548
|
+
const latestActive = rows.find((row) => row.is_active) || null;
|
|
549
|
+
|
|
550
|
+
const existingChannelRow = await getChannelRow(scope);
|
|
551
|
+
const existingRollbackId = existingChannelRow?.active_update_id || null;
|
|
552
|
+
const hasValidRollback =
|
|
553
|
+
Boolean(existingRollbackId) &&
|
|
554
|
+
existingRollbackId !== latestActive?.update_id &&
|
|
555
|
+
rows.some((row) => row.update_id === existingRollbackId && row.is_active);
|
|
556
|
+
|
|
557
|
+
const nextRollbackId = hasValidRollback ? existingRollbackId : null;
|
|
558
|
+
const now = new Date().toISOString();
|
|
559
|
+
|
|
560
|
+
const payload: Partial<OtaChannelRow> = {
|
|
561
|
+
latest_update_id: latestActive?.update_id || null,
|
|
562
|
+
latest_created_at: latestActive?.created_at || null,
|
|
563
|
+
latest_created_at_path: latestActive?.created_at_path || null,
|
|
564
|
+
active_update_id: nextRollbackId,
|
|
565
|
+
active_changed_at:
|
|
566
|
+
nextRollbackId !== (existingChannelRow?.active_update_id || null)
|
|
567
|
+
? now
|
|
568
|
+
: existingChannelRow?.active_changed_at || now,
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
const channelRow = await upsertChannelRow(scope, payload);
|
|
572
|
+
return { channelRow, latestActive, rollbackId: nextRollbackId };
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
async function assignRollbackForScope(
|
|
576
|
+
scope: Scope,
|
|
577
|
+
rollbackUpdateId: string | null,
|
|
578
|
+
) {
|
|
579
|
+
const rows = await getUpdateRowsByScope(scope);
|
|
580
|
+
const latestActive = rows.find((row) => row.is_active) || null;
|
|
581
|
+
|
|
582
|
+
const validRollback =
|
|
583
|
+
rollbackUpdateId &&
|
|
584
|
+
rows.some((row) => row.update_id === rollbackUpdateId && row.is_active)
|
|
585
|
+
? rollbackUpdateId
|
|
586
|
+
: null;
|
|
587
|
+
|
|
588
|
+
const normalizedRollback =
|
|
589
|
+
validRollback && validRollback !== latestActive?.update_id
|
|
590
|
+
? validRollback
|
|
591
|
+
: null;
|
|
592
|
+
|
|
593
|
+
const channelRow = await upsertChannelRow(scope, {
|
|
594
|
+
latest_update_id: latestActive?.update_id || null,
|
|
595
|
+
latest_created_at: latestActive?.created_at || null,
|
|
596
|
+
latest_created_at_path: latestActive?.created_at_path || null,
|
|
597
|
+
active_update_id: normalizedRollback,
|
|
598
|
+
active_changed_at: new Date().toISOString(),
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
return { channelRow, latestActive, rollbackId: normalizedRollback };
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
function extractMetadataFromManifest(
|
|
605
|
+
manifest: Record<string, unknown>,
|
|
606
|
+
): Record<string, unknown> | null {
|
|
607
|
+
const metadata = manifest.metadata;
|
|
608
|
+
if (metadata && typeof metadata === "object" && !Array.isArray(metadata)) {
|
|
609
|
+
return metadata as Record<string, unknown>;
|
|
610
|
+
}
|
|
611
|
+
return null;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function parseUpdateMetaContent(content: Record<string, unknown>): {
|
|
615
|
+
is_active: boolean;
|
|
616
|
+
updated_at: string;
|
|
617
|
+
disabled_at: string | null;
|
|
618
|
+
} {
|
|
619
|
+
const now = new Date().toISOString();
|
|
620
|
+
const nextIsActive = content.isActive !== false;
|
|
621
|
+
|
|
622
|
+
return {
|
|
623
|
+
is_active: nextIsActive,
|
|
624
|
+
updated_at: now,
|
|
625
|
+
disabled_at: nextIsActive ? null : now,
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function buildLatestPayload(row: OtaUpdateRow, reason: string) {
|
|
630
|
+
return {
|
|
631
|
+
id: row.update_id,
|
|
632
|
+
updateId: row.update_id,
|
|
633
|
+
buildId: row.build_id,
|
|
634
|
+
runtimeVersion: row.runtime_version,
|
|
635
|
+
channel: row.channel,
|
|
636
|
+
platform: row.platform,
|
|
637
|
+
createdAt: row.created_at,
|
|
638
|
+
createdAtPath: row.created_at_path,
|
|
639
|
+
isActive: row.is_active,
|
|
640
|
+
updatedAt: row.updated_at,
|
|
641
|
+
reason,
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
export function encodeUpdateKey(value: string): string {
|
|
646
|
+
return Buffer.from(value, "utf-8").toString("base64url");
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
export function decodeUpdateKey(value: string): string {
|
|
650
|
+
return Buffer.from(value, "base64url").toString("utf-8");
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
export function parseUpdateKey(key: string) {
|
|
654
|
+
return {
|
|
655
|
+
updateId: extractUpdateIdFromKey(key),
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
export async function getUpdatesPage(options: ListOptions) {
|
|
660
|
+
const { data, error } = await supabase
|
|
661
|
+
.from("ota_updates")
|
|
662
|
+
.select("*")
|
|
663
|
+
.order("created_at", { ascending: false })
|
|
664
|
+
.execute();
|
|
665
|
+
|
|
666
|
+
if (error) {
|
|
667
|
+
throw new Error(`Failed to fetch ota_updates: ${error.message}`);
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
const rows = (data || []) as OtaUpdateRow[];
|
|
671
|
+
const scopeKeys = Array.from(
|
|
672
|
+
new Set(
|
|
673
|
+
rows.map(
|
|
674
|
+
(row) => `${row.runtime_version}::${row.channel}::${row.platform}`,
|
|
675
|
+
),
|
|
676
|
+
),
|
|
677
|
+
);
|
|
678
|
+
|
|
679
|
+
const channelsByScope = new Map<string, OtaChannelRow | null>();
|
|
680
|
+
await Promise.all(
|
|
681
|
+
scopeKeys.map(async (scopeKey) => {
|
|
682
|
+
const [runtimeVersion, channel, platform] = scopeKey.split("::");
|
|
683
|
+
const channelRow = await getChannelRow({
|
|
684
|
+
runtimeVersion,
|
|
685
|
+
channel,
|
|
686
|
+
platform: platform as "ios" | "android",
|
|
687
|
+
});
|
|
688
|
+
channelsByScope.set(scopeKey, channelRow);
|
|
689
|
+
}),
|
|
690
|
+
);
|
|
691
|
+
|
|
692
|
+
const allUpdates = rows.map((row) => {
|
|
693
|
+
const scopeKey = `${row.runtime_version}::${row.channel}::${row.platform}`;
|
|
694
|
+
const channelRow = channelsByScope.get(scopeKey) || null;
|
|
695
|
+
return mapRowToRecord(row, channelRow);
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
const rollbackLockByScope = scopeKeys.map((scopeKey) => {
|
|
699
|
+
const [runtimeVersion, channel, platform] = scopeKey.split("::");
|
|
700
|
+
const channelRow = channelsByScope.get(scopeKey) || null;
|
|
701
|
+
const rollbackActive =
|
|
702
|
+
Boolean(channelRow?.active_update_id) &&
|
|
703
|
+
channelRow?.active_update_id !== channelRow?.latest_update_id;
|
|
704
|
+
|
|
705
|
+
return {
|
|
706
|
+
scopeKey,
|
|
707
|
+
runtimeVersion,
|
|
708
|
+
channel,
|
|
709
|
+
platform: platform as "ios" | "android",
|
|
710
|
+
rollbackActive,
|
|
711
|
+
autoDeliveryEnabled: !rollbackActive,
|
|
712
|
+
activeUpdateId: channelRow?.active_update_id || null,
|
|
713
|
+
latestUpdateId: channelRow?.latest_update_id || null,
|
|
714
|
+
};
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
const sorted = sortUpdates(allUpdates, options.sortBy, options.order);
|
|
718
|
+
const total = sorted.length;
|
|
719
|
+
const totalPages = Math.max(1, Math.ceil(total / options.pageSize));
|
|
720
|
+
const page = Math.min(Math.max(1, options.page), totalPages);
|
|
721
|
+
const start = (page - 1) * options.pageSize;
|
|
722
|
+
const end = start + options.pageSize;
|
|
723
|
+
|
|
724
|
+
return {
|
|
725
|
+
items: sorted.slice(start, end),
|
|
726
|
+
total,
|
|
727
|
+
page,
|
|
728
|
+
pageSize: options.pageSize,
|
|
729
|
+
totalPages,
|
|
730
|
+
rollbackLockByScope,
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
export async function getUpdateDetailByKey(key: string): Promise<UpdateDetail> {
|
|
735
|
+
const updateId = extractUpdateIdFromKey(key);
|
|
736
|
+
const row = await getUpdateRowById(updateId);
|
|
737
|
+
const scope = makeScopeFromRow(row);
|
|
738
|
+
const channelRow = await getChannelRow(scope);
|
|
739
|
+
const newestRow = await getNewestUpdateByScope(scope);
|
|
740
|
+
const record = mapRowToRecord(row, channelRow);
|
|
741
|
+
|
|
742
|
+
const manifest =
|
|
743
|
+
row.manifest &&
|
|
744
|
+
typeof row.manifest === "object" &&
|
|
745
|
+
!Array.isArray(row.manifest)
|
|
746
|
+
? row.manifest
|
|
747
|
+
: {};
|
|
748
|
+
|
|
749
|
+
return {
|
|
750
|
+
...record,
|
|
751
|
+
updateInfo: manifest,
|
|
752
|
+
updateMeta: mapUpdateMeta(row),
|
|
753
|
+
metadata: extractMetadataFromManifest(manifest),
|
|
754
|
+
channelLatest: channelLatestToJson(
|
|
755
|
+
channelRow,
|
|
756
|
+
newestRow?.update_id || null,
|
|
757
|
+
newestRow?.created_at || null,
|
|
758
|
+
),
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
export async function deleteUpdateByKey(
|
|
763
|
+
key: string,
|
|
764
|
+
): Promise<{ removedFiles: number }> {
|
|
765
|
+
const updateId = extractUpdateIdFromKey(key);
|
|
766
|
+
const row = await getUpdateRowById(updateId);
|
|
767
|
+
const scope = makeScopeFromRow(row);
|
|
768
|
+
|
|
769
|
+
let removedFiles = 0;
|
|
770
|
+
const bucket = row.storage_bucket || SUPABASE_BUCKET;
|
|
771
|
+
const basePath = toPosixPath(row.storage_base_path || "");
|
|
772
|
+
|
|
773
|
+
if (basePath) {
|
|
774
|
+
const files = await listAllFilesRecursive(bucket, basePath).catch(() => []);
|
|
775
|
+
removedFiles = files.length;
|
|
776
|
+
|
|
777
|
+
if (files.length > 0) {
|
|
778
|
+
const batchSize = 100;
|
|
779
|
+
for (let index = 0; index < files.length; index += batchSize) {
|
|
780
|
+
const batch = files.slice(index, index + batchSize);
|
|
781
|
+
const { error } = await supabase.storage.from(bucket).remove(batch);
|
|
782
|
+
if (error) {
|
|
783
|
+
throw new Error(`Failed to remove files: ${error.message}`);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
const { error: deleteError } = await supabase
|
|
790
|
+
.from("ota_updates")
|
|
791
|
+
.delete()
|
|
792
|
+
.eq("update_id", updateId)
|
|
793
|
+
.execute();
|
|
794
|
+
|
|
795
|
+
if (deleteError) {
|
|
796
|
+
throw new Error(`Failed to delete ota_updates row: ${deleteError.message}`);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
await recalculateChannelPointers(scope);
|
|
800
|
+
|
|
801
|
+
return { removedFiles };
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
export async function updateJsonFileByKey(
|
|
805
|
+
key: string,
|
|
806
|
+
fileName:
|
|
807
|
+
| "update-info.json"
|
|
808
|
+
| "update-meta.json"
|
|
809
|
+
| "metadata.json"
|
|
810
|
+
| "channel-latest.json",
|
|
811
|
+
nextJson: unknown,
|
|
812
|
+
) {
|
|
813
|
+
const updateId = extractUpdateIdFromKey(key);
|
|
814
|
+
const row = await getUpdateRowById(updateId);
|
|
815
|
+
const scope = makeScopeFromRow(row);
|
|
816
|
+
|
|
817
|
+
if (!nextJson || typeof nextJson !== "object" || Array.isArray(nextJson)) {
|
|
818
|
+
throw new Error("content must be a JSON object");
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
const content = nextJson as Record<string, unknown>;
|
|
822
|
+
|
|
823
|
+
if (fileName === "update-info.json") {
|
|
824
|
+
const assets = Array.isArray(content.assets) ? content.assets : [];
|
|
825
|
+
const launchAssetPath =
|
|
826
|
+
content.launchAsset && typeof content.launchAsset === "object"
|
|
827
|
+
? ((content.launchAsset as { path?: string }).path ?? null)
|
|
828
|
+
: null;
|
|
829
|
+
|
|
830
|
+
const { error } = await supabase
|
|
831
|
+
.from("ota_updates")
|
|
832
|
+
.update({
|
|
833
|
+
manifest: content,
|
|
834
|
+
assets_count: assets.length,
|
|
835
|
+
launch_asset_path: launchAssetPath,
|
|
836
|
+
updated_at: new Date().toISOString(),
|
|
837
|
+
})
|
|
838
|
+
.eq("update_id", updateId)
|
|
839
|
+
.execute();
|
|
840
|
+
|
|
841
|
+
if (error) {
|
|
842
|
+
throw new Error(`Failed to update manifest: ${error.message}`);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
return { path: `${row.storage_base_path}/update-info.json` };
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
if (fileName === "metadata.json") {
|
|
849
|
+
const currentManifest =
|
|
850
|
+
row.manifest && typeof row.manifest === "object" ? row.manifest : {};
|
|
851
|
+
|
|
852
|
+
const nextManifest = {
|
|
853
|
+
...currentManifest,
|
|
854
|
+
metadata: content,
|
|
855
|
+
};
|
|
856
|
+
|
|
857
|
+
const { error } = await supabase
|
|
858
|
+
.from("ota_updates")
|
|
859
|
+
.update({
|
|
860
|
+
manifest: nextManifest,
|
|
861
|
+
updated_at: new Date().toISOString(),
|
|
862
|
+
})
|
|
863
|
+
.eq("update_id", updateId)
|
|
864
|
+
.execute();
|
|
865
|
+
|
|
866
|
+
if (error) {
|
|
867
|
+
throw new Error(
|
|
868
|
+
`Failed to update metadata in manifest: ${error.message}`,
|
|
869
|
+
);
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
return { path: `${row.storage_base_path}/metadata.json` };
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
if (fileName === "update-meta.json") {
|
|
876
|
+
const parsedMeta = parseUpdateMetaContent(content);
|
|
877
|
+
|
|
878
|
+
const { error } = await supabase
|
|
879
|
+
.from("ota_updates")
|
|
880
|
+
.update(parsedMeta)
|
|
881
|
+
.eq("update_id", updateId)
|
|
882
|
+
.execute();
|
|
883
|
+
|
|
884
|
+
if (error) {
|
|
885
|
+
throw new Error(`Failed to update ota meta fields: ${error.message}`);
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
await recalculateChannelPointers(scope);
|
|
889
|
+
return { path: `${row.storage_base_path}/update-meta.json` };
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
const channelTargetIdRaw =
|
|
893
|
+
(typeof content.updateId === "string" && content.updateId) ||
|
|
894
|
+
(typeof content.id === "string" && content.id) ||
|
|
895
|
+
(typeof content.buildId === "string" && content.buildId) ||
|
|
896
|
+
null;
|
|
897
|
+
|
|
898
|
+
if (!channelTargetIdRaw || !ensureUuid(channelTargetIdRaw)) {
|
|
899
|
+
throw new Error(
|
|
900
|
+
"channel-latest.json content must include valid updateId/id/buildId",
|
|
901
|
+
);
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
const channelTarget = await getUpdateRowById(channelTargetIdRaw);
|
|
905
|
+
const channelScope = makeScopeFromRow(channelTarget);
|
|
906
|
+
const currentChannelRow = await getChannelRow(channelScope);
|
|
907
|
+
const rollbackActive =
|
|
908
|
+
Boolean(currentChannelRow?.active_update_id) &&
|
|
909
|
+
currentChannelRow?.active_update_id !== currentChannelRow?.latest_update_id;
|
|
910
|
+
|
|
911
|
+
if (rollbackActive) {
|
|
912
|
+
throw new Error(
|
|
913
|
+
"Rollback is active. New updates are ignored until rollback is disabled via Promote.",
|
|
914
|
+
);
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
await upsertChannelRow(channelScope, {
|
|
918
|
+
latest_update_id: channelTarget.update_id,
|
|
919
|
+
latest_created_at: channelTarget.created_at,
|
|
920
|
+
latest_created_at_path: channelTarget.created_at_path,
|
|
921
|
+
active_update_id: null,
|
|
922
|
+
active_changed_at: new Date().toISOString(),
|
|
923
|
+
});
|
|
924
|
+
|
|
925
|
+
return {
|
|
926
|
+
path: `${channelTarget.runtime_version}/${channelTarget.platform}/channels/${channelTarget.channel}/latest.json`,
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
export async function rollbackToUpdateByKey(
|
|
931
|
+
key: string,
|
|
932
|
+
options?: RollbackOptions,
|
|
933
|
+
) {
|
|
934
|
+
const updateId = extractUpdateIdFromKey(key);
|
|
935
|
+
const row = await getUpdateRowById(updateId);
|
|
936
|
+
const scope = makeScopeFromRow(row);
|
|
937
|
+
const now = new Date().toISOString();
|
|
938
|
+
|
|
939
|
+
const latestRuntime = await getLatestRuntimeForChannelPlatform({
|
|
940
|
+
channel: row.channel,
|
|
941
|
+
platform: row.platform,
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
if (latestRuntime && latestRuntime !== row.runtime_version) {
|
|
945
|
+
throw new Error(
|
|
946
|
+
`Rollback is allowed only for runtimeVersion=${latestRuntime}. Selected update runtimeVersion=${row.runtime_version}`,
|
|
947
|
+
);
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
const currentChannel = await getChannelRow(scope);
|
|
951
|
+
const currentServedUpdateId =
|
|
952
|
+
currentChannel?.active_update_id ||
|
|
953
|
+
currentChannel?.latest_update_id ||
|
|
954
|
+
null;
|
|
955
|
+
|
|
956
|
+
if (!currentServedUpdateId) {
|
|
957
|
+
throw new Error(
|
|
958
|
+
"Rollback is not available: no active update is currently served for this scope.",
|
|
959
|
+
);
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
if (currentServedUpdateId === updateId) {
|
|
963
|
+
throw new Error(
|
|
964
|
+
"Rollback is not available for the currently served update.",
|
|
965
|
+
);
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
const currentServedRow = await getUpdateRowById(currentServedUpdateId);
|
|
969
|
+
if (!currentServedRow.is_active) {
|
|
970
|
+
throw new Error(
|
|
971
|
+
"Rollback is not available: currently served update is not active.",
|
|
972
|
+
);
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
const currentServedCreatedAt = parseDate(
|
|
976
|
+
currentServedRow.created_at || currentServedRow.created_at_path,
|
|
977
|
+
);
|
|
978
|
+
const targetCreatedAt = parseDate(row.created_at || row.created_at_path);
|
|
979
|
+
if (currentServedCreatedAt <= targetCreatedAt) {
|
|
980
|
+
throw new Error(
|
|
981
|
+
"Rollback is allowed only when a newer active update exists in the same scope.",
|
|
982
|
+
);
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
const { error: deactivateOthersError } = await supabase
|
|
986
|
+
.from("ota_updates")
|
|
987
|
+
.update({
|
|
988
|
+
is_active: false,
|
|
989
|
+
updated_at: now,
|
|
990
|
+
disabled_at: now,
|
|
991
|
+
})
|
|
992
|
+
.eq("runtime_version", scope.runtimeVersion)
|
|
993
|
+
.eq("channel", scope.channel)
|
|
994
|
+
.eq("platform", scope.platform)
|
|
995
|
+
.neq("update_id", updateId)
|
|
996
|
+
.eq("is_active", true)
|
|
997
|
+
.execute();
|
|
998
|
+
|
|
999
|
+
if (deactivateOthersError) {
|
|
1000
|
+
throw new Error(
|
|
1001
|
+
`Failed to deactivate other active updates in scope: ${deactivateOthersError.message}`,
|
|
1002
|
+
);
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
const { error: activationError } = await supabase
|
|
1006
|
+
.from("ota_updates")
|
|
1007
|
+
.update({
|
|
1008
|
+
is_active: true,
|
|
1009
|
+
updated_at: now,
|
|
1010
|
+
disabled_at: null,
|
|
1011
|
+
})
|
|
1012
|
+
.eq("update_id", updateId)
|
|
1013
|
+
.execute();
|
|
1014
|
+
|
|
1015
|
+
if (activationError) {
|
|
1016
|
+
throw new Error(
|
|
1017
|
+
`Failed to activate rollback target: ${activationError.message}`,
|
|
1018
|
+
);
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
await supabase
|
|
1022
|
+
.from("ota_updates")
|
|
1023
|
+
.update({
|
|
1024
|
+
rolled_back_from_update_id:
|
|
1025
|
+
currentServedUpdateId && currentServedUpdateId !== updateId
|
|
1026
|
+
? currentServedUpdateId
|
|
1027
|
+
: null,
|
|
1028
|
+
updated_at: now,
|
|
1029
|
+
})
|
|
1030
|
+
.eq("update_id", updateId)
|
|
1031
|
+
.execute();
|
|
1032
|
+
|
|
1033
|
+
const refreshed = await getUpdateRowById(updateId);
|
|
1034
|
+
const newestRow = await getNewestUpdateByScope(scope);
|
|
1035
|
+
const rollbackLatestRow = newestRow || currentServedRow;
|
|
1036
|
+
|
|
1037
|
+
await clearRollbackForOtherRuntimes(scope);
|
|
1038
|
+
|
|
1039
|
+
const channelRow = await upsertChannelRow(scope, {
|
|
1040
|
+
// Keep latest pointer on the newest update in scope.
|
|
1041
|
+
latest_update_id: rollbackLatestRow.update_id,
|
|
1042
|
+
latest_created_at: rollbackLatestRow.created_at,
|
|
1043
|
+
latest_created_at_path: rollbackLatestRow.created_at_path,
|
|
1044
|
+
// Enable rollback mode by pinning active pointer to an older update.
|
|
1045
|
+
active_update_id: refreshed.update_id,
|
|
1046
|
+
active_changed_at: now,
|
|
1047
|
+
});
|
|
1048
|
+
|
|
1049
|
+
// Pre-populate the mapping so the first device request after rollback
|
|
1050
|
+
// can immediately resolve served_manifest_id → update_id without a cold miss.
|
|
1051
|
+
if (channelRow.served_manifest_id) {
|
|
1052
|
+
logServedManifestMapping({
|
|
1053
|
+
servedManifestId: channelRow.served_manifest_id,
|
|
1054
|
+
updateId: refreshed.update_id,
|
|
1055
|
+
runtimeVersion: scope.runtimeVersion,
|
|
1056
|
+
channel: scope.channel,
|
|
1057
|
+
platform: scope.platform,
|
|
1058
|
+
reason: "rollback",
|
|
1059
|
+
});
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
return {
|
|
1063
|
+
...buildLatestPayload(refreshed, options?.reason || "rollback"),
|
|
1064
|
+
activeChangedAt: channelRow.active_changed_at,
|
|
1065
|
+
rollbackAssigned:
|
|
1066
|
+
Boolean(channelRow.active_update_id) &&
|
|
1067
|
+
channelRow.active_update_id !== channelRow.latest_update_id,
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
export async function promoteLatestByKey(key: string) {
|
|
1072
|
+
const updateId = extractUpdateIdFromKey(key);
|
|
1073
|
+
const row = await getUpdateRowById(updateId);
|
|
1074
|
+
const scope = makeScopeFromRow(row);
|
|
1075
|
+
|
|
1076
|
+
const channelRow = await getChannelRow(scope);
|
|
1077
|
+
const latestUpdateId = channelRow?.latest_update_id || null;
|
|
1078
|
+
const rollbackActive =
|
|
1079
|
+
Boolean(channelRow?.active_update_id) &&
|
|
1080
|
+
channelRow?.active_update_id !== channelRow?.latest_update_id;
|
|
1081
|
+
|
|
1082
|
+
if (!latestUpdateId) {
|
|
1083
|
+
throw new Error("Cannot promote: latest update is not set for this scope.");
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
if (latestUpdateId !== row.update_id) {
|
|
1087
|
+
throw new Error("Promote is allowed only on the latest update.");
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
if (!rollbackActive) {
|
|
1091
|
+
throw new Error("Promote is not available because rollback is not active.");
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
const promoteNow = new Date().toISOString();
|
|
1095
|
+
|
|
1096
|
+
const { error: deactivateOthersError } = await supabase
|
|
1097
|
+
.from("ota_updates")
|
|
1098
|
+
.update({
|
|
1099
|
+
is_active: false,
|
|
1100
|
+
updated_at: promoteNow,
|
|
1101
|
+
disabled_at: promoteNow,
|
|
1102
|
+
})
|
|
1103
|
+
.eq("runtime_version", scope.runtimeVersion)
|
|
1104
|
+
.eq("channel", scope.channel)
|
|
1105
|
+
.eq("platform", scope.platform)
|
|
1106
|
+
.neq("update_id", latestUpdateId)
|
|
1107
|
+
.eq("is_active", true)
|
|
1108
|
+
.execute();
|
|
1109
|
+
|
|
1110
|
+
if (deactivateOthersError) {
|
|
1111
|
+
throw new Error(
|
|
1112
|
+
`Failed to deactivate active updates during promote: ${deactivateOthersError.message}`,
|
|
1113
|
+
);
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
const { error: activateLatestError } = await supabase
|
|
1117
|
+
.from("ota_updates")
|
|
1118
|
+
.update({
|
|
1119
|
+
is_active: true,
|
|
1120
|
+
updated_at: promoteNow,
|
|
1121
|
+
disabled_at: null,
|
|
1122
|
+
})
|
|
1123
|
+
.eq("update_id", latestUpdateId)
|
|
1124
|
+
.execute();
|
|
1125
|
+
|
|
1126
|
+
if (activateLatestError) {
|
|
1127
|
+
throw new Error(
|
|
1128
|
+
`Failed to activate latest update during promote: ${activateLatestError.message}`,
|
|
1129
|
+
);
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
const updatedChannel = await upsertChannelRow(scope, {
|
|
1133
|
+
latest_update_id: channelRow?.latest_update_id || row.update_id,
|
|
1134
|
+
latest_created_at: channelRow?.latest_created_at || row.created_at,
|
|
1135
|
+
latest_created_at_path:
|
|
1136
|
+
channelRow?.latest_created_at_path || row.created_at_path,
|
|
1137
|
+
active_update_id: null,
|
|
1138
|
+
active_changed_at: promoteNow,
|
|
1139
|
+
});
|
|
1140
|
+
|
|
1141
|
+
// Pre-populate the mapping so the first device request after promote
|
|
1142
|
+
// can immediately resolve served_manifest_id → update_id without a cold miss.
|
|
1143
|
+
if (updatedChannel.served_manifest_id && updatedChannel.latest_update_id) {
|
|
1144
|
+
logServedManifestMapping({
|
|
1145
|
+
servedManifestId: updatedChannel.served_manifest_id,
|
|
1146
|
+
updateId: updatedChannel.latest_update_id,
|
|
1147
|
+
runtimeVersion: scope.runtimeVersion,
|
|
1148
|
+
channel: scope.channel,
|
|
1149
|
+
platform: scope.platform,
|
|
1150
|
+
reason: "promote",
|
|
1151
|
+
});
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
return {
|
|
1155
|
+
promoted: true,
|
|
1156
|
+
latestUpdateId: updatedChannel.latest_update_id,
|
|
1157
|
+
activeUpdateId: updatedChannel.active_update_id,
|
|
1158
|
+
rollbackActive: false,
|
|
1159
|
+
};
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
export async function setUpdateDisabledByKey(key: string, disabled: boolean) {
|
|
1163
|
+
const updateId = extractUpdateIdFromKey(key);
|
|
1164
|
+
const row = await getUpdateRowById(updateId);
|
|
1165
|
+
const scope = makeScopeFromRow(row);
|
|
1166
|
+
const now = new Date().toISOString();
|
|
1167
|
+
|
|
1168
|
+
const scopeChannelRow = await getChannelRow(scope);
|
|
1169
|
+
const rollbackActive =
|
|
1170
|
+
Boolean(scopeChannelRow?.active_update_id) &&
|
|
1171
|
+
scopeChannelRow?.active_update_id !== scopeChannelRow?.latest_update_id;
|
|
1172
|
+
const newestRow = await getNewestUpdateByScope(scope);
|
|
1173
|
+
const isNewestInScope = newestRow?.update_id === updateId;
|
|
1174
|
+
|
|
1175
|
+
if (!disabled) {
|
|
1176
|
+
if (!isNewestInScope) {
|
|
1177
|
+
throw new Error(
|
|
1178
|
+
"Direct activation is allowed only for the newest update in scope. Use Rollback for older updates.",
|
|
1179
|
+
);
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
if (rollbackActive) {
|
|
1183
|
+
throw new Error(
|
|
1184
|
+
"Direct activation is unavailable while rollback mode is active. Use Promote on the newest update.",
|
|
1185
|
+
);
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
const { error: deactivateOthersError } = await supabase
|
|
1189
|
+
.from("ota_updates")
|
|
1190
|
+
.update({
|
|
1191
|
+
is_active: false,
|
|
1192
|
+
updated_at: now,
|
|
1193
|
+
disabled_at: now,
|
|
1194
|
+
})
|
|
1195
|
+
.eq("runtime_version", scope.runtimeVersion)
|
|
1196
|
+
.eq("channel", scope.channel)
|
|
1197
|
+
.eq("platform", scope.platform)
|
|
1198
|
+
.neq("update_id", updateId)
|
|
1199
|
+
.eq("is_active", true)
|
|
1200
|
+
.execute();
|
|
1201
|
+
|
|
1202
|
+
if (deactivateOthersError) {
|
|
1203
|
+
throw new Error(
|
|
1204
|
+
`Failed to deactivate other active updates in scope: ${deactivateOthersError.message}`,
|
|
1205
|
+
);
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
const { error } = await supabase
|
|
1210
|
+
.from("ota_updates")
|
|
1211
|
+
.update({
|
|
1212
|
+
is_active: !disabled,
|
|
1213
|
+
updated_at: now,
|
|
1214
|
+
disabled_at: disabled ? now : null,
|
|
1215
|
+
})
|
|
1216
|
+
.eq("update_id", updateId)
|
|
1217
|
+
.execute();
|
|
1218
|
+
|
|
1219
|
+
if (error) {
|
|
1220
|
+
throw new Error(`Failed to change update active state: ${error.message}`);
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
const currentChannel = await getChannelRow(scope);
|
|
1224
|
+
const shouldClearRollback =
|
|
1225
|
+
disabled && currentChannel?.active_update_id === updateId;
|
|
1226
|
+
|
|
1227
|
+
const { channelRow, latestActive, rollbackId } = shouldClearRollback
|
|
1228
|
+
? await assignRollbackForScope(scope, null)
|
|
1229
|
+
: await recalculateChannelPointers(scope);
|
|
1230
|
+
|
|
1231
|
+
return {
|
|
1232
|
+
updated: true,
|
|
1233
|
+
disabled,
|
|
1234
|
+
state: disabled ? "disabled" : "active",
|
|
1235
|
+
fallbackApplied: Boolean(latestActive),
|
|
1236
|
+
activeUpdateId: channelRow.active_update_id,
|
|
1237
|
+
latestUpdateId: channelRow.latest_update_id,
|
|
1238
|
+
rollbackActive: Boolean(rollbackId),
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
export function normalizeSortBy(value: string | null): SortField {
|
|
1243
|
+
const allowed: SortField[] = [
|
|
1244
|
+
"createdAt",
|
|
1245
|
+
"runtimeVersion",
|
|
1246
|
+
"platform",
|
|
1247
|
+
"channel",
|
|
1248
|
+
"stage",
|
|
1249
|
+
"status",
|
|
1250
|
+
"updateId",
|
|
1251
|
+
];
|
|
1252
|
+
|
|
1253
|
+
if (value && allowed.includes(value as SortField)) {
|
|
1254
|
+
return value as SortField;
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
return "createdAt";
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
export function normalizeSortOrder(value: string | null): SortOrder {
|
|
1261
|
+
return value === "asc" ? "asc" : "desc";
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
export function normalizePage(value: string | null): number {
|
|
1265
|
+
const parsed = Number(value);
|
|
1266
|
+
if (!Number.isFinite(parsed) || parsed < 1) {
|
|
1267
|
+
return 1;
|
|
1268
|
+
}
|
|
1269
|
+
return Math.floor(parsed);
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
export function normalizePageSize(value: string | null): number {
|
|
1273
|
+
const parsed = Number(value);
|
|
1274
|
+
if (!Number.isFinite(parsed) || parsed < 1) {
|
|
1275
|
+
return 20;
|
|
1276
|
+
}
|
|
1277
|
+
return Math.min(100, Math.floor(parsed));
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
export function toStoragePath(value: string): string {
|
|
1281
|
+
return toPosixPath(value);
|
|
1282
|
+
}
|