const rid = 'rad:z4NUTDwCFyoExPEHanFqvSYKu2Wsk';
const defaultSeed = 'ash.radicle.garden';
export default {
async fetch(request) {
async function MethodNotAllowed(request) {
return new Response(`Method ${request.method} not allowed.`, {
status: 405,
headers: {
Allow: "GET",
},
});
}
async function FallbackMirrors(seed, suffix) {
return new Response(`
Woops, that didn't work with seed ${seed}. Try other seeds:
https://shishqa.xyz/at/SEED/${suffix}.
Available seeds are:
`, {
status: 404,
headers: {
'Content-Type': 'text/html'
}
});
}
if (request.method !== "GET") return MethodNotAllowed(request);
const url = new URL(request.url);
const urlRegex = /https:\/\/shishqa\.xyz(?:\/at\/([^\/]+))?\/?(.*)/;
const match = url.toString().match(urlRegex);
if (!match) {
return new Response(`Error: bad url`, {
status: 500,
});
}
const seed = match[1] || defaultSeed;
var suffix = match[2] || '';
console.log(`seed: ${seed}`);
const repoInfo = await fetch(`https://${seed}/api/v1/projects/${rid}?v=4.0.0`);
if (!repoInfo.ok) return FallbackMirrors(seed, suffix);
const headRevision = (await repoInfo.json())['head'];
console.log(`revision: ${headRevision}`);
const extensionPattern = /.*\.[^\/]*$/;
if (suffix.length == 0) {
suffix += 'index.html';
} else if (suffix.endsWith('/')) {
suffix = suffix.slice(0, -1) + '.html';
} else if (!extensionPattern.test(suffix)) {
suffix += '.html';
}
const originUrl = `https://${seed}/raw/${rid}/${headRevision}/public/${suffix}`;
console.log(`querying: ${originUrl}`);
const originPage = await fetch(originUrl);
if (!originPage.ok) return FallbackMirrors(seed, suffix);
return new Response(originPage.body, originPage);
},
};