fix: improve file upload handling in prepare-request to use streaming (#5637)

* fix: improve file upload handling in prepare-request to use streaming
* feat: add unit tests

---------

Co-authored-by: Bijin Bruno <bijin@usebruno.com>
This commit is contained in:
Sanjai Kumar
2025-09-30 22:47:09 +05:30
committed by GitHub
parent 14966f6e6c
commit c7029d1cda
7 changed files with 222 additions and 26 deletions

View File

@@ -343,6 +343,21 @@ const getPaths = async (source) => {
return paths;
}
/**
* Checks if a file is larger than a given threshold.
* @param {string} filePath - The path to the file.
* @param {number} threshold - The threshold in bytes. Default is 10MB.
* @returns {boolean} True if the file is larger than the threshold, false otherwise.
*/
const isLargeFile = (filePath, threshold = 10 * 1024 * 1024) => {
if (!isFile(filePath)) {
throw new Error(`File ${filePath} is not a file`);
}
const size = fs.statSync(filePath).size;
return size > threshold;
};
module.exports = {
isValidPathname,
@@ -373,5 +388,6 @@ module.exports = {
safeWriteFileSync,
copyPath,
removePath,
getPaths
getPaths,
isLargeFile
};

View File

@@ -1,4 +1,5 @@
const { sanitizeName, isWSLPath, normalizeWSLPath, normalizeAndResolvePath } = require('./filesystem.js');
const { sanitizeName, isWSLPath, normalizeWSLPath, normalizeAndResolvePath, isLargeFile } = require('./filesystem.js');
const fs = require('fs-extra');
describe('sanitizeName', () => {
it('should replace invalid characters with hyphens', () => {
@@ -29,6 +30,54 @@ describe('sanitizeName', () => {
});
});
describe('isLargeFile', () => {
let existsSyncSpy;
let lstatSyncSpy;
let statSyncSpy;
beforeEach(() => {
existsSyncSpy = jest.spyOn(fs, 'existsSync');
lstatSyncSpy = jest.spyOn(fs, 'lstatSync');
statSyncSpy = jest.spyOn(fs, 'statSync');
});
afterEach(() => {
jest.restoreAllMocks();
});
it('should return false when file size is below default threshold (10MB)', () => {
existsSyncSpy.mockReturnValue(true);
lstatSyncSpy.mockReturnValue({ isFile: () => true });
statSyncSpy.mockReturnValue({ size: 5 * 1024 * 1024 }); // 5MB
expect(isLargeFile('/path/small.bin')).toBe(false);
});
it('should return true when file size is above default threshold (10MB)', () => {
existsSyncSpy.mockReturnValue(true);
lstatSyncSpy.mockReturnValue({ isFile: () => true });
statSyncSpy.mockReturnValue({ size: 15 * 1024 * 1024 }); // 15MB
expect(isLargeFile('/path/large.bin')).toBe(true);
});
it('should respect custom threshold (args true or false)', () => {
existsSyncSpy.mockReturnValue(true);
lstatSyncSpy.mockReturnValue({ isFile: () => true });
statSyncSpy.mockReturnValue({ size: 50 });
expect(isLargeFile('/path/file.bin', 100)).toBe(false); // 50 < 100
expect(isLargeFile('/path/file.bin', 10)).toBe(true); // 50 > 10
});
it('should throw on invalid values (not a file)', () => {
existsSyncSpy.mockReturnValue(false);
lstatSyncSpy.mockReturnValue({ isFile: () => false });
expect(() => isLargeFile('/path/not-a-file.bin')).toThrow('File /path/not-a-file.bin is not a file');
});
});
describe('WSL Path Utilities', () => {
describe('isWSLPath', () => {
it('should identify WSL paths starting with double backslash', () => {