From 4ddf00499aa06f8ba62c7d63ecdd165aa4ff36ae Mon Sep 17 00:00:00 2001 From: Chaphasilor Date: Mon, 2 Aug 2021 22:01:45 +0200 Subject: [PATCH] fix lamba function URI encoding issues --- public/netlify-functions/checkLinkAlive.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/public/netlify-functions/checkLinkAlive.js b/public/netlify-functions/checkLinkAlive.js index 7a59c89..a6ce1b6 100644 --- a/public/netlify-functions/checkLinkAlive.js +++ b/public/netlify-functions/checkLinkAlive.js @@ -24,9 +24,20 @@ const fetchTimeout = (url, ms, options = {}) => { return promise.finally(() => clearTimeout(timeout)); }; +function encodeUriOnce(url, useURIComponent = false) { + + let decoder = useURIComponent ? decodeURIComponent : decodeURI + while(url !== decoder(url)) { + url = decoder(url); + } + + return encodeURI(url); + +} + function resolveLink(url) { - let resolvedUrl = url; + let resolvedUrl = encodeUriOnce(url); let resolvedHeaders = {}; if (url.includes(`driveindex.ga/`)) { @@ -48,7 +59,7 @@ function checkLink(urlData) { return new Promise((resolve) => { const startTime = performance.now() - urlData.url = encodeURI(urlData.url); + urlData.url = encodeUriOnce(urlData.url); fetchTimeout(urlData.url, REQUEST_TIMEOUT, { method: `HEAD`,