Skip to content

Commit 7af4e37

Browse files
author
Julien Gilli
committed
assert on unformatted body including in the res.send case when no formatter found
1 parent 9a59b0f commit 7af4e37

File tree

1 file changed

+64
-56
lines changed

1 file changed

+64
-56
lines changed

lib/response.js

+64-56
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ function patch(Response) {
346346
};
347347
}
348348

349+
assert.ok(
350+
typeof sendArgs.body === 'string' || Buffer.isBuffer(sendArgs.body),
351+
'res.sendRaw() accepts only strings or buffers'
352+
);
353+
349354
sendArgs.format = false;
350355
return self.__send(sendArgs);
351356
};
@@ -417,71 +422,66 @@ function patch(Response) {
417422
return flush(self);
418423
}
419424

420-
// if no formatting, assert that the value to be written is a string
421-
// or a buffer, then send it.
422-
if (opts.format === false) {
423-
assert.ok(
424-
typeof body === 'string' || Buffer.isBuffer(body),
425-
'res.sendRaw() accepts only strings or buffers'
426-
);
427-
return flush(self, body);
428-
}
429-
430-
// if no body, then no need to format. if this was an error caught by a
431-
// domain, don't send the domain error either.
432-
if (body === undefined || (body instanceof Error && body.domain)) {
433-
return flush(self);
434-
}
425+
if (opts.format === true) {
426+
// if no body, then no need to format. if this was an error caught
427+
// by a domain, don't send the domain error either.
428+
if (body === undefined || (body instanceof Error && body.domain)) {
429+
return flush(self);
430+
}
435431

436-
// At this point we know we have a body that needs to be formatted, so
437-
// lets derive the formatter based on the response object's properties
432+
// At this point we know we have a body that needs to be formatted,
433+
// so lets derive the formatter based on the response object's
434+
// properties
438435

439-
var formatter;
440-
var type = self.contentType || self.getHeader('Content-Type');
436+
var formatter;
437+
var type = self.contentType || self.getHeader('Content-Type');
441438

442-
// Set Content-Type to application/json when
443-
// res.send is called with an Object instead of calling res.json
444-
if (!type && typeof body === 'object' && !Buffer.isBuffer(body)) {
445-
type = 'application/json';
446-
}
439+
// Set Content-Type to application/json when
440+
// res.send is called with an Object instead of calling res.json
441+
if (!type && typeof body === 'object' && !Buffer.isBuffer(body)) {
442+
type = 'application/json';
443+
}
447444

448-
// Derive type if not provided by the user
449-
type = type || self.req.accepts(self.acceptable);
450-
451-
// Check to see if we could find a content type to use for the response.
452-
if (!type) {
453-
return formatterError(
454-
self,
455-
new errors.NotAcceptableError({
456-
message:
457-
'could not find suitable content-type to use ' +
458-
'for the response'
459-
})
460-
);
461-
}
445+
// Derive type if not provided by the user
446+
type = type || self.req.accepts(self.acceptable);
447+
448+
// Check to see if we could find a content type to use for the
449+
// response.
450+
if (!type) {
451+
return formatterError(
452+
self,
453+
new errors.NotAcceptableError({
454+
message:
455+
'could not find suitable content-type to use ' +
456+
'for the response'
457+
})
458+
);
459+
}
462460

463-
type = type.split(';')[0];
461+
type = type.split(';')[0];
464462

465-
if (!self.formatters[type] && type.indexOf('/') === -1) {
466-
type = mime.lookup(type);
467-
}
463+
if (!self.formatters[type] && type.indexOf('/') === -1) {
464+
type = mime.lookup(type);
465+
}
468466

469-
formatter = self.formatters[type] || self.formatters['*/*'];
467+
formatter = self.formatters[type] || self.formatters['*/*'];
470468

471-
if (self._charSet) {
472-
type = type + '; charset=' + self._charSet;
473-
}
469+
if (self._charSet) {
470+
type = type + '; charset=' + self._charSet;
471+
}
474472

475-
// Update Content-Type header to the one originally set or to the type
476-
// inferred from the most relevant formatter found.
477-
self.setHeader('Content-Type', type);
473+
// Update Content-Type header to the one originally set or to the
474+
// type inferred from the most relevant formatter found.
475+
self.setHeader('Content-Type', type);
478476

479-
if (!formatter) {
480-
return flush(self, body);
477+
if (formatter) {
478+
// Finally, invoke the formatter and flush the request with it's
479+
// results
480+
return flush(self, formatter(self.req, self, body));
481+
}
481482
}
482483

483-
// Finally, invoke the formatter and flush the request with it's results
484-
return flush(self, formatter(self.req, self, body));
484+
return flush(self, body);
485485
};
486486

487487
/**
@@ -802,11 +802,19 @@ function patch(Response) {
802802
* @private
803803
* @function flush
804804
* @param {Response} res - response
805-
* @param {String|Buffer} formattedBody - formatted body
805+
* @param {String|Buffer} body - response body
806806
* @returns {Response} response
807807
*/
808-
function flush(res, formattedBody) {
809-
res._data = formattedBody;
808+
function flush(res, body) {
809+
assert.ok(
810+
body === null ||
811+
body === undefined ||
812+
typeof body === 'string' ||
813+
Buffer.isBuffer(body),
814+
'body must be a string or a Buffer instance'
815+
);
816+
817+
res._data = body;
810818

811819
// Flush headers
812820
res.writeHead(res.statusCode);

0 commit comments

Comments
 (0)