Skip to content
Myr

response.js

This file

Path

lib/response.js

1,044 lines · 23.9 KiB · JavaScript

Last changed by

Merge tag '4.19.1' into 5.xWes Toddbefore adoption60fb1d2

History of this file

Take it

The walk stopped after 200 commits: lines older than ab8d116 point at it.

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 1–7why, one step back
1/*!
2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */7

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 8–9why, one step back
8'use strict';
9

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 10–11why, one step back
10/**
11 * Module dependencies.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 12why, one step back
12 * @private

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 13–14why, one step back
13 */
14

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 15why, one step back
15var Buffer = require('safe-buffer').Buffer

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

line 16why, one step back
16var contentDisposition = require('content-disposition');

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 17–18why, one step back
17var createError = require('http-errors')
18var deprecate = require('depd')('express');

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

line 19why, one step back
19var encodeUrl = require('encodeurl');

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 20–21why, one step back
20var escapeHtml = require('escape-html');
21var http = require('http');

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 22why, one step back
22var onFinished = require('on-finished');

Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6

line 23why, one step back
23var mime = require('mime-types')

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 24why, one step back
24var path = require('path');

Use path-is-absolute module for absolute path detectionJeremiah Senkpielbefore adoption694869d

line 25why, one step back
25var pathIsAbsolute = require('path-is-absolute');

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

line 26why, one step back
26var statuses = require('statuses')

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

line 27why, one step back
27var merge = require('utils-merge');

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 28–34why, one step back
28var sign = require('cookie-signature').sign;
29var normalizeType = require('./utils').normalizeType;30var normalizeTypes = require('./utils').normalizeTypes;31var setCharset = require('./utils').setCharset;32var cookie = require('cookie');33var send = require('send');34var extname = path.extname;

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

line 35why, one step back
35var resolve = path.resolve;

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 36why, one step back
36var vary = require('vary');

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 37why, one step back
37var urlParse = require('url').parse;

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 38–40why, one step back
38
39/**40 * Response prototype.

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

line 41why, one step back
41 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 42–43why, one step back
42 */
43

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

line 44why, one step back
44var res = Object.create(http.ServerResponse.prototype)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 45–46why, one step back
45
46/**

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

lines 47–53why, one step back
47 * Module exports.
48 * @public49 */5051module.exports = res5253/**

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 54–57why, one step back
54 * Set status `code`.
55 *56 * @param {Number} code57 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 58why, one step back
58 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 59–60why, one step back
59 */
60

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 61why, one step back
61res.status = function status(code) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 62–64why, one step back
62 if ((typeof code === 'string' || Math.floor(code) !== code) && code > 99 && code < 1000) {
63 deprecate('res.status(' + JSON.stringify(code) + '): use res.status(' + Math.floor(code) + ') instead')64 }

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 65–80why, one step back
65 this.statusCode = code;
66 return this;67};6869/**70 * Set Link header field with the given `links`.71 *72 * Examples:73 *74 * res.links({75 * next: 'http://api.example.com/users?page=2',76 * last: 'http://api.example.com/users?page=5'77 * });78 *79 * @param {Object} links80 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 81why, one step back
81 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 82–96why, one step back
82 */
8384res.links = function(links){85 var link = this.get('Link') || '';86 if (link) link += ', ';87 return this.set('Link', link + Object.keys(links).map(function(rel){88 return '<' + links[rel] + '>; rel="' + rel + '"';89 }).join(', '));90};9192/**93 * Send a response.94 *95 * Examples:96 *

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 97why, one step back
97 * res.send(Buffer.from('wahoo'));

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 98–101why, one step back
98 * res.send({ some: 'json' });
99 * res.send('<p>some html</p>');100 *101 * @param {string|number|boolean|object|Buffer} body

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 102why, one step back
102 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 103–146why, one step back
103 */
104105res.send = function send(body) {106 var chunk = body;107 var encoding;108 var req = this.req;109 var type;110111 // settings112 var app = this.app;113114 switch (typeof chunk) {115 // string defaulting to html116 case 'string':117 if (!this.get('Content-Type')) {118 this.type('html');119 }120 break;121 case 'boolean':122 case 'number':123 case 'object':124 if (chunk === null) {125 chunk = '';126 } else if (Buffer.isBuffer(chunk)) {127 if (!this.get('Content-Type')) {128 this.type('bin');129 }130 } else {131 return this.json(chunk);132 }133 break;134 }135136 // write strings in utf-8137 if (typeof chunk === 'string') {138 encoding = 'utf8';139 type = this.get('Content-Type');140141 // reflect this in content-type142 if (typeof type === 'string') {143 this.set('Content-Type', setCharset(type, 'utf-8'));144 }145 }146

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 147–150why, one step back
147 // determine if ETag should be generated
148 var etagFn = app.get('etag fn')149 var generateETag = !this.get('ETag') && typeof etagFn === 'function'150

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 151why, one step back
151 // populate Content-Length

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 152why, one step back
152 var len

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 153why, one step back
153 if (chunk !== undefined) {

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 154–162why, one step back
154 if (Buffer.isBuffer(chunk)) {
155 // get length of Buffer156 len = chunk.length157 } else if (!generateETag && chunk.length < 1000) {158 // just calculate length when no ETag + small chunk159 len = Buffer.byteLength(chunk, encoding)160 } else {161 // convert chunk to Buffer and calculate162 chunk = Buffer.from(chunk, encoding)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 163why, one step back
163 encoding = undefined;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 164why, one step back
164 len = chunk.length

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 165–169why, one step back
165 }
166167 this.set('Content-Length', len);168 }169

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 170–171why, one step back
170 // populate ETag
171 var etag;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 172–173why, one step back
172 if (generateETag && len !== undefined) {
173 if ((etag = etagFn(chunk, encoding))) {

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

line 174why, one step back
174 this.set('ETag', etag);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 175–181why, one step back
175 }
176 }177178 // freshness179 if (req.fresh) this.statusCode = 304;180181 // strip irrelevant headers

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

line 182why, one step back
182 if (204 === this.statusCode || 304 === this.statusCode) {

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 183–188why, one step back
183 this.removeHeader('Content-Type');
184 this.removeHeader('Content-Length');185 this.removeHeader('Transfer-Encoding');186 chunk = '';187 }188

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 189–195why, one step back
189 // alter headers for 205
190 if (this.statusCode === 205) {191 this.set('Content-Length', '0')192 this.removeHeader('Transfer-Encoding')193 chunk = ''194 }195

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

line 196why, one step back
196 if (req.method === 'HEAD') {

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 197why, one step back
197 // skip body for HEAD

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 198why, one step back
198 this.end();

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 199–201why, one step back
199 } else {
200 // respond201 this.end(chunk, encoding);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 202–215why, one step back
202 }
203204 return this;205};206207/**208 * Send JSON response.209 *210 * Examples:211 *212 * res.json(null);213 * res.json({ user: 'tj' });214 *215 * @param {string|number|boolean|object} obj

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 216why, one step back
216 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 217–221why, one step back
217 */
218219res.json = function json(obj) {220 // settings221 var app = this.app;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 222why, one step back
222 var escape = app.get('json escape')

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 223–224why, one step back
223 var replacer = app.get('json replacer');
224 var spaces = app.get('json spaces');

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 225why, one step back
225 var body = stringify(obj, replacer, spaces, escape)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 226–243why, one step back
226
227 // content-type228 if (!this.get('Content-Type')) {229 this.set('Content-Type', 'application/json');230 }231232 return this.send(body);233};234235/**236 * Send JSON response with JSONP callback support.237 *238 * Examples:239 *240 * res.jsonp(null);241 * res.jsonp({ user: 'tj' });242 *243 * @param {string|number|boolean|object} obj

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 244why, one step back
244 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 245–249why, one step back
245 */
246247res.jsonp = function jsonp(obj) {248 // settings249 var app = this.app;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 250why, one step back
250 var escape = app.get('json escape')

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 251–252why, one step back
251 var replacer = app.get('json replacer');
252 var spaces = app.get('json spaces');

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 253why, one step back
253 var body = stringify(obj, replacer, spaces, escape)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 254–274why, one step back
254 var callback = this.req.query[app.get('jsonp callback name')];
255256 // content-type257 if (!this.get('Content-Type')) {258 this.set('X-Content-Type-Options', 'nosniff');259 this.set('Content-Type', 'application/json');260 }261262 // fixup callback263 if (Array.isArray(callback)) {264 callback = callback[0];265 }266267 // jsonp268 if (typeof callback === 'string' && callback.length !== 0) {269 this.set('X-Content-Type-Options', 'nosniff');270 this.set('Content-Type', 'text/javascript');271272 // restrict callback charset273 callback = callback.replace(/[^\[\]\w$.]/g, '');274

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

lines 275–283why, one step back
275 if (body === undefined) {
276 // empty argument277 body = ''278 } else if (typeof body === 'string') {279 // replace chars not allowed in JavaScript that are in JSON280 body = body281 .replace(/\u2028/g, '\\u2028')282 .replace(/\u2029/g, '\\u2029')283 }

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 284–290why, one step back
284
285 // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"286 // the typeof check is just to reduce client error noise287 body = '/**/ typeof ' + callback + ' === \'function\' && ' + callback + '(' + body + ');';288 }289290 return this.send(body);

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 291–293why, one step back
291};
292293/**

Add res.sendStatusSeth Samuelbefore adoption12f92a5

lines 294–304why, one step back
294 * Send given HTTP status code.
295 *296 * Sets the response status to `statusCode` and the body of the297 * response to the standard description from node's http.STATUS_CODES298 * or the statusCode number if no description.299 *300 * Examples:301 *302 * res.sendStatus(200);303 *304 * @param {number} statusCode

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 305why, one step back
305 * @public

Add res.sendStatusSeth Samuelbefore adoption12f92a5

lines 306–308why, one step back
306 */
307308res.sendStatus = function sendStatus(statusCode) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 309why, one step back
309 var body = statuses.message[statusCode] || String(statusCode)

Add res.sendStatusSeth Samuelbefore adoption12f92a5

lines 310–317why, one step back
310
311 this.statusCode = statusCode;312 this.type('txt');313314 return this.send(body);315};316317/**

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 318–320why, one step back
318 * Transfer the file at the given `path`.
319 *320 * Automatically sets the _Content-Type_ response header field.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 321why, one step back
321 * The callback `callback(err)` is invoked when the transfer is complete

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 322why, one step back
322 * or when an error occurs. Be sure to check `res.headersSent`

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 323–354why, one step back
323 * if you wish to attempt responding, as the header and some data
324 * may have already been transferred.325 *326 * Options:327 *328 * - `maxAge` defaulting to 0 (can be string converted by `ms`)329 * - `root` root directory for relative filenames330 * - `headers` object of headers to serve with file331 * - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them332 *333 * Other options are passed along to `send`.334 *335 * Examples:336 *337 * The following example illustrates how `res.sendFile()` may338 * be used as an alternative for the `static()` middleware for339 * dynamic situations. The code backing `res.sendFile()` is actually340 * the same code, so HTTP cache support etc is identical.341 *342 * app.get('/user/:uid/photos/:file', function(req, res){343 * var uid = req.params.uid344 * , file = req.params.file;345 *346 * req.user.mayViewFilesFrom(uid, function(yes){347 * if (yes) {348 * res.sendFile('/uploads/' + uid + '/' + file);349 * } else {350 * res.send(403, 'Sorry! you cant see that.');351 * }352 * });353 * });354 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 355why, one step back
355 * @public

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 356–357why, one step back
356 */
357

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 358–359why, one step back
358res.sendFile = function sendFile(path, options, callback) {
359 var done = callback;

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 360why, one step back
360 var req = this.req;

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 361why, one step back
361 var res = this;

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 362why, one step back
362 var next = req.next;

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 363why, one step back
363 var opts = options || {};

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 364–368why, one step back
364
365 if (!path) {366 throw new TypeError('path argument is required to res.sendFile');367 }368

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 369–372why, one step back
369 if (typeof path !== 'string') {
370 throw new TypeError('path must be a string to res.sendFile')371 }372

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 373–374why, one step back
373 // support function as second arg
374 if (typeof options === 'function') {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 375–376why, one step back
375 done = options;
376 opts = {};

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 377–378why, one step back
377 }
378

Use path-is-absolute module for absolute path detectionJeremiah Senkpielbefore adoption694869d

line 379why, one step back
379 if (!opts.root && !pathIsAbsolute(path)) {

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 380–382why, one step back
380 throw new TypeError('path must be absolute or specify root to res.sendFile');
381 }382

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 383why, one step back
383 // create file stream

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 384why, one step back
384 var pathname = encodeURI(path);

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 385why, one step back
385 var file = send(req, pathname, opts);

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 386why, one step back
386

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 387why, one step back
387 // transfer

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 388–389why, one step back
388 sendfile(res, file, opts, function (err) {
389 if (done) return done(err);

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 390why, one step back
390 if (err && err.code === 'EISDIR') return next();

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 391why, one step back
391

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 392why, one step back
392 // next() all but write errors

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

line 393why, one step back
393 if (err && err.code !== 'ECONNABORTED' && err.syscall !== 'write') {

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 394–396why, one step back
394 next(err);
395 }396 });

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 397–402why, one step back
397};
398399/**400 * Transfer the file at the given `path` as an attachment.401 *402 * Optionally providing an alternate attachment `filename`,

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 403why, one step back
403 * and optional callback `callback(err)`. The callback is invoked

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 404why, one step back
404 * when the data transfer is complete, or when an error has

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

line 405why, one step back
405 * occurred. Be sure to check `res.headersSent` if you plan to respond.

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 406why, one step back
406 *

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 407–411why, one step back
407 * Optionally providing an `options` object to use with `res.sendFile()`.
408 * This function will set the `Content-Disposition` header, overriding409 * any `Content-Disposition` header passed as header options in order410 * to set the attachment and filename.411 *

Remove res.sendfileDouglas Christopher Wilsonbefore adoption0fc4f07

line 412why, one step back
412 * This method uses `res.sendFile()`.

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 413why, one step back
413 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 414why, one step back
414 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 415–416why, one step back
415 */
416

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 417why, one step back
417res.download = function download (path, filename, options, callback) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 418–419why, one step back
418 var done = callback;
419 var name = filename;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 420why, one step back
420 var opts = options || null

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 421why, one step back
421

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 422why, one step back
422 // support function as second or third arg

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

line 423why, one step back
423 if (typeof filename === 'function') {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 424–425why, one step back
424 done = filename;
425 name = null;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 426–429why, one step back
426 opts = null
427 } else if (typeof options === 'function') {428 done = options429 opts = null

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 430–431why, one step back
430 }
431

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 432–438why, one step back
432 // support optional filename, where options may be in it's place
433 if (typeof filename === 'object' &&434 (typeof options === 'function' || options === undefined)) {435 name = null436 opts = filename437 }438

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 439–440why, one step back
439 // set Content-Disposition when file is sent
440 var headers = {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 441why, one step back
441 'Content-Disposition': contentDisposition(name || path)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 442–443why, one step back
442 };
443

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 444–458why, one step back
444 // merge user-provided headers
445 if (opts && opts.headers) {446 var keys = Object.keys(opts.headers)447 for (var i = 0; i < keys.length; i++) {448 var key = keys[i]449 if (key.toLowerCase() !== 'content-disposition') {450 headers[key] = opts.headers[key]451 }452 }453 }454455 // merge user-provided options456 opts = Object.create(opts)457 opts.headers = headers458

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

line 459why, one step back
459 // Resolve the full path for sendFile

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 460–462why, one step back
460 var fullPath = !opts.root
461 ? resolve(path)462 : path

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

line 463why, one step back
463

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 464–465why, one step back
464 // send file
465 return this.sendFile(fullPath, opts, done)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 466–468why, one step back
466};
467468/**

Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6

line 469why, one step back
469 * Set _Content-Type_ response header with `type` through `mime.contentType()`

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 470why, one step back
470 * when it does not contain "/", or set the Content-Type to `type` otherwise.

Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6

lines 471–472why, one step back
471 * When no mapping is found though `mime.contentType()`, the type is set to
472 * "application/octet-stream".

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 473–483why, one step back
473 *
474 * Examples:475 *476 * res.type('.html');477 * res.type('html');478 * res.type('json');479 * res.type('application/json');480 * res.type('png');481 *482 * @param {String} type483 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 484why, one step back
484 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 485–487why, one step back
485 */
486487res.contentType =

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 488–489why, one step back
488res.type = function contentType(type) {
489 var ct = type.indexOf('/') === -1

Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6

line 490why, one step back
490 ? (mime.contentType(type) || 'application/octet-stream')

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 491–493why, one step back
491 : type;
492493 return this.set('Content-Type', ct);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 494–519why, one step back
494};
495496/**497 * Respond to the Acceptable formats using an `obj`498 * of mime-type callbacks.499 *500 * This method uses `req.accepted`, an array of501 * acceptable types ordered by their quality values.502 * When "Accept" is not present the _first_ callback503 * is invoked, otherwise the first match is used. When504 * no match is performed the server responds with505 * 406 "Not Acceptable".506 *507 * Content-Type is set for you, however if you choose508 * you may alter this within the callback using `res.type()`509 * or `res.set('Content-Type', ...)`.510 *511 * res.format({512 * 'text/plain': function(){513 * res.send('hey');514 * },515 *516 * 'text/html': function(){517 * res.send('<p>hey</p>');518 * },519 *

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 520why, one step back
520 * 'application/json': function () {

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 521–549why, one step back
521 * res.send({ message: 'hey' });
522 * }523 * });524 *525 * In addition to canonicalized MIME types you may526 * also use extnames mapped to these types:527 *528 * res.format({529 * text: function(){530 * res.send('hey');531 * },532 *533 * html: function(){534 * res.send('<p>hey</p>');535 * },536 *537 * json: function(){538 * res.send({ message: 'hey' });539 * }540 * });541 *542 * By default Express passes an `Error`543 * with a `.status` of 406 to `next(err)`544 * if a match is not made. If you provide545 * a `.default` callback it will be invoked546 * instead.547 *548 * @param {Object} obj549 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 550why, one step back
550 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 551–556why, one step back
551 */
552553res.format = function(obj){554 var req = this.req;555 var next = req.next;556

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 557–558why, one step back
557 var keys = Object.keys(obj)
558 .filter(function (v) { return v !== 'default' })

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 559why, one step back
559

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 560–562why, one step back
560 var key = keys.length > 0
561 ? req.accepts(keys)562 : false;

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 563–568why, one step back
563
564 this.vary("Accept");565566 if (key) {567 this.set('Content-Type', normalizeType(key).value);568 obj[key](req, this, next);

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 569–570why, one step back
569 } else if (obj.default) {
570 obj.default(req, this, next)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 571why, one step back
571 } else {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 572–574why, one step back
572 next(createError(406, {
573 types: normalizeTypes(keys).map(function (o) { return o.value })574 }))

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 575–584why, one step back
575 }
576577 return this;578};579580/**581 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.582 *583 * @param {String} filename584 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 585why, one step back
585 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 586–587why, one step back
586 */
587

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

lines 588–592why, one step back
588res.attachment = function attachment(filename) {
589 if (filename) {590 this.type(extname(filename));591 }592

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 593why, one step back
593 this.set('Content-Disposition', contentDisposition(filename));

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

line 594why, one step back
594

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 595–598why, one step back
595 return this;
596};597598/**

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 599–609why, one step back
599 * Append additional header `field` with value `val`.
600 *601 * Example:602 *603 * res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);604 * res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');605 * res.append('Warning', '199 Miscellaneous warning');606 *607 * @param {String} field608 * @param {String|Array} val609 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 610why, one step back
610 * @public

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 611–620why, one step back
611 */
612613res.append = function append(field, val) {614 var prev = this.get(field);615 var value = val;616617 if (prev) {618 // concat the new and prev vals619 value = Array.isArray(prev) ? prev.concat(val)620 : Array.isArray(val) ? [prev].concat(val)

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 621why, one step back
621 : [prev, val]

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 622–627why, one step back
622 }
623624 return this.set(field, value);625};626627/**

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 628–638why, one step back
628 * Set header `field` to `val`, or pass
629 * an object of header fields.630 *631 * Examples:632 *633 * res.set('Foo', ['bar', 'baz']);634 * res.set('Accept', 'application/json');635 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });636 *637 * Aliased as `res.header()`.638 *

Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6

lines 639–641why, one step back
639 * When the set header is "Content-Type", the type is expanded to include
640 * the charset if not present using `mime.contentType()`.641 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 642–643why, one step back
642 * @param {String|Object} field
643 * @param {String|Array} val

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 644why, one step back
644 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 645why, one step back
645 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 646–650why, one step back
646 */
647648res.set =649res.header = function header(field, val) {650 if (arguments.length === 2) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 651–655why, one step back
651 var value = Array.isArray(val)
652 ? val.map(String)653 : String(val);654655 // add charset to content-type

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

lines 656–659why, one step back
656 if (field.toLowerCase() === 'content-type') {
657 if (Array.isArray(value)) {658 throw new TypeError('Content-Type cannot be set to an Array');659 }

Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6

line 660why, one step back
660 value = mime.contentType(value)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 661why, one step back
661 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 662–663why, one step back
662
663 this.setHeader(field, value);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 664–676why, one step back
664 } else {
665 for (var key in field) {666 this.set(key, field[key]);667 }668 }669 return this;670};671672/**673 * Get value for header `field`.674 *675 * @param {String} field676 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 677why, one step back
677 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 678–687why, one step back
678 */
679680res.get = function(field){681 return this.getHeader(field);682};683684/**685 * Clear cookie `name`.686 *687 * @param {String} name

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

line 688why, one step back
688 * @param {Object} [options]

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 689why, one step back
689 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 690why, one step back
690 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 691–692why, one step back
691 */
692

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 693–696why, one step back
693res.clearCookie = function clearCookie(name, options) {
694 var opts = merge({ expires: new Date(1), path: '/' }, options);695696 return this.cookie(name, '', opts);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 697–699why, one step back
697};
698699/**

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 700why, one step back
700 * Set cookie `name` to `value`, with the given `options`.

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 701–712why, one step back
701 *
702 * Options:703 *704 * - `maxAge` max-age in milliseconds, converted to `expires`705 * - `signed` sign the cookie706 * - `path` defaults to "/"707 *708 * Examples:709 *710 * // "Remember Me" for 15 minutes711 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });712 *

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

line 713why, one step back
713 * // same as above

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 714–716why, one step back
714 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })
715 *716 * @param {String} name

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 717why, one step back
717 * @param {String|Object} value

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

line 718why, one step back
718 * @param {Object} [options]

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 719why, one step back
719 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 720why, one step back
720 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 721–722why, one step back
721 */
722

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 723–724why, one step back
723res.cookie = function (name, value, options) {
724 var opts = merge({}, options);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 725why, one step back
725 var secret = this.req.secret;

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 726–729why, one step back
726 var signed = opts.signed;
727728 if (signed && !secret) {729 throw new Error('cookieParser("secret") required for signed cookies');

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 730–731why, one step back
730 }
731

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 732–737why, one step back
732 var val = typeof value === 'object'
733 ? 'j:' + JSON.stringify(value)734 : String(value);735736 if (signed) {737 val = 's:' + sign(val, secret);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 738why, one step back
738 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 739why, one step back
739

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 740–746why, one step back
740 if (opts.maxAge != null) {
741 var maxAge = opts.maxAge - 0742743 if (!isNaN(maxAge)) {744 opts.expires = new Date(Date.now() + maxAge)745 opts.maxAge = Math.floor(maxAge / 1000)746 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 747–754why, one step back
747 }
748749 if (opts.path == null) {750 opts.path = '/';751 }752753 this.append('Set-Cookie', cookie.serialize(name, String(val), opts));754

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 755–771why, one step back
755 return this;
756};757758/**759 * Set the location header to `url`.760 *761 * The given `url` can also be "back", which redirects762 * to the _Referrer_ or _Referer_ headers or "/".763 *764 * Examples:765 *766 * res.location('/foo/bar').;767 * res.location('http://example.com');768 * res.location('../login');769 *770 * @param {String} url771 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 772why, one step back
772 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 773–774why, one step back
773 */
774

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 775why, one step back
775res.location = function location(url) {

Merge tag '4.19.1' into 5.xWes Toddbefore adoption60fb1d2

line 776why, one step back
776 var loc = String(url);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 777–778why, one step back
777
778 // "back" is an alias for the referrer

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 779–781why, one step back
779 if (url === 'back') {
780 loc = this.req.get('Referrer') || '/';781 }

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 782why, one step back
782

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 783–799why, one step back
783 var lowerLoc = loc.toLowerCase();
784 var encodedUrl = encodeUrl(loc);785 if (lowerLoc.indexOf('https://') === 0 || lowerLoc.indexOf('http://') === 0) {786 try {787 var parsedUrl = urlParse(loc);788 var parsedEncodedUrl = urlParse(encodedUrl);789 // Because this can encode the host, check that we did not change the host790 if (parsedUrl.host !== parsedEncodedUrl.host) {791 // If the host changes after encodeUrl, return the original url792 return this.set('Location', loc);793 }794 } catch (e) {795 // If parse fails, return the original url796 return this.set('Location', loc);797 }798 }799

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 800why, one step back
800 // set location

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 801why, one step back
801 return this.set('Location', encodedUrl);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 802–818why, one step back
802};
803804/**805 * Redirect to the given `url` with optional response `status`806 * defaulting to 302.807 *808 * The resulting `url` is determined by `res.location()`, so809 * it will play nicely with mounted apps, relative paths,810 * `"back"` etc.811 *812 * Examples:813 *814 * res.redirect('/foo/bar');815 * res.redirect('http://example.com');816 * res.redirect(301, 'http://example.com');817 * res.redirect('../login'); // /blog/post/1 -> /blog/login818 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 819why, one step back
819 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 820–828why, one step back
820 */
821822res.redirect = function redirect(url) {823 var address = url;824 var body;825 var status = 302;826827 // allow status / url828 if (arguments.length === 2) {

Remove res.redirect(url, status) signatureMike Tunnicliffebefore adoption71395f5

lines 829–830why, one step back
829 status = arguments[0]
830 address = arguments[1]

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 831–833why, one step back
831 }
832833 // Set location header

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

line 834why, one step back
834 address = this.location(address).get('Location');

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 835–838why, one step back
835
836 // Support text/{plain,html} by default837 this.format({838 text: function(){

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 839why, one step back
839 body = statuses.message[status] + '. Redirecting to ' + address

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 840–842why, one step back
840 },
841842 html: function(){

Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f

line 843why, one step back
843 var u = escapeHtml(address);

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 844why, one step back
844 body = '<p>' + statuses.message[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>'

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 845–857why, one step back
845 },
846847 default: function(){848 body = '';849 }850 });851852 // Respond853 this.statusCode = status;854 this.set('Content-Length', Buffer.byteLength(body));855856 if (this.req.method === 'HEAD') {857 this.end();

Merge tag '4.11.2'Douglas Christopher Wilsonbefore adoptiona9ef9e1

lines 858–859why, one step back
858 } else {
859 this.end(body);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 860–868why, one step back
860 }
861};862863/**864 * Add `field` to Vary. If already present in the Vary set, then865 * this call is simply ignored.866 *867 * @param {Array|String} field868 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 869why, one step back
869 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 870–887why, one step back
870 */
871872res.vary = function(field){873 vary(this, field);874875 return this;876};877878/**879 * Render `view` with the given `options` and optional callback `fn`.880 * When a callback function is given a response will _not_ be made881 * automatically, otherwise a response of _200_ and _text/html_ is given.882 *883 * Options:884 *885 * - `cache` boolean hinting to the engine it should cache886 * - `filename` filename of the view being rendered887 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 888why, one step back
888 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 889–890why, one step back
889 */
890

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 891–894why, one step back
891res.render = function render(view, options, callback) {
892 var app = this.req.app;893 var done = callback;894 var opts = options || {};

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 895why, one step back
895 var req = this.req;

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 896why, one step back
896 var self = this;

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 897–898why, one step back
897
898 // support callback function as second arg

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 899–901why, one step back
899 if (typeof options === 'function') {
900 done = options;901 opts = {};

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 902–904why, one step back
902 }
903904 // merge res.locals

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 905why, one step back
905 opts._locals = self.locals;

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 906–907why, one step back
906
907 // default callback to respond

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 908why, one step back
908 done = done || function (err, str) {

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 909–913why, one step back
909 if (err) return req.next(err);
910 self.send(str);911 };912913 // render

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 914why, one step back
914 app.render(view, opts, done);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 915why, one step back
915};

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 916–919why, one step back
916
917// pipe the send file stream918function sendfile(res, file, options, callback) {919 var done = false;

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 920why, one step back
920 var streaming;

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 921why, one step back
921

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 922–927why, one step back
922 // request aborted
923 function onaborted() {924 if (done) return;925 done = true;926927 var err = new Error('Request aborted');

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

line 928why, one step back
928 err.code = 'ECONNABORTED';

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 929–931why, one step back
929 callback(err);
930 }931

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 932–955why, one step back
932 // directory
933 function ondirectory() {934 if (done) return;935 done = true;936937 var err = new Error('EISDIR, read');938 err.code = 'EISDIR';939 callback(err);940 }941942 // errors943 function onerror(err) {944 if (done) return;945 done = true;946 callback(err);947 }948949 // ended950 function onend() {951 if (done) return;952 done = true;953 callback();954 }955

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 956–960why, one step back
956 // file
957 function onfile() {958 streaming = false;959 }960

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 961–962why, one step back
961 // finished
962 function onfinish(err) {

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

line 963why, one step back
963 if (err && err.code === 'ECONNRESET') return onaborted();

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 964–967why, one step back
964 if (err) return onerror(err);
965 if (done) return;966967 setImmediate(function () {

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 968–972why, one step back
968 if (streaming !== false && !done) {
969 onaborted();970 return;971 }972

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 973–974why, one step back
973 if (done) return;
974 done = true;

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 975why, one step back
975 callback();

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 976–978why, one step back
976 });
977 }978

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 979–984why, one step back
979 // streaming
980 function onstream() {981 streaming = true;982 }983984 file.on('directory', ondirectory);

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 985–986why, one step back
985 file.on('end', onend);
986 file.on('error', onerror);

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 987–988why, one step back
987 file.on('file', onfile);
988 file.on('stream', onstream);

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 989–1005why, one step back
989 onFinished(res, onfinish);
990991 if (options.headers) {992 // set headers on successful transfer993 file.on('headers', function headers(res) {994 var obj = options.headers;995 var keys = Object.keys(obj);996997 for (var i = 0; i < keys.length; i++) {998 var k = keys[i];999 res.setHeader(k, obj[k]);1000 }1001 });1002 }10031004 // pipe1005 file.pipe(res);

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 1006–1008why, one step back
1006}
10071008/**

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1009–1012why, one step back
1009 * Stringify JSON, like JSON.stringify, but v8 optimized, with the
1010 * ability to escape characters that can trigger HTML sniffing.1011 *1012 * @param {*} value

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 1013why, one step back
1013 * @param {function} replacer

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1014–1016why, one step back
1014 * @param {number} spaces
1015 * @param {boolean} escape1016 * @returns {string}

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 1017–1019why, one step back
1017 * @private
1018 */1019

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 1020why, one step back
1020function stringify (value, replacer, spaces, escape) {

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 1021–1022why, one step back
1021 // v8 checks arguments.length for optimizing simple call
1022 // https://bugs.chromium.org/p/v8/issues/detail?id=4730

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 1023why, one step back
1023 var json = replacer || spaces

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 1024–1025why, one step back
1024 ? JSON.stringify(value, replacer, spaces)
1025 : JSON.stringify(value);

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 1026why, one step back
1026

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

line 1027why, one step back
1027 if (escape && typeof json === 'string') {

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1028–1035why, one step back
1028 json = json.replace(/[<>&]/g, function (c) {
1029 switch (c.charCodeAt(0)) {1030 case 0x3c:1031 return '\\u003c'1032 case 0x3e:1033 return '\\u003e'1034 case 0x26:1035 return '\\u0026'

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

line 1036why, one step back
1036 /* istanbul ignore next: unreachable default */

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1037–1043why, one step back
1037 default:
1038 return c1039 }1040 })1041 }10421043 return json

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 1044why, one step back
1044}