Skip to content

Commit

Permalink
Closes #1266
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Dec 22, 2013
1 parent ae854f6 commit e474aa1
Show file tree
Hide file tree
Showing 18 changed files with 64 additions and 227 deletions.
14 changes: 3 additions & 11 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1893,9 +1893,7 @@ var server = new Hapi.Server();
server.ext('onPostHandler', function (request, next) {

var response = request.response();
if (response.variety === 'obj') {
delete response.raw._id; // Remove internal key
}
delete response.source._id; // Remove internal key
next();
});
```
Expand Down Expand Up @@ -1927,10 +1925,6 @@ var handler = function (request, reply) {

### Response types

Every response type includes the following property:

- `variety` - the response type name in lower case (e.g. `'text'`).

#### `Generic`

The `Generic` response type is used as the parent prototype for all other response types. It cannot be instantiated directly and is only made available
Expand Down Expand Up @@ -2099,10 +2093,8 @@ var server = new Hapi.Server();
server.ext('onPostHandler', function (request, next) {

var response = request.response();
if (response.variety === 'obj') {
delete response.source._id; // Remove internal key
response.options({ space: 4 });
}
delete response.source._id; // Remove internal key
response.spaces(4);
next();
});
```
Expand Down
2 changes: 1 addition & 1 deletion lib/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ internals.generateListing = function (path, resource, selection, hasTrailingSlas

html += '</ul></body></html>';

return reply(new Response.Text(html, 'text/html'));
return reply(new Response.Generic(html));
});
};

Expand Down
2 changes: 1 addition & 1 deletion lib/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ exports.load = function (path, options, callback) {
// Prepare Stream response

var fileStream = Fs.createReadStream(filePath);
var response = new Response.Stream(fileStream);
var response = new Response.Generic(fileStream);

response.bytes(stat.size);
response._header('content-type', Mime.lookup(filePath) || 'application/octet-stream');
Expand Down
13 changes: 6 additions & 7 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var Async = require('async');
var Boom = require('boom');
var Utils = require('./utils');
var Response = require('./response');
var Closed = require('./response/closed');
var Ext = require('./ext');
var File = require('./file');
var Directory = require('./directory');
Expand Down Expand Up @@ -136,7 +135,7 @@ internals.decorateReply = function (request, finalize) {

reply.close = function () {

finalize(new Closed());
finalize({ closed: true });
};

var viewsManager = request._route.env.views || request.server._views;
Expand Down Expand Up @@ -170,14 +169,14 @@ exports.response = function (result, request, onSend) {
result === undefined ||
result === '') {

response = new Response.Empty();
response = new Response.Generic();
}
else if (typeof result === 'string') {
response = new Response.Text(result);
response = new Response.Generic(result);
}
else if (typeof result === 'object') {
if (Buffer.isBuffer(result)) {
response = new Response.Buffer(result);
response = new Response.Generic(result);
}
else if (result instanceof Response.Generic ||
(result.isBoom && result instanceof Error)) { // Checking 'instanceof Boom' is unreliable
Expand All @@ -188,12 +187,12 @@ exports.response = function (result, request, onSend) {
response = new Boom(result);
}
else if (result instanceof Stream) {
response = new Response.Stream(result);
response = new Response.Generic(result);
}
}

if (!response) {
response = new Response.Obj(result);
response = new Response.Generic(result);
}

if (!response.isBoom) {
Expand Down
2 changes: 1 addition & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ internals.Request.prototype._reply = function (exit) {
var process = function () {

if (self._response &&
self._response.variety === 'closed') {
self._response.closed) {

self.raw.res.end(); // End the response in case it wasn't already closed
return Utils.nextTick(finalize)();
Expand Down
31 changes: 0 additions & 31 deletions lib/response/buffer.js

This file was deleted.

14 changes: 0 additions & 14 deletions lib/response/closed.js

This file was deleted.

28 changes: 0 additions & 28 deletions lib/response/empty.js

This file was deleted.

31 changes: 25 additions & 6 deletions lib/response/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ var internals = {};

// Generic response (Generic)

exports = module.exports = internals.Generic = function () {

Utils.assert(this.constructor !== internals.Generic, 'Generic must not be instantiated directly');

this.variety = 'generic';
exports = module.exports = internals.Generic = function (source) {

this._flags = {};
this._states = {};
Expand All @@ -27,6 +23,27 @@ exports = module.exports = internals.Generic = function () {

this._payload = null; // Readable stream
this._preview = new internals.Peek();

// Default content-type and type-specific method

if (source) {
if (typeof source === 'string') {
this._header('content-type', 'text/html');
}
else if (Buffer.isBuffer(source)) {
this._header('content-type', 'application/octet-stream');
}
else if (source instanceof Stream === false) {
this._flags.stringify = {}; // JSON.stringify options

this.replacer = this._replacer;
this.spaces = this._spaces;

this._header('content-type', 'application/json');
}

this.source = source;
}
};


Expand All @@ -43,7 +60,7 @@ internals.Generic.prototype.header = function (key, value, isAppend, sep) {
if (key === 'vary') {
return this.vary(value);
}

return this._header(key, value, isAppend, sep);
};

Expand Down Expand Up @@ -261,6 +278,8 @@ internals.Generic.prototype._prepare = function (request, callback) {

var self = this;

this._payload = this._payload || (this.source instanceof Stream ? this.source : new Payload(this.source, request, this._flags));

if (request.jsonp &&
this._payload.jsonp) {

Expand Down
13 changes: 4 additions & 9 deletions lib/response/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ var internals = {


exports.Generic = internals.Generic = require('./generic');
exports.Empty = internals.Empty = require('./empty');
exports.Obj = internals.Obj = require('./obj');
exports.Text = internals.Text = require('./text');
exports.Stream = internals.Stream = require('./stream');
exports.View = internals.View = require('./view');
exports.Buffer = internals.Buffer = require('./buffer');


internals.prepare = function (item, request, callback) {
Expand Down Expand Up @@ -68,7 +63,7 @@ exports._respond = function (item, request, callback) {

var boom = response;
var error = boom.response;
response = new internals.Obj(error.payload);
response = new internals.Generic(error.payload);
response._err = boom;
response.code(error.code);

Expand Down Expand Up @@ -128,7 +123,7 @@ exports._respond = function (item, request, callback) {

var unchanged = function () {

var empty = new internals.Empty();
var empty = new internals.Generic();
empty.code(304);
internals.prepare(empty, request, send);
};
Expand All @@ -137,7 +132,7 @@ exports._respond = function (item, request, callback) {

// Injection

if ((response.variety === 'obj' || response._err) &&
if ((response._flags.stringify || response._err) &&
Shot.isInjection(request.raw.req)) {

request.raw.res._hapi = { result: response.source };
Expand All @@ -146,7 +141,7 @@ exports._respond = function (item, request, callback) {
internals.transmit(response, request, function () {

request._response = response; // Error occurs late and should update request.response object
request.log(['hapi', 'response', response.variety]);
request.log(['hapi', 'response']);
return callback();
});
};
Expand Down
37 changes: 0 additions & 37 deletions lib/response/obj.js

This file was deleted.

20 changes: 10 additions & 10 deletions lib/response/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ var Utils = require('../utils');
var internals = {};


exports = module.exports = internals.Payload = function (payload) {
exports = module.exports = internals.Payload = function (payload, request, options) {

Stream.Readable.call(this);
this._data = payload || null;
this._prefix = null;
this._suffix = null;
this._sizeOffset = 0;
this._played = false;

if (options && options.stringify) {
var space = options.stringify.space || request.server.settings.json.space;
var replacer = options.stringify.replacer || request.server.settings.json.replacer;
this._data = JSON.stringify(payload, replacer, space);
}
};

Utils.inherits(internals.Payload, Stream.Readable);


internals.Payload.prototype._read = function (size) {

if (!this._played) {
this._played = true;

this._prefix && this.push(this._prefix);
this._data && this.push(this._data);
this._suffix && this.push(this._suffix);
}

this._prefix && this.push(this._prefix);
this._data && this.push(this._data);
this._suffix && this.push(this._suffix);
this.push(null);
};

Expand Down
Loading

0 comments on commit e474aa1

Please sign in to comment.