@astrale-os/adapter-cloudflare 0.5.0-beta.19 → 0.5.0-beta.191
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 +241 -2
- package/dist/adapter.d.ts +8 -17
- package/dist/adapter.js +41 -310
- package/dist/build/bundle/admit.d.ts +3 -0
- package/dist/build/bundle/admit.js +46 -0
- package/dist/build/bundle/compiled-wasm-loader.d.ts +8 -0
- package/dist/build/bundle/compiled-wasm-loader.js +21 -0
- package/dist/build/bundler.d.ts +21 -0
- package/dist/build/bundler.js +96 -0
- package/dist/build/closure.d.ts +4 -2
- package/dist/build/closure.js +7 -2
- package/dist/build/codegen/execution.d.ts +3 -14
- package/dist/build/codegen/execution.js +2 -41
- package/dist/build/codegen/merge.d.ts +0 -1
- package/dist/build/codegen/merge.js +0 -1
- package/dist/build/codegen/wrangler.d.ts +30 -6
- package/dist/build/codegen/wrangler.js +76 -20
- package/dist/build/configuration/invocation-timeout.d.ts +2 -0
- package/dist/build/configuration/invocation-timeout.js +9 -0
- package/dist/build/configuration/request-limit.d.ts +2 -0
- package/dist/build/configuration/request-limit.js +9 -0
- package/dist/build/configuration/router.d.ts +7 -0
- package/dist/build/configuration/router.js +17 -0
- package/dist/build/configuration/settings.d.ts +6 -0
- package/dist/build/configuration/settings.js +40 -0
- package/dist/build/configuration.d.ts +4 -9
- package/dist/build/configuration.js +4 -26
- package/dist/build/frontend/files.d.ts +19 -0
- package/dist/build/frontend/files.js +99 -0
- package/dist/build/frontend/readiness.d.ts +16 -0
- package/dist/build/frontend/readiness.js +21 -0
- package/dist/build/frontend/revision.d.ts +5 -0
- package/dist/build/frontend/revision.js +14 -0
- package/dist/build/frontend/vite.d.ts +14 -0
- package/dist/build/frontend/vite.js +42 -0
- package/dist/build/frontend/vite.process.d.ts +7 -0
- package/dist/build/frontend/vite.process.js +65 -0
- package/dist/build/frontend/vite.runner.d.ts +1 -0
- package/dist/build/frontend/vite.runner.js +155 -0
- package/dist/build/index.d.ts +10 -5
- package/dist/build/index.js +10 -5
- package/dist/build/material.d.ts +10 -0
- package/dist/build/material.js +25 -0
- package/dist/build/metafile.d.ts +0 -1
- package/dist/build/metafile.js +0 -1
- package/dist/build/qualification.d.ts +0 -1
- package/dist/build/qualification.js +0 -1
- package/dist/build/roots.d.ts +0 -1
- package/dist/build/roots.js +0 -1
- package/dist/build/worker/codegen/entry.d.ts +9 -0
- package/dist/build/worker/codegen/entry.js +65 -0
- package/dist/build/worker/codegen/literal.d.ts +28 -0
- package/dist/build/worker/codegen/literal.js +136 -0
- package/dist/build/worker/codegen/service.d.ts +14 -0
- package/dist/build/worker/codegen/service.js +57 -0
- package/dist/build/worker/execution.prepare.d.ts +20 -0
- package/dist/build/worker/execution.prepare.js +128 -0
- package/dist/build/worker/media-type.d.ts +1 -0
- package/dist/build/worker/media-type.js +20 -0
- package/dist/build/worker/modules.d.ts +20 -0
- package/dist/build/worker/modules.js +32 -0
- package/dist/build/worker/specifier.d.ts +5 -0
- package/dist/build/worker/specifier.js +15 -0
- package/dist/build/worker/upload.d.ts +23 -0
- package/dist/build/worker/upload.js +42 -0
- package/dist/cloudflare.d.ts +1 -2
- package/dist/cloudflare.js +1 -2
- package/dist/hosting/activation.d.ts +38 -0
- package/dist/hosting/activation.js +109 -0
- package/dist/hosting/assets.d.ts +14 -0
- package/dist/hosting/assets.js +53 -0
- package/dist/hosting/bounded.d.ts +10 -0
- package/dist/hosting/bounded.js +32 -0
- package/dist/hosting/bytes.d.ts +6 -0
- package/dist/hosting/bytes.js +25 -0
- package/dist/hosting/client.d.ts +144 -0
- package/dist/hosting/client.js +491 -0
- package/dist/hosting/dispatcher/cron.d.mts +10 -0
- package/dist/hosting/dispatcher/cron.mjs +135 -0
- package/dist/hosting/dispatcher/deployments.d.mts +26 -0
- package/dist/hosting/dispatcher/deployments.mjs +158 -0
- package/dist/hosting/dispatcher/dispatcher.d.mts +17 -0
- package/dist/hosting/dispatcher/dispatcher.mjs +76 -0
- package/dist/hosting/dispatcher/env.d.mts +51 -0
- package/dist/hosting/dispatcher/failure.d.mts +4 -0
- package/dist/hosting/dispatcher/failure.mjs +15 -0
- package/dist/hosting/dispatcher/index.d.mts +4 -0
- package/dist/hosting/dispatcher/index.mjs +8 -0
- package/dist/hosting/dispatcher/modules.d.ts +24 -0
- package/dist/hosting/dispatcher/modules.js +55 -0
- package/dist/hosting/dispatcher/services.d.mts +23 -0
- package/dist/hosting/dispatcher/services.mjs +202 -0
- package/dist/hosting/identity.d.ts +15 -0
- package/dist/hosting/identity.js +36 -0
- package/dist/hosting/index.d.ts +34 -0
- package/dist/hosting/index.js +32 -0
- package/dist/hosting/kv.d.ts +90 -0
- package/dist/hosting/kv.js +207 -0
- package/dist/hosting/listing.d.ts +34 -0
- package/dist/hosting/listing.js +50 -0
- package/dist/hosting/names.d.ts +16 -0
- package/dist/hosting/names.js +28 -0
- package/dist/hosting/provision/account.d.ts +11 -0
- package/dist/hosting/provision/account.js +86 -0
- package/dist/hosting/provision/dispatcher.d.ts +42 -0
- package/dist/hosting/provision/dispatcher.js +99 -0
- package/dist/hosting/provision/ingress.d.ts +12 -0
- package/dist/hosting/provision/ingress.js +31 -0
- package/dist/hosting/provision/plan.d.ts +143 -0
- package/dist/hosting/provision/plan.js +246 -0
- package/dist/hosting/provision/routing.d.ts +82 -0
- package/dist/hosting/provision/routing.js +226 -0
- package/dist/hosting/readiness.d.ts +53 -0
- package/dist/hosting/readiness.js +82 -0
- package/dist/hosting/refusal.d.ts +28 -0
- package/dist/hosting/refusal.js +13 -0
- package/dist/hosting/retirement.d.ts +70 -0
- package/dist/hosting/retirement.js +107 -0
- package/dist/hosting/retry-after.d.ts +6 -0
- package/dist/hosting/retry-after.js +55 -0
- package/dist/hosting/script.d.ts +186 -0
- package/dist/hosting/script.js +324 -0
- package/dist/hosting/testing/fake-cloudflare.d.ts +212 -0
- package/dist/hosting/testing/fake-cloudflare.js +757 -0
- package/dist/hosting/testing/index.d.ts +6 -0
- package/dist/hosting/testing/index.js +6 -0
- package/dist/hosting/time.d.ts +4 -0
- package/dist/hosting/time.js +20 -0
- package/dist/identity/binding.d.ts +2 -0
- package/dist/identity/binding.js +2 -0
- package/dist/identity/material.d.ts +6 -0
- package/dist/identity/material.js +29 -0
- package/dist/index.d.ts +14 -13
- package/dist/index.js +13 -12
- package/dist/namespace/configure.d.ts +22 -0
- package/dist/namespace/configure.js +199 -0
- package/dist/namespace/coordinates.d.ts +15 -0
- package/dist/namespace/coordinates.js +49 -0
- package/dist/namespace/deploy-release.d.ts +53 -0
- package/dist/namespace/deploy-release.js +389 -0
- package/dist/namespace/hosting.d.ts +29 -0
- package/dist/namespace/hosting.js +120 -0
- package/dist/namespace/index.d.ts +12 -0
- package/dist/namespace/index.js +12 -0
- package/dist/namespace/list.d.ts +18 -0
- package/dist/namespace/list.js +63 -0
- package/dist/namespace/readiness.d.ts +30 -0
- package/dist/namespace/readiness.js +31 -0
- package/dist/namespace/record.d.ts +21 -0
- package/dist/namespace/record.js +53 -0
- package/dist/params.d.ts +59 -66
- package/dist/params.js +0 -1
- package/dist/release/build.d.ts +35 -0
- package/dist/release/build.js +44 -0
- package/dist/release/deployment.d.ts +44 -0
- package/dist/release/deployment.js +141 -0
- package/dist/release/index.d.ts +2 -0
- package/dist/release/index.js +2 -0
- package/dist/worker/build-digest.d.ts +10 -0
- package/dist/worker/build-digest.js +22 -0
- package/dist/worker/cache.d.ts +28 -0
- package/dist/worker/cache.js +70 -0
- package/dist/worker/durable.d.ts +3 -0
- package/dist/worker/durable.js +5 -0
- package/dist/worker/entry.d.ts +38 -25
- package/dist/worker/entry.js +82 -110
- package/dist/worker/fetch.d.ts +0 -1
- package/dist/worker/fetch.js +0 -1
- package/dist/worker/front.d.ts +56 -0
- package/dist/worker/front.js +59 -0
- package/dist/worker/frontend.d.ts +4 -0
- package/dist/worker/frontend.js +83 -0
- package/dist/worker/identity.d.ts +7 -0
- package/dist/worker/identity.js +23 -0
- package/dist/worker/index.d.ts +5 -1
- package/dist/worker/index.js +5 -1
- package/dist/worker/restate.d.ts +6 -0
- package/dist/worker/restate.js +6 -0
- package/dist/worker/settings.d.ts +47 -0
- package/dist/worker/settings.js +161 -0
- package/dist/wrangler/bundle.d.ts +10 -0
- package/dist/wrangler/bundle.js +27 -0
- package/dist/wrangler/index.d.ts +3 -0
- package/dist/wrangler/index.js +3 -0
- package/dist/wrangler/process.d.ts +15 -0
- package/dist/wrangler/process.js +102 -0
- package/dist/wrangler/type-generation.d.ts +6 -0
- package/dist/wrangler/type-generation.js +19 -0
- package/package.json +40 -16
- package/template/astrale.config.ts +43 -31
- package/dist/adapter.d.ts.map +0 -1
- package/dist/adapter.js.map +0 -1
- package/dist/build/artifact.d.ts +0 -9
- package/dist/build/artifact.d.ts.map +0 -1
- package/dist/build/artifact.js +0 -145
- package/dist/build/artifact.js.map +0 -1
- package/dist/build/closure.d.ts.map +0 -1
- package/dist/build/closure.js.map +0 -1
- package/dist/build/codegen/execution.d.ts.map +0 -1
- package/dist/build/codegen/execution.js.map +0 -1
- package/dist/build/codegen/identity.d.ts +0 -10
- package/dist/build/codegen/identity.d.ts.map +0 -1
- package/dist/build/codegen/identity.js +0 -32
- package/dist/build/codegen/identity.js.map +0 -1
- package/dist/build/codegen/merge.d.ts.map +0 -1
- package/dist/build/codegen/merge.js.map +0 -1
- package/dist/build/codegen/wrangler.d.ts.map +0 -1
- package/dist/build/codegen/wrangler.js.map +0 -1
- package/dist/build/configuration.d.ts.map +0 -1
- package/dist/build/configuration.js.map +0 -1
- package/dist/build/frontend-v1.d.ts +0 -17
- package/dist/build/frontend-v1.d.ts.map +0 -1
- package/dist/build/frontend-v1.js +0 -134
- package/dist/build/frontend-v1.js.map +0 -1
- package/dist/build/index.d.ts.map +0 -1
- package/dist/build/index.js.map +0 -1
- package/dist/build/metafile.d.ts.map +0 -1
- package/dist/build/metafile.js.map +0 -1
- package/dist/build/parse-output.d.ts +0 -27
- package/dist/build/parse-output.d.ts.map +0 -1
- package/dist/build/parse-output.js +0 -58
- package/dist/build/parse-output.js.map +0 -1
- package/dist/build/qualification.d.ts.map +0 -1
- package/dist/build/qualification.js.map +0 -1
- package/dist/build/roots.d.ts.map +0 -1
- package/dist/build/roots.js.map +0 -1
- package/dist/build/wrangler-cli.d.ts +0 -115
- package/dist/build/wrangler-cli.d.ts.map +0 -1
- package/dist/build/wrangler-cli.js +0 -377
- package/dist/build/wrangler-cli.js.map +0 -1
- package/dist/cloudflare.d.ts.map +0 -1
- package/dist/cloudflare.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/params.d.ts.map +0 -1
- package/dist/params.js.map +0 -1
- package/dist/worker/entry.d.ts.map +0 -1
- package/dist/worker/entry.js.map +0 -1
- package/dist/worker/fetch.d.ts.map +0 -1
- package/dist/worker/fetch.js.map +0 -1
- package/dist/worker/index.d.ts.map +0 -1
- package/dist/worker/index.js.map +0 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { WorkerRecord } from '@astrale-os/sdk/deployment/worker';
|
|
2
|
+
import type { RoutingKv } from './kv.js';
|
|
3
|
+
/**
|
|
4
|
+
* Activate one deployment: write `record:<label>`, which the dispatcher reads to route the label,
|
|
5
|
+
* with the record its deployer publishes (opaque bytes and their media type, served verbatim at
|
|
6
|
+
* `/.well-known/astrale/deployment.json`), or `null` for a deployment that publishes none. A
|
|
7
|
+
* record never changes once published: one already there is kept and answered when it is the same
|
|
8
|
+
* record (same media type, same bytes, or both none), and refused (`deployment-conflict`)
|
|
9
|
+
* otherwise, without any write.
|
|
10
|
+
*/
|
|
11
|
+
export declare function activateDeployment(kv: RoutingKv, label: string, record: WorkerRecord | null): Promise<WorkerRecord | null>;
|
|
12
|
+
/**
|
|
13
|
+
* The record a label was activated with: the record, `null` when the deployment publishes none,
|
|
14
|
+
* or undefined when the label is not activated. A value whose metadata is not exactly
|
|
15
|
+
* `{"contentType":<media type or null>}` (none at all included) is no record the kit writes, and
|
|
16
|
+
* is refused (`record-invalid`) rather than read as any record.
|
|
17
|
+
*/
|
|
18
|
+
export declare function readRecord(kv: RoutingKv, label: string): Promise<WorkerRecord | null | undefined>;
|
|
19
|
+
/** Whether two activations publish the same record (or both none). */
|
|
20
|
+
export declare function sameRecord(left: WorkerRecord | null, right: WorkerRecord | null): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Point a name (a deployment line) at the deployment its URL `https://<name>.<routing domain>`
|
|
23
|
+
* serves: `current:<name>` = `label`, or no deployment when `label` is null (the key is deleted).
|
|
24
|
+
* The label must be one of the name's own. Writing the same value again writes nothing, so a rerun
|
|
25
|
+
* converges.
|
|
26
|
+
*/
|
|
27
|
+
export declare function promoteDeployment(kv: RoutingKv, name: string, label: string | null): Promise<void>;
|
|
28
|
+
/** The deployment a name serves now, or null. */
|
|
29
|
+
export declare function readCurrent(kv: RoutingKv, name: string): Promise<string | null>;
|
|
30
|
+
/**
|
|
31
|
+
* Replace a name's crons with canonical UTC crons (`cron:<name>`), or delete them when the set is
|
|
32
|
+
* empty. A set that is not canonical is refused (`schedules-invalid`) before anything is written.
|
|
33
|
+
* The dispatcher fires a due cron on the name's current deployment, once its upload bound a
|
|
34
|
+
* `cron` trigger.
|
|
35
|
+
*/
|
|
36
|
+
export declare function replaceSchedules(kv: RoutingKv, name: string, crons: readonly string[]): Promise<readonly string[]>;
|
|
37
|
+
/** A name's crons: empty when it has none; a stored set that is not canonical is refused. */
|
|
38
|
+
export declare function readSchedules(kv: RoutingKv, name: string): Promise<readonly string[]>;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { parseDeploymentLabel } from '@astrale-os/sdk/deployment/address';
|
|
2
|
+
import { acceptWorkerRecord, sameWorkerRecord } from '@astrale-os/sdk/deployment/worker';
|
|
3
|
+
import { cronKey, currentKey, decodeCurrent, decodeSchedules, encodeSchedules, isLabelOf, recordKey, } from './kv.js';
|
|
4
|
+
import { HostingRefusal } from './refusal.js';
|
|
5
|
+
/** What `record:<label>` holds for a deployment that publishes no record: never served. */
|
|
6
|
+
const NO_RECORD = new TextEncoder().encode('-');
|
|
7
|
+
/**
|
|
8
|
+
* Activate one deployment: write `record:<label>`, which the dispatcher reads to route the label,
|
|
9
|
+
* with the record its deployer publishes (opaque bytes and their media type, served verbatim at
|
|
10
|
+
* `/.well-known/astrale/deployment.json`), or `null` for a deployment that publishes none. A
|
|
11
|
+
* record never changes once published: one already there is kept and answered when it is the same
|
|
12
|
+
* record (same media type, same bytes, or both none), and refused (`deployment-conflict`)
|
|
13
|
+
* otherwise, without any write.
|
|
14
|
+
*/
|
|
15
|
+
export async function activateDeployment(kv, label, record) {
|
|
16
|
+
parseDeploymentLabel(label);
|
|
17
|
+
const published = record === null ? null : acceptWorkerRecord(record);
|
|
18
|
+
const stored = await readRecord(kv, label);
|
|
19
|
+
if (stored !== undefined) {
|
|
20
|
+
if (!sameRecord(stored, published)) {
|
|
21
|
+
throw new HostingRefusal('deployment-conflict', `Another record is already published for ${label}; nothing was written.`);
|
|
22
|
+
}
|
|
23
|
+
return stored;
|
|
24
|
+
}
|
|
25
|
+
await kv.putWithMetadata(recordKey(label), published?.bytes ?? NO_RECORD, {
|
|
26
|
+
contentType: published?.contentType ?? null,
|
|
27
|
+
});
|
|
28
|
+
return published;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The record a label was activated with: the record, `null` when the deployment publishes none,
|
|
32
|
+
* or undefined when the label is not activated. A value whose metadata is not exactly
|
|
33
|
+
* `{"contentType":<media type or null>}` (none at all included) is no record the kit writes, and
|
|
34
|
+
* is refused (`record-invalid`) rather than read as any record.
|
|
35
|
+
*/
|
|
36
|
+
export async function readRecord(kv, label) {
|
|
37
|
+
const stored = await kv.getWithMetadata(recordKey(label));
|
|
38
|
+
return stored === undefined ? undefined : storedRecord(stored, label);
|
|
39
|
+
}
|
|
40
|
+
/** Admit what `record:<label>` holds, value and metadata, as the dispatcher serves it. */
|
|
41
|
+
function storedRecord(stored, label) {
|
|
42
|
+
const { metadata } = stored;
|
|
43
|
+
if (metadata === null) {
|
|
44
|
+
throw new HostingRefusal('record-invalid', `The record of ${label} has no metadata.`);
|
|
45
|
+
}
|
|
46
|
+
if (typeof metadata !== 'object' ||
|
|
47
|
+
Array.isArray(metadata) ||
|
|
48
|
+
Reflect.ownKeys(metadata).length !== 1 ||
|
|
49
|
+
!Object.hasOwn(metadata, 'contentType')) {
|
|
50
|
+
throw new HostingRefusal('record-invalid', `The record of ${label} has invalid metadata.`);
|
|
51
|
+
}
|
|
52
|
+
const { contentType } = metadata;
|
|
53
|
+
if (contentType === null)
|
|
54
|
+
return null;
|
|
55
|
+
try {
|
|
56
|
+
return acceptWorkerRecord({ contentType: contentType, bytes: stored.bytes });
|
|
57
|
+
}
|
|
58
|
+
catch (cause) {
|
|
59
|
+
throw new HostingRefusal('record-invalid', `The record of ${label} is not a record: ${String(cause)}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Whether two activations publish the same record (or both none). */
|
|
63
|
+
export function sameRecord(left, right) {
|
|
64
|
+
return left === null || right === null ? left === right : sameWorkerRecord(left, right);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Point a name (a deployment line) at the deployment its URL `https://<name>.<routing domain>`
|
|
68
|
+
* serves: `current:<name>` = `label`, or no deployment when `label` is null (the key is deleted).
|
|
69
|
+
* The label must be one of the name's own. Writing the same value again writes nothing, so a rerun
|
|
70
|
+
* converges.
|
|
71
|
+
*/
|
|
72
|
+
export async function promoteDeployment(kv, name, label) {
|
|
73
|
+
if (label !== null && !isLabelOf(label, name)) {
|
|
74
|
+
throw new HostingRefusal('route-invalid', `${label} is not a deployment of ${name}.`);
|
|
75
|
+
}
|
|
76
|
+
if ((await kv.get(currentKey(name))) === (label ?? undefined))
|
|
77
|
+
return;
|
|
78
|
+
if (label === null)
|
|
79
|
+
await kv.delete(currentKey(name));
|
|
80
|
+
else
|
|
81
|
+
await kv.put(currentKey(name), label, 'text/plain');
|
|
82
|
+
}
|
|
83
|
+
/** The deployment a name serves now, or null. */
|
|
84
|
+
export async function readCurrent(kv, name) {
|
|
85
|
+
const text = await kv.get(currentKey(name));
|
|
86
|
+
return text === undefined ? null : decodeCurrent(text, name);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Replace a name's crons with canonical UTC crons (`cron:<name>`), or delete them when the set is
|
|
90
|
+
* empty. A set that is not canonical is refused (`schedules-invalid`) before anything is written.
|
|
91
|
+
* The dispatcher fires a due cron on the name's current deployment, once its upload bound a
|
|
92
|
+
* `cron` trigger.
|
|
93
|
+
*/
|
|
94
|
+
export async function replaceSchedules(kv, name, crons) {
|
|
95
|
+
const encoded = encodeSchedules(crons);
|
|
96
|
+
if (encoded.text === undefined) {
|
|
97
|
+
if ((await kv.get(cronKey(name))) !== undefined)
|
|
98
|
+
await kv.delete(cronKey(name));
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
await kv.put(cronKey(name), encoded.text, 'application/json');
|
|
102
|
+
}
|
|
103
|
+
return encoded.crons;
|
|
104
|
+
}
|
|
105
|
+
/** A name's crons: empty when it has none; a stored set that is not canonical is refused. */
|
|
106
|
+
export async function readSchedules(kv, name) {
|
|
107
|
+
const text = await kv.get(cronKey(name));
|
|
108
|
+
return text === undefined ? Object.freeze([]) : decodeSchedules(text, name);
|
|
109
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { CloudflareClient } from './client.js';
|
|
2
|
+
/** One static asset of a deployment, served by its Worker through the `ASSETS` binding. */
|
|
3
|
+
export interface ScriptAsset {
|
|
4
|
+
/** The URL path it is served at, `/`-prefixed. */
|
|
5
|
+
readonly path: string;
|
|
6
|
+
readonly bytes: Uint8Array;
|
|
7
|
+
readonly contentType?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Upload one script's static assets through its namespace's asset upload session, the buckets
|
|
11
|
+
* Cloudflare asks for in parallel (at most `CLOUDFLARE_FAN_OUT` at once), and answer the completion
|
|
12
|
+
* token the script upload binds them with.
|
|
13
|
+
*/
|
|
14
|
+
export declare function uploadAssets(client: CloudflareClient, namespace: string, script: string, assets: readonly ScriptAsset[]): Promise<string>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { CLOUDFLARE_FAN_OUT, mapBounded } from './bounded.js';
|
|
2
|
+
import { base64, sha256Hex } from './bytes.js';
|
|
3
|
+
import { CloudflareApiError } from './client.js';
|
|
4
|
+
/**
|
|
5
|
+
* Upload one script's static assets through its namespace's asset upload session, the buckets
|
|
6
|
+
* Cloudflare asks for in parallel (at most `CLOUDFLARE_FAN_OUT` at once), and answer the completion
|
|
7
|
+
* token the script upload binds them with.
|
|
8
|
+
*/
|
|
9
|
+
export async function uploadAssets(client, namespace, script, assets) {
|
|
10
|
+
const byHash = new Map();
|
|
11
|
+
const manifest = {};
|
|
12
|
+
for (const asset of assets) {
|
|
13
|
+
const hash = (await sha256Hex(asset.bytes)).slice(0, 32);
|
|
14
|
+
byHash.set(hash, asset);
|
|
15
|
+
manifest[asset.path] = { hash, size: asset.bytes.byteLength };
|
|
16
|
+
}
|
|
17
|
+
const session = decodeSession(await client.json('POST', client.account(`/workers/dispatch/namespaces/${encodeURIComponent(namespace)}/scripts/${encodeURIComponent(script)}/assets-upload-session`), { manifest }));
|
|
18
|
+
if (session.buckets.length === 0)
|
|
19
|
+
return session.jwt;
|
|
20
|
+
const tokens = await mapBounded(session.buckets, CLOUDFLARE_FAN_OUT, async (bucket) => {
|
|
21
|
+
const form = new FormData();
|
|
22
|
+
for (const hash of bucket) {
|
|
23
|
+
const asset = byHash.get(hash);
|
|
24
|
+
if (asset === undefined)
|
|
25
|
+
invalid('Cloudflare asked for an unknown asset.');
|
|
26
|
+
form.append(hash, new File([base64(asset.bytes)], hash, {
|
|
27
|
+
type: asset.contentType ?? 'application/octet-stream',
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
const result = (await client.form('POST', client.account('/workers/assets/upload?base64=true'), form, { bearer: session.jwt }));
|
|
31
|
+
return typeof result?.jwt === 'string' && result.jwt.length > 0 ? result.jwt : undefined;
|
|
32
|
+
});
|
|
33
|
+
const completion = tokens.find((token) => token !== undefined);
|
|
34
|
+
if (completion === undefined)
|
|
35
|
+
invalid('Cloudflare returned no asset completion token.');
|
|
36
|
+
return completion;
|
|
37
|
+
}
|
|
38
|
+
function decodeSession(value) {
|
|
39
|
+
const session = value;
|
|
40
|
+
if (typeof session?.jwt !== 'string' || session.jwt.length === 0) {
|
|
41
|
+
invalid('Cloudflare returned an invalid asset upload session.');
|
|
42
|
+
}
|
|
43
|
+
const buckets = session.buckets ?? [];
|
|
44
|
+
if (!Array.isArray(buckets) ||
|
|
45
|
+
buckets.some((bucket) => !Array.isArray(bucket) ||
|
|
46
|
+
bucket.some((hash) => typeof hash !== 'string' || hash.length === 0))) {
|
|
47
|
+
invalid('Cloudflare returned invalid asset upload buckets.');
|
|
48
|
+
}
|
|
49
|
+
return { jwt: session.jwt, buckets: buckets };
|
|
50
|
+
}
|
|
51
|
+
function invalid(message) {
|
|
52
|
+
throw new CloudflareApiError('upstream', message);
|
|
53
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cloudflare API requests one fan-out keeps in flight. A Worker invocation holds at most six
|
|
3
|
+
* simultaneous connections, and Cloudflare's API rate limit is per user (the token owner).
|
|
4
|
+
*/
|
|
5
|
+
export declare const CLOUDFLARE_FAN_OUT = 6;
|
|
6
|
+
/**
|
|
7
|
+
* Map items through a sliding window of at most `limit` operations, preserving item order. The
|
|
8
|
+
* first failure stops new work and rejects once the work already in flight settles.
|
|
9
|
+
*/
|
|
10
|
+
export declare function mapBounded<Item, Result>(items: readonly Item[], limit: number, operation: (item: Item) => Promise<Result>): Promise<Result[]>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cloudflare API requests one fan-out keeps in flight. A Worker invocation holds at most six
|
|
3
|
+
* simultaneous connections, and Cloudflare's API rate limit is per user (the token owner).
|
|
4
|
+
*/
|
|
5
|
+
export const CLOUDFLARE_FAN_OUT = 6;
|
|
6
|
+
/**
|
|
7
|
+
* Map items through a sliding window of at most `limit` operations, preserving item order. The
|
|
8
|
+
* first failure stops new work and rejects once the work already in flight settles.
|
|
9
|
+
*/
|
|
10
|
+
export async function mapBounded(items, limit, operation) {
|
|
11
|
+
if (!Number.isSafeInteger(limit) || limit < 1) {
|
|
12
|
+
throw new RangeError('Fan-out limit must be a positive safe integer.');
|
|
13
|
+
}
|
|
14
|
+
const results = [];
|
|
15
|
+
let failure;
|
|
16
|
+
let next = 0;
|
|
17
|
+
const worker = async () => {
|
|
18
|
+
while (failure === undefined && next < items.length) {
|
|
19
|
+
const index = next++;
|
|
20
|
+
try {
|
|
21
|
+
results[index] = await operation(items[index]);
|
|
22
|
+
}
|
|
23
|
+
catch (cause) {
|
|
24
|
+
failure ??= { cause };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
29
|
+
if (failure !== undefined)
|
|
30
|
+
throw failure.cause;
|
|
31
|
+
return results;
|
|
32
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** The lower-case hexadecimal SHA-256 of text (UTF-8) or bytes, with WebCrypto. */
|
|
2
|
+
export declare function sha256Hex(input: string | Uint8Array): Promise<string>;
|
|
3
|
+
/** Bytes as base64, in chunks small enough for `String.fromCharCode`. */
|
|
4
|
+
export declare function base64(bytes: Uint8Array): string;
|
|
5
|
+
/** Bytes as unpadded base64url. */
|
|
6
|
+
export declare function base64url(bytes: Uint8Array): string;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const encoder = new TextEncoder();
|
|
2
|
+
/** The lower-case hexadecimal SHA-256 of text (UTF-8) or bytes, with WebCrypto. */
|
|
3
|
+
export async function sha256Hex(input) {
|
|
4
|
+
const bytes = typeof input === 'string' ? encoder.encode(input) : input;
|
|
5
|
+
const digest = await crypto.subtle.digest('SHA-256', bytes);
|
|
6
|
+
return hex(new Uint8Array(digest));
|
|
7
|
+
}
|
|
8
|
+
/** Bytes as base64, in chunks small enough for `String.fromCharCode`. */
|
|
9
|
+
export function base64(bytes) {
|
|
10
|
+
let binary = '';
|
|
11
|
+
for (let offset = 0; offset < bytes.byteLength; offset += 32_768) {
|
|
12
|
+
binary += String.fromCharCode(...bytes.subarray(offset, offset + 32_768));
|
|
13
|
+
}
|
|
14
|
+
return btoa(binary);
|
|
15
|
+
}
|
|
16
|
+
/** Bytes as unpadded base64url. */
|
|
17
|
+
export function base64url(bytes) {
|
|
18
|
+
return base64(bytes).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/u, '');
|
|
19
|
+
}
|
|
20
|
+
function hex(bytes) {
|
|
21
|
+
let text = '';
|
|
22
|
+
for (const byte of bytes)
|
|
23
|
+
text += byte.toString(16).padStart(2, '0');
|
|
24
|
+
return text;
|
|
25
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/** Cloudflare's REST API. The kit calls it directly: Wrangler has no create-only upload. */
|
|
2
|
+
export declare const CLOUDFLARE_API_BASE = "https://api.cloudflare.com/client/v4";
|
|
3
|
+
/**
|
|
4
|
+
* How the client waits out Cloudflare rate limits (HTTP 429) before failing as `unavailable`.
|
|
5
|
+
* Cloudflare refuses a rate-limited request before acting on it, so resending is always safe.
|
|
6
|
+
*/
|
|
7
|
+
export interface RateLimitPolicy {
|
|
8
|
+
/** Rate-limited requests resent, each after the provider's `Retry-After` (else 1 s, doubling). */
|
|
9
|
+
readonly retries: number;
|
|
10
|
+
/** Longest single wait; a later `Retry-After` fails at once as `unavailable`. */
|
|
11
|
+
readonly maximumWaitMs: number;
|
|
12
|
+
}
|
|
13
|
+
/** A deploy's policy: three resends, none waiting more than 10 s. */
|
|
14
|
+
export declare const DEFAULT_RATE_LIMIT: RateLimitPolicy;
|
|
15
|
+
/**
|
|
16
|
+
* An operator command's policy: it has no deadline, so it waits out the five minutes Cloudflare
|
|
17
|
+
* blocks a user (the token owner) that went over its API limit.
|
|
18
|
+
*/
|
|
19
|
+
export declare const OPERATOR_RATE_LIMIT: RateLimitPolicy;
|
|
20
|
+
export interface CloudflareClientOptions {
|
|
21
|
+
readonly accountId: string;
|
|
22
|
+
/** The API token, sent only to `base`; never logged, never part of an error. */
|
|
23
|
+
readonly token: string;
|
|
24
|
+
/** Default: the global `fetch`. With another one, `base` may name any host (a test fake). */
|
|
25
|
+
readonly fetch?: typeof fetch;
|
|
26
|
+
/** Default `CLOUDFLARE_API_BASE`; without `fetch`, only api.cloudflare.com or a loopback host. */
|
|
27
|
+
readonly base?: string;
|
|
28
|
+
/** Cancels every request and wait; a cancelled call rejects with the signal's reason. */
|
|
29
|
+
readonly signal?: AbortSignal;
|
|
30
|
+
/** An absolute instant (epoch ms) no request or rate-limit wait outlives. */
|
|
31
|
+
readonly deadline?: number;
|
|
32
|
+
/** Per-request timeout; multipart uploads get at least 120 s. Default 30 s. */
|
|
33
|
+
readonly timeoutMs?: number;
|
|
34
|
+
/** Default `DEFAULT_RATE_LIMIT`; `false` fails a rate-limited request at once. */
|
|
35
|
+
readonly rateLimit?: RateLimitPolicy | false;
|
|
36
|
+
/** Wait between rate-limited attempts; replaceable in tests. */
|
|
37
|
+
readonly pause?: (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
38
|
+
/** The clock `Retry-After` dates and the deadline are read on. */
|
|
39
|
+
readonly now?: () => number;
|
|
40
|
+
}
|
|
41
|
+
/** What a failure says about the request: whether to fix it, retry it, or treat it as absent. */
|
|
42
|
+
export type CloudflareFailureKind =
|
|
43
|
+
/** 404: the resource does not exist. */
|
|
44
|
+
'not_found'
|
|
45
|
+
/** 401/403: the token lacks the permission. */
|
|
46
|
+
| 'unauthorized'
|
|
47
|
+
/** Another 4xx: the request is refused as it is (412 for a create-only upload over a script). */
|
|
48
|
+
| 'invalid'
|
|
49
|
+
/** 5xx, 429, a network failure or a timeout: the request may or may not have been applied. */
|
|
50
|
+
| 'unavailable'
|
|
51
|
+
/** A success Cloudflare answered in a shape the kit does not admit. */
|
|
52
|
+
| 'upstream';
|
|
53
|
+
/**
|
|
54
|
+
* A Cloudflare API refusal or failure. `status` is the HTTP status (absent when no response came
|
|
55
|
+
* back); `codes` are the error codes of Cloudflare's envelope (10018 for a create-only upload over
|
|
56
|
+
* an existing script). The message names the operation, never an account, script, key or value.
|
|
57
|
+
*/
|
|
58
|
+
export declare class CloudflareApiError extends Error {
|
|
59
|
+
readonly name = "CloudflareApiError";
|
|
60
|
+
readonly kind: CloudflareFailureKind;
|
|
61
|
+
readonly status: number | undefined;
|
|
62
|
+
readonly codes: readonly number[];
|
|
63
|
+
constructor(kind: CloudflareFailureKind, message: string, status?: number, codes?: readonly number[], options?: {
|
|
64
|
+
readonly cause?: unknown;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** One page of a listing, as Cloudflare's `result_info` reports it. */
|
|
68
|
+
export interface CloudflarePage {
|
|
69
|
+
readonly result: unknown;
|
|
70
|
+
readonly cursor?: string;
|
|
71
|
+
readonly page?: number;
|
|
72
|
+
readonly totalPages?: number;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The Cloudflare account API the hosting kit calls: dispatch namespaces and their scripts, account
|
|
76
|
+
* Workers, Workers KV, zones' DNS records and Worker routes. It runs wherever `fetch` does (Node or
|
|
77
|
+
* a Worker), reads bounded bodies, waits out rate limits by policy and turns every failure into a
|
|
78
|
+
* `CloudflareApiError`. Responses are never followed across redirects.
|
|
79
|
+
*/
|
|
80
|
+
export declare class CloudflareClient {
|
|
81
|
+
readonly accountId: string;
|
|
82
|
+
private readonly token;
|
|
83
|
+
private readonly fetcher;
|
|
84
|
+
private readonly base;
|
|
85
|
+
private readonly signal;
|
|
86
|
+
private readonly deadline;
|
|
87
|
+
private readonly timeoutMs;
|
|
88
|
+
private readonly rateLimit;
|
|
89
|
+
private readonly pause;
|
|
90
|
+
private readonly now;
|
|
91
|
+
constructor(options: CloudflareClientOptions);
|
|
92
|
+
/** `/accounts/<account><path>`. */
|
|
93
|
+
account(path: string): string;
|
|
94
|
+
/** `/zones/<zone><path>`. */
|
|
95
|
+
zone(zoneId: string, path: string): string;
|
|
96
|
+
/** Send a JSON request (or none) and return the envelope's `result`. */
|
|
97
|
+
json(method: string, path: string, body?: unknown): Promise<unknown>;
|
|
98
|
+
/** Send an effect whose success may answer an envelope or no body at all. */
|
|
99
|
+
effect(method: string, path: string, body?: unknown): Promise<void>;
|
|
100
|
+
/** One page of a JSON listing, its page evidence admitted. */
|
|
101
|
+
page(path: string): Promise<CloudflarePage>;
|
|
102
|
+
/**
|
|
103
|
+
* Every entry of a cursor-paged listing (`?cursor=`), across all of its pages. `path` may carry a
|
|
104
|
+
* query already. A cursor that repeats fails: the listing would never end.
|
|
105
|
+
*/
|
|
106
|
+
cursorList(path: string): Promise<readonly unknown[]>;
|
|
107
|
+
/**
|
|
108
|
+
* Every entry of a numbered listing (`?per_page=&page=`), across all of its pages, stopping at
|
|
109
|
+
* the first page `found` matches (when given). A page that is not the one asked, or a total that
|
|
110
|
+
* changes between pages, fails.
|
|
111
|
+
*/
|
|
112
|
+
pagedList(path: string, found?: (entry: unknown) => boolean, perPage?: number): Promise<readonly unknown[]>;
|
|
113
|
+
/**
|
|
114
|
+
* Send a multipart form (a script upload, an asset bucket) and return the envelope's `result`.
|
|
115
|
+
* `bearer` replaces the API token (an asset upload session's token).
|
|
116
|
+
*/
|
|
117
|
+
form(method: string, path: string, body: FormData, options?: {
|
|
118
|
+
readonly bearer?: string;
|
|
119
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
120
|
+
}): Promise<unknown>;
|
|
121
|
+
/**
|
|
122
|
+
* A raw text request, for KV values: the response body as text, or undefined when a GET or a
|
|
123
|
+
* DELETE answers 404. Bodies are bounded to 1 MiB.
|
|
124
|
+
*/
|
|
125
|
+
text(method: string, path: string, body?: {
|
|
126
|
+
readonly text: string;
|
|
127
|
+
readonly contentType: string;
|
|
128
|
+
}): Promise<string | undefined>;
|
|
129
|
+
/**
|
|
130
|
+
* A raw GET of bytes, for a KV value whose bytes are its meaning (a deployment record): the
|
|
131
|
+
* response body, or undefined when it answers 404. Bodies are bounded to 1 MiB.
|
|
132
|
+
*/
|
|
133
|
+
bytes(path: string): Promise<Uint8Array | undefined>;
|
|
134
|
+
private send;
|
|
135
|
+
private request;
|
|
136
|
+
/** The request timeout, bounded by the deadline and the caller's cancellation. */
|
|
137
|
+
private requestSignal;
|
|
138
|
+
/** The wait before resending a rate-limited request, or undefined when its response stands. */
|
|
139
|
+
private rateLimitWait;
|
|
140
|
+
private read;
|
|
141
|
+
private unavailable;
|
|
142
|
+
}
|
|
143
|
+
/** Whether a failure is Cloudflare saying the resource does not exist. */
|
|
144
|
+
export declare function isNotFound(cause: unknown): boolean;
|