@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.
- package/cli/demo-host.mjs +1 -1
- package/cli/deploy-command.mjs +7 -5
- package/cli/deploy-observation.mjs +20 -2
- package/cli/deploy-owner.mjs +12 -7
- package/cli/deploy.mjs +5 -4
- package/cli/site.mjs +2 -2
- package/global/CHANGELOG.md +16 -0
- package/modules/container/adapters/d1.mjs +1 -1
- package/modules/container/transfer.mjs +0 -1
- package/modules/container/worker-engine.mjs +1 -1
- package/modules/demo-host/README.md +29 -7
- package/modules/demo-host/container.mjs +19 -7
- package/modules/demo-host/placement.mjs +19 -5
- package/modules/demo-host/plan.mjs +11 -6
- package/modules/demo-host/start-script.mjs +2 -1
- package/modules/demo-host/startup.mjs +3 -3
- package/modules/demo-host/worker.mjs +27 -8
- package/modules/deploy-kit/README.md +35 -2
- package/modules/deploy-kit/diagnostics.mjs +16 -1
- package/modules/deploy-kit/owner-cli.mjs +3 -3
- package/modules/deploy-kit/owner-remove.mjs +23 -5
- package/modules/deploy-kit/pagination.mjs +99 -0
- package/modules/deploy-kit/provider.mjs +30 -70
- package/modules/deploy-kit/r2-objects.mjs +6 -17
- package/modules/deploy-kit/site-readback.mjs +67 -0
- package/modules/deploy-kit/site.mjs +17 -31
- package/modules/deploy-kit/uploaded-worker.mjs +9 -3
- package/modules/erasure/queue.mjs +0 -2
- package/modules/job-queue/cli.mjs +1 -1
- package/modules/operator-auth/identity/oidc/jwt.mjs +1 -1
- package/modules/portability/bridge.mjs +3 -3
- package/modules/portability/catalogue.mjs +1 -1
- package/modules/portability/cli-options.mjs +1 -1
- package/modules/portability/legacy-files.mjs +1 -1
- package/modules/portability/legacy-safety.mjs +3 -3
- package/modules/portability/payloads.mjs +1 -1
- package/modules/release/source.mjs +1 -1
- package/package.json +1 -1
|
@@ -186,7 +186,7 @@ No zone ruleset is read or written. Every request now invokes the Worker: one pa
|
|
|
186
186
|
- the Worker is the kit's when its latest deployment serves, at 100%, the Worker's newest version, and that version's message annotation is the kit's `rer-site:<fingerprint>` (every site upload sets it);
|
|
187
187
|
- a Custom Domain is the kit's when its service is the kit's Worker.
|
|
188
188
|
|
|
189
|
-
A read-only preflight reads the zone (it must be in this account, else `site-zone-missing`), the Worker and the zone's Custom Domains, and refuses before any write: `resource-name-collision` for a Worker of the name whose live version the kit did not upload, or for a hostname attached to another service (or to a Worker that is not the kit's); `owned-resource-replaced` for a kit Worker whose latest deployment is not the kit's upload (a dashboard rollback or a split deployment). It then uploads the assets and the site Worker, checks that the new version is live, turns workers.dev off, attaches each missing hostname, and reads the Worker and both domains back. A repeat attaches nothing new. `.rer/deploy/site-<environment>.json` receives a receipt for the person; it is never read.
|
|
189
|
+
A read-only preflight reads the zone (it must be in this account, else `site-zone-missing`), the Worker and the zone's Custom Domains, and refuses before any write: `resource-name-collision` for a Worker of the name whose live version the kit did not upload, or for a hostname attached to another service (or to a Worker that is not the kit's); `owned-resource-replaced` for a kit Worker whose latest deployment is not the kit's upload (a dashboard rollback or a split deployment). It then uploads the assets and the site Worker, checks that the new version is live, turns workers.dev off, attaches each missing hostname, and reads the Worker and both domains back. Both post-upload Worker checks share a 90-second upload window: missing visibility or the exact preflight version waits with backoff until the upload response's version serves alone as newest with its expected fingerprint. A foreign version, changed fingerprint or split deployment refuses immediately. A timeout names the last serving/newest versions and fingerprint. A repeat attaches nothing new. `.rer/deploy/site-<environment>.json` receives a receipt for the person; it is never read. Provider propagation cases are tested against the fake Cloudflare API in `tests/site-provider.mjs`, where every zone ruleset path answers 403.
|
|
190
190
|
|
|
191
191
|
The token needs, beyond the owner-account permissions above:
|
|
192
192
|
|
|
@@ -290,7 +290,11 @@ budget, and no request starts at the deadline. An unchanged repeat uses the
|
|
|
290
290
|
existing library window and does not record first-upload readiness attempts.
|
|
291
291
|
The standalone CLI `health` command sends a single request; only an answer from
|
|
292
292
|
another Worker version (an edge still converging after a secret change) is asked
|
|
293
|
-
again, within 30 seconds.
|
|
293
|
+
again, within 30 seconds. Immediate post-upload readback also waits through an
|
|
294
|
+
HTTPS workers.dev 404 whose complete body is `error code: 1042`, including after
|
|
295
|
+
a changed-source redeploy, within the original upload deadline and the health
|
|
296
|
+
command's 30-second bound. This does not grant first-creation setup-token replay
|
|
297
|
+
permission to a redeploy. Library `verifyHealth`
|
|
294
298
|
retains the existing window, retrying only 404, 503 and network failures for
|
|
295
299
|
30 seconds by default.
|
|
296
300
|
|
|
@@ -407,3 +411,32 @@ narrowing where Cloudflare omits service-binding `props` from settings/version
|
|
|
407
411
|
readback. Ordinary `allowedHosts` still maps to `props` and retains strict
|
|
408
412
|
readback; missing props are never silently accepted. See the
|
|
409
413
|
[egress service guide](../egress-service/README.md).
|
|
414
|
+
|
|
415
|
+
### List completion and deployment failures
|
|
416
|
+
|
|
417
|
+
Every Cloudflare list read uses `pagination.mjs`, including reads through
|
|
418
|
+
`request` (Worker versions, deployments, secrets and schedules, consumers,
|
|
419
|
+
domains, zones, routes and Container applications), account inventories and streaming R2 object pages.
|
|
420
|
+
The endpoint's strategy chooses request parameters; the shared reader decides
|
|
421
|
+
completion. Missing/null `result_info` on a short requested-limit page is
|
|
422
|
+
terminal. Missing/empty cursors and reached `total_pages` are terminal unless
|
|
423
|
+
other metadata explicitly advertises unread rows. Continuation cursors and page
|
|
424
|
+
counts are followed. Contradictory totals, repeated cursors, malformed metadata,
|
|
425
|
+
request limits and a truncated reply with no continuation still refuse.
|
|
426
|
+
|
|
427
|
+
`provider-incomplete-pagination` identifies a list with insufficient continuation
|
|
428
|
+
information, rather than a Cloudflare error code. CLI failures name its GET path;
|
|
429
|
+
Cloudflare error codes/messages are printed when the provider supplied them, and
|
|
430
|
+
otherwise explicitly identified as unavailable. Resolve the named call's cause
|
|
431
|
+
and rerun the printed command with the same account and environment; preserve
|
|
432
|
+
the ownership record. Removal detaches all consumers of an owned queue before
|
|
433
|
+
queue deletion and polls its listing for absence without repeating the DELETE.
|
|
434
|
+
|
|
435
|
+
`owner-claim-failed` is the hosted bootstrap failure, after an app may already
|
|
436
|
+
have been deployed. The CLI retains the observed POST path, HTTP status and
|
|
437
|
+
application error code even when a function hook throws a generic failure.
|
|
438
|
+
Correct owner authorization or token inputs and rerun deploy; unknown supplied
|
|
439
|
+
tokens should be left unset so rer issues a fresh one. The existing owner is
|
|
440
|
+
kept, and a successful claim retires the one-time token. A hook that fails
|
|
441
|
+
without an HTTP exchange can only supply its own cause. Seeds copy this guide
|
|
442
|
+
and `RUN-LOCALLY.md` for these recovery instructions.
|
|
@@ -14,6 +14,7 @@ export function deployDiagnostic(error, redact) {
|
|
|
14
14
|
return {code,step:detail.step ?? 'deploy',created:(detail.created ?? []).map(clean),
|
|
15
15
|
...(typeof error.resource==='string' && /^(?:d1|r2|kv|queues|worker|service):[a-z0-9-]+$/.test(error.resource) ? {resource:clean(error.resource)} : {}),
|
|
16
16
|
...(detail.providerCodes?.length ? {providerCodes:detail.providerCodes} : {}),
|
|
17
|
+
...(redact && providerFailure(error,redact) ? {provider:providerFailure(error,redact)} : {}),
|
|
17
18
|
output:(detail.output ? (redact ? clean(detail.output) : '(provider output withheld; rer deploy provides redacted details)') : '(no provider output)').split(/\r?\n/).slice(0,8).map(line=>line.slice(0,500)).join('\n')};
|
|
18
19
|
}
|
|
19
20
|
/** One line naming the provider call that was refused and what Cloudflare said, from
|
|
@@ -50,10 +51,11 @@ const CREATED_KINDS = {D1:'d1',R2:'r2',KV:'kv',queue:'queues'};
|
|
|
50
51
|
export function deploymentDiagnostics(api, {onCreated} = {}) {
|
|
51
52
|
const created = [];
|
|
52
53
|
const recordCreated = resource => { created.push(resource); onCreated?.(resource); };
|
|
53
|
-
let currentStep = 'prepare deploy';
|
|
54
|
+
let currentStep = 'prepare deploy', currentCall;
|
|
54
55
|
const guard = method => async (path, options) => {
|
|
55
56
|
const step = operation(path,options);
|
|
56
57
|
currentStep = step;
|
|
58
|
+
currentCall = `${method === 'list' ? 'GET' : options?.method ?? 'GET'} ${path.split('?')[0].replace(/(\/r2\/buckets\/[^/]+\/objects)\/.*/, '$1/[object]')}`;
|
|
57
59
|
try {
|
|
58
60
|
const result = await api[method](path,options);
|
|
59
61
|
if (step.startsWith('create ')) recordCreated(`${step.slice(7)}:${options.body.name ?? options.body.queue_name ?? options.body.title}`);
|
|
@@ -61,6 +63,7 @@ export function deploymentDiagnostics(api, {onCreated} = {}) {
|
|
|
61
63
|
} catch (error) {
|
|
62
64
|
// A failed create names the resource it asked for: a lost answer may still have created it.
|
|
63
65
|
if (step.startsWith('create ') && error.resource === undefined) error.resource = `${CREATED_KINDS[step.slice(7)]}:${options.body.name ?? options.body.queue_name ?? options.body.title}`;
|
|
66
|
+
error.providerCall ??= {operation:currentCall,reason:error.message};
|
|
64
67
|
throw recordDeployFailure(error,{step,created:[...created]});
|
|
65
68
|
}
|
|
66
69
|
};
|
|
@@ -72,6 +75,7 @@ export function deploymentDiagnostics(api, {onCreated} = {}) {
|
|
|
72
75
|
},
|
|
73
76
|
annotateFailure(error, step = currentStep) {
|
|
74
77
|
const prior = failures.get(error);
|
|
78
|
+
if (currentCall) error.providerCall ??= {operation:currentCall,reason:error.message};
|
|
75
79
|
// Node filesystem/CLI codes are structural; their messages can hold paths
|
|
76
80
|
// and inputs, so retain them privately for the caller's redaction boundary.
|
|
77
81
|
const cliCode = typeof error.code === 'string' && /^(?:E[A-Z0-9_]{1,63}|health-[a-z-]+)$/.test(error.code) ? error.code : undefined;
|
|
@@ -80,3 +84,14 @@ export function deploymentDiagnostics(api, {onCreated} = {}) {
|
|
|
80
84
|
return error;
|
|
81
85
|
}};
|
|
82
86
|
}
|
|
87
|
+
|
|
88
|
+
/** All provider failure paths share the same safe, portable rendering. */
|
|
89
|
+
export function providerFailure(error, redact = String) {
|
|
90
|
+
const specific = providerRefusal(error,redact) ?? providerTransport(error,redact);
|
|
91
|
+
if (!specific && error?.cause instanceof Error) return providerFailure(error.cause,redact);
|
|
92
|
+
if (specific) return specific;
|
|
93
|
+
const call = error?.providerCall;
|
|
94
|
+
if (typeof call?.operation !== 'string') return undefined;
|
|
95
|
+
const clean = value => redact(String(value)).replace(/[\x00-\x1f\x7f-\x9f]+/g,' ').replace(/\b[a-f0-9]{32}\b/gi,'[REDACTED]').slice(0,300);
|
|
96
|
+
return `Cloudflare call ${clean(call.operation)}: ${clean(call.reason ?? error.message)} (no Cloudflare error code or message supplied)`;
|
|
97
|
+
}
|
|
@@ -46,7 +46,7 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
|
|
|
46
46
|
assert(typeof declaration?.project === 'string','missing-owner-project');
|
|
47
47
|
// Removal reads identity before each object DELETE and may span many pages.
|
|
48
48
|
// Bound each response, not the owner's bucket size; failures retain ownership.
|
|
49
|
-
let requestWindow = propagation?.
|
|
49
|
+
let requestWindow = propagation?.uploaded ? propagation.window : undefined;
|
|
50
50
|
const request = fetch;
|
|
51
51
|
fetch = (input, init = {}) => {
|
|
52
52
|
if (!requestWindow?.active) return request(input, init);
|
|
@@ -94,7 +94,7 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
|
|
|
94
94
|
if (command === 'health') {
|
|
95
95
|
const readiness = {...(healthOptions?.now ? {clock:healthOptions.now} : {}), ...(healthOptions?.sleep ? {sleep:healthOptions.sleep} : {}),
|
|
96
96
|
...healthOptions?.readiness,single:true,totalMs:30000,
|
|
97
|
-
...(propagation ? {window:propagation.window,freshUpload:propagation.
|
|
97
|
+
...(propagation ? {window:propagation.window,freshUpload:propagation.uploaded && propagation.versionId === state.receipt?.provider.versionId} : {})};
|
|
98
98
|
await ownerHealth({api,target,plan,statePath,fetch,healthOptions:{...healthOptions,readiness}}).catch(error=>{throw api.annotateFailure(error,'health');});
|
|
99
99
|
return {ok:true,worker:target.worker,origin:target.origin};
|
|
100
100
|
}
|
|
@@ -110,7 +110,7 @@ export async function main(argv = process.argv.slice(2), env = process.env, {fet
|
|
|
110
110
|
});
|
|
111
111
|
const result = {ok:true,changed:receipt.changed,worker:target.worker,origin:receipt.origin,versionId:receipt.provider.versionId,created,sentSecrets,retiredSecrets,...(state.ownerClaim ? {ownerClaim:state.ownerClaim} : {}),state:statePath};
|
|
112
112
|
const sentTokens = new Map(sentSecrets.map(binding => [binding,env[declaration.secrets.find(item => item.binding === binding)?.environment]]));
|
|
113
|
-
Object.defineProperty(result, 'propagation', {value: {window,freshUpload,sentSecrets,versionId:receipt.provider.versionId,
|
|
113
|
+
Object.defineProperty(result, 'propagation', {value: {window,uploaded:receipt.changed,freshUpload,sentSecrets,versionId:receipt.provider.versionId,
|
|
114
114
|
matchesToken:(binding,token) => sentTokens.has(binding) && sentTokens.get(binding) === token}});
|
|
115
115
|
rememberReadiness(result);
|
|
116
116
|
return result;
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { r2ObjectPages, r2ObjectPath } from './r2-objects.mjs';
|
|
2
2
|
import { rm } from 'node:fs/promises';
|
|
3
|
-
import { assert } from './validation.mjs';
|
|
3
|
+
import { assert, hexId } from './validation.mjs';
|
|
4
4
|
import { resourceKinds, findResource } from './resources.mjs';
|
|
5
5
|
import { activeWorker } from './worker.mjs';
|
|
6
6
|
import { deleteWorker } from './teardown.mjs';
|
|
7
|
-
import { workerConsumerScript } from './queue-consumer.mjs';
|
|
8
7
|
import { query } from './migrations.mjs';
|
|
9
8
|
import { KINDS, readOwnerState, writeOwnerState, withStateLock, withScopeHints, withSettledLists, workerSettings, sameDeployment, resourceId, checkResourceIdentity, refuse, settlePendingSecret } from './owner.mjs';
|
|
10
9
|
|
|
@@ -75,7 +74,8 @@ export async function ownerRemove({api: provider, target, statePath, clock, slee
|
|
|
75
74
|
// A Worker that consumes one of these queues is released first.
|
|
76
75
|
for (const queue of doomed.filter(item => item.kind === 'queues')) {
|
|
77
76
|
for (const consumer of await api.list(`/queues/${queue.id}/consumers`, {pagination:'single'})) {
|
|
78
|
-
|
|
77
|
+
assert(hexId(consumer.consumer_id), 'invalid-consumer-id');
|
|
78
|
+
await api.request(`/queues/${queue.id}/consumers/${consumer.consumer_id}`, {method:'DELETE'});
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
if (worker) { await deleteWorker({api, worker, ...(clock ? {clock} : {}), ...(sleep ? {sleep} : {})}); removed.push(`worker:${worker}`); }
|
|
@@ -83,10 +83,13 @@ export async function ownerRemove({api: provider, target, statePath, clock, slee
|
|
|
83
83
|
for (const item of doomed) {
|
|
84
84
|
try {
|
|
85
85
|
if (item.kind === 'r2') await removeBucket(api, item);
|
|
86
|
-
else
|
|
86
|
+
else {
|
|
87
|
+
await remove[item.kind](api, item.id, item.name);
|
|
88
|
+
if (item.kind === 'queues') await waitForQueueRemoval(api,item,{clock,sleep});
|
|
89
|
+
}
|
|
87
90
|
removed.push(`${item.kind}:${item.name}`);
|
|
88
91
|
}
|
|
89
|
-
catch (error) { failed.push({resource:`${item.kind}:${item.name}`, code:error.message}); }
|
|
92
|
+
catch (error) { failed.push({resource:`${item.kind}:${item.name}`, code:error.message, error}); }
|
|
90
93
|
}
|
|
91
94
|
// Read back: nothing this environment created is still listed.
|
|
92
95
|
const remaining = [];
|
|
@@ -137,3 +140,18 @@ async function removeBucket(api, {name, entry}) {
|
|
|
137
140
|
await checkBucket(api, name, entry);
|
|
138
141
|
await api.request(`/r2/buckets/${name}`, {method:'DELETE'});
|
|
139
142
|
}
|
|
143
|
+
|
|
144
|
+
// Queue deletion and its account listing can settle separately. Finish the
|
|
145
|
+
// readback within this removal invocation, without replaying the DELETE.
|
|
146
|
+
async function waitForQueueRemoval(api, item, {clock = () => performance.now(), sleep = ms => new Promise(resolve => setTimeout(resolve,ms))}) {
|
|
147
|
+
const start = clock();
|
|
148
|
+
for (;;) {
|
|
149
|
+
if (!(await resourceKinds.queues.list(api)).some(row => row.queue_id === item.id)) return;
|
|
150
|
+
if (clock() - start >= 60000) {
|
|
151
|
+
const error = new Error('queue-removal-not-confirmed');
|
|
152
|
+
error.providerCall = {operation:'GET /queues',reason:'Cloudflare still lists the deleted queue after 60 seconds'};
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
await sleep(500);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { assert } from './validation.mjs';
|
|
2
|
+
|
|
3
|
+
// Request strategies describe the wire parameters only. Every endpoint uses
|
|
4
|
+
// this reader to decide whether a reply actually has another page.
|
|
5
|
+
export async function* providerPages(envelope, path, {field, pagination = 'page', zone = false, signal} = {}) {
|
|
6
|
+
assert(['page','cursor','r2-bounded','single'].includes(pagination), 'invalid-list-pagination');
|
|
7
|
+
assert(pagination !== 'r2-bounded' || path === '/r2/buckets' && field === 'buckets', 'invalid-r2-bounded-list');
|
|
8
|
+
const cursorMode = pagination === 'cursor' || pagination === 'r2-bounded';
|
|
9
|
+
const url = new URL(path, 'https://api.cloudflare.com');
|
|
10
|
+
const perPage = Number(url.searchParams.get('per_page') ?? url.searchParams.get('limit') ?? (cursorMode ? 1000 : pagination === 'single' ? 10000 : 100));
|
|
11
|
+
assert(Number.isSafeInteger(perPage) && perPage > 0, 'invalid-list-pagination');
|
|
12
|
+
let cursor, total, count = 0, pageMode = false;
|
|
13
|
+
const seen = new Set();
|
|
14
|
+
for (let page = 1; page <= 100; page++) {
|
|
15
|
+
const query = new URLSearchParams(url.searchParams);
|
|
16
|
+
if (cursor) {
|
|
17
|
+
query.delete('page');
|
|
18
|
+
if (pagination !== 'single' && !query.has('limit')) query.set('per_page',String(perPage));
|
|
19
|
+
query.set('cursor',cursor);
|
|
20
|
+
}
|
|
21
|
+
else if ((pageMode || !cursorMode) && (pagination !== 'single' || page > 1)) query.set('page',String(page));
|
|
22
|
+
if (pagination !== 'single' && !query.has('limit')) query.set('per_page',String(perPage));
|
|
23
|
+
const requestPath = url.pathname + (query.size ? `?${query}` : '');
|
|
24
|
+
let data, rows, next;
|
|
25
|
+
try {
|
|
26
|
+
data = await envelope(requestPath, {zone,signal});
|
|
27
|
+
rows = field ? data.result?.[field] : data.result;
|
|
28
|
+
assert(Array.isArray(rows), 'provider-invalid-list');
|
|
29
|
+
assert(rows.length <= perPage, 'provider-partial-list');
|
|
30
|
+
count += rows.length;
|
|
31
|
+
const info = data.result_info;
|
|
32
|
+
if (info == null) {
|
|
33
|
+
assert(rows.length < perPage, 'provider-incomplete-pagination');
|
|
34
|
+
assert(total === undefined || count === total, 'provider-partial-list');
|
|
35
|
+
} else {
|
|
36
|
+
assert(typeof info === 'object' && !Array.isArray(info), 'provider-incomplete-pagination');
|
|
37
|
+
if (info.per_page !== undefined) {
|
|
38
|
+
assert(Number.isSafeInteger(info.per_page) && info.per_page > 0 && info.per_page <= perPage, 'provider-incomplete-pagination');
|
|
39
|
+
assert(rows.length <= info.per_page, 'provider-partial-list');
|
|
40
|
+
}
|
|
41
|
+
if (info.count !== undefined) assert(info.count === rows.length, 'provider-partial-list');
|
|
42
|
+
if (info.page !== undefined) assert(info.page === page, 'provider-incomplete-pagination');
|
|
43
|
+
if (info.total_count !== undefined) {
|
|
44
|
+
assert(Number.isSafeInteger(info.total_count) && info.total_count >= 0, 'provider-incomplete-pagination');
|
|
45
|
+
total ??= info.total_count;
|
|
46
|
+
assert(total === info.total_count, 'provider-pagination-changed');
|
|
47
|
+
assert(count <= total, 'provider-partial-list');
|
|
48
|
+
}
|
|
49
|
+
if (info.total_pages !== undefined) assert(Number.isSafeInteger(info.total_pages) && info.total_pages >= 0 && (info.total_pages > 0 || count === 0), 'provider-incomplete-pagination');
|
|
50
|
+
if (info.is_truncated !== undefined) assert(typeof info.is_truncated === 'boolean', 'provider-incomplete-pagination');
|
|
51
|
+
assert(!info.delimited || Array.isArray(info.delimited) && !info.delimited.length, 'provider-incomplete-pagination');
|
|
52
|
+
assert(info.cursor == null || typeof info.cursor === 'string', 'provider-invalid-cursor');
|
|
53
|
+
const morePages = info.total_pages !== undefined && page < info.total_pages;
|
|
54
|
+
const moreRows = total !== undefined && count < total;
|
|
55
|
+
if (info.cursor) {
|
|
56
|
+
assert(info.is_truncated !== false, 'provider-invalid-cursor');
|
|
57
|
+
assert((info.total_pages === undefined || morePages) && (total === undefined || moreRows), 'provider-partial-list');
|
|
58
|
+
assert(info.cursor.length <= 4096 && /^[a-zA-Z0-9_+\/=-]+$/.test(info.cursor) && !seen.has(info.cursor), 'provider-invalid-cursor');
|
|
59
|
+
seen.add(info.cursor); next = info.cursor;
|
|
60
|
+
} else if (info.is_truncated === true) {
|
|
61
|
+
throw new Error('provider-incomplete-pagination');
|
|
62
|
+
} else if (cursorMode && moreRows && !morePages) {
|
|
63
|
+
throw new Error('provider-partial-list');
|
|
64
|
+
} else if (morePages || moreRows) {
|
|
65
|
+
assert(info.total_pages === undefined || morePages, 'provider-partial-list');
|
|
66
|
+
next = true;
|
|
67
|
+
} else {
|
|
68
|
+
assert(total === undefined || count === total, 'provider-partial-list');
|
|
69
|
+
}
|
|
70
|
+
if (next) assert(rows.length > 0 || morePages || typeof next === 'string', 'provider-partial-list');
|
|
71
|
+
}
|
|
72
|
+
} catch (error) {
|
|
73
|
+
// No response bodies, query values, account IDs or cursors in diagnostics.
|
|
74
|
+
error.providerCall ??= {operation:`GET ${url.pathname}`,reason:error.message};
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
yield {rows,data};
|
|
78
|
+
if (!next) return;
|
|
79
|
+
cursor = typeof next === 'string' ? next : undefined;
|
|
80
|
+
pageMode = next === true;
|
|
81
|
+
}
|
|
82
|
+
const error = new Error('provider-pagination-limit');
|
|
83
|
+
error.providerCall = {operation:`GET ${url.pathname}`,reason:'Cloudflare continued advertising pages beyond the request limit'};
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Keep result wrappers (versions/items, schedules, deployments) for existing kit
|
|
88
|
+
// callers while ensuring their GETs cannot bypass pagination. Query filters stay
|
|
89
|
+
// on every request; writes never pass through the reader.
|
|
90
|
+
export function providerListShape(path) {
|
|
91
|
+
const base = path.split('?')[0];
|
|
92
|
+
if (base === '/r2/buckets') return {field:'buckets',pagination:'cursor'};
|
|
93
|
+
if (/^\/r2\/buckets\/[^/]+\/objects$/.test(base) || /^\/storage\/kv\/namespaces\/[^/]+\/keys$/.test(base)) return {pagination:'cursor'};
|
|
94
|
+
const field = /\/workers\/scripts\/[^/]+\/(versions|deployments|schedules)$/.exec(base)?.[1];
|
|
95
|
+
if (field) return {field:field === 'versions' ? 'items' : field,...(field === 'schedules' ? {pagination:'single'} : {})};
|
|
96
|
+
if (['/d1/database','/storage/kv/namespaces','/queues','/workers/durable_objects/namespaces','/workers/domains','/zones','/containers/applications'].includes(base)) return {};
|
|
97
|
+
if (['/workers/scripts','/d1/database','/storage/kv/namespaces','/queues','/workers/durable_objects/namespaces','/workers/domains','/zones'].includes(base) || /^\/queues\/[^/]+\/consumers$/.test(base) || /^\/workers\/scripts\/[^/]+\/secrets$/.test(base) || /^\/zones\/[^/]+\/workers\/routes$/.test(base)) return {pagination:'single'};
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { providerPages, providerListShape } from './pagination.mjs';
|
|
1
2
|
import { deleteR2Object } from './r2-object-transport.mjs';
|
|
2
3
|
import { isR2ObjectDelete } from './r2-objects.mjs';
|
|
3
4
|
import { recordDeployFailure } from './diagnostics.mjs';
|
|
@@ -30,7 +31,7 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
|
|
|
30
31
|
assert(typeof token === 'string' && token.trim() && !/[\r\n]/.test(token), 'invalid-cloudflare-api-token');
|
|
31
32
|
let requests = 0;
|
|
32
33
|
// `zone` scope: the zone-level API (/zones/...), for a run site's zone lookup and redirect rule.
|
|
33
|
-
async function
|
|
34
|
+
async function rawEnvelope(path, { method = 'GET', body, assetToken, zone = false, signal: callerSignal } = {}) {
|
|
34
35
|
assert(typeof path === 'string' && (isR2ObjectDelete(path, method) || /^\/[a-z0-9/_?=&.,%-]+$/i.test(path) && !path.includes('..') && !path.split('?')[0].includes('%')), 'invalid-provider-path');
|
|
35
36
|
assert(!zone || /^\/zones(?:[/?]|$)/.test(path), 'invalid-provider-path');
|
|
36
37
|
assert(++requests <= maxRequests, 'provider-request-budget');
|
|
@@ -69,7 +70,7 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
|
|
|
69
70
|
const bounded = text => redactAuth(text).replace(/[\x00-\x1f\x7f-\x9f]+/g,' ').trim().slice(0,300);
|
|
70
71
|
const said = (Array.isArray(payload?.errors) ? payload.errors : []).filter(x => Number.isSafeInteger(x?.code) || typeof x?.message === 'string').slice(0,3)
|
|
71
72
|
.map(x => ({...(Number.isSafeInteger(x.code) ? {code:x.code} : {}),...(typeof x.message === 'string' ? {message:bounded(x.message)} : {})}));
|
|
72
|
-
error.providerRefusal = {operation
|
|
73
|
+
error.providerRefusal = {operation:redactCredentials(operation),errors:said.length || payload !== undefined || !output.trim() ? said : [{message:bounded(output.trim().split('\n')[0])}]};
|
|
73
74
|
throw recordDeployFailure(error,{providerCodes:(Array.isArray(payload?.errors) ? payload.errors : []).filter(x => Number.isSafeInteger(x?.code)).map(x=>x.code).slice(0,8),
|
|
74
75
|
output:redactAuth(Array.isArray(payload?.errors) ? payload.errors.filter(x=>typeof x?.message==='string').map(x=>x.message).join('\n') : output)});
|
|
75
76
|
}
|
|
@@ -80,79 +81,38 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
|
|
|
80
81
|
// D1 can fail inside a successful HTTP/API envelope. Read only typed
|
|
81
82
|
// error fields; query results and metadata are never diagnostic output.
|
|
82
83
|
const errors = failed.flatMap(row => [typeof row.error === 'string' ? {message:row.error} : row.error, ...(Array.isArray(row.errors) ? row.errors : [])]);
|
|
83
|
-
|
|
84
|
+
const error = new Error('d1-query-failed');
|
|
85
|
+
error.providerRefusal = {operation:redactCredentials(operation),errors:errors.filter(item => Number.isSafeInteger(item?.code) || typeof item?.message === 'string').map(item => ({code:item.code,message:typeof item.message === 'string' ? redactAuth(item.message).slice(0,300) : undefined})).slice(0,3)};
|
|
86
|
+
throw recordDeployFailure(error,{providerCodes:errors.filter(error=>Number.isSafeInteger(error?.code)).map(error=>error.code).slice(0,8),
|
|
84
87
|
output:redactAuth(errors.filter(error=>typeof error?.message==='string').map(error=>error.message).join('\n'))});
|
|
85
88
|
}
|
|
86
89
|
}
|
|
87
90
|
return payload;
|
|
88
91
|
}
|
|
89
|
-
async function
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const info = data.result_info;
|
|
95
|
-
if (info != null) {
|
|
96
|
-
assert(typeof info === 'object' && !Array.isArray(info),'provider-incomplete-pagination');
|
|
97
|
-
for (const key of ['count','total_count']) if (Object.hasOwn(info,key)) assert(info[key] === data.result.length,'provider-partial-list');
|
|
98
|
-
assert([undefined,null,''].includes(info.cursor) &&
|
|
99
|
-
(info.total_pages === undefined || info.total_pages === 1 || info.total_pages === 0 && data.result.length === 0) &&
|
|
100
|
-
(info.page === undefined || info.page === 1),'provider-incomplete-pagination');
|
|
101
|
-
}
|
|
102
|
-
return data.result;
|
|
92
|
+
async function envelope(path, options = {}) {
|
|
93
|
+
try { return await rawEnvelope(path,options); }
|
|
94
|
+
catch (error) {
|
|
95
|
+
error.providerCall ??= {operation:`${options.method ?? 'GET'} ${path.split('?')[0].replace(/(\/r2\/buckets\/[^/]+\/objects)\/.*/, '$1/[object]')}`,reason:error.message};
|
|
96
|
+
throw error;
|
|
103
97
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
assert(Array.isArray(rows), 'provider-invalid-list');
|
|
112
|
-
result.push(...rows); assert(result.length <= 10000, 'provider-list-too-large');
|
|
113
|
-
if (r2Bounded) {
|
|
114
|
-
const info = data.result_info;
|
|
115
|
-
assert(rows.length <= 1000, 'provider-partial-list');
|
|
116
|
-
// Only this endpoint's explicit bounded strategy uses the requested
|
|
117
|
-
// limit when metadata is absent. Never invent metadata for other modes.
|
|
118
|
-
if (info === undefined || info === null) {
|
|
119
|
-
assert(rows.length < 1000, 'provider-incomplete-pagination');
|
|
120
|
-
assert(total === undefined || result.length === total, 'provider-partial-list');
|
|
121
|
-
return result;
|
|
122
|
-
}
|
|
123
|
-
assert(typeof info === 'object' && !Array.isArray(info) && Number.isSafeInteger(info.per_page) && info.per_page > 0 && info.per_page <= 1000, 'provider-incomplete-pagination');
|
|
124
|
-
assert(rows.length <= info.per_page, 'provider-partial-list');
|
|
125
|
-
if (Object.hasOwn(info, 'count')) assert(info.count === rows.length, 'provider-partial-list');
|
|
126
|
-
if (Object.hasOwn(info, 'total_count')) {
|
|
127
|
-
assert(Number.isSafeInteger(info.total_count) && info.total_count >= 0, 'provider-incomplete-pagination');
|
|
128
|
-
total ??= info.total_count; assert(total === info.total_count, 'provider-pagination-changed');
|
|
129
|
-
}
|
|
130
|
-
assert(total === undefined || result.length <= total, 'provider-partial-list');
|
|
131
|
-
assert(info.cursor === undefined || typeof info.cursor === 'string', 'provider-invalid-cursor');
|
|
132
|
-
if (!info.cursor) {
|
|
133
|
-
assert(rows.length < info.per_page && (total === undefined || result.length === total), 'provider-partial-list');
|
|
134
|
-
return result;
|
|
135
|
-
}
|
|
136
|
-
assert(total === undefined || result.length < total, 'provider-partial-list');
|
|
137
|
-
cursor = info.cursor; assert(/^[a-zA-Z0-9_+\/=-]+$/.test(cursor) && !seen.has(cursor), 'provider-invalid-cursor'); seen.add(cursor);
|
|
138
|
-
} else if (pagination === 'cursor') {
|
|
139
|
-
const info = data.result_info;
|
|
140
|
-
// R2's terminal short page may explicitly report null pagination. The
|
|
141
|
-
// requested documented limit supplies the bound; a full page is ambiguous.
|
|
142
|
-
if (path === '/r2/buckets' && info === null) { assert(rows.length < 1000, 'provider-incomplete-pagination'); return result; }
|
|
143
|
-
assert(info && Number.isSafeInteger(info.per_page) && info.per_page > 0, 'provider-incomplete-pagination');
|
|
144
|
-
if (!info.cursor) { assert(rows.length < info.per_page, 'provider-partial-list'); return result; }
|
|
145
|
-
cursor = info.cursor; assert(typeof cursor === 'string' && /^[a-zA-Z0-9_+\/=-]+$/.test(cursor) && !seen.has(cursor), 'provider-invalid-cursor'); seen.add(cursor);
|
|
146
|
-
} else {
|
|
147
|
-
const info = data.result_info;
|
|
148
|
-
assert(info && info.page === page && Number.isSafeInteger(info.total_count) && info.total_count >= 0, 'provider-incomplete-pagination');
|
|
149
|
-
total ??= info.total_count; assert(total === info.total_count, 'provider-pagination-changed');
|
|
150
|
-
assert(result.length <= total, 'provider-partial-list');
|
|
151
|
-
if (result.length === total) return result;
|
|
152
|
-
assert(rows.length > 0 && (info.total_pages === undefined || page < info.total_pages), 'provider-partial-list');
|
|
153
|
-
}
|
|
98
|
+
}
|
|
99
|
+
async function collect(path, options) {
|
|
100
|
+
let result, rows = [];
|
|
101
|
+
for await (const page of providerPages(envelope, path, options)) {
|
|
102
|
+
result ??= page.data.result;
|
|
103
|
+
rows.push(...page.rows);
|
|
104
|
+
assert(rows.length <= 10000, 'provider-list-too-large');
|
|
154
105
|
}
|
|
155
|
-
|
|
106
|
+
return options?.field ? {...result,[options.field]:rows} : rows;
|
|
107
|
+
}
|
|
108
|
+
async function list(path, options = {}) {
|
|
109
|
+
const shape = {...providerListShape(path),...options};
|
|
110
|
+
const result = await collect(path,shape);
|
|
111
|
+
return shape.field ? result[shape.field] : result;
|
|
112
|
+
}
|
|
113
|
+
async function requestResult(path, options = {}) {
|
|
114
|
+
const shape = (!options.method || options.method === 'GET') && providerListShape(path);
|
|
115
|
+
return shape ? collect(path,{...shape,zone:options.zone,signal:options.signal}) : (await envelope(path,options)).result;
|
|
156
116
|
}
|
|
157
117
|
async function upload(url, bytes, etag) {
|
|
158
118
|
const destination = importUploadURL(url);
|
|
@@ -167,6 +127,6 @@ export function cloudflare({ accountId, token, fetch: request = globalThis.fetch
|
|
|
167
127
|
assert(response.status === 200, 'd1-import-upload-failed');
|
|
168
128
|
assert(response.headers.get('etag')?.replace(/^"|"$/g, '') === etag, 'd1-import-upload-etag-mismatch');
|
|
169
129
|
}
|
|
170
|
-
return { accountId, envelope, list, importSql(id, sql) { return importD1({envelope,upload}, id, sql); },
|
|
171
|
-
|
|
130
|
+
return { accountId, envelope, list, importSql(id, sql) { return importD1({envelope,upload}, id, sql); }, request:requestResult,
|
|
131
|
+
zoneRequest(path, options = {}) { return requestResult(path, { ...options, zone: true }); } };
|
|
172
132
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { providerPages } from './pagination.mjs';
|
|
1
2
|
import { assert } from './validation.mjs';
|
|
2
3
|
|
|
3
4
|
// Cloudflare's object REST API requires literal slashes, with other reserved
|
|
@@ -17,24 +18,12 @@ export function isR2ObjectDelete(path, method) {
|
|
|
17
18
|
// The API returns result: Object[] and result_info.is_truncated/cursor, not the
|
|
18
19
|
// bucket-list shape. Yield bounded pages so removal never collects the bucket.
|
|
19
20
|
export async function* r2ObjectPages(api, bucket) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const data = await api.envelope(`/r2/buckets/${bucket}/objects?per_page=1000${cursor ? `&cursor=${encodeURIComponent(cursor)}` : ''}`);
|
|
23
|
-
const rows = data.result, info = data.result_info;
|
|
24
|
-
assert(Array.isArray(rows) && rows.length <= 1000 && rows.every(row => typeof row?.key === 'string' && row.key.length), 'invalid-r2-object-list');
|
|
21
|
+
try { for await (const {rows} of providerPages(api.envelope, `/r2/buckets/${bucket}/objects`, {pagination:'cursor'})) {
|
|
22
|
+
assert(rows.length <= 1000 && rows.every(row => typeof row?.key === 'string' && row.key.length), 'invalid-r2-object-list');
|
|
25
23
|
assert(new Set(rows.map(row => row.key)).size === rows.length, 'invalid-r2-object-list');
|
|
26
|
-
// Cloudflare may omit pagination (or return null) on any terminal page
|
|
27
|
-
// shorter than the requested limit. A full page cannot prove completion.
|
|
28
|
-
if (info == null) {
|
|
29
|
-
assert(rows.length < 1000, 'provider-incomplete-pagination');
|
|
30
|
-
yield rows; return;
|
|
31
|
-
}
|
|
32
|
-
assert(typeof info.is_truncated === 'boolean' && (!info.delimited || Array.isArray(info.delimited) && !info.delimited.length), 'provider-incomplete-pagination');
|
|
33
|
-
if (info.is_truncated) {
|
|
34
|
-
assert(rows.length > 0 && typeof info.cursor === 'string' && info.cursor.length > 0 && info.cursor.length <= 4096 && !seen.has(info.cursor), 'provider-invalid-cursor');
|
|
35
|
-
cursor = info.cursor; seen.add(cursor);
|
|
36
|
-
} else assert(info.cursor == null || info.cursor === '', 'provider-invalid-cursor');
|
|
37
24
|
yield rows;
|
|
38
|
-
|
|
25
|
+
} } catch (error) {
|
|
26
|
+
error.providerCall ??= {operation:`GET /r2/buckets/${bucket}/objects`,reason:error.message};
|
|
27
|
+
throw error;
|
|
39
28
|
}
|
|
40
29
|
}
|
|
@@ -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
|
+
}
|