Today I added Zlib compression for the results of my Lambda functions, on the server:
@Nullable
private static String compress(@NotNull String serialized) throws UnsupportedEncodingException {
byte[] input = serialized.getBytes("UTF-8");
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
byte[] output = new byte[input.length + 8000];
int compressedDataLength = compresser.deflate(output);
compresser.end();
byte[] result = Arrays.copyOf(output, compressedDataLength);
return "___CMP_v2:" + new String(Base64.getEncoder().encode(result), "UTF-8");
}
And on the client:
if (typeof data.Payload === "string" && data.Payload.indexOf("\"___CMP_v2:") === 0) {
let compressed = "\"" + data.Payload.substring("\"___CMP_v1:".length);
// console.log("Compressed", JSON.parse(compressed));
const binary = atob(JSON.parse(compressed));
// console.log("Binary", binary);
const text = new TextDecoder("utf-8").decode(pako.inflate(binary));
console.log("Inflated Response for ", JSON.stringify(params), " was ", text);
response = JSON.parse(text);
console.log(
"Payload was compressed to " + Math.floor(
(data.Payload.length / text.length) * 100) + "% of original size");
}
As you can see I used pako (https://github.com/nodeca/pako) on the client side combined with the "text-encoding" (https://github.com/inexorabletash/text-encoding) library