Files
todotree/vite.config.ts

43 lines
1.5 KiB
TypeScript

import { Agent as HttpAgent } from 'node:http'
import { Agent as HttpsAgent } from 'node:https'
import react from '@vitejs/plugin-react'
import { defineConfig, loadEnv, type ProxyOptions } from 'vite'
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
const proxy: Record<string, ProxyOptions> = {}
if (env.CALDAV_URL) {
const target = new URL(env.CALDAV_URL)
const basePath = target.pathname.replace(/\/$/, '')
// Credentials never reach the client bundle: the dev server injects
// Basic auth on the proxied request, browser code only ever calls /caldav/.
const headers: Record<string, string> = {}
if (env.CALDAV_USERNAME && env.CALDAV_PASSWORD) {
const token = Buffer.from(`${env.CALDAV_USERNAME}:${env.CALDAV_PASSWORD}`).toString('base64')
headers.Authorization = `Basic ${token}`
}
proxy['/caldav'] = {
target: target.origin,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/caldav/, basePath),
headers,
// The CalDAV server answers every request with `Connection: close`,
// but http-proxy's default agent still pools/reuses sockets — that
// races with the server closing them and intermittently surfaces as
// a 503 (ECONNRESET) here. A fresh connection per request avoids it.
agent: target.protocol === 'https:'
? new HttpsAgent({ keepAlive: false })
: new HttpAgent({ keepAlive: false }),
}
}
return {
plugins: [react()],
server: { proxy },
}
})