googleics/index.js

39 lines
1.4 KiB
JavaScript

/**
* Respond with whatever Google gives us
* @param {Request} request
*/
async function handleRequest(request) {
let response;
try {
const prefix = 'https://calendar.google.com/calendar/ical';
let postscript = '/public/basic.ics';
let requestPath = new URL(request.url).pathname;
if (requestPath.endsWith('/')) {
requestPath = requestPath.substring(0, requestPath.length - 1);
}
if (requestPath.search('/private-') >= 0) {
postscript = '/basic.ics';
}
const googleCal = `${prefix}${requestPath}${postscript}`;
response = await fetch(googleCal);
} catch (err) {
// Without event.waitUntil(), our fetch() to our logging service may
// or may not complete.
// event.waitUntil(postLog(err));
const stack = JSON.stringify(err.stack) || err;
// Copy the response and initialize body to the stack trace
response = new Response(stack, response);
// Shove our rewritten URL into a header to find out what it was.
response.headers.set('X-Debug-stack', stack);
response.headers.set('X-Debug-err', err);
}
return response;
}
// This is "browser", but it's running in a V8 isolate, so it's really
// neither browser nor node. This is the hook into the CF Worker ecosystem,
// so we need to have this code and is safe to disable eslint for this line
addEventListener('fetch', (event) => { // eslint-disable-line no-restricted-globals
event.respondWith(handleRequest(event.request));
});