Skip to content

Instantly share code, notes, and snippets.

@szanata
Last active April 22, 2021 22:00
Show Gist options
  • Save szanata/44405b59b5ad54dd856ddc40dc0951bb to your computer and use it in GitHub Desktop.
Save szanata/44405b59b5ad54dd856ddc40dc0951bb to your computer and use it in GitHub Desktop.
Native JS json tarball decompression
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