Fix proxy streaming for small files like HLS encryption keys
This commit is contained in:
+21
-13
@@ -745,12 +745,16 @@ router.get('/stream', async (req, res) => {
|
|||||||
manifest = manifest.split('\n').map(line => {
|
manifest = manifest.split('\n').map(line => {
|
||||||
const trimmed = line.trim();
|
const trimmed = line.trim();
|
||||||
if (trimmed === '' || trimmed.startsWith('#')) {
|
if (trimmed === '' || trimmed.startsWith('#')) {
|
||||||
if (trimmed.includes('URI="')) {
|
// Handle both URI="..." and URI='...' formats
|
||||||
return line.replace(/URI="([^"]+)"/g, (match, p1) => {
|
if (trimmed.includes('URI=')) {
|
||||||
|
// Replace both double and single quoted URIs
|
||||||
|
return line.replace(/URI=["']([^"']+)["']/g, (match, p1) => {
|
||||||
try {
|
try {
|
||||||
const absoluteUrl = new URL(p1, baseUrl).href;
|
const absoluteUrl = new URL(p1, baseUrl).href;
|
||||||
return `URI="${req.protocol}://${req.get('host')}${req.baseUrl}/stream?url=${encodeURIComponent(absoluteUrl)}"`;
|
return `URI="${req.protocol}://${req.get('host')}${req.baseUrl}/stream?url=${encodeURIComponent(absoluteUrl)}"`;
|
||||||
} catch (e) { return match; }
|
} catch (e) {
|
||||||
|
return match;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return line;
|
return line;
|
||||||
@@ -771,19 +775,23 @@ router.get('/stream', async (req, res) => {
|
|||||||
return res.send(manifest);
|
return res.send(manifest);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Binary content (Video Segment): Efficient Pipe
|
// Binary content (Video Segment or Key): Collect and send
|
||||||
console.log(`[Proxy] Piping binary stream (${contentType})`);
|
console.log(`[Proxy] Serving binary content (${contentType})`);
|
||||||
res.set('Content-Type', contentType || 'application/octet-stream');
|
res.set('Content-Type', contentType || 'application/octet-stream');
|
||||||
|
|
||||||
// Write the chunk we peeked
|
// For small files (like encryption keys), collect all data and send at once
|
||||||
res.write(firstChunk);
|
// This ensures proper Content-Length and response completion
|
||||||
|
const chunks = [firstChunk];
|
||||||
|
let result = await iterator.next();
|
||||||
|
while (!result.done) {
|
||||||
|
chunks.push(Buffer.from(result.value));
|
||||||
|
result = await iterator.next();
|
||||||
|
}
|
||||||
|
const fullContent = Buffer.concat(chunks);
|
||||||
|
|
||||||
// Stream the rest
|
// Set Content-Length for proper client handling
|
||||||
// Create a readable stream from the iterator
|
res.set('Content-Length', fullContent.length);
|
||||||
const restOfStream = Readable.from(iterator);
|
res.send(fullContent);
|
||||||
|
|
||||||
// Pipe to response
|
|
||||||
restOfStream.pipe(res);
|
|
||||||
return; // Success - exit the retry loop
|
return; // Success - exit the retry loop
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
Reference in New Issue
Block a user