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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user