Refactored server/db.js and all routes to use asynchronous fs operations, refactored m3uParser and epgParser to use streaming (via readline and sax) to prevent errors with large files, updated server/routes/proxy.js to use native stream piping for images and video, reducing latency and memory overhead
This commit is contained in:
+13
-12
@@ -3,10 +3,10 @@ const router = express.Router();
|
||||
const { hiddenItems } = require('../db');
|
||||
|
||||
// Get all hidden items
|
||||
router.get('/hidden', (req, res) => {
|
||||
router.get('/hidden', async (req, res) => {
|
||||
try {
|
||||
const { sourceId } = req.query;
|
||||
const items = hiddenItems.getAll(sourceId ? parseInt(sourceId) : null);
|
||||
const items = await hiddenItems.getAll(sourceId ? parseInt(sourceId) : null);
|
||||
res.json(items);
|
||||
} catch (err) {
|
||||
console.error('Error getting hidden items:', err);
|
||||
@@ -15,7 +15,7 @@ router.get('/hidden', (req, res) => {
|
||||
});
|
||||
|
||||
// Hide a channel, group, or category
|
||||
router.post('/hide', (req, res) => {
|
||||
router.post('/hide', async (req, res) => {
|
||||
try {
|
||||
const { sourceId, itemType, itemId } = req.body;
|
||||
|
||||
@@ -28,7 +28,7 @@ router.post('/hide', (req, res) => {
|
||||
return res.status(400).json({ error: `itemType must be one of: ${validTypes.join(', ')}` });
|
||||
}
|
||||
|
||||
hiddenItems.hide(sourceId, itemType, itemId);
|
||||
await hiddenItems.hide(sourceId, itemType, itemId);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
console.error('Error hiding item:', err);
|
||||
@@ -37,7 +37,7 @@ router.post('/hide', (req, res) => {
|
||||
});
|
||||
|
||||
// Show (unhide) a channel or group
|
||||
router.post('/show', (req, res) => {
|
||||
router.post('/show', async (req, res) => {
|
||||
try {
|
||||
const { sourceId, itemType, itemId } = req.body;
|
||||
|
||||
@@ -45,7 +45,7 @@ router.post('/show', (req, res) => {
|
||||
return res.status(400).json({ error: 'sourceId, itemType, and itemId are required' });
|
||||
}
|
||||
|
||||
hiddenItems.show(sourceId, itemType, itemId);
|
||||
await hiddenItems.show(sourceId, itemType, itemId);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
console.error('Error showing item:', err);
|
||||
@@ -54,7 +54,7 @@ router.post('/show', (req, res) => {
|
||||
});
|
||||
|
||||
// Check if item is hidden
|
||||
router.get('/hidden/check', (req, res) => {
|
||||
router.get('/hidden/check', async (req, res) => {
|
||||
try {
|
||||
const { sourceId, itemType, itemId } = req.query;
|
||||
|
||||
@@ -62,7 +62,8 @@ router.get('/hidden/check', (req, res) => {
|
||||
return res.status(400).json({ error: 'sourceId, itemType, and itemId are required' });
|
||||
}
|
||||
|
||||
const isHidden = hiddenItems.isHidden(parseInt(sourceId), itemType, itemId);
|
||||
// isHidden is now async
|
||||
const isHidden = await hiddenItems.isHidden(parseInt(sourceId), itemType, itemId);
|
||||
res.json({ hidden: isHidden });
|
||||
} catch (err) {
|
||||
console.error('Error checking hidden status:', err);
|
||||
@@ -71,7 +72,7 @@ router.get('/hidden/check', (req, res) => {
|
||||
});
|
||||
|
||||
// Bulk hide channels and groups
|
||||
router.post('/hide/bulk', (req, res) => {
|
||||
router.post('/hide/bulk', async (req, res) => {
|
||||
try {
|
||||
const { items } = req.body;
|
||||
|
||||
@@ -79,7 +80,7 @@ router.post('/hide/bulk', (req, res) => {
|
||||
return res.status(400).json({ error: 'items array is required' });
|
||||
}
|
||||
|
||||
hiddenItems.bulkHide(items);
|
||||
await hiddenItems.bulkHide(items);
|
||||
res.json({ success: true, count: items.length });
|
||||
} catch (err) {
|
||||
console.error('Error bulk hiding items:', err);
|
||||
@@ -88,7 +89,7 @@ router.post('/hide/bulk', (req, res) => {
|
||||
});
|
||||
|
||||
// Bulk show channels and groups
|
||||
router.post('/show/bulk', (req, res) => {
|
||||
router.post('/show/bulk', async (req, res) => {
|
||||
try {
|
||||
const { items } = req.body;
|
||||
|
||||
@@ -96,7 +97,7 @@ router.post('/show/bulk', (req, res) => {
|
||||
return res.status(400).json({ error: 'items array is required' });
|
||||
}
|
||||
|
||||
hiddenItems.bulkShow(items);
|
||||
await hiddenItems.bulkShow(items);
|
||||
res.json({ success: true, count: items.length });
|
||||
} catch (err) {
|
||||
console.error('Error bulk showing items:', err);
|
||||
|
||||
@@ -3,10 +3,10 @@ const router = express.Router();
|
||||
const { favorites } = require('../db');
|
||||
|
||||
// Get all favorites
|
||||
router.get('/', (req, res) => {
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const { sourceId, itemType } = req.query;
|
||||
const items = favorites.getAll(sourceId, itemType);
|
||||
const items = await favorites.getAll(sourceId, itemType);
|
||||
res.json(items);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
@@ -14,14 +14,14 @@ router.get('/', (req, res) => {
|
||||
});
|
||||
|
||||
// Add favorite
|
||||
router.post('/', (req, res) => {
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const { sourceId, itemId, itemType = 'channel' } = req.body;
|
||||
if (!sourceId || !itemId) {
|
||||
return res.status(400).json({ error: 'Source ID and Item ID are required' });
|
||||
}
|
||||
|
||||
favorites.add(sourceId, itemId, itemType);
|
||||
await favorites.add(sourceId, itemId, itemType);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
@@ -29,14 +29,14 @@ router.post('/', (req, res) => {
|
||||
});
|
||||
|
||||
// Remove favorite
|
||||
router.delete('/', (req, res) => {
|
||||
router.delete('/', async (req, res) => {
|
||||
try {
|
||||
const { sourceId, itemId, itemType = 'channel' } = req.body;
|
||||
if (!sourceId || !itemId) {
|
||||
return res.status(400).json({ error: 'Source ID and Item ID are required' });
|
||||
}
|
||||
|
||||
favorites.remove(sourceId, itemId, itemType);
|
||||
await favorites.remove(sourceId, itemId, itemType);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
@@ -44,14 +44,14 @@ router.delete('/', (req, res) => {
|
||||
});
|
||||
|
||||
// Check if item is favorited
|
||||
router.get('/check', (req, res) => {
|
||||
router.get('/check', async (req, res) => {
|
||||
try {
|
||||
const { sourceId, itemId, itemType = 'channel' } = req.query;
|
||||
if (!sourceId || !itemId) {
|
||||
return res.status(400).json({ error: 'Source ID and Item ID are required' });
|
||||
}
|
||||
|
||||
const isFav = favorites.isFavorite(sourceId, itemId, itemType);
|
||||
const isFav = await favorites.isFavorite(sourceId, itemId, itemType);
|
||||
res.json({ isFavorite: isFav });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
|
||||
+26
-42
@@ -5,6 +5,7 @@ const xtreamApi = require('../services/xtreamApi');
|
||||
const m3uParser = require('../services/m3uParser');
|
||||
const epgParser = require('../services/epgParser');
|
||||
const cache = require('../services/cache');
|
||||
const { Readable } = require('stream');
|
||||
|
||||
// Default cache TTL: 24 hours
|
||||
const DEFAULT_MAX_AGE_HOURS = 24;
|
||||
@@ -16,7 +17,7 @@ const DEFAULT_MAX_AGE_HOURS = 24;
|
||||
router.get('/xtream/:sourceId/:action', async (req, res) => {
|
||||
try {
|
||||
const sourceId = req.params.sourceId;
|
||||
const source = sources.getById(sourceId);
|
||||
const source = await sources.getById(sourceId);
|
||||
if (!source || source.type !== 'xtream') {
|
||||
return res.status(404).json({ error: 'Xtream source not found' });
|
||||
}
|
||||
@@ -99,9 +100,9 @@ router.get('/xtream/:sourceId/:action', async (req, res) => {
|
||||
* Get Xtream stream URL
|
||||
* GET /api/proxy/xtream/:sourceId/stream/:streamId
|
||||
*/
|
||||
router.get('/xtream/:sourceId/stream/:streamId/:type?', (req, res) => {
|
||||
router.get('/xtream/:sourceId/stream/:streamId/:type?', async (req, res) => {
|
||||
try {
|
||||
const source = sources.getById(req.params.sourceId);
|
||||
const source = await sources.getById(req.params.sourceId);
|
||||
if (!source || source.type !== 'xtream') {
|
||||
return res.status(404).json({ error: 'Xtream source not found' });
|
||||
}
|
||||
@@ -125,7 +126,7 @@ router.get('/xtream/:sourceId/stream/:streamId/:type?', (req, res) => {
|
||||
router.get('/m3u/:sourceId', async (req, res) => {
|
||||
try {
|
||||
const sourceId = req.params.sourceId;
|
||||
const source = sources.getById(sourceId);
|
||||
const source = await sources.getById(sourceId);
|
||||
if (!source || source.type !== 'm3u') {
|
||||
return res.status(404).json({ error: 'M3U source not found' });
|
||||
}
|
||||
@@ -164,7 +165,7 @@ router.get('/m3u/:sourceId', async (req, res) => {
|
||||
router.get('/epg/:sourceId', async (req, res) => {
|
||||
try {
|
||||
const sourceId = req.params.sourceId;
|
||||
const source = sources.getById(sourceId);
|
||||
const source = await sources.getById(sourceId);
|
||||
if (!source || (source.type !== 'epg' && source.type !== 'xtream')) {
|
||||
return res.status(404).json({ error: 'Valid EPG source not found' });
|
||||
}
|
||||
@@ -226,7 +227,7 @@ router.delete('/epg/:sourceId/cache', (req, res) => {
|
||||
*/
|
||||
router.post('/epg/:sourceId/channels', async (req, res) => {
|
||||
try {
|
||||
const source = sources.getById(req.params.sourceId);
|
||||
const source = await sources.getById(req.params.sourceId);
|
||||
if (!source || source.type !== 'epg') {
|
||||
return res.status(404).json({ error: 'EPG source not found' });
|
||||
}
|
||||
@@ -277,7 +278,6 @@ router.get('/stream', async (req, res) => {
|
||||
const response = await fetch(url, { headers });
|
||||
if (!response.ok) {
|
||||
console.error(`Upstream error for ${url.substring(0, 80)}...: ${response.status} ${response.statusText}`);
|
||||
// Log response body for debugging 403s
|
||||
if (response.status === 403) {
|
||||
const errorBody = await response.text().catch(() => 'N/A');
|
||||
console.error(`403 Response body: ${errorBody.substring(0, 200)}`);
|
||||
@@ -289,25 +289,22 @@ router.get('/stream', async (req, res) => {
|
||||
res.set('Access-Control-Allow-Origin', '*');
|
||||
|
||||
// Create an async iterator for the response body
|
||||
// Note: undici fetch returns an iterable body
|
||||
const iterator = response.body[Symbol.asyncIterator]();
|
||||
const first = await iterator.next();
|
||||
|
||||
if (first.done) {
|
||||
// Empty response
|
||||
res.set('Content-Type', contentType || 'application/octet-stream');
|
||||
return res.end();
|
||||
}
|
||||
|
||||
const firstChunk = Buffer.from(first.value);
|
||||
|
||||
// Peek at first bytes to check for HLS manifest (#EXTM3U)
|
||||
// Peek at first bytes to check for HLS manifest ({ #EXTM3U })
|
||||
const textPrefix = firstChunk.subarray(0, 7).toString('utf8');
|
||||
const contentLooksLikeHls = textPrefix === '#EXTM3U';
|
||||
|
||||
if (contentLooksLikeHls) {
|
||||
// HLS Manifest: We must read the WHOLE stream to rewrite it
|
||||
// This is fine because manifests are small text files
|
||||
// HLS Manifest: We must read the WHOLE manifest to rewrite it
|
||||
const chunks = [firstChunk];
|
||||
|
||||
// Consume the rest of the stream
|
||||
@@ -318,8 +315,6 @@ router.get('/stream', async (req, res) => {
|
||||
}
|
||||
|
||||
const buffer = Buffer.concat(chunks);
|
||||
|
||||
// Use the final URL after redirects for base URL calculation
|
||||
const finalUrl = response.url || url;
|
||||
console.log(`[Proxy] Processing HLS manifest from: ${finalUrl.substring(0, 80)}...`);
|
||||
res.set('Content-Type', 'application/vnd.apple.mpegurl');
|
||||
@@ -328,11 +323,9 @@ router.get('/stream', async (req, res) => {
|
||||
|
||||
const finalUrlObj = new URL(finalUrl);
|
||||
const baseUrl = finalUrlObj.origin + finalUrlObj.pathname.substring(0, finalUrlObj.pathname.lastIndexOf('/') + 1);
|
||||
console.log(`[Proxy] Base URL for rewriting: ${baseUrl}`);
|
||||
|
||||
manifest = manifest.split('\n').map(line => {
|
||||
const trimmed = line.trim();
|
||||
// ... same rewrite logic as before ...
|
||||
if (trimmed === '' || trimmed.startsWith('#')) {
|
||||
if (trimmed.includes('URI="')) {
|
||||
return line.replace(/URI="([^"]+)"/g, (match, p1) => {
|
||||
@@ -360,34 +353,19 @@ router.get('/stream', async (req, res) => {
|
||||
return res.send(manifest);
|
||||
}
|
||||
|
||||
// Binary content (Video Segment): STREAM IT!
|
||||
// This is the critical fix for "SocketError: other side closed"
|
||||
// Binary content (Video Segment): Efficient Pipe
|
||||
console.log(`[Proxy] Piping binary stream (${contentType})`);
|
||||
res.set('Content-Type', contentType || 'application/octet-stream');
|
||||
|
||||
// Write the first chunk we already peeked
|
||||
// Write the chunk we peeked
|
||||
res.write(firstChunk);
|
||||
|
||||
// Pipe the rest of the iterator to the response
|
||||
try {
|
||||
let result = await iterator.next();
|
||||
while (!result.done) {
|
||||
// If client disconnects, stop reading
|
||||
if (res.writableEnded || res.closed) break;
|
||||
// Stream the rest
|
||||
// Create a readable stream from the iterator
|
||||
const restOfStream = Readable.from(iterator);
|
||||
|
||||
const canWrite = res.write(Buffer.from(result.value));
|
||||
if (!canWrite) {
|
||||
// Handle backpressure
|
||||
await new Promise(resolve => res.once('drain', resolve));
|
||||
}
|
||||
result = await iterator.next();
|
||||
}
|
||||
res.end();
|
||||
} catch (streamErr) {
|
||||
console.error('Stream pipe error:', streamErr);
|
||||
if (!res.headersSent) res.status(500).end();
|
||||
else res.end();
|
||||
}
|
||||
// Pipe to response
|
||||
restOfStream.pipe(res);
|
||||
|
||||
} catch (err) {
|
||||
console.error('Stream proxy error:', err);
|
||||
@@ -420,15 +398,21 @@ router.get('/image', async (req, res) => {
|
||||
return res.status(response.status).send('Failed to fetch image');
|
||||
}
|
||||
|
||||
// Forward content type
|
||||
const contentType = response.headers.get('content-type') || 'image/png';
|
||||
res.set('Content-Type', contentType);
|
||||
res.set('Access-Control-Allow-Origin', '*');
|
||||
res.set('Cache-Control', 'public, max-age=86400'); // Cache for 24 hours
|
||||
|
||||
// Pipe the image data
|
||||
const buffer = await response.arrayBuffer();
|
||||
res.send(Buffer.from(buffer));
|
||||
// Efficiently pipe the response body
|
||||
if (response.body) {
|
||||
// response.body is an AsyncIterable in standard fetch/undici
|
||||
// Readable.from converts it to a Node.js Readable stream
|
||||
const stream = Readable.from(response.body);
|
||||
stream.pipe(res);
|
||||
} else {
|
||||
res.end();
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.error('Image proxy error:', err.message);
|
||||
res.status(500).send('Image proxy error');
|
||||
|
||||
+17
-17
@@ -4,9 +4,9 @@ const { sources } = require('../db');
|
||||
const xtreamApi = require('../services/xtreamApi');
|
||||
|
||||
// Get all sources
|
||||
router.get('/', (req, res) => {
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const allSources = sources.getAll();
|
||||
const allSources = await sources.getAll();
|
||||
// Don't expose passwords in list view
|
||||
const sanitized = allSources.map(s => ({
|
||||
...s,
|
||||
@@ -20,9 +20,9 @@ router.get('/', (req, res) => {
|
||||
});
|
||||
|
||||
// Get sources by type
|
||||
router.get('/type/:type', (req, res) => {
|
||||
router.get('/type/:type', async (req, res) => {
|
||||
try {
|
||||
const typeSources = sources.getByType(req.params.type);
|
||||
const typeSources = await sources.getByType(req.params.type);
|
||||
res.json(typeSources);
|
||||
} catch (err) {
|
||||
console.error('Error getting sources by type:', err);
|
||||
@@ -31,9 +31,9 @@ router.get('/type/:type', (req, res) => {
|
||||
});
|
||||
|
||||
// Get single source
|
||||
router.get('/:id', (req, res) => {
|
||||
router.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const source = sources.getById(req.params.id);
|
||||
const source = await sources.getById(req.params.id);
|
||||
if (!source) {
|
||||
return res.status(404).json({ error: 'Source not found' });
|
||||
}
|
||||
@@ -45,7 +45,7 @@ router.get('/:id', (req, res) => {
|
||||
});
|
||||
|
||||
// Create source
|
||||
router.post('/', (req, res) => {
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const { type, name, url, username, password } = req.body;
|
||||
|
||||
@@ -57,7 +57,7 @@ router.post('/', (req, res) => {
|
||||
return res.status(400).json({ error: 'Invalid source type' });
|
||||
}
|
||||
|
||||
const source = sources.create({ type, name, url, username, password });
|
||||
const source = await sources.create({ type, name, url, username, password });
|
||||
res.status(201).json(source);
|
||||
} catch (err) {
|
||||
console.error('Error creating source:', err);
|
||||
@@ -66,15 +66,15 @@ router.post('/', (req, res) => {
|
||||
});
|
||||
|
||||
// Update source
|
||||
router.put('/:id', (req, res) => {
|
||||
router.put('/:id', async (req, res) => {
|
||||
try {
|
||||
const existing = sources.getById(req.params.id);
|
||||
const existing = await sources.getById(req.params.id);
|
||||
if (!existing) {
|
||||
return res.status(404).json({ error: 'Source not found' });
|
||||
}
|
||||
|
||||
const { name, url, username, password } = req.body;
|
||||
const updated = sources.update(req.params.id, {
|
||||
const updated = await sources.update(req.params.id, {
|
||||
name: name || existing.name,
|
||||
url: url || existing.url,
|
||||
username: username !== undefined ? username : existing.username,
|
||||
@@ -88,13 +88,13 @@ router.put('/:id', (req, res) => {
|
||||
});
|
||||
|
||||
// Delete source
|
||||
router.delete('/:id', (req, res) => {
|
||||
router.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const existing = sources.getById(req.params.id);
|
||||
const existing = await sources.getById(req.params.id);
|
||||
if (!existing) {
|
||||
return res.status(404).json({ error: 'Source not found' });
|
||||
}
|
||||
sources.delete(req.params.id);
|
||||
await sources.delete(req.params.id);
|
||||
res.json({ success: true });
|
||||
} catch (err) {
|
||||
console.error('Error deleting source:', err);
|
||||
@@ -103,9 +103,9 @@ router.delete('/:id', (req, res) => {
|
||||
});
|
||||
|
||||
// Toggle source enabled/disabled
|
||||
router.post('/:id/toggle', (req, res) => {
|
||||
router.post('/:id/toggle', async (req, res) => {
|
||||
try {
|
||||
const updated = sources.toggleEnabled(req.params.id);
|
||||
const updated = await sources.toggleEnabled(req.params.id);
|
||||
if (!updated) {
|
||||
return res.status(404).json({ error: 'Source not found' });
|
||||
}
|
||||
@@ -119,7 +119,7 @@ router.post('/:id/toggle', (req, res) => {
|
||||
// Test source connection
|
||||
router.post('/:id/test', async (req, res) => {
|
||||
try {
|
||||
const source = sources.getById(req.params.id);
|
||||
const source = await sources.getById(req.params.id);
|
||||
if (!source) {
|
||||
return res.status(404).json({ error: 'Source not found' });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user