@astrale-os/adapter-cloudflare 0.4.10 → 0.5.0-beta.100
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/CHANGELOG.md +1193 -0
- package/README.md +29 -0
- package/dist/adapter.d.ts +15 -0
- package/dist/adapter.js +76 -0
- package/dist/build/artifact.d.ts +7 -0
- package/dist/build/artifact.js +165 -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/{codegen → build/codegen}/merge.d.ts +0 -1
- package/dist/{codegen → build/codegen}/merge.js +0 -1
- package/dist/{codegen → build/codegen}/wrangler.d.ts +19 -4
- package/dist/{codegen → build/codegen}/wrangler.js +49 -13
- package/dist/build/configuration/addressing.d.ts +2 -0
- package/dist/build/configuration/addressing.js +11 -0
- 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 +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 +5 -0
- package/dist/build/configuration.js +5 -0
- package/dist/build/direct.d.ts +4 -0
- package/dist/build/direct.js +93 -0
- package/dist/build/frontend/files.d.ts +7 -0
- package/dist/build/frontend/files.js +60 -0
- package/dist/build/frontend/routes.d.ts +2 -0
- package/dist/build/frontend/routes.js +11 -0
- package/dist/build/index.d.ts +8 -0
- package/dist/build/index.js +7 -0
- 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/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 +40 -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 +308 -0
- package/dist/build/worker/artifact.manifest.d.ts +48 -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 +18 -0
- package/dist/build/worker/codegen/service.js +53 -0
- package/dist/build/worker/execution.prepare.d.ts +18 -0
- package/dist/build/worker/execution.prepare.js +112 -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/cloudflare.d.ts +1 -37
- package/dist/cloudflare.js +1 -271
- package/dist/deploy/deploy.d.ts +16 -0
- package/dist/deploy/deploy.js +204 -0
- package/dist/deploy/index.d.ts +1 -0
- package/dist/deploy/index.js +1 -0
- package/dist/deploy/output.d.ts +2 -0
- package/dist/deploy/output.js +10 -0
- package/dist/development/develop.d.ts +15 -0
- package/dist/development/develop.js +270 -0
- package/dist/development/frontend.d.ts +11 -0
- package/dist/development/frontend.js +109 -0
- package/dist/development/host.d.ts +2 -0
- package/dist/development/host.js +46 -0
- package/dist/development/port.d.ts +1 -0
- package/dist/development/port.js +12 -0
- package/dist/development/readiness.d.ts +14 -0
- package/dist/development/readiness.js +71 -0
- package/dist/development/resolved-fetch.d.ts +17 -0
- package/dist/development/resolved-fetch.js +80 -0
- package/dist/development/worker-output.d.ts +2 -0
- package/dist/development/worker-output.js +25 -0
- package/dist/development/worker.d.ts +17 -0
- package/dist/development/worker.js +147 -0
- package/dist/identity/binding.d.ts +2 -0
- package/dist/identity/binding.js +2 -0
- package/dist/identity/index.d.ts +3 -0
- package/dist/identity/index.js +3 -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 +127 -0
- package/dist/index.d.ts +8 -8
- package/dist/index.js +7 -7
- package/dist/params.d.ts +50 -23
- package/dist/params.js +0 -1
- package/dist/release/deployment.d.ts +44 -0
- package/dist/release/deployment.js +141 -0
- package/dist/release/index.d.ts +2 -0
- package/dist/release/index.js +2 -0
- package/dist/release/staged-worker-name.d.ts +3 -0
- package/dist/release/staged-worker-name.js +10 -0
- package/dist/worker/entry.d.ts +44 -0
- package/dist/worker/entry.js +184 -0
- package/dist/worker/fetch.d.ts +11 -0
- package/dist/worker/fetch.js +31 -0
- package/dist/worker/identity.d.ts +7 -0
- package/dist/worker/identity.js +23 -0
- package/dist/worker/index.d.ts +2 -0
- package/dist/worker/index.js +2 -0
- package/dist/wrangler/bundle.d.ts +10 -0
- package/dist/wrangler/bundle.js +27 -0
- package/dist/wrangler/deploy.d.ts +24 -0
- package/dist/wrangler/deploy.js +44 -0
- package/dist/wrangler/index.d.ts +5 -0
- package/dist/wrangler/index.js +5 -0
- package/dist/wrangler/process.d.ts +17 -0
- package/dist/wrangler/process.js +115 -0
- package/dist/wrangler/secrets.d.ts +6 -0
- package/dist/wrangler/secrets.js +58 -0
- package/dist/wrangler/status.d.ts +14 -0
- package/dist/wrangler/status.js +56 -0
- package/dist/wrangler/type-generation.d.ts +6 -0
- package/dist/wrangler/type-generation.js +19 -0
- package/package.json +41 -18
- package/template/astrale.config.ts +26 -18
- package/dist/assets-pack.d.ts +0 -8
- package/dist/assets-pack.d.ts.map +0 -1
- package/dist/assets-pack.js +0 -30
- package/dist/assets-pack.js.map +0 -1
- package/dist/build.d.ts +0 -16
- package/dist/build.d.ts.map +0 -1
- package/dist/build.js +0 -15
- package/dist/build.js.map +0 -1
- package/dist/client.d.ts +0 -17
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -59
- package/dist/client.js.map +0 -1
- package/dist/cloudflare.d.ts.map +0 -1
- package/dist/cloudflare.js.map +0 -1
- package/dist/codegen/identity.d.ts +0 -10
- package/dist/codegen/identity.d.ts.map +0 -1
- package/dist/codegen/identity.js +0 -32
- package/dist/codegen/identity.js.map +0 -1
- package/dist/codegen/merge.d.ts.map +0 -1
- package/dist/codegen/merge.js.map +0 -1
- package/dist/codegen/worker.d.ts +0 -51
- package/dist/codegen/worker.d.ts.map +0 -1
- package/dist/codegen/worker.js +0 -120
- package/dist/codegen/worker.js.map +0 -1
- package/dist/codegen/wrangler.d.ts.map +0 -1
- package/dist/codegen/wrangler.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/parse-output.d.ts +0 -25
- package/dist/parse-output.d.ts.map +0 -1
- package/dist/parse-output.js +0 -53
- package/dist/parse-output.js.map +0 -1
- package/dist/wrangler-cli.d.ts +0 -78
- package/dist/wrangler-cli.d.ts.map +0 -1
- package/dist/wrangler-cli.js +0 -251
- package/dist/wrangler-cli.js.map +0 -1
- package/src/assets-pack.ts +0 -32
- package/src/build.ts +0 -16
- package/src/client.ts +0 -89
- package/src/cloudflare.ts +0 -321
- package/src/codegen/identity.ts +0 -40
- package/src/codegen/merge.ts +0 -92
- package/src/codegen/worker.ts +0 -136
- package/src/codegen/wrangler.ts +0 -105
- package/src/index.ts +0 -21
- package/src/params.ts +0 -96
- package/src/parse-output.ts +0 -54
- package/src/wrangler-cli.ts +0 -324
- package/template/.env.example +0 -9
- package/template/.gitignore.template +0 -16
- package/template/CLAUDE.md +0 -13
- package/template/README.md +0 -47
- package/template/client/.gitignore.template +0 -4
- package/template/client/__tests__/app.test.tsx +0 -15
- package/template/client/components.json +0 -25
- package/template/client/index.html +0 -12
- package/template/client/package.json +0 -35
- package/template/client/src/app.tsx +0 -52
- package/template/client/src/components/ui/cn.ts +0 -6
- package/template/client/src/main.tsx +0 -9
- package/template/client/src/styles.css +0 -78
- package/template/client/tsconfig.json +0 -26
- package/template/client/vite.config.ts +0 -22
- package/template/client/vitest.config.ts +0 -13
- package/template/core/README.md +0 -7
- package/template/deps.ts +0 -9
- package/template/domain.ts +0 -11
- package/template/env.ts +0 -8
- package/template/icons.ts +0 -4
- package/template/integrations/README.md +0 -7
- package/template/oxlint.config.ts +0 -1
- package/template/package.json +0 -58
- package/template/pnpm-workspace.yaml +0 -31
- package/template/runtime/index.ts +0 -8
- package/template/schema/index.ts +0 -10
- package/template/simulation/README.md +0 -12
- package/template/tsconfig.json +0 -28
- package/template/vitest.config.ts +0 -7
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { WorkerDeploymentId } from '../../release/deployment.js';
|
|
2
|
+
import type { WorkerModuleKind } from './artifact.js';
|
|
3
|
+
import { WORKER_MANIFEST_PATH } from './artifact.paths.js';
|
|
4
|
+
export declare const WORKER_ARTIFACT_FORMAT = "astrale.cloudflare.worker";
|
|
5
|
+
export declare const WORKER_ARTIFACT_VERSION = 2;
|
|
6
|
+
export interface WorkerArtifactManifestV2 {
|
|
7
|
+
readonly format: typeof WORKER_ARTIFACT_FORMAT;
|
|
8
|
+
readonly version: typeof WORKER_ARTIFACT_VERSION;
|
|
9
|
+
readonly origin: string;
|
|
10
|
+
readonly revision: string;
|
|
11
|
+
readonly deployment: WorkerDeploymentId;
|
|
12
|
+
readonly release: ManifestExecutable;
|
|
13
|
+
readonly runtime: {
|
|
14
|
+
readonly compatibilityDate: string;
|
|
15
|
+
readonly compatibilityFlags: readonly string[];
|
|
16
|
+
};
|
|
17
|
+
readonly bindings: {
|
|
18
|
+
readonly vars: Readonly<Record<string, string>>;
|
|
19
|
+
readonly services: readonly {
|
|
20
|
+
readonly name: string;
|
|
21
|
+
readonly service: string;
|
|
22
|
+
}[];
|
|
23
|
+
readonly requiredSecrets: readonly string[];
|
|
24
|
+
};
|
|
25
|
+
readonly assets?: {
|
|
26
|
+
readonly files: readonly {
|
|
27
|
+
readonly path: string;
|
|
28
|
+
readonly artifactPath: string;
|
|
29
|
+
readonly digest: string;
|
|
30
|
+
readonly contentType?: string;
|
|
31
|
+
}[];
|
|
32
|
+
readonly notFound: 'none' | '404-page' | 'single-page-application';
|
|
33
|
+
readonly runWorkerFirst: boolean;
|
|
34
|
+
readonly htmlHandling?: 'none';
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
interface ManifestExecutable {
|
|
38
|
+
readonly entrypoint: string;
|
|
39
|
+
readonly modules: readonly {
|
|
40
|
+
readonly path: string;
|
|
41
|
+
readonly kind: WorkerModuleKind;
|
|
42
|
+
readonly digest: string;
|
|
43
|
+
}[];
|
|
44
|
+
}
|
|
45
|
+
export declare function encodeWorkerArtifactManifest(input: Omit<WorkerArtifactManifestV2, 'format' | 'version'>): Uint8Array;
|
|
46
|
+
export declare function workerContentDigest(input: Uint8Array): string;
|
|
47
|
+
export declare function inferMediaType(path: string): string | undefined;
|
|
48
|
+
export { WORKER_MANIFEST_PATH };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { artifactPath, assetPath, comparePath, WORKER_MANIFEST_PATH } from './artifact.paths.js';
|
|
3
|
+
export const WORKER_ARTIFACT_FORMAT = 'astrale.cloudflare.worker';
|
|
4
|
+
export const WORKER_ARTIFACT_VERSION = 2;
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
export function encodeWorkerArtifactManifest(input) {
|
|
7
|
+
const manifest = {
|
|
8
|
+
format: WORKER_ARTIFACT_FORMAT,
|
|
9
|
+
version: WORKER_ARTIFACT_VERSION,
|
|
10
|
+
origin: input.origin,
|
|
11
|
+
revision: input.revision,
|
|
12
|
+
deployment: input.deployment,
|
|
13
|
+
release: canonicalExecutable(input.release),
|
|
14
|
+
runtime: {
|
|
15
|
+
compatibilityDate: input.runtime.compatibilityDate,
|
|
16
|
+
compatibilityFlags: Object.freeze([...input.runtime.compatibilityFlags].sort()),
|
|
17
|
+
},
|
|
18
|
+
bindings: {
|
|
19
|
+
vars: Object.fromEntries(Object.entries(input.bindings.vars).sort(([a], [b]) => comparePath(a, b))),
|
|
20
|
+
services: Object.freeze([...input.bindings.services].sort((a, b) => comparePath(a.name, b.name))),
|
|
21
|
+
requiredSecrets: Object.freeze([...input.bindings.requiredSecrets].sort(comparePath)),
|
|
22
|
+
},
|
|
23
|
+
...(input.assets === undefined
|
|
24
|
+
? {}
|
|
25
|
+
: {
|
|
26
|
+
assets: {
|
|
27
|
+
files: Object.freeze([...input.assets.files]
|
|
28
|
+
.map((file) => ({
|
|
29
|
+
path: assetPath(file.path),
|
|
30
|
+
artifactPath: artifactPath(file.artifactPath, 'Worker asset artifact path'),
|
|
31
|
+
digest: contentDigest(file.digest),
|
|
32
|
+
...(file.contentType === undefined ? {} : { contentType: file.contentType }),
|
|
33
|
+
}))
|
|
34
|
+
.sort((a, b) => comparePath(a.path, b.path))),
|
|
35
|
+
notFound: input.assets.notFound,
|
|
36
|
+
runWorkerFirst: input.assets.runWorkerFirst,
|
|
37
|
+
...(input.assets.htmlHandling === undefined
|
|
38
|
+
? {}
|
|
39
|
+
: { htmlHandling: input.assets.htmlHandling }),
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
};
|
|
43
|
+
return encoder.encode(`${JSON.stringify(manifest)}\n`);
|
|
44
|
+
}
|
|
45
|
+
function canonicalExecutable(input) {
|
|
46
|
+
const modules = [...input.modules]
|
|
47
|
+
.map((module) => ({
|
|
48
|
+
path: artifactPath(module.path, 'Worker module path'),
|
|
49
|
+
kind: module.kind,
|
|
50
|
+
digest: contentDigest(module.digest),
|
|
51
|
+
}))
|
|
52
|
+
.sort((a, b) => comparePath(a.path, b.path));
|
|
53
|
+
return {
|
|
54
|
+
entrypoint: artifactPath(input.entrypoint, 'Worker entrypoint'),
|
|
55
|
+
modules: Object.freeze(modules),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export function workerContentDigest(input) {
|
|
59
|
+
return `sha256:${createHash('sha256').update(input).digest('hex')}`;
|
|
60
|
+
}
|
|
61
|
+
function contentDigest(input) {
|
|
62
|
+
if (!/^sha256:[0-9a-f]{64}$/u.test(input)) {
|
|
63
|
+
throw new TypeError('Worker artifact content digest is invalid.');
|
|
64
|
+
}
|
|
65
|
+
return input;
|
|
66
|
+
}
|
|
67
|
+
export function inferMediaType(path) {
|
|
68
|
+
const extension = path.split('.').at(-1)?.toLowerCase();
|
|
69
|
+
return extension === undefined ? undefined : MEDIA_TYPES[extension];
|
|
70
|
+
}
|
|
71
|
+
const MEDIA_TYPES = Object.freeze({
|
|
72
|
+
html: 'text/html; charset=utf-8',
|
|
73
|
+
js: 'text/javascript; charset=utf-8',
|
|
74
|
+
mjs: 'text/javascript; charset=utf-8',
|
|
75
|
+
css: 'text/css; charset=utf-8',
|
|
76
|
+
json: 'application/json; charset=utf-8',
|
|
77
|
+
svg: 'image/svg+xml',
|
|
78
|
+
png: 'image/png',
|
|
79
|
+
jpg: 'image/jpeg',
|
|
80
|
+
jpeg: 'image/jpeg',
|
|
81
|
+
webp: 'image/webp',
|
|
82
|
+
ico: 'image/x-icon',
|
|
83
|
+
woff: 'font/woff',
|
|
84
|
+
woff2: 'font/woff2',
|
|
85
|
+
wasm: 'application/wasm',
|
|
86
|
+
});
|
|
87
|
+
export { WORKER_MANIFEST_PATH };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const WORKER_MANIFEST_PATH = "worker/manifest.json";
|
|
2
|
+
export declare function artifactPath(input: unknown, label: string): string;
|
|
3
|
+
export declare function assetPath(input: unknown): string;
|
|
4
|
+
export declare function comparePath(left: string, right: string): number;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const WORKER_MANIFEST_PATH = 'worker/manifest.json';
|
|
2
|
+
export function artifactPath(input, label) {
|
|
3
|
+
if (typeof input !== 'string' ||
|
|
4
|
+
input.length === 0 ||
|
|
5
|
+
input.startsWith('/') ||
|
|
6
|
+
input.includes('\\') ||
|
|
7
|
+
input.split('/').some((segment) => segment === '' || segment === '.' || segment === '..')) {
|
|
8
|
+
throw new TypeError(`${label} must be a canonical relative POSIX path.`);
|
|
9
|
+
}
|
|
10
|
+
return input;
|
|
11
|
+
}
|
|
12
|
+
export function assetPath(input) {
|
|
13
|
+
if (typeof input !== 'string' ||
|
|
14
|
+
!input.startsWith('/') ||
|
|
15
|
+
input.length > 1024 ||
|
|
16
|
+
input.includes('\\') ||
|
|
17
|
+
Array.from(input).some((character) => character.codePointAt(0) <= 0x1f) ||
|
|
18
|
+
input.split('/').some((segment) => segment === '.' || segment === '..')) {
|
|
19
|
+
throw new TypeError('Worker asset path is invalid.');
|
|
20
|
+
}
|
|
21
|
+
return input;
|
|
22
|
+
}
|
|
23
|
+
export function comparePath(left, right) {
|
|
24
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
25
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Generate the isolate-startup entry that realizes the heavy Service on first request. */
|
|
2
|
+
export function generateExecutionEntry(serviceSpecifier, workerSpecifier = '@astrale-os/adapter-cloudflare/worker') {
|
|
3
|
+
return `// AUTO-GENERATED by @astrale-os/adapter-cloudflare — do not edit.
|
|
4
|
+
import type { CloudflareWorker } from ${JSON.stringify(workerSpecifier)}
|
|
5
|
+
|
|
6
|
+
const SERVICE_SPECIFIER = ${JSON.stringify(serviceSpecifier)}
|
|
7
|
+
let servicePromise: Promise<CloudflareWorker<CloudflareEnv>> | undefined
|
|
8
|
+
|
|
9
|
+
export default {
|
|
10
|
+
async fetch(request: Request, environment: CloudflareEnv, context: ExecutionContext) {
|
|
11
|
+
return (await service()).fetch(request, environment, context)
|
|
12
|
+
},
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function service(): Promise<CloudflareWorker<CloudflareEnv>> {
|
|
16
|
+
return (servicePromise ??= import(SERVICE_SPECIFIER).then((loaded: unknown) => {
|
|
17
|
+
if (loaded === null || typeof loaded !== 'object') invalid()
|
|
18
|
+
const worker = Reflect.get(loaded, 'default') as unknown
|
|
19
|
+
if (worker === null || typeof worker !== 'object' || typeof Reflect.get(worker, 'fetch') !== 'function') invalid()
|
|
20
|
+
return worker as CloudflareWorker<CloudflareEnv>
|
|
21
|
+
}))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function invalid(): never {
|
|
25
|
+
throw new TypeError('Cloudflare execution Service module is invalid.')
|
|
26
|
+
}
|
|
27
|
+
`;
|
|
28
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Build } from '@astrale-os/sdk/deployment/build';
|
|
2
|
+
import type { ExchangeOptions } from '@astrale-os/sdk/execution';
|
|
3
|
+
export interface ExecutionCodegenOptions {
|
|
4
|
+
readonly origin: string;
|
|
5
|
+
readonly runtimeSpecifier: string;
|
|
6
|
+
readonly workerSpecifier?: string;
|
|
7
|
+
readonly build: Build;
|
|
8
|
+
readonly maximumRequestBytes?: number;
|
|
9
|
+
readonly invocationTimeoutMs?: number;
|
|
10
|
+
readonly providerAssignedWorker?: string;
|
|
11
|
+
readonly tokenExchange?: ExchangeOptions | false;
|
|
12
|
+
readonly router?: {
|
|
13
|
+
readonly binding: string;
|
|
14
|
+
readonly hostSuffix: string;
|
|
15
|
+
readonly minLabels: number;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export declare function generateExecutionService(options: ExecutionCodegenOptions): string;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { generatedMaterial } from '@astrale-os/sdk/deployment/build';
|
|
2
|
+
export function generateExecutionService(options) {
|
|
3
|
+
const workerSpecifier = options.workerSpecifier ?? '@astrale-os/adapter-cloudflare/worker';
|
|
4
|
+
const generated = generatedMaterial(options.build);
|
|
5
|
+
const router = options.router;
|
|
6
|
+
const routerPredicate = router === undefined
|
|
7
|
+
? ''
|
|
8
|
+
: `
|
|
9
|
+
function isInstanceHost(host: string): boolean {
|
|
10
|
+
return host.endsWith(${JSON.stringify(router.hostSuffix)}) && host.split('.').length >= ${router.minLabels}
|
|
11
|
+
}
|
|
12
|
+
`;
|
|
13
|
+
const routeSubrequest = router === undefined
|
|
14
|
+
? ''
|
|
15
|
+
: `
|
|
16
|
+
routeSubrequest: (url, env) => {
|
|
17
|
+
const labels = url.hostname.split('.')
|
|
18
|
+
if (env.DISPATCHER && labels[1] === 'svc') return env.DISPATCHER.get(labels[0]!)
|
|
19
|
+
if (env.${router.binding} && isInstanceHost(url.hostname)) return env.${router.binding}
|
|
20
|
+
return null
|
|
21
|
+
},`;
|
|
22
|
+
const frontend = generated.material.frontend === undefined
|
|
23
|
+
? ''
|
|
24
|
+
: ` frontend: ${JSON.stringify(generated.material.frontend)} as const satisfies NonNullable<BuildMaterial['frontend']>,\n`;
|
|
25
|
+
return `// AUTO-GENERATED by @astrale-os/adapter-cloudflare — do not edit.
|
|
26
|
+
// Domain origin: ${options.origin}
|
|
27
|
+
import type { BuildMaterial, GeneratedBuildMaterial } from '@astrale-os/sdk/deployment/build'
|
|
28
|
+
import type { CloudflareAdapterBindings } from ${JSON.stringify(workerSpecifier)}
|
|
29
|
+
|
|
30
|
+
import { generatedCloudflareWorkerEntry } from ${JSON.stringify(workerSpecifier)}
|
|
31
|
+
import { decodeSigningIdentity, runtimeBindings } from ${JSON.stringify(workerSpecifier)}
|
|
32
|
+
|
|
33
|
+
type WorkerEnv = CloudflareEnv & CloudflareAdapterBindings & {
|
|
34
|
+
readonly ASTRALE_SIGNING_IDENTITY: string
|
|
35
|
+
}
|
|
36
|
+
type RuntimeEnv = Omit<WorkerEnv, 'ASTRALE_SIGNING_IDENTITY'>
|
|
37
|
+
${routerPredicate}
|
|
38
|
+
export default generatedCloudflareWorkerEntry<WorkerEnv, RuntimeEnv>()({
|
|
39
|
+
${frontend} load: async () => ({
|
|
40
|
+
runtime: (await import(${JSON.stringify(options.runtimeSpecifier)})).default,
|
|
41
|
+
material: ${JSON.stringify(generated)} as unknown as GeneratedBuildMaterial,
|
|
42
|
+
}),
|
|
43
|
+
privateKey: (env) => decodeSigningIdentity(env.ASTRALE_SIGNING_IDENTITY),
|
|
44
|
+
resolveEnvironment: runtimeBindings,
|
|
45
|
+
resolveUrl: (env, requestOrigin) => env.WORKER_URL ?? env.IDENTITY_ISS ?? requestOrigin,
|
|
46
|
+
${options.maximumRequestBytes === undefined ? '' : `maximumRequestBytes: ${options.maximumRequestBytes},`}
|
|
47
|
+
${options.invocationTimeoutMs === undefined ? '' : `invocationTimeoutMs: ${options.invocationTimeoutMs},`}
|
|
48
|
+
${options.providerAssignedWorker === undefined ? '' : `providerAssignedWorker: ${JSON.stringify(options.providerAssignedWorker)},`}
|
|
49
|
+
${options.tokenExchange === undefined ? '' : `tokenExchange: ${JSON.stringify(options.tokenExchange)} as const,`}
|
|
50
|
+
selfBinding: (env) => env.SELF,${routeSubrequest}
|
|
51
|
+
})
|
|
52
|
+
`;
|
|
53
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PrepareContext } from '@astrale-os/sdk/deployment/adapter';
|
|
2
|
+
import type { CloudflareParams } from '../../params.js';
|
|
3
|
+
import type { resolveRouter } from '../configuration.js';
|
|
4
|
+
export declare const RELEASE_ENTRY = "bundle/index.mjs";
|
|
5
|
+
export declare const RELEASE_SERVICE = "bundle/service.mjs";
|
|
6
|
+
export declare function prepareExecution(input: {
|
|
7
|
+
readonly params: CloudflareParams;
|
|
8
|
+
readonly context: PrepareContext;
|
|
9
|
+
readonly directory: string;
|
|
10
|
+
readonly name: string;
|
|
11
|
+
readonly direct: boolean;
|
|
12
|
+
readonly configuredIssuer?: string;
|
|
13
|
+
readonly router: ReturnType<typeof resolveRouter>;
|
|
14
|
+
}): Promise<{
|
|
15
|
+
readonly entry: Uint8Array;
|
|
16
|
+
readonly service: Uint8Array;
|
|
17
|
+
}>;
|
|
18
|
+
export declare function runtimeVars(params: CloudflareParams, configuredIssuer: string | undefined): Record<string, string>;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { dirname, join, relative, resolve } from 'node:path';
|
|
3
|
+
import { SIGNING_IDENTITY_BINDING } from '../../identity/binding.js';
|
|
4
|
+
import { runWranglerBundle } from '../../wrangler/bundle.js';
|
|
5
|
+
import { admitWorkerBundle } from '../bundle/admit.js';
|
|
6
|
+
import { generateExecutionEntry, generateExecutionService } from '../codegen/execution.js';
|
|
7
|
+
import { generateWranglerConfig } from '../codegen/wrangler.js';
|
|
8
|
+
import { workerInvocationTimeout, workerRequestLimit } from '../configuration.js';
|
|
9
|
+
import { qualifyWorkerBundle } from '../qualification.js';
|
|
10
|
+
import { workerModuleSpecifier } from './specifier.js';
|
|
11
|
+
export const RELEASE_ENTRY = 'bundle/index.mjs';
|
|
12
|
+
export const RELEASE_SERVICE = 'bundle/service.mjs';
|
|
13
|
+
export async function prepareExecution(input) {
|
|
14
|
+
const runtimePath = resolve(input.context.projectDir, input.context.runtime.reference.path);
|
|
15
|
+
const runtimeSpecifier = moduleSpecifier(relative(input.directory, runtimePath));
|
|
16
|
+
const workerSpecifier = workerModuleSpecifier();
|
|
17
|
+
const maximumRequestBytes = workerRequestLimit(input.params.maximumRequestBytes);
|
|
18
|
+
const invocationTimeoutMs = workerInvocationTimeout(input.params.invocationTimeoutMs);
|
|
19
|
+
const serviceSource = generateExecutionService({
|
|
20
|
+
origin: input.context.build.schema.compiled.root.origin,
|
|
21
|
+
runtimeSpecifier,
|
|
22
|
+
workerSpecifier,
|
|
23
|
+
build: input.context.build,
|
|
24
|
+
...(maximumRequestBytes === undefined ? {} : { maximumRequestBytes }),
|
|
25
|
+
...(invocationTimeoutMs === undefined ? {} : { invocationTimeoutMs }),
|
|
26
|
+
...(input.params.tokenExchange === undefined
|
|
27
|
+
? {}
|
|
28
|
+
: { tokenExchange: input.params.tokenExchange }),
|
|
29
|
+
...(input.configuredIssuer === undefined && input.direct
|
|
30
|
+
? { providerAssignedWorker: input.name }
|
|
31
|
+
: {}),
|
|
32
|
+
...(input.router === undefined
|
|
33
|
+
? {}
|
|
34
|
+
: {
|
|
35
|
+
router: {
|
|
36
|
+
binding: input.router.binding,
|
|
37
|
+
hostSuffix: input.router.hostSuffix,
|
|
38
|
+
minLabels: input.router.minLabels,
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
const serviceSourcePath = join(input.directory, 'service.gen.ts');
|
|
43
|
+
await writeFile(serviceSourcePath, serviceSource);
|
|
44
|
+
const serviceConfigPath = join(input.directory, 'wrangler.service.prepare.jsonc');
|
|
45
|
+
await writeFile(serviceConfigPath, generateWranglerConfig({
|
|
46
|
+
workerName: input.name,
|
|
47
|
+
main: './service.gen.ts',
|
|
48
|
+
vars: runtimeVars(input.params, input.configuredIssuer),
|
|
49
|
+
secretBindings: [SIGNING_IDENTITY_BINDING],
|
|
50
|
+
...(input.router === undefined
|
|
51
|
+
? {}
|
|
52
|
+
: { router: { binding: input.router.binding, service: input.router.service } }),
|
|
53
|
+
...(input.params.wrangler === undefined ? {} : { wrangler: input.params.wrangler }),
|
|
54
|
+
selfBinding: false,
|
|
55
|
+
}));
|
|
56
|
+
const serviceOutput = join(input.directory, 'dist-service');
|
|
57
|
+
await rm(serviceOutput, { recursive: true, force: true });
|
|
58
|
+
const emittedService = await runWranglerBundle({
|
|
59
|
+
projectDir: input.context.projectDir,
|
|
60
|
+
configPath: serviceConfigPath,
|
|
61
|
+
outDir: serviceOutput,
|
|
62
|
+
});
|
|
63
|
+
await qualifyWorkerBundle({
|
|
64
|
+
metafilePath: emittedService.metafilePath,
|
|
65
|
+
inputRoot: dirname(serviceConfigPath),
|
|
66
|
+
});
|
|
67
|
+
await admitWorkerBundle(emittedService.bundlePath);
|
|
68
|
+
const service = new Uint8Array(await readFile(emittedService.bundlePath));
|
|
69
|
+
await mkdir(dirname(join(input.directory, RELEASE_SERVICE)), { recursive: true });
|
|
70
|
+
await writeFile(join(input.directory, RELEASE_SERVICE), service);
|
|
71
|
+
const sourcePath = join(input.directory, 'worker.gen.ts');
|
|
72
|
+
await writeFile(sourcePath, generateExecutionEntry('./service.mjs', workerSpecifier));
|
|
73
|
+
const configPath = join(input.directory, 'wrangler.prepare.jsonc');
|
|
74
|
+
await writeFile(configPath, generateWranglerConfig({
|
|
75
|
+
workerName: input.name,
|
|
76
|
+
...(input.params.route === undefined ? {} : { route: input.params.route }),
|
|
77
|
+
vars: runtimeVars(input.params, input.configuredIssuer),
|
|
78
|
+
secretBindings: [SIGNING_IDENTITY_BINDING],
|
|
79
|
+
modules: { baseDir: '.', rules: [{ type: 'ESModule', globs: [RELEASE_SERVICE] }] },
|
|
80
|
+
...(input.router === undefined
|
|
81
|
+
? {}
|
|
82
|
+
: { router: { binding: input.router.binding, service: input.router.service } }),
|
|
83
|
+
...(input.params.wrangler === undefined ? {} : { wrangler: input.params.wrangler }),
|
|
84
|
+
selfBinding: false,
|
|
85
|
+
}));
|
|
86
|
+
const output = join(input.directory, 'dist-prepare');
|
|
87
|
+
await rm(output, { recursive: true, force: true });
|
|
88
|
+
const emitted = await runWranglerBundle({
|
|
89
|
+
projectDir: input.context.projectDir,
|
|
90
|
+
configPath,
|
|
91
|
+
outDir: output,
|
|
92
|
+
});
|
|
93
|
+
await qualifyWorkerBundle({
|
|
94
|
+
metafilePath: emitted.metafilePath,
|
|
95
|
+
inputRoot: dirname(configPath),
|
|
96
|
+
});
|
|
97
|
+
await admitWorkerBundle(emitted.bundlePath);
|
|
98
|
+
return Object.freeze({
|
|
99
|
+
entry: new Uint8Array(await readFile(emitted.bundlePath)),
|
|
100
|
+
service,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
export function runtimeVars(params, configuredIssuer) {
|
|
104
|
+
return {
|
|
105
|
+
...params.vars,
|
|
106
|
+
...(configuredIssuer === undefined ? {} : { WORKER_URL: configuredIssuer }),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function moduleSpecifier(input) {
|
|
110
|
+
const normalized = input.split('\\').join('/');
|
|
111
|
+
return normalized.startsWith('.') ? normalized : `./${normalized}`;
|
|
112
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { loadWorkerArtifact } from './artifact.load.js';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { WorkerArtifact } from './artifact.js';
|
|
2
|
+
type WranglerModuleType = 'ESModule' | 'CompiledWasm' | 'Text' | 'Data';
|
|
3
|
+
/** Project one admitted Worker module inventory into Wrangler's relative module discovery. */
|
|
4
|
+
export declare function workerModules(worker: WorkerArtifact['release']): {
|
|
5
|
+
readonly baseDir: string;
|
|
6
|
+
readonly rules: readonly {
|
|
7
|
+
readonly type: WranglerModuleType;
|
|
8
|
+
readonly globs: readonly string[];
|
|
9
|
+
}[];
|
|
10
|
+
};
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { posix } from 'node:path';
|
|
2
|
+
const TYPES = Object.freeze({
|
|
3
|
+
esm: 'ESModule',
|
|
4
|
+
wasm: 'CompiledWasm',
|
|
5
|
+
text: 'Text',
|
|
6
|
+
data: 'Data',
|
|
7
|
+
});
|
|
8
|
+
/** Project one admitted Worker module inventory into Wrangler's relative module discovery. */
|
|
9
|
+
export function workerModules(worker) {
|
|
10
|
+
const directory = posix.dirname(worker.entrypoint);
|
|
11
|
+
const groups = new Map();
|
|
12
|
+
for (const module of worker.modules) {
|
|
13
|
+
if (module.path === worker.entrypoint)
|
|
14
|
+
continue;
|
|
15
|
+
const relative = posix.relative(directory, module.path);
|
|
16
|
+
if (relative === '' || relative === '..' || relative.startsWith('../')) {
|
|
17
|
+
throw new TypeError('Cloudflare Worker module escapes its entrypoint directory.');
|
|
18
|
+
}
|
|
19
|
+
const type = TYPES[module.kind];
|
|
20
|
+
const globs = groups.get(type) ?? [];
|
|
21
|
+
globs.push(relative);
|
|
22
|
+
groups.set(type, globs);
|
|
23
|
+
}
|
|
24
|
+
const rules = [...groups].map(([type, globs]) => Object.freeze({ type, globs: Object.freeze(globs) }));
|
|
25
|
+
return Object.freeze({ baseDir: `./${directory}`, rules: Object.freeze(rules) });
|
|
26
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface WorkerRuntimeConfiguration {
|
|
2
|
+
readonly compatibilityDate: string;
|
|
3
|
+
readonly compatibilityFlags: readonly string[];
|
|
4
|
+
readonly vars: Readonly<Record<string, string>>;
|
|
5
|
+
readonly services: readonly {
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly service: string;
|
|
8
|
+
}[];
|
|
9
|
+
}
|
|
10
|
+
export declare function admitWorkerRuntimeConfiguration(input: string): WorkerRuntimeConfiguration;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export function admitWorkerRuntimeConfiguration(input) {
|
|
2
|
+
const value = JSON.parse(input);
|
|
3
|
+
const compatibilityDate = value.compatibility_date;
|
|
4
|
+
const compatibilityFlags = value.compatibility_flags;
|
|
5
|
+
const vars = value.vars ?? {};
|
|
6
|
+
const services = value.services ?? [];
|
|
7
|
+
if (typeof compatibilityDate !== 'string' ||
|
|
8
|
+
!Array.isArray(compatibilityFlags) ||
|
|
9
|
+
compatibilityFlags.some((flag) => typeof flag !== 'string') ||
|
|
10
|
+
vars === null ||
|
|
11
|
+
typeof vars !== 'object' ||
|
|
12
|
+
Array.isArray(vars) ||
|
|
13
|
+
Object.values(vars).some((item) => typeof item !== 'string') ||
|
|
14
|
+
!Array.isArray(services)) {
|
|
15
|
+
invalidRuntime();
|
|
16
|
+
}
|
|
17
|
+
const admittedServices = services.map((input) => {
|
|
18
|
+
if (input === null || typeof input !== 'object' || Array.isArray(input))
|
|
19
|
+
invalidRuntime();
|
|
20
|
+
const binding = Reflect.get(input, 'binding');
|
|
21
|
+
const service = Reflect.get(input, 'service');
|
|
22
|
+
if (typeof binding !== 'string' || typeof service !== 'string')
|
|
23
|
+
invalidRuntime();
|
|
24
|
+
return Object.freeze({ name: binding, service });
|
|
25
|
+
});
|
|
26
|
+
return Object.freeze({
|
|
27
|
+
compatibilityDate,
|
|
28
|
+
compatibilityFlags: Object.freeze([...compatibilityFlags]),
|
|
29
|
+
vars: Object.freeze({ ...vars }),
|
|
30
|
+
services: Object.freeze(admittedServices),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function invalidRuntime() {
|
|
34
|
+
throw new TypeError('Generated Cloudflare runtime configuration is invalid.');
|
|
35
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function workerModuleSpecifier(): string;
|
package/dist/cloudflare.d.ts
CHANGED
|
@@ -1,37 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* `cloudflare(envs)` — the Cloudflare deployment adapter.
|
|
3
|
-
*
|
|
4
|
-
* Owns everything target-specific: it codegens the Worker entry + wrangler
|
|
5
|
-
* config (the dev never sees a `wrangler.jsonc`), builds the optional client
|
|
6
|
-
* SPA, and shells out to `wrangler`. `watch` → `wrangler dev` (local URL);
|
|
7
|
-
* `deploy` → `wrangler deploy` (workers.dev or a custom-domain URL); secrets →
|
|
8
|
-
* `wrangler secret bulk`. Both `watch` and `deploy` return the URL the CLI
|
|
9
|
-
* prints and `astrale domain install <url>` consumes.
|
|
10
|
-
*/
|
|
11
|
-
import type { DeployCtx, DomainAdapter, WatchCtx } from '@astrale-os/sdk';
|
|
12
|
-
import type { CloudflareParams } from './params';
|
|
13
|
-
export declare function cloudflare(envs: Record<string, CloudflareParams>): DomainAdapter<CloudflareParams>;
|
|
14
|
-
export declare function prepare(params: CloudflareParams, ctx: WatchCtx | DeployCtx, mode: 'dev' | 'deploy', clientAssets?: {
|
|
15
|
-
servesClient: boolean;
|
|
16
|
-
bundleAssets: boolean;
|
|
17
|
-
}): Promise<{
|
|
18
|
-
configPath: string;
|
|
19
|
-
fallbackConfigPath?: string;
|
|
20
|
-
port: number;
|
|
21
|
-
workerName: string;
|
|
22
|
-
}>;
|
|
23
|
-
export declare function workerName(params: CloudflareParams, origin: string): string;
|
|
24
|
-
/**
|
|
25
|
-
* Resolve the instance-router config. ON by default (targets `admin-router`) so
|
|
26
|
-
* every adapter-deployed worker can reach other instances' hosts internally and
|
|
27
|
-
* verify cross-instance credentials without a 522; `false` disables it. The
|
|
28
|
-
* returned shape carries every value the worker- and wrangler-codegen need.
|
|
29
|
-
*/
|
|
30
|
-
export declare function resolveRouter(ir: CloudflareParams['router']): {
|
|
31
|
-
binding: string;
|
|
32
|
-
service: string;
|
|
33
|
-
hostSuffix: string;
|
|
34
|
-
minLabels: number;
|
|
35
|
-
} | undefined;
|
|
36
|
-
export declare function logTo(): (line: string) => void;
|
|
37
|
-
//# sourceMappingURL=cloudflare.d.ts.map
|
|
1
|
+
export { cloudflare } from './adapter.js';
|