Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit 7119815

Browse files
thunder7553SteveSandersonMS
authored andcommitted
Added OnBeforeStartExternalProcess callback which to NodeServicesOptions (and OutOfProcessNodeInstance, SocketNodeInstance and HttpNodeInstance) to configure environment of the node.exe process to be started, and the path to the node executable itself. Fixes #20
1 parent 057efb4 commit 7119815

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

src/Microsoft.AspNetCore.NodeServices/Configuration/NodeServicesOptions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public NodeServicesOptions()
1414
HostingModel = DefaultNodeHostingModel;
1515
WatchFileExtensions = (string[])DefaultWatchFileExtensions.Clone();
1616
}
17-
17+
public Action<System.Diagnostics.ProcessStartInfo> OnBeforeStartExternalProcess { get; set; }
1818
public NodeHostingModel HostingModel { get; set; }
1919
public Func<INodeInstance> NodeInstanceFactory { get; set; }
2020
public string ProjectPath { get; set; }

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

+16-14
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,22 @@ internal class HttpNodeInstance : OutOfProcessNodeInstance
2828
ContractResolver = new CamelCasePropertyNamesContractResolver()
2929
};
3030

31-
private readonly HttpClient _client;
31+
private readonly HttpClient _client;
3232
private bool _disposed;
3333
private int _portNumber;
3434

35-
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0)
35+
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0, Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess = null)
3636
: base(
3737
EmbeddedResourceReader.Read(
3838
typeof(HttpNodeInstance),
3939
"/Content/Node/entrypoint-http.js"),
4040
projectPath,
4141
watchFileExtensions,
42-
MakeCommandLineOptions(port))
42+
MakeCommandLineOptions(port),
43+
onBeforeStartExternalProcess)
4344
{
4445
_client = new HttpClient();
45-
}
46+
}
4647

4748
private static string MakeCommandLineOptions(int port)
4849
{
@@ -112,18 +113,19 @@ protected override void OnOutputDataReceived(string outputData)
112113
}
113114
}
114115

115-
protected override void Dispose(bool disposing) {
116-
base.Dispose(disposing);
116+
protected override void Dispose(bool disposing)
117+
{
118+
base.Dispose(disposing);
117119

118-
if (!_disposed)
120+
if (!_disposed)
119121
{
120-
if (disposing)
122+
if (disposing)
121123
{
122-
_client.Dispose();
123-
}
124+
_client.Dispose();
125+
}
124126

125-
_disposed = true;
126-
}
127-
}
128-
}
127+
_disposed = true;
128+
}
129+
}
130+
}
129131
}

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ public OutOfProcessNodeInstance(
3131
string entryPointScript,
3232
string projectPath,
3333
string[] watchFileExtensions,
34-
string commandLineArguments)
34+
string commandLineArguments,
35+
Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess = null)
3536
{
3637
_entryPointScript = new StringAsTempFile(entryPointScript);
37-
_nodeProcess = LaunchNodeProcess(_entryPointScript.FileName, projectPath, commandLineArguments);
38+
_nodeProcess = LaunchNodeProcess(_entryPointScript.FileName, projectPath, commandLineArguments, onBeforeStartExternalProcess);
3839
_watchFileExtensions = watchFileExtensions;
3940
_fileSystemWatcher = BeginFileWatcher(projectPath);
4041
ConnectToInputOutputStreams();
@@ -111,7 +112,7 @@ private void EnsureFileSystemWatcherIsDisposed()
111112
}
112113
}
113114

114-
private static Process LaunchNodeProcess(string entryPointFilename, string projectPath, string commandLineArguments)
115+
private static Process LaunchNodeProcess(string entryPointFilename, string projectPath, string commandLineArguments, Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess)
115116
{
116117
var startInfo = new ProcessStartInfo("node")
117118
{
@@ -136,6 +137,10 @@ private static Process LaunchNodeProcess(string entryPointFilename, string proje
136137
#else
137138
startInfo.Environment["NODE_PATH"] = nodePathValue;
138139
#endif
140+
if (onBeforeStartExternalProcess != null)
141+
{
142+
onBeforeStartExternalProcess(startInfo);
143+
}
139144

140145
var process = Process.Start(startInfo);
141146

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
2525
/// <seealso cref="Microsoft.AspNetCore.NodeServices.HostingModels.OutOfProcessNodeInstance" />
2626
internal class SocketNodeInstance : OutOfProcessNodeInstance
2727
{
28-
private readonly static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings
28+
private readonly static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings
2929
{
3030
ContractResolver = new CamelCasePropertyNamesContractResolver()
3131
};
@@ -36,16 +36,17 @@ internal class SocketNodeInstance : OutOfProcessNodeInstance
3636
private string _socketAddress;
3737
private VirtualConnectionClient _virtualConnectionClient;
3838

39-
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress): base(
39+
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress, Action<System.Diagnostics.ProcessStartInfo> onBeforeStartExternalProcess = null) : base(
4040
EmbeddedResourceReader.Read(
4141
typeof(SocketNodeInstance),
4242
"/Content/Node/entrypoint-socket.js"),
4343
projectPath,
4444
watchFileExtensions,
45-
MakeNewCommandLineOptions(socketAddress))
45+
MakeNewCommandLineOptions(socketAddress),
46+
onBeforeStartExternalProcess)
4647
{
4748
_socketAddress = socketAddress;
48-
}
49+
}
4950

5051
protected override async Task<T> InvokeExportAsync<T>(NodeInvocationInfo invocationInfo)
5152
{
@@ -170,7 +171,7 @@ private static async Task<T> ReadJsonAsync<T>(Stream stream)
170171

171172
private static async Task<byte[]> ReadAllBytesAsync(Stream input)
172173
{
173-
byte[] buffer = new byte[16*1024];
174+
byte[] buffer = new byte[16 * 1024];
174175

175176
using (var ms = new MemoryStream())
176177
{

0 commit comments

Comments
 (0)