Move packages under top-level 'src' folder

This commit is contained in:
SteveSandersonMS
2016-01-26 15:06:36 +00:00
parent 5e5b12dc54
commit 47c956cc4d
43 changed files with 5 additions and 5 deletions

View File

@@ -0,0 +1 @@
/bin/

View File

@@ -0,0 +1,26 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.AspNet.NodeServices {
public static class Configuration {
public static void AddNodeServices(this IServiceCollection serviceCollection, NodeHostingModel hostingModel = NodeHostingModel.Http) {
serviceCollection.AddSingleton(typeof(INodeServices), (serviceProvider) => {
var appEnv = serviceProvider.GetRequiredService<IApplicationEnvironment>();
return CreateNodeServices(hostingModel, appEnv.ApplicationBasePath);
});
}
public static INodeServices CreateNodeServices(NodeHostingModel hostingModel, string projectPath)
{
switch (hostingModel)
{
case NodeHostingModel.Http:
return new HttpNodeInstance(projectPath);
case NodeHostingModel.InputOutputStream:
return new InputOutputStreamNodeInstance(projectPath);
default:
throw new System.ArgumentException("Unknown hosting model: " + hostingModel.ToString());
}
}
}
}

View File

@@ -0,0 +1,77 @@
// Limit dependencies to core Node modules. This means the code in this file has to be very low-level and unattractive,
// but simplifies things for the consumer of this module.
var http = require('http');
var path = require('path');
var requestedPortOrZero = parseInt(process.argv[2]) || 0; // 0 means 'let the OS decide'
autoQuitOnFileChange(process.cwd(), ['.js', '.json', '.html']);
var server = http.createServer(function(req, res) {
readRequestBodyAsJson(req, function(bodyJson) {
var resolvedPath = path.resolve(process.cwd(), bodyJson.moduleName);
var invokedModule = require(resolvedPath);
var func = bodyJson.exportedFunctionName ? invokedModule[bodyJson.exportedFunctionName] : invokedModule;
if (!func) {
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
}
var hasSentResult = false;
var callback = function(errorValue, successValue) {
if (!hasSentResult) {
hasSentResult = true;
if (errorValue) {
res.statusCode = 500;
if (errorValue.stack) {
res.end(errorValue.stack);
} else {
res.end(errorValue.toString());
}
} else if (typeof successValue !== 'string') {
// Arbitrary object/number/etc - JSON-serialize it
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(successValue));
} else {
// String - can bypass JSON-serialization altogether
res.setHeader('Content-Type', 'text/plain');
res.end(successValue);
}
}
};
try {
func.apply(null, [callback].concat(bodyJson.args));
} catch (synchronousException) {
callback(synchronousException, null);
}
});
});
server.listen(requestedPortOrZero, 'localhost', function () {
// Signal to HttpNodeHost which port it should make its HTTP connections on
console.log('[Microsoft.AspNet.NodeServices.HttpNodeHost:Listening on port ' + server.address().port + '\]');
// Signal to the NodeServices base class that we're ready to accept invocations
console.log('[Microsoft.AspNet.NodeServices:Listening]');
});
function readRequestBodyAsJson(request, callback) {
var requestBodyAsString = '';
request
.on('data', function(chunk) { requestBodyAsString += chunk; })
.on('end', function() { callback(JSON.parse(requestBodyAsString)); });
}
function autoQuitOnFileChange(rootDir, extensions) {
// Note: This will only work on Windows/OS X, because the 'recursive' option isn't supported on Linux.
// Consider using a different watch mechanism (though ideally without forcing further NPM dependencies).
var fs = require('fs');
var path = require('path');
fs.watch(rootDir, { persistent: false, recursive: true }, function(event, filename) {
var ext = path.extname(filename);
if (extensions.indexOf(ext) >= 0) {
console.log('Restarting due to file change: ' + filename);
process.exit(0);
}
});
}

View File

@@ -0,0 +1,23 @@
var path = require('path');
var readline = require('readline');
var invocationPrefix = 'invoke:';
function invocationCallback(errorValue, successValue) {
if (errorValue) {
throw new Error('InputOutputStreamHost doesn\'t support errors. Got error: ' + errorValue.toString());
} else {
var serializedResult = JSON.stringify(successValue);
console.log(serializedResult);
}
}
readline.createInterface({ input: process.stdin }).on('line', function (message) {
if (message && message.substring(0, invocationPrefix.length) === invocationPrefix) {
var invocation = JSON.parse(message.substring(invocationPrefix.length));
var invokedModule = require(path.resolve(process.cwd(), invocation.moduleName));
var func = invocation.exportedFunctionName ? invokedModule[invocation.exportedFunctionName] : invokedModule;
func.apply(null, [invocationCallback].concat(invocation.args));
}
});
console.log('[Microsoft.AspNet.NodeServices:Listening]'); // The .NET app waits for this signal before sending any invocations

View File

@@ -0,0 +1,64 @@
using System;
using System.Net;
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 HttpNodeInstance : OutOfProcessNodeInstance {
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 HttpNodeInstance(string projectPath, int port = 0)
: base(EmbeddedResourceReader.Read(typeof(HttpNodeInstance), "/Content/Node/entrypoint-http.js"), projectPath, port.ToString())
{
}
public override async Task<T> Invoke<T>(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();
if (!response.IsSuccessStatusCode) {
throw new Exception("Call to Node module failed with error: " + responseString);
}
var responseIsJson = response.Content.Headers.ContentType.MediaType == "application/json";
if (responseIsJson) {
return JsonConvert.DeserializeObject<T>(responseString);
} else if (typeof(T) != typeof(string)) {
throw new System.ArgumentException("Node module responded with non-JSON string. This cannot be converted to the requested generic type: " + typeof(T).FullName);
} else {
return (T)(object)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;
}
}
}

View File

@@ -0,0 +1,58 @@
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 InputOutputStreamNodeInstance : OutOfProcessNodeInstance
{
private SemaphoreSlim _invocationSemaphore = new SemaphoreSlim(1);
private TaskCompletionSource<string> _currentInvocationResult;
private readonly static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings {
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
public InputOutputStreamNodeInstance(string projectPath)
: base(EmbeddedResourceReader.Read(typeof(InputOutputStreamNodeInstance), "/Content/Node/entrypoint-stream.js"), projectPath)
{
}
public override async Task<T> Invoke<T>(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
var resultString = await this._currentInvocationResult.Task;
return JsonConvert.DeserializeObject<T>(resultString);
} 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);
}
}
}
}

View File

@@ -0,0 +1,8 @@
namespace Microsoft.AspNet.NodeServices {
public class NodeInvocationInfo
{
public string ModuleName;
public string ExportedFunctionName;
public object[] Args;
}
}

View File

@@ -0,0 +1,152 @@
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.
*/
public abstract class OutOfProcessNodeInstance : INodeServices {
private object _childProcessLauncherLock;
private bool disposed;
private StringAsTempFile _entryPointScript;
private string _projectPath;
private string _commandLineArguments;
private Process _nodeProcess;
private TaskCompletionSource<bool> _nodeProcessIsReadySource;
protected Process NodeProcess {
get {
// 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 OutOfProcessNodeInstance(string entryPointScript, string projectPath, string commandLineArguments = null)
{
this._childProcessLauncherLock = new object();
this._entryPointScript = new StringAsTempFile(entryPointScript);
this._projectPath = projectPath;
this._commandLineArguments = commandLineArguments ?? string.Empty;
}
public abstract Task<T> Invoke<T>(NodeInvocationInfo invocationInfo);
public Task<T> Invoke<T>(string moduleName, params object[] args) {
return this.InvokeExport<T>(moduleName, null, args);
}
public async Task<T> InvokeExport<T>(string moduleName, string exportedFunctionName, params object[] args) {
return await this.Invoke<T>(new NodeInvocationInfo {
ModuleName = moduleName,
ExportedFunctionName = exportedFunctionName,
Args = args
});
}
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,
WorkingDirectory = this._projectPath
};
// Append projectPath 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(this._projectPath, "node_modules");
#if NET451
startInfo.EnvironmentVariables.Add("NODE_PATH", nodePathValue);
#else
startInfo.Environment.Add("NODE_PATH", nodePathValue);
#endif
this.OnBeforeLaunchProcess();
this._nodeProcess = Process.Start(startInfo);
this.ConnectToInputOutputStreams();
}
}
var task = this._nodeProcessIsReadySource.Task;
var initializationSucceeded = await task;
if (!initializationSucceeded) {
throw new InvalidOperationException("The Node.js process failed to initialize", task.Exception);
}
}
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 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?
}
disposed = true;
}
}
~OutOfProcessNodeInstance() {
Dispose (false);
}
}
}

View File

@@ -0,0 +1,10 @@
using System;
using System.Threading.Tasks;
namespace Microsoft.AspNet.NodeServices {
public interface INodeServices : IDisposable {
Task<T> Invoke<T>(string moduleName, params object[] args);
Task<T> InvokeExport<T>(string moduleName, string exportedFunctionName, params object[] args);
}
}

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>b0fa4175-8b29-4904-9780-28b3c24b0567</ProjectGuid>
<RootNamespace>Microsoft.AspNet.NodeServices</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\NodeServices.sln\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\NodeServices.sln\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@@ -0,0 +1,6 @@
namespace Microsoft.AspNet.NodeServices {
public enum NodeHostingModel {
Http,
InputOutputStream,
}
}

View File

@@ -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();
}
}
}
}

View File

@@ -0,0 +1,39 @@
using System;
using System.IO;
namespace Microsoft.AspNet.NodeServices {
// Makes it easier to pass script files to Node in a way that's sure to clean up after the process exits
public sealed class StringAsTempFile : IDisposable {
public string FileName { get; private set; }
private bool _disposedValue;
public StringAsTempFile(string content) {
this.FileName = Path.GetTempFileName();
File.WriteAllText(this.FileName, content);
}
private void DisposeImpl(bool disposing)
{
if (!_disposedValue) {
if (disposing) {
// TODO: dispose managed state (managed objects).
}
File.Delete(this.FileName);
_disposedValue = true;
}
}
public void Dispose()
{
DisposeImpl(true);
GC.SuppressFinalize(this);
}
~StringAsTempFile() {
DisposeImpl(false);
}
}
}

View File

@@ -0,0 +1,33 @@
{
"version": "1.0.0-alpha7",
"description": "Invoke Node.js modules at runtime in ASP.NET 5 applications.",
"authors": [ "Microsoft" ],
"repository": {
"type": "git",
"url": "git://github.com/aspnet/nodeservices"
},
"dependencies": {
"Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0-rc1-final",
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-rc1-final",
"Newtonsoft.Json": "8.0.1-beta3",
"System.Net.Http": "4.0.1-beta-*"
},
"frameworks": {
"net451": { },
"dotnet5.4": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-*",
"System.Collections": "4.0.11-beta-*",
"System.Console": "4.0.0-beta-*",
"System.Diagnostics.Process": "4.1.0-beta-*",
"System.IO.FileSystem": "4.0.1-beta-*",
"System.Linq": "4.0.1-beta-*",
"System.Text.RegularExpressions": "4.0.11-beta-*",
"System.Threading": "4.0.11-beta-*"
}
}
},
"resource": [
"Content/**/*"
]
}