Skip to content

Instantly share code, notes, and snippets.

@PopeFelix
Created October 13, 2020 16:47
Show Gist options
  • Select an option

  • Save PopeFelix/bab16207a639fde8e9d1317d9b218a24 to your computer and use it in GitHub Desktop.

Select an option

Save PopeFelix/bab16207a639fde8e9d1317d9b218a24 to your computer and use it in GitHub Desktop.
encode / decode gzipped JSON
const util = require('util');
const zlib = require('zlib');
const gzip = util.promisify(zlib.gzip);
const gunzip = util.promisify(zlib.gunzip);
const encodeGzippedJson = async (obj) => {
const gzBuf = await gzip(JSON.stringify(obj));
return JSON.stringify(gzBuf);
}
const decodeGzippedJson = async (json) => {
const gzBuf = Buffer.from(JSON.parse(json));
const innerJson = gunzip(gzBuf);
return JSON.parse(innerJSON);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment