Skip to content

Commit

Permalink
Use base64 encoding to more reliably send data across
Browse files Browse the repository at this point in the history
  • Loading branch information
riasvdv committed Sep 1, 2024
1 parent 405dfb6 commit 00abe1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
15 changes: 12 additions & 3 deletions bin/mjml.mjs
Original file line number Diff line number Diff line change
@@ -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];
Expand All @@ -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)));
}
6 changes: 4 additions & 2 deletions src/Mjml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
];
}

Expand Down Expand Up @@ -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);
}
}

1 comment on commit 00abe1f

@Lambutty
Copy link

@Lambutty Lambutty commented on 00abe1f Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im pretty this commit is breaking my code since atob is not globally available in node versions < 16

Please sign in to comment.