KristofferStrube.Blazor.Streams 0.4.2

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package KristofferStrube.Blazor.Streams --version 0.4.2
NuGet\Install-Package KristofferStrube.Blazor.Streams -Version 0.4.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="KristofferStrube.Blazor.Streams" Version="0.4.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add KristofferStrube.Blazor.Streams --version 0.4.2
#r "nuget: KristofferStrube.Blazor.Streams, 0.4.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install KristofferStrube.Blazor.Streams as a Cake Addin
#addin nuget:?package=KristofferStrube.Blazor.Streams&version=0.4.2

// Install KristofferStrube.Blazor.Streams as a Cake Tool
#tool nuget:?package=KristofferStrube.Blazor.Streams&version=0.4.2

License: MIT GitHub issues GitHub forks GitHub stars

NuGet Downloads (official NuGet)

Introduction

A Blazor wrapper for the browser API Streams

The API standardizes ways to create, compose, and consume streams of data that map to low-level I/O primitives in the browser. This project implements a wrapper around the API for Blazor so that we can easily and safely interact with the streams of the browser.

Demo

The sample project can be demoed at https://kristofferstrube.github.io/Blazor.Streams/

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 get an overview over what parts of the API we support currently.

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.Streams

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.Streams

Creating wrapper instance

We can call the constructor for ReadableStream, WritableStream, or TransformStream from C# and work on these objects like so:

@inject IJSInProcessRuntime JSRuntime

@code {
    protected override async Task OnInitializedAsync()
    {
        // Construct a stream in .NET.
        using var data = new System.IO.MemoryStream(new byte[1000 * 1024]);
        
        // Convert a .NET Stream to a JS ReadableStream.
        using var streamRef = new DotNetStreamReference(stream: data, leaveOpen: false);
        var jSStreamReference = await JSRuntime.InvokeAsync<IJSInProcessObjectReference>("jSStreamReference", streamRef);
        
        // Create a wrapper instance of the ReadableStream.
        var readableStream = await ReadableStream.CreateAsync(JSRuntime, jSStreamReference);

        // Get the reader and iterate that.
        var readableStreamReader = await readableStream.GetDefaultReaderAsync();
        await foreach (var chunk in readableStreamReader)
        {
            var length = await JSRuntime.InvokeAsync<int>("getAttribute", chunk, "length");
            Console.WriteLine(length);
            await Task.Delay(100);
        }
    }
}

For the above example we use two small JavaScript functions that basically serves as a way to convert the .NET DotNetStreamReference object into an IJSObjectReference and a way to get any attribute of an IJSObjectReference. These methods looks like this and can be defined in the index.html or _host.razor page.

function jSStreamReference(streamRef) { return streamRef.stream(); }
function getAttribute(object, attribute) { return object[attribute]; }

Issues

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:

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on KristofferStrube.Blazor.Streams:

Package Downloads
KristofferStrube.Blazor.FileAPI The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

File API wrapper implementation for Blazor.

KristofferStrube.Blazor.CompressionStreams The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Compression Streams wrapper implementation for Blazor.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.4.2 92 3/11/2024
0.4.1 84 3/10/2024
0.4.0 89 3/10/2024
0.3.0 17,926 3/15/2023
0.3.0-alpha.0 81 3/12/2023
0.2.2 6,336 11/9/2022
0.2.1 308 11/9/2022
0.2.0 431 10/31/2022
0.1.0 345 10/30/2022