From 6780cb42665bb0ee0ab7c9de011cbc89c68d3e81 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Tue, 21 Apr 2020 20:51:46 +0800 Subject: [PATCH] refactor: utilize url#from --- lib/decode_url.js | 6 ++---- lib/encode_url.js | 6 ++---- lib/full_url_for.js | 2 +- lib/is_external_link.js | 2 +- lib/url_for.js | 2 +- package.json | 3 +-- 6 files changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/decode_url.js b/lib/decode_url.js index fe3fbf70..5793352a 100644 --- a/lib/decode_url.js +++ b/lib/decode_url.js @@ -1,7 +1,6 @@ 'use strict'; -const { parse, URL } = require('url'); -const { toUnicode } = require('punycode.js'); +const { parse, format } = require('url'); const safeDecodeURI = str => { try { @@ -18,8 +17,7 @@ const decodeURL = str => { // Exit if input is a data url if (parsed.origin === 'null') return str; - // TODO: refactor to `url.format()` once Node 8 is dropped - const url = parsed.toString().replace(parsed.hostname, toUnicode(parsed.hostname)); + const url = format(parsed, { unicode: true }); return safeDecodeURI(url); } diff --git a/lib/encode_url.js b/lib/encode_url.js index 78b084a1..3579e140 100644 --- a/lib/encode_url.js +++ b/lib/encode_url.js @@ -1,7 +1,6 @@ 'use strict'; -const { toUnicode } = require('punycode.js'); -const { parse, URL } = require('url'); +const { parse, format } = require('url'); const safeDecodeURI = str => { try { @@ -20,8 +19,7 @@ const encodeURL = str => { parsed.search = encodeURI(safeDecodeURI(parsed.search)); // preserve IDN - // TODO: refactor to url.format() once Node 8 EOL - return parsed.toString().replace(parsed.hostname, toUnicode(parsed.hostname)); + return format(parsed, { unicode: true }); } return encodeURI(safeDecodeURI(str)); diff --git a/lib/full_url_for.js b/lib/full_url_for.js index 68c14bc6..27233843 100755 --- a/lib/full_url_for.js +++ b/lib/full_url_for.js @@ -1,6 +1,6 @@ 'use strict'; -const { parse, URL } = require('url'); +const { parse } = require('url'); const encodeURL = require('./encode_url'); const prettyUrls = require('./pretty_urls'); diff --git a/lib/is_external_link.js b/lib/is_external_link.js index 86eeec70..2915bb8c 100644 --- a/lib/is_external_link.js +++ b/lib/is_external_link.js @@ -1,6 +1,6 @@ 'use strict'; -const { parse, URL } = require('url'); +const { parse } = require('url'); const Cache = require('./cache'); const cache = new Cache(); diff --git a/lib/url_for.js b/lib/url_for.js index d3a8fe68..cbfdb6ca 100644 --- a/lib/url_for.js +++ b/lib/url_for.js @@ -1,6 +1,6 @@ 'use strict'; -const { parse, URL } = require('url'); +const { parse } = require('url'); const encodeURL = require('./encode_url'); const relative_url = require('./relative_url'); const prettyUrls = require('./pretty_urls'); diff --git a/package.json b/package.json index 580d5447..3bb63385 100644 --- a/package.json +++ b/package.json @@ -50,11 +50,10 @@ "highlight.js": "^9.13.1", "htmlparser2": "^4.0.0", "prismjs": "^1.17.1", - "punycode.js": "^2.1.0", "strip-indent": "^3.0.0", "striptags": "^3.1.1" }, "engines": { - "node": ">=8.6.0" + "node": ">=10.18.1" } }