MCPcopy Index your code
hub / github.com/nodejs/node / pathToFileURL

Function pathToFileURL

lib/internal/url.js:1651–1691  ·  view source on GitHub ↗
(filepath, options = kEmptyObject)

Source from the content-addressed store, hash-verified

1649}
1650
1651function 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
1693function toPathIfFileURL(fileURLOrPath) {
1694 if (!isURL(fileURLOrPath))

Callers 15

getCWDURLFunction · 0.70
constructorMethod · 0.70
getPackageJSONURLFunction · 0.50
findPackageJSONFunction · 0.50
executeUserEntryPointFunction · 0.50
convertCJSFilenameToURLFunction · 0.50
normalizeReferrerURLFunction · 0.50
constructorMethod · 0.50
legacyMainResolveFunction · 0.50
finalizeResolutionFunction · 0.50
packageImportsResolveFunction · 0.50
packageResolveFunction · 0.50

Calls 1

resolveMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…