bewcloud/pages/caldav.ts
Bruno Bernardino c26cae625e
Remove fresh
This implements a huge change, where Fresh is removed as a framework and serving files, allowing more control over importing, bundling, and serving files and components.

The biggest challenge was to continue making sure that there weren't too many places to look into for import versions, and `PasswordlessPasskeyLogin.tsx` became a prototype in migrating a component to fully SSR, no need for frontend parsing (via Babel) or bundling (via a custom-script, downloading frontend dependencies from esm.sh). Still, there are too many components to migrate like that, and it's all working, so I likely won't even attempt it unless there's some bug, new feature, or security vulnerability to address that warrants a rewrite of those.

This also updates all dependencies (except `@libs/xml` because that still causes some breaking in DAV endpoints), including Deno!

All other advantages can be seen in the related issues, and the breaking change this (v4.0.0) introduces is related simply to `config.email.tlsMode` (which had a deprecation warning throughout v3), and because, while I tested many things exhaustively, it's not impossible something broke that I didn't see.

Closes #141
Closes #132
2026-02-20 10:54:31 +00:00

88 lines
2.2 KiB
TypeScript

import page, { RequestHandlerParams } from '/lib/page.ts';
import { AppConfig } from '/lib/config.ts';
async function get({ request, user, match }: RequestHandlerParams) {
const calendarConfig = await AppConfig.getCalendarConfig();
if (!calendarConfig.enableCalDavServer) {
return new Response('Not Found', { status: 404 });
}
if (!user) {
return new Response('Unauthorized', {
status: 401,
headers: { 'www-authenticate': 'Basic realm="bewCloud", charset="UTF-8"' },
});
}
let { path } = match.pathname.groups;
if (!path) {
path = '/';
}
const userId = user.id;
try {
const requestBodyText = await request.clone().text();
// Remove the `/caldav/` prefix from the hrefs in the request
let parsedRequestBodyText: string | undefined = requestBodyText.replaceAll('<href>/caldav/', `<href>/`).replaceAll(
':href>/caldav/',
`:href>/`,
);
// The spec doesn't allow a body for GET or HEAD requests (and Deno fails if you try)
if (request.method === 'GET' || request.method === 'HEAD') {
parsedRequestBodyText = undefined;
}
const response = await fetch(`${calendarConfig.calDavUrl}/${path}`, {
headers: {
...Object.fromEntries(request.headers.entries()),
'X-Remote-User': `${userId}`,
},
method: request.method,
body: parsedRequestBodyText,
});
if (response.status === 204) {
return new Response(null, { status: 204 });
}
const responseBodyText = await response.clone().text();
// Add the `/caldav/` prefix to the hrefs in the response
const parsedBodyResponseText = responseBodyText.replaceAll('<href>/', `<href>/caldav/`).replaceAll(
':href>/',
`:href>/caldav/`,
);
return new Response(parsedBodyResponseText, {
status: response.status,
headers: response.headers,
});
} catch (error) {
console.error(error);
}
return new Response(null, { status: 405 });
}
export default page({
get,
put: get,
delete: get,
options: get,
copy: get,
move: get,
mkcol: get,
mkcalendar: get,
lock: get,
unlock: get,
propfind: get,
proppatch: get,
report: get,
accessMode: 'public',
});