From 8b71006fbec6b4a0f57828aea69420b610e9db4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Acid=20Chicken=20=28=E7=A1=AB=E9=85=B8=E9=B6=8F=29?= Date: Mon, 4 Feb 2019 00:09:24 +0900 Subject: [PATCH 1/5] Bye 'is-url' --- package.json | 1 - src/@types/is-url.d.ts | 7 ------- src/config/load.ts | 14 +++++++++----- 3 files changed, 9 insertions(+), 13 deletions(-) delete mode 100644 src/@types/is-url.d.ts diff --git a/package.json b/package.json index b480519f9..820c76bad 100644 --- a/package.json +++ b/package.json @@ -142,7 +142,6 @@ "insert-text-at-cursor": "0.1.1", "is-root": "2.0.0", "is-svg": "3.0.0", - "is-url": "1.2.4", "js-yaml": "3.12.1", "jsdom": "13.1.0", "json5": "2.1.0", diff --git a/src/@types/is-url.d.ts b/src/@types/is-url.d.ts deleted file mode 100644 index c1ccadd49..000000000 --- a/src/@types/is-url.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -declare module 'is-url' { - function isUrl(string: string): boolean; - - namespace isUrl {} // Hack - - export = isUrl; -} diff --git a/src/config/load.ts b/src/config/load.ts index 57cfb8075..fc3e69919 100644 --- a/src/config/load.ts +++ b/src/config/load.ts @@ -6,7 +6,6 @@ import * as fs from 'fs'; import { URL } from 'url'; import * as yaml from 'js-yaml'; import { Source, Mixin } from './types'; -import * as isUrl from 'is-url'; import * as pkg from '../../package.json'; /** @@ -26,10 +25,7 @@ export default function load() { const mixin = {} as Mixin; - // Validate URLs - if (!isUrl(config.url)) throw `url="${config.url}" is not a valid URL`; - - const url = new URL(config.url); + const url = validateUrl(config.url); config.url = normalizeUrl(config.url); mixin.host = url.host; @@ -51,6 +47,14 @@ export default function load() { return Object.assign(config, mixin); } +function validateUrl(url: string) { + try { + return new URL(url); + } catch (e) { + throw `url="${url}" is not a valid URL`; + } +} + function normalizeUrl(url: string) { return url.endsWith('/') ? url.substr(0, url.length - 1) : url; } From cf9e8ed39ec2acd098ba3587f0957a6afa641a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Acid=20Chicken=20=28=E7=A1=AB=E9=85=B8=E9=B6=8F=29?= Date: Mon, 4 Feb 2019 02:06:08 +0900 Subject: [PATCH 2/5] Update load.ts --- src/config/load.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/config/load.ts b/src/config/load.ts index fc3e69919..904305647 100644 --- a/src/config/load.ts +++ b/src/config/load.ts @@ -47,14 +47,21 @@ export default function load() { return Object.assign(config, mixin); } -function validateUrl(url: string) { +function tryCreateUrl(url: string) { try { return new URL(url); } catch (e) { - throw `url="${url}" is not a valid URL`; + throw `url="${url}" is not a valid URL.`; } } +function validateUrl(url: string) { + const result = tryCreateUrl(url); + if (result.pathname.trim('/').length) throw `url="${url}" is not a valid URL, has a pathname.`; + if (!url.includes(result.host)) throw `url="${url}" is not a valid URL, has an invalid hostname.`; + return result; +} + function normalizeUrl(url: string) { return url.endsWith('/') ? url.substr(0, url.length - 1) : url; } From 67792fcb5e866cfad6fc6788b8555526658099fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Acid=20Chicken=20=28=E7=A1=AB=E9=85=B8=E9=B6=8F=29?= Date: Mon, 4 Feb 2019 02:09:41 +0900 Subject: [PATCH 3/5] Update load.ts --- src/config/load.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/config/load.ts b/src/config/load.ts index 904305647..1cec3b148 100644 --- a/src/config/load.ts +++ b/src/config/load.ts @@ -59,6 +59,7 @@ function validateUrl(url: string) { const result = tryCreateUrl(url); if (result.pathname.trim('/').length) throw `url="${url}" is not a valid URL, has a pathname.`; if (!url.includes(result.host)) throw `url="${url}" is not a valid URL, has an invalid hostname.`; + if (!/^https?:$/.test(result.protocol)) throw `url="${url}" is not a valid URL, has an invalid protocol.`; return result; } From c7e8c27ce6652b64a55d7d5ea85c66b4a1cbc06c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Acid=20Chicken=20=28=E7=A1=AB=E9=85=B8=E9=B6=8F=29?= Date: Mon, 4 Feb 2019 02:14:18 +0900 Subject: [PATCH 4/5] Fix bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit C#っぽく使ってしまっていた。 --- src/config/load.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/load.ts b/src/config/load.ts index 1cec3b148..c0b966d53 100644 --- a/src/config/load.ts +++ b/src/config/load.ts @@ -57,7 +57,7 @@ function tryCreateUrl(url: string) { function validateUrl(url: string) { const result = tryCreateUrl(url); - if (result.pathname.trim('/').length) throw `url="${url}" is not a valid URL, has a pathname.`; + if (result.pathname.replace('/', '').length) throw `url="${url}" is not a valid URL, has a pathname.`; if (!url.includes(result.host)) throw `url="${url}" is not a valid URL, has an invalid hostname.`; if (!/^https?:$/.test(result.protocol)) throw `url="${url}" is not a valid URL, has an invalid protocol.`; return result; From 9595a56346166b466903b2ed3f4dcfb3c3d5e514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Acid=20Chicken=20=28=E7=A1=AB=E9=85=B8=E9=B6=8F=29?= Date: Mon, 4 Feb 2019 22:30:24 +0900 Subject: [PATCH 5/5] Revert "Update load.ts" This reverts commit cf9e8ed39ec2acd098ba3587f0957a6afa641a74, commit 67792fcb5e866cfad6fc6788b8555526658099fc, and commit c7e8c27ce6652b64a55d7d5ea85c66b4a1cbc06c. --- src/config/load.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/config/load.ts b/src/config/load.ts index c0b966d53..fc3e69919 100644 --- a/src/config/load.ts +++ b/src/config/load.ts @@ -47,22 +47,14 @@ export default function load() { return Object.assign(config, mixin); } -function tryCreateUrl(url: string) { +function validateUrl(url: string) { try { return new URL(url); } catch (e) { - throw `url="${url}" is not a valid URL.`; + throw `url="${url}" is not a valid URL`; } } -function validateUrl(url: string) { - const result = tryCreateUrl(url); - if (result.pathname.replace('/', '').length) throw `url="${url}" is not a valid URL, has a pathname.`; - if (!url.includes(result.host)) throw `url="${url}" is not a valid URL, has an invalid hostname.`; - if (!/^https?:$/.test(result.protocol)) throw `url="${url}" is not a valid URL, has an invalid protocol.`; - return result; -} - function normalizeUrl(url: string) { return url.endsWith('/') ? url.substr(0, url.length - 1) : url; }