Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/jsonp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Load modules

var Hapi = require('../lib');


// Declare internals

var internals = {};


internals.main = function () {

var server = new Hapi.Server(8000);

var handler = function () {

console.log(this.raw.req.headers);
var parts = this.params.name.split('/');
this.reply({ first: parts[0], last: parts[1] });
};

server.route({ method: 'GET', path: '/user/{name*2}', config: { handler: handler, jsonp: 'callback' } });
server.start(function () {

console.log('Try: http://localhost:8000/user/1/2?callback=docall');
});
};


internals.main();
41 changes: 24 additions & 17 deletions lib/response/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exports = module.exports = internals.Generic = function () {
this._code = 200;
this._payload = [];
this._headers = {};

this._preview = new internals.Peek();

return this;
Expand Down Expand Up @@ -117,21 +117,34 @@ internals.Generic.prototype._transmit = function (request, callback) {

var compress = function (encoding) {

var encoder = (encoding === 'gzip' ? Zlib.gzip : Zlib.deflate);
Async.map(self._payload, encoder, function (err, results) {
var compressed = [];
var compressedLength = 0;

var length = 0;
results.forEach(function (result) {
var encoderStream = (encoding === 'gzip' ? Zlib.createGzip() : Zlib.createDeflate());
encoderStream.on('readable', function () {

length += result.length;
});
var chunk = encoderStream.read();
if (chunk) {
compressed.push(chunk);
compressedLength += chunk.length;
}
});

encoderStream.on('end', function () {

self.header('Content-Encoding', encoding);
self.header('Vary', 'Accept-Encoding');
self.header('Content-Length', length);
self.header('Content-Length', compressedLength);

return send(results);
return send(compressed);
});

self._payload.forEach(function (chunk) {

encoderStream.write(chunk, self._flags.encoding);
});

encoderStream.end();
};

var send = function (payload) {
Expand All @@ -153,14 +166,8 @@ internals.Generic.prototype._transmit = function (request, callback) {

payload.forEach(function (chunk) {

if (Buffer.isBuffer(chunk)) {
self._preview.write(chunk);
request.raw.res.write(chunk);
}
else {
self._preview.write(chunk, self._flags.encoding);
request.raw.res.write(chunk, self._flags.encoding);
}
self._preview.write(chunk, self._flags.encoding);
request.raw.res.write(chunk, self._flags.encoding);
});
}

Expand Down
32 changes: 32 additions & 0 deletions test/integration/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var Lab = require('lab');
var Fs = require('fs');
var Stream = require('stream');
var Zlib = require('zlib');
var Request = require('request');
var Hapi = require('../..');
var ResponseError = require('../../lib/response/error');
Expand Down Expand Up @@ -139,6 +140,37 @@ describe('Response', function () {
});
});

it('returns an JSONP response with compression', function (done) {

var handler = function () {

var parts = this.params.name.split('/');
this.reply({ first: parts[0], last: parts[1] });
};

var server = new Hapi.Server();
server.route({
method: 'GET',
path: '/user/{name*2}',
config: {
handler: handler,
jsonp: 'callback'
}
});

server.inject({ url: '/user/1/2?callback=docall', headers: { 'accept-encoding': 'gzip' } }, function (res) {

expect(res.headers['content-type']).to.equal('text/javascript; charset=utf-8');
expect(res.headers['content-encoding']).to.equal('gzip');
Zlib.unzip(res.rawPayload, function (err, result) {

expect(err).to.not.exist;
expect(result.toString()).to.equal('docall({"first":"1","last":"2"});');
done();
});
});
});

it('returns an JSONP response when response is a buffer', function (done) {

var handler = function (request) {
Expand Down