From c5e24196b1e74b7aaed5e91efbaef54787ec068b Mon Sep 17 00:00:00 2001 From: chen zhihao <2490284538@qq.com> Date: Fri, 4 Sep 2026 23:13:08 +0800 Subject: [PATCH] fix: keep malformed URLs during normalization (#49870) --- scripts/utils.ts | 13 +++++++++++-- .../input/playlist_normalize/bad_encoding.m3u | 3 +++ tests/commands/playlist/format.test.ts | 13 +++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 tests/__data__/input/playlist_normalize/bad_encoding.m3u diff --git a/scripts/utils.ts b/scripts/utils.ts index 6e31eafc9b..094d4dd3e0 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -39,9 +39,18 @@ export function isURI(string: string): boolean { } export function normalizeURL(url: string): string { - const normalized = normalizeUrl(url, { stripWWW: false }) + let normalized: string + try { + normalized = normalizeUrl(url, { stripWWW: false }) + } catch { + return url.replace(/\s/g, '+') + } - return decodeURIComponent(normalized).replace(/\s/g, '+').toString() + try { + return decodeURIComponent(normalized).replace(/\s/g, '+') + } catch { + return normalized.replace(/\s/g, '+') + } } export function truncate(string: string, limit: number = 100) { diff --git a/tests/__data__/input/playlist_normalize/bad_encoding.m3u b/tests/__data__/input/playlist_normalize/bad_encoding.m3u new file mode 100644 index 0000000000..4d3f26af70 --- /dev/null +++ b/tests/__data__/input/playlist_normalize/bad_encoding.m3u @@ -0,0 +1,3 @@ +#EXTM3U +#EXTINF:-1 tvg-id="",Example (720p) +http://example.com/a%ZZ diff --git a/tests/commands/playlist/format.test.ts b/tests/commands/playlist/format.test.ts index f169322d84..dd36971028 100644 --- a/tests/commands/playlist/format.test.ts +++ b/tests/commands/playlist/format.test.ts @@ -30,6 +30,19 @@ describe('playlist:format', () => { ) }) }) + + it('keeps links with malformed percent escapes', () => { + fs.emptyDirSync('tests/__data__/output') + fs.copySync('tests/__data__/input/playlist_normalize', 'tests/__data__/output/streams') + + const cmd = + 'cross-env STREAMS_DIR=tests/__data__/output/streams DATA_DIR=tests/__data__/input/data npm run playlist:format' + execSync(cmd, { encoding: 'utf8' }) + + expect(content('tests/__data__/output/streams/bad_encoding.m3u')).toContain( + 'http://example.com/a%ZZ' + ) + }) }) function content(filepath: string) {