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,24 @@
|
|
|
1
|
+
export function signRSASHA256(data, privateKey) {
|
|
2
|
+
const sign = crypto.createSign("RSA-SHA256");
|
|
3
|
+
sign.update(data, "utf8");
|
|
4
|
+
sign.end();
|
|
5
|
+
return sign.sign(privateKey, "base64");
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export async function getPrivateKeyAsync() {
|
|
9
|
+
const privateKeyPath = process.env.PRIVATE_KEY_PATH;
|
|
10
|
+
if (!privateKeyPath) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function buildRequestContext({
|
|
16
|
+
channel,
|
|
17
|
+
runtimeVersion,
|
|
18
|
+
platform,
|
|
19
|
+
updateId,
|
|
20
|
+
createdAtPath,
|
|
21
|
+
buildId,
|
|
22
|
+
} = {}) {
|
|
23
|
+
return `channel=${channel || "unknown"} runtime=${runtimeVersion || "unknown"} platform=${platform || "unknown"} date=${createdAtPath || "unknown"} updateId=${updateId || buildId || "unknown"}`;
|
|
24
|
+
}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { supabase } from "./supabase.js";
|
|
3
|
+
|
|
4
|
+
export type EmergencyRedirectPlatform = "android" | "ios";
|
|
5
|
+
export type EmergencyRedirectTargetMode = "pinned" | "follow";
|
|
6
|
+
|
|
7
|
+
export type EmergencyChannelRedirect = {
|
|
8
|
+
id: string;
|
|
9
|
+
name: string;
|
|
10
|
+
enabled: boolean;
|
|
11
|
+
embeddedUpdateId: string;
|
|
12
|
+
runtimeVersion: string;
|
|
13
|
+
platform: EmergencyRedirectPlatform;
|
|
14
|
+
fromChannel: string;
|
|
15
|
+
toChannel: string;
|
|
16
|
+
targetMode: EmergencyRedirectTargetMode;
|
|
17
|
+
expectedUpdateId: string | null;
|
|
18
|
+
createdAt: string;
|
|
19
|
+
modifiedAt: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type EmergencyChannelRedirectInput = Omit<
|
|
23
|
+
EmergencyChannelRedirect,
|
|
24
|
+
"id" | "createdAt" | "modifiedAt"
|
|
25
|
+
>;
|
|
26
|
+
|
|
27
|
+
type EmergencyChannelRedirectRow = {
|
|
28
|
+
id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
enabled: boolean;
|
|
31
|
+
embedded_update_id: string;
|
|
32
|
+
runtime_version: string;
|
|
33
|
+
platform: EmergencyRedirectPlatform;
|
|
34
|
+
from_channel: string;
|
|
35
|
+
to_channel: string;
|
|
36
|
+
target_mode: EmergencyRedirectTargetMode;
|
|
37
|
+
expected_update_id: string | null;
|
|
38
|
+
created_at: string;
|
|
39
|
+
modified_at: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const UUID_PATTERN =
|
|
43
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
44
|
+
|
|
45
|
+
export class EmergencyRedirectValidationError extends Error {}
|
|
46
|
+
|
|
47
|
+
function requiredString(
|
|
48
|
+
input: Record<string, unknown>,
|
|
49
|
+
field: string,
|
|
50
|
+
): string {
|
|
51
|
+
const value = input[field];
|
|
52
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
53
|
+
throw new EmergencyRedirectValidationError(
|
|
54
|
+
`${field} must be a non-empty string`,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
return value.trim();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function parseEmergencyRedirectInput(
|
|
61
|
+
value: unknown,
|
|
62
|
+
): EmergencyChannelRedirectInput {
|
|
63
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
64
|
+
throw new EmergencyRedirectValidationError(
|
|
65
|
+
"Emergency redirect must be a JSON object",
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const input = value as Record<string, unknown>;
|
|
70
|
+
const name = requiredString(input, "name");
|
|
71
|
+
const embeddedUpdateId = requiredString(
|
|
72
|
+
input,
|
|
73
|
+
"embeddedUpdateId",
|
|
74
|
+
).toLowerCase();
|
|
75
|
+
const runtimeVersion = requiredString(input, "runtimeVersion");
|
|
76
|
+
const fromChannel = requiredString(input, "fromChannel").toLowerCase();
|
|
77
|
+
const toChannel = requiredString(input, "toChannel").toLowerCase();
|
|
78
|
+
const platform = input.platform;
|
|
79
|
+
const targetMode = input.targetMode;
|
|
80
|
+
const enabled = input.enabled;
|
|
81
|
+
const expectedUpdateId =
|
|
82
|
+
typeof input.expectedUpdateId === "string" && input.expectedUpdateId.trim()
|
|
83
|
+
? input.expectedUpdateId.trim().toLowerCase()
|
|
84
|
+
: null;
|
|
85
|
+
|
|
86
|
+
if (name.length > 120) {
|
|
87
|
+
throw new EmergencyRedirectValidationError(
|
|
88
|
+
"name must contain at most 120 characters",
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
if (!UUID_PATTERN.test(embeddedUpdateId)) {
|
|
92
|
+
throw new EmergencyRedirectValidationError(
|
|
93
|
+
"embeddedUpdateId must be a UUID",
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
if (platform !== "android" && platform !== "ios") {
|
|
97
|
+
throw new EmergencyRedirectValidationError(
|
|
98
|
+
"platform must be android or ios",
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
if (fromChannel === toChannel) {
|
|
102
|
+
throw new EmergencyRedirectValidationError(
|
|
103
|
+
"fromChannel and toChannel must differ",
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
if (targetMode !== "pinned" && targetMode !== "follow") {
|
|
107
|
+
throw new EmergencyRedirectValidationError(
|
|
108
|
+
"targetMode must be pinned or follow",
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
if (typeof enabled !== "boolean") {
|
|
112
|
+
throw new EmergencyRedirectValidationError("enabled must be boolean");
|
|
113
|
+
}
|
|
114
|
+
if (targetMode === "pinned" && !expectedUpdateId) {
|
|
115
|
+
throw new EmergencyRedirectValidationError(
|
|
116
|
+
"expectedUpdateId is required in pinned mode",
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
if (expectedUpdateId && !UUID_PATTERN.test(expectedUpdateId)) {
|
|
120
|
+
throw new EmergencyRedirectValidationError(
|
|
121
|
+
"expectedUpdateId must be a UUID",
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
name,
|
|
127
|
+
enabled,
|
|
128
|
+
embeddedUpdateId,
|
|
129
|
+
runtimeVersion,
|
|
130
|
+
platform,
|
|
131
|
+
fromChannel,
|
|
132
|
+
toChannel,
|
|
133
|
+
targetMode,
|
|
134
|
+
expectedUpdateId,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function mapRow(row: EmergencyChannelRedirectRow): EmergencyChannelRedirect {
|
|
139
|
+
return {
|
|
140
|
+
id: row.id,
|
|
141
|
+
name: row.name,
|
|
142
|
+
enabled: row.enabled,
|
|
143
|
+
embeddedUpdateId: row.embedded_update_id,
|
|
144
|
+
runtimeVersion: row.runtime_version,
|
|
145
|
+
platform: row.platform,
|
|
146
|
+
fromChannel: row.from_channel,
|
|
147
|
+
toChannel: row.to_channel,
|
|
148
|
+
targetMode: row.target_mode,
|
|
149
|
+
expectedUpdateId: row.expected_update_id,
|
|
150
|
+
createdAt: row.created_at,
|
|
151
|
+
modifiedAt: row.modified_at,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function toRow(
|
|
156
|
+
id: string,
|
|
157
|
+
input: EmergencyChannelRedirectInput,
|
|
158
|
+
): EmergencyChannelRedirectRow {
|
|
159
|
+
const now = new Date().toISOString();
|
|
160
|
+
return {
|
|
161
|
+
id,
|
|
162
|
+
name: input.name,
|
|
163
|
+
enabled: input.enabled,
|
|
164
|
+
embedded_update_id: input.embeddedUpdateId,
|
|
165
|
+
runtime_version: input.runtimeVersion,
|
|
166
|
+
platform: input.platform,
|
|
167
|
+
from_channel: input.fromChannel,
|
|
168
|
+
to_channel: input.toChannel,
|
|
169
|
+
target_mode: input.targetMode,
|
|
170
|
+
expected_update_id: input.expectedUpdateId,
|
|
171
|
+
created_at: now,
|
|
172
|
+
modified_at: now,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export async function resolveEmergencyChannelRedirect({
|
|
177
|
+
requestedChannel,
|
|
178
|
+
runtimeVersion,
|
|
179
|
+
platform,
|
|
180
|
+
embeddedUpdateId,
|
|
181
|
+
}: {
|
|
182
|
+
requestedChannel: string;
|
|
183
|
+
runtimeVersion: string | null;
|
|
184
|
+
platform: string | null;
|
|
185
|
+
embeddedUpdateId: string | null;
|
|
186
|
+
}): Promise<EmergencyChannelRedirect | null> {
|
|
187
|
+
if (
|
|
188
|
+
!runtimeVersion ||
|
|
189
|
+
(platform !== "android" && platform !== "ios") ||
|
|
190
|
+
!embeddedUpdateId
|
|
191
|
+
) {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const { data, error } = await supabase
|
|
196
|
+
.from("ota_emergency_redirects")
|
|
197
|
+
.select("*")
|
|
198
|
+
.eq("enabled", true)
|
|
199
|
+
.eq("embedded_update_id", embeddedUpdateId.toLowerCase())
|
|
200
|
+
.eq("runtime_version", runtimeVersion)
|
|
201
|
+
.eq("platform", platform)
|
|
202
|
+
.eq("from_channel", requestedChannel)
|
|
203
|
+
.maybeSingle();
|
|
204
|
+
|
|
205
|
+
if (error) {
|
|
206
|
+
throw new Error(`Failed to resolve emergency redirect: ${error.message}`);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return data ? mapRow(data as EmergencyChannelRedirectRow) : null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export async function listEmergencyRedirects(): Promise<
|
|
213
|
+
EmergencyChannelRedirect[]
|
|
214
|
+
> {
|
|
215
|
+
const { data, error } = await supabase
|
|
216
|
+
.from("ota_emergency_redirects")
|
|
217
|
+
.select("*")
|
|
218
|
+
.order("modified_at", { ascending: false })
|
|
219
|
+
.execute();
|
|
220
|
+
|
|
221
|
+
if (error) {
|
|
222
|
+
throw new Error(`Failed to list emergency redirects: ${error.message}`);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return ((data || []) as EmergencyChannelRedirectRow[]).map(mapRow);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export async function createEmergencyRedirect(
|
|
229
|
+
value: unknown,
|
|
230
|
+
): Promise<EmergencyChannelRedirect> {
|
|
231
|
+
const input = parseEmergencyRedirectInput(value);
|
|
232
|
+
const row = toRow(randomUUID(), input);
|
|
233
|
+
const { data, error } = await supabase
|
|
234
|
+
.from("ota_emergency_redirects")
|
|
235
|
+
.upsert(row, { onConflict: "id" })
|
|
236
|
+
.select("*")
|
|
237
|
+
.single();
|
|
238
|
+
|
|
239
|
+
if (error || !data) {
|
|
240
|
+
throw new Error(`Failed to create emergency redirect: ${error?.message}`);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return mapRow(data as EmergencyChannelRedirectRow);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async function getEmergencyRedirectRow(
|
|
247
|
+
id: string,
|
|
248
|
+
): Promise<EmergencyChannelRedirectRow> {
|
|
249
|
+
const { data, error } = await supabase
|
|
250
|
+
.from("ota_emergency_redirects")
|
|
251
|
+
.select("*")
|
|
252
|
+
.eq("id", id)
|
|
253
|
+
.single();
|
|
254
|
+
|
|
255
|
+
if (error || !data) {
|
|
256
|
+
throw new Error(`Emergency redirect not found: ${id}`);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return data as EmergencyChannelRedirectRow;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export async function updateEmergencyRedirect(
|
|
263
|
+
id: string,
|
|
264
|
+
patch: unknown,
|
|
265
|
+
): Promise<EmergencyChannelRedirect> {
|
|
266
|
+
if (!patch || typeof patch !== "object" || Array.isArray(patch)) {
|
|
267
|
+
throw new EmergencyRedirectValidationError("Patch must be a JSON object");
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const current = mapRow(await getEmergencyRedirectRow(id));
|
|
271
|
+
const input = parseEmergencyRedirectInput({
|
|
272
|
+
...current,
|
|
273
|
+
...(patch as Record<string, unknown>),
|
|
274
|
+
});
|
|
275
|
+
const { data, error } = await supabase
|
|
276
|
+
.from("ota_emergency_redirects")
|
|
277
|
+
.update({
|
|
278
|
+
name: input.name,
|
|
279
|
+
enabled: input.enabled,
|
|
280
|
+
embedded_update_id: input.embeddedUpdateId,
|
|
281
|
+
runtime_version: input.runtimeVersion,
|
|
282
|
+
platform: input.platform,
|
|
283
|
+
from_channel: input.fromChannel,
|
|
284
|
+
to_channel: input.toChannel,
|
|
285
|
+
target_mode: input.targetMode,
|
|
286
|
+
expected_update_id: input.expectedUpdateId,
|
|
287
|
+
modified_at: new Date().toISOString(),
|
|
288
|
+
})
|
|
289
|
+
.eq("id", id)
|
|
290
|
+
.select("*")
|
|
291
|
+
.single();
|
|
292
|
+
|
|
293
|
+
if (error || !data) {
|
|
294
|
+
throw new Error(`Failed to update emergency redirect: ${error?.message}`);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return mapRow(data as EmergencyChannelRedirectRow);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export async function deleteEmergencyRedirect(id: string): Promise<void> {
|
|
301
|
+
const { error } = await supabase
|
|
302
|
+
.from("ota_emergency_redirects")
|
|
303
|
+
.delete()
|
|
304
|
+
.eq("id", id)
|
|
305
|
+
.execute();
|
|
306
|
+
|
|
307
|
+
if (error) {
|
|
308
|
+
throw new Error(`Failed to delete emergency redirect: ${error.message}`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import crypto from "crypto";
|
|
3
|
+
import { Buffer } from "buffer";
|
|
4
|
+
import { supabase, SUPABASE_BUCKET, SIGNED_URL_TTL } from "./supabase.js";
|
|
5
|
+
|
|
6
|
+
const SIGNED_URL_RETRIES = parseInt(
|
|
7
|
+
process.env.OTA_SIGNED_URL_RETRIES ||
|
|
8
|
+
process.env.SUPABASE_SIGNED_URL_RETRIES ||
|
|
9
|
+
"3",
|
|
10
|
+
10,
|
|
11
|
+
);
|
|
12
|
+
const SIGNED_URL_RETRY_DELAY_MS = parseInt(
|
|
13
|
+
process.env.OTA_SIGNED_URL_RETRY_DELAY_MS ||
|
|
14
|
+
process.env.SUPABASE_SIGNED_URL_RETRY_DELAY_MS ||
|
|
15
|
+
"250",
|
|
16
|
+
10,
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
function createHash(file, hashingAlgorithm, encoding) {
|
|
20
|
+
return crypto.createHash(hashingAlgorithm).update(file).digest(encoding);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function convertSHA256HashToUUID(value) {
|
|
24
|
+
return `${value.slice(0, 8)}-${value.slice(8, 12)}-${value.slice(12, 16)}-${value.slice(16, 20)}-${value.slice(20, 32)}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function toPosixPath(value) {
|
|
28
|
+
return value.split(path.sep).join(path.posix.sep);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function downloadJsonFromStorage(storagePath) {
|
|
32
|
+
const { data, error } = await supabase.storage
|
|
33
|
+
.from(SUPABASE_BUCKET)
|
|
34
|
+
.download(storagePath);
|
|
35
|
+
|
|
36
|
+
if (error) {
|
|
37
|
+
console.error(
|
|
38
|
+
`[${new Date().toLocaleTimeString()}] ⚠️ Download error for ${storagePath}:`,
|
|
39
|
+
error,
|
|
40
|
+
);
|
|
41
|
+
throw new Error(
|
|
42
|
+
`Failed to download ${storagePath}: ${error.message || JSON.stringify(error)}`,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!data) {
|
|
47
|
+
throw new Error(`No data returned for ${storagePath}`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const arrayBuffer = await data.arrayBuffer();
|
|
51
|
+
return JSON.parse(Buffer.from(arrayBuffer).toString("utf-8"));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function createSignedUrl(storagePath) {
|
|
55
|
+
for (let attempt = 1; attempt <= SIGNED_URL_RETRIES; attempt += 1) {
|
|
56
|
+
const { data, error } = await supabase.storage
|
|
57
|
+
.from(SUPABASE_BUCKET)
|
|
58
|
+
.createSignedUrl(storagePath, SIGNED_URL_TTL);
|
|
59
|
+
|
|
60
|
+
if (!error) {
|
|
61
|
+
return data.signedUrl;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const message = error.message || "Unknown error";
|
|
65
|
+
const isRetryable =
|
|
66
|
+
message.includes("Bad Gateway") ||
|
|
67
|
+
message.includes("Gateway Timeout") ||
|
|
68
|
+
message.includes("Service Unavailable") ||
|
|
69
|
+
message.includes("Internal Server Error");
|
|
70
|
+
|
|
71
|
+
if (!isRetryable || attempt === SIGNED_URL_RETRIES) {
|
|
72
|
+
throw new Error(`Failed to sign ${storagePath}: ${message}`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const delayMs = SIGNED_URL_RETRY_DELAY_MS * attempt;
|
|
76
|
+
console.warn(
|
|
77
|
+
`[${new Date().toLocaleTimeString()}] ⚠️ createSignedUrl retry ${attempt}/${SIGNED_URL_RETRIES} for ${storagePath} after ${delayMs}ms: ${message}`,
|
|
78
|
+
);
|
|
79
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
throw new Error(`Failed to sign ${storagePath}: exhausted retries`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export {
|
|
86
|
+
createHash,
|
|
87
|
+
convertSHA256HashToUUID,
|
|
88
|
+
toPosixPath,
|
|
89
|
+
downloadJsonFromStorage,
|
|
90
|
+
createSignedUrl,
|
|
91
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
function sendMultipartResponse({ res, protocolVersion, boundary, buffer }) {
|
|
2
|
+
res.statusCode = 200;
|
|
3
|
+
res.setHeader("expo-protocol-version", protocolVersion);
|
|
4
|
+
res.setHeader("expo-sfv-version", 0);
|
|
5
|
+
res.setHeader("cache-control", "private, max-age=0");
|
|
6
|
+
res.setHeader("content-type", `multipart/mixed; boundary=${boundary}`);
|
|
7
|
+
res.write(buffer);
|
|
8
|
+
res.end();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { sendMultipartResponse };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import crypto from "crypto";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
|
|
4
|
+
function serializeSignatureHeader(signature, keyId = "main") {
|
|
5
|
+
return `sig="${signature}", keyid="${keyId}"`;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function signRSASHA256(data, privateKey) {
|
|
9
|
+
const sign = crypto.createSign("RSA-SHA256");
|
|
10
|
+
sign.update(data, "utf8");
|
|
11
|
+
sign.end();
|
|
12
|
+
return sign.sign(privateKey, "base64");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function getPrivateKey() {
|
|
16
|
+
const inlineKey = process.env.CODE_SIGNING_PRIVATE_KEY?.trim();
|
|
17
|
+
if (inlineKey) {
|
|
18
|
+
return inlineKey.replaceAll("\\n", "\n");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const keyPath = process.env.CODE_SIGNING_PRIVATE_KEY_PATH?.trim();
|
|
22
|
+
if (!keyPath) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return readFile(keyPath, "utf8");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function createSignatureHeaderIfRequested(req, payloadString) {
|
|
30
|
+
const expectSignatureHeader = req.headers["expo-expect-signature"];
|
|
31
|
+
console.log(
|
|
32
|
+
`[${new Date().toLocaleTimeString()}] 📋 expo-expect-signature header: ${expectSignatureHeader || "not present"}`,
|
|
33
|
+
);
|
|
34
|
+
if (!expectSignatureHeader) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
console.log(
|
|
39
|
+
`[${new Date().toLocaleTimeString()}] 🔐 Signature requested, loading private key...`,
|
|
40
|
+
);
|
|
41
|
+
const privateKey = await getPrivateKey();
|
|
42
|
+
if (!privateKey) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
"Code signing requested but no key supplied when starting server.",
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const hashSignature = signRSASHA256(payloadString, privateKey);
|
|
49
|
+
console.log(
|
|
50
|
+
`[${new Date().toLocaleTimeString()}] ✍️ Signature generated: ${hashSignature.substring(0, 50)}...`,
|
|
51
|
+
);
|
|
52
|
+
return serializeSignatureHeader(hashSignature, "main");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export { createSignatureHeaderIfRequested };
|