Skip to content
Advertisement

Creating Compressed JWT Payload in JavaScript

I have a function which parses a compressed JWT Payload into JSON. It makes sense to me the way the function works. I want to create a function that can do the exact opposite: Take a JSON object and turn it into a COMPRESSED JWT Payload.

The function that parses and decompresses:

function parseJwtPayload(payload) {
    const buffer = Buffer.from(payload, "base64");
    const payloadJson = zlib.inflateRawSync(buffer);
    return JSON.parse(payloadJson);
}

…outputs the JSON.

…inputs the JSON. My attempt at a function that creates a payload and compresses it:

function makeJwtPayload(json) { // Where json is a String from JSON.stringify().
    const buffer = Buffer.from(json, 'utf8');
    var payload = zlib.deflateRawSync(buffer).toString('base64');

    payload = payload.replace(/=+$/, '');
    payload = payload.replace(/+/g, '-');
    payload = payload.replace(///g, '_');

    return payload;
}

Now this function WORKS, because the compressed, encoded string can be used in the original function and decoded and it will keep outputting the same JSON object. But when re-encoded, the Base64 string looks different. Its longer than the original and only some characters stay consistent.

Advertisement

Answer

If you are able to decompress and get the original payload exactly, then you have no problem. There is never a guarantee that decompress-compress will give you the same thing. The only guarantee is that compress-decompress will give you the same thing.

There are many ways to compress the same data, and the same compression code with different settings, different versions of the compression code with the same settings, or simply different compression code, all can give different compressed output for the same data. There’s no telling what version of what compress code with what settings made what you received, and you are unlikely to be able to reconstruct that exactly. And there is no reason or need to.

If you would like to try to compress it a little more, then set the level option of deflateRawSync to 9.

Advertisement