@@ -7,13 +7,16 @@ use spin_manifest::schema::v2::TargetEnvironmentRef;
77const DEFAULT_REGISTRY : & str = "fermyon.com" ;
88
99/// Loads the given `TargetEnvironment` from a registry.
10- pub async fn load_environment ( env_id : & TargetEnvironmentRef ) -> anyhow:: Result < TargetEnvironment > {
10+ pub async fn load_environment (
11+ env_id : & TargetEnvironmentRef ,
12+ cache : & spin_loader:: cache:: Cache ,
13+ ) -> anyhow:: Result < TargetEnvironment > {
1114 match env_id {
1215 TargetEnvironmentRef :: DefaultRegistry ( package) => {
13- load_environment_from_registry ( DEFAULT_REGISTRY , package) . await
16+ load_environment_from_registry ( DEFAULT_REGISTRY , package, cache ) . await
1417 }
1518 TargetEnvironmentRef :: Registry { registry, package } => {
16- load_environment_from_registry ( registry, package) . await
19+ load_environment_from_registry ( registry, package, cache ) . await
1720 }
1821 TargetEnvironmentRef :: WitDirectory { path } => load_environment_from_dir ( path) ,
1922 }
@@ -22,23 +25,32 @@ pub async fn load_environment(env_id: &TargetEnvironmentRef) -> anyhow::Result<T
2225async fn load_environment_from_registry (
2326 registry : & str ,
2427 env_id : & str ,
28+ cache : & spin_loader:: cache:: Cache ,
2529) -> anyhow:: Result < TargetEnvironment > {
2630 use futures_util:: TryStreamExt ;
2731
32+ let cache_file = cache. wit_path ( registry, env_id) ;
33+ if cache. wit_path ( registry, env_id) . exists ( ) {
34+ // Failure to read from cache is not fatal - fall through to origin.
35+ if let Ok ( bytes) = tokio:: fs:: read ( cache_file) . await {
36+ return TargetEnvironment :: from_package_bytes ( env_id, bytes) ;
37+ }
38+ }
39+
2840 let ( pkg_name, pkg_ver) = env_id. split_once ( '@' ) . with_context ( || format ! ( "Failed to parse target environment {env_id} as package reference - is the target correct?" ) ) ?;
2941 let env_pkg_ref: wasm_pkg_loader:: PackageRef = pkg_name
3042 . parse ( )
3143 . with_context ( || format ! ( "Environment {pkg_name} is not a valid package name" ) ) ?;
3244
33- let registry : wasm_pkg_loader:: Registry = registry
45+ let wkg_registry : wasm_pkg_loader:: Registry = registry
3446 . parse ( )
3547 . with_context ( || format ! ( "Registry {registry} is not a valid registry name" ) ) ?;
3648
3749 // TODO: this requires wkg configuration which shouldn't be on users:
3850 // is there a better way to handle it?
3951 let mut wkg_config = wasm_pkg_loader:: Config :: global_defaults ( )
4052 . unwrap_or_else ( |_| wasm_pkg_loader:: Config :: empty ( ) ) ;
41- wkg_config. set_package_registry_override ( env_pkg_ref, registry ) ;
53+ wkg_config. set_package_registry_override ( env_pkg_ref, wkg_registry ) ;
4254
4355 let mut client = wasm_pkg_loader:: Client :: new ( wkg_config) ;
4456
@@ -64,7 +76,9 @@ async fn load_environment_from_registry(
6476 . with_context ( || format ! ( "Failed to get {env_id} package data from registry" ) ) ?
6577 . to_vec ( ) ;
6678
67- TargetEnvironment :: from_package_bytes ( env_id. to_owned ( ) , bytes)
79+ _ = cache. write_wit ( & bytes, registry, env_id) . await ; // Failure to cache is not fatal
80+
81+ TargetEnvironment :: from_package_bytes ( env_id, bytes)
6882}
6983
7084fn load_environment_from_dir ( path : & Path ) -> anyhow:: Result < TargetEnvironment > {
@@ -93,7 +107,7 @@ pub struct TargetEnvironment {
93107}
94108
95109impl TargetEnvironment {
96- fn from_package_bytes ( name : String , bytes : Vec < u8 > ) -> anyhow:: Result < Self > {
110+ fn from_package_bytes ( name : & str , bytes : Vec < u8 > ) -> anyhow:: Result < Self > {
97111 let decoded = wit_component:: decode ( & bytes)
98112 . with_context ( || format ! ( "Failed to decode package for environment {name}" ) ) ?;
99113 let package_id = decoded. package ( ) ;
@@ -107,7 +121,7 @@ impl TargetEnvironment {
107121 . clone ( ) ;
108122
109123 Ok ( Self {
110- name,
124+ name : name . to_owned ( ) ,
111125 decoded,
112126 package,
113127 package_id,
0 commit comments