Skip to content

Commit

Permalink
Refactor to match current Node internals
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 9, 2023
1 parent ab764ab commit 47eec44
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
6 changes: 3 additions & 3 deletions lib/minurl.browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {isUrl} from './minurl.shared.js'

// See: <https://github.com/nodejs/node/blob/fcf8ba4/lib/internal/url.js>
export {isUrl} from './minurl.shared.js'

// See: <https://github.com/nodejs/node/blob/6a3403c/lib/internal/url.js>

/**
* @param {URL | string} path
Expand Down Expand Up @@ -72,5 +74,3 @@ function getPathFromURLPosix(url) {

return decodeURIComponent(pathname)
}

export {isUrl} from './minurl.shared.js'
20 changes: 16 additions & 4 deletions lib/minurl.shared.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
/**
* Check if `fileUrlOrPath` looks like a URL.
* Checks if a value has the shape of a WHATWG URL object.
*
* Using a symbol or instanceof would not be able to recognize URL objects
* coming from other implementations (e.g. in Electron), so instead we are
* checking some well known properties for a lack of a better test.
*
* We use `href` and `protocol` as they are the only properties that are
* easy to retrieve and calculate due to the lazy nature of the getters.
*
* We check for auth attribute to distinguish legacy url instance with
* WHATWG URL instance.
*
* @param {unknown} fileUrlOrPath
* File path or URL.
* @returns {fileUrlOrPath is URL}
* Whether it’s a URL.
*/
// From: <https://github.com/nodejs/node/blob/fcf8ba4/lib/internal/url.js#L1501>
// From: <https://github.com/nodejs/node/blob/6a3403c/lib/internal/url.js#L720>
export function isUrl(fileUrlOrPath) {
return Boolean(
fileUrlOrPath !== null &&
typeof fileUrlOrPath === 'object' &&
'href' in fileUrlOrPath &&
fileUrlOrPath.href &&
'origin' in fileUrlOrPath &&
fileUrlOrPath.origin
'protocol' in fileUrlOrPath &&
fileUrlOrPath.protocol &&
// @ts-expect-error: indexing is fine.
fileUrlOrPath.auth === undefined
)
}

0 comments on commit 47eec44

Please sign in to comment.