mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-24 10:40:23 +00:00
Initial state
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Microsoft.AspNet.NodeServices {
|
||||
public static class EmbeddedResourceReader {
|
||||
public static string Read(Type assemblyContainingType, string path) {
|
||||
var asm = assemblyContainingType.GetTypeInfo().Assembly;
|
||||
var embeddedResourceName = asm.GetName().Name + path.Replace("/", ".");
|
||||
|
||||
using (var stream = asm.GetManifestResourceStream(embeddedResourceName))
|
||||
using (var sr = new StreamReader(stream)) {
|
||||
return sr.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
50
Microsoft.AspNet.NodeServices/HostingModels/HttpNodeHost.cs
Normal file
50
Microsoft.AspNet.NodeServices/HostingModels/HttpNodeHost.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Microsoft.AspNet.NodeServices {
|
||||
internal class HttpNodeHost : OutOfProcessNodeRunner {
|
||||
private readonly static Regex PortMessageRegex = new Regex(@"^\[Microsoft.AspNet.NodeServices.HttpNodeHost:Listening on port (\d+)\]$");
|
||||
|
||||
private readonly static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings {
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
|
||||
private int _portNumber;
|
||||
|
||||
public HttpNodeHost(int port = 0)
|
||||
: base(EmbeddedResourceReader.Read(typeof(HttpNodeHost), "/Content/Node/entrypoint-http.js"), port.ToString())
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task<string> Invoke(NodeInvocationInfo invocationInfo) {
|
||||
await this.EnsureReady();
|
||||
|
||||
using (var client = new HttpClient()) {
|
||||
// TODO: Use System.Net.Http.Formatting (PostAsJsonAsync etc.)
|
||||
var payloadJson = JsonConvert.SerializeObject(invocationInfo, jsonSerializerSettings);
|
||||
var payload = new StringContent(payloadJson, Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync("http://localhost:" + this._portNumber, payload);
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
return responseString;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnOutputDataReceived(string outputData) {
|
||||
var match = this._portNumber != 0 ? null : PortMessageRegex.Match(outputData);
|
||||
if (match != null && match.Success) {
|
||||
this._portNumber = int.Parse(match.Groups[1].Captures[0].Value);
|
||||
} else {
|
||||
base.OnOutputDataReceived(outputData);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnBeforeLaunchProcess() {
|
||||
// Prepare to receive a new port number
|
||||
this._portNumber = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace Microsoft.AspNet.NodeServices {
|
||||
// This is just to demonstrate that other transports are possible. This implementation is extremely
|
||||
// dubious - if the Node-side code fails to conform to the expected protocol in any way (e.g., has an
|
||||
// error), then it will just hang forever. So don't use this.
|
||||
//
|
||||
// But it's fast - the communication round-trip time is about 0.2ms (tested on OS X on a recent machine),
|
||||
// versus 2-3ms for the HTTP transport.
|
||||
//
|
||||
// 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
|
||||
{
|
||||
private SemaphoreSlim _invocationSemaphore = new SemaphoreSlim(1);
|
||||
private TaskCompletionSource<string> _currentInvocationResult;
|
||||
|
||||
private readonly static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings {
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
|
||||
public InputOutputStreamNodeHost()
|
||||
: base(EmbeddedResourceReader.Read(typeof(InputOutputStreamNodeHost), "/Content/Node/entrypoint-stream.js"))
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task<string> Invoke(NodeInvocationInfo invocationInfo) {
|
||||
await this._invocationSemaphore.WaitAsync();
|
||||
try {
|
||||
await this.EnsureReady();
|
||||
|
||||
var payloadJson = JsonConvert.SerializeObject(invocationInfo, jsonSerializerSettings);
|
||||
var nodeProcess = this.NodeProcess;
|
||||
this._currentInvocationResult = new TaskCompletionSource<string>();
|
||||
nodeProcess.StandardInput.Write("\ninvoke:");
|
||||
nodeProcess.StandardInput.WriteLine(payloadJson); // WriteLineAsync isn't supported cross-platform
|
||||
return await this._currentInvocationResult.Task;
|
||||
} finally {
|
||||
this._invocationSemaphore.Release();
|
||||
this._currentInvocationResult = null;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnOutputDataReceived(string outputData) {
|
||||
if (this._currentInvocationResult != null) {
|
||||
this._currentInvocationResult.SetResult(outputData);
|
||||
} else {
|
||||
base.OnOutputDataReceived(outputData);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Microsoft.AspNet.NodeServices/HostingModels/NodeHost.cs
Normal file
10
Microsoft.AspNet.NodeServices/HostingModels/NodeHost.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Microsoft.AspNet.NodeServices {
|
||||
public class NodeInvocationInfo
|
||||
{
|
||||
public string ModuleName;
|
||||
public string ExportedFunctionName;
|
||||
public object[] Args;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
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 {
|
||||
private object _childProcessLauncherLock;
|
||||
private bool disposed;
|
||||
private StringAsTempFile _entryPointScript;
|
||||
private string _commandLineArguments;
|
||||
private Process _nodeProcess;
|
||||
private TaskCompletionSource<bool> _nodeProcessIsReadySource;
|
||||
|
||||
protected Process NodeProcess {
|
||||
get {
|
||||
// This is only exposed to support the UnreliableStreamNodeHost, 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)
|
||||
{
|
||||
this._childProcessLauncherLock = new object();
|
||||
this._entryPointScript = new StringAsTempFile(entryPointScript);
|
||||
this._commandLineArguments = commandLineArguments ?? string.Empty;
|
||||
}
|
||||
|
||||
protected async Task EnsureReady() {
|
||||
lock (this._childProcessLauncherLock) {
|
||||
if (this._nodeProcess == null || this._nodeProcess.HasExited) {
|
||||
var startInfo = new ProcessStartInfo("node") {
|
||||
Arguments = this._entryPointScript.FileName + " " + this._commandLineArguments,
|
||||
UseShellExecute = false,
|
||||
RedirectStandardInput = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true
|
||||
};
|
||||
|
||||
// Append current directory to NODE_PATH so it can locate node_modules
|
||||
var existingNodePath = Environment.GetEnvironmentVariable("NODE_PATH") ?? string.Empty;
|
||||
if (existingNodePath != string.Empty) {
|
||||
existingNodePath += ":";
|
||||
}
|
||||
|
||||
var nodePathValue = existingNodePath + Path.Combine(Directory.GetCurrentDirectory(), "node_modules");
|
||||
#if DNX451
|
||||
startInfo.EnvironmentVariables.Add("NODE_PATH", nodePathValue);
|
||||
#else
|
||||
startInfo.Environment.Add("NODE_PATH", nodePathValue);
|
||||
#endif
|
||||
|
||||
this.OnBeforeLaunchProcess();
|
||||
this._nodeProcess = Process.Start(startInfo);
|
||||
this.ConnectToInputOutputStreams();
|
||||
}
|
||||
}
|
||||
|
||||
var initializationSucceeded = await this._nodeProcessIsReadySource.Task;
|
||||
if (!initializationSucceeded) {
|
||||
throw new InvalidOperationException("The Node.js process failed to initialize");
|
||||
}
|
||||
}
|
||||
|
||||
private void ConnectToInputOutputStreams() {
|
||||
var initializationIsCompleted = false; // TODO: Make this thread-safe? (Interlocked.Exchange etc.)
|
||||
this._nodeProcessIsReadySource = new TaskCompletionSource<bool>();
|
||||
|
||||
this._nodeProcess.OutputDataReceived += (sender, evt) => {
|
||||
if (evt.Data == "[Microsoft.AspNet.NodeServices:Listening]" && !initializationIsCompleted) {
|
||||
this._nodeProcessIsReadySource.SetResult(true);
|
||||
initializationIsCompleted = true;
|
||||
} else if (evt.Data != null) {
|
||||
this.OnOutputDataReceived(evt.Data);
|
||||
}
|
||||
};
|
||||
|
||||
this._nodeProcess.ErrorDataReceived += (sender, evt) => {
|
||||
if (evt.Data != null) {
|
||||
this.OnErrorDataReceived(evt.Data);
|
||||
if (!initializationIsCompleted) {
|
||||
this._nodeProcessIsReadySource.SetResult(false);
|
||||
initializationIsCompleted = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this._nodeProcess.BeginOutputReadLine();
|
||||
this._nodeProcess.BeginErrorReadLine();
|
||||
}
|
||||
|
||||
protected virtual void OnBeforeLaunchProcess() {
|
||||
}
|
||||
|
||||
protected virtual void OnOutputDataReceived(string outputData) {
|
||||
Console.WriteLine("[Node] " + outputData);
|
||||
}
|
||||
|
||||
protected virtual void OnErrorDataReceived(string errorData) {
|
||||
Console.WriteLine("[Node] " + errorData);
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposed) {
|
||||
if (disposing) {
|
||||
this._entryPointScript.Dispose();
|
||||
}
|
||||
|
||||
if (this._nodeProcess != null && !this._nodeProcess.HasExited) {
|
||||
this._nodeProcess.Kill(); // TODO: Is there a more graceful way to end it? Or does this still let it perform any cleanup? System.Console.WriteLine("Killed");
|
||||
}
|
||||
|
||||
disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
~OutOfProcessNodeRunner() {
|
||||
Dispose (false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user