Let's assume I have the following in NodeJS:
import { gzip } from "node-gzip"; const samlToken = fs.readFileSync( "./localDevToken.txt", "utf8", ); const bufferSamlToken = Buffer.from( samlToken.replace("\r\n", ""), "utf8", ); const gZipToken = await gzip(bufferSamlToken); localDevToken = gZipToken .toString("base64") .replace(/\+/g, "-") .replace(/\//g, "_") .replace(/=+$/g, ""); And I want to do the same in the frontend. How can I achieve it ?
This is what I've tried using the Pako library from https://github.com/nodeca/pako
function convertSamlToken(input) { var samlToken = input.replace("\r\n", ""); samlToken = pako.gzip(samlToken, { to: 'string' }); samlToken = btoa(samlToken) .replace(/\+/g, "-") .replace(/\//g, "_") .replace(/=+$/g, ""); return samlToken; } But the output is different. What is wrong ?
btoaon it will first convert that Uint8Array to its string representation: a series of numbers between 0 and 255 separated by commas, and then turn that string into its b64 representation. That's not what you want at all, you want the actual bytes to be turned to their base64 representation. Have a look at stackoverflow.com/questions/12710001/… to do it properly