create-expo-updates-server 0.1.4 → 0.1.5

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 (67) hide show
  1. package/dist/template/.env.docker.example +1 -0
  2. package/dist/template/.env.example +2 -0
  3. package/dist/template/README.md +4 -1
  4. package/dist/template/app/api/admin/distribution-control/route.test.ts +153 -0
  5. package/dist/template/app/api/admin/distribution-control/route.ts +111 -0
  6. package/dist/template/app/api/admin/guard-actions/[id]/route.test.ts +60 -0
  7. package/dist/template/app/api/admin/guard-actions/[id]/route.ts +32 -0
  8. package/dist/template/app/api/admin/guard-actions/route.test.ts +123 -0
  9. package/dist/template/app/api/admin/guard-actions/route.ts +66 -0
  10. package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +4 -1
  11. package/dist/template/app/api/admin/updates/[encodedKey]/policy/route.test.ts +171 -0
  12. package/dist/template/app/api/admin/updates/[encodedKey]/policy/route.ts +75 -0
  13. package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +4 -1
  14. package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +4 -1
  15. package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +4 -1
  16. package/dist/template/app/updates/[encodedKey]/page.tsx +74 -6
  17. package/dist/template/app/updates/page.tsx +22 -1
  18. package/dist/template/docs/getting-started.md +1 -0
  19. package/dist/template/docs/getting-started.ru.md +2 -1
  20. package/dist/template/docs/migrations/2026-09-01-ota-update-policy.sql +108 -0
  21. package/dist/template/docs/migrations/2026-09-01-p1-guard-actions.sql +19 -0
  22. package/dist/template/docs/migrations/2026-09-01-p2-simplify-ota-guard-policy.sql +69 -0
  23. package/dist/template/docs/migrations/2026-09-01-policy-publication-correction.sql +44 -0
  24. package/dist/template/docs/migrations/2026-09-02-ota-distribution-control.sql +377 -0
  25. package/dist/template/docs/migrations/schema.sql +539 -2
  26. package/dist/template/docs/operations-guide.md +52 -1
  27. package/dist/template/docs/ota-emergency-recovery.md +22 -0
  28. package/dist/template/docs/ota-update-policy.md +89 -0
  29. package/dist/template/docs/supabase-setup.md +2 -1
  30. package/dist/template/package-lock.json +2 -2
  31. package/dist/template/package.json +1 -1
  32. package/dist/template/src/client/admin-api.ts +80 -1
  33. package/dist/template/src/client/admin-types.ts +50 -2
  34. package/dist/template/src/client/distribution-control.tsx +231 -0
  35. package/dist/template/src/client/guard-action-combobox.test.ts +26 -0
  36. package/dist/template/src/client/guard-action-combobox.tsx +267 -0
  37. package/dist/template/src/client/guard-action-options.test.ts +62 -0
  38. package/dist/template/src/client/guard-action-options.ts +70 -0
  39. package/dist/template/src/client/update-policy-editor.test.ts +36 -0
  40. package/dist/template/src/client/update-policy-editor.tsx +436 -0
  41. package/dist/template/src/server/controllers/assets.test.ts +87 -0
  42. package/dist/template/src/server/controllers/assets.ts +26 -0
  43. package/dist/template/src/server/controllers/manifest.test.ts +413 -2
  44. package/dist/template/src/server/controllers/manifest.ts +103 -38
  45. package/dist/template/src/server/lib/admin-distribution-guard.test.ts +61 -0
  46. package/dist/template/src/server/lib/admin-update-policy.test.ts +176 -0
  47. package/dist/template/src/server/lib/admin-updates.test.ts +72 -0
  48. package/dist/template/src/server/lib/admin-updates.ts +169 -24
  49. package/dist/template/src/server/lib/distribution-control-migration.test.ts +94 -0
  50. package/dist/template/src/server/lib/distribution-control.test.ts +160 -0
  51. package/dist/template/src/server/lib/distribution-control.ts +250 -0
  52. package/dist/template/src/server/lib/guard-actions-migration.test.ts +56 -0
  53. package/dist/template/src/server/lib/guard-actions.test.ts +142 -0
  54. package/dist/template/src/server/lib/guard-actions.ts +106 -0
  55. package/dist/template/src/server/lib/ota-policy-migrations.test.ts +64 -0
  56. package/dist/template/src/server/lib/simple-policy-migration.test.ts +97 -0
  57. package/dist/template/src/server/lib/supabase.js +95 -8
  58. package/dist/template/src/server/lib/supabase.test.ts +194 -0
  59. package/dist/template/src/server/lib/update-policy.test.ts +98 -0
  60. package/dist/template/src/server/lib/update-policy.ts +171 -0
  61. package/dist/template/src/server/manifest/manifest-repository.ts +10 -1
  62. package/dist/template/src/server/manifest/manifest-response.ts +0 -16
  63. package/dist/template/src/server/manifest/manifest-values.test.ts +54 -0
  64. package/dist/template/src/server/manifest/manifest-values.ts +145 -0
  65. package/dist/template/tsconfig.json +4 -1
  66. package/dist/template-manifest.json +239 -59
  67. package/package.json +1 -1
@@ -0,0 +1,171 @@
1
+ export const UPDATE_POLICY_SCHEMA_VERSION = 1 as const;
2
+
3
+ export type UpdateDeliveryMode = "manual" | "background";
4
+ export type UpdatePolicyGuard = {
5
+ action: string;
6
+ payload?: unknown;
7
+ };
8
+ export type UpdatePolicyInput = {
9
+ delivery: UpdateDeliveryMode;
10
+ guard: UpdatePolicyGuard | null;
11
+ };
12
+ export type ManifestUpdatePolicy = {
13
+ schemaVersion: 1;
14
+ policyVersion: number;
15
+ delivery: UpdateDeliveryMode;
16
+ guard?: UpdatePolicyGuard;
17
+ };
18
+
19
+ const MAX_ACTION_LENGTH = 100;
20
+ const MAX_PAYLOAD_BYTES = 16 * 1024;
21
+ const MAX_PAYLOAD_DEPTH = 10;
22
+ const CONTROL_CHARACTER_PATTERN = /[\u0000-\u001f\u007f-\u009f]/;
23
+
24
+ export class UpdatePolicyValidationError extends Error {
25
+ constructor(message: string) {
26
+ super(message);
27
+ this.name = "UpdatePolicyValidationError";
28
+ }
29
+ }
30
+
31
+ function fail(path: string, message: string): never {
32
+ throw new UpdatePolicyValidationError(`${path}: ${message}`);
33
+ }
34
+
35
+ function isObject(value: unknown): value is Record<string, unknown> {
36
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
37
+ }
38
+
39
+ function validateJsonValue(
40
+ value: unknown,
41
+ path: string,
42
+ depth = 0,
43
+ ): void {
44
+ if (depth > MAX_PAYLOAD_DEPTH) {
45
+ fail(path, `JSON depth must not exceed ${MAX_PAYLOAD_DEPTH}`);
46
+ }
47
+ if (
48
+ value === null ||
49
+ typeof value === "string" ||
50
+ typeof value === "boolean"
51
+ ) {
52
+ return;
53
+ }
54
+ if (typeof value === "number") {
55
+ if (!Number.isFinite(value)) fail(path, "numbers must be finite");
56
+ return;
57
+ }
58
+ if (Array.isArray(value)) {
59
+ value.forEach((item, index) =>
60
+ validateJsonValue(item, `${path}[${index}]`, depth + 1),
61
+ );
62
+ return;
63
+ }
64
+ if (isObject(value)) {
65
+ Object.entries(value).forEach(([key, item]) =>
66
+ validateJsonValue(item, `${path}.${key}`, depth + 1),
67
+ );
68
+ return;
69
+ }
70
+ fail(path, "must be JSON-serializable");
71
+ }
72
+
73
+ function validatePayload(payload: unknown): void {
74
+ validateJsonValue(payload, "policy.guard.payload");
75
+ let serialized: string;
76
+ try {
77
+ serialized = JSON.stringify(payload);
78
+ } catch {
79
+ fail("policy.guard.payload", "must be JSON-serializable");
80
+ }
81
+ if (Buffer.byteLength(serialized, "utf8") > MAX_PAYLOAD_BYTES) {
82
+ fail(
83
+ "policy.guard.payload",
84
+ `must not exceed ${MAX_PAYLOAD_BYTES} UTF-8 bytes`,
85
+ );
86
+ }
87
+ }
88
+
89
+ export function validateUpdatePolicy(value: unknown): UpdatePolicyInput {
90
+ if (!isObject(value)) fail("policy", "must be an object");
91
+ if (value.delivery !== "manual" && value.delivery !== "background") {
92
+ fail("policy.delivery", "must be manual or background");
93
+ }
94
+
95
+ if (value.guard === null) {
96
+ return { delivery: value.delivery, guard: null };
97
+ }
98
+ if (!isObject(value.guard)) {
99
+ fail("policy.guard", "must be an object or null");
100
+ }
101
+
102
+ const action = value.guard.action;
103
+ if (typeof action !== "string") {
104
+ fail("policy.guard.action", "must be a string");
105
+ }
106
+ if (!action.trim()) {
107
+ if (value.guard.payload !== undefined && value.guard.payload !== null) {
108
+ fail("policy.guard.payload", "must be absent when action is empty");
109
+ }
110
+ return { delivery: value.delivery, guard: null };
111
+ }
112
+ if (
113
+ action !== action.trim() ||
114
+ Array.from(action).length > MAX_ACTION_LENGTH ||
115
+ CONTROL_CHARACTER_PATTERN.test(action)
116
+ ) {
117
+ fail(
118
+ "policy.guard.action",
119
+ "must be trimmed, contain 1-100 characters, and have no control characters",
120
+ );
121
+ }
122
+
123
+ if (value.guard.payload !== undefined && value.guard.payload !== null) {
124
+ validatePayload(value.guard.payload);
125
+ }
126
+
127
+ return {
128
+ delivery: value.delivery,
129
+ guard: {
130
+ action,
131
+ ...(value.guard.payload !== undefined && value.guard.payload !== null
132
+ ? { payload: value.guard.payload }
133
+ : {}),
134
+ },
135
+ };
136
+ }
137
+
138
+ export function createManifestUpdatePolicy({
139
+ delivery,
140
+ guard,
141
+ policyVersion,
142
+ onCorrupt,
143
+ }: {
144
+ delivery: unknown;
145
+ guard: unknown;
146
+ policyVersion: unknown;
147
+ onCorrupt?: (error: Error) => void;
148
+ }): ManifestUpdatePolicy {
149
+ try {
150
+ const validated = validateUpdatePolicy({ delivery, guard });
151
+ if (!Number.isInteger(policyVersion) || (policyVersion as number) < 1) {
152
+ fail("policy.policyVersion", "must be a positive integer");
153
+ }
154
+ return {
155
+ schemaVersion: UPDATE_POLICY_SCHEMA_VERSION,
156
+ policyVersion: policyVersion as number,
157
+ delivery: validated.delivery,
158
+ ...(validated.guard ? { guard: validated.guard } : {}),
159
+ };
160
+ } catch (error: unknown) {
161
+ onCorrupt?.(error as Error);
162
+ return {
163
+ schemaVersion: UPDATE_POLICY_SCHEMA_VERSION,
164
+ policyVersion:
165
+ Number.isInteger(policyVersion) && (policyVersion as number) > 0
166
+ ? (policyVersion as number)
167
+ : 1,
168
+ delivery: "manual",
169
+ };
170
+ }
171
+ }
@@ -1,4 +1,6 @@
1
1
  import { supabase } from "../lib/supabase.js";
2
+ import { UpdateDeliveryMode } from "../lib/update-policy";
3
+ import { getManifestAppVersion } from "./manifest-values";
2
4
 
3
5
  export type OtaUpdateRow = {
4
6
  update_id: string;
@@ -10,6 +12,11 @@ export type OtaUpdateRow = {
10
12
  created_at_path: string;
11
13
  storage_base_path: string;
12
14
  is_active: boolean;
15
+ delivery_mode: UpdateDeliveryMode;
16
+ guard_action: string | null;
17
+ guard_payload: unknown | null;
18
+ policy_version: number;
19
+ policy_published_at: string | Date | null;
13
20
  manifest: Record<string, unknown>;
14
21
  };
15
22
 
@@ -26,6 +33,7 @@ export type EmbeddedUpdateMeta = {
26
33
  created_at: string | Date;
27
34
  channel: string;
28
35
  platform: "ios" | "android";
36
+ app_version: string | null;
29
37
  };
30
38
 
31
39
  let hasServedManifestLogTable: boolean | null = null;
@@ -193,7 +201,7 @@ async function getEmbeddedUpdateMetaById(
193
201
  ): Promise<EmbeddedUpdateMeta | null> {
194
202
  const { data, error } = await supabase
195
203
  .from("ota_embedded_updates")
196
- .select("embedded_update_id,created_at,channel,platform")
204
+ .select("embedded_update_id,created_at,channel,platform,app_version")
197
205
  .eq("embedded_update_id", embeddedUpdateId)
198
206
  .maybeSingle();
199
207
 
@@ -227,6 +235,7 @@ function toEmbeddedUpdateMeta(updateRow: OtaUpdateRow): EmbeddedUpdateMeta {
227
235
  created_at: updateRow.created_at,
228
236
  channel: updateRow.channel,
229
237
  platform: updateRow.platform,
238
+ app_version: getManifestAppVersion(updateRow.manifest),
230
239
  };
231
240
  }
232
241
 
@@ -1,5 +1,4 @@
1
1
  import NodeFormData from "form-data";
2
- import path from "node:path";
3
2
  import { NextRequest } from "next/server";
4
3
  import { toPosixPath } from "../lib/manifest-helpers.js";
5
4
  import { createSignatureHeaderIfRequested } from "../lib/signing.js";
@@ -61,11 +60,6 @@ function getServerBaseUrl(request: NextRequest): string {
61
60
  return `${protocol}://${host}`;
62
61
  }
63
62
 
64
- function getCdnBaseUrl(): string | null {
65
- const value = process.env.OTA_CDN_BASE_URL?.trim();
66
- return value ? removeTrailingSlash(value) : null;
67
- }
68
-
69
63
  export function buildAssetUrl({
70
64
  request,
71
65
  runtimeVersion,
@@ -74,7 +68,6 @@ export function buildAssetUrl({
74
68
  channel,
75
69
  updateId,
76
70
  assetPath,
77
- storageBasePath,
78
71
  }: {
79
72
  request: NextRequest;
80
73
  runtimeVersion: string;
@@ -83,16 +76,7 @@ export function buildAssetUrl({
83
76
  channel: string;
84
77
  updateId: string;
85
78
  assetPath: string;
86
- storageBasePath: string;
87
79
  }): string {
88
- const cdnBaseUrl = getCdnBaseUrl();
89
- if (cdnBaseUrl && storageBasePath) {
90
- const objectPath = toPosixPath(
91
- path.posix.join(storageBasePath, toPosixPath(assetPath)),
92
- );
93
- return `${cdnBaseUrl}/${objectPath}`;
94
- }
95
-
96
80
  const params = new URLSearchParams({
97
81
  runtimeVersion,
98
82
  platform,
@@ -0,0 +1,54 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import {
3
+ compareAppVersions,
4
+ getManifestAppVersion,
5
+ } from "./manifest-values";
6
+
7
+ describe("compareAppVersions", () => {
8
+ it.each([
9
+ ["1.7.11", "1.7.10", 1],
10
+ ["1.10", "1.9", 1],
11
+ ["1.0.0", "1", 0],
12
+ ["1.0.0-beta.2", "1.0.0-beta.11", -1],
13
+ ["1.0.0-beta", "1.0.0", -1],
14
+ ["1.0.0", "1.0.0-rc.1", 1],
15
+ ["1.0.0+build.2", "1.0.0+build.1", 0],
16
+ ])("compares %s with %s", (left, right, expected) => {
17
+ expect(compareAppVersions(left, right)).toBe(expected);
18
+ });
19
+
20
+ it.each([
21
+ ["", "1.0.0"],
22
+ ["version 2", "1.0.0"],
23
+ ["1..2", "1.0.0"],
24
+ ["1.0.0_beta", "1.0.0"],
25
+ ["1.02.0", "1.2.0"],
26
+ ["1.0.0-beta.01", "1.0.0-beta.1"],
27
+ [null, "1.0.0"],
28
+ ])("returns indeterminate for %s and %s", (left, right) => {
29
+ expect(compareAppVersions(left, right)).toBeNull();
30
+ });
31
+ });
32
+
33
+ describe("getManifestAppVersion", () => {
34
+ it("reads the root expoClient version", () => {
35
+ expect(
36
+ getManifestAppVersion({ expoClient: { version: " 1.7.11 " } }),
37
+ ).toBe("1.7.11");
38
+ });
39
+
40
+ it("reads the extra expoClient version", () => {
41
+ expect(
42
+ getManifestAppVersion({ extra: { expoClient: { version: "1.7.10" } } }),
43
+ ).toBe("1.7.10");
44
+ });
45
+
46
+ it("prefers the extra expoClient version when both are present", () => {
47
+ expect(
48
+ getManifestAppVersion({
49
+ expoClient: { version: "2.0.0" },
50
+ extra: { expoClient: { version: "1.0.0" } },
51
+ }),
52
+ ).toBe("1.0.0");
53
+ });
54
+ });
@@ -13,6 +13,151 @@ export function debugString(value: string | null | undefined): string {
13
13
  return normalizeId(value) || "none";
14
14
  }
15
15
 
16
+ export function getManifestAppVersion(manifest: unknown): string | null {
17
+ if (!manifest || typeof manifest !== "object" || Array.isArray(manifest)) {
18
+ return null;
19
+ }
20
+
21
+ const manifestRecord = manifest as Record<string, unknown>;
22
+ const extra =
23
+ manifestRecord.extra &&
24
+ typeof manifestRecord.extra === "object" &&
25
+ !Array.isArray(manifestRecord.extra)
26
+ ? (manifestRecord.extra as Record<string, unknown>)
27
+ : null;
28
+ for (const expoClient of [extra?.expoClient, manifestRecord.expoClient]) {
29
+ if (
30
+ expoClient &&
31
+ typeof expoClient === "object" &&
32
+ !Array.isArray(expoClient)
33
+ ) {
34
+ const version = (expoClient as Record<string, unknown>).version;
35
+ if (typeof version === "string" && version.trim()) {
36
+ return version.trim();
37
+ }
38
+ }
39
+ }
40
+
41
+ return null;
42
+ }
43
+
44
+ type ParsedAppVersion = {
45
+ core: string[];
46
+ prerelease: string[] | null;
47
+ };
48
+
49
+ function parseAppVersion(value: string): ParsedAppVersion | null {
50
+ const normalized = value.trim();
51
+ const match =
52
+ /^(0|[1-9]\d*(?:\.(?:0|[1-9]\d*))*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/.exec(
53
+ normalized,
54
+ );
55
+ if (!match) {
56
+ return null;
57
+ }
58
+
59
+ const prerelease = match[2]?.split(".") ?? null;
60
+ if (
61
+ prerelease?.some(
62
+ (identifier) =>
63
+ /^\d+$/.test(identifier) &&
64
+ identifier.length > 1 &&
65
+ identifier.startsWith("0"),
66
+ )
67
+ ) {
68
+ return null;
69
+ }
70
+
71
+ return {
72
+ core: match[1].split("."),
73
+ prerelease,
74
+ };
75
+ }
76
+
77
+ function compareNumericIdentifiers(left: string, right: string): -1 | 0 | 1 {
78
+ if (left.length !== right.length) {
79
+ return left.length < right.length ? -1 : 1;
80
+ }
81
+ if (left === right) {
82
+ return 0;
83
+ }
84
+ return left < right ? -1 : 1;
85
+ }
86
+
87
+ /**
88
+ * Compares semver-style numeric app versions without coercing numeric
89
+ * identifiers to JavaScript numbers. Build metadata is ignored.
90
+ *
91
+ * Returns null when either value is malformed and cannot be compared safely.
92
+ */
93
+ export function compareAppVersions(
94
+ left: string | null | undefined,
95
+ right: string | null | undefined,
96
+ ): -1 | 0 | 1 | null {
97
+ if (typeof left !== "string" || typeof right !== "string") {
98
+ return null;
99
+ }
100
+
101
+ const parsedLeft = parseAppVersion(left);
102
+ const parsedRight = parseAppVersion(right);
103
+ if (!parsedLeft || !parsedRight) {
104
+ return null;
105
+ }
106
+
107
+ const coreLength = Math.max(parsedLeft.core.length, parsedRight.core.length);
108
+ for (let index = 0; index < coreLength; index += 1) {
109
+ const comparison = compareNumericIdentifiers(
110
+ parsedLeft.core[index] ?? "0",
111
+ parsedRight.core[index] ?? "0",
112
+ );
113
+ if (comparison !== 0) {
114
+ return comparison;
115
+ }
116
+ }
117
+
118
+ const leftPrerelease = parsedLeft.prerelease;
119
+ const rightPrerelease = parsedRight.prerelease;
120
+ if (!leftPrerelease && !rightPrerelease) {
121
+ return 0;
122
+ }
123
+ if (!leftPrerelease) {
124
+ return 1;
125
+ }
126
+ if (!rightPrerelease) {
127
+ return -1;
128
+ }
129
+
130
+ const prereleaseLength = Math.max(
131
+ leftPrerelease.length,
132
+ rightPrerelease.length,
133
+ );
134
+ for (let index = 0; index < prereleaseLength; index += 1) {
135
+ const leftIdentifier = leftPrerelease[index];
136
+ const rightIdentifier = rightPrerelease[index];
137
+ if (leftIdentifier === undefined) {
138
+ return -1;
139
+ }
140
+ if (rightIdentifier === undefined) {
141
+ return 1;
142
+ }
143
+ if (leftIdentifier === rightIdentifier) {
144
+ continue;
145
+ }
146
+
147
+ const leftIsNumeric = /^\d+$/.test(leftIdentifier);
148
+ const rightIsNumeric = /^\d+$/.test(rightIdentifier);
149
+ if (leftIsNumeric && rightIsNumeric) {
150
+ return compareNumericIdentifiers(leftIdentifier, rightIdentifier);
151
+ }
152
+ if (leftIsNumeric !== rightIsNumeric) {
153
+ return leftIsNumeric ? -1 : 1;
154
+ }
155
+ return leftIdentifier > rightIdentifier ? 1 : -1;
156
+ }
157
+
158
+ return 0;
159
+ }
160
+
16
161
  export function normalizeExpoManifestDate(
17
162
  value: string | Date | null | undefined,
18
163
  ): string | null {
@@ -32,5 +32,8 @@
32
32
  ".next/dev/types/**/*.ts",
33
33
  "**/*.mts"
34
34
  ],
35
- "exclude": ["node_modules"]
35
+ "exclude": [
36
+ "node_modules",
37
+ "packages/create-expo-updates-server/dist"
38
+ ]
36
39
  }