Skip to content

Commit

Permalink
Allow configuration of node path
Browse files Browse the repository at this point in the history
  • Loading branch information
PieterjanDeClippel committed Jun 10, 2024
1 parent ca466d3 commit 8697fc7
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,9 @@ public NodeServicesOptions(IServiceProvider serviceProvider)
/// A token that indicates when the host application is stopping.
/// </summary>
public CancellationToken ApplicationStoppingToken { get; set; }

/// <summary>
/// Explicitly pass the path to the Node executable.
/// </summary>
public string NodePath { get; set; } = "node";
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ internal class HttpNodeInstance : OutOfProcessNodeInstance

public HttpNodeInstance(NodeServicesOptions options, int port = 0)
: base(
EmbeddedResourceReader.Read(
typeof(HttpNodeInstance),
"/Content/Node/entrypoint-http.js"),
EmbeddedResourceReader.Read(typeof(HttpNodeInstance), "/Content/Node/entrypoint-http.js"),
options.ProjectPath,
options.WatchFileExtensions,
MakeCommandLineOptions(port),
Expand All @@ -45,7 +43,8 @@ public HttpNodeInstance(NodeServicesOptions options, int port = 0)
options.EnvironmentVariables,
options.InvocationTimeoutMilliseconds,
options.LaunchWithDebugging,
options.DebuggingPort)
options.DebuggingPort,
options.NodePath)
{
_client = new HttpClient();
_client.Timeout = TimeSpan.FromMilliseconds(options.InvocationTimeoutMilliseconds + 1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public static class NodeServicesOptionsExtensions
/// </summary>
public static void UseHttpHosting(this NodeServicesOptions options)
{
options.NodeInstanceFactory = () => new HttpNodeInstance(options);
options.NodeInstanceFactory = () =>
{
var instance = new HttpNodeInstance(options);
return instance;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public OutOfProcessNodeInstance(
IDictionary<string, string> environmentVars,
int invocationTimeoutMilliseconds,
bool launchWithDebugging,
int debuggingPort)
int debuggingPort,
string nodePath)
{
if (nodeOutputLogger == null)
{
Expand All @@ -70,7 +71,7 @@ public OutOfProcessNodeInstance(
_launchWithDebugging = launchWithDebugging;

var startInfo = PrepareNodeProcessStartInfo(_entryPointScript.FileName, projectPath, commandLineArguments,
environmentVars, _launchWithDebugging, debuggingPort);
environmentVars, _launchWithDebugging, debuggingPort, nodePath);
_nodeProcess = LaunchNodeProcess(startInfo);
_watchFileExtensions = watchFileExtensions;
_fileSystemWatcher = BeginFileWatcher(projectPath);
Expand Down Expand Up @@ -208,7 +209,7 @@ protected abstract Task<T> InvokeExportAsync<T>(
/// <returns></returns>
protected virtual ProcessStartInfo PrepareNodeProcessStartInfo(
string entryPointFilename, string projectPath, string commandLineArguments,
IDictionary<string, string> environmentVars, bool launchWithDebugging, int debuggingPort)
IDictionary<string, string> environmentVars, bool launchWithDebugging, int debuggingPort, string nodePath)
{
// This method is virtual, as it provides a way to override the NODE_PATH or the path to node.exe
string debuggingArgs;
Expand All @@ -223,7 +224,7 @@ protected virtual ProcessStartInfo PrepareNodeProcessStartInfo(
}

var thisProcessPid = Process.GetCurrentProcess().Id;
var startInfo = new ProcessStartInfo("node")
var startInfo = new ProcessStartInfo(nodePath)
{
Arguments = $"{debuggingArgs}\"{entryPointFilename}\" --parentPid {thisProcessPid} {commandLineArguments ?? string.Empty}",
UseShellExecute = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<IsPackable>true</IsPackable>
<PackageId>MintPlayer.AspNetCore.NodeServices</PackageId>
<Version>8.2.0</Version>
<Version>8.3.0</Version>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<IsPackable>true</IsPackable>
<PackageId>MintPlayer.AspNetCore.SpaServices.Abstractions</PackageId>
<Version>8.2.0</Version>
<Version>8.3.0</Version>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<IsPackable>true</IsPackable>
<PackageId>MintPlayer.AspNetCore.SpaServices.Prerendering</PackageId>
<Version>8.2.0</Version>
<Version>8.3.0</Version>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ public static IApplicationBuilder UseSpaPrerendering(
// Get all the necessary context info that will be used for each prerendering call
var applicationBuilder = spaBuilder.ApplicationBuilder;
var serviceProvider = applicationBuilder.ApplicationServices;
var nodeServices = GetNodeServices(serviceProvider);
var applicationStoppingToken = serviceProvider.GetRequiredService<IHostApplicationLifetime>()
.ApplicationStopping;
var applicationBasePath = serviceProvider.GetRequiredService<IWebHostEnvironment>()
.ContentRootPath;
var nodeServices = GetNodeServices(serviceProvider, opts => opts.NodePath = options.NodePath);
var applicationStoppingToken = serviceProvider.GetRequiredService<IHostApplicationLifetime>().ApplicationStopping;
var applicationBasePath = serviceProvider.GetRequiredService<IWebHostEnvironment>().ContentRootPath;
var moduleExport = new JavaScriptModuleExport(capturedBootModulePath);
var excludePathStrings = (options.ExcludeUrls ?? Array.Empty<string>())
.Select(url => new PathString(url))
Expand Down Expand Up @@ -266,11 +264,22 @@ private static async Task ServePrerenderResult(HttpContext context, RenderToStri
}
}

private static INodeServices GetNodeServices(IServiceProvider serviceProvider)
private static INodeServices GetNodeServices(IServiceProvider serviceProvider, Action<NodeServicesOptions> optionAction)
{
// Use the registered instance, or create a new private instance if none is registered
var instance = serviceProvider.GetService<INodeServices>();
return instance ?? NodeServicesFactory.CreateNodeServices(
new NodeServicesOptions(serviceProvider));
if (instance == null)
{
// Will always be this case
var opts = new NodeServicesOptions(serviceProvider);
optionAction(opts);
var result = NodeServicesFactory.CreateNodeServices(opts);
return result;
}
else
{
// Will never be called for the moment
return instance;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public class SpaPrerenderingOptions
/// </summary>
public string[] ExcludeUrls { get; set; }

/// <summary>
/// Path to the Node executable
/// </summary>
public string NodePath { get; set; } = "node";

/// <summary>
/// This method is called after the prerendering logic completes, and before the next middleware is called.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<IsPackable>true</IsPackable>
<PackageId>MintPlayer.AspNetCore.SpaServices.Routing</PackageId>
<Version>8.2.0</Version>
<Version>8.3.0</Version>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<IsPackable>true</IsPackable>
<PackageId>MintPlayer.AspNetCore.SpaServices.Xsrf</PackageId>
<Version>8.2.0</Version>
<Version>8.3.0</Version>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<IsPackable>true</IsPackable>
<PackageId>MintPlayer.AspNetCore.SpaServices</PackageId>
<Version>8.2.0</Version>
<Version>8.3.0</Version>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>

Expand Down

0 comments on commit 8697fc7

Please sign in to comment.