-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleLoader.cs
More file actions
180 lines (153 loc) · 6.76 KB
/
ModuleLoader.cs
File metadata and controls
180 lines (153 loc) · 6.76 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
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
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
using System.Linq;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
namespace ModuleLoader
{
public class ModuleLoader
{
private readonly RequestDelegate _next;
private readonly ILogger<ModuleLoader> _logger;
public ModuleLoader(RequestDelegate next, ILoggerFactory logger)
{
_next = next;
_logger = logger.CreateLogger<ModuleLoader>();
}
public async Task Invoke(HttpContext context)
{
await _next(context);
_logger.LogInformation("Requested by ModuleLoader middleware: {0}", context.Request.Path);
System.Console.WriteLine();
}
}
public static partial class MiddleWareExtentions
{
public static IApplicationBuilder UseModuleLoader(this IApplicationBuilder builder)
{
return builder.Map("/module", HandleModuleLoad)
.Map("/fonts", HandleFontLoad);
}
private static void HandleFontLoad(IApplicationBuilder app)
{
var fontDirs = new string[] { "wwwroot/styles", "node_modules/font-awesome/fonts" };
var mappedFonts = new Dictionary<string, string>();
foreach (var dir in fontDirs)
{
foreach (var item in Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories))
{
mappedFonts.Add(Path.GetFileName(item), item.Replace("\\", "/"));
}
}
app.Run(async context =>
{
var fileName = Path.GetFileName(context.Request.Path);
if (mappedFonts.ContainsKey(fileName))
{
await context.Response.SendFileAsync(mappedFonts[fileName]);
}
});
}
private static void HandleModuleLoad(IApplicationBuilder app)
{
var packagesMap = app.ApplicationServices.GetRequiredService<PackageMap>();
var logger = app.ApplicationServices.GetRequiredService<ILoggerFactory>().CreateLogger("RAModuleLoader");
app.Run(async context =>
{
var expectedPackage = context.Request.Path.Value.Replace("/", string.Empty).Replace(".js", string.Empty);
logger.LogInformation("RAModuleLoader. Request module: {0}", expectedPackage);
if (packagesMap.ContainsKey(expectedPackage))
{
string package = string.Empty;
if (packagesMap.TryGetValue(expectedPackage, out package))
{
logger.LogInformation("RAModuleLoader. Found package: {0}", package);
var file = File.ReadAllText(package);
if (!file.StartsWith("define"))
{
file = "define(function (require, exports, module) {\n" + file + "\n});";
await GenerateStreamFromString(file).CopyToAsync(context.Response.Body);
return;
}
else
{
await context.Response.SendFileAsync(package);
return;
}
}
logger.LogInformation("RAModuleLoader. Package not found: {0}", expectedPackage);
}
else
{
var fileName = $"wwwroot{context.Request.Path}";
logger.LogInformation("RAModuleLoader. Looking for: {0}", fileName);
await context.Response.SendFileAsync(fileName);
}
});
}
public static Stream GenerateStreamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
private static Task<Stream> WrapPackage(string packageName)
{
var script = File.ReadAllText(packageName);
if (!script.StartsWith("define"))
{
script = "define(function (require, exports, module) {" + script + "});";
}
return Task.FromResult(GenerateStreamFromString(script));
}
public static IServiceCollection AddModuleLoader(this IServiceCollection services)
{
var currentDirectory = Directory.GetCurrentDirectory();
var nodeModulesDir = Path.Combine(currentDirectory, "node_modules");
var wwwroot = Path.Combine(currentDirectory, "wwwroot");
var fileProvider = new PhysicalFileProvider(nodeModulesDir);
var packagesMap = new PackageMap();
var confBuilder = new ConfigurationBuilder();
foreach (var item in Directory.GetDirectories(nodeModulesDir).Where(x => !x.Contains(".bin") && !x.Contains("@types")))
{
var packageName = item.Split(Path.DirectorySeparatorChar).Last();
var mainFileLocation = JObject.Parse(File.ReadAllText(Path.Combine(nodeModulesDir, packageName, "package.json")))["main"];
if (packageName.Contains("vue"))
{
mainFileLocation = mainFileLocation.Value<string>().Replace(".common", "").Replace(".runtime", "");
}
if (packageName == "got")
{
mainFileLocation = Path.Combine("wwwroot", "got.js");
}
if (mainFileLocation == null)
{
mainFileLocation = Path.Combine(item, "index.js");
if (!File.Exists(mainFileLocation.Value<string>())) continue;
}
if (mainFileLocation.Value<string>().StartsWith("./"))
mainFileLocation = mainFileLocation.Value<string>().Replace("./", string.Empty);
var mainFile = Path.Combine("node_modules", packageName, mainFileLocation.Value<string>()).Replace('\\', Path.AltDirectorySeparatorChar);
if (!Path.HasExtension(mainFile))
{
mainFile += ".js";
}
if (File.Exists(mainFile))
{
packagesMap.Add(packageName, mainFile);
}
}
services.AddSingleton<PackageMap>(packagesMap);
return services;
}
}
}