getSchemeFromUrl.cjs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. /**
  5. * Get unit from value node
  6. *
  7. * Returns `null` if the unit is not found.
  8. *
  9. * @param {string} urlString
  10. * @returns {string | null}
  11. */
  12. function getSchemeFromUrl(urlString) {
  13. let protocol = null;
  14. try {
  15. protocol = new URL(urlString).protocol;
  16. } catch {
  17. return null;
  18. }
  19. if (protocol === null || typeof protocol === 'undefined') {
  20. return null;
  21. }
  22. const scheme = protocol.slice(0, -1); // strip trailing `:`
  23. // The URL spec does not require a scheme to be followed by `//`, but checking
  24. // for it allows this rule to differentiate <scheme>:<hostname> urls from
  25. // <hostname>:<port> urls. `data:` scheme urls are an exception to this rule.
  26. const slashIndex = protocol.length;
  27. const expectedSlashes = urlString.slice(slashIndex, slashIndex + 2);
  28. const isSchemeLessUrl = expectedSlashes !== '//' && scheme !== 'data';
  29. if (isSchemeLessUrl) {
  30. return null;
  31. }
  32. return scheme;
  33. }
  34. module.exports = getSchemeFromUrl;