identifier.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Ivan Kopeykin @vankop
  4. */
  5. "use strict";
  6. const memorize = require("./memoize");
  7. const getUrl = memorize(() => require("url"));
  8. const PATH_QUERY_FRAGMENT_REGEXP =
  9. /^(#?(?:\0.|[^?#\0])*)(\?(?:\0.|[^#\0])*)?(#.*)?$/;
  10. const ZERO_ESCAPE_REGEXP = /\0(.)/g;
  11. const FILE_REG_EXP = /file:/i;
  12. /**
  13. * Index past a DOS device path prefix (`\\?\…` or `\\.\…`), or 0. Kept
  14. * out of `parseIdentifier` on purpose: inlining it back bloats the caller
  15. * beyond the size where V8's interpreter and JIT both handle it well
  16. * (the cause of the description-files-multi CodSpeed regression).
  17. * @param {string} identifier identifier known to start with `\`
  18. * @returns {number} 4 if identifier starts with a DOS device prefix, else 0
  19. */
  20. function dosPrefixEnd(identifier) {
  21. if (
  22. identifier.length >= 4 &&
  23. identifier.charCodeAt(1) === 92 &&
  24. identifier.charCodeAt(3) === 92
  25. ) {
  26. const c2 = identifier.charCodeAt(2);
  27. if (c2 === 63 || c2 === 46) return 4;
  28. }
  29. return 0;
  30. }
  31. /**
  32. * @param {string} identifier identifier
  33. * @returns {[string, string, string] | null} parsed identifier
  34. */
  35. function parseIdentifier(identifier) {
  36. if (!identifier) {
  37. return null;
  38. }
  39. if (FILE_REG_EXP.test(identifier)) {
  40. identifier = getUrl().fileURLToPath(identifier);
  41. }
  42. const firstEscape = identifier.indexOf("\0");
  43. // Handle `\0`
  44. if (firstEscape !== -1) {
  45. const match = PATH_QUERY_FRAGMENT_REGEXP.exec(identifier);
  46. if (!match) return null;
  47. return [
  48. match[1].replace(ZERO_ESCAPE_REGEXP, "$1"),
  49. match[2] ? match[2].replace(ZERO_ESCAPE_REGEXP, "$1") : "",
  50. match[3] || "",
  51. ];
  52. }
  53. // Fast path for inputs that don't use \0 escaping. DOS device paths
  54. // (`\\?\…`, `\\.\…`) embed a literal `?` / `.` that must not be read
  55. // as a query separator; skip past the prefix when the input actually
  56. // starts with `\`. Gate is a single char-code compare so this function
  57. // stays inside V8's inline budget for its hot callers (resolver parse).
  58. const scanStart =
  59. identifier.charCodeAt(0) === 92 ? dosPrefixEnd(identifier) : 0;
  60. const queryStart = identifier.indexOf("?", scanStart);
  61. // Start at index 1 (or past a DOS prefix) to ignore a possible leading hash.
  62. const fragmentStart = identifier.indexOf("#", scanStart || 1);
  63. if (fragmentStart < 0) {
  64. if (queryStart < 0) {
  65. // No fragment, no query
  66. return [identifier, "", ""];
  67. }
  68. // Query, no fragment
  69. return [identifier.slice(0, queryStart), identifier.slice(queryStart), ""];
  70. }
  71. if (queryStart < 0 || fragmentStart < queryStart) {
  72. // Fragment, no query
  73. return [
  74. identifier.slice(0, fragmentStart),
  75. "",
  76. identifier.slice(fragmentStart),
  77. ];
  78. }
  79. // Query and fragment
  80. return [
  81. identifier.slice(0, queryStart),
  82. identifier.slice(queryStart, fragmentStart),
  83. identifier.slice(fragmentStart),
  84. ];
  85. }
  86. module.exports.parseIdentifier = parseIdentifier;