Skip to content

Commit

Permalink
feat(lib): [fileURLToPath] platform defaults
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Sep 22, 2024
1 parent 5ef0893 commit 9883d58
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/internal/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @module pathe/internal/constants
*/

import process from '#internal/process'
import type { WindowsDelimiter, WindowsSep } from '@flex-development/pathe'

/**
Expand All @@ -22,17 +23,30 @@ const DRIVE_PATH_REGEX: RegExp = /^(?<drive>(?<letter>[a-z]):(?:\/|\\{2})?)/i
*
* @see {@linkcode WindowsDelimiter}
*
* @internal
*
* @const {WindowsDelimiter} delimiterWindows
*/
const delimiterWindows: WindowsDelimiter = ';'

/**
* Windows operating system?
*
* @internal
*
* @const {boolean} isWindows
*/
const isWindows: boolean = process.platform === 'win32'

/**
* Windows path segment separator.
*
* @see {@linkcode WindowsSep}
*
* @internal
*
* @const {WindowsSep} sepWindows
*/
const sepWindows: WindowsSep = '\\'

export { delimiterWindows, DRIVE_PATH_REGEX, sepWindows }
export { delimiterWindows, DRIVE_PATH_REGEX, isWindows, sepWindows }
12 changes: 10 additions & 2 deletions src/lib/file-url-to-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @module pathe/lib/fileURLToPath
*/

import { isWindows } from '#internal/constants'
import domainToUnicode from '#internal/domain-to-unicode'
import isURL from '#internal/is-url'
import process from '#internal/process'
Expand Down Expand Up @@ -79,9 +80,16 @@ function fileURLToPath(
// decode pathname
pathname = decodeURIComponent(pathname)

/**
* Windows operating system?
*
* @const {boolean} windows
*/
const windows: boolean = options?.windows ?? /* c8 ignore next */ isWindows

// hostname -> UNC path
if (url.hostname) {
if (options?.windows) {
if (windows) {
// pass the hostname through domainToUnicode just in case it is an IDN
// using punycode encoding.
// note: this only causes IDNs with an `xn--` prefix to be decoded.
Expand All @@ -94,7 +102,7 @@ function fileURLToPath(
// drive path
if (isSep(pathname[0]) && isDeviceRoot(pathname.slice(1, 4))) {
if (!url.hostname) pathname = pathname.slice(1)
} else if (options?.windows && !url.hostname) {
} else if (windows && !url.hostname) {
throw new ERR_INVALID_FILE_URL_PATH('must be absolute')
}

Expand Down

0 comments on commit 9883d58

Please sign in to comment.