d2-rs 0.1.7

The d2-rs command-line tool, a Rust port of D2's d2cli package.
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
// SPDX-License-Identifier: MPL-2.0
//! D2 master 6a7b48378 lib/imgbundler/imgbundler.go: bounded generated-SVG
//! scanning, cumulative replacement budgets and transactional replacement.
//! Acquisition/policy belongs to the caller's image resolver.
use std::collections::{BTreeMap, BTreeSet};

pub const MAX_REFERENCES: usize = 4096;
pub const MAX_REFERENCE_BYTES: usize = 64 << 10;
pub const MAX_BUNDLED_BYTES: i64 = 512 << 20;
pub const MAX_OUTPUT_BYTES: i64 = 512 << 20;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ImageMatch {
    pub start: usize,
    pub end: usize,
    pub href_start: usize,
    pub href_end: usize,
}

#[derive(Default, Clone, Copy)]
pub struct Options {
    pub local: bool,
    pub remote: bool,
}

/// Decode only for classification/resolution; retain raw hrefs as replacement
/// keys so entity spellings and duplicate occurrences survive the scan.
pub fn decode_href(href: &str) -> String {
    String::from_utf8_lossy(&d2_rs_text::markdown::html::unescape_html(href.as_bytes()))
        .into_owned()
}

fn is_remote(href: &str) -> bool {
    // Reuse the shared Go net/url parser, including opaque URIs, authority
    // validation, and the distinct query/path/fragment escape rules.
    d2_rs_language::compiler::import_urls::parse(href.as_bytes(), false)
        .is_ok_and(|url| url.scheme.starts_with(b"http"))
}

fn limit_error(name: &str, actual: i64, limit: i64) -> String {
    format!("{name} exceeds maximum of {limit} bytes: {actual}")
}

/// findImageElementsForOptions. Literal matching mirrors imageRegex, including
/// its nonempty href and exact generated-markup prefix.
pub fn find_image_elements(
    svg: &[u8],
    options: Options,
    context: &impl Fn() -> Result<(), String>,
) -> Result<(Vec<ImageMatch>, Vec<String>), String> {
    const PREFIX: &[u8] = b"<image href=\"";
    let mut matches = Vec::new();
    let mut hrefs = Vec::new();
    let mut unique = BTreeSet::new();
    let mut offset = 0;
    while offset < svg.len() {
        context()?;
        let Some(relative) = svg[offset..]
            .windows(PREFIX.len())
            .position(|s| s == PREFIX)
        else {
            break;
        };
        let start = offset + relative;
        let href_start = start + PREFIX.len();
        let Some(length) = svg[href_start..].iter().position(|b| *b == b'"') else {
            break;
        };
        let href_end = href_start + length;
        offset = href_end + 1;
        if length == 0 {
            continue;
        }
        let href = String::from_utf8_lossy(&svg[href_start..href_end]).into_owned();
        let decoded = decode_href(&href);
        if decoded
            .get(..5)
            .is_some_and(|s| s.eq_ignore_ascii_case("data:"))
        {
            continue;
        }
        let remote = is_remote(&decoded);
        if (remote && !options.remote) || (!remote && !options.local) {
            continue;
        }
        if length > MAX_REFERENCE_BYTES {
            return Err(limit_error(
                "image reference",
                length as i64,
                MAX_REFERENCE_BYTES as i64,
            ));
        }
        if matches.len() == MAX_REFERENCES {
            return Err(format!(
                "image references exceed maximum of {MAX_REFERENCES}"
            ));
        }
        matches.push(ImageMatch {
            start,
            end: offset,
            href_start,
            href_end,
        });
        if unique.insert(href.clone()) {
            hrefs.push(href);
        }
    }
    Ok((matches, hrefs))
}

/// bundleBudget.reserveBundled: failed reservations never consume bytes and
/// the first error remains sticky for all later reservations.
pub struct Budget {
    pub used: i64,
    pub limit: i64,
    pub exhausted: Option<String>,
}
impl Budget {
    pub fn new(limit: i64) -> Self {
        Self {
            used: 0,
            limit,
            exhausted: None,
        }
    }
    pub fn reserve(&mut self, count: i64) -> Result<(), String> {
        if let Some(error) = &self.exhausted {
            return Err(error.clone());
        }
        if count > self.limit - self.used {
            let error = limit_error(
                "cumulative bundled image bytes",
                self.used + count,
                self.limit,
            );
            self.exhausted = Some(error.clone());
            return Err(error);
        }
        self.used += count;
        Ok(())
    }
}

/// resolveReplacement's serialization/budget boundary, after Resource.DataURI.
pub fn replacement(data_uri: &str, budget: &mut Budget) -> Result<Vec<u8>, String> {
    let count = b"<image href=\"".len() + data_uri.len() + 1;
    budget.reserve(count as i64)?;
    Ok(format!("<image href=\"{data_uri}\"").into_bytes())
}

/// applyReplacements: size/context errors return no partially assembled output.
/// The caller retains `svg` when this returns Err.
pub fn apply_replacements(
    svg: &[u8],
    matches: &[ImageMatch],
    replacements: &BTreeMap<String, Vec<u8>>,
    max_output: i64,
    context: &impl Fn() -> Result<(), String>,
) -> Result<Vec<u8>, String> {
    if replacements.is_empty() {
        return Ok(svg.to_vec());
    }
    let mut output_bytes = svg.len() as i64;
    for m in matches {
        context()?;
        if let Some(replacement) =
            replacements.get(String::from_utf8_lossy(&svg[m.href_start..m.href_end]).as_ref())
        {
            output_bytes += replacement.len() as i64 - (m.end - m.start) as i64;
            if output_bytes > max_output {
                return Err(limit_error("bundled SVG output", output_bytes, max_output));
            }
        }
    }
    if output_bytes > max_output {
        return Err(limit_error("bundled SVG output", output_bytes, max_output));
    }
    let mut out = Vec::with_capacity(output_bytes as usize);
    let mut previous = 0;
    for m in matches {
        context()?;
        if let Some(replacement) =
            replacements.get(String::from_utf8_lossy(&svg[m.href_start..m.href_end]).as_ref())
        {
            out.extend_from_slice(&svg[previous..m.start]);
            out.extend_from_slice(replacement);
            previous = m.end;
        }
    }
    out.extend_from_slice(&svg[previous..]);
    Ok(out)
}

/// Resolve a raw generated-SVG href through the caller's imageasset session.
/// Source: imgbundler.resolveReplacement (368–389). DataURIContext canonicalizes
/// MIME from validated bytes; raw HTTP header strings never enter SVG markup.
pub fn resolve_replacement(
    resolver: &d2_rs_scenebuild::RasterImageResolver,
    href: &str,
    budget: &mut Budget,
    cancel: Option<&d2_rs_core::CancellationToken>,
) -> Result<Vec<u8>, String> {
    let resource = resolver
        .resolve_with_context(&decode_href(href), cancel)
        .map_err(|e| e.message)?;
    let uri = resource.data_uri(cancel).map_err(|e| e.message)?;
    replacement(
        std::str::from_utf8(&uri).expect("data URI is ASCII"),
        budget,
    )
}

/// Caller-owned resolver session, equivalent to BundleWithResolver. The
/// deprecated wrappers create a fresh resolver for each document invocation.
/// Successful replacements survive a later resolution failure; scanner/output
/// admission errors preserve the complete input. The returned error is separate
/// from the returned (possibly partially bundled) document.
pub fn bundle_with_resolver(
    svg: &[u8],
    options: Options,
    resolver: Option<&d2_rs_scenebuild::RasterImageResolver>,
    cancel: Option<&d2_rs_core::CancellationToken>,
) -> (Vec<u8>, Option<String>) {
    let context = || context_error(cancel);
    let (matches, hrefs) = match find_image_elements(svg, options, &context) {
        Ok(result) => result,
        Err(error) => {
            return (
                svg.to_vec(),
                Some(format!("failed to bundle images: {error}")),
            );
        }
    };
    if hrefs.is_empty() {
        return (svg.to_vec(), None);
    }
    let Some(resolver) = resolver else {
        return (
            svg.to_vec(),
            Some("failed to bundle images: image resolver is required".into()),
        );
    };
    let (output, error) = run_resolver_workers(
        svg,
        &matches,
        &hrefs,
        resolver,
        MAX_BUNDLED_BYTES,
        16,
        cancel,
    );
    (
        output,
        error.map(|e| format!("failed to bundle images: {e}")),
    )
}

fn context_error(cancel: Option<&d2_rs_core::CancellationToken>) -> Result<(), String> {
    cancel
        .and_then(d2_rs_core::CancellationToken::error)
        .map_or(Ok(()), |e| Err(e.to_string()))
}

/// runResolverWorkersWithLimit (296–366). Stop scheduling after the first
/// failure and retain that original cause while in-flight work observes
/// cancellation. Applying completed replacements uses the parent's context,
/// rather than the internally canceled worker context.
pub fn run_resolver_workers(
    svg: &[u8],
    matches: &[ImageMatch],
    hrefs: &[String],
    resolver: &d2_rs_scenebuild::RasterImageResolver,
    max_bundled_bytes: i64,
    max_workers: usize,
    cancel: Option<&d2_rs_core::CancellationToken>,
) -> (Vec<u8>, Option<String>) {
    use std::sync::Mutex;
    let worker_cancel = cancel.map_or_else(d2_rs_core::CancellationToken::new, |c| c.child());
    let state = Mutex::new((0usize, BTreeMap::new(), None::<String>));
    let budget = Mutex::new(Budget::new(max_bundled_bytes));
    std::thread::scope(|scope| {
        for _ in 0..max_workers.min(hrefs.len()) {
            scope.spawn(|| {
                loop {
                    let href = {
                        let mut state = state.lock().unwrap();
                        if state.2.is_some()
                            || state.0 >= hrefs.len()
                            || worker_cancel.is_cancelled()
                        {
                            break;
                        }
                        let href = &hrefs[state.0];
                        state.0 += 1;
                        href
                    };
                    // The resolver and DataURI support cancellation independently;
                    // reserve only completed replacement bytes under the shared budget.
                    let result = (|| {
                        let resource = resolver
                            .resolve_with_context(&decode_href(href), Some(&worker_cancel))
                            .map_err(|e| e.message)?;
                        let uri = resource
                            .data_uri(Some(&worker_cancel))
                            .map_err(|e| e.message)?;
                        replacement(
                            std::str::from_utf8(&uri).expect("data URI is ASCII"),
                            &mut budget.lock().unwrap(),
                        )
                    })();
                    let mut state = state.lock().unwrap();
                    match result {
                        Ok(replacement) => {
                            state.1.insert(href.clone(), replacement);
                        }
                        Err(error) => {
                            if state.2.is_none() {
                                state.2 = Some(error);
                            }
                            worker_cancel.cancel();
                            break;
                        }
                    }
                }
            });
        }
    });
    let (_, replacements, cause) = state.into_inner().unwrap();
    let cause = cause.or_else(|| context_error(cancel).err());
    match apply_replacements(svg, matches, &replacements, MAX_OUTPUT_BYTES, &|| {
        context_error(cancel)
    }) {
        Ok(output) => (output, cause),
        Err(error) => (
            svg.to_vec(),
            Some(cause.map_or_else(|| error.clone(), |cause| format!("{error}\n{cause}"))),
        ),
    }
}

/// BundleLocal / BundleLocalWithPolicy (imgbundler.go:84–118). Each deprecated
/// wrapper invocation owns a new resolver and, when requested, a new cache.
/// Passing ImageLocalFiles::default() denies all local access.
pub fn bundle_local_with_policy(
    input_path: &std::path::Path,
    svg: &[u8],
    files: d2_rs_scenebuild::ImageLocalFiles,
    cache_images: bool,
    cancel: Option<&d2_rs_core::CancellationToken>,
) -> (Vec<u8>, Option<String>) {
    use d2_rs_scenebuild::{ImageMemoryCache, RasterImageResolver};
    let prefix = |error| format!("failed to bundle local images: {error}");
    let base = if input_path.as_os_str().is_empty() || input_path == std::path::Path::new("-") {
        files.root_path().unwrap_or(std::path::Path::new("."))
    } else {
        input_path.parent().unwrap_or(std::path::Path::new("."))
    };
    let limits = legacy_image_limits();
    let resolver = (|| {
        let resolver = RasterImageResolver::new(base, limits, std::sync::Arc::new(files.clone()))?;
        if cache_images {
            resolver.with_shared_cache(
                std::sync::Arc::new(ImageMemoryCache::new(
                    MAX_REFERENCES as i64,
                    MAX_BUNDLED_BYTES,
                )?),
                "imgbundler/imageasset/v2/default-http",
                false,
            )
        } else {
            Ok(resolver)
        }
    })();
    let resolver = match resolver {
        Ok(resolver) => resolver,
        Err(e) => return (svg.to_vec(), Some(prefix(e.message))),
    };
    let (output, error) = bundle_with_resolver(
        svg,
        Options {
            local: true,
            remote: false,
        },
        Some(&resolver),
        cancel,
    );
    (output, error.map(prefix))
}

/// BundleRemote / BundleRemoteWithPolicy (imgbundler.go:121–148). A caller's
/// opt-in is scoped to this invocation, never a process-wide HTTP policy.
pub fn bundle_remote_with_policy(
    svg: &[u8],
    cache_images: bool,
    allow_private_networks: bool,
    cancel: Option<&d2_rs_core::CancellationToken>,
) -> (Vec<u8>, Option<String>) {
    use d2_rs_scenebuild::{
        ImageLocalFiles, ImageMemoryCache, NativeImageHttpClient, RasterImageResolver,
    };
    let prefix = |error| format!("failed to bundle remote images: {error}");
    let resolver = (|| {
        let resolver = RasterImageResolver::new(
            std::path::Path::new("."),
            legacy_image_limits(),
            std::sync::Arc::new(ImageLocalFiles::default()),
        )?
        .with_http_client(std::sync::Arc::new(NativeImageHttpClient {
            allow_private_networks,
        }));
        if cache_images {
            resolver.with_shared_cache(
                std::sync::Arc::new(ImageMemoryCache::new(
                    MAX_REFERENCES as i64,
                    MAX_BUNDLED_BYTES,
                )?),
                "imgbundler/imageasset/v2/default-http",
                allow_private_networks,
            )
        } else {
            Ok(resolver)
        }
    })();
    let resolver = match resolver {
        Ok(resolver) => resolver,
        Err(e) => return (svg.to_vec(), Some(prefix(e.message))),
    };
    let (output, error) = bundle_with_resolver(
        svg,
        Options {
            local: false,
            remote: true,
        },
        Some(&resolver),
        cancel,
    );
    (output, error.map(prefix))
}

fn legacy_image_limits() -> d2_rs_scenebuild::ImageAssetLimits {
    d2_rs_scenebuild::ImageAssetLimits {
        max_fetched_bytes: 1 << 25,
        max_encoded_bytes: 1 << 25,
        max_decompressed_bytes: 1 << 25,
        max_svg_bytes: 1 << 25,
        max_decoded_width: 32768,
        max_decoded_height: 32768,
        max_decoded_pixels: 64 << 20,
        max_assets: MAX_REFERENCES as i64,
        max_cumulative_encoded_bytes: MAX_BUNDLED_BYTES,
        max_cumulative_decoded_bytes: MAX_BUNDLED_BYTES,
    }
}