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 fileTake 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 back1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */7Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 8–9why, one step back8'use strict';9tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 10–11why, one step back10/**11 * Module dependencies.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 12why, one step back12 * @privatetests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 13–14why, one step back13 */14Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 15why, one step back15var Buffer = require('safe-buffer').BufferUse content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 16why, one step back16var contentDisposition = require('content-disposition');Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 17–18why, one step back17var createError = require('http-errors')18var deprecate = require('depd')('express');Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 19why, one step back19var encodeUrl = require('encodeurl');tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 20–21why, one step back20var escapeHtml = require('escape-html');21var http = require('http');Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 22why, one step back22var onFinished = require('on-finished');Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 23why, one step back23var mime = require('mime-types')tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 24why, one step back24var path = require('path');Use path-is-absolute module for absolute path detectionJeremiah Senkpielbefore adoption694869d
line 25why, one step back25var pathIsAbsolute = require('path-is-absolute');Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 26why, one step back26var statuses = require('statuses')Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 27why, one step back27var merge = require('utils-merge');tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 28–34why, one step back28var 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 back35var resolve = path.resolve;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 36why, one step back36var vary = require('vary');Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 37why, one step back37var urlParse = require('url').parse;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 38–40why, one step back3839/**40 * Response prototype.Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 41why, one step back41 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 42–43why, one step back42 */43Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 44why, one step back44var res = Object.create(http.ServerResponse.prototype)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 45–46why, one step back4546/**Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 47–53why, one step back47 * Module exports.48 * @public49 */5051module.exports = res5253/**tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 54–57why, one step back54 * Set status `code`.55 *56 * @param {Number} code57 * @return {ServerResponse}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 58why, one step back58 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 59–60why, one step back59 */60Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 61why, one step back61res.status = function status(code) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 62–64why, one step back62 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 back65 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 back81 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 82–96why, one step back82 */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 back97 * res.send(Buffer.from('wahoo'));tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 98–101why, one step back98 * res.send({ some: 'json' });99 * res.send('<p>some html</p>');100 *101 * @param {string|number|boolean|object|Buffer} bodyMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 102why, one step back102 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 103–146why, one step back103 */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 }146Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 147–150why, one step back147 // determine if ETag should be generated148 var etagFn = app.get('etag fn')149 var generateETag = !this.get('ETag') && typeof etagFn === 'function'150tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 151why, one step back151 // populate Content-LengthMerge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 152why, one step back152 var lentests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 153why, one step back153 if (chunk !== undefined) {Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 154–162why, one step back154 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 back163 encoding = undefined;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 164why, one step back164 len = chunk.lengthtests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 165–169why, one step back165 }166167 this.set('Content-Length', len);168 }169Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 170–171why, one step back170 // populate ETag171 var etag;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 172–173why, one step back172 if (generateETag && len !== undefined) {173 if ((etag = etagFn(chunk, encoding))) {Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 174why, one step back174 this.set('ETag', etag);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 175–181why, one step back175 }176 }177178 // freshness179 if (req.fresh) this.statusCode = 304;180181 // strip irrelevant headersMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 182why, one step back182 if (204 === this.statusCode || 304 === this.statusCode) {tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 183–188why, one step back183 this.removeHeader('Content-Type');184 this.removeHeader('Content-Length');185 this.removeHeader('Transfer-Encoding');186 chunk = '';187 }188Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 189–195why, one step back189 // alter headers for 205190 if (this.statusCode === 205) {191 this.set('Content-Length', '0')192 this.removeHeader('Transfer-Encoding')193 chunk = ''194 }195Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 196why, one step back196 if (req.method === 'HEAD') {Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 197why, one step back197 // skip body for HEADtests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 198why, one step back198 this.end();Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 199–201why, one step back199 } else {200 // respond201 this.end(chunk, encoding);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 202–215why, one step back202 }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} objMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 216why, one step back216 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 217–221why, one step back217 */218219res.json = function json(obj) {220 // settings221 var app = this.app;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 222why, one step back222 var escape = app.get('json escape')tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 223–224why, one step back223 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 back225 var body = stringify(obj, replacer, spaces, escape)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 226–243why, one step back226227 // 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} objMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 244why, one step back244 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 245–249why, one step back245 */246247res.jsonp = function jsonp(obj) {248 // settings249 var app = this.app;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 250why, one step back250 var escape = app.get('json escape')tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 251–252why, one step back251 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 back253 var body = stringify(obj, replacer, spaces, escape)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 254–274why, one step back254 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, '');274Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
lines 275–283why, one step back275 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 back284285 // 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 back291};292293/**Add res.sendStatusSeth Samuelbefore adoption12f92a5
lines 294–304why, one step back294 * 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} statusCodeMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 305why, one step back305 * @publicAdd res.sendStatusSeth Samuelbefore adoption12f92a5
lines 306–308why, one step back306 */307308res.sendStatus = function sendStatus(statusCode) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 309why, one step back309 var body = statuses.message[statusCode] || String(statusCode)Add res.sendStatusSeth Samuelbefore adoption12f92a5
lines 310–317why, one step back310311 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 back318 * 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 back321 * The callback `callback(err)` is invoked when the transfer is completeMerge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 322why, one step back322 * or when an error occurs. Be sure to check `res.headersSent`Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 323–354why, one step back323 * if you wish to attempt responding, as the header and some data324 * 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 back355 * @publicAdd res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 356–357why, one step back356 */357Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 358–359why, one step back358res.sendFile = function sendFile(path, options, callback) {359 var done = callback;Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 360why, one step back360 var req = this.req;Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 361why, one step back361 var res = this;Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 362why, one step back362 var next = req.next;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 363why, one step back363 var opts = options || {};Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 364–368why, one step back364365 if (!path) {366 throw new TypeError('path argument is required to res.sendFile');367 }368Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 369–372why, one step back369 if (typeof path !== 'string') {370 throw new TypeError('path must be a string to res.sendFile')371 }372Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 373–374why, one step back373 // support function as second arg374 if (typeof options === 'function') {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 375–376why, one step back375 done = options;376 opts = {};Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 377–378why, one step back377 }378Use path-is-absolute module for absolute path detectionJeremiah Senkpielbefore adoption694869d
line 379why, one step back379 if (!opts.root && !pathIsAbsolute(path)) {Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 380–382why, one step back380 throw new TypeError('path must be absolute or specify root to res.sendFile');381 }382Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 383why, one step back383 // create file streamAdd res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 384why, one step back384 var pathname = encodeURI(path);Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 385why, one step back385 var file = send(req, pathname, opts);Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 386why, one step back386Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 387why, one step back387 // transferMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 388–389why, one step back388 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 back390 if (err && err.code === 'EISDIR') return next();Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 391why, one step back391Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 392why, one step back392 // next() all but write errorsMerge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 393why, one step back393 if (err && err.code !== 'ECONNABORTED' && err.syscall !== 'write') {Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 394–396why, one step back394 next(err);395 }396 });tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 397–402why, one step back397};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 back403 * and optional callback `callback(err)`. The callback is invokedtests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 404why, one step back404 * when the data transfer is complete, or when an error hasMerge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 405why, one step back405 * 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 back406 *Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 407–411why, one step back407 * 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 back412 * This method uses `res.sendFile()`.tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 413why, one step back413 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 414why, one step back414 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 415–416why, one step back415 */416Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 417why, one step back417res.download = function download (path, filename, options, callback) {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 418–419why, one step back418 var done = callback;419 var name = filename;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 420why, one step back420 var opts = options || nullMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 421why, one step back421Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 422why, one step back422 // support function as second or third argFix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7
line 423why, one step back423 if (typeof filename === 'function') {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 424–425why, one step back424 done = filename;425 name = null;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 426–429why, one step back426 opts = null427 } else if (typeof options === 'function') {428 done = options429 opts = nulltests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 430–431why, one step back430 }431Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 432–438why, one step back432 // support optional filename, where options may be in it's place433 if (typeof filename === 'object' &&434 (typeof options === 'function' || options === undefined)) {435 name = null436 opts = filename437 }438tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 439–440why, one step back439 // set Content-Disposition when file is sent440 var headers = {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 441why, one step back441 'Content-Disposition': contentDisposition(name || path)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 442–443why, one step back442 };443Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 444–458why, one step back444 // merge user-provided headers445 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 = headers458Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7
line 459why, one step back459 // Resolve the full path for sendFileMerge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 460–462why, one step back460 var fullPath = !opts.root461 ? resolve(path)462 : pathFix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7
line 463why, one step back463Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 464–465why, one step back464 // send file465 return this.sendFile(fullPath, opts, done)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 466–468why, one step back466};467468/**Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 469why, one step back469 * Set _Content-Type_ response header with `type` through `mime.contentType()`tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 470why, one step back470 * 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 back471 * When no mapping is found though `mime.contentType()`, the type is set to472 * "application/octet-stream".tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 473–483why, one step back473 *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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 484why, one step back484 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 485–487why, one step back485 */486487res.contentType =Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 488–489why, one step back488res.type = function contentType(type) {489 var ct = type.indexOf('/') === -1Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 490why, one step back490 ? (mime.contentType(type) || 'application/octet-stream')Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 491–493why, one step back491 : type;492493 return this.set('Content-Type', ct);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 494–519why, one step back494};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 back520 * 'application/json': function () {tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 521–549why, one step back521 * 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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 550why, one step back550 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 551–556why, one step back551 */552553res.format = function(obj){554 var req = this.req;555 var next = req.next;556Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 557–558why, one step back557 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 back559Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 560–562why, one step back560 var key = keys.length > 0561 ? req.accepts(keys)562 : false;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 563–568why, one step back563564 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 back569 } else if (obj.default) {570 obj.default(req, this, next)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 571why, one step back571 } else {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 572–574why, one step back572 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 back575 }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 back585 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 586–587why, one step back586 */587Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
lines 588–592why, one step back588res.attachment = function attachment(filename) {589 if (filename) {590 this.type(extname(filename));591 }592tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 593why, one step back593 this.set('Content-Disposition', contentDisposition(filename));Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 594why, one step back594tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 595–598why, one step back595 return this;596};597598/**Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 599–609why, one step back599 * 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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 610why, one step back610 * @publicMerge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 611–620why, one step back611 */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 back621 : [prev, val]Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 622–627why, one step back622 }623624 return this.set(field, value);625};626627/**tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 628–638why, one step back628 * Set header `field` to `val`, or pass629 * 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 back639 * When the set header is "Content-Type", the type is expanded to include640 * the charset if not present using `mime.contentType()`.641 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 642–643why, one step back642 * @param {String|Object} field643 * @param {String|Array} valtests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 644why, one step back644 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 645why, one step back645 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 646–650why, one step back646 */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 back651 var value = Array.isArray(val)652 ? val.map(String)653 : String(val);654655 // add charset to content-typeMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
lines 656–659why, one step back656 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 back660 value = mime.contentType(value)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 661why, one step back661 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 662–663why, one step back662663 this.setHeader(field, value);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 664–676why, one step back664 } 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 back677 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 678–687why, one step back678 */679680res.get = function(field){681 return this.getHeader(field);682};683684/**685 * Clear cookie `name`.686 *687 * @param {String} nameMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 688why, one step back688 * @param {Object} [options]tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 689why, one step back689 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 690why, one step back690 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 691–692why, one step back691 */692Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 693–696why, one step back693res.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 back697};698699/**Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 700why, one step back700 * Set cookie `name` to `value`, with the given `options`.tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 701–712why, one step back701 *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 back713 * // same as abovetests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 714–716why, one step back714 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })715 *716 * @param {String} nameMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 717why, one step back717 * @param {String|Object} valueMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 718why, one step back718 * @param {Object} [options]tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 719why, one step back719 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 720why, one step back720 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 721–722why, one step back721 */722Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 723–724why, one step back723res.cookie = function (name, value, options) {724 var opts = merge({}, options);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 725why, one step back725 var secret = this.req.secret;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 726–729why, one step back726 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 back730 }731Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 732–737why, one step back732 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 back738 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 739why, one step back739Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 740–746why, one step back740 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 back747 }748749 if (opts.path == null) {750 opts.path = '/';751 }752753 this.append('Set-Cookie', cookie.serialize(name, String(val), opts));754tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 755–771why, one step back755 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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 772why, one step back772 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 773–774why, one step back773 */774Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 775why, one step back775res.location = function location(url) {Merge tag '4.19.1' into 5.xWes Toddbefore adoption60fb1d2
line 776why, one step back776 var loc = String(url);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 777–778why, one step back777778 // "back" is an alias for the referrerMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 779–781why, one step back779 if (url === 'back') {780 loc = this.req.get('Referrer') || '/';781 }tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 782why, one step back782Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 783–799why, one step back783 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 }799Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 800why, one step back800 // set locationMerge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 801why, one step back801 return this.set('Location', encodedUrl);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 802–818why, one step back802};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 back819 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 820–828why, one step back820 */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 back829 status = arguments[0]830 address = arguments[1]tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 831–833why, one step back831 }832833 // Set location headerMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 834why, one step back834 address = this.location(address).get('Location');tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 835–838why, one step back835836 // 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 back839 body = statuses.message[status] + '. Redirecting to ' + addresstests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 840–842why, one step back840 },841842 html: function(){Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f
line 843why, one step back843 var u = escapeHtml(address);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 844why, one step back844 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 back845 },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 back858 } else {859 this.end(body);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 860–868why, one step back860 }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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 869why, one step back869 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 870–887why, one step back870 */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 back888 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 889–890why, one step back889 */890Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 891–894why, one step back891res.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 back895 var req = this.req;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 896why, one step back896 var self = this;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 897–898why, one step back897898 // support callback function as second argMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 899–901why, one step back899 if (typeof options === 'function') {900 done = options;901 opts = {};tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 902–904why, one step back902 }903904 // merge res.localsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 905why, one step back905 opts._locals = self.locals;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 906–907why, one step back906907 // default callback to respondMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 908why, one step back908 done = done || function (err, str) {tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 909–913why, one step back909 if (err) return req.next(err);910 self.send(str);911 };912913 // renderMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 914why, one step back914 app.render(view, opts, done);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 915why, one step back915};Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 916–919why, one step back916917// 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 back920 var streaming;Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 921why, one step back921Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 922–927why, one step back922 // request aborted923 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 back928 err.code = 'ECONNABORTED';Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 929–931why, one step back929 callback(err);930 }931Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 932–955why, one step back932 // directory933 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 }955Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 956–960why, one step back956 // file957 function onfile() {958 streaming = false;959 }960Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 961–962why, one step back961 // finished962 function onfinish(err) {Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 963why, one step back963 if (err && err.code === 'ECONNRESET') return onaborted();Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 964–967why, one step back964 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 back968 if (streaming !== false && !done) {969 onaborted();970 return;971 }972Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 973–974why, one step back973 if (done) return;974 done = true;Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 975why, one step back975 callback();Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 976–978why, one step back976 });977 }978Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 979–984why, one step back979 // streaming980 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 back985 file.on('end', onend);986 file.on('error', onerror);Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 987–988why, one step back987 file.on('file', onfile);988 file.on('stream', onstream);Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 989–1005why, one step back989 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 back1006}10071008/**Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1009–1012why, one step back1009 * Stringify JSON, like JSON.stringify, but v8 optimized, with the1010 * ability to escape characters that can trigger HTML sniffing.1011 *1012 * @param {*} valueMerge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 1013why, one step back1013 * @param {function} replacerMerge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1014–1016why, one step back1014 * @param {number} spaces1015 * @param {boolean} escape1016 * @returns {string}Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 1017–1019why, one step back1017 * @private1018 */1019Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 1020why, one step back1020function stringify (value, replacer, spaces, escape) {Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 1021–1022why, one step back1021 // v8 checks arguments.length for optimizing simple call1022 // https://bugs.chromium.org/p/v8/issues/detail?id=4730Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 1023why, one step back1023 var json = replacer || spacesMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 1024–1025why, one step back1024 ? JSON.stringify(value, replacer, spaces)1025 : JSON.stringify(value);Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 1026why, one step back1026Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 1027why, one step back1027 if (escape && typeof json === 'string') {Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1028–1035why, one step back1028 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 back1036 /* istanbul ignore next: unreachable default */Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1037–1043why, one step back1037 default:1038 return c1039 }1040 })1041 }10421043 return jsonInvoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 1044why, one step back1044}