Skip to content

Commit

Permalink
fix: crash in node due to broken URL parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed May 13, 2019
1 parent 3f67650 commit 8df2b97
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function resolveUrl(url: string, to: string) {
let res;
if (to.startsWith('//')) {
const { protocol: specProtocol } = parse(url);
res = `${specProtocol}${to}`;
res = `${specProtocol || 'https:'}${to}`;
} else if (isAbsoluteUrl(to)) {
res = to;
} else if (!to.startsWith('/')) {
Expand All @@ -163,15 +163,24 @@ export function resolveUrl(url: string, to: string) {
}

export function getBasePath(serverUrl: string): string {
return new URL(serverUrl).pathname;
return parseURL(serverUrl).pathname;
}

export function titleize(text: string) {
return text.charAt(0).toUpperCase() + text.slice(1);
}

export function removeQueryString(serverUrl: string): string {
const url = new URL(serverUrl);
const url = parseURL(serverUrl);
url.search = '';
return url.toString();
}

function parseURL(url: string) {
if (typeof URL === undefined) {
// node
return new (require('url')).URL(url);
} else {
return new URL(url);
}
}

0 comments on commit 8df2b97

Please sign in to comment.