From f3fff5cc091dfa61c196907bb87d1096f422e923 Mon Sep 17 00:00:00 2001 From: Emilien <4016501+unixfox@users.noreply.github.com> Date: Thu, 12 Dec 2024 23:28:49 +0100 Subject: [PATCH] trying to improve again dynamic import --- compile.env | 1 + src/lib/helpers/youtubePlayerHandling.ts | 10 +- src/lib/jobs/potoken.ts | 14 ++- src/main.ts | 145 ++++++++++++----------- src/routes/videoPlaybackProxy.ts | 10 +- 5 files changed, 103 insertions(+), 77 deletions(-) create mode 100644 compile.env diff --git a/compile.env b/compile.env new file mode 100644 index 0000000..816eabc --- /dev/null +++ b/compile.env @@ -0,0 +1 @@ +DENO_COMPILED=true diff --git a/src/lib/helpers/youtubePlayerHandling.ts b/src/lib/helpers/youtubePlayerHandling.ts index 92d4746..60e6e5d 100644 --- a/src/lib/helpers/youtubePlayerHandling.ts +++ b/src/lib/helpers/youtubePlayerHandling.ts @@ -4,8 +4,14 @@ import { compress, decompress } from "https://deno.land/x/brotli@0.1.7/mod.ts"; import { Store } from "@willsoto/node-konfig-core"; let youtubePlayerReqLocation = "youtubePlayerReq"; if (Deno.env.get("YT_PLAYER_REQ_LOCATION")) { - youtubePlayerReqLocation = import.meta.dirname + "/" + - Deno.env.get("YT_PLAYER_REQ_LOCATION"); + if (Deno.env.has("DENO_COMPILED")) { + youtubePlayerReqLocation = Deno.mainModule.replace("main.ts", "") + + Deno.env.get("YT_PLAYER_REQ_LOCATION"); + } else { + youtubePlayerReqLocation = Deno.env.get( + "YT_PLAYER_REQ_LOCATION", + ) as string; + } } const { youtubePlayerReq } = await import(youtubePlayerReqLocation); diff --git a/src/lib/jobs/potoken.ts b/src/lib/jobs/potoken.ts index ab336c9..898ce39 100644 --- a/src/lib/jobs/potoken.ts +++ b/src/lib/jobs/potoken.ts @@ -5,8 +5,14 @@ import { Innertube, UniversalCache } from "youtubei.js"; import { Store } from "@willsoto/node-konfig-core"; let getFetchClientLocation = "getFetchClient"; if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) { - getFetchClientLocation = import.meta.dirname + "/" + - Deno.env.get("GET_FETCH_CLIENT_LOCATION"); + if (Deno.env.has("DENO_COMPILED")) { + getFetchClientLocation = Deno.mainModule.replace("main.ts", "") + + Deno.env.get("GET_FETCH_CLIENT_LOCATION"); + } else { + getFetchClientLocation = Deno.env.get( + "GET_FETCH_CLIENT_LOCATION", + ) as string; + } } const { getFetchClient } = await import(getFetchClientLocation); @@ -14,7 +20,7 @@ const { getFetchClient } = await import(getFetchClientLocation); export const poTokenGenerate = async ( innertubeClient: Innertube, konfigStore: Store>, - innertubeClientCache: UniversalCache + innertubeClientCache: UniversalCache, ): Promise => { const requestKey = "O43z0dpjhgX20SCx4KAo"; @@ -62,7 +68,7 @@ export const poTokenGenerate = async ( bgConfig, }); - await BG.PoToken.generatePlaceholder(visitorData);; + await BG.PoToken.generatePlaceholder(visitorData); return (await Innertube.create({ po_token: poTokenResult.poToken, diff --git a/src/main.ts b/src/main.ts index b4329c5..e23e4c9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,8 +5,14 @@ import { poTokenGenerate } from "./lib/jobs/potoken.ts"; import { konfigLoader } from "./lib/helpers/konfigLoader.ts"; let getFetchClientLocation = "getFetchClient"; if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) { - getFetchClientLocation = import.meta.dirname + "/" + - Deno.env.get("GET_FETCH_CLIENT_LOCATION"); + if (Deno.env.has("DENO_COMPILED")) { + getFetchClientLocation = Deno.mainModule.replace("main.ts", "") + + Deno.env.get("GET_FETCH_CLIENT_LOCATION"); + } else { + getFetchClientLocation = Deno.env.get( + "GET_FETCH_CLIENT_LOCATION", + ) as string; + } } const { getFetchClient } = await import(getFetchClientLocation); @@ -16,100 +22,101 @@ const konfigStore = await konfigLoader(); let innertubeClient: Innertube; let innertubeClientFetchPlayer = true; const innertubeClientOauthEnabled = konfigStore.get( - "youtube_session.oauth_enabled", + "youtube_session.oauth_enabled", ) as boolean; const innertubeClientJobPoTokenEnabled = konfigStore.get( - "jobs.youtube_session.po_token_enabled", + "jobs.youtube_session.po_token_enabled", ) as boolean; const innertubeClientCookies = konfigStore.get( - "jobs.youtube_session.cookies", + "jobs.youtube_session.cookies", ) as string; let innertubeClientCache = new UniversalCache( - true, - konfigStore.get('cache.directory') as string + "/youtubei.js/", + true, + konfigStore.get("cache.directory") as string + "/youtubei.js/", ) as UniversalCache; -Deno.env.set('TMPDIR', konfigStore.get("cache.directory") as string) +Deno.env.set("TMPDIR", konfigStore.get("cache.directory") as string); if (!innertubeClientOauthEnabled) { - if (innertubeClientJobPoTokenEnabled) { - console.log("[INFO] job po_token is active."); - // Don't fetch fetch player yet for po_token - innertubeClientFetchPlayer = false; - } else if (!innertubeClientJobPoTokenEnabled) { - console.log("[INFO] job po_token is NOT active."); - } + if (innertubeClientJobPoTokenEnabled) { + console.log("[INFO] job po_token is active."); + // Don't fetch fetch player yet for po_token + innertubeClientFetchPlayer = false; + } else if (!innertubeClientJobPoTokenEnabled) { + console.log("[INFO] job po_token is NOT active."); + } } else if (innertubeClientOauthEnabled) { - // Can't use cache if using OAuth#cacheCredentials - innertubeClientCache = new UniversalCache(false); + // Can't use cache if using OAuth#cacheCredentials + innertubeClientCache = new UniversalCache(false); } innertubeClient = await Innertube.create({ - cache: innertubeClientCache, - retrieve_player: innertubeClientFetchPlayer, - fetch: getFetchClient(konfigStore), - cookie: innertubeClientCookies || undefined, + cache: innertubeClientCache, + retrieve_player: innertubeClientFetchPlayer, + fetch: getFetchClient(konfigStore), + cookie: innertubeClientCookies || undefined, }); if (!innertubeClientOauthEnabled) { - if (innertubeClientJobPoTokenEnabled) { - innertubeClient = await poTokenGenerate( - innertubeClient, - konfigStore, - innertubeClientCache as UniversalCache, - ); - } - Deno.cron( - "regenerate youtube session", - konfigStore.get("jobs.youtube_session.frequency") as string, - async () => { - if (innertubeClientJobPoTokenEnabled) { + if (innertubeClientJobPoTokenEnabled) { innertubeClient = await poTokenGenerate( - innertubeClient, - konfigStore, - innertubeClientCache, + innertubeClient, + konfigStore, + innertubeClientCache as UniversalCache, ); - } else { - innertubeClient = await Innertube.create({ - cache: innertubeClientCache, - retrieve_player: innertubeClientFetchPlayer, - }); - } - }, - ); -} else if (innertubeClientOauthEnabled) { - // Fired when waiting for the user to authorize the sign in attempt. - innertubeClient.session.on("auth-pending", (data) => { - console.log( - `[INFO] [OAUTH] Go to ${data.verification_url} in your browser and enter code ${data.user_code} to authenticate.`, + } + Deno.cron( + "regenerate youtube session", + konfigStore.get("jobs.youtube_session.frequency") as string, + async () => { + if (innertubeClientJobPoTokenEnabled) { + innertubeClient = await poTokenGenerate( + innertubeClient, + konfigStore, + innertubeClientCache, + ); + } else { + innertubeClient = await Innertube.create({ + cache: innertubeClientCache, + retrieve_player: innertubeClientFetchPlayer, + }); + } + }, ); - }); - // Fired when authentication is successful. - innertubeClient.session.on("auth", () => { - console.log("[INFO] [OAUTH] Sign in successful!"); - }); - // Fired when the access token expires. - innertubeClient.session.on("update-credentials", async () => { - console.log("[INFO] [OAUTH] Credentials updated."); - await innertubeClient.session.oauth.cacheCredentials(); - }); +} else if (innertubeClientOauthEnabled) { + // Fired when waiting for the user to authorize the sign in attempt. + innertubeClient.session.on("auth-pending", (data) => { + console.log( + `[INFO] [OAUTH] Go to ${data.verification_url} in your browser and enter code ${data.user_code} to authenticate.`, + ); + }); + // Fired when authentication is successful. + innertubeClient.session.on("auth", () => { + console.log("[INFO] [OAUTH] Sign in successful!"); + }); + // Fired when the access token expires. + innertubeClient.session.on("update-credentials", async () => { + console.log("[INFO] [OAUTH] Credentials updated."); + await innertubeClient.session.oauth.cacheCredentials(); + }); - // Attempt to sign in and then cache the credentials - await innertubeClient.session.signIn(); - await innertubeClient.session.oauth.cacheCredentials(); + // Attempt to sign in and then cache the credentials + await innertubeClient.session.signIn(); + await innertubeClient.session.oauth.cacheCredentials(); } app.use("*", async (c, next) => { - // @ts-ignore Do not understand how to fix this error. - c.set("innertubeClient", innertubeClient); - // @ts-ignore Do not understand how to fix this error. - c.set("konfigStore", konfigStore); - await next(); + // @ts-ignore Do not understand how to fix this error. + c.set("innertubeClient", innertubeClient); + // @ts-ignore Do not understand how to fix this error. + c.set("konfigStore", konfigStore); + await next(); }); routes(app, konfigStore); Deno.serve({ - port: Number(Deno.env.get("PORT")) || konfigStore.get("server.port") as number, - hostname: Deno.env.get("HOST") || konfigStore.get("server.host") as string, + port: Number(Deno.env.get("PORT")) || + konfigStore.get("server.port") as number, + hostname: Deno.env.get("HOST") || konfigStore.get("server.host") as string, }, app.fetch); diff --git a/src/routes/videoPlaybackProxy.ts b/src/routes/videoPlaybackProxy.ts index b16151b..fbd0ac1 100644 --- a/src/routes/videoPlaybackProxy.ts +++ b/src/routes/videoPlaybackProxy.ts @@ -3,8 +3,14 @@ import { Store } from "@willsoto/node-konfig-core"; import { HTTPException } from "hono/http-exception"; let getFetchClientLocation = "getFetchClient"; if (Deno.env.get("GET_FETCH_CLIENT_LOCATION")) { - getFetchClientLocation = import.meta.dirname + "/" + - Deno.env.get("GET_FETCH_CLIENT_LOCATION"); + if (Deno.env.has("DENO_COMPILED")) { + getFetchClientLocation = Deno.mainModule.replace("main.ts", "") + + Deno.env.get("GET_FETCH_CLIENT_LOCATION"); + } else { + getFetchClientLocation = Deno.env.get( + "GET_FETCH_CLIENT_LOCATION", + ) as string; + } } const { getFetchClient } = await import(getFetchClientLocation);