diff options
| -rw-r--r-- | apps/cdn/index.ts | 192 | ||||
| -rw-r--r-- | apps/cdn/package.json | 10 | ||||
| -rw-r--r-- | apps/proxy/src/index.js | 23 |
3 files changed, 116 insertions, 109 deletions
diff --git a/apps/cdn/index.ts b/apps/cdn/index.ts index 55c51256..e0d63db5 100644 --- a/apps/cdn/index.ts +++ b/apps/cdn/index.ts @@ -1,112 +1,112 @@ // <https://github.com/soruly/trace.moe-www/blob/master/image-proxy.js> addEventListener("fetch", async (event) => { - event.respondWith(handleRequest(event.request)); + event.respondWith(handleRequest(event.request)); }); const errorResponse = (errorMessage) => - new Response(errorMessage, { - status: 400, - statusText: "Bad Request", - }); + new Response(errorMessage, { + status: 400, + statusText: "Bad Request", + }); const handleRequest = async (originalRequest) => { - let originalURL = new URL(originalRequest.url); - if (!originalURL.searchParams.get("url")) { - return errorResponse("Error: Cannot get url from param"); - } + const originalURL = new URL(originalRequest.url); + if (!originalURL.searchParams.get("url")) { + return errorResponse("Error: Cannot get url from param"); + } - let imageURL = null; - try { - imageURL = new URL(originalURL.searchParams.get("url")); - } catch (e) {} - if (!imageURL) { - return errorResponse("Error: Invalid URL string"); - } + let imageURL = null; + try { + imageURL = new URL(originalURL.searchParams.get("url")); + } catch {} + if (!imageURL) { + return errorResponse("Error: Invalid URL string"); + } - let imageRequest = new Request(imageURL, { - redirect: "follow", - headers: { - referer: imageURL.origin, - }, - }); + const imageRequest = new Request(imageURL, { + redirect: "follow", + headers: { + referer: imageURL.origin, + }, + }); - let response = await fetch(imageRequest, { - cf: { - polish: "lossy", - cacheEverything: true, - }, - }); + const response = await fetch(imageRequest, { + cf: { + polish: "lossy", + cacheEverything: true, + }, + }); - if (response.status >= 400) { - return new Response(response.body, { - status: response.status, - statusText: response.statusText, - headers: response.headers, - }); - } + if (response.status >= 400) { + return new Response(response.body, { + status: response.status, + statusText: response.statusText, + headers: response.headers, + }); + } - // if ( - // response.headers.get("Content-Type").toLowerCase() !== - // "application/octet-stream" && - // !["image", "video"].includes( - // response.headers.get("Content-Type").split("/")[0].toLowerCase() - // ) - // ) { - // // retry as bot to get og:image - // let webResponse = await fetch( - // new Request(imageURL, { - // redirect: "follow", - // headers: { - // referer: imageURL.origin, - // "User-Agent": "googlebot", - // }, - // }) - // ); - // if (response.status === 200) { - // const ogImageURL = (await webResponse.text()) - // ?.match(/<[^<]+?"og:image".*?>/, "$1")?.[0] - // ?.match(/content="(.*?)"/, "$1")?.[1]; + // if ( + // response.headers.get("Content-Type").toLowerCase() !== + // "application/octet-stream" && + // !["image", "video"].includes( + // response.headers.get("Content-Type").split("/")[0].toLowerCase() + // ) + // ) { + // // retry as bot to get og:image + // let webResponse = await fetch( + // new Request(imageURL, { + // redirect: "follow", + // headers: { + // referer: imageURL.origin, + // "User-Agent": "googlebot", + // }, + // }) + // ); + // if (response.status === 200) { + // const ogImageURL = (await webResponse.text()) + // ?.match(/<[^<]+?"og:image".*?>/, "$1")?.[0] + // ?.match(/content="(.*?)"/, "$1")?.[1]; - // if (ogImageURL && ogImageURL.match(/^https?:\/\//)) { - // response = await fetch( - // new Request(ogImageURL, { - // redirect: "follow", - // headers: { - // referer: imageURL.origin, - // }, - // }) - // ); - // if (response.status >= 400) { - // return new Response(response.body, { - // status: response.status, - // statusText: response.statusText, - // headers: response.headers, - // }); - // } - // } - // } - // } - // if ( - // response.headers.get("Content-Type").toLowerCase() !== - // "application/octet-stream" && - // !["image", "video"].includes( - // response.headers.get("Content-Type").split("/")[0].toLowerCase() - // ) - // ) { - // return errorResponse( - // "Error: Content-Type is not image or video or application/octet-stream" - // ); - // } + // if (ogImageURL && ogImageURL.match(/^https?:\/\//)) { + // response = await fetch( + // new Request(ogImageURL, { + // redirect: "follow", + // headers: { + // referer: imageURL.origin, + // }, + // }) + // ); + // if (response.status >= 400) { + // return new Response(response.body, { + // status: response.status, + // statusText: response.statusText, + // headers: response.headers, + // }); + // } + // } + // } + // } + // if ( + // response.headers.get("Content-Type").toLowerCase() !== + // "application/octet-stream" && + // !["image", "video"].includes( + // response.headers.get("Content-Type").split("/")[0].toLowerCase() + // ) + // ) { + // return errorResponse( + // "Error: Content-Type is not image or video or application/octet-stream" + // ); + // } - return new Response(response.body, { - status: response.status, - statusText: response.statusText, - headers: { - "Cache-Control": - "public, immutable, s-maxage=31536000, max-age=31536000, stale-while-revalidate=60", - "Access-Control-Allow-Origin": "https://due.moe", - ...response.headers, - }, - }); + return new Response(response.body, { + status: response.status, + statusText: response.statusText, + headers: { + "Cache-Control": + "public, immutable, s-maxage=31536000, max-age=31536000, stale-while-revalidate=60", + "Access-Control-Allow-Origin": "https://due.moe", + ...response.headers, + }, + }); }; diff --git a/apps/cdn/package.json b/apps/cdn/package.json index 33970b95..cc85f6cb 100644 --- a/apps/cdn/package.json +++ b/apps/cdn/package.json @@ -1,7 +1,7 @@ { - "private": true, - "devDependencies": { - "@types/node": "^17.0.42", - "typescript": "^4.7.3" - } + "private": true, + "devDependencies": { + "@types/node": "^17.0.42", + "typescript": "^4.7.3" + } } diff --git a/apps/proxy/src/index.js b/apps/proxy/src/index.js index f90adfa7..80d87b1b 100644 --- a/apps/proxy/src/index.js +++ b/apps/proxy/src/index.js @@ -4,8 +4,8 @@ import { getMangadexFailureRowsByAniListIds, getMangadexRowsByAniListIds, hasSupabaseConfig, - upsertMangadexRows, upsertMangadexFailureRows, + upsertMangadexRows, } from "./supabase.js"; const DEFAULT_ALLOWED_ORIGIN = "https://due.moe"; @@ -144,7 +144,10 @@ const parseMangaPayload = async (request) => { }; const bootstrapRetryMinutes = (env) => { - const minutes = Number.parseInt(env.MANGADEX_BOOTSTRAP_RETRY_MINUTES || "", 10); + const minutes = Number.parseInt( + env.MANGADEX_BOOTSTRAP_RETRY_MINUTES || "", + 10, + ); return Number.isFinite(minutes) && minutes > 0 ? minutes @@ -152,7 +155,10 @@ const bootstrapRetryMinutes = (env) => { }; const pendingRetryMs = (env) => { - const milliseconds = Number.parseInt(env.MANGA_CHAPTER_COUNTS_RETRY_MS || "", 10); + const milliseconds = Number.parseInt( + env.MANGA_CHAPTER_COUNTS_RETRY_MS || "", + 10, + ); return Number.isFinite(milliseconds) && milliseconds > 0 ? milliseconds @@ -248,7 +254,11 @@ const handleMangaChapterCounts = async (request, env, ctx) => { }, ]), ); - const pending = [...new Set([...pendingRows, ...queueableRows].map((entry) => entry.anilistId))]; + const pending = [ + ...new Set( + [...pendingRows, ...queueableRows].map((entry) => entry.anilistId), + ), + ]; return jsonResponse(request, { data, @@ -292,10 +302,7 @@ export default { if (request.method === "OPTIONS") return handleOptions(request); - if ( - url.pathname === "/manga/chapter-counts" && - request.method === "POST" - ) + if (url.pathname === "/manga/chapter-counts" && request.method === "POST") return handleMangaChapterCounts(request, env, ctx); if (url.pathname === "/manga/sync" && request.method === "POST") |