From 3fa0bd553b2e9a0872abed36e36d947235905d4d Mon Sep 17 00:00:00 2001 From: sstent Date: Mon, 16 Feb 2026 07:48:32 -0800 Subject: [PATCH] Robust fetch with User-Agent and URL encoding --- .github/workflows/publish.yml | 7 ++----- icalendar.ts | 28 +++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index f64cf73..40f8728 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -22,7 +22,6 @@ jobs: - name: Build Plug run: | - deno --version deno task build -- --no-check ls -lh *.plug.js @@ -30,18 +29,16 @@ jobs: run: | git config --local user.email "action@github.com" git config --local user.name "GitHub Action" - git status - git branch -a git add icalendar.plug.js if git diff --quiet --staged; then echo "No changes to commit" else git commit -m "Build and update icalendar.plug.js [skip ci]" - git push --force + git push fi - name: Upload Build Artifact uses: actions/upload-artifact@v4 with: name: icalendar-plug - path: "*.plug.js" \ No newline at end of file + path: "*.plug.js" diff --git a/icalendar.ts b/icalendar.ts index 995ff76..f9053ad 100644 --- a/icalendar.ts +++ b/icalendar.ts @@ -154,14 +154,32 @@ async function getSources(): Promise { * Fetches and parses events from a single calendar source */ async function fetchAndParseCalendar(source: Source): Promise { - const url = source.url.trim(); + let url = source.url.trim(); + + // Handle internal spaces which are common in copy-pasted URLs + if (url.includes(" ")) { + console.log(`[iCalendar] URL contains spaces, encoding: "${url}"`); + url = encodeURI(url); + } + console.log(`[iCalendar] Fetching: ${url}`); - const response = await fetch(url); + + const response = await fetch(url, { + headers: { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" + } + }); if (!response.ok) { - const error = new Error(`HTTP ${response.status}: ${response.statusText}`); - console.error(`[iCalendar] HTTP error:`, { source, status: response.status, statusText: response.statusText }); - throw error; + const body = await response.text(); + const errorDetail = body.slice(0, 200).replace(/\n/g, " "); + console.error(`[iCalendar] HTTP ${response.status} Error:`, { + url, + status: response.status, + statusText: response.statusText, + bodyPreview: errorDetail + }); + throw new Error(`HTTP ${response.status}: ${response.statusText || 'Error'} - ${errorDetail}`); } const icsData = await response.text();