-
-
Notifications
You must be signed in to change notification settings - Fork 567
/
Copy pathDefaultModuleLoader.cs
159 lines (138 loc) · 5.51 KB
/
DefaultModuleLoader.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
namespace Jint.Runtime.Modules;
public class DefaultModuleLoader : ModuleLoader
{
private readonly Uri _basePath;
private readonly bool _restrictToBasePath;
public DefaultModuleLoader(string basePath, bool restrictToBasePath = true)
{
if (string.IsNullOrWhiteSpace(basePath))
{
ExceptionHelper.ThrowArgumentException("Value cannot be null or whitespace.", nameof(basePath));
}
_restrictToBasePath = restrictToBasePath;
if (!Uri.TryCreate(basePath, UriKind.Absolute, out var temp))
{
if (!Path.IsPathRooted(basePath))
{
ExceptionHelper.ThrowArgumentException("Path must be rooted", nameof(basePath));
}
basePath = Path.GetFullPath(basePath);
_basePath = new Uri(basePath, UriKind.Absolute);
}
else
{
_basePath = temp;
}
if (_basePath.AbsolutePath[^1] != '/')
{
var uriBuilder = new UriBuilder(_basePath);
uriBuilder.Path += '/';
_basePath = uriBuilder.Uri;
}
}
public override ResolvedSpecifier Resolve(string? referencingModuleLocation, ModuleRequest moduleRequest)
{
var specifier = moduleRequest.Specifier;
if (string.IsNullOrEmpty(specifier))
{
ExceptionHelper.ThrowModuleResolutionException("Invalid Module Specifier", specifier, referencingModuleLocation);
return default;
}
// Specifications from ESM_RESOLVE Algorithm: https://nodejs.org/api/esm.html#resolution-algorithm
Uri resolved;
if (Uri.TryCreate(specifier, UriKind.Absolute, out var uri))
{
resolved = uri;
}
else if (IsRelative(specifier))
{
var baseUri = BuildBaseUri(referencingModuleLocation);
resolved = new Uri(baseUri, specifier);
}
else if (specifier[0] == '#')
{
ExceptionHelper.ThrowNotSupportedException($"PACKAGE_IMPORTS_RESOLVE is not supported: '{specifier}'");
return default;
}
else
{
return new ResolvedSpecifier(
moduleRequest,
specifier,
Uri: null,
SpecifierType.Bare
);
}
if (resolved.IsFile)
{
if (resolved.UserEscaped)
{
ExceptionHelper.ThrowModuleResolutionException("Invalid Module Specifier", specifier, referencingModuleLocation);
return default;
}
if (!Path.HasExtension(resolved.LocalPath))
{
ExceptionHelper.ThrowModuleResolutionException("Unsupported Directory Import", specifier, referencingModuleLocation);
return default;
}
}
if (_restrictToBasePath && !_basePath.IsBaseOf(resolved))
{
ExceptionHelper.ThrowModuleResolutionException($"Unauthorized Module Path", specifier, referencingModuleLocation);
return default;
}
return new ResolvedSpecifier(
moduleRequest,
resolved.AbsoluteUri,
resolved,
SpecifierType.RelativeOrAbsolute
);
}
private Uri BuildBaseUri(string? referencingModuleLocation)
{
if (referencingModuleLocation is not null)
{
/*
"referencingModuleLocation" might be relative or an invalid URI when a module imports other
modules and the importing module is called directly from .NET code.
e.g. "engine.Modules.Import("my-module")" and "my-module" imports other modules.
Path traversal prevention is not a concern here because it is checked later
(if _restrictToBasePath is set to true).
*/
if (Uri.TryCreate(referencingModuleLocation, UriKind.Absolute, out var referencingLocation) ||
Uri.TryCreate(_basePath, referencingModuleLocation, out referencingLocation))
{
if (!Path.HasExtension(referencingLocation.LocalPath) && referencingLocation.LocalPath[^1] != '/')
{
var uriBuilder = new UriBuilder(referencingLocation);
uriBuilder.Path += '/';
return uriBuilder.Uri;
}
return referencingLocation;
}
}
return _basePath;
}
protected override string LoadModuleContents(Engine engine, ResolvedSpecifier resolved)
{
var specifier = resolved.ModuleRequest.Specifier;
if (resolved.Type != SpecifierType.RelativeOrAbsolute)
{
ExceptionHelper.ThrowNotSupportedException($"The default module loader can only resolve files. You can define modules directly to allow imports using {nameof(Engine)}.{nameof(Engine.Modules.Add)}(). Attempted to resolve: '{specifier}'.");
}
if (resolved.Uri == null)
{
ExceptionHelper.ThrowInvalidOperationException($"Module '{specifier}' of type '{resolved.Type}' has no resolved URI.");
}
var fileName = Uri.UnescapeDataString(resolved.Uri.AbsolutePath);
if (!File.Exists(fileName))
{
ExceptionHelper.ThrowModuleResolutionException("Module Not Found", specifier, parent: null, fileName);
}
return File.ReadAllText(fileName);
}
private static bool IsRelative(string specifier)
{
return specifier.StartsWith('.') || specifier.StartsWith('/');
}
}