diff --git a/wrappers/Cargo.toml b/wrappers/Cargo.toml index 78a1a953..d956f062 100644 --- a/wrappers/Cargo.toml +++ b/wrappers/Cargo.toml @@ -139,6 +139,7 @@ wasm_fdw = [ "serde", "serde_json", "jwt-simple", + "bytes", ] # Does not include helloworld_fdw because of its general uselessness native_fdws = [ @@ -241,7 +242,7 @@ jwt-simple = { version = "0.12.9", default-features = false, features = [ dirs = { version = "5.0.1", optional = true } sha2 = { version = "0.10.8", optional = true } hex = { version = "0.4.3", optional = true } -bytes = "1.9.0" +bytes = { version = "1.9.0", optional = true } thiserror = { version = "1.0.48", optional = true } anyhow = { version = "1.0.81", optional = true } diff --git a/wrappers/src/fdw/wasm_fdw/README.md b/wrappers/src/fdw/wasm_fdw/README.md index edefea29..06100a0e 100644 --- a/wrappers/src/fdw/wasm_fdw/README.md +++ b/wrappers/src/fdw/wasm_fdw/README.md @@ -6,6 +6,7 @@ This is Wasm foreign data wrapper host, please visit each Wasm foreign data wrap | Version | Date | Notes | | ------- | ---------- | ---------------------------------------------------- | +| 0.1.4 | 2024-12-09 | Improve remote wasm downloading and caching | | 0.1.3 | 2024-09-30 | Support for pgrx 0.12.6 | | 0.1.2 | 2024-07-07 | Add fdw_package_checksum server option | | 0.1.1 | 2024-07-05 | Fix missing wasm package cache dir issue | diff --git a/wrappers/src/fdw/wasm_fdw/wasm_fdw.rs b/wrappers/src/fdw/wasm_fdw/wasm_fdw.rs index aa56d800..5e991f64 100644 --- a/wrappers/src/fdw/wasm_fdw/wasm_fdw.rs +++ b/wrappers/src/fdw/wasm_fdw/wasm_fdw.rs @@ -51,17 +51,17 @@ fn download_component( version: &str, checksum: Option<&str>, ) -> WasmFdwResult { - // Handle local file paths + // handle local file paths if let Some(file_path) = url.strip_prefix("file://") { return load_component_from_file(engine, file_path); } - // Handle warg registry URLs + // handle warg registry URLs if url.starts_with("warg://") || url.starts_with("wargs://") { - return Ok(download_from_warg(rt, engine, url, name, version)?); + return download_from_warg(rt, engine, url, name, version); } - // Handle direct URLs with caching + // handle direct URLs with caching download_from_url(rt, engine, url, name, version, checksum) } @@ -88,10 +88,10 @@ fn download_from_warg( ))?; let pkg_name = warg_protocol::registry::PackageName::new(name) - .map_err(|e| format!("Invalid package name '{}': {}", name, e))?; + .map_err(|e| format!("invalid package name '{}': {}", name, e))?; let ver = semver::VersionReq::parse(version) - .map_err(|e| format!("Invalid version requirement '{}': {}", version, e))?; + .map_err(|e| format!("invalid version requirement '{}': {}", version, e))?; let pkg = rt .block_on(client.download(&pkg_name, &ver))? @@ -108,34 +108,34 @@ fn download_from_url( version: &str, checksum: Option<&str>, ) -> WasmFdwResult { - // Validate URL + // validate URL let url = url .parse::() - .map_err(|e| format!("Invalid URL '{}': {}", url, e))?; + .map_err(|e| format!("invalid URL '{}': {}", url, e))?; - // Calculate cache path + // calculate cache path let cache_path = get_cache_path(url.as_str(), name, version)?; - // Return cached component if it exists and is valid + // return cached component if it exists and is valid if cache_path.exists() { if let Ok(component) = load_component_from_file(engine, &cache_path) { return Ok(component); } - // If loading fails, remove invalid cache file + // if loading fails, remove invalid cache file let _ = fs::remove_file(&cache_path); } - // Ensure checksum is provided for remote downloads + // ensure checksum is provided for remote downloads let checksum = checksum - .ok_or_else(|| "Package checksum must be specified for remote downloads".to_string())?; + .ok_or_else(|| "package checksum must be specified for remote downloads".to_string())?; - // Download and verify component + // download and verify component let bytes = download_and_verify(rt, url, checksum)?; - // Save to cache + // save to cache save_to_cache(&cache_path, &bytes)?; - // Load component + // load component load_component_from_file(engine, &cache_path).inspect_err(|_| { let _ = fs::remove_file(&cache_path); }) @@ -151,7 +151,7 @@ fn get_cache_path(url: &str, name: &str, version: &str) -> WasmFdwResult WasmFdwResult<()> { if let Some(parent) = path.parent() { - fs::create_dir_all(parent).map_err(|_| "Cache access error".to_string())?; + fs::create_dir_all(parent).map_err(|_| "cache access error".to_string())?; } - fs::write(path, bytes).map_err(|_| "Cache write error".to_string())?; + fs::write(path, bytes).map_err(|_| "cache write error".to_string())?; Ok(()) } #[wrappers_fdw( - version = "0.1.3", + version = "0.1.4", author = "Supabase", website = "https://github.com/supabase/wrappers/tree/main/wrappers/src/fdw/wasm_fdw", error_type = "WasmFdwError"