@astrale-os/adapter-cloudflare 0.5.0-beta.20 → 0.5.0-beta.201
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 +245 -0
- package/dist/adapter.d.ts +9 -15
- package/dist/adapter.js +43 -129
- package/dist/build/bundle/admit.d.ts +3 -0
- package/dist/build/bundle/admit.js +47 -0
- package/dist/build/bundle/compiled-wasm-loader.d.ts +8 -0
- package/dist/build/bundle/compiled-wasm-loader.js +21 -0
- package/dist/build/bundler.d.ts +21 -0
- package/dist/build/bundler.js +96 -0
- package/dist/build/closure.d.ts +4 -1
- package/dist/build/closure.js +7 -1
- package/dist/build/codegen/execution.d.ts +3 -13
- package/dist/build/codegen/execution.js +2 -44
- package/dist/build/codegen/wrangler.d.ts +28 -10
- package/dist/build/codegen/wrangler.js +71 -21
- package/dist/build/configuration/invocation-timeout.d.ts +2 -0
- package/dist/build/configuration/invocation-timeout.js +9 -0
- package/dist/build/configuration/request-limit.d.ts +2 -0
- package/dist/build/configuration/request-limit.js +9 -0
- package/dist/build/configuration/router.d.ts +7 -0
- package/dist/build/configuration/router.js +17 -0
- package/dist/build/configuration/settings.d.ts +6 -0
- package/dist/build/configuration/settings.js +40 -0
- package/dist/build/configuration.d.ts +4 -9
- package/dist/build/configuration.js +4 -40
- package/dist/build/frontend/files.d.ts +19 -0
- package/dist/build/frontend/files.js +99 -0
- package/dist/build/frontend/readiness.d.ts +16 -0
- package/dist/build/frontend/readiness.js +21 -0
- package/dist/build/frontend/revision.d.ts +5 -0
- package/dist/build/frontend/revision.js +14 -0
- package/dist/build/frontend/vite.d.ts +14 -0
- package/dist/build/frontend/vite.js +42 -0
- package/dist/build/frontend/vite.process.d.ts +7 -0
- package/dist/build/frontend/vite.process.js +65 -0
- package/dist/build/frontend/vite.runner.d.ts +1 -0
- package/dist/build/frontend/vite.runner.js +155 -0
- package/dist/build/index.d.ts +10 -6
- package/dist/build/index.js +10 -6
- package/dist/build/material.d.ts +10 -0
- package/dist/build/material.js +25 -0
- package/dist/build/worker/codegen/entry.d.ts +9 -0
- package/dist/build/worker/codegen/entry.js +65 -0
- package/dist/build/worker/codegen/literal.d.ts +28 -0
- package/dist/build/worker/codegen/literal.js +136 -0
- package/dist/build/worker/codegen/service.d.ts +14 -0
- package/dist/build/worker/codegen/service.js +57 -0
- package/dist/build/worker/execution.prepare.d.ts +20 -0
- package/dist/build/worker/execution.prepare.js +129 -0
- package/dist/build/worker/media-type.d.ts +1 -0
- package/dist/build/worker/media-type.js +20 -0
- package/dist/build/worker/modules.d.ts +20 -0
- package/dist/build/worker/modules.js +32 -0
- package/dist/build/worker/specifier.d.ts +5 -0
- package/dist/build/worker/specifier.js +15 -0
- package/dist/build/worker/upload.d.ts +23 -0
- package/dist/build/worker/upload.js +42 -0
- package/dist/hosting/activation.d.ts +38 -0
- package/dist/hosting/activation.js +109 -0
- package/dist/hosting/assets.d.ts +14 -0
- package/dist/hosting/assets.js +53 -0
- package/dist/hosting/bounded.d.ts +10 -0
- package/dist/hosting/bounded.js +32 -0
- package/dist/hosting/bytes.d.ts +6 -0
- package/dist/hosting/bytes.js +25 -0
- package/dist/hosting/client.d.ts +144 -0
- package/dist/hosting/client.js +491 -0
- package/dist/hosting/dispatcher/cron.d.mts +10 -0
- package/dist/hosting/dispatcher/cron.mjs +135 -0
- package/dist/hosting/dispatcher/deployments.d.mts +26 -0
- package/dist/hosting/dispatcher/deployments.mjs +158 -0
- package/dist/hosting/dispatcher/dispatcher.d.mts +17 -0
- package/dist/hosting/dispatcher/dispatcher.mjs +76 -0
- package/dist/hosting/dispatcher/env.d.mts +51 -0
- package/dist/hosting/dispatcher/failure.d.mts +4 -0
- package/dist/hosting/dispatcher/failure.mjs +15 -0
- package/dist/hosting/dispatcher/index.d.mts +4 -0
- package/dist/hosting/dispatcher/index.mjs +8 -0
- package/dist/hosting/dispatcher/modules.d.ts +24 -0
- package/dist/hosting/dispatcher/modules.js +55 -0
- package/dist/hosting/dispatcher/services.d.mts +23 -0
- package/dist/hosting/dispatcher/services.mjs +202 -0
- package/dist/hosting/identity.d.ts +15 -0
- package/dist/hosting/identity.js +36 -0
- package/dist/hosting/index.d.ts +34 -0
- package/dist/hosting/index.js +32 -0
- package/dist/hosting/kv.d.ts +90 -0
- package/dist/hosting/kv.js +207 -0
- package/dist/hosting/listing.d.ts +34 -0
- package/dist/hosting/listing.js +50 -0
- package/dist/hosting/names.d.ts +16 -0
- package/dist/hosting/names.js +28 -0
- package/dist/hosting/provision/account.d.ts +11 -0
- package/dist/hosting/provision/account.js +86 -0
- package/dist/hosting/provision/dispatcher.d.ts +42 -0
- package/dist/hosting/provision/dispatcher.js +99 -0
- package/dist/hosting/provision/ingress.d.ts +12 -0
- package/dist/hosting/provision/ingress.js +31 -0
- package/dist/hosting/provision/plan.d.ts +143 -0
- package/dist/hosting/provision/plan.js +246 -0
- package/dist/hosting/provision/routing.d.ts +82 -0
- package/dist/hosting/provision/routing.js +226 -0
- package/dist/hosting/readiness.d.ts +53 -0
- package/dist/hosting/readiness.js +82 -0
- package/dist/hosting/refusal.d.ts +28 -0
- package/dist/hosting/refusal.js +13 -0
- package/dist/hosting/retirement.d.ts +70 -0
- package/dist/hosting/retirement.js +107 -0
- package/dist/hosting/retry-after.d.ts +6 -0
- package/dist/hosting/retry-after.js +55 -0
- package/dist/hosting/script.d.ts +186 -0
- package/dist/hosting/script.js +324 -0
- package/dist/hosting/testing/fake-cloudflare.d.ts +212 -0
- package/dist/hosting/testing/fake-cloudflare.js +757 -0
- package/dist/hosting/testing/index.d.ts +6 -0
- package/dist/hosting/testing/index.js +6 -0
- package/dist/hosting/time.d.ts +4 -0
- package/dist/hosting/time.js +20 -0
- package/dist/identity/binding.d.ts +1 -1
- package/dist/identity/binding.js +1 -1
- package/dist/identity/material.d.ts +1 -2
- package/dist/identity/material.js +5 -18
- package/dist/index.d.ts +14 -12
- package/dist/index.js +13 -11
- package/dist/namespace/configure.d.ts +22 -0
- package/dist/namespace/configure.js +199 -0
- package/dist/namespace/coordinates.d.ts +15 -0
- package/dist/namespace/coordinates.js +49 -0
- package/dist/namespace/deploy-release.d.ts +53 -0
- package/dist/namespace/deploy-release.js +389 -0
- package/dist/namespace/hosting.d.ts +29 -0
- package/dist/namespace/hosting.js +120 -0
- package/dist/namespace/index.d.ts +12 -0
- package/dist/namespace/index.js +12 -0
- package/dist/namespace/list.d.ts +18 -0
- package/dist/namespace/list.js +63 -0
- package/dist/namespace/readiness.d.ts +30 -0
- package/dist/namespace/readiness.js +31 -0
- package/dist/namespace/record.d.ts +21 -0
- package/dist/namespace/record.js +53 -0
- package/dist/params.d.ts +60 -76
- package/dist/release/build.d.ts +35 -0
- package/dist/release/build.js +44 -0
- package/dist/release/deployment.d.ts +44 -0
- package/dist/release/deployment.js +141 -0
- package/dist/release/index.d.ts +2 -2
- package/dist/release/index.js +2 -2
- package/dist/worker/binary.d.ts +21 -0
- package/dist/worker/binary.js +31 -0
- package/dist/worker/build-digest.d.ts +10 -0
- package/dist/worker/build-digest.js +22 -0
- package/dist/worker/cache.d.ts +28 -0
- package/dist/worker/cache.js +70 -0
- package/dist/worker/durable.d.ts +3 -0
- package/dist/worker/durable.js +5 -0
- package/dist/worker/entry.d.ts +38 -25
- package/dist/worker/entry.js +87 -109
- package/dist/worker/front.d.ts +56 -0
- package/dist/worker/front.js +59 -0
- package/dist/worker/frontend.d.ts +4 -0
- package/dist/worker/frontend.js +83 -0
- package/dist/worker/index.d.ts +4 -1
- package/dist/worker/index.js +4 -1
- package/dist/worker/restate.d.ts +6 -0
- package/dist/worker/restate.js +6 -0
- package/dist/worker/settings.d.ts +47 -0
- package/dist/worker/settings.js +163 -0
- package/dist/wrangler/bundle.d.ts +10 -0
- package/dist/wrangler/bundle.js +27 -0
- package/dist/wrangler/index.d.ts +3 -0
- package/dist/wrangler/index.js +3 -0
- package/dist/wrangler/process.d.ts +15 -0
- package/dist/wrangler/process.js +113 -0
- package/dist/wrangler/type-generation.d.ts +6 -0
- package/dist/wrangler/type-generation.js +19 -0
- package/package.json +39 -12
- package/template/astrale.config.ts +42 -36
- package/dist/build/artifact.d.ts +0 -8
- package/dist/build/artifact.js +0 -179
- package/dist/build/codegen/gateway.d.ts +0 -1
- package/dist/build/codegen/gateway.js +0 -7
- package/dist/build/frontend-v1.d.ts +0 -16
- package/dist/build/frontend-v1.js +0 -133
- package/dist/build/parse-output.d.ts +0 -26
- package/dist/build/parse-output.js +0 -57
- package/dist/build/wrangler-cli.d.ts +0 -105
- package/dist/build/wrangler-cli.js +0 -448
- package/dist/deploy/deploy.d.ts +0 -17
- package/dist/deploy/deploy.js +0 -207
- package/dist/deploy/index.d.ts +0 -1
- package/dist/deploy/index.js +0 -1
- package/dist/identity/secret.d.ts +0 -3
- package/dist/identity/secret.js +0 -7
- package/dist/identity/source.d.ts +0 -26
- package/dist/identity/source.js +0 -98
- package/dist/release/namespace.d.ts +0 -2
- package/dist/release/namespace.js +0 -6
- package/dist/release/target.d.ts +0 -2
- package/dist/release/target.js +0 -9
- package/dist/worker/gateway.d.ts +0 -15
- package/dist/worker/gateway.js +0 -26
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Emit one JavaScript literal that shares every structurally identical subtree.
|
|
3
|
+
*
|
|
4
|
+
* Build material reaches a Worker as an inline literal, and JSON has no references, so a flat
|
|
5
|
+
* literal repeats every repeated subtree. A compiled Domain is dominated by pattern automata, and
|
|
6
|
+
* a handful of distinct expressions account for dozens of identical copies of one automaton, so
|
|
7
|
+
* the flat form allocates an order of magnitude more objects than the value actually contains.
|
|
8
|
+
*
|
|
9
|
+
* Sharing changes nothing an observer can see. The literal evaluates to a value that serialises
|
|
10
|
+
* identically, so the material digest taken over it is unchanged, and the material is deeply
|
|
11
|
+
* frozen before anything reads it, so no holder can observe the aliasing.
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* An object literal assigns the prototype for a plain `__proto__` key, where a computed key
|
|
15
|
+
* defines an own property, as JSON does.
|
|
16
|
+
*/
|
|
17
|
+
function memberKey(key) {
|
|
18
|
+
return key === '__proto__' ? `[${JSON.stringify(key)}]:` : `${JSON.stringify(key)}:`;
|
|
19
|
+
}
|
|
20
|
+
export function sharedLiteral(value, prefix = '$m') {
|
|
21
|
+
const subtrees = [];
|
|
22
|
+
const references = [];
|
|
23
|
+
const byShape = new Map();
|
|
24
|
+
const byIdentity = new WeakMap();
|
|
25
|
+
/** A piece list, plus the shape text that decides whether two subtrees are the same one. */
|
|
26
|
+
const encode = (input) => {
|
|
27
|
+
const pieces = [];
|
|
28
|
+
const shape = [];
|
|
29
|
+
const open = Array.isArray(input) ? '[' : '{';
|
|
30
|
+
pieces.push(open);
|
|
31
|
+
shape.push(open);
|
|
32
|
+
const keys = Array.isArray(input) ? undefined : Object.keys(input);
|
|
33
|
+
const values = Array.isArray(input)
|
|
34
|
+
? input
|
|
35
|
+
: (keys ?? []).map((key) => input[key]);
|
|
36
|
+
for (const [index, element] of values.entries()) {
|
|
37
|
+
if (index > 0) {
|
|
38
|
+
pieces.push(',');
|
|
39
|
+
shape.push(',');
|
|
40
|
+
}
|
|
41
|
+
if (keys !== undefined) {
|
|
42
|
+
const member = memberKey(keys[index] ?? '');
|
|
43
|
+
pieces.push(member);
|
|
44
|
+
shape.push(member);
|
|
45
|
+
}
|
|
46
|
+
if (element !== null && typeof element === 'object') {
|
|
47
|
+
const id = identify(element);
|
|
48
|
+
pieces.push({ subtree: id });
|
|
49
|
+
// Quoted JSON text can never start with `#`, so a reference is unambiguous here.
|
|
50
|
+
shape.push(`#${id}`);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const text = JSON.stringify(element);
|
|
54
|
+
if (text === undefined) {
|
|
55
|
+
throw new TypeError(`Build material cannot hold a ${typeof element} value.`);
|
|
56
|
+
}
|
|
57
|
+
pieces.push(text);
|
|
58
|
+
shape.push(text);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const close = Array.isArray(input) ? ']' : '}';
|
|
62
|
+
pieces.push(close);
|
|
63
|
+
shape.push(close);
|
|
64
|
+
return { pieces, shape: shape.join('') };
|
|
65
|
+
};
|
|
66
|
+
const identify = (input) => {
|
|
67
|
+
const known = byIdentity.get(input);
|
|
68
|
+
if (known !== undefined) {
|
|
69
|
+
references[known] += 1;
|
|
70
|
+
return known;
|
|
71
|
+
}
|
|
72
|
+
const { pieces, shape } = encode(input);
|
|
73
|
+
const existing = byShape.get(shape);
|
|
74
|
+
if (existing !== undefined) {
|
|
75
|
+
byIdentity.set(input, existing);
|
|
76
|
+
references[existing] += 1;
|
|
77
|
+
return existing;
|
|
78
|
+
}
|
|
79
|
+
const id = subtrees.length;
|
|
80
|
+
subtrees.push({ pieces });
|
|
81
|
+
references.push(1);
|
|
82
|
+
byShape.set(shape, id);
|
|
83
|
+
byIdentity.set(input, id);
|
|
84
|
+
return id;
|
|
85
|
+
};
|
|
86
|
+
if (value === null || typeof value !== 'object') {
|
|
87
|
+
const text = JSON.stringify(value);
|
|
88
|
+
if (text === undefined)
|
|
89
|
+
throw new TypeError('Build material cannot hold an undefined value.');
|
|
90
|
+
return { declarations: [], expression: text };
|
|
91
|
+
}
|
|
92
|
+
const root = identify(value);
|
|
93
|
+
references[root] -= 1; // The root is the expression itself, not a nested reference.
|
|
94
|
+
// A nested subtree always carries a lower identifier than the one holding it, so ascending
|
|
95
|
+
// order declares every name before its first use.
|
|
96
|
+
const declarations = [];
|
|
97
|
+
const names = new Map();
|
|
98
|
+
const inlined = new Map();
|
|
99
|
+
for (const [id, subtree] of subtrees.entries()) {
|
|
100
|
+
const text = subtree.pieces
|
|
101
|
+
.map((piece) => typeof piece === 'string'
|
|
102
|
+
? piece
|
|
103
|
+
: (names.get(piece.subtree) ?? inlined.get(piece.subtree) ?? ''))
|
|
104
|
+
.join('');
|
|
105
|
+
if (id !== root && (references[id] ?? 0) > 1) {
|
|
106
|
+
const name = `${prefix}${id}`;
|
|
107
|
+
names.set(id, name);
|
|
108
|
+
declarations.push(`const ${name} = ${text}`);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
inlined.set(id, text);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return { declarations, expression: names.get(root) ?? inlined.get(root) ?? 'null' };
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Refuse a literal that does not evaluate back to the value it was built from.
|
|
118
|
+
*
|
|
119
|
+
* Emitting source is the one part of sharing that a type cannot check, so the generator proves its
|
|
120
|
+
* own output instead: a defect becomes a failed build rather than a Worker that boots with the
|
|
121
|
+
* wrong material. The comparison is the serialised form, which is also what the material digest
|
|
122
|
+
* is taken over.
|
|
123
|
+
*/
|
|
124
|
+
export function verifySharedLiteral(literal, expected) {
|
|
125
|
+
const source = `${literal.declarations.join('\n')}\nreturn ${literal.expression}`;
|
|
126
|
+
let produced;
|
|
127
|
+
try {
|
|
128
|
+
produced = new Function(source)();
|
|
129
|
+
}
|
|
130
|
+
catch (cause) {
|
|
131
|
+
throw new TypeError('Generated Build material literal is not valid JavaScript.', { cause });
|
|
132
|
+
}
|
|
133
|
+
if (JSON.stringify(produced) !== JSON.stringify(expected)) {
|
|
134
|
+
throw new TypeError('Generated Build material literal does not reproduce its material.');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Build } from '@astrale-os/sdk/deployment/build';
|
|
2
|
+
/** Build inputs only: the deployment binds every environment value (`deploymentSettings`). */
|
|
3
|
+
export interface ExecutionCodegenOptions {
|
|
4
|
+
readonly origin: string;
|
|
5
|
+
readonly runtimeSpecifier: string;
|
|
6
|
+
readonly workerSpecifier?: string;
|
|
7
|
+
readonly build: Build;
|
|
8
|
+
readonly frontendRevision?: string;
|
|
9
|
+
readonly frontendAddressed?: readonly string[];
|
|
10
|
+
/** Static Schema capability; deployment options remain in bindings. */
|
|
11
|
+
readonly durable?: boolean;
|
|
12
|
+
readonly restateSpecifier?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function generateExecutionService(options: ExecutionCodegenOptions): string;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { generatedMaterial } from '@astrale-os/sdk/deployment/build';
|
|
2
|
+
import { sharedLiteral, verifySharedLiteral } from './literal.js';
|
|
3
|
+
export function generateExecutionService(options) {
|
|
4
|
+
const workerSpecifier = options.workerSpecifier ?? '@astrale-os/adapter-cloudflare/worker';
|
|
5
|
+
const generated = generatedMaterial(options.build);
|
|
6
|
+
// JSON has no references, so a flat literal repeats every repeated subtree. A compiled Domain
|
|
7
|
+
// is dominated by pattern automata, of which a few expressions account for dozens of identical
|
|
8
|
+
// copies, so sharing them cuts what the isolate allocates and seals by an order of magnitude.
|
|
9
|
+
const material = sharedLiteral(generated);
|
|
10
|
+
verifySharedLiteral(material, generated);
|
|
11
|
+
const materialDeclarations = material.declarations.map((line) => ` ${line}\n`).join('');
|
|
12
|
+
const durableImports = options.durable === true
|
|
13
|
+
? `import { restateServing } from ${JSON.stringify(workerSpecifier)}
|
|
14
|
+
import { isCanonicalDurableRequest } from '@astrale-os/sdk/execution'
|
|
15
|
+
// A variable specifier preserves the engine as a separate deployed module.
|
|
16
|
+
const RESTATE_SPECIFIER = ${JSON.stringify(options.restateSpecifier ?? './restate.mjs')}
|
|
17
|
+
`
|
|
18
|
+
: '';
|
|
19
|
+
const frontend = generated.material.frontend === undefined
|
|
20
|
+
? ''
|
|
21
|
+
: ` frontend: ${JSON.stringify(generated.material.frontend)} as const satisfies NonNullable<BuildMaterial['frontend']>,\n`;
|
|
22
|
+
// Keep adapter types on the resolved value import. A standalone absolute type import leaks
|
|
23
|
+
// checkout characters into esbuild's minifier histogram, making identical Services differ.
|
|
24
|
+
return `// AUTO-GENERATED by @astrale-os/adapter-cloudflare — do not edit.
|
|
25
|
+
// Domain origin: ${options.origin}
|
|
26
|
+
import type { BuildMaterial, GeneratedBuildMaterial } from '@astrale-os/sdk/deployment/build'
|
|
27
|
+
|
|
28
|
+
import { generatedCloudflareWorkerEntry, type CloudflareAdapterBindings } from ${JSON.stringify(workerSpecifier)}
|
|
29
|
+
import { decodeSigningIdentity, deploymentSettings, runtimeBindings } from ${JSON.stringify(workerSpecifier)}
|
|
30
|
+
|
|
31
|
+
${durableImports}
|
|
32
|
+
type WorkerEnv = CloudflareEnv & CloudflareAdapterBindings & {
|
|
33
|
+
readonly ASTRALE_SIGNING_IDENTITY: string
|
|
34
|
+
}
|
|
35
|
+
type RuntimeEnv = Omit<WorkerEnv, 'ASTRALE_SIGNING_IDENTITY'>
|
|
36
|
+
|
|
37
|
+
export default generatedCloudflareWorkerEntry<WorkerEnv, RuntimeEnv>()({
|
|
38
|
+
${frontend}${options.frontendRevision === undefined ? '' : ` frontendRevision: ${JSON.stringify(options.frontendRevision)},\n`}${options.frontendAddressed === undefined ? '' : ` frontendAddressed: ${JSON.stringify(options.frontendAddressed)},\n`} load: async () => {
|
|
39
|
+
${materialDeclarations} return {
|
|
40
|
+
runtime: (await import(${JSON.stringify(options.runtimeSpecifier)})).default,
|
|
41
|
+
material: ${material.expression} as unknown as GeneratedBuildMaterial,
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
privateKey: (env) => decodeSigningIdentity(env.ASTRALE_SIGNING_IDENTITY),
|
|
45
|
+
resolveEnvironment: runtimeBindings,
|
|
46
|
+
settings: deploymentSettings,
|
|
47
|
+
selfBinding: (env) => env.SELF,${options.durable === true
|
|
48
|
+
? `
|
|
49
|
+
durable: async (env, request, options) => {
|
|
50
|
+
if (!isCanonicalDurableRequest(request)) return undefined
|
|
51
|
+
const restate = await import(RESTATE_SPECIFIER)
|
|
52
|
+
return restateServing(restate, request, options)
|
|
53
|
+
},`
|
|
54
|
+
: ''}
|
|
55
|
+
})
|
|
56
|
+
`;
|
|
57
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { BundleContext } from '@astrale-os/sdk/deployment/adapter';
|
|
2
|
+
export declare const RELEASE_ENTRY = "bundle/index.mjs";
|
|
3
|
+
export declare const RELEASE_SERVICE = "bundle/service.mjs";
|
|
4
|
+
export declare const RELEASE_RESTATE = "bundle/restate.mjs";
|
|
5
|
+
/** A deployed module retained outside the startup bundle. */
|
|
6
|
+
export interface ReleaseModule {
|
|
7
|
+
readonly kind: 'esm' | 'wasm';
|
|
8
|
+
readonly path: string;
|
|
9
|
+
readonly content: Uint8Array;
|
|
10
|
+
}
|
|
11
|
+
export declare function prepareExecution(input: {
|
|
12
|
+
readonly context: BundleContext;
|
|
13
|
+
readonly directory: string;
|
|
14
|
+
readonly frontendRevision?: string;
|
|
15
|
+
readonly frontendAddressed?: readonly string[];
|
|
16
|
+
}): Promise<{
|
|
17
|
+
readonly entry: Uint8Array;
|
|
18
|
+
readonly modules: readonly ReleaseModule[];
|
|
19
|
+
}>;
|
|
20
|
+
export declare function declaresDurable(build: BundleContext['build']): boolean;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { generatedMaterial } from '@astrale-os/sdk/deployment/build';
|
|
2
|
+
import { DURABLE_ENGINE_EXPORTS } from '@astrale-os/sdk/execution';
|
|
3
|
+
import { mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { dirname, join, relative, resolve } from 'node:path';
|
|
5
|
+
import { SIGNING_IDENTITY_BINDING } from '../../identity/binding.js';
|
|
6
|
+
import { runWranglerBundle } from '../../wrangler/bundle.js';
|
|
7
|
+
import { admitDurableEngineBundle, admitWorkerBundle } from '../bundle/admit.js';
|
|
8
|
+
import { generateExecutionEntry, generateExecutionService } from '../codegen/execution.js';
|
|
9
|
+
import { generateWranglerConfig } from '../codegen/wrangler.js';
|
|
10
|
+
import { qualifyWorkerBundle } from '../qualification.js';
|
|
11
|
+
import { workerModules } from './modules.js';
|
|
12
|
+
import { frontModuleSpecifier, restateModuleSpecifier, workerModuleSpecifier } from './specifier.js';
|
|
13
|
+
export const RELEASE_ENTRY = 'bundle/index.mjs';
|
|
14
|
+
export const RELEASE_SERVICE = 'bundle/service.mjs';
|
|
15
|
+
export const RELEASE_RESTATE = 'bundle/restate.mjs';
|
|
16
|
+
/**
|
|
17
|
+
* Bundling sees the adapter base configuration only: no Environment, route, var, router or
|
|
18
|
+
* `wrangler` overlay reaches the bytes, so every Environment of one source bundles identically.
|
|
19
|
+
*/
|
|
20
|
+
const BUNDLE_WORKER_NAME = 'astrale-domain';
|
|
21
|
+
export async function prepareExecution(input) {
|
|
22
|
+
const runtimePath = resolve(input.context.projectDir, input.context.runtime.reference.path);
|
|
23
|
+
const runtimeSpecifier = moduleSpecifier(relative(input.directory, runtimePath));
|
|
24
|
+
const workerSpecifier = workerModuleSpecifier();
|
|
25
|
+
const serviceSource = generateExecutionService({
|
|
26
|
+
origin: input.context.build.schema.compiled.root.origin,
|
|
27
|
+
runtimeSpecifier,
|
|
28
|
+
workerSpecifier,
|
|
29
|
+
build: input.context.build,
|
|
30
|
+
...(declaresDurable(input.context.build)
|
|
31
|
+
? { durable: true, restateSpecifier: './restate.mjs' }
|
|
32
|
+
: {}),
|
|
33
|
+
...(input.frontendRevision === undefined ? {} : { frontendRevision: input.frontendRevision }),
|
|
34
|
+
...(input.frontendAddressed === undefined
|
|
35
|
+
? {}
|
|
36
|
+
: { frontendAddressed: input.frontendAddressed }),
|
|
37
|
+
});
|
|
38
|
+
const modules = [];
|
|
39
|
+
const bundle = async (name, path, source, admit) => {
|
|
40
|
+
const sourcePath = join(input.directory, `${name}.gen.ts`);
|
|
41
|
+
await writeFile(sourcePath, source);
|
|
42
|
+
const configPath = join(input.directory, `wrangler.${name}.prepare.jsonc`);
|
|
43
|
+
await writeFile(configPath, generateWranglerConfig({
|
|
44
|
+
workerName: BUNDLE_WORKER_NAME,
|
|
45
|
+
main: `./${name}.gen.ts`,
|
|
46
|
+
secretBindings: [SIGNING_IDENTITY_BINDING],
|
|
47
|
+
selfBinding: false,
|
|
48
|
+
}));
|
|
49
|
+
const output = join(input.directory, `dist-${name}`);
|
|
50
|
+
await rm(output, { recursive: true, force: true });
|
|
51
|
+
const emitted = await runWranglerBundle({
|
|
52
|
+
projectDir: input.context.projectDir,
|
|
53
|
+
configPath,
|
|
54
|
+
outDir: output,
|
|
55
|
+
signal: input.context.signal,
|
|
56
|
+
});
|
|
57
|
+
await qualifyWorkerBundle({
|
|
58
|
+
metafilePath: emitted.metafilePath,
|
|
59
|
+
inputRoot: dirname(configPath),
|
|
60
|
+
});
|
|
61
|
+
await admit(emitted.bundlePath);
|
|
62
|
+
const content = new Uint8Array(await readFile(emitted.bundlePath));
|
|
63
|
+
await mkdir(dirname(join(input.directory, path)), { recursive: true });
|
|
64
|
+
await writeFile(join(input.directory, path), content);
|
|
65
|
+
modules.push(Object.freeze({ path, kind: 'esm', content }));
|
|
66
|
+
// Imported WASM modules are emitted beside their owner under content-hashed names.
|
|
67
|
+
for (const file of (await readdir(output)).filter((name) => name.endsWith('.wasm')).sort()) {
|
|
68
|
+
const wasmPath = `${dirname(path)}/${file}`;
|
|
69
|
+
const content = new Uint8Array(await readFile(join(output, file)));
|
|
70
|
+
await writeFile(join(input.directory, wasmPath), content);
|
|
71
|
+
if (!modules.some((module) => module.path === wasmPath)) {
|
|
72
|
+
modules.push(Object.freeze({ path: wasmPath, kind: 'wasm', content }));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
await bundle('service', RELEASE_SERVICE, serviceSource, admitWorkerBundle);
|
|
77
|
+
if (declaresDurable(input.context.build)) {
|
|
78
|
+
await bundle('restate', RELEASE_RESTATE,
|
|
79
|
+
// Wrangler requires a default export to classify an independently bundled ESM entry.
|
|
80
|
+
`export { ${DURABLE_ENGINE_EXPORTS.join(', ')} } from ${JSON.stringify(restateModuleSpecifier())}\nexport default {}\n`, admitDurableEngineBundle);
|
|
81
|
+
}
|
|
82
|
+
const sourcePath = join(input.directory, 'worker.gen.ts');
|
|
83
|
+
const frontend = generatedMaterial(input.context.build).material.frontend;
|
|
84
|
+
await writeFile(sourcePath, generateExecutionEntry('./service.mjs', {
|
|
85
|
+
frontSpecifier: frontModuleSpecifier(),
|
|
86
|
+
...(frontend === undefined ? {} : { frontend }),
|
|
87
|
+
...(input.frontendRevision === undefined ? {} : { frontendRevision: input.frontendRevision }),
|
|
88
|
+
...(input.frontendAddressed === undefined
|
|
89
|
+
? {}
|
|
90
|
+
: { frontendAddressed: input.frontendAddressed }),
|
|
91
|
+
}));
|
|
92
|
+
const configPath = join(input.directory, 'wrangler.prepare.jsonc');
|
|
93
|
+
await writeFile(configPath, generateWranglerConfig({
|
|
94
|
+
workerName: BUNDLE_WORKER_NAME,
|
|
95
|
+
secretBindings: [SIGNING_IDENTITY_BINDING],
|
|
96
|
+
modules: workerModules({ entrypoint: 'worker.gen.ts', modules }),
|
|
97
|
+
selfBinding: false,
|
|
98
|
+
}));
|
|
99
|
+
const output = join(input.directory, 'dist-prepare');
|
|
100
|
+
await rm(output, { recursive: true, force: true });
|
|
101
|
+
const emitted = await runWranglerBundle({
|
|
102
|
+
projectDir: input.context.projectDir,
|
|
103
|
+
configPath,
|
|
104
|
+
outDir: output,
|
|
105
|
+
signal: input.context.signal,
|
|
106
|
+
});
|
|
107
|
+
await qualifyWorkerBundle({
|
|
108
|
+
metafilePath: emitted.metafilePath,
|
|
109
|
+
inputRoot: dirname(configPath),
|
|
110
|
+
});
|
|
111
|
+
await admitWorkerBundle(emitted.bundlePath);
|
|
112
|
+
return Object.freeze({
|
|
113
|
+
entry: new Uint8Array(await readFile(emitted.bundlePath)),
|
|
114
|
+
modules: Object.freeze(modules),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
function moduleSpecifier(input) {
|
|
118
|
+
const normalized = input.split('\\').join('/');
|
|
119
|
+
return normalized.startsWith('.') ? normalized : `./${normalized}`;
|
|
120
|
+
}
|
|
121
|
+
export function declaresDurable(build) {
|
|
122
|
+
const compiled = build.schema.compiled;
|
|
123
|
+
const root = compiled.closure.find(({ origin }) => origin === compiled.root.origin);
|
|
124
|
+
if (root === undefined)
|
|
125
|
+
return false;
|
|
126
|
+
return (root.definitions.functions.some((definition) => definition.execution?.durable === true) ||
|
|
127
|
+
root.definitions.classes.some((definition) => 'methods' in definition &&
|
|
128
|
+
definition.methods.some((method) => method.execution?.durable === true)));
|
|
129
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function inferMediaType(path: string): string | undefined;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function inferMediaType(path) {
|
|
2
|
+
const extension = path.split('.').at(-1)?.toLowerCase();
|
|
3
|
+
return extension === undefined ? undefined : MEDIA_TYPES[extension];
|
|
4
|
+
}
|
|
5
|
+
const MEDIA_TYPES = Object.freeze({
|
|
6
|
+
html: 'text/html; charset=utf-8',
|
|
7
|
+
js: 'text/javascript; charset=utf-8',
|
|
8
|
+
mjs: 'text/javascript; charset=utf-8',
|
|
9
|
+
css: 'text/css; charset=utf-8',
|
|
10
|
+
json: 'application/json; charset=utf-8',
|
|
11
|
+
svg: 'image/svg+xml',
|
|
12
|
+
png: 'image/png',
|
|
13
|
+
jpg: 'image/jpeg',
|
|
14
|
+
jpeg: 'image/jpeg',
|
|
15
|
+
webp: 'image/webp',
|
|
16
|
+
ico: 'image/x-icon',
|
|
17
|
+
woff: 'font/woff',
|
|
18
|
+
woff2: 'font/woff2',
|
|
19
|
+
wasm: 'application/wasm',
|
|
20
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { WorkerModuleKind } from '@astrale-os/sdk/deployment/worker';
|
|
2
|
+
type WranglerModuleType = 'ESModule' | 'CompiledWasm' | 'Text' | 'Data';
|
|
3
|
+
/**
|
|
4
|
+
* Project one admitted Worker module inventory into Wrangler's relative module discovery. Rules
|
|
5
|
+
* follow module kinds, so the projection does not depend on the inventory's order.
|
|
6
|
+
*/
|
|
7
|
+
export declare function workerModules(worker: {
|
|
8
|
+
readonly entrypoint: string;
|
|
9
|
+
readonly modules: readonly {
|
|
10
|
+
readonly path: string;
|
|
11
|
+
readonly kind: WorkerModuleKind;
|
|
12
|
+
}[];
|
|
13
|
+
}): {
|
|
14
|
+
readonly baseDir: string;
|
|
15
|
+
readonly rules: readonly {
|
|
16
|
+
readonly type: WranglerModuleType;
|
|
17
|
+
readonly globs: readonly string[];
|
|
18
|
+
}[];
|
|
19
|
+
};
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
/**
|
|
9
|
+
* Project one admitted Worker module inventory into Wrangler's relative module discovery. Rules
|
|
10
|
+
* follow module kinds, so the projection does not depend on the inventory's order.
|
|
11
|
+
*/
|
|
12
|
+
export function workerModules(worker) {
|
|
13
|
+
const directory = posix.dirname(worker.entrypoint);
|
|
14
|
+
const groups = new Map();
|
|
15
|
+
for (const module of worker.modules) {
|
|
16
|
+
if (module.path === worker.entrypoint)
|
|
17
|
+
continue;
|
|
18
|
+
const relative = posix.relative(directory, module.path);
|
|
19
|
+
if (relative === '' || relative === '..' || relative.startsWith('../')) {
|
|
20
|
+
throw new TypeError('Cloudflare Worker module escapes its entrypoint directory.');
|
|
21
|
+
}
|
|
22
|
+
const type = TYPES[module.kind];
|
|
23
|
+
const globs = groups.get(type) ?? [];
|
|
24
|
+
globs.push(relative);
|
|
25
|
+
groups.set(type, globs);
|
|
26
|
+
}
|
|
27
|
+
const rules = Object.values(TYPES).flatMap((type) => {
|
|
28
|
+
const globs = groups.get(type);
|
|
29
|
+
return globs === undefined ? [] : [Object.freeze({ type, globs: Object.freeze(globs) })];
|
|
30
|
+
});
|
|
31
|
+
return Object.freeze({ baseDir: `./${directory}`, rules: Object.freeze(rules) });
|
|
32
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function workerModuleSpecifier(): string;
|
|
2
|
+
/** The isolate-startup module, which must stay free of the execution Service's dependencies. */
|
|
3
|
+
export declare function frontModuleSpecifier(): string;
|
|
4
|
+
/** The engine's Workers build, resolved from this adapter so a Domain needs no engine dependency. */
|
|
5
|
+
export declare function restateModuleSpecifier(): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { fileURLToPath } from 'node:url';
|
|
2
|
+
const workerModule = import.meta.resolve('@astrale-os/adapter-cloudflare/worker');
|
|
3
|
+
const restateModule = import.meta.resolve('@astrale-os/adapter-cloudflare/worker/restate');
|
|
4
|
+
const frontModule = import.meta.resolve('@astrale-os/adapter-cloudflare/worker/front');
|
|
5
|
+
export function workerModuleSpecifier() {
|
|
6
|
+
return fileURLToPath(workerModule);
|
|
7
|
+
}
|
|
8
|
+
/** The isolate-startup module, which must stay free of the execution Service's dependencies. */
|
|
9
|
+
export function frontModuleSpecifier() {
|
|
10
|
+
return fileURLToPath(frontModule);
|
|
11
|
+
}
|
|
12
|
+
/** The engine's Workers build, resolved from this adapter so a Domain needs no engine dependency. */
|
|
13
|
+
export function restateModuleSpecifier() {
|
|
14
|
+
return fileURLToPath(restateModule);
|
|
15
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Bundle } from '@astrale-os/sdk/deployment/adapter';
|
|
2
|
+
import { type WorkerUpload } from '../../release/build.js';
|
|
3
|
+
/**
|
|
4
|
+
* The portable upload of one Bundle of the Cloudflare Worker bundler, as an immutable deployment's
|
|
5
|
+
* host receives it: the entry and the modules it loads (ES modules and their WASM), and the
|
|
6
|
+
* frontend as Worker assets with the media type their extension names. Its build digest is the Bundle's own, so the host that
|
|
7
|
+
* recomputes it from these bytes finds exactly the build the release names; any other Bundle is
|
|
8
|
+
* refused.
|
|
9
|
+
*/
|
|
10
|
+
export declare function bundleUpload(bundle: Bundle): BundleUpload;
|
|
11
|
+
/** The Worker upload of one Bundle, with each module's kind and each asset's media type. */
|
|
12
|
+
export interface BundleUpload extends WorkerUpload {
|
|
13
|
+
readonly modules: readonly {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly kind: 'esm' | 'wasm';
|
|
16
|
+
readonly bytes: Uint8Array;
|
|
17
|
+
}[];
|
|
18
|
+
readonly assets?: readonly {
|
|
19
|
+
readonly path: string;
|
|
20
|
+
readonly bytes: Uint8Array;
|
|
21
|
+
readonly contentType?: string;
|
|
22
|
+
}[];
|
|
23
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { buildDigest } from '@astrale-os/sdk/deployment/build/manifest';
|
|
2
|
+
import { workerBuildManifest } from '../../release/build.js';
|
|
3
|
+
import { bundledWorker } from '../bundler.js';
|
|
4
|
+
import { RELEASE_ENTRY } from './execution.prepare.js';
|
|
5
|
+
import { inferMediaType } from './media-type.js';
|
|
6
|
+
/**
|
|
7
|
+
* The portable upload of one Bundle of the Cloudflare Worker bundler, as an immutable deployment's
|
|
8
|
+
* host receives it: the entry and the modules it loads (ES modules and their WASM), and the
|
|
9
|
+
* frontend as Worker assets with the media type their extension names. Its build digest is the Bundle's own, so the host that
|
|
10
|
+
* recomputes it from these bytes finds exactly the build the release names; any other Bundle is
|
|
11
|
+
* refused.
|
|
12
|
+
*/
|
|
13
|
+
export function bundleUpload(bundle) {
|
|
14
|
+
const worker = bundledWorker(bundle, bundle.build);
|
|
15
|
+
const root = bundle.build.schema.compiled.root;
|
|
16
|
+
const upload = Object.freeze({
|
|
17
|
+
origin: root.origin,
|
|
18
|
+
revision: root.revision,
|
|
19
|
+
bundle: bundle.manifest.schema.bundle,
|
|
20
|
+
entry: RELEASE_ENTRY,
|
|
21
|
+
modules: Object.freeze([
|
|
22
|
+
Object.freeze({ name: RELEASE_ENTRY, kind: 'esm', bytes: worker.entry }),
|
|
23
|
+
...worker.modules.map((module) => Object.freeze({ name: module.path, kind: module.kind, bytes: module.content })),
|
|
24
|
+
]),
|
|
25
|
+
...(worker.frontend.length === 0
|
|
26
|
+
? {}
|
|
27
|
+
: {
|
|
28
|
+
assets: Object.freeze(worker.frontend.map((file) => {
|
|
29
|
+
const contentType = inferMediaType(file.path);
|
|
30
|
+
return Object.freeze({
|
|
31
|
+
path: `/${file.path}`,
|
|
32
|
+
bytes: file.bytes(),
|
|
33
|
+
...(contentType === undefined ? {} : { contentType }),
|
|
34
|
+
});
|
|
35
|
+
})),
|
|
36
|
+
}),
|
|
37
|
+
});
|
|
38
|
+
if (buildDigest(workerBuildManifest(upload)) !== bundle.digest) {
|
|
39
|
+
throw new TypeError('Worker upload of the Bundle does not reproduce its build digest.');
|
|
40
|
+
}
|
|
41
|
+
return upload;
|
|
42
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { WorkerRecord } from '@astrale-os/sdk/deployment/worker';
|
|
2
|
+
import type { RoutingKv } from './kv.js';
|
|
3
|
+
/**
|
|
4
|
+
* Activate one deployment: write `record:<label>`, which the dispatcher reads to route the label,
|
|
5
|
+
* with the record its deployer publishes (opaque bytes and their media type, served verbatim at
|
|
6
|
+
* `/.well-known/astrale/deployment.json`), or `null` for a deployment that publishes none. A
|
|
7
|
+
* record never changes once published: one already there is kept and answered when it is the same
|
|
8
|
+
* record (same media type, same bytes, or both none), and refused (`deployment-conflict`)
|
|
9
|
+
* otherwise, without any write.
|
|
10
|
+
*/
|
|
11
|
+
export declare function activateDeployment(kv: RoutingKv, label: string, record: WorkerRecord | null): Promise<WorkerRecord | null>;
|
|
12
|
+
/**
|
|
13
|
+
* The record a label was activated with: the record, `null` when the deployment publishes none,
|
|
14
|
+
* or undefined when the label is not activated. A value whose metadata is not exactly
|
|
15
|
+
* `{"contentType":<media type or null>}` (none at all included) is no record the kit writes, and
|
|
16
|
+
* is refused (`record-invalid`) rather than read as any record.
|
|
17
|
+
*/
|
|
18
|
+
export declare function readRecord(kv: RoutingKv, label: string): Promise<WorkerRecord | null | undefined>;
|
|
19
|
+
/** Whether two activations publish the same record (or both none). */
|
|
20
|
+
export declare function sameRecord(left: WorkerRecord | null, right: WorkerRecord | null): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Point a name (a deployment line) at the deployment its URL `https://<name>.<routing domain>`
|
|
23
|
+
* serves: `current:<name>` = `label`, or no deployment when `label` is null (the key is deleted).
|
|
24
|
+
* The label must be one of the name's own. Writing the same value again writes nothing, so a rerun
|
|
25
|
+
* converges.
|
|
26
|
+
*/
|
|
27
|
+
export declare function promoteDeployment(kv: RoutingKv, name: string, label: string | null): Promise<void>;
|
|
28
|
+
/** The deployment a name serves now, or null. */
|
|
29
|
+
export declare function readCurrent(kv: RoutingKv, name: string): Promise<string | null>;
|
|
30
|
+
/**
|
|
31
|
+
* Replace a name's crons with canonical UTC crons (`cron:<name>`), or delete them when the set is
|
|
32
|
+
* empty. A set that is not canonical is refused (`schedules-invalid`) before anything is written.
|
|
33
|
+
* The dispatcher fires a due cron on the name's current deployment, once its upload bound a
|
|
34
|
+
* `cron` trigger.
|
|
35
|
+
*/
|
|
36
|
+
export declare function replaceSchedules(kv: RoutingKv, name: string, crons: readonly string[]): Promise<readonly string[]>;
|
|
37
|
+
/** A name's crons: empty when it has none; a stored set that is not canonical is refused. */
|
|
38
|
+
export declare function readSchedules(kv: RoutingKv, name: string): Promise<readonly string[]>;
|