Skip to content

Commit 86bbc21

Browse files
refactor(satellite): move common assertion (#1663)
* refactor(satellite): move cdn and storage within a same package * refactor(satellite): move common assertion * chore: merge main
1 parent caaca0c commit 86bbc21

File tree

5 files changed

+51
-49
lines changed

5 files changed

+51
-49
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use crate::assets::cdn::constants::{CDN_JUNO_PATH, CDN_JUNO_RELEASES_COLLECTION_KEY};
2+
use junobuild_cdn::storage::assert_releases_description;
3+
use junobuild_cdn::storage::errors::{
4+
JUNO_CDN_STORAGE_ERROR_INVALID_COLLECTION, JUNO_CDN_STORAGE_ERROR_INVALID_RELEASES_PATH,
5+
};
6+
use junobuild_collections::types::core::CollectionKey;
7+
use junobuild_shared::regex::build_regex;
8+
use junobuild_storage::types::state::FullPath;
9+
10+
pub fn assert_cdn_asset_keys(
11+
full_path: &FullPath,
12+
description: &Option<String>,
13+
collection: &CollectionKey,
14+
) -> Result<(), String> {
15+
match collection.as_str() {
16+
CDN_JUNO_RELEASES_COLLECTION_KEY => {
17+
assert_releases_keys(full_path)?;
18+
assert_releases_description(description)?;
19+
20+
Ok(())
21+
}
22+
_ => {
23+
if full_path.starts_with(CDN_JUNO_PATH) {
24+
return Err(format!(
25+
"{} ({} - {})",
26+
JUNO_CDN_STORAGE_ERROR_INVALID_COLLECTION, full_path, collection
27+
));
28+
}
29+
30+
Ok(())
31+
}
32+
}
33+
}
34+
35+
fn assert_releases_keys(full_path: &FullPath) -> Result<(), String> {
36+
let full_path_re = build_regex(r"^/_juno/releases/satellite[^/]*\.wasm\.gz$")?;
37+
38+
if !full_path_re.is_match(full_path) {
39+
return Err(format!(
40+
"{} ({})",
41+
JUNO_CDN_STORAGE_ERROR_INVALID_RELEASES_PATH, full_path
42+
));
43+
}
44+
45+
Ok(())
46+
}

src/libs/satellite/src/assets/cdn/assert.rs

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use crate::assets::cdn::constants::{CDN_JUNO_PATH, CDN_JUNO_RELEASES_COLLECTION_KEY};
1+
use crate::assets::cdn::constants::CDN_JUNO_RELEASES_COLLECTION_KEY;
22
use candid::Principal;
3-
use junobuild_cdn::storage::assert_releases_description;
4-
use junobuild_cdn::storage::errors::{
5-
JUNO_CDN_STORAGE_ERROR_INVALID_COLLECTION, JUNO_CDN_STORAGE_ERROR_INVALID_RELEASES_PATH,
6-
};
73
use junobuild_collections::assert::stores::{
84
assert_create_permission, assert_create_permission_with, assert_permission,
95
assert_permission_with,
@@ -12,49 +8,7 @@ use junobuild_collections::constants::assets::COLLECTION_ASSET_KEY;
128
use junobuild_collections::types::core::CollectionKey;
139
use junobuild_collections::types::rules::Permission;
1410
use junobuild_shared::controllers::{controller_can_write, is_controller};
15-
use junobuild_shared::regex::build_regex;
1611
use junobuild_shared::types::state::Controllers;
17-
use junobuild_storage::types::state::FullPath;
18-
19-
// TODO: storage module uses cdn because of this reference. Refactor modules.
20-
21-
pub fn assert_cdn_asset_keys(
22-
full_path: &FullPath,
23-
description: &Option<String>,
24-
collection: &CollectionKey,
25-
) -> Result<(), String> {
26-
match collection.as_str() {
27-
CDN_JUNO_RELEASES_COLLECTION_KEY => {
28-
assert_releases_keys(full_path)?;
29-
assert_releases_description(description)?;
30-
31-
Ok(())
32-
}
33-
_ => {
34-
if full_path.starts_with(CDN_JUNO_PATH) {
35-
return Err(format!(
36-
"{} ({} - {})",
37-
JUNO_CDN_STORAGE_ERROR_INVALID_COLLECTION, full_path, collection
38-
));
39-
}
40-
41-
Ok(())
42-
}
43-
}
44-
}
45-
46-
fn assert_releases_keys(full_path: &FullPath) -> Result<(), String> {
47-
let full_path_re = build_regex(r"^/_juno/releases/satellite[^/]*\.wasm\.gz$")?;
48-
49-
if !full_path_re.is_match(full_path) {
50-
return Err(format!(
51-
"{} ({})",
52-
JUNO_CDN_STORAGE_ERROR_INVALID_RELEASES_PATH, full_path
53-
));
54-
}
55-
56-
Ok(())
57-
}
5812

5913
pub fn assert_cdn_write_on_dapp_collection(caller: Principal, controllers: &Controllers) -> bool {
6014
// When using proposals, we allow any controllers to upload - submit - an asset to be served from #dapp collection

src/libs/satellite/src/assets/cdn/strategies_impls/storage.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use crate::assets::assert::assert_cdn_asset_keys;
12
use crate::assets::cdn::assert::{
2-
assert_cdn_asset_keys, assert_cdn_create_permission, assert_cdn_update_permission,
3+
assert_cdn_create_permission, assert_cdn_update_permission,
34
assert_cdn_write_on_dapp_collection, assert_cdn_write_on_system_collection,
45
};
56
use crate::assets::cdn::strategies_impls::cdn::{CdnHeap, CdnStable};

src/libs/satellite/src/assets/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
mod assert;
12
pub mod cdn;
23
pub mod storage;

src/libs/satellite/src/assets/storage/strategy_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::assets::cdn::assert::assert_cdn_asset_keys;
1+
use crate::assets::assert::assert_cdn_asset_keys;
22
use crate::assets::storage::assert::assert_storage_list_permission;
33
use crate::assets::storage::state::{
44
delete_asset, get_asset, get_config, get_domains, get_rule, insert_asset, insert_asset_encoding,

0 commit comments

Comments
 (0)