Skip to content

Commit

Permalink
fix(url-parser): pathnames assigning an unneeded forward slash to com…
Browse files Browse the repository at this point in the history
…ponents without hostnames
  • Loading branch information
Michael committed Dec 13, 2022
1 parent c19ebb5 commit ec8acd7
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/core/parsers/base-url-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface ParamsConversionOutput {
const DEFAULT_PROTOCOL = "http";

const PARSE_URL =
/^(?:([^:\/?#]+):)?(?:\/\/((?:([^@\/\n]+)@)?((?:[0-9]{1,3}\.){3}[0-9]{1,3}|\[[0-9a-f:]+\]|[^#:\/?\n]+)(?::(\d*))?))?(?:[\/]*([^?#]*))(?:[\?]*([^#]*))?(?:[\#]*(.*))?/i;
/^(?:([^:\/?#]+):)?(?:[\/\/]+((?:([^@\/\n]+)@)?((?:[0-9]{1,3}\.){3}[0-9]{1,3}|\[[0-9a-f:]+\]|[^#:\/?\n]+)(?::(\d*))?))?(?:[\/]*([^?#]*))(?:[\?]*([^#]*))?(?:[\#]*(.*))?/i;

const IPV4_REGEX = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
const IPV6_REGEX = /^(\[[0-9a-f:]+\])$/i;
Expand Down Expand Up @@ -144,14 +144,18 @@ export class BaseURLParser {
protected $component: EnforceComponent;

constructor(input: string | SerializeComponent) {
if (input && !isString(input) && !isObject(input)) {
throw new TypeError("Invalid input. Must be a string or an object.");
}

this.$component = createEmptyScheme<EnforceComponent>(URL_COMPONENT_KEYS, "");

if (isString(input) && input) {
this.$component = this.parse(input);
return;
}

if (isObject(input)) {
if (isObject(input) && !isEmpty(input)) {
if (input.href) {
this.$component = this.parse(input.href);
} else {
Expand Down Expand Up @@ -291,7 +295,11 @@ export class BaseURLParser {
return;
}

this.$component.pathname = ensureLeadingToken("/", pathname);
if (this.$component.hostname) {
this.$component.pathname = ensureLeadingToken("/", pathname);
} else {
this.$component.pathname = pathname;
}
}

protected parseParams(params: CustomSearchParams): void {
Expand Down

0 comments on commit ec8acd7

Please sign in to comment.