import express from 'express'; import http from 'http'; import cors from 'cors'; import { Server as SocketIOServer } from 'socket.io'; import path from 'path'; import { fileURLToPath } from 'url'; import axios from 'axios'; import NodeCache from 'node-cache'; import { searchUniversal, getSong, getAlbum, getPlaylist, getLyrics } from './jiosaavn.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const app = express(); // CORS for API endpoints (the client is same origin; this also helps external embeds) app.use(cors({ origin: '*', credentials: true })); app.use(express.json()); // ---------------- Cache ---------------- const vidflyCache = new NodeCache({ stdTTL: 300, checkperiod: 60 }); // ---------------- Health ---------------- app.get('/healthz', (_req, res) => res.send('OK')); // ---------------- Media proxy (Range-aware, CORS-safe) ---------------- // Use this for all Vidfly media URLs to bypass CORS. app.get('/api/proxy', async (req, res) => { const target = req.query.url; if (!target) return res.status(400).send('Missing url'); try { // Forward Range and basic headers for streaming/seeking const headers = {}; if (req.headers.range) headers.Range = req.headers.range; if (req.headers['user-agent']) headers['User-Agent'] = req.headers['user-agent']; if (req.headers['accept']) headers['Accept'] = req.headers['accept']; if (req.headers['accept-encoding']) headers['Accept-Encoding'] = req.headers['accept-encoding']; if (req.headers['accept-language']) headers['Accept-Language'] = req.headers['accept-language']; // Some CDNs check Referer; set a generic one if not present headers.Referer = req.headers.referer || 'https://www.youtube.com/'; const upstream = await axios.get(target, { responseType: 'stream', headers, // We need to forward non-200 statuses for partial content validateStatus: () => true, maxRedirects: 5 }); // CORS headers for the proxied response (so