2
0

DefaultModuleLoader.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. namespace Jint.Runtime.Modules;
  2. public class DefaultModuleLoader : ModuleLoader
  3. {
  4. private readonly Uri _basePath;
  5. private readonly bool _restrictToBasePath;
  6. public DefaultModuleLoader(string basePath, bool restrictToBasePath = true)
  7. {
  8. if (string.IsNullOrWhiteSpace(basePath))
  9. {
  10. Throw.ArgumentException("Value cannot be null or whitespace.", nameof(basePath));
  11. }
  12. _restrictToBasePath = restrictToBasePath;
  13. if (!Uri.TryCreate(basePath, UriKind.Absolute, out var temp))
  14. {
  15. if (!Path.IsPathRooted(basePath))
  16. {
  17. Throw.ArgumentException("Path must be rooted", nameof(basePath));
  18. }
  19. basePath = Path.GetFullPath(basePath);
  20. _basePath = new Uri(basePath, UriKind.Absolute);
  21. }
  22. else
  23. {
  24. _basePath = temp;
  25. }
  26. if (_basePath.AbsolutePath[^1] != '/')
  27. {
  28. var uriBuilder = new UriBuilder(_basePath);
  29. uriBuilder.Path += '/';
  30. _basePath = uriBuilder.Uri;
  31. }
  32. }
  33. public override ResolvedSpecifier Resolve(string? referencingModuleLocation, ModuleRequest moduleRequest)
  34. {
  35. var specifier = moduleRequest.Specifier;
  36. if (string.IsNullOrEmpty(specifier))
  37. {
  38. Throw.ModuleResolutionException("Invalid Module Specifier", specifier, referencingModuleLocation);
  39. return default;
  40. }
  41. // Specifications from ESM_RESOLVE Algorithm: https://nodejs.org/api/esm.html#resolution-algorithm
  42. Uri resolved;
  43. if (Uri.TryCreate(specifier, UriKind.Absolute, out var uri))
  44. {
  45. resolved = uri;
  46. }
  47. else if (IsRelative(specifier))
  48. {
  49. var baseUri = BuildBaseUri(referencingModuleLocation);
  50. resolved = new Uri(baseUri, specifier);
  51. }
  52. else if (specifier[0] == '#')
  53. {
  54. Throw.NotSupportedException($"PACKAGE_IMPORTS_RESOLVE is not supported: '{specifier}'");
  55. return default;
  56. }
  57. else
  58. {
  59. return new ResolvedSpecifier(
  60. moduleRequest,
  61. specifier,
  62. Uri: null,
  63. SpecifierType.Bare
  64. );
  65. }
  66. if (resolved.IsFile)
  67. {
  68. if (resolved.UserEscaped)
  69. {
  70. Throw.ModuleResolutionException("Invalid Module Specifier", specifier, referencingModuleLocation);
  71. return default;
  72. }
  73. if (!Path.HasExtension(resolved.LocalPath))
  74. {
  75. Throw.ModuleResolutionException("Unsupported Directory Import", specifier, referencingModuleLocation);
  76. return default;
  77. }
  78. }
  79. if (_restrictToBasePath && !_basePath.IsBaseOf(resolved))
  80. {
  81. Throw.ModuleResolutionException($"Unauthorized Module Path", specifier, referencingModuleLocation);
  82. return default;
  83. }
  84. return new ResolvedSpecifier(
  85. moduleRequest,
  86. resolved.AbsoluteUri,
  87. resolved,
  88. SpecifierType.RelativeOrAbsolute
  89. );
  90. }
  91. private Uri BuildBaseUri(string? referencingModuleLocation)
  92. {
  93. if (referencingModuleLocation is not null)
  94. {
  95. /*
  96. "referencingModuleLocation" might be relative or an invalid URI when a module imports other
  97. modules and the importing module is called directly from .NET code.
  98. e.g. "engine.Modules.Import("my-module")" and "my-module" imports other modules.
  99. Path traversal prevention is not a concern here because it is checked later
  100. (if _restrictToBasePath is set to true).
  101. */
  102. if (Uri.TryCreate(referencingModuleLocation, UriKind.Absolute, out var referencingLocation) ||
  103. Uri.TryCreate(_basePath, referencingModuleLocation, out referencingLocation))
  104. return referencingLocation;
  105. }
  106. return _basePath;
  107. }
  108. protected override string LoadModuleContents(Engine engine, ResolvedSpecifier resolved)
  109. {
  110. var specifier = resolved.ModuleRequest.Specifier;
  111. if (resolved.Type != SpecifierType.RelativeOrAbsolute)
  112. {
  113. Throw.NotSupportedException($"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}'.");
  114. }
  115. if (resolved.Uri == null)
  116. {
  117. Throw.InvalidOperationException($"Module '{specifier}' of type '{resolved.Type}' has no resolved URI.");
  118. }
  119. var fileName = Uri.UnescapeDataString(resolved.Uri.AbsolutePath);
  120. if (!File.Exists(fileName))
  121. {
  122. Throw.ModuleResolutionException("Module Not Found", specifier, parent: null, fileName);
  123. }
  124. return File.ReadAllText(fileName);
  125. }
  126. private static bool IsRelative(string specifier)
  127. {
  128. return specifier.StartsWith('.') || specifier.StartsWith('/');
  129. }
  130. }