|
| 1 | +using Microsoft.AspNetCore.Builder; |
| 2 | +using Microsoft.AspNetCore.NodeServices.Npm; |
| 3 | +using Microsoft.AspNetCore.NodeServices.Util; |
| 4 | +using Microsoft.AspNetCore.SpaServices.Extensions.Util; |
| 5 | +using Microsoft.AspNetCore.SpaServices.Util; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.IO; |
| 10 | +using System.Net.Http; |
| 11 | +using System.Text; |
| 12 | +using System.Text.RegularExpressions; |
| 13 | +using System.Threading; |
| 14 | +using System.Threading.Tasks; |
| 15 | + |
| 16 | +namespace Microsoft.AspNetCore.SpaServices.Extensions.Vue |
| 17 | +{ |
| 18 | + internal static class VueCliMiddleware |
| 19 | + { |
| 20 | + private const string LogCategoryName = "Microsoft.AspNetCore.SpaServices"; |
| 21 | + private static TimeSpan RegexMatchTimeout = TimeSpan.FromSeconds(5); // This is a development-time only feature, so a very long timeout is fine |
| 22 | + |
| 23 | + public static void Attach( |
| 24 | + ISpaBuilder spaBuilder, |
| 25 | + string npmScriptName) |
| 26 | + { |
| 27 | + var sourcePath = spaBuilder.Options.SourcePath; |
| 28 | + if (string.IsNullOrEmpty(sourcePath)) |
| 29 | + { |
| 30 | + throw new ArgumentException("Cannot be null or empty", nameof(sourcePath)); |
| 31 | + } |
| 32 | + |
| 33 | + if (string.IsNullOrEmpty(npmScriptName)) |
| 34 | + { |
| 35 | + throw new ArgumentException("Cannot be null or empty", nameof(npmScriptName)); |
| 36 | + } |
| 37 | + |
| 38 | + // Start Vue CLI and attach to middleware pipeline |
| 39 | + var appBuilder = spaBuilder.ApplicationBuilder; |
| 40 | + var logger = LoggerFinder.GetOrCreateLogger(appBuilder, LogCategoryName); |
| 41 | + var angularCliServerInfoTask = StartVueCliServerAsync(sourcePath, npmScriptName, logger); |
| 42 | + |
| 43 | + // Everything we proxy is hardcoded to target http://localhost because: |
| 44 | + // - the requests are always from the local machine (we're not accepting remote |
| 45 | + // requests that go directly to the Vue CLI middleware server) |
| 46 | + // - given that, there's no reason to use https, and we couldn't even if we |
| 47 | + // wanted to, because in general the Vue CLI server has no certificate |
| 48 | + var targetUriTask = angularCliServerInfoTask.ContinueWith( |
| 49 | + task => new UriBuilder("http", "localhost", task.Result.Port).Uri); |
| 50 | + |
| 51 | + SpaProxyingExtensions.UseProxyToSpaDevelopmentServer(spaBuilder, () => |
| 52 | + { |
| 53 | + // On each request, we create a separate startup task with its own timeout. That way, even if |
| 54 | + // the first request times out, subsequent requests could still work. |
| 55 | + var timeout = spaBuilder.Options.StartupTimeout; |
| 56 | + return targetUriTask.WithTimeout(timeout, |
| 57 | + $"The Vue CLI process did not start listening for requests " + |
| 58 | + $"within the timeout period of {timeout.Seconds} seconds. " + |
| 59 | + $"Check the log output for error information."); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + private static async Task<VueCliServerInfo> StartVueCliServerAsync( |
| 64 | + string sourcePath, string npmScriptName, ILogger logger) |
| 65 | + { |
| 66 | + var portNumber = TcpPortFinder.FindAvailablePort(); |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + //logger.LogTrace($"Starting Vue/dev-server on port {portNumber}..."); |
| 72 | + //logger.LogDebug($"Starting Vue/dev-server on port {portNumber}..."); |
| 73 | + //logger.LogInformation($"Starting Vue/dev-server on port {portNumber}..."); |
| 74 | + //logger.LogWarning($"Starting Vue/dev-server on port {portNumber}..."); |
| 75 | + //logger.LogError($"Starting Vue/dev-server on port {portNumber}..."); |
| 76 | + //logger.LogCritical($"Starting Vue/dev-server on port {portNumber}..."); |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + var npmScriptRunner = new NpmScriptRunner( |
| 82 | + //sourcePath, npmScriptName, $"--port {portNumber}"); |
| 83 | + sourcePath, npmScriptName, $"{portNumber}"); |
| 84 | + |
| 85 | + npmScriptRunner.AttachToLogger(logger); |
| 86 | + |
| 87 | + Match openBrowserLine; |
| 88 | + using (var stdErrReader = new EventedStreamStringReader(npmScriptRunner.StdErr)) |
| 89 | + { |
| 90 | + try |
| 91 | + { |
| 92 | + //openBrowserLine = await npmScriptRunner.StdOut.WaitForMatch( |
| 93 | + // new Regex("open your browser on (http\\S+)", RegexOptions.None, RegexMatchTimeout)); |
| 94 | + openBrowserLine = await npmScriptRunner.StdOut.WaitForMatch( |
| 95 | + new Regex("- Local: (http:\\S+/)", RegexOptions.None, RegexMatchTimeout)); |
| 96 | + } |
| 97 | + catch (EndOfStreamException ex) |
| 98 | + { |
| 99 | + throw new InvalidOperationException( |
| 100 | + $"The NPM script '{npmScriptName}' exited without indicating that the " + |
| 101 | + $"Angular CLI was listening for requests. The error output was: " + |
| 102 | + $"{stdErrReader.ReadAsString()}", ex); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + var uri = new Uri(openBrowserLine.Groups[1].Value); |
| 107 | + var serverInfo = new VueCliServerInfo { Port = uri.Port }; |
| 108 | + |
| 109 | + // Even after the Angular CLI claims to be listening for requests, there's a short |
| 110 | + // period where it will give an error if you make a request too quickly |
| 111 | + await WaitForVueCliServerToAcceptRequests(uri); |
| 112 | + return serverInfo; |
| 113 | + } |
| 114 | + |
| 115 | + private static async Task WaitForVueCliServerToAcceptRequests(Uri cliServerUri) |
| 116 | + { |
| 117 | + // To determine when it's actually ready, try making HEAD requests to '/'. If it |
| 118 | + // produces any HTTP response (even if it's 404) then it's ready. If it rejects the |
| 119 | + // connection then it's not ready. We keep trying forever because this is dev-mode |
| 120 | + // only, and only a single startup attempt will be made, and there's a further level |
| 121 | + // of timeouts enforced on a per-request basis. |
| 122 | + var timeoutMilliseconds = 1000; |
| 123 | + using (var client = new HttpClient()) |
| 124 | + { |
| 125 | + while (true) |
| 126 | + { |
| 127 | + try |
| 128 | + { |
| 129 | + // If we get any HTTP response, the CLI server is ready |
| 130 | + await client.SendAsync( |
| 131 | + new HttpRequestMessage(HttpMethod.Head, cliServerUri), |
| 132 | + new CancellationTokenSource(timeoutMilliseconds).Token); |
| 133 | + return; |
| 134 | + } |
| 135 | + catch (Exception) |
| 136 | + { |
| 137 | + await Task.Delay(500); |
| 138 | + |
| 139 | + // Depending on the host's networking configuration, the requests can take a while |
| 140 | + // to go through, most likely due to the time spent resolving 'localhost'. |
| 141 | + // Each time we have a failure, allow a bit longer next time (up to a maximum). |
| 142 | + // This only influences the time until we regard the dev server as 'ready', so it |
| 143 | + // doesn't affect the runtime perf (even in dev mode) once the first connection is made. |
| 144 | + // Resolves https://github.com/aspnet/JavaScriptServices/issues/1611 |
| 145 | + if (timeoutMilliseconds < 10000) |
| 146 | + { |
| 147 | + timeoutMilliseconds += 3000; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + class VueCliServerInfo |
| 155 | + { |
| 156 | + public int Port { get; set; } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments