77using Newtonsoft . Json ;
88using Newtonsoft . Json . Serialization ;
99
10- namespace Microsoft . AspNetCore . NodeServices {
11- internal class HttpNodeInstance : OutOfProcessNodeInstance {
12- private readonly static Regex PortMessageRegex = new Regex ( @"^\[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on port (\d+)\]$" ) ;
10+ namespace Microsoft . AspNetCore . NodeServices
11+ {
12+ internal class HttpNodeInstance : OutOfProcessNodeInstance
13+ {
14+ private static readonly Regex PortMessageRegex =
15+ new Regex ( @"^\[Microsoft.AspNetCore.NodeServices.HttpNodeHost:Listening on port (\d+)\]$" ) ;
1316
14- private readonly static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings {
17+ private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings
18+ {
1519 ContractResolver = new CamelCasePropertyNamesContractResolver ( )
1620 } ;
1721
1822 private int _portNumber ;
1923
20- public HttpNodeInstance ( string projectPath , int port = 0 , string [ ] watchFileExtensions = null )
21- : base ( EmbeddedResourceReader . Read ( typeof ( HttpNodeInstance ) , "/Content/Node/entrypoint-http.js" ) , projectPath , MakeCommandLineOptions ( port , watchFileExtensions ) )
24+ public HttpNodeInstance ( string projectPath , int port = 0 , string [ ] watchFileExtensions = null )
25+ : base (
26+ EmbeddedResourceReader . Read (
27+ typeof ( HttpNodeInstance ) ,
28+ "/Content/Node/entrypoint-http.js" ) ,
29+ projectPath ,
30+ MakeCommandLineOptions ( port , watchFileExtensions ) )
2231 {
23- }
32+ }
2433
25- private static string MakeCommandLineOptions ( int port , string [ ] watchFileExtensions ) {
26- var result = "--port " + port . ToString ( ) ;
27- if ( watchFileExtensions != null && watchFileExtensions . Length > 0 ) {
34+ private static string MakeCommandLineOptions ( int port , string [ ] watchFileExtensions )
35+ {
36+ var result = "--port " + port ;
37+ if ( watchFileExtensions != null && watchFileExtensions . Length > 0 )
38+ {
2839 result += " --watch " + string . Join ( "," , watchFileExtensions ) ;
2940 }
41+
3042 return result ;
3143 }
3244
33- public override async Task < T > Invoke < T > ( NodeInvocationInfo invocationInfo ) {
34- await this . EnsureReady ( ) ;
45+ public override async Task < T > Invoke < T > ( NodeInvocationInfo invocationInfo )
46+ {
47+ await EnsureReady ( ) ;
3548
36- using ( var client = new HttpClient ( ) ) {
49+ using ( var client = new HttpClient ( ) )
50+ {
3751 // TODO: Use System.Net.Http.Formatting (PostAsJsonAsync etc.)
38- var payloadJson = JsonConvert . SerializeObject ( invocationInfo , jsonSerializerSettings ) ;
52+ var payloadJson = JsonConvert . SerializeObject ( invocationInfo , JsonSerializerSettings ) ;
3953 var payload = new StringContent ( payloadJson , Encoding . UTF8 , "application/json" ) ;
40- var response = await client . PostAsync ( "http://localhost:" + this . _portNumber , payload ) ;
54+ var response = await client . PostAsync ( "http://localhost:" + _portNumber , payload ) ;
4155 var responseString = await response . Content . ReadAsStringAsync ( ) ;
4256
43- if ( ! response . IsSuccessStatusCode ) {
57+ if ( ! response . IsSuccessStatusCode )
58+ {
4459 throw new Exception ( "Call to Node module failed with error: " + responseString ) ;
4560 }
4661
4762 var responseIsJson = response . Content . Headers . ContentType . MediaType == "application/json" ;
48- if ( responseIsJson ) {
63+ if ( responseIsJson )
64+ {
4965 return JsonConvert . DeserializeObject < T > ( responseString ) ;
50- } else if ( typeof ( T ) != typeof ( string ) ) {
51- throw new System . ArgumentException ( "Node module responded with non-JSON string. This cannot be converted to the requested generic type: " + typeof ( T ) . FullName ) ;
52- } else {
53- return ( T ) ( object ) responseString ;
5466 }
67+ if ( typeof ( T ) != typeof ( string ) )
68+ {
69+ throw new ArgumentException (
70+ "Node module responded with non-JSON string. This cannot be converted to the requested generic type: " +
71+ typeof ( T ) . FullName ) ;
72+ }
73+ return ( T ) ( object ) responseString ;
5574 }
5675 }
5776
58- protected override void OnOutputDataReceived ( string outputData ) {
59- var match = this . _portNumber != 0 ? null : PortMessageRegex . Match ( outputData ) ;
60- if ( match != null && match . Success ) {
61- this . _portNumber = int . Parse ( match . Groups [ 1 ] . Captures [ 0 ] . Value ) ;
62- } else {
77+ protected override void OnOutputDataReceived ( string outputData )
78+ {
79+ var match = _portNumber != 0 ? null : PortMessageRegex . Match ( outputData ) ;
80+ if ( match != null && match . Success )
81+ {
82+ _portNumber = int . Parse ( match . Groups [ 1 ] . Captures [ 0 ] . Value ) ;
83+ }
84+ else
85+ {
6386 base . OnOutputDataReceived ( outputData ) ;
6487 }
6588 }
6689
67- protected override void OnBeforeLaunchProcess ( ) {
90+ protected override void OnBeforeLaunchProcess ( )
91+ {
6892 // Prepare to receive a new port number
69- this . _portNumber = 0 ;
93+ _portNumber = 0 ;
7094 }
7195 }
72- }
96+ }
0 commit comments