Skip to content

Commit

Permalink
fix build + fix ssh bug with spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
monil-patel committed Nov 21, 2023
1 parent 7b6b9dd commit 12c6124
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 135 deletions.
90 changes: 46 additions & 44 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,53 +286,55 @@ function normalizeUrl(urlString, options) {
* - `parse_failed` (Boolean): Whether the parsing failed or not.
*/
const parseUrl = (url, normalize = false) => {

// Constants
const GIT_RE = /^(?:([a-z_][a-z0-9_-]{0,31})@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/;

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);
// Constants
// const GIT_RE = /^(?:([a-z_][a-z0-9_-]{0,31})@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/
const GIT_RE =
/^(?:([a-z_][a-z0-9_-]{0,31})@|https?:\/\/)([\w\.\-@]+)[\/:](([\~,\.\w,\-,\_,\/,\s]|%[0-9A-Fa-f]{2})+?(?:\.git|\/)?)$/;
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,
};
}

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[2];
parsed.host = matched[2];
parsed.user = matched[1];
parsed.pathname = `/${matched[3]}`;
parsed.parse_failed = false;
} else {
throwErr("URL parsing failed.");
}
url = normalizeUrl(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[2];
parsed.host = matched[2];
parsed.user = matched[1];
parsed.pathname = `/${matched[3]}`;
parsed.parse_failed = false;
} else {
throwErr("URL parsing failed.");
}
}

return parsed;
return parsed;
};

parseUrl.MAX_INPUT_LENGTH = 2048;
Expand Down
90 changes: 46 additions & 44 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -280,53 +280,55 @@ function normalizeUrl(urlString, options) {
* - `parse_failed` (Boolean): Whether the parsing failed or not.
*/
const parseUrl = (url, normalize = false) => {

// Constants
const GIT_RE = /^(?:([a-z_][a-z0-9_-]{0,31})@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/;

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);
// Constants
// const GIT_RE = /^(?:([a-z_][a-z0-9_-]{0,31})@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/
const GIT_RE =
/^(?:([a-z_][a-z0-9_-]{0,31})@|https?:\/\/)([\w\.\-@]+)[\/:](([\~,\.\w,\-,\_,\/,\s]|%[0-9A-Fa-f]{2})+?(?:\.git|\/)?)$/;
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,
};
}

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[2];
parsed.host = matched[2];
parsed.user = matched[1];
parsed.pathname = `/${matched[3]}`;
parsed.parse_failed = false;
} else {
throwErr("URL parsing failed.");
}
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[2];
parsed.host = matched[2];
parsed.user = matched[1];
parsed.pathname = `/${matched[3]}`;
parsed.parse_failed = false;
} else {
throwErr("URL parsing failed.");
}
}

return parsed;
return parsed;
};

parseUrl.MAX_INPUT_LENGTH = 2048;
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"module": "./dist/index.mjs",
"types": "./index.d.ts",
"exports": {
"types": {
"require": "./index.d.ts",
"import": "./index.d.mts"
"require": {
"types": "./index.d.ts",
"default": "./dist/index.js"
},
"require": "./dist/index.js",
"import": "./dist/index.mjs"
"import": {
"types": "./index.d.mts",
"default": "./dist/index.mjs"
}
},
"directories": {
"example": "example",
Expand Down
83 changes: 43 additions & 40 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Dependencies
import parsePath from "parse-path";

import normalizeUrl from "normalize-url";
import parsePath from "parse-path";

/**
* parseUrl
Expand Down Expand Up @@ -33,54 +34,56 @@ import normalizeUrl from "normalize-url";
* - `parse_failed` (Boolean): Whether the parsing failed or not.
*/
const parseUrl = (url, normalize = false) => {
// Constants
// const GIT_RE = /^(?:([a-z_][a-z0-9_-]{0,31})@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/
const GIT_RE =
/^(?:([a-z_][a-z0-9_-]{0,31})@|https?:\/\/)([\w\.\-@]+)[\/:](([\~,\.\w,\-,\_,\/,\s]|%[0-9A-Fa-f]{2})+?(?:\.git|\/)?)$/;
const throwErr = (msg) => {
const err = new Error(msg);
err.subject_url = url;
throw err;
};

// Constants
const GIT_RE = /^(?:([a-z_][a-z0-9_-]{0,31})@|https?:\/\/)([\w\.\-@]+)[\/:]([\~,\.\w,\-,\_,\/]+?(?:\.git|\/)?)$/

const throwErr = msg => {
const err = new Error(msg)
err.subject_url = url
throw err
}

if (typeof url !== "string" || !url.trim()) {
throwErr("Invalid url.")
}
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 (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)
if (normalize) {
if (typeof normalize !== "object") {
normalize = {
stripHash: false,
};
}
url = normalizeUrl(url, normalize);
}

const parsed = parsePath(url)
const parsed = parsePath(url);

// Potential git-ssh urls
if (parsed.parse_failed) {
const matched = parsed.href.match(GIT_RE)
// 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[2]
parsed.host = matched[2]
parsed.user = matched[1]
parsed.pathname = `/${matched[3]}`
parsed.parse_failed = false
} else {
throwErr("URL parsing failed.")
}
if (matched) {
parsed.protocols = ["ssh"];
parsed.protocol = "ssh";
parsed.resource = matched[2];
parsed.host = matched[2];
parsed.user = matched[1];
parsed.pathname = `/${matched[3]}`
parsed.parse_failed = false;
} else {
throwErr("URL parsing failed.");
}
}

return parsed;
}
return parsed;
};

parseUrl.MAX_INPUT_LENGTH = 2048

Expand Down
Loading

0 comments on commit 12c6124

Please sign in to comment.