From cb289fd3879d7a5eec67372a0ad7f133fd65666f Mon Sep 17 00:00:00 2001 From: SteveSandersonMS Date: Tue, 31 May 2016 22:29:27 +0100 Subject: [PATCH] Further stylistic tweaks --- .../PrimeCacheHelper.cs | 32 +++++++++---------- .../Configuration.cs | 9 +++--- .../HostingModels/HttpNodeInstance.cs | 2 ++ .../HostingModels/OutOfProcessNodeInstance.cs | 31 ++++++++++++------ .../Prerendering/JavaScriptModuleExport.cs | 2 +- .../Prerendering/Prerenderer.cs | 6 ++-- .../Routing/SpaRouteConstraint.cs | 9 ++++-- .../Routing/SpaRouteExtensions.cs | 5 +-- .../Webpack/ConditionalProxyMiddleware.cs | 8 ++--- .../Webpack/WebpackDevMiddleware.cs | 2 +- 10 files changed, 64 insertions(+), 42 deletions(-) diff --git a/src/Microsoft.AspNetCore.AngularServices/PrimeCacheHelper.cs b/src/Microsoft.AspNetCore.AngularServices/PrimeCacheHelper.cs index 53ce741..1b01662 100644 --- a/src/Microsoft.AspNetCore.AngularServices/PrimeCacheHelper.cs +++ b/src/Microsoft.AspNetCore.AngularServices/PrimeCacheHelper.cs @@ -25,16 +25,14 @@ namespace Microsoft.AspNetCore.AngularServices try { var request = html.ViewContext.HttpContext.Request; - var baseUri = - new Uri( - string.Concat( - request.Scheme, - "://", - request.Host.ToUriComponent(), - request.PathBase.ToUriComponent(), - request.Path.ToUriComponent(), - request.QueryString.ToUriComponent())); - var fullUri = new Uri(baseUri, url); + var baseUriString = string.Concat( + request.Scheme, + "://", + request.Host.ToUriComponent(), + request.PathBase.ToUriComponent(), + request.Path.ToUriComponent(), + request.QueryString.ToUriComponent()); + var fullUri = new Uri(new Uri(baseUriString), url); var response = await new HttpClient().GetAsync(fullUri.ToString()); var responseBody = await response.Content.ReadAsStringAsync(); return new HtmlString(FormatAsScript(url, response.StatusCode, responseBody)); @@ -48,11 +46,13 @@ namespace Microsoft.AspNetCore.AngularServices } private static string FormatAsScript(string url, HttpStatusCode responseStatusCode, string responseBody) - => - ""; + { + var preCachedUrl = JsonConvert.SerializeObject(url); + var preCachedJson = JsonConvert.SerializeObject(new { statusCode = responseStatusCode, body = responseBody }); + return ""; + } } } \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.NodeServices/Configuration.cs b/src/Microsoft.AspNetCore.NodeServices/Configuration.cs index d40733a..1a0e4ed 100644 --- a/src/Microsoft.AspNetCore.NodeServices/Configuration.cs +++ b/src/Microsoft.AspNetCore.NodeServices/Configuration.cs @@ -1,11 +1,9 @@ +using System; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.PlatformAbstractions; using Microsoft.AspNetCore.Hosting; namespace Microsoft.AspNetCore.NodeServices { - using System; - public static class Configuration { private static readonly string[] DefaultWatchFileExtensions = {".js", ".jsx", ".ts", ".tsx", ".json", ".html"}; @@ -20,15 +18,18 @@ namespace Microsoft.AspNetCore.NodeServices => AddNodeServices(serviceCollection, DefaultOptions); public static void AddNodeServices(this IServiceCollection serviceCollection, NodeServicesOptions options) - => serviceCollection.AddSingleton(typeof(INodeServices), serviceProvider => + { + serviceCollection.AddSingleton(typeof(INodeServices), serviceProvider => { var hostEnv = serviceProvider.GetRequiredService(); if (string.IsNullOrEmpty(options.ProjectPath)) { options.ProjectPath = hostEnv.ContentRootPath; } + return CreateNodeServices(options); }); + } public static INodeServices CreateNodeServices(NodeServicesOptions options) { diff --git a/src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs b/src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs index 8cff883..26edeb3 100644 --- a/src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs +++ b/src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs @@ -64,12 +64,14 @@ namespace Microsoft.AspNetCore.NodeServices { return JsonConvert.DeserializeObject(responseString); } + if (typeof(T) != typeof(string)) { throw new ArgumentException( "Node module responded with non-JSON string. This cannot be converted to the requested generic type: " + typeof(T).FullName); } + return (T)(object)responseString; } } diff --git a/src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs b/src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs index 65e21a3..ef25618 100644 --- a/src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs +++ b/src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs @@ -15,6 +15,7 @@ namespace Microsoft.AspNetCore.NodeServices private readonly object _childProcessLauncherLock; private readonly string _commandLineArguments; private readonly StringAsTempFile _entryPointScript; + private Process _nodeProcess; private TaskCompletionSource _nodeProcessIsReadySource; private readonly string _projectPath; private bool _disposed; @@ -27,18 +28,28 @@ namespace Microsoft.AspNetCore.NodeServices _commandLineArguments = commandLineArguments ?? string.Empty; } - protected Process NodeProcess { get; private set; } + protected Process NodeProcess + { + get + { + // This is only exposed to support the unreliable InputOutputStreamNodeInstance, which is just to verify that + // other hosting/transport mechanisms are possible. This shouldn't really be exposed, and will be removed. + return this._nodeProcess; + } + } public Task Invoke(string moduleName, params object[] args) => InvokeExport(moduleName, null, args); public Task InvokeExport(string moduleName, string exportedFunctionName, params object[] args) - => Invoke(new NodeInvocationInfo + { + return Invoke(new NodeInvocationInfo { ModuleName = moduleName, ExportedFunctionName = exportedFunctionName, Args = args }); + } public void Dispose() { @@ -52,7 +63,7 @@ namespace Microsoft.AspNetCore.NodeServices { lock (_childProcessLauncherLock) { - if (NodeProcess == null || NodeProcess.HasExited) + if (_nodeProcess == null || _nodeProcess.HasExited) { var startInfo = new ProcessStartInfo("node") { @@ -79,7 +90,7 @@ namespace Microsoft.AspNetCore.NodeServices #endif OnBeforeLaunchProcess(); - NodeProcess = Process.Start(startInfo); + _nodeProcess = Process.Start(startInfo); ConnectToInputOutputStreams(); } } @@ -98,7 +109,7 @@ namespace Microsoft.AspNetCore.NodeServices var initializationIsCompleted = false; // TODO: Make this thread-safe? (Interlocked.Exchange etc.) _nodeProcessIsReadySource = new TaskCompletionSource(); - NodeProcess.OutputDataReceived += (sender, evt) => + _nodeProcess.OutputDataReceived += (sender, evt) => { if (evt.Data == "[Microsoft.AspNetCore.NodeServices:Listening]" && !initializationIsCompleted) { @@ -111,7 +122,7 @@ namespace Microsoft.AspNetCore.NodeServices } }; - NodeProcess.ErrorDataReceived += (sender, evt) => + _nodeProcess.ErrorDataReceived += (sender, evt) => { if (evt.Data != null) { @@ -124,8 +135,8 @@ namespace Microsoft.AspNetCore.NodeServices } }; - NodeProcess.BeginOutputReadLine(); - NodeProcess.BeginErrorReadLine(); + _nodeProcess.BeginOutputReadLine(); + _nodeProcess.BeginErrorReadLine(); } protected virtual void OnBeforeLaunchProcess() @@ -151,9 +162,9 @@ namespace Microsoft.AspNetCore.NodeServices _entryPointScript.Dispose(); } - if (NodeProcess != null && !NodeProcess.HasExited) + if (_nodeProcess != null && !_nodeProcess.HasExited) { - NodeProcess.Kill(); + _nodeProcess.Kill(); // TODO: Is there a more graceful way to end it? Or does this still let it perform any cleanup? } diff --git a/src/Microsoft.AspNetCore.SpaServices/Prerendering/JavaScriptModuleExport.cs b/src/Microsoft.AspNetCore.SpaServices/Prerendering/JavaScriptModuleExport.cs index 054a693..3403866 100644 --- a/src/Microsoft.AspNetCore.SpaServices/Prerendering/JavaScriptModuleExport.cs +++ b/src/Microsoft.AspNetCore.SpaServices/Prerendering/JavaScriptModuleExport.cs @@ -4,7 +4,7 @@ namespace Microsoft.AspNetCore.SpaServices.Prerendering { public JavaScriptModuleExport(string moduleName) { - this.ModuleName = moduleName; + ModuleName = moduleName; } public string ModuleName { get; private set; } diff --git a/src/Microsoft.AspNetCore.SpaServices/Prerendering/Prerenderer.cs b/src/Microsoft.AspNetCore.SpaServices/Prerendering/Prerenderer.cs index 5ed8552..3b08eff 100644 --- a/src/Microsoft.AspNetCore.SpaServices/Prerendering/Prerenderer.cs +++ b/src/Microsoft.AspNetCore.SpaServices/Prerendering/Prerenderer.cs @@ -17,18 +17,20 @@ namespace Microsoft.AspNetCore.SpaServices.Prerendering }); } - public static async Task RenderToString( + public static Task RenderToString( string applicationBasePath, INodeServices nodeServices, JavaScriptModuleExport bootModule, string requestAbsoluteUrl, string requestPathAndQuery) - => await nodeServices.InvokeExport( + { + return nodeServices.InvokeExport( NodeScript.Value.FileName, "renderToString", applicationBasePath, bootModule, requestAbsoluteUrl, requestPathAndQuery); + } } } \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.SpaServices/Routing/SpaRouteConstraint.cs b/src/Microsoft.AspNetCore.SpaServices/Routing/SpaRouteConstraint.cs index 025d11a..d6a1d5b 100644 --- a/src/Microsoft.AspNetCore.SpaServices/Routing/SpaRouteConstraint.cs +++ b/src/Microsoft.AspNetCore.SpaServices/Routing/SpaRouteConstraint.cs @@ -24,9 +24,14 @@ namespace Microsoft.AspNetCore.SpaServices string routeKey, RouteValueDictionary values, RouteDirection routeDirection) - => !HasDotInLastSegment(values[_clientRouteTokenName] as string ?? string.Empty); + { + return !HasDotInLastSegment(values[_clientRouteTokenName] as string ?? string.Empty); + } private bool HasDotInLastSegment(string uri) - => uri.IndexOf('.', uri.LastIndexOf('/') + 1) >= 0; + { + var lastSegmentStartPos = uri.LastIndexOf('/'); + return uri.IndexOf('.', lastSegmentStartPos + 1) >= 0; + } } } \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.SpaServices/Routing/SpaRouteExtensions.cs b/src/Microsoft.AspNetCore.SpaServices/Routing/SpaRouteExtensions.cs index 7bdebc6..919390e 100644 --- a/src/Microsoft.AspNetCore.SpaServices/Routing/SpaRouteExtensions.cs +++ b/src/Microsoft.AspNetCore.SpaServices/Routing/SpaRouteExtensions.cs @@ -17,13 +17,15 @@ namespace Microsoft.AspNetCore.Builder object defaults, object constraints = null, object dataTokens = null) - => MapSpaFallbackRoute( + { + MapSpaFallbackRoute( routeBuilder, name, /* templatePrefix */ null, defaults, constraints, dataTokens); + } public static void MapSpaFallbackRoute( this IRouteBuilder routeBuilder, @@ -34,7 +36,6 @@ namespace Microsoft.AspNetCore.Builder object dataTokens = null) { var template = CreateRouteTemplate(templatePrefix); - var constraintsDict = ObjectToDictionary(constraints); constraintsDict.Add(ClientRouteTokenName, new SpaRouteConstraint(ClientRouteTokenName)); diff --git a/src/Microsoft.AspNetCore.SpaServices/Webpack/ConditionalProxyMiddleware.cs b/src/Microsoft.AspNetCore.SpaServices/Webpack/ConditionalProxyMiddleware.cs index 63c4f3e..72b66c3 100644 --- a/src/Microsoft.AspNetCore.SpaServices/Webpack/ConditionalProxyMiddleware.cs +++ b/src/Microsoft.AspNetCore.SpaServices/Webpack/ConditionalProxyMiddleware.cs @@ -67,10 +67,10 @@ namespace Microsoft.AspNetCore.SpaServices.Webpack requestMessage.Method = new HttpMethod(context.Request.Method); using ( - var responseMessage = - await - _httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, - context.RequestAborted)) + var responseMessage = await _httpClient.SendAsync( + requestMessage, + HttpCompletionOption.ResponseHeadersRead, + context.RequestAborted)) { if (responseMessage.StatusCode == HttpStatusCode.NotFound) { diff --git a/src/Microsoft.AspNetCore.SpaServices/Webpack/WebpackDevMiddleware.cs b/src/Microsoft.AspNetCore.SpaServices/Webpack/WebpackDevMiddleware.cs index 88fb329..7c59721 100644 --- a/src/Microsoft.AspNetCore.SpaServices/Webpack/WebpackDevMiddleware.cs +++ b/src/Microsoft.AspNetCore.SpaServices/Webpack/WebpackDevMiddleware.cs @@ -91,4 +91,4 @@ namespace Microsoft.AspNetCore.Builder } } #pragma warning restore CS0649 -} +} \ No newline at end of file