From 10cbe7a6370ef12a6e4e059e1bef635209004266 Mon Sep 17 00:00:00 2001 From: Linus Rath <139418639+rathlinus@users.noreply.github.com> Date: Wed, 8 Apr 2026 13:16:28 +0200 Subject: [PATCH] fix: stream WebDAV PUT uploads to avoid buffering in memory #162 --- app/api/webdav/route.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/api/webdav/route.ts b/app/api/webdav/route.ts index 700c201d..91cc8f6a 100644 --- a/app/api/webdav/route.ts +++ b/app/api/webdav/route.ts @@ -79,10 +79,14 @@ export async function POST(request: NextRequest) { const overwrite = request.headers.get('Overwrite'); if (overwrite) upstreamHeaders['Overwrite'] = overwrite; - // Forward request body for methods that need it - let body: ArrayBuffer | null = null; - if (method === 'PROPFIND' || method === 'PUT') { + // Forward request body for methods that need it. + // PUT streams directly to upstream to avoid buffering large uploads in memory. + // PROPFIND bodies are small XML and are read fully. + let body: ArrayBuffer | ReadableStream | null = null; + if (method === 'PROPFIND') { body = await request.arrayBuffer(); + } else if (method === 'PUT') { + body = request.body; } const response = await fetch(targetUrl, { @@ -90,7 +94,9 @@ export async function POST(request: NextRequest) { headers: upstreamHeaders, body, redirect: 'follow', - }); + // `duplex: 'half'` is required by undici when sending a streaming request body. + ...(method === 'PUT' ? { duplex: 'half' } : {}), + } as RequestInit & { duplex?: 'half' }); // For file downloads (GET), stream the response back if (method === 'GET') {