@runeditrun/rer 0.2.22 → 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 (72) hide show
  1. package/cli/README.md +19 -0
  2. package/cli/bounded.mjs +5 -1
  3. package/cli/delivery-declarations.mjs +2 -2
  4. package/cli/demo-host.mjs +1 -1
  5. package/cli/deploy-command.mjs +7 -5
  6. package/cli/deploy-inputs.mjs +4 -1
  7. package/cli/deploy-observation.mjs +20 -2
  8. package/cli/deploy-owner.mjs +12 -7
  9. package/cli/deploy.mjs +9 -6
  10. package/cli/deployment-record.mjs +52 -0
  11. package/cli/local.mjs +11 -3
  12. package/cli/managed-server.mjs +2 -1
  13. package/cli/output.mjs +1 -1
  14. package/cli/production-auth.mjs +7 -27
  15. package/cli/production-mail.mjs +72 -0
  16. package/cli/signin.mjs +11 -6
  17. package/cli/site.mjs +2 -2
  18. package/global/CHANGELOG.md +25 -0
  19. package/modules/container/README.md +8 -3
  20. package/modules/container/adapters/d1.mjs +1 -1
  21. package/modules/container/build.mjs +1 -1
  22. package/modules/container/host.mjs +7 -4
  23. package/modules/container/transfer.mjs +0 -1
  24. package/modules/container/websockets.mjs +54 -0
  25. package/modules/container/worker-engine.mjs +3 -3
  26. package/modules/demo/README.md +4 -0
  27. package/modules/demo/label.mjs +7 -4
  28. package/modules/demo/responses.mjs +1 -1
  29. package/modules/demo-host/README.md +34 -9
  30. package/modules/demo-host/capacity.mjs +2 -2
  31. package/modules/demo-host/container.mjs +19 -7
  32. package/modules/demo-host/placement.mjs +19 -5
  33. package/modules/demo-host/plan.mjs +11 -6
  34. package/modules/demo-host/start-script.mjs +2 -1
  35. package/modules/demo-host/startup.mjs +3 -3
  36. package/modules/demo-host/worker.mjs +36 -13
  37. package/modules/deploy-kit/README.md +35 -2
  38. package/modules/deploy-kit/diagnostics.mjs +16 -1
  39. package/modules/deploy-kit/owner-cli.mjs +3 -3
  40. package/modules/deploy-kit/owner-remove.mjs +23 -5
  41. package/modules/deploy-kit/owner-signin.mjs +1 -1
  42. package/modules/deploy-kit/pagination.mjs +99 -0
  43. package/modules/deploy-kit/provider.mjs +30 -70
  44. package/modules/deploy-kit/r2-objects.mjs +6 -17
  45. package/modules/deploy-kit/site-readback.mjs +67 -0
  46. package/modules/deploy-kit/site.mjs +17 -31
  47. package/modules/deploy-kit/uploaded-worker.mjs +9 -3
  48. package/modules/erasure/queue.mjs +0 -2
  49. package/modules/job-queue/cli.mjs +1 -1
  50. package/modules/mail/README.md +14 -5
  51. package/modules/mail/src/adapters/rer/contract.mjs +33 -5
  52. package/modules/mail/src/adapters/rer/index.mjs +4 -1
  53. package/modules/mail/src/core/transport.mjs +9 -3
  54. package/modules/operator-auth/README.md +5 -0
  55. package/modules/operator-auth/identity/email-link/index.mjs +5 -0
  56. package/modules/operator-auth/identity/oidc/jwt.mjs +1 -1
  57. package/modules/portability/bridge.mjs +3 -3
  58. package/modules/portability/catalogue.mjs +1 -1
  59. package/modules/portability/cli-options.mjs +1 -1
  60. package/modules/portability/legacy-files.mjs +1 -1
  61. package/modules/portability/legacy-safety.mjs +3 -3
  62. package/modules/portability/payloads.mjs +1 -1
  63. package/modules/release/source.mjs +1 -1
  64. package/onboarding/local/operation.mjs +4 -2
  65. package/onboarding/local/owner.mjs +9 -3
  66. package/onboarding/local/state-files.mjs +17 -0
  67. package/onboarding/process-diagnostics.mjs +25 -5
  68. package/package.json +1 -1
  69. package/scripts/publish-check/README.md +5 -2
  70. package/scripts/publish-check/content_identities.py +1 -1
  71. package/scripts/publish-check/json_identity.py +10 -1
  72. package/scripts/publish-check/public_hygiene.py +30 -4
@@ -0,0 +1,67 @@
1
+ import { workerSettings } from './owner.mjs';
2
+ import { readinessAPI } from './readiness-window.mjs';
3
+ import { assert } from './validation.mjs';
4
+
5
+ const ANNOTATION = /^rer-site:[0-9a-f]{64}$/;
6
+ function refuse(code, worker) { return Object.assign(new Error(code), { resource: `worker:${worker}` }); }
7
+
8
+ /** A provider snapshot; only a fresh upload may have incomplete visibility. */
9
+ export async function servedVersion(api, worker, { pending = false } = {}) {
10
+ if (!await workerSettings(api, worker)) return null;
11
+ const { deployments } = await api.request(`/workers/scripts/${worker}/deployments`);
12
+ assert(Array.isArray(deployments) && (pending || deployments.length > 0), 'invalid-deployments');
13
+ const [latest] = deployments;
14
+ const single = latest?.versions?.length === 1 && latest.versions[0].percentage === 100 ? latest.versions[0].version_id : null;
15
+ const newest = (await api.request(`/workers/scripts/${worker}/versions`))?.items?.[0]?.id;
16
+ let version;
17
+ try { version = single ? await api.request(`/workers/scripts/${worker}/versions/${single}`) : null; }
18
+ catch (error) { if (!pending || error.status !== 404) throw error; }
19
+ assert(!version || version.id === single, 'invalid-active-version');
20
+ const annotation = version?.annotations?.['workers/message'];
21
+ return { kit: typeof annotation === 'string' && ANNOTATION.test(annotation), annotation,
22
+ current: Boolean(single) && single === newest, versionId: single, newest, deploymentId: latest?.id, deployed: Boolean(latest), visible: Boolean(version) };
23
+ }
24
+
25
+ /** Preflight remains strict: no propagation allowance from an earlier invocation. */
26
+ export async function ownedSiteVersion(api, worker) {
27
+ const served = await servedVersion(api, worker);
28
+ if (!served) return null;
29
+ if (!served.kit) throw refuse('resource-name-collision', worker);
30
+ if (!served.current) throw refuse('owned-resource-replaced', worker);
31
+ return served;
32
+ }
33
+
34
+ /** Only the upload response and the exact preflight version may appear while
35
+ * Cloudflare's deployment and version reads converge. Never replay the upload. */
36
+ export async function waitForSiteUpload({ api, worker, versionId, fingerprint, previous, window }) {
37
+ const bounded = readinessAPI(api, window, 'site-upload-timeout');
38
+ const annotation = `rer-site:${fingerprint}`;
39
+ let lastSeen = null, delay = 2000;
40
+ const timeout = cause => Object.assign(refuse('site-upload-timeout', worker), { lastSeen, cause });
41
+ while (window.remaining() > 0) {
42
+ try { lastSeen = await servedVersion(bounded, worker, { pending: true }); }
43
+ catch (error) { if (window.remaining() <= 0 || error.name === 'TimeoutError') throw timeout(error); throw error; }
44
+ if (lastSeen) {
45
+ const allowed = id => !id || id === versionId || id === previous?.versionId;
46
+ const expected = lastSeen.versionId === versionId ? annotation : previous?.annotation;
47
+ if (!allowed(lastSeen.versionId) || !allowed(lastSeen.newest) || lastSeen.deployed && !lastSeen.versionId
48
+ || lastSeen.visible && lastSeen.annotation !== expected
49
+ || lastSeen.versionId === previous?.versionId && lastSeen.deploymentId !== previous.deploymentId)
50
+ throw refuse('owned-resource-replaced', worker);
51
+ if (lastSeen.current && lastSeen.versionId === versionId && lastSeen.annotation === annotation) return lastSeen;
52
+ }
53
+ await window.sleep(Math.min(delay, window.remaining()));
54
+ delay = Math.min(delay * 2, 10000);
55
+ }
56
+ throw timeout();
57
+ }
58
+
59
+ /** Only provider UUIDs and kit fingerprints are safe to include in CLI output. */
60
+ export function siteReadbackSummary(error) {
61
+ if (error.message !== 'site-upload-timeout') return '';
62
+ const seen = error.lastSeen;
63
+ if (!seen) return ' Last readback: Worker not visible.';
64
+ const id = value => typeof value === 'string' && /^[a-f0-9-]{36}$/i.test(value) ? value : 'not visible';
65
+ const fingerprint = typeof seen.annotation === 'string' && ANNOTATION.test(seen.annotation) ? seen.annotation.slice('rer-site:'.length) : 'not visible';
66
+ return ` Last readback: serving ${id(seen.versionId)}, newest ${id(seen.newest)}, fingerprint ${fingerprint}.`;
67
+ }
@@ -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})`);
@@ -148,17 +148,26 @@ await sender.send({
148
148
 
149
149
  Pass `relayUrl` only for `rer`. It must be the assigned HTTPS URL, without URL
150
150
  credentials, query or fragment. The adapter sends one POST with Bearer
151
- authentication and `redirect: 'error'`. Its JSON contains one mailbox `to`,
151
+ authentication and `redirect: 'manual'` (Cloudflare Workers reject
152
+ `redirect: 'error'`); a 3xx is never followed. Its JSON contains one mailbox `to`,
152
153
  `subject`, `text` and optional nonempty `html`. RER owns the sender and
153
154
  installation identity; `from` and `deliveryId` are not transmitted. Subjects
154
155
  must be nonempty, without newlines and at most 200 characters. Plain text is
155
156
  required; each body is limited to 65,536 characters and the JSON to 128 KiB.
156
- Other recipients, attachments, reply-to, headers and unsubscribe features are
157
- unsupported and fail before dispatch.
157
+
158
+ The relay sets `Auto-Submitted: auto-generated` on every message, so the
159
+ adapter accepts that header and does not transmit it; other `Auto-Submitted`
160
+ values are refused. `List-Unsubscribe` (one or two `<https:…>`/`<mailto:…>`
161
+ entries, at most 2,000 characters) travels as `listUnsubscribe`, and
162
+ `List-Unsubscribe-Post: List-Unsubscribe=One-Click` as `listUnsubscribePost:
163
+ true`, which requires an HTTPS entry. The structured `unsubscribe` request
164
+ assembles the same headers. Other recipients, attachments, reply-to and every
165
+ other header are unsupported and fail before dispatch.
158
166
 
159
167
  HTTP 202 returns `accepted` with no provider message ID. HTTP 400, 401, 402,
160
- 403 and 413 return `rejected`; 429 returns `rate_limited`; 502 and 503 return
161
- `uncertain`. Network failures and refused redirects are also `uncertain`.
168
+ 403 and 413 return `rejected`, and so does any 3xx (`provider_redirected`);
169
+ 429 returns `rate_limited`; 502 and 503 return `uncertain`. Network failures
170
+ are also `uncertain`.
162
171
  The usual application delivery policy decides whether to retry or stop; the
163
172
  adapter makes no retries and never reads or logs provider error bodies.
164
173
 
@@ -1,5 +1,9 @@
1
1
  import { fail } from '../../core/errors.mjs';
2
2
 
3
+ // The relay's own List-Unsubscribe rule: one or two <https:…>/<mailto:…> entries.
4
+ const listEntries = /^[ \t]*<([^<>\s]+)>[ \t]*(?:,[ \t]*<([^<>\s]+)>[ \t]*)?$/;
5
+ const relayMailbox = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
6
+
3
7
  export function validateRerConfig(config) {
4
8
  let url;
5
9
  try { url = new URL(config.relayUrl); } catch { fail('rer_relay_url_required'); }
@@ -8,14 +12,38 @@ export function validateRerConfig(config) {
8
12
  url.href !== config.relayUrl) fail('invalid_rer_relay_url');
9
13
  }
10
14
 
15
+ /** Relay request fields for the headers it supports; it sets Auto-Submitted: auto-generated itself. */
16
+ export function rerHeaderFields(headers = {}) {
17
+ const fields = {};
18
+ for (const [name, value] of Object.entries(headers)) {
19
+ const lower = name.toLowerCase();
20
+ if (lower === 'auto-submitted') {
21
+ if (value.trim().toLowerCase() !== 'auto-generated') fail('unsupported_provider_setting');
22
+ } else if (lower === 'list-unsubscribe') fields.listUnsubscribe = value;
23
+ else if (lower === 'list-unsubscribe-post') {
24
+ if (value !== 'List-Unsubscribe=One-Click') fail('invalid_list_unsubscribe_post');
25
+ fields.listUnsubscribePost = true;
26
+ } else fail('unsupported_provider_setting');
27
+ }
28
+ let https = false;
29
+ if (fields.listUnsubscribe !== undefined) {
30
+ const entries = fields.listUnsubscribe.length <= 2000 && fields.listUnsubscribe.match(listEntries);
31
+ if (!entries) fail('invalid_list_unsubscribe');
32
+ for (const entry of entries.slice(1).filter(Boolean)) {
33
+ if (/^https:\/\//i.test(entry)) https = true;
34
+ else if (!/^mailto:/i.test(entry) || !relayMailbox.test(entry.slice(7))) fail('invalid_list_unsubscribe');
35
+ }
36
+ }
37
+ if (fields.listUnsubscribePost && !https) fail('invalid_list_unsubscribe_post');
38
+ return fields;
39
+ }
40
+
11
41
  export function validateRerMessage(message) {
12
42
  if (message.to.length !== 1) fail('rer_one_recipient_required');
13
43
  // The relay cannot preserve these features. Refuse them rather than silently
14
- // dropping recipients, attachments, threading or unsubscribe instructions.
15
- if (['cc', 'bcc', 'replyTo', 'attachments', 'unsubscribe'].some(field => message[field] !== undefined) ||
16
- (message.headers !== undefined && Reflect.ownKeys(message.headers).length > 0)) {
17
- fail('unsupported_provider_setting');
18
- }
44
+ // dropping recipients, attachments or threading.
45
+ if (['cc', 'bcc', 'replyTo', 'attachments'].some(field => message[field] !== undefined)) fail('unsupported_provider_setting');
46
+ rerHeaderFields(message.headers);
19
47
  if (!message.subject.trim() || message.subject.length > 200) fail('invalid_subject');
20
48
  if (!message.text?.trim() || message.text.length > 65_536 ||
21
49
  (message.html !== undefined && message.html.length > 65_536)) fail('invalid_body');
@@ -1,6 +1,7 @@
1
1
  import { validateConfig, validateMessage, parseAddress } from '../../core/validation.mjs';
2
2
  import { fail } from '../../core/errors.mjs';
3
- import { requestProvider, classifyHttpStatus, sendResult } from '../../core/transport.mjs';
3
+ import { requestProvider, redirected, classifyHttpStatus, sendResult } from '../../core/transport.mjs';
4
+ import { rerHeaderFields } from './contract.mjs';
4
5
 
5
6
  /** One Managed relay submission. RER owns the sender and installation identity. */
6
7
  export async function sendRer(config, message, dependencies = {}) {
@@ -12,6 +13,7 @@ export async function sendRer(config, message, dependencies = {}) {
12
13
  subject: message.subject,
13
14
  text: message.text,
14
15
  ...(message.html ? { html: message.html } : {}),
16
+ ...rerHeaderFields(message.headers),
15
17
  });
16
18
  if (new TextEncoder().encode(body).byteLength > 128 * 1024) fail('rer_message_too_large');
17
19
  const response = await requestProvider(config.relayUrl, {
@@ -21,6 +23,7 @@ export async function sendRer(config, message, dependencies = {}) {
21
23
  body,
22
24
  }, dependencies);
23
25
  if (response === null) return sendResult('rer', 'uncertain', null, 'transport_uncertain');
26
+ if (response === redirected) return sendResult('rer', 'rejected', null, 'provider_redirected');
24
27
  // Do not consume provider bodies: they may contain credentials or personal data.
25
28
  if (response.status === 202) return sendResult('rer', 'accepted');
26
29
  const status = classifyHttpStatus(response.status) ?? 'uncertain';
@@ -1,6 +1,10 @@
1
1
  import { validateSendResult } from './validation.mjs';
2
2
 
3
- /** One bounded request. Fetch exceptions never escape with credential/body text. */
3
+ /** What a caller that asked for redirect: 'error' receives for any 3xx. */
4
+ export const redirected = Object.freeze({ redirected: true });
5
+
6
+ /** One bounded request. Fetch exceptions never escape with credential/body text.
7
+ * Workers reject redirect: 'error', so every request is manual and never follows. */
4
8
  export async function requestProvider(url, init, dependencies = {}) {
5
9
  const timeout = dependencies.timeoutMilliseconds ?? 15_000;
6
10
  let timer;
@@ -8,8 +12,10 @@ export async function requestProvider(url, init, dependencies = {}) {
8
12
  const deadline = new Promise(resolve => { timer = setTimeout(() => resolve(null), timeout); });
9
13
  return await Promise.race([
10
14
  Promise.resolve().then(() => (dependencies.fetch ?? globalThis.fetch)(url, {
11
- ...init, redirect: init.redirect === 'error' ? 'error' : 'manual', signal: AbortSignal.timeout(timeout),
12
- })).then(response => response instanceof Response ? response : null, () => null),
15
+ ...init, redirect: 'manual', signal: AbortSignal.timeout(timeout),
16
+ })).then(response => !(response instanceof Response) ? null
17
+ : init.redirect === 'error' && (response.type === 'opaqueredirect' || response.status >= 300 && response.status < 400) ? redirected
18
+ : response, () => null),
13
19
  deadline,
14
20
  ]);
15
21
  } catch { return null; }
@@ -425,6 +425,11 @@ owner invitation cancellation, and owner activation or deactivation. Unchanged
425
425
  roles/statuses and non-owner status changes retain their usual authorization.
426
426
  The existing version fences and last-active-owner protection still apply after
427
427
  the proof succeeds.
428
+ With the `RER_DEMO=1` demo guard active, the configured owner's authenticated
429
+ session supplies this proof inside its own visitor container because mail is
430
+ mocked. Authentication still checks the container's own session secret and
431
+ store; another visitor's session is refused. Ordinary hosted instances keep
432
+ requiring the real proof described above.
428
433
  Creating a server-declared owner dev persona also requires the signed-in owner's
429
434
  fresh local proof in addition to the checkout key. Its creation repeats the actor
430
435
  session/version fence in the database write.
@@ -59,6 +59,11 @@ export function createEmailLinkLifecycle({ config: input, clock = Date.now, mail
59
59
  // Authentication already required the local key gate for this session.
60
60
  // Dev confirms actions directly rather than sending an unusable mail link.
61
61
  if (config.local && actor.kind === 'dev') return;
62
+ // The demo's mail is mocked. Its configured owner can confirm directly,
63
+ // after authentication against this visitor's isolated session store.
64
+ // Another demo's cookie cannot authenticate here; ordinary runtimes never
65
+ // install the demo guard, even if a caller supplies demo request headers.
66
+ if (demoEffectsEnabled() && actor.operator.role === 'owner' && actor.operator.email === config.OWNER_EMAIL) return;
62
67
  if (!await store.steppedUp(actor, clock() - STEP_UP_SECONDS * 1000, clock())) deny('step_up_required', 403);
63
68
  }
64
69
  const authenticate = request => store.authenticate(request);
@@ -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';
@@ -3,12 +3,14 @@ import { link, lstat, open, readFile, rename, unlink } from 'node:fs/promises'
3
3
  import { join } from 'node:path'
4
4
  import { privateDirectory } from './files.mjs'
5
5
  import { processStart } from './process-table.mjs'
6
- import {processName} from '../process-diagnostics.mjs'
6
+ import {processName,inspectionContext,uncheckedInspection} from '../process-diagnostics.mjs'
7
7
 
8
8
  const busy = async (path) => {
9
9
  let owner;try{owner=JSON.parse(await readFile(path,'utf8'));}catch{}
10
10
  const pid=Number.isSafeInteger(owner?.pid)&&owner.pid>0?owner.pid:undefined;
11
- const identity=pid?`PID ${pid} (${await processName(pid)})`:'an unidentified process';
11
+ let command='process';
12
+ if(pid)try{command=await processName(pid,inspectionContext({budgetMs:1000}));}catch(error){if(!uncheckedInspection(error))throw error;}
13
+ const identity=pid?`PID ${pid} (${command})`:'an unidentified process';
12
14
  return Object.assign(new Error(`An active local operation owns the state: ${identity}. Stop it with rer dev --stop or rer preview --stop; for an unmanaged process, stop PID ${pid ?? 'shown in the lock'} before retrying.`), {code: 'LOCAL_OPERATION_BUSY', ownerPid: pid});
13
15
  }
14
16
  const graceMs = 10000
@@ -1,6 +1,12 @@
1
1
  import {readdir,lstat,realpath} from 'node:fs/promises';
2
2
  import {basename,join} from 'node:path';
3
- import {DatabaseSync} from 'node:sqlite';
3
+
4
+ // Importing the owner-name helper must not load the database engine or emit
5
+ // its runtime warning before the launcher registers its worker.
6
+ async function openDatabase(path,options={}){
7
+ const {DatabaseSync}=await import('node:sqlite');
8
+ return new DatabaseSync(path,options);
9
+ }
4
10
 
5
11
  // The owner the zero-setup `rer dev` gives a checkout before its real owner is known.
6
12
  export function zeroSetupOwner(checkout){
@@ -21,7 +27,7 @@ export async function localOwners(root){
21
27
  const found=[];
22
28
  for(const path of await databases(root)){
23
29
  let db,owners=[];
24
- try{db=new DatabaseSync(path,{readOnly:true});
30
+ try{db=await openDatabase(path,{readOnly:true});
25
31
  const tables=new Set(db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all().map(row=>row.name));
26
32
  if(tables.has('oa_operators'))owners=db.prepare("SELECT email FROM oa_operators WHERE role='owner' AND status='active'").all();
27
33
  }finally{db?.close();}
@@ -47,7 +53,7 @@ export async function assertLocalOwner(root,email,failure=message=>Error(message
47
53
  // retires sign-in links issued to the placeholder.
48
54
  export async function replaceLocalOwner(root,from,to,failure=message=>Error(message)){
49
55
  for(const path of await databases(root)){
50
- const db=new DatabaseSync(path);
56
+ const db=await openDatabase(path);
51
57
  try{
52
58
  const tables=new Set(db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all().map(row=>row.name));
53
59
  if(!tables.has('oa_operators'))continue;
@@ -0,0 +1,17 @@
1
+ import {readdir} from 'node:fs/promises';
2
+ import {join} from 'node:path';
3
+
4
+ // Select only regular files in canonical state, including SQLite sidecars.
5
+ // Do not follow links into other checkouts or pass directories to lsof.
6
+ export async function stateFiles(directory) {
7
+ let entries;
8
+ try{entries=await readdir(directory,{withFileTypes:true});}
9
+ catch(error){if(error.code==='ENOENT')return [];throw error;}
10
+ const files=[];
11
+ for(const entry of entries){
12
+ const path=join(directory,entry.name);
13
+ if(entry.isDirectory())files.push(...await stateFiles(path));
14
+ else if(entry.isFile())files.push(path);
15
+ }
16
+ return files;
17
+ }
@@ -6,14 +6,15 @@ import {homedir} from 'node:os';
6
6
  import {isIP} from 'node:net';
7
7
  import {setTimeout as delay} from 'node:timers/promises';
8
8
  import {commandFailure,approvedFailure} from './failures.mjs';
9
+ import {stateFiles} from './local/state-files.mjs';
9
10
  const exec=promisify(execFile);
10
11
  // macOS supplies lsof in /usr/sbin, which a command PATH may omit.
11
12
  // Diagnose sockets with the system utility even when that PATH is constrained.
12
13
  const lsof=process.platform==='darwin'?'/usr/sbin/lsof':'lsof';
13
14
  // One monotonic budget covers every command in an ownership decision.
14
- export const inspectionContext=(details={})=>({deadline:performance.now()+15000,...details});
15
+ export const inspectionContext=({budgetMs=15000,...details}={})=>({deadline:performance.now()+budgetMs,budgetMs,...details});
15
16
  export const uncheckedInspection=error=>approvedFailure(error)?.ownershipUnchecked===true;
16
- const run=async(command,args,context=inspectionContext(),{maxBuffer=1024*1024,noMatch=false}={})=>{
17
+ const run=async(command,args,context=inspectionContext(),{maxBuffer=1024*1024,noMatch=false,positiveResult}={})=>{
17
18
  const invocation=[command,...args].join(' ').replaceAll(homedir(),'~');
18
19
  let failure;
19
20
  for(let attempt=0;attempt<2;attempt++){
@@ -22,6 +23,9 @@ const run=async(command,args,context=inspectionContext(),{maxBuffer=1024*1024,no
22
23
  const timeout=attempt===0?Math.min(3000,remaining):remaining;
23
24
  try{return (await exec(command,args,{timeout,maxBuffer})).stdout;}
24
25
  catch(error){
26
+ // A selected state file positively observed before a timeout still has a
27
+ // holder. Partial output can never establish the absence of a holder.
28
+ if(typeof error.stdout==='string'&&positiveResult?.(error.stdout))return error.stdout;
25
29
  // A quiet lsof exit 1 means no match, including a selected PID exiting
26
30
  // while other selected PIDs still return records. Diagnostic output instead
27
31
  // means inspection failed, even if lsof also returned partial records.
@@ -31,7 +35,7 @@ const run=async(command,args,context=inspectionContext(),{maxBuffer=1024*1024,no
31
35
  }
32
36
  const subject=context.port?`who uses port ${context.port}`:'local process ownership';
33
37
  const cause=failure?.startsWith('timed out')||performance.now()>=context.deadline?'in time (system busy)':'because the system process check failed';
34
- throw commandFailure(`Could not check ${subject} ${cause}. Run rer ${context.verb??'dev'} again. No other owner was identified.\nOwnership check: ${invocation} ${failure??'total inspection deadline exhausted'} (at most 2 attempts; shared total bound 15000 ms).`,{ownershipUnchecked:true});
38
+ throw commandFailure(`Could not check ${subject} ${cause}. Run rer ${context.verb??'dev'} again. No other owner was identified.\nOwnership check: ${invocation} ${failure??'total inspection deadline exhausted'} (at most 2 attempts; shared total bound ${context.budgetMs??15000} ms).`,{ownershipUnchecked:true});
35
39
  };
36
40
 
37
41
  export async function processName(pid,context=inspectionContext()){
@@ -46,6 +50,7 @@ function records(text,socketTypes=false){
46
50
  let record,family;const rows=[];
47
51
  for(const line of text.split('\n')){
48
52
  if(line[0]==='p'){record={pid:Number(line.slice(1)),names:[],...(socketTypes?{sockets:[]}: {})};family=undefined;rows.push(record);}
53
+ else if(line[0]==='c'&&record)record.command=basename(line.slice(1)).replace(/[^a-zA-Z0-9._-]/g,'').slice(0,80);
49
54
  else if(line[0]==='f')family=undefined;
50
55
  else if(line[0]==='t')family=['IPv4','IPv6'].includes(line.slice(1))?line.slice(1):undefined;
51
56
  else if(line[0]==='n'&&record){const name=line.slice(1);record.names.push(name);if(socketTypes)record.sockets.push({name,family});}
@@ -192,8 +197,23 @@ export async function localProcessConflict(root,savedOrigin,context=inspectionCo
192
197
  let held;
193
198
  if(hasState){
194
199
  if(process.platform==='linux'){for(const pid of await linuxPids())if(!lineage.has(pid)&&(await descriptors(pid)).some(path=>path.startsWith(state+'/'))){held={pid};break;}}
195
- else held=records(await run(lsof,['-nP','-b','-w','-Fpn'],context,{maxBuffer:16*1024*1024})).find(row=>!lineage.has(row.pid)&&row.names.some(path=>path.startsWith(state+'/')));
196
- if(held)return {pid:held.pid,command:await processName(held.pid,context),reason:'the canonical local state'};
200
+ else {
201
+ const files=await stateFiles(state);
202
+ // Path selection needs stat, so -b (which skips stat) is inappropriate here.
203
+ // No system-wide open-file output, and no query at all for empty state.
204
+ if(files.length){
205
+ const budgetMs=context.verb==='dev'?3000:context.budgetMs??15000;
206
+ const stateContext=inspectionContext({verb:context.verb,budgetMs,deadline:Math.min(context.deadline,performance.now()+budgetMs)});
207
+ const holder=text=>records(text).find(row=>!lineage.has(row.pid)&&row.names.some(path=>path.startsWith(state+'/')));
208
+ held=holder(await run(lsof,['-nP','-w','-Fpcn','--',...files],stateContext,{positiveResult:holder}));
209
+ }
210
+ }
211
+ if(held){
212
+ // A slow optional label cannot discard a positively observed holder.
213
+ let command=held.command;
214
+ if(!command)try{command=await processName(held.pid,context);}catch(error){if(!uncheckedInspection(error))throw error;}
215
+ return {pid:held.pid,command:command||'process',reason:'the canonical local state'};
216
+ }
197
217
  }
198
218
  let all;
199
219
  try{all=await listeners(undefined,context);}