URLAbsoluteSpecifier.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. /** @typedef {import("./fs").InputFileSystem} InputFileSystem */
  7. /** @typedef {(error: Error|null, result?: Buffer) => void} ErrorFirstCallback */
  8. const backSlashCharCode = "\\".charCodeAt(0);
  9. const slashCharCode = "/".charCodeAt(0);
  10. const aLowerCaseCharCode = "a".charCodeAt(0);
  11. const zLowerCaseCharCode = "z".charCodeAt(0);
  12. const aUpperCaseCharCode = "A".charCodeAt(0);
  13. const zUpperCaseCharCode = "Z".charCodeAt(0);
  14. const _0CharCode = "0".charCodeAt(0);
  15. const _9CharCode = "9".charCodeAt(0);
  16. const plusCharCode = "+".charCodeAt(0);
  17. const hyphenCharCode = "-".charCodeAt(0);
  18. const colonCharCode = ":".charCodeAt(0);
  19. const hashCharCode = "#".charCodeAt(0);
  20. const queryCharCode = "?".charCodeAt(0);
  21. /**
  22. * Get scheme if specifier is an absolute URL specifier
  23. * e.g. Absolute specifiers like 'file:///user/webpack/index.js'
  24. * https://tools.ietf.org/html/rfc3986#section-3.1
  25. * @param {string} specifier specifier
  26. * @returns {string|undefined} scheme if absolute URL specifier provided
  27. */
  28. function getScheme(specifier) {
  29. const start = specifier.charCodeAt(0);
  30. // First char maybe only a letter
  31. if (
  32. (start < aLowerCaseCharCode || start > zLowerCaseCharCode) &&
  33. (start < aUpperCaseCharCode || start > zUpperCaseCharCode)
  34. ) {
  35. return undefined;
  36. }
  37. let i = 1;
  38. let ch = specifier.charCodeAt(i);
  39. while (
  40. (ch >= aLowerCaseCharCode && ch <= zLowerCaseCharCode) ||
  41. (ch >= aUpperCaseCharCode && ch <= zUpperCaseCharCode) ||
  42. (ch >= _0CharCode && ch <= _9CharCode) ||
  43. ch === plusCharCode ||
  44. ch === hyphenCharCode
  45. ) {
  46. if (++i === specifier.length) return undefined;
  47. ch = specifier.charCodeAt(i);
  48. }
  49. // Scheme must end with colon
  50. if (ch !== colonCharCode) return undefined;
  51. // Check for Windows absolute path
  52. // https://url.spec.whatwg.org/#url-miscellaneous
  53. if (i === 1) {
  54. const nextChar = i + 1 < specifier.length ? specifier.charCodeAt(i + 1) : 0;
  55. if (
  56. nextChar === 0 ||
  57. nextChar === backSlashCharCode ||
  58. nextChar === slashCharCode ||
  59. nextChar === hashCharCode ||
  60. nextChar === queryCharCode
  61. ) {
  62. return undefined;
  63. }
  64. }
  65. return specifier.slice(0, i).toLowerCase();
  66. }
  67. /**
  68. * @param {string} specifier specifier
  69. * @returns {string | null | undefined} protocol if absolute URL specifier provided
  70. */
  71. function getProtocol(specifier) {
  72. const scheme = getScheme(specifier);
  73. return scheme === undefined ? undefined : scheme + ":";
  74. }
  75. exports.getScheme = getScheme;
  76. exports.getProtocol = getProtocol;