(filepath, options = kEmptyObject)
| 1649 | } |
| 1650 | |
| 1651 | function pathToFileURL(filepath, options = kEmptyObject) { |
| 1652 | const windows = options?.windows ?? isWindows; |
| 1653 | const isUNC = windows && StringPrototypeStartsWith(filepath, '\\\\'); |
| 1654 | let resolved = isUNC ? |
| 1655 | filepath : |
| 1656 | (windows ? path.win32.resolve(filepath) : path.posix.resolve(filepath)); |
| 1657 | if (isUNC || (windows && StringPrototypeStartsWith(resolved, '\\\\'))) { |
| 1658 | // UNC path format: \\server\share\resource |
| 1659 | // Handle extended UNC path and standard UNC path |
| 1660 | // "\\?\UNC\" path prefix should be ignored. |
| 1661 | // Ref: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation |
| 1662 | const isExtendedUNC = StringPrototypeStartsWith(resolved, '\\\\?\\UNC\\'); |
| 1663 | const prefixLength = isExtendedUNC ? 8 : 2; |
| 1664 | const hostnameEndIndex = StringPrototypeIndexOf(resolved, '\\', prefixLength); |
| 1665 | if (hostnameEndIndex === -1) { |
| 1666 | throw new ERR_INVALID_ARG_VALUE( |
| 1667 | 'path', |
| 1668 | resolved, |
| 1669 | 'Missing UNC resource path', |
| 1670 | ); |
| 1671 | } |
| 1672 | if (hostnameEndIndex === 2) { |
| 1673 | throw new ERR_INVALID_ARG_VALUE( |
| 1674 | 'path', |
| 1675 | resolved, |
| 1676 | 'Empty UNC servername', |
| 1677 | ); |
| 1678 | } |
| 1679 | const hostname = StringPrototypeSlice(resolved, prefixLength, hostnameEndIndex); |
| 1680 | return new URL(StringPrototypeSlice(resolved, hostnameEndIndex), hostname, kCreateURLFromWindowsPathSymbol); |
| 1681 | } |
| 1682 | // path.resolve strips trailing slashes so we must add them back |
| 1683 | const filePathLast = StringPrototypeCharCodeAt(filepath, |
| 1684 | filepath.length - 1); |
| 1685 | if ((filePathLast === CHAR_FORWARD_SLASH || |
| 1686 | ((windows ?? isWindows) && filePathLast === CHAR_BACKWARD_SLASH)) && |
| 1687 | resolved[resolved.length - 1] !== path.sep) |
| 1688 | resolved += '/'; |
| 1689 | |
| 1690 | return new URL(resolved, undefined, windows ? kCreateURLFromWindowsPathSymbol : kCreateURLFromPosixPathSymbol); |
| 1691 | } |
| 1692 | |
| 1693 | function toPathIfFileURL(fileURLOrPath) { |
| 1694 | if (!isURL(fileURLOrPath)) |
no test coverage detected
searching dependent graphs…