Spaces:
Paused

dongsik22 commited on
Commit
7c1452d
ยท
1 Parent(s): 4d2b191

Resilience: timeouts on tvchak paths, frontend boot fail-safe, stale domain fallback

Browse files
Files changed (2) hide show
  1. index.html +23 -10
  2. server.js +44 -13
index.html CHANGED
@@ -714,10 +714,18 @@ const esc = s => String(s).replace(/[&<>"']/g, c => ({'&':'&amp;','<':'&lt;','>'
714
  const isMobile = () => matchMedia('(max-width: 1023.98px)').matches;
715
 
716
  // โ”€โ”€โ”€ API โ”€โ”€โ”€
717
- async function api(path) {
718
- const r = await fetch(path, { cache: 'no-store' });
719
- if (!r.ok) throw new Error('HTTP ' + r.status);
720
- return r.json();
 
 
 
 
 
 
 
 
721
  }
722
 
723
  // โ”€โ”€โ”€ ๋„๋ฉ”์ธ ํ‘œ์‹œ โ”€โ”€โ”€
@@ -2121,15 +2129,20 @@ async function togglePip() {
2121
  }
2122
 
2123
  (async function boot() {
 
2124
  renderProfileTabs();
2125
  renderFavs();
2126
- await loadDomain();
2127
  loadRanking('daily');
2128
- await pullFavsFromServer();
2129
- renderFavs();
2130
- if (lastFavId && favs.find(f => f.id == lastFavId)) {
2131
- await selectFav(+lastFavId, { silent: true });
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 page.evaluate(async (p) => {
317
- const r = await fetch(p, { credentials: 'include' });
318
- return { status: r.status, body: await r.text() };
319
- }, pathOnSite);
 
 
 
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
- writeCache('domain.json', result);
 
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
  }