A Blazor wrapper for the browser API Compression Streams
The API defines way to compress and decompress streams of binary data. This project implements a wrapper around the API for Blazor so that we can easily and safely compress and decompress streams.
The sample project can be demoed at https://kristofferstrube.github.io/Blazor.CompressionStreams/
On each page you can find the corresponding code for the example in the top right corner.
On the API Coverage Status page you can see how much of the WebIDL specs this wrapper has covered.
The package can be used in Blazor projects.
You need to install .NET 7.0 or newer to use the library.
You can install the package via Nuget with the Package Manager in your IDE or alternatively using the command line:
dotnet add package KristofferStrube.Blazor.CompressionStreams
You need to reference the package in order to use it in your pages. This can be done in _Import.razor
by adding the following.
@using KristofferStrube.Blazor.CompressionStreams
You can create wrapper instances for CompressionStream
and DecompressionStream
with the two following sets of constructors which either take a format or a reference to an existing JS CompressionStream.
// Compression Stream constructions
var newCompressionStream = await CompressionStream.CreateAsync(JSRuntime, CompressionAlgorithm.Deflate);
IJSObjectReference jSCompressionStream;
var existingCompressionStream = await CompressionStream.CreateAsync(JSRuntime, jSCompressionStream);
// Decompression Stream constructions
var newDecompressionStream = await DecompressionStream.CreateAsync(JSRuntime, CompressionAlgorithm.Deflate);
IJSObjectReference jSDecompressionStream;
var existingDecompressionStream = await DecompressionStream.CreateAsync(JSRuntime, jSDecompressionStream);
The primary usage of the CompressionStream
s and DecompressionStream
s are in combination with the Blazor.Streams library where the ReadableStream
can be piped through any stream that implements the IGenericTransformStream
interface which both CompressionStream
and DecompressionStream
does.
ReadableStream readableStream;
ReadableStream compressed = await readableStream.PipeThroughAsync(newCompressionStream);
ReadableStream decompressed = await compressed.PipeThroughAsync(newDecompressionStream);
This can be useful if you have limited bandwidth and want to stream some content.
Feel free to open issues on the repository if you find any errors with the package or have wishes for features.
This repository was build with inspiration and help from the following series of articles: