From 8697fc7a6fe80c5c799a3ebf24d44fa46bf8d6fa Mon Sep 17 00:00:00 2001 From: Pieterjan De Clippel Date: Mon, 10 Jun 2024 23:24:06 +0200 Subject: [PATCH] Allow configuration of node path --- .../Configuration/NodeServicesOptions.cs | 5 ++++ .../HostingModels/HttpNodeInstance.cs | 7 +++--- .../NodeServicesOptionsExtensions.cs | 6 ++++- .../HostingModels/OutOfProcessNodeInstance.cs | 9 ++++--- .../MintPlayer.AspNetCore.NodeServices.csproj | 2 +- ...AspNetCore.SpaServices.Abstractions.csproj | 2 +- ...AspNetCore.SpaServices.Prerendering.csproj | 2 +- .../Prerendering/SpaPrerenderingExtensions.cs | 25 +++++++++++++------ .../Prerendering/SpaPrerenderingOptions.cs | 5 ++++ ...ayer.AspNetCore.SpaServices.Routing.csproj | 2 +- ...tPlayer.AspNetCore.SpaServices.Xsrf.csproj | 2 +- .../MintPlayer.AspNetCore.SpaServices.csproj | 2 +- 12 files changed, 46 insertions(+), 23 deletions(-) diff --git a/MintPlayer.AspNetCore.NodeServices/Configuration/NodeServicesOptions.cs b/MintPlayer.AspNetCore.NodeServices/Configuration/NodeServicesOptions.cs index 8ecb143..5266a77 100644 --- a/MintPlayer.AspNetCore.NodeServices/Configuration/NodeServicesOptions.cs +++ b/MintPlayer.AspNetCore.NodeServices/Configuration/NodeServicesOptions.cs @@ -105,4 +105,9 @@ public NodeServicesOptions(IServiceProvider serviceProvider) /// A token that indicates when the host application is stopping. /// public CancellationToken ApplicationStoppingToken { get; set; } + + /// + /// Explicitly pass the path to the Node executable. + /// + public string NodePath { get; set; } = "node"; } diff --git a/MintPlayer.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs b/MintPlayer.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs index dea5746..aa82ea5 100644 --- a/MintPlayer.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs +++ b/MintPlayer.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs @@ -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), @@ -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); diff --git a/MintPlayer.AspNetCore.NodeServices/HostingModels/NodeServicesOptionsExtensions.cs b/MintPlayer.AspNetCore.NodeServices/HostingModels/NodeServicesOptionsExtensions.cs index 8a79f25..cabb7c7 100644 --- a/MintPlayer.AspNetCore.NodeServices/HostingModels/NodeServicesOptionsExtensions.cs +++ b/MintPlayer.AspNetCore.NodeServices/HostingModels/NodeServicesOptionsExtensions.cs @@ -14,6 +14,10 @@ public static class NodeServicesOptionsExtensions /// public static void UseHttpHosting(this NodeServicesOptions options) { - options.NodeInstanceFactory = () => new HttpNodeInstance(options); + options.NodeInstanceFactory = () => + { + var instance = new HttpNodeInstance(options); + return instance; + }; } } diff --git a/MintPlayer.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs b/MintPlayer.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs index 7d28637..b5f67ea 100644 --- a/MintPlayer.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs +++ b/MintPlayer.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs @@ -57,7 +57,8 @@ public OutOfProcessNodeInstance( IDictionary environmentVars, int invocationTimeoutMilliseconds, bool launchWithDebugging, - int debuggingPort) + int debuggingPort, + string nodePath) { if (nodeOutputLogger == null) { @@ -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); @@ -208,7 +209,7 @@ protected abstract Task InvokeExportAsync( /// protected virtual ProcessStartInfo PrepareNodeProcessStartInfo( string entryPointFilename, string projectPath, string commandLineArguments, - IDictionary environmentVars, bool launchWithDebugging, int debuggingPort) + IDictionary 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; @@ -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, diff --git a/MintPlayer.AspNetCore.NodeServices/MintPlayer.AspNetCore.NodeServices.csproj b/MintPlayer.AspNetCore.NodeServices/MintPlayer.AspNetCore.NodeServices.csproj index 84782bb..7b2a23d 100644 --- a/MintPlayer.AspNetCore.NodeServices/MintPlayer.AspNetCore.NodeServices.csproj +++ b/MintPlayer.AspNetCore.NodeServices/MintPlayer.AspNetCore.NodeServices.csproj @@ -9,7 +9,7 @@ true MintPlayer.AspNetCore.NodeServices - 8.2.0 + 8.3.0 true snupkg diff --git a/MintPlayer.AspNetCore.SpaServices.Abstractions/MintPlayer.AspNetCore.SpaServices.Abstractions.csproj b/MintPlayer.AspNetCore.SpaServices.Abstractions/MintPlayer.AspNetCore.SpaServices.Abstractions.csproj index 10eda2d..19e55f8 100644 --- a/MintPlayer.AspNetCore.SpaServices.Abstractions/MintPlayer.AspNetCore.SpaServices.Abstractions.csproj +++ b/MintPlayer.AspNetCore.SpaServices.Abstractions/MintPlayer.AspNetCore.SpaServices.Abstractions.csproj @@ -9,7 +9,7 @@ true MintPlayer.AspNetCore.SpaServices.Abstractions - 8.2.0 + 8.3.0 true snupkg diff --git a/MintPlayer.AspNetCore.SpaServices.Prerendering/MintPlayer.AspNetCore.SpaServices.Prerendering.csproj b/MintPlayer.AspNetCore.SpaServices.Prerendering/MintPlayer.AspNetCore.SpaServices.Prerendering.csproj index ec72945..4482e7a 100644 --- a/MintPlayer.AspNetCore.SpaServices.Prerendering/MintPlayer.AspNetCore.SpaServices.Prerendering.csproj +++ b/MintPlayer.AspNetCore.SpaServices.Prerendering/MintPlayer.AspNetCore.SpaServices.Prerendering.csproj @@ -9,7 +9,7 @@ true MintPlayer.AspNetCore.SpaServices.Prerendering - 8.2.0 + 8.3.0 true snupkg diff --git a/MintPlayer.AspNetCore.SpaServices.Prerendering/Prerendering/SpaPrerenderingExtensions.cs b/MintPlayer.AspNetCore.SpaServices.Prerendering/Prerendering/SpaPrerenderingExtensions.cs index 4b7715b..81256c4 100644 --- a/MintPlayer.AspNetCore.SpaServices.Prerendering/Prerendering/SpaPrerenderingExtensions.cs +++ b/MintPlayer.AspNetCore.SpaServices.Prerendering/Prerendering/SpaPrerenderingExtensions.cs @@ -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() - .ApplicationStopping; - var applicationBasePath = serviceProvider.GetRequiredService() - .ContentRootPath; + var nodeServices = GetNodeServices(serviceProvider, opts => opts.NodePath = options.NodePath); + var applicationStoppingToken = serviceProvider.GetRequiredService().ApplicationStopping; + var applicationBasePath = serviceProvider.GetRequiredService().ContentRootPath; var moduleExport = new JavaScriptModuleExport(capturedBootModulePath); var excludePathStrings = (options.ExcludeUrls ?? Array.Empty()) .Select(url => new PathString(url)) @@ -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 optionAction) { // Use the registered instance, or create a new private instance if none is registered var instance = serviceProvider.GetService(); - 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; + } } } diff --git a/MintPlayer.AspNetCore.SpaServices.Prerendering/Prerendering/SpaPrerenderingOptions.cs b/MintPlayer.AspNetCore.SpaServices.Prerendering/Prerendering/SpaPrerenderingOptions.cs index aa1b5dc..e358f70 100644 --- a/MintPlayer.AspNetCore.SpaServices.Prerendering/Prerendering/SpaPrerenderingOptions.cs +++ b/MintPlayer.AspNetCore.SpaServices.Prerendering/Prerendering/SpaPrerenderingOptions.cs @@ -29,6 +29,11 @@ public class SpaPrerenderingOptions /// public string[] ExcludeUrls { get; set; } + /// + /// Path to the Node executable + /// + public string NodePath { get; set; } = "node"; + /// /// This method is called after the prerendering logic completes, and before the next middleware is called. /// diff --git a/MintPlayer.AspNetCore.SpaServices.Routing/MintPlayer.AspNetCore.SpaServices.Routing.csproj b/MintPlayer.AspNetCore.SpaServices.Routing/MintPlayer.AspNetCore.SpaServices.Routing.csproj index fc810a8..a895775 100644 --- a/MintPlayer.AspNetCore.SpaServices.Routing/MintPlayer.AspNetCore.SpaServices.Routing.csproj +++ b/MintPlayer.AspNetCore.SpaServices.Routing/MintPlayer.AspNetCore.SpaServices.Routing.csproj @@ -9,7 +9,7 @@ true MintPlayer.AspNetCore.SpaServices.Routing - 8.2.0 + 8.3.0 true snupkg diff --git a/MintPlayer.AspNetCore.SpaServices.Xsrf/MintPlayer.AspNetCore.SpaServices.Xsrf.csproj b/MintPlayer.AspNetCore.SpaServices.Xsrf/MintPlayer.AspNetCore.SpaServices.Xsrf.csproj index 247aa51..f5a6491 100644 --- a/MintPlayer.AspNetCore.SpaServices.Xsrf/MintPlayer.AspNetCore.SpaServices.Xsrf.csproj +++ b/MintPlayer.AspNetCore.SpaServices.Xsrf/MintPlayer.AspNetCore.SpaServices.Xsrf.csproj @@ -9,7 +9,7 @@ true MintPlayer.AspNetCore.SpaServices.Xsrf - 8.2.0 + 8.3.0 true snupkg diff --git a/MintPlayer.AspNetCore.SpaServices/MintPlayer.AspNetCore.SpaServices.csproj b/MintPlayer.AspNetCore.SpaServices/MintPlayer.AspNetCore.SpaServices.csproj index 80f5044..18b20b1 100644 --- a/MintPlayer.AspNetCore.SpaServices/MintPlayer.AspNetCore.SpaServices.csproj +++ b/MintPlayer.AspNetCore.SpaServices/MintPlayer.AspNetCore.SpaServices.csproj @@ -10,7 +10,7 @@ true MintPlayer.AspNetCore.SpaServices - 8.2.0 + 8.3.0 true snupkg