identifier.js 2.8 KB

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