JavaScript Python PHP
async function executar(values) {
async function formatFrontendCode(v) {
const input = feText(v.input), language = feChoice(v.language, ['CSS', 'JavaScript']), mode = feChoice(v.mode, ['Formatar', 'Minificar']);
if (!input.trim())
return { text: '' };
if (mode === 'Formatar') {
const prettier = await import('prettier/standalone');
const plugins = language === 'CSS' ? [await import('prettier/plugins/postcss')] : [await import('prettier/plugins/babel'), await import('prettier/plugins/estree')];
return { text: await prettier.format(input, { parser: language === 'CSS' ? 'css' : 'babel', plugins, tabWidth: 2, endOfLine: 'lf', embeddedLanguageFormatting: 'off' }) };
}
if (language === 'CSS') {
const { minify } = await import('csso');
return { text: minify(input, { restructure: false, comments: false }).css };
}
const { minify } = await import('terser');
return { text: (await minify(input, { module: true, compress: false, mangle: false, format: { comments: false } })).code || '' };
}
function feText(value, max = 100000) {
if (typeof value !== 'string' || value.length > max)
throw new Error(`Use texto com até ${max} caracteres.`);
if (/[\u0000-\u0008\u000b\u000c\u000e-\u001f\ufffe\uffff]/u.test(value) || /[\uD800-\uDFFF]/u.test(value))
throw new Error('Há caracteres de controle ou Unicode inválidos na entrada.');
return value;
}
function feChoice(value, choices) {
if (!choices.includes(value))
throw new Error('Opção não suportada: ' + value);
return value;
}
for (const value of Object.values(values)) if (typeof value !== "string" || value.length > 100000) throw new Error("Use até 100 mil caracteres por campo.");
return await formatFrontendCode(values);
}
import json
import subprocess
def executar(values):
# Requer Node.js 22+ e os pacotes npm indicados abaixo; toda a lógica está visível.
source = r'''
async function executar(values) {
async function formatFrontendCode(v) {
const input = feText(v.input), language = feChoice(v.language, ['CSS', 'JavaScript']), mode = feChoice(v.mode, ['Formatar', 'Minificar']);
if (!input.trim())
return { text: '' };
if (mode === 'Formatar') {
const prettier = await import('prettier/standalone');
const plugins = language === 'CSS' ? [await import('prettier/plugins/postcss')] : [await import('prettier/plugins/babel'), await import('prettier/plugins/estree')];
return { text: await prettier.format(input, { parser: language === 'CSS' ? 'css' : 'babel', plugins, tabWidth: 2, endOfLine: 'lf', embeddedLanguageFormatting: 'off' }) };
}
if (language === 'CSS') {
const { minify } = await import('csso');
return { text: minify(input, { restructure: false, comments: false }).css };
}
const { minify } = await import('terser');
return { text: (await minify(input, { module: true, compress: false, mangle: false, format: { comments: false } })).code || '' };
}
function feText(value, max = 100000) {
if (typeof value !== 'string' || value.length > max)
throw new Error(`Use texto com até ${max} caracteres.`);
if (/[\u0000-\u0008\u000b\u000c\u000e-\u001f\ufffe\uffff]/u.test(value) || /[\uD800-\uDFFF]/u.test(value))
throw new Error('Há caracteres de controle ou Unicode inválidos na entrada.');
return value;
}
function feChoice(value, choices) {
if (!choices.includes(value))
throw new Error('Opção não suportada: ' + value);
return value;
}
for (const value of Object.values(values)) if (typeof value !== "string" || value.length > 100000) throw new Error("Use até 100 mil caracteres por campo.");
return await formatFrontendCode(values);
}
let payload = "";
for await (const chunk of process.stdin) payload += chunk;
try { const input = JSON.parse(payload); const result = await executar(input.values, input.image); process.stdout.write(JSON.stringify({ok: true, result}, (_, value) => value instanceof Uint8Array ? Array.from(value) : value)); }
catch (error) { process.stdout.write(JSON.stringify({ok: false, error: error.message})); }
'''
payload = json.dumps({"values": values}, ensure_ascii=True)
process = subprocess.run(["node", "--input-type=module", "-e", source], input=payload, capture_output=True, text=True, timeout=15, check=True)
response = json.loads(process.stdout)
if not response["ok"]:
raise ValueError(response["error"])
return response["result"]
<?php
function executar(array $values): array {
// Requer Node.js 22+ e os pacotes npm indicados abaixo; toda a lógica está visível.
$source = <<<'JAVASCRIPT'
async function executar(values) {
async function formatFrontendCode(v) {
const input = feText(v.input), language = feChoice(v.language, ['CSS', 'JavaScript']), mode = feChoice(v.mode, ['Formatar', 'Minificar']);
if (!input.trim())
return { text: '' };
if (mode === 'Formatar') {
const prettier = await import('prettier/standalone');
const plugins = language === 'CSS' ? [await import('prettier/plugins/postcss')] : [await import('prettier/plugins/babel'), await import('prettier/plugins/estree')];
return { text: await prettier.format(input, { parser: language === 'CSS' ? 'css' : 'babel', plugins, tabWidth: 2, endOfLine: 'lf', embeddedLanguageFormatting: 'off' }) };
}
if (language === 'CSS') {
const { minify } = await import('csso');
return { text: minify(input, { restructure: false, comments: false }).css };
}
const { minify } = await import('terser');
return { text: (await minify(input, { module: true, compress: false, mangle: false, format: { comments: false } })).code || '' };
}
function feText(value, max = 100000) {
if (typeof value !== 'string' || value.length > max)
throw new Error(`Use texto com até ${max} caracteres.`);
if (/[\u0000-\u0008\u000b\u000c\u000e-\u001f\ufffe\uffff]/u.test(value) || /[\uD800-\uDFFF]/u.test(value))
throw new Error('Há caracteres de controle ou Unicode inválidos na entrada.');
return value;
}
function feChoice(value, choices) {
if (!choices.includes(value))
throw new Error('Opção não suportada: ' + value);
return value;
}
for (const value of Object.values(values)) if (typeof value !== "string" || value.length > 100000) throw new Error("Use até 100 mil caracteres por campo.");
return await formatFrontendCode(values);
}
let payload = "";
for await (const chunk of process.stdin) payload += chunk;
try { const input = JSON.parse(payload); const result = await executar(input.values, input.image); process.stdout.write(JSON.stringify({ok: true, result}, (_, value) => value instanceof Uint8Array ? Array.from(value) : value)); }
catch (error) { process.stdout.write(JSON.stringify({ok: false, error: error.message})); }
JAVASCRIPT;
$payload = json_encode(["values" => $values], JSON_THROW_ON_ERROR);
$process = proc_open(["node", "--input-type=module", "-e", $source], [["pipe", "r"], ["pipe", "w"], ["pipe", "w"]], $pipes);
if (!is_resource($process)) { throw new RuntimeException("Não foi possível iniciar Node.js."); }
$offset = 0;
while ($offset < strlen($payload)) {
$written = fwrite($pipes[0], substr($payload, $offset));
if ($written === false || $written === 0) { proc_terminate($process); foreach ($pipes as $pipe) fclose($pipe); proc_close($process); throw new RuntimeException("Falha ao enviar a entrada."); }
$offset += $written;
}
fclose($pipes[0]); stream_set_timeout($pipes[1], 15); $output = stream_get_contents($pipes[1]); $timeout = stream_get_meta_data($pipes[1])["timed_out"]; fclose($pipes[1]); if ($timeout) proc_terminate($process); $stderr = stream_get_contents($pipes[2]); fclose($pipes[2]); $status = proc_close($process);
if ($timeout || $status !== 0) { throw new RuntimeException("Falha técnica no Node.js; confira as dependências npm."); }
$response = json_decode($output, true, 512, JSON_THROW_ON_ERROR);
if (!$response["ok"]) { throw new InvalidArgumentException($response["error"]); }
return $response["result"];
}