Skip to content

Commit a90855c

Browse files
committed
Decode URL percent-encoding in pathLikeToString fallback
Fix URL pathname decoding in the fileURLToPath() catch block by adding decodeURIComponent() to convert percent-encoded characters (like %20) back to their literal values (spaces). The URL pathname property retains percent-encoding, but file paths require decoded characters. This is not platform-specific; all URLs use percent-encoding on all operating systems.
1 parent 06c910c commit a90855c

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

registry/src/lib/path.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,31 +380,37 @@ export function pathLikeToString(
380380
// - Windows valid: file:///C:/path → handled by fileURLToPath()
381381
// - Windows invalid: file:///path → pathname '/path' → strips to 'path'
382382
const pathname = pathLike.pathname
383+
384+
// Decode percent-encoded characters (e.g., %20 → space).
385+
// The pathname property keeps URL encoding, but file paths need decoded characters.
386+
// This is not platform-specific; all URLs use percent-encoding regardless of OS.
387+
const decodedPathname = decodeURIComponent(pathname)
388+
383389
// URL pathnames always start with `/`.
384390
// On Windows, strip the leading slash only for malformed URLs that lack drive letters
385391
// (e.g., `/path` should be `path`, but `/C:/path` should be `C:/path`).
386392
// On Unix, keep the leading slash for absolute paths (e.g., `/home/user`).
387393
const WIN32 = /*@__PURE__*/ require('./constants/WIN32.js')
388-
if (WIN32 && pathname.startsWith('/')) {
394+
if (WIN32 && decodedPathname.startsWith('/')) {
389395
// Check for drive letter pattern following Node.js source: /[a-zA-Z]:/
390396
// Character at index 1 should be a letter, character at index 2 should be ':'
391397
// Convert to lowercase
392-
const letter = pathname.charCodeAt(1) | 0x20
398+
const letter = decodedPathname.charCodeAt(1) | 0x20
393399
const hasValidDriveLetter =
394-
pathname.length >= 3 &&
400+
decodedPathname.length >= 3 &&
395401
letter >= 97 &&
396402
// 'a' to 'z'
397403
letter <= 122 &&
398-
pathname.charAt(2) === ':'
404+
decodedPathname.charAt(2) === ':'
399405

400406
if (!hasValidDriveLetter) {
401407
// On Windows, preserve Unix-style absolute paths that don't start with a drive letter.
402408
// Only strip the leading slash for truly malformed Windows paths.
403409
// Since fileURLToPath() failed, this is likely a valid Unix-style absolute path.
404-
return pathname
410+
return decodedPathname
405411
}
406412
}
407-
return pathname
413+
return decodedPathname
408414
}
409415
}
410416
return String(pathLike)

0 commit comments

Comments
 (0)