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,69 @@
1
+ begin;
2
+
3
+ drop trigger if exists trg_ota_updates_lock_published_policy
4
+ on public.ota_updates;
5
+
6
+ alter table public.ota_updates
7
+ add column if not exists guard_action text null,
8
+ add column if not exists guard_payload jsonb null;
9
+
10
+ alter table public.ota_updates
11
+ drop constraint if exists ota_updates_guard_rules_array_chk,
12
+ drop constraint if exists ota_updates_guard_action_chk,
13
+ drop constraint if exists ota_updates_guard_payload_requires_action_chk,
14
+ drop column if exists guard_rules,
15
+ add constraint ota_updates_guard_action_chk check (
16
+ guard_action is null
17
+ or (
18
+ guard_action = btrim(guard_action)
19
+ and char_length(guard_action) between 1 and 100
20
+ and guard_action !~ '[[:cntrl:]]'
21
+ )
22
+ ),
23
+ add constraint ota_updates_guard_payload_requires_action_chk check (
24
+ guard_payload is null or guard_action is not null
25
+ );
26
+
27
+ create or replace function public.ota_updates_lock_published_policy()
28
+ returns trigger
29
+ language plpgsql
30
+ as $$
31
+ begin
32
+ if tg_op = 'INSERT' then
33
+ if new.is_active and new.policy_published_at is null then
34
+ new.policy_published_at := coalesce(
35
+ new.updated_at,
36
+ new.created_at,
37
+ now()
38
+ );
39
+ end if;
40
+ return new;
41
+ end if;
42
+
43
+ if new.is_active and not old.is_active
44
+ and new.policy_published_at is null then
45
+ new.policy_published_at := coalesce(
46
+ new.updated_at,
47
+ new.created_at,
48
+ now()
49
+ );
50
+ end if;
51
+
52
+ if old.policy_published_at is not null and (
53
+ new.delivery_mode is distinct from old.delivery_mode
54
+ or new.guard_action is distinct from old.guard_action
55
+ or new.guard_payload is distinct from old.guard_payload
56
+ or new.policy_version is distinct from old.policy_version
57
+ or new.policy_published_at is distinct from old.policy_published_at
58
+ ) then
59
+ raise exception 'OTA update policy is immutable after publication';
60
+ end if;
61
+ return new;
62
+ end;
63
+ $$;
64
+
65
+ create trigger trg_ota_updates_lock_published_policy
66
+ before insert or update on public.ota_updates
67
+ for each row execute function public.ota_updates_lock_published_policy();
68
+
69
+ commit;
@@ -0,0 +1,44 @@
1
+ begin;
2
+
3
+ -- The original policy migration could mistake the upload-time disabled marker
4
+ -- for historical publication. Remove only markers on conservative,
5
+ -- never-published draft signatures. The trigger must be absent for this
6
+ -- one-time repair because clearing policy_published_at is otherwise correctly
7
+ -- rejected for published rows.
8
+ drop trigger if exists trg_ota_updates_lock_published_policy
9
+ on public.ota_updates;
10
+
11
+ update public.ota_updates u
12
+ set policy_published_at = null
13
+ where u.policy_published_at is not null
14
+ and u.policy_published_at = coalesce(
15
+ u.updated_at,
16
+ u.disabled_at,
17
+ u.created_at
18
+ )
19
+ and not u.is_active
20
+ and u.disabled_at is not null
21
+ and u.disabled_at = u.created_at
22
+ and u.delivery_mode = 'manual'
23
+ and u.guard_action is null
24
+ and u.guard_payload is null
25
+ and u.policy_version = 1
26
+ and u.rolled_back_from_update_id is null
27
+ and not exists (
28
+ select 1
29
+ from public.ota_served_manifest_log sml
30
+ where sml.update_id = u.update_id
31
+ )
32
+ -- latest_update_id tracks the newest upload and may reference this inactive
33
+ -- draft. It is intentionally not treated as publication evidence.
34
+ and not exists (
35
+ select 1
36
+ from public.ota_update_channels c
37
+ where c.active_update_id = u.update_id
38
+ );
39
+
40
+ create trigger trg_ota_updates_lock_published_policy
41
+ before insert or update on public.ota_updates
42
+ for each row execute function public.ota_updates_lock_published_policy();
43
+
44
+ commit;
@@ -0,0 +1,377 @@
1
+ create table if not exists public.ota_distribution_control (
2
+ singleton_id smallint primary key default 1,
3
+ blocked boolean not null default false,
4
+ version bigint not null default 1,
5
+ reason text null,
6
+ changed_at timestamptz not null default now(),
7
+ changed_by jsonb not null default
8
+ '{"type":"system","id":"migration","label":"Database migration"}'::jsonb,
9
+ constraint ota_distribution_control_singleton_chk check (singleton_id = 1),
10
+ constraint ota_distribution_control_version_chk check (version > 0),
11
+ constraint ota_distribution_control_reason_chk check (
12
+ (not blocked and reason is null)
13
+ or (
14
+ blocked
15
+ and reason = btrim(reason)
16
+ and char_length(reason) between 1 and 500
17
+ and reason !~ '[[:cntrl:]]'
18
+ )
19
+ )
20
+ );
21
+
22
+ insert into public.ota_distribution_control (
23
+ singleton_id,
24
+ blocked,
25
+ version,
26
+ reason,
27
+ changed_by
28
+ )
29
+ values (
30
+ 1,
31
+ false,
32
+ 1,
33
+ null,
34
+ '{"type":"system","id":"migration","label":"Database migration"}'::jsonb
35
+ )
36
+ on conflict (singleton_id) do nothing;
37
+
38
+ create table if not exists public.ota_distribution_control_events (
39
+ id bigserial primary key,
40
+ previous_blocked boolean not null,
41
+ blocked boolean not null,
42
+ previous_version bigint not null,
43
+ version bigint not null,
44
+ reason text null,
45
+ changed_at timestamptz not null,
46
+ changed_by jsonb not null,
47
+ constraint ota_distribution_control_events_versions_chk check (
48
+ previous_version > 0 and version = previous_version + 1
49
+ )
50
+ );
51
+
52
+ create index if not exists idx_ota_distribution_control_events_changed_at
53
+ on public.ota_distribution_control_events (changed_at desc);
54
+
55
+ -- Serialize the emergency switch with every mutation that can start or
56
+ -- redirect OTA distribution. A transaction that gets this lock first is the
57
+ -- operation that takes effect first; the other transaction observes its
58
+ -- committed result after waiting. Keep this lock first in every code path
59
+ -- before taking row locks to avoid lock-order inversions.
60
+ create or replace function public.lock_ota_distribution_control()
61
+ returns void
62
+ language sql
63
+ set search_path = pg_catalog
64
+ as $$
65
+ select pg_catalog.pg_advisory_xact_lock(20260902, 1);
66
+ $$;
67
+
68
+ revoke all on function public.lock_ota_distribution_control() from public;
69
+
70
+ create or replace function public.serialize_ota_distribution_statement()
71
+ returns trigger
72
+ language plpgsql
73
+ security definer
74
+ set search_path = public, pg_temp
75
+ as $$
76
+ begin
77
+ perform public.lock_ota_distribution_control();
78
+ return null;
79
+ end;
80
+ $$;
81
+
82
+ -- This statement-level trigger also serializes ordinary direct SQL updates
83
+ -- of the singleton. The RPC explicitly takes the same lock before touching
84
+ -- the row.
85
+ drop trigger if exists trg_ota_distribution_control_serialize_write
86
+ on public.ota_distribution_control;
87
+ create trigger trg_ota_distribution_control_serialize_write
88
+ before insert or update or delete on public.ota_distribution_control
89
+ for each statement
90
+ execute function public.serialize_ota_distribution_statement();
91
+
92
+ -- Acquire the lock before PostgreSQL starts locking affected update/channel
93
+ -- rows. The row triggers below take it again (reentrantly) only when they need
94
+ -- to check BLOCKED, but the statement triggers establish a consistent global
95
+ -- lock order for multi-row and direct-SQL mutations.
96
+ drop trigger if exists trg_ota_distribution_serialize_update_insert
97
+ on public.ota_updates;
98
+ create trigger trg_ota_distribution_serialize_update_insert
99
+ before insert on public.ota_updates
100
+ for each statement
101
+ execute function public.serialize_ota_distribution_statement();
102
+
103
+ drop trigger if exists trg_ota_distribution_serialize_update_activation
104
+ on public.ota_updates;
105
+ create trigger trg_ota_distribution_serialize_update_activation
106
+ before update of is_active on public.ota_updates
107
+ for each statement
108
+ execute function public.serialize_ota_distribution_statement();
109
+
110
+ drop trigger if exists trg_ota_distribution_serialize_channel_insert
111
+ on public.ota_update_channels;
112
+ create trigger trg_ota_distribution_serialize_channel_insert
113
+ before insert on public.ota_update_channels
114
+ for each statement
115
+ execute function public.serialize_ota_distribution_statement();
116
+
117
+ drop trigger if exists trg_ota_distribution_serialize_channel_change
118
+ on public.ota_update_channels;
119
+ create trigger trg_ota_distribution_serialize_channel_change
120
+ before update of
121
+ runtime_version,
122
+ channel,
123
+ platform,
124
+ latest_update_id,
125
+ latest_created_at,
126
+ latest_created_at_path,
127
+ active_update_id,
128
+ active_changed_at,
129
+ served_manifest_id,
130
+ served_manifest_changed_at
131
+ on public.ota_update_channels
132
+ for each statement
133
+ execute function public.serialize_ota_distribution_statement();
134
+
135
+ create or replace function public.assert_ota_distribution_mutation_allowed(
136
+ p_operation text
137
+ )
138
+ returns void
139
+ language plpgsql
140
+ security definer
141
+ set search_path = public, pg_temp
142
+ as $$
143
+ declare
144
+ distribution_blocked boolean;
145
+ begin
146
+ perform public.lock_ota_distribution_control();
147
+
148
+ select blocked
149
+ into distribution_blocked
150
+ from public.ota_distribution_control
151
+ where singleton_id = 1
152
+ for share;
153
+
154
+ if not found then
155
+ raise exception
156
+ 'OTA_DISTRIBUTION_CONTROL_MISSING: singleton row is required'
157
+ using errcode = '55000';
158
+ end if;
159
+
160
+ if distribution_blocked then
161
+ raise exception
162
+ 'OTA_DISTRIBUTION_BLOCKED: % is not allowed while global OTA distribution is blocked',
163
+ p_operation
164
+ using
165
+ errcode = 'P0OTA',
166
+ hint = 'Deactivate updates or resume OTA distribution first.';
167
+ end if;
168
+ end;
169
+ $$;
170
+
171
+ revoke all on function public.assert_ota_distribution_mutation_allowed(text)
172
+ from public;
173
+
174
+ create or replace function public.guard_ota_update_distribution()
175
+ returns trigger
176
+ language plpgsql
177
+ security definer
178
+ set search_path = public, pg_temp
179
+ as $$
180
+ begin
181
+ -- Inactive inserts and all edits that do not activate a row stay available.
182
+ -- In particular, true -> false is deliberately allowed for incident repair.
183
+ if (tg_op = 'INSERT' and not new.is_active)
184
+ or (
185
+ tg_op = 'UPDATE'
186
+ and not (new.is_active and not old.is_active)
187
+ ) then
188
+ return new;
189
+ end if;
190
+
191
+ perform public.assert_ota_distribution_mutation_allowed(
192
+ case
193
+ when tg_op = 'INSERT' then 'inserting an active OTA update'
194
+ else 'activating an OTA update'
195
+ end
196
+ );
197
+ return new;
198
+ end;
199
+ $$;
200
+
201
+ drop trigger if exists trg_ota_distribution_guard_updates
202
+ on public.ota_updates;
203
+ create trigger trg_ota_distribution_guard_updates
204
+ before insert or update on public.ota_updates
205
+ for each row
206
+ execute function public.guard_ota_update_distribution();
207
+
208
+ create or replace function public.guard_ota_channel_distribution()
209
+ returns trigger
210
+ language plpgsql
211
+ security definer
212
+ set search_path = public, pg_temp
213
+ as $$
214
+ declare
215
+ changes_distribution_state boolean;
216
+ begin
217
+ if tg_op = 'INSERT' then
218
+ -- Empty channel rows do not distribute an update.
219
+ changes_distribution_state :=
220
+ new.latest_update_id is not null
221
+ or new.active_update_id is not null;
222
+ else
223
+ changes_distribution_state :=
224
+ new.runtime_version is distinct from old.runtime_version
225
+ or new.channel is distinct from old.channel
226
+ or new.platform is distinct from old.platform
227
+ or new.latest_update_id is distinct from old.latest_update_id
228
+ or new.latest_created_at is distinct from old.latest_created_at
229
+ or new.latest_created_at_path is distinct from old.latest_created_at_path
230
+ or new.active_update_id is distinct from old.active_update_id
231
+ or new.active_changed_at is distinct from old.active_changed_at
232
+ or new.served_manifest_id is distinct from old.served_manifest_id
233
+ or new.served_manifest_changed_at
234
+ is distinct from old.served_manifest_changed_at;
235
+ end if;
236
+
237
+ if not changes_distribution_state then
238
+ return new;
239
+ end if;
240
+
241
+ -- Clearing every serving pointer only removes distribution and is the
242
+ -- channel-side part of deactivation. Clearing a rollback pointer while a
243
+ -- latest pointer remains would promote latest, so it is still guarded.
244
+ if new.latest_update_id is null and new.active_update_id is null then
245
+ return new;
246
+ end if;
247
+
248
+ perform public.assert_ota_distribution_mutation_allowed(
249
+ 'changing OTA channel distribution state'
250
+ );
251
+ return new;
252
+ end;
253
+ $$;
254
+
255
+ drop trigger if exists trg_ota_distribution_guard_channels
256
+ on public.ota_update_channels;
257
+ create trigger trg_ota_distribution_guard_channels
258
+ before insert or update on public.ota_update_channels
259
+ for each row
260
+ execute function public.guard_ota_channel_distribution();
261
+
262
+ create or replace function public.set_ota_distribution_control(
263
+ p_blocked boolean,
264
+ p_reason text,
265
+ p_expected_version bigint,
266
+ p_changed_by jsonb
267
+ )
268
+ returns setof public.ota_distribution_control
269
+ language plpgsql
270
+ security definer
271
+ set search_path = public, pg_temp
272
+ as $$
273
+ declare
274
+ previous_row public.ota_distribution_control%rowtype;
275
+ updated_row public.ota_distribution_control%rowtype;
276
+ normalized_reason text;
277
+ begin
278
+ if p_expected_version is null or p_expected_version < 1 then
279
+ raise exception 'expectedVersion must be a positive integer'
280
+ using errcode = '22023';
281
+ end if;
282
+
283
+ if p_changed_by is null
284
+ or jsonb_typeof(p_changed_by) <> 'object'
285
+ or nullif(btrim(p_changed_by->>'type'), '') is null
286
+ or nullif(btrim(p_changed_by->>'id'), '') is null
287
+ or nullif(btrim(p_changed_by->>'label'), '') is null then
288
+ raise exception 'changedBy principal metadata is invalid'
289
+ using errcode = '22023';
290
+ end if;
291
+
292
+ normalized_reason := nullif(btrim(coalesce(p_reason, '')), '');
293
+ if p_blocked and normalized_reason is null then
294
+ raise exception 'reason is required when OTA distribution is blocked'
295
+ using errcode = '22023';
296
+ end if;
297
+ if p_blocked and (
298
+ char_length(normalized_reason) > 500
299
+ or normalized_reason ~ '[[:cntrl:]]'
300
+ ) then
301
+ raise exception 'reason must contain at most 500 characters and no control characters'
302
+ using errcode = '22023';
303
+ end if;
304
+ if not p_blocked then
305
+ normalized_reason := null;
306
+ end if;
307
+
308
+ perform public.lock_ota_distribution_control();
309
+
310
+ select *
311
+ into previous_row
312
+ from public.ota_distribution_control
313
+ where singleton_id = 1
314
+ for update;
315
+
316
+ if not found then
317
+ raise exception 'OTA distribution control singleton is missing'
318
+ using errcode = '55000';
319
+ end if;
320
+
321
+ update public.ota_distribution_control
322
+ set blocked = p_blocked,
323
+ version = version + 1,
324
+ reason = normalized_reason,
325
+ changed_at = now(),
326
+ changed_by = p_changed_by
327
+ where singleton_id = 1
328
+ and version = p_expected_version
329
+ returning * into updated_row;
330
+
331
+ if not found then
332
+ raise exception 'OTA distribution control version conflict'
333
+ using errcode = '40001';
334
+ end if;
335
+
336
+ insert into public.ota_distribution_control_events (
337
+ previous_blocked,
338
+ blocked,
339
+ previous_version,
340
+ version,
341
+ reason,
342
+ changed_at,
343
+ changed_by
344
+ )
345
+ values (
346
+ previous_row.blocked,
347
+ updated_row.blocked,
348
+ previous_row.version,
349
+ updated_row.version,
350
+ updated_row.reason,
351
+ updated_row.changed_at,
352
+ updated_row.changed_by
353
+ );
354
+
355
+ return next updated_row;
356
+ end;
357
+ $$;
358
+
359
+ revoke all on function public.set_ota_distribution_control(
360
+ boolean,
361
+ text,
362
+ bigint,
363
+ jsonb
364
+ ) from public;
365
+
366
+ do $$
367
+ begin
368
+ if exists (select 1 from pg_roles where rolname = 'service_role') then
369
+ grant execute on function public.set_ota_distribution_control(
370
+ boolean,
371
+ text,
372
+ bigint,
373
+ jsonb
374
+ ) to service_role;
375
+ end if;
376
+ end;
377
+ $$;