import { gzip } from '@gfx/zopfli' ; import { Transform , TransformCallback } from 'stream' ; import { ZopfliOptions } from '../interfaces' ; export class ZopfliStream extends Transform { private buffer : Buffer ; private options : ZopfliOptions ; constructor ( options ) { super ( ) ; this . buffer = Buffer . alloc ( 0 ) ; this . options = options ; } _transform ( chunk , _ , callback ) { this . buffer = Buffer . concat ( [ this . buffer , chunk ] ) ; callback ( ) ; } _flush ( callback ) { const buffer = Buffer . from ( this . buffer ) ; gzip ( buffer , this . options , ( err , buffer ) => { if ( err ) { callback ( err ) ; } else { this . push ( buffer ) ; callback ( ) ; } } ) ; } }