DefaultModuleLoader.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. ExceptionHelper.ThrowArgumentException("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. ExceptionHelper.ThrowArgumentException("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. ExceptionHelper.ThrowModuleResolutionException("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. resolved = new Uri(referencingModuleLocation != null ? new Uri(referencingModuleLocation, UriKind.Absolute) : _basePath, specifier);
  50. }
  51. else if (specifier[0] == '#')
  52. {
  53. ExceptionHelper.ThrowNotSupportedException($"PACKAGE_IMPORTS_RESOLVE is not supported: '{specifier}'");
  54. return default;
  55. }
  56. else
  57. {
  58. return new ResolvedSpecifier(
  59. moduleRequest,
  60. specifier,
  61. Uri: null,
  62. SpecifierType.Bare
  63. );
  64. }
  65. if (resolved.IsFile)
  66. {
  67. if (resolved.UserEscaped)
  68. {
  69. ExceptionHelper.ThrowModuleResolutionException("Invalid Module Specifier", specifier, referencingModuleLocation);
  70. return default;
  71. }
  72. if (!Path.HasExtension(resolved.LocalPath))
  73. {
  74. ExceptionHelper.ThrowModuleResolutionException("Unsupported Directory Import", specifier, referencingModuleLocation);
  75. return default;
  76. }
  77. }
  78. if (_restrictToBasePath && !_basePath.IsBaseOf(resolved))
  79. {
  80. ExceptionHelper.ThrowModuleResolutionException($"Unauthorized Module Path", specifier, referencingModuleLocation);
  81. return default;
  82. }
  83. return new ResolvedSpecifier(
  84. moduleRequest,
  85. resolved.AbsoluteUri,
  86. resolved,
  87. SpecifierType.RelativeOrAbsolute
  88. );
  89. }
  90. protected override string LoadModuleContents(Engine engine, ResolvedSpecifier resolved)
  91. {
  92. var specifier = resolved.ModuleRequest.Specifier;
  93. if (resolved.Type != SpecifierType.RelativeOrAbsolute)
  94. {
  95. 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}'.");
  96. }
  97. if (resolved.Uri == null)
  98. {
  99. ExceptionHelper.ThrowInvalidOperationException($"Module '{specifier}' of type '{resolved.Type}' has no resolved URI.");
  100. }
  101. var fileName = Uri.UnescapeDataString(resolved.Uri.AbsolutePath);
  102. if (!File.Exists(fileName))
  103. {
  104. ExceptionHelper.ThrowModuleResolutionException("Module Not Found", specifier, parent: null, fileName);
  105. }
  106. return File.ReadAllText(fileName);
  107. }
  108. private static bool IsRelative(string specifier)
  109. {
  110. return specifier.StartsWith('.') || specifier.StartsWith('/');
  111. }
  112. }