Nodejs on gzip and deflate compression details

  • 2020-05-10 17:45:06
  • OfStack

0 x01. About

When writing http, when receiving the request of http, the garbled code appeared, and it was later found that gzip had not been decompressed.

For gzip/deflate compression, there are in-pipe compression and non-pipe compression methods.

0x02. Pipe compression

I/O in Node is asynchronous, so reads and writes to disk and network require a callback function to read the data.

Data flow is used when there is no way to hold the data in memory at once, or when it is more efficient to read from one side and process from the other side.

Operations on data streams are provided in NodeJS through a variety of Stream.

The website provides the plumbing method:


// client request example
var zlib = require('zlib');
var http = require('http');
var fs = require('fs');
var request = http.get({ host: 'homeway.me',
                     path: '/',
                     port: 80,
                     headers: { 'accept-encoding': 'gzip,deflate' } });
request.on('response', function(response) {
    var output = fs.createWriteStream('izs.me_index.html');
    switch (response.headers['content-encoding']) {
        // or, just use zlib.createUnzip() to handle both cases
        case 'gzip':
            response.pipe(zlib.createGunzip()).pipe(output);
            break;
        case 'deflate':
            response.pipe(zlib.createInflate()).pipe(output);
            break;
        default:
            response.pipe(output);
            break;
    }
});

0x03. Non-pipe compression

The code is as follows:


#! /usr/local/bin/node
var http = require('http'),
    querystring = require('querystring'),
    zlib = require('zlib');
var args = {
    // Parameters and alternate data
    contents : querystring.stringify({ 
        // The information of the contract
        name:'homeway.me',
    }),
};
var options = {
    hostname: 'homeway.me',
    port: 80,
    path: '/',
    method: 'GET',
    headers: {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Content-Length': args.contents.length,
        'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.11 Safari/537.36',
        'Accept-Encoding':'gzip, deflate',
   },
};
var get = function ( options, args, callback ){
    var req = http.request(options, function (res) {
        var chunks =[], data, encoding = res.headers['content-encoding'];
        // non gzip/deflate To be converted to utf-8 format
        if( encoding === 'undefined'){
            res.setEncoding('utf-8');
        }
        res.on('data', function (chunk){
            chunks.push(chunk);
        });
        res.on('end', function (){
            var buffer = Buffer.concat(chunks);
            if (encoding == 'gzip') {
                zlib.gunzip(buffer, function (err, decoded) {
                    data = decoded.toString();
                    callback( err, args, res.headers, data);
                });
            } else if (encoding == 'deflate') {
                zlib.inflate(buffer, function (err, decoded) {
                    data = decoded.toString();
                    callback( err, args, res.headers, data);
                });
            } else {
                data = buffer.toString();
                callback( null, args, res.headers, data);
            }
        });
    });
    req.write( args.contents );
    req.end();
};
get( options, args, function (err, args, headers, data){
    console.log('==>header \n', headers);
    console.log('==data \n', data);
});

That's all Nodejs has to say about gzip/deflate compression. I hope you enjoy it.


Related articles: