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,275 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import process from "node:process";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
|
|
7
|
+
const ROOT_DIR = path.resolve(
|
|
8
|
+
path.dirname(fileURLToPath(import.meta.url)),
|
|
9
|
+
"../..",
|
|
10
|
+
);
|
|
11
|
+
const CHANNEL_PATTERN = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
|
12
|
+
|
|
13
|
+
function loadEnvFile(filePath) {
|
|
14
|
+
if (!fs.existsSync(filePath)) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
for (const rawLine of fs.readFileSync(filePath, "utf8").split(/\r?\n/)) {
|
|
19
|
+
const line = rawLine.trim();
|
|
20
|
+
if (!line || line.startsWith("#")) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const separator = line.indexOf("=");
|
|
24
|
+
if (separator <= 0) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const key = line.slice(0, separator).trim();
|
|
29
|
+
let value = line.slice(separator + 1).trim();
|
|
30
|
+
if (
|
|
31
|
+
(value.startsWith('"') && value.endsWith('"')) ||
|
|
32
|
+
(value.startsWith("'") && value.endsWith("'"))
|
|
33
|
+
) {
|
|
34
|
+
value = value.slice(1, -1);
|
|
35
|
+
}
|
|
36
|
+
if (process.env[key] === undefined) {
|
|
37
|
+
process.env[key] = value;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
loadEnvFile(path.join(ROOT_DIR, ".env.ota"));
|
|
43
|
+
|
|
44
|
+
function parseJson(value) {
|
|
45
|
+
if (typeof value !== "string") {
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
return JSON.parse(value);
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function readPath(value, paths) {
|
|
57
|
+
for (const keys of paths) {
|
|
58
|
+
let current = value;
|
|
59
|
+
for (const key of keys) {
|
|
60
|
+
if (!current || typeof current !== "object" || !(key in current)) {
|
|
61
|
+
current = null;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
current = current[key];
|
|
65
|
+
}
|
|
66
|
+
if (typeof current === "string" && current.trim()) {
|
|
67
|
+
return current.trim();
|
|
68
|
+
}
|
|
69
|
+
if (typeof current === "number" && Number.isFinite(current)) {
|
|
70
|
+
return current;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function normalizeDate(value, fallbackDate) {
|
|
78
|
+
if (typeof value === "number") {
|
|
79
|
+
const milliseconds = value > 100_000_000_000 ? value : value * 1000;
|
|
80
|
+
return new Date(milliseconds).toISOString();
|
|
81
|
+
}
|
|
82
|
+
if (typeof value === "string" && !Number.isNaN(Date.parse(value))) {
|
|
83
|
+
return new Date(value).toISOString();
|
|
84
|
+
}
|
|
85
|
+
return fallbackDate.toISOString();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function parseEmbeddedManifest(content, fallbackDate = new Date()) {
|
|
89
|
+
let root = parseJson(content);
|
|
90
|
+
root = parseJson(root);
|
|
91
|
+
if (!root || typeof root !== "object") {
|
|
92
|
+
throw new Error("app.manifest does not contain valid JSON");
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const nested = parseJson(root.manifest);
|
|
96
|
+
const manifest = nested && typeof nested === "object" ? nested : root;
|
|
97
|
+
const embeddedUpdateId = readPath(manifest, [
|
|
98
|
+
["id"],
|
|
99
|
+
["updateId"],
|
|
100
|
+
["embeddedUpdateId"],
|
|
101
|
+
["metadata", "updateId"],
|
|
102
|
+
["extra", "expoClient", "id"],
|
|
103
|
+
]);
|
|
104
|
+
const createdAt = readPath(manifest, [
|
|
105
|
+
["createdAt"],
|
|
106
|
+
["commitTime"],
|
|
107
|
+
["publishedTime"],
|
|
108
|
+
["metadata", "createdAt"],
|
|
109
|
+
["extra", "expoClient", "createdAt"],
|
|
110
|
+
]);
|
|
111
|
+
const fallbackId = crypto
|
|
112
|
+
.createHash("sha256")
|
|
113
|
+
.update(content)
|
|
114
|
+
.digest("hex")
|
|
115
|
+
.slice(0, 32);
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
embeddedUpdateId:
|
|
119
|
+
embeddedUpdateId ||
|
|
120
|
+
`${fallbackId.slice(0, 8)}-${fallbackId.slice(8, 12)}-4${fallbackId.slice(13, 16)}-a${fallbackId.slice(17, 20)}-${fallbackId.slice(20)}`,
|
|
121
|
+
createdAt: normalizeDate(createdAt, fallbackDate),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function parseArgs(argv) {
|
|
126
|
+
const args = {};
|
|
127
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
128
|
+
const key = argv[index];
|
|
129
|
+
if (key?.startsWith("--")) {
|
|
130
|
+
args[key.slice(2)] = argv[index + 1];
|
|
131
|
+
index += 1;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return args;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function readChannel(appRoot, explicitChannel) {
|
|
138
|
+
if (explicitChannel) {
|
|
139
|
+
return explicitChannel.trim().toLowerCase();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const appJson = JSON.parse(
|
|
143
|
+
fs.readFileSync(path.join(appRoot, "app.json"), "utf8"),
|
|
144
|
+
);
|
|
145
|
+
return String(
|
|
146
|
+
process.env.EXPO_UPDATE_CHANNEL ||
|
|
147
|
+
appJson.expo?.updates?.requestHeaders?.["expo-channel-name"] ||
|
|
148
|
+
appJson.expo?.extra?.updateChannel ||
|
|
149
|
+
"production",
|
|
150
|
+
)
|
|
151
|
+
.trim()
|
|
152
|
+
.toLowerCase();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function getRegistrationUrl() {
|
|
156
|
+
if (process.env.OTA_EMBEDDED_REGISTER_URL) {
|
|
157
|
+
return process.env.OTA_EMBEDDED_REGISTER_URL;
|
|
158
|
+
}
|
|
159
|
+
if (!process.env.OTA_SERVER_URL) {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const url = new URL(process.env.OTA_SERVER_URL);
|
|
164
|
+
url.pathname = `${url.pathname.replace(/\/api\/manifest\/?$/, "").replace(/\/+$/, "")}/api/admin/embedded-updates`;
|
|
165
|
+
url.search = "";
|
|
166
|
+
url.hash = "";
|
|
167
|
+
return url.toString();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function registerWithHttp(input) {
|
|
171
|
+
const url = getRegistrationUrl();
|
|
172
|
+
const accessToken = process.env.OTA_ACCESS_TOKEN;
|
|
173
|
+
if (!url || !accessToken) {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const response = await fetch(url, {
|
|
178
|
+
method: "POST",
|
|
179
|
+
headers: {
|
|
180
|
+
authorization: `Bearer ${accessToken}`,
|
|
181
|
+
"content-type": "application/json",
|
|
182
|
+
},
|
|
183
|
+
body: JSON.stringify(input),
|
|
184
|
+
});
|
|
185
|
+
if (!response.ok) {
|
|
186
|
+
throw new Error(
|
|
187
|
+
`Embedded registration failed: HTTP ${response.status} ${await response.text()}`,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async function registerWithDatabase(input) {
|
|
194
|
+
const connectionString = process.env.OTA_DATABASE_URL;
|
|
195
|
+
if (!connectionString) {
|
|
196
|
+
throw new Error(
|
|
197
|
+
"Set OTA_ACCESS_TOKEN with OTA_SERVER_URL, or set OTA_DATABASE_URL.",
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const { default: pg } = await import("pg");
|
|
202
|
+
const client = new pg.Client({
|
|
203
|
+
connectionString,
|
|
204
|
+
ssl:
|
|
205
|
+
process.env.OTA_DATABASE_SSL === "true"
|
|
206
|
+
? { rejectUnauthorized: false }
|
|
207
|
+
: false,
|
|
208
|
+
});
|
|
209
|
+
await client.connect();
|
|
210
|
+
try {
|
|
211
|
+
await client.query(
|
|
212
|
+
`INSERT INTO ota_embedded_updates (
|
|
213
|
+
embedded_update_id, created_at, channel, platform, is_embedded
|
|
214
|
+
) VALUES ($1, $2, $3, $4, true)
|
|
215
|
+
ON CONFLICT (embedded_update_id) DO UPDATE SET
|
|
216
|
+
created_at = EXCLUDED.created_at,
|
|
217
|
+
channel = EXCLUDED.channel,
|
|
218
|
+
platform = EXCLUDED.platform,
|
|
219
|
+
is_embedded = true,
|
|
220
|
+
modified_at = now()`,
|
|
221
|
+
[
|
|
222
|
+
input.embeddedUpdateId,
|
|
223
|
+
input.createdAt,
|
|
224
|
+
input.channel,
|
|
225
|
+
input.platform,
|
|
226
|
+
],
|
|
227
|
+
);
|
|
228
|
+
} finally {
|
|
229
|
+
await client.end();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export async function registerEmbeddedUpdate(input) {
|
|
234
|
+
if (!CHANNEL_PATTERN.test(input.channel)) {
|
|
235
|
+
throw new Error("Invalid Expo update channel");
|
|
236
|
+
}
|
|
237
|
+
if (input.platform !== "ios" && input.platform !== "android") {
|
|
238
|
+
throw new Error("--platform must be ios or android");
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (!(await registerWithHttp(input))) {
|
|
242
|
+
await registerWithDatabase(input);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async function main() {
|
|
247
|
+
const args = parseArgs(process.argv.slice(2));
|
|
248
|
+
if (!args.manifest) {
|
|
249
|
+
throw new Error("--manifest is required");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const manifestPath = path.resolve(args.manifest);
|
|
253
|
+
const channel = readChannel(ROOT_DIR, args.channel);
|
|
254
|
+
const fields = parseEmbeddedManifest(
|
|
255
|
+
fs.readFileSync(manifestPath, "utf8"),
|
|
256
|
+
fs.statSync(manifestPath).mtime,
|
|
257
|
+
);
|
|
258
|
+
const input = {
|
|
259
|
+
...fields,
|
|
260
|
+
channel,
|
|
261
|
+
platform: String(args.platform || "").toLowerCase(),
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
await registerEmbeddedUpdate(input);
|
|
265
|
+
console.log(
|
|
266
|
+
`Registered embedded update ${input.embeddedUpdateId} (${input.platform}, ${input.channel}).`,
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
271
|
+
main().catch((error) => {
|
|
272
|
+
console.error(`Embedded update registration failed: ${error.message}`);
|
|
273
|
+
process.exitCode = 1;
|
|
274
|
+
});
|
|
275
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2017",
|
|
4
|
+
"lib": ["dom", "dom.iterable", "esnext"],
|
|
5
|
+
"allowJs": true,
|
|
6
|
+
"forceConsistentCasingInFileNames": true,
|
|
7
|
+
"skipLibCheck": true,
|
|
8
|
+
"strict": true,
|
|
9
|
+
"noImplicitAny": false,
|
|
10
|
+
"noEmit": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"module": "esnext",
|
|
13
|
+
"moduleResolution": "bundler",
|
|
14
|
+
"resolveJsonModule": true,
|
|
15
|
+
"isolatedModules": true,
|
|
16
|
+
"jsx": "react-jsx",
|
|
17
|
+
"incremental": true,
|
|
18
|
+
"plugins": [
|
|
19
|
+
{
|
|
20
|
+
"name": "next"
|
|
21
|
+
}
|
|
22
|
+
],
|
|
23
|
+
"paths": {
|
|
24
|
+
"@/*": ["./*"]
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"include": [
|
|
28
|
+
"next-env.d.ts",
|
|
29
|
+
"**/*.ts",
|
|
30
|
+
"**/*.tsx",
|
|
31
|
+
".next/types/**/*.ts",
|
|
32
|
+
".next/dev/types/**/*.ts",
|
|
33
|
+
"**/*.mts"
|
|
34
|
+
],
|
|
35
|
+
"exclude": ["node_modules"]
|
|
36
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { configDefaults, defineConfig } from "vitest/config";
|
|
4
|
+
|
|
5
|
+
const rootDir = path.dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
|
|
7
|
+
export default defineConfig({
|
|
8
|
+
resolve: {
|
|
9
|
+
alias: {
|
|
10
|
+
"@": rootDir,
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
test: {
|
|
14
|
+
environment: "node",
|
|
15
|
+
exclude: [
|
|
16
|
+
...configDefaults.exclude,
|
|
17
|
+
"packages/create-expo-updates-server/**",
|
|
18
|
+
],
|
|
19
|
+
},
|
|
20
|
+
});
|