Skip to content

KristofferStrube/Blazor.CompressionStreams

Repository files navigation

License: MIT GitHub issues GitHub forks GitHub stars

NuGet Downloads (official NuGet)

Introduction

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.

Demo

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.

Getting Started

The package can be used in Blazor projects.

Prerequisites

You need to install .NET 7.0 or newer to use the library.

Download .NET 7

Installation

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

Import

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

Creating wrapper instance

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);

Using CompressionStream and DecompressionStream

The primary usage of the CompressionStreams and DecompressionStreams 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.

Issues

Feel free to open issues on the repository if you find any errors with the package or have wishes for features.

Related articles

This repository was build with inspiration and help from the following series of articles:

About

A Blazor wrapper for the Compression Streams browser API.

Resources

License

Stars

Watchers

Forks