fix: jsonata $base64decode/$base64encode in Web Worker
The jsonata library checks `typeof window !== 'undefined'` to decide whether to use browser `atob`/`btoa` or Node.js `Buffer.from` for base64 operations. Since CyberChef runs operations in a Web Worker where `window` is undefined, it falls back to `global.Buffer` which also does not exist, causing "Cannot read properties of undefined (reading 'from')". Fix by registering custom base64 functions on the expression that use `atob`/`btoa` directly, which are available in both browser and Web Worker scopes. Closes #2063
This commit is contained in:
parent
9cf82cc1a1
commit
597e7f33f7
@ -51,6 +51,18 @@ class JsonataQuery extends Operation {
|
||||
|
||||
try {
|
||||
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);
|
||||
} catch (err) {
|
||||
throw new OperationError(
|
||||
|
||||
@ -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!")'],
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user