@runeditrun/rer 0.2.23 → 0.2.24

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 (38) hide show
  1. package/cli/demo-host.mjs +1 -1
  2. package/cli/deploy-command.mjs +7 -5
  3. package/cli/deploy-observation.mjs +20 -2
  4. package/cli/deploy-owner.mjs +12 -7
  5. package/cli/deploy.mjs +5 -4
  6. package/cli/site.mjs +2 -2
  7. package/global/CHANGELOG.md +16 -0
  8. package/modules/container/adapters/d1.mjs +1 -1
  9. package/modules/container/transfer.mjs +0 -1
  10. package/modules/container/worker-engine.mjs +1 -1
  11. package/modules/demo-host/README.md +29 -7
  12. package/modules/demo-host/container.mjs +19 -7
  13. package/modules/demo-host/placement.mjs +19 -5
  14. package/modules/demo-host/plan.mjs +11 -6
  15. package/modules/demo-host/start-script.mjs +2 -1
  16. package/modules/demo-host/startup.mjs +3 -3
  17. package/modules/demo-host/worker.mjs +27 -8
  18. package/modules/deploy-kit/README.md +35 -2
  19. package/modules/deploy-kit/diagnostics.mjs +16 -1
  20. package/modules/deploy-kit/owner-cli.mjs +3 -3
  21. package/modules/deploy-kit/owner-remove.mjs +23 -5
  22. package/modules/deploy-kit/pagination.mjs +99 -0
  23. package/modules/deploy-kit/provider.mjs +30 -70
  24. package/modules/deploy-kit/r2-objects.mjs +6 -17
  25. package/modules/deploy-kit/site-readback.mjs +67 -0
  26. package/modules/deploy-kit/site.mjs +17 -31
  27. package/modules/deploy-kit/uploaded-worker.mjs +9 -3
  28. package/modules/erasure/queue.mjs +0 -2
  29. package/modules/job-queue/cli.mjs +1 -1
  30. package/modules/operator-auth/identity/oidc/jwt.mjs +1 -1
  31. package/modules/portability/bridge.mjs +3 -3
  32. package/modules/portability/catalogue.mjs +1 -1
  33. package/modules/portability/cli-options.mjs +1 -1
  34. package/modules/portability/legacy-files.mjs +1 -1
  35. package/modules/portability/legacy-safety.mjs +3 -3
  36. package/modules/portability/payloads.mjs +1 -1
  37. package/modules/release/source.mjs +1 -1
  38. package/package.json +1 -1
@@ -1,8 +1,12 @@
1
1
  import { mkdir, rename, writeFile } from 'node:fs/promises';
2
2
  import { dirname } from 'node:path';
3
3
  import { assert, hash, resourceName } from './validation.mjs';
4
- import { ownerWorkerName, workerSettings } from './owner.mjs';
4
+ import { ownerWorkerName } from './owner.mjs';
5
5
  import { uploadAssets, workerOrigin } from './worker.mjs';
6
+ import { readinessWindow, readinessAPI } from './readiness-window.mjs';
7
+ import { uploadVersionId } from './uploaded-worker.mjs';
8
+ import { ownedSiteVersion, waitForSiteUpload } from './site-readback.mjs';
9
+ export { servedVersion, siteReadbackSummary } from './site-readback.mjs';
6
10
 
7
11
  /* A seed's run site (run-<seed>.com): the folder `rer build --site` writes,
8
12
  * served on the owner's own account by a Base-generated module Worker (fixed
@@ -24,7 +28,6 @@ import { uploadAssets, workerOrigin } from './worker.mjs';
24
28
 
25
29
  const RECEIPT_KIND = 'rer-deploy-site-receipt';
26
30
  const APEX = /^(?!www\.)(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z][a-z0-9-]{0,62}$/;
27
- const ANNOTATION = /^rer-site:[0-9a-f]{64}$/;
28
31
  export const SITE_ASSETS_CONFIG = Object.freeze({ html_handling: 'auto-trailing-slash', not_found_handling: '404-page' });
29
32
  export const SITE_MODULE = 'site-worker.mjs';
30
33
  /** The site Worker. It names no domain: only the site's two Custom Domains route to it. */
@@ -65,28 +68,6 @@ export function sitePlan({ project, environment, compatibilityDate, declaration,
65
68
  redirects: [{ from: `https://www.${domain}/*`, to: `https://${domain}/`, status: 301, keeps: ['path', 'query'] }] };
66
69
  }
67
70
 
68
- /** The version the Worker's latest deployment serves, as the kit reads ownership: null when there is no Worker. */
69
- export async function servedVersion(api, worker) {
70
- if (!await workerSettings(api, worker)) return null;
71
- const { deployments } = await api.request(`/workers/scripts/${worker}/deployments`);
72
- assert(Array.isArray(deployments) && deployments.length > 0, 'invalid-deployments');
73
- const [latest] = deployments, single = latest.versions?.length === 1 && latest.versions[0].percentage === 100 ? latest.versions[0].version_id : null;
74
- const newest = (await api.request(`/workers/scripts/${worker}/versions`))?.items?.[0]?.id;
75
- const version = single ? await api.request(`/workers/scripts/${worker}/versions/${single}`) : null;
76
- const annotation = version?.annotations?.['workers/message'];
77
- return { kit: typeof annotation === 'string' && ANNOTATION.test(annotation), annotation, current: Boolean(single) && single === newest };
78
- }
79
- /** The Worker is absent (false) or the kit's (true); anything else is refused. */
80
- async function ownWorker(api, worker) {
81
- const served = await servedVersion(api, worker);
82
- if (!served) return false;
83
- // Someone else's Worker of this name, or one whose live version the kit did not upload.
84
- if (!served.kit) throw refuse('resource-name-collision', 'worker', worker);
85
- // A kit version that is no longer the newest one served alone: a dashboard rollback or a split deployment.
86
- if (!served.current) throw refuse('owned-resource-replaced', 'worker', worker);
87
- return true;
88
- }
89
-
90
71
  async function writeReceipt(path, receipt) {
91
72
  await mkdir(dirname(path), { recursive: true, mode: 0o700 });
92
73
  const temporary = `${path}.${process.pid}.tmp`;
@@ -95,19 +76,19 @@ async function writeReceipt(path, receipt) {
95
76
  }
96
77
 
97
78
  /** Read-only preflight, then the site Worker upload and both Custom Domains, then readback. */
98
- export async function siteDeploy({ api, plan, files, receiptPath }) {
79
+ export async function siteDeploy({ api, plan, files, receiptPath, readiness = {} }) {
99
80
  assert(typeof receiptPath === 'string' && receiptPath.length, 'missing-receipt-path');
100
81
  assert(plan?.module === SITE_MODULE && Array.isArray(files) && files.some(file => file.path === 'index.html'), 'invalid-site-assets');
101
82
  const zones = await api.zoneRequest(`/zones?name=${plan.domain}&account.id=${api.accountId}`);
102
83
  const zone = Array.isArray(zones) ? zones.find(row => row?.name === plan.domain && row.account?.id === api.accountId) : null;
103
84
  if (!zone) throw refuse('site-zone-missing', 'zone', plan.domain);
104
85
  // Preflight: the Worker and each hostname are the kit's own or absent before any write.
105
- const existed = await ownWorker(api, plan.worker);
86
+ const previous = await ownedSiteVersion(api, plan.worker);
106
87
  const attached = await api.request(`/workers/domains?zone_id=${zone.id}`);
107
88
  assert(Array.isArray(attached), 'invalid-domain-list');
108
89
  for (const hostname of plan.hostnames) {
109
90
  const row = attached.find(item => item?.hostname === hostname);
110
- if (row && (!existed || row.service !== plan.worker)) throw refuse('resource-name-collision', 'domain', hostname);
91
+ if (row && (!previous || row.service !== plan.worker)) throw refuse('resource-name-collision', 'domain', hostname);
111
92
  }
112
93
 
113
94
  const fingerprint = hash({ files: files.map(({ path, sha256 }) => ({ path, sha256 })), config: plan.assetsConfig, module: hash(SITE_WORKER), compatibilityDate: plan.compatibilityDate });
@@ -116,9 +97,14 @@ export async function siteDeploy({ api, plan, files, receiptPath }) {
116
97
  body.set('metadata', new Blob([JSON.stringify({ main_module: SITE_MODULE, compatibility_date: plan.compatibilityDate, bindings: [{ type: 'assets', name: 'ASSETS' }],
117
98
  assets: { jwt, config: { ...plan.assetsConfig, run_worker_first: true } }, annotations: { 'workers/message': `rer-site:${fingerprint}` } })], { type: 'application/json' }));
118
99
  body.set(SITE_MODULE, new Blob([SITE_WORKER], { type: 'application/javascript+module' }), SITE_MODULE);
119
- await api.request(`/workers/scripts/${plan.worker}`, { method: 'PUT', body });
120
- const uploaded = await servedVersion(api, plan.worker);
121
- assert(uploaded?.current && uploaded.annotation === `rer-site:${fingerprint}`, 'site-upload-not-live');
100
+ const window = readiness.window ?? readinessWindow(readiness);
101
+ window.start();
102
+ api = readinessAPI(api, window, 'site-upload-timeout');
103
+ const upload = await api.request(`/workers/scripts/${plan.worker}`, { method: 'PUT', body });
104
+ const versionId = uploadVersionId(upload);
105
+ assert(versionId, 'invalid-upload-version');
106
+ const readback = () => waitForSiteUpload({ api, worker: plan.worker, versionId, fingerprint, previous, window });
107
+ await readback();
122
108
  await workerOrigin(api, plan.worker, { enable: true, workersDev: false });
123
109
  for (const hostname of plan.hostnames) {
124
110
  if (attached.some(row => row?.hostname === hostname)) continue;
@@ -126,7 +112,7 @@ export async function siteDeploy({ api, plan, files, receiptPath }) {
126
112
  assert(row?.hostname === hostname && row.service === plan.worker && typeof row.id === 'string', 'invalid-domain-attach');
127
113
  }
128
114
  // Readback: the kit's Worker live and both domains on it.
129
- assert(await ownWorker(api, plan.worker), 'site-worker-missing');
115
+ await readback();
130
116
  const domains = await api.request(`/workers/domains?zone_id=${zone.id}`);
131
117
  for (const hostname of plan.hostnames) assert(domains.some(row => row.hostname === hostname && row.service === plan.worker), 'domain-readback-mismatch');
132
118
  const receipt = { worker: plan.worker, domain: plan.domain, zoneId: zone.id, fingerprint, files: files.length, hostnames: plan.hostnames, redirect: plan.redirects[0] };
@@ -2,14 +2,20 @@ import { readinessAPI } from './readiness-window.mjs';
2
2
  import { activeWorker, deployedFingerprint } from './worker.mjs';
3
3
  import { assert, uuid } from './validation.mjs';
4
4
 
5
- // Poll only the missing fresh Worker or the exact pre-upload version. A new
6
- // unrelated version is drift, even during propagation; provider errors fail.
7
- export async function uploadedWorker({ api, worker, upload, previous, fingerprint, window }) {
5
+ /** Normalize the version identity returned by the script upload endpoint. */
6
+ export function uploadVersionId(upload) {
8
7
  const rawVersion = typeof upload?.deployment_id === 'string' ? upload.deployment_id.toLowerCase() : upload?.deployment_id;
9
8
  // The legacy upload API also returns the UUID without separators (Wrangler
10
9
  // normalizes this field before treating it as its uploaded version ID).
11
10
  const versionId = typeof rawVersion === 'string' && /^[a-f0-9]{32}$/i.test(rawVersion) ? rawVersion.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, '$1-$2-$3-$4-$5').toLowerCase() : rawVersion;
12
11
  if (versionId !== undefined) assert(uuid(versionId), 'invalid-upload-version');
12
+ return versionId;
13
+ }
14
+
15
+ // Poll only the missing fresh Worker or the exact pre-upload version. A new
16
+ // unrelated version is drift, even during propagation; provider errors fail.
17
+ export async function uploadedWorker({ api, worker, upload, previous, fingerprint, window }) {
18
+ const versionId = uploadVersionId(upload);
13
19
  const bounded = readinessAPI(api, window, 'uploaded-worker-timeout');
14
20
  while (window.remaining() > 0) {
15
21
  const current = await activeWorker(bounded, worker);
@@ -8,8 +8,6 @@ import { foreignKeyLinks } from './order.mjs';
8
8
  * the whole erasure stays linear in its rows. The slice is verified inside the
9
9
  * step's batch; anything the queue does not know about (rows written after it
10
10
  * was built) is found by the full collection that runs once the queue is empty. */
11
- const seq = n => String(n).padStart(10, '0');
12
-
13
11
  export const clearQueue = (db, id) => statement(db, "DELETE FROM rer_erasure_scope WHERE operation=? AND kind='queue'", id);
14
12
 
15
13
  /** Replaces the operation's queue with `entries`; chunks stay small enough for one bound value. */
@@ -45,7 +45,7 @@ async function main() {
45
45
  let result;
46
46
  if (verb === 'run') {
47
47
  const statusCommand = [process.execPath, fileURLToPath(import.meta.url), 'status', '--json'].map(shellQuote).join(' ');
48
- result = await queue.run(command, { ...options, onWaiting: (reason, details) => {
48
+ result = await queue.run(command, { ...options, onWaiting: (_reason, details) => {
49
49
  if (!options.json) console.error(`${waitingMessage(details)}\nInspect the queue with: ${statusCommand}`);
50
50
  } });
51
51
  if (result.timedOut) console.error(`Job queue max-wait expired: ${result.reason} (exit ${result.code})`);
@@ -26,7 +26,7 @@ export function knowsKey(jwks, kid) {
26
26
 
27
27
  export async function verifyIdToken(parsed, jwks, link, provider, settings, transaction, tokens, now) {
28
28
  check(!Object.hasOwn(parsed.claims, 'events'), 'oidc_invalid_id_token');
29
- const { header, claims } = parsed;
29
+ const { claims } = parsed;
30
30
  await verifySignature(parsed, jwks);
31
31
  // The token's issuer is the pinned issuer, and its audience is this client.
32
32
  check(claims.iss === link.issuer && typeof claims.sub === 'string' && claims.sub.length > 0 && claims.sub.length <= 255, 'oidc_issuer_mismatch');
@@ -25,7 +25,7 @@ export async function appliedMigrations(db) {
25
25
  // earlier prefix. Restored rows never pass through a later migration, so one
26
26
  // that writes or rewrites restorable data is refused, whether or not it changes
27
27
  // the schema.
28
- async function placement(db,manifest,cat,replay,applied) {
28
+ async function placement(manifest,cat,replay,applied) {
29
29
  insist(canonicalJson(replay.names)===canonicalJson(applied) && await replay.schemaAfter(applied)===cat.identity,'schema_mismatch');
30
30
  let earlier,unapplied;
31
31
  if(manifest.migrations) {
@@ -62,7 +62,7 @@ export async function sameSchemaMigrations(db,manifest,cat,migrations) {
62
62
  if(!applied)return;
63
63
  insist(!manifest.migrations || manifest.migrations.every(name=>applied.includes(name)),'schema_mismatch:migrations');
64
64
  if(!migrations){insist(!manifest.migrations || applied.every(name=>manifest.migrations.includes(name)),'schema_mismatch:migrations');return;}
65
- await placement(db,manifest,cat,await migrations(),applied);
65
+ await placement(manifest,cat,await migrations(),applied);
66
66
  }
67
67
  // An archive made before later migrations restores only when those migrations
68
68
  // are additive: new tables, new columns that take a default, or indexes (which
@@ -71,7 +71,7 @@ export async function additiveBridge(db,manifest,cat,migrations) {
71
71
  insist(migrations,'schema_mismatch');
72
72
  const replay=await migrations(),applied=await appliedMigrations(db)??[];
73
73
  insist(!manifest.migrations || manifest.migrations.every(name=>applied.includes(name)),'schema_mismatch:migrations');
74
- const {earlier}=await placement(db,manifest,cat,replay,applied);
74
+ const {earlier}=await placement(manifest,cat,replay,applied);
75
75
  const columns={};
76
76
  for(const table of manifest.tables) {
77
77
  const target=cat.tables.find(t=>t.name===table.name),label=':'+table.name;
@@ -1,4 +1,4 @@
1
- import { all, insist, quote, PortabilityError } from './storage.mjs';
1
+ import { all, insist, quote } from './storage.mjs';
2
2
  import { canonicalJson, sha256, base64, unbase64, utf8 } from './hash.mjs';
3
3
  /** Tables no archive carries: SQLite/Cloudflare internals, migration ledgers,
4
4
  * portability bookkeeping, development outboxes, operator identity and dependency
@@ -1,4 +1,4 @@
1
- import { lstat, readFile, mkdir, open, rename } from 'node:fs/promises';
1
+ import { lstat, mkdir, open, rename } from 'node:fs/promises';
2
2
  import { isAbsolute, dirname } from 'node:path';
3
3
  import { constants } from 'node:fs';
4
4
  export function options(args) {
@@ -12,7 +12,7 @@ export async function recoverLegacyArchive({db,buckets,policy:input,migrations},
12
12
  insist(typeof output==='string' && output!==path,'legacy_output_required',409);
13
13
  insist(!(await lstat(output).catch(error=>{if(error.code==='ENOENT')return null;throw error;})),'legacy_output_exists',409);
14
14
  // Whole-file verification precedes even staging/audit schema writes.
15
- for await(const record of fileRecords(path,readLegacyArchive)){}
15
+ for await(const _record of fileRecords(path,readLegacyArchive)){}
16
16
  const context={db,buckets,config:policy(input),migrations},target=await legacyTargetState(db,migrations);
17
17
  await requireLegacyEmpty(context,target);
18
18
  let id,manifest,closing,integrity,status,complete=false;
@@ -45,16 +45,16 @@ export async function legacyTargetState(db,migrations) {
45
45
  }
46
46
  return {emptyTables:names.filter(name=>!seeded.some(s=>s.name===name)),seededTables:seeded};
47
47
  }
48
- const seededCondition=(db,table)=>[`(SELECT count(*) FROM ${quote(table.name)})=? AND NOT EXISTS(SELECT 1 FROM (${imageSql(table.name,table.columns)}) WHERE image NOT IN (SELECT value FROM json_each(?)))`,table.images.length,JSON.stringify(table.images)];
48
+ const seededCondition=table=>[`(SELECT count(*) FROM ${quote(table.name)})=? AND NOT EXISTS(SELECT 1 FROM (${imageSql(table.name,table.columns)}) WHERE image NOT IN (SELECT value FROM json_each(?)))`,table.images.length,JSON.stringify(table.images)];
49
49
  export async function requireLegacyEmpty({db,buckets},{emptyTables,seededTables=[]}) {
50
50
  for(const table of emptyTables)insist(!(await statement(db,`SELECT 1 FROM ${quote(table)} LIMIT 1`).first()),'legacy_target_not_empty:'+table,409);
51
- for(const table of seededTables){const [condition,...values]=seededCondition(db,table);insist((await statement(db,`SELECT CASE WHEN (${condition}) THEN 1 ELSE 0 END AS ok`,...values).first()).ok===1,'legacy_target_not_empty:'+table.name,409);}
51
+ for(const table of seededTables){const [condition,...values]=seededCondition(table);insist((await statement(db,`SELECT CASE WHEN (${condition}) THEN 1 ELSE 0 END AS ok`,...values).first()).ok===1,'legacy_target_not_empty:'+table.name,409);}
52
52
  for(const [name,bucket] of Object.entries(buckets))insist(!(await bucket.list({limit:1})).objects.length,'legacy_bucket_not_empty:'+name,409);
53
53
  }
54
54
  export function legacyEmptyGuards(db,row) {
55
55
  const recovery=row.metadata.legacyRecovery;
56
56
  return [...(recovery?.emptyTables??[]).map(table=>guard(db,`NOT EXISTS(SELECT 1 FROM ${quote(table)})`)),
57
- ...(recovery?.seededTables??[]).map(table=>{const [condition,...values]=seededCondition(db,table);return guard(db,condition,...values);})];
57
+ ...(recovery?.seededTables??[]).map(table=>{const [condition,...values]=seededCondition(table);return guard(db,condition,...values);})];
58
58
  }
59
59
  export async function ensureLegacyAudit(db) {
60
60
  // Additive sidecar, installed only by recovery; existing product migrations
@@ -1,4 +1,4 @@
1
- import { stream, run, all, insist } from './storage.mjs';
1
+ import { run, all, insist } from './storage.mjs';
2
2
  import { base64, unbase64, StreamHash } from './hash.mjs';
3
3
  export async function* byteChunks(source,limit) {
4
4
  const reader=(source instanceof ReadableStream?source:new Response(source).body).getReader();
@@ -1,7 +1,7 @@
1
1
  import { mkdtemp,rm } from 'node:fs/promises';
2
2
  import { tmpdir } from 'node:os';
3
3
  import { shared } from '../../cli/shared.mjs';
4
- import { execFileSync, spawn } from 'node:child_process';
4
+ import { execFileSync } from 'node:child_process';
5
5
  import { realpathSync, existsSync, lstatSync, mkdirSync } from 'node:fs';
6
6
  import { resolve, dirname, relative, isAbsolute } from 'node:path';
7
7
  import { stripVTControlCharacters } from 'node:util';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runeditrun/rer",
3
- "version": "0.2.23",
3
+ "version": "0.2.24",
4
4
  "description": "The rer command: install, set up, run and launch Run Edit Run checkouts.",
5
5
  "license": "MIT",
6
6
  "type": "module",