Skip to content

Commit

Permalink
feat: Added support for request body inflation
Browse files Browse the repository at this point in the history
closes #38
  • Loading branch information
bminer authored and fengmk2 committed May 4, 2016
1 parent 474a929 commit b001734
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

var raw = require('raw-body');
var inflate = require('inflation');
var qs = require('qs');

/**
Expand All @@ -24,13 +25,14 @@ module.exports = function(req, opts){

// defaults
var len = req.headers['content-length'];
if (len) opts.length = ~~len;
var encoding = req.headers['content-encoding'] || 'identity';
if (len && encoding === 'identity') opts.length = ~~len;
opts.encoding = opts.encoding || 'utf8';
opts.limit = opts.limit || '56kb';
opts.qs = opts.qs || qs;

// raw-body returns a Promise when no callback is specified
return raw(req, opts)
return raw(inflate(req), opts)
.then(function(str){
try {
return opts.qs.parse(str, opts.queryString);
Expand Down
6 changes: 4 additions & 2 deletions lib/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

var raw = require('raw-body');
var inflate = require('inflation');

// Allowed whitespace is defined in RFC 7159
// http://www.rfc-editor.org/rfc/rfc7159.txt
Expand All @@ -27,13 +28,14 @@ module.exports = function(req, opts){

// defaults
var len = req.headers['content-length'];
if (len) opts.length = len = ~~len;
var encoding = req.headers['content-encoding'] || 'identity';
if (len && encoding === 'identity') opts.length = len = ~~len;
opts.encoding = opts.encoding || 'utf8';
opts.limit = opts.limit || '1mb';
var strict = opts.strict !== false;

// raw-body returns a promise when no callback is specified
return raw(req, opts)
return raw(inflate(req), opts)
.then(function(str) {
try {
return parse(str);
Expand Down
6 changes: 4 additions & 2 deletions lib/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var raw = require('raw-body');
var inflate = require('inflation');

/**
* Return a Promise which parses text/plain requests.
Expand All @@ -22,10 +23,11 @@ module.exports = function(req, opts){

// defaults
var len = req.headers['content-length'];
if (len) opts.length = ~~len;
var encoding = req.headers['content-encoding'] || 'identity';
if (len && encoding === 'identity') opts.length = ~~len;
opts.encoding = opts.encoding || 'utf8';
opts.limit = opts.limit || '1mb';

// raw-body returns a Promise when no callback is specified
return raw(req, opts);
return raw(inflate(req), opts);
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"qs": "~4.0.0",
"raw-body": "~2.1.2",
"inflation": "~2.0.0",
"type-is": "~1.6.6"
},
"devDependencies": {
Expand Down
54 changes: 54 additions & 0 deletions test/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var request = require('supertest');
var parse = require('..');
var koa = require('koa');
var zlib = require('zlib');

describe('parse(req, opts)', function(){
describe('with valid form body', function(){
Expand Down Expand Up @@ -147,4 +148,57 @@ describe('parse(req, opts)', function(){
.expect(415, 'Unsupported Media Type', done);
})
})

describe('with content-encoding', function(){
it('should inflate gzip', function(done){
var app = koa();
var json = JSON.stringify({ foo: 'bar' });

app.use(function *(){
var body = yield parse(this);
body.should.eql({ foo: 'bar' });
done();
});

var req = request(app.listen())
.post('/')
.type('json')
.set('Content-Encoding', 'gzip');
req.write(zlib.gzipSync(json));
req.end(function(){});
})
it('should inflate deflate', function(done){
var app = koa();
var json = JSON.stringify({ foo: 'bar' });

app.use(function *(){
var body = yield parse(this);
body.should.eql({ foo: 'bar' });
done();
});

var req = request(app.listen())
.post('/')
.type('json')
.set('Content-Encoding', 'deflate');
req.write(zlib.deflateSync(json));
req.end(function(){});
})
it('should pass-through identity', function(done){
var app = koa();

app.use(function *(){
var body = yield parse(this);
body.should.eql({ foo: 'bar' });
done();
});

request(app.listen())
.post('/')
.set('Content-Encoding', 'identity')
.send({ foo: 'bar' })
.end(function(){});
})
})

})

0 comments on commit b001734

Please sign in to comment.