presolve-cli 0.2.0-beta.11

The Presolve compiler and application command-line interface.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Cloudflare Workers Static Assets projection over compiler-published routes.
//!
//! The adapter validates compiler artifacts and executes only a compiler-issued
//! route table. It never reads application source or adds a framework runtime.

use std::collections::BTreeSet;
use std::fs;
use std::path::Path;

use presolve_compiler::{ApplicationPublicationArtifactV1, FileRoutePublicationManifestV1};
use serde::Serialize;
use sha2::{Digest as _, Sha256};

pub const CLOUDFLARE_WORKERS_STATIC_DEPLOYMENT_SCHEMA_VERSION: u32 = 1;
/// Tested Workers compatibility target for the current Presolve release train.
pub const CLOUDFLARE_WORKERS_COMPATIBILITY_DATE_V1: &str = "2026-07-23";

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CloudflareWorkersDeploymentOptionsV1 {
    pub worker_name: String,
    pub compatibility_date: String,
    pub required_secrets: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CloudflareWorkersDeploymentErrorV1 {
    pub code: &'static str,
    pub message: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CloudflareWorkersStaticDeploymentPlanV1 {
    pub schema_version: u32,
    pub provider: String,
    pub worker_name: String,
    pub compatibility_date: String,
    pub release_id: String,
    pub required_secrets: Vec<String>,
    pub assets_binding: String,
    pub routes: Vec<CloudflareWorkersStaticRouteV1>,
    pub artifacts: Vec<CloudflareWorkersArtifactV1>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CloudflareWorkersStaticRouteV1 {
    pub path: String,
    pub artifact_root: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CloudflareWorkersArtifactV1 {
    pub path: String,
    pub digest: String,
}

/// Projects one compiler file-route manifest into immutable Cloudflare facts.
///
/// # Errors
///
/// Returns stable errors for invalid public configuration or unsafe compiler
/// artifact paths. This does not read application source.
pub fn build_cloudflare_workers_static_deployment_plan_v1(
    manifest: &FileRoutePublicationManifestV1,
    options: &CloudflareWorkersDeploymentOptionsV1,
) -> Result<CloudflareWorkersStaticDeploymentPlanV1, CloudflareWorkersDeploymentErrorV1> {
    if !is_worker_name(&options.worker_name) {
        return Err(CloudflareWorkersDeploymentErrorV1 {
            code: "PSCFL1001_WORKER_NAME_INVALID",
            message: options.worker_name.clone(),
        });
    }
    if !is_compatibility_date(&options.compatibility_date) {
        return Err(CloudflareWorkersDeploymentErrorV1 {
            code: "PSCFL1002_COMPATIBILITY_DATE_INVALID",
            message: options.compatibility_date.clone(),
        });
    }
    let required_secrets = canonical_secret_names(&options.required_secrets)?;
    let mut artifacts = manifest
        .artifacts
        .iter()
        .map(cloudflare_artifact)
        .collect::<Result<Vec<_>, _>>()?;
    artifacts.sort_by(|left, right| left.path.cmp(&right.path));
    let routes = manifest
        .routes
        .iter()
        .map(|route| CloudflareWorkersStaticRouteV1 {
            path: route.path.clone(),
            artifact_root: route.artifact_root.clone(),
        })
        .collect::<Vec<_>>();
    if routes.is_empty() {
        return Err(CloudflareWorkersDeploymentErrorV1 {
            code: "PSCFL1003_ROUTE_SET_EMPTY",
            message: "Cloudflare deployment requires at least one compiler route".into(),
        });
    }
    if routes
        .iter()
        .any(|route| !is_route_path(&route.path) || !is_artifact_root(&route.artifact_root))
    {
        return Err(CloudflareWorkersDeploymentErrorV1 {
            code: "PSCFL1004_ROUTE_PROJECTION_INVALID",
            message: "compiler route manifest contains an unsafe Cloudflare projection".into(),
        });
    }
    let release_id = format!(
        "sha256:{:x}",
        Sha256::digest(
            artifacts
                .iter()
                .map(|artifact| format!("{}:{}\n", artifact.path, artifact.digest))
                .collect::<String>(),
        )
    );
    Ok(CloudflareWorkersStaticDeploymentPlanV1 {
        schema_version: CLOUDFLARE_WORKERS_STATIC_DEPLOYMENT_SCHEMA_VERSION,
        provider: "cloudflare_workers_static_assets".into(),
        worker_name: options.worker_name.clone(),
        compatibility_date: options.compatibility_date.clone(),
        release_id,
        required_secrets,
        assets_binding: "ASSETS".into(),
        routes,
        artifacts,
    })
}

/// Confirms that provider-upload bytes still equal the compiler inventory.
///
/// # Errors
///
/// Returns an error for a missing or modified artifact.
pub fn validate_cloudflare_workers_artifacts_v1(
    output_root: &Path,
    plan: &CloudflareWorkersStaticDeploymentPlanV1,
) -> Result<(), CloudflareWorkersDeploymentErrorV1> {
    for artifact in &plan.artifacts {
        let path = output_root.join(&artifact.path);
        let bytes = fs::read(&path).map_err(|error| CloudflareWorkersDeploymentErrorV1 {
            code: "PSCFL1005_ARTIFACT_READ_FAILED",
            message: format!("{}: {error}", path.display()),
        })?;
        if format!("sha256:{:x}", Sha256::digest(&bytes)) != artifact.digest {
            return Err(CloudflareWorkersDeploymentErrorV1 {
                code: "PSCFL1006_ARTIFACT_INTEGRITY_MISMATCH",
                message: artifact.path.clone(),
            });
        }
    }
    Ok(())
}

#[must_use]
pub fn cloudflare_workers_static_plan_json_v1(
    plan: &CloudflareWorkersStaticDeploymentPlanV1,
) -> String {
    serde_json::to_string_pretty(plan).expect("Cloudflare static plan serializes") + "\n"
}

/// Generates a Worker that consumes only the canonical route table. It is a
/// static asset executor, not a generic server or client router.
#[must_use]
pub fn cloudflare_workers_static_worker_module_v1(
    plan: &CloudflareWorkersStaticDeploymentPlanV1,
) -> String {
    let routes = serde_json::to_string(&plan.routes).expect("Cloudflare routes serialize");
    format!(
        "// Generated by Presolve. Do not edit.\n\
const routes = {routes};\n\
function segments(pathname) {{ return pathname.split('/').filter(Boolean); }}\n\
function score(parts) {{ return [parts.filter((value) => !value.startsWith(':')).length, parts.length]; }}\n\
function routeFor(requested) {{\n\
  let selected;\n\
  for (const route of routes) {{\n\
    const parts = segments(route.path);\n\
    if (requested.length < parts.length) continue;\n\
    if (!parts.every((part, index) => part.startsWith(':') ? requested[index].length > 0 : part === requested[index])) continue;\n\
    const candidate = score(parts);\n\
    if (!selected || candidate[0] > selected.score[0] || (candidate[0] === selected.score[0] && candidate[1] > selected.score[1])) selected = {{ route, parts, score: candidate }};\n\
  }}\n\
  return selected;\n\
}}\n\
function safeAssetSegments(values) {{ return values.every((value) => value && value !== '.' && value !== '..' && !value.includes('\\\\')); }}\n\
async function fetchCompilerAsset(request, env, artifactUrl) {{\n\
  const response = await env.ASSETS.fetch(new Request(artifactUrl, request));\n\
  if (response.status < 300 || response.status > 399) return response;\n\
  const location = response.headers.get('Location');\n\
  if (!location) return response;\n\
  const redirected = new URL(location, artifactUrl);\n\
  const internal = new URL(artifactUrl);\n\
  if (redirected.origin !== internal.origin || !redirected.pathname.startsWith('/routes/')) return response;\n\
  return env.ASSETS.fetch(new Request(redirected, request));\n\
}}\n\
export default {{ async fetch(request, env) {{\n\
  if (request.method !== 'GET' && request.method !== 'HEAD') return new Response('Method Not Allowed\\n', {{ status: 405, headers: {{ Allow: 'GET, HEAD' }} }});\n\
  const url = new URL(request.url);\n\
  const direct = await env.ASSETS.fetch(request);\n\
  if (direct.status !== 404) {{\n\
    const location = direct.headers.get('Location');\n\
    if (location) {{\n\
      const redirected = new URL(location, url);\n\
      if (redirected.origin === url.origin && redirected.pathname.startsWith('/routes/')) return fetchCompilerAsset(request, env, redirected);\n\
    }}\n\
    return direct;\n\
  }}\n\
  const requested = segments(url.pathname);\n\
  const selected = routeFor(requested);\n\
  if (!selected) return new Response('Not Found\\n', {{ status: 404 }});\n\
  if (requested.length === selected.parts.length) {{\n\
    if (selected.route.path !== '/' && !url.pathname.endsWith('/')) return Response.redirect(new URL(`${{url.pathname}}/`, url).toString(), 308);\n\
    return fetchCompilerAsset(request, env, new URL(`/${{selected.route.artifact_root}}/index.html`, url));\n\
  }}\n\
  const suffix = requested.slice(selected.parts.length);\n\
  if (!safeAssetSegments(suffix)) return new Response('Not Found\\n', {{ status: 404 }});\n\
  return fetchCompilerAsset(request, env, new URL(`/${{selected.route.artifact_root}}/${{suffix.join('/')}}`, url));\n\
}} }};\n"
    )
}

#[must_use]
pub fn cloudflare_workers_wrangler_jsonc_v1(
    plan: &CloudflareWorkersStaticDeploymentPlanV1,
    assets_directory: &str,
) -> String {
    let mut value = serde_json::json!({
        "name": plan.worker_name,
        "main": "worker.mjs",
        "compatibility_date": plan.compatibility_date,
        "assets": {
            "directory": assets_directory,
            "binding": plan.assets_binding,
            "run_worker_first": true,
        },
    });
    if !plan.required_secrets.is_empty() {
        value["secrets"] = serde_json::json!({ "required": plan.required_secrets });
    }
    serde_json::to_string_pretty(&value).expect("Wrangler configuration serializes") + "\n"
}

fn cloudflare_artifact(
    artifact: &ApplicationPublicationArtifactV1,
) -> Result<CloudflareWorkersArtifactV1, CloudflareWorkersDeploymentErrorV1> {
    if !is_artifact_path(&artifact.path) || !artifact.digest.starts_with("sha256:") {
        return Err(CloudflareWorkersDeploymentErrorV1 {
            code: "PSCFL1007_ARTIFACT_PROJECTION_INVALID",
            message: artifact.path.clone(),
        });
    }
    Ok(CloudflareWorkersArtifactV1 {
        path: artifact.path.clone(),
        digest: artifact.digest.clone(),
    })
}

fn canonical_secret_names(
    values: &[String],
) -> Result<Vec<String>, CloudflareWorkersDeploymentErrorV1> {
    let mut names = BTreeSet::new();
    for value in values {
        if !is_secret_name(value) {
            return Err(CloudflareWorkersDeploymentErrorV1 {
                code: "PSCFL1008_SECRET_NAME_INVALID",
                message: value.clone(),
            });
        }
        names.insert(value.clone());
    }
    Ok(names.into_iter().collect())
}

fn is_worker_name(value: &str) -> bool {
    !value.is_empty()
        && value.len() <= 63
        && value.chars().all(|character| {
            character.is_ascii_lowercase() || character.is_ascii_digit() || character == '-'
        })
}

fn is_compatibility_date(value: &str) -> bool {
    value.len() == 10
        && value.as_bytes()[4] == b'-'
        && value.as_bytes()[7] == b'-'
        && value
            .bytes()
            .enumerate()
            .all(|(index, byte)| matches!(index, 4 | 7) || byte.is_ascii_digit())
}

fn is_secret_name(value: &str) -> bool {
    value
        .chars()
        .next()
        .is_some_and(|character| character.is_ascii_uppercase() || character == '_')
        && value.chars().all(|character| {
            character.is_ascii_uppercase() || character.is_ascii_digit() || character == '_'
        })
}

fn is_route_path(value: &str) -> bool {
    value.starts_with('/') && !value.contains('?') && !value.contains('#') && !value.contains('\\')
}

fn is_artifact_root(value: &str) -> bool {
    is_artifact_path(value) && value.starts_with("routes/")
}

fn is_artifact_path(value: &str) -> bool {
    !value.is_empty()
        && !value.starts_with('/')
        && value.split('/').all(|segment| {
            !segment.is_empty() && segment != "." && segment != ".." && !segment.contains('\\')
        })
}

#[cfg(test)]
mod tests {
    use std::fs;

    use presolve_compiler::{
        ApplicationPublicationArtifactV1, FileRoutePublicationManifestV1,
        FileRoutePublicationRouteV1,
    };
    use sha2::{Digest as _, Sha256};

    use super::{
        build_cloudflare_workers_static_deployment_plan_v1,
        cloudflare_workers_static_worker_module_v1, cloudflare_workers_wrangler_jsonc_v1,
        validate_cloudflare_workers_artifacts_v1, CloudflareWorkersDeploymentOptionsV1,
        CLOUDFLARE_WORKERS_COMPATIBILITY_DATE_V1,
    };

    fn manifest(digest: String) -> FileRoutePublicationManifestV1 {
        FileRoutePublicationManifestV1 {
            schema_version: 1,
            compiler_contract: "presolve-file-route-publication:1".into(),
            profile: "production".into(),
            routes: vec![FileRoutePublicationRouteV1 {
                path: "/posts/:slug".into(),
                entry_component_id: "component:post".into(),
                artifact_root: "routes/segment-posts/parameter-slug".into(),
                layout_component_ids: Vec::new(),
            }],
            artifacts: vec![ApplicationPublicationArtifactV1 {
                path: "routes/segment-posts/parameter-slug/index.html".into(),
                digest,
            }],
        }
    }

    #[test]
    fn projects_only_compiler_owned_routes_artifacts_and_public_bindings() {
        let plan = build_cloudflare_workers_static_deployment_plan_v1(
            &manifest(
                "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".into(),
            ),
            &CloudflareWorkersDeploymentOptionsV1 {
                worker_name: "presolve-docs".into(),
                compatibility_date: CLOUDFLARE_WORKERS_COMPATIBILITY_DATE_V1.into(),
                required_secrets: vec!["DOCS_TOKEN".into()],
            },
        )
        .unwrap();
        assert_eq!(plan.routes[0].path, "/posts/:slug");
        assert_eq!(plan.required_secrets, ["DOCS_TOKEN"]);
        let worker = cloudflare_workers_static_worker_module_v1(&plan);
        assert!(worker.contains("routeFor"));
        assert!(worker.contains("routes/segment-posts/parameter-slug"));
        assert!(worker.contains("fetchCompilerAsset"));
        assert!(worker.contains("redirected.pathname.startsWith('/routes/')"));
        assert!(worker.contains("if (location)"));
        assert!(!worker.contains("url.pathname === '/' && location"));
        assert!(!worker.contains("component:post"));
        let config = cloudflare_workers_wrangler_jsonc_v1(&plan, "../../dist");
        assert!(config.contains("run_worker_first"));
        assert!(config.contains("DOCS_TOKEN"));
    }

    #[test]
    fn rejects_mutated_compiler_artifacts_before_upload() {
        let bytes = b"<main>post</main>".to_vec();
        let digest = format!("sha256:{:x}", Sha256::digest(&bytes));
        let plan = build_cloudflare_workers_static_deployment_plan_v1(
            &manifest(digest),
            &CloudflareWorkersDeploymentOptionsV1 {
                worker_name: "presolve-docs".into(),
                compatibility_date: CLOUDFLARE_WORKERS_COMPATIBILITY_DATE_V1.into(),
                required_secrets: Vec::new(),
            },
        )
        .unwrap();
        let root =
            std::env::temp_dir().join(format!("presolve-cloudflare-plan-{}", std::process::id()));
        let path = root.join("routes/segment-posts/parameter-slug/index.html");
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(&path, &bytes).unwrap();
        assert!(validate_cloudflare_workers_artifacts_v1(&root, &plan).is_ok());
        fs::write(&path, b"tampered").unwrap();
        assert_eq!(
            validate_cloudflare_workers_artifacts_v1(&root, &plan)
                .unwrap_err()
                .code,
            "PSCFL1006_ARTIFACT_INTEGRITY_MISMATCH"
        );
        fs::remove_dir_all(root).unwrap();
    }
}