Update src/libpoketube/libpoketube-core.js

This commit is contained in:
ashley 2025-08-18 01:06:49 +02:00
parent 4e0f05b856
commit 40f60a8490

View File

@ -10,7 +10,7 @@ const { toJson } = require("xml2json");
const { curly } = require("node-libcurl"); const { curly } = require("node-libcurl");
const getdislikes = require("../libpoketube/libpoketube-dislikes.js"); const getdislikes = require("../libpoketube/libpoketube-dislikes.js");
const getColors = require("get-image-colors"); const getColors = require("get-image-colors");
const config = require("../../config.json"); const config = require("../../config.json")
class InnerTubePokeVidious { class InnerTubePokeVidious {
constructor(config) { constructor(config) {
@ -27,34 +27,6 @@ class InnerTubePokeVidious {
this.INNERTUBE_CONTEXT_CLIENT_VERSION = "1"; this.INNERTUBE_CONTEXT_CLIENT_VERSION = "1";
this.region = "region=US"; this.region = "region=US";
this.sqp = "-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw"; this.sqp = "-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw";
// Lazy-initialized undici pieces for connection reuse + pipelining
this._fetch = null;
this._agent = null;
this._undiciInit = null;
// Small LRU-ish caches to avoid redundant work within the same process lifespan
this._channelCache = new Map(); // key: authorId -> {data, ts}
this._commentsCache = new Map(); // key: videoId -> {data, ts}
}
async _ensureUndici() {
if (this._undiciInit) return this._undiciInit;
this._undiciInit = (async () => {
const { fetch, Agent, setGlobalDispatcher } = await import("undici");
// Aggressive keep-alive + pipelining to cut handshake latency
const agent = new Agent({
keepAliveTimeout: 60_000,
keepAliveMaxTimeout: 60_000,
connections: 16,
pipelining: 1, // keep conservative; many APIs dislike >1
maxRedirections: 2
});
setGlobalDispatcher(agent);
this._fetch = fetch;
this._agent = agent;
})();
return this._undiciInit;
} }
getJson(str) { getJson(str) {
@ -73,256 +45,158 @@ class InnerTubePokeVidious {
return new Promise(r => setTimeout(r, ms)); return new Promise(r => setTimeout(r, ms));
} }
// Faster, low-tail decorrelated jitter backoff backoffDelay(attempt, base = 160, cap = 12000) {
// See AWS "Exponential Backoff And Jitter" — decorrelated variant const exp = Math.min(cap, base * Math.pow(2, attempt));
backoffDelay(attempt, base = 80, cap = 2000) { const jitter = Math.floor(Math.random() * (base + 1));
const prev = attempt === 0 ? base : Math.min(cap, base * Math.pow(2, attempt - 1)); return Math.min(cap, exp + jitter);
const next = Math.min(cap, base + Math.floor(Math.random() * (prev * 3)));
return next;
} }
shouldRetryStatus(status) { shouldRetryStatus(status) {
if (!status) return true; if (!status) return true;
if (status === 408 || status === 425 || status === 429) return true; if (status === 408 || status === 425 || status === 429) return true;
if (status >= 500 && status <= 599) return true; if (status >= 500 && status <= 599) return true;
// hard no-retry statuses
if (status === 400 || status === 401 || status === 403 || status === 404 || status === 409 || status === 410) return false;
return false; return false;
} }
_mergeHeaders(a = {}, b = {}) {
const o = { ...a };
for (const k of Object.keys(b)) {
o[k] = b[k];
}
return o;
}
// Ultra-lean fetch with:
// - connection reuse (undici agent)
// - short per-attempt timeouts (attempt-scaled)
// - decorrelated jitter backoff
// - minimal retries by default, but smart on 429/5xx
async fetchWithRetry(url, options = {}, cfg = {}) { async fetchWithRetry(url, options = {}, cfg = {}) {
await this._ensureUndici(); const { fetch } = await import("undici");
const fetch = this._fetch; const maxRetries = Number.isInteger(cfg.retries) ? Math.max(0, cfg.retries) : 8;
const baseDelay = cfg.baseDelay ?? 160;
const maxRetries = Number.isInteger(cfg.retries) ? Math.max(0, cfg.retries) : 5; const maxDelay = cfg.maxDelay ?? 12000;
const baseDelay = cfg.baseDelay ?? 80; const perAttemptTimeout = cfg.timeout ?? 12000;
const maxDelay = cfg.maxDelay ?? 2000;
const hardCapMs = cfg.hardCapMs ?? 8000; // total time budget
const extraRetryOn = cfg.retryOnStatuses || []; const extraRetryOn = cfg.retryOnStatuses || [];
const started = Date.now();
const uah = { const uah = {
"User-Agent": this.useragent, "User-Agent": this.useragent,
"Accept": "application/json, text/plain, */*",
"Accept-Encoding": "gzip, deflate, br"
}; };
let lastErr = null; let lastErr = null;
for (let attempt = 0; attempt <= maxRetries; attempt++) { for (let attempt = 0; attempt <= maxRetries; attempt++) {
const elapsed = Date.now() - started;
if (elapsed >= hardCapMs) break;
const perAttemptBudget = Math.max(500, Math.min(2500, hardCapMs - elapsed));
const ac = new AbortController(); const ac = new AbortController();
const tm = setTimeout(() => ac.abort(new Error("timeout")), perAttemptBudget); const t = setTimeout(() => ac.abort(new Error("timeout")), perAttemptTimeout);
try { try {
const res = await fetch(url, { const res = await fetch(url, {
...options, ...options,
signal: ac.signal, signal: ac.signal,
headers: this._mergeHeaders(options.headers, uah) headers: { ...(options.headers || {}), ...uah }
}); });
clearTimeout(tm); clearTimeout(t);
if (res.ok) return res; if (res.ok) return res;
const should = this.shouldRetryStatus(res.status) || extraRetryOn.includes(res.status); const should = this.shouldRetryStatus(res.status) || extraRetryOn.includes(res.status);
if (!should || attempt === maxRetries) return res; if (!should || attempt === maxRetries) return res;
} catch (e) { } catch (e) {
clearTimeout(tm); clearTimeout(t);
lastErr = e; lastErr = e;
if (attempt === maxRetries) throw e; if (attempt === maxRetries) throw e;
} }
await this.wait(this.backoffDelay(attempt, baseDelay, maxDelay));
const backoff = this.backoffDelay(attempt, baseDelay, maxDelay);
const remain = hardCapMs - (Date.now() - started);
if (remain <= 0) break;
await this.wait(Math.min(backoff, Math.max(0, remain - 50)));
} }
if (lastErr) throw lastErr; if (lastErr) throw lastErr;
throw new Error("fetchWithRetry failed"); throw new Error("fetchWithRetry failed");
} }
// Hedged requests with early secondary, winner-takes-all, loser aborted.
// Cuts tail latency when a backend is slow/spotty.
async hedgedGetJsonFromBases(bases, path, query) { async hedgedGetJsonFromBases(bases, path, query) {
await this._ensureUndici();
const fetch = this._fetch;
const qs = query ? (query.startsWith("?") ? query : "?" + query) : ""; const qs = query ? (query.startsWith("?") ? query : "?" + query) : "";
const primary = `${bases[0]}${path}${qs}`; const primary = `${bases[0]}${path}${qs}`;
const secondary = bases[1] ? `${bases[1]}${path}${qs}` : null; const secondary = bases[1] ? `${bases[1]}${path}${qs}` : null;
const attemptOnce = async (url) => {
const headers = { const r = await this.fetchWithRetry(url, {}, { retries: 4, baseDelay: 120, maxDelay: 6000, timeout: 10000 });
"User-Agent": this.useragent, const tx = await r.text();
"Accept": "application/json, text/plain, */*",
"Accept-Encoding": "gzip, deflate, br"
};
const attemptOnce = async (url, signal) => {
const res = await this.fetchWithRetry(
url,
{ headers, signal },
{ retries: 3, baseDelay: 80, maxDelay: 1200, hardCapMs: 4000 }
);
const tx = await res.text();
return this.getJson(tx); return this.getJson(tx);
}; };
if (!secondary) return attemptOnce(primary); if (!secondary) return attemptOnce(primary);
let winner;
const acPrimary = new AbortController(); let errorPrimary, errorSecondary;
const acSecondary = new AbortController(); const delayedSecondary = (async () => {
await this.wait(300);
// Fire secondary quickly (200ms) to hedge return attemptOnce(secondary);
const secondaryKick = (async () => {
await this.wait(200);
if (!acPrimary.signal.aborted) {
return attemptOnce(secondary, acSecondary.signal);
}
throw new Error("secondary canceled");
})(); })();
const primaryP = attemptOnce(primary);
const primaryP = attemptOnce(primary, acPrimary.signal);
try { try {
const winner = await Promise.race([ winner = await Promise.any([primaryP, delayedSecondary]);
primaryP.then(v => ({ v, who: "p" })),
secondaryKick.then(v => ({ v, who: "s" }))
]);
// Abort the loser ASAP
if (winner.who === "p") acSecondary.abort();
else acPrimary.abort();
if (winner.v) return winner.v;
// Fallback: await the other if the winner returned null-ish
const other = winner.who === "p" ? secondaryKick : primaryP;
const v2 = await other.catch(() => null);
return v2 || null;
} catch { } catch {
// Final fallback: try primary once more, fast budget
try { try {
return await attemptOnce(primary, acPrimary.signal); const a = await primaryP;
} catch { if (a) return a;
try { } catch (e) {
return await attemptOnce(secondary, acSecondary.signal); errorPrimary = e;
} catch {
return null;
}
} }
} finally { try {
acPrimary.abort(); const b = await delayedSecondary;
acSecondary.abort(); if (b) return b;
} catch (e) {
errorSecondary = e;
}
if (errorPrimary) throw errorPrimary;
if (errorSecondary) throw errorSecondary;
return null;
} }
return winner;
} }
async getColorsSafe(url) { async getColorsSafe(url) {
for (let i = 0; i < 2; i++) { for (let i = 0; i < 3; i++) {
try { try {
const c = await getColors(url); const c = await getColors(url);
if (Array.isArray(c) && c[0] && c[1]) return [c[0].hex(), c[1].hex()]; if (Array.isArray(c) && c[0] && c[1]) return [c[0].hex(), c[1].hex()];
} catch {} } catch {}
await this.wait(this.backoffDelay(i, 80, 600)); await this.wait(this.backoffDelay(i, 120, 4000));
} }
return ["#0ea5e9", "#111827"]; return ["#0ea5e9", "#111827"];
} }
// Fast libcurl retry: fewer attempts, tighter timeouts, early exit on non-retryable
async curlGetWithRetry(url, httpHeader) { async curlGetWithRetry(url, httpHeader) {
let lastErr = null; let lastErr = null;
for (let i = 0; i <= 3; i++) { for (let i = 0; i <= 4; i++) {
try { try {
const res = await curly.get(url, { httpHeader, timeoutMs: 7000, acceptEncoding: "gzip, deflate, br" }); const res = await curly.get(url, { httpHeader, timeoutMs: 12000 });
if (res && res.statusCode && res.statusCode >= 200 && res.statusCode < 300 && res.data) return res; if (res && res.statusCode && res.statusCode >= 200 && res.statusCode < 300 && res.data) return res;
if (res && res.statusCode && !this.shouldRetryStatus(res.statusCode)) return res; if (res && res.statusCode && !this.shouldRetryStatus(res.statusCode)) return res;
} catch (e) { } catch (e) {
lastErr = e; lastErr = e;
} }
await this.wait(this.backoffDelay(i, 80, 1500)); await this.wait(this.backoffDelay(i, 160, 8000));
} }
if (lastErr) throw lastErr; if (lastErr) throw lastErr;
throw new Error("curlGetWithRetry failed"); throw new Error("curlGetWithRetry failed");
} }
async getYouTubeApiVideo(f, v, contentlang, contentregion) { async getYouTubeApiVideo(f, v, contentlang, contentregion) {
const { fetch } = await import("undici");
if (v == null) return "Gib ID"; if (v == null) return "Gib ID";
// 1h video-level cache
if (this.cache[v] && Date.now() - this.cache[v].timestamp < 3600000) { if (this.cache[v] && Date.now() - this.cache[v].timestamp < 3600000) {
return this.cache[v].result; return this.cache[v].result;
} }
const headers = {
const headersArr = Object.entries({
"User-Agent": this.useragent, "User-Agent": this.useragent,
"Accept": "application/json, text/plain, */*", };
"Accept-Encoding": "gzip, deflate, br"
}).map(([k, v]) => `${k}: ${v}`);
const bases = [this.config.invapi, this.config.invapi_alt]; const bases = [this.config.invapi, this.config.invapi_alt];
const b64ts = Buffer.from(String(Date.now())).toString("base64"); const b64ts = Buffer.from(String(Date.now())).toString("base64");
const q = `hl=${contentlang}&region=${contentregion}&h=${b64ts}`; const q = `hl=${contentlang}&region=${contentregion}&h=${b64ts}`;
try { try {
const [comments, vid, videoData] = await Promise.all([ const [comments, vid, videoData] = await Promise.all([
(async () => { this.hedgedGetJsonFromBases(bases, `/comments/${v}`, q),
const hit = this._commentsCache.get(v);
if (hit && Date.now() - hit.ts < 300_000) return hit.data;
const data = await this.hedgedGetJsonFromBases(bases, `/comments/${v}`, q);
this._commentsCache.set(v, { data, ts: Date.now() });
return data;
})(),
this.hedgedGetJsonFromBases(bases, `/videos/${v}`, q), this.hedgedGetJsonFromBases(bases, `/videos/${v}`, q),
(async () => { (async () => {
const res = await this.curlGetWithRetry(`${this.config.tubeApi}video?v=${v}`, headersArr); const res = await this.curlGetWithRetry(`${this.config.tubeApi}video?v=${v}`, Object.entries(headers).map(([k, v]) => `${k}: ${v}`));
const str = Buffer.isBuffer(res.data) ? res.data.toString("utf8") : String(res.data || ""); const str = Buffer.isBuffer(res.data) ? res.data.toString("utf8") : String(res.data || "");
const jsonStr = toJson(str); const jsonStr = toJson(str);
const video = this.getJson(jsonStr); const video = this.getJson(jsonStr);
return { json: jsonStr, video }; return { json: jsonStr, video };
})() })()
]); ]);
let p = {}; let p = {};
if (f === "true" && vid && vid.authorId) { if (f === "true" && vid && vid.authorId) {
const cKey = vid.authorId; p = await this.hedgedGetJsonFromBases(bases, `/channels/${vid.authorId}`, `hl=${contentlang}&region=${contentregion}`);
const cached = this._channelCache.get(cKey);
if (cached && Date.now() - cached.ts < 300_000) {
p = cached.data;
} else {
p = await this.hedgedGetJsonFromBases(bases, `/channels/${vid.authorId}`, `hl=${contentlang}&region=${contentregion}`);
this._channelCache.set(cKey, { data: p || {}, ts: Date.now() });
}
} }
if (!vid) { if (!vid) {
this.initError("Video JSON missing", new Error("no vid")); this.initError("Video JSON missing", new Error("no vid"));
return null; return null;
} }
if (this.checkUnexistingObject(vid)) { if (this.checkUnexistingObject(vid)) {
let fe = { engagement: null }; let fe = { engagement: null };
try { try {
fe = await getdislikes(v); fe = await getdislikes(v);
} catch {} } catch {}
const [c1, c2] = await this.getColorsSafe(`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}`); const [c1, c2] = await this.getColorsSafe(`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}`);
this.cache[v] = { this.cache[v] = {
result: { result: {
json: videoData?.json?.video, json: videoData?.json?.video,
@ -336,7 +210,7 @@ class InnerTubePokeVidious {
color: c1, color: c1,
color2: c2 color2: c2
}, },
timestamp: Date.now() timestamp: Date.now(),
}; };
return this.cache[v].result; return this.cache[v].result;
} }
@ -363,7 +237,7 @@ const pokeTubeApiCore = new InnerTubePokeVidious({
invapi_alt: config.proxylocation === "EU" ? "https://invid-api.poketube.fun/api/v1" : "https://iv.ggtyler.dev/api/v1", invapi_alt: config.proxylocation === "EU" ? "https://invid-api.poketube.fun/api/v1" : "https://iv.ggtyler.dev/api/v1",
dislikes: "https://returnyoutubedislikeapi.com/votes?videoId=", dislikes: "https://returnyoutubedislikeapi.com/votes?videoId=",
t_url: "https://t.poketube.fun/", t_url: "https://t.poketube.fun/",
useragent: config.useragent useragent: config.useragent,
}); });
module.exports = pokeTubeApiCore; module.exports = pokeTubeApiCore;