Skip to content

Instantly share code, notes, and snippets.

@neilellis
Last active September 7, 2016 21:47
Show Gist options
  • Save neilellis/c557f64f8318dd2c57569a216f67fe63 to your computer and use it in GitHub Desktop.
Save neilellis/c557f64f8318dd2c57569a216f67fe63 to your computer and use it in GitHub Desktop.
My DEV Journal

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment