KristofferStrube.Blazor.Streams
0.2.1
Prefix Reserved
See the version list below for details.
dotnet add package KristofferStrube.Blazor.Streams --version 0.2.1
NuGet\Install-Package KristofferStrube.Blazor.Streams -Version 0.2.1
<PackageReference Include="KristofferStrube.Blazor.Streams" Version="0.2.1" />
paket add KristofferStrube.Blazor.Streams --version 0.2.1
#r "nuget: KristofferStrube.Blazor.Streams, 0.2.1"
// Install KristofferStrube.Blazor.Streams as a Cake Addin #addin nuget:?package=KristofferStrube.Blazor.Streams&version=0.2.1 // Install KristofferStrube.Blazor.Streams as a Cake Tool #tool nuget:?package=KristofferStrube.Blazor.Streams&version=0.2.1
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 6.0 or newer to use the library.
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 ReadableStreamInProcess.CreateAsync(JSRuntime, jSStreamReference);
// Get the reader and iterate that.
var readableStreamReader = readableStream.GetDefaultReader();
await foreach (var chunk in reader)
{
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.
Related articles
This repository was build with inspiration and help from the following series of articles:
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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. |
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.11)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on KristofferStrube.Blazor.Streams:
Package | Downloads |
---|---|
KristofferStrube.Blazor.FileAPI
File API wrapper implementation for Blazor. |
|
KristofferStrube.Blazor.CompressionStreams
Compression Streams wrapper implementation for Blazor. |
GitHub repositories
This package is not used by any popular GitHub repositories.