aboutsummaryrefslogtreecommitdiff
path: root/src/normalize-url.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/normalize-url.js')
-rw-r--r--src/normalize-url.js58
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
+}