src/compress.js

Method compressed

Pipes compressed asset to Client

Parameters:

  • req must be an Object.
    (HTTP(S) request Object)

  • res must be an Object.
    (HTTP(S) response Object)

  • body must be an Object.
    (Response body)

  • type must be an Object.
    (gzip (gz) or deflate (df))

  • etag must be a String.
    (Etag)

  • file must be a Boolean.
    (Indicates body is a file path)

Returns an Objet
(TurtleIO instance)

TurtleIO.prototype.compress = function ( req, res, body, type, etag, file ) { var self = this, method = REGEX_GZIP.test( type ) ? "createGzip" : "createDeflate", sMethod = method.replace( "create", "" ).toLowerCase(), fp = this.config.tmp + "/" + etag + "." + type; fs.exists( fp, function ( exist ) { if ( exist ) {

Pipe compressed asset to Client

fs.createReadStream( fp ).on( "error", function () { self.error( req, res, self.codes.SERVER_ERROR ); } ).pipe( res ); } else if ( !file ) {

Pipe Stream through compression to Client & disk

if ( typeof body.pipe === "function" ) { body.pipe( zlib[method]() ).pipe( res ); body.pipe( zlib[method]() ).pipe( fs.createWriteStream( fp ) ); }

Raw response body, compress and send to Client & disk

else { zlib[sMethod]( body, function ( e, data ) { if ( e ) { self.log( e ); self.unregister( req.parsed.href ); self.error( req, res, self.codes.SERVER_ERROR ); } else { res.end( data ); fs.writeFile( fp, data, "utf8", function ( e ) { if ( e ) { self.log( e ); self.unregister( req.parsed.href ); } } ); } } ); } } else {

Pipe compressed asset to Client

fs.createReadStream( body ).on( "error", function ( e ) { self.log( e ); self.unregister( req.parsed.href ); self.error( req, res, self.codes.SERVER_ERROR ); } ).pipe( zlib[method]() ).pipe( res );

Pipe compressed asset to disk

fs.createReadStream( body ).on( "error", function ( e ) { self.log( e ); } ).pipe( zlib[method]() ).pipe( fs.createWriteStream( fp ) ); } } ); return this; };