-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f0dfd7
commit 2373f7f
Showing
5 changed files
with
241 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,3 @@ node_modules | |
.DS_Store | ||
package-lock.json | ||
.bloggify/* | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict'; | ||
|
||
var parsePath = require('parse-path'); | ||
var normalizeUrl = require('normalize-url'); | ||
|
||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
|
||
var parsePath__default = /*#__PURE__*/_interopDefaultLegacy(parsePath); | ||
var normalizeUrl__default = /*#__PURE__*/_interopDefaultLegacy(normalizeUrl); | ||
|
||
// Dependencies | ||
|
||
/** | ||
* parseUrl | ||
* Parses the input url. | ||
* | ||
* **Note**: This *throws* if invalid urls are provided. | ||
* | ||
* @name parseUrl | ||
* @function | ||
* @param {String} url The input url. | ||
* @param {Boolean|Object} normalize Whether to normalize the url or not. | ||
* Default is `false`. If `true`, the url will | ||
* be normalized. If an object, it will be the | ||
* options object sent to [`normalize-url`](https://github.com/sindresorhus/normalize-url). | ||
* | ||
* For SSH urls, normalize won't work. | ||
* | ||
* @return {Object} An object containing the following fields: | ||
* | ||
* - `protocols` (Array): An array with the url protocols (usually it has one element). | ||
* - `protocol` (String): The first protocol, `"ssh"` (if the url is a ssh url) or `"file"`. | ||
* - `port` (null|Number): The domain port. | ||
* - `resource` (String): The url domain (including subdomains). | ||
* - `user` (String): The authentication user (usually for ssh urls). | ||
* - `pathname` (String): The url pathname. | ||
* - `hash` (String): The url hash. | ||
* - `search` (String): The url querystring value. | ||
* - `href` (String): The input url. | ||
* - `query` (Object): The url querystring, parsed as object. | ||
* - `parse_failed` (Boolean): Whether the parsing failed or not. | ||
*/ | ||
const parseUrl = (url, normalize = false) => { | ||
|
||
// Constants | ||
const GIT_RE = /(^(git@|http(s)?:\/\/)([\w\.\-@]+)(\/|:))(([\~,\.\w,\-,\_,\/]+)(.git){0,1}((\/){0,1}))/; | ||
|
||
const throwErr = msg => { | ||
const err = new Error(msg); | ||
err.subject_url = url; | ||
throw err | ||
}; | ||
|
||
if (typeof url !== "string" || !url.trim()) { | ||
throwErr("Invalid url."); | ||
} | ||
|
||
if (url.length > parseUrl.MAX_INPUT_LENGTH) { | ||
throwErr("Input exceeds maximum length. If needed, change the value of parseUrl.MAX_INPUT_LENGTH."); | ||
} | ||
|
||
if (normalize) { | ||
if (typeof normalize !== "object") { | ||
normalize = { | ||
stripHash: false | ||
}; | ||
} | ||
url = normalizeUrl__default["default"](url, normalize); | ||
} | ||
|
||
const parsed = parsePath__default["default"](url); | ||
|
||
// Potential git-ssh urls | ||
if (parsed.parse_failed) { | ||
const matched = parsed.href.match(GIT_RE); | ||
if (matched) { | ||
parsed.protocols = ["ssh"]; | ||
parsed.protocol = "ssh"; | ||
parsed.resource = matched[4]; | ||
parsed.host = matched[4]; | ||
parsed.user = "git"; | ||
parsed.pathname = `/${matched[6]}`; | ||
parsed.parse_failed = false; | ||
} else { | ||
throwErr("URL parsing failed."); | ||
} | ||
} | ||
|
||
return parsed; | ||
}; | ||
|
||
parseUrl.MAX_INPUT_LENGTH = 2048; | ||
|
||
module.exports = parseUrl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import parsePath from 'parse-path'; | ||
import normalizeUrl from 'normalize-url'; | ||
|
||
// Dependencies | ||
|
||
/** | ||
* parseUrl | ||
* Parses the input url. | ||
* | ||
* **Note**: This *throws* if invalid urls are provided. | ||
* | ||
* @name parseUrl | ||
* @function | ||
* @param {String} url The input url. | ||
* @param {Boolean|Object} normalize Whether to normalize the url or not. | ||
* Default is `false`. If `true`, the url will | ||
* be normalized. If an object, it will be the | ||
* options object sent to [`normalize-url`](https://github.com/sindresorhus/normalize-url). | ||
* | ||
* For SSH urls, normalize won't work. | ||
* | ||
* @return {Object} An object containing the following fields: | ||
* | ||
* - `protocols` (Array): An array with the url protocols (usually it has one element). | ||
* - `protocol` (String): The first protocol, `"ssh"` (if the url is a ssh url) or `"file"`. | ||
* - `port` (null|Number): The domain port. | ||
* - `resource` (String): The url domain (including subdomains). | ||
* - `user` (String): The authentication user (usually for ssh urls). | ||
* - `pathname` (String): The url pathname. | ||
* - `hash` (String): The url hash. | ||
* - `search` (String): The url querystring value. | ||
* - `href` (String): The input url. | ||
* - `query` (Object): The url querystring, parsed as object. | ||
* - `parse_failed` (Boolean): Whether the parsing failed or not. | ||
*/ | ||
const parseUrl = (url, normalize = false) => { | ||
|
||
// Constants | ||
const GIT_RE = /(^(git@|http(s)?:\/\/)([\w\.\-@]+)(\/|:))(([\~,\.\w,\-,\_,\/]+)(.git){0,1}((\/){0,1}))/; | ||
|
||
const throwErr = msg => { | ||
const err = new Error(msg); | ||
err.subject_url = url; | ||
throw err | ||
}; | ||
|
||
if (typeof url !== "string" || !url.trim()) { | ||
throwErr("Invalid url."); | ||
} | ||
|
||
if (url.length > parseUrl.MAX_INPUT_LENGTH) { | ||
throwErr("Input exceeds maximum length. If needed, change the value of parseUrl.MAX_INPUT_LENGTH."); | ||
} | ||
|
||
if (normalize) { | ||
if (typeof normalize !== "object") { | ||
normalize = { | ||
stripHash: false | ||
}; | ||
} | ||
url = normalizeUrl(url, normalize); | ||
} | ||
|
||
const parsed = parsePath(url); | ||
|
||
// Potential git-ssh urls | ||
if (parsed.parse_failed) { | ||
const matched = parsed.href.match(GIT_RE); | ||
if (matched) { | ||
parsed.protocols = ["ssh"]; | ||
parsed.protocol = "ssh"; | ||
parsed.resource = matched[4]; | ||
parsed.host = matched[4]; | ||
parsed.user = "git"; | ||
parsed.pathname = `/${matched[6]}`; | ||
parsed.parse_failed = false; | ||
} else { | ||
throwErr("URL parsing failed."); | ||
} | ||
} | ||
|
||
return parsed; | ||
}; | ||
|
||
parseUrl.MAX_INPUT_LENGTH = 2048; | ||
|
||
export { parseUrl as default }; |