fix: import ndjson-curl handling binary data check with type file or not (#7210)

* fix: import ndjson-curl handling binary data check with type file

* chore: handle ansi escaping manually

* chore: avoid duplicate replacements

---------

Co-authored-by: shubh-bruno <shubh-bruno@shubh-bruno.local>
This commit is contained in:
shubh-bruno
2026-02-19 14:56:56 +05:30
committed by GitHub
parent c093354938
commit cb716e5978
2 changed files with 33 additions and 4 deletions

View File

@@ -173,8 +173,8 @@ const curlToJson = (curlCommand) => {
requestJson.headers = {};
}
requestJson.headers['Content-Type'] = 'multipart/form-data';
} else if (request.isDataBinary) {
Object.assign(requestJson, getFilesString(request));
} else if (request.isDataBinary && (typeof request.data === 'string' && request.data.startsWith('@'))) {
Object.assign(requestJson, getFilesString(request)); // file case
} else if (typeof request.data === 'string' || typeof request.data === 'number') {
Object.assign(requestJson, getDataString(request));
}

View File

@@ -522,8 +522,8 @@ const cleanRequest = (request) => {
* Handles escape sequences, line continuations, and method concatenation
*/
const cleanCurlCommand = (curlCommand) => {
// Handle escape sequences
curlCommand = curlCommand.replace(/\$('.*')/g, (match, group) => group);
// Handle bash ANSI $'..' escapes by decoding common sequences
curlCommand = curlCommand.replace(/\$'((?:\\.|[^'])*)'/g, (match, group) => quoteForShell(decodeAnsiEscapes(group)));
// Convert escaped single quotes to shell quote pattern
curlCommand = curlCommand.replace(/\\'(?!')/g, '\'\\\'\'');
// Fix concatenated HTTP methods
@@ -555,4 +555,33 @@ const fixConcatenatedMethods = (command) => {
return command;
};
/**
* Decode bash ANSI $'..' escape sequences
*/
const decodeAnsiEscapes = (value) => {
return value.replace(/\\(\\|'|n|r|t|v|f|a|b|x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4})/g, (match, seq) => {
switch (seq[0]) {
case '\\': return '\\';
case '\'': return '\'';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
case 'v': return '\v';
case 'f': return '\f';
case 'a': return '\x07';
case 'b': return '\b';
case 'x': return String.fromCharCode(parseInt(seq.slice(1), 16));
case 'u': return String.fromCharCode(parseInt(seq.slice(1), 16));
default: return match;
}
});
};
/**
* Wrap value in single quotes while preserving embedded single quotes
*/
const quoteForShell = (value) => {
return `'${value.replace(/'/g, '\'\\\'\'')}'`;
};
export default parseCurlCommand;