Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds shared profile CDN check #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function makeError(code: number, error: String, description: String): Response {
},
});
}
const CUSTOM_DATA = 'custom_files';

interface UrlData {
projectId: string;
Expand Down Expand Up @@ -68,10 +69,29 @@ async function countDownload(request: Request, env: Env, urlData: UrlData) {
}),
);

console.log(`Finished counting download. Status code: ${res.status}`);
console.log(`Finished checking download. Status code: ${res.status}`);
console.log(`Response body: ${await res.text()}`);
}

async function checkUrl(request: Request, env: Env) {
console.log(`Attempting to check url `);
const url = `${env.LABRINTH_URL}minecraft/check_token?url=${encodeURIComponent(request.url)}`;
console.log(`url: ${url}`);
let res = await fetch(
new Request(url, {
method: 'GET',
headers: {
// Pass 'authorization' header through
authorization: request.headers.get('authorization') ?? '',
},
}),
);

console.log(`Finished checking URL-token validity. Status code: ${res.status}`);
console.log(`Response body: ${await res.text()}`);
return res;
}

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
if (allowedMethods.indexOf(request.method) === -1) {
Expand Down Expand Up @@ -108,6 +128,15 @@ export default {
return makeError(404, 'not_found', 'the requested resource does not exist');
}

if (key.startsWith(CUSTOM_DATA)) {
let res = await checkUrl(request, env);
if (res.status !== 200) {
return makeError(404, 'not_found', 'the requested resource does not exist');
}
}

console.log(`Getting response for object with key ${key}`);

const response = new Response(request.method === 'HEAD' ? null : (object as R2ObjectBody).body, {
status: 200,
headers: {
Expand Down
6 changes: 3 additions & 3 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ compatibility_date = "2023-10-30"
account_id = "9ddae624c98677d68d93df6e524a6061"

[vars]
LABRINTH_URL = "http://127.0.0.1:8000/v2/"
LABRINTH_URL = "http://127.0.0.1:8000/v3/"

[env.staging.vars]
LABRINTH_URL = "https://staging-api.modrinth.com/v2/"
LABRINTH_URL = "https://staging-api.modrinth.com/v3/"

[env.prod.vars]
LABRINTH_URL = "https://api.modrinth.com/v2/"
LABRINTH_URL = "https://api.modrinth.com/v3/"

[[r2_buckets]]
binding = "MODRINTH_CDN"
Expand Down