1+ /*
2+ * Copyright (c) 2015, Facebook, Inc.
3+ * All rights reserved.
4+ *
5+ * This source code is licensed under the BSD-style license found in the
6+ * LICENSE file in the root directory of this source tree. An additional grant
7+ * of patent rights can be found in the PATENTS file in the same directory.
8+ */
9+
10+ using System ;
11+ using System . Linq ;
12+ using System . Threading . Tasks ;
13+ using Microsoft . AspNet . Builder ;
14+ using Microsoft . AspNet . Hosting ;
15+ using Microsoft . AspNet . Http ;
16+ using Microsoft . AspNet . StaticFiles ;
17+ using Microsoft . Framework . Logging ;
18+
19+ namespace React . AspNet5
20+ {
21+ /// <summary>
22+ /// Enables serving static JSX files transformed to pure JavaScript. Wraps around StaticFileMiddleware.
23+ /// </summary>
24+ public class JsxFileMiddleware
25+ {
26+ private readonly RequestDelegate _next ;
27+ private readonly IHostingEnvironment _hostingEnv ;
28+ private readonly ILoggerFactory _loggerFactory ;
29+ private readonly JsxFileOptions _options ;
30+
31+ /// <summary>
32+ /// Creates a new instance of the JsxFileMiddleware.
33+ /// </summary>
34+ /// <param name="next">The next middleware in the pipeline.</param>
35+ /// <param name="options">The configuration options.</param>
36+ /// <param name="hostingEnv">The hosting environment.</param>
37+ /// <param name="loggerFactory">An <see cref="ILoggerFactory"/> instance used to create loggers.</param>
38+ public JsxFileMiddleware ( RequestDelegate next , JsxFileOptions options , IHostingEnvironment hostingEnv , ILoggerFactory loggerFactory )
39+ {
40+ if ( next == null )
41+ throw new ArgumentNullException ( "next" ) ;
42+
43+ _next = next ;
44+ _hostingEnv = hostingEnv ;
45+ _loggerFactory = loggerFactory ;
46+
47+ // Default values
48+ _options = options ?? new JsxFileOptions ( ) ;
49+ }
50+
51+ /// <summary>
52+ /// Processes a request to determine if it matches a known JSX file, and if so, serves it compiled to JavaScript.
53+ /// </summary>
54+ /// <param name="context">ASP.NET HTTP context</param>
55+ public async Task Invoke ( HttpContext context )
56+ {
57+ if ( ! context . Request . Path . HasValue || ! _options . Extensions . Any ( context . Request . Path . Value . EndsWith ) )
58+ {
59+ // Not a request for a JSX file, so just pass through to the next middleware
60+ await _next ( context ) ;
61+ return ;
62+ }
63+
64+ var reactEnvironment = React . AssemblyRegistration . Container . Resolve < IReactEnvironment > ( ) ;
65+ var internalStaticMiddleware = CreateFileMiddleware ( reactEnvironment . JsxTransformer ) ;
66+ await internalStaticMiddleware . Invoke ( context ) ;
67+ }
68+
69+ /// <summary>
70+ /// Creates the internal <see cref="StaticFileMiddleware"/> used to serve JSX files.
71+ /// </summary>
72+ /// <param name="jsxTransformer"></param>
73+ /// <returns></returns>
74+ private StaticFileMiddleware CreateFileMiddleware ( IJsxTransformer jsxTransformer )
75+ {
76+ return new StaticFileMiddleware (
77+ _next ,
78+ _hostingEnv ,
79+ new StaticFileOptions
80+ {
81+ ContentTypeProvider = _options . StaticFileOptions . ContentTypeProvider ,
82+ DefaultContentType = _options . StaticFileOptions . DefaultContentType ,
83+ OnPrepareResponse = _options . StaticFileOptions . OnPrepareResponse ,
84+ RequestPath = _options . StaticFileOptions . RequestPath ,
85+ ServeUnknownFileTypes = _options . StaticFileOptions . ServeUnknownFileTypes ,
86+ FileSystem = new JsxFileSystem (
87+ jsxTransformer ,
88+ _options . StaticFileOptions . FileSystem ?? _hostingEnv . WebRootFileSystem ,
89+ _options . Extensions
90+ )
91+ } ,
92+ _loggerFactory
93+ ) ;
94+ }
95+ }
96+ }
97+
0 commit comments