Store a WASM binary in the system catalog, returning its content hash. Content-addressed: if the same binary is uploaded twice, it's stored once. The hash is used as the key for deduplication and cache lookup.
(
catalog: &crate::control::security::catalog::types::SystemCatalog,
wasm_bytes: &[u8],
max_size: usize,
)
| 13 | /// Content-addressed: if the same binary is uploaded twice, it's stored once. |
| 14 | /// The hash is used as the key for deduplication and cache lookup. |
| 15 | pub fn store_wasm_binary( |
| 16 | catalog: &crate::control::security::catalog::types::SystemCatalog, |
| 17 | wasm_bytes: &[u8], |
| 18 | max_size: usize, |
| 19 | ) -> crate::Result<String> { |
| 20 | if wasm_bytes.is_empty() { |
| 21 | return Err(crate::Error::BadRequest { |
| 22 | detail: "WASM binary is empty".into(), |
| 23 | }); |
| 24 | } |
| 25 | if wasm_bytes.len() > max_size { |
| 26 | return Err(crate::Error::BadRequest { |
| 27 | detail: format!( |
| 28 | "WASM binary exceeds maximum size ({} bytes > {max_size} bytes)", |
| 29 | wasm_bytes.len() |
| 30 | ), |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | // Validate WASM magic number: \0asm |
| 35 | if wasm_bytes.len() < 4 || &wasm_bytes[..4] != b"\0asm" { |
| 36 | return Err(crate::Error::BadRequest { |
| 37 | detail: "invalid WASM binary: missing \\0asm magic header".into(), |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | let hash = sha256_hex(wasm_bytes); |
| 42 | let key = format!("wasm_module:{hash}"); |
| 43 | |
| 44 | catalog |
| 45 | .put_raw(key.as_bytes(), wasm_bytes) |
| 46 | .map_err(|e| crate::Error::Internal { |
| 47 | detail: format!("failed to store WASM binary: {e}"), |
| 48 | })?; |
| 49 | |
| 50 | Ok(hash) |
| 51 | } |
| 52 | |
| 53 | /// Load a WASM binary from the system catalog by its content hash. |
| 54 | pub fn load_wasm_binary( |
no test coverage detected