junobuild_storage/
certified_assets.rs1use 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 extend_certified_assets(asset_hashes, config, storage_state);
19
20 init_certified_assets(asset_hashes);
22
23 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 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 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 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}