always send something on stream failure

prevents reverse proxies (namely nginx) from assuming the server died because of an empty response
This commit is contained in:
dumbmoron 2023-08-17 21:04:17 +00:00
parent 91a60c1ec2
commit b0bed82167

View file

@ -4,6 +4,14 @@ import got from "got";
import { ffmpegArgs, genericUserAgent } from "../config.js"; import { ffmpegArgs, genericUserAgent } from "../config.js";
import { getThreads, metadataManager, msToTime } from "../sub/utils.js"; import { getThreads, metadataManager, msToTime } from "../sub/utils.js";
function fail(res) {
if (!res.headersSent) {
res.sendStatus(500);
}
return res.destroy();
}
export function streamDefault(streamInfo, res) { export function streamDefault(streamInfo, res) {
try { try {
let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1]; let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1];
@ -15,25 +23,19 @@ export function streamDefault(streamInfo, res) {
}, },
isStream: true isStream: true
}); });
stream.pipe(res).on('error', () => { stream.pipe(res).on('error', () => fail(res));
res.destroy(); stream.on('error', () => fail(res));
}); stream.on('aborted', () => fail(res));
stream.on('error', () => {
res.destroy();
});
stream.on('aborted', () => {
res.destroy();
});
} catch (e) { } catch (e) {
res.destroy(); fail(res);
} }
} }
export function streamLiveRender(streamInfo, res) { export function streamLiveRender(streamInfo, res) {
try { try {
if (streamInfo.urls.length !== 2) { if (streamInfo.urls.length !== 2) {
res.destroy(); return fail(res);
return;
} }
let audio = got.get(streamInfo.urls[1], { isStream: true }); let audio = got.get(streamInfo.urls[1], { isStream: true });
let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1], args = [ let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1], args = [
'-loglevel', '-8', '-loglevel', '-8',
@ -57,24 +59,24 @@ export function streamLiveRender(streamInfo, res) {
res.setHeader('Content-Disposition', `attachment; filename="${streamInfo.filename}"`); res.setHeader('Content-Disposition', `attachment; filename="${streamInfo.filename}"`);
res.on('error', () => { res.on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
ffmpegProcess.stdio[4].pipe(res).on('error', () => { ffmpegProcess.stdio[4].pipe(res).on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
audio.pipe(ffmpegProcess.stdio[3]).on('error', () => { audio.pipe(ffmpegProcess.stdio[3]).on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
audio.on('error', () => { audio.on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
audio.on('aborted', () => { audio.on('aborted', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
ffmpegProcess.on('disconnect', () => ffmpegProcess.kill()); ffmpegProcess.on('disconnect', () => ffmpegProcess.kill());
@ -84,11 +86,11 @@ export function streamLiveRender(streamInfo, res) {
res.on('close', () => ffmpegProcess.kill()); res.on('close', () => ffmpegProcess.kill());
ffmpegProcess.on('error', () => { ffmpegProcess.on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
} catch (e) { } catch (e) {
res.destroy(); fail(res);
} }
} }
export function streamAudioOnly(streamInfo, res) { export function streamAudioOnly(streamInfo, res) {
@ -132,10 +134,10 @@ export function streamAudioOnly(streamInfo, res) {
res.on('close', () => ffmpegProcess.kill()); res.on('close', () => ffmpegProcess.kill());
ffmpegProcess.on('error', () => { ffmpegProcess.on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
} catch (e) { } catch (e) {
res.destroy(); fail(res);
} }
} }
export function streamVideoOnly(streamInfo, res) { export function streamVideoOnly(streamInfo, res) {
@ -168,9 +170,9 @@ export function streamVideoOnly(streamInfo, res) {
res.on('close', () => ffmpegProcess.kill()); res.on('close', () => ffmpegProcess.kill());
ffmpegProcess.on('error', () => { ffmpegProcess.on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
} catch (e) { } catch (e) {
res.destroy(); fail(res);
} }
} }