fix(clean_url): encode caret

This commit is contained in:
MDLeom 2025-03-22 07:39:33 +00:00
parent 0f4847a03d
commit 9708b2c289
No known key found for this signature in database
GPG Key ID: 32D3E28E96A695E8
1 changed files with 16 additions and 4 deletions

View File

@ -2,6 +2,19 @@
import { createInterface } from 'node:readline'
// nodejs does not percent-encode ^ yet
// https://github.com/nodejs/node/issues/57313
// Applies to path, exclude query string
const caretPath = (pathname) => {
if (!pathname.includes('?')) return pathname.replaceAll('^', '%5E')
const pathArray = pathname.split('?')
const path = pathArray[0].replaceAll('^', '%5E')
const search = pathArray.slice(1).join('?')
return `${path}?${search}`
}
for await (const line of createInterface({ input: process.stdin, terminal: false })) {
// parse hostname from url
if (process.argv[2] === 'hostname') {
@ -33,16 +46,15 @@ for await (const line of createInterface({ input: process.stdin, terminal: false
}
url.host = url.host.replace(/^www\./, '')
// nodejs does not percent-encode ^ yet
// https://github.com/nodejs/node/issues/57313
url.pathname = url.pathname.replaceAll('^', encodeURI('^'))
url.pathname = caretPath(url.pathname)
const outUrl = `${url.host}${url.pathname}${url.search}`
// remove trailing slash from domain except path #43
.replace(/(^[^/]*)\/+$/, '$1')
console.log(outUrl)
} else {
const outUrl = line
const outUrl = caretPath(line)
// remove protocol
.split('/').slice(2).join('/')
// remove www