这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion MLS.Agent.Tests/ApiViaHttpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,27 @@ public async Task Can_serve_blazor_console_code_runner()
result.Should().Contain("Loading...");
}
}


[Fact]
public async Task Can_serve_from_webassembly_controller()
{
var (name, addSource) = await Create.NupkgWithBlazorEnabled();
using (var agent = new AgentService(new StartupOptions(addPackageSource: new WorkspaceServer.PackageSource(addSource.FullName))))
{
var response = await agent.GetAsync($@"/LocalCodeRunner/{name}");

response.EnsureSuccess();
var result = await response.Content.ReadAsStringAsync();
result.Should().Contain("Loading...");

response = await agent.GetAsync($@"/LocalCodeRunner/{name}/interop.js");

response.EnsureSuccess();
result = await response.Content.ReadAsStringAsync();
result.Should().Contain("DotNet.invokeMethodAsync");
}
}

[Fact]
public async Task Can_serve_nodatime_code_runner()
{
Expand Down
2 changes: 2 additions & 0 deletions MLS.Agent/Controllers/EmbeddableController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public IActionResult Html()

private string GetClientParameters()
{
// Uncomment to enable testing /ide without going through orchestrator
// var referrer = "http://localhost:4242";
var referrer = HttpContext.Request.Headers["referer"].ToString();

if (!string.IsNullOrWhiteSpace(referrer) && Uri.TryCreate(referrer, UriKind.Absolute, out var uri))
Expand Down
74 changes: 74 additions & 0 deletions MLS.Agent/Controllers/WebAssemblyController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.DotNet.Try.Markdown;
using MLS.Agent.CommandLine;
using WorkspaceServer;
using WorkspaceServer.Packaging;

namespace MLS.Agent.Controllers
{
public class WebAssemblyController : Controller
{
private PackageRegistry _registry;

public WebAssemblyController(PackageRegistry packageRegistry)
{
_registry = packageRegistry ?? throw new ArgumentNullException(nameof(packageRegistry));
}

[HttpGet]
[Route("/LocalCodeRunner/{packageName}/")]
[Route("/LocalCodeRunner/{packageName}/{*requestedPath}")]
public async Task<IActionResult> GetFile(string packageName, string requestedPath = "Index.html")
{
var package = await _registry.Get<Package2>(packageName);
var asset = package.Assets.OfType<WebAssemblyAsset>().FirstOrDefault();
if (asset == null)
{
return NotFound();
}

var file = asset.DirectoryAccessor.GetFullyQualifiedPath(new RelativeFilePath(requestedPath));
if (!file.Exists)
{
file = asset.DirectoryAccessor.GetFullyQualifiedFilePath("index.html");
return await FileContents(file);
}

return await FileContents(file);
}

private async Task<IActionResult> FileContents(FileSystemInfo file)
{
var contentType = GetContentType(file.FullName);
var bytes = await System.IO.File.ReadAllBytesAsync(file.FullName);

return File(bytes, contentType);
}

private string GetContentType(string path)
{
var extension = Path.GetExtension(path);
switch (extension)
{
case ".dll":
return "application/octet-stream";
case ".json":
return "application/json";
case ".wasm":
return "application/wasm";
case ".woff":
return "application/font-woff";
case ".woff2":
return "application/font-woff";
case ".js":
return "application/javascript";
default:
return "text/html";
}
}
}
}
1 change: 0 additions & 1 deletion MLS.Agent/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public void Configure(

app.UseDefaultFiles()
.UseStaticFilesFromToolLocation()
.UseRouter(new StaticFilesProxyRouter())
.UseMvc();

operation.Succeed();
Expand Down
48 changes: 0 additions & 48 deletions MLS.Agent/StaticFilesProxyRouter.cs

This file was deleted.

2 changes: 1 addition & 1 deletion MLS.PackageTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static string BuildDirectoryLocation() =>
Path.Combine(Path.GetDirectoryName(AssemblyLocation()), "project", "build");

public static string WasmDirectoryLocation() =>
Path.Combine(Path.GetDirectoryName(AssemblyLocation()), "project", "wasm");
Path.Combine(Path.GetDirectoryName(AssemblyLocation()), "project", "wasm", "MLS.Blazor", "dist");

public static string AssemblyLocation()
{
Expand Down
2 changes: 1 addition & 1 deletion WorkspaceServer.Tests/PrebuiltBlazorPackageLocatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task Discovers_built_blazor_package()
var locator = new PrebuiltBlazorPackageLocator();

var asset = await locator.Locate(packageName);
asset.DirectoryAccessor.DirectoryExists("MLS.Blazor").Should().Be(true);
asset.DirectoryAccessor.FileExists("index.html").Should().Be(true);
}

public void Dispose()
Expand Down
20 changes: 12 additions & 8 deletions WorkspaceServer/PackageRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class PackageRegistry :

private readonly ConcurrentDictionary<string, Task<PackageBuilder>> _packageBuilders = new ConcurrentDictionary<string, Task<PackageBuilder>>();
private readonly ConcurrentDictionary<string, Task<IPackage>> _packages = new ConcurrentDictionary<string, Task<IPackage>>();
private readonly ConcurrentDictionary<PackageDescriptor, Task<IPackage>> _packages2 = new ConcurrentDictionary<PackageDescriptor, Task<IPackage>>();
private readonly List<IPackageDiscoveryStrategy> _strategies = new List<IPackageDiscoveryStrategy>();

public PackageRegistry(
Expand Down Expand Up @@ -87,26 +88,29 @@ public async Task<T> Get<T>(string packageName, Budget budget = null)
// FIX: (Get) move this into the cache
var package = await GetPackage2<T>(descriptor);

if (package == null)
if (package == null || !(package is T))
{
package = await GetPackageFromPackageBuilder<T>(packageName, budget, descriptor);
}

return (T) package;
return (T)package;
}

private async Task<IPackage> GetPackage2<T>(PackageDescriptor descriptor)
private Task<IPackage> GetPackage2<T>(PackageDescriptor descriptor)
where T : class, IPackage
{
foreach (var packgeFinder in _packageFinders)
return _packages2.GetOrAdd(descriptor, async descriptor2 =>
{
if (await packgeFinder.Find<T>(descriptor) is T pkg)
foreach (var packgeFinder in _packageFinders)
{
return pkg;
if (await packgeFinder.Find<T>(descriptor) is T pkg)
{
return pkg;
}
}
}

return default;
return default;
});
}

private Task<IPackage> GetPackageFromPackageBuilder<T>(string packageName, Budget budget, PackageDescriptor descriptor)
Expand Down
18 changes: 18 additions & 0 deletions WorkspaceServer/Packaging/PackageDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;

namespace WorkspaceServer.Packaging
{
Expand All @@ -27,6 +28,23 @@ public PackageDescriptor(

internal bool IsPathSpecified { get; }

public override bool Equals(object obj)
{
return obj is PackageDescriptor descriptor &&
Name == descriptor.Name &&
Version == descriptor.Version &&
IsPathSpecified == descriptor.IsPathSpecified;
}

public override int GetHashCode()
{
var hashCode = -858720875;
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Name);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Version);
hashCode = hashCode * -1521134295 + IsPathSpecified.GetHashCode();
return hashCode;
}

public override string ToString() => Name;
}
}
6 changes: 3 additions & 3 deletions WorkspaceServer/Packaging/WebAssemblyAssetFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public WebAssemblyAssetFinder(DirectoryInfo workingDirectory, PackageSource addS
_addSource = addSource;
}

async Task<IMightSupportBlazor> IPackageFinder.Find<IMightSupportBlazor>(PackageDescriptor descriptor)
async Task<TPackage> IPackageFinder.Find<TPackage>(PackageDescriptor descriptor)
{
if (descriptor.IsPathSpecified)
{
Expand All @@ -34,10 +34,10 @@ async Task<IMightSupportBlazor> IPackageFinder.Find<IMightSupportBlazor>(Package
if (candidate.Exists)
{
var package = await CreatePackage(descriptor, candidate);
return (IMightSupportBlazor)package;
return package as TPackage;
}

return (IMightSupportBlazor)(await TryInstallAndLocateTool(descriptor));
return (await TryInstallAndLocateTool(descriptor) as TPackage);
}

private async Task<IPackage> TryInstallAndLocateTool(PackageDescriptor packageDesciptor)
Expand Down