rest-decompressor

0.0.2 • Public • Published

rest-decompressor.js

Content-Encoding support interceptor for rest.js. You plug it in on the top of interceptor chain, and all outgoing requests now tell server you know how to deal with compression. Handy.

Usage

Just add as early as you can into interceptor chain, and you're done.

var rest = require('./rest-node-client');
var decompressor = require('./rest-decompressor');
 
var client = rest.chain(decompressor);
 
client('http://httpbin.org/gzip')
    .tap(function(response) {
        var json = JSON.parse(response.entity);
        if (json.headers['Accept-Encoding'] && response.headers['Content-Encoding']) {
            console.log('Yay, it worked!');
        } else {
            throw new Error('impossibru!');
        }
    })
    .done();

This interceptor adds Accept-Encoding: gzip, deflate to all outgoing requests (strip it from request.headers if unwanted), so server sends you compressed data if server is capable of doing so. Also it looks for Content-Encoding header in the reply, and in case it's identity, gzip or deflate, interceptor changes response.entity to decompressed data.

Caveat

Default rest.js node client implementation is bugged — it converts any incoming data into UTF8, and as UTF8 is not lossless for arbitrary binary data, you get garbage. Please consider using rest-node-client module instead of default client — it's fully backward compatible with rest/client/node.js, and gives you control over this.

Copyright

rest-decompressor.js is made available under the terms of MIT license. See LICENSE for details.

Package Sidebar

Install

npm i rest-decompressor

Weekly Downloads

0

Version

0.0.2

License

MIT

Last publish

Collaborators

  • alexprobchansky