From 8df2b97a669485d9a15a45ef252c26888df778f8 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Mon, 13 May 2019 12:07:31 +0300 Subject: [PATCH] fix: crash in node due to broken URL parsing --- src/utils/helpers.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index de46f654..bf0f5088 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -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('/')) { @@ -163,7 +163,7 @@ 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) { @@ -171,7 +171,16 @@ export function titleize(text: string) { } 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); + } +}