From 814bfa404a24626f441d47353dd967b3b3547e2a Mon Sep 17 00:00:00 2001 From: dumbmoron <136796770+dumbmoron@users.noreply.github.com> Date: Sun, 20 Aug 2023 14:44:34 +0000 Subject: [PATCH 1/2] replace got with undici might as well since it already comes with youtubei.js --- package.json | 2 +- src/modules/stream/stream.js | 6 +++--- src/modules/stream/types.js | 17 ++++++++--------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 45ccbf51..0ed6132f 100644 --- a/package.json +++ b/package.json @@ -30,10 +30,10 @@ "express": "^4.18.1", "express-rate-limit": "^6.3.0", "ffmpeg-static": "^5.1.0", - "got": "^12.1.0", "nanoid": "^4.0.2", "node-cache": "^5.1.2", "set-cookie-parser": "2.6.0", + "undici": "^5.19.1", "url-pattern": "1.0.3", "xml-js": "^1.6.11", "youtubei.js": "^5.4.0" diff --git a/src/modules/stream/stream.js b/src/modules/stream/stream.js index 0cc6ea86..4b03196b 100644 --- a/src/modules/stream/stream.js +++ b/src/modules/stream/stream.js @@ -1,6 +1,6 @@ import { streamAudioOnly, streamDefault, streamLiveRender, streamVideoOnly } from "./types.js"; -export default function(res, streamInfo) { +export default async function(res, streamInfo) { try { if (streamInfo.isAudioOnly && streamInfo.type !== "bridge") { streamAudioOnly(streamInfo, res); @@ -8,14 +8,14 @@ export default function(res, streamInfo) { } switch (streamInfo.type) { case "render": - streamLiveRender(streamInfo, res); + await streamLiveRender(streamInfo, res); break; case "videoM3U8": case "mute": streamVideoOnly(streamInfo, res); break; default: - streamDefault(streamInfo, res); + await streamDefault(streamInfo, res); break; } } catch (e) { diff --git a/src/modules/stream/types.js b/src/modules/stream/types.js index b5271095..a0dc68fb 100644 --- a/src/modules/stream/types.js +++ b/src/modules/stream/types.js @@ -1,24 +1,22 @@ import { spawn } from "child_process"; import ffmpeg from "ffmpeg-static"; -import got from "got"; import { ffmpegArgs, genericUserAgent } from "../config.js"; import { getThreads, metadataManager, msToTime } from "../sub/utils.js"; +import { request } from 'undici'; function fail(res) { if (!res.headersSent) res.sendStatus(500); return res.destroy(); } -export function streamDefault(streamInfo, res) { +export async function streamDefault(streamInfo, res) { try { let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1]; let regFilename = !streamInfo.mute ? streamInfo.filename : `${streamInfo.filename.split('.')[0]}_mute.${format}`; res.setHeader('Content-disposition', `attachment; filename="${streamInfo.isAudioOnly ? `${streamInfo.filename}.${streamInfo.audioFormat}` : regFilename}"`); - const stream = got.get(streamInfo.urls, { - headers: { - "user-agent": genericUserAgent - }, - isStream: true + + const { body: stream } = await request(streamInfo.urls, { + headers: { 'user-agent': genericUserAgent } }); stream.pipe(res).on('error', () => fail(res)); stream.on('error', () => fail(res)); @@ -27,11 +25,12 @@ export function streamDefault(streamInfo, res) { fail(res); } } -export function streamLiveRender(streamInfo, res) { +export async function streamLiveRender(streamInfo, res) { try { if (streamInfo.urls.length !== 2) return fail(res); - let audio = got.get(streamInfo.urls[1], { isStream: true }); + let { body: audio } = await request(streamInfo.urls[1]); + let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1], args = [ '-loglevel', '-8', From d54655c6b00f6c835536bd30e8eec2b2fb679eb9 Mon Sep 17 00:00:00 2001 From: dumbmoron <136796770+dumbmoron@users.noreply.github.com> Date: Sun, 20 Aug 2023 15:00:03 +0000 Subject: [PATCH 2/2] passthrough content-type; content-length --- src/modules/stream/types.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/modules/stream/types.js b/src/modules/stream/types.js index a0dc68fb..f3bb76bd 100644 --- a/src/modules/stream/types.js +++ b/src/modules/stream/types.js @@ -15,9 +15,13 @@ export async function streamDefault(streamInfo, res) { let regFilename = !streamInfo.mute ? streamInfo.filename : `${streamInfo.filename.split('.')[0]}_mute.${format}`; res.setHeader('Content-disposition', `attachment; filename="${streamInfo.isAudioOnly ? `${streamInfo.filename}.${streamInfo.audioFormat}` : regFilename}"`); - const { body: stream } = await request(streamInfo.urls, { + const { body: stream, headers } = await request(streamInfo.urls, { headers: { 'user-agent': genericUserAgent } }); + + res.setHeader('content-type', headers['content-type']); + res.setHeader('content-length', headers['content-length']); + stream.pipe(res).on('error', () => fail(res)); stream.on('error', () => fail(res)); stream.on('aborted', () => fail(res));