@astrale-os/adapter-cloudflare 0.5.0-beta.4 → 0.5.0-beta.41
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 +11 -0
- package/dist/adapter.d.ts +17 -0
- package/dist/adapter.js +178 -0
- package/dist/build/artifact.d.ts +7 -0
- package/dist/build/artifact.js +151 -0
- package/dist/build/bundle/admit.d.ts +1 -0
- package/dist/build/bundle/admit.js +9 -0
- package/dist/build/closure.d.ts +2 -0
- package/dist/build/closure.js +22 -0
- package/dist/build/codegen/execution.d.ts +3 -0
- package/dist/build/codegen/execution.js +2 -0
- package/dist/build/codegen/gateway.d.ts +1 -0
- package/dist/build/codegen/gateway.js +1 -0
- package/dist/build/codegen/merge.d.ts +0 -1
- package/dist/build/codegen/merge.js +0 -1
- package/dist/build/codegen/wrangler.d.ts +18 -1
- package/dist/build/codegen/wrangler.js +36 -17
- package/dist/build/configuration/addressing.d.ts +2 -0
- package/dist/build/configuration/addressing.js +11 -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 +11 -0
- package/dist/build/configuration/worker-name.d.ts +2 -0
- package/dist/build/configuration/worker-name.js +18 -0
- package/dist/build/configuration.d.ts +4 -0
- package/dist/build/configuration.js +4 -0
- package/dist/build/direct.d.ts +4 -0
- package/dist/build/direct.js +91 -0
- package/dist/build/frontend/routes.d.ts +2 -0
- package/dist/build/frontend/routes.js +11 -0
- package/dist/build/frontend-v1.d.ts +16 -0
- package/dist/build/frontend-v1.js +133 -0
- package/dist/build/gateway/prepare.d.ts +8 -0
- package/dist/build/gateway/prepare.js +33 -0
- package/dist/build/gateway/source.d.ts +1 -0
- package/dist/build/gateway/source.js +7 -0
- package/dist/build/index.d.ts +10 -16
- package/dist/build/index.js +9 -16
- package/dist/build/materialize.d.ts +4 -0
- package/dist/build/materialize.js +10 -0
- package/dist/build/metafile.d.ts +5 -0
- package/dist/build/metafile.js +14 -0
- package/dist/build/parse-output.d.ts +0 -1
- package/dist/build/parse-output.js +0 -1
- package/dist/build/qualification.d.ts +5 -0
- package/dist/build/qualification.js +7 -0
- package/dist/build/roots.d.ts +2 -0
- package/dist/build/roots.js +34 -0
- package/dist/build/worker/artifact.d.ts +39 -0
- package/dist/build/worker/artifact.js +1 -0
- package/dist/build/worker/artifact.load.d.ts +3 -0
- package/dist/build/worker/artifact.load.js +277 -0
- package/dist/build/worker/artifact.manifest.d.ts +47 -0
- package/dist/build/worker/artifact.manifest.js +87 -0
- package/dist/build/worker/artifact.paths.d.ts +4 -0
- package/dist/build/worker/artifact.paths.js +25 -0
- package/dist/build/worker/codegen/entry.d.ts +2 -0
- package/dist/build/worker/codegen/entry.js +28 -0
- package/dist/build/worker/codegen/service.d.ts +15 -0
- package/dist/build/worker/codegen/service.js +46 -0
- package/dist/build/worker/execution.prepare.d.ts +18 -0
- package/dist/build/worker/execution.prepare.js +108 -0
- package/dist/build/worker/index.d.ts +2 -0
- package/dist/build/worker/index.js +1 -0
- package/dist/build/worker/modules.d.ts +11 -0
- package/dist/build/worker/modules.js +26 -0
- package/dist/build/worker/runtime.d.ts +10 -0
- package/dist/build/worker/runtime.js +35 -0
- package/dist/build/worker/specifier.d.ts +1 -0
- package/dist/build/worker/specifier.js +5 -0
- package/dist/build/wrangler/bundle.d.ts +10 -0
- package/dist/build/wrangler/bundle.js +29 -0
- package/dist/build/wrangler/deploy.d.ts +23 -0
- package/dist/build/wrangler/deploy.js +35 -0
- package/dist/build/wrangler/development.d.ts +15 -0
- package/dist/build/wrangler/development.js +135 -0
- package/dist/build/wrangler/namespace.d.ts +10 -0
- package/dist/build/wrangler/namespace.js +26 -0
- package/dist/build/wrangler/process.d.ts +17 -0
- package/dist/build/wrangler/process.js +115 -0
- package/dist/build/wrangler/release.d.ts +11 -0
- package/dist/build/wrangler/release.js +27 -0
- package/dist/build/wrangler/secrets.d.ts +6 -0
- package/dist/build/wrangler/secrets.js +58 -0
- package/dist/build/wrangler/status.d.ts +14 -0
- package/dist/build/wrangler/status.js +56 -0
- package/dist/build/wrangler/type-generation.d.ts +6 -0
- package/dist/build/wrangler/type-generation.js +19 -0
- package/dist/build/wrangler-cli.d.ts +8 -86
- package/dist/build/wrangler-cli.js +8 -275
- package/dist/cloudflare.d.ts +1 -34
- package/dist/cloudflare.js +1 -205
- package/dist/deploy/deploy.d.ts +17 -0
- package/dist/deploy/deploy.js +234 -0
- package/dist/deploy/index.d.ts +1 -0
- package/dist/deploy/index.js +1 -0
- package/dist/identity/binding.d.ts +2 -0
- package/dist/identity/binding.js +2 -0
- package/dist/identity/material.d.ts +7 -0
- package/dist/identity/material.js +42 -0
- package/dist/identity/secret.d.ts +3 -0
- package/dist/identity/secret.js +7 -0
- package/dist/identity/source.d.ts +26 -0
- package/dist/identity/source.js +98 -0
- package/dist/index.d.ts +3 -4
- package/dist/index.js +3 -4
- package/dist/params.d.ts +24 -5
- package/dist/params.js +0 -1
- package/dist/release/index.d.ts +2 -0
- package/dist/release/index.js +2 -0
- package/dist/release/namespace.d.ts +2 -0
- package/dist/release/namespace.js +6 -0
- package/dist/release/target.d.ts +2 -0
- package/dist/release/target.js +9 -0
- package/dist/worker/entry.d.ts +39 -0
- package/dist/worker/entry.js +162 -0
- package/dist/worker/fetch.d.ts +11 -0
- package/dist/worker/fetch.js +31 -0
- package/dist/worker/gateway.d.ts +15 -0
- package/dist/worker/gateway.js +26 -0
- package/dist/worker/identity.d.ts +7 -0
- package/dist/worker/identity.js +23 -0
- package/dist/worker/index.d.ts +3 -59
- package/dist/worker/index.js +3 -209
- package/package.json +21 -18
- package/template/astrale.config.ts +19 -11
- 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/worker.d.ts +0 -12
- package/dist/build/codegen/worker.d.ts.map +0 -1
- package/dist/build/codegen/worker.js +0 -40
- package/dist/build/codegen/worker.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/frontend.d.ts +0 -26
- package/dist/build/frontend.d.ts.map +0 -1
- package/dist/build/frontend.js +0 -263
- package/dist/build/frontend.js.map +0 -1
- package/dist/build/index.d.ts.map +0 -1
- package/dist/build/index.js.map +0 -1
- package/dist/build/parse-output.d.ts.map +0 -1
- package/dist/build/parse-output.js.map +0 -1
- package/dist/build/prepare.d.ts +0 -18
- package/dist/build/prepare.d.ts.map +0 -1
- package/dist/build/prepare.js +0 -117
- package/dist/build/prepare.js.map +0 -1
- package/dist/build/worker.d.ts +0 -15
- package/dist/build/worker.d.ts.map +0 -1
- package/dist/build/worker.js +0 -21
- package/dist/build/worker.js.map +0 -1
- package/dist/build/wrangler-cli.d.ts.map +0 -1
- 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/index.d.ts.map +0 -1
- package/dist/worker/index.js.map +0 -1
- package/src/build/codegen/identity.ts +0 -40
- package/src/build/codegen/merge.ts +0 -92
- package/src/build/codegen/worker.ts +0 -53
- package/src/build/codegen/wrangler.ts +0 -127
- package/src/build/frontend.ts +0 -324
- package/src/build/index.ts +0 -29
- package/src/build/parse-output.ts +0 -60
- package/src/build/prepare.ts +0 -161
- package/src/build/worker.ts +0 -37
- package/src/build/wrangler-cli.ts +0 -362
- package/src/cloudflare.ts +0 -251
- package/src/index.ts +0 -21
- package/src/params.ts +0 -92
- package/src/worker/index.ts +0 -314
- package/src/worker/tsconfig.json +0 -10
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `@astrale-os/adapter-cloudflare` — deploy an Astrale
|
|
3
|
-
* Cloudflare
|
|
2
|
+
* `@astrale-os/adapter-cloudflare` — deploy an Astrale application through
|
|
3
|
+
* Cloudflare Workers on YOUR OWN Cloudflare account.
|
|
4
4
|
*
|
|
5
5
|
* import { cloudflare } from '@astrale-os/adapter-cloudflare'
|
|
6
6
|
*
|
|
7
7
|
* adapter: cloudflare({
|
|
8
8
|
* dev: { secrets: '.env.dev' },
|
|
9
|
-
* prod: { route: 'crm.acme.dev', secrets: '.env.prod' },
|
|
9
|
+
* prod: { route: 'crm.acme.dev', secrets: '.env.prod', signingIdentity: '.astrale/identity.json' },
|
|
10
10
|
* })
|
|
11
11
|
*
|
|
12
12
|
* `dev` runs `wrangler dev` locally; an env with no `route` ships to
|
|
@@ -18,4 +18,3 @@
|
|
|
18
18
|
*/
|
|
19
19
|
export { cloudflare } from './cloudflare.js';
|
|
20
20
|
export type { CloudflareParams } from './params.js';
|
|
21
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `@astrale-os/adapter-cloudflare` — deploy an Astrale
|
|
3
|
-
* Cloudflare
|
|
2
|
+
* `@astrale-os/adapter-cloudflare` — deploy an Astrale application through
|
|
3
|
+
* Cloudflare Workers on YOUR OWN Cloudflare account.
|
|
4
4
|
*
|
|
5
5
|
* import { cloudflare } from '@astrale-os/adapter-cloudflare'
|
|
6
6
|
*
|
|
7
7
|
* adapter: cloudflare({
|
|
8
8
|
* dev: { secrets: '.env.dev' },
|
|
9
|
-
* prod: { route: 'crm.acme.dev', secrets: '.env.prod' },
|
|
9
|
+
* prod: { route: 'crm.acme.dev', secrets: '.env.prod', signingIdentity: '.astrale/identity.json' },
|
|
10
10
|
* })
|
|
11
11
|
*
|
|
12
12
|
* `dev` runs `wrangler dev` locally; an env with no `route` ships to
|
|
@@ -17,4 +17,3 @@
|
|
|
17
17
|
* `/build` toolkit and ships it through the Astrale platform.
|
|
18
18
|
*/
|
|
19
19
|
export { cloudflare } from './cloudflare.js';
|
|
20
|
-
//# sourceMappingURL=index.js.map
|
package/dist/params.d.ts
CHANGED
|
@@ -17,8 +17,22 @@ export interface CloudflareParams {
|
|
|
17
17
|
* localhost) but still used as the placeholder base domain there.
|
|
18
18
|
*/
|
|
19
19
|
readonly route?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Exact public Addressing expected after deployment. Use this for an already
|
|
22
|
+
* known provider-assigned address such as `*.workers.dev`; `route` remains
|
|
23
|
+
* the field that asks Cloudflare to attach a custom hostname. When both are
|
|
24
|
+
* present they must identify the same HTTPS origin.
|
|
25
|
+
*/
|
|
26
|
+
readonly addressing?: string;
|
|
20
27
|
/** Gitignored secrets file (whole contents = secrets), e.g. `'.env.dev'`. */
|
|
21
28
|
readonly secrets?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Private Ed25519 JWK file retained outside source control. Remote deploys
|
|
31
|
+
* require this explicit source so a fresh runner cannot silently rotate an
|
|
32
|
+
* installed Domain identity. Local watch generates `.astrale/identity.json`
|
|
33
|
+
* once when this field is omitted.
|
|
34
|
+
*/
|
|
35
|
+
readonly signingIdentity?: string;
|
|
22
36
|
/** Local dev port for `wrangler dev`. Default 8787. */
|
|
23
37
|
readonly port?: number;
|
|
24
38
|
/**
|
|
@@ -36,6 +50,12 @@ export interface CloudflareParams {
|
|
|
36
50
|
readonly remote?: boolean;
|
|
37
51
|
/** Extra plain (non-secret) vars to inject as Worker vars. */
|
|
38
52
|
readonly vars?: Record<string, string>;
|
|
53
|
+
/**
|
|
54
|
+
* Maximum encoded invocation request bytes retained by the Worker Server.
|
|
55
|
+
* Defaults to 1 MiB and is capped at 16 MiB. Increase only from measured
|
|
56
|
+
* application traffic; Publication and Bundle delivery have separate limits.
|
|
57
|
+
*/
|
|
58
|
+
readonly maximumRequestBytes?: number;
|
|
39
59
|
/**
|
|
40
60
|
* Escape hatch: raw wrangler config deep-merged over the generated base. Use
|
|
41
61
|
* it to declare extra bindings the adapter has no typed field for — KV, R2,
|
|
@@ -60,10 +80,10 @@ export interface CloudflareParams {
|
|
|
60
80
|
* instead of the public edge. A Cloudflare Worker can't fetch a same-zone
|
|
61
81
|
* hostname served by another Worker — Cloudflare 522s it — so without this the
|
|
62
82
|
* worker can't fetch an instance kernel's JWKS to verify an inbound credential
|
|
63
|
-
* from that instance, and every cross-instance call (install hooks,
|
|
64
|
-
*
|
|
65
|
-
* failed`. Point it at a router Worker that
|
|
66
|
-
* (Astrale: `admin-router`).
|
|
83
|
+
* from that instance, and every cross-instance call (install hooks, delegated
|
|
84
|
+
* GUI calls, and traffic initiated from a Shell-handshaken View) fails with
|
|
85
|
+
* `2002 Credential verification failed`. Point it at a router Worker that
|
|
86
|
+
* reaches those hosts internally (Astrale: `admin-router`).
|
|
67
87
|
*
|
|
68
88
|
* Wires BOTH halves the SDK needs: the `<binding>` service binding in the
|
|
69
89
|
* generated wrangler config, and a `routeSubrequest` predicate in the worker
|
|
@@ -88,4 +108,3 @@ export interface CloudflareParams {
|
|
|
88
108
|
readonly minLabels?: number;
|
|
89
109
|
};
|
|
90
110
|
}
|
|
91
|
-
//# sourceMappingURL=params.d.ts.map
|
package/dist/params.js
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
const REVISION = /^sha256:([0-9a-f]{64})$/u;
|
|
2
|
+
/** Exact Schema revisions select stable Workers inside the private namespace. */
|
|
3
|
+
export function releaseWorkerName(revision) {
|
|
4
|
+
const digest = REVISION.exec(revision)?.[1];
|
|
5
|
+
if (digest === undefined)
|
|
6
|
+
throw new TypeError('Cloudflare Release revision is invalid.');
|
|
7
|
+
const encoded = BigInt(`0x${digest}`).toString(36).padStart(50, '0');
|
|
8
|
+
return `r-${encoded}`;
|
|
9
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { JsonWebKey } from '@astrale-os/sdk/auth';
|
|
2
|
+
import type { BuildMaterial } from '@astrale-os/sdk/deployment/build/material';
|
|
3
|
+
import type { ExecutionContext } from '@astrale-os/sdk/execution';
|
|
4
|
+
import type { Runtime } from '@astrale-os/sdk/runtime';
|
|
5
|
+
export interface Fetcher {
|
|
6
|
+
fetch(request: Request): Response | Promise<Response>;
|
|
7
|
+
}
|
|
8
|
+
export interface CloudflareAdapterBindings {
|
|
9
|
+
readonly ASTRALE_SIGNING_IDENTITY?: string;
|
|
10
|
+
readonly WORKER_URL?: string;
|
|
11
|
+
readonly IDENTITY_ISS?: string;
|
|
12
|
+
readonly ASSETS?: Fetcher;
|
|
13
|
+
readonly SELF?: Fetcher;
|
|
14
|
+
readonly FRONTEND_DEV_URL?: string;
|
|
15
|
+
readonly DISPATCHER?: {
|
|
16
|
+
get(name: string): Fetcher;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export interface CloudflareWorkerConfig<RuntimeValue extends Runtime, Bindings extends CloudflareAdapterBindings, Environment> {
|
|
20
|
+
/** Load the exact Runtime and Build once, after the isolate has accepted a request. */
|
|
21
|
+
readonly load: () => CloudflareWorkerSource<RuntimeValue> | Promise<CloudflareWorkerSource<RuntimeValue>>;
|
|
22
|
+
readonly privateKey: JsonWebKey | ((bindings: Bindings) => JsonWebKey);
|
|
23
|
+
readonly resolveEnvironment?: (bindings: Bindings) => Environment;
|
|
24
|
+
readonly resolveUrl?: (bindings: Bindings, requestOrigin: string) => string;
|
|
25
|
+
readonly providerAssignedWorker?: string;
|
|
26
|
+
readonly selfBinding?: (bindings: Bindings) => Fetcher | null | undefined;
|
|
27
|
+
readonly routeSubrequest?: (url: URL, bindings: Bindings) => Fetcher | null | undefined;
|
|
28
|
+
readonly invocationTimeoutMs?: number;
|
|
29
|
+
readonly maximumRequestBytes?: number;
|
|
30
|
+
}
|
|
31
|
+
export interface CloudflareWorkerSource<RuntimeValue extends Runtime> {
|
|
32
|
+
readonly runtime: RuntimeValue;
|
|
33
|
+
readonly material: BuildMaterial;
|
|
34
|
+
}
|
|
35
|
+
export interface CloudflareWorker<Bindings> {
|
|
36
|
+
fetch(request: Request, environment: Bindings, executionContext?: ExecutionContext): Response | Promise<Response>;
|
|
37
|
+
}
|
|
38
|
+
/** Compose one exact revision Service; the provider gateway owns revision selection. */
|
|
39
|
+
export declare function cloudflareWorkerEntry<Bindings extends CloudflareAdapterBindings, Environment = Bindings>(): <RuntimeValue extends Runtime>(config: CloudflareWorkerConfig<RuntimeValue, Bindings, Environment>) => CloudflareWorker<Bindings>;
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { issuer } from '@astrale-os/sdk/auth';
|
|
2
|
+
import { restoreBuild } from '@astrale-os/sdk/deployment/build/material';
|
|
3
|
+
import { addressing, assemble } from '@astrale-os/sdk/deployment/release/runtime';
|
|
4
|
+
import { createIdentity, serve } from '@astrale-os/sdk/execution';
|
|
5
|
+
import { dispatchCloudflareRequest, prepareCloudflareRequest } from './fetch.js';
|
|
6
|
+
/** Compose one exact revision Service; the provider gateway owns revision selection. */
|
|
7
|
+
export function cloudflareWorkerEntry() {
|
|
8
|
+
return function (config) {
|
|
9
|
+
let currentIssuer;
|
|
10
|
+
let realized;
|
|
11
|
+
const realize = () => {
|
|
12
|
+
if (realized !== undefined)
|
|
13
|
+
return realized;
|
|
14
|
+
realized = Promise.resolve()
|
|
15
|
+
.then(config.load)
|
|
16
|
+
.then((source) => {
|
|
17
|
+
const build = restoreBuild(source.material, source.runtime);
|
|
18
|
+
const application = serve({
|
|
19
|
+
runtime: source.runtime,
|
|
20
|
+
load(environment) {
|
|
21
|
+
if (currentIssuer !== environment.issuer) {
|
|
22
|
+
throw new TypeError('Cloudflare Worker request origin differs from its current Release.');
|
|
23
|
+
}
|
|
24
|
+
return assemble(build, addressing(environment.issuer));
|
|
25
|
+
},
|
|
26
|
+
initialize: (environment) => config.resolveEnvironment === undefined
|
|
27
|
+
? environment.bindings
|
|
28
|
+
: config.resolveEnvironment(environment.bindings),
|
|
29
|
+
identity(environment) {
|
|
30
|
+
return createIdentity({
|
|
31
|
+
issuer: issuer.accept(environment.issuer),
|
|
32
|
+
subject: build.schema.compiled.root.origin,
|
|
33
|
+
privateKey: typeof config.privateKey === 'function'
|
|
34
|
+
? config.privateKey(environment.bindings)
|
|
35
|
+
: config.privateKey,
|
|
36
|
+
fetch: routedFetch(application, environment, config),
|
|
37
|
+
...(new URL(environment.issuer).protocol === 'http:'
|
|
38
|
+
? { allowInsecureHttp: true }
|
|
39
|
+
: {}),
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
server: {
|
|
43
|
+
health: { probe: { check: () => true } },
|
|
44
|
+
limits: {
|
|
45
|
+
maximumRequestBytes: config.maximumRequestBytes ?? 1024 * 1024,
|
|
46
|
+
maximumPublicationBytes: 1024 * 1024,
|
|
47
|
+
maximumBundleBytes: 8 * 1024 * 1024,
|
|
48
|
+
maximumDeliveryBytes: 8 * 1024 * 1024,
|
|
49
|
+
invocationTimeoutMs: config.invocationTimeoutMs ?? 30_000,
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
return Object.freeze({ build, application });
|
|
54
|
+
});
|
|
55
|
+
return realized;
|
|
56
|
+
};
|
|
57
|
+
return Object.freeze({
|
|
58
|
+
async fetch(request, bindings, context) {
|
|
59
|
+
const url = new URL(request.url);
|
|
60
|
+
const issuer = canonicalServingUrl(bindings.WORKER_URL ??
|
|
61
|
+
bindings.IDENTITY_ISS ??
|
|
62
|
+
config.resolveUrl?.(bindings, requestOrigin(url, request)) ??
|
|
63
|
+
requestOrigin(url, request));
|
|
64
|
+
if (config.providerAssignedWorker !== undefined &&
|
|
65
|
+
bindings.WORKER_URL === undefined &&
|
|
66
|
+
bindings.IDENTITY_ISS === undefined &&
|
|
67
|
+
!matchesWorkersDevClaim(issuer, config.providerAssignedWorker)) {
|
|
68
|
+
return new Response('Request origin differs from provider-assigned Addressing.', {
|
|
69
|
+
status: 421,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
if (currentIssuer !== undefined && currentIssuer !== issuer) {
|
|
73
|
+
return new Response('Request origin differs from the current Release.', { status: 421 });
|
|
74
|
+
}
|
|
75
|
+
currentIssuer = issuer;
|
|
76
|
+
const { application, build } = await realize();
|
|
77
|
+
const frontend = await frontendAsset(build.frontend, bindings, url, request);
|
|
78
|
+
if (frontend !== undefined)
|
|
79
|
+
return frontend;
|
|
80
|
+
return application.fetch(request, Object.freeze({ bindings, issuer }), context);
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function routedFetch(application, environment, config) {
|
|
86
|
+
return async (input, init) => {
|
|
87
|
+
const { request, rejectRedirect } = prepareCloudflareRequest(input, init);
|
|
88
|
+
const destination = new URL(request.url);
|
|
89
|
+
if (destination.origin === environment.issuer) {
|
|
90
|
+
const self = config.selfBinding?.(environment.bindings);
|
|
91
|
+
return dispatchCloudflareRequest(request, rejectRedirect, (selected) => self === null || self === undefined
|
|
92
|
+
? application.fetch(selected, environment)
|
|
93
|
+
: self.fetch(selected));
|
|
94
|
+
}
|
|
95
|
+
const route = config.routeSubrequest?.(destination, environment.bindings);
|
|
96
|
+
return dispatchCloudflareRequest(request, rejectRedirect, (selected) => route === null || route === undefined ? fetch(selected) : route.fetch(selected));
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
async function frontendAsset(frontend, environment, url, request) {
|
|
100
|
+
if (frontend === undefined || frontend.source.kind === 'external')
|
|
101
|
+
return undefined;
|
|
102
|
+
if (reserved(url.pathname) || (request.method !== 'GET' && request.method !== 'HEAD')) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
const development = environment.FRONTEND_DEV_URL?.replace(/\/+$/u, '');
|
|
106
|
+
if (development !== undefined && development.length > 0) {
|
|
107
|
+
return fetch(new Request(new URL(`${url.pathname}${url.search}`, `${development}/`), request));
|
|
108
|
+
}
|
|
109
|
+
const assetPath = url.pathname.startsWith('/assets/');
|
|
110
|
+
const route = assetPath
|
|
111
|
+
? undefined
|
|
112
|
+
: frontend.routes
|
|
113
|
+
.filter((candidate) => candidate.path === url.pathname ||
|
|
114
|
+
(frontend.source.kind === 'vite' &&
|
|
115
|
+
frontend.source.spa &&
|
|
116
|
+
(candidate.path === '/' || url.pathname.startsWith(`${candidate.path}/`))))
|
|
117
|
+
.sort((left, right) => right.path.length - left.path.length)[0];
|
|
118
|
+
if (route === undefined && !assetPath)
|
|
119
|
+
return undefined;
|
|
120
|
+
const assets = environment.ASSETS;
|
|
121
|
+
if (assets === undefined)
|
|
122
|
+
return undefined;
|
|
123
|
+
if (route === undefined)
|
|
124
|
+
return assets.fetch(request);
|
|
125
|
+
return assets.fetch(new Request(new URL(`/${route.document}${url.search}`, url.origin), request));
|
|
126
|
+
}
|
|
127
|
+
function reserved(path) {
|
|
128
|
+
return ['/releases', '/install', '/health', '/meta', '/.well-known'].some((prefix) => path === prefix || path.startsWith(`${prefix}/`));
|
|
129
|
+
}
|
|
130
|
+
function requestOrigin(url, request) {
|
|
131
|
+
if (url.protocol !== 'http:')
|
|
132
|
+
return url.origin;
|
|
133
|
+
const forwarded = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim().toLowerCase();
|
|
134
|
+
return forwarded === 'https' ? `https://${url.host}` : url.origin;
|
|
135
|
+
}
|
|
136
|
+
function canonicalServingUrl(input) {
|
|
137
|
+
let url;
|
|
138
|
+
try {
|
|
139
|
+
url = new URL(input);
|
|
140
|
+
}
|
|
141
|
+
catch (cause) {
|
|
142
|
+
throw new TypeError('Cloudflare Worker serving URL is invalid.', { cause });
|
|
143
|
+
}
|
|
144
|
+
if ((url.protocol !== 'https:' && url.protocol !== 'http:') ||
|
|
145
|
+
url.username !== '' ||
|
|
146
|
+
url.password !== '' ||
|
|
147
|
+
url.pathname !== '/' ||
|
|
148
|
+
url.search !== '' ||
|
|
149
|
+
url.hash !== '') {
|
|
150
|
+
throw new TypeError('Cloudflare Worker serving URL must be one HTTP(S) origin.');
|
|
151
|
+
}
|
|
152
|
+
return url.origin;
|
|
153
|
+
}
|
|
154
|
+
function matchesWorkersDevClaim(issuer, expectedWorker) {
|
|
155
|
+
const url = new URL(issuer);
|
|
156
|
+
const labels = url.hostname.split('.');
|
|
157
|
+
return (url.protocol === 'https:' &&
|
|
158
|
+
labels.length >= 4 &&
|
|
159
|
+
labels[0] === expectedWorker &&
|
|
160
|
+
labels.at(-2) === 'workers' &&
|
|
161
|
+
labels.at(-1) === 'dev');
|
|
162
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type RequestDispatcher = (request: Request) => Response | Promise<Response>;
|
|
2
|
+
export interface CloudflareRequest {
|
|
3
|
+
readonly request: Request;
|
|
4
|
+
readonly rejectRedirect: boolean;
|
|
5
|
+
}
|
|
6
|
+
/** Construct a Cloudflare-compatible transport request without weakening redirect:error. */
|
|
7
|
+
export declare function prepareCloudflareRequest(input: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]): CloudflareRequest;
|
|
8
|
+
/** Preserve Fetch redirect:error semantics on Cloudflare, whose transport rejects that mode. */
|
|
9
|
+
export declare function dispatchCloudflareRequest(request: Request, rejectRedirect: boolean, dispatch: RequestDispatcher): Promise<Response>;
|
|
10
|
+
/** Restore the requested URL omitted by Cloudflare service-binding Responses. */
|
|
11
|
+
export declare function adaptCloudflareResponse(request: Request, response: Response): Response;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
|
|
2
|
+
/** Construct a Cloudflare-compatible transport request without weakening redirect:error. */
|
|
3
|
+
export function prepareCloudflareRequest(input, init) {
|
|
4
|
+
const rejectRedirect = init?.redirect === 'error' ||
|
|
5
|
+
(init?.redirect === undefined && input instanceof Request && input.redirect === 'error');
|
|
6
|
+
const request = new Request(input, rejectRedirect ? { ...init, redirect: 'manual' } : init);
|
|
7
|
+
return Object.freeze({ request, rejectRedirect });
|
|
8
|
+
}
|
|
9
|
+
/** Preserve Fetch redirect:error semantics on Cloudflare, whose transport rejects that mode. */
|
|
10
|
+
export async function dispatchCloudflareRequest(request, rejectRedirect, dispatch) {
|
|
11
|
+
const response = adaptCloudflareResponse(request, await dispatch(request));
|
|
12
|
+
if (!rejectRedirect)
|
|
13
|
+
return response;
|
|
14
|
+
if (!response.redirected && !REDIRECT_STATUSES.has(response.status))
|
|
15
|
+
return response;
|
|
16
|
+
await response.body?.cancel();
|
|
17
|
+
throw new TypeError('Redirects are not allowed for this request.');
|
|
18
|
+
}
|
|
19
|
+
/** Restore the requested URL omitted by Cloudflare service-binding Responses. */
|
|
20
|
+
export function adaptCloudflareResponse(request, response) {
|
|
21
|
+
if (response.url !== '')
|
|
22
|
+
return response;
|
|
23
|
+
return new Proxy(response, {
|
|
24
|
+
get(target, property) {
|
|
25
|
+
if (property === 'url')
|
|
26
|
+
return request.url;
|
|
27
|
+
const value = Reflect.get(target, property, target);
|
|
28
|
+
return typeof value === 'function' ? value.bind(target) : value;
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Revision } from '@astrale-os/sdk/schema';
|
|
2
|
+
export interface DispatchNamespace {
|
|
3
|
+
get(name: string): {
|
|
4
|
+
fetch(request: Request): Response | Promise<Response>;
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
export interface CloudflareGatewayBindings {
|
|
8
|
+
readonly RELEASES: DispatchNamespace;
|
|
9
|
+
}
|
|
10
|
+
/** Stable public Worker that selects one exact immutable Schema-revision Service. */
|
|
11
|
+
export declare function cloudflareGatewayEntry(config: {
|
|
12
|
+
readonly current: Revision;
|
|
13
|
+
}): {
|
|
14
|
+
fetch(request: Request, bindings: CloudflareGatewayBindings): Promise<Response>;
|
|
15
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { releaseUnavailable, requestedRevision } from '@astrale-os/sdk/deployment/release/runtime';
|
|
2
|
+
import { releaseWorkerName } from '../release/target.js';
|
|
3
|
+
/** Stable public Worker that selects one exact immutable Schema-revision Service. */
|
|
4
|
+
export function cloudflareGatewayEntry(config) {
|
|
5
|
+
const current = releaseWorkerName(config.current);
|
|
6
|
+
return Object.freeze({
|
|
7
|
+
async fetch(request, bindings) {
|
|
8
|
+
const revision = requestedRevision(new URL(request.url).pathname);
|
|
9
|
+
const target = revision === undefined ? current : releaseWorkerName(revision);
|
|
10
|
+
let worker;
|
|
11
|
+
try {
|
|
12
|
+
worker = bindings.RELEASES.get(target);
|
|
13
|
+
return await worker.fetch(request);
|
|
14
|
+
}
|
|
15
|
+
catch (cause) {
|
|
16
|
+
if (isMissingWorker(cause))
|
|
17
|
+
return releaseUnavailable();
|
|
18
|
+
throw cause;
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/** Admit only Cloudflare's documented missing-script failure at the provider boundary. */
|
|
24
|
+
function isMissingWorker(cause) {
|
|
25
|
+
return cause instanceof Error && /^Worker not found(?:: [^\r\n]+|\.)?$/u.test(cause.message);
|
|
26
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { JsonWebKey } from '@astrale-os/sdk/auth';
|
|
2
|
+
/** Decode the secret representation at the Worker binding boundary. */
|
|
3
|
+
export declare function decodeSigningIdentity(input: string): JsonWebKey;
|
|
4
|
+
/** Remove adapter-owned signing material before Runtime initialization. */
|
|
5
|
+
export declare function runtimeBindings<Bindings extends {
|
|
6
|
+
readonly ASTRALE_SIGNING_IDENTITY: string;
|
|
7
|
+
}>(bindings: Bindings): Omit<Bindings, 'ASTRALE_SIGNING_IDENTITY'>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Decode the secret representation at the Worker binding boundary. */
|
|
2
|
+
export function decodeSigningIdentity(input) {
|
|
3
|
+
if (typeof input !== 'string' || input.length === 0)
|
|
4
|
+
invalid();
|
|
5
|
+
let decoded;
|
|
6
|
+
try {
|
|
7
|
+
decoded = JSON.parse(input);
|
|
8
|
+
}
|
|
9
|
+
catch (cause) {
|
|
10
|
+
throw new TypeError('Cloudflare signing identity secret is invalid JSON.', { cause });
|
|
11
|
+
}
|
|
12
|
+
if (decoded === null || typeof decoded !== 'object' || Array.isArray(decoded))
|
|
13
|
+
invalid();
|
|
14
|
+
return decoded;
|
|
15
|
+
}
|
|
16
|
+
/** Remove adapter-owned signing material before Runtime initialization. */
|
|
17
|
+
export function runtimeBindings(bindings) {
|
|
18
|
+
const { ASTRALE_SIGNING_IDENTITY: _identity, ...runtime } = bindings;
|
|
19
|
+
return runtime;
|
|
20
|
+
}
|
|
21
|
+
function invalid() {
|
|
22
|
+
throw new TypeError('Cloudflare signing identity secret is invalid.');
|
|
23
|
+
}
|
package/dist/worker/index.d.ts
CHANGED
|
@@ -1,59 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import type { ExecutionContext } from 'hono';
|
|
5
|
-
/** SDK-owned Publication manifest passed through without adapter interpretation. */
|
|
6
|
-
export type CloudflareManifest = NonNullable<RemoteDeploymentInput['manifest']>;
|
|
7
|
-
export interface Fetcher {
|
|
8
|
-
fetch(request: Request): Response | Promise<Response>;
|
|
9
|
-
}
|
|
10
|
-
/** Adapter-owned bindings generated into every Cloudflare deployment. */
|
|
11
|
-
export interface CloudflareAdapterBindings {
|
|
12
|
-
/** Native Cloudflare deployment metadata (direct Wrangler deployments). */
|
|
13
|
-
readonly ASTRALE_VERSION_METADATA?: {
|
|
14
|
-
readonly id: string;
|
|
15
|
-
readonly tag: string;
|
|
16
|
-
readonly timestamp: string;
|
|
17
|
-
};
|
|
18
|
-
/** Content-derived generation supplied by providers without version metadata. */
|
|
19
|
-
readonly ASTRALE_DEPLOYMENT_VERSION?: string;
|
|
20
|
-
readonly WORKER_URL?: string;
|
|
21
|
-
readonly ASSETS?: Fetcher;
|
|
22
|
-
readonly SELF?: Fetcher;
|
|
23
|
-
readonly FRONTEND_DEV_URL?: string;
|
|
24
|
-
readonly DISPATCHER?: {
|
|
25
|
-
get(name: string): Fetcher;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
interface CloudflareWorkerSharedConfig<Bindings extends CloudflareAdapterBindings, Environment> {
|
|
29
|
-
readonly privateKey: RemoteIdentityInput['privateKey'] | ((bindings: Bindings) => RemoteIdentityInput['privateKey']);
|
|
30
|
-
/** Compose provider bindings into the exact Domain environment once per deployment generation. */
|
|
31
|
-
readonly resolveEnvironment?: (bindings: Bindings) => Environment | Promise<Environment>;
|
|
32
|
-
readonly manifest?: CloudflareManifest;
|
|
33
|
-
readonly resolveUrl?: (bindings: Bindings, requestOrigin: string) => string;
|
|
34
|
-
readonly selfBinding?: (bindings: Bindings) => Fetcher | null | undefined;
|
|
35
|
-
readonly routeSubrequest?: (url: URL, bindings: Bindings) => Fetcher | null | undefined;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Resolve a Domain lazily when its module graph is too expensive for provider startup admission.
|
|
39
|
-
* The adapter resolves at most once concurrently and retries after a rejected resolution.
|
|
40
|
-
*/
|
|
41
|
-
export type CloudflareWorkerConfig<S extends schema.DomainSchema, Bindings extends CloudflareAdapterBindings, Environment> = CloudflareWorkerSharedConfig<Bindings, Environment> & ({
|
|
42
|
-
readonly definition: DomainDefinition<S>;
|
|
43
|
-
readonly resolveDefinition?: never;
|
|
44
|
-
} | {
|
|
45
|
-
readonly definition?: never;
|
|
46
|
-
readonly resolveDefinition: (bindings: Bindings) => DomainDefinition<S> | Promise<DomainDefinition<S>>;
|
|
47
|
-
});
|
|
48
|
-
export interface CloudflareWorker<Environment> {
|
|
49
|
-
fetch(request: Request, environment: Environment, executionContext?: ExecutionContext): Response | Promise<Response>;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Compose one Cloudflare module Worker without request-global mutation.
|
|
53
|
-
* Environment bindings are captured once per immutable serving URL/version generation.
|
|
54
|
-
*/
|
|
55
|
-
export declare function cloudflareWorkerEntry<Bindings extends CloudflareAdapterBindings, Environment = Bindings>(): <const S extends schema.DomainSchema>(config: CloudflareWorkerConfig<S, Bindings, Environment>) => CloudflareWorker<Bindings>;
|
|
56
|
-
/** Close an evicted Application before its environment bindings leave the cache. */
|
|
57
|
-
export declare function closeEvictedApplication(application: Pick<Application, 'close'>, now?: () => number): Promise<void>;
|
|
58
|
-
export {};
|
|
59
|
-
//# sourceMappingURL=index.d.ts.map
|
|
1
|
+
export * from './entry.js';
|
|
2
|
+
export { decodeSigningIdentity, runtimeBindings } from './identity.js';
|
|
3
|
+
export * from './gateway.js';
|