match-remote-pattern.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.matchRemotePattern = matchRemotePattern;
  6. exports.hasMatch = hasMatch;
  7. var _micromatch = require("next/dist/compiled/micromatch");
  8. function matchRemotePattern(pattern, url) {
  9. if (pattern.protocol !== undefined) {
  10. const actualProto = url.protocol.slice(0, -1);
  11. if (pattern.protocol !== actualProto) {
  12. return false;
  13. }
  14. }
  15. if (pattern.port !== undefined) {
  16. if (pattern.port !== url.port) {
  17. return false;
  18. }
  19. }
  20. if (pattern.hostname === undefined) {
  21. throw new Error(`Pattern should define hostname but found\n${JSON.stringify(pattern)}`);
  22. } else {
  23. if (!(0, _micromatch).makeRe(pattern.hostname).test(url.hostname)) {
  24. return false;
  25. }
  26. }
  27. var _pathname;
  28. if (!(0, _micromatch).makeRe((_pathname = pattern.pathname) != null ? _pathname : '**').test(url.pathname)) {
  29. return false;
  30. }
  31. return true;
  32. }
  33. function hasMatch(domains, remotePatterns, url) {
  34. return domains.some((domain)=>url.hostname === domain) || remotePatterns.some((p)=>matchRemotePattern(p, url));
  35. }
  36. //# sourceMappingURL=match-remote-pattern.js.map