-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor: Add proxy JS runtime to intercept calls to import module
- Loading branch information
1 parent
0c7bc64
commit 4b0c0c4
Showing
4 changed files
with
62 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Blazor.BrowserExtension | ||
{ | ||
internal class ProxyJsRuntime : IJSInProcessRuntime | ||
{ | ||
private readonly IJSRuntime instance; | ||
private readonly IJSInProcessRuntime inProcessInstance; | ||
|
||
public ProxyJsRuntime(IJSRuntime instance) | ||
{ | ||
this.instance = instance; | ||
inProcessInstance = instance as IJSInProcessRuntime; | ||
} | ||
|
||
public ValueTask<TValue> InvokeAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(string identifier, object[] args) | ||
{ | ||
return instance.InvokeAsync<TValue>(identifier, InterceptArgs(identifier, args)); | ||
} | ||
|
||
public ValueTask<TValue> InvokeAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TValue>(string identifier, CancellationToken cancellationToken, object[] args) | ||
{ | ||
return instance.InvokeAsync<TValue>(identifier, cancellationToken, InterceptArgs(identifier, args)); | ||
} | ||
|
||
public TResult Invoke<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)] TResult>(string identifier, params object[] args) | ||
{ | ||
return inProcessInstance.Invoke<TResult>(identifier, args); | ||
} | ||
|
||
static object[] InterceptArgs(string identifier, object[] args) | ||
{ | ||
if (identifier == "import") | ||
{ | ||
return args?.Select(importArg => importArg is string importPath ? ReplaceImportPath(importPath) : importArg).ToArray(); | ||
} | ||
return args; | ||
} | ||
|
||
static string ReplaceImportPath(string importPath) | ||
{ | ||
var splitPaths = importPath.Split('/'); | ||
return string.Join('/', splitPaths.Select((path, index) => index < splitPaths.Length - 1 && path.StartsWith('_') ? path[1..] : path)); | ||
} | ||
} | ||
} |