-
Notifications
You must be signed in to change notification settings - Fork 927
/
Copy pathBabelFileSystem.cs
207 lines (183 loc) · 5.89 KB
/
BabelFileSystem.cs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright (c) 2014-Present, 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;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
#if OWIN
using Microsoft.Owin.FileSystems;
using IOwinFileSystem = Microsoft.Owin.FileSystems.IFileSystem;
#else
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
using IOwinFileSystem = Microsoft.Extensions.FileProviders.IFileProvider;
using PhysicalFileSystem = Microsoft.Extensions.FileProviders.PhysicalFileProvider;
#endif
#if OWIN
namespace React.Owin
#else
namespace React.AspNet
#endif
{
/// <summary>
/// File system that serves transformed JavaScript files.
/// </summary>
public class BabelFileSystem : IOwinFileSystem
{
private readonly IBabel _transformer;
private readonly IOwinFileSystem _physicalFileSystem;
private readonly string[] _extensions;
/// <summary>
/// Creates a new instance of the BabelFileSystem.
/// </summary>
/// <param name="transformer">Babel transformer used to compile files</param>
/// <param name="root">The root directory</param>
/// <param name="extensions">Extensions of files that will be treated as JavaScript files</param>
public BabelFileSystem(IBabel transformer, string root, IEnumerable<string> extensions)
: this(transformer, new PhysicalFileSystem(root), extensions)
{
}
/// <summary>
/// Creates a new instance of the BabelFileSystem.
/// </summary>
/// <param name="transformer">Babel transformer used to compile files</param>
/// <param name="fileSystem">File system used to look up files</param>
/// <param name="extensions">Extensions of files that will be treated as JavaScript files</param>
public BabelFileSystem(IBabel transformer, IOwinFileSystem fileSystem, IEnumerable<string> extensions)
{
_transformer = transformer;
_physicalFileSystem = fileSystem;
if (extensions != null)
{
// Make sure the extensions start with dot
_extensions = extensions.Select(extension => extension.StartsWith(".") ? extension : "." + extension).ToArray();
}
}
#if OWIN
/// <summary>
/// Locate a JavaScript file at the given path.
/// </summary>
/// <param name="subpath">The path that identifies the file</param>
/// <param name="fileInfo">The discovered file if any</param>
/// <returns>
/// True if a JavaScript file was located at the given path
/// </returns>
public bool TryGetFileInfo(string subpath, out IFileInfo fileInfo)
{
IFileInfo internalFileInfo;
fileInfo = null;
if (!_physicalFileSystem.TryGetFileInfo(subpath, out internalFileInfo))
return false;
if (_extensions != null && !_extensions.Any(internalFileInfo.Name.EndsWith))
return false;
if (internalFileInfo.IsDirectory)
return false;
fileInfo = new BabelFileInfo(_transformer, internalFileInfo);
return true;
}
/// <summary>
/// Enumerate a directory at the given path, if any
/// </summary>
/// <param name="subpath">The path that identifies the directory</param>
/// <param name="contents">The contents if any</param>
/// <returns>
/// True if a directory was located at the given path
/// </returns>
public bool TryGetDirectoryContents(string subpath, out IEnumerable<IFileInfo> contents)
{
return _physicalFileSystem.TryGetDirectoryContents(subpath, out contents);
}
#else
/// <summary>
/// Locate a file at the given path.
/// </summary>
/// <param name="subpath">The path that identifies the file</param>
/// <returns>The discovered file if any</returns>
public IFileInfo GetFileInfo(string subpath)
{
var internalFileInfo = _physicalFileSystem.GetFileInfo(subpath);
return new BabelFileInfo(_transformer, internalFileInfo);
}
/// <summary>
/// Enumerate a directory at the given path, if any
/// </summary>
/// <param name="subpath">The path that identifies the directory</param>
/// <returns>The contents if any</returns>
public IDirectoryContents GetDirectoryContents(string subpath)
{
return _physicalFileSystem.GetDirectoryContents(subpath);
}
/// <summary>
/// Creates a <see cref="T:Microsoft.Framework.Primitives.IChangeToken"/> for the
/// specified <paramref name="filter"/>.
/// </summary>
/// <param name="filter">
/// Filter string used to determine what files or folders to monitor.
/// Example: **/*.cs, *.*, subFolder/**/*.cshtml.</param>
/// <returns>
/// An <see cref="T:Microsoft.Framework.Primitives.IChangeToken"/> that is notified
/// when a file matching <paramref name="filter"/> is added, modified or deleted.
/// </returns>
public IChangeToken Watch(string filter)
{
return _physicalFileSystem.Watch(filter);
}
#endif
private class BabelFileInfo : IFileInfo
{
private readonly IBabel _babel;
private readonly IFileInfo _fileInfo;
private readonly Lazy<byte[]> _content;
public BabelFileInfo(IBabel babel, IFileInfo fileInfo)
{
_babel = babel;
_fileInfo = fileInfo;
_content = new Lazy<byte[]>(
() => Encoding.UTF8.GetBytes(_babel.TransformFile(fileInfo.PhysicalPath))
);
}
public Stream CreateReadStream()
{
return new MemoryStream(_content.Value);
}
public long Length
{
get { return _content.Value.Length; }
}
public string PhysicalPath
{
get { return null; }
}
public string Name
{
get { return _fileInfo.Name; }
}
public bool IsDirectory
{
get { return _fileInfo.IsDirectory; }
}
#if OWIN
public DateTime LastModified
{
get { return _fileInfo.LastModified; }
}
#else
public DateTimeOffset LastModified
{
get { return _fileInfo.LastModified; }
}
public bool Exists
{
get { return _fileInfo.Exists; }
}
#endif
}
}
}