Skip to content

Commit 2c4b9ed

Browse files
author
Marcin Drobik
committed
Further Code Review fixes: strong signing, moved MemoryFileCache, documentation
1 parent f25f4a8 commit 2c4b9ed

File tree

7 files changed

+108
-65
lines changed

7 files changed

+108
-65
lines changed

src/React.Owin/JsxFileMiddleware.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ static JsxFileMiddleware()
2828
Initializer.Initialize(_ => _);
2929
}
3030

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>
3136
public JsxFileMiddleware(Func<IDictionary<string, object>, Task> next, JsxFileOptions options)
3237
{
3338
if (next == null)
3439
throw new ArgumentNullException("next");
3540

3641
// Default values
3742
options = options ?? new JsxFileOptions();
38-
var extenstions = (options.Extensions == null || !options.Extensions.Any()) ? new[] { ".jsx" } : options.Extensions;
43+
var extensions = (options.Extensions == null || !options.Extensions.Any()) ? new[] { ".jsx", ".js" } : options.Extensions;
3944
var fileOptions = options.StaticFileOptions ?? new StaticFileOptions();
4045

4146
// Wrap the file system with JSX file system
@@ -49,10 +54,15 @@ public JsxFileMiddleware(Func<IDictionary<string, object>, Task> next, JsxFileOp
4954
OnPrepareResponse = fileOptions.OnPrepareResponse,
5055
RequestPath = fileOptions.RequestPath,
5156
ServeUnknownFileTypes = fileOptions.ServeUnknownFileTypes,
52-
FileSystem = new JsxFileSystem(reactEnvironment.JsxTransformer, fileOptions.FileSystem, extenstions)
57+
FileSystem = new JsxFileSystem(reactEnvironment.JsxTransformer, fileOptions.FileSystem, extensions)
5358
});
5459
}
5560

61+
/// <summary>
62+
/// Processes a request to determine if it matches a known JSX file, and if so, serves it compiled to JavaScript.
63+
/// </summary>
64+
/// <param name="environment">OWIN environment dictionary which stores state information about the request, response and relevant server state.</param>
65+
/// <returns/>
5666
public Task Invoke(IDictionary<string, object> environment)
5767
{
5868
return _internalStaticMiddleware.Invoke(environment);

src/React.Owin/JsxFileOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace React.Owin
1919
public class JsxFileOptions
2020
{
2121
/// <summary>
22-
/// Collection of extensions that will be treated as JSX files. Defaults to ".jsx".
22+
/// Collection of extensions that will be treated as JSX files. Defaults to ".jsx" and ".js".
2323
/// </summary>
2424
public IEnumerable<string> Extensions { get; set; }
2525

src/React.Owin/JsxFileSystem.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ internal class JsxFileSystem : Microsoft.Owin.FileSystems.IFileSystem
2424
{
2525
private readonly IJsxTransformer _transformer;
2626
private readonly Microsoft.Owin.FileSystems.IFileSystem _physicalFileSystem;
27-
private readonly string[] _extenstions;
27+
private readonly string[] _extensions;
2828

29-
public JsxFileSystem(IJsxTransformer transformer, string root, IEnumerable<string> extenstions)
30-
: this(transformer, new PhysicalFileSystem(root), extenstions)
29+
public JsxFileSystem(IJsxTransformer transformer, string root, IEnumerable<string> extensions)
30+
: this(transformer, new PhysicalFileSystem(root), extensions)
3131
{
3232
}
3333

34-
public JsxFileSystem(IJsxTransformer transformer, Microsoft.Owin.FileSystems.IFileSystem fileSystem, IEnumerable<string> extenstions)
34+
public JsxFileSystem(IJsxTransformer transformer, Microsoft.Owin.FileSystems.IFileSystem fileSystem, IEnumerable<string> extensions)
3535
{
3636
_transformer = transformer;
3737
_physicalFileSystem = fileSystem;
3838

3939
// Make sure the extensions start with dot
40-
_extenstions = extenstions.Select(extenstion => extenstion.StartsWith(".") ? extenstion : "." + extenstion).ToArray();
40+
_extensions = extensions.Select(extension => extension.StartsWith(".") ? extension : "." + extension).ToArray();
4141
}
4242

4343
public bool TryGetFileInfo(string subpath, out IFileInfo fileInfo)
@@ -48,7 +48,7 @@ public bool TryGetFileInfo(string subpath, out IFileInfo fileInfo)
4848
if (!_physicalFileSystem.TryGetFileInfo(subpath, out internalFileInfo))
4949
return false;
5050

51-
if (internalFileInfo.IsDirectory || !_extenstions.Any(internalFileInfo.Name.EndsWith))
51+
if (internalFileInfo.IsDirectory || !_extensions.Any(internalFileInfo.Name.EndsWith))
5252
return false;
5353

5454
fileInfo = new JsxFileInfo(_transformer, internalFileInfo);

src/React.Owin/MemoryFileCache.cs

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/React.Owin/React.Owin.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<DebugSymbols>true</DebugSymbols>
1818
<DebugType>full</DebugType>
1919
<Optimize>false</Optimize>
20-
<OutputPath>bin\Debug\</OutputPath>
20+
<OutputPath>..\..\bin\Debug\React.Owin\</OutputPath>
2121
<DefineConstants>DEBUG;TRACE</DefineConstants>
2222
<ErrorReport>prompt</ErrorReport>
2323
<WarningLevel>4</WarningLevel>
@@ -27,7 +27,7 @@
2727
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2828
<DebugType>pdbonly</DebugType>
2929
<Optimize>true</Optimize>
30-
<OutputPath>bin\Release\</OutputPath>
30+
<OutputPath>..\..\bin\Release\React.Owin\</OutputPath>
3131
<DefineConstants>TRACE</DefineConstants>
3232
<ErrorReport>prompt</ErrorReport>
3333
<WarningLevel>4</WarningLevel>
@@ -39,6 +39,9 @@
3939
<PropertyGroup>
4040
<AssemblyOriginatorKeyFile>..\Key.snk</AssemblyOriginatorKeyFile>
4141
</PropertyGroup>
42+
<PropertyGroup>
43+
<SignAssembly>true</SignAssembly>
44+
</PropertyGroup>
4245
<ItemGroup>
4346
<Reference Include="Microsoft.Owin, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
4447
<SpecificVersion>False</SpecificVersion>
@@ -69,7 +72,6 @@
6972
</Compile>
7073
<Compile Include="AssemblyRegistration.cs" />
7174
<Compile Include="EntryAssemblyFileSystem.cs" />
72-
<Compile Include="MemoryFileCache.cs" />
7375
<Compile Include="JsxFileMiddleware.cs" />
7476
<Compile Include="JsxFileExtensions.cs" />
7577
<Compile Include="JsxFileOptions.cs" />

src/React/MemoryFileCache.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2014-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.Collections.Generic;
12+
using System.Linq;
13+
using System.Runtime.Caching;
14+
15+
namespace React
16+
{
17+
/// <summary>
18+
/// Memory cache implementation for React.ICache. Uses System.Runtime.Caching.
19+
/// </summary>
20+
public class MemoryFileCache : ICache
21+
{
22+
private readonly ObjectCache _cache;
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="MemoryFileCache"/> class.
26+
/// </summary>
27+
public MemoryFileCache()
28+
{
29+
_cache = MemoryCache.Default;
30+
}
31+
32+
/// <summary>
33+
/// Get an item from the cache. Returns <paramref name="fallback"/> if the item does
34+
/// not exist.
35+
/// </summary>
36+
/// <typeparam name="T">Type of data</typeparam>
37+
/// <param name="key">The cache key</param>
38+
/// <param name="fallback">Value to return if item is not in the cache</param>
39+
/// <returns>Data from cache, otherwise <paramref name="fallback"/></returns>
40+
public T Get<T>(string key, T fallback = default(T))
41+
{
42+
return (T)(_cache.Get(key) ?? fallback);
43+
}
44+
45+
/// <summary>
46+
/// Sets an item in the cache.
47+
/// </summary>
48+
/// <typeparam name="T">Type of data</typeparam>
49+
/// <param name="key">The cache key</param>
50+
/// <param name="data">Data to cache</param>
51+
/// <param name="slidingExpiration">
52+
/// Sliding expiration, if cache key is not accessed in this time period it will
53+
/// automatically be removed from the cache
54+
/// </param>
55+
/// <param name="cacheDependencyFiles">
56+
/// Filenames this cached item is dependent on. If any of these files change, the cache
57+
/// will be cleared automatically
58+
/// </param>
59+
/// <param name="cacheDependencyKeys">
60+
/// Other cache keys this cached item is dependent on. If any of these keys change, the
61+
/// cache will be cleared automatically
62+
/// </param>
63+
public void Set<T>(string key, T data, TimeSpan slidingExpiration, IEnumerable<string> cacheDependencyFiles = null, IEnumerable<string> cacheDependencyKeys = null)
64+
{
65+
if (data == null)
66+
{
67+
_cache.Remove(key);
68+
return;
69+
}
70+
71+
var policy = new CacheItemPolicy { SlidingExpiration = slidingExpiration };
72+
73+
if (cacheDependencyFiles != null && cacheDependencyFiles.Any())
74+
policy.ChangeMonitors.Add(new HostFileChangeMonitor(cacheDependencyFiles.ToList()));
75+
76+
if (cacheDependencyKeys != null && cacheDependencyKeys.Any())
77+
policy.ChangeMonitors.Add(_cache.CreateCacheEntryChangeMonitor(cacheDependencyKeys));
78+
79+
_cache.Set(key, data, policy);
80+
}
81+
}
82+
}

src/React/React.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<Reference Include="System.Configuration" />
7979
<Reference Include="System.Core" />
8080
<Reference Include="Microsoft.CSharp" />
81+
<Reference Include="System.Runtime.Caching" />
8182
</ItemGroup>
8283
<ItemGroup>
8384
<Compile Include="..\SharedAssemblyInfo.cs">
@@ -90,6 +91,7 @@
9091
<Compile Include="Exceptions\ReactScriptLoadException.cs" />
9192
<Compile Include="Exceptions\ReactServerRenderingException.cs" />
9293
<Compile Include="JavaScriptWithSourceMap.cs" />
94+
<Compile Include="MemoryFileCache.cs" />
9395
<Compile Include="SourceMap.cs" />
9496
<Compile Include="SystemEnvironmentUtils.cs" />
9597
<Compile Include="Exceptions\JsxUnsupportedEngineException.cs" />

0 commit comments

Comments
 (0)