Skip to content

Commit 702dfdc

Browse files
committed
refactor(lib): [fileURLToPath] input validation
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent 4dbc19d commit 702dfdc

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/internal/validate-file-url.mts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @file Internal - validateFileURL
3+
* @module pathe/internal/validateFileURL
4+
*/
5+
6+
import validateURLString from '#internal/validate-url-string'
7+
import {
8+
ERR_INVALID_URL_SCHEME,
9+
type ErrInvalidArgType,
10+
type ErrInvalidUrlScheme
11+
} from '@flex-development/errnode'
12+
13+
/**
14+
* Check if `value` is a `file:` {@linkcode URL} object or string.
15+
*
16+
* @internal
17+
*
18+
* @param {unknown} value
19+
* Value to check
20+
* @param {string} name
21+
* Name of invalid argument or property
22+
* @return {value is URL | string}
23+
* `true` if `value` is `file:` URL object or string
24+
* @throws {ErrInvalidArgType}
25+
* If `value` is not `URL` object or string
26+
* @throws {ErrInvalidUrlScheme}
27+
* If `value` is not a `file:` URL object or string
28+
*/
29+
function validateFileURL(
30+
value: unknown,
31+
name: string
32+
): value is URL | string {
33+
validateURLString(value, name)
34+
if (String(value).startsWith('file:')) return true
35+
throw new ERR_INVALID_URL_SCHEME('file')
36+
}
37+
38+
export default validateFileURL

src/lib/__tests__/file-url-to-path.spec.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @file Unit Tests - fileURLToPath
33
* @module pathe/lib/tests/unit/fileURLToPath
4-
* @see https://github.com/nodejs/node/blob/v23.2.0/test/parallel/test-url-fileurltopath.js
4+
* @see https://github.com/nodejs/node/blob/v23.4.0/test/parallel/test-url-fileurltopath.js
55
*/
66

77
import process from '#internal/process'

src/lib/file-url-to-path.mts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
import { isWindows } from '#internal/constants'
77
import domainToUnicode from '#internal/domain-to-unicode'
88
import process from '#internal/process'
9-
import validateURLString from '#internal/validate-url-string'
9+
import validateFileURL from '#internal/validate-file-url'
1010
import isDeviceRoot from '#lib/is-device-root'
1111
import isSep from '#lib/is-sep'
1212
import sep from '#lib/sep'
1313
import toPosix from '#lib/to-posix'
1414
import {
1515
ERR_INVALID_FILE_URL_HOST,
1616
ERR_INVALID_FILE_URL_PATH,
17-
ERR_INVALID_URL_SCHEME,
1817
type ErrInvalidFileUrlHost,
1918
type ErrInvalidFileUrlPath,
2019
type ErrInvalidUrlScheme
@@ -49,9 +48,7 @@ function fileURLToPath(
4948
url: URL | string,
5049
options?: FileUrlToPathOptions | null | undefined
5150
): string {
52-
validateURLString(url, 'url')
53-
54-
if (!String(url).startsWith('file:')) throw new ERR_INVALID_URL_SCHEME('file')
51+
validateFileURL(url, 'url')
5552
if (typeof url === 'string') url = new URL(url)
5653

5754
/**

0 commit comments

Comments
 (0)