Ns/web dav range fixes (#2459)
Docker Image CI / build-and-push-image (push) Has been cancelled
Maintain Release Merge PR / update-release-pr (push) Has been cancelled
release-please / release-please (push) Has been cancelled
test / test-backend (24.x) (push) Has been cancelled
test / API tests (node env, api-test) (24.x) (push) Has been cancelled
test / puterjs (node env, vitest) (24.x) (push) Has been cancelled

* better calculation for ContentLength

* lets try that again
This commit is contained in:
Neal Shah
2026-02-10 16:11:01 -08:00
committed by GitHub
parent 4f84decdec
commit 56ee3d23df
@@ -1,6 +1,6 @@
import { fsOperations, getProperMimeType } from '../utils.mjs';
const parseRangeHeader = ( rangeHeader ) => {
const parseRangeHeader = (rangeHeader) => {
// Check if this is a multipart range request
if ( rangeHeader.includes(',') ) {
// For now, we'll only serve the first range in multipart requests
@@ -32,11 +32,11 @@ const parseRangeHeader = ( rangeHeader ) => {
/**
* @type {import('./method.mjs').HandlerFunction}
*/
export const HEAD_GET = async ( req, res, _filePath, fileNode, _headerLockToken ) => {
export const HEAD_GET = async (req, res, _filePath, fileNode, _headerLockToken) => {
try {
const exists = await fileNode?.exists();
if ( ! exists ) {
res.status(404).end( 'File not found');
res.status(404).end('File not found');
return;
}
@@ -63,8 +63,6 @@ export const HEAD_GET = async ( req, res, _filePath, fileNode, _headerLockToken
// Set ETag
headers['ETag'] = `"${fileStat.uid}-${Math.floor(fileStat.modified)}"`;
res.set(headers);
// For HEAD requests, only send headers, no body
if ( req.method === 'HEAD' ) {
res.status(200).end();
@@ -73,7 +71,7 @@ export const HEAD_GET = async ( req, res, _filePath, fileNode, _headerLockToken
// For GET requests, send the file content
if ( fileStat.is_dir ) {
res.status(400).end( 'Cannot GET a directory');
res.status(400).end('Cannot GET a directory');
return;
}
@@ -107,6 +105,7 @@ export const HEAD_GET = async ( req, res, _filePath, fileNode, _headerLockToken
const totalSize = fileSize !== null ? fileSize : '*';
const contentRange = `bytes ${start}-${actualEnd}/${totalSize}`;
res.set('Content-Range', contentRange);
headers['Content-Length'] = (actualEnd - start) + 1;
}
// If this was a multipart request, modify the range header to only include the first range
@@ -115,20 +114,21 @@ export const HEAD_GET = async ( req, res, _filePath, fileNode, _headerLockToken
}
}
}
res.set(headers);
const stream = await fsOperations.read(fileNode, options);
stream.on('data', ( data ) => {
stream.on('data', (data) => {
res.write(data);
});
stream.on('end', () => {
res.end();
});
stream.on('error', ( error ) => {
stream.on('error', (error) => {
console.error('Stream error:', error);
res.status(500).end( 'Internal server error');
res.status(500).end('Internal server error');
});
} catch ( error ) {
console.error('HEAD or GET error:', error);
res.status(500).end( 'Internal Server Error');
res.status(500).end('Internal Server Error');
}
};