From fe863c455ac0446ab5d27e3ed2a0ab8ceea76ccd Mon Sep 17 00:00:00 2001 From: Ankit Ranjan Date: Fri, 3 Jul 2026 17:44:13 +0530 Subject: [PATCH] Fix Reddit popular HTML response handling --- clis/reddit/popular.js | 13 ++++++++++++- clis/reddit/popular.test.js | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/clis/reddit/popular.js b/clis/reddit/popular.js index 8c938f8..c205eed 100644 --- a/clis/reddit/popular.js +++ b/clis/reddit/popular.js @@ -12,6 +12,7 @@ cli({ ], columns: ['rank', 'id', 'title', 'subreddit', 'score', 'comments', 'author', 'url', 'created_utc', 'selftext', 'post_hint', 'url_overridden_by_dest', 'preview_image_url', 'gallery_urls'], pipeline: [ + { navigate: 'https://www.reddit.com' }, { evaluate: `(async () => { function decodeHtml(s) { if (typeof s !== 'string' || !s) return ''; @@ -43,7 +44,17 @@ cli({ const res = await fetch('/r/popular.json?limit=' + limit + '&raw_json=1', { credentials: 'include' }); - const d = await res.json(); + const text = await res.text(); + if (!res.ok) { + throw new Error('Reddit popular request failed: HTTP ' + res.status + ' ' + text.replace(/\\s+/g, ' ').slice(0, 160)); + } + let d; + try { + d = JSON.parse(text); + } catch { + const kind = /^\\s* ({ id: c.data.id, title: c.data.title, diff --git a/clis/reddit/popular.test.js b/clis/reddit/popular.test.js index 8cbb0ac..aac7673 100644 --- a/clis/reddit/popular.test.js +++ b/clis/reddit/popular.test.js @@ -4,6 +4,8 @@ import './popular.js'; describe('reddit popular adapter', () => { const command = getRegistry().get('reddit/popular'); + const evaluate = command?.pipeline?.find((step) => step.evaluate)?.evaluate; + const map = command?.pipeline?.find((step) => step.map)?.map; it('exposes the full post-list shape including the 4 media columns', () => { expect(command?.columns).toEqual([ @@ -14,13 +16,20 @@ describe('reddit popular adapter', () => { }); it('surfaces media via extractRedditMedia in evaluate + map', () => { - expect(command?.pipeline?.[0]?.evaluate).toContain('function extractRedditMedia'); - expect(command?.pipeline?.[0]?.evaluate).toContain('...extractRedditMedia(c.data)'); - expect(command?.pipeline?.[1]?.map).toMatchObject({ + expect(evaluate).toContain('function extractRedditMedia'); + expect(evaluate).toContain('...extractRedditMedia(c.data)'); + expect(map).toMatchObject({ post_hint: '${{ item.post_hint }}', url_overridden_by_dest: '${{ item.url_overridden_by_dest }}', preview_image_url: '${{ item.preview_image_url }}', gallery_urls: '${{ item.gallery_urls }}', }); }); + + it('navigates to Reddit and guards HTML responses before JSON parsing', () => { + expect(command?.pipeline?.[0]).toEqual({ navigate: 'https://www.reddit.com' }); + expect(evaluate).toContain('await res.text()'); + expect(evaluate).toContain('Reddit popular expected JSON'); + expect(evaluate).not.toContain('await res.json()'); + }); });