create-expo-updates-server 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -0
  3. package/bin/create-expo-updates-server.mjs +5 -0
  4. package/dist/template/.env.docker.example +15 -0
  5. package/dist/template/.env.example +35 -0
  6. package/dist/template/Dockerfile +42 -0
  7. package/dist/template/LICENSE +21 -0
  8. package/dist/template/README.md +665 -0
  9. package/dist/template/app/api/admin/access-tokens/[id]/route.ts +25 -0
  10. package/dist/template/app/api/admin/access-tokens/route.test.ts +73 -0
  11. package/dist/template/app/api/admin/access-tokens/route.ts +90 -0
  12. package/dist/template/app/api/admin/embedded-updates/[embeddedUpdateId]/route.ts +25 -0
  13. package/dist/template/app/api/admin/embedded-updates/route.test.ts +102 -0
  14. package/dist/template/app/api/admin/embedded-updates/route.ts +47 -0
  15. package/dist/template/app/api/admin/emergency-redirects/[id]/route.ts +62 -0
  16. package/dist/template/app/api/admin/emergency-redirects/route.test.ts +84 -0
  17. package/dist/template/app/api/admin/emergency-redirects/route.ts +56 -0
  18. package/dist/template/app/api/admin/login/route.test.ts +55 -0
  19. package/dist/template/app/api/admin/login/route.ts +52 -0
  20. package/dist/template/app/api/admin/logout/route.ts +20 -0
  21. package/dist/template/app/api/admin/session/route.ts +15 -0
  22. package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +33 -0
  23. package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +29 -0
  24. package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +29 -0
  25. package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +96 -0
  26. package/dist/template/app/api/admin/updates/route.test.ts +142 -0
  27. package/dist/template/app/api/admin/updates/route.ts +32 -0
  28. package/dist/template/app/api/assets/route.ts +6 -0
  29. package/dist/template/app/api/health/route.test.ts +26 -0
  30. package/dist/template/app/api/health/route.ts +42 -0
  31. package/dist/template/app/api/manifest/route.ts +6 -0
  32. package/dist/template/app/api/update-complete/route.ts +6 -0
  33. package/dist/template/app/favicon.ico +0 -0
  34. package/dist/template/app/globals.css +26 -0
  35. package/dist/template/app/layout.tsx +34 -0
  36. package/dist/template/app/login/page.tsx +77 -0
  37. package/dist/template/app/page.tsx +12 -0
  38. package/dist/template/app/updates/[encodedKey]/page.tsx +482 -0
  39. package/dist/template/app/updates/embedded/page.tsx +164 -0
  40. package/dist/template/app/updates/emergency-redirects/page.tsx +430 -0
  41. package/dist/template/app/updates/layout.tsx +14 -0
  42. package/dist/template/app/updates/page.tsx +281 -0
  43. package/dist/template/docker-compose.yml +100 -0
  44. package/dist/template/docs/expo-app-setup.md +497 -0
  45. package/dist/template/docs/getting-started.md +502 -0
  46. package/dist/template/docs/getting-started.ru.md +623 -0
  47. package/dist/template/docs/index.md +57 -0
  48. package/dist/template/docs/migrations/schema.sql +363 -0
  49. package/dist/template/docs/npm-installer.md +83 -0
  50. package/dist/template/docs/operations-guide.md +270 -0
  51. package/dist/template/docs/ota-emergency-recovery.md +109 -0
  52. package/dist/template/docs/supabase-setup.md +133 -0
  53. package/dist/template/eslint.config.mjs +30 -0
  54. package/dist/template/gitignore.template +58 -0
  55. package/dist/template/next.config.ts +7 -0
  56. package/dist/template/package-lock.json +12156 -0
  57. package/dist/template/package.json +50 -0
  58. package/dist/template/postcss.config.mjs +7 -0
  59. package/dist/template/public/file.svg +1 -0
  60. package/dist/template/public/globe.svg +1 -0
  61. package/dist/template/public/next.svg +1 -0
  62. package/dist/template/public/vercel.svg +1 -0
  63. package/dist/template/public/window.svg +1 -0
  64. package/dist/template/scripts/configure-expo-app.mjs +145 -0
  65. package/dist/template/scripts/create-access-token.mjs +135 -0
  66. package/dist/template/scripts/create-admin.mjs +169 -0
  67. package/dist/template/scripts/import-emergency-redirects.mjs +126 -0
  68. package/dist/template/scripts/lib/embedded-update-registrar.test.ts +64 -0
  69. package/dist/template/scripts/lib/expo-app-configurator.mjs +666 -0
  70. package/dist/template/scripts/lib/expo-app-configurator.test.ts +463 -0
  71. package/dist/template/scripts/lib/expo-app-prompts.mjs +139 -0
  72. package/dist/template/scripts/lib/expo-app-prompts.test.ts +97 -0
  73. package/dist/template/scripts/lib/expo-export-command.test.ts +31 -0
  74. package/dist/template/scripts/lib/ota-publish-retry.test.ts +33 -0
  75. package/dist/template/scripts/manage-emergency-redirect.mjs +483 -0
  76. package/dist/template/scripts/sync-docker-database-password.sh +16 -0
  77. package/dist/template/src/client/admin-api.ts +174 -0
  78. package/dist/template/src/client/admin-types.ts +93 -0
  79. package/dist/template/src/client/logout-button.tsx +31 -0
  80. package/dist/template/src/server/controllers/assets.ts +216 -0
  81. package/dist/template/src/server/controllers/manifest.test.ts +403 -0
  82. package/dist/template/src/server/controllers/manifest.ts +626 -0
  83. package/dist/template/src/server/controllers/update-complete.ts +36 -0
  84. package/dist/template/src/server/lib/admin-access-tokens.ts +169 -0
  85. package/dist/template/src/server/lib/admin-api.ts +101 -0
  86. package/dist/template/src/server/lib/admin-auth.ts +208 -0
  87. package/dist/template/src/server/lib/admin-embedded-updates.ts +150 -0
  88. package/dist/template/src/server/lib/admin-updates.ts +1282 -0
  89. package/dist/template/src/server/lib/common.js +24 -0
  90. package/dist/template/src/server/lib/emergency-channel-redirects.ts +310 -0
  91. package/dist/template/src/server/lib/env.js +8 -0
  92. package/dist/template/src/server/lib/manifest-helpers.js +91 -0
  93. package/dist/template/src/server/lib/response.js +11 -0
  94. package/dist/template/src/server/lib/signing.js +55 -0
  95. package/dist/template/src/server/lib/supabase.js +763 -0
  96. package/dist/template/src/server/manifest/manifest-repository.ts +269 -0
  97. package/dist/template/src/server/manifest/manifest-request.ts +21 -0
  98. package/dist/template/src/server/manifest/manifest-response.ts +235 -0
  99. package/dist/template/src/server/manifest/manifest-values.ts +62 -0
  100. package/dist/template/templates/expo-app/.env.ota.example +18 -0
  101. package/dist/template/templates/expo-app/ci_scripts/ci_post_xcodebuild.sh +6 -0
  102. package/dist/template/templates/expo-app/ci_scripts/register-embedded-update.sh +36 -0
  103. package/dist/template/templates/expo-app/plugins/with-ota-embedded-registration.js +33 -0
  104. package/dist/template/templates/expo-app/scripts/ota-publish/api/database-client.mjs +87 -0
  105. package/dist/template/templates/expo-app/scripts/ota-publish/api/storage-client.mjs +57 -0
  106. package/dist/template/templates/expo-app/scripts/ota-publish/index.mjs +182 -0
  107. package/dist/template/templates/expo-app/scripts/ota-publish/lib/common.mjs +84 -0
  108. package/dist/template/templates/expo-app/scripts/ota-publish/lib/expo-export-command.mjs +24 -0
  109. package/dist/template/templates/expo-app/scripts/ota-publish/lib/retry.mjs +25 -0
  110. package/dist/template/templates/expo-app/scripts/ota-publish/shared/config.mjs +48 -0
  111. package/dist/template/templates/expo-app/scripts/ota-register-embedded/android-register-embedded-update.sh +18 -0
  112. package/dist/template/templates/expo-app/scripts/ota-register-embedded/index.mjs +275 -0
  113. package/dist/template/tsconfig.json +36 -0
  114. package/dist/template/vitest.config.mts +20 -0
  115. package/dist/template-manifest.json +564 -0
  116. package/lib/cli.mjs +549 -0
  117. package/package.json +41 -0
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "expo-updates-server",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "license": "MIT",
6
+ "engines": {
7
+ "node": ">=20.9"
8
+ },
9
+ "scripts": {
10
+ "dev": "next dev",
11
+ "build": "next build",
12
+ "start": "next start",
13
+ "lint": "eslint",
14
+ "docs:dev": "vitepress dev docs",
15
+ "docs:build": "vitepress build docs",
16
+ "docs:preview": "vitepress preview docs",
17
+ "emergency:import": "node scripts/import-emergency-redirects.mjs",
18
+ "docker:sync-db-password": "sh scripts/sync-docker-database-password.sh",
19
+ "configure-app": "node scripts/configure-expo-app.mjs",
20
+ "test": "vitest run",
21
+ "test:watch": "vitest",
22
+ "create-admin": "node scripts/create-admin.mjs",
23
+ "create-access-token": "node scripts/create-access-token.mjs",
24
+ "restart-docker": "docker compose up -d --force-recreate app"
25
+ },
26
+ "dependencies": {
27
+ "@aws-sdk/client-s3": "^3.888.0",
28
+ "@aws-sdk/s3-request-presigner": "^3.888.0",
29
+ "@supabase/supabase-js": "^2.57.4",
30
+ "@types/form-data": "^2.2.1",
31
+ "bcryptjs": "^3.0.3",
32
+ "form-data": "^4.0.5",
33
+ "next": "16.1.6",
34
+ "pg": "^8.16.3",
35
+ "react": "19.2.3",
36
+ "react-dom": "19.2.3"
37
+ },
38
+ "devDependencies": {
39
+ "@tailwindcss/postcss": "^4",
40
+ "@types/node": "^20",
41
+ "@types/react": "^19",
42
+ "@types/react-dom": "^19",
43
+ "eslint": "^9",
44
+ "eslint-config-next": "16.1.6",
45
+ "tailwindcss": "^4",
46
+ "typescript": "^5",
47
+ "vitepress": "^1.6.4",
48
+ "vitest": "^4.1.11"
49
+ }
50
+ }
@@ -0,0 +1,7 @@
1
+ const config = {
2
+ plugins: {
3
+ "@tailwindcss/postcss": {},
4
+ },
5
+ };
6
+
7
+ export default config;
@@ -0,0 +1 @@
1
+ <svg fill="none" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M14.5 13.5V5.41a1 1 0 0 0-.3-.7L9.8.29A1 1 0 0 0 9.08 0H1.5v13.5A2.5 2.5 0 0 0 4 16h8a2.5 2.5 0 0 0 2.5-2.5m-1.5 0v-7H8v-5H3v12a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1M9.5 5V2.12L12.38 5zM5.13 5h-.62v1.25h2.12V5zm-.62 3h7.12v1.25H4.5zm.62 3h-.62v1.25h7.12V11z" clip-rule="evenodd" fill="#666" fill-rule="evenodd"/></svg>
@@ -0,0 +1 @@
1
+ <svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><g clip-path="url(#a)"><path fill-rule="evenodd" clip-rule="evenodd" d="M10.27 14.1a6.5 6.5 0 0 0 3.67-3.45q-1.24.21-2.7.34-.31 1.83-.97 3.1M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16m.48-1.52a7 7 0 0 1-.96 0H7.5a4 4 0 0 1-.84-1.32q-.38-.89-.63-2.08a40 40 0 0 0 3.92 0q-.25 1.2-.63 2.08a4 4 0 0 1-.84 1.31zm2.94-4.76q1.66-.15 2.95-.43a7 7 0 0 0 0-2.58q-1.3-.27-2.95-.43a18 18 0 0 1 0 3.44m-1.27-3.54a17 17 0 0 1 0 3.64 39 39 0 0 1-4.3 0 17 17 0 0 1 0-3.64 39 39 0 0 1 4.3 0m1.1-1.17q1.45.13 2.69.34a6.5 6.5 0 0 0-3.67-3.44q.65 1.26.98 3.1M8.48 1.5l.01.02q.41.37.84 1.31.38.89.63 2.08a40 40 0 0 0-3.92 0q.25-1.2.63-2.08a4 4 0 0 1 .85-1.32 7 7 0 0 1 .96 0m-2.75.4a6.5 6.5 0 0 0-3.67 3.44 29 29 0 0 1 2.7-.34q.31-1.83.97-3.1M4.58 6.28q-1.66.16-2.95.43a7 7 0 0 0 0 2.58q1.3.27 2.95.43a18 18 0 0 1 0-3.44m.17 4.71q-1.45-.12-2.69-.34a6.5 6.5 0 0 0 3.67 3.44q-.65-1.27-.98-3.1" fill="#666"/></g><defs><clipPath id="a"><path fill="#fff" d="M0 0h16v16H0z"/></clipPath></defs></svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 394 80"><path fill="#000" d="M262 0h68.5v12.7h-27.2v66.6h-13.6V12.7H262V0ZM149 0v12.7H94v20.4h44.3v12.6H94v21h55v12.6H80.5V0h68.7zm34.3 0h-17.8l63.8 79.4h17.9l-32-39.7 32-39.6h-17.9l-23 28.6-23-28.6zm18.3 56.7-9-11-27.1 33.7h17.8l18.3-22.7z"/><path fill="#000" d="M81 79.3 17 0H0v79.3h13.6V17l50.2 62.3H81Zm252.6-.4c-1 0-1.8-.4-2.5-1s-1.1-1.6-1.1-2.6.3-1.8 1-2.5 1.6-1 2.6-1 1.8.3 2.5 1a3.4 3.4 0 0 1 .6 4.3 3.7 3.7 0 0 1-3 1.8zm23.2-33.5h6v23.3c0 2.1-.4 4-1.3 5.5a9.1 9.1 0 0 1-3.8 3.5c-1.6.8-3.5 1.3-5.7 1.3-2 0-3.7-.4-5.3-1s-2.8-1.8-3.7-3.2c-.9-1.3-1.4-3-1.4-5h6c.1.8.3 1.6.7 2.2s1 1.2 1.6 1.5c.7.4 1.5.5 2.4.5 1 0 1.8-.2 2.4-.6a4 4 0 0 0 1.6-1.8c.3-.8.5-1.8.5-3V45.5zm30.9 9.1a4.4 4.4 0 0 0-2-3.3 7.5 7.5 0 0 0-4.3-1.1c-1.3 0-2.4.2-3.3.5-.9.4-1.6 1-2 1.6a3.5 3.5 0 0 0-.3 4c.3.5.7.9 1.3 1.2l1.8 1 2 .5 3.2.8c1.3.3 2.5.7 3.7 1.2a13 13 0 0 1 3.2 1.8 8.1 8.1 0 0 1 3 6.5c0 2-.5 3.7-1.5 5.1a10 10 0 0 1-4.4 3.5c-1.8.8-4.1 1.2-6.8 1.2-2.6 0-4.9-.4-6.8-1.2-2-.8-3.4-2-4.5-3.5a10 10 0 0 1-1.7-5.6h6a5 5 0 0 0 3.5 4.6c1 .4 2.2.6 3.4.6 1.3 0 2.5-.2 3.5-.6 1-.4 1.8-1 2.4-1.7a4 4 0 0 0 .8-2.4c0-.9-.2-1.6-.7-2.2a11 11 0 0 0-2.1-1.4l-3.2-1-3.8-1c-2.8-.7-5-1.7-6.6-3.2a7.2 7.2 0 0 1-2.4-5.7 8 8 0 0 1 1.7-5 10 10 0 0 1 4.3-3.5c2-.8 4-1.2 6.4-1.2 2.3 0 4.4.4 6.2 1.2 1.8.8 3.2 2 4.3 3.4 1 1.4 1.5 3 1.5 5h-5.8z"/></svg>
@@ -0,0 +1 @@
1
+ <svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1155 1000"><path d="m577.3 0 577.4 1000H0z" fill="#fff"/></svg>
@@ -0,0 +1 @@
1
+ <svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><path fill-rule="evenodd" clip-rule="evenodd" d="M1.5 2.5h13v10a1 1 0 0 1-1 1h-11a1 1 0 0 1-1-1zM0 1h16v11.5a2.5 2.5 0 0 1-2.5 2.5h-11A2.5 2.5 0 0 1 0 12.5zm3.75 4.5a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5M7 4.75a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0m1.75.75a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5" fill="#666"/></svg>
@@ -0,0 +1,145 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { createInterface } from "node:readline/promises";
4
+ import { configureExpoApp } from "./lib/expo-app-configurator.mjs";
5
+ import { collectInteractiveOptions } from "./lib/expo-app-prompts.mjs";
6
+
7
+ function printHelp() {
8
+ console.log(`
9
+ Configure an Expo application for this OTA server.
10
+
11
+ Interactive usage:
12
+ npm run configure-app
13
+
14
+ Usage:
15
+ npm run configure-app -- \\
16
+ --app /path/to/expo-app \\
17
+ --certificate /path/to/code-signing-certificate.pem \\
18
+ --server-url https://updates.example.com \\
19
+ --channel production \\
20
+ --runtime-version 1.0.0
21
+
22
+ Required:
23
+ --app Expo application root containing app.json
24
+ --certificate Public PEM code-signing certificate
25
+ --server-url OTA server base URL or /api/manifest URL
26
+ --channel Expo update channel
27
+ --runtime-version Native runtime version
28
+
29
+ Optional:
30
+ --platform all, ios or android (default: all)
31
+ --force Replace generated publisher/certificate files
32
+ --help Show this message
33
+ `);
34
+ }
35
+
36
+ function parseArgs(argv) {
37
+ const options = {
38
+ platform: "all",
39
+ force: false,
40
+ };
41
+
42
+ for (let index = 0; index < argv.length; index += 1) {
43
+ const argument = argv[index];
44
+
45
+ if (argument === "--help") {
46
+ return { help: true };
47
+ }
48
+ if (argument === "--force") {
49
+ options.force = true;
50
+ continue;
51
+ }
52
+ if (!argument.startsWith("--")) {
53
+ throw new Error(`Unexpected argument: ${argument}`);
54
+ }
55
+
56
+ const key = argument.slice(2);
57
+ const value = argv[index + 1];
58
+ if (!value || value.startsWith("--")) {
59
+ throw new Error(`Missing value for ${argument}`);
60
+ }
61
+ index += 1;
62
+
63
+ if (key === "app") options.app = value;
64
+ else if (key === "certificate") options.certificate = value;
65
+ else if (key === "server-url") options.serverUrl = value;
66
+ else if (key === "channel") options.channel = value;
67
+ else if (key === "runtime-version") options.runtimeVersion = value;
68
+ else if (key === "platform") options.platform = value;
69
+ else throw new Error(`Unknown option: ${argument}`);
70
+ }
71
+
72
+ return options;
73
+ }
74
+
75
+ try {
76
+ const argv = process.argv.slice(2);
77
+ let options = parseArgs(argv);
78
+ if (options.help) {
79
+ printHelp();
80
+ process.exit(0);
81
+ }
82
+
83
+ const requiredValues = [
84
+ options.app,
85
+ options.certificate,
86
+ options.serverUrl,
87
+ options.channel,
88
+ options.runtimeVersion,
89
+ ];
90
+
91
+ if (requiredValues.some((value) => !value)) {
92
+ if (!process.stdin.isTTY || !process.stdout.isTTY) {
93
+ printHelp();
94
+ throw new Error(
95
+ "Missing required options and interactive input is unavailable.",
96
+ );
97
+ }
98
+
99
+ const readline = createInterface({
100
+ input: process.stdin,
101
+ output: process.stdout,
102
+ });
103
+ try {
104
+ options = await collectInteractiveOptions(options, {
105
+ ask: (question) => readline.question(question),
106
+ serverRoot: process.cwd(),
107
+ });
108
+ } finally {
109
+ readline.close();
110
+ }
111
+ }
112
+
113
+ const result = configureExpoApp(options);
114
+ console.log("\nExpo OTA configuration completed.");
115
+ console.log(`Application: ${result.appRoot}`);
116
+ console.log(`Manifest: ${result.manifestUrl}`);
117
+ console.log(`Channel: ${result.channel}`);
118
+ console.log(`Runtime: ${result.runtimeVersion}`);
119
+ console.log(`Platform: ${result.platform}`);
120
+ console.log(
121
+ `Publisher environment: ${
122
+ result.publisherEnvConfigured
123
+ ? "configured from local Docker .env"
124
+ : "fill .env.ota.example manually"
125
+ }`,
126
+ );
127
+ console.log("\nNext:");
128
+ if (!result.publisherEnvConfigured) {
129
+ console.log(
130
+ "1. Copy .env.ota.example to .env.ota and add publisher secrets.",
131
+ );
132
+ }
133
+ console.log(
134
+ `${result.publisherEnvConfigured ? "1" : "2"}. Publish with: npm run ota:publish -- --message "Update description"`,
135
+ );
136
+ console.log(
137
+ `${result.publisherEnvConfigured ? "2" : "3"}. Activate the update in the OTA admin panel.`,
138
+ );
139
+ console.log(
140
+ `${result.publisherEnvConfigured ? "3" : "4"}. Build Android with npm run ota:build:android:apk or build iOS in Xcode.`,
141
+ );
142
+ } catch (error) {
143
+ console.error(`Expo OTA configuration failed: ${error.message}`);
144
+ process.exit(1);
145
+ }
@@ -0,0 +1,135 @@
1
+ import { createHash, randomBytes, randomUUID } from "node:crypto";
2
+ import { stdin, stdout } from "node:process";
3
+ import { createInterface } from "node:readline/promises";
4
+ import { createClient } from "@supabase/supabase-js";
5
+ import { Pool } from "pg";
6
+
7
+ try {
8
+ process.loadEnvFile?.(".env.local");
9
+ } catch {
10
+ // Environment may already be provided by Docker or the deployment platform.
11
+ }
12
+
13
+ const SUPPORTED_SCOPES = new Set([
14
+ "updates:read",
15
+ "updates:write",
16
+ "redirects:read",
17
+ "redirects:write",
18
+ ]);
19
+
20
+ function parseScopes(value) {
21
+ const scopes = value
22
+ .split(",")
23
+ .map((scope) => scope.trim())
24
+ .filter(Boolean);
25
+ if (
26
+ scopes.length === 0 ||
27
+ scopes.some((scope) => !SUPPORTED_SCOPES.has(scope))
28
+ ) {
29
+ throw new Error(
30
+ "Scopes must be a comma-separated list of supported values.",
31
+ );
32
+ }
33
+ return [...new Set(scopes)];
34
+ }
35
+
36
+ async function insertToken(row) {
37
+ const provider = (
38
+ process.env.DATABASE_PROVIDER ||
39
+ process.env.DB_PROVIDER ||
40
+ "pg"
41
+ ).toLowerCase();
42
+
43
+ if (provider === "supabase") {
44
+ const url = process.env.SUPABASE_PROJECT_URL || process.env.SUPABASE_URL;
45
+ const key = process.env.SUPABASE_SERVICE_ROLE_KEY;
46
+ if (!url || !key) {
47
+ throw new Error(
48
+ "SUPABASE_PROJECT_URL and SUPABASE_SERVICE_ROLE_KEY are required.",
49
+ );
50
+ }
51
+ const client = createClient(url, key, {
52
+ auth: { persistSession: false, autoRefreshToken: false },
53
+ });
54
+ const { error } = await client.from("ota_access_tokens").insert(row);
55
+ if (error) {
56
+ throw new Error(error.message);
57
+ }
58
+ return;
59
+ }
60
+
61
+ if (!process.env.DATABASE_URL) {
62
+ throw new Error("DATABASE_URL is required.");
63
+ }
64
+ const sslEnabled = ["true", "1", "require"].includes(
65
+ (process.env.DATABASE_SSL || "false").toLowerCase(),
66
+ );
67
+ const pool = new Pool({
68
+ connectionString: process.env.DATABASE_URL,
69
+ ssl: sslEnabled ? { rejectUnauthorized: false } : false,
70
+ });
71
+ try {
72
+ await pool.query(
73
+ `INSERT INTO ota_access_tokens (
74
+ id, name, token_prefix, token_hash, scopes, expires_at
75
+ ) VALUES ($1, $2, $3, $4, $5, $6)`,
76
+ [
77
+ row.id,
78
+ row.name,
79
+ row.token_prefix,
80
+ row.token_hash,
81
+ JSON.stringify(row.scopes),
82
+ row.expires_at,
83
+ ],
84
+ );
85
+ } finally {
86
+ await pool.end();
87
+ }
88
+ }
89
+
90
+ async function main() {
91
+ const terminal = createInterface({ input: stdin, output: stdout });
92
+ try {
93
+ const name = (await terminal.question("Token name [mobile-builds]: "))
94
+ .trim() || "mobile-builds";
95
+ if (name.length > 100) {
96
+ throw new Error("Token name must contain at most 100 characters.");
97
+ }
98
+
99
+ const scopes = parseScopes(
100
+ (await terminal.question("Scopes [updates:write]: ")).trim() ||
101
+ "updates:write",
102
+ );
103
+ const expiresInput = (
104
+ await terminal.question(
105
+ "Expiration as ISO date, or leave empty for no expiration: ",
106
+ )
107
+ ).trim();
108
+ const expiresAt = expiresInput
109
+ ? new Date(expiresInput).toISOString()
110
+ : null;
111
+ if (expiresAt && Date.parse(expiresAt) <= Date.now()) {
112
+ throw new Error("Expiration must be in the future.");
113
+ }
114
+
115
+ const token = `ota_${randomBytes(32).toString("base64url")}`;
116
+ await insertToken({
117
+ id: randomUUID(),
118
+ name,
119
+ token_prefix: token.slice(0, 12),
120
+ token_hash: createHash("sha256").update(token, "utf8").digest("hex"),
121
+ scopes,
122
+ expires_at: expiresAt,
123
+ });
124
+
125
+ console.log("\nAccess token created. Copy it now; it is shown only once:");
126
+ console.log(token);
127
+ } finally {
128
+ terminal.close();
129
+ }
130
+ }
131
+
132
+ main().catch((error) => {
133
+ console.error(error instanceof Error ? error.message : String(error));
134
+ process.exitCode = 1;
135
+ });
@@ -0,0 +1,169 @@
1
+ import { hash } from "bcryptjs";
2
+ import { Pool } from "pg";
3
+ import { createClient } from "@supabase/supabase-js";
4
+ import { stdin, stdout } from "node:process";
5
+ import { createInterface } from "node:readline/promises";
6
+
7
+ try {
8
+ process.loadEnvFile?.(".env.local");
9
+ } catch {
10
+ // Environment may already be provided by the shell or deployment platform.
11
+ }
12
+
13
+ const USERNAME_PATTERN = /^[a-z0-9._-]{3,64}$/;
14
+ const ROLES = new Set(["admin", "operator", "viewer"]);
15
+
16
+ function normalizeUsername(value) {
17
+ return value.trim().toLowerCase();
18
+ }
19
+
20
+ async function readHidden(question) {
21
+ if (!stdin.isTTY || typeof stdin.setRawMode !== "function") {
22
+ throw new Error("Password input requires an interactive terminal.");
23
+ }
24
+
25
+ stdout.write(question);
26
+ stdin.setRawMode(true);
27
+ stdin.resume();
28
+ stdin.setEncoding("utf8");
29
+
30
+ return new Promise((resolve, reject) => {
31
+ let value = "";
32
+
33
+ const cleanup = () => {
34
+ stdin.off("data", onData);
35
+ stdin.setRawMode(false);
36
+ stdin.pause();
37
+ stdout.write("\n");
38
+ };
39
+
40
+ const onData = (character) => {
41
+ if (character === "\u0003") {
42
+ cleanup();
43
+ reject(new Error("Cancelled."));
44
+ return;
45
+ }
46
+
47
+ if (character === "\r" || character === "\n") {
48
+ cleanup();
49
+ resolve(value);
50
+ return;
51
+ }
52
+
53
+ if (character === "\u007f") {
54
+ if (value.length > 0) {
55
+ value = value.slice(0, -1);
56
+ stdout.write("\b \b");
57
+ }
58
+ return;
59
+ }
60
+
61
+ value += character;
62
+ stdout.write("*");
63
+ };
64
+
65
+ stdin.on("data", onData);
66
+ });
67
+ }
68
+
69
+ async function main() {
70
+ const provider = (
71
+ process.env.DATABASE_PROVIDER ||
72
+ process.env.DB_PROVIDER ||
73
+ "pg"
74
+ ).toLowerCase();
75
+ const databaseUrl = process.env.DATABASE_URL;
76
+ if (provider !== "supabase" && !databaseUrl) {
77
+ throw new Error("DATABASE_URL is required.");
78
+ }
79
+
80
+ const terminal = createInterface({ input: stdin, output: stdout });
81
+ try {
82
+ const username = normalizeUsername(
83
+ await terminal.question("Username: "),
84
+ );
85
+ if (!USERNAME_PATTERN.test(username)) {
86
+ throw new Error(
87
+ "Username must contain 3-64 lowercase letters, numbers, dots, underscores or dashes.",
88
+ );
89
+ }
90
+
91
+ const roleInput = (
92
+ await terminal.question("Role [admin]: ")
93
+ ).trim().toLowerCase();
94
+ const role = roleInput || "admin";
95
+ if (!ROLES.has(role)) {
96
+ throw new Error("Role must be admin, operator or viewer.");
97
+ }
98
+
99
+ terminal.pause();
100
+ const password = await readHidden("Password: ");
101
+ const confirmation = await readHidden("Confirm password: ");
102
+ if (password !== confirmation) {
103
+ throw new Error("Passwords do not match.");
104
+ }
105
+ if (password.length < 12) {
106
+ throw new Error("Password must contain at least 12 characters.");
107
+ }
108
+
109
+ const passwordHash = await hash(password, 12);
110
+ if (provider === "supabase") {
111
+ const url = process.env.SUPABASE_PROJECT_URL || process.env.SUPABASE_URL;
112
+ const key = process.env.SUPABASE_SERVICE_ROLE_KEY;
113
+ if (!url || !key) {
114
+ throw new Error(
115
+ "SUPABASE_PROJECT_URL and SUPABASE_SERVICE_ROLE_KEY are required.",
116
+ );
117
+ }
118
+
119
+ const client = createClient(url, key, {
120
+ auth: { persistSession: false, autoRefreshToken: false },
121
+ });
122
+ const { error } = await client.from("ota_admin_users").upsert(
123
+ {
124
+ username,
125
+ password_hash: passwordHash,
126
+ role,
127
+ is_active: true,
128
+ },
129
+ { onConflict: "username" },
130
+ );
131
+ if (error) {
132
+ throw new Error(error.message);
133
+ }
134
+ } else {
135
+ const sslEnabled = ["true", "1", "require"].includes(
136
+ (process.env.DATABASE_SSL || "false").toLowerCase(),
137
+ );
138
+ const pool = new Pool({
139
+ connectionString: databaseUrl,
140
+ ssl: sslEnabled ? { rejectUnauthorized: false } : false,
141
+ });
142
+
143
+ try {
144
+ await pool.query(
145
+ `insert into public.ota_admin_users
146
+ (username, password_hash, role, is_active)
147
+ values ($1, $2, $3, true)
148
+ on conflict (username) do update
149
+ set password_hash = excluded.password_hash,
150
+ role = excluded.role,
151
+ is_active = true,
152
+ modified_at = now()`,
153
+ [username, passwordHash, role],
154
+ );
155
+ } finally {
156
+ await pool.end();
157
+ }
158
+ }
159
+
160
+ console.log(`User "${username}" is ready with role "${role}".`);
161
+ } finally {
162
+ terminal.close();
163
+ }
164
+ }
165
+
166
+ main().catch((error) => {
167
+ console.error(error instanceof Error ? error.message : String(error));
168
+ process.exitCode = 1;
169
+ });
@@ -0,0 +1,126 @@
1
+ import fs from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { Pool } from "pg";
4
+ import { createClient } from "@supabase/supabase-js";
5
+
6
+ try {
7
+ process.loadEnvFile?.(".env.local");
8
+ } catch {
9
+ // Environment may already be provided by the shell.
10
+ }
11
+
12
+ async function main() {
13
+ const provider = (
14
+ process.env.DATABASE_PROVIDER ||
15
+ process.env.DB_PROVIDER ||
16
+ "pg"
17
+ ).toLowerCase();
18
+ const databaseUrl = process.env.DATABASE_URL;
19
+ if (provider !== "supabase" && !databaseUrl) {
20
+ throw new Error("DATABASE_URL is required.");
21
+ }
22
+
23
+ const configPath = path.resolve(
24
+ process.env.OTA_EMERGENCY_REDIRECT_CONFIG_PATH ||
25
+ "config/ota-emergency-channel-redirects.json",
26
+ );
27
+ const rules = JSON.parse(await fs.readFile(configPath, "utf8"));
28
+ if (!Array.isArray(rules)) {
29
+ throw new Error("Emergency redirect config must contain an array.");
30
+ }
31
+
32
+ if (provider === "supabase") {
33
+ const url = process.env.SUPABASE_PROJECT_URL || process.env.SUPABASE_URL;
34
+ const key = process.env.SUPABASE_SERVICE_ROLE_KEY;
35
+ if (!url || !key) {
36
+ throw new Error(
37
+ "SUPABASE_PROJECT_URL and SUPABASE_SERVICE_ROLE_KEY are required.",
38
+ );
39
+ }
40
+
41
+ const client = createClient(url, key, {
42
+ auth: { persistSession: false, autoRefreshToken: false },
43
+ });
44
+ for (const rule of rules) {
45
+ const { error } = await client.from("ota_emergency_redirects").upsert(
46
+ {
47
+ name: rule.id,
48
+ enabled: rule.enabled,
49
+ embedded_update_id: rule.embeddedUpdateId,
50
+ runtime_version: rule.runtimeVersion,
51
+ platform: rule.platform,
52
+ from_channel: rule.fromChannel,
53
+ to_channel: rule.toChannel,
54
+ target_mode: rule.targetMode,
55
+ expected_update_id: rule.expectedUpdateId || null,
56
+ modified_at: new Date().toISOString(),
57
+ },
58
+ {
59
+ onConflict:
60
+ "embedded_update_id,runtime_version,platform,from_channel",
61
+ },
62
+ );
63
+ if (error) {
64
+ throw new Error(error.message);
65
+ }
66
+ }
67
+ } else {
68
+ const sslEnabled = ["true", "1", "require"].includes(
69
+ (process.env.DATABASE_SSL || "false").toLowerCase(),
70
+ );
71
+ const pool = new Pool({
72
+ connectionString: databaseUrl,
73
+ ssl: sslEnabled ? { rejectUnauthorized: false } : false,
74
+ });
75
+
76
+ try {
77
+ for (const rule of rules) {
78
+ await pool.query(
79
+ `insert into public.ota_emergency_redirects (
80
+ name,
81
+ enabled,
82
+ embedded_update_id,
83
+ runtime_version,
84
+ platform,
85
+ from_channel,
86
+ to_channel,
87
+ target_mode,
88
+ expected_update_id
89
+ ) values ($1, $2, $3, $4, $5, $6, $7, $8, $9)
90
+ on conflict (
91
+ embedded_update_id,
92
+ runtime_version,
93
+ platform,
94
+ from_channel
95
+ ) do update set
96
+ name = excluded.name,
97
+ enabled = excluded.enabled,
98
+ to_channel = excluded.to_channel,
99
+ target_mode = excluded.target_mode,
100
+ expected_update_id = excluded.expected_update_id,
101
+ modified_at = now()`,
102
+ [
103
+ rule.id,
104
+ rule.enabled,
105
+ rule.embeddedUpdateId,
106
+ rule.runtimeVersion,
107
+ rule.platform,
108
+ rule.fromChannel,
109
+ rule.toChannel,
110
+ rule.targetMode,
111
+ rule.expectedUpdateId || null,
112
+ ],
113
+ );
114
+ }
115
+ } finally {
116
+ await pool.end();
117
+ }
118
+ }
119
+
120
+ console.log(`Imported ${rules.length} emergency redirect rule(s).`);
121
+ }
122
+
123
+ main().catch((error) => {
124
+ console.error(error instanceof Error ? error.message : String(error));
125
+ process.exitCode = 1;
126
+ });