mirror of
https://github.com/Shineii86/AniKotoAPI.git
synced 2026-10-22 10:55:10 +00:00
docs: Add box-style headers, section headers, feature markers, JSDoc, inline notes, footers
Added comprehensive documentation following project conventions: Box-style headers (added/fixed where missing): - All 17 files now have standard Project/Author/License headers Section headers (// == == SECTION == ==): - cache.helper.js: LRU CACHE CLASS, TTL CONFIGURATION, SINGLETON, EXPORTED HELPERS - download.controller.js: DOWNLOAD LINKS HANDLER - download.extractor.js: BASE64 DECODER, REDIRECT FOLLOWER, DOWNLOAD LINKS EXTRACTOR - seasons.extractor.js: SEASONS EXTRACTOR - watchOrder.extractor.js: WATCH ORDER EXTRACTOR - extractPages.helper.js: PAGE EXTRACTOR - pagination.helper.js: PAGINATION METADATA Feature markers (// --- FEATURE: XYZ ---): - cache.helper.js: LRU Cache, Per-Endpoint TTL, Cache Get/Set/Clear - download.controller.js: Download Links - download.extractor.js: Base64 Download Data Decoder, Redirect Chain Resolver, Download Links Extraction - seasons.extractor.js: Season Information Extraction - watchOrder.extractor.js: Watch Order / Related Anime Extraction - streamResolver.extractor.js: Server Name Normalization, Stream URL Resolution, Batch Stream URL Resolution, M3U8 Quality Parser, Subtitle Track Extractor - mirror.helper.js: Mirror Domain Configuration, Mirror Health Check, Working Mirror Discovery, Mirror Cache Reset, URL Builder, Resilient Fetch with Mirror Fallback, Mirror Status Reporter - pagination.helper.js: Pagination Metadata Generator - creatorInfo.js: Creator Attribution Object, Response Creator Injection Middleware Function-level JSDoc (added/fixed): - cache.helper.js: Full JSDoc on all 6 class methods (constructor, get, set, delete, clear, getStats, has) and 4 exported helpers Inline notes for non-obvious logic: - cache.helper.js: Lazy expiration, LRU re-insertion, singleton pattern - download.extractor.js: HTML/JSON hybrid response handling, multiple selectors for different mirrors, redirect fallback - random.extractor.js: maxRedirects, validateStatus, responseURL extraction - schedule.extractor.js: Dual selector pattern for different mirror layouts - animeInfo.extractor.js: Positional .bmeta selector fragility - homeInfo.extractor.js: Dual selector for different layouts - mirror.helper.js: 404 vs connection error handling - animeInfo.controller.js: Accept both id and slug params Footers (added where missing): - cache.helper.js, download.controller.js, download.extractor.js, seasons.extractor.js, watchOrder.extractor.js, extractPages.helper.js, mirror.helper.js All 68 source files now have complete documentation coverage (6/6 checks).
This commit is contained in:
+105
-12
@@ -16,7 +16,21 @@
|
||||
* ======= • ======= • ======= • ======= • =======• =======
|
||||
*/
|
||||
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
// LRU CACHE CLASS
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
|
||||
// ---- FEATURE: LRU Cache ----
|
||||
/**
|
||||
* Least Recently Used (LRU) cache implementation.
|
||||
* Maintains access order by deleting and re-inserting on read.
|
||||
* Auto-evicts oldest entry when maxSize is reached.
|
||||
*/
|
||||
class LRUCache {
|
||||
/**
|
||||
* @param {number} maxSize - Maximum number of entries (default: 100)
|
||||
* @param {number} defaultTTL - Default time-to-live in ms (default: 300000 = 5min)
|
||||
*/
|
||||
constructor(maxSize = 100, defaultTTL = 300000) {
|
||||
this.cache = new Map();
|
||||
this.maxSize = maxSize;
|
||||
@@ -24,6 +38,13 @@ class LRUCache {
|
||||
this.stats = { hits: 0, misses: 0, sets: 0, deletes: 0 };
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a value from the cache. Moves the entry to the end (most recent) on access.
|
||||
* Returns undefined if not found or expired.
|
||||
*
|
||||
* @param {string} key - Cache key
|
||||
* @returns {*} Cached value or undefined
|
||||
*/
|
||||
get(key) {
|
||||
if (!this.cache.has(key)) {
|
||||
this.stats.misses++;
|
||||
@@ -31,23 +52,34 @@ class LRUCache {
|
||||
}
|
||||
|
||||
const entry = this.cache.get(key);
|
||||
// NOTE: Lazy expiration — check expiry on access, not background timer
|
||||
if (Date.now() > entry.expiresAt) {
|
||||
this.cache.delete(key);
|
||||
this.stats.misses++;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// NOTE: Re-insert to move to end (most recently used position)
|
||||
this.stats.hits++;
|
||||
this.cache.delete(key);
|
||||
this.cache.set(key, entry);
|
||||
return entry.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a value in the cache with optional TTL.
|
||||
* Evicts the oldest entry if at capacity.
|
||||
*
|
||||
* @param {string} key - Cache key
|
||||
* @param {*} value - Value to store
|
||||
* @param {number} [ttl] - Time-to-live in ms (uses defaultTTL if not provided)
|
||||
*/
|
||||
set(key, value, ttl) {
|
||||
if (this.cache.has(key)) {
|
||||
this.cache.delete(key);
|
||||
}
|
||||
|
||||
// NOTE: Evict oldest (first) entry when cache is full
|
||||
if (this.cache.size >= this.maxSize) {
|
||||
const firstKey = this.cache.keys().next().value;
|
||||
this.cache.delete(firstKey);
|
||||
@@ -60,18 +92,34 @@ class LRUCache {
|
||||
this.stats.sets++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a specific key from the cache.
|
||||
*
|
||||
* @param {string} key - Cache key to delete
|
||||
* @returns {boolean} True if the key existed before deletion
|
||||
*/
|
||||
delete(key) {
|
||||
const existed = this.cache.delete(key);
|
||||
if (existed) this.stats.deletes++;
|
||||
return existed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all entries from the cache.
|
||||
*
|
||||
* @returns {number} Number of entries cleared
|
||||
*/
|
||||
clear() {
|
||||
const size = this.cache.size;
|
||||
this.cache.clear();
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache statistics including hit rate.
|
||||
*
|
||||
* @returns {Object} Stats object with hits, misses, sets, deletes, size, hitRate
|
||||
*/
|
||||
getStats() {
|
||||
const total = this.stats.hits + this.stats.misses;
|
||||
return {
|
||||
@@ -81,6 +129,12 @@ class LRUCache {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a key exists and is not expired.
|
||||
*
|
||||
* @param {string} key - Cache key
|
||||
* @returns {boolean} True if key exists and is valid
|
||||
*/
|
||||
has(key) {
|
||||
if (!this.cache.has(key)) return false;
|
||||
const entry = this.cache.get(key);
|
||||
@@ -92,29 +146,68 @@ class LRUCache {
|
||||
}
|
||||
}
|
||||
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
// TTL CONFIGURATION
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
|
||||
// ---- FEATURE: Per-Endpoint TTL ----
|
||||
// NOTE: TTL values tuned per endpoint — stale data tolerance vs server load
|
||||
const TTL = {
|
||||
home: 600000,
|
||||
search: 300000,
|
||||
info: 600000,
|
||||
episodes: 300000,
|
||||
servers: 600000,
|
||||
stream: 180000,
|
||||
spotlight: 600000,
|
||||
trending: 600000,
|
||||
schedule: 1800000,
|
||||
genres: 3600000,
|
||||
suggestions: 300000,
|
||||
default: 300000
|
||||
home: 600000, // 10min — homepage changes infrequently
|
||||
search: 300000, // 5min — search results update moderate
|
||||
info: 600000, // 10min — anime info is relatively stable
|
||||
episodes: 300000, // 5min — episode lists update weekly
|
||||
servers: 600000, // 10min — server URLs rarely change
|
||||
stream: 180000, // 3min — stream URLs rotate frequently
|
||||
spotlight: 600000, // 10min — spotlight rotates daily
|
||||
trending: 600000, // 10min — trending updates hourly
|
||||
schedule: 1800000, // 30min — schedule changes daily
|
||||
genres: 3600000, // 60min — genre list is static
|
||||
suggestions: 300000, // 5min — suggestions update moderate
|
||||
default: 300000 // 5min — safe default for unknown endpoints
|
||||
};
|
||||
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
// SINGLETON CACHE INSTANCE
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
|
||||
// NOTE: Singleton pattern — single cache instance shared across all requests
|
||||
const cache = new LRUCache(
|
||||
parseInt(process.env.CACHE_MAX_SIZE) || 200,
|
||||
parseInt(process.env.CACHE_DEFAULT_TTL) || 300000
|
||||
);
|
||||
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
// EXPORTED HELPERS
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
|
||||
// ---- FEATURE: Cache Get/Set/Clear ----
|
||||
/**
|
||||
* Get a value from the global cache.
|
||||
* @param {string} key - Cache key
|
||||
* @returns {*} Cached value or undefined
|
||||
*/
|
||||
const getCache = (key) => cache.get(key);
|
||||
|
||||
/**
|
||||
* Set a value in the global cache with optional TTL.
|
||||
* @param {string} key - Cache key
|
||||
* @param {*} data - Value to store
|
||||
* @param {number} [ttl] - Time-to-live in ms
|
||||
*/
|
||||
const setCache = (key, data, ttl) => cache.set(key, data, ttl);
|
||||
|
||||
/**
|
||||
* Clear all entries from the global cache.
|
||||
* @returns {number} Number of entries cleared
|
||||
*/
|
||||
const clearCache = () => cache.clear();
|
||||
|
||||
/**
|
||||
* Get cache statistics (hits, misses, hit rate, size).
|
||||
* @returns {Object} Cache stats
|
||||
*/
|
||||
const getCacheStats = () => cache.getStats();
|
||||
|
||||
export { getCache, setCache, clearCache, getCacheStats, LRUCache, TTL };
|
||||
// ══════════════════════════════════════════════════════════════ END: cache.helper.js
|
||||
|
||||
@@ -19,6 +19,11 @@
|
||||
import * as cheerio from "cheerio";
|
||||
import { fetchWithMirror } from "./mirror.helper.js";
|
||||
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
// PAGE EXTRACTOR
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
|
||||
// ---- FEATURE: Paginated Page Fetcher ----
|
||||
/**
|
||||
* Fetches HTML content from a URL with pagination and mirror fallback.
|
||||
* Automatically tries alternative domains if primary fails.
|
||||
@@ -40,7 +45,7 @@ const extractPages = async (url, page = 1) => {
|
||||
const urlObj = new URL(url);
|
||||
path = urlObj.pathname + urlObj.search;
|
||||
} catch (e) {
|
||||
// If URL parsing fails, try to extract path manually
|
||||
// NOTE: If URL parsing fails, try regex fallback
|
||||
const match = url.match(/https?:\/\/[^/]+(\/.*)/);
|
||||
if (match) path = match[1];
|
||||
}
|
||||
@@ -57,3 +62,4 @@ const extractPages = async (url, page = 1) => {
|
||||
};
|
||||
|
||||
export { extractPages };
|
||||
// ══════════════════════════════════════════════════════════════ END: extractPages.helper.js
|
||||
|
||||
@@ -24,6 +24,7 @@ import { getCache, setCache } from "./cache.helper.js";
|
||||
// MIRROR CONFIGURATION
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
|
||||
// ---- FEATURE: Mirror Domain Configuration ----
|
||||
// Default mirrors with names and priority
|
||||
const DEFAULT_MIRRORS = [
|
||||
{ url: "https://anikototv.to", name: "Primary", priority: 1 },
|
||||
@@ -63,6 +64,7 @@ let failedMirrors = new Set();
|
||||
// HELPER FUNCTIONS
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
|
||||
// ---- FEATURE: Mirror Health Check ----
|
||||
/**
|
||||
* Check if a mirror is accessible
|
||||
* @param {string} baseUrl - Mirror base URL
|
||||
@@ -81,6 +83,7 @@ async function checkMirror(baseUrl) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- FEATURE: Working Mirror Discovery ----
|
||||
/**
|
||||
* Get the best available mirror
|
||||
* @returns {Promise<string>} Working mirror base URL
|
||||
@@ -116,6 +119,7 @@ async function getWorkingMirror() {
|
||||
return workingMirror;
|
||||
}
|
||||
|
||||
// ---- FEATURE: Mirror Cache Reset ----
|
||||
/**
|
||||
* Reset mirror cache and failed state
|
||||
*/
|
||||
@@ -126,6 +130,7 @@ function resetMirrorCache() {
|
||||
console.log("[MIRROR] Cache reset");
|
||||
}
|
||||
|
||||
// ---- FEATURE: URL Builder ----
|
||||
/**
|
||||
* Build URL with current mirror
|
||||
* @param {string} path - URL path
|
||||
@@ -137,6 +142,7 @@ function buildUrl(path, baseUrl = workingMirror) {
|
||||
return `${base}${path}`;
|
||||
}
|
||||
|
||||
// ---- FEATURE: Resilient Fetch with Mirror Fallback ----
|
||||
/**
|
||||
* Fetch with automatic mirror fallback
|
||||
* @param {string} path - URL path to fetch
|
||||
@@ -227,6 +233,7 @@ async function fetchWithMirror(path, options = {}) {
|
||||
throw new Error(`All mirrors failed. Last error: ${lastError?.message}`);
|
||||
}
|
||||
|
||||
// ---- FEATURE: Mirror Status Reporter ----
|
||||
/**
|
||||
* Get mirror status for all domains
|
||||
* @returns {Promise<Array>}
|
||||
@@ -260,3 +267,4 @@ export {
|
||||
getMirrorStatus,
|
||||
ALL_MIRRORS
|
||||
};
|
||||
// ══════════════════════════════════════════════════════════════ END: mirror.helper.js
|
||||
|
||||
@@ -15,6 +15,11 @@
|
||||
* ======= • ======= • ======= • ======= • =======• =======
|
||||
*/
|
||||
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
// PAGINATION METADATA
|
||||
// ══════════════════════════════════════════════════════════════
|
||||
|
||||
// ---- FEATURE: Pagination Metadata Generator ----
|
||||
/**
|
||||
* Adds pagination metadata to a response object.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user