Skip to main content

junobuild_storage/
certified_assets.rs

1use crate::certification::types::certified::CertifiedAssetHashes;
2use crate::memory::STATE;
3use crate::rewrites::rewrite_source_to_path;
4use crate::routing::get_routing;
5use crate::runtime::init_certified_assets;
6use crate::strategies::{StorageCertificateStrategy, StorageStateStrategy};
7use crate::types::config::StorageConfig;
8use crate::types::http_request::{Routing, RoutingDefault};
9use crate::types::interface::{CertifyAssetsCursor, CertifyAssetsStrategy};
10
11pub fn extend_and_init_certified_assets(
12    asset_hashes: &mut CertifiedAssetHashes,
13    config: &StorageConfig,
14    storage_state: &impl StorageStateStrategy,
15    certificate: &impl StorageCertificateStrategy,
16) {
17    // 1. Extend with rewrite etc.
18    extend_certified_assets(asset_hashes, config, storage_state);
19
20    // 2. Save the asset tree in the runtime heap
21    init_certified_assets(asset_hashes);
22
23    // 3. Update the root hash and the canister certified data
24    certificate.update_certified_data();
25}
26
27pub fn certify_assets_chunk<F>(
28    strategy: CertifyAssetsStrategy,
29    config: &StorageConfig,
30    storage_state: &impl StorageStateStrategy,
31    certificate: &impl StorageCertificateStrategy,
32    insert: F,
33) -> Option<CertifyAssetsCursor>
34where
35    F: FnOnce(&mut CertifiedAssetHashes) -> Option<CertifyAssetsCursor>,
36{
37    // 1. Borrow or re-create the asset tree
38    let next_cursor = STATE.with(|state| {
39        let asset_hashes = &mut state.borrow_mut().runtime.storage.asset_hashes;
40
41        if let CertifyAssetsStrategy::Clear = strategy {
42            *asset_hashes = CertifiedAssetHashes::default();
43        }
44
45        let next_cursor = insert(asset_hashes);
46
47        // 2. Extend with rewrite etc.
48        if next_cursor.is_none() {
49            if let CertifyAssetsStrategy::AppendWithRouting = strategy {
50                extend_certified_assets(asset_hashes, config, storage_state);
51            }
52        }
53
54        next_cursor
55    });
56
57    // 3. Update the root hash and the canister certified data
58    certificate.update_certified_data();
59
60    next_cursor
61}
62
63fn extend_certified_assets(
64    asset_hashes: &mut CertifiedAssetHashes,
65    config: &StorageConfig,
66    storage_state: &impl StorageStateStrategy,
67) {
68    for (source, destination) in config.rewrites.clone() {
69        if let Ok(Routing::Default(RoutingDefault { url: _, asset })) =
70            get_routing(destination, &Vec::new(), false, storage_state)
71        {
72            let src_path = rewrite_source_to_path(&source);
73
74            if let Some((asset, _)) = asset {
75                asset_hashes.insert_rewrite_v2(&src_path, &asset, config);
76            }
77        }
78    }
79
80    for (source, redirect) in config.unwrap_redirects() {
81        asset_hashes.insert_redirect_v2(
82            &source,
83            redirect.status_code,
84            &redirect.location,
85            &config.unwrap_iframe(),
86        );
87    }
88}