mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-22 17:47:53 +00:00
Switch to using DI to acquire Node instances. Bump versions to alpha2.
This commit is contained in:
@@ -20,8 +20,6 @@ namespace Microsoft.AspNet.NodeServices.Angular
|
||||
const string PrerenderModuleAttributeName = "aspnet-ng2-prerender-module";
|
||||
const string PrerenderExportAttributeName = "aspnet-ng2-prerender-export";
|
||||
|
||||
private static NodeInstance nodeInstance = new NodeInstance();
|
||||
|
||||
[HtmlAttributeName(PrerenderModuleAttributeName)]
|
||||
public string ModuleName { get; set; }
|
||||
|
||||
@@ -29,15 +27,17 @@ namespace Microsoft.AspNet.NodeServices.Angular
|
||||
public string ExportName { get; set; }
|
||||
|
||||
private IHttpContextAccessor contextAccessor;
|
||||
private INodeServices nodeServices;
|
||||
|
||||
public AngularRunAtServerTagHelper(IHttpContextAccessor contextAccessor)
|
||||
public AngularRunAtServerTagHelper(INodeServices nodeServices, IHttpContextAccessor contextAccessor)
|
||||
{
|
||||
this.contextAccessor = contextAccessor;
|
||||
this.nodeServices = nodeServices;
|
||||
}
|
||||
|
||||
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
|
||||
{
|
||||
var result = await nodeInstance.InvokeExport(nodeScript.FileName, "renderComponent", new {
|
||||
var result = await this.nodeServices.InvokeExport(nodeScript.FileName, "renderComponent", new {
|
||||
componentModule = this.ModuleName,
|
||||
componentExport = this.ExportName,
|
||||
tagName = output.TagName,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "1.0.0-alpha1",
|
||||
"version": "1.0.0-alpha2",
|
||||
"description": "Microsoft.AspNet.NodeServices.Angular Class Library",
|
||||
"authors": [
|
||||
"Microsoft"
|
||||
@@ -25,7 +25,7 @@
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.NodeServices": "1.0.0-alpha1",
|
||||
"Microsoft.AspNet.NodeServices": "1.0.0-alpha2",
|
||||
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta8"
|
||||
},
|
||||
"resource": [
|
||||
|
||||
@@ -5,7 +5,6 @@ namespace Microsoft.AspNet.NodeServices.React
|
||||
public static class ReactRenderer
|
||||
{
|
||||
private static StringAsTempFile nodeScript;
|
||||
private static NodeInstance nodeInstance = new NodeInstance();
|
||||
|
||||
static ReactRenderer() {
|
||||
// Consider populating this lazily
|
||||
@@ -13,8 +12,8 @@ namespace Microsoft.AspNet.NodeServices.React
|
||||
nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit
|
||||
}
|
||||
|
||||
public static async Task<string> RenderToString(string moduleName, string exportName, string baseUrl) {
|
||||
return await nodeInstance.InvokeExport(nodeScript.FileName, "renderToString", new {
|
||||
public static async Task<string> RenderToString(INodeServices nodeServices, string moduleName, string exportName, string baseUrl) {
|
||||
return await nodeServices.InvokeExport(nodeScript.FileName, "renderToString", new {
|
||||
moduleName,
|
||||
exportName,
|
||||
baseUrl
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "1.0.0-alpha1",
|
||||
"version": "1.0.0-alpha2",
|
||||
"description": "Microsoft.AspNet.NodeServices.React Class Library",
|
||||
"authors": [
|
||||
"Microsoft"
|
||||
@@ -25,7 +25,7 @@
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"Microsoft.AspNet.NodeServices": "1.0.0-alpha1"
|
||||
"Microsoft.AspNet.NodeServices": "1.0.0-alpha2"
|
||||
},
|
||||
"resource": [
|
||||
"Content/**/*"
|
||||
|
||||
24
Microsoft.AspNet.NodeServices/Configuration.cs
Normal file
24
Microsoft.AspNet.NodeServices/Configuration.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
|
||||
namespace Microsoft.AspNet.NodeServices {
|
||||
public static class Configuration {
|
||||
public static void AddNodeServices(this IServiceCollection serviceCollection, NodeHostingModel hostingModel = NodeHostingModel.Http) {
|
||||
serviceCollection.AddSingleton(typeof(INodeServices), (serviceProvider) => {
|
||||
return CreateNodeServices(hostingModel);
|
||||
});
|
||||
}
|
||||
|
||||
private static INodeServices CreateNodeServices(NodeHostingModel hostingModel)
|
||||
{
|
||||
switch (hostingModel)
|
||||
{
|
||||
case NodeHostingModel.Http:
|
||||
return new HttpNodeInstance();
|
||||
case NodeHostingModel.InputOutputStream:
|
||||
return new InputOutputStreamNodeInstance();
|
||||
default:
|
||||
throw new System.ArgumentException("Unknown hosting model: " + hostingModel.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Microsoft.AspNet.NodeServices {
|
||||
internal class HttpNodeHost : OutOfProcessNodeRunner {
|
||||
internal class HttpNodeInstance : OutOfProcessNodeInstance {
|
||||
private readonly static Regex PortMessageRegex = new Regex(@"^\[Microsoft.AspNet.NodeServices.HttpNodeHost:Listening on port (\d+)\]$");
|
||||
|
||||
private readonly static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings {
|
||||
@@ -15,8 +15,8 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
|
||||
private int _portNumber;
|
||||
|
||||
public HttpNodeHost(int port = 0)
|
||||
: base(EmbeddedResourceReader.Read(typeof(HttpNodeHost), "/Content/Node/entrypoint-http.js"), port.ToString())
|
||||
public HttpNodeInstance(int port = 0)
|
||||
: base(EmbeddedResourceReader.Read(typeof(HttpNodeInstance), "/Content/Node/entrypoint-http.js"), port.ToString())
|
||||
{
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
// Instead of directly using stdin/stdout, we could use either regular sockets (TCP) or use named pipes
|
||||
// on Windows and domain sockets on Linux / OS X, but either way would need a system for framing the
|
||||
// requests, associating them with responses, and scheduling use of the comms channel.
|
||||
internal class InputOutputStreamNodeHost : OutOfProcessNodeRunner
|
||||
internal class InputOutputStreamNodeInstance : OutOfProcessNodeInstance
|
||||
{
|
||||
private SemaphoreSlim _invocationSemaphore = new SemaphoreSlim(1);
|
||||
private TaskCompletionSource<string> _currentInvocationResult;
|
||||
@@ -24,8 +24,8 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
|
||||
public InputOutputStreamNodeHost()
|
||||
: base(EmbeddedResourceReader.Read(typeof(InputOutputStreamNodeHost), "/Content/Node/entrypoint-stream.js"))
|
||||
public InputOutputStreamNodeInstance()
|
||||
: base(EmbeddedResourceReader.Read(typeof(InputOutputStreamNodeInstance), "/Content/Node/entrypoint-stream.js"))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNet.NodeServices {
|
||||
public abstract class NodeHost : System.IDisposable
|
||||
{
|
||||
public abstract Task<string> Invoke(NodeInvocationInfo invocationInfo);
|
||||
|
||||
public abstract void Dispose();
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
* Class responsible for launching the Node child process, determining when it is ready to accept invocations,
|
||||
* and finally killing it when the parent process exits. Also it restarts the child process if it dies.
|
||||
*/
|
||||
internal abstract class OutOfProcessNodeRunner : NodeHost {
|
||||
public abstract class OutOfProcessNodeInstance : INodeServices {
|
||||
private object _childProcessLauncherLock;
|
||||
private bool disposed;
|
||||
private StringAsTempFile _entryPointScript;
|
||||
@@ -18,19 +18,33 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
|
||||
protected Process NodeProcess {
|
||||
get {
|
||||
// This is only exposed to support the UnreliableStreamNodeHost, which is just to verify that
|
||||
// This is only exposed to support the unreliable OutOfProcessNodeRunner, which is just to verify that
|
||||
// other hosting/transport mechanisms are possible. This shouldn't really be exposed.
|
||||
return this._nodeProcess;
|
||||
}
|
||||
}
|
||||
|
||||
public OutOfProcessNodeRunner(string entryPointScript, string commandLineArguments = null)
|
||||
public OutOfProcessNodeInstance(string entryPointScript, string commandLineArguments = null)
|
||||
{
|
||||
this._childProcessLauncherLock = new object();
|
||||
this._entryPointScript = new StringAsTempFile(entryPointScript);
|
||||
this._commandLineArguments = commandLineArguments ?? string.Empty;
|
||||
}
|
||||
|
||||
public abstract Task<string> Invoke(NodeInvocationInfo invocationInfo);
|
||||
|
||||
public Task<string> Invoke(string moduleName, params object[] args) {
|
||||
return this.InvokeExport(moduleName, null, args);
|
||||
}
|
||||
|
||||
public async Task<string> InvokeExport(string moduleName, string exportedFunctionName, params object[] args) {
|
||||
return await this.Invoke(new NodeInvocationInfo {
|
||||
ModuleName = moduleName,
|
||||
ExportedFunctionName = exportedFunctionName,
|
||||
Args = args
|
||||
});
|
||||
}
|
||||
|
||||
protected async Task EnsureReady() {
|
||||
lock (this._childProcessLauncherLock) {
|
||||
if (this._nodeProcess == null || this._nodeProcess.HasExited) {
|
||||
@@ -105,7 +119,7 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
Console.WriteLine("[Node] " + errorData);
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
@@ -126,7 +140,7 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
}
|
||||
}
|
||||
|
||||
~OutOfProcessNodeRunner() {
|
||||
~OutOfProcessNodeInstance() {
|
||||
Dispose (false);
|
||||
}
|
||||
}
|
||||
10
Microsoft.AspNet.NodeServices/INodeInstance.cs
Normal file
10
Microsoft.AspNet.NodeServices/INodeInstance.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNet.NodeServices {
|
||||
public interface INodeServices : IDisposable {
|
||||
Task<string> Invoke(string moduleName, params object[] args);
|
||||
|
||||
Task<string> InvokeExport(string moduleName, string exportedFunctionName, params object[] args);
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNet.NodeServices {
|
||||
public class NodeInstance : IDisposable {
|
||||
private readonly NodeHost _nodeHost;
|
||||
|
||||
public NodeInstance(NodeHostingModel hostingModel = NodeHostingModel.Http) {
|
||||
switch (hostingModel) {
|
||||
case NodeHostingModel.Http:
|
||||
this._nodeHost = new HttpNodeHost();
|
||||
break;
|
||||
case NodeHostingModel.InputOutputStream:
|
||||
this._nodeHost = new InputOutputStreamNodeHost();
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException("Unknown hosting model: " + hostingModel.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public Task<string> Invoke(string moduleName, params object[] args) {
|
||||
return this.InvokeExport(moduleName, null, args);
|
||||
}
|
||||
|
||||
public async Task<string> InvokeExport(string moduleName, string exportedFunctionName, params object[] args) {
|
||||
return await this._nodeHost.Invoke(new NodeInvocationInfo {
|
||||
ModuleName = moduleName,
|
||||
ExportedFunctionName = exportedFunctionName,
|
||||
Args = args
|
||||
});
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this._nodeHost.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "1.0.0-alpha1",
|
||||
"version": "1.0.0-alpha2",
|
||||
"description": "Microsoft.AspNet.NodeServices",
|
||||
"authors": [ "Microsoft" ],
|
||||
"tags": [""],
|
||||
@@ -8,7 +8,8 @@
|
||||
|
||||
"dependencies": {
|
||||
"System.Net.Http": "4.0.1-beta-23409",
|
||||
"Newtonsoft.Json": "8.0.1-beta1"
|
||||
"Newtonsoft.Json": "8.0.1-beta1",
|
||||
"Microsoft.Framework.DependencyInjection": "1.0.0-beta8"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
|
||||
@@ -79,6 +79,9 @@ namespace MusicStore
|
||||
Mapper.CreateMap<ArtistResultDto, Artist>();
|
||||
Mapper.CreateMap<Genre, GenreResultDto>();
|
||||
Mapper.CreateMap<GenreResultDto, Genre>();
|
||||
|
||||
// Enable Node Services
|
||||
services.AddNodeServices();
|
||||
}
|
||||
|
||||
// Configure is called after ConfigureServices is called.
|
||||
@@ -108,17 +111,6 @@ namespace MusicStore
|
||||
app.UseExceptionHandler("/Home/Error");
|
||||
}
|
||||
|
||||
var nodeInstance = new NodeInstance();
|
||||
app.Use(async (context, next) => {
|
||||
if (context.Request.Path.Value.EndsWith(".less")) {
|
||||
// Note: check for directory traversal
|
||||
var output = await nodeInstance.Invoke("lessCompiler.js", env.WebRootPath + context.Request.Path.Value);
|
||||
await context.Response.WriteAsync(output);
|
||||
} else {
|
||||
await next();
|
||||
}
|
||||
});
|
||||
|
||||
// Add static files to the request pipeline.
|
||||
app.UseStaticFiles();
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
"EntityFramework.SQLite": "7.0.0-beta8",
|
||||
"Microsoft.AspNet.Identity.EntityFramework": "3.0.0-beta8",
|
||||
"AutoMapper": "4.0.0-alpha1",
|
||||
"Microsoft.AspNet.NodeServices.Angular": "1.0.0-alpha1"
|
||||
"Microsoft.AspNet.NodeServices.Angular": "1.0.0-alpha2"
|
||||
},
|
||||
"commands": {
|
||||
"web": "Microsoft.AspNet.Server.Kestrel"
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace ES2015Example.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
public async Task<IActionResult> Index(int pageIndex)
|
||||
public IActionResult Index(int pageIndex)
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
@@ -6,13 +6,17 @@ namespace ES2015Example.Controllers
|
||||
{
|
||||
public class ScriptController : Controller
|
||||
{
|
||||
private static NodeInstance nodeInstance = new NodeInstance();
|
||||
private INodeServices nodeServices;
|
||||
|
||||
public ScriptController(INodeServices nodeServices) {
|
||||
this.nodeServices = nodeServices;
|
||||
}
|
||||
|
||||
public async Task<ContentResult> Transpile(string filename)
|
||||
{
|
||||
// TODO: Don't hard-code wwwroot; use proper path conversions
|
||||
var fileContents = System.IO.File.ReadAllText("wwwroot/" + filename);
|
||||
var transpiledResult = await nodeInstance.Invoke("transpilation.js", fileContents, Request.Path.Value);
|
||||
var transpiledResult = await this.nodeServices.Invoke("transpilation.js", fileContents, Request.Path.Value);
|
||||
return Content(transpiledResult, "application/javascript");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using Microsoft.Dnx.Runtime;
|
||||
using Microsoft.Framework.Configuration;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.Logging;
|
||||
using Microsoft.AspNet.NodeServices;
|
||||
|
||||
namespace ES2015Example
|
||||
{
|
||||
@@ -27,6 +28,9 @@ namespace ES2015Example
|
||||
{
|
||||
// Add MVC services to the services container.
|
||||
services.AddMvc();
|
||||
|
||||
// Enable Node Services
|
||||
services.AddNodeServices();
|
||||
}
|
||||
|
||||
// Configure is called after ConfigureServices is called.
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
"Microsoft.Framework.Logging": "1.0.0-beta8",
|
||||
"Microsoft.Framework.Logging.Console": "1.0.0-beta8",
|
||||
"Microsoft.Framework.Logging.Debug": "1.0.0-beta8",
|
||||
"Microsoft.AspNet.NodeServices": "1.0.0-alpha1"
|
||||
"Microsoft.AspNet.NodeServices": "1.0.0-alpha2"
|
||||
},
|
||||
"commands": {
|
||||
"web": "Microsoft.AspNet.Server.Kestrel"
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.NodeServices;
|
||||
using Microsoft.AspNet.NodeServices.React;
|
||||
|
||||
namespace ReactExample.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
private INodeServices nodeServices;
|
||||
|
||||
public HomeController(INodeServices nodeServices) {
|
||||
this.nodeServices = nodeServices;
|
||||
}
|
||||
|
||||
public async Task<IActionResult> Index(int pageIndex)
|
||||
{
|
||||
ViewData["ReactOutput"] = await ReactRenderer.RenderToString(
|
||||
ViewData["ReactOutput"] = await ReactRenderer.RenderToString(this.nodeServices,
|
||||
moduleName: "ReactApp/components/ReactApp.jsx",
|
||||
exportName: "ReactApp",
|
||||
baseUrl: Request.Path
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Hosting;
|
||||
using Microsoft.AspNet.NodeServices;
|
||||
using Microsoft.Dnx.Runtime;
|
||||
using Microsoft.Framework.Configuration;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
@@ -26,6 +27,9 @@ namespace ReactExample
|
||||
{
|
||||
// Add MVC services to the services container.
|
||||
services.AddMvc();
|
||||
|
||||
// Enable Node Services
|
||||
services.AddNodeServices();
|
||||
}
|
||||
|
||||
// Configure is called after ConfigureServices is called.
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
"Microsoft.Framework.Logging": "1.0.0-beta8",
|
||||
"Microsoft.Framework.Logging.Console": "1.0.0-beta8",
|
||||
"Microsoft.Framework.Logging.Debug": "1.0.0-beta8",
|
||||
"Microsoft.AspNet.NodeServices.React": "1.0.0-alpha1"
|
||||
"Microsoft.AspNet.NodeServices.React": "1.0.0-alpha2"
|
||||
},
|
||||
"commands": {
|
||||
"web": "Microsoft.AspNet.Server.Kestrel"
|
||||
|
||||
Reference in New Issue
Block a user