response.js
This file
Path
lib/response.js
1,039 lines · 24.0 KiB · JavaScript
Last changed by
feat: Extend res.links() to allow adding multiple links with the same rel (closes #2729) (#4885)Andrea Polverinobefore adoptioncaa4f68
History of this fileTake it
The walk stopped after 200 commits: lines older than 6aa4a45 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';9Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 10–11why, one step back10/**11 * Module dependencies.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 12why, one step back12 * @privateMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 13–14why, one step back13 */14Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 15why, one step back15var contentDisposition = require('content-disposition');Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 16why, one step back16var createError = require('http-errors')Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 17why, one step back17var encodeUrl = require('encodeurl');Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 18why, one step back18var escapeHtml = require('escape-html');fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
line 19why, one step back19var http = require('node:http');Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 20why, one step back20var onFinished = require('on-finished');Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 21why, one step back21var mime = require('mime-types')fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
lines 22–23why, one step back22var path = require('node:path');23var pathIsAbsolute = require('node:path').isAbsolute;Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 24why, one step back24var statuses = require('statuses')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 25–36why, one step back25var sign = require('cookie-signature').sign;26var normalizeType = require('./utils').normalizeType;27var normalizeTypes = require('./utils').normalizeTypes;28var setCharset = require('./utils').setCharset;29var cookie = require('cookie');30var send = require('send');31var extname = path.extname;32var resolve = path.resolve;33var vary = require('vary');3435/**36 * Response prototype.Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 37why, one step back37 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 38–39why, one step back38 */39Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 40why, one step back40var res = Object.create(http.ServerResponse.prototype)Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 41–42why, one step back4142/**Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 43–49why, one step back43 * Module exports.44 * @public45 */4647module.exports = res4849/**Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
line 50why, one step back50 * Set the HTTP status code for the response.Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 51why, one step back51 *Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
lines 52–58why, one step back52 * Expects an integer value between 100 and 999 inclusive.53 * Throws an error if the provided status code is not an integer or if it's outside the allowable range.54 *55 * @param {number} code - The HTTP status code to set.56 * @return {ServerResponse} - Returns itself for chaining methods.57 * @throws {TypeError} If `code` is not an integer.58 * @throws {RangeError} If `code` is outside the range 100 to 999.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 59why, one step back59 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 60–61why, one step back60 */61Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 62why, one step back62res.status = function status(code) {Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
lines 63–65why, one step back63 // Check if the status code is not an integer64 if (!Number.isInteger(code)) {65 throw new TypeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be an integer.`);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 66why, one step back66 }Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
lines 67–71why, one step back67 // Check if the status code is outside of Node's valid range68 if (code < 100 || code > 999) {69 throw new RangeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be greater than 99 and less than 1000.`);70 }71Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 72–82why, one step back72 this.statusCode = code;73 return this;74};7576/**77 * Set Link header field with the given `links`.78 *79 * Examples:80 *81 * res.links({82 * next: 'http://api.example.com/users?page=2',feat: Extend res.links() to allow adding multiple links with the same rel (closes #2729) (#4885)Andrea Polverinobefore adoptioncaa4f68
lines 83–87why, one step back83 * last: 'http://api.example.com/users?page=5',84 * pages: [85 * 'http://api.example.com/users?page=1',86 * 'http://api.example.com/users?page=2'87 * ]Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 88–91why, one step back88 * });89 *90 * @param {Object} links91 * @return {ServerResponse}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 92why, one step back92 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 93–94why, one step back93 */94feat: Extend res.links() to allow adding multiple links with the same rel (closes #2729) (#4885)Andrea Polverinobefore adoptioncaa4f68
line 95why, one step back95res.links = function(links) {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 96–97why, one step back96 var link = this.get('Link') || '';97 if (link) link += ', ';feat: Extend res.links() to allow adding multiple links with the same rel (closes #2729) (#4885)Andrea Polverinobefore adoptioncaa4f68
lines 98–106why, one step back98 return this.set('Link', link + Object.keys(links).map(function(rel) {99 // Allow multiple links if links[rel] is an array100 if (Array.isArray(links[rel])) {101 return links[rel].map(function (singleLink) {102 return `<${singleLink}>; rel="${rel}"`;103 }).join(', ');104 } else {105 return `<${links[rel]}>; rel="${rel}"`;106 }Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 107–114why, one step back107 }).join(', '));108};109110/**111 * Send a response.112 *113 * Examples:114 *Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 115why, one step back115 * res.send(Buffer.from('wahoo'));Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 116–119why, one step back116 * res.send({ some: 'json' });117 * res.send('<p>some html</p>');118 *119 * @param {string|number|boolean|object|Buffer} bodyMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 120why, one step back120 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 121–143why, one step back121 */122123res.send = function send(body) {124 var chunk = body;125 var encoding;126 var req = this.req;127 var type;128129 // settings130 var app = this.app;131132 switch (typeof chunk) {133 // string defaulting to html134 case 'string':135 if (!this.get('Content-Type')) {136 this.type('html');137 }138 break;139 case 'boolean':140 case 'number':141 case 'object':142 if (chunk === null) {143 chunk = '';feat: Added check to support Uint8Array in response sending (#6285)Alexander Ceruttibefore adoption55869f4
line 144why, one step back144 } else if (ArrayBuffer.isView(chunk)) {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 145–164why, one step back145 if (!this.get('Content-Type')) {146 this.type('bin');147 }148 } else {149 return this.json(chunk);150 }151 break;152 }153154 // write strings in utf-8155 if (typeof chunk === 'string') {156 encoding = 'utf8';157 type = this.get('Content-Type');158159 // reflect this in content-type160 if (typeof type === 'string') {161 this.set('Content-Type', setCharset(type, 'utf-8'));162 }163 }164Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 165–168why, one step back165 // determine if ETag should be generated166 var etagFn = app.get('etag fn')167 var generateETag = !this.get('ETag') && typeof etagFn === 'function'168Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 169why, one step back169 // populate Content-LengthMerge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 170why, one step back170 var lenMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 171why, one step back171 if (chunk !== undefined) {Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 172–180why, one step back172 if (Buffer.isBuffer(chunk)) {173 // get length of Buffer174 len = chunk.length175 } else if (!generateETag && chunk.length < 1000) {176 // just calculate length when no ETag + small chunk177 len = Buffer.byteLength(chunk, encoding)178 } else {179 // convert chunk to Buffer and calculate180 chunk = Buffer.from(chunk, encoding)Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 181why, one step back181 encoding = undefined;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 182why, one step back182 len = chunk.lengthMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 183–187why, one step back183 }184185 this.set('Content-Length', len);186 }187Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 188–189why, one step back188 // populate ETag189 var etag;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 190–191why, one step back190 if (generateETag && len !== undefined) {191 if ((etag = etagFn(chunk, encoding))) {Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 192why, one step back192 this.set('ETag', etag);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 193–196why, one step back193 }194 }195196 // freshnessMerge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
line 197why, one step back197 if (req.fresh) this.status(304);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 198–199why, one step back198199 // strip irrelevant headersMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 200why, one step back200 if (204 === this.statusCode || 304 === this.statusCode) {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 201–206why, one step back201 this.removeHeader('Content-Type');202 this.removeHeader('Content-Length');203 this.removeHeader('Transfer-Encoding');204 chunk = '';205 }206Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 207–213why, one step back207 // alter headers for 205208 if (this.statusCode === 205) {209 this.set('Content-Length', '0')210 this.removeHeader('Transfer-Encoding')211 chunk = ''212 }213Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 214why, one step back214 if (req.method === 'HEAD') {Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 215why, one step back215 // skip body for HEADMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 216why, one step back216 this.end();Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 217–219why, one step back217 } else {218 // respond219 this.end(chunk, encoding);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 220–233why, one step back220 }221222 return this;223};224225/**226 * Send JSON response.227 *228 * Examples:229 *230 * res.json(null);231 * res.json({ user: 'tj' });232 *233 * @param {string|number|boolean|object} objMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 234why, one step back234 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 235–239why, one step back235 */236237res.json = function json(obj) {238 // settings239 var app = this.app;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 240why, one step back240 var escape = app.get('json escape')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 241–242why, one step back241 var replacer = app.get('json replacer');242 var spaces = app.get('json spaces');Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 243why, one step back243 var body = stringify(obj, replacer, spaces, escape)Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 244–261why, one step back244245 // content-type246 if (!this.get('Content-Type')) {247 this.set('Content-Type', 'application/json');248 }249250 return this.send(body);251};252253/**254 * Send JSON response with JSONP callback support.255 *256 * Examples:257 *258 * res.jsonp(null);259 * res.jsonp({ user: 'tj' });260 *261 * @param {string|number|boolean|object} objMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 262why, one step back262 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 263–267why, one step back263 */264265res.jsonp = function jsonp(obj) {266 // settings267 var app = this.app;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 268why, one step back268 var escape = app.get('json escape')Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 269–270why, one step back269 var replacer = app.get('json replacer');270 var spaces = app.get('json spaces');Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 271why, one step back271 var body = stringify(obj, replacer, spaces, escape)Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 272–292why, one step back272 var callback = this.req.query[app.get('jsonp callback name')];273274 // content-type275 if (!this.get('Content-Type')) {276 this.set('X-Content-Type-Options', 'nosniff');277 this.set('Content-Type', 'application/json');278 }279280 // fixup callback281 if (Array.isArray(callback)) {282 callback = callback[0];283 }284285 // jsonp286 if (typeof callback === 'string' && callback.length !== 0) {287 this.set('X-Content-Type-Options', 'nosniff');288 this.set('Content-Type', 'text/javascript');289290 // restrict callback charset291 callback = callback.replace(/[^\[\]\w$.]/g, '');292Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
lines 293–301why, one step back293 if (body === undefined) {294 // empty argument295 body = ''296 } else if (typeof body === 'string') {297 // replace chars not allowed in JavaScript that are in JSON298 body = body299 .replace(/\u2028/g, '\\u2028')300 .replace(/\u2029/g, '\\u2029')301 }Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 302–322why, one step back302303 // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"304 // the typeof check is just to reduce client error noise305 body = '/**/ typeof ' + callback + ' === \'function\' && ' + callback + '(' + body + ');';306 }307308 return this.send(body);309};310311/**312 * Send given HTTP status code.313 *314 * Sets the response status to `statusCode` and the body of the315 * response to the standard description from node's http.STATUS_CODES316 * or the statusCode number if no description.317 *318 * Examples:319 *320 * res.sendStatus(200);321 *322 * @param {number} statusCodeMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 323why, one step back323 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 324–326why, one step back324 */325326res.sendStatus = function sendStatus(statusCode) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 327why, one step back327 var body = statuses.message[statusCode] || String(statusCode)Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 328why, one step back328Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
line 329why, one step back329 this.status(statusCode);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 330–338why, one step back330 this.type('txt');331332 return this.send(body);333};334335/**336 * Transfer the file at the given `path`.337 *338 * Automatically sets the _Content-Type_ response header field.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 339why, one step back339 * The callback `callback(err)` is invoked when the transfer is completeMerge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 340why, one step back340 * or when an error occurs. Be sure to check `res.headersSent`Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 341–372why, one step back341 * if you wish to attempt responding, as the header and some data342 * may have already been transferred.343 *344 * Options:345 *346 * - `maxAge` defaulting to 0 (can be string converted by `ms`)347 * - `root` root directory for relative filenames348 * - `headers` object of headers to serve with file349 * - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them350 *351 * Other options are passed along to `send`.352 *353 * Examples:354 *355 * The following example illustrates how `res.sendFile()` may356 * be used as an alternative for the `static()` middleware for357 * dynamic situations. The code backing `res.sendFile()` is actually358 * the same code, so HTTP cache support etc is identical.359 *360 * app.get('/user/:uid/photos/:file', function(req, res){361 * var uid = req.params.uid362 * , file = req.params.file;363 *364 * req.user.mayViewFilesFrom(uid, function(yes){365 * if (yes) {366 * res.sendFile('/uploads/' + uid + '/' + file);367 * } else {368 * res.send(403, 'Sorry! you cant see that.');369 * }370 * });371 * });372 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 373why, one step back373 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 374–375why, one step back374 */375Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 376–377why, one step back376res.sendFile = function sendFile(path, options, callback) {377 var done = callback;Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 378–380why, one step back378 var req = this.req;379 var res = this;380 var next = req.next;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 381why, one step back381 var opts = options || {};Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 382–386why, one step back382383 if (!path) {384 throw new TypeError('path argument is required to res.sendFile');385 }386Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 387–390why, one step back387 if (typeof path !== 'string') {388 throw new TypeError('path must be a string to res.sendFile')389 }390Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 391–392why, one step back391 // support function as second arg392 if (typeof options === 'function') {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 393–394why, one step back393 done = options;394 opts = {};Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 395–396why, one step back395 }396Use path-is-absolute module for absolute path detectionJeremiah Senkpielbefore adoption694869d
line 397why, one step back397 if (!opts.root && !pathIsAbsolute(path)) {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 398–402why, one step back398 throw new TypeError('path must be absolute or specify root to res.sendFile');399 }400401 // create file stream402 var pathname = encodeURI(path);feat: add support for ETag option in res.sendFile (#6073)Juan Josébefore adoption327af12
lines 403–405why, one step back403404 // wire application etag option to send405 opts.etag = this.app.enabled('etag');Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 406why, one step back406 var file = send(req, pathname, opts);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 407–408why, one step back407408 // transferMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 409–410why, one step back409 sendfile(res, file, opts, function (err) {410 if (done) return done(err);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 411–412why, one step back411 if (err && err.code === 'EISDIR') return next();412Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 413why, one step back413 // next() all but write errorsMerge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 414why, one step back414 if (err && err.code !== 'ECONNABORTED' && err.syscall !== 'write') {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 415–423why, one step back415 next(err);416 }417 });418};419420/**421 * Transfer the file at the given `path` as an attachment.422 *423 * Optionally providing an alternate attachment `filename`,Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 424why, one step back424 * and optional callback `callback(err)`. The callback is invokedMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 425why, one step back425 * when the data transfer is complete, or when an error hasMerge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 426why, one step back426 * occurred. Be sure to check `res.headersSent` if you plan to respond.Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 427why, one step back427 *Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 428–432why, one step back428 * Optionally providing an `options` object to use with `res.sendFile()`.429 * This function will set the `Content-Disposition` header, overriding430 * any `Content-Disposition` header passed as header options in order431 * to set the attachment and filename.432 *Remove res.sendfileDouglas Christopher Wilsonbefore adoption0fc4f07
line 433why, one step back433 * This method uses `res.sendFile()`.Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 434why, one step back434 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 435why, one step back435 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 436–437why, one step back436 */437Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 438why, one step back438res.download = function download (path, filename, options, callback) {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 439–440why, one step back439 var done = callback;440 var name = filename;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 441why, one step back441 var opts = options || nullMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 442why, one step back442Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 443why, one step back443 // support function as second or third argMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 444why, one step back444 if (typeof filename === 'function') {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 445–446why, one step back445 done = filename;446 name = null;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 447–450why, one step back447 opts = null448 } else if (typeof options === 'function') {449 done = options450 opts = nullMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 451–452why, one step back451 }452Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 453–459why, one step back453 // support optional filename, where options may be in it's place454 if (typeof filename === 'object' &&455 (typeof options === 'function' || options === undefined)) {456 name = null457 opts = filename458 }459Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 460–461why, one step back460 // set Content-Disposition when file is sent461 var headers = {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 462why, one step back462 'Content-Disposition': contentDisposition(name || path)Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 463–464why, one step back463 };464Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 465–479why, one step back465 // merge user-provided headers466 if (opts && opts.headers) {467 var keys = Object.keys(opts.headers)468 for (var i = 0; i < keys.length; i++) {469 var key = keys[i]470 if (key.toLowerCase() !== 'content-disposition') {471 headers[key] = opts.headers[key]472 }473 }474 }475476 // merge user-provided options477 opts = Object.create(opts)478 opts.headers = headers479Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 480why, one step back480 // Resolve the full path for sendFileMerge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 481–483why, one step back481 var fullPath = !opts.root482 ? resolve(path)483 : pathMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 484why, one step back484Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 485–486why, one step back485 // send file486 return this.sendFile(fullPath, opts, done)Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 487–489why, one step back487};488489/**Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 490why, one step back490 * Set _Content-Type_ response header with `type` through `mime.contentType()`Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 491why, one step back491 * 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 492–493why, one step back492 * When no mapping is found though `mime.contentType()`, the type is set to493 * "application/octet-stream".Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 494–504why, one step back494 *495 * Examples:496 *497 * res.type('.html');498 * res.type('html');499 * res.type('json');500 * res.type('application/json');501 * res.type('png');502 *503 * @param {String} type504 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 505why, one step back505 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 506–508why, one step back506 */507508res.contentType =Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 509–510why, one step back509res.type = function contentType(type) {510 var ct = type.indexOf('/') === -1Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 511why, one step back511 ? (mime.contentType(type) || 'application/octet-stream')Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 512–514why, one step back512 : type;513514 return this.set('Content-Type', ct);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 515–540why, one step back515};516517/**518 * Respond to the Acceptable formats using an `obj`519 * of mime-type callbacks.520 *521 * This method uses `req.accepted`, an array of522 * acceptable types ordered by their quality values.523 * When "Accept" is not present the _first_ callback524 * is invoked, otherwise the first match is used. When525 * no match is performed the server responds with526 * 406 "Not Acceptable".527 *528 * Content-Type is set for you, however if you choose529 * you may alter this within the callback using `res.type()`530 * or `res.set('Content-Type', ...)`.531 *532 * res.format({533 * 'text/plain': function(){534 * res.send('hey');535 * },536 *537 * 'text/html': function(){538 * res.send('<p>hey</p>');539 * },540 *Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 541why, one step back541 * 'application/json': function () {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 542–570why, one step back542 * res.send({ message: 'hey' });543 * }544 * });545 *546 * In addition to canonicalized MIME types you may547 * also use extnames mapped to these types:548 *549 * res.format({550 * text: function(){551 * res.send('hey');552 * },553 *554 * html: function(){555 * res.send('<p>hey</p>');556 * },557 *558 * json: function(){559 * res.send({ message: 'hey' });560 * }561 * });562 *563 * By default Express passes an `Error`564 * with a `.status` of 406 to `next(err)`565 * if a match is not made. If you provide566 * a `.default` callback it will be invoked567 * instead.568 *569 * @param {Object} obj570 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 571why, one step back571 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 572–577why, one step back572 */573574res.format = function(obj){575 var req = this.req;576 var next = req.next;577Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 578–579why, one step back578 var keys = Object.keys(obj)579 .filter(function (v) { return v !== 'default' })Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 580why, one step back580Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 581–583why, one step back581 var key = keys.length > 0582 ? req.accepts(keys)583 : false;Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 584–589why, one step back584585 this.vary("Accept");586587 if (key) {588 this.set('Content-Type', normalizeType(key).value);589 obj[key](req, this, next);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 590–591why, one step back590 } else if (obj.default) {591 obj.default(req, this, next)Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 592why, one step back592 } else {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 593–595why, one step back593 next(createError(406, {594 types: normalizeTypes(keys).map(function (o) { return o.value })595 }))Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 596–605why, one step back596 }597598 return this;599};600601/**602 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.603 *604 * @param {String} filename605 * @return {ServerResponse}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 606why, one step back606 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 607–608why, one step back607 */608Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
lines 609–613why, one step back609res.attachment = function attachment(filename) {610 if (filename) {611 this.type(extname(filename));612 }613Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 614why, one step back614 this.set('Content-Disposition', contentDisposition(filename));Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 615why, one step back615Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 616–619why, one step back616 return this;617};618619/**Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 620–630why, one step back620 * Append additional header `field` with value `val`.621 *622 * Example:623 *624 * res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);625 * res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');626 * res.append('Warning', '199 Miscellaneous warning');627 *628 * @param {String} field629 * @param {String|Array} val630 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 631why, one step back631 * @publicMerge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 632–641why, one step back632 */633634res.append = function append(field, val) {635 var prev = this.get(field);636 var value = val;637638 if (prev) {639 // concat the new and prev vals640 value = Array.isArray(prev) ? prev.concat(val)641 : Array.isArray(val) ? [prev].concat(val)Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 642why, one step back642 : [prev, val]Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 643–648why, one step back643 }644645 return this.set(field, value);646};647648/**Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 649–659why, one step back649 * Set header `field` to `val`, or pass650 * an object of header fields.651 *652 * Examples:653 *654 * res.set('Foo', ['bar', 'baz']);655 * res.set('Accept', 'application/json');656 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });657 *658 * Aliased as `res.header()`.659 *Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
lines 660–662why, one step back660 * When the set header is "Content-Type", the type is expanded to include661 * the charset if not present using `mime.contentType()`.662 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 663–664why, one step back663 * @param {String|Object} field664 * @param {String|Array} valMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 665why, one step back665 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 666why, one step back666 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 667–671why, one step back667 */668669res.set =670res.header = function header(field, val) {671 if (arguments.length === 2) {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 672–676why, one step back672 var value = Array.isArray(val)673 ? val.map(String)674 : String(val);675676 // add charset to content-typeMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
lines 677–680why, one step back677 if (field.toLowerCase() === 'content-type') {678 if (Array.isArray(value)) {679 throw new TypeError('Content-Type cannot be set to an Array');680 }Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 681why, one step back681 value = mime.contentType(value)Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 682why, one step back682 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 683–684why, one step back683684 this.setHeader(field, value);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 685–697why, one step back685 } else {686 for (var key in field) {687 this.set(key, field[key]);688 }689 }690 return this;691};692693/**694 * Get value for header `field`.695 *696 * @param {String} field697 * @return {String}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 698why, one step back698 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 699–708why, one step back699 */700701res.get = function(field){702 return this.getHeader(field);703};704705/**706 * Clear cookie `name`.707 *708 * @param {String} nameMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 709why, one step back709 * @param {Object} [options]Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 710why, one step back710 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 711why, one step back711 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 712–713why, one step back712 */713Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 714why, one step back714res.clearCookie = function clearCookie(name, options) {Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
lines 715–718why, one step back715 // Force cookie expiration by setting expires to the past716 const opts = { path: '/', ...options, expires: new Date(1)};717 // ensure maxAge is not passed718 delete opts.maxAgeMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 719–720why, one step back719720 return this.cookie(name, '', opts);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 721–723why, one step back721};722723/**Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 724why, one step back724 * Set cookie `name` to `value`, with the given `options`.Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 725–736why, one step back725 *726 * Options:727 *728 * - `maxAge` max-age in milliseconds, converted to `expires`729 * - `signed` sign the cookie730 * - `path` defaults to "/"731 *732 * Examples:733 *734 * // "Remember Me" for 15 minutes735 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });736 *Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
line 737why, one step back737 * // same as aboveMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 738–740why, one step back738 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })739 *740 * @param {String} nameMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 741why, one step back741 * @param {String|Object} valueMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 742why, one step back742 * @param {Object} [options]Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 743why, one step back743 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 744why, one step back744 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 745–746why, one step back745 */746Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 747why, one step back747res.cookie = function (name, value, options) {fix: Remove `utils-merge` dependency - use spread syntax instead (#6091)Phillip Bartabefore adoption246f6f5
line 748why, one step back748 var opts = { ...options };Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 749why, one step back749 var secret = this.req.secret;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 750–753why, one step back750 var signed = opts.signed;751752 if (signed && !secret) {753 throw new Error('cookieParser("secret") required for signed cookies');Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 754–755why, one step back754 }755Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 756–761why, one step back756 var val = typeof value === 'object'757 ? 'j:' + JSON.stringify(value)758 : String(value);759760 if (signed) {761 val = 's:' + sign(val, secret);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 762why, one step back762 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 763why, one step back763Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 764–770why, one step back764 if (opts.maxAge != null) {765 var maxAge = opts.maxAge - 0766767 if (!isNaN(maxAge)) {768 opts.expires = new Date(Date.now() + maxAge)769 opts.maxAge = Math.floor(maxAge / 1000)770 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 771–778why, one step back771 }772773 if (opts.path == null) {774 opts.path = '/';775 }776777 this.append('Set-Cookie', cookie.serialize(name, String(val), opts));778Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 779–795why, one step back779 return this;780};781782/**783 * Set the location header to `url`.784 *785 * The given `url` can also be "back", which redirects786 * to the _Referrer_ or _Referer_ headers or "/".787 *788 * Examples:789 *790 * res.location('/foo/bar').;791 * res.location('http://example.com');792 * res.location('../login');793 *794 * @param {String} url795 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 796why, one step back796 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 797–798why, one step back797 */798Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 799why, one step back799res.location = function location(url) {Delete `back` as a magic string (#5933)Blake Embreybefore adoptionbdd81f8
line 800why, one step back800 return this.set('Location', encodeUrl(url));Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 801–813why, one step back801};802803/**804 * Redirect to the given `url` with optional response `status`805 * defaulting to 302.806 *807 * Examples:808 *809 * res.redirect('/foo/bar');810 * res.redirect('http://example.com');811 * res.redirect(301, 'http://example.com');812 * res.redirect('../login'); // /blog/post/1 -> /blog/login813 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 814why, one step back814 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 815–823why, one step back815 */816817res.redirect = function redirect(url) {818 var address = url;819 var body;820 var status = 302;821822 // allow status / url823 if (arguments.length === 2) {Remove res.redirect(url, status) signatureMike Tunnicliffebefore adoption71395f5
lines 824–825why, one step back824 status = arguments[0]825 address = arguments[1]Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 826–828why, one step back826 }827828 // Set location headerMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 829why, one step back829 address = this.location(address).get('Location');Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 830–833why, one step back830831 // Support text/{plain,html} by default832 this.format({833 text: function(){Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 834why, one step back834 body = statuses.message[status] + '. Redirecting to ' + addressMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 835–837why, one step back835 },836837 html: function(){Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f
line 838why, one step back838 var u = escapeHtml(address);Merge tag '4.20.0' into 5.0Wes Toddbefore adoptione5feb9f
line 839why, one step back839 body = '<p>' + statuses.message[status] + '. Redirecting to ' + u + '</p>'Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 840–847why, one step back840 },841842 default: function(){843 body = '';844 }845 });846847 // RespondMerge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
line 848why, one step back848 this.status(status);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 849–852why, one step back849 this.set('Content-Length', Buffer.byteLength(body));850851 if (this.req.method === 'HEAD') {852 this.end();Merge tag '4.11.2'Douglas Christopher Wilsonbefore adoptiona9ef9e1
lines 853–854why, one step back853 } else {854 this.end(body);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 855–863why, one step back855 }856};857858/**859 * Add `field` to Vary. If already present in the Vary set, then860 * this call is simply ignored.861 *862 * @param {Array|String} field863 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 864why, one step back864 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 865–882why, one step back865 */866867res.vary = function(field){868 vary(this, field);869870 return this;871};872873/**874 * Render `view` with the given `options` and optional callback `fn`.875 * When a callback function is given a response will _not_ be made876 * automatically, otherwise a response of _200_ and _text/html_ is given.877 *878 * Options:879 *880 * - `cache` boolean hinting to the engine it should cache881 * - `filename` filename of the view being rendered882 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 883why, one step back883 * @publicMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 884–885why, one step back884 */885Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 886–889why, one step back886res.render = function render(view, options, callback) {887 var app = this.req.app;888 var done = callback;889 var opts = options || {};Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 890why, one step back890 var req = this.req;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 891why, one step back891 var self = this;Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 892–893why, one step back892893 // support callback function as second argMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 894–896why, one step back894 if (typeof options === 'function') {895 done = options;896 opts = {};Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 897–899why, one step back897 }898899 // merge res.localsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 900why, one step back900 opts._locals = self.locals;Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 901–902why, one step back901902 // default callback to respondMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 903why, one step back903 done = done || function (err, str) {Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 904–908why, one step back904 if (err) return req.next(err);905 self.send(str);906 };907908 // renderMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 909why, one step back909 app.render(view, opts, done);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 910–914why, one step back910};911912// pipe the send file stream913function sendfile(res, file, options, callback) {914 var done = false;Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 915why, one step back915 var streaming;Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 916why, one step back916Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 917–922why, one step back917 // request aborted918 function onaborted() {919 if (done) return;920 done = true;921922 var err = new Error('Request aborted');Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 923why, one step back923 err.code = 'ECONNABORTED';Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 924–926why, one step back924 callback(err);925 }926Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 927–950why, one step back927 // directory928 function ondirectory() {929 if (done) return;930 done = true;931932 var err = new Error('EISDIR, read');933 err.code = 'EISDIR';934 callback(err);935 }936937 // errors938 function onerror(err) {939 if (done) return;940 done = true;941 callback(err);942 }943944 // ended945 function onend() {946 if (done) return;947 done = true;948 callback();949 }950Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 951–955why, one step back951 // file952 function onfile() {953 streaming = false;954 }955Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 956–957why, one step back956 // finished957 function onfinish(err) {Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 958why, one step back958 if (err && err.code === 'ECONNRESET') return onaborted();Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 959–962why, one step back959 if (err) return onerror(err);960 if (done) return;961962 setImmediate(function () {Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 963–967why, one step back963 if (streaming !== false && !done) {964 onaborted();965 return;966 }967Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 968–969why, one step back968 if (done) return;969 done = true;Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 970why, one step back970 callback();Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 971–973why, one step back971 });972 }973Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 974–979why, one step back974 // streaming975 function onstream() {976 streaming = true;977 }978979 file.on('directory', ondirectory);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 980–981why, one step back980 file.on('end', onend);981 file.on('error', onerror);Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 982–983why, one step back982 file.on('file', onfile);983 file.on('stream', onstream);Merge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
lines 984–1000why, one step back984 onFinished(res, onfinish);985986 if (options.headers) {987 // set headers on successful transfer988 file.on('headers', function headers(res) {989 var obj = options.headers;990 var keys = Object.keys(obj);991992 for (var i = 0; i < keys.length; i++) {993 var k = keys[i];994 res.setHeader(k, obj[k]);995 }996 });997 }998999 // pipe1000 file.pipe(res);Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 1001–1003why, one step back1001}10021003/**Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1004–1007why, one step back1004 * Stringify JSON, like JSON.stringify, but v8 optimized, with the1005 * ability to escape characters that can trigger HTML sniffing.1006 *1007 * @param {*} valueMerge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 1008why, one step back1008 * @param {function} replacerMerge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1009–1011why, one step back1009 * @param {number} spaces1010 * @param {boolean} escape1011 * @returns {string}Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 1012–1014why, one step back1012 * @private1013 */1014Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 1015why, one step back1015function stringify (value, replacer, spaces, escape) {Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 1016–1017why, one step back1016 // v8 checks arguments.length for optimizing simple call1017 // https://bugs.chromium.org/p/v8/issues/detail?id=4730Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 1018why, one step back1018 var json = replacer || spacesMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 1019–1020why, one step back1019 ? JSON.stringify(value, replacer, spaces)1020 : JSON.stringify(value);Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 1021why, one step back1021Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 1022why, one step back1022 if (escape && typeof json === 'string') {Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1023–1030why, one step back1023 json = json.replace(/[<>&]/g, function (c) {1024 switch (c.charCodeAt(0)) {1025 case 0x3c:1026 return '\\u003c'1027 case 0x3e:1028 return '\\u003e'1029 case 0x26:1030 return '\\u0026'Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
line 1031why, one step back1031 /* istanbul ignore next: unreachable default */Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1032–1038why, one step back1032 default:1033 return c1034 }1035 })1036 }10371038 return jsonMerge tag '3.17.7'Douglas Christopher Wilsonbefore adoption6aa4a45
line 1039why, one step back1039}