From 00abe1fe937df93eb7c66a6bac329e4011ef3a5a Mon Sep 17 00:00:00 2001 From: Rias Date: Sun, 1 Sep 2024 14:31:54 +0200 Subject: [PATCH] Use base64 encoding to more reliably send data across --- bin/mjml.mjs | 15 ++++++++++++--- src/Mjml.php | 6 ++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/bin/mjml.mjs b/bin/mjml.mjs index cfa0e70..927e54a 100644 --- a/bin/mjml.mjs +++ b/bin/mjml.mjs @@ -1,6 +1,6 @@ import mjml2html from 'mjml' -const args = JSON.parse(process.argv.slice(2)); +const args = JSON.parse(atob(process.argv.slice(2))); const mjml = args[0]; const options = args[1]; @@ -12,8 +12,17 @@ try { } catch (exception) { const errorString = JSON.stringify({mjmlError: exception.toString()}); - process.stdout.write(errorString); + process.stdout.write(utoa(errorString)); process.exit(0); } -process.stdout.write(JSON.stringify(result)); +process.stdout.write(utoa(JSON.stringify(result))); + +/** + * Unicode to ASCII (encode data to Base64) + * @param {string} data + * @return {string} + */ +function utoa(data) { + return btoa(unescape(encodeURIComponent(data))); +} diff --git a/src/Mjml.php b/src/Mjml.php index f09fe23..3f43c3e 100755 --- a/src/Mjml.php +++ b/src/Mjml.php @@ -176,7 +176,7 @@ protected function getCommand(array $arguments): array return [ (new ExecutableFinder())->find('node', 'node', $extraDirectories), 'mjml.mjs', - json_encode(array_values($arguments)), + base64_encode(json_encode(array_values($arguments))), ]; } @@ -219,6 +219,8 @@ protected function getLocalResult(array $arguments): string throw new ProcessFailedException($process); } - return $process->getOutput(); + $output = last(explode("\n", $process->getOutput())); + + return base64_decode($output); } }