getInnerRequest.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("./Resolver")} Resolver */
  7. /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
  8. /**
  9. * @param {Resolver} resolver resolver
  10. * @param {ResolveRequest} request string
  11. * @returns {string} inner request
  12. */
  13. module.exports = function getInnerRequest(resolver, request) {
  14. if (
  15. typeof request.__innerRequest === "string" &&
  16. request.__innerRequest_request === request.request &&
  17. request.__innerRequest_relativePath === request.relativePath
  18. )
  19. return request.__innerRequest;
  20. /** @type {string|undefined} */
  21. let innerRequest;
  22. if (request.request) {
  23. innerRequest = request.request;
  24. if (/^\.\.?(?:\/|$)/.test(innerRequest) && request.relativePath) {
  25. innerRequest = resolver.join(request.relativePath, innerRequest);
  26. }
  27. } else {
  28. innerRequest = request.relativePath;
  29. }
  30. request.__innerRequest_request = request.request;
  31. request.__innerRequest_relativePath = request.relativePath;
  32. return (request.__innerRequest = /** @type {string} */ (innerRequest));
  33. };