Skip to content

Commit

Permalink
assert on unformatted body including in the res.send case when no for…
Browse files Browse the repository at this point in the history
…matter found
  • Loading branch information
Julien Gilli committed Nov 30, 2018
1 parent 9a59b0f commit c282890
Showing 1 changed file with 54 additions and 53 deletions.
107 changes: 54 additions & 53 deletions lib/response.js
Expand Up @@ -417,71 +417,72 @@ function patch(Response) {
return flush(self);
}

// if no formatting, assert that the value to be written is a string
// or a buffer, then send it.
if (opts.format === false) {
assert.ok(
typeof body === 'string' || Buffer.isBuffer(body),
'res.sendRaw() accepts only strings or buffers'
);
return flush(self, body);
}

// if no body, then no need to format. if this was an error caught by a
// domain, don't send the domain error either.
if (body === undefined || (body instanceof Error && body.domain)) {
return flush(self);
}
if (opts.format === true) {
// if no body, then no need to format. if this was an error caught
// by a domain, don't send the domain error either.
if (body === undefined || (body instanceof Error && body.domain)) {
return flush(self);
}

// At this point we know we have a body that needs to be formatted, so
// lets derive the formatter based on the response object's properties
// At this point we know we have a body that needs to be formatted,
// so lets derive the formatter based on the response object's
// properties

var formatter;
var type = self.contentType || self.getHeader('Content-Type');
var formatter;
var type = self.contentType || self.getHeader('Content-Type');

// Set Content-Type to application/json when
// res.send is called with an Object instead of calling res.json
if (!type && typeof body === 'object' && !Buffer.isBuffer(body)) {
type = 'application/json';
}
// Set Content-Type to application/json when
// res.send is called with an Object instead of calling res.json
if (!type && typeof body === 'object' && !Buffer.isBuffer(body)) {
type = 'application/json';
}

// Derive type if not provided by the user
type = type || self.req.accepts(self.acceptable);

// Check to see if we could find a content type to use for the response.
if (!type) {
return formatterError(
self,
new errors.NotAcceptableError({
message:
'could not find suitable content-type to use ' +
'for the response'
})
);
}
// Derive type if not provided by the user
type = type || self.req.accepts(self.acceptable);

// Check to see if we could find a content type to use for the
// response.
if (!type) {
return formatterError(
self,
new errors.NotAcceptableError({
message:
'could not find suitable content-type to use ' +
'for the response'
})
);
}

type = type.split(';')[0];
type = type.split(';')[0];

if (!self.formatters[type] && type.indexOf('/') === -1) {
type = mime.lookup(type);
}
if (!self.formatters[type] && type.indexOf('/') === -1) {
type = mime.lookup(type);
}

formatter = self.formatters[type] || self.formatters['*/*'];
formatter = self.formatters[type] || self.formatters['*/*'];

if (self._charSet) {
type = type + '; charset=' + self._charSet;
}
if (self._charSet) {
type = type + '; charset=' + self._charSet;
}

// Update Content-Type header to the one originally set or to the type
// inferred from the most relevant formatter found.
self.setHeader('Content-Type', type);
// Update Content-Type header to the one originally set or to the
// type inferred from the most relevant formatter found.
self.setHeader('Content-Type', type);

if (!formatter) {
return flush(self, body);
if (formatter) {
// Finally, invoke the formatter and flush the request with it's
// results
return flush(self, formatter(self.req, self, body));
}
}

// Finally, invoke the formatter and flush the request with it's results
return flush(self, formatter(self.req, self, body));
// if no formatting, assert that the value to be written is a string or
// a buffer, then send it.
assert.ok(
typeof body === 'string' || Buffer.isBuffer(body),
'res.sendRaw() accepts only strings or buffers'
);
return flush(self, body);
};

/**
Expand Down

0 comments on commit c282890

Please sign in to comment.