Skip to content
Myr

merge

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 1021c86 · Browse files · 2 files+76 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+76 added−2 removed

  • other:+76 added−2 removed

2 files+76 added−2 removed

modified:lib/response.js+75 added-1 removed
11 unmodified lines
12 , statusCodes = http.STATUS_CODES13 , send = connect.static.send14 , cookie = require('cookie')15 , send = require('send')16 , crc = require('crc')17 , mime = connect.mime18 , basename = path.basename19 , extname = path.extname20 , join = path.join;
56 unmodified lines
77 * @return {ServerResponse}78 * @api public79 */8080res.send = require('response-send');81res.send = function(body){82 var req = this.req83 , head = 'HEAD' == req.method84 , len;8586 // allow status / body87 if (2 == arguments.length) {88 // res.send(body, status) backwards compat89 if ('number' != typeof body && 'number' == typeof arguments[1]) {90 this.statusCode = arguments[1];91 } else {92 this.statusCode = body;93 body = arguments[1];94 }95 }9697 // convert string objects to primitives98 if (body instanceof String) body = body.toString();99100 switch (typeof body) {101 // response status102 case 'number':103 this.get('Content-Type') || this.type('txt');104 this.statusCode = body;105 body = http.STATUS_CODES[body];106 break;107 // string defaulting to html108 case 'string':109 if (!this.get('Content-Type')) {110 this.charset = this.charset || 'utf-8';111 this.type('html');112 }113 break;114 case 'boolean':115 case 'object':116 if (null == body) {117 body = '';118 } else if (Buffer.isBuffer(body)) {119 this.get('Content-Type') || this.type('bin');120 } else {121 return this.json(body);122 }123 break;124 }125126 // populate Content-Length127 if (undefined !== body && !this.get('Content-Length')) {128 this.set('Content-Length', len = Buffer.isBuffer(body)129 ? body.length130 : Buffer.byteLength(body));131 }132133 // ETag support134 // TODO: W/ support135 if (len > 1024) {136 if (!this.get('ETag')) this.set('ETag', Buffer.isBuffer(body)137 ? crc.buffer.crc32(body)138 : crc.crc32(body));139 }140141 // freshness142 if (req.fresh) this.statusCode = 304;143144 // strip irrelevant headers145 if (204 == this.statusCode || 304 == this.statusCode) {146 this.removeHeader('Content-Type');147 this.removeHeader('Content-Length');148 body = '';149 }150151 // respond152 this.end(head ? null : body);153 return this;154};155156/**157 * Send JSON response.158 *159 * Examples:
518 unmodified lines
modified:package.json+1 added-1 removed