From d7c1a01cf698087b1212e815f7211a8076bc0ba2 Mon Sep 17 00:00:00 2001 From: leoca Date: Sat, 14 Nov 2026 03:37:50 +0000 Subject: [PATCH 1/2] fix: keep encoded query delimiters when playlist:format normalizes links normalize-url sorts the query parameters by default and then runs decodeURIComponent on the whole query, so %26, %3D, %3F and %2B come back as &, =, ? and +. A link that passes another URL as a parameter loses part of it to the outer query, and %2B turns into a space. The order of the parameters is now left as is. --- scripts/utils.ts | 2 +- tests/__data__/input/playlist_normalize/reserved_escapes.m3u | 2 ++ tests/commands/playlist/format.test.ts | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/scripts/utils.ts b/scripts/utils.ts index 27b76471af..e5a17110db 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -41,7 +41,7 @@ export function isURI(string: string): boolean { export function normalizeURL(url: string): string { let normalized: string try { - normalized = normalizeUrl(url, { stripWWW: false }) + normalized = normalizeUrl(url, { stripWWW: false, sortQueryParameters: false }) } catch { return url.replace(/\s/g, '+') } diff --git a/tests/__data__/input/playlist_normalize/reserved_escapes.m3u b/tests/__data__/input/playlist_normalize/reserved_escapes.m3u index 592ffd05b6..3e854af934 100644 --- a/tests/__data__/input/playlist_normalize/reserved_escapes.m3u +++ b/tests/__data__/input/playlist_normalize/reserved_escapes.m3u @@ -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 diff --git a/tests/commands/playlist/format.test.ts b/tests/commands/playlist/format.test.ts index c3f9b0b825..a7e4165dca 100644 --- a/tests/commands/playlist/format.test.ts +++ b/tests/commands/playlist/format.test.ts @@ -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' + ) }) }) From ad0ebc21aa694627fb8e50e850b44d983ebdee50 Mon Sep 17 00:00:00 2001 From: leoca Date: Sat, 14 Nov 2026 04:25:14 +0000 Subject: [PATCH 2/2] fix: sort query parameters without decoding their escapes Disabling sortQueryParameters left the parameters in their original order, so the same link written with a different order would no longer be recognized as a duplicate. The parameters are now sorted by name like URLSearchParams.sort() does, but each one is kept as written, so %26, %3D, %3F and %2B are not decoded. --- scripts/utils.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/scripts/utils.ts b/scripts/utils.ts index e5a17110db..400b0b691e 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -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, sortQueryParameters: false }) + normalized = sortQueryParameters( + normalizeUrl(url, { stripWWW: false, sortQueryParameters: false }) + ) } catch { return url.replace(/\s/g, '+') }