AssetArchive class

Namespace Runic.Assets in Runic.Assets 0.23.0

public static class AssetArchive

Reads and writes the portable Runic Assets ZIP archive format.

Fields

CurrentVersion

public const string CurrentVersion = "runic.assets.archive/1"

The archive contract written to runic-assets.json when no two assets have the same content. An archive that stores shared content once declares runic.assets.archive/2; readers accept both.

DefaultResourceName

public const string DefaultResourceName = "Runic.Assets.StaticFiles"

The default manifest-resource name generated by Runic Assets' build target.

Methods

Inspect

public static AssetArchiveInspection Inspect(Stream source, AssetArchiveReadOptions? options = null)

Validates an archive and returns its canonical, deterministic manifest inspection.

Read

public static AssetArchiveSource Read(Stream source, AssetArchiveReadOptions? options = null)

Loads and validates a portable Runic Assets archive into memory.

ReadAsync

public static ValueTask<AssetArchiveSource> ReadAsync(Stream source, AssetArchiveReadOptions? options = null, CancellationToken cancellationToken = default)

Loads and validates a portable Runic Assets archive into memory, reading source asynchronously. The limits and validation are those of AssetArchive.Read.

Parameters

source

A readable stream positioned at the start of the archive; it need not be seekable.

options

The limits for untrusted input; null uses the defaults.

cancellationToken

Cancels the read and the validation.

Returns

The validated in-memory source.

Exceptions

InvalidDataException

The archive is invalid or exceeds a limit of options.

ReadEmbedded

public static AssetArchiveSource ReadEmbedded(Assembly assembly, string resourceName = "Runic.Assets.StaticFiles", AssetArchiveReadOptions? options = null)

Loads a canonical Runic Assets archive from an assembly manifest resource. The manifest and the ZIP directory are read and checked at once; each asset is inflated when it is first opened and then kept in memory.

Parameters

assembly

The assembly that holds the resource.

resourceName

The manifest resource name; AssetArchive.DefaultResourceName by default.

options

The limits; null uses the defaults.

Returns

The source, which reads its assets from the resource.

Exceptions

ArgumentException

The assembly has no such resource.

InvalidDataException

The manifest or the ZIP directory is invalid or exceeds a limit of options.

Remarks

Since 0.15 an embedded archive is not checked against its SHA-256 digests when it is loaded or opened. It is part of the assembly that holds the application's code, so whoever can change its bytes can change that code too, and the Runic.Assets build verified it when it packed or embedded it (RunicAssetsDist, RunicAssetsEmbeddedArchive). An asset whose inflated length differs from its declared length, or whose ZIP entry is damaged, fails to open with InvalidDataException.

AssetArchiveSource.ValidateAsync checks every asset against its digest on demand, for a resource that another build step embedded. AssetArchive.Read and AssetArchive.ReadAsync, which read an archive from outside the assembly such as a file, check every digest before they return.

WriteAsync

public static ValueTask WriteAsync(IAssetSource source, Stream destination, CancellationToken cancellationToken = default)

Writes a deterministic, portable ZIP archive from one validated source.

WriteDirectoryAsync

public static ValueTask WriteDirectoryAsync(string sourceDirectory, Stream destination, string entryPointRelativePath = "index.html", IEnumerable<string>? excludedPaths = null, CancellationToken cancellationToken = default)

Writes a canonical archive from a local directory using the same manifest, validation, media-type, cache, and ZIP writer authority as every other archive producer.

Remarks

This overload packs every regular file that excludedPaths does not name, including .git directories and .env files. The overload that takes AssetArchiveDirectoryOptions leaves them out by default.

WriteDirectoryAsync

public static ValueTask WriteDirectoryAsync(string sourceDirectory, Stream destination, AssetArchiveDirectoryOptions options, CancellationToken cancellationToken)

Writes a canonical archive from a local directory through pinned no-follow Linux handles, with the entry point and exclusions of options. By default .git directories and files whose names start with .env are left out at any depth.

Parameters

sourceDirectory

The directory to pack.

destination

A writable stream that receives the archive.

options

The entry point and the paths to leave out.

cancellationToken

Cancels the scan and the write.

Exceptions

PlatformNotSupportedException

The operating system is not Linux.

WriteTrustedDirectoryAsync

public static ValueTask WriteTrustedDirectoryAsync(string sourceDirectory, Stream destination, AssetArchiveDirectoryOptions? options = null, CancellationToken cancellationToken = default)

Writes a canonical archive from a directory that the same trusted build produced, on every operating system. By default .git directories and files whose names start with .env are left out at any depth.

Parameters

sourceDirectory

The directory to pack.

destination

A writable stream that receives the archive.

options

The entry point and the paths to leave out; null uses the defaults.

cancellationToken

Cancels the scan and the write.

Remarks

The directory is read with ordinary file APIs into memory before the archive is written. Symbolic links, and on Windows every reparse point such as a junction, that the scan observes are rejected with InvalidDataException. The snapshot is not safe against a process that changes the directory while it is read; use AssetArchive.WriteDirectoryAsync on Linux for a directory that a less trusted process can write.