Last active
April 22, 2021 22:00
-
-
Save szanata/44405b59b5ad54dd856ddc40dc0951bb to your computer and use it in GitHub Desktop.
Native JS json tarball decompression
This file contains 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 { unzipSync } = require( 'zlib' ); | |
const firstIndexOf = ( c, ...vars ) => Math.min( ...vars.map( v => c.indexOf( v ) ).filter( n => n > -1 ) ); | |
const lastIndexOf = ( c, ...vars ) => Math.max( ...vars.map( v => c.lastIndexOf( v ) ) ); | |
/** | |
* Decompress JSON | |
* | |
* Reads a gzipped tarball (.tar.gz) | |
* 1. Unzip it | |
* 2. Convert all to utf-8 | |
* 3. Split files using the \0star separator | |
* 4. Trim files until JSON markup start/end ({}) and JSON.parse | |
* | |
* Enjoy this 100% native tarball decompression! | |
* @szanata ;) | |
*/ | |
module.exports = raw => unzipSync( raw ) | |
.toString( 'utf-8' ) | |
.split( '\0ustar' ) | |
.slice( 1 ) | |
.map( c => | |
JSON.parse( | |
c.substring( firstIndexOf( c, '{', '[' ), lastIndexOf( c, '}', ']' ) + 1 ) | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment