Spaces:
Paused
Paused
Resilience: timeouts on tvchak paths, frontend boot fail-safe, stale domain fallback
Browse files- index.html +23 -10
- server.js +44 -13
index.html
CHANGED
|
@@ -714,10 +714,18 @@ const esc = s => String(s).replace(/[&<>"']/g, c => ({'&':'&','<':'<','>'
|
|
| 714 |
const isMobile = () => matchMedia('(max-width: 1023.98px)').matches;
|
| 715 |
|
| 716 |
// โโโ API โโโ
|
| 717 |
-
async function api(path) {
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 721 |
}
|
| 722 |
|
| 723 |
// โโโ ๋๋ฉ์ธ ํ์ โโโ
|
|
@@ -2121,15 +2129,20 @@ async function togglePip() {
|
|
| 2121 |
}
|
| 2122 |
|
| 2123 |
(async function boot() {
|
|
|
|
| 2124 |
renderProfileTabs();
|
| 2125 |
renderFavs();
|
| 2126 |
-
|
| 2127 |
loadRanking('daily');
|
| 2128 |
-
|
| 2129 |
-
|
| 2130 |
-
|
| 2131 |
-
|
| 2132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2133 |
})();
|
| 2134 |
</script>
|
| 2135 |
</body>
|
|
|
|
| 714 |
const isMobile = () => matchMedia('(max-width: 1023.98px)').matches;
|
| 715 |
|
| 716 |
// โโโ API โโโ
|
| 717 |
+
async function api(path, opts = {}) {
|
| 718 |
+
// ๊ธฐ๋ณธ 60์ด timeout (HF Spaces ์๋ต ๋๋ฆด ๋ ๋๋น). hang ๋ฐฉ์ง.
|
| 719 |
+
const timeoutMs = opts.timeout || 60000;
|
| 720 |
+
const ctrl = new AbortController();
|
| 721 |
+
const t = setTimeout(() => ctrl.abort(), timeoutMs);
|
| 722 |
+
try {
|
| 723 |
+
const r = await fetch(path, { cache: 'no-store', signal: ctrl.signal });
|
| 724 |
+
if (!r.ok) throw new Error('HTTP ' + r.status);
|
| 725 |
+
return await r.json();
|
| 726 |
+
} finally {
|
| 727 |
+
clearTimeout(t);
|
| 728 |
+
}
|
| 729 |
}
|
| 730 |
|
| 731 |
// โโโ ๋๋ฉ์ธ ํ์ โโโ
|
|
|
|
| 2129 |
}
|
| 2130 |
|
| 2131 |
(async function boot() {
|
| 2132 |
+
// ์ด๋ ๋จ๊ณ๊ฐ ์คํจํด๋ UI๋ ๋ฌด์กฐ๊ฑด ๊ทธ๋ ค์ง๋๋ก try/catch ๋ณดํธ.
|
| 2133 |
renderProfileTabs();
|
| 2134 |
renderFavs();
|
| 2135 |
+
loadDomain().catch(e => console.warn('boot loadDomain:', e));
|
| 2136 |
loadRanking('daily');
|
| 2137 |
+
try {
|
| 2138 |
+
await pullFavsFromServer();
|
| 2139 |
+
renderFavs();
|
| 2140 |
+
} catch (e) { console.warn('boot pullFavs:', e); }
|
| 2141 |
+
try {
|
| 2142 |
+
if (lastFavId && favs.find(f => f.id == lastFavId)) {
|
| 2143 |
+
await selectFav(+lastFavId, { silent: true });
|
| 2144 |
+
}
|
| 2145 |
+
} catch (e) { console.warn('boot selectFav:', e); }
|
| 2146 |
})();
|
| 2147 |
</script>
|
| 2148 |
</body>
|
server.js
CHANGED
|
@@ -309,17 +309,21 @@ async function ensureTvPage(domain) {
|
|
| 309 |
}
|
| 310 |
|
| 311 |
async function tvFetch(domain, pathOnSite) {
|
| 312 |
-
// ์ต๋ 2๋ฒ ์๋ (ํ์ด์ง๊ฐ ๊ด๊ณ ๋ก ๋ ๋ ๊ฒฝ์ฐ ์ฌ์ด๊ธฐํ)
|
| 313 |
for (let attempt = 0; attempt < 2; attempt++) {
|
| 314 |
const page = await ensureTvPage(domain);
|
| 315 |
try {
|
| 316 |
-
const result = await
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
| 320 |
return { ok: true, ...result };
|
| 321 |
} catch (e) {
|
| 322 |
-
// ์ปจํ
์คํธ ํ๊ดด
|
|
|
|
| 323 |
_tvPage = null;
|
| 324 |
if (attempt === 1) return { ok: false, error: String(e.message || e) };
|
| 325 |
}
|
|
@@ -670,9 +674,29 @@ async function _aniPlayInternal(uniqueId) {
|
|
| 670 |
|
| 671 |
// โโโโโ ๋๋ฉ์ธ ํ์ง โโโโโ
|
| 672 |
async function findLiveDomain() {
|
|
|
|
| 673 |
const cached = readCache('domain.json', 3600);
|
| 674 |
if (cached && cached.domain) return cached;
|
| 675 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 676 |
const ctx = await getContext();
|
| 677 |
const p = await ctx.newPage();
|
| 678 |
let domain = null;
|
|
@@ -690,8 +714,13 @@ async function findLiveDomain() {
|
|
| 690 |
} finally {
|
| 691 |
await p.close().catch(() => {});
|
| 692 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 693 |
const result = { domain, announced: domain, source: 'tvchak.org', checked_at: new Date().toISOString() };
|
| 694 |
-
|
|
|
|
| 695 |
return result;
|
| 696 |
}
|
| 697 |
|
|
@@ -709,12 +738,12 @@ async function fetchEpisodes(id, opts = {}) {
|
|
| 709 |
const d = await findLiveDomain();
|
| 710 |
if (!d.domain) return { error: 'no live domain' };
|
| 711 |
|
| 712 |
-
// DOM ํ์ฑ ๋ฐฉ์ (๋ผ๋ฒจ ํ
์คํธ๊น์ง ์ ํํ ์ถ์ถ)
|
| 713 |
let result = null;
|
| 714 |
for (let attempt = 0; attempt < 2; attempt++) {
|
| 715 |
const page = await ensureTvPage(d.domain);
|
| 716 |
try {
|
| 717 |
-
const r = await page.evaluate(async (params) => {
|
| 718 |
const resp = await fetch(`/index.php/vod/detail/id/${params.id}.html`, { credentials: 'include' });
|
| 719 |
if (!resp.ok) return { error: 'HTTP ' + resp.status };
|
| 720 |
const html = await resp.text();
|
|
@@ -744,7 +773,7 @@ async function fetchEpisodes(id, opts = {}) {
|
|
| 744 |
eps.push({ sid, nid, label });
|
| 745 |
}
|
| 746 |
return { title, episodes: eps };
|
| 747 |
-
}, { id });
|
| 748 |
if (r.error) {
|
| 749 |
if (attempt === 1) return { error: r.error };
|
| 750 |
continue;
|
|
@@ -752,6 +781,7 @@ async function fetchEpisodes(id, opts = {}) {
|
|
| 752 |
result = r;
|
| 753 |
break;
|
| 754 |
} catch (e) {
|
|
|
|
| 755 |
_tvPage = null;
|
| 756 |
if (attempt === 1) return { error: String(e.message || e) };
|
| 757 |
}
|
|
@@ -1126,12 +1156,12 @@ async function searchSite(wd) {
|
|
| 1126 |
if (!d.domain) return { error: 'no live domain' };
|
| 1127 |
|
| 1128 |
// ๊ฐ์ origin DOM ํ์ฑ์ด ํ์ โ page์์ fetch + DOMParser
|
| 1129 |
-
// ๊ด๊ณ navigate๋ก ์ปจํ
์คํธ๊ฐ ํ๊ดด๋ ์ ์์ด retry
|
| 1130 |
let items = null;
|
| 1131 |
for (let attempt = 0; attempt < 2; attempt++) {
|
| 1132 |
const page = await ensureTvPage(d.domain);
|
| 1133 |
try {
|
| 1134 |
-
items = await page.evaluate(async (sUrl) => {
|
| 1135 |
const r = await fetch(sUrl, { credentials: 'include' });
|
| 1136 |
if (!r.ok) return { error: 'HTTP ' + r.status };
|
| 1137 |
const html = await r.text();
|
|
@@ -1153,9 +1183,10 @@ async function searchSite(wd) {
|
|
| 1153 |
out.push({ id, title: lines[0] || '', meta: lines[1] || '', href });
|
| 1154 |
}
|
| 1155 |
return out;
|
| 1156 |
-
}, `/index.php/vod/search.html?wd=${encodeURIComponent(wd)}`);
|
| 1157 |
break;
|
| 1158 |
} catch (e) {
|
|
|
|
| 1159 |
_tvPage = null; // ํ์ด์ง ์ฌ์์ฑ ๊ฐ์
|
| 1160 |
if (attempt === 1) return { error: String(e.message || e) };
|
| 1161 |
}
|
|
|
|
| 309 |
}
|
| 310 |
|
| 311 |
async function tvFetch(domain, pathOnSite) {
|
| 312 |
+
// ์ต๋ 2๋ฒ ์๋ (ํ์ด์ง๊ฐ ๊ด๊ณ ๋ก ๋ ๋ ๊ฒฝ์ฐ ์ฌ์ด๊ธฐํ). ๊ฐ ํธ์ถ์ timeout์ผ๋ก hang ๋ฐฉ์ง.
|
| 313 |
for (let attempt = 0; attempt < 2; attempt++) {
|
| 314 |
const page = await ensureTvPage(domain);
|
| 315 |
try {
|
| 316 |
+
const result = await withTimeout(
|
| 317 |
+
page.evaluate(async (p) => {
|
| 318 |
+
const r = await fetch(p, { credentials: 'include' });
|
| 319 |
+
return { status: r.status, body: await r.text() };
|
| 320 |
+
}, pathOnSite),
|
| 321 |
+
15000, `tvFetch ${pathOnSite}`
|
| 322 |
+
);
|
| 323 |
return { ok: true, ...result };
|
| 324 |
} catch (e) {
|
| 325 |
+
// ์ปจํ
์คํธ ํ๊ดด/hang โ ๋ค์ ๋ง๋ค๊ณ ์ฌ์๋
|
| 326 |
+
try { if (_tvPage) await _tvPage.close().catch(() => {}); } catch {}
|
| 327 |
_tvPage = null;
|
| 328 |
if (attempt === 1) return { ok: false, error: String(e.message || e) };
|
| 329 |
}
|
|
|
|
| 674 |
|
| 675 |
// โโโโโ ๋๋ฉ์ธ ํ์ง โโโโโ
|
| 676 |
async function findLiveDomain() {
|
| 677 |
+
// ์ด์์๋ ๋๋ฉ์ธ ์บ์๋ 1์๊ฐ ์ ์ง. domain์ด null์ธ ์บ์๋ ์ ๋ขฐ X (์ฌ์๋).
|
| 678 |
const cached = readCache('domain.json', 3600);
|
| 679 |
if (cached && cached.domain) return cached;
|
| 680 |
|
| 681 |
+
// stale (์ด์ ์ ๋ฐ์๋ ๋๋ฉ์ธ) โ ์ ์๋ ์คํจํด๋ ์ตํ fallback์ผ๋ก ์ฌ์ฉ
|
| 682 |
+
const stale = readCachePermanent('domain.json');
|
| 683 |
+
|
| 684 |
+
try {
|
| 685 |
+
return await withTimeout(_findLiveDomainImpl(stale), 25000, 'findLiveDomain');
|
| 686 |
+
} catch (e) {
|
| 687 |
+
console.warn('findLiveDomain timeout:', e.message);
|
| 688 |
+
// tvchak ํธ์ถ์ด hangํ ๊ฑฐ๋๊น _tvPage ์ฌ์์ฑํ๋๋ก reset
|
| 689 |
+
try { if (_tvPage) await _tvPage.close().catch(() => {}); } catch {}
|
| 690 |
+
_tvPage = null;
|
| 691 |
+
// stale ์์ผ๋ฉด ๊ทธ๊ฑฐ๋ผ๋ ๋ฐํ (๋น ํ๋ฉด ๋ฐฉ์ง)
|
| 692 |
+
if (stale && stale.domain) {
|
| 693 |
+
return { ...stale, stale: true };
|
| 694 |
+
}
|
| 695 |
+
return { domain: null, error: e.message, source: 'tvchak.org', checked_at: new Date().toISOString() };
|
| 696 |
+
}
|
| 697 |
+
}
|
| 698 |
+
|
| 699 |
+
async function _findLiveDomainImpl(stale) {
|
| 700 |
const ctx = await getContext();
|
| 701 |
const p = await ctx.newPage();
|
| 702 |
let domain = null;
|
|
|
|
| 714 |
} finally {
|
| 715 |
await p.close().catch(() => {});
|
| 716 |
}
|
| 717 |
+
// ์๋ก ํ์ง ์คํจํ๊ณ stale ์์ผ๋ฉด stale ์ฌ์ฉ (null ์บ์ ์ ์ฅ ์ ํจ)
|
| 718 |
+
if (!domain && stale && stale.domain) {
|
| 719 |
+
return { ...stale, stale: true };
|
| 720 |
+
}
|
| 721 |
const result = { domain, announced: domain, source: 'tvchak.org', checked_at: new Date().toISOString() };
|
| 722 |
+
// ์ฑ๊ณตํ ๊ฒฝ์ฐ๋ง ์บ์ ์ ์ฅ (null ๋ฎ์ด์ฐ๊ธฐ ๋ฐฉ์ง)
|
| 723 |
+
if (domain) writeCache('domain.json', result);
|
| 724 |
return result;
|
| 725 |
}
|
| 726 |
|
|
|
|
| 738 |
const d = await findLiveDomain();
|
| 739 |
if (!d.domain) return { error: 'no live domain' };
|
| 740 |
|
| 741 |
+
// DOM ํ์ฑ ๋ฐฉ์ (๋ผ๋ฒจ ํ
์คํธ๊น์ง ์ ํํ ์ถ์ถ). hang ๋ฐฉ์ง timeout.
|
| 742 |
let result = null;
|
| 743 |
for (let attempt = 0; attempt < 2; attempt++) {
|
| 744 |
const page = await ensureTvPage(d.domain);
|
| 745 |
try {
|
| 746 |
+
const r = await withTimeout(page.evaluate(async (params) => {
|
| 747 |
const resp = await fetch(`/index.php/vod/detail/id/${params.id}.html`, { credentials: 'include' });
|
| 748 |
if (!resp.ok) return { error: 'HTTP ' + resp.status };
|
| 749 |
const html = await resp.text();
|
|
|
|
| 773 |
eps.push({ sid, nid, label });
|
| 774 |
}
|
| 775 |
return { title, episodes: eps };
|
| 776 |
+
}, { id }), 15000, `fetchEpisodes ${id}`);
|
| 777 |
if (r.error) {
|
| 778 |
if (attempt === 1) return { error: r.error };
|
| 779 |
continue;
|
|
|
|
| 781 |
result = r;
|
| 782 |
break;
|
| 783 |
} catch (e) {
|
| 784 |
+
try { if (_tvPage) await _tvPage.close().catch(() => {}); } catch {}
|
| 785 |
_tvPage = null;
|
| 786 |
if (attempt === 1) return { error: String(e.message || e) };
|
| 787 |
}
|
|
|
|
| 1156 |
if (!d.domain) return { error: 'no live domain' };
|
| 1157 |
|
| 1158 |
// ๊ฐ์ origin DOM ํ์ฑ์ด ํ์ โ page์์ fetch + DOMParser
|
| 1159 |
+
// ๊ด๊ณ navigate๋ก ์ปจํ
์คํธ๊ฐ ํ๊ดด๋ ์ ์์ด retry + timeout
|
| 1160 |
let items = null;
|
| 1161 |
for (let attempt = 0; attempt < 2; attempt++) {
|
| 1162 |
const page = await ensureTvPage(d.domain);
|
| 1163 |
try {
|
| 1164 |
+
items = await withTimeout(page.evaluate(async (sUrl) => {
|
| 1165 |
const r = await fetch(sUrl, { credentials: 'include' });
|
| 1166 |
if (!r.ok) return { error: 'HTTP ' + r.status };
|
| 1167 |
const html = await r.text();
|
|
|
|
| 1183 |
out.push({ id, title: lines[0] || '', meta: lines[1] || '', href });
|
| 1184 |
}
|
| 1185 |
return out;
|
| 1186 |
+
}, `/index.php/vod/search.html?wd=${encodeURIComponent(wd)}`), 15000, `searchSite ${wd}`);
|
| 1187 |
break;
|
| 1188 |
} catch (e) {
|
| 1189 |
+
try { if (_tvPage) await _tvPage.close().catch(() => {}); } catch {}
|
| 1190 |
_tvPage = null; // ํ์ด์ง ์ฌ์์ฑ ๊ฐ์
|
| 1191 |
if (attempt === 1) return { error: String(e.message || e) };
|
| 1192 |
}
|