Merge pull request #58637 from Dev-next-gen/fix/format-keep-encoded-query

fix: keep encoded query delimiters when playlist:format normalizes links
This commit is contained in:
PChaicot
2026-11-14 13:49:13 +07:00
committed by GitHub
3 changed files with 28 additions and 1 deletions
+23 -1
View File
@@ -38,10 +38,32 @@ export function isURI(string: string): boolean {
}
}
// Sorts the query parameters like URLSearchParams.sort(), but keeps each one as written, so
// escapes like %26, %3D and %2B are not decoded the way normalize-url's own sorting does
function sortQueryParameters(url: string): string {
const queryStart = url.indexOf('?')
if (queryStart === -1) return url
const hashStart = url.indexOf('#', queryStart)
const queryEnd = hashStart === -1 ? url.length : hashStart
const getName = (param: string) => new URLSearchParams(param).keys().next().value ?? ''
const params = url
.slice(queryStart + 1, queryEnd)
.split('&')
.filter(Boolean)
.map(param => ({ param, name: getName(param) }))
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0))
.map(({ param }) => param)
const query = params.length ? `?${params.join('&')}` : ''
return url.slice(0, queryStart) + query + url.slice(queryEnd)
}
export function normalizeURL(url: string): string {
let normalized: string
try {
normalized = normalizeUrl(url, { stripWWW: false })
normalized = sortQueryParameters(
normalizeUrl(url, { stripWWW: false, sortQueryParameters: false })
)
} catch {
return url.replace(/\s/g, '+')
}
@@ -5,3 +5,5 @@ http://example.com/TVP%20Wilno%20HD/index.m3u8
http://example.com/a%2Fb/index.m3u8
#EXTINF:-1 tvg-id="",Example Query Hash (720p)
http://example.com/index.m3u8?id=1%232
#EXTINF:-1 tvg-id="",Example Query Nested URL (720p)
http://example.com/index.m3u8?name=TV%2B&url=http%3A%2F%2Fexample.org%2Fa.m3u8%3Fk%3D1%26exp%3D2
+3
View File
@@ -67,6 +67,9 @@ describe('playlist:format', () => {
expect(output).toContain('http://example.com/TVP%20Wilno%20HD/index.m3u8')
expect(output).toContain('http://example.com/a%2Fb/index.m3u8')
expect(output).toContain('http://example.com/index.m3u8?id=1%232')
expect(output).toContain(
'http://example.com/index.m3u8?name=TV%2B&url=http%3A%2F%2Fexample.org%2Fa.m3u8%3Fk%3D1%26exp%3D2'
)
})
})