typst-as-lib 0.15.5

Small wrapper for typst that makes it easier to use it as a templating engine
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use std::{
    borrow::Cow,
    collections::HashMap,
    io::Read,
    path::{Path, PathBuf},
    sync::{Arc, Mutex},
};

use binstall_tar::Archive;
use ecow::eco_format;
use flate2::read::GzDecoder;
use typst::{
    diag::{FileError, FileResult, PackageError},
    foundations::Bytes,
    syntax::{FileId, Source, VirtualPath, package::PackageSpec},
};

use crate::{
    cached_file_resolver::{CachedFileResolver, IntoCachedFileResolver},
    file_resolver::{DEFAULT_PACKAGES_SUBDIR, FileResolver},
    util::{bytes_to_source, not_found},
};

// https://github.com/typst/typst/blob/16736feb13eec87eb9ca114deaeb4f7eeb7409d2/crates/typst-kit/src/package.rs#L15
/// The default Typst registry.
static PACKAGE_REPOSITORY_URL: &str = "https://packages.typst.org";

static REQUEST_RETRY_COUNT: u32 = 3;

/// Builder for constructing a [`PackageResolver`].
#[derive(Debug, Clone, Default)]
pub struct PackageResolverBuilder<C = ()> {
    #[cfg(feature = "ureq")]
    ureq: Option<ureq::Agent>,
    #[cfg(feature = "reqwest")]
    reqwest: Option<reqwest::blocking::Client>,
    cache: C,
    request_retry_count: Option<u32>,
}

impl PackageResolverBuilder<()> {
    /// Creates a new builder.
    #[deprecated(since = "0.14.0", note = "Use `PackageResolver::builder()` instead")]
    pub fn new() -> PackageResolverBuilder<()> {
        PackageResolverBuilder::default()
    }

    /// Creates a new builder.
    #[deprecated(since = "0.14.1", note = "Use `PackageResolver::builder()` instead")]
    pub fn builder() -> PackageResolverBuilder<()> {
        PackageResolverBuilder::default()
    }
}

impl<C> PackageResolverBuilder<C> {
    /// Sets the number of retry attempts for failed HTTP requests.
    pub fn request_retry_count(mut self, request_retry_count: u32) -> Self {
        self.request_retry_count = Some(request_retry_count);
        self
    }

    /// Sets a custom `ureq` HTTP client.
    #[cfg(feature = "ureq")]
    pub fn ureq_agent(self, ureq: ureq::Agent) -> Self {
        Self {
            ureq: Some(ureq),
            ..self
        }
    }

    /// Sets a custom `reqwest` HTTP client.
    #[cfg(feature = "reqwest")]
    pub fn reqwest_client(self, reqwest: reqwest::blocking::Client) -> Self {
        Self {
            reqwest: Some(reqwest),
            ..self
        }
    }

    /// Sets a custom cache implementation.
    pub fn cache<C1>(self, cache: C1) -> PackageResolverBuilder<C1> {
        let Self {
            request_retry_count,
            #[cfg(feature = "ureq")]
            ureq,
            #[cfg(feature = "reqwest")]
            reqwest,
            ..
        } = self;
        PackageResolverBuilder {
            request_retry_count,
            #[cfg(feature = "ureq")]
            ureq,
            #[cfg(feature = "reqwest")]
            reqwest,
            cache,
        }
    }

    /// Uses the file system for caching packages.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use typst_as_lib::package_resolver::PackageResolver;
    /// let resolver = PackageResolver::builder()
    ///     .with_file_system_cache()
    ///     .build();
    /// ```
    pub fn with_file_system_cache(self) -> PackageResolverBuilder<FileSystemCache> {
        let Self {
            request_retry_count,
            #[cfg(feature = "ureq")]
            ureq,
            #[cfg(feature = "reqwest")]
            reqwest,
            ..
        } = self;
        PackageResolverBuilder {
            request_retry_count,
            #[cfg(feature = "ureq")]
            ureq,
            #[cfg(feature = "reqwest")]
            reqwest,
            cache: FileSystemCache::new(),
        }
    }

    /// Uses in-memory caching for packages.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use typst_as_lib::package_resolver::PackageResolver;
    /// let resolver = PackageResolver::builder()
    ///     .with_in_memory_cache()
    ///     .build();
    /// ```
    pub fn with_in_memory_cache(self) -> PackageResolverBuilder<InMemoryCache> {
        let Self {
            request_retry_count,
            #[cfg(feature = "ureq")]
            ureq,
            #[cfg(feature = "reqwest")]
            reqwest,
            ..
        } = self;
        PackageResolverBuilder {
            request_retry_count,
            #[cfg(feature = "ureq")]
            ureq,
            #[cfg(feature = "reqwest")]
            reqwest,
            cache: InMemoryCache::new(),
        }
    }

    /// Builds the package resolver with the configured options.
    pub fn build(self) -> PackageResolver<C> {
        let Self {
            request_retry_count,
            #[cfg(feature = "ureq")]
            ureq,
            #[cfg(feature = "reqwest")]
            reqwest,
            cache,
        } = self;
        PackageResolver {
            request_retry_count: request_retry_count.unwrap_or(REQUEST_RETRY_COUNT),
            #[cfg(feature = "ureq")]
            ureq: ureq.unwrap_or_else(ureq::Agent::new_with_defaults),
            #[cfg(feature = "reqwest")]
            reqwest: reqwest.unwrap_or_else(reqwest::blocking::Client::default),
            cache,
        }
    }
}

/// Resolves and downloads packages from the Typst package repository.
#[derive(Debug, Clone)]
pub struct PackageResolver<C = ()> {
    #[cfg(feature = "ureq")]
    #[allow(dead_code)]
    ureq: ureq::Agent,
    #[cfg(feature = "reqwest")]
    #[allow(dead_code)]
    reqwest: reqwest::blocking::Client,
    cache: C,
    request_retry_count: u32,
}

impl PackageResolver {
    /// Creates a new builder for configuring a package resolver.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use typst_as_lib::package_resolver::PackageResolver;
    /// let resolver = PackageResolver::builder()
    ///     .with_file_system_cache()
    ///     .build();
    /// ```
    pub fn builder() -> PackageResolverBuilder<()> {
        PackageResolverBuilder::default()
    }
}

impl<C> PackageResolver<C> {
    fn resolve_bytes<T>(&self, id: FileId) -> FileResult<T>
    where
        SourceOrBytesCreator: CreateBytesOrSource<T>,
        C: PackageResolverCache,
    {
        let Self {
            request_retry_count,
            cache,
            ..
        } = self;

        let Some(package) = id.package() else {
            return Err(not_found(id));
        };

        // https://github.com/typst/typst/blob/16736feb13eec87eb9ca114deaeb4f7eeb7409d2/crates/typst-kit/src/package.rs#L102C16-L102C38
        if package.namespace != "preview" {
            return Err(not_found(id));
        }

        if let Ok(Some(cached)) = cache.lookup_cached(package, id) {
            return Ok(cached);
        }

        let PackageSpec {
            namespace,
            name,
            version,
        } = package;

        let url = format!(
            "{}/{}/{}-{}.tar.gz",
            PACKAGE_REPOSITORY_URL, namespace, name, version,
        );

        let mut reader = Err(PackageError::Other(None));
        for i in 0..*request_retry_count {
            reader = self.make_get_request(&url);
            match reader {
                Err(_) => eprintln!("Failed fetching {url} (try {})", i + 1),
                Ok(_) => break,
            }
        }

        let mut d = GzDecoder::new(reader?);
        let mut archive = Vec::new();
        d.read_to_end(&mut archive)
            .map_err(|error| PackageError::MalformedArchive(Some(eco_format!("{error}"))))?;

        let archive = Archive::new(&archive[..]);
        cache.cache_archive(archive, package)?;
        cache
            .lookup_cached(package, id)
            .and_then(|f| f.ok_or_else(|| not_found(id)))
    }

    #[cfg(feature = "ureq")]
    fn make_get_request(&self, url: &str) -> Result<ureq::BodyReader<'static>, PackageError> {
        let Self { ureq, .. } = self;
        let resp = ureq
            .get(url)
            .call()
            .map_err(|err| PackageError::NetworkFailed(Some(eco_format!("{err}"))))?;

        let status = resp.status();
        if status != 200 {
            return Err(PackageError::NetworkFailed(Some(eco_format!(
                "response returned unsuccessful status code {status}"
            ))));
        }
        let (_, body) = resp.into_parts();
        Ok(body.into_reader())
    }

    #[cfg(all(not(feature = "ureq"), feature = "reqwest"))]
    fn make_get_request(
        &self,
        url: &str,
    ) -> Result<bytes::buf::Reader<bytes::Bytes>, PackageError> {
        use bytes::Buf;

        let Self { reqwest, .. } = self;
        let resp = reqwest
            .get(url)
            .send()
            .map_err(|err| PackageError::NetworkFailed(Some(eco_format!("{err}"))))?;

        let status = resp.status();
        if status != 200 {
            return Err(PackageError::NetworkFailed(Some(eco_format!(
                "response returned unsuccessful status code {status}"
            ))));
        }
        let bytes = resp
            .bytes()
            .map_err(|err| PackageError::NetworkFailed(Some(eco_format!("{err}"))))?;
        Ok(bytes.reader())
    }
}

impl<C> FileResolver for PackageResolver<C>
where
    C: PackageResolverCache,
{
    fn resolve_binary(&self, id: FileId) -> FileResult<Cow<'_, Bytes>> {
        let cached: Bytes = self.resolve_bytes(id)?;
        Ok(Cow::Owned(cached))
    }

    fn resolve_source(&self, id: FileId) -> FileResult<Cow<'_, Source>> {
        let cached: Source = self.resolve_bytes(id)?;
        Ok(Cow::Owned(cached))
    }
}

fn compose_cache_file_path(root: &Path, package: &PackageSpec) -> FileResult<PathBuf> {
    let subdir = Path::new(package.namespace.as_str())
        .join(package.name.as_str())
        .join(package.version.to_string());

    Ok(root.join(subdir))
}

trait PackageResolverCache {
    fn lookup_cached<T>(&self, package: &PackageSpec, id: FileId) -> FileResult<Option<T>>
    where
        SourceOrBytesCreator: CreateBytesOrSource<T>;
    fn cache_archive(&self, archive: Archive<&[u8]>, package: &PackageSpec) -> FileResult<()>;
}

/// File system cache for downloaded packages.
///
/// Uses the OS cache directory by default.
#[derive(Debug, Clone)]
pub struct FileSystemCache(pub PathBuf);

impl FileSystemCache {
    /// Creates a new file system cache with the default cache directory.
    pub fn new() -> Self {
        Self::default()
    }
}

impl Default for FileSystemCache {
    fn default() -> Self {
        let cache_dir = dirs::cache_dir()
            .map(Cow::Owned)
            .unwrap_or_else(|| Cow::Borrowed(Path::new(".")));
        let path = cache_dir.join(DEFAULT_PACKAGES_SUBDIR);
        Self(path)
    }
}

impl PackageResolverCache for FileSystemCache {
    fn lookup_cached<T>(&self, package: &PackageSpec, id: FileId) -> FileResult<Option<T>>
    where
        SourceOrBytesCreator: CreateBytesOrSource<T>,
    {
        let FileSystemCache(path) = self;
        let dir = compose_cache_file_path(path, package)?;

        let Some(path) = id.vpath().resolve(&dir) else {
            return Ok(None);
        };
        let content = std::fs::read(&path).map_err(|error| FileError::from_io(error, &path))?;
        let cached = SourceOrBytesCreator.try_create(id, &content)?;
        Ok(Some(cached))
    }

    fn cache_archive(&self, mut archive: Archive<&[u8]>, package: &PackageSpec) -> FileResult<()> {
        let FileSystemCache(path) = self;
        let dir = compose_cache_file_path(path, package)?;
        std::fs::create_dir_all(&dir).map_err(|error| FileError::from_io(error, &dir))?;
        archive
            .unpack(&dir)
            .map_err(|error| FileError::from_io(error, &dir))?;
        Ok(())
    }
}

/// In-memory cache for downloaded packages.
#[derive(Debug, Clone, Default)]
pub struct InMemoryCache(pub Arc<Mutex<HashMap<FileId, Vec<u8>>>>);

impl InMemoryCache {
    /// Creates a new in-memory cache.
    pub fn new() -> Self {
        Self::default()
    }
}

impl PackageResolverCache for InMemoryCache {
    fn lookup_cached<T>(&self, _package: &PackageSpec, id: FileId) -> FileResult<Option<T>>
    where
        SourceOrBytesCreator: CreateBytesOrSource<T>,
    {
        let InMemoryCache(cache) = self;
        let mutex_guard = cache
            .as_ref()
            .lock()
            .map_err(|_| FileError::Other(Some(eco_format!("Could not lock cache"))))?;
        let cached = if let Some(value) = mutex_guard.get(&id) {
            let cached = SourceOrBytesCreator.try_create(id, value)?;
            Some(cached)
        } else {
            None
        };

        Ok(cached)
    }

    fn cache_archive(&self, mut archive: Archive<&[u8]>, package: &PackageSpec) -> FileResult<()> {
        let InMemoryCache(cache) = self;
        let entries = archive
            .entries()
            .map_err(|error| PackageError::MalformedArchive(Some(eco_format!("{error}"))))?;
        for entry in entries {
            let Ok(mut file) = entry else {
                continue;
            };
            let Ok(p) = file.path() else {
                continue;
            };
            let file_id = FileId::new(Some(package.clone()), VirtualPath::new(p));
            let mut buf = Vec::new();
            let Ok(_) = file.read_to_end(&mut buf) else {
                continue;
            };
            let mut mutex_guard = cache
                .lock()
                .map_err(|_| FileError::Other(Some(eco_format!("Could not lock cache"))))?;
            mutex_guard.insert(file_id, buf);
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Copy)]
struct SourceOrBytesCreator;

trait CreateBytesOrSource<T> {
    fn try_create(&self, id: FileId, value: &[u8]) -> FileResult<T>;
}

impl CreateBytesOrSource<Source> for SourceOrBytesCreator {
    fn try_create(&self, id: FileId, value: &[u8]) -> FileResult<Source> {
        let source = bytes_to_source(id, value)?;
        Ok(source)
    }
}

impl CreateBytesOrSource<Bytes> for SourceOrBytesCreator {
    fn try_create(&self, _id: FileId, value: &[u8]) -> FileResult<Bytes> {
        Ok(Bytes::new(value.to_vec()))
    }
}

impl IntoCachedFileResolver for PackageResolver<InMemoryCache> {
    fn into_cached(self) -> CachedFileResolver<Self> {
        CachedFileResolver::new(self).with_in_memory_source_cache()
    }
}

impl IntoCachedFileResolver for PackageResolver<FileSystemCache> {
    fn into_cached(self) -> CachedFileResolver<Self> {
        CachedFileResolver::new(self)
            .with_in_memory_source_cache()
            .with_in_memory_binary_cache()
    }
}