X Tutup
Skip to content

Commit cb289fd

Browse files
Further stylistic tweaks
1 parent 5bb92d0 commit cb289fd

File tree

10 files changed

+64
-42
lines changed

10 files changed

+64
-42
lines changed

src/Microsoft.AspNetCore.AngularServices/PrimeCacheHelper.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@ public static async Task<HtmlString> PrimeCache(this IHtmlHelper html, string ur
2525
try
2626
{
2727
var request = html.ViewContext.HttpContext.Request;
28-
var baseUri =
29-
new Uri(
30-
string.Concat(
31-
request.Scheme,
32-
"://",
33-
request.Host.ToUriComponent(),
34-
request.PathBase.ToUriComponent(),
35-
request.Path.ToUriComponent(),
36-
request.QueryString.ToUriComponent()));
37-
var fullUri = new Uri(baseUri, url);
28+
var baseUriString = string.Concat(
29+
request.Scheme,
30+
"://",
31+
request.Host.ToUriComponent(),
32+
request.PathBase.ToUriComponent(),
33+
request.Path.ToUriComponent(),
34+
request.QueryString.ToUriComponent());
35+
var fullUri = new Uri(new Uri(baseUriString), url);
3836
var response = await new HttpClient().GetAsync(fullUri.ToString());
3937
var responseBody = await response.Content.ReadAsStringAsync();
4038
return new HtmlString(FormatAsScript(url, response.StatusCode, responseBody));
@@ -48,11 +46,13 @@ public static async Task<HtmlString> PrimeCache(this IHtmlHelper html, string ur
4846
}
4947

5048
private static string FormatAsScript(string url, HttpStatusCode responseStatusCode, string responseBody)
51-
=>
52-
"<script>" +
53-
"window.__preCachedResponses = window.__preCachedResponses || {}; " +
54-
$"window.__preCachedResponses[{JsonConvert.SerializeObject(url)}] " +
55-
$"= {JsonConvert.SerializeObject(new { statusCode = responseStatusCode, body = responseBody })};" +
56-
"</script>";
49+
{
50+
var preCachedUrl = JsonConvert.SerializeObject(url);
51+
var preCachedJson = JsonConvert.SerializeObject(new { statusCode = responseStatusCode, body = responseBody });
52+
return "<script>"
53+
+ "window.__preCachedResponses = window.__preCachedResponses || {};"
54+
+ $"window.__preCachedResponses[{preCachedUrl}] = {preCachedJson};"
55+
+ "</script>";
56+
}
5757
}
5858
}

src/Microsoft.AspNetCore.NodeServices/Configuration.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
using System;
12
using Microsoft.Extensions.DependencyInjection;
2-
using Microsoft.Extensions.PlatformAbstractions;
33
using Microsoft.AspNetCore.Hosting;
44

55
namespace Microsoft.AspNetCore.NodeServices
66
{
7-
using System;
8-
97
public static class Configuration
108
{
119
private static readonly string[] DefaultWatchFileExtensions = {".js", ".jsx", ".ts", ".tsx", ".json", ".html"};
@@ -20,15 +18,18 @@ public static void AddNodeServices(this IServiceCollection serviceCollection)
2018
=> AddNodeServices(serviceCollection, DefaultOptions);
2119

2220
public static void AddNodeServices(this IServiceCollection serviceCollection, NodeServicesOptions options)
23-
=> serviceCollection.AddSingleton(typeof(INodeServices), serviceProvider =>
21+
{
22+
serviceCollection.AddSingleton(typeof(INodeServices), serviceProvider =>
2423
{
2524
var hostEnv = serviceProvider.GetRequiredService<IHostingEnvironment>();
2625
if (string.IsNullOrEmpty(options.ProjectPath))
2726
{
2827
options.ProjectPath = hostEnv.ContentRootPath;
2928
}
29+
3030
return CreateNodeServices(options);
3131
});
32+
}
3233

3334
public static INodeServices CreateNodeServices(NodeServicesOptions options)
3435
{

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ public override async Task<T> Invoke<T>(NodeInvocationInfo invocationInfo)
6464
{
6565
return JsonConvert.DeserializeObject<T>(responseString);
6666
}
67+
6768
if (typeof(T) != typeof(string))
6869
{
6970
throw new ArgumentException(
7071
"Node module responded with non-JSON string. This cannot be converted to the requested generic type: " +
7172
typeof(T).FullName);
7273
}
74+
7375
return (T)(object)responseString;
7476
}
7577
}

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public abstract class OutOfProcessNodeInstance : INodeServices
1515
private readonly object _childProcessLauncherLock;
1616
private readonly string _commandLineArguments;
1717
private readonly StringAsTempFile _entryPointScript;
18+
private Process _nodeProcess;
1819
private TaskCompletionSource<bool> _nodeProcessIsReadySource;
1920
private readonly string _projectPath;
2021
private bool _disposed;
@@ -27,18 +28,28 @@ public OutOfProcessNodeInstance(string entryPointScript, string projectPath, str
2728
_commandLineArguments = commandLineArguments ?? string.Empty;
2829
}
2930

30-
protected Process NodeProcess { get; private set; }
31+
protected Process NodeProcess
32+
{
33+
get
34+
{
35+
// This is only exposed to support the unreliable InputOutputStreamNodeInstance, which is just to verify that
36+
// other hosting/transport mechanisms are possible. This shouldn't really be exposed, and will be removed.
37+
return this._nodeProcess;
38+
}
39+
}
3140

3241
public Task<T> Invoke<T>(string moduleName, params object[] args)
3342
=> InvokeExport<T>(moduleName, null, args);
3443

3544
public Task<T> InvokeExport<T>(string moduleName, string exportedFunctionName, params object[] args)
36-
=> Invoke<T>(new NodeInvocationInfo
45+
{
46+
return Invoke<T>(new NodeInvocationInfo
3747
{
3848
ModuleName = moduleName,
3949
ExportedFunctionName = exportedFunctionName,
4050
Args = args
4151
});
52+
}
4253

4354
public void Dispose()
4455
{
@@ -52,7 +63,7 @@ protected async Task EnsureReady()
5263
{
5364
lock (_childProcessLauncherLock)
5465
{
55-
if (NodeProcess == null || NodeProcess.HasExited)
66+
if (_nodeProcess == null || _nodeProcess.HasExited)
5667
{
5768
var startInfo = new ProcessStartInfo("node")
5869
{
@@ -79,7 +90,7 @@ protected async Task EnsureReady()
7990
#endif
8091

8192
OnBeforeLaunchProcess();
82-
NodeProcess = Process.Start(startInfo);
93+
_nodeProcess = Process.Start(startInfo);
8394
ConnectToInputOutputStreams();
8495
}
8596
}
@@ -98,7 +109,7 @@ private void ConnectToInputOutputStreams()
98109
var initializationIsCompleted = false; // TODO: Make this thread-safe? (Interlocked.Exchange etc.)
99110
_nodeProcessIsReadySource = new TaskCompletionSource<bool>();
100111

101-
NodeProcess.OutputDataReceived += (sender, evt) =>
112+
_nodeProcess.OutputDataReceived += (sender, evt) =>
102113
{
103114
if (evt.Data == "[Microsoft.AspNetCore.NodeServices:Listening]" && !initializationIsCompleted)
104115
{
@@ -111,7 +122,7 @@ private void ConnectToInputOutputStreams()
111122
}
112123
};
113124

114-
NodeProcess.ErrorDataReceived += (sender, evt) =>
125+
_nodeProcess.ErrorDataReceived += (sender, evt) =>
115126
{
116127
if (evt.Data != null)
117128
{
@@ -124,8 +135,8 @@ private void ConnectToInputOutputStreams()
124135
}
125136
};
126137

127-
NodeProcess.BeginOutputReadLine();
128-
NodeProcess.BeginErrorReadLine();
138+
_nodeProcess.BeginOutputReadLine();
139+
_nodeProcess.BeginErrorReadLine();
129140
}
130141

131142
protected virtual void OnBeforeLaunchProcess()
@@ -151,9 +162,9 @@ protected virtual void Dispose(bool disposing)
151162
_entryPointScript.Dispose();
152163
}
153164

154-
if (NodeProcess != null && !NodeProcess.HasExited)
165+
if (_nodeProcess != null && !_nodeProcess.HasExited)
155166
{
156-
NodeProcess.Kill();
167+
_nodeProcess.Kill();
157168
// TODO: Is there a more graceful way to end it? Or does this still let it perform any cleanup?
158169
}
159170

src/Microsoft.AspNetCore.SpaServices/Prerendering/JavaScriptModuleExport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class JavaScriptModuleExport
44
{
55
public JavaScriptModuleExport(string moduleName)
66
{
7-
this.ModuleName = moduleName;
7+
ModuleName = moduleName;
88
}
99

1010
public string ModuleName { get; private set; }

src/Microsoft.AspNetCore.SpaServices/Prerendering/Prerenderer.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ static Prerenderer()
1717
});
1818
}
1919

20-
public static async Task<RenderToStringResult> RenderToString(
20+
public static Task<RenderToStringResult> RenderToString(
2121
string applicationBasePath,
2222
INodeServices nodeServices,
2323
JavaScriptModuleExport bootModule,
2424
string requestAbsoluteUrl,
2525
string requestPathAndQuery)
26-
=> await nodeServices.InvokeExport<RenderToStringResult>(
26+
{
27+
return nodeServices.InvokeExport<RenderToStringResult>(
2728
NodeScript.Value.FileName,
2829
"renderToString",
2930
applicationBasePath,
3031
bootModule,
3132
requestAbsoluteUrl,
3233
requestPathAndQuery);
34+
}
3335
}
3436
}

src/Microsoft.AspNetCore.SpaServices/Routing/SpaRouteConstraint.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ public bool Match(
2424
string routeKey,
2525
RouteValueDictionary values,
2626
RouteDirection routeDirection)
27-
=> !HasDotInLastSegment(values[_clientRouteTokenName] as string ?? string.Empty);
27+
{
28+
return !HasDotInLastSegment(values[_clientRouteTokenName] as string ?? string.Empty);
29+
}
2830

2931
private bool HasDotInLastSegment(string uri)
30-
=> uri.IndexOf('.', uri.LastIndexOf('/') + 1) >= 0;
32+
{
33+
var lastSegmentStartPos = uri.LastIndexOf('/');
34+
return uri.IndexOf('.', lastSegmentStartPos + 1) >= 0;
35+
}
3136
}
3237
}

src/Microsoft.AspNetCore.SpaServices/Routing/SpaRouteExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ public static void MapSpaFallbackRoute(
1717
object defaults,
1818
object constraints = null,
1919
object dataTokens = null)
20-
=> MapSpaFallbackRoute(
20+
{
21+
MapSpaFallbackRoute(
2122
routeBuilder,
2223
name,
2324
/* templatePrefix */ null,
2425
defaults,
2526
constraints,
2627
dataTokens);
28+
}
2729

2830
public static void MapSpaFallbackRoute(
2931
this IRouteBuilder routeBuilder,
@@ -34,7 +36,6 @@ public static void MapSpaFallbackRoute(
3436
object dataTokens = null)
3537
{
3638
var template = CreateRouteTemplate(templatePrefix);
37-
3839
var constraintsDict = ObjectToDictionary(constraints);
3940
constraintsDict.Add(ClientRouteTokenName, new SpaRouteConstraint(ClientRouteTokenName));
4041

src/Microsoft.AspNetCore.SpaServices/Webpack/ConditionalProxyMiddleware.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ private async Task<bool> PerformProxyRequest(HttpContext context)
6767
requestMessage.Method = new HttpMethod(context.Request.Method);
6868

6969
using (
70-
var responseMessage =
71-
await
72-
_httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead,
73-
context.RequestAborted))
70+
var responseMessage = await _httpClient.SendAsync(
71+
requestMessage,
72+
HttpCompletionOption.ResponseHeadersRead,
73+
context.RequestAborted))
7474
{
7575
if (responseMessage.StatusCode == HttpStatusCode.NotFound)
7676
{

src/Microsoft.AspNetCore.SpaServices/Webpack/WebpackDevMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,4 @@ class WebpackDevServerInfo
9191
}
9292
}
9393
#pragma warning restore CS0649
94-
}
94+
}

0 commit comments

Comments
 (0)
X Tutup