Created
October 13, 2020 16:47
-
-
Save PopeFelix/bab16207a639fde8e9d1317d9b218a24 to your computer and use it in GitHub Desktop.
encode / decode gzipped JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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