@astrale-os/adapter-cloudflare 0.5.0-beta.22 → 0.5.0-beta.221
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 +246 -0
- package/dist/adapter.d.ts +9 -15
- package/dist/adapter.js +43 -129
- package/dist/build/bundle/admit.d.ts +3 -0
- package/dist/build/bundle/admit.js +47 -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 -1
- package/dist/build/closure.js +7 -1
- package/dist/build/codegen/execution.d.ts +3 -13
- package/dist/build/codegen/execution.js +2 -44
- package/dist/build/codegen/wrangler.d.ts +28 -10
- package/dist/build/codegen/wrangler.js +71 -21
- 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 -40
- 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 -6
- package/dist/build/index.js +10 -6
- package/dist/build/material.d.ts +10 -0
- package/dist/build/material.js +25 -0
- 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 +129 -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/hosting/activation.d.ts +46 -0
- package/dist/hosting/activation.js +127 -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 +49 -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 +68 -0
- package/dist/hosting/retirement.js +99 -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 +1 -1
- package/dist/identity/binding.js +1 -1
- package/dist/identity/material.d.ts +1 -2
- package/dist/identity/material.js +5 -18
- package/dist/index.d.ts +14 -12
- package/dist/index.js +13 -11
- 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 +386 -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 +60 -76
- 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 -2
- package/dist/release/index.js +2 -2
- package/dist/worker/binary.d.ts +21 -0
- package/dist/worker/binary.js +31 -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 +87 -109
- 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/index.d.ts +4 -1
- package/dist/worker/index.js +4 -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 +163 -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 +113 -0
- package/dist/wrangler/type-generation.d.ts +6 -0
- package/dist/wrangler/type-generation.js +19 -0
- package/package.json +36 -14
- package/template/astrale.config.ts +42 -36
- package/dist/build/artifact.d.ts +0 -8
- package/dist/build/artifact.js +0 -179
- package/dist/build/codegen/gateway.d.ts +0 -1
- package/dist/build/codegen/gateway.js +0 -7
- package/dist/build/frontend-v1.d.ts +0 -16
- package/dist/build/frontend-v1.js +0 -133
- package/dist/build/parse-output.d.ts +0 -26
- package/dist/build/parse-output.js +0 -57
- package/dist/build/wrangler-cli.d.ts +0 -105
- package/dist/build/wrangler-cli.js +0 -448
- package/dist/deploy/deploy.d.ts +0 -17
- package/dist/deploy/deploy.js +0 -207
- package/dist/deploy/index.d.ts +0 -1
- package/dist/deploy/index.js +0 -1
- package/dist/identity/secret.d.ts +0 -3
- package/dist/identity/secret.js +0 -7
- package/dist/identity/source.d.ts +0 -26
- package/dist/identity/source.js +0 -98
- package/dist/release/namespace.d.ts +0 -2
- package/dist/release/namespace.js +0 -6
- package/dist/release/target.d.ts +0 -2
- package/dist/release/target.js +0 -9
- package/dist/worker/gateway.d.ts +0 -15
- package/dist/worker/gateway.js +0 -26
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { base64url } from './bytes.js';
|
|
2
|
+
/**
|
|
3
|
+
* Generate the Ed25519 key of one new deployment with WebCrypto, so it runs in Node and in a
|
|
4
|
+
* Worker. Every deployment is its own issuer with a key of its own.
|
|
5
|
+
*/
|
|
6
|
+
export async function createSigningIdentity() {
|
|
7
|
+
const pair = (await crypto.subtle.generateKey({ name: 'Ed25519' }, true, [
|
|
8
|
+
'sign',
|
|
9
|
+
'verify',
|
|
10
|
+
]));
|
|
11
|
+
const exported = (await crypto.subtle.exportKey('jwk', pair.privateKey));
|
|
12
|
+
if (exported.kty !== 'OKP' ||
|
|
13
|
+
exported.crv !== 'Ed25519' ||
|
|
14
|
+
typeof exported.x !== 'string' ||
|
|
15
|
+
typeof exported.d !== 'string') {
|
|
16
|
+
throw new TypeError('The generated deployment key is not an Ed25519 key.');
|
|
17
|
+
}
|
|
18
|
+
const keyId = await thumbprint(exported.crv, exported.kty, exported.x);
|
|
19
|
+
return Object.freeze({
|
|
20
|
+
keyId,
|
|
21
|
+
secret: JSON.stringify({
|
|
22
|
+
kty: exported.kty,
|
|
23
|
+
crv: exported.crv,
|
|
24
|
+
x: exported.x,
|
|
25
|
+
d: exported.d,
|
|
26
|
+
alg: 'EdDSA',
|
|
27
|
+
kid: keyId,
|
|
28
|
+
}),
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/** RFC 7638: SHA-256 of the required public members in lexicographic order, base64url. */
|
|
32
|
+
async function thumbprint(crv, kty, x) {
|
|
33
|
+
const members = JSON.stringify({ crv, kty, x });
|
|
34
|
+
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(members));
|
|
35
|
+
return base64url(new Uint8Array(digest));
|
|
36
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@astrale-os/adapter-cloudflare/hosting` — host immutable Astrale deployments on your own
|
|
3
|
+
* Cloudflare account with Workers for Platforms and a domain of your own, with no Astrale
|
|
4
|
+
* infrastructure:
|
|
5
|
+
*
|
|
6
|
+
* - provision once: a dispatch namespace, its routing KV, the dispatcher Worker and the wildcard
|
|
7
|
+
* ingress of the routing domain (`planHosting`, `provisionHosting`; `astrale-domain cloudflare init`);
|
|
8
|
+
* - deploy: one create-only script per deployment label, activated by its record in the routing
|
|
9
|
+
* KV, opaque bytes the dispatcher serves verbatim (`uploadDeploymentScript`,
|
|
10
|
+
* `activateDeployment`; `cloudflare({ namespace })` and Admin's Services do it);
|
|
11
|
+
* - operate: point a name at a deployment, schedule crons, retire in two phases, list a line.
|
|
12
|
+
*
|
|
13
|
+
* Everything but provisioning runs in Node and inside a Worker (`fetch` and WebCrypto only).
|
|
14
|
+
* Provisioning reads the dispatcher modules shipped with this package, so it runs in Node.
|
|
15
|
+
*/
|
|
16
|
+
export { CLOUDFLARE_API_BASE, CloudflareApiError, CloudflareClient, DEFAULT_RATE_LIMIT, isNotFound, OPERATOR_RATE_LIMIT, type CloudflareClientOptions, type CloudflareFailureKind, type CloudflarePage, type RateLimitPolicy, } from './client.js';
|
|
17
|
+
export { HostingRefusal } from './refusal.js';
|
|
18
|
+
export { callsDataset, isWorkerName, routingKvTitle } from './names.js';
|
|
19
|
+
export { CRON_PREFIX, cronKey, CURRENT_PREFIX, currentKey, decodeCurrent, decodeSchedules, encodeSchedules, findKvNamespace, isLabelOf, RECORD_PREFIX, recordKey, RETIREMENT_PREFIX, retirementKey, RoutingKv, WITHDRAWN, type RoutingKvLocator, } from './kv.js';
|
|
20
|
+
export { ARTIFACT_TAG_PREFIX, artifactOf, ASSETS_BINDING, BUILD_DIGEST_BINDING, decodeScript, deleteScript, deploymentBindings, deploymentTags, isExistingScript, KEY_TAG_PREFIX, LINE_TAG_PREFIX, observeScript, putScriptSecret, ROUTER_BINDING, scriptPath, scriptSecretNames, scriptSettings, SETTINGS_BINDING, settingsBind, SIGNING_IDENTITY_BINDING, uploadDeploymentScript, VERSION_METADATA_BINDING, versionUuid, WORKER_URL_BINDING, type DeploymentBindingsInput, type NamespaceScript, type ScriptBinding, type ScriptModule, type ScriptSettings, type ScriptUpload, } from './script.js';
|
|
21
|
+
export { uploadAssets, type ScriptAsset } from './assets.js';
|
|
22
|
+
export { createSigningIdentity, type DeploymentSigningIdentity } from './identity.js';
|
|
23
|
+
export { activateDeployment, promoteDeployment, readCurrent, readRecord, readRetirement, readSchedules, replaceSchedules, sameRecord, } from './activation.js';
|
|
24
|
+
export { completeRetirement, markRetired, RETIREMENT_PROBE_PATH, RETIREMENT_PROBE_RETRY_MS, RETIREMENT_PROPAGATION_MS, retirementVisible, type RetirementCompletion, } from './retirement.js';
|
|
25
|
+
export { dispatchFetcher, publicFetcher, VERSION_HEADER, versionFetch, waitForReadiness, type ReadinessFetcher, } from './readiness.js';
|
|
26
|
+
export { listLineDeployments, type LineDeployment } from './listing.js';
|
|
27
|
+
export { createDispatcher } from './dispatcher/dispatcher.mjs';
|
|
28
|
+
export type { Dispatcher } from './dispatcher/dispatcher.mjs';
|
|
29
|
+
export type { DispatcherEnvironment, DispatchFetcher, DispatchNamespaceBinding, RoutingKvBinding, } from './dispatcher/env.mjs';
|
|
30
|
+
export { DISPATCHER_MAIN_MODULE, DISPATCHER_MODULE_NAMES, dispatcherModules, type DispatcherModule, } from './dispatcher/modules.js';
|
|
31
|
+
export { DISPATCHER_COMPATIBILITY_DATE, DISPATCHER_COMPATIBILITY_FLAGS, dispatcherBindings, type WorkerBinding, } from './provision/dispatcher.js';
|
|
32
|
+
export { dispatcherIngressReady } from './provision/ingress.js';
|
|
33
|
+
export { findZone, routeReaches } from './provision/routing.js';
|
|
34
|
+
export { applyHosting, openHostingKv, planHosting, provisionHosting, type HostingConfig, type HostingPlan, type HostingProvisioning, } from './provision/plan.js';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@astrale-os/adapter-cloudflare/hosting` — host immutable Astrale deployments on your own
|
|
3
|
+
* Cloudflare account with Workers for Platforms and a domain of your own, with no Astrale
|
|
4
|
+
* infrastructure:
|
|
5
|
+
*
|
|
6
|
+
* - provision once: a dispatch namespace, its routing KV, the dispatcher Worker and the wildcard
|
|
7
|
+
* ingress of the routing domain (`planHosting`, `provisionHosting`; `astrale-domain cloudflare init`);
|
|
8
|
+
* - deploy: one create-only script per deployment label, activated by its record in the routing
|
|
9
|
+
* KV, opaque bytes the dispatcher serves verbatim (`uploadDeploymentScript`,
|
|
10
|
+
* `activateDeployment`; `cloudflare({ namespace })` and Admin's Services do it);
|
|
11
|
+
* - operate: point a name at a deployment, schedule crons, retire in two phases, list a line.
|
|
12
|
+
*
|
|
13
|
+
* Everything but provisioning runs in Node and inside a Worker (`fetch` and WebCrypto only).
|
|
14
|
+
* Provisioning reads the dispatcher modules shipped with this package, so it runs in Node.
|
|
15
|
+
*/
|
|
16
|
+
export { CLOUDFLARE_API_BASE, CloudflareApiError, CloudflareClient, DEFAULT_RATE_LIMIT, isNotFound, OPERATOR_RATE_LIMIT, } from './client.js';
|
|
17
|
+
export { HostingRefusal } from './refusal.js';
|
|
18
|
+
export { callsDataset, isWorkerName, routingKvTitle } from './names.js';
|
|
19
|
+
export { CRON_PREFIX, cronKey, CURRENT_PREFIX, currentKey, decodeCurrent, decodeSchedules, encodeSchedules, findKvNamespace, isLabelOf, RECORD_PREFIX, recordKey, RETIREMENT_PREFIX, retirementKey, RoutingKv, WITHDRAWN, } from './kv.js';
|
|
20
|
+
export { ARTIFACT_TAG_PREFIX, artifactOf, ASSETS_BINDING, BUILD_DIGEST_BINDING, decodeScript, deleteScript, deploymentBindings, deploymentTags, isExistingScript, KEY_TAG_PREFIX, LINE_TAG_PREFIX, observeScript, putScriptSecret, ROUTER_BINDING, scriptPath, scriptSecretNames, scriptSettings, SETTINGS_BINDING, settingsBind, SIGNING_IDENTITY_BINDING, uploadDeploymentScript, VERSION_METADATA_BINDING, versionUuid, WORKER_URL_BINDING, } from './script.js';
|
|
21
|
+
export { uploadAssets } from './assets.js';
|
|
22
|
+
export { createSigningIdentity } from './identity.js';
|
|
23
|
+
export { activateDeployment, promoteDeployment, readCurrent, readRecord, readRetirement, readSchedules, replaceSchedules, sameRecord, } from './activation.js';
|
|
24
|
+
export { completeRetirement, markRetired, RETIREMENT_PROBE_PATH, RETIREMENT_PROBE_RETRY_MS, RETIREMENT_PROPAGATION_MS, retirementVisible, } from './retirement.js';
|
|
25
|
+
export { dispatchFetcher, publicFetcher, VERSION_HEADER, versionFetch, waitForReadiness, } from './readiness.js';
|
|
26
|
+
export { listLineDeployments } from './listing.js';
|
|
27
|
+
export { createDispatcher } from './dispatcher/dispatcher.mjs';
|
|
28
|
+
export { DISPATCHER_MAIN_MODULE, DISPATCHER_MODULE_NAMES, dispatcherModules, } from './dispatcher/modules.js';
|
|
29
|
+
export { DISPATCHER_COMPATIBILITY_DATE, DISPATCHER_COMPATIBILITY_FLAGS, dispatcherBindings, } from './provision/dispatcher.js';
|
|
30
|
+
export { dispatcherIngressReady } from './provision/ingress.js';
|
|
31
|
+
export { findZone, routeReaches } from './provision/routing.js';
|
|
32
|
+
export { applyHosting, openHostingKv, planHosting, provisionHosting, } from './provision/plan.js';
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { CloudflareClient } from './client.js';
|
|
2
|
+
/**
|
|
3
|
+
* The routing KV contract every dispatcher reads (`SCHEDULES`) and every writer of the kit keeps:
|
|
4
|
+
*
|
|
5
|
+
* | key | value | metadata |
|
|
6
|
+
* |---|---|---|
|
|
7
|
+
* | `record:<label>` | the deployment's record, opaque bytes; `-` when it publishes none; never rewritten | `{"contentType":<its media type>}`, `{"contentType":null}` without a record |
|
|
8
|
+
* | `retirement:<label>` | `withdrawn`; any value retires the label at the dispatcher | none |
|
|
9
|
+
* | `current:<name>` | the label a name serves now, one of `<name>-…` | none |
|
|
10
|
+
* | `cron:<name>` | JSON array of distinct canonical five-field UTC crons; deleted when empty | none |
|
|
11
|
+
*
|
|
12
|
+
* `record:<label>` activates the label and outlives its retirement. Its metadata is mandatory: a
|
|
13
|
+
* value without it is no record the kit writes: it activates nothing at the dispatcher, and the
|
|
14
|
+
* kit refuses it (`record-invalid`). Workers KV has no compare-and-set: every write is read
|
|
15
|
+
* back before it counts.
|
|
16
|
+
*/
|
|
17
|
+
export declare const RECORD_PREFIX = "record:";
|
|
18
|
+
export declare const RETIREMENT_PREFIX = "retirement:";
|
|
19
|
+
export declare const CURRENT_PREFIX = "current:";
|
|
20
|
+
export declare const CRON_PREFIX = "cron:";
|
|
21
|
+
/** The one tombstone value the kit writes: a withdrawal by the deployment's owner. */
|
|
22
|
+
export declare const WITHDRAWN = "withdrawn";
|
|
23
|
+
/** The key whose presence activates a label at the dispatcher. */
|
|
24
|
+
export declare function recordKey(label: string): string;
|
|
25
|
+
/** The key whose presence retires a label at the dispatcher, whatever the record says. */
|
|
26
|
+
export declare function retirementKey(label: string): string;
|
|
27
|
+
/** The key that points a name (a deployment line) at the deployment its host serves now. */
|
|
28
|
+
export declare function currentKey(name: string): string;
|
|
29
|
+
/** The key of a name's UTC crons, which the dispatcher fires on its current deployment. */
|
|
30
|
+
export declare function cronKey(name: string): string;
|
|
31
|
+
/** Admit the label `current:<name>` holds: a deployment label of that name's line. */
|
|
32
|
+
export declare function decodeCurrent(text: string, name: string): string;
|
|
33
|
+
/** Whether `label` is a deployment label whose line is `name`, as the dispatcher admits it. */
|
|
34
|
+
export declare function isLabelOf(label: string, name: string): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* The text `cron:<name>` holds for these crons: their canonical form as JSON, or undefined when
|
|
37
|
+
* there are none and the key is deleted. Crons that are not distinct five-field UTC expressions are
|
|
38
|
+
* refused before anything is written.
|
|
39
|
+
*/
|
|
40
|
+
export declare function encodeSchedules(crons: readonly string[]): {
|
|
41
|
+
readonly crons: readonly string[];
|
|
42
|
+
readonly text: string | undefined;
|
|
43
|
+
};
|
|
44
|
+
/** Admit the text `cron:<name>` holds as canonical crons. */
|
|
45
|
+
export declare function decodeSchedules(text: string, name: string): readonly string[];
|
|
46
|
+
/** Where a routing KV is: by the id a caller keeps, or by its title (`routingKvTitle`). */
|
|
47
|
+
export type RoutingKvLocator = {
|
|
48
|
+
readonly id: string;
|
|
49
|
+
} | {
|
|
50
|
+
readonly title: string;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* One routing KV, through the account API. Reads answer the stored text (undefined when absent);
|
|
54
|
+
* every write and delete is read back before it counts, and fails `unavailable` otherwise.
|
|
55
|
+
*/
|
|
56
|
+
export declare class RoutingKv {
|
|
57
|
+
private readonly client;
|
|
58
|
+
readonly id: string;
|
|
59
|
+
private constructor();
|
|
60
|
+
/**
|
|
61
|
+
* Open the routing KV by id, or find it by title; a title the account holds no KV under is a
|
|
62
|
+
* `not-provisioned` refusal: only provisioning creates it.
|
|
63
|
+
*/
|
|
64
|
+
static open(client: CloudflareClient, locator: RoutingKvLocator): Promise<RoutingKv>;
|
|
65
|
+
/** The text stored at `key`, or undefined. */
|
|
66
|
+
get(key: string): Promise<string | undefined>;
|
|
67
|
+
/** Write `text` at `key`, then read it back. */
|
|
68
|
+
put(key: string, text: string, contentType: string): Promise<void>;
|
|
69
|
+
/**
|
|
70
|
+
* The bytes stored at `key` and their metadata (`null` when they were written without), or
|
|
71
|
+
* undefined when the key is absent.
|
|
72
|
+
*/
|
|
73
|
+
getWithMetadata(key: string): Promise<{
|
|
74
|
+
readonly bytes: Uint8Array;
|
|
75
|
+
readonly metadata: unknown;
|
|
76
|
+
} | undefined>;
|
|
77
|
+
/** Write `bytes` at `key` with its JSON `metadata`, then read both back. */
|
|
78
|
+
putWithMetadata(key: string, bytes: Uint8Array, metadata: Readonly<Record<string, unknown>>): Promise<void>;
|
|
79
|
+
/** Delete `key`, then read back its absence. */
|
|
80
|
+
delete(key: string): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Every key name that starts with `prefix`, in Cloudflare's (lexicographic) order, following
|
|
83
|
+
* the listing's cursor to its end.
|
|
84
|
+
*/
|
|
85
|
+
keys(prefix: string): Promise<readonly string[]>;
|
|
86
|
+
private value;
|
|
87
|
+
private path;
|
|
88
|
+
}
|
|
89
|
+
/** The id of the account's KV namespace with exactly this title, or undefined. */
|
|
90
|
+
export declare function findKvNamespace(client: CloudflareClient, title: string): Promise<string | undefined>;
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { parseDeploymentLabel } from '@astrale-os/sdk/deployment/address';
|
|
2
|
+
import { CloudflareApiError, isNotFound } from './client.js';
|
|
3
|
+
import { canonicalUtcCrons } from './dispatcher/cron.mjs';
|
|
4
|
+
import { HostingRefusal } from './refusal.js';
|
|
5
|
+
/**
|
|
6
|
+
* The routing KV contract every dispatcher reads (`SCHEDULES`) and every writer of the kit keeps:
|
|
7
|
+
*
|
|
8
|
+
* | key | value | metadata |
|
|
9
|
+
* |---|---|---|
|
|
10
|
+
* | `record:<label>` | the deployment's record, opaque bytes; `-` when it publishes none; never rewritten | `{"contentType":<its media type>}`, `{"contentType":null}` without a record |
|
|
11
|
+
* | `retirement:<label>` | `withdrawn`; any value retires the label at the dispatcher | none |
|
|
12
|
+
* | `current:<name>` | the label a name serves now, one of `<name>-…` | none |
|
|
13
|
+
* | `cron:<name>` | JSON array of distinct canonical five-field UTC crons; deleted when empty | none |
|
|
14
|
+
*
|
|
15
|
+
* `record:<label>` activates the label and outlives its retirement. Its metadata is mandatory: a
|
|
16
|
+
* value without it is no record the kit writes: it activates nothing at the dispatcher, and the
|
|
17
|
+
* kit refuses it (`record-invalid`). Workers KV has no compare-and-set: every write is read
|
|
18
|
+
* back before it counts.
|
|
19
|
+
*/
|
|
20
|
+
export const RECORD_PREFIX = 'record:';
|
|
21
|
+
export const RETIREMENT_PREFIX = 'retirement:';
|
|
22
|
+
export const CURRENT_PREFIX = 'current:';
|
|
23
|
+
export const CRON_PREFIX = 'cron:';
|
|
24
|
+
/** The one tombstone value the kit writes: a withdrawal by the deployment's owner. */
|
|
25
|
+
export const WITHDRAWN = 'withdrawn';
|
|
26
|
+
/** The key whose presence activates a label at the dispatcher. */
|
|
27
|
+
export function recordKey(label) {
|
|
28
|
+
return `${RECORD_PREFIX}${label}`;
|
|
29
|
+
}
|
|
30
|
+
/** The key whose presence retires a label at the dispatcher, whatever the record says. */
|
|
31
|
+
export function retirementKey(label) {
|
|
32
|
+
return `${RETIREMENT_PREFIX}${label}`;
|
|
33
|
+
}
|
|
34
|
+
/** The key that points a name (a deployment line) at the deployment its host serves now. */
|
|
35
|
+
export function currentKey(name) {
|
|
36
|
+
return `${CURRENT_PREFIX}${name}`;
|
|
37
|
+
}
|
|
38
|
+
/** The key of a name's UTC crons, which the dispatcher fires on its current deployment. */
|
|
39
|
+
export function cronKey(name) {
|
|
40
|
+
return `${CRON_PREFIX}${name}`;
|
|
41
|
+
}
|
|
42
|
+
/** Admit the label `current:<name>` holds: a deployment label of that name's line. */
|
|
43
|
+
export function decodeCurrent(text, name) {
|
|
44
|
+
if (!isLabelOf(text, name)) {
|
|
45
|
+
throw new HostingRefusal('route-invalid', `current:${name} names ${text}, which is not a deployment of ${name}.`);
|
|
46
|
+
}
|
|
47
|
+
return text;
|
|
48
|
+
}
|
|
49
|
+
/** Whether `label` is a deployment label whose line is `name`, as the dispatcher admits it. */
|
|
50
|
+
export function isLabelOf(label, name) {
|
|
51
|
+
if (!label.startsWith(`${name}-`))
|
|
52
|
+
return false;
|
|
53
|
+
try {
|
|
54
|
+
return parseDeploymentLabel(label).line === name;
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The text `cron:<name>` holds for these crons: their canonical form as JSON, or undefined when
|
|
62
|
+
* there are none and the key is deleted. Crons that are not distinct five-field UTC expressions are
|
|
63
|
+
* refused before anything is written.
|
|
64
|
+
*/
|
|
65
|
+
export function encodeSchedules(crons) {
|
|
66
|
+
const canonical = canonicalUtcCrons(crons);
|
|
67
|
+
if (canonical === undefined) {
|
|
68
|
+
throw new HostingRefusal('schedules-invalid', 'Schedules must be distinct five-field UTC cron expressions.');
|
|
69
|
+
}
|
|
70
|
+
return Object.freeze({
|
|
71
|
+
crons: canonical,
|
|
72
|
+
text: canonical.length === 0 ? undefined : JSON.stringify(canonical),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/** Admit the text `cron:<name>` holds as canonical crons. */
|
|
76
|
+
export function decodeSchedules(text, name) {
|
|
77
|
+
let value;
|
|
78
|
+
try {
|
|
79
|
+
value = JSON.parse(text);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
value = undefined;
|
|
83
|
+
}
|
|
84
|
+
const crons = canonicalUtcCrons(value);
|
|
85
|
+
if (crons === undefined) {
|
|
86
|
+
throw new HostingRefusal('schedules-invalid', `cron:${name} holds no canonical UTC crons.`);
|
|
87
|
+
}
|
|
88
|
+
return crons;
|
|
89
|
+
}
|
|
90
|
+
/** The most keys one page of a KV key listing returns, as Cloudflare caps it. */
|
|
91
|
+
const KEYS_PAGE = 1_000;
|
|
92
|
+
/**
|
|
93
|
+
* One routing KV, through the account API. Reads answer the stored text (undefined when absent);
|
|
94
|
+
* every write and delete is read back before it counts, and fails `unavailable` otherwise.
|
|
95
|
+
*/
|
|
96
|
+
export class RoutingKv {
|
|
97
|
+
client;
|
|
98
|
+
id;
|
|
99
|
+
constructor(client, id) {
|
|
100
|
+
this.client = client;
|
|
101
|
+
this.id = id;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Open the routing KV by id, or find it by title; a title the account holds no KV under is a
|
|
105
|
+
* `not-provisioned` refusal: only provisioning creates it.
|
|
106
|
+
*/
|
|
107
|
+
static async open(client, locator) {
|
|
108
|
+
if ('id' in locator) {
|
|
109
|
+
if (locator.id.length === 0)
|
|
110
|
+
throw new TypeError('Routing KV id is required.');
|
|
111
|
+
return new RoutingKv(client, locator.id);
|
|
112
|
+
}
|
|
113
|
+
const id = await findKvNamespace(client, locator.title);
|
|
114
|
+
if (id === undefined) {
|
|
115
|
+
throw new HostingRefusal('not-provisioned', `The account has no routing KV titled ${locator.title}; the namespace is not provisioned ` +
|
|
116
|
+
'(`astrale-domain cloudflare init <environment>` provisions it).');
|
|
117
|
+
}
|
|
118
|
+
return new RoutingKv(client, id);
|
|
119
|
+
}
|
|
120
|
+
/** The text stored at `key`, or undefined. */
|
|
121
|
+
async get(key) {
|
|
122
|
+
return this.client.text('GET', this.value(key));
|
|
123
|
+
}
|
|
124
|
+
/** Write `text` at `key`, then read it back. */
|
|
125
|
+
async put(key, text, contentType) {
|
|
126
|
+
await this.client.text('PUT', this.value(key), { text, contentType });
|
|
127
|
+
if ((await this.get(key)) !== text)
|
|
128
|
+
notKept(key);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* The bytes stored at `key` and their metadata (`null` when they were written without), or
|
|
132
|
+
* undefined when the key is absent.
|
|
133
|
+
*/
|
|
134
|
+
async getWithMetadata(key) {
|
|
135
|
+
const bytes = await this.client.bytes(this.value(key));
|
|
136
|
+
if (bytes === undefined)
|
|
137
|
+
return undefined;
|
|
138
|
+
let metadata;
|
|
139
|
+
try {
|
|
140
|
+
metadata = await this.client.json('GET', this.path('metadata', key));
|
|
141
|
+
}
|
|
142
|
+
catch (cause) {
|
|
143
|
+
if (isNotFound(cause))
|
|
144
|
+
return undefined;
|
|
145
|
+
throw cause;
|
|
146
|
+
}
|
|
147
|
+
return Object.freeze({ bytes, metadata: metadata ?? null });
|
|
148
|
+
}
|
|
149
|
+
/** Write `bytes` at `key` with its JSON `metadata`, then read both back. */
|
|
150
|
+
async putWithMetadata(key, bytes, metadata) {
|
|
151
|
+
const text = JSON.stringify(metadata);
|
|
152
|
+
const form = new FormData();
|
|
153
|
+
form.append('value', new Blob([bytes]));
|
|
154
|
+
form.append('metadata', text);
|
|
155
|
+
await this.client.form('PUT', this.value(key), form);
|
|
156
|
+
const stored = await this.getWithMetadata(key);
|
|
157
|
+
if (stored === undefined ||
|
|
158
|
+
stored.bytes.byteLength !== bytes.byteLength ||
|
|
159
|
+
stored.bytes.some((byte, index) => byte !== bytes[index]) ||
|
|
160
|
+
JSON.stringify(stored.metadata) !== text) {
|
|
161
|
+
notKept(key);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/** Delete `key`, then read back its absence. */
|
|
165
|
+
async delete(key) {
|
|
166
|
+
await this.client.text('DELETE', this.value(key));
|
|
167
|
+
if ((await this.get(key)) !== undefined)
|
|
168
|
+
notKept(key);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Every key name that starts with `prefix`, in Cloudflare's (lexicographic) order, following
|
|
172
|
+
* the listing's cursor to its end.
|
|
173
|
+
*/
|
|
174
|
+
async keys(prefix) {
|
|
175
|
+
const query = new URLSearchParams({ prefix, limit: String(KEYS_PAGE) });
|
|
176
|
+
const entries = await this.client.cursorList(this.client.account(`/storage/kv/namespaces/${encodeURIComponent(this.id)}/keys?${query}`));
|
|
177
|
+
return Object.freeze(entries.map((entry) => {
|
|
178
|
+
const name = entry?.name;
|
|
179
|
+
if (typeof name !== 'string' || !name.startsWith(prefix)) {
|
|
180
|
+
throw new CloudflareApiError('upstream', 'Cloudflare returned an invalid KV key listing.');
|
|
181
|
+
}
|
|
182
|
+
return name;
|
|
183
|
+
}));
|
|
184
|
+
}
|
|
185
|
+
value(key) {
|
|
186
|
+
return this.path('values', key);
|
|
187
|
+
}
|
|
188
|
+
path(kind, key) {
|
|
189
|
+
return this.client.account(`/storage/kv/namespaces/${encodeURIComponent(this.id)}/${kind}/${encodeURIComponent(key)}`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/** The id of the account's KV namespace with exactly this title, or undefined. */
|
|
193
|
+
export async function findKvNamespace(client, title) {
|
|
194
|
+
const matches = (entry) => entry?.title === title;
|
|
195
|
+
const listed = await client.pagedList(client.account('/storage/kv/namespaces'), matches);
|
|
196
|
+
const found = listed.find(matches);
|
|
197
|
+
if (found === undefined)
|
|
198
|
+
return undefined;
|
|
199
|
+
if (typeof found.id !== 'string' || found.id.length === 0) {
|
|
200
|
+
throw new CloudflareApiError('upstream', 'Cloudflare returned an invalid KV namespace id.');
|
|
201
|
+
}
|
|
202
|
+
return found.id;
|
|
203
|
+
}
|
|
204
|
+
function notKept(key) {
|
|
205
|
+
const kind = key.slice(0, key.indexOf(':') + 1) || 'a key';
|
|
206
|
+
throw new CloudflareApiError('unavailable', `Cloudflare did not keep the write at ${kind}.`);
|
|
207
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { WorkerRecord } from '@astrale-os/sdk/deployment/worker';
|
|
2
|
+
import type { CloudflareClient } from './client.js';
|
|
3
|
+
import type { RoutingKv } from './kv.js';
|
|
4
|
+
import type { NamespaceScript } from './script.js';
|
|
5
|
+
/**
|
|
6
|
+
* One deployment of a line as the routing KV and the namespace hold it:
|
|
7
|
+
*
|
|
8
|
+
* - `record`: the record `record:<label>` publishes (its media type and bytes, never read), `null`
|
|
9
|
+
* when the deployment publishes none; absent when the label is not activated;
|
|
10
|
+
* - `retired`: whether `retirement:<label>` withdraws it;
|
|
11
|
+
* - `script`: the script under the label, absent once deleted (a completed retirement).
|
|
12
|
+
*
|
|
13
|
+
* The dispatcher serves it (`active`) exactly when it is activated, has no tombstone and a script.
|
|
14
|
+
*/
|
|
15
|
+
export interface LineDeployment {
|
|
16
|
+
readonly label: string;
|
|
17
|
+
readonly record?: WorkerRecord | null;
|
|
18
|
+
readonly retired: boolean;
|
|
19
|
+
readonly script?: NamespaceScript;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The deployments of one line, from the keys under `record:<line>-` and `retirement:<line>-` of
|
|
23
|
+
* the routing KV, each with its script, in label order. A key there that names no deployment
|
|
24
|
+
* label is refused (`record-invalid`, `retirement-invalid`); one whose label is on another line,
|
|
25
|
+
* whose readable part happens to extend this one, is not this line's. A label that only has an
|
|
26
|
+
* uploaded script (never activated nor retired) is not listed: nothing in the KV names it. A stored
|
|
27
|
+
* record the kit refuses is refused (`record-invalid`). Nothing is written.
|
|
28
|
+
*/
|
|
29
|
+
export declare function listLineDeployments(input: {
|
|
30
|
+
readonly client: CloudflareClient;
|
|
31
|
+
readonly kv: RoutingKv;
|
|
32
|
+
readonly namespace: string;
|
|
33
|
+
readonly line: string;
|
|
34
|
+
}): Promise<readonly LineDeployment[]>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { parseDeploymentLabel } from '@astrale-os/sdk/deployment/address';
|
|
2
|
+
import { readRecord, readRetirement } from './activation.js';
|
|
3
|
+
import { CLOUDFLARE_FAN_OUT, mapBounded } from './bounded.js';
|
|
4
|
+
import { RECORD_PREFIX, RETIREMENT_PREFIX } from './kv.js';
|
|
5
|
+
import { HostingRefusal } from './refusal.js';
|
|
6
|
+
import { observeScript } from './script.js';
|
|
7
|
+
/**
|
|
8
|
+
* The deployments of one line, from the keys under `record:<line>-` and `retirement:<line>-` of
|
|
9
|
+
* the routing KV, each with its script, in label order. A key there that names no deployment
|
|
10
|
+
* label is refused (`record-invalid`, `retirement-invalid`); one whose label is on another line,
|
|
11
|
+
* whose readable part happens to extend this one, is not this line's. A label that only has an
|
|
12
|
+
* uploaded script (never activated nor retired) is not listed: nothing in the KV names it. A stored
|
|
13
|
+
* record the kit refuses is refused (`record-invalid`). Nothing is written.
|
|
14
|
+
*/
|
|
15
|
+
export async function listLineDeployments(input) {
|
|
16
|
+
const { client, kv, namespace, line } = input;
|
|
17
|
+
// Tombstones are listed before records: a label retired during the listing is seen retired.
|
|
18
|
+
const retired = new Set(await labelsOf(kv, RETIREMENT_PREFIX, line));
|
|
19
|
+
const activated = new Set(await labelsOf(kv, RECORD_PREFIX, line));
|
|
20
|
+
const labels = [...new Set([...activated, ...retired])].sort();
|
|
21
|
+
return Object.freeze(await mapBounded(labels, CLOUDFLARE_FAN_OUT, async (label) => {
|
|
22
|
+
const stored = activated.has(label) ? await readRecord(kv, label) : undefined;
|
|
23
|
+
if (retired.has(label))
|
|
24
|
+
await readRetirement(kv, label);
|
|
25
|
+
const script = await observeScript(client, namespace, label);
|
|
26
|
+
return Object.freeze({
|
|
27
|
+
label,
|
|
28
|
+
...(stored === undefined ? {} : { record: stored }),
|
|
29
|
+
retired: retired.has(label),
|
|
30
|
+
...(script === undefined ? {} : { script }),
|
|
31
|
+
});
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
async function labelsOf(kv, prefix, line) {
|
|
35
|
+
const labels = [];
|
|
36
|
+
for (const name of await kv.keys(`${prefix}${line}-`)) {
|
|
37
|
+
const label = name.slice(prefix.length);
|
|
38
|
+
let parsed;
|
|
39
|
+
try {
|
|
40
|
+
parsed = parseDeploymentLabel(label);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
throw new HostingRefusal(prefix === RECORD_PREFIX ? 'record-invalid' : 'retirement-invalid', `The routing KV holds ${name}, which names no deployment label.`);
|
|
44
|
+
}
|
|
45
|
+
if (parsed.line === line)
|
|
46
|
+
labels.push(label);
|
|
47
|
+
}
|
|
48
|
+
return labels;
|
|
49
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Whether `name` is a Worker or dispatch namespace name Cloudflare admits (lower case, ≤ 63). */
|
|
2
|
+
export declare function isWorkerName(name: string): boolean;
|
|
3
|
+
/**
|
|
4
|
+
* The title of a dispatch namespace's routing KV: `astrale-deployment-routing-<hex32>`, where
|
|
5
|
+
* `<hex32>` is the first 16 bytes of `sha256(utf8(namespace))` in lower-case hexadecimal.
|
|
6
|
+
* Provisioning creates the KV under this title and every deployer finds it by this title alone, so
|
|
7
|
+
* a deployer's configuration holds no KV id. Byte-compatible with every adapter that ever deployed
|
|
8
|
+
* into a namespace: renaming it would orphan their deployments.
|
|
9
|
+
*/
|
|
10
|
+
export declare function routingKvTitle(namespace: string): Promise<string>;
|
|
11
|
+
/**
|
|
12
|
+
* The Analytics Engine dataset of a dispatch namespace's calls: `astrale_deployment_calls_<hex32>`,
|
|
13
|
+
* with the `<hex32>` of `routingKvTitle`. The dispatcher writes one data point `{ indexes: [label] }`
|
|
14
|
+
* to it per call that runs a deployment, when it is bound as `CALLS`; nothing in the kit reads it.
|
|
15
|
+
*/
|
|
16
|
+
export declare function callsDataset(namespace: string): Promise<string>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { sha256Hex } from './bytes.js';
|
|
2
|
+
/** A dispatch namespace, KV title component or Worker name, as Cloudflare admits a script name. */
|
|
3
|
+
const WORKER_NAME = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/u;
|
|
4
|
+
/** Whether `name` is a Worker or dispatch namespace name Cloudflare admits (lower case, ≤ 63). */
|
|
5
|
+
export function isWorkerName(name) {
|
|
6
|
+
return WORKER_NAME.test(name);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The title of a dispatch namespace's routing KV: `astrale-deployment-routing-<hex32>`, where
|
|
10
|
+
* `<hex32>` is the first 16 bytes of `sha256(utf8(namespace))` in lower-case hexadecimal.
|
|
11
|
+
* Provisioning creates the KV under this title and every deployer finds it by this title alone, so
|
|
12
|
+
* a deployer's configuration holds no KV id. Byte-compatible with every adapter that ever deployed
|
|
13
|
+
* into a namespace: renaming it would orphan their deployments.
|
|
14
|
+
*/
|
|
15
|
+
export async function routingKvTitle(namespace) {
|
|
16
|
+
return `astrale-deployment-routing-${await digestPrefix(namespace)}`;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The Analytics Engine dataset of a dispatch namespace's calls: `astrale_deployment_calls_<hex32>`,
|
|
20
|
+
* with the `<hex32>` of `routingKvTitle`. The dispatcher writes one data point `{ indexes: [label] }`
|
|
21
|
+
* to it per call that runs a deployment, when it is bound as `CALLS`; nothing in the kit reads it.
|
|
22
|
+
*/
|
|
23
|
+
export async function callsDataset(namespace) {
|
|
24
|
+
return `astrale_deployment_calls_${await digestPrefix(namespace)}`;
|
|
25
|
+
}
|
|
26
|
+
async function digestPrefix(value) {
|
|
27
|
+
return (await sha256Hex(value)).slice(0, 32);
|
|
28
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { CloudflareClient } from '../client.js';
|
|
2
|
+
/** Whether the account holds the dispatch namespace called `name`. */
|
|
3
|
+
export declare function dispatchNamespaceExists(client: CloudflareClient, name: string): Promise<boolean>;
|
|
4
|
+
/** Create the dispatch namespace `name` unless it exists; a concurrent creation is adopted. */
|
|
5
|
+
export declare function ensureDispatchNamespace(client: CloudflareClient, name: string): Promise<void>;
|
|
6
|
+
/** The id of the KV namespace titled `title`, created when the account holds none. */
|
|
7
|
+
export declare function ensureKvNamespace(client: CloudflareClient, title: string): Promise<string>;
|
|
8
|
+
/** Whether the account holds the KV namespace with this id. */
|
|
9
|
+
export declare function kvNamespaceExists(client: CloudflareClient, id: string): Promise<boolean>;
|
|
10
|
+
/** The names of every script of a dispatch namespace, across every page of its listing. */
|
|
11
|
+
export declare function namespaceScriptNames(client: CloudflareClient, namespace: string): Promise<readonly string[]>;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { CloudflareApiError, isNotFound } from '../client.js';
|
|
2
|
+
import { findKvNamespace } from '../kv.js';
|
|
3
|
+
/** Whether the account holds the dispatch namespace called `name`. */
|
|
4
|
+
export async function dispatchNamespaceExists(client, name) {
|
|
5
|
+
let value;
|
|
6
|
+
try {
|
|
7
|
+
value = await client.json('GET', client.account(`/workers/dispatch/namespaces/${encodeURIComponent(name)}`));
|
|
8
|
+
}
|
|
9
|
+
catch (cause) {
|
|
10
|
+
if (isNotFound(cause))
|
|
11
|
+
return false;
|
|
12
|
+
throw cause;
|
|
13
|
+
}
|
|
14
|
+
requiredText(value, 'namespace_name', 'dispatch namespace');
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
/** Create the dispatch namespace `name` unless it exists; a concurrent creation is adopted. */
|
|
18
|
+
export async function ensureDispatchNamespace(client, name) {
|
|
19
|
+
await converge(async () => ((await dispatchNamespaceExists(client, name)) ? name : undefined), async () => {
|
|
20
|
+
const created = await client.json('POST', client.account('/workers/dispatch/namespaces'), {
|
|
21
|
+
name,
|
|
22
|
+
});
|
|
23
|
+
return requiredText(created, 'namespace_name', 'dispatch namespace creation');
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** The id of the KV namespace titled `title`, created when the account holds none. */
|
|
27
|
+
export async function ensureKvNamespace(client, title) {
|
|
28
|
+
return converge(() => findKvNamespace(client, title), async () => {
|
|
29
|
+
const created = await client.json('POST', client.account('/storage/kv/namespaces'), {
|
|
30
|
+
title,
|
|
31
|
+
});
|
|
32
|
+
return requiredText(created, 'id', 'KV namespace creation');
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/** Whether the account holds the KV namespace with this id. */
|
|
36
|
+
export async function kvNamespaceExists(client, id) {
|
|
37
|
+
try {
|
|
38
|
+
await client.json('GET', client.account(`/storage/kv/namespaces/${encodeURIComponent(id)}`));
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
catch (cause) {
|
|
42
|
+
if (isNotFound(cause))
|
|
43
|
+
return false;
|
|
44
|
+
throw cause;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** The names of every script of a dispatch namespace, across every page of its listing. */
|
|
48
|
+
export async function namespaceScriptNames(client, namespace) {
|
|
49
|
+
const entries = await client.cursorList(client.account(`/workers/dispatch/namespaces/${encodeURIComponent(namespace)}/scripts?limit=1000`));
|
|
50
|
+
return Object.freeze(entries.map((entry) => {
|
|
51
|
+
const id = entry?.id;
|
|
52
|
+
if (typeof id !== 'string' || id.length === 0)
|
|
53
|
+
invalid('namespace script listing');
|
|
54
|
+
return id;
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
function requiredText(value, key, context) {
|
|
58
|
+
const selected = value?.[key];
|
|
59
|
+
if (typeof selected !== 'string' || selected.length === 0)
|
|
60
|
+
invalid(context);
|
|
61
|
+
return selected;
|
|
62
|
+
}
|
|
63
|
+
function invalid(context) {
|
|
64
|
+
throw new CloudflareApiError('upstream', `Cloudflare returned invalid ${context} evidence.`);
|
|
65
|
+
}
|
|
66
|
+
/** Create-then-adopt: a creation that fails because another run created it meanwhile is adopted. */
|
|
67
|
+
async function converge(find, create) {
|
|
68
|
+
const existing = await find();
|
|
69
|
+
if (existing !== undefined)
|
|
70
|
+
return existing;
|
|
71
|
+
try {
|
|
72
|
+
return await create();
|
|
73
|
+
}
|
|
74
|
+
catch (cause) {
|
|
75
|
+
let adopted;
|
|
76
|
+
try {
|
|
77
|
+
adopted = await find();
|
|
78
|
+
}
|
|
79
|
+
catch (observation) {
|
|
80
|
+
throw new AggregateError([cause, observation], 'Cloudflare create and adoption lookup failed.');
|
|
81
|
+
}
|
|
82
|
+
if (adopted !== undefined)
|
|
83
|
+
return adopted;
|
|
84
|
+
throw cause;
|
|
85
|
+
}
|
|
86
|
+
}
|