@astrale-os/adapter-cloudflare 0.5.0-beta.2 → 0.5.0-beta.21
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 +140 -0
- package/dist/build/artifact.d.ts +8 -0
- package/dist/build/artifact.js +179 -0
- package/dist/build/closure.d.ts +2 -0
- package/dist/build/closure.js +22 -0
- package/dist/build/codegen/execution.d.ts +13 -0
- package/dist/build/codegen/execution.js +44 -0
- package/dist/build/codegen/gateway.d.ts +1 -0
- package/dist/build/codegen/gateway.js +7 -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 +10 -1
- package/dist/build/codegen/wrangler.js +22 -16
- package/dist/build/configuration.d.ts +9 -0
- package/dist/build/configuration.js +40 -0
- package/dist/build/frontend-v1.d.ts +16 -0
- package/dist/build/frontend-v1.js +133 -0
- package/dist/build/index.d.ts +8 -16
- package/dist/build/index.js +8 -16
- 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/wrangler-cli.d.ts +53 -34
- package/dist/build/wrangler-cli.js +284 -111
- 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 +207 -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 +18 -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 +34 -0
- package/dist/worker/entry.js +157 -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 -48
- package/dist/worker/index.js +3 -186
- 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 -276
- package/src/worker/tsconfig.json +0 -10
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
|
/**
|
|
@@ -60,10 +74,10 @@ export interface CloudflareParams {
|
|
|
60
74
|
* instead of the public edge. A Cloudflare Worker can't fetch a same-zone
|
|
61
75
|
* hostname served by another Worker — Cloudflare 522s it — so without this the
|
|
62
76
|
* 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`).
|
|
77
|
+
* from that instance, and every cross-instance call (install hooks, delegated
|
|
78
|
+
* GUI calls, and traffic initiated from a Shell-handshaken View) fails with
|
|
79
|
+
* `2002 Credential verification failed`. Point it at a router Worker that
|
|
80
|
+
* reaches those hosts internally (Astrale: `admin-router`).
|
|
67
81
|
*
|
|
68
82
|
* Wires BOTH halves the SDK needs: the `<binding>` service binding in the
|
|
69
83
|
* generated wrangler config, and a `routeSubrequest` predicate in the worker
|
|
@@ -88,4 +102,3 @@ export interface CloudflareParams {
|
|
|
88
102
|
readonly minLabels?: number;
|
|
89
103
|
};
|
|
90
104
|
}
|
|
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,34 @@
|
|
|
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
|
+
readonly runtime: RuntimeValue;
|
|
21
|
+
readonly material: BuildMaterial;
|
|
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
|
+
}
|
|
30
|
+
export interface CloudflareWorker<Bindings> {
|
|
31
|
+
fetch(request: Request, environment: Bindings, executionContext?: ExecutionContext): Response | Promise<Response>;
|
|
32
|
+
}
|
|
33
|
+
/** Compose one exact revision Service; the provider gateway owns revision selection. */
|
|
34
|
+
export declare function cloudflareWorkerEntry<Bindings extends CloudflareAdapterBindings, Environment = Bindings>(): <RuntimeValue extends Runtime>(config: CloudflareWorkerConfig<RuntimeValue, Bindings, Environment>) => CloudflareWorker<Bindings>;
|
|
@@ -0,0 +1,157 @@
|
|
|
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
|
+
const build = restoreBuild(config.material, config.runtime);
|
|
15
|
+
const application = serve({
|
|
16
|
+
runtime: config.runtime,
|
|
17
|
+
load(environment) {
|
|
18
|
+
if (currentIssuer !== environment.issuer) {
|
|
19
|
+
throw new TypeError('Cloudflare Worker request origin differs from its current Release.');
|
|
20
|
+
}
|
|
21
|
+
return assemble(build, addressing(environment.issuer));
|
|
22
|
+
},
|
|
23
|
+
initialize: (environment) => config.resolveEnvironment === undefined
|
|
24
|
+
? environment.bindings
|
|
25
|
+
: config.resolveEnvironment(environment.bindings),
|
|
26
|
+
identity(environment) {
|
|
27
|
+
return createIdentity({
|
|
28
|
+
issuer: issuer.accept(environment.issuer),
|
|
29
|
+
subject: build.schema.compiled.root.origin,
|
|
30
|
+
privateKey: typeof config.privateKey === 'function'
|
|
31
|
+
? config.privateKey(environment.bindings)
|
|
32
|
+
: config.privateKey,
|
|
33
|
+
fetch: routedFetch(application, environment, config),
|
|
34
|
+
...(new URL(environment.issuer).protocol === 'http:'
|
|
35
|
+
? { allowInsecureHttp: true }
|
|
36
|
+
: {}),
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
server: {
|
|
40
|
+
health: { probe: { check: () => true } },
|
|
41
|
+
limits: {
|
|
42
|
+
maximumRequestBytes: 1024 * 1024,
|
|
43
|
+
maximumPublicationBytes: 1024 * 1024,
|
|
44
|
+
maximumBundleBytes: 8 * 1024 * 1024,
|
|
45
|
+
maximumDeliveryBytes: 8 * 1024 * 1024,
|
|
46
|
+
invocationTimeoutMs: config.invocationTimeoutMs ?? 30_000,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
realized = Object.freeze({ build, application });
|
|
51
|
+
return realized;
|
|
52
|
+
};
|
|
53
|
+
return Object.freeze({
|
|
54
|
+
async fetch(request, bindings, context) {
|
|
55
|
+
const url = new URL(request.url);
|
|
56
|
+
const issuer = canonicalServingUrl(bindings.WORKER_URL ??
|
|
57
|
+
bindings.IDENTITY_ISS ??
|
|
58
|
+
config.resolveUrl?.(bindings, requestOrigin(url, request)) ??
|
|
59
|
+
requestOrigin(url, request));
|
|
60
|
+
if (config.providerAssignedWorker !== undefined &&
|
|
61
|
+
bindings.WORKER_URL === undefined &&
|
|
62
|
+
bindings.IDENTITY_ISS === undefined &&
|
|
63
|
+
!matchesWorkersDevClaim(issuer, config.providerAssignedWorker)) {
|
|
64
|
+
return new Response('Request origin differs from provider-assigned Addressing.', {
|
|
65
|
+
status: 421,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (currentIssuer !== undefined && currentIssuer !== issuer) {
|
|
69
|
+
return new Response('Request origin differs from the current Release.', { status: 421 });
|
|
70
|
+
}
|
|
71
|
+
currentIssuer = issuer;
|
|
72
|
+
const { application, build } = realize();
|
|
73
|
+
const frontend = await frontendAsset(build.frontend, bindings, url, request);
|
|
74
|
+
if (frontend !== undefined)
|
|
75
|
+
return frontend;
|
|
76
|
+
return application.fetch(request, Object.freeze({ bindings, issuer }), context);
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function routedFetch(application, environment, config) {
|
|
82
|
+
return async (input, init) => {
|
|
83
|
+
const { request, rejectRedirect } = prepareCloudflareRequest(input, init);
|
|
84
|
+
const destination = new URL(request.url);
|
|
85
|
+
if (destination.origin === environment.issuer) {
|
|
86
|
+
const self = config.selfBinding?.(environment.bindings);
|
|
87
|
+
return dispatchCloudflareRequest(request, rejectRedirect, (selected) => self === null || self === undefined
|
|
88
|
+
? application.fetch(selected, environment)
|
|
89
|
+
: self.fetch(selected));
|
|
90
|
+
}
|
|
91
|
+
const route = config.routeSubrequest?.(destination, environment.bindings);
|
|
92
|
+
return dispatchCloudflareRequest(request, rejectRedirect, (selected) => route === null || route === undefined ? fetch(selected) : route.fetch(selected));
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
async function frontendAsset(frontend, environment, url, request) {
|
|
96
|
+
if (frontend === undefined || frontend.source.kind === 'external')
|
|
97
|
+
return undefined;
|
|
98
|
+
if (reserved(url.pathname) || (request.method !== 'GET' && request.method !== 'HEAD')) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
const development = environment.FRONTEND_DEV_URL?.replace(/\/+$/u, '');
|
|
102
|
+
if (development !== undefined && development.length > 0) {
|
|
103
|
+
return fetch(new Request(new URL(`${url.pathname}${url.search}`, `${development}/`), request));
|
|
104
|
+
}
|
|
105
|
+
const route = frontend.routes
|
|
106
|
+
.filter((candidate) => candidate.path === url.pathname ||
|
|
107
|
+
(frontend.source.kind === 'vite' &&
|
|
108
|
+
frontend.source.spa &&
|
|
109
|
+
(candidate.path === '/' || url.pathname.startsWith(`${candidate.path}/`))))
|
|
110
|
+
.sort((left, right) => right.path.length - left.path.length)[0];
|
|
111
|
+
if (route === undefined && !url.pathname.startsWith('/assets/'))
|
|
112
|
+
return undefined;
|
|
113
|
+
const assets = environment.ASSETS;
|
|
114
|
+
if (assets === undefined)
|
|
115
|
+
return undefined;
|
|
116
|
+
const response = await assets.fetch(request);
|
|
117
|
+
if (response.status !== 404 || route === undefined)
|
|
118
|
+
return response;
|
|
119
|
+
await response.body?.cancel();
|
|
120
|
+
return assets.fetch(new Request(new URL(`/${route.document}${url.search}`, url.origin), request));
|
|
121
|
+
}
|
|
122
|
+
function reserved(path) {
|
|
123
|
+
return ['/releases', '/install', '/health', '/meta', '/.well-known'].some((prefix) => path === prefix || path.startsWith(`${prefix}/`));
|
|
124
|
+
}
|
|
125
|
+
function requestOrigin(url, request) {
|
|
126
|
+
if (url.protocol !== 'http:')
|
|
127
|
+
return url.origin;
|
|
128
|
+
const forwarded = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim().toLowerCase();
|
|
129
|
+
return forwarded === 'https' ? `https://${url.host}` : url.origin;
|
|
130
|
+
}
|
|
131
|
+
function canonicalServingUrl(input) {
|
|
132
|
+
let url;
|
|
133
|
+
try {
|
|
134
|
+
url = new URL(input);
|
|
135
|
+
}
|
|
136
|
+
catch (cause) {
|
|
137
|
+
throw new TypeError('Cloudflare Worker serving URL is invalid.', { cause });
|
|
138
|
+
}
|
|
139
|
+
if ((url.protocol !== 'https:' && url.protocol !== 'http:') ||
|
|
140
|
+
url.username !== '' ||
|
|
141
|
+
url.password !== '' ||
|
|
142
|
+
url.pathname !== '/' ||
|
|
143
|
+
url.search !== '' ||
|
|
144
|
+
url.hash !== '') {
|
|
145
|
+
throw new TypeError('Cloudflare Worker serving URL must be one HTTP(S) origin.');
|
|
146
|
+
}
|
|
147
|
+
return url.origin;
|
|
148
|
+
}
|
|
149
|
+
function matchesWorkersDevClaim(issuer, expectedWorker) {
|
|
150
|
+
const url = new URL(issuer);
|
|
151
|
+
const labels = url.hostname.split('.');
|
|
152
|
+
return (url.protocol === 'https:' &&
|
|
153
|
+
labels.length >= 4 &&
|
|
154
|
+
labels[0] === expectedWorker &&
|
|
155
|
+
labels.at(-2) === 'workers' &&
|
|
156
|
+
labels.at(-1) === 'dev');
|
|
157
|
+
}
|
|
@@ -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
|
+
}
|
|
14
|
+
catch (cause) {
|
|
15
|
+
if (isMissingWorker(cause))
|
|
16
|
+
return releaseUnavailable();
|
|
17
|
+
throw cause;
|
|
18
|
+
}
|
|
19
|
+
return worker.fetch(request);
|
|
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(?:\b|:)/iu.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,48 +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
|
-
export interface CloudflareWorkerConfig<S extends schema.DomainSchema, Bindings extends CloudflareAdapterBindings, Environment> {
|
|
29
|
-
readonly definition: DomainDefinition<S>;
|
|
30
|
-
readonly privateKey: RemoteIdentityInput['privateKey'] | ((bindings: Bindings) => RemoteIdentityInput['privateKey']);
|
|
31
|
-
/** Compose provider bindings into the exact Domain environment once per deployment generation. */
|
|
32
|
-
readonly resolveEnvironment?: (bindings: Bindings) => Environment | Promise<Environment>;
|
|
33
|
-
readonly manifest?: CloudflareManifest;
|
|
34
|
-
readonly resolveUrl?: (bindings: Bindings, requestOrigin: string) => string;
|
|
35
|
-
readonly selfBinding?: (bindings: Bindings) => Fetcher | null | undefined;
|
|
36
|
-
readonly routeSubrequest?: (url: URL, bindings: Bindings) => Fetcher | null | undefined;
|
|
37
|
-
}
|
|
38
|
-
export interface CloudflareWorker<Environment> {
|
|
39
|
-
fetch(request: Request, environment: Environment, executionContext?: ExecutionContext): Response | Promise<Response>;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Compose one Cloudflare module Worker without request-global mutation.
|
|
43
|
-
* Environment bindings are captured once per immutable serving URL/version generation.
|
|
44
|
-
*/
|
|
45
|
-
export declare function cloudflareWorkerEntry<Bindings extends CloudflareAdapterBindings, Environment = Bindings>(): <const S extends schema.DomainSchema>(config: CloudflareWorkerConfig<S, Bindings, Environment>) => CloudflareWorker<Bindings>;
|
|
46
|
-
/** Close an evicted Application before its environment bindings leave the cache. */
|
|
47
|
-
export declare function closeEvictedApplication(application: Pick<Application, 'close'>, now?: () => number): Promise<void>;
|
|
48
|
-
//# sourceMappingURL=index.d.ts.map
|
|
1
|
+
export * from './entry.js';
|
|
2
|
+
export { decodeSigningIdentity, runtimeBindings } from './identity.js';
|
|
3
|
+
export * from './gateway.js';
|
package/dist/worker/index.js
CHANGED
|
@@ -1,186 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const MAXIMUM_CACHED_APPLICATIONS = 4;
|
|
5
|
-
const APPLICATION_CLOSE_TIMEOUT_MS = 5_000;
|
|
6
|
-
/**
|
|
7
|
-
* Compose one Cloudflare module Worker without request-global mutation.
|
|
8
|
-
* Environment bindings are captured once per immutable serving URL/version generation.
|
|
9
|
-
*/
|
|
10
|
-
export function cloudflareWorkerEntry() {
|
|
11
|
-
return function (config) {
|
|
12
|
-
const applications = new Map();
|
|
13
|
-
const applicationFor = async (url, bindings) => {
|
|
14
|
-
const version = deploymentVersion(bindings);
|
|
15
|
-
const key = `${url}\u0000${version}`;
|
|
16
|
-
const cached = applications.get(key);
|
|
17
|
-
if (cached !== undefined)
|
|
18
|
-
return cached;
|
|
19
|
-
const environment = config.resolveEnvironment === undefined
|
|
20
|
-
? bindings
|
|
21
|
-
: await config.resolveEnvironment(bindings);
|
|
22
|
-
const routedFetch = async (input, init) => {
|
|
23
|
-
const request = new Request(input, init);
|
|
24
|
-
const destination = new URL(request.url);
|
|
25
|
-
if (destination.origin === new URL(url).origin) {
|
|
26
|
-
const self = config.selfBinding?.(bindings);
|
|
27
|
-
if (self !== null && self !== undefined)
|
|
28
|
-
return self.fetch(request);
|
|
29
|
-
return serving.app.fetch(request, environment);
|
|
30
|
-
}
|
|
31
|
-
const route = config.routeSubrequest?.(destination, bindings);
|
|
32
|
-
return route === null || route === undefined ? fetch(request) : route.fetch(request);
|
|
33
|
-
};
|
|
34
|
-
const deploymentQuery = `deployment=${encodeURIComponent(version)}`;
|
|
35
|
-
const issuerId = issuer.accept(url);
|
|
36
|
-
const host = new HonoApplication();
|
|
37
|
-
host.get('/meta', (context) => context.json({
|
|
38
|
-
origin: config.definition.schema.origin,
|
|
39
|
-
issuer: issuerId,
|
|
40
|
-
schemaRevision: config.definition.domain.$.revision,
|
|
41
|
-
deploymentVersion: version,
|
|
42
|
-
}));
|
|
43
|
-
const serving = createRemoteApplication({
|
|
44
|
-
definition: config.definition,
|
|
45
|
-
environment,
|
|
46
|
-
identity: {
|
|
47
|
-
issuer: issuerId,
|
|
48
|
-
subject: config.definition.schema.origin,
|
|
49
|
-
privateKey: typeof config.privateKey === 'function'
|
|
50
|
-
? config.privateKey(bindings)
|
|
51
|
-
: config.privateKey,
|
|
52
|
-
fetch: routedFetch,
|
|
53
|
-
...(new URL(url).protocol === 'http:' ? { allowInsecureHttp: true } : {}),
|
|
54
|
-
},
|
|
55
|
-
deployment: {
|
|
56
|
-
bundleHref: `${url}/.well-known/astrale/domain.bundle.json?${deploymentQuery}`,
|
|
57
|
-
endpoint: { http: `${url}/invoke` },
|
|
58
|
-
...(config.definition.frontend === undefined
|
|
59
|
-
? {}
|
|
60
|
-
: { frontendBaseHref: url, frontendCacheKey: version }),
|
|
61
|
-
...(config.manifest === undefined ? {} : { manifest: config.manifest }),
|
|
62
|
-
installation: {
|
|
63
|
-
href: `${url}/install`,
|
|
64
|
-
method: 'POST',
|
|
65
|
-
authentication: { kind: 'public' },
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
app: host,
|
|
69
|
-
server: {
|
|
70
|
-
path: '/invoke',
|
|
71
|
-
health: { probe: { check: () => true } },
|
|
72
|
-
limits: {
|
|
73
|
-
maximumRequestBytes: 1024 * 1024,
|
|
74
|
-
maximumPublicationBytes: 1024 * 1024,
|
|
75
|
-
maximumBundleBytes: 8 * 1024 * 1024,
|
|
76
|
-
maximumInstallationBytes: 8 * 1024 * 1024,
|
|
77
|
-
invocationTimeoutMs: 30_000,
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
const created = Object.freeze({
|
|
82
|
-
origin: new URL(url).origin,
|
|
83
|
-
application: serving,
|
|
84
|
-
environment,
|
|
85
|
-
});
|
|
86
|
-
if (applications.size >= MAXIMUM_CACHED_APPLICATIONS) {
|
|
87
|
-
const oldest = applications.keys().next().value;
|
|
88
|
-
if (oldest !== undefined) {
|
|
89
|
-
const evicted = applications.get(oldest);
|
|
90
|
-
applications.delete(oldest);
|
|
91
|
-
if (evicted !== undefined) {
|
|
92
|
-
try {
|
|
93
|
-
await closeEvictedApplication(evicted.application);
|
|
94
|
-
}
|
|
95
|
-
catch (cause) {
|
|
96
|
-
await serving
|
|
97
|
-
.close({ deadline: Date.now() + APPLICATION_CLOSE_TIMEOUT_MS })
|
|
98
|
-
.catch(() => undefined);
|
|
99
|
-
throw cause;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
applications.set(key, created);
|
|
105
|
-
return created;
|
|
106
|
-
};
|
|
107
|
-
return Object.freeze({
|
|
108
|
-
async fetch(request, bindings, executionContext) {
|
|
109
|
-
const requestUrl = new URL(request.url);
|
|
110
|
-
if (config.definition.frontend !== undefined) {
|
|
111
|
-
const asset = await frontendAsset(config.definition, bindings, requestUrl, request);
|
|
112
|
-
if (asset !== undefined)
|
|
113
|
-
return asset;
|
|
114
|
-
}
|
|
115
|
-
const publicUrl = canonicalServingUrl(config.resolveUrl?.(bindings, requestOrigin(requestUrl, request)) ??
|
|
116
|
-
bindings.WORKER_URL ??
|
|
117
|
-
requestOrigin(requestUrl, request));
|
|
118
|
-
const serving = await applicationFor(publicUrl, bindings);
|
|
119
|
-
return serving.application.app.fetch(request, serving.environment, executionContext);
|
|
120
|
-
},
|
|
121
|
-
});
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
/** Close an evicted Application before its environment bindings leave the cache. */
|
|
125
|
-
export function closeEvictedApplication(application, now = Date.now) {
|
|
126
|
-
return application.close({ deadline: now() + APPLICATION_CLOSE_TIMEOUT_MS });
|
|
127
|
-
}
|
|
128
|
-
async function frontendAsset(definition, environment, url, request) {
|
|
129
|
-
const frontend = definition.frontend;
|
|
130
|
-
if (frontend === undefined || frontend.source.kind === 'external')
|
|
131
|
-
return undefined;
|
|
132
|
-
const routes = frontend.routes;
|
|
133
|
-
const route = Object.values(routes).find((candidate) => candidate.path === url.pathname);
|
|
134
|
-
const staticAsset = url.pathname.startsWith('/assets/');
|
|
135
|
-
if (route === undefined && !staticAsset)
|
|
136
|
-
return undefined;
|
|
137
|
-
if (request.method !== 'GET' && request.method !== 'HEAD')
|
|
138
|
-
return undefined;
|
|
139
|
-
const binding = environment.ASSETS;
|
|
140
|
-
if (binding === undefined)
|
|
141
|
-
return undefined;
|
|
142
|
-
const dev = environment.FRONTEND_DEV_URL?.replace(/\/+$/u, '');
|
|
143
|
-
if (dev !== undefined && dev.length > 0) {
|
|
144
|
-
return fetch(new Request(`${dev}${url.pathname}${url.search}`, request));
|
|
145
|
-
}
|
|
146
|
-
const path = route === undefined ? `${url.pathname}${url.search}` : `/${route.document}${url.search}`;
|
|
147
|
-
return binding.fetch(new Request(new URL(path, url.origin), request));
|
|
148
|
-
}
|
|
149
|
-
function requestOrigin(url, request) {
|
|
150
|
-
if (url.protocol !== 'http:')
|
|
151
|
-
return url.origin;
|
|
152
|
-
const forwarded = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim().toLowerCase();
|
|
153
|
-
return forwarded === 'https' ? `https://${url.host}` : url.origin;
|
|
154
|
-
}
|
|
155
|
-
function canonicalServingUrl(input) {
|
|
156
|
-
let url;
|
|
157
|
-
try {
|
|
158
|
-
url = new URL(input);
|
|
159
|
-
}
|
|
160
|
-
catch (cause) {
|
|
161
|
-
throw new TypeError('Cloudflare Worker serving URL is invalid.', { cause });
|
|
162
|
-
}
|
|
163
|
-
if ((url.protocol !== 'https:' && url.protocol !== 'http:') ||
|
|
164
|
-
url.username !== '' ||
|
|
165
|
-
url.password !== '' ||
|
|
166
|
-
url.pathname !== '/' ||
|
|
167
|
-
url.search !== '' ||
|
|
168
|
-
url.hash !== '') {
|
|
169
|
-
throw new TypeError('Cloudflare Worker serving URL must be one HTTP(S) origin.');
|
|
170
|
-
}
|
|
171
|
-
return url.origin;
|
|
172
|
-
}
|
|
173
|
-
function deploymentVersion(environment) {
|
|
174
|
-
const input = environment.ASTRALE_VERSION_METADATA;
|
|
175
|
-
if (input !== null &&
|
|
176
|
-
typeof input === 'object' &&
|
|
177
|
-
typeof input.id === 'string' &&
|
|
178
|
-
input.id.length > 0) {
|
|
179
|
-
return input.id;
|
|
180
|
-
}
|
|
181
|
-
const fallback = environment.ASTRALE_DEPLOYMENT_VERSION;
|
|
182
|
-
if (typeof fallback === 'string' && fallback.length > 0)
|
|
183
|
-
return fallback;
|
|
184
|
-
throw new TypeError('Cloudflare Worker deployment version is unavailable.');
|
|
185
|
-
}
|
|
186
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export * from './entry.js';
|
|
2
|
+
export { decodeSigningIdentity, runtimeBindings } from './identity.js';
|
|
3
|
+
export * from './gateway.js';
|