zebflow 0.11.4

Reactive web automation platform — pipelines, templates, and real-time collaboration in a single binary.
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! Files: put, the object operations, archives, images, PDFs and barcodes.
//! Every 0.10 file node merged its answer; the source a node read without a
//! flag (`saved`, `files.file`, `svg`) becomes an explicit `--from`.

use serde_json::{Value, json};

use super::{Config, Rule, words};
use crate::platform::services::migration::RewriteContext;
use crate::platform::services::migration::graph::OldOutput;
use crate::platform::services::migration::node::NodeRewrite;

pub fn rule(kind: &str) -> Option<Rule> {
    Some(match kind {
        "n.fs.save" => Rule::new("fs.file.put", save_output, save),
        "n.fs.put" => Rule::new("fs.file.put", object_output, put),
        "n.fs.list" => Rule::new("fs.folder.list", list_output, list),
        "n.fs.head" => Rule::new("fs.file.head", object_output, head),
        "n.fs.get" => Rule::new("fs.file.get", object_output, get),
        "n.fs.delete" => Rule::new("fs.file.delete", delete_output, delete),
        "n.fs.copy" => Rule::new("fs.file.copy", copy_output, copy_move),
        "n.fs.move" => Rule::new("fs.file.move", copy_output, copy_move),
        "n.fs.mkdir" => Rule::new("fs.folder.create", mkdir_output, mkdir),
        "n.fs.compress" => Rule::new("fs.archive.create", compress_output, compress),
        "n.fs.decompress" => Rule::new("fs.archive.extract", decompress_output, decompress),
        "n.fs.image.thumbnail" => Rule::new("fs.image.thumbnail", thumbnail_output, thumbnail),
        "n.fs.image.chromakey" => Rule::new("fs.image.chromakey", chromakey_output, chromakey),
        "n.fs.svg.convert" => Rule::new("fs.image.render", svg_output, svg),
        "n.fs.pdf.convert" => Rule::new("fs.pdf.convert", pdf_output, pdf),
        "n.fs.barcode.qr" => Rule::new("fs.barcode.render", qr_output, barcode),
        "n.fs.barcode.code128" => Rule::new("fs.barcode.render", code128_output, barcode),
        _ => return None,
    })
}

/// Kinds whose answer at a key is a FileRef in 0.10, so a value read there
/// is a stored file (`--from`), never text.
fn produces_file(kind: &str) -> bool {
    kind.starts_with("n.fs.") || kind == "n.trigger.webhook" || kind.starts_with("n.input.")
}

fn keep_all(n: &mut NodeRewrite<'_>, keys: &[&str]) {
    for key in keys {
        n.keep(key);
    }
}

const DESTINATION: &[&str] = &["folder", "filename", "path", "store", "on_conflict"];

/// The source an old node read from the payload at `default` (or at the
/// path its `source_key` named), as `--from`.
fn source_from(n: &mut NodeRewrite<'_>, default: &str) {
    let key = n.take_str("source_key").unwrap_or_else(|| default.to_string());
    let path: Vec<String> = key.split('.').map(str::to_string).collect();
    let refs: Vec<&str> = path.iter().map(String::as_str).collect();
    if let Some(expr) = n.implicit(&refs) {
        n.note(format!("the source read from input.{key} → from {expr}"));
        n.set("from", Value::String(expr));
    }
}

// ── put ──────────────────────────────────────────────────────────────────────

fn save_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[("saved", "file")])
}

fn save(n: &mut NodeRewrite<'_>) {
    n.kind("fs.file.put");
    if n.has("source_key") {
        // 79c7149 spelling: a dot path into the payload.
        source_from(n, "files.file");
    } else {
        let field = n.take_str("field").unwrap_or_else(|| "file".to_string());
        // 0.10 looked in files.<field>, then <field>, then response.body.
        let candidates: [Vec<&str>; 3] = [vec!["files", field.as_str()], vec![field.as_str()], vec!["response", "body"]];
        let mut found = None;
        for candidate in &candidates {
            match n.locate(candidate) {
                Ok(Some(expr)) => {
                    found = Some((candidate.join("."), expr));
                    break;
                }
                Ok(None) => continue,
                Err(why) => {
                    n.unresolved(format!("the upload n.fs.save read (input.{}) cannot be traced: {why}", candidate.join(".")));
                    return;
                }
            }
        }
        match found {
            Some((path, expr)) => {
                let expr = format!("{{{{ {expr} }}}}");
                n.note(format!("the upload read from input.{path} → from {expr}"));
                n.set("from", Value::String(expr));
            }
            None => n.unresolved(format!(
                "n.fs.save found its file by looking through the payload (files.{field}, {field}, response.body, any one FileRef) and none of those is answered upstream"
            )),
        }
    }
    path_or_folder(n);
    keep_all(n, &["folder", "filename", "store", "on_conflict"]);
    if let Some(kinds) = n.take("allowed_kinds") {
        match words(&kinds) {
            Some(list) => {
                let accept: Vec<String> = list.into_iter().map(|w| if w == "images" { "image".to_string() } else { w }).collect();
                n.note(format!("allowed_kinds {kinds} → accept {accept:?}"));
                n.set("accept", json!(accept));
            }
            None => n.unresolved(format!("allowed_kinds {kinds} is not a list of kinds")),
        }
    }
    match n.take("max_size_mb") {
        Some(value) => match value.as_f64().or_else(|| value.as_str().and_then(|s| s.trim().parse().ok())) {
            Some(mb) => {
                // 0.10 multiplied by 1024 × 1024: the unit was MiB.
                let text = format!("{}MiB", trim(mb));
                n.note(format!("max_size_mb {value} → max_size {text}"));
                n.set("max_size", Value::String(text));
            }
            None => n.unresolved(format!("max_size_mb {value} is not a number")),
        },
        None => n.set_default("max_size", json!("10MiB"), "the 0.10 default was 10 × 1024 × 1024 bytes"),
    }
}

fn trim(number: f64) -> String {
    if number.fract() == 0.0 { format!("{}", number as i64) } else { format!("{number}") }
}

/// A 0.10 `--path` ending in `/` named a folder.
fn path_or_folder(n: &mut NodeRewrite<'_>) {
    if let Some(path) = n.take_str("path") {
        if let Some(folder) = path.strip_suffix('/') {
            n.note(format!("path {path} → folder {folder} (a trailing / named a folder in 0.10)"));
            n.set("folder", Value::String(folder.to_string()));
        } else {
            n.set("path", Value::String(path));
        }
    }
}

fn object_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[
        ("fs", "!the fs answer is file in 0.11; read file.<field>"),
        ("fs.object", "file"),
        ("fs.path", "file.path"),
        ("fs.operation", "!0.11 does not echo the operation"),
        ("fs.skipped", "!0.11 has no skipped flag"),
    ])
}

fn put(n: &mut NodeRewrite<'_>) {
    n.kind("fs.file.put");
    let key = n.take_str("from_key").or_else(|| n.take_str("source_key"));
    if let Some(key) = key {
        let path: Vec<String> = key.split('.').map(str::to_string).collect();
        let resolution = n.graph.resolve_input(n.index, &path);
        match resolution {
            Ok(origin) if produces_file(&n.graph.nodes[origin.producer].kind) => {
                if let Some(expr) = n.implicit(&path.iter().map(String::as_str).collect::<Vec<_>>()) {
                    n.note(format!("source_key {key} → from {expr}"));
                    n.set("from", Value::String(expr));
                }
            }
            Ok(origin) => n.unresolved(format!(
                "n.fs.put wrote whatever was at input.{key} (text as text, JSON as JSON, a FileRef as the file), and what node `{}` answers there cannot be told before it runs",
                n.graph.nodes[origin.producer].id
            )),
            Err(failure) => n.unresolved(format!("n.fs.put read input.{key}: {}", failure.why)),
        }
    }
    n.keep("text");
    if let Some(b64) = n.take("base64") {
        n.set("text", b64);
        n.set("encoding", json!("base64"));
        n.note("base64 → text with encoding base64");
    }
    keep_all(n, &["filename", "path", "store", "on_conflict", "folder"]);
    n.set_default("folder", json!("files"), "n.fs.put wrote under files/ by default; fs.file.put under uploads/");
}

fn list_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[
        ("fs", "!the fs answer is folder in 0.11; read folder.<field>"),
        ("fs.entries", "folder.items"),
        ("fs.count", "folder.count"),
        ("fs.path", "folder.path"),
        ("fs.operation", "!0.11 does not echo the operation"),
    ])
}

fn list(n: &mut NodeRewrite<'_>) {
    n.kind("fs.folder.list");
    let path = n.take("path").or_else(|| n.take("prefix")).unwrap_or_else(|| json!("/"));
    n.set("from", path);
    n.keep("store");
}

fn head(n: &mut NodeRewrite<'_>) {
    n.kind("fs.file.head");
    n.rename("path", "from");
    n.keep("store");
}

fn get(n: &mut NodeRewrite<'_>) {
    n.kind("fs.file.get");
    n.rename("path", "from");
    n.keep("store");
    match n.take_str("encoding") {
        None => {}
        Some(e) if e.eq_ignore_ascii_case("base64") => n.set("encoding", json!("base64")),
        Some(e) if e.eq_ignore_ascii_case("text") => n.set("encoding", json!("text")),
        Some(e) if e.contains("{{") => n.unresolved(format!("encoding `{e}` is chosen at run time; 0.11 takes text or base64 only")),
        Some(e) => n.note(format!("encoding {e} dropped (0.10 read anything but base64 as text)")),
    }
}

fn delete_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[
        ("fs", "!the fs answer is file in 0.11; read file.<field>"),
        ("fs.path", "file.ref"),
        ("fs.deleted", "file.deleted"),
        ("fs.operation", "!0.11 does not echo the operation"),
    ])
}

fn delete(n: &mut NodeRewrite<'_>) {
    n.kind("fs.file.delete");
    n.rename("path", "from");
    n.keep("store");
    n.set("recursive", Value::Bool(true));
    n.note("recursive = true (0.10 deleted a folder and everything in it)");
}

fn copy_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[
        ("fs", "!the fs answer is file in 0.11; read file.<field>"),
        ("fs.object", "file"),
        ("fs.path", "file.ref"),
        ("fs.source_path", "!0.11 does not echo the source"),
        ("fs.skipped", "!0.11 has no skipped flag"),
        ("fs.operation", "!0.11 does not echo the operation"),
    ])
}

fn copy_move(n: &mut NodeRewrite<'_>) {
    let kind = if n.old_kind == "n.fs.move" { "fs.file.move" } else { "fs.file.copy" };
    n.kind(kind);
    keep_all(n, &["from", "folder", "filename", "path", "store", "on_conflict"]);
}

fn mkdir_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[
        ("fs", "!the fs answer is folder in 0.11; read folder.<field>"),
        ("fs.path", "folder.path"),
        ("fs.object", "!0.11 answers path and created"),
        ("fs.operation", "!0.11 does not echo the operation"),
    ])
}

fn mkdir(n: &mut NodeRewrite<'_>) {
    n.kind("fs.folder.create");
    n.rename("path", "folder");
    n.keep("store");
}

// ── archives ─────────────────────────────────────────────────────────────────

fn compress_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[("compressed", "archive")])
}

fn compress(n: &mut NodeRewrite<'_>) {
    n.kind("fs.archive.create");
    let first = n.take_str("source_key").unwrap_or_else(|| "saved".to_string());
    let extra = n.take("extra_source_keys").and_then(|v| words(&v)).unwrap_or_default();
    let mut sources = Vec::new();
    for key in std::iter::once(first).chain(extra) {
        let path: Vec<String> = key.split('.').map(str::to_string).collect();
        match n.implicit(&path.iter().map(String::as_str).collect::<Vec<_>>()) {
            Some(expr) => sources.push(Value::String(expr)),
            None => return,
        }
    }
    n.note(format!("the sources read from the payload → from {}", Value::Array(sources.clone())));
    n.set("from", if sources.len() == 1 { sources.remove(0) } else { Value::Array(sources) });
    keep_all(n, &["folder", "filename", "path", "store", "on_conflict", "format"]);
}

fn decompress_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[
        ("decompressed", "!the decompressed answer is archive in 0.11; read archive.<field>"),
        ("decompressed.files", "archive.items"),
        ("decompressed.folder", "archive.folder"),
        ("decompressed.source", "!0.11 does not echo the source"),
        ("decompressed.store", "!0.11 does not echo the store"),
        ("decompressed.format", "!0.11 does not echo the format"),
    ])
}

fn decompress(n: &mut NodeRewrite<'_>) {
    n.kind("fs.archive.extract");
    source_from(n, "saved");
    keep_all(n, &["folder", "format", "delete_source", "store", "on_conflict"]);
    if n.config.get("on_conflict").and_then(Value::as_str) == Some("overwrite") {
        n.behaviour("--on-conflict overwrite empties the destination folder first in 0.11; 0.10 left files the archive did not hold");
    }
}

// ── images ───────────────────────────────────────────────────────────────────

fn thumbnail_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[("thumbnail", "image")])
}

/// A closed word 0.10 did not check: an unknown one behaved as the default.
fn closed(n: &mut NodeRewrite<'_>, key: &str, words: &[&str], default: &str) {
    let Some(value) = n.take_str(key) else { return };
    let word = value.trim().to_ascii_lowercase();
    if words.contains(&word.as_str()) {
        n.set(key, Value::String(word));
    } else if value.contains("{{") {
        n.unresolved(format!("{key} `{value}` is chosen at run time; 0.11 takes {} only", words.join("|")));
    } else {
        n.note(format!("{key} {value} → {default} (0.10 treated an unknown word as {default})"));
        n.set(key, Value::String(default.to_string()));
    }
}

fn thumbnail(n: &mut NodeRewrite<'_>) {
    n.kind("fs.image.thumbnail");
    source_from(n, "saved");
    keep_all(n, &["width", "height", "folder", "delete_source"]);
    keep_all(n, DESTINATION);
    closed(n, "fit", &["cover", "contain", "fill"], "cover");
    closed(n, "format", &["jpg", "png", "webp"], "jpg");
    if let Some(quality) = n.take("quality") {
        match quality.as_f64() {
            Some(q) if !(1.0..=100.0).contains(&q) => {
                let clamped = q.clamp(1.0, 100.0) as i64;
                n.note(format!("quality {q} → {clamped} (0.10 clamped it)"));
                n.set("quality", json!(clamped));
            }
            _ => n.set("quality", quality),
        }
    }
}

fn chromakey_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[("image", "image")])
}

fn chromakey(n: &mut NodeRewrite<'_>) {
    n.kind("fs.image.chromakey");
    source_from(n, "saved");
    keep_all(n, &["color", "tolerance", "soften", "format", "delete_source"]);
    keep_all(n, DESTINATION);
}

fn svg_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[("image", "image"), ("layout", "image.layout")])
}

fn svg(n: &mut NodeRewrite<'_>) {
    n.kind("fs.image.render");
    let key = n.take_str("source_key").unwrap_or_else(|| "svg".to_string());
    let path: Vec<String> = key.split('.').map(str::to_string).collect();
    match n.graph.resolve_input(n.index, &path) {
        Ok(origin) if produces_file(&n.graph.nodes[origin.producer].kind) => {
            if let Some(expr) = n.implicit(&path.iter().map(String::as_str).collect::<Vec<_>>()) {
                n.note(format!("the stored SVG at input.{key} → from {expr}"));
                n.set("from", Value::String(expr));
            }
        }
        Ok(origin) => n.unresolved(format!(
            "n.fs.svg.convert decided per request whether input.{key} was SVG markup or a stored file; node `{}` answers it, and which one it is cannot be told before it runs (0.11 takes --text or --from)",
            n.graph.nodes[origin.producer].id
        )),
        Err(failure) => n.unresolved(format!("n.fs.svg.convert read input.{key}: {}", failure.why)),
    }
    keep_all(n, &["width", "height", "fit", "format", "quality", "delete_source"]);
    keep_all(n, DESTINATION);
}

// ── pdf ──────────────────────────────────────────────────────────────────────

fn pdf_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[
        ("pdf_convert", "!the pdf_convert answer is pdf in 0.11; read pdf.<field>"),
        ("pdf_convert.files", "pdf.items"),
        ("pdf_convert.folder", "pdf.folder"),
        ("pdf_convert.manifest_path", "pdf.manifest_path"),
        ("pdf_convert.page_count", "pdf.page_count"),
        ("pdf_convert.pages", "pdf.pages"),
        ("pdf_convert.source", "!0.11 does not echo the source"),
        ("pdf_convert.store", "!0.11 does not echo the store"),
        ("pdf_convert.options", "!0.11 does not echo the options"),
        ("pdf_convert.skipped", "!0.11 has no skipped flag"),
    ])
}

fn pdf(n: &mut NodeRewrite<'_>) {
    n.kind("fs.pdf.convert");
    source_from(n, "saved");
    keep_all(n, &["folder", "store", "on_conflict"]);
    let flags = [("emit_fulltext", "text"), ("emit_page_images", "image"), ("emit_page_raster", "raster")];
    let mut include = Vec::new();
    for (key, word) in flags {
        if n.take_bool(key).unwrap_or(true) {
            include.push(word);
        }
    }
    match include.len() {
        3 => {}
        0 => n.unresolved("every emit_* switch is off; 0.11 always converts something (an empty --include means all)"),
        _ => {
            n.note(format!("emit_* → include {include:?}"));
            n.set("include", json!(include));
        }
    }
    if let Some(dpi) = n.take("dpi") {
        match dpi.as_f64() {
            Some(d) if !(36.0..=600.0).contains(&d) => n.unresolved(format!("dpi {d} is outside the 0.11 range 36–600")),
            _ => n.set("dpi", dpi),
        }
    }
}

// ── barcodes ─────────────────────────────────────────────────────────────────

fn qr_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[
        ("qr", "barcode"),
        ("qr.version", "!0.11 does not report the QR version"),
        ("qr.ecc", "!0.11 does not report the error correction"),
        ("qr.modules", "!0.11 does not report the module count"),
        ("qr.svg", "!0.11 does not inline the SVG markup; read the stored file"),
    ])
}

fn code128_output(_: &Config, _: &RewriteContext) -> OldOutput {
    OldOutput::merge(&[
        ("barcode", "barcode"),
        ("barcode.svg", "!0.11 does not inline the SVG markup; read the stored file"),
    ])
}

fn barcode(n: &mut NodeRewrite<'_>) {
    n.kind("fs.barcode.render");
    if n.old_kind == "n.fs.barcode.code128" {
        n.set("symbology", json!("code128"));
        n.note("symbology = code128");
        n.keep("height");
    } else {
        n.keep("ecc");
    }
    n.rename("size", "width");
    keep_all(n, &["text", "format", "margin", "color", "background"]);
    keep_all(n, DESTINATION);
}