@astrale-os/adapter-cloudflare 0.5.0-beta.1 → 0.5.0-beta.101
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 +1211 -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/build/codegen/merge.d.ts +0 -1
- package/dist/build/codegen/merge.js +0 -1
- package/dist/build/codegen/wrangler.d.ts +13 -1
- package/dist/build/codegen/wrangler.js +35 -18
- 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 -16
- package/dist/build/index.js +7 -16
- package/dist/build/materialize.d.ts +4 -0
- package/dist/build/materialize.js +10 -0
- package/dist/build/metafile.d.ts +5 -0
- package/dist/build/metafile.js +14 -0
- package/dist/build/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 -34
- package/dist/cloudflare.js +1 -205
- 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 +5 -5
- package/dist/index.js +5 -5
- package/dist/params.d.ts +37 -7
- 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 -48
- package/dist/worker/index.js +2 -186
- 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 +34 -19
- package/template/astrale.config.ts +25 -17
- 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 +0 -27
- package/dist/build/parse-output.d.ts.map +0 -1
- package/dist/build/parse-output.js +0 -58
- 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 +0 -86
- package/dist/build/wrangler-cli.d.ts.map +0 -1
- package/dist/build/wrangler-cli.js +0 -275
- 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 -278
- package/src/worker/tsconfig.json +0 -10
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { addressing, assemble } from '@astrale-os/sdk/deployment/release';
|
|
2
|
+
import { mkdir, mkdtemp, rm } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { workerName } from '../build/configuration.js';
|
|
5
|
+
import { admitDirectPlacement } from '../build/direct.js';
|
|
6
|
+
import { loadWorkerArtifact } from '../build/worker/index.js';
|
|
7
|
+
import { signingIdentitySecrets } from '../identity/secret.js';
|
|
8
|
+
import { WranglerDeployError } from '../wrangler/deploy.js';
|
|
9
|
+
const NONE = Object.freeze({ kind: 'none' });
|
|
10
|
+
/** Atomically advance one stable Worker address to the prepared implementation. */
|
|
11
|
+
export async function deployCloudflare(parameters, context, dependencies) {
|
|
12
|
+
let secrets;
|
|
13
|
+
try {
|
|
14
|
+
const identity = await dependencies.signingIdentity({
|
|
15
|
+
projectDir: context.projectDir,
|
|
16
|
+
source: parameters.signingIdentity,
|
|
17
|
+
origin: context.artifact.build.schema.compiled.root.origin,
|
|
18
|
+
mode: 'deploy',
|
|
19
|
+
});
|
|
20
|
+
secrets = signingIdentitySecrets(context.secrets, identity);
|
|
21
|
+
}
|
|
22
|
+
catch (cause) {
|
|
23
|
+
return failed('prerequisite', progress({}), cause);
|
|
24
|
+
}
|
|
25
|
+
let worker;
|
|
26
|
+
try {
|
|
27
|
+
worker = loadWorkerArtifact(context.artifact);
|
|
28
|
+
for (const name of worker.bindings.requiredSecrets) {
|
|
29
|
+
if (!Object.hasOwn(secrets, name)) {
|
|
30
|
+
throw new TypeError(`Cloudflare Worker requires secret ${name}.`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
admitDirectPlacement(context.artifact, worker, parameters);
|
|
34
|
+
}
|
|
35
|
+
catch (cause) {
|
|
36
|
+
return failed('prerequisite', progress({}), cause);
|
|
37
|
+
}
|
|
38
|
+
const stateDirectory = join(context.projectDir, '.astrale');
|
|
39
|
+
await mkdir(stateDirectory, { recursive: true });
|
|
40
|
+
const directory = await mkdtemp(join(stateDirectory, 'deploy-'));
|
|
41
|
+
try {
|
|
42
|
+
await dependencies.materialize(directory, context.artifact.files);
|
|
43
|
+
const name = workerName(parameters, context.artifact.build.schema.compiled.root.origin);
|
|
44
|
+
const configPath = join(directory, 'wrangler.jsonc');
|
|
45
|
+
let deployed;
|
|
46
|
+
try {
|
|
47
|
+
deployed = await dependencies.runWranglerDeploy({
|
|
48
|
+
projectDir: context.projectDir,
|
|
49
|
+
configPath,
|
|
50
|
+
secrets,
|
|
51
|
+
...(parameters.route === undefined ? {} : { route: parameters.route }),
|
|
52
|
+
signal: context.signal,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
catch (cause) {
|
|
56
|
+
return failed('provider', progress({ provider: partialProvider(cause, context.artifact) }), cause);
|
|
57
|
+
}
|
|
58
|
+
const configuration = Object.freeze({
|
|
59
|
+
kind: 'applied',
|
|
60
|
+
evidence: Object.freeze({ names: Object.keys(secrets).sort() }),
|
|
61
|
+
});
|
|
62
|
+
const retained = Object.freeze({
|
|
63
|
+
kind: 'partial',
|
|
64
|
+
evidence: Object.freeze({
|
|
65
|
+
provider: 'cloudflare',
|
|
66
|
+
deployment: deployed.deploymentVersion,
|
|
67
|
+
artifact: context.artifact.digest,
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
let selected;
|
|
71
|
+
try {
|
|
72
|
+
selected = observeAddressing(context.artifact, deployed.url, name);
|
|
73
|
+
}
|
|
74
|
+
catch (cause) {
|
|
75
|
+
return failed('addressing', progress({
|
|
76
|
+
provider: retained,
|
|
77
|
+
configuration,
|
|
78
|
+
}), cause);
|
|
79
|
+
}
|
|
80
|
+
let observed;
|
|
81
|
+
try {
|
|
82
|
+
observed = await dependencies.runWranglerDeploymentStatus({
|
|
83
|
+
projectDir: context.projectDir,
|
|
84
|
+
configPath,
|
|
85
|
+
workerName: name,
|
|
86
|
+
signal: context.signal,
|
|
87
|
+
});
|
|
88
|
+
requireCurrentDeployment(observed, deployed.deploymentVersion);
|
|
89
|
+
}
|
|
90
|
+
catch (cause) {
|
|
91
|
+
return failed('readiness', progress({
|
|
92
|
+
provider: retained,
|
|
93
|
+
configuration,
|
|
94
|
+
}), cause);
|
|
95
|
+
}
|
|
96
|
+
let release;
|
|
97
|
+
try {
|
|
98
|
+
release = assemble(context.artifact.build, selected);
|
|
99
|
+
}
|
|
100
|
+
catch (cause) {
|
|
101
|
+
return failed('release', progress({ provider: retained, configuration }), cause);
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
await dependencies.waitForReadiness(release, context.signal);
|
|
105
|
+
}
|
|
106
|
+
catch (cause) {
|
|
107
|
+
return failed('readiness', progress({ provider: retained, configuration }), cause, release);
|
|
108
|
+
}
|
|
109
|
+
const provider = Object.freeze({
|
|
110
|
+
kind: 'applied',
|
|
111
|
+
evidence: Object.freeze({
|
|
112
|
+
provider: 'cloudflare',
|
|
113
|
+
deployment: deployed.deploymentVersion,
|
|
114
|
+
artifact: context.artifact.digest,
|
|
115
|
+
issuer: selected.issuer,
|
|
116
|
+
}),
|
|
117
|
+
});
|
|
118
|
+
return Object.freeze({
|
|
119
|
+
status: 'ready',
|
|
120
|
+
phase: 'complete',
|
|
121
|
+
progress: Object.freeze({ ...progress({ configuration }), provider }),
|
|
122
|
+
release,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
await rm(directory, { recursive: true, force: true });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
function partialProvider(cause, artifact) {
|
|
130
|
+
if (!(cause instanceof WranglerDeployError) || cause.applied?.deploymentVersion === undefined) {
|
|
131
|
+
return Object.freeze({ kind: 'unknown' });
|
|
132
|
+
}
|
|
133
|
+
let issuer;
|
|
134
|
+
if (cause.applied.url !== undefined) {
|
|
135
|
+
try {
|
|
136
|
+
issuer = addressing(cause.applied.url).issuer;
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// The provider effect remains known when its printed URL is not Addressing.
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return Object.freeze({
|
|
143
|
+
kind: 'partial',
|
|
144
|
+
evidence: Object.freeze({
|
|
145
|
+
provider: 'cloudflare',
|
|
146
|
+
deployment: cause.applied.deploymentVersion,
|
|
147
|
+
artifact: artifact.digest,
|
|
148
|
+
...(issuer === undefined ? {} : { issuer }),
|
|
149
|
+
}),
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
function requireCurrentDeployment(observed, expectedVersion) {
|
|
153
|
+
if (observed.versions.length !== 1 ||
|
|
154
|
+
observed.versions[0].percentage !== 100 ||
|
|
155
|
+
observed.versions[0].version !== expectedVersion) {
|
|
156
|
+
throw new TypeError('Cloudflare Worker is not one exact version at 100%.');
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function observeAddressing(artifact, deployedUrl, expectedWorker) {
|
|
160
|
+
if (artifact.addressing.kind === 'configured') {
|
|
161
|
+
if (deployedUrl === undefined)
|
|
162
|
+
return artifact.addressing.addressing;
|
|
163
|
+
const observed = addressing(deployedUrl);
|
|
164
|
+
if (observed.issuer !== artifact.addressing.addressing.issuer) {
|
|
165
|
+
throw new TypeError('Cloudflare deployed URL differs from configured Addressing.');
|
|
166
|
+
}
|
|
167
|
+
return observed;
|
|
168
|
+
}
|
|
169
|
+
if (deployedUrl === undefined) {
|
|
170
|
+
throw new TypeError('Cloudflare did not report provider-assigned Addressing.');
|
|
171
|
+
}
|
|
172
|
+
const observed = addressing(deployedUrl);
|
|
173
|
+
if (artifact.addressing.claim !== expectedWorker) {
|
|
174
|
+
throw new TypeError('PreparedArtifact worker claim differs from the deployment target.');
|
|
175
|
+
}
|
|
176
|
+
const url = new URL(observed.issuer);
|
|
177
|
+
const labels = url.hostname.split('.');
|
|
178
|
+
if (url.protocol !== 'https:' ||
|
|
179
|
+
labels.length < 4 ||
|
|
180
|
+
labels[0] !== artifact.addressing.claim ||
|
|
181
|
+
labels.at(-2) !== 'workers' ||
|
|
182
|
+
labels.at(-1) !== 'dev') {
|
|
183
|
+
throw new TypeError('Cloudflare workers.dev URL does not satisfy its prepared worker claim.');
|
|
184
|
+
}
|
|
185
|
+
return observed;
|
|
186
|
+
}
|
|
187
|
+
function progress(input) {
|
|
188
|
+
return Object.freeze({
|
|
189
|
+
prerequisite: input.prerequisite ?? NONE,
|
|
190
|
+
provider: input.provider ?? NONE,
|
|
191
|
+
configuration: input.configuration ?? NONE,
|
|
192
|
+
installation: input.installation ?? NONE,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
function failed(phase, progressValue, cause, release) {
|
|
196
|
+
const error = cause instanceof Error ? cause : new Error(String(cause));
|
|
197
|
+
return Object.freeze({
|
|
198
|
+
status: 'failed',
|
|
199
|
+
phase,
|
|
200
|
+
progress: progressValue,
|
|
201
|
+
error,
|
|
202
|
+
...(release === undefined ? {} : { release }),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { deployCloudflare, type CloudflareDeployDependencies } from './deploy.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { deployCloudflare } from './deploy.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const WORKERS_DEV_RE = /(https:\/\/[^\s]+\.workers\.dev)/iu;
|
|
2
|
+
const VERSION_ID_RE = /(?:Current\s+)?Version ID:\s*([a-z0-9_-]+)/iu;
|
|
3
|
+
export function parseDeployUrl(text, route) {
|
|
4
|
+
if (route !== undefined)
|
|
5
|
+
return `https://${route.replace(/\/.*$/u, '')}`;
|
|
6
|
+
return WORKERS_DEV_RE.exec(text)?.[1];
|
|
7
|
+
}
|
|
8
|
+
export function parseDeployVersionId(text) {
|
|
9
|
+
return VERSION_ID_RE.exec(text)?.[1];
|
|
10
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { DevelopmentContext, DevelopmentSession } from '@astrale-os/sdk/deployment/adapter';
|
|
2
|
+
import type { CloudflareParams } from '../params.js';
|
|
3
|
+
import { signingIdentity } from '../identity/source.js';
|
|
4
|
+
import { startFrontend } from './frontend.js';
|
|
5
|
+
import { allocatePort } from './port.js';
|
|
6
|
+
import { verifyDevelopmentRelease } from './readiness.js';
|
|
7
|
+
import { startWorker } from './worker.js';
|
|
8
|
+
export interface CloudflareDevelopmentDependencies {
|
|
9
|
+
readonly signingIdentity: typeof signingIdentity;
|
|
10
|
+
readonly startFrontend: typeof startFrontend;
|
|
11
|
+
readonly startWorker: typeof startWorker;
|
|
12
|
+
readonly allocatePort: typeof allocatePort;
|
|
13
|
+
readonly verify: typeof verifyDevelopmentRelease;
|
|
14
|
+
}
|
|
15
|
+
export declare function developCloudflare(parameters: CloudflareParams, context: DevelopmentContext, dependencies?: CloudflareDevelopmentDependencies): Promise<DevelopmentSession>;
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { addressing, assemble } from '@astrale-os/sdk/deployment/release';
|
|
2
|
+
import { mkdir, rename, rm, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname, join } from 'node:path';
|
|
4
|
+
import { generateWranglerConfig } from '../build/codegen/wrangler.js';
|
|
5
|
+
import { resolveRouter, workerName } from '../build/configuration.js';
|
|
6
|
+
import { loadWorkerArtifact } from '../build/worker/index.js';
|
|
7
|
+
import { workerModules } from '../build/worker/modules.js';
|
|
8
|
+
import { signingIdentitySecrets } from '../identity/secret.js';
|
|
9
|
+
import { signingIdentity } from '../identity/source.js';
|
|
10
|
+
import { startFrontend } from './frontend.js';
|
|
11
|
+
import { admitHost } from './host.js';
|
|
12
|
+
import { allocatePort } from './port.js';
|
|
13
|
+
import { verifyDevelopmentRelease } from './readiness.js';
|
|
14
|
+
import { startWorker } from './worker.js';
|
|
15
|
+
export async function developCloudflare(parameters, context, dependencies = {
|
|
16
|
+
signingIdentity,
|
|
17
|
+
startFrontend,
|
|
18
|
+
startWorker,
|
|
19
|
+
allocatePort,
|
|
20
|
+
verify: verifyDevelopmentRelease,
|
|
21
|
+
}) {
|
|
22
|
+
if (context.overrides.credentialFile !== undefined) {
|
|
23
|
+
throw new TypeError('Cloudflare development does not own Kernel credentials.');
|
|
24
|
+
}
|
|
25
|
+
const hostSource = context.overrides.publicUrl ?? parameters.host;
|
|
26
|
+
const host = hostSource === undefined ? undefined : admitHost(hostSource);
|
|
27
|
+
const configuredPort = context.overrides.localPort ?? parameters.port;
|
|
28
|
+
if (host !== undefined && configuredPort === undefined) {
|
|
29
|
+
throw new TypeError('Cloudflare development with a public host requires a strict local port.');
|
|
30
|
+
}
|
|
31
|
+
const port = configuredPort ?? (await dependencies.allocatePort());
|
|
32
|
+
const directory = join(context.projectDir, '.astrale', 'development', context.environment, 'cloudflare');
|
|
33
|
+
await mkdir(directory, { recursive: true });
|
|
34
|
+
const configPath = join(directory, 'wrangler.development.jsonc');
|
|
35
|
+
const identity = await dependencies.signingIdentity({
|
|
36
|
+
projectDir: context.projectDir,
|
|
37
|
+
source: parameters.signingIdentity,
|
|
38
|
+
origin: context.artifact.build.schema.compiled.root.origin,
|
|
39
|
+
mode: 'local',
|
|
40
|
+
});
|
|
41
|
+
let current = Object.freeze({ artifact: context.artifact, secrets: context.secrets });
|
|
42
|
+
let artifactPaths = new Set();
|
|
43
|
+
let resources;
|
|
44
|
+
let stopping = false;
|
|
45
|
+
let resolveFinished;
|
|
46
|
+
let rejectFinished;
|
|
47
|
+
const finished = new Promise((resolve, reject) => {
|
|
48
|
+
resolveFinished = resolve;
|
|
49
|
+
rejectFinished = reject;
|
|
50
|
+
});
|
|
51
|
+
const realize = async (input) => {
|
|
52
|
+
artifactPaths = await applyArtifact(directory, artifactPaths, input.artifact);
|
|
53
|
+
if (input.artifact.build.frontend?.source.kind === 'vite') {
|
|
54
|
+
await mkdir(join(directory, 'frontend'), { recursive: true });
|
|
55
|
+
}
|
|
56
|
+
const frontend = await dependencies.startFrontend({
|
|
57
|
+
frontend: input.artifact.build.frontend,
|
|
58
|
+
projectDir: context.projectDir,
|
|
59
|
+
});
|
|
60
|
+
try {
|
|
61
|
+
await writeFile(configPath, configuration(parameters, input.artifact, frontend?.url, host, Object.keys(signingIdentitySecrets(input.secrets, identity)).sort()));
|
|
62
|
+
const worker = await dependencies.startWorker({
|
|
63
|
+
projectDir: context.projectDir,
|
|
64
|
+
configPath,
|
|
65
|
+
port,
|
|
66
|
+
remote: parameters.remote ?? false,
|
|
67
|
+
secrets: signingIdentitySecrets(input.secrets, identity),
|
|
68
|
+
autoPickPort: false,
|
|
69
|
+
pinLocalOrigin: host === undefined && parameters.remote !== true,
|
|
70
|
+
...(host === undefined ? {} : { ip: '0.0.0.0' }),
|
|
71
|
+
});
|
|
72
|
+
const value = Object.freeze({ ...(frontend === undefined ? {} : { frontend }), worker });
|
|
73
|
+
supervise(value);
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
catch (cause) {
|
|
77
|
+
await frontend?.stop().catch(() => undefined);
|
|
78
|
+
throw cause;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
const supervise = (selected) => {
|
|
82
|
+
for (const child of [selected.worker.finished, selected.frontend?.finished]) {
|
|
83
|
+
child?.catch((cause) => {
|
|
84
|
+
if (!stopping && resources === selected)
|
|
85
|
+
rejectFinished(cause);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
const placementFor = (selected) => Object.freeze({ releaseUrl: host ?? selected.worker.url, localUrl: selected.worker.url });
|
|
90
|
+
const verify = async (input, selected) => {
|
|
91
|
+
const placement = placementFor(selected);
|
|
92
|
+
const release = assemble(input.artifact.build, addressing(placement.releaseUrl));
|
|
93
|
+
await dependencies.verify({ release, localUrl: placement.localUrl, signal: context.signal });
|
|
94
|
+
await context.onPlacement(placement);
|
|
95
|
+
};
|
|
96
|
+
try {
|
|
97
|
+
resources = await realize(current);
|
|
98
|
+
await verify(current, resources);
|
|
99
|
+
}
|
|
100
|
+
catch (cause) {
|
|
101
|
+
stopping = true;
|
|
102
|
+
if (resources !== undefined)
|
|
103
|
+
await stopResources(resources).catch(() => undefined);
|
|
104
|
+
await rm(directory, { recursive: true, force: true }).catch(() => undefined);
|
|
105
|
+
throw cause;
|
|
106
|
+
}
|
|
107
|
+
let updateTail = Promise.resolve();
|
|
108
|
+
const session = Object.freeze({
|
|
109
|
+
current: () => placementFor(resources),
|
|
110
|
+
update(next) {
|
|
111
|
+
if (stopping)
|
|
112
|
+
return Promise.reject(new Error('Cloudflare development session is stopping.'));
|
|
113
|
+
const operation = updateTail.then(async () => {
|
|
114
|
+
if (stopping)
|
|
115
|
+
throw new Error('Cloudflare development session is stopping.');
|
|
116
|
+
sameDomain(current.artifact, next.artifact);
|
|
117
|
+
const previous = current;
|
|
118
|
+
const previousResources = resources;
|
|
119
|
+
const restart = frontendSignature(previous.artifact) !== frontendSignature(next.artifact) ||
|
|
120
|
+
stable(previous.secrets) !== stable(next.secrets);
|
|
121
|
+
if (!restart) {
|
|
122
|
+
try {
|
|
123
|
+
artifactPaths = await applyArtifact(directory, artifactPaths, next.artifact);
|
|
124
|
+
await writeFile(configPath, configuration(parameters, next.artifact, resources.frontend?.url, host, Object.keys(signingIdentitySecrets(next.secrets, identity)).sort()));
|
|
125
|
+
await verify(next, resources);
|
|
126
|
+
current = Object.freeze({ artifact: next.artifact, secrets: next.secrets });
|
|
127
|
+
}
|
|
128
|
+
catch (cause) {
|
|
129
|
+
artifactPaths = await applyArtifact(directory, artifactPaths, previous.artifact);
|
|
130
|
+
await writeFile(configPath, configuration(parameters, previous.artifact, resources.frontend?.url, host, Object.keys(signingIdentitySecrets(previous.secrets, identity)).sort()));
|
|
131
|
+
throw cause;
|
|
132
|
+
}
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
await stopResources(previousResources);
|
|
137
|
+
}
|
|
138
|
+
catch (cause) {
|
|
139
|
+
rejectFinished(cause);
|
|
140
|
+
throw cause;
|
|
141
|
+
}
|
|
142
|
+
let replacement;
|
|
143
|
+
try {
|
|
144
|
+
replacement = await realize(next);
|
|
145
|
+
await verify(next, replacement);
|
|
146
|
+
resources = replacement;
|
|
147
|
+
current = Object.freeze({ artifact: next.artifact, secrets: next.secrets });
|
|
148
|
+
}
|
|
149
|
+
catch (cause) {
|
|
150
|
+
if (replacement !== undefined)
|
|
151
|
+
await stopResources(replacement).catch(() => undefined);
|
|
152
|
+
try {
|
|
153
|
+
resources = await realize(previous);
|
|
154
|
+
await verify(previous, resources);
|
|
155
|
+
}
|
|
156
|
+
catch (rollback) {
|
|
157
|
+
rejectFinished(new AggregateError([cause, rollback], 'Cloudflare development update and rollback failed.'));
|
|
158
|
+
}
|
|
159
|
+
throw cause;
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
updateTail = operation.catch(() => undefined);
|
|
163
|
+
return operation;
|
|
164
|
+
},
|
|
165
|
+
finished,
|
|
166
|
+
async stop() {
|
|
167
|
+
if (stopping)
|
|
168
|
+
return finished.catch(() => undefined);
|
|
169
|
+
stopping = true;
|
|
170
|
+
await updateTail;
|
|
171
|
+
try {
|
|
172
|
+
try {
|
|
173
|
+
await stopResources(resources);
|
|
174
|
+
}
|
|
175
|
+
finally {
|
|
176
|
+
await rm(directory, { recursive: true, force: true });
|
|
177
|
+
}
|
|
178
|
+
resolveFinished();
|
|
179
|
+
}
|
|
180
|
+
catch (cause) {
|
|
181
|
+
rejectFinished(cause);
|
|
182
|
+
throw cause;
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
return session;
|
|
187
|
+
}
|
|
188
|
+
function configuration(parameters, artifact, frontendUrl, host, secretBindings) {
|
|
189
|
+
const worker = loadWorkerArtifact(artifact);
|
|
190
|
+
const router = parameters.remote === true || parameters.router !== undefined
|
|
191
|
+
? resolveRouter(parameters.router)
|
|
192
|
+
: undefined;
|
|
193
|
+
return generateWranglerConfig({
|
|
194
|
+
workerName: workerName(parameters, artifact.build.schema.compiled.root.origin),
|
|
195
|
+
main: `./${worker.release.entrypoint}`,
|
|
196
|
+
modules: workerModules(worker.release),
|
|
197
|
+
vars: {
|
|
198
|
+
...parameters.vars,
|
|
199
|
+
...(host === undefined ? {} : { WORKER_URL: host }),
|
|
200
|
+
...(frontendUrl === undefined ? {} : { FRONTEND_DEV_URL: frontendUrl }),
|
|
201
|
+
},
|
|
202
|
+
secretBindings,
|
|
203
|
+
...(artifact.build.frontend?.source.kind === 'vite'
|
|
204
|
+
? { frontend: { directory: './frontend', routes: frontendRoutes(artifact), dev: true } }
|
|
205
|
+
: {}),
|
|
206
|
+
...(router === undefined
|
|
207
|
+
? {}
|
|
208
|
+
: { router: { binding: router.binding, service: router.service } }),
|
|
209
|
+
...(parameters.wrangler === undefined ? {} : { wrangler: parameters.wrangler }),
|
|
210
|
+
selfBinding: true,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
async function applyArtifact(root, previous, artifact) {
|
|
214
|
+
const next = new Set(artifact.files.map((file) => file.path));
|
|
215
|
+
for (const file of artifact.files) {
|
|
216
|
+
const path = join(root, file.path);
|
|
217
|
+
const staging = `${path}.next`;
|
|
218
|
+
await mkdir(dirname(path), { recursive: true });
|
|
219
|
+
await writeFile(staging, file.bytes());
|
|
220
|
+
await rename(staging, path);
|
|
221
|
+
}
|
|
222
|
+
for (const path of previous) {
|
|
223
|
+
if (!next.has(path))
|
|
224
|
+
await unlink(join(root, path)).catch(() => undefined);
|
|
225
|
+
}
|
|
226
|
+
return next;
|
|
227
|
+
}
|
|
228
|
+
async function stopResources(resources) {
|
|
229
|
+
const results = await Promise.allSettled([
|
|
230
|
+
resources.worker.stop(),
|
|
231
|
+
resources.frontend?.stop() ?? Promise.resolve(),
|
|
232
|
+
]);
|
|
233
|
+
const failures = results.filter((result) => result.status === 'rejected');
|
|
234
|
+
if (failures.length === 1)
|
|
235
|
+
throw failures[0].reason;
|
|
236
|
+
if (failures.length > 1) {
|
|
237
|
+
throw new AggregateError(failures.map((failure) => failure.reason), 'Cloudflare development cleanup failed.');
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function frontendRoutes(artifact) {
|
|
241
|
+
const frontend = artifact.build.frontend;
|
|
242
|
+
if (frontend === undefined || frontend.source.kind !== 'vite')
|
|
243
|
+
return [];
|
|
244
|
+
return Object.freeze(frontend.routes
|
|
245
|
+
.flatMap((route) => frontend.source.kind === 'vite' && frontend.source.spa && route.path !== '/'
|
|
246
|
+
? [route.path, `${route.path}/*`]
|
|
247
|
+
: route.path === '/'
|
|
248
|
+
? ['/*']
|
|
249
|
+
: [route.path])
|
|
250
|
+
.sort());
|
|
251
|
+
}
|
|
252
|
+
function frontendSignature(artifact) {
|
|
253
|
+
return stable(artifact.build.frontend);
|
|
254
|
+
}
|
|
255
|
+
function sameDomain(previous, next) {
|
|
256
|
+
if (previous.build.schema.compiled.root.origin !== next.build.schema.compiled.root.origin) {
|
|
257
|
+
throw new TypeError('Cloudflare development update changed the Domain origin.');
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
function stable(input) {
|
|
261
|
+
if (input === null || typeof input !== 'object')
|
|
262
|
+
return JSON.stringify(input);
|
|
263
|
+
if (Array.isArray(input))
|
|
264
|
+
return `[${input.map(stable).join(',')}]`;
|
|
265
|
+
const value = input;
|
|
266
|
+
return `{${Object.keys(value)
|
|
267
|
+
.sort()
|
|
268
|
+
.map((key) => `${JSON.stringify(key)}:${stable(value[key])}`)
|
|
269
|
+
.join(',')}}`;
|
|
270
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Build } from '@astrale-os/sdk/deployment/build';
|
|
2
|
+
export interface FrontendHandle {
|
|
3
|
+
readonly url: string;
|
|
4
|
+
readonly finished: Promise<void>;
|
|
5
|
+
stop(): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export declare function startFrontend(input: {
|
|
8
|
+
readonly frontend: Build['frontend'];
|
|
9
|
+
readonly projectDir: string;
|
|
10
|
+
readonly onLog?: (line: string) => void;
|
|
11
|
+
}): Promise<FrontendHandle | undefined>;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { createServer } from 'node:net';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
export async function startFrontend(input) {
|
|
6
|
+
const frontend = input.frontend;
|
|
7
|
+
if (frontend === undefined || frontend.source.kind !== 'vite')
|
|
8
|
+
return undefined;
|
|
9
|
+
const port = await ephemeralPort();
|
|
10
|
+
const child = spawn(viteCommand(input.projectDir), [
|
|
11
|
+
'--host',
|
|
12
|
+
'127.0.0.1',
|
|
13
|
+
'--port',
|
|
14
|
+
String(port),
|
|
15
|
+
'--strictPort',
|
|
16
|
+
...(frontend.source.config === undefined ? [] : ['--config', frontend.source.config]),
|
|
17
|
+
], { cwd: input.projectDir, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
18
|
+
const output = [];
|
|
19
|
+
const collect = (buffer) => {
|
|
20
|
+
for (const line of buffer.toString().split('\n')) {
|
|
21
|
+
if (line.trim() === '')
|
|
22
|
+
continue;
|
|
23
|
+
output.push(line);
|
|
24
|
+
if (output.length > 40)
|
|
25
|
+
output.shift();
|
|
26
|
+
input.onLog?.(line);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
child.stdout?.on('data', collect);
|
|
30
|
+
child.stderr?.on('data', collect);
|
|
31
|
+
const url = `http://127.0.0.1:${port}`;
|
|
32
|
+
try {
|
|
33
|
+
await ready(url, child, output);
|
|
34
|
+
}
|
|
35
|
+
catch (cause) {
|
|
36
|
+
await stopChild(child);
|
|
37
|
+
throw cause;
|
|
38
|
+
}
|
|
39
|
+
let stopping = false;
|
|
40
|
+
const finished = child.exitCode === null
|
|
41
|
+
? new Promise((resolve, reject) => {
|
|
42
|
+
child.once('exit', (code, signal) => {
|
|
43
|
+
if (stopping)
|
|
44
|
+
resolve();
|
|
45
|
+
else {
|
|
46
|
+
reject(new Error(`Vite stopped unexpectedly (code ${code ?? 'null'}, signal ${signal ?? 'none'}).\n${output.join('\n')}`));
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
child.once('error', reject);
|
|
50
|
+
})
|
|
51
|
+
: Promise.reject(new Error(`Vite stopped unexpectedly (code ${child.exitCode}).`));
|
|
52
|
+
return Object.freeze({
|
|
53
|
+
url,
|
|
54
|
+
finished,
|
|
55
|
+
async stop() {
|
|
56
|
+
if (stopping)
|
|
57
|
+
return finished.catch(() => undefined);
|
|
58
|
+
stopping = true;
|
|
59
|
+
await stopChild(child);
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function viteCommand(projectDir) {
|
|
64
|
+
const command = join(projectDir, 'node_modules', '.bin', 'vite');
|
|
65
|
+
if (!existsSync(command))
|
|
66
|
+
throw new TypeError('Vite frontend requires a project-local vite binary.');
|
|
67
|
+
return command;
|
|
68
|
+
}
|
|
69
|
+
function ephemeralPort() {
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
const server = createServer();
|
|
72
|
+
server.once('error', reject);
|
|
73
|
+
server.listen(0, '127.0.0.1', () => {
|
|
74
|
+
const address = server.address();
|
|
75
|
+
const port = typeof address === 'object' && address !== null ? address.port : 0;
|
|
76
|
+
server.close((cause) => (cause === undefined ? resolve(port) : reject(cause)));
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
async function ready(url, child, output) {
|
|
81
|
+
const deadline = Date.now() + 15_000;
|
|
82
|
+
while (Date.now() < deadline) {
|
|
83
|
+
if (child.exitCode !== null) {
|
|
84
|
+
throw new Error(`Vite exited before readiness (code ${child.exitCode}).\n${output.join('\n')}`);
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
const response = await fetch(url, { signal: AbortSignal.timeout(500) });
|
|
88
|
+
await response.body?.cancel();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
throw new Error(`Vite did not become ready at ${url}.`);
|
|
96
|
+
}
|
|
97
|
+
function stopChild(child) {
|
|
98
|
+
if (child.exitCode !== null)
|
|
99
|
+
return Promise.resolve();
|
|
100
|
+
return new Promise((resolve) => {
|
|
101
|
+
child.once('exit', () => resolve());
|
|
102
|
+
child.kill('SIGTERM');
|
|
103
|
+
setTimeout(() => {
|
|
104
|
+
if (child.exitCode === null)
|
|
105
|
+
child.kill('SIGKILL');
|
|
106
|
+
resolve();
|
|
107
|
+
}, 4_000).unref?.();
|
|
108
|
+
});
|
|
109
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** Admit one externally owned public origin without inferring lifecycle ownership. */
|
|
2
|
+
export function admitHost(input) {
|
|
3
|
+
let url;
|
|
4
|
+
try {
|
|
5
|
+
url = new URL(input);
|
|
6
|
+
}
|
|
7
|
+
catch (cause) {
|
|
8
|
+
throw new TypeError('Cloudflare development host must be one public HTTP(S) origin.', { cause });
|
|
9
|
+
}
|
|
10
|
+
if ((url.protocol !== 'https:' && url.protocol !== 'http:') ||
|
|
11
|
+
url.username !== '' ||
|
|
12
|
+
url.password !== '' ||
|
|
13
|
+
url.pathname !== '/' ||
|
|
14
|
+
url.search !== '' ||
|
|
15
|
+
url.hash !== '' ||
|
|
16
|
+
privateHostname(url.hostname)) {
|
|
17
|
+
throw new TypeError('Cloudflare development host must be one public HTTP(S) origin.');
|
|
18
|
+
}
|
|
19
|
+
return url.origin;
|
|
20
|
+
}
|
|
21
|
+
function privateHostname(input) {
|
|
22
|
+
const host = input
|
|
23
|
+
.toLowerCase()
|
|
24
|
+
.replace(/^\[|\]$/gu, '')
|
|
25
|
+
.replace(/\.$/u, '');
|
|
26
|
+
if (host === 'localhost' || host.endsWith('.localhost'))
|
|
27
|
+
return true;
|
|
28
|
+
if (isIP(host) === 4) {
|
|
29
|
+
const parts = host.split('.').map(Number);
|
|
30
|
+
return (parts[0] === 0 ||
|
|
31
|
+
parts[0] === 10 ||
|
|
32
|
+
parts[0] === 127 ||
|
|
33
|
+
(parts[0] === 169 && parts[1] === 254) ||
|
|
34
|
+
(parts[0] === 172 && (parts[1] ?? 0) >= 16 && (parts[1] ?? 0) <= 31) ||
|
|
35
|
+
(parts[0] === 192 && parts[1] === 168));
|
|
36
|
+
}
|
|
37
|
+
if (isIP(host) !== 6)
|
|
38
|
+
return false;
|
|
39
|
+
return (host === '::' ||
|
|
40
|
+
host === '::1' ||
|
|
41
|
+
host.startsWith('fc') ||
|
|
42
|
+
host.startsWith('fd') ||
|
|
43
|
+
/^fe[89ab]/u.test(host) ||
|
|
44
|
+
/^::ffff:(?:0|10|127|169\.254|172\.(?:1[6-9]|2\d|3[01])|192\.168)\./u.test(host));
|
|
45
|
+
}
|
|
46
|
+
import { isIP } from 'node:net';
|