Skip to content
Myr

response.js

This file

Path

lib/response.js

1,040 lines · 23.9 KiB · JavaScript

Last changed by

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

History of this file

Take it

The walk stopped after 200 commits: lines older than 7d0f1c3 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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

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

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

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

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

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

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

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 35–38why, one step back
35var vary = require('vary');
3637/**38 * Response prototype.

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

line 39why, one step back
39 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

lines 45–51why, one step back
45 * Module exports.
46 * @public47 */4849module.exports = res5051/**

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

line 53why, one step back
53 *

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

lines 54–60why, one step back
54 * Expects an integer value between 100 and 999 inclusive.
55 * Throws an error if the provided status code is not an integer or if it's outside the allowable range.56 *57 * @param {number} code - The HTTP status code to set.58 * @return {ServerResponse} - Returns itself for chaining methods.59 * @throws {TypeError} If `code` is not an integer.60 * @throws {RangeError} If `code` is outside the range 100 to 999.

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 61why, one step back
61 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 62–63why, one step back
62 */
63

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

lines 65–67why, one step back
65 // Check if the status code is not an integer
66 if (!Number.isInteger(code)) {67 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 68why, one step back
68 }

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

lines 69–73why, one step back
69 // Check if the status code is outside of Node's valid range
70 if (code < 100 || code > 999) {71 throw new RangeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be greater than 99 and less than 1000.`);72 }73

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 74–89why, one step back
74 this.statusCode = code;
75 return this;76};7778/**79 * Set Link header field with the given `links`.80 *81 * Examples:82 *83 * res.links({84 * next: 'http://api.example.com/users?page=2',85 * last: 'http://api.example.com/users?page=5'86 * });87 *88 * @param {Object} links89 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 90why, one step back
90 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 91–105why, one step back
91 */
9293res.links = function(links){94 var link = this.get('Link') || '';95 if (link) link += ', ';96 return this.set('Link', link + Object.keys(links).map(function(rel){97 return '<' + links[rel] + '>; rel="' + rel + '"';98 }).join(', '));99};100101/**102 * Send a response.103 *104 * Examples:105 *

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 111why, one step back
111 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 112–155why, one step back
112 */
113114res.send = function send(body) {115 var chunk = body;116 var encoding;117 var req = this.req;118 var type;119120 // settings121 var app = this.app;122123 switch (typeof chunk) {124 // string defaulting to html125 case 'string':126 if (!this.get('Content-Type')) {127 this.type('html');128 }129 break;130 case 'boolean':131 case 'number':132 case 'object':133 if (chunk === null) {134 chunk = '';135 } else if (Buffer.isBuffer(chunk)) {136 if (!this.get('Content-Type')) {137 this.type('bin');138 }139 } else {140 return this.json(chunk);141 }142 break;143 }144145 // write strings in utf-8146 if (typeof chunk === 'string') {147 encoding = 'utf8';148 type = this.get('Content-Type');149150 // reflect this in content-type151 if (typeof type === 'string') {152 this.set('Content-Type', setCharset(type, 'utf-8'));153 }154 }155

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 161why, one step back
161 var len

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 163–171why, one step back
163 if (Buffer.isBuffer(chunk)) {
164 // get length of Buffer165 len = chunk.length166 } else if (!generateETag && chunk.length < 1000) {167 // just calculate length when no ETag + small chunk168 len = Buffer.byteLength(chunk, encoding)169 } else {170 // convert chunk to Buffer and calculate171 chunk = Buffer.from(chunk, encoding)

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 174–178why, one step back
174 }
175176 this.set('Content-Length', len);177 }178

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 179–180why, one step back
179 // populate ETag
180 var etag;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 184–187why, one step back
184 }
185 }186187 // freshness

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 189–190why, one step back
189
190 // strip irrelevant headers

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 208–210why, one step back
208 } else {
209 // respond210 this.end(chunk, encoding);

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 211–224why, one step back
211 }
212213 return this;214};215216/**217 * Send JSON response.218 *219 * Examples:220 *221 * res.json(null);222 * res.json({ user: 'tj' });223 *224 * @param {string|number|boolean|object} obj

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 225why, one step back
225 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 226–230why, one step back
226 */
227228res.json = function json(obj) {229 // settings230 var app = this.app;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 235–252why, one step back
235
236 // content-type237 if (!this.get('Content-Type')) {238 this.set('Content-Type', 'application/json');239 }240241 return this.send(body);242};243244/**245 * Send JSON response with JSONP callback support.246 *247 * Examples:248 *249 * res.jsonp(null);250 * res.jsonp({ user: 'tj' });251 *252 * @param {string|number|boolean|object} obj

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 253why, one step back
253 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 254–258why, one step back
254 */
255256res.jsonp = function jsonp(obj) {257 // settings258 var app = this.app;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 263–283why, one step back
263 var callback = this.req.query[app.get('jsonp callback name')];
264265 // content-type266 if (!this.get('Content-Type')) {267 this.set('X-Content-Type-Options', 'nosniff');268 this.set('Content-Type', 'application/json');269 }270271 // fixup callback272 if (Array.isArray(callback)) {273 callback = callback[0];274 }275276 // jsonp277 if (typeof callback === 'string' && callback.length !== 0) {278 this.set('X-Content-Type-Options', 'nosniff');279 this.set('Content-Type', 'text/javascript');280281 // restrict callback charset282 callback = callback.replace(/[^\[\]\w$.]/g, '');283

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

lines 284–292why, one step back
284 if (body === undefined) {
285 // empty argument286 body = ''287 } else if (typeof body === 'string') {288 // replace chars not allowed in JavaScript that are in JSON289 body = body290 .replace(/\u2028/g, '\\u2028')291 .replace(/\u2029/g, '\\u2029')292 }

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 293–299why, one step back
293
294 // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"295 // the typeof check is just to reduce client error noise296 body = '/**/ typeof ' + callback + ' === \'function\' && ' + callback + '(' + body + ');';297 }298299 return this.send(body);

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 300–302why, one step back
300};
301302/**

Add res.sendStatusSeth Samuelbefore adoption12f92a5

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 314why, one step back
314 * @public

Add res.sendStatusSeth Samuelbefore adoption12f92a5

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Add res.sendStatusSeth Samuelbefore adoption12f92a5

line 319why, one step back
319

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

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

Add res.sendStatusSeth Samuelbefore adoption12f92a5

lines 321–326why, one step back
321 this.type('txt');
322323 return this.send(body);324};325326/**

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 364why, one step back
364 * @public

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 365–366why, one step back
365 */
366

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 384–385why, one step back
384 done = options;
385 opts = {};

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 386–387why, one step back
386 }
387

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

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 395why, one step back
395

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 396why, one step back
396 // transfer

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 400why, one step back
400

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 403–405why, one step back
403 next(err);
404 }405 });

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 406–411why, one step back
406};
407408/**409 * Transfer the file at the given `path` as an attachment.410 *411 * Optionally providing an alternate attachment `filename`,

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

line 415why, one step back
415 *

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 416–420why, one step back
416 * Optionally providing an `options` object to use with `res.sendFile()`.
417 * This function will set the `Content-Disposition` header, overriding418 * any `Content-Disposition` header passed as header options in order419 * to set the attachment and filename.420 *

Remove res.sendfileDouglas Christopher Wilsonbefore adoption0fc4f07

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

line 422why, one step back
422 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 423why, one step back
423 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 424–425why, one step back
424 */
425

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 427–428why, one step back
427 var done = callback;
428 var name = filename;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 430why, one step back
430

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 433–434why, one step back
433 done = filename;
434 name = null;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 435–438why, one step back
435 opts = null
436 } else if (typeof options === 'function') {437 done = options438 opts = null

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 439–440why, one step back
439 }
440

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 441–447why, one step back
441 // support optional filename, where options may be in it's place
442 if (typeof filename === 'object' &&443 (typeof options === 'function' || options === undefined)) {444 name = null445 opts = filename446 }447

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 451–452why, one step back
451 };
452

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 453–467why, one step back
453 // merge user-provided headers
454 if (opts && opts.headers) {455 var keys = Object.keys(opts.headers)456 for (var i = 0; i < keys.length; i++) {457 var key = keys[i]458 if (key.toLowerCase() !== 'content-disposition') {459 headers[key] = opts.headers[key]460 }461 }462 }463464 // merge user-provided options465 opts = Object.create(opts)466 opts.headers = headers467

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

line 472why, one step back
472

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 475–477why, one step back
475};
476477/**

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

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 493why, one step back
493 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 494–496why, one step back
494 */
495496res.contentType =

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

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

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 559why, one step back
559 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 560–565why, one step back
560 */
561562res.format = function(obj){563 var req = this.req;564 var next = req.next;565

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

line 568why, one step back
568

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 572–577why, one step back
572
573 this.vary("Accept");574575 if (key) {576 this.set('Content-Type', normalizeType(key).value);577 obj[key](req, this, next);

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

line 580why, one step back
580 } else {

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 584–593why, one step back
584 }
585586 return this;587};588589/**590 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.591 *592 * @param {String} filename593 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 594why, one step back
594 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 595–596why, one step back
595 */
596

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

line 603why, one step back
603

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 604–607why, one step back
604 return this;
605};606607/**

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 619why, one step back
619 * @public

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 620–629why, one step back
620 */
621622res.append = function append(field, val) {623 var prev = this.get(field);624 var value = val;625626 if (prev) {627 // concat the new and prev vals628 value = Array.isArray(prev) ? prev.concat(val)629 : Array.isArray(val) ? [prev].concat(val)

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 631–636why, one step back
631 }
632633 return this.set(field, value);634};635636/**

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

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

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 654why, one step back
654 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 655–659why, one step back
655 */
656657res.set =658res.header = function header(field, val) {659 if (arguments.length === 2) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

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

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

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

line 670why, one step back
670 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 671–672why, one step back
671
672 this.setHeader(field, value);

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 673–685why, one step back
673 } else {
674 for (var key in field) {675 this.set(key, field[key]);676 }677 }678 return this;679};680681/**682 * Get value for header `field`.683 *684 * @param {String} field685 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 686why, one step back
686 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 687–696why, one step back
687 */
688689res.get = function(field){690 return this.getHeader(field);691};692693/**694 * Clear cookie `name`.695 *696 * @param {String} name

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 699why, one step back
699 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 700–701why, one step back
700 */
701

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 709–711why, one step back
709};
710711/**

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 732why, one step back
732 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 733–734why, one step back
733 */
734

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 742–743why, one step back
742 }
743

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

line 750why, one step back
750 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 751why, one step back
751

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

lines 752–758why, one step back
752 if (opts.maxAge != null) {
753 var maxAge = opts.maxAge - 0754755 if (!isNaN(maxAge)) {756 opts.expires = new Date(Date.now() + maxAge)757 opts.maxAge = Math.floor(maxAge / 1000)758 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 759–766why, one step back
759 }
760761 if (opts.path == null) {762 opts.path = '/';763 }764765 this.append('Set-Cookie', cookie.serialize(name, String(val), opts));766

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 767–783why, one step back
767 return this;
768};769770/**771 * Set the location header to `url`.772 *773 * The given `url` can also be "back", which redirects774 * to the _Referrer_ or _Referer_ headers or "/".775 *776 * Examples:777 *778 * res.location('/foo/bar').;779 * res.location('http://example.com');780 * res.location('../login');781 *782 * @param {String} url783 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 784why, one step back
784 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 785–786why, one step back
785 */
786

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge branch '5.0' into 5.xWes Toddbefore adoption5e2345e

line 788why, one step back
788 var loc;

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 789–790why, one step back
789
790 // "back" is an alias for the referrer

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 791–792why, one step back
791 if (url === 'back') {
792 loc = this.req.get('Referrer') || '/';

Merge branch '5.0' into 5.xWes Toddbefore adoption5e2345e

lines 793–794why, one step back
793 } else {
794 loc = String(url);

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 795why, one step back
795 }

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

line 796why, one step back
796

Merge branch 'master' into 5.0ctcpipbefore adoptionc96c690

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 798–814why, one step back
798};
799800/**801 * Redirect to the given `url` with optional response `status`802 * defaulting to 302.803 *804 * The resulting `url` is determined by `res.location()`, so805 * it will play nicely with mounted apps, relative paths,806 * `"back"` etc.807 *808 * Examples:809 *810 * res.redirect('/foo/bar');811 * res.redirect('http://example.com');812 * res.redirect(301, 'http://example.com');813 * res.redirect('../login'); // /blog/post/1 -> /blog/login814 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 815why, one step back
815 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 816–824why, one step back
816 */
817818res.redirect = function redirect(url) {819 var address = url;820 var body;821 var status = 302;822823 // allow status / url824 if (arguments.length === 2) {

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

lines 825–826why, one step back
825 status = arguments[0]
826 address = arguments[1]

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 827–829why, one step back
827 }
828829 // Set location header

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 831–834why, one step back
831
832 // Support text/{plain,html} by default833 this.format({834 text: function(){

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 836–838why, one step back
836 },
837838 html: function(){

Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 841–848why, one step back
841 },
842843 default: function(){844 body = '';845 }846 });847848 // Respond

Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.11.2'Douglas Christopher Wilsonbefore adoptiona9ef9e1

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 856–864why, one step back
856 }
857};858859/**860 * Add `field` to Vary. If already present in the Vary set, then861 * this call is simply ignored.862 *863 * @param {Array|String} field864 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 865why, one step back
865 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 866–883why, one step back
866 */
867868res.vary = function(field){869 vary(this, field);870871 return this;872};873874/**875 * Render `view` with the given `options` and optional callback `fn`.876 * When a callback function is given a response will _not_ be made877 * automatically, otherwise a response of _200_ and _text/html_ is given.878 *879 * Options:880 *881 * - `cache` boolean hinting to the engine it should cache882 * - `filename` filename of the view being rendered883 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 884why, one step back
884 * @public

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 885–886why, one step back
885 */
886

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 887–890why, one step back
887res.render = function render(view, options, callback) {
888 var app = this.req.app;889 var done = callback;890 var opts = options || {};

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 893–894why, one step back
893
894 // support callback function as second arg

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 895–897why, one step back
895 if (typeof options === 'function') {
896 done = options;897 opts = {};

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 898–900why, one step back
898 }
899900 // merge res.locals

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 902–903why, one step back
902
903 // default callback to respond

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

lines 905–909why, one step back
905 if (err) return req.next(err);
906 self.send(str);907 };908909 // render

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

deps: serve-static@~1.5.0Douglas Christopher Wilsonbefore adoption7d0f1c3

line 911why, one step back
911};

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 912–915why, one step back
912
913// pipe the send file stream914function sendfile(res, file, options, callback) {915 var done = false;

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 916why, one step back
916 var streaming;

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 917why, one step back
917

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 918–923why, one step back
918 // request aborted
919 function onaborted() {920 if (done) return;921 done = true;922923 var err = new Error('Request aborted');

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 925–927why, one step back
925 callback(err);
926 }927

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 928–951why, one step back
928 // directory
929 function ondirectory() {930 if (done) return;931 done = true;932933 var err = new Error('EISDIR, read');934 err.code = 'EISDIR';935 callback(err);936 }937938 // errors939 function onerror(err) {940 if (done) return;941 done = true;942 callback(err);943 }944945 // ended946 function onend() {947 if (done) return;948 done = true;949 callback();950 }951

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 952–956why, one step back
952 // file
953 function onfile() {954 streaming = false;955 }956

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 957–958why, one step back
957 // finished
958 function onfinish(err) {

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 960–963why, one step back
960 if (err) return onerror(err);
961 if (done) return;962963 setImmediate(function () {

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 964–968why, one step back
964 if (streaming !== false && !done) {
965 onaborted();966 return;967 }968

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 969–970why, one step back
969 if (done) return;
970 done = true;

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 972–974why, one step back
972 });
973 }974

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 975–980why, one step back
975 // streaming
976 function onstream() {977 streaming = true;978 }979980 file.on('directory', ondirectory);

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 985–1001why, one step back
985 onFinished(res, onfinish);
986987 if (options.headers) {988 // set headers on successful transfer989 file.on('headers', function headers(res) {990 var obj = options.headers;991 var keys = Object.keys(obj);992993 for (var i = 0; i < keys.length; i++) {994 var k = keys[i];995 res.setHeader(k, obj[k]);996 }997 });998 }9991000 // pipe1001 file.pipe(res);

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 1002–1004why, one step back
1002}
10031004/**

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1010–1012why, one step back
1010 * @param {number} spaces
1011 * @param {boolean} escape1012 * @returns {string}

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 1013–1015why, one step back
1013 * @private
1014 */1015

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 1022why, one step back
1022

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 1033–1039why, one step back
1033 default:
1034 return c1035 }1036 })1037 }10381039 return json

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 1040why, one step back
1040}