Skip to content

Commit 4fb3b18

Browse files
Create new top-level DefaultNodeInstance concept that will soon hold the "connection draining" logic
1 parent 4ee09cb commit 4fb3b18

File tree

19 files changed

+210
-89
lines changed

19 files changed

+210
-89
lines changed

samples/misc/LatencyTest/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ namespace ConsoleApplication
1212
public class Program
1313
{
1414
public static void Main(string[] args) {
15-
using (var nodeServices = CreateNodeServices(Configuration.DefaultNodeHostingModel)) {
15+
using (var nodeServices = CreateNodeServices(NodeServicesOptions.DefaultNodeHostingModel)) {
1616
MeasureLatency(nodeServices).Wait();
1717
}
1818
}
1919

2020
private static async Task MeasureLatency(INodeServices nodeServices) {
2121
// Ensure the connection is open, so we can measure per-request timings below
22-
var response = await nodeServices.Invoke<string>("latencyTest", "C#");
22+
var response = await nodeServices.InvokeAsync<string>("latencyTest", "C#");
2323
Console.WriteLine(response);
2424

2525
// Now perform a series of requests, capturing the time taken
2626
const int requestCount = 100;
2727
var watch = Stopwatch.StartNew();
2828
for (var i = 0; i < requestCount; i++) {
29-
await nodeServices.Invoke<string>("latencyTest", "C#");
29+
await nodeServices.InvokeAsync<string>("latencyTest", "C#");
3030
}
3131

3232
// Display results

samples/misc/NodeServicesExamples/Controllers/ResizeImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public async Task<IActionResult> Index(string imagePath, int maxWidth, int maxHe
4646
}
4747

4848
// Invoke Node and pipe the result to the response
49-
var imageStream = await _nodeServices.Invoke<Stream>(
49+
var imageStream = await _nodeServices.InvokeAsync<Stream>(
5050
"./Node/resizeImage",
5151
fileInfo.PhysicalPath,
5252
mimeType,

samples/misc/NodeServicesExamples/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory, IHo
3030
if (requestPath.StartsWith("/js/") && requestPath.EndsWith(".js")) {
3131
var fileInfo = env.WebRootFileProvider.GetFileInfo(requestPath);
3232
if (fileInfo.Exists) {
33-
var transpiled = await nodeServices.Invoke<string>("./Node/transpilation.js", fileInfo.PhysicalPath, requestPath);
33+
var transpiled = await nodeServices.InvokeAsync<string>("./Node/transpilation.js", fileInfo.PhysicalPath, requestPath);
3434
await context.Response.WriteAsync(transpiled);
3535
return;
3636
}

src/Microsoft.AspNetCore.NodeServices/Configuration.cs

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.AspNetCore.NodeServices.HostingModels;
5+
6+
namespace Microsoft.AspNetCore.NodeServices
7+
{
8+
public static class Configuration
9+
{
10+
public static void AddNodeServices(this IServiceCollection serviceCollection)
11+
=> AddNodeServices(serviceCollection, new NodeServicesOptions());
12+
13+
public static void AddNodeServices(this IServiceCollection serviceCollection, NodeServicesOptions options)
14+
{
15+
serviceCollection.AddSingleton(typeof(INodeServices), serviceProvider =>
16+
{
17+
// Since this instance is being created through DI, we can access the IHostingEnvironment
18+
// to populate options.ProjectPath if it wasn't explicitly specified.
19+
var hostEnv = serviceProvider.GetRequiredService<IHostingEnvironment>();
20+
if (string.IsNullOrEmpty(options.ProjectPath))
21+
{
22+
options.ProjectPath = hostEnv.ContentRootPath;
23+
}
24+
25+
return new NodeServicesImpl(options, () => CreateNodeInstance(options));
26+
});
27+
}
28+
29+
public static INodeServices CreateNodeServices(NodeServicesOptions options)
30+
{
31+
return new NodeServicesImpl(options, () => CreateNodeInstance(options));
32+
}
33+
34+
private static INodeInstance CreateNodeInstance(NodeServicesOptions options)
35+
{
36+
if (options.NodeInstanceFactory != null)
37+
{
38+
// If you've explicitly supplied an INodeInstance factory, we'll use that. This is useful for
39+
// custom INodeInstance implementations.
40+
return options.NodeInstanceFactory();
41+
}
42+
else
43+
{
44+
// Otherwise we'll construct the type of INodeInstance specified by the HostingModel property,
45+
// which itself has a useful default value.
46+
switch (options.HostingModel)
47+
{
48+
case NodeHostingModel.Http:
49+
return new HttpNodeInstance(options.ProjectPath, /* port */ 0, options.WatchFileExtensions);
50+
case NodeHostingModel.Socket:
51+
return new SocketNodeInstance(options.ProjectPath, options.WatchFileExtensions);
52+
default:
53+
throw new ArgumentException("Unknown hosting model: " + options.HostingModel);
54+
}
55+
}
56+
}
57+
}
58+
}

src/Microsoft.AspNetCore.NodeServices/NodeHostingModel.cs renamed to src/Microsoft.AspNetCore.NodeServices/Configuration/NodeHostingModel.cs

File renamed without changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using Microsoft.AspNetCore.NodeServices.HostingModels;
3+
4+
namespace Microsoft.AspNetCore.NodeServices
5+
{
6+
public class NodeServicesOptions
7+
{
8+
public const NodeHostingModel DefaultNodeHostingModel = NodeHostingModel.Http;
9+
10+
private static readonly string[] DefaultWatchFileExtensions = { ".js", ".jsx", ".ts", ".tsx", ".json", ".html" };
11+
12+
public NodeServicesOptions()
13+
{
14+
HostingModel = DefaultNodeHostingModel;
15+
WatchFileExtensions = (string[])DefaultWatchFileExtensions.Clone();
16+
}
17+
18+
public NodeHostingModel HostingModel { get; set; }
19+
public Func<INodeInstance> NodeInstanceFactory { get; set; }
20+
public string ProjectPath { get; set; }
21+
public string[] WatchFileExtensions { get; set; }
22+
}
23+
}

src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Newtonsoft.Json;
88
using Newtonsoft.Json.Serialization;
99

10-
namespace Microsoft.AspNetCore.NodeServices
10+
namespace Microsoft.AspNetCore.NodeServices.HostingModels
1111
{
1212
internal class HttpNodeInstance : OutOfProcessNodeInstance
1313
{
@@ -45,7 +45,7 @@ private static string MakeCommandLineOptions(int port, string[] watchFileExtensi
4545
return result;
4646
}
4747

48-
public override async Task<T> Invoke<T>(NodeInvocationInfo invocationInfo)
48+
protected override async Task<T> InvokeExportAsync<T>(NodeInvocationInfo invocationInfo)
4949
{
5050
await EnsureReady();
5151

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace Microsoft.AspNetCore.NodeServices.HostingModels
5+
{
6+
public interface INodeInstance : IDisposable
7+
{
8+
Task<T> InvokeExportAsync<T>(string moduleName, string exportNameOrNull, params object[] args);
9+
}
10+
}

src/Microsoft.AspNetCore.NodeServices/HostingModels/NodeInvocationException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace Microsoft.AspNetCore.NodeServices
3+
namespace Microsoft.AspNetCore.NodeServices.HostingModels
44
{
55
public class NodeInvocationException : Exception
66
{

0 commit comments

Comments
 (0)