fix: jsonata $base64decode/$base64encode in Web Worker (#2275)

This commit is contained in:
min23asdw 2026-06-20 17:40:37 +07:00 committed by GitHub
parent 08e5c13da4
commit 0d2af8ce00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 0 deletions

View File

@ -51,6 +51,18 @@ class JsonataQuery extends Operation {
try { try {
const expression = jsonata(query); const expression = jsonata(query);
// Override built-in base64 functions which fail in Web Worker
// context where `window` is undefined. The jsonata library falls
// back to `global.Buffer` which also does not exist in workers.
// `atob`/`btoa` are available in both browser and worker scopes.
expression.registerFunction("base64decode", (str) => {
if (typeof str === "undefined") return undefined;
return atob(str);
}, "<s-:s>");
expression.registerFunction("base64encode", (str) => {
if (typeof str === "undefined") return undefined;
return btoa(str);
}, "<s-:s>");
result = await expression.evaluate(jsonObj); result = await expression.evaluate(jsonObj);
} catch (err) { } catch (err) {
throw new OperationError( throw new OperationError(

View File

@ -218,6 +218,7 @@ module.exports = {
testOpHtml(browser, "JSON Beautify", "{a:1}", ".json-dict .json-literal", "1"); testOpHtml(browser, "JSON Beautify", "{a:1}", ".json-dict .json-literal", "1");
// testOp(browser, "JSON Minify", "test input", "test_output"); // testOp(browser, "JSON Minify", "test input", "test_output");
// testOp(browser, "JSON to CSV", "test input", "test_output"); // testOp(browser, "JSON to CSV", "test input", "test_output");
testOp(browser, "Jsonata Query", '{"a": "SGVsbG8gV29ybGQh"}', '"Hello World!"', ["$base64decode($.a)"]);
// testOp(browser, "JWT Decode", "test input", "test_output"); // testOp(browser, "JWT Decode", "test input", "test_output");
// testOp(browser, "JWT Sign", "test input", "test_output"); // testOp(browser, "JWT Sign", "test input", "test_output");
// testOp(browser, "JWT Verify", "test input", "test_output"); // testOp(browser, "JWT Verify", "test input", "test_output");

View File

@ -548,4 +548,27 @@ TestRegister.addTests([
}, },
], ],
}, },
// Base64 functions (issue #2063)
{
name: "Jsonata: $base64decode",
input: "{}",
expectedOutput: '"Hello World!"',
recipeConfig: [
{
op: "Jsonata Query",
args: ['$base64decode("SGVsbG8gV29ybGQh")'],
},
],
},
{
name: "Jsonata: $base64encode",
input: "{}",
expectedOutput: '"SGVsbG8gV29ybGQh"',
recipeConfig: [
{
op: "Jsonata Query",
args: ['$base64encode("Hello World!")'],
},
],
},
]); ]);