fix: stream WebDAV PUT uploads to avoid buffering in memory #162

This commit is contained in:
Linus Rath
2026-04-08 13:16:28 +02:00
parent 79418f013a
commit 10cbe7a637
+10 -4
View File
@@ -79,10 +79,14 @@ export async function POST(request: NextRequest) {
const overwrite = request.headers.get('Overwrite'); const overwrite = request.headers.get('Overwrite');
if (overwrite) upstreamHeaders['Overwrite'] = overwrite; if (overwrite) upstreamHeaders['Overwrite'] = overwrite;
// Forward request body for methods that need it // Forward request body for methods that need it.
let body: ArrayBuffer | null = null; // PUT streams directly to upstream to avoid buffering large uploads in memory.
if (method === 'PROPFIND' || method === 'PUT') { // PROPFIND bodies are small XML and are read fully.
let body: ArrayBuffer | ReadableStream<Uint8Array> | null = null;
if (method === 'PROPFIND') {
body = await request.arrayBuffer(); body = await request.arrayBuffer();
} else if (method === 'PUT') {
body = request.body;
} }
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
@@ -90,7 +94,9 @@ export async function POST(request: NextRequest) {
headers: upstreamHeaders, headers: upstreamHeaders,
body, body,
redirect: 'follow', 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 // For file downloads (GET), stream the response back
if (method === 'GET') { if (method === 'GET') {