mirror of
https://github.com/usebruno/bruno.git
synced 2026-07-01 08:34:07 +00:00
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:
@@ -4,10 +4,13 @@ const filter = require('lodash/filter');
|
||||
const find = require('lodash/find');
|
||||
const decomment = require('decomment');
|
||||
const crypto = require('node:crypto');
|
||||
const fs = require('node:fs/promises');
|
||||
const fs = require('node:fs');
|
||||
const { mergeHeaders, mergeScripts, mergeVars, mergeAuth, getTreePathFromCollectionToItem } = require('../utils/collection');
|
||||
const { buildFormUrlEncodedPayload } = require('../utils/form-data');
|
||||
const path = require('node:path');
|
||||
const { isLargeFile } = require('../utils/filesystem');
|
||||
|
||||
const STREAMING_FILE_SIZE_THRESHOLD = 20 * 1024 * 1024; // 20MB
|
||||
|
||||
const prepareRequest = async (item = {}, collection = {}) => {
|
||||
const request = item?.request;
|
||||
@@ -311,8 +314,14 @@ const prepareRequest = async (item = {}, collection = {}) => {
|
||||
}
|
||||
|
||||
try {
|
||||
const fileContent = await fs.readFile(filePath);
|
||||
axiosRequest.data = fileContent;
|
||||
// Large files can cause "JavaScript heap out of memory" errors when loaded entirely into memory.
|
||||
if (isLargeFile(filePath, STREAMING_FILE_SIZE_THRESHOLD)) {
|
||||
// For large files: Use streaming to avoid memory issues
|
||||
axiosRequest.data = fs.createReadStream(filePath);
|
||||
} else {
|
||||
// For smaller files: Use synchronous read for better performance
|
||||
axiosRequest.data = fs.readFileSync(filePath);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error reading file:', error);
|
||||
}
|
||||
|
||||
@@ -158,6 +158,22 @@ const validateName = (name) => {
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 = {
|
||||
exists,
|
||||
isSymbolicLink,
|
||||
@@ -173,5 +189,6 @@ module.exports = {
|
||||
stripExtension,
|
||||
getSubDirectories,
|
||||
sanitizeName,
|
||||
validateName
|
||||
validateName,
|
||||
isLargeFile
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user