Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Jun 8, 2021
1 parent 4baca60 commit b5c47ca
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 80 deletions.
149 changes: 70 additions & 79 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ exports = module.exports = internals.Response = class {
this._contentType = null; // Used if no explicit content-type is set and type is known
this._takeover = false;
this._statusCode = false; // true when code() called
this._state = 'init'; // One of 'init', 'prepared', 'marshalled', 'closed'
this._state = this._error ? 'prepare' : 'init'; // One of 'init', 'prepare', 'marshall', 'close'

this._processors = {
marshal: options.marshal,
Expand Down Expand Up @@ -498,22 +498,19 @@ exports = module.exports = internals.Response = class {

Hoek.assert(this._state === 'init');

try {
this._passThrough();
this._state = 'prepare';

if (!this._processors.prepare) {
return this;
}
this._passThrough();

try {
return this._processors.prepare(this);
}
catch (err) {
throw Boom.boomify(err);
}
if (!this._processors.prepare) {
return this;
}

try {
return this._processors.prepare(this);
}
finally {
this._state = 'prepared';
catch (err) {
throw Boom.boomify(err);
}
}

Expand Down Expand Up @@ -566,74 +563,71 @@ exports = module.exports = internals.Response = class {

async _marshal() {

Hoek.assert(this._state === 'prepared' || this._error);
Hoek.assert(this._state === 'prepare');

try {
let source = this.source;
this._state = 'marshall';

// Processor marshal
// Processor marshal

if (this._processors.marshal) {
try {
source = await this._processors.marshal(this);
}
catch (err) {
throw Boom.boomify(err);
}
let source = this.source;

if (this._processors.marshal) {
try {
source = await this._processors.marshal(this);
}
catch (err) {
throw Boom.boomify(err);
}
}

// Stream source
// Stream source

if (Streams.isStream(source)) {
this._payload = source;
return;
}
if (Streams.isStream(source)) {
this._payload = source;
return;
}

// Plain source (non string or null)
// Plain source (non string or null)

const jsonify = this.variety === 'plain' && source !== null && typeof source !== 'string';
const jsonify = this.variety === 'plain' && source !== null && typeof source !== 'string';

if (!jsonify &&
this.settings.stringify) {
if (!jsonify &&
this.settings.stringify) {

throw Boom.badImplementation('Cannot set formatting options on non object response');
}
throw Boom.badImplementation('Cannot set formatting options on non object response');
}

let payload = source;
let payload = source;

if (jsonify) {
const options = this.settings.stringify || {};
const space = options.space || this.request.route.settings.json.space;
const replacer = options.replacer || this.request.route.settings.json.replacer;
const suffix = options.suffix || this.request.route.settings.json.suffix || '';
const escape = this.request.route.settings.json.escape || false;
if (jsonify) {
const options = this.settings.stringify || {};
const space = options.space || this.request.route.settings.json.space;
const replacer = options.replacer || this.request.route.settings.json.replacer;
const suffix = options.suffix || this.request.route.settings.json.suffix || '';
const escape = this.request.route.settings.json.escape || false;

try {
if (replacer || space) {
payload = JSON.stringify(payload, replacer, space);
}
else {
payload = JSON.stringify(payload);
}
}
catch (err) {
throw Boom.boomify(err);
try {
if (replacer || space) {
payload = JSON.stringify(payload, replacer, space);
}

if (suffix) {
payload = payload + suffix;
else {
payload = JSON.stringify(payload);
}
}
catch (err) {
throw Boom.boomify(err);
}

if (escape) {
payload = Hoek.escapeJson(payload);
}
if (suffix) {
payload = payload + suffix;
}

this._payload = new internals.Response.Payload(payload, this.settings);
}
finally {
this._state = 'marshalled';
if (escape) {
payload = Hoek.escapeJson(payload);
}
}

this._payload = new internals.Response.Payload(payload, this.settings);
}

_tap() {
Expand All @@ -653,28 +647,25 @@ exports = module.exports = internals.Response = class {

_close() {

if (this._state === 'closed') {
if (this._state === 'close') {
return;
}

try {
if (this._processors.close) {
try {
this._processors.close(this);
}
catch (err) {
Bounce.rethrow(err, 'system');
this.request._log(['response', 'cleanup', 'error'], err);
}
}
this._state = 'close';

const stream = this._payload || this.source;
if (Streams.isStream(stream)) {
internals.Response.drain(stream);
if (this._processors.close) {
try {
this._processors.close(this);
}
catch (err) {
Bounce.rethrow(err, 'system');
this.request._log(['response', 'cleanup', 'error'], err);
}
}
finally {
this._state = 'closed';

const stream = this._payload || this.source;
if (Streams.isStream(stream)) {
internals.Response.drain(stream);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/transmit.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ exports.send = async function (request) {
internals.marshal = async function (response) {

for (const func of response.request._route._marshalCycle) {
if (response._state !== 'closed') {
if (response._state !== 'close') {
await func(response);
}
}
Expand Down

0 comments on commit b5c47ca

Please sign in to comment.