@runeditrun/rer 0.2.24 → 0.2.25
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/README.md +16 -7
- package/cli/browser-runtime.mjs +65 -0
- package/cli/deploy-claim-request.mjs +5 -4
- package/cli/deploy-command.mjs +3 -3
- package/cli/deploy-inputs.mjs +2 -1
- package/cli/deploy.mjs +16 -4
- package/cli/export.mjs +1 -1
- package/cli/hydration/browser.mjs +3 -9
- package/cli/inspect.mjs +3 -1
- package/cli/lifecycle.mjs +4 -1
- package/global/CHANGELOG.md +21 -0
- package/global/README.md +16 -7
- package/global/browser-runtime.mjs +45 -0
- package/global/cli/layout.mjs +8 -4
- package/global/main.mjs +13 -3
- package/install.sh +56 -3
- package/modules/container/README.md +6 -0
- package/modules/container/build.mjs +2 -1
- package/modules/container/image-variables.mjs +27 -0
- package/modules/delivery-producer/declarations.mjs +6 -1
- package/modules/delivery-producer/mail.mjs +66 -0
- package/modules/demo/label.mjs +5 -2
- package/modules/deploy-kit/README.md +6 -4
- package/modules/deploy-kit/asset-health.mjs +28 -0
- package/modules/deploy-kit/d1-import.mjs +22 -4
- package/modules/deploy-kit/diagnostics.mjs +30 -0
- package/modules/deploy-kit/health.mjs +2 -23
- package/modules/deploy-kit/migration-ledger.mjs +6 -2
- package/modules/deploy-kit/migration-retry.mjs +91 -0
- package/modules/deploy-kit/migrations.mjs +9 -5
- package/modules/deploy-kit/owner-cli.mjs +6 -3
- package/modules/deploy-kit/owner-remove.mjs +87 -65
- package/modules/deploy-kit/provider-transport.mjs +28 -5
- package/modules/deploy-kit/provider.mjs +9 -6
- package/modules/deploy-kit/worker.mjs +5 -2
- package/modules/operator-auth/README.md +10 -2
- package/modules/operator-auth/config.mjs +1 -0
- package/modules/operator-auth/conformance/presentation.mjs +2 -2
- package/modules/operator-auth/identity/email-link/confirmation.mjs +2 -1
- package/modules/operator-auth/identity/email-link/expiry.mjs +8 -0
- package/modules/operator-auth/identity/email-link/index.mjs +2 -1
- package/modules/operator-auth/identity/email-link/message.mjs +3 -2
- package/modules/operator-auth/index.d.mts +2 -0
- package/modules/portability/README.md +7 -2
- package/modules/portability/import-staging.mjs +45 -0
- package/modules/portability/import.mjs +8 -8
- package/modules/portability/storage.mjs +5 -2
- package/package.json +1 -1
|
@@ -42,78 +42,100 @@ export async function ownerRemove({api: provider, target, statePath, clock, slee
|
|
|
42
42
|
const api = withSettledLists(withScopeHints(provider));
|
|
43
43
|
return withStateLock(statePath, async () => {
|
|
44
44
|
const state = await readOwnerState(statePath, target);
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
listed
|
|
63
|
-
for (const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
45
|
+
const remaining = new Set([...(state.owned.worker ? [`worker:${target.worker}`] : []), ...KINDS.flatMap(kind => Object.keys(state.owned[kind]).map(name => `${kind}:${name}`))]);
|
|
46
|
+
const removed = [], failed = [];
|
|
47
|
+
try {
|
|
48
|
+
if (await settlePendingSecret(api, target.worker, state, {preflight:true})) await writeOwnerState(statePath, state);
|
|
49
|
+
const owned = KINDS.flatMap(kind => Object.entries(state.owned[kind]).map(([name, entry]) => ({kind, name, entry})));
|
|
50
|
+
assert(state.owned.worker || owned.length, 'nothing-to-remove');
|
|
51
|
+
// Preflight, read-only.
|
|
52
|
+
let worker = null;
|
|
53
|
+
if (await workerSettings(api, target.worker) !== null) {
|
|
54
|
+
if (!state.owned.worker) throw refuse('resource-name-collision', 'worker', target.worker);
|
|
55
|
+
const expected = state.pendingUpload?.provider ?? state.receipt?.provider;
|
|
56
|
+
if (!expected) throw refuse('uncertain-resource-create', 'worker', target.worker);
|
|
57
|
+
const live = await activeWorker(api, target.worker);
|
|
58
|
+
if (!sameDeployment(live, expected)) throw refuse('owned-resource-replaced', 'worker', target.worker);
|
|
59
|
+
worker = target.worker;
|
|
60
|
+
} else remaining.delete(`worker:${target.worker}`);
|
|
61
|
+
assert(!state.pendingSecret, 'secret-change-unsettled');
|
|
62
|
+
const listed = {}, doomed = [];
|
|
63
|
+
for (const kind of KINDS) {
|
|
64
|
+
if (!owned.some(item => item.kind === kind)) continue;
|
|
65
|
+
listed[kind] = await resourceKinds[kind].list(api);
|
|
66
|
+
for (const {name, entry} of owned.filter(item => item.kind === kind)) {
|
|
67
|
+
const row = findResource(listed[kind], kind, name);
|
|
68
|
+
if (!row) { remaining.delete(`${kind}:${name}`); continue; }
|
|
69
|
+
if (kind === 'r2') requireBucketIdentity(name, entry);
|
|
70
|
+
if (entry.status === 'pending') await confirmEmpty(api, target.worker, kind, name, resourceId(kind, row));
|
|
71
|
+
else checkResourceIdentity(kind, name, row, entry);
|
|
72
|
+
if (kind === 'r2' && entry.status === 'created' && entry.id) await checkBucket(api, name, entry);
|
|
73
|
+
doomed.push({kind, name, entry, id:resourceId(kind, row)});
|
|
74
|
+
}
|
|
71
75
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
await api.request(`/queues/${queue.id}/consumers/${consumer.consumer_id}`, {method:'DELETE'});
|
|
76
|
+
// A Worker that consumes one of these queues is released first.
|
|
77
|
+
for (const queue of doomed.filter(item => item.kind === 'queues')) {
|
|
78
|
+
for (const consumer of await api.list(`/queues/${queue.id}/consumers`, {pagination:'single'})) {
|
|
79
|
+
assert(hexId(consumer.consumer_id), 'invalid-consumer-id');
|
|
80
|
+
await api.request(`/queues/${queue.id}/consumers/${consumer.consumer_id}`, {method:'DELETE'});
|
|
81
|
+
}
|
|
79
82
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
83
|
+
if (worker) { await deleteWorker({api, worker, ...(clock ? {clock} : {}), ...(sleep ? {sleep} : {})}); removed.push(`worker:${worker}`); }
|
|
84
|
+
for (const item of doomed) {
|
|
85
|
+
try {
|
|
86
|
+
if (item.kind === 'r2') await removeBucket(api, item);
|
|
87
|
+
else {
|
|
88
|
+
await remove[item.kind](api, item.id, item.name);
|
|
89
|
+
if (item.kind === 'queues') await waitForQueueRemoval(api,item,{clock,sleep});
|
|
90
|
+
}
|
|
91
|
+
removed.push(`${item.kind}:${item.name}`);
|
|
89
92
|
}
|
|
90
|
-
|
|
93
|
+
catch (error) { failed.push({resource:`${item.kind}:${item.name}`, code:error.message, error}); }
|
|
91
94
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (!remaining.
|
|
108
|
-
|
|
109
|
-
|
|
95
|
+
// Only complete, successful reads prove absence. Keep every unverified
|
|
96
|
+
// resource recorded even when its DELETE returned success.
|
|
97
|
+
let readFailure;
|
|
98
|
+
const readback = async read => { try { await read(); } catch (error) { readFailure ??= error; } };
|
|
99
|
+
await readback(async () => {
|
|
100
|
+
if (await workerSettings(api, target.worker) !== null) remaining.add(`worker:${target.worker}`);
|
|
101
|
+
else remaining.delete(`worker:${target.worker}`);
|
|
102
|
+
});
|
|
103
|
+
let namespacesKnown = false;
|
|
104
|
+
await readback(async () => {
|
|
105
|
+
const namespaces = await api.list('/workers/durable_objects/namespaces');
|
|
106
|
+
for (const row of namespaces) if (row.script === target.worker) remaining.add(`durable-object:${row.class}`);
|
|
107
|
+
namespacesKnown = true;
|
|
108
|
+
});
|
|
109
|
+
// Missing namespace readback cannot establish that the Worker's data is gone.
|
|
110
|
+
if (!namespacesKnown && state.owned.worker) remaining.add(`worker:${target.worker}`);
|
|
111
|
+
for (const kind of KINDS) {
|
|
112
|
+
if (!listed[kind]) continue;
|
|
113
|
+
await readback(async () => {
|
|
114
|
+
const rows = await resourceKinds[kind].list(api);
|
|
115
|
+
for (const {name} of owned.filter(item => item.kind === kind)) {
|
|
116
|
+
if (findResource(rows, kind, name)) remaining.add(`${kind}:${name}`);
|
|
117
|
+
else remaining.delete(`${kind}:${name}`);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
if (remaining.size || readFailure) {
|
|
122
|
+
// Keep proof for every survivor or resource whose absence is unverified.
|
|
123
|
+
for (const kind of KINDS) for (const name of Object.keys(state.owned[kind])) if (!remaining.has(`${kind}:${name}`)) delete state.owned[kind][name];
|
|
124
|
+
if (!remaining.has(`worker:${target.worker}`) && ![...remaining].some(name => name.startsWith('durable-object:'))) {
|
|
125
|
+
state.owned.worker = null; state.receipt = null; delete state.pendingUpload;
|
|
126
|
+
delete state.ownerClaim; delete state.retiredSecrets;
|
|
127
|
+
}
|
|
128
|
+
await writeOwnerState(statePath, state);
|
|
129
|
+
throw readFailure ?? new Error('remove-incomplete');
|
|
110
130
|
}
|
|
111
|
-
await
|
|
112
|
-
|
|
131
|
+
await rm(statePath, {force:true});
|
|
132
|
+
return {worker:target.worker, removed, remaining:[]};
|
|
133
|
+
} catch (error) {
|
|
134
|
+
error.remaining = [...remaining];
|
|
135
|
+
error.failed = failed;
|
|
136
|
+
error.removed = removed.filter(resource => !remaining.has(resource));
|
|
113
137
|
throw error;
|
|
114
138
|
}
|
|
115
|
-
await rm(statePath, {force:true});
|
|
116
|
-
return {worker:target.worker, removed, remaining:[]};
|
|
117
139
|
});
|
|
118
140
|
}
|
|
119
141
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { setTimeout as pause } from 'node:timers/promises';
|
|
2
|
+
|
|
1
3
|
// A missing HTTP status does not prove that a write never reached Cloudflare.
|
|
2
4
|
// Only these connection-establishment failures make replaying a write safe.
|
|
3
5
|
const UNSENT = new Set(['ENOTFOUND', 'EAI_AGAIN', 'ECONNREFUSED', 'UND_ERR_CONNECT_TIMEOUT']);
|
|
@@ -9,11 +11,13 @@ function unsent(error, seen = new Set()) {
|
|
|
9
11
|
return unsent(error.cause, seen);
|
|
10
12
|
}
|
|
11
13
|
|
|
12
|
-
/**
|
|
13
|
-
export async function providerResponse({send, method, timeoutMs, callerSignal, reserveRetry, failure}) {
|
|
14
|
+
/** Scoped safe replays share the original deadline and request budget. */
|
|
15
|
+
export async function providerResponse({send, method, timeoutMs, callerSignal, reserveRetry, failure, removal = false, replayableAsset = false, consume}) {
|
|
14
16
|
const timeout = AbortSignal.timeout(timeoutMs);
|
|
15
17
|
const signal = callerSignal ? AbortSignal.any([callerSignal, timeout]) : timeout;
|
|
16
18
|
const read = method === 'GET' || method === 'HEAD';
|
|
19
|
+
const removalReplay = removal && ['GET','DELETE'].includes(method);
|
|
20
|
+
const maxAttempts = removalReplay ? 3 : 2;
|
|
17
21
|
for (let attempt = 1; ; attempt++) {
|
|
18
22
|
// Leave time for a second read when the first one times out. Caller deadlines
|
|
19
23
|
// remain authoritative and are never reset by a retry.
|
|
@@ -21,12 +25,31 @@ export async function providerResponse({send, method, timeoutMs, callerSignal, r
|
|
|
21
25
|
const timer = firstRead ? setTimeout(() => firstRead.abort(new DOMException('Provider read timed out','TimeoutError')), Math.max(1, Math.ceil(timeoutMs / 2))) : undefined;
|
|
22
26
|
timer?.unref();
|
|
23
27
|
const attemptSignal = firstRead ? AbortSignal.any([signal, firstRead.signal]) : signal;
|
|
24
|
-
|
|
28
|
+
let readingBody = false;
|
|
29
|
+
try {
|
|
30
|
+
const response = await send(attemptSignal);
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
// A retried delete can observe the first attempt's committed deletion.
|
|
33
|
+
if (removalReplay && method === 'DELETE' && attempt > 1 && response.status === 404) {
|
|
34
|
+
void response.body?.cancel().catch(() => {});
|
|
35
|
+
return {response:new Response(null,{status:204}),attempts:attempt};
|
|
36
|
+
}
|
|
37
|
+
if ((removalReplay || replayableAsset) && response.status >= 500 && response.status <= 599 && attempt < maxAttempts && !signal.aborted && reserveRetry()) {
|
|
38
|
+
void response.body?.cancel().catch(() => {});
|
|
39
|
+
await pause(250 * 2 ** (attempt - 1),undefined,{signal});
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
readingBody = true;
|
|
43
|
+
const output = consume ? await consume(response,attempt) : undefined;
|
|
44
|
+
return {response,attempts:attempt,output};
|
|
45
|
+
}
|
|
25
46
|
catch (cause) {
|
|
26
|
-
|
|
47
|
+
const bodyReset = readingBody && replayableAsset && cause.message === 'provider-transport-failed';
|
|
48
|
+
if (attempt === 1 && !signal.aborted && (bodyReset || !readingBody && (read || replayableAsset || unsent(cause))) && reserveRetry()) continue;
|
|
49
|
+
if (readingBody) throw cause;
|
|
27
50
|
throw failure(cause, attempt);
|
|
28
51
|
}
|
|
29
|
-
//
|
|
52
|
+
// Body consumption and scoped asset replays keep the total deadline.
|
|
30
53
|
finally { clearTimeout(timer); }
|
|
31
54
|
}
|
|
32
55
|
}
|
|
@@ -25,8 +25,9 @@ async function boundedText(response, limit = 4 * 1024 * 1024, onReadError) {
|
|
|
25
25
|
export async function boundedJSON(response, limit) {
|
|
26
26
|
try { return JSON.parse(await boundedText(response,limit)); } catch (error) { if (error instanceof SyntaxError) throw new Error('malformed-response'); throw error; }
|
|
27
27
|
}
|
|
28
|
+
const redactValues = (text, values) => values.filter(Boolean).flatMap(value=>[value,encodeURIComponent(value),JSON.stringify(value).slice(1,-1)]).sort((a,b)=>b.length-a.length).reduce((value,secret)=>value.split(secret).join('[REDACTED]'),text);
|
|
28
29
|
/** Only this fixed origin and the explicitly selected account ever receive the token. */
|
|
29
|
-
export function cloudflare({ accountId, token, fetch: request = globalThis.fetch, objectDelete = deleteR2Object, timeoutMs = 30000, maxRequests = 500 }) {
|
|
30
|
+
export function cloudflare({ accountId, token, fetch: request = globalThis.fetch, objectDelete = deleteR2Object, timeoutMs = 30000, maxRequests = 500, removal = false }) {
|
|
30
31
|
assert(hexId(accountId), 'invalid-cloudflare-account-id');
|
|
31
32
|
assert(typeof token === 'string' && token.trim() && !/[\r\n]/.test(token), 'invalid-cloudflare-api-token');
|
|
32
33
|
let requests = 0;
|
|
@@ -38,8 +39,9 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
|
|
|
38
39
|
assert(!assetToken || path === '/workers/assets/upload?base64=true', 'invalid-asset-auth-target');
|
|
39
40
|
// Asset-session credentials are discovered at runtime, outside the CLI env.
|
|
40
41
|
// Preserve complete text for the CLI's remaining redaction before truncation.
|
|
41
|
-
const redactCredentials = text => [accountId,token,assetToken]
|
|
42
|
-
|
|
42
|
+
const redactCredentials = text => redactValues(text,[accountId,token,assetToken]);
|
|
43
|
+
// D1 import text keeps Cloudflare's reason, never its signed upload destinations.
|
|
44
|
+
const redactAuth = text => /\/r2\/buckets\/[^/]+\/objects(?:[/?]|$)/.test(path) ? '[R2 object details omitted]' : /\/d1\/database\/[^/]+\/import$/.test(path) ? redactCredentials(text).replace(/\bhttps?:\/\/\S+/gi,'[URL omitted]') : redactCredentials(text);
|
|
43
45
|
const operation = `${method} ${path.split('?')[0].replace(/(\/r2\/buckets\/[^/]+\/objects)\/.*/, '$1/[object]')}`;
|
|
44
46
|
const endpoint = redactCredentials(`https://api.cloudflare.com/client/v4${zone ? '' : '/accounts/[REDACTED]'}${operation.slice(method.length + 1)}`);
|
|
45
47
|
const transportFailure = (cause, attempts, status = null) => {
|
|
@@ -49,13 +51,14 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
|
|
|
49
51
|
error.providerTransport = {operation:redactCredentials(operation),endpoint,attempts,status,reason:transportReason(cause)};
|
|
50
52
|
return recordDeployFailure(error,{output:redactAuth(transportOutput(cause))});
|
|
51
53
|
};
|
|
52
|
-
const {response,
|
|
54
|
+
const {response,output = ''} = await providerResponse({method,timeoutMs,callerSignal,removal,
|
|
55
|
+
replayableAsset:method === 'POST' && (/^\/workers\/scripts\/[^/]+\/assets-upload-session$/.test(path) || path === '/workers/assets/upload?base64=true'),
|
|
53
56
|
reserveRetry:() => requests < maxRequests && (++requests, true),failure:transportFailure,
|
|
57
|
+
consume:(response,attempts) => response.body ? boundedText(response,undefined,cause => transportFailure(cause,attempts,response.status)) : '',
|
|
54
58
|
send:signal => isR2ObjectDelete(path, method) ? objectDelete(`/client/v4/accounts/${accountId}${path}`, {headers:{Authorization:`Bearer ${token}`},signal}) : request(zone ? `https://api.cloudflare.com/client/v4${path}` : `https://api.cloudflare.com/client/v4/accounts/${accountId}${path}`, {
|
|
55
59
|
method, redirect: 'error', signal, headers: { Authorization: `Bearer ${assetToken ?? token}`, ...(body && !(body instanceof FormData) ? { 'Content-Type':'application/json' } : {}) },
|
|
56
60
|
...(body === undefined ? {} : { body: body instanceof FormData ? body : JSON.stringify(body) }),
|
|
57
61
|
})});
|
|
58
|
-
const output = response.body ? await boundedText(response,undefined,cause => transportFailure(cause,attempts,response.status)) : '';
|
|
59
62
|
// A successful DELETE may have no response body (including HTTP 200).
|
|
60
63
|
// Nonempty replies and all other methods still require the API envelope.
|
|
61
64
|
if (method === 'DELETE' && response.ok && output === '') return { success: true, result: null };
|
|
@@ -127,6 +130,6 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
|
|
|
127
130
|
assert(response.status === 200, 'd1-import-upload-failed');
|
|
128
131
|
assert(response.headers.get('etag')?.replace(/^"|"$/g, '') === etag, 'd1-import-upload-etag-mismatch');
|
|
129
132
|
}
|
|
130
|
-
return { accountId, envelope, list, importSql(id, sql) { return importD1({envelope,upload}, id, sql); }, request:requestResult,
|
|
133
|
+
return { accountId, envelope, list, importSql(id, sql) { return importD1({envelope,upload,redact:text => redactValues(text,[accountId,token])}, id, sql); }, request:requestResult,
|
|
131
134
|
zoneRequest(path, options = {}) { return requestResult(path, { ...options, zone: true }); } };
|
|
132
135
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { nameAssetFailure } from './diagnostics.mjs';
|
|
1
2
|
import { extname } from 'node:path';
|
|
2
3
|
import { assert, hash, hexId, uuid } from './validation.mjs';
|
|
3
4
|
import { moduleType, assetType } from './artifact.mjs';
|
|
@@ -61,7 +62,8 @@ export async function uploadAssets(api, worker, files) {
|
|
|
61
62
|
manifest[`/${file.path}`] = {hash:digest,size:Buffer.from(file.base64,'base64').length};
|
|
62
63
|
byHash.set(digest,file);
|
|
63
64
|
}
|
|
64
|
-
const session = await api.request(`/workers/scripts/${worker}/assets-upload-session`,{method:'POST',body:{manifest}})
|
|
65
|
+
const session = await api.request(`/workers/scripts/${worker}/assets-upload-session`,{method:'POST',body:{manifest}})
|
|
66
|
+
.catch(error => { throw nameAssetFailure(error,files.map(file => file.path)); });
|
|
65
67
|
assert(typeof session.jwt === 'string' && session.jwt && Array.isArray(session.buckets),'invalid-asset-session');
|
|
66
68
|
let completion = session.buckets.length ? null : session.jwt;
|
|
67
69
|
const seen = new Set();
|
|
@@ -72,7 +74,8 @@ export async function uploadAssets(api, worker, files) {
|
|
|
72
74
|
assert(byHash.has(key) && !seen.has(key),'invalid-asset-request'); seen.add(key);
|
|
73
75
|
const file = byHash.get(key); data.set(key,new Blob([file.base64],{type:assetType(file.path)}),file.path.split('/').at(-1));
|
|
74
76
|
}
|
|
75
|
-
const uploaded = await api.request('/workers/assets/upload?base64=true',{method:'POST',body:data,assetToken:session.jwt})
|
|
77
|
+
const uploaded = await api.request('/workers/assets/upload?base64=true',{method:'POST',body:data,assetToken:session.jwt})
|
|
78
|
+
.catch(error => { throw nameAssetFailure(error,bucket.map(key => byHash.get(key).path)); });
|
|
76
79
|
if (uploaded.jwt) completion = uploaded.jwt;
|
|
77
80
|
}
|
|
78
81
|
assert(typeof completion === 'string' && completion,'incomplete-asset-upload');
|
|
@@ -151,7 +151,8 @@ Do not log confirmation URLs, input tokens, cookies or provider message bodies.
|
|
|
151
151
|
GET/HEAD confirmation uses a self-contained, responsive document. A magic link
|
|
152
152
|
says “Confirm sign-in to Example”; an invitation says “Accept your invitation to
|
|
153
153
|
Example”. Both explain that the link works once and show its remaining lifetime,
|
|
154
|
-
rounded up to
|
|
154
|
+
rounded up to days for lifetimes of a day or more, otherwise minutes (singular
|
|
155
|
+
for one day or minute). The sole primary form button is
|
|
155
156
|
“Confirm”. Only its hidden POST field contains the token. Invalid, expired,
|
|
156
157
|
consumed or unauthorized links return a styled 400 document with “Request a new
|
|
157
158
|
link” pointing to the configured sign-in page. HEAD has the same status and
|
|
@@ -219,7 +220,8 @@ Cookies are host-only, Path=/, HttpOnly, SameSite=Lax; production uses Secure an
|
|
|
219
220
|
127.0.0.1 or [::1] and uses `operator_session_local_<port>` (for example
|
|
220
221
|
`operator_session_local_8787`): browsers keep cookies per host, not per port, so
|
|
221
222
|
checkouts side by side on one loopback host each keep their own session. Session expiry defaults to
|
|
222
|
-
7 days (maximum 30), links to 15 minutes (maximum 60)
|
|
223
|
+
7 days (maximum 30), sign-in and step-up links to 15 minutes (maximum 60), and
|
|
224
|
+
invitations to 7 days; no silent sliding renewal.
|
|
223
225
|
`returnTo` must be a local absolute path; unsafe values become `/app`. It is stored
|
|
224
226
|
with the token, and cannot be replaced on confirmation. Auth methods never return
|
|
225
227
|
raw link tokens. `CompletedSession.cookie` is server-only and must only be emitted
|
|
@@ -309,6 +311,12 @@ The exported `isActiveOperator` is the shared admission predicate.
|
|
|
309
311
|
|
|
310
312
|
## Mail, OIDC, password and review seams
|
|
311
313
|
|
|
314
|
+
Invitation links default to seven days (604800 seconds). `invitationSeconds`
|
|
315
|
+
can shorten that lifetime, up to the seven-day maximum. `linkSeconds` remains
|
|
316
|
+
independent: sign-in and step-up links default to 15 minutes and allow at most
|
|
317
|
+
one hour. Invitation mail and confirmation pages describe the longer lifetime
|
|
318
|
+
in days; single use, cancellation and issuer-authority checks still apply.
|
|
319
|
+
|
|
312
320
|
Magic and invitation mail include both plain text and HTML. Each part has exactly
|
|
313
321
|
one confirmation URL; the HTML part presents it as a button with inline mail-safe
|
|
314
322
|
styling. The body sets Arial; its centered main content has a 480px maximum width
|
|
@@ -28,6 +28,7 @@ export function configure(input) {
|
|
|
28
28
|
allowList: input.allowList === undefined ? undefined : input.allowList.map(normalizeEmail),
|
|
29
29
|
sessionSeconds: seconds(input.sessionSeconds, 7 * 86400, 30 * 86400),
|
|
30
30
|
linkSeconds: seconds(input.linkSeconds, 900, 3600),
|
|
31
|
+
invitationSeconds: seconds(input.invitationSeconds, 7 * 86400, 7 * 86400),
|
|
31
32
|
reviewSeconds: seconds(input.reviewSeconds, 3600, 3600),
|
|
32
33
|
limitWindowSeconds: seconds(input.limitWindowSeconds, 900, 86400),
|
|
33
34
|
ipLimit: seconds(input.ipLimit, 30, 10000), emailLimit: seconds(input.emailLimit, 5, 1000),
|
|
@@ -35,7 +35,7 @@ export async function checkConfirmation(response, { kind, productName, minutes =
|
|
|
35
35
|
expect(html.includes(escape(productName)), 'escaped product name');
|
|
36
36
|
if (valid) {
|
|
37
37
|
expect(html.includes(`<h1>${kind === 'invite' ? 'Accept your invitation to' : 'Confirm sign-in to'} ${escape(productName)}</h1>`), 'plain confirmation heading');
|
|
38
|
-
expect(html.includes(`This link works once and expires in ${minutes} minutes.`), 'preview lifetime');
|
|
38
|
+
expect(html.includes(`This link works once and expires in ${kind === 'invite' ? '7 days' : `${minutes} minutes`}.`), 'preview lifetime');
|
|
39
39
|
expect(html.includes('<button type="submit">Confirm</button>'), 'explicit confirmation control');
|
|
40
40
|
expect(html.includes(`<input type="hidden" name="token" value="${token}">`) && html.split(token).length === 2, 'token appears only in hidden field');
|
|
41
41
|
} else {
|
|
@@ -47,7 +47,7 @@ export function checkMail(message, { kind, productName, token }) {
|
|
|
47
47
|
expect(message.subject === (kind === 'invite' ? `You're invited to ${productName}` : `Your sign-in link for ${productName}`), 'plain mail subject');
|
|
48
48
|
expect(!message.subject.includes(token), 'subject contains no credential');
|
|
49
49
|
for (const part of [message.text, message.html]) {
|
|
50
|
-
expect(typeof part === 'string' && part.includes(
|
|
50
|
+
expect(typeof part === 'string' && part.includes(`This link works once and expires in ${kind === 'invite' ? '7 days' : '15 minutes'}.`), 'multipart expiry');
|
|
51
51
|
expect(part.split(token).length === 2 && (part.match(/https?:\/\//g) ?? []).length === 1, 'one confirmation URL per part');
|
|
52
52
|
}
|
|
53
53
|
expect((message.html.match(/<a /g) ?? []).length === 1 && message.html.includes('>Confirm</a>'), 'one confirmation mail button');
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { escapeHtml } from '../../http.mjs';
|
|
2
2
|
import { pageResponse } from '../../page.mjs';
|
|
3
|
+
import { linkExpiry } from './expiry.mjs';
|
|
3
4
|
|
|
4
5
|
const HEADINGS = { invite: 'Accept your invitation to', magic: 'Confirm sign-in to', step_up: "Confirm it's you in", owner: 'Sign in as the owner of' };
|
|
5
6
|
|
|
@@ -7,7 +8,7 @@ export async function confirmationResponse({ branding, preview, token, method, a
|
|
|
7
8
|
const product = escapeHtml(branding.productName);
|
|
8
9
|
const heading = preview ? `${HEADINGS[preview.kind]} ${product}` : 'This link is invalid or expired';
|
|
9
10
|
const content = preview
|
|
10
|
-
? `<p>This link works once and expires in ${preview.expiresInMinutes}
|
|
11
|
+
? `<p>This link works once and expires in ${linkExpiry(preview.expiresInMinutes)}.</p><form method="post" action="${escapeHtml(action)}"><input type="hidden" name="token" value="${escapeHtml(token)}"><button type="submit">Confirm</button></form>`
|
|
11
12
|
: `<p>This link may have expired or already been used. Request a new link to continue.</p><a class="action" href="${escapeHtml(branding.signInPath)}">Request a new link</a>`;
|
|
12
13
|
// Native form POSTs need their Origin; never disclose the token path/query.
|
|
13
14
|
return pageResponse({ branding, heading, content, status: preview ? 200 : 400, method, referrer: preview ? 'strict-origin' : undefined });
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Mail and confirmation pages describe the same configured/remaining lifetime.
|
|
2
|
+
export function linkExpiry(minutes) {
|
|
3
|
+
if (minutes >= 1440) {
|
|
4
|
+
const days = Math.ceil(minutes / 1440);
|
|
5
|
+
return `${days} ${days === 1 ? 'day' : 'days'}`;
|
|
6
|
+
}
|
|
7
|
+
return `${minutes} ${minutes === 1 ? 'minute' : 'minutes'}`;
|
|
8
|
+
}
|
|
@@ -36,7 +36,8 @@ export function createEmailLinkLifecycle({ config: input, clock = Date.now, mail
|
|
|
36
36
|
}
|
|
37
37
|
async function newToken(kind, returnTo) {
|
|
38
38
|
const token = opaqueToken();
|
|
39
|
-
|
|
39
|
+
const lifetime = kind === 'invite' ? config.invitationSeconds : config.linkSeconds;
|
|
40
|
+
return { id: crypto.randomUUID(), token, hash: await hash('link', token), kind, returnTo, expiresAt: clock() + lifetime * 1000 };
|
|
40
41
|
}
|
|
41
42
|
// The email link: the app's own proof that a person holds an address.
|
|
42
43
|
async function sendLink(email, token, revoke = () => store.revokeLink(token.id, clock())) {
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { accentInk } from '../../branding.mjs';
|
|
2
2
|
import { escapeHtml } from '../../http.mjs';
|
|
3
|
+
import { linkExpiry } from './expiry.mjs';
|
|
3
4
|
|
|
4
5
|
export function linkMessage(config, token, url) {
|
|
5
6
|
const { productName, accentColor } = config.branding;
|
|
6
7
|
const invitation = token.kind === 'invite', stepUp = token.kind === 'step_up';
|
|
7
8
|
const action = invitation ? 'accept your invitation to' : stepUp ? 'confirm a sensitive change in' : 'sign in to';
|
|
8
|
-
const minutes = config.linkSeconds / 60;
|
|
9
|
-
const expiry = `This link works once and expires in ${minutes}
|
|
9
|
+
const minutes = (invitation ? config.invitationSeconds : config.linkSeconds) / 60;
|
|
10
|
+
const expiry = `This link works once and expires in ${linkExpiry(minutes)}.`;
|
|
10
11
|
const heading = invitation ? `Accept your invitation to ${productName}` : stepUp ? `Confirm it's you in ${productName}` : `Confirm sign-in to ${productName}`;
|
|
11
12
|
return {
|
|
12
13
|
subject: invitation ? `You're invited to ${productName}` : stepUp ? `Confirm it's you in ${productName}` : `Your sign-in link for ${productName}`,
|
|
@@ -46,6 +46,8 @@ export interface AuthConfig {
|
|
|
46
46
|
origin: string; OWNER_EMAIL: string; SESSION_SECRET: string; SETUP_TOKEN?: string; MAIL_FROM?: string;
|
|
47
47
|
/** Local HTTP is allowed only for literal localhost, 127.0.0.1 or [::1] with matching Host. */
|
|
48
48
|
local?: boolean; allowList?: string[]; sessionSeconds?: number; linkSeconds?: number;
|
|
49
|
+
/** Invitation lifetime in seconds, up to seven days (the default); independent of sign-in/step-up links. */
|
|
50
|
+
invitationSeconds?: number;
|
|
49
51
|
review?: { enabled: boolean; operatorEmail: string }; reviewSeconds?: number;
|
|
50
52
|
limitWindowSeconds?: number; ipLimit?: number; emailLimit?: number;
|
|
51
53
|
/** Issuers the original admitVerifiedIdentity/createIdentityAdmission seams accept. */
|
|
@@ -301,8 +301,12 @@ transactions remain atomic.
|
|
|
301
301
|
download streams completed jobs. Abort removes snapshots and stored parts.
|
|
302
302
|
|
|
303
303
|
`imports.create(manifest,actor?)` creates staging under one durable import lock.
|
|
304
|
-
Upload parts strictly in order.
|
|
305
|
-
|
|
304
|
+
Upload parts strictly in order. Each upload stages rows and archived JSON in
|
|
305
|
+
batches bounded by D1 transport bytes, checkpointing each batch atomically.
|
|
306
|
+
A same-index/same-hash retry resumes an interrupted part or acknowledges a finished
|
|
307
|
+
part; different content returns 409. Reassembled bytes pass the part checksum
|
|
308
|
+
before completion advances. Staged data stays private until finalization.
|
|
309
|
+
Finalize requires the closing `archiveHash` and verifies all
|
|
306
310
|
parts, semantic contents, destination
|
|
307
311
|
emptiness, erasure and product invariants. R2 ownership is recorded after the
|
|
308
312
|
empty-namespace check. Verified object writes precede one atomic D1 batch, with
|
|
@@ -439,6 +443,7 @@ modules/portability/migration-statements.mjs
|
|
|
439
443
|
modules/portability/legacy-safety.mjs
|
|
440
444
|
modules/portability/export.mjs
|
|
441
445
|
modules/portability/import.mjs
|
|
446
|
+
modules/portability/import-staging.mjs
|
|
442
447
|
modules/portability/payloads.mjs
|
|
443
448
|
modules/portability/journal.mjs
|
|
444
449
|
modules/portability/http.mjs
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { RPC_LIMIT, rpcSize, rpcSizeBytes } from '../d1-transport/index.mjs';
|
|
2
|
+
import { canonicalJson } from './hash.mjs';
|
|
3
|
+
import { statement, save, readPart, insist } from './storage.mjs';
|
|
4
|
+
import { checkPart } from './format.mjs';
|
|
5
|
+
|
|
6
|
+
const combine=(a,b)=>({utf8:a.utf8+b.utf8-2,units:a.units+b.units-2,hasWide:a.hasWide||b.hasWide});
|
|
7
|
+
const guardSql=`INSERT INTO rer_portability_guard(value) SELECT CASE WHEN (
|
|
8
|
+
EXISTS(SELECT 1 FROM rer_portability_jobs WHERE id=? AND phase='staging' AND completed=? AND metadata=?)
|
|
9
|
+
AND (SELECT import_job FROM rer_portability_state WHERE id=1)=?
|
|
10
|
+
) THEN 1 ELSE 0 END`;
|
|
11
|
+
|
|
12
|
+
// Rows and the archived JSON share the same durable cursor. Each checkpoint
|
|
13
|
+
// commits with its writes; retries cannot duplicate rows, even after a lost ACK.
|
|
14
|
+
// Product tables remain untouched until finalizeImport's atomic publication.
|
|
15
|
+
export async function stagePart(context,row,part,queries,stagedCounts) {
|
|
16
|
+
const {db,config}=context;
|
|
17
|
+
const pending=row.metadata.pendingPart;
|
|
18
|
+
insist(!pending || (pending.index===part.index && pending.sha256===part.sha256),'part_conflict',409);
|
|
19
|
+
let next=pending?.next??0;
|
|
20
|
+
while(next<queries.length) {
|
|
21
|
+
const before=canonicalJson(row.metadata),chunk=[];
|
|
22
|
+
// Reserve the largest cursor for this part before admitting any data.
|
|
23
|
+
row.metadata.pendingPart={index:part.index,sha256:part.sha256,next:queries.length};
|
|
24
|
+
const guard={sql:guardSql,params:[row.id,row.completed,before,row.id]};
|
|
25
|
+
const checkpoint={sql:'UPDATE rer_portability_jobs SET phase=?,completed=?,metadata=? WHERE id=?',params:[row.phase,row.completed,canonicalJson(row.metadata),row.id]};
|
|
26
|
+
let size=rpcSize([guard,checkpoint,{sql:'DELETE FROM rer_portability_guard'}]),end=next;
|
|
27
|
+
for(;end<queries.length;end++) {
|
|
28
|
+
const candidate=combine(size,rpcSize([queries[end]]));
|
|
29
|
+
if(rpcSizeBytes(candidate)>RPC_LIMIT)break;
|
|
30
|
+
size=candidate;chunk.push(queries[end]);
|
|
31
|
+
}
|
|
32
|
+
insist(end>next,'D1_RPC_TRANSPORT_LIMIT',413);
|
|
33
|
+
row.metadata.pendingPart.next=end;
|
|
34
|
+
await db.batch([statement(db,guard.sql,...guard.params),...chunk.map(q=>statement(db,q.sql,...q.params)),save(db,row),db.prepare('DELETE FROM rer_portability_guard')]);
|
|
35
|
+
next=end;
|
|
36
|
+
}
|
|
37
|
+
// Authenticate the reassembled stored bytes before acknowledging the part.
|
|
38
|
+
const stored=await readPart(db,row.id,part.index);
|
|
39
|
+
await checkPart(stored,part.index,config.partBytes);insist(stored.sha256===part.sha256,'part_checksum');
|
|
40
|
+
const before=canonicalJson(row.metadata),completed=row.completed;
|
|
41
|
+
delete row.metadata.pendingPart;row.metadata.stagedCounts=stagedCounts;row.completed++;
|
|
42
|
+
await db.batch([statement(db,guardSql,row.id,completed,before,row.id),
|
|
43
|
+
statement(db,'INSERT INTO rer_portability_parts VALUES(?,?,?)',row.id,part.index,part.sha256),
|
|
44
|
+
save(db,row),db.prepare('DELETE FROM rer_portability_guard')]);
|
|
45
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { all, statement, state, guard, job, save,
|
|
1
|
+
import { all, statement, state, guard, job, save, partQueries, parts, readPart, cleanCopies, quote, stream, insist, run } from './storage.mjs';
|
|
2
|
+
import { stagePart } from './import-staging.mjs';
|
|
2
3
|
import { refuseProviderData } from './provider-data.mjs';
|
|
3
4
|
import { refuseDevData } from './dev-data.mjs';
|
|
4
5
|
import { catalogue, validateRow, bindCell, writableColumns } from './catalogue.mjs';
|
|
@@ -69,20 +70,19 @@ export async function uploadPart(context,id,index,part) {
|
|
|
69
70
|
insist(entry && item.offset+unbase64(item.data).length<=entry.size,'binary_inventory');
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
|
-
|
|
73
|
+
const stagedCounts=Object.assign(Object.create(null),row.metadata.stagedCounts);
|
|
73
74
|
for(const [table,group] of groups) {
|
|
74
75
|
const width=group.rows[0].length,count=Math.min(30,Math.floor(100/width));
|
|
75
76
|
insist(count>0,'unsupported_restore_width:'+table);
|
|
76
|
-
const copy=row.metadata.copies[table],first=(
|
|
77
|
+
const copy=row.metadata.copies[table],first=(stagedCounts[table]??0)+1;
|
|
77
78
|
for(let start=0;start<group.rows.length;start+=count) {
|
|
78
79
|
const chunk=group.rows.slice(start,start+count);
|
|
79
|
-
inserts.push(
|
|
80
|
-
inserts.push(
|
|
80
|
+
inserts.push({sql:`INSERT INTO ${quote(copy)} (${group.columns}) VALUES${chunk.map(()=> '('+Array(width).fill('?').join(',')+')').join(',')}`,params:chunk.flat()});
|
|
81
|
+
inserts.push({sql:`INSERT INTO ${quote(sidecar(copy))} VALUES${chunk.map((_,i)=>`(?,${first+start+i},?,?)`).join(',')}`,params:group.metadata.slice(start,start+count).flat()});
|
|
81
82
|
}
|
|
82
|
-
|
|
83
|
+
stagedCounts[table]=first-1+group.rows.length;
|
|
83
84
|
}
|
|
84
|
-
|
|
85
|
-
try{await db.batch([guard(db,'(SELECT completed FROM rer_portability_jobs WHERE id=?)=?',id,before),...inserts,...partStatements(db,id,part),save(db,row),db.prepare('DELETE FROM rer_portability_guard')]);}
|
|
85
|
+
try{await stagePart(context,row,part,[...inserts,...partQueries(id,part).slice(1)],stagedCounts);}
|
|
86
86
|
catch(e){const duplicate=await statement(db,'SELECT sha256 FROM rer_portability_parts WHERE job=? AND idx=?',id,index).first();if(!duplicate || duplicate.sha256!==part.sha256)throw e;}
|
|
87
87
|
return importStatus(context,id);
|
|
88
88
|
}
|
|
@@ -26,12 +26,15 @@ export function save(db,row) {
|
|
|
26
26
|
return statement(db,'UPDATE rer_portability_jobs SET phase=?,completed=?,metadata=? WHERE id=?',row.phase,row.completed,canonicalJson(row.metadata),row.id);
|
|
27
27
|
}
|
|
28
28
|
export function partStatements(db,id,part) {
|
|
29
|
-
|
|
29
|
+
return partQueries(id,part).map(({sql,params})=>statement(db,sql,...params));
|
|
30
|
+
}
|
|
31
|
+
export function partQueries(id,part) {
|
|
32
|
+
const json=canonicalJson(part), result=[{sql:'INSERT INTO rer_portability_parts VALUES(?,?,?)',params:[id,part.index,part.sha256]}];
|
|
30
33
|
for(let i=0,segment=0;i<json.length;segment++) {
|
|
31
34
|
let end=Math.min(i+32000,json.length);
|
|
32
35
|
const last=json.charCodeAt(end-1),next=json.charCodeAt(end);
|
|
33
36
|
if(last>=0xd800 && last<=0xdbff && next>=0xdc00 && next<=0xdfff) end--;
|
|
34
|
-
result.push(
|
|
37
|
+
result.push({sql:'INSERT INTO rer_portability_segments VALUES(?,?,?,?)',params:[id,part.index,segment,json.slice(i,end)]});i=end;
|
|
35
38
|
}
|
|
36
39
|
return result;
|
|
37
40
|
}
|