forked from reactjs/React.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsxHandler.cs
More file actions
119 lines (109 loc) · 3.53 KB
/
JsxHandler.cs
File metadata and controls
119 lines (109 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright (c) 2014-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using System.Web;
namespace React.Web
{
/// <summary>
/// ASP.NET handler that transforms JSX into JavaScript.
/// </summary>
public class JsxHandler : IJsxHandler
{
private readonly IReactEnvironment _environment;
private readonly IFileSystem _fileSystem;
private readonly HttpRequestBase _request;
private readonly HttpResponseBase _response;
/// <summary>
/// Initializes a new instance of the <see cref="JsxHandler"/> class.
/// </summary>
/// <param name="environment">The environment.</param>
/// <param name="fileSystem">File system</param>
/// <param name="request">HTTP request</param>
/// <param name="response">HTTP response</param>
public JsxHandler(
IReactEnvironment environment,
IFileSystem fileSystem,
HttpRequestBase request,
HttpResponseBase response
)
{
_environment = environment;
_fileSystem = fileSystem;
_request = request;
_response = response;
}
/// <summary>
/// Executes the handler. Outputs JavaScript to the response.
/// </summary>
public void Execute()
{
if (_request.QueryString["map"] != null)
{
RenderSourceMap();
}
else
{
RenderJsxFile();
}
}
/// <summary>
/// Renders the JSX file converted to regular JavaScript.
/// </summary>
private void RenderJsxFile()
{
var relativePath = _request.Url.LocalPath;
var result = _environment.JsxTransformer.TransformJsxFileWithSourceMap(relativePath);
var sourceMapUri = GetSourceMapUri(relativePath, result.Hash);
ConfigureCaching();
_response.ContentType = "text/javascript";
// The sourcemap spec says to use SourceMap, but Firefox only accepts X-SourceMap
_response.AddHeader("SourceMap", sourceMapUri);
_response.AddHeader("X-SourceMap", sourceMapUri);
_response.Write(result.Code);
}
/// <summary>
/// Renders the source map for this JSX file.
/// </summary>
private void RenderSourceMap()
{
var relativePath = _request.Url.LocalPath;
var result = _environment.JsxTransformer.TransformJsxFileWithSourceMap(relativePath, forceGenerateSourceMap: true);
if (result.SourceMap == null)
{
_response.StatusCode = 500;
_response.StatusDescription = "Unable to generate source map";
return;
}
var sourceMap = result.SourceMap.ToJson();
ConfigureCaching();
_response.ContentType = "application/json";
//_response.Write(")]}\n"); // Recommended by the spec but Firefox doesn't support it
_response.Write(sourceMap);
}
/// <summary>
/// Send headers to cache the response. Only caches on the server-side for now
/// </summary>
private void ConfigureCaching()
{
_response.AddFileDependency(_fileSystem.MapPath(_request.Url.LocalPath));
_response.Cache.SetCacheability(HttpCacheability.Server);
_response.Cache.SetLastModifiedFromFileDependencies();
_response.Cache.SetETagFromFileDependencies();
}
/// <summary>
/// Gets the URI to the source map of the specified file
/// </summary>
/// <param name="relativePath">Relative path to the JavaScript file</param>
/// <param name="hash">Hash of the file</param>
/// <returns>URI to the file</returns>
private static string GetSourceMapUri(string relativePath, string hash)
{
return string.Format("{0}?map={1}", relativePath, hash);
}
}
}