Decoding a base64 string

Hi folks.

I have a base64 string that I wish to decode in a JS node.

My problem is that we cannot use atob or Buffer in the JS nodes.

Has anyone gotten around this yet?

Here is my code so far:

var base64String = inputs.input1.result;
var base64Cleaned = base64String.replace(/-/g, '+').replace(/_/g, '/');

var jsonPayload = decodeURIComponent(atob(base64Cleaned).toString().split('').map(function(c) {
    return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));

return { result: JSON.parse(jsonPayload)};

Thanks

Hi Richard,

there are 2 flow functions regarding base64, but as you are advanced with JS, you can also write custom code in the ‘Javascript’ node.
image

image

Thank you @stayfoolish

I am not dealing with a file. Just a base64 string that needs to be decoded into a JSON object so I don’t see how the file flow functions can help there.

The Javascript node doesn’t work is it is most likely a cut down version and seems not to have access to the required functions to make the code work as described above.

@Richard_Soderblom, check out this thread on Stack Overflow for a solution without Buffer:

1 Like

Awesome. Thank you @Mari

I will give it a shot this weekend.

Richard, did that work?

Sorry for not updating earlier.

The solution worked perfectly.

I incorporated it into a bit of code that could decode JWT tokens as well with success.

1 Like

I’m using JWT as well, but I don’t need to decode them. My node.JS backend is decrypting as needed, mostly in my auth route.
Haven’t done anything with JS inside AG.

Dear Richard, could you share your decoding in the JS node?

I try to follow the solution but something maybe not right.

TQ

I need this too, to be able to do anything with basic auth. Seems like it would be a standard flow function.