Skip to content

Commit

Permalink
function wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
22yeets22 authored Apr 12, 2024
1 parent 11d968a commit 20e945e
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,54 +35,40 @@ routes.forEach((route) => {
app.get("/misc/*", async (req, res, next) => {
const baseUrl = "https://raw.githubusercontent.com/kfm5/a/main";
const secondaryUrl = "https://raw.githubusercontent.com/22yeets22/a/main";
await fetchData(req, res, next, baseUrl, secondaryUrl);
await fetchDataFromGithub(req, res, next, baseUrl, secondaryUrl);
});

async function fetchData(req, res, next, baseUrl, secondaryUrl = null) {
function hasFileExtension(urlString) {
const lastPathComponent = urlString.split('/').pop();
return lastPathComponent.includes('.');
async function fetchDataFromGithub(req, res, next, baseUrl, secondaryUrl = null) {
function isEmptyFile(urlString) {
return urlString.trim().split('/').pop().length === 0;
}
try {
if (hasFileExtension(req.params[0])) {

function fetchDataOneSource(req, res, next, url) {
if (isEmptyFile(req.params[0])) {
const reqTarget = `${baseUrl}/${req.params[0]}`;
const asset = await fetch(reqTarget);
if (asset.ok) {
const data = await asset.arrayBuffer();
res.end(Buffer.from(data));
return;
return true;
}
} else {
const indexReqTarget = `${baseUrl}/${req.params[0]}/index.html`;
const indexAsset = await fetch(indexReqTarget);
if (indexAsset.ok) {
const indexData = await indexAsset.arrayBuffer();
res.end(Buffer.from(indexData));
return;
return true;
}
}
return false;
}

try {
if (fetchDataOneSource(req, res, next, baseUrl)) return;
if (secondaryUrl) {
// The secondary url was provided, try to fetch from it
if (hasFileExtension(req.params[0])) {
const reqTarget = `${secondaryUrl}/${req.params[0]}`;
const asset = await fetch(reqTarget);
if (asset.ok) {
const data = await asset.arrayBuffer();
res.end(Buffer.from(data));
return;
}
} else {
const indexReqTarget = `${secondaryUrl}/${req.params[0]}/index.html`;
const indexAsset = await fetch(indexReqTarget);
if (indexAsset.ok) {
const indexData = await indexAsset.arrayBuffer();
res.end(Buffer.from(indexData));
return;
}
if (fetchDataOneSource(req, res, next, secondaryUrl)) return;
}

res.status(404).send("Resource not found");
} catch (error) {
console.error("Error fetching data, internal server error:", error);
Expand Down

0 comments on commit 20e945e

Please sign in to comment.