zlayer-registry
OCI image pulling, caching, and WASM artifact detection for ZLayer.
Features
- Image Pulling: Pull OCI images from container registries
- Blob Caching: Local caching of image layers for faster subsequent pulls
- WASM Detection: Automatic detection of WASM artifacts vs container images
- Authentication: Support for registry authentication
Feature Flags
persistent: Enable persistent disk-based blob cache using SQLite (via SQLx)
s3: Enable S3-compatible storage backend for blob caching
local: Enable local OCI registry for storing built images
Installation
Add to your Cargo.toml:
[dependencies]
zlayer-registry = { path = "../zlayer-registry" }
zlayer-registry = { path = "../zlayer-registry", features = ["persistent", "local"] }
Usage
Basic Image Pulling
use zlayer_registry::{ImagePuller, BlobCache, RegistryAuth};
use std::path::Path;
async fn pull_image() -> Result<(), Box<dyn std::error::Error>> {
let cache = BlobCache::open(Path::new("/var/lib/zlayer/cache.sqlite"))?;
let puller = ImagePuller::new(cache);
let auth = RegistryAuth::Anonymous;
let (manifest, layers) = puller.pull("ghcr.io/org/image:latest", &auth).await?;
println!("Pulled image with {} layers", layers.len());
Ok(())
}
WASM Artifact Detection
The registry module can automatically detect whether an OCI artifact is a WebAssembly module or a traditional container image.
Types
use zlayer_registry::wasm::{ArtifactType, WasiVersion, WasmArtifactInfo};
enum ArtifactType {
Container, Wasm { wasi_version: WasiVersion }, }
enum WasiVersion {
Preview1, Preview2, Unknown, }
struct WasmArtifactInfo {
wasi_version: WasiVersion,
wasm_layer_digest: Option<String>,
wasm_size: Option<i64>,
module_name: Option<String>,
}
Detection
use zlayer_registry::wasm::{detect_artifact_type, extract_wasm_info, ArtifactType};
use oci_client::manifest::OciImageManifest;
fn check_artifact(manifest: &OciImageManifest) {
let artifact_type = detect_artifact_type(manifest);
match artifact_type {
ArtifactType::Container => {
println!("This is a container image");
}
ArtifactType::Wasm { wasi_version } => {
println!("This is a WASM artifact with WASI {}", wasi_version);
if let Some(info) = extract_wasm_info(manifest) {
println!("WASM layer digest: {:?}", info.wasm_layer_digest);
println!("WASM size: {:?} bytes", info.wasm_size);
println!("Module name: {:?}", info.module_name);
}
}
}
}
Detection Strategy
WASM artifacts are detected through multiple signals in order of specificity:
-
artifact_type field (OCI 1.1+): Most explicit indicator
application/vnd.wasm.component.v1+wasm -> wasip2
application/vnd.wasm.module.v1+wasm -> wasip1
-
config.media_type field: WASM-specific config types
application/vnd.wasm.config.v1+json
application/vnd.wasm.config.v0+json
-
Layer media types: WASM binary layers
application/vnd.wasm.content.layer.v1+wasm
application/wasm
Pulling WASM Artifacts
use zlayer_registry::{ImagePuller, BlobCache, RegistryAuth};
async fn pull_wasm(image: &str) -> Result<(), Box<dyn std::error::Error>> {
let cache = BlobCache::open("/var/lib/zlayer/cache.sqlite")?;
let puller = ImagePuller::new(cache);
let auth = RegistryAuth::Anonymous;
let (wasm_bytes, wasm_info) = puller.pull_wasm(image, &auth).await?;
println!("Pulled WASM module: {} bytes", wasm_bytes.len());
println!("WASI version: {}", wasm_info.wasi_version);
Ok(())
}
Getting WASM Info Without Pulling
use zlayer_registry::{ImagePuller, BlobCache, RegistryAuth};
async fn check_wasm(image: &str) -> Result<(), Box<dyn std::error::Error>> {
let cache = BlobCache::open("/var/lib/zlayer/cache.sqlite")?;
let puller = ImagePuller::new(cache);
let auth = RegistryAuth::Anonymous;
if let Some(info) = puller.get_wasm_info(image, &auth).await? {
println!("Image is a WASM artifact:");
println!(" WASI version: {}", info.wasi_version);
println!(" Layer digest: {:?}", info.wasm_layer_digest);
println!(" Size: {:?} bytes", info.wasm_size);
} else {
println!("Image is a container (not WASM)");
}
Ok(())
}
Module Structure
client.rs: OCI distribution client for pulling manifests and blobs
cache.rs: In-memory blob cache implementation
persistent_cache.rs: Persistent disk-based cache (feature: persistent)
s3_cache.rs: S3-compatible storage backend (feature: s3)
unpack.rs: OCI layer unpacking utilities
wasm.rs: WASM artifact detection and types
local_registry.rs: Local OCI registry (feature: local)
oci_export.rs: OCI image export/import (feature: local)
Media Types
WASM Config Media Types
| Media Type |
Description |
application/vnd.wasm.config.v1+json |
Standard WASM config |
application/vnd.wasm.config.v0+json |
Legacy WASM config |
WASM Layer Media Types
| Media Type |
Description |
application/vnd.wasm.content.layer.v1+wasm |
Standard WASM layer |
application/wasm |
Generic WASM binary |
WASM Artifact Types (OCI 1.1+)
| Media Type |
WASI Version |
application/vnd.wasm.component.v1+wasm |
Preview 2 (wasip2) |
application/vnd.wasm.module.v1+wasm |
Preview 1 (wasip1) |
Helper Methods
ArtifactType
impl ArtifactType {
fn is_wasm(&self) -> bool;
fn is_container(&self) -> bool;
fn wasi_version(&self) -> Option<&WasiVersion>;
}
WasiVersion
impl WasiVersion {
fn is_preview1(&self) -> bool;
fn is_preview2(&self) -> bool;
fn target_triple_suffix(&self) -> &'static str;
}
Examples
Detecting WASM in a Pipeline
use zlayer_registry::wasm::{detect_artifact_type, ArtifactType};
use zlayer_registry::{ImagePuller, BlobCache, RegistryAuth};
async fn handle_image(image: &str) -> Result<(), Box<dyn std::error::Error>> {
let cache = BlobCache::open("/var/lib/zlayer/cache.sqlite")?;
let puller = ImagePuller::new(cache);
let auth = RegistryAuth::Anonymous;
let manifest = puller.fetch_manifest(image, &auth).await?;
match detect_artifact_type(&manifest) {
ArtifactType::Container => {
pull_and_run_container(image).await?;
}
ArtifactType::Wasm { wasi_version } => {
let (wasm_bytes, _) = puller.pull_wasm(image, &auth).await?;
run_wasm_module(&wasm_bytes, wasi_version).await?;
}
}
Ok(())
}
License
Apache-2.0