diff --git a/src/modules/stream/types.js b/src/modules/stream/types.js index 6fee9a43..f1725bee 100644 --- a/src/modules/stream/types.js +++ b/src/modules/stream/types.js @@ -4,6 +4,14 @@ import got from "got"; import { ffmpegArgs, genericUserAgent } from "../config.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) { try { let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1]; @@ -15,25 +23,19 @@ export function streamDefault(streamInfo, res) { }, isStream: true }); - stream.pipe(res).on('error', () => { - res.destroy(); - }); - stream.on('error', () => { - res.destroy(); - }); - stream.on('aborted', () => { - res.destroy(); - }); + stream.pipe(res).on('error', () => fail(res)); + stream.on('error', () => fail(res)); + stream.on('aborted', () => fail(res)); } catch (e) { - res.destroy(); + fail(res); } } export function streamLiveRender(streamInfo, res) { try { if (streamInfo.urls.length !== 2) { - res.destroy(); - return; + return fail(res); } + let audio = got.get(streamInfo.urls[1], { isStream: true }); let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1], args = [ '-loglevel', '-8', @@ -57,24 +59,24 @@ export function streamLiveRender(streamInfo, res) { res.setHeader('Content-Disposition', `attachment; filename="${streamInfo.filename}"`); res.on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); ffmpegProcess.stdio[4].pipe(res).on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); audio.pipe(ffmpegProcess.stdio[3]).on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); audio.on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); audio.on('aborted', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); ffmpegProcess.on('disconnect', () => ffmpegProcess.kill()); @@ -84,11 +86,11 @@ export function streamLiveRender(streamInfo, res) { res.on('close', () => ffmpegProcess.kill()); ffmpegProcess.on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); } catch (e) { - res.destroy(); + fail(res); } } export function streamAudioOnly(streamInfo, res) { @@ -132,10 +134,10 @@ export function streamAudioOnly(streamInfo, res) { res.on('close', () => ffmpegProcess.kill()); ffmpegProcess.on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); } catch (e) { - res.destroy(); + fail(res); } } export function streamVideoOnly(streamInfo, res) { @@ -168,9 +170,9 @@ export function streamVideoOnly(streamInfo, res) { res.on('close', () => ffmpegProcess.kill()); ffmpegProcess.on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); } catch (e) { - res.destroy(); + fail(res); } }