dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.16k stars 9.92k forks source link

Extend the JS interop transport contract to allow passing UTF8 bytes instead of JS/.NET strings #35065

Open SteveSandersonMS opened 3 years ago

SteveSandersonMS commented 3 years ago

Currently, the lowest level contract in JS interop is defined in terms of strings. That is:

This is nice in that all environments have a notion of passing/receiving strings to/from the browser. In the case of WebView2, this is the only thing you can do. However Blazor Server and Blazor WebAssembly could actually exchange UTF8-encoded byte data if they wanted.

This makes things less efficient than they could be, because on the .NET side, JSON (de)serialization is done in terms of UTF8 bytes. So JS interop from JS to .NET currently:

If JSInterop's contract allowed for passing UTF8 bytes instead of a string, then we could:

That would reduce 2x copy-and-en/decode transformations to no transformations at all on the .NET side, possibly not even having to copy the data out of the SignalR message buffer, and just JSON-deserializing directly over that buffer.

Drawback

This complicates the underlying JS interop contract as there would be two ways to do the same thing, only one of which we'd be using in Blazor. Or we could do a breaking change and entirely remove the string-based APIs.

This would not affect regular applications since the transport is an internal detail. It would only affect people implementing platforms, which is probably nobody except us. It wouldn't even affect the IJSRuntime contract at all, because that's the contract used by application developers, not by platform authors. It would affect the JSRuntime abstract base class that application developers don't normally use.

For WebView, we'd need to take the byte data we're given on either side of the contract and encode it as a string for transport in/out of the WebView2 then convert back to byte data. This is probably no less efficient than what we have today, since it's the same number of transformations - they would just be happening in different places, and it would seem a bit more complicated inside the WebView2 code. I don't think it would be very difficult though.

I think we should consider doing this for .NET 7, but not in .NET 6 as we haven't budgeted time so late in the process for a significant change like this.

ghost commented 3 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost commented 2 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost commented 8 months ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.