create-expo-updates-server 0.1.3 → 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.
- package/dist/template/.env.docker.example +1 -0
- package/dist/template/.env.example +2 -0
- package/dist/template/README.md +16 -8
- package/dist/template/app/api/admin/distribution-control/route.test.ts +153 -0
- package/dist/template/app/api/admin/distribution-control/route.ts +111 -0
- package/dist/template/app/api/admin/embedded-updates/route.test.ts +36 -1
- package/dist/template/app/api/admin/guard-actions/[id]/route.test.ts +60 -0
- package/dist/template/app/api/admin/guard-actions/[id]/route.ts +32 -0
- package/dist/template/app/api/admin/guard-actions/route.test.ts +123 -0
- package/dist/template/app/api/admin/guard-actions/route.ts +66 -0
- package/dist/template/app/api/admin/updates/[encodedKey]/deactivate/route.ts +4 -1
- package/dist/template/app/api/admin/updates/[encodedKey]/policy/route.test.ts +171 -0
- package/dist/template/app/api/admin/updates/[encodedKey]/policy/route.ts +75 -0
- package/dist/template/app/api/admin/updates/[encodedKey]/promote/route.ts +4 -1
- package/dist/template/app/api/admin/updates/[encodedKey]/rollback/route.ts +4 -1
- package/dist/template/app/api/admin/updates/[encodedKey]/route.ts +4 -1
- package/dist/template/app/api/admin/updates/route.test.ts +29 -1
- package/dist/template/app/updates/[encodedKey]/page.tsx +74 -6
- package/dist/template/app/updates/embedded/page.tsx +6 -2
- package/dist/template/app/updates/page.tsx +28 -3
- package/dist/template/docs/expo-app-setup.md +14 -3
- package/dist/template/docs/getting-started.md +13 -7
- package/dist/template/docs/getting-started.ru.md +15 -7
- package/dist/template/docs/migrations/2026-09-01-app-version-columns.sql +23 -0
- package/dist/template/docs/migrations/2026-09-01-ota-update-policy.sql +108 -0
- package/dist/template/docs/migrations/2026-09-01-p1-guard-actions.sql +19 -0
- package/dist/template/docs/migrations/2026-09-01-p2-simplify-ota-guard-policy.sql +69 -0
- package/dist/template/docs/migrations/2026-09-01-policy-publication-correction.sql +44 -0
- package/dist/template/docs/migrations/2026-09-02-ota-distribution-control.sql +377 -0
- package/dist/template/docs/migrations/schema.sql +561 -2
- package/dist/template/docs/operations-guide.md +52 -1
- package/dist/template/docs/ota-emergency-recovery.md +22 -0
- package/dist/template/docs/ota-update-policy.md +89 -0
- package/dist/template/docs/supabase-setup.md +2 -1
- package/dist/template/package-lock.json +2 -2
- package/dist/template/package.json +1 -1
- package/dist/template/scripts/configure-expo-app.mjs +6 -0
- package/dist/template/scripts/lib/embedded-update-registrar.test.ts +383 -3
- package/dist/template/scripts/lib/expo-app-configurator.mjs +165 -9
- package/dist/template/scripts/lib/expo-app-configurator.test.ts +658 -9
- package/dist/template/src/client/admin-api.ts +80 -1
- package/dist/template/src/client/admin-types.ts +52 -2
- package/dist/template/src/client/distribution-control.tsx +231 -0
- package/dist/template/src/client/guard-action-combobox.test.ts +26 -0
- package/dist/template/src/client/guard-action-combobox.tsx +267 -0
- package/dist/template/src/client/guard-action-options.test.ts +62 -0
- package/dist/template/src/client/guard-action-options.ts +70 -0
- package/dist/template/src/client/update-policy-editor.test.ts +36 -0
- package/dist/template/src/client/update-policy-editor.tsx +436 -0
- package/dist/template/src/server/controllers/assets.test.ts +87 -0
- package/dist/template/src/server/controllers/assets.ts +26 -0
- package/dist/template/src/server/controllers/manifest.test.ts +413 -2
- package/dist/template/src/server/controllers/manifest.ts +103 -38
- package/dist/template/src/server/lib/admin-distribution-guard.test.ts +61 -0
- package/dist/template/src/server/lib/admin-embedded-updates.test.ts +57 -0
- package/dist/template/src/server/lib/admin-embedded-updates.ts +42 -25
- package/dist/template/src/server/lib/admin-update-policy.test.ts +176 -0
- package/dist/template/src/server/lib/admin-updates.test.ts +133 -0
- package/dist/template/src/server/lib/admin-updates.ts +174 -9
- package/dist/template/src/server/lib/distribution-control-migration.test.ts +94 -0
- package/dist/template/src/server/lib/distribution-control.test.ts +160 -0
- package/dist/template/src/server/lib/distribution-control.ts +250 -0
- package/dist/template/src/server/lib/guard-actions-migration.test.ts +56 -0
- package/dist/template/src/server/lib/guard-actions.test.ts +142 -0
- package/dist/template/src/server/lib/guard-actions.ts +106 -0
- package/dist/template/src/server/lib/ota-policy-migrations.test.ts +64 -0
- package/dist/template/src/server/lib/simple-policy-migration.test.ts +97 -0
- package/dist/template/src/server/lib/supabase.js +95 -8
- package/dist/template/src/server/lib/supabase.test.ts +194 -0
- package/dist/template/src/server/lib/update-policy.test.ts +98 -0
- package/dist/template/src/server/lib/update-policy.ts +171 -0
- package/dist/template/src/server/manifest/manifest-repository.ts +10 -1
- package/dist/template/src/server/manifest/manifest-response.ts +0 -16
- package/dist/template/src/server/manifest/manifest-values.test.ts +54 -0
- package/dist/template/src/server/manifest/manifest-values.ts +145 -0
- package/dist/template/templates/expo-app/ci_scripts/ci_post_xcodebuild.sh +23 -1
- package/dist/template/templates/expo-app/plugins/with-ota-embedded-registration.js +13 -4
- package/dist/template/templates/expo-app/scripts/ota-register-embedded/index.mjs +53 -7
- package/dist/template/templates/expo-app/scripts/ota-register-embedded/register-ios.sh +87 -0
- package/dist/template/tsconfig.json +4 -1
- package/dist/template-manifest.json +283 -88
- package/package.json +1 -1
- package/dist/template/templates/expo-app/ci_scripts/register-embedded-update.sh +0 -36
- /package/dist/template/templates/expo-app/scripts/ota-register-embedded/{android-register-embedded-update.sh → register-android.sh} +0 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
assertExpectedPolicyVersion,
|
|
4
|
+
mapRowToRecord,
|
|
5
|
+
OtaUpdateRow,
|
|
6
|
+
UpdatePolicyConflictError,
|
|
7
|
+
} from "./admin-updates";
|
|
8
|
+
import { UpdatePolicyValidationError } from "./update-policy";
|
|
9
|
+
|
|
10
|
+
function makeRow(manifest: Record<string, unknown> = {}): OtaUpdateRow {
|
|
11
|
+
return {
|
|
12
|
+
id: "11111111-1111-4111-8111-111111111111",
|
|
13
|
+
update_id: "22222222-2222-4222-8222-222222222222",
|
|
14
|
+
build_id: "33333333-3333-4333-8333-333333333333",
|
|
15
|
+
comment: null,
|
|
16
|
+
runtime_version: "runtime-1",
|
|
17
|
+
channel: "production",
|
|
18
|
+
platform: "ios",
|
|
19
|
+
created_at: "2026-01-01T00:00:00.000Z",
|
|
20
|
+
created_at_path: "2026/01/01/000000",
|
|
21
|
+
storage_bucket: "updates",
|
|
22
|
+
storage_base_path: "runtime-1/ios/update",
|
|
23
|
+
is_active: true,
|
|
24
|
+
updated_at: null,
|
|
25
|
+
disabled_at: null,
|
|
26
|
+
assets_count: 2,
|
|
27
|
+
launch_asset_path: "bundle.js",
|
|
28
|
+
rolled_back_from_update_id: null,
|
|
29
|
+
delivery_mode: "manual",
|
|
30
|
+
guard_action: null,
|
|
31
|
+
guard_payload: null,
|
|
32
|
+
policy_version: 1,
|
|
33
|
+
policy_published_at: "2026-01-01T00:00:00.000Z",
|
|
34
|
+
manifest,
|
|
35
|
+
inserted_at: "2026-01-01T00:00:00.000Z",
|
|
36
|
+
modified_at: "2026-01-01T00:00:00.000Z",
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe("OTA admin update mapping", () => {
|
|
41
|
+
it("requires expectedPolicyVersion at the service boundary", () => {
|
|
42
|
+
expect(() => assertExpectedPolicyVersion(undefined)).toThrow(
|
|
43
|
+
UpdatePolicyValidationError,
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("rejects a stale expectedPolicyVersion at the service boundary", () => {
|
|
48
|
+
expect(() => assertExpectedPolicyVersion(2, 3)).toThrow(
|
|
49
|
+
UpdatePolicyConflictError,
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("maps an inactive initial-disabled draft as policy-editable", () => {
|
|
54
|
+
const row = makeRow();
|
|
55
|
+
row.is_active = false;
|
|
56
|
+
row.disabled_at = row.created_at;
|
|
57
|
+
row.policy_published_at = null;
|
|
58
|
+
|
|
59
|
+
const item = mapRowToRecord(row, null);
|
|
60
|
+
|
|
61
|
+
expect(item.status).toBe("disabled");
|
|
62
|
+
expect(item.policyPublishedAt).toBeNull();
|
|
63
|
+
expect(item.policyEditable).toBe(true);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("maps the simple Guard badge without rule counting", () => {
|
|
67
|
+
const row = makeRow();
|
|
68
|
+
row.guard_action = "require-confirmation";
|
|
69
|
+
expect(mapRowToRecord(row, null).hasGuard).toBe(true);
|
|
70
|
+
row.guard_action = null;
|
|
71
|
+
expect(mapRowToRecord(row, null).hasGuard).toBe(false);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it.each([
|
|
75
|
+
["active without a marker", true, null],
|
|
76
|
+
[
|
|
77
|
+
"inactive after publication",
|
|
78
|
+
false,
|
|
79
|
+
"2026-01-01T00:00:00.000Z",
|
|
80
|
+
],
|
|
81
|
+
])("maps an %s update as locked", (_case, active, publishedAt) => {
|
|
82
|
+
const row = makeRow();
|
|
83
|
+
row.is_active = active;
|
|
84
|
+
row.disabled_at = active ? null : "2026-01-02T00:00:00.000Z";
|
|
85
|
+
row.policy_published_at = publishedAt;
|
|
86
|
+
|
|
87
|
+
const item = mapRowToRecord(row, null);
|
|
88
|
+
|
|
89
|
+
expect(item.policyPublishedAt).toBe(publishedAt);
|
|
90
|
+
expect(item.policyEditable).toBe(false);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("maps expoClient.version from the stored manifest", () => {
|
|
94
|
+
const item = mapRowToRecord(
|
|
95
|
+
makeRow({ expoClient: { version: "2.4.1" } }),
|
|
96
|
+
null,
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
expect(item.appVersion).toBe("2.4.1");
|
|
100
|
+
expect(item.runtimeVersion).toBe("runtime-1");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("normalizes a non-empty manifest app version", () => {
|
|
104
|
+
const item = mapRowToRecord(
|
|
105
|
+
makeRow({ expoClient: { version: " 2.4.1 " } }),
|
|
106
|
+
null,
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
expect(item.appVersion).toBe("2.4.1");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("prefers extra.expoClient.version from uploaded manifests", () => {
|
|
113
|
+
const item = mapRowToRecord(
|
|
114
|
+
makeRow({
|
|
115
|
+
expoClient: { version: "1.0.0" },
|
|
116
|
+
extra: { expoClient: { version: "2.4.1" } },
|
|
117
|
+
}),
|
|
118
|
+
null,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
expect(item.appVersion).toBe("2.4.1");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it.each([
|
|
125
|
+
["missing expoClient", {}],
|
|
126
|
+
["missing version", { expoClient: {} }],
|
|
127
|
+
["blank version", { expoClient: { version: " \t " } }],
|
|
128
|
+
["non-string version", { expoClient: { version: 241 } }],
|
|
129
|
+
["malformed expoClient", { expoClient: "invalid" }],
|
|
130
|
+
])("maps %s to null", (_case, manifest) => {
|
|
131
|
+
expect(mapRowToRecord(makeRow(manifest), null).appVersion).toBeNull();
|
|
132
|
+
});
|
|
133
|
+
});
|
|
@@ -1,5 +1,13 @@
|
|
|
1
|
-
import { supabase, SUPABASE_BUCKET } from "./supabase.js";
|
|
1
|
+
import { jsonb, supabase, SUPABASE_BUCKET } from "./supabase.js";
|
|
2
2
|
import { toPosixPath } from "./manifest-helpers.js";
|
|
3
|
+
import {
|
|
4
|
+
UpdateDeliveryMode,
|
|
5
|
+
UpdatePolicyInput,
|
|
6
|
+
UpdatePolicyValidationError,
|
|
7
|
+
validateUpdatePolicy,
|
|
8
|
+
} from "./update-policy";
|
|
9
|
+
import { getManifestAppVersion } from "../manifest/manifest-values";
|
|
10
|
+
import { assertOtaDistributionAllowed } from "./distribution-control";
|
|
3
11
|
|
|
4
12
|
let hasServedManifestLogTable: boolean | null = null;
|
|
5
13
|
|
|
@@ -89,7 +97,7 @@ type UpdateMetaState = {
|
|
|
89
97
|
disabledAt: string | null;
|
|
90
98
|
};
|
|
91
99
|
|
|
92
|
-
type OtaUpdateRow = {
|
|
100
|
+
export type OtaUpdateRow = {
|
|
93
101
|
id: string;
|
|
94
102
|
update_id: string;
|
|
95
103
|
build_id: string;
|
|
@@ -107,12 +115,17 @@ type OtaUpdateRow = {
|
|
|
107
115
|
assets_count: number;
|
|
108
116
|
launch_asset_path: string | null;
|
|
109
117
|
rolled_back_from_update_id: string | null;
|
|
118
|
+
delivery_mode: UpdateDeliveryMode;
|
|
119
|
+
guard_action: string | null;
|
|
120
|
+
guard_payload: unknown | null;
|
|
121
|
+
policy_version: number;
|
|
122
|
+
policy_published_at: string | null;
|
|
110
123
|
manifest: Record<string, unknown>;
|
|
111
124
|
inserted_at: string;
|
|
112
125
|
modified_at: string;
|
|
113
126
|
};
|
|
114
127
|
|
|
115
|
-
type OtaChannelRow = {
|
|
128
|
+
export type OtaChannelRow = {
|
|
116
129
|
id: number;
|
|
117
130
|
runtime_version: string;
|
|
118
131
|
channel: string;
|
|
@@ -131,6 +144,7 @@ type OtaChannelRow = {
|
|
|
131
144
|
export type UpdateRecord = {
|
|
132
145
|
key: string;
|
|
133
146
|
encodedKey: string;
|
|
147
|
+
appVersion: string | null;
|
|
134
148
|
comment: string | null;
|
|
135
149
|
runtimeVersion: string;
|
|
136
150
|
platform: "ios" | "android";
|
|
@@ -151,6 +165,11 @@ export type UpdateRecord = {
|
|
|
151
165
|
isIgnoredByRollback: boolean;
|
|
152
166
|
launchAssetPath: string | null;
|
|
153
167
|
assetsCount: number;
|
|
168
|
+
deliveryMode: UpdateDeliveryMode;
|
|
169
|
+
hasGuard: boolean;
|
|
170
|
+
policyVersion: number;
|
|
171
|
+
policyPublishedAt: string | null;
|
|
172
|
+
policyEditable: boolean;
|
|
154
173
|
};
|
|
155
174
|
|
|
156
175
|
export type UpdateDetail = UpdateRecord & {
|
|
@@ -273,7 +292,7 @@ async function getNewestUpdateByScope(
|
|
|
273
292
|
return (data as OtaUpdateRow | null) || null;
|
|
274
293
|
}
|
|
275
294
|
|
|
276
|
-
function mapRowToRecord(
|
|
295
|
+
export function mapRowToRecord(
|
|
277
296
|
row: OtaUpdateRow,
|
|
278
297
|
channel: OtaChannelRow | null,
|
|
279
298
|
): UpdateRecord {
|
|
@@ -292,6 +311,7 @@ function mapRowToRecord(
|
|
|
292
311
|
return {
|
|
293
312
|
key,
|
|
294
313
|
encodedKey: encodeUpdateKey(row.update_id),
|
|
314
|
+
appVersion: getManifestAppVersion(row.manifest),
|
|
295
315
|
comment: row.comment,
|
|
296
316
|
runtimeVersion: row.runtime_version,
|
|
297
317
|
platform: row.platform,
|
|
@@ -312,9 +332,132 @@ function mapRowToRecord(
|
|
|
312
332
|
isIgnoredByRollback,
|
|
313
333
|
launchAssetPath: row.launch_asset_path,
|
|
314
334
|
assetsCount: row.assets_count,
|
|
335
|
+
deliveryMode:
|
|
336
|
+
row.delivery_mode === "background" ? "background" : "manual",
|
|
337
|
+
hasGuard:
|
|
338
|
+
typeof row.guard_action === "string" && row.guard_action.length > 0,
|
|
339
|
+
policyVersion:
|
|
340
|
+
Number.isInteger(row.policy_version) && row.policy_version > 0
|
|
341
|
+
? row.policy_version
|
|
342
|
+
: 1,
|
|
343
|
+
policyPublishedAt: row.policy_published_at || null,
|
|
344
|
+
policyEditable: !row.is_active && !row.policy_published_at,
|
|
315
345
|
};
|
|
316
346
|
}
|
|
317
347
|
|
|
348
|
+
export type UpdatePolicyRecord = UpdatePolicyInput & {
|
|
349
|
+
policyVersion: number;
|
|
350
|
+
publishedAt: string | null;
|
|
351
|
+
editable: boolean;
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
export class UpdatePolicyConflictError extends Error {}
|
|
355
|
+
export class UpdatePolicyPublishedError extends Error {}
|
|
356
|
+
|
|
357
|
+
export function assertExpectedPolicyVersion(
|
|
358
|
+
expectedPolicyVersion: unknown,
|
|
359
|
+
currentPolicyVersion?: number,
|
|
360
|
+
): asserts expectedPolicyVersion is number {
|
|
361
|
+
if (
|
|
362
|
+
!Number.isInteger(expectedPolicyVersion) ||
|
|
363
|
+
(expectedPolicyVersion as number) < 1
|
|
364
|
+
) {
|
|
365
|
+
throw new UpdatePolicyValidationError(
|
|
366
|
+
"expectedPolicyVersion must be a positive integer.",
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
if (
|
|
370
|
+
currentPolicyVersion !== undefined &&
|
|
371
|
+
expectedPolicyVersion !== currentPolicyVersion
|
|
372
|
+
) {
|
|
373
|
+
throw new UpdatePolicyConflictError(
|
|
374
|
+
`Policy version conflict. Current version is ${currentPolicyVersion}.`,
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function rowToPolicy(row: OtaUpdateRow): UpdatePolicyRecord {
|
|
380
|
+
const validated = validateUpdatePolicy({
|
|
381
|
+
delivery: row.delivery_mode,
|
|
382
|
+
guard: row.guard_action
|
|
383
|
+
? {
|
|
384
|
+
action: row.guard_action,
|
|
385
|
+
...(row.guard_payload !== null
|
|
386
|
+
? { payload: row.guard_payload }
|
|
387
|
+
: {}),
|
|
388
|
+
}
|
|
389
|
+
: null,
|
|
390
|
+
});
|
|
391
|
+
return {
|
|
392
|
+
...validated,
|
|
393
|
+
policyVersion: row.policy_version,
|
|
394
|
+
publishedAt: row.policy_published_at,
|
|
395
|
+
editable: !row.is_active && !row.policy_published_at,
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
export async function getUpdatePolicyByKey(
|
|
400
|
+
key: string,
|
|
401
|
+
): Promise<UpdatePolicyRecord> {
|
|
402
|
+
return rowToPolicy(await getUpdateRowById(extractUpdateIdFromKey(key)));
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export async function replaceUpdatePolicyByKey(
|
|
406
|
+
key: string,
|
|
407
|
+
input: unknown,
|
|
408
|
+
expectedPolicyVersion: unknown,
|
|
409
|
+
): Promise<UpdatePolicyRecord> {
|
|
410
|
+
assertExpectedPolicyVersion(expectedPolicyVersion);
|
|
411
|
+
const updateId = extractUpdateIdFromKey(key);
|
|
412
|
+
const row = await getUpdateRowById(updateId);
|
|
413
|
+
if (row.is_active || row.policy_published_at) {
|
|
414
|
+
throw new UpdatePolicyPublishedError(
|
|
415
|
+
"Update policy is immutable after publication.",
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
assertExpectedPolicyVersion(expectedPolicyVersion, row.policy_version);
|
|
419
|
+
|
|
420
|
+
const policy = validateUpdatePolicy(input);
|
|
421
|
+
const { data, error } = await supabase
|
|
422
|
+
.from("ota_updates")
|
|
423
|
+
.update({
|
|
424
|
+
delivery_mode: policy.delivery,
|
|
425
|
+
guard_action: policy.guard?.action || null,
|
|
426
|
+
guard_payload:
|
|
427
|
+
policy.guard?.payload !== undefined
|
|
428
|
+
? jsonb(policy.guard.payload)
|
|
429
|
+
: null,
|
|
430
|
+
policy_version: row.policy_version + 1,
|
|
431
|
+
})
|
|
432
|
+
.eq("update_id", updateId)
|
|
433
|
+
.eq("is_active", false)
|
|
434
|
+
.eq("policy_version", row.policy_version)
|
|
435
|
+
.is("policy_published_at", null)
|
|
436
|
+
.select("*")
|
|
437
|
+
.maybeSingle();
|
|
438
|
+
|
|
439
|
+
if (error) {
|
|
440
|
+
if (error.message?.toLowerCase().includes("immutable")) {
|
|
441
|
+
throw new UpdatePolicyPublishedError(
|
|
442
|
+
"Update policy is immutable after publication.",
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
throw new Error(`Failed to update policy: ${error.message}`);
|
|
446
|
+
}
|
|
447
|
+
if (!data) {
|
|
448
|
+
const current = await getUpdateRowById(updateId);
|
|
449
|
+
if (current.is_active || current.policy_published_at) {
|
|
450
|
+
throw new UpdatePolicyPublishedError(
|
|
451
|
+
"Update policy is immutable after publication.",
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
throw new UpdatePolicyConflictError(
|
|
455
|
+
`Policy version conflict. Current version is ${current.policy_version}.`,
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
return rowToPolicy(data as OtaUpdateRow);
|
|
459
|
+
}
|
|
460
|
+
|
|
318
461
|
function compareValues(a: string | number, b: string | number): number {
|
|
319
462
|
if (a < b) {
|
|
320
463
|
return -1;
|
|
@@ -810,16 +953,23 @@ export async function updateJsonFileByKey(
|
|
|
810
953
|
| "channel-latest.json",
|
|
811
954
|
nextJson: unknown,
|
|
812
955
|
) {
|
|
813
|
-
const updateId = extractUpdateIdFromKey(key);
|
|
814
|
-
const row = await getUpdateRowById(updateId);
|
|
815
|
-
const scope = makeScopeFromRow(row);
|
|
816
|
-
|
|
817
956
|
if (!nextJson || typeof nextJson !== "object" || Array.isArray(nextJson)) {
|
|
818
957
|
throw new Error("content must be a JSON object");
|
|
819
958
|
}
|
|
820
959
|
|
|
821
960
|
const content = nextJson as Record<string, unknown>;
|
|
822
961
|
|
|
962
|
+
if (
|
|
963
|
+
fileName === "channel-latest.json" ||
|
|
964
|
+
(fileName === "update-meta.json" && content.isActive !== false)
|
|
965
|
+
) {
|
|
966
|
+
await assertOtaDistributionAllowed();
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
const updateId = extractUpdateIdFromKey(key);
|
|
970
|
+
const row = await getUpdateRowById(updateId);
|
|
971
|
+
const scope = makeScopeFromRow(row);
|
|
972
|
+
|
|
823
973
|
if (fileName === "update-info.json") {
|
|
824
974
|
const assets = Array.isArray(content.assets) ? content.assets : [];
|
|
825
975
|
const launchAssetPath =
|
|
@@ -877,7 +1027,12 @@ export async function updateJsonFileByKey(
|
|
|
877
1027
|
|
|
878
1028
|
const { error } = await supabase
|
|
879
1029
|
.from("ota_updates")
|
|
880
|
-
.update(
|
|
1030
|
+
.update({
|
|
1031
|
+
...parsedMeta,
|
|
1032
|
+
...(parsedMeta.is_active && !row.policy_published_at
|
|
1033
|
+
? { policy_published_at: parsedMeta.updated_at }
|
|
1034
|
+
: {}),
|
|
1035
|
+
})
|
|
881
1036
|
.eq("update_id", updateId)
|
|
882
1037
|
.execute();
|
|
883
1038
|
|
|
@@ -931,6 +1086,7 @@ export async function rollbackToUpdateByKey(
|
|
|
931
1086
|
key: string,
|
|
932
1087
|
options?: RollbackOptions,
|
|
933
1088
|
) {
|
|
1089
|
+
await assertOtaDistributionAllowed();
|
|
934
1090
|
const updateId = extractUpdateIdFromKey(key);
|
|
935
1091
|
const row = await getUpdateRowById(updateId);
|
|
936
1092
|
const scope = makeScopeFromRow(row);
|
|
@@ -1008,6 +1164,7 @@ export async function rollbackToUpdateByKey(
|
|
|
1008
1164
|
is_active: true,
|
|
1009
1165
|
updated_at: now,
|
|
1010
1166
|
disabled_at: null,
|
|
1167
|
+
policy_published_at: row.policy_published_at || now,
|
|
1011
1168
|
})
|
|
1012
1169
|
.eq("update_id", updateId)
|
|
1013
1170
|
.execute();
|
|
@@ -1069,6 +1226,7 @@ export async function rollbackToUpdateByKey(
|
|
|
1069
1226
|
}
|
|
1070
1227
|
|
|
1071
1228
|
export async function promoteLatestByKey(key: string) {
|
|
1229
|
+
await assertOtaDistributionAllowed();
|
|
1072
1230
|
const updateId = extractUpdateIdFromKey(key);
|
|
1073
1231
|
const row = await getUpdateRowById(updateId);
|
|
1074
1232
|
const scope = makeScopeFromRow(row);
|
|
@@ -1119,6 +1277,7 @@ export async function promoteLatestByKey(key: string) {
|
|
|
1119
1277
|
is_active: true,
|
|
1120
1278
|
updated_at: promoteNow,
|
|
1121
1279
|
disabled_at: null,
|
|
1280
|
+
policy_published_at: row.policy_published_at || promoteNow,
|
|
1122
1281
|
})
|
|
1123
1282
|
.eq("update_id", latestUpdateId)
|
|
1124
1283
|
.execute();
|
|
@@ -1160,6 +1319,9 @@ export async function promoteLatestByKey(key: string) {
|
|
|
1160
1319
|
}
|
|
1161
1320
|
|
|
1162
1321
|
export async function setUpdateDisabledByKey(key: string, disabled: boolean) {
|
|
1322
|
+
if (!disabled) {
|
|
1323
|
+
await assertOtaDistributionAllowed();
|
|
1324
|
+
}
|
|
1163
1325
|
const updateId = extractUpdateIdFromKey(key);
|
|
1164
1326
|
const row = await getUpdateRowById(updateId);
|
|
1165
1327
|
const scope = makeScopeFromRow(row);
|
|
@@ -1212,6 +1374,9 @@ export async function setUpdateDisabledByKey(key: string, disabled: boolean) {
|
|
|
1212
1374
|
is_active: !disabled,
|
|
1213
1375
|
updated_at: now,
|
|
1214
1376
|
disabled_at: disabled ? now : null,
|
|
1377
|
+
...(!disabled && !row.policy_published_at
|
|
1378
|
+
? { policy_published_at: now }
|
|
1379
|
+
: {}),
|
|
1215
1380
|
})
|
|
1216
1381
|
.eq("update_id", updateId)
|
|
1217
1382
|
.execute();
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
|
|
5
|
+
const migrationSql = readFileSync(
|
|
6
|
+
resolve(
|
|
7
|
+
process.cwd(),
|
|
8
|
+
"docs/migrations/2026-09-02-ota-distribution-control.sql",
|
|
9
|
+
),
|
|
10
|
+
"utf8",
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
const canonicalSchemaSql = readFileSync(
|
|
14
|
+
resolve(process.cwd(), "docs/migrations/schema.sql"),
|
|
15
|
+
"utf8",
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
describe.each([
|
|
19
|
+
["distribution-control migration", migrationSql],
|
|
20
|
+
["canonical schema", canonicalSchemaSql],
|
|
21
|
+
])("%s database guard", (_name, sql) => {
|
|
22
|
+
it("serializes switch writes and guarded mutations on one transaction lock", () => {
|
|
23
|
+
expect(sql).toContain(
|
|
24
|
+
"pg_catalog.pg_advisory_xact_lock(20260902, 1)",
|
|
25
|
+
);
|
|
26
|
+
expect(sql).toContain(
|
|
27
|
+
"before insert or update or delete on public.ota_distribution_control",
|
|
28
|
+
);
|
|
29
|
+
expect(sql).toContain("before insert on public.ota_updates");
|
|
30
|
+
expect(sql).toContain(
|
|
31
|
+
"before update of is_active on public.ota_updates",
|
|
32
|
+
);
|
|
33
|
+
expect(sql).toContain(
|
|
34
|
+
"on public.ota_update_channels\nfor each statement",
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
const rpc = sql.slice(
|
|
38
|
+
sql.indexOf(
|
|
39
|
+
"create or replace function public.set_ota_distribution_control(",
|
|
40
|
+
),
|
|
41
|
+
);
|
|
42
|
+
expect(rpc).toContain(
|
|
43
|
+
"perform public.lock_ota_distribution_control();",
|
|
44
|
+
);
|
|
45
|
+
expect(sql).toContain(
|
|
46
|
+
"perform public.assert_ota_distribution_mutation_allowed(",
|
|
47
|
+
);
|
|
48
|
+
expect(sql).toMatch(
|
|
49
|
+
/from public\.ota_distribution_control\s+where singleton_id = 1\s+for share;/,
|
|
50
|
+
);
|
|
51
|
+
expect(rpc).toMatch(
|
|
52
|
+
/from public\.ota_distribution_control\s+where singleton_id = 1\s+for update;/,
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("guards activation while leaving deactivation and inactive inserts alone", () => {
|
|
57
|
+
expect(sql).toContain("tg_op = 'INSERT' and not new.is_active");
|
|
58
|
+
expect(sql).toContain(
|
|
59
|
+
"new.is_active and not old.is_active",
|
|
60
|
+
);
|
|
61
|
+
expect(sql).toContain(
|
|
62
|
+
"before insert or update on public.ota_updates",
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("guards every channel field that can alter effective distribution", () => {
|
|
67
|
+
for (const field of [
|
|
68
|
+
"runtime_version",
|
|
69
|
+
"channel",
|
|
70
|
+
"platform",
|
|
71
|
+
"latest_update_id",
|
|
72
|
+
"latest_created_at",
|
|
73
|
+
"latest_created_at_path",
|
|
74
|
+
"active_update_id",
|
|
75
|
+
"active_changed_at",
|
|
76
|
+
"served_manifest_id",
|
|
77
|
+
"served_manifest_changed_at",
|
|
78
|
+
]) {
|
|
79
|
+
expect(sql).toMatch(
|
|
80
|
+
new RegExp(
|
|
81
|
+
`new\\.${field}\\s+is distinct from old\\.${field}`,
|
|
82
|
+
),
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
expect(sql).toContain(
|
|
86
|
+
"new.latest_update_id is null and new.active_update_id is null",
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("raises a stable database error for blocked distribution", () => {
|
|
91
|
+
expect(sql).toContain("errcode = 'P0OTA'");
|
|
92
|
+
expect(sql).toContain("OTA_DISTRIBUTION_BLOCKED:");
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
const database = vi.hoisted(() => ({
|
|
4
|
+
row: {
|
|
5
|
+
blocked: false,
|
|
6
|
+
version: 1,
|
|
7
|
+
reason: null as string | null,
|
|
8
|
+
changed_at: "2026-09-02T00:00:00.000Z",
|
|
9
|
+
changed_by: {
|
|
10
|
+
type: "system",
|
|
11
|
+
id: "migration",
|
|
12
|
+
label: "Database migration",
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
readError: null as { message: string } | null,
|
|
16
|
+
rpcError: null as { message: string } | null,
|
|
17
|
+
rpcParams: null as Record<string, unknown> | null,
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
vi.mock("./supabase.js", () => ({
|
|
21
|
+
jsonb: (value: unknown) => value,
|
|
22
|
+
supabase: {
|
|
23
|
+
from: () => ({
|
|
24
|
+
select: () => ({
|
|
25
|
+
eq: () => ({
|
|
26
|
+
maybeSingle: async () => ({
|
|
27
|
+
data: database.readError ? null : database.row,
|
|
28
|
+
error: database.readError,
|
|
29
|
+
}),
|
|
30
|
+
}),
|
|
31
|
+
}),
|
|
32
|
+
}),
|
|
33
|
+
rpc: async (_name: string, params: Record<string, unknown>) => {
|
|
34
|
+
database.rpcParams = params;
|
|
35
|
+
if (database.rpcError) {
|
|
36
|
+
return { data: null, error: database.rpcError };
|
|
37
|
+
}
|
|
38
|
+
database.row = {
|
|
39
|
+
...database.row,
|
|
40
|
+
blocked: params.p_blocked === true,
|
|
41
|
+
version: database.row.version + 1,
|
|
42
|
+
reason:
|
|
43
|
+
typeof params.p_reason === "string" ? params.p_reason : null,
|
|
44
|
+
changed_at: "2026-09-02T00:01:00.000Z",
|
|
45
|
+
changed_by: params.p_changed_by as typeof database.row.changed_by,
|
|
46
|
+
};
|
|
47
|
+
return { data: [database.row], error: null };
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
}));
|
|
51
|
+
|
|
52
|
+
import {
|
|
53
|
+
DistributionControlConflictError,
|
|
54
|
+
DistributionControlValidationError,
|
|
55
|
+
getDistributionControlState,
|
|
56
|
+
isOtaDistributionBlocked,
|
|
57
|
+
setDistributionControlState,
|
|
58
|
+
} from "./distribution-control";
|
|
59
|
+
|
|
60
|
+
const principal = {
|
|
61
|
+
type: "session" as const,
|
|
62
|
+
id: "admin-id",
|
|
63
|
+
label: "operator",
|
|
64
|
+
role: "operator",
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
describe("OTA distribution control service", () => {
|
|
68
|
+
beforeEach(() => {
|
|
69
|
+
database.row = {
|
|
70
|
+
blocked: false,
|
|
71
|
+
version: 1,
|
|
72
|
+
reason: null,
|
|
73
|
+
changed_at: "2026-09-02T00:00:00.000Z",
|
|
74
|
+
changed_by: {
|
|
75
|
+
type: "system",
|
|
76
|
+
id: "migration",
|
|
77
|
+
label: "Database migration",
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
database.readError = null;
|
|
81
|
+
database.rpcError = null;
|
|
82
|
+
database.rpcParams = null;
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("reads and maps the singleton without a process-local fallback", async () => {
|
|
86
|
+
await expect(getDistributionControlState()).resolves.toMatchObject({
|
|
87
|
+
blocked: false,
|
|
88
|
+
version: 1,
|
|
89
|
+
reason: null,
|
|
90
|
+
});
|
|
91
|
+
await expect(isOtaDistributionBlocked()).resolves.toBe(false);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("does not interpret a database error or missing singleton as active", async () => {
|
|
95
|
+
database.readError = { message: "relation does not exist" };
|
|
96
|
+
await expect(isOtaDistributionBlocked()).rejects.toThrow(
|
|
97
|
+
"Failed to read OTA distribution control",
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
database.readError = null;
|
|
101
|
+
const savedRow = database.row;
|
|
102
|
+
Object.assign(database, { row: null });
|
|
103
|
+
await expect(getDistributionControlState()).rejects.toThrow("missing");
|
|
104
|
+
database.row = savedRow;
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("validates and atomically delegates state plus principal audit metadata", async () => {
|
|
108
|
+
const state = await setDistributionControlState({
|
|
109
|
+
blocked: true,
|
|
110
|
+
reason: " INC-42 bad rollout ",
|
|
111
|
+
expectedVersion: 1,
|
|
112
|
+
principal,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
expect(state).toMatchObject({
|
|
116
|
+
blocked: true,
|
|
117
|
+
version: 2,
|
|
118
|
+
reason: "INC-42 bad rollout",
|
|
119
|
+
changedBy: principal,
|
|
120
|
+
});
|
|
121
|
+
expect(database.rpcParams).toMatchObject({
|
|
122
|
+
p_blocked: true,
|
|
123
|
+
p_reason: "INC-42 bad rollout",
|
|
124
|
+
p_expected_version: 1,
|
|
125
|
+
p_changed_by: principal,
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("requires a bounded reason and positive expectedVersion", async () => {
|
|
130
|
+
await expect(
|
|
131
|
+
setDistributionControlState({
|
|
132
|
+
blocked: true,
|
|
133
|
+
reason: " ",
|
|
134
|
+
expectedVersion: 1,
|
|
135
|
+
principal,
|
|
136
|
+
}),
|
|
137
|
+
).rejects.toBeInstanceOf(DistributionControlValidationError);
|
|
138
|
+
await expect(
|
|
139
|
+
setDistributionControlState({
|
|
140
|
+
blocked: false,
|
|
141
|
+
expectedVersion: 0,
|
|
142
|
+
principal,
|
|
143
|
+
}),
|
|
144
|
+
).rejects.toBeInstanceOf(DistributionControlValidationError);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("maps optimistic database conflicts to a typed 409 error", async () => {
|
|
148
|
+
database.rpcError = {
|
|
149
|
+
message: "OTA distribution control version conflict",
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
await expect(
|
|
153
|
+
setDistributionControlState({
|
|
154
|
+
blocked: false,
|
|
155
|
+
expectedVersion: 1,
|
|
156
|
+
principal,
|
|
157
|
+
}),
|
|
158
|
+
).rejects.toBeInstanceOf(DistributionControlConflictError);
|
|
159
|
+
});
|
|
160
|
+
});
|