refactor: dedupe github latest release fetch wiring (#1704)

This commit is contained in:
Elie Habib
2026-03-16 08:59:33 +04:00
committed by GitHub
parent fb60f308af
commit 463831625b
3 changed files with 20 additions and 23 deletions

12
api/_github-release.js Normal file
View File

@@ -0,0 +1,12 @@
const RELEASES_URL = 'https://api.github.com/repos/koala73/worldmonitor/releases/latest';
export async function fetchLatestRelease(userAgent) {
const res = await fetch(RELEASES_URL, {
headers: {
'Accept': 'application/vnd.github+json',
'User-Agent': userAgent,
},
});
if (!res.ok) return null;
return res.json();
}

View File

@@ -1,7 +1,8 @@
import { fetchLatestRelease } from './_github-release.js';
// Non-sebuf: returns XML/HTML, stays as standalone Vercel function
export const config = { runtime: 'edge' };
const RELEASES_URL = 'https://api.github.com/repos/koala73/worldmonitor/releases/latest';
const RELEASES_PAGE = 'https://github.com/koala73/worldmonitor/releases/latest';
const PLATFORM_PATTERNS = {
@@ -48,18 +49,10 @@ export default async function handler(req) {
}
try {
const res = await fetch(RELEASES_URL, {
headers: {
'Accept': 'application/vnd.github+json',
'User-Agent': 'WorldMonitor-Download-Redirect',
},
});
if (!res.ok) {
const release = await fetchLatestRelease('WorldMonitor-Download-Redirect');
if (!release) {
return Response.redirect(RELEASES_PAGE, 302);
}
const release = await res.json();
const matcher = PLATFORM_PATTERNS[platform];
const assets = Array.isArray(release.assets) ? release.assets : [];
const asset = variant

View File

@@ -1,25 +1,17 @@
import { fetchLatestRelease } from './_github-release.js';
// Non-sebuf: returns XML/HTML, stays as standalone Vercel function
export const config = { runtime: 'edge' };
const RELEASES_URL = 'https://api.github.com/repos/koala73/worldmonitor/releases/latest';
export default async function handler() {
try {
const res = await fetch(RELEASES_URL, {
headers: {
'Accept': 'application/vnd.github+json',
'User-Agent': 'WorldMonitor-Version-Check',
},
});
if (!res.ok) {
const release = await fetchLatestRelease('WorldMonitor-Version-Check');
if (!release) {
return new Response(JSON.stringify({ error: 'upstream' }), {
status: 502,
headers: { 'Content-Type': 'application/json' },
});
}
const release = await res.json();
const tag = release.tag_name ?? '';
const version = tag.replace(/^v/, '');