Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils.unzip function does not set a decoder #125

Closed
hlobil opened this issue Oct 12, 2012 · 9 comments
Closed

utils.unzip function does not set a decoder #125

hlobil opened this issue Oct 12, 2012 · 9 comments

Comments

@hlobil
Copy link

hlobil commented Oct 12, 2012

My mocha unit test fails. It appears to be because the utils.unzip function does not set a decoder.

app.get('/sitemap.xml.gz', function (req, res) {
    var xml = 'sitemap';

    // Set the appropriate HTTP headers to help old and new browsers equally to how to handle the output
    res.header('Content-Type', 'application/x-gzip');
    res.header('Content-Encoding', 'gzip');
    res.header('Content-Disposition', 'attachment; filename="sitemap.xml.gz"');
    zlib.gzip(new Buffer(xml, 'utf8'), function (err, data) {
        res.send(data);
    });
});


describe('GET /sitemap.xml.gz', function () {
    it.skip('should respond with 200', function (done) {
        request(app)
            .get('/sitemap.xml.gz')
            .end(function (err, res) {
                res.should.have.status(200);
                done();
            });
    });
});

ERROR

TypeError: Cannot call method 'write' of undefined
at Unzip.exports.unzip._on (/Users/pavelsmacbookpro/Projects/recipediary/node_modules/supertest/node_modules/superagent/lib/node/utils.js:112:23)
at Unzip.EventEmitter.emit (events.js:93:17)
at Zlib.callback (zlib.js:405:12)

if I add

res.setEncoding('utf8');

to utils.unzip the mocha test passes.

I am not sure that is the correct fix?

Thank you,

Pavel

@blakeembrey
Copy link

I noticed I was crashing the process at the same spot when I ask for gzipped pages via the Accept-Encoding: gzip header.

@tj
Copy link
Contributor

tj commented Oct 16, 2012

why not use the express.compress() middleware? it'll do all this for you

@tj
Copy link
Contributor

tj commented Oct 16, 2012

your request doesn't make sense because you're saying the response is a gzipped gzip file

@blakeembrey
Copy link

Here is how I can replicate the bug:

var request = require('superagent');

request('GET', 'http://a248.e.akamai.net/assets.github.com/assets/github-6f9ac9220676fa355e8b13e0403cf7972fdabbfb.js')
  .set({
    'Accept-Encoding': 'gzip'
  })
  .end(function (res) {
    console.log(res);
  });

That snippet is just using the JS file from github. I can also replicate the bug on other CDNs.

@tj
Copy link
Contributor

tj commented Oct 17, 2012

hmm odd, works 100% fine to me:

var request = require('./');

request
  .get('http://a248.e.akamai.net/assets.github.com/assets/github-6f9ac9220676fa355e8b13e0403cf7972fdabbfb.js')
  .set('Accept-Encoding', 'gzip')
  .end(function(res){
    console.log(res.text);
  })

@tj
Copy link
Contributor

tj commented Oct 17, 2012

nvm that seems to not even be responding with gzipped data

@tj
Copy link
Contributor

tj commented Oct 17, 2012

var request = require('./');

request
  .get('https://github.com')
  .set('Accept-Encoding', 'gzip')
  .end(function(res){
    console.log(res.header['content-encoding']);
    console.log(res.text);
  })

@blakeembrey
Copy link

Okay, can't replicate it with that url anymore. But this causes an error (for me at least):

var request = require('superagent');

request
  .get('http://othstatic.propertycdn.com/clean/js/load.v5.6.4.js')
  .set('Accept-Encoding', 'gzip')
  .end(function (res) {
    console.log(res.header['content-encoding'], !!res.text);
  });
gzip false

/Users/blakeembrey/Dropbox/Projects/requestmaker/node_modules/superagent/lib/node/utils.js:111
    var str = decoder.write(buf);
                      ^
TypeError: Cannot call method 'write' of undefined
    at Unzip.exports.unzip._on (/Users/blakeembrey/Dropbox/Projects/requestmaker/node_modules/superagent/lib/node/utils.js:111:23)
    at Unzip.EventEmitter.emit (events.js:93:17)
    at Zlib.callback (zlib.js:405:12)

For some reason I am not even getting any response text?

@tj
Copy link
Contributor

tj commented Oct 18, 2012

ah that did it for me, ill take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants