youtube: don't block api startup waiting for innertube to activate

cobalt api has been getting blocked for several seconds
during startup, and also crashing when unable to connect
to youtube (e.g. when it's blocked); this should fix both
those things
This commit is contained in:
dumbmoron 2024-05-29 08:06:34 +00:00
parent 35ba3dc1a3
commit 679d623ebc
No known key found for this signature in database

View File

@ -3,7 +3,7 @@ import { env } from '../../config.js';
import { cleanString } from '../../sub/utils.js'; import { cleanString } from '../../sub/utils.js';
import { fetch } from 'undici' import { fetch } from 'undici'
const ytBase = await Innertube.create(); const ytBase = Innertube.create().catch(e => e);
const codecMatch = { const codecMatch = {
h264: { h264: {
@ -23,16 +23,21 @@ const codecMatch = {
} }
} }
const cloneInnertube = (customFetch) => { const cloneInnertube = async (customFetch) => {
const innertube = await ytBase;
if (innertube instanceof Error) {
throw innertube;
}
const session = new Session( const session = new Session(
ytBase.session.context, innertube.session.context,
ytBase.session.key, innertube.session.key,
ytBase.session.api_version, innertube.session.api_version,
ytBase.session.account_index, innertube.session.account_index,
ytBase.session.player, innertube.session.player,
undefined, undefined,
customFetch ?? ytBase.session.http.fetch, customFetch ?? innertube.session.http.fetch,
ytBase.session.cache innertube.session.cache
); );
const yt = new Innertube(session); const yt = new Innertube(session);
@ -40,7 +45,7 @@ const cloneInnertube = (customFetch) => {
} }
export default async function(o) { export default async function(o) {
const yt = cloneInnertube( const yt = await cloneInnertube(
(input, init) => fetch(input, { ...init, dispatcher: o.dispatcher }) (input, init) => fetch(input, { ...init, dispatcher: o.dispatcher })
); );