fixing curl normalization

This commit is contained in:
Ritul Jadhav 2026-04-08 17:47:01 +05:30
parent b80d9404c4
commit f3fcc1ef51

View File

@ -30,11 +30,81 @@ function createExecutionError(message, extra = {}) {
return error; return error;
} }
function skipShellIndentation(input, index) {
let cursor = index;
while (cursor < input.length && /[\t \f\v\u00a0]/.test(input[cursor])) {
cursor += 1;
}
return cursor;
}
function normalizeCommand(command) { function normalizeCommand(command) {
return String(command || '') const input = String(command || '')
.replace(/\r\n/g, '\n') .replace(/\r\n/g, '\n')
.replace(/\\\n\s*/g, ' ') .replace(/\r/g, '\n');
.trim();
let output = '';
let quote = null;
for (let index = 0; index < input.length; index += 1) {
const char = input[index];
if (quote === '\'') {
output += char;
if (char === '\'') {
quote = null;
}
continue;
}
if (quote === '"') {
output += char;
if (char === '\\' && index + 1 < input.length) {
output += input[index + 1];
index += 1;
continue;
}
if (char === '"') {
quote = null;
}
continue;
}
if (char === '\'' || char === '"') {
quote = char;
output += char;
continue;
}
if (char === '\\') {
const nextChar = input[index + 1];
if (nextChar === '\n') {
output += ' ';
index = skipShellIndentation(input, index + 2) - 1;
continue;
}
if (nextChar === 'n') {
output += ' ';
index = skipShellIndentation(input, index + 2) - 1;
continue;
}
if (nextChar === 'r' && input[index + 2] === 'n') {
output += ' ';
index = skipShellIndentation(input, index + 3) - 1;
continue;
}
}
output += char;
}
return output.trim();
} }
function tokenizeCurlCommand(command) { function tokenizeCurlCommand(command) {
@ -164,20 +234,112 @@ function replaceTokensInJsonValue(value, tokenValues = {}) {
return value; return value;
} }
function normalizeJsonFormattingEscapes(value) {
const input = String(value || '')
.replace(/\r\n/g, '\n')
.replace(/\r/g, '\n');
let output = '';
let inString = false;
let escaping = false;
for (let index = 0; index < input.length; index += 1) {
const char = input[index];
if (inString) {
output += char;
if (escaping) {
escaping = false;
continue;
}
if (char === '\\') {
escaping = true;
continue;
}
if (char === '"') {
inString = false;
}
continue;
}
if (char === '"') {
inString = true;
output += char;
continue;
}
if (char === '\\') {
const nextChar = input[index + 1];
if (nextChar === 'n') {
output += '\n';
index += 1;
continue;
}
if (nextChar === 'r' && input[index + 2] === 'n') {
output += '\n';
index += 2;
continue;
}
if (nextChar === 'r') {
output += '\n';
index += 1;
continue;
}
if (nextChar === 't') {
output += '\t';
index += 1;
continue;
}
}
output += char;
}
return output;
}
function parseJsonLikeArgument(value) {
const trimmed = String(value || '').trim();
if (
!trimmed
|| !(
(trimmed.startsWith('{') && trimmed.endsWith('}'))
|| (trimmed.startsWith('[') && trimmed.endsWith(']'))
)
) {
return null;
}
try {
return JSON.parse(trimmed);
} catch {
const normalized = normalizeJsonFormattingEscapes(trimmed);
if (normalized === trimmed) {
return null;
}
try {
return JSON.parse(normalized);
} catch {
return null;
}
}
}
function hydrateDataArgument(rawArgument, tokenValues = {}) { function hydrateDataArgument(rawArgument, tokenValues = {}) {
const trimmed = String(rawArgument || '').trim(); const trimmed = String(rawArgument || '').trim();
if (!trimmed) return ''; if (!trimmed) return '';
if ( const parsedJson = parseJsonLikeArgument(trimmed);
(trimmed.startsWith('{') && trimmed.endsWith('}')) if (parsedJson !== null) {
|| (trimmed.startsWith('[') && trimmed.endsWith(']')) return JSON.stringify(replaceTokensInJsonValue(parsedJson, tokenValues));
) {
try {
const parsed = JSON.parse(trimmed);
return JSON.stringify(replaceTokensInJsonValue(parsed, tokenValues));
} catch {
return replaceTokensInString(rawArgument, tokenValues);
}
} }
return replaceTokensInString(rawArgument, tokenValues); return replaceTokensInString(rawArgument, tokenValues);