AK-21/Graphite-Industrial-Intelligence
0
1import {isUrl} from './minurl.shared.js'2 3export {isUrl} from './minurl.shared.js'4 5// See: <https://github.com/nodejs/node/blob/6a3403c/lib/internal/url.js>6 7/**8 * @param {URL | string} path9 * File URL.10 * @returns {string}11 * File URL.12 */13export function urlToPath(path) {14 if (typeof path === 'string') {15 path = new URL(path)16 } else if (!isUrl(path)) {17 /** @type {NodeJS.ErrnoException} */18 const error = new TypeError(19 'The "path" argument must be of type string or an instance of URL. Received `' +20 path +21 '`'22 )23 error.code = 'ERR_INVALID_ARG_TYPE'24 throw error25 }26 27 if (path.protocol !== 'file:') {28 /** @type {NodeJS.ErrnoException} */29 const error = new TypeError('The URL must be of scheme file')30 error.code = 'ERR_INVALID_URL_SCHEME'31 throw error32 }33 34 return getPathFromURLPosix(path)35}36 37/**38 * Get a path from a POSIX URL.39 *40 * @param {URL} url41 * URL.42 * @returns {string}43 * File path.44 */45function getPathFromURLPosix(url) {46 if (url.hostname !== '') {47 /** @type {NodeJS.ErrnoException} */48 const error = new TypeError(49 'File URL host must be "localhost" or empty on darwin'50 )51 error.code = 'ERR_INVALID_FILE_URL_HOST'52 throw error53 }54 55 const pathname = url.pathname56 let index = -157 58 while (++index < pathname.length) {59 if (60 pathname.codePointAt(index) === 37 /* `%` */ &&61 pathname.codePointAt(index + 1) === 50 /* `2` */62 ) {63 const third = pathname.codePointAt(index + 2)64 if (third === 70 /* `F` */ || third === 102 /* `f` */) {65 /** @type {NodeJS.ErrnoException} */66 const error = new TypeError(67 'File URL path must not include encoded / characters'68 )69 error.code = 'ERR_INVALID_FILE_URL_PATH'70 throw error71 }72 }73 }74 75 return decodeURIComponent(pathname)76}77 