Skip to content
Myr

response.js

This file

Path

lib/response.js

1,025 lines · 23.6 KiB · JavaScript

Last changed by

feat: Added check to support Uint8Array in response sending (#6285)Alexander Ceruttibefore adoption55869f4

History of this file

Take it

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 12why, one step back
12 * @private

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

line 16why, one step back
16var createError = require('http-errors')

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

line 18why, one step back
18var escapeHtml = require('escape-html');

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

line 19why, one step back
19var http = require('node:http');

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

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

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

fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359

lines 22–23why, one step back
22var path = require('node:path');
23var pathIsAbsolute = require('node:path').isAbsolute;

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 25–36why, one step back
25var 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 back
37 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 38–39why, one step back
38 */
39

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

lines 43–49why, one step back
43 * Module exports.
44 * @public45 */4647module.exports = res4849/**

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

line 50why, one step back
50 * Set the HTTP status code for the response.

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

line 51why, one step back
51 *

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

lines 52–58why, one step back
52 * 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 back
59 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

lines 63–65why, one step back
63 // Check if the status code is not an integer
64 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 back
66 }

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

lines 67–71why, one step back
67 // Check if the status code is outside of Node's valid range
68 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 }71

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 72–87why, one step back
72 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',83 * last: 'http://api.example.com/users?page=5'84 * });85 *86 * @param {Object} links87 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 88why, one step back
88 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 89–103why, one step back
89 */
9091res.links = function(links){92 var link = this.get('Link') || '';93 if (link) link += ', ';94 return this.set('Link', link + Object.keys(links).map(function(rel){95 return '<' + links[rel] + '>; rel="' + rel + '"';96 }).join(', '));97};9899/**100 * Send a response.101 *102 * Examples:103 *

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 109why, one step back
109 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 110–132why, one step back
110 */
111112res.send = function send(body) {113 var chunk = body;114 var encoding;115 var req = this.req;116 var type;117118 // settings119 var app = this.app;120121 switch (typeof chunk) {122 // string defaulting to html123 case 'string':124 if (!this.get('Content-Type')) {125 this.type('html');126 }127 break;128 case 'boolean':129 case 'number':130 case 'object':131 if (chunk === null) {132 chunk = '';

feat: Added check to support Uint8Array in response sending (#6285)Alexander Ceruttibefore adoption55869f4

line 133why, one step back
133 } else if (ArrayBuffer.isView(chunk)) {

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 134–153why, one step back
134 if (!this.get('Content-Type')) {
135 this.type('bin');136 }137 } else {138 return this.json(chunk);139 }140 break;141 }142143 // write strings in utf-8144 if (typeof chunk === 'string') {145 encoding = 'utf8';146 type = this.get('Content-Type');147148 // reflect this in content-type149 if (typeof type === 'string') {150 this.set('Content-Type', setCharset(type, 'utf-8'));151 }152 }153

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 154–157why, one step back
154 // determine if ETag should be generated
155 var etagFn = app.get('etag fn')156 var generateETag = !this.get('ETag') && typeof etagFn === 'function'157

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 159why, one step back
159 var len

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 161–169why, one step back
161 if (Buffer.isBuffer(chunk)) {
162 // get length of Buffer163 len = chunk.length164 } else if (!generateETag && chunk.length < 1000) {165 // just calculate length when no ETag + small chunk166 len = Buffer.byteLength(chunk, encoding)167 } else {168 // convert chunk to Buffer and calculate169 chunk = Buffer.from(chunk, encoding)

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 172–176why, one step back
172 }
173174 this.set('Content-Length', len);175 }176

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 177–178why, one step back
177 // populate ETag
178 var etag;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 182–185why, one step back
182 }
183 }184185 // freshness

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

line 186why, one step back
186 if (req.fresh) this.status(304);

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 187–188why, one step back
187
188 // strip irrelevant headers

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 190–195why, one step back
190 this.removeHeader('Content-Type');
191 this.removeHeader('Content-Length');192 this.removeHeader('Transfer-Encoding');193 chunk = '';194 }195

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 196–202why, one step back
196 // alter headers for 205
197 if (this.statusCode === 205) {198 this.set('Content-Length', '0')199 this.removeHeader('Transfer-Encoding')200 chunk = ''201 }202

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 206–208why, one step back
206 } else {
207 // respond208 this.end(chunk, encoding);

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 209–222why, one step back
209 }
210211 return this;212};213214/**215 * Send JSON response.216 *217 * Examples:218 *219 * res.json(null);220 * res.json({ user: 'tj' });221 *222 * @param {string|number|boolean|object} obj

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 223why, one step back
223 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 224–228why, one step back
224 */
225226res.json = function json(obj) {227 // settings228 var app = this.app;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 233–250why, one step back
233
234 // content-type235 if (!this.get('Content-Type')) {236 this.set('Content-Type', 'application/json');237 }238239 return this.send(body);240};241242/**243 * Send JSON response with JSONP callback support.244 *245 * Examples:246 *247 * res.jsonp(null);248 * res.jsonp({ user: 'tj' });249 *250 * @param {string|number|boolean|object} obj

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 251why, one step back
251 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 252–256why, one step back
252 */
253254res.jsonp = function jsonp(obj) {255 // settings256 var app = this.app;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 261–281why, one step back
261 var callback = this.req.query[app.get('jsonp callback name')];
262263 // content-type264 if (!this.get('Content-Type')) {265 this.set('X-Content-Type-Options', 'nosniff');266 this.set('Content-Type', 'application/json');267 }268269 // fixup callback270 if (Array.isArray(callback)) {271 callback = callback[0];272 }273274 // jsonp275 if (typeof callback === 'string' && callback.length !== 0) {276 this.set('X-Content-Type-Options', 'nosniff');277 this.set('Content-Type', 'text/javascript');278279 // restrict callback charset280 callback = callback.replace(/[^\[\]\w$.]/g, '');281

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

lines 282–290why, one step back
282 if (body === undefined) {
283 // empty argument284 body = ''285 } else if (typeof body === 'string') {286 // replace chars not allowed in JavaScript that are in JSON287 body = body288 .replace(/\u2028/g, '\\u2028')289 .replace(/\u2029/g, '\\u2029')290 }

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 291–300why, one step back
291
292 // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"293 // the typeof check is just to reduce client error noise294 body = '/**/ typeof ' + callback + ' === \'function\' && ' + callback + '(' + body + ');';295 }296297 return this.send(body);298};299300/**

Add res.sendStatusSeth Samuelbefore adoption12f92a5

lines 301–311why, one step back
301 * Send given HTTP status code.
302 *303 * Sets the response status to `statusCode` and the body of the304 * response to the standard description from node's http.STATUS_CODES305 * or the statusCode number if no description.306 *307 * Examples:308 *309 * res.sendStatus(200);310 *311 * @param {number} statusCode

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 312why, one step back
312 * @public

Add res.sendStatusSeth Samuelbefore adoption12f92a5

lines 313–315why, one step back
313 */
314315res.sendStatus = function sendStatus(statusCode) {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Add res.sendStatusSeth Samuelbefore adoption12f92a5

line 317why, one step back
317

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

line 318why, one step back
318 this.status(statusCode);

Add res.sendStatusSeth Samuelbefore adoption12f92a5

lines 319–324why, one step back
319 this.type('txt');
320321 return this.send(body);322};323324/**

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 330–361why, one step back
330 * if you wish to attempt responding, as the header and some data
331 * may have already been transferred.332 *333 * Options:334 *335 * - `maxAge` defaulting to 0 (can be string converted by `ms`)336 * - `root` root directory for relative filenames337 * - `headers` object of headers to serve with file338 * - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them339 *340 * Other options are passed along to `send`.341 *342 * Examples:343 *344 * The following example illustrates how `res.sendFile()` may345 * be used as an alternative for the `static()` middleware for346 * dynamic situations. The code backing `res.sendFile()` is actually347 * the same code, so HTTP cache support etc is identical.348 *349 * app.get('/user/:uid/photos/:file', function(req, res){350 * var uid = req.params.uid351 * , file = req.params.file;352 *353 * req.user.mayViewFilesFrom(uid, function(yes){354 * if (yes) {355 * res.sendFile('/uploads/' + uid + '/' + file);356 * } else {357 * res.send(403, 'Sorry! you cant see that.');358 * }359 * });360 * });361 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 362why, one step back
362 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 363–364why, one step back
363 */
364

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 371–375why, one step back
371
372 if (!path) {373 throw new TypeError('path argument is required to res.sendFile');374 }375

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 376–379why, one step back
376 if (typeof path !== 'string') {
377 throw new TypeError('path must be a string to res.sendFile')378 }379

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 382–383why, one step back
382 done = options;
383 opts = {};

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 384–385why, one step back
384 }
385

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

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

line 393why, one step back
393

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 394why, one step back
394 // transfer

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

line 398why, one step back
398

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 401–403why, one step back
401 next(err);
402 }403 });

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 404–409why, one step back
404};
405406/**407 * Transfer the file at the given `path` as an attachment.408 *409 * Optionally providing an alternate attachment `filename`,

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

line 413why, one step back
413 *

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 414–418why, one step back
414 * Optionally providing an `options` object to use with `res.sendFile()`.
415 * This function will set the `Content-Disposition` header, overriding416 * any `Content-Disposition` header passed as header options in order417 * to set the attachment and filename.418 *

Remove res.sendfileDouglas Christopher Wilsonbefore adoption0fc4f07

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

line 420why, one step back
420 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 421why, one step back
421 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 422–423why, one step back
422 */
423

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 425–426why, one step back
425 var done = callback;
426 var name = filename;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 428why, one step back
428

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 431–432why, one step back
431 done = filename;
432 name = null;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 433–436why, one step back
433 opts = null
434 } else if (typeof options === 'function') {435 done = options436 opts = null

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 437–438why, one step back
437 }
438

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 439–445why, one step back
439 // support optional filename, where options may be in it's place
440 if (typeof filename === 'object' &&441 (typeof options === 'function' || options === undefined)) {442 name = null443 opts = filename444 }445

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 449–450why, one step back
449 };
450

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 451–465why, one step back
451 // merge user-provided headers
452 if (opts && opts.headers) {453 var keys = Object.keys(opts.headers)454 for (var i = 0; i < keys.length; i++) {455 var key = keys[i]456 if (key.toLowerCase() !== 'content-disposition') {457 headers[key] = opts.headers[key]458 }459 }460 }461462 // merge user-provided options463 opts = Object.create(opts)464 opts.headers = headers465

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 467–469why, one step back
467 var fullPath = !opts.root
468 ? resolve(path)469 : path

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

line 470why, one step back
470

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 473–475why, one step back
473};
474475/**

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

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

line 477why, one step back
477 * 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 478–479why, one step back
478 * When no mapping is found though `mime.contentType()`, the type is set to
479 * "application/octet-stream".

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 480–490why, one step back
480 *
481 * Examples:482 *483 * res.type('.html');484 * res.type('html');485 * res.type('json');486 * res.type('application/json');487 * res.type('png');488 *489 * @param {String} type490 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 491why, one step back
491 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 492–494why, one step back
492 */
493494res.contentType =

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

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

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 498–500why, one step back
498 : type;
499500 return this.set('Content-Type', ct);

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 501–526why, one step back
501};
502503/**504 * Respond to the Acceptable formats using an `obj`505 * of mime-type callbacks.506 *507 * This method uses `req.accepted`, an array of508 * acceptable types ordered by their quality values.509 * When "Accept" is not present the _first_ callback510 * is invoked, otherwise the first match is used. When511 * no match is performed the server responds with512 * 406 "Not Acceptable".513 *514 * Content-Type is set for you, however if you choose515 * you may alter this within the callback using `res.type()`516 * or `res.set('Content-Type', ...)`.517 *518 * res.format({519 * 'text/plain': function(){520 * res.send('hey');521 * },522 *523 * 'text/html': function(){524 * res.send('<p>hey</p>');525 * },526 *

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 557why, one step back
557 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 558–563why, one step back
558 */
559560res.format = function(obj){561 var req = this.req;562 var next = req.next;563

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

line 566why, one step back
566

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 567–569why, one step back
567 var key = keys.length > 0
568 ? req.accepts(keys)569 : false;

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 570–575why, one step back
570
571 this.vary("Accept");572573 if (key) {574 this.set('Content-Type', normalizeType(key).value);575 obj[key](req, this, next);

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

line 578why, one step back
578 } else {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 582–591why, one step back
582 }
583584 return this;585};586587/**588 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.589 *590 * @param {String} filename591 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 592why, one step back
592 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 593–594why, one step back
593 */
594

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

lines 595–599why, one step back
595res.attachment = function attachment(filename) {
596 if (filename) {597 this.type(extname(filename));598 }599

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

line 601why, one step back
601

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 602–605why, one step back
602 return this;
603};604605/**

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 606–616why, one step back
606 * Append additional header `field` with value `val`.
607 *608 * Example:609 *610 * res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);611 * res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');612 * res.append('Warning', '199 Miscellaneous warning');613 *614 * @param {String} field615 * @param {String|Array} val616 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 617why, one step back
617 * @public

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 618–627why, one step back
618 */
619620res.append = function append(field, val) {621 var prev = this.get(field);622 var value = val;623624 if (prev) {625 // concat the new and prev vals626 value = Array.isArray(prev) ? prev.concat(val)627 : Array.isArray(val) ? [prev].concat(val)

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 629–634why, one step back
629 }
630631 return this.set(field, value);632};633634/**

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 635–645why, one step back
635 * Set header `field` to `val`, or pass
636 * an object of header fields.637 *638 * Examples:639 *640 * res.set('Foo', ['bar', 'baz']);641 * res.set('Accept', 'application/json');642 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });643 *644 * Aliased as `res.header()`.645 *

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

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 652why, one step back
652 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 653–657why, one step back
653 */
654655res.set =656res.header = function header(field, val) {657 if (arguments.length === 2) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 658–662why, one step back
658 var value = Array.isArray(val)
659 ? val.map(String)660 : String(val);661662 // add charset to content-type

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

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

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

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

line 668why, one step back
668 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 669–670why, one step back
669
670 this.setHeader(field, value);

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 671–683why, one step back
671 } else {
672 for (var key in field) {673 this.set(key, field[key]);674 }675 }676 return this;677};678679/**680 * Get value for header `field`.681 *682 * @param {String} field683 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 684why, one step back
684 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 685–694why, one step back
685 */
686687res.get = function(field){688 return this.getHeader(field);689};690691/**692 * Clear cookie `name`.693 *694 * @param {String} name

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 697why, one step back
697 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 698–699why, one step back
698 */
699

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 700why, one step back
700res.clearCookie = function clearCookie(name, options) {

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

lines 701–704why, one step back
701 // Force cookie expiration by setting expires to the past
702 const opts = { path: '/', ...options, expires: new Date(1)};703 // ensure maxAge is not passed704 delete opts.maxAge

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 705–706why, one step back
705
706 return this.cookie(name, '', opts);

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 707–709why, one step back
707};
708709/**

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 711–722why, one step back
711 *
712 * Options:713 *714 * - `maxAge` max-age in milliseconds, converted to `expires`715 * - `signed` sign the cookie716 * - `path` defaults to "/"717 *718 * Examples:719 *720 * // "Remember Me" for 15 minutes721 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });722 *

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 730why, one step back
730 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 731–732why, one step back
731 */
732

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 733why, one step back
733res.cookie = function (name, value, options) {

fix: Remove `utils-merge` dependency - use spread syntax instead (#6091)Phillip Bartabefore adoption246f6f5

line 734why, one step back
734 var opts = { ...options };

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 740–741why, one step back
740 }
741

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 742–747why, one step back
742 var val = typeof value === 'object'
743 ? 'j:' + JSON.stringify(value)744 : String(value);745746 if (signed) {747 val = 's:' + sign(val, secret);

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

line 748why, one step back
748 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 749why, one step back
749

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 750–756why, one step back
750 if (opts.maxAge != null) {
751 var maxAge = opts.maxAge - 0752753 if (!isNaN(maxAge)) {754 opts.expires = new Date(Date.now() + maxAge)755 opts.maxAge = Math.floor(maxAge / 1000)756 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 757–764why, one step back
757 }
758759 if (opts.path == null) {760 opts.path = '/';761 }762763 this.append('Set-Cookie', cookie.serialize(name, String(val), opts));764

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 765–781why, one step back
765 return this;
766};767768/**769 * Set the location header to `url`.770 *771 * The given `url` can also be "back", which redirects772 * to the _Referrer_ or _Referer_ headers or "/".773 *774 * Examples:775 *776 * res.location('/foo/bar').;777 * res.location('http://example.com');778 * res.location('../login');779 *780 * @param {String} url781 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 782why, one step back
782 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 783–784why, one step back
783 */
784

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Delete `back` as a magic string (#5933)Blake Embreybefore adoptionbdd81f8

line 786why, one step back
786 return this.set('Location', encodeUrl(url));

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 787–799why, one step back
787};
788789/**790 * Redirect to the given `url` with optional response `status`791 * defaulting to 302.792 *793 * Examples:794 *795 * res.redirect('/foo/bar');796 * res.redirect('http://example.com');797 * res.redirect(301, 'http://example.com');798 * res.redirect('../login'); // /blog/post/1 -> /blog/login799 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 800why, one step back
800 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 801–809why, one step back
801 */
802803res.redirect = function redirect(url) {804 var address = url;805 var body;806 var status = 302;807808 // allow status / url809 if (arguments.length === 2) {

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

lines 810–811why, one step back
810 status = arguments[0]
811 address = arguments[1]

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 812–814why, one step back
812 }
813814 // Set location header

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 816–819why, one step back
816
817 // Support text/{plain,html} by default818 this.format({819 text: function(){

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 821–823why, one step back
821 },
822823 html: function(){

Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f

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

Merge tag '4.20.0' into 5.0Wes Toddbefore adoptione5feb9f

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 826–833why, one step back
826 },
827828 default: function(){829 body = '';830 }831 });832833 // Respond

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

line 834why, one step back
834 this.status(status);

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 835–838why, one step back
835 this.set('Content-Length', Buffer.byteLength(body));
836837 if (this.req.method === 'HEAD') {838 this.end();

Merge tag '4.11.2'Douglas Christopher Wilsonbefore adoptiona9ef9e1

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 841–849why, one step back
841 }
842};843844/**845 * Add `field` to Vary. If already present in the Vary set, then846 * this call is simply ignored.847 *848 * @param {Array|String} field849 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 850why, one step back
850 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 851–868why, one step back
851 */
852853res.vary = function(field){854 vary(this, field);855856 return this;857};858859/**860 * Render `view` with the given `options` and optional callback `fn`.861 * When a callback function is given a response will _not_ be made862 * automatically, otherwise a response of _200_ and _text/html_ is given.863 *864 * Options:865 *866 * - `cache` boolean hinting to the engine it should cache867 * - `filename` filename of the view being rendered868 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 869why, one step back
869 * @public

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 870–871why, one step back
870 */
871

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 872–875why, one step back
872res.render = function render(view, options, callback) {
873 var app = this.req.app;874 var done = callback;875 var opts = options || {};

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 878–879why, one step back
878
879 // support callback function as second arg

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 880–882why, one step back
880 if (typeof options === 'function') {
881 done = options;882 opts = {};

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 883–885why, one step back
883 }
884885 // merge res.locals

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 887–888why, one step back
887
888 // default callback to respond

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

lines 890–894why, one step back
890 if (err) return req.next(err);
891 self.send(str);892 };893894 // render

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0

line 896why, one step back
896};

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 897–900why, one step back
897
898// pipe the send file stream899function sendfile(res, file, options, callback) {900 var done = false;

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 901why, one step back
901 var streaming;

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 902why, one step back
902

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 903–908why, one step back
903 // request aborted
904 function onaborted() {905 if (done) return;906 done = true;907908 var err = new Error('Request aborted');

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 910–912why, one step back
910 callback(err);
911 }912

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 913–936why, one step back
913 // directory
914 function ondirectory() {915 if (done) return;916 done = true;917918 var err = new Error('EISDIR, read');919 err.code = 'EISDIR';920 callback(err);921 }922923 // errors924 function onerror(err) {925 if (done) return;926 done = true;927 callback(err);928 }929930 // ended931 function onend() {932 if (done) return;933 done = true;934 callback();935 }936

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 937–941why, one step back
937 // file
938 function onfile() {939 streaming = false;940 }941

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 942–943why, one step back
942 // finished
943 function onfinish(err) {

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 945–948why, one step back
945 if (err) return onerror(err);
946 if (done) return;947948 setImmediate(function () {

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 949–953why, one step back
949 if (streaming !== false && !done) {
950 onaborted();951 return;952 }953

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 954–955why, one step back
954 if (done) return;
955 done = true;

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 957–959why, one step back
957 });
958 }959

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 960–965why, one step back
960 // streaming
961 function onstream() {962 streaming = true;963 }964965 file.on('directory', ondirectory);

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 970–986why, one step back
970 onFinished(res, onfinish);
971972 if (options.headers) {973 // set headers on successful transfer974 file.on('headers', function headers(res) {975 var obj = options.headers;976 var keys = Object.keys(obj);977978 for (var i = 0; i < keys.length; i++) {979 var k = keys[i];980 res.setHeader(k, obj[k]);981 }982 });983 }984985 // pipe986 file.pipe(res);

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 987–989why, one step back
987}
988989/**

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 995–997why, one step back
995 * @param {number} spaces
996 * @param {boolean} escape997 * @returns {string}

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 998–1000why, one step back
998 * @private
999 */1000

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 1007why, one step back
1007

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1009–1016why, one step back
1009 json = json.replace(/[<>&]/g, function (c) {
1010 switch (c.charCodeAt(0)) {1011 case 0x3c:1012 return '\\u003c'1013 case 0x3e:1014 return '\\u003e'1015 case 0x26:1016 return '\\u0026'

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1018–1024why, one step back
1018 default:
1019 return c1020 }1021 })1022 }10231024 return json

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 1025why, one step back
1025}