fileURLToPath.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. // A runtime-agnostic port of Node's `url.fileURLToPath`, built on the global
  6. // `URL` (available in Node, browsers, Deno and Bun) so the resolver does not
  7. // depend on the Node `url` builtin. The platform branch can be forced via the
  8. // `windows` option; otherwise it follows the host platform like Node does.
  9. const CHAR_LOWERCASE_A = 97;
  10. const CHAR_LOWERCASE_Z = 122;
  11. const isWindows =
  12. typeof process !== "undefined" && process.platform === "win32";
  13. const forwardSlashRegEx = /\//g;
  14. /**
  15. * @param {URL} url file: URL instance
  16. * @returns {string} Windows filesystem path
  17. */
  18. function getPathFromURLWin32(url) {
  19. const { hostname } = url;
  20. let { pathname } = url;
  21. for (let n = 0; n < pathname.length; n++) {
  22. if (pathname[n] === "%") {
  23. const third = (pathname.codePointAt(n + 2) || 0) | 0x20;
  24. if (
  25. (pathname[n + 1] === "2" && third === 102) || // 2f 2F (/)
  26. (pathname[n + 1] === "5" && third === 99) // 5c 5C (\)
  27. ) {
  28. throw new TypeError(
  29. "File URL path must not include encoded \\ or / characters",
  30. );
  31. }
  32. }
  33. }
  34. pathname = pathname.replace(forwardSlashRegEx, "\\");
  35. pathname = decodeURIComponent(pathname);
  36. if (hostname !== "") {
  37. // UNC path: `\\host\share\…`. Node runs the host through
  38. // `domainToUnicode`; we keep the (already punycode) hostname as-is,
  39. // which is identical for ASCII hosts and only differs for rare IDN UNC.
  40. return `\\\\${hostname}${pathname}`;
  41. }
  42. const letter = (pathname.codePointAt(1) || 0) | 0x20;
  43. const sep = pathname.charAt(2);
  44. if (letter < CHAR_LOWERCASE_A || letter > CHAR_LOWERCASE_Z || sep !== ":") {
  45. throw new TypeError("File URL path must be absolute");
  46. }
  47. return pathname.slice(1);
  48. }
  49. /**
  50. * @param {URL} url file: URL instance
  51. * @returns {string} POSIX filesystem path
  52. */
  53. function getPathFromURLPosix(url) {
  54. if (url.hostname !== "") {
  55. throw new TypeError('File URL host must be "localhost" or empty');
  56. }
  57. const { pathname } = url;
  58. for (let n = 0; n < pathname.length; n++) {
  59. if (pathname[n] === "%") {
  60. const third = (pathname.codePointAt(n + 2) || 0) | 0x20;
  61. if (pathname[n + 1] === "2" && third === 102) {
  62. throw new TypeError(
  63. "File URL path must not include encoded / characters",
  64. );
  65. }
  66. }
  67. }
  68. return decodeURIComponent(pathname);
  69. }
  70. /**
  71. * @param {string | URL} path a `file:` URL string or `URL` instance
  72. * @param {{ windows?: boolean }=} options force the platform branch
  73. * @returns {string} the filesystem path
  74. */
  75. function fileURLToPath(path, options) {
  76. const url = typeof path === "string" ? new URL(path) : path;
  77. if (url.protocol !== "file:") {
  78. throw new TypeError("The URL must be of scheme file");
  79. }
  80. const windows =
  81. options && options.windows !== undefined ? options.windows : isWindows;
  82. return windows ? getPathFromURLWin32(url) : getPathFromURLPosix(url);
  83. }
  84. module.exports = fileURLToPath;