Is there a function that encodes a string like bas...
# support
b
Is there a function that encodes a string like base64? I’m trying Buffer and btoa but those are undefined in the transformation environment
m
Hey There! 👋 Your message has been received by the RudderStack team. We will forward this request to your Technical Account Manager, and they will get back to you shortly. Please use the thread for any additional comments.
b
I had to redefine the Base64 as follows:
Copy code
function base64Encode(str) {
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  let output = '';

  for (let block = 0, charCode, i = 0, map = chars;
      str.charAt(i | 0) || (map = '=', i % 1);
      output += map.charAt(63 & block >> 8 - i % 1 * 8)) {

    charCode = str.charCodeAt(i += 3/4);

    if (charCode > 0xFF) {
      throw new Error('"btoa" failed: The string to be encoded contains characters outside of the Latin1 range.');
    }

    block = block << 8 | charCode;
  }

  return output;
}
m
Can you check the Base64 function in the transformation library 'encrypt'. You can find the transformation library tab next to the transformations tab
In the transformations page
b
@shy-kite-21035 i took a look at the
encrypt
library…i can’t see how to use it to do the equivalent of
Buffer.from('some_value').toString('base64')
or equivalent with
btoa
do you have an example using the
encrypt
library?
m
Hi Chris, yes this seems like a bug raising it to the team.
Hi Chris, sorry there isn't an encoding function provided within the transformation library, however, you can write one in your own library and call it in the user transformation.