replace got with undici

merge pull request #182 from dumbmoron/undici
This commit is contained in:
wukko 2023-08-20 21:25:13 +06:00 committed by GitHub
commit 68b9a5321d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 13 deletions

View file

@ -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"

View file

@ -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) {

View file

@ -1,25 +1,27 @@
import { spawn } from "child_process";
import ffmpeg from "ffmpeg-static";
import got from "got";
import { ffmpegArgs, genericUserAgent } from "../config.js";
import { getThreads, metadataManager } 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, 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));
@ -27,11 +29,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',