fix(jsontools): read Node.js input from stdin (#14190)

This commit is contained in:
umutcagand
2026-10-19 17:21:44 -07:00
committed by GitHub
parent 741642d7a9
commit 0650a25040
2 changed files with 77 additions and 6 deletions
+13 -6
View File
@@ -22,14 +22,17 @@ fi
# Define json tools for each method
case "$JSONTOOLS_METHOD" in
node)
# node doesn't make it easy to deal with stdin, so we pass it as an argument with xargs -0
# Read stdin directly so input is not limited by command-line argument size.
function pp_json() {
xargs -0 node -e 'console.log(JSON.stringify(JSON.parse(process.argv[1]), null, 4));'
node -e '
const input = require("fs").readFileSync(0, "utf8");
console.log(JSON.stringify(JSON.parse(input), null, 4));
'
}
function is_json() {
xargs -0 node -e '
node -e '
try {
json = JSON.parse(process.argv[1]);
JSON.parse(require("fs").readFileSync(0, "utf8"));
console.log("true");
process.exit(0);
} catch (e) {
@@ -39,10 +42,14 @@ case "$JSONTOOLS_METHOD" in
'
}
function urlencode_json() {
xargs -0 node -e "console.log(encodeURIComponent(process.argv[1]))"
node -e '
console.log(encodeURIComponent(require("fs").readFileSync(0, "utf8")));
'
}
function urldecode_json() {
xargs -0 node -e "console.log(decodeURIComponent(process.argv[1]))"
node -e '
console.log(decodeURIComponent(require("fs").readFileSync(0, "utf8")));
'
}
;;
python3)