diff options
| author | Travis Fischer <[email protected]> | 2021-06-02 13:59:27 -0400 |
|---|---|---|
| committer | Travis Fischer <[email protected]> | 2021-06-02 13:59:27 -0400 |
| commit | f6321fc3249d83a0059ef47978ed101d3c75375f (patch) | |
| tree | 9da8b59a0c94c08f92506e57653b8c9f55fa85e8 /src/normalize-url.js | |
| download | cf-image-proxy-f6321fc3249d83a0059ef47978ed101d3c75375f.tar.xz cf-image-proxy-f6321fc3249d83a0059ef47978ed101d3c75375f.zip | |
feat: import from notion2site repo
Diffstat (limited to 'src/normalize-url.js')
| -rw-r--r-- | src/normalize-url.js | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/normalize-url.js b/src/normalize-url.js new file mode 100644 index 0000000..c22e243 --- /dev/null +++ b/src/normalize-url.js @@ -0,0 +1,58 @@ +/** + * Stripped down version of [normalize-url](https://github.com/sindresorhus/normalize-url) + * by sindresorhus + * + * - always converts http => https + * - removed unused options + * - removed dataURL support + */ +export const normalizeUrl = (urlString) => { + const urlObj = new URL(urlString) + + if (urlObj.protocol === 'http:') { + urlObj.protocol = 'https:' + } + + /* + // Remove auth + // TODO: Cloudflare Workers seems to have a subtle bug where if you set URL.username and + // URL.password at all, it will include the @ authentication prefix in the resulting URL. + // This does not repro in normal web or Node.js contexts. + if (options.stripAuthentication) { + urlObj.username = '' + urlObj.password = '' + } + */ + + // Remove duplicate slashes if not preceded by a protocol + if (urlObj.pathname) { + urlObj.pathname = urlObj.pathname.replace(/(?<!https?:)\/{2,}/g, '/') + } + + // Decode URI octets + if (urlObj.pathname) { + urlObj.pathname = decodeURI(urlObj.pathname) + } + + if (urlObj.hostname) { + // Remove trailing dot + urlObj.hostname = urlObj.hostname.replace(/\.$/, '') + } + + // Sort query parameters + urlObj.searchParams.sort() + + // Remove trailing `/` + urlObj.pathname = urlObj.pathname.replace(/\/$/, '') + + urlString = urlObj.toString() + + // Remove trailing `/` for real this time + if (urlObj.pathname === '/' && urlObj.hash === '') { + urlString = urlString.replace(/\/$/, '') + } + + urlString = urlString.replace(/https:%2F%2F/g, 'https%3A%2F%2F') + + return urlString +} |