Skip to content
Myr

response.js

This file

Path

lib/response.js

1,003 lines · 22.5 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 12why, one step back
12 * @private

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

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

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 18–19why, one step back
18var escapeHtml = require('escape-html');
19var http = require('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')

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

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

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

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

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

Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 34–37why, one step back
34var vary = require('vary');
3536/**37 * Response prototype.

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

line 38why, one step back
38 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

lines 44–50why, one step back
44 * Module exports.
45 * @public46 */4748module.exports = res4950/**

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 51–54why, one step back
51 * Set status `code`.
52 *53 * @param {Number} code54 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 55why, one step back
55 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 56–57why, one step back
56 */
57

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 59–74why, one step back
59 this.statusCode = code;
60 return this;61};6263/**64 * Set Link header field with the given `links`.65 *66 * Examples:67 *68 * res.links({69 * next: 'http://api.example.com/users?page=2',70 * last: 'http://api.example.com/users?page=5'71 * });72 *73 * @param {Object} links74 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 75why, one step back
75 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 76–90why, one step back
76 */
7778res.links = function(links){79 var link = this.get('Link') || '';80 if (link) link += ', ';81 return this.set('Link', link + Object.keys(links).map(function(rel){82 return '<' + links[rel] + '>; rel="' + rel + '"';83 }).join(', '));84};8586/**87 * Send a response.88 *89 * Examples:90 *

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 96why, one step back
96 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 97–140why, one step back
97 */
9899res.send = function send(body) {100 var chunk = body;101 var encoding;102 var req = this.req;103 var type;104105 // settings106 var app = this.app;107108 switch (typeof chunk) {109 // string defaulting to html110 case 'string':111 if (!this.get('Content-Type')) {112 this.type('html');113 }114 break;115 case 'boolean':116 case 'number':117 case 'object':118 if (chunk === null) {119 chunk = '';120 } else if (Buffer.isBuffer(chunk)) {121 if (!this.get('Content-Type')) {122 this.type('bin');123 }124 } else {125 return this.json(chunk);126 }127 break;128 }129130 // write strings in utf-8131 if (typeof chunk === 'string') {132 encoding = 'utf8';133 type = this.get('Content-Type');134135 // reflect this in content-type136 if (typeof type === 'string') {137 this.set('Content-Type', setCharset(type, 'utf-8'));138 }139 }140

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 141–144why, one step back
141 // determine if ETag should be generated
142 var etagFn = app.get('etag fn')143 var generateETag = !this.get('ETag') && typeof etagFn === 'function'144

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 146why, one step back
146 var len

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 148–156why, one step back
148 if (Buffer.isBuffer(chunk)) {
149 // get length of Buffer150 len = chunk.length151 } else if (!generateETag && chunk.length < 1000) {152 // just calculate length when no ETag + small chunk153 len = Buffer.byteLength(chunk, encoding)154 } else {155 // convert chunk to Buffer and calculate156 chunk = Buffer.from(chunk, encoding)

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 159–163why, one step back
159 }
160161 this.set('Content-Length', len);162 }163

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

lines 164–165why, one step back
164 // populate ETag
165 var etag;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 169–175why, one step back
169 }
170 }171172 // freshness173 if (req.fresh) this.statusCode = 304;174175 // strip irrelevant headers

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 177–182why, one step back
177 this.removeHeader('Content-Type');
178 this.removeHeader('Content-Length');179 this.removeHeader('Transfer-Encoding');180 chunk = '';181 }182

Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 186–188why, one step back
186 } else {
187 // respond188 this.end(chunk, encoding);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 189–202why, one step back
189 }
190191 return this;192};193194/**195 * Send JSON response.196 *197 * Examples:198 *199 * res.json(null);200 * res.json({ user: 'tj' });201 *202 * @param {string|number|boolean|object} obj

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 203why, one step back
203 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 204–208why, one step back
204 */
205206res.json = function json(obj) {207 // settings208 var app = this.app;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 213–230why, one step back
213
214 // content-type215 if (!this.get('Content-Type')) {216 this.set('Content-Type', 'application/json');217 }218219 return this.send(body);220};221222/**223 * Send JSON response with JSONP callback support.224 *225 * Examples:226 *227 * res.jsonp(null);228 * res.jsonp({ user: 'tj' });229 *230 * @param {string|number|boolean|object} obj

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 231why, one step back
231 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 232–236why, one step back
232 */
233234res.jsonp = function jsonp(obj) {235 // settings236 var app = this.app;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 241–261why, one step back
241 var callback = this.req.query[app.get('jsonp callback name')];
242243 // content-type244 if (!this.get('Content-Type')) {245 this.set('X-Content-Type-Options', 'nosniff');246 this.set('Content-Type', 'application/json');247 }248249 // fixup callback250 if (Array.isArray(callback)) {251 callback = callback[0];252 }253254 // jsonp255 if (typeof callback === 'string' && callback.length !== 0) {256 this.set('X-Content-Type-Options', 'nosniff');257 this.set('Content-Type', 'text/javascript');258259 // restrict callback charset260 callback = callback.replace(/[^\[\]\w$.]/g, '');261

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

lines 262–270why, one step back
262 if (body === undefined) {
263 // empty argument264 body = ''265 } else if (typeof body === 'string') {266 // replace chars not allowed in JavaScript that are in JSON267 body = body268 .replace(/\u2028/g, '\\u2028')269 .replace(/\u2029/g, '\\u2029')270 }

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 271–277why, one step back
271
272 // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"273 // the typeof check is just to reduce client error noise274 body = '/**/ typeof ' + callback + ' === \'function\' && ' + callback + '(' + body + ');';275 }276277 return this.send(body);

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 278–280why, one step back
278};
279280/**

Add res.sendStatusSeth Samuelbefore adoption12f92a5

lines 281–291why, one step back
281 * Send given HTTP status code.
282 *283 * Sets the response status to `statusCode` and the body of the284 * response to the standard description from node's http.STATUS_CODES285 * or the statusCode number if no description.286 *287 * Examples:288 *289 * res.sendStatus(200);290 *291 * @param {number} statusCode

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 292why, one step back
292 * @public

Add res.sendStatusSeth Samuelbefore adoption12f92a5

lines 293–295why, one step back
293 */
294295res.sendStatus = function sendStatus(statusCode) {

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

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

Add res.sendStatusSeth Samuelbefore adoption12f92a5

lines 297–304why, one step back
297
298 this.statusCode = statusCode;299 this.type('txt');300301 return this.send(body);302};303304/**

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 310–341why, one step back
310 * if you wish to attempt responding, as the header and some data
311 * may have already been transferred.312 *313 * Options:314 *315 * - `maxAge` defaulting to 0 (can be string converted by `ms`)316 * - `root` root directory for relative filenames317 * - `headers` object of headers to serve with file318 * - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them319 *320 * Other options are passed along to `send`.321 *322 * Examples:323 *324 * The following example illustrates how `res.sendFile()` may325 * be used as an alternative for the `static()` middleware for326 * dynamic situations. The code backing `res.sendFile()` is actually327 * the same code, so HTTP cache support etc is identical.328 *329 * app.get('/user/:uid/photos/:file', function(req, res){330 * var uid = req.params.uid331 * , file = req.params.file;332 *333 * req.user.mayViewFilesFrom(uid, function(yes){334 * if (yes) {335 * res.sendFile('/uploads/' + uid + '/' + file);336 * } else {337 * res.send(403, 'Sorry! you cant see that.');338 * }339 * });340 * });341 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 342why, one step back
342 * @public

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 343–344why, one step back
343 */
344

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 351–355why, one step back
351
352 if (!path) {353 throw new TypeError('path argument is required to res.sendFile');354 }355

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

lines 356–359why, one step back
356 if (typeof path !== 'string') {
357 throw new TypeError('path must be a string to res.sendFile')358 }359

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 362–363why, one step back
362 done = options;
363 opts = {};

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

lines 364–365why, one step back
364 }
365

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

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 373why, one step back
373

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 374why, one step back
374 // transfer

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f

line 378why, one step back
378

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 381–383why, one step back
381 next(err);
382 }383 });

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 384–389why, one step back
384};
385386/**387 * Transfer the file at the given `path` as an attachment.388 *389 * Optionally providing an alternate attachment `filename`,

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 391–393why, one step back
391 * when the data transfer is complete, or when an error has
392 * ocurred. Be sure to check `res.headersSent` if you plan to respond.393 *

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 394–398why, one step back
394 * Optionally providing an `options` object to use with `res.sendFile()`.
395 * This function will set the `Content-Disposition` header, overriding396 * any `Content-Disposition` header passed as header options in order397 * to set the attachment and filename.398 *

Remove res.sendfileDouglas Christopher Wilsonbefore adoption0fc4f07

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 400why, one step back
400 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 401why, one step back
401 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 402–403why, one step back
402 */
403

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 405–406why, one step back
405 var done = callback;
406 var name = filename;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 408why, one step back
408

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 411–412why, one step back
411 done = filename;
412 name = null;

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 413–416why, one step back
413 opts = null
414 } else if (typeof options === 'function') {415 done = options416 opts = null

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 417–420why, one step back
417 }
418419 // set Content-Disposition when file is sent420 var headers = {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 422–423why, one step back
422 };
423

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 424–438why, one step back
424 // merge user-provided headers
425 if (opts && opts.headers) {426 var keys = Object.keys(opts.headers)427 for (var i = 0; i < keys.length; i++) {428 var key = keys[i]429 if (key.toLowerCase() !== 'content-disposition') {430 headers[key] = opts.headers[key]431 }432 }433 }434435 // merge user-provided options436 opts = Object.create(opts)437 opts.headers = headers438

Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7

lines 439–441why, one step back
439 // Resolve the full path for sendFile
440 var fullPath = resolve(path);441

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 444–446why, one step back
444};
445446/**

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

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 451–461why, one step back
451 *
452 * Examples:453 *454 * res.type('.html');455 * res.type('html');456 * res.type('json');457 * res.type('application/json');458 * res.type('png');459 *460 * @param {String} type461 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 462why, one step back
462 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 463–465why, one step back
463 */
464465res.contentType =

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

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

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 469–471why, one step back
469 : type;
470471 return this.set('Content-Type', ct);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 472–497why, one step back
472};
473474/**475 * Respond to the Acceptable formats using an `obj`476 * of mime-type callbacks.477 *478 * This method uses `req.accepted`, an array of479 * acceptable types ordered by their quality values.480 * When "Accept" is not present the _first_ callback481 * is invoked, otherwise the first match is used. When482 * no match is performed the server responds with483 * 406 "Not Acceptable".484 *485 * Content-Type is set for you, however if you choose486 * you may alter this within the callback using `res.type()`487 * or `res.set('Content-Type', ...)`.488 *489 * res.format({490 * 'text/plain': function(){491 * res.send('hey');492 * },493 *494 * 'text/html': function(){495 * res.send('<p>hey</p>');496 * },497 *

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 499–527why, one step back
499 * res.send({ message: 'hey' });
500 * }501 * });502 *503 * In addition to canonicalized MIME types you may504 * also use extnames mapped to these types:505 *506 * res.format({507 * text: function(){508 * res.send('hey');509 * },510 *511 * html: function(){512 * res.send('<p>hey</p>');513 * },514 *515 * json: function(){516 * res.send({ message: 'hey' });517 * }518 * });519 *520 * By default Express passes an `Error`521 * with a `.status` of 406 to `next(err)`522 * if a match is not made. If you provide523 * a `.default` callback it will be invoked524 * instead.525 *526 * @param {Object} obj527 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 528why, one step back
528 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 529–538why, one step back
529 */
530531res.format = function(obj){532 var req = this.req;533 var next = req.next;534535 var fn = obj.default;536 if (fn) delete obj.default;537 var keys = Object.keys(obj);538

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 539–541why, one step back
539 var key = keys.length > 0
540 ? req.accepts(keys)541 : false;

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 542–551why, one step back
542
543 this.vary("Accept");544545 if (key) {546 this.set('Content-Type', normalizeType(key).value);547 obj[key](req, this, next);548 } else if (fn) {549 fn();550 } else {551 var err = new Error('Not Acceptable');

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 552why, one step back
552 err.status = err.statusCode = 406;

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 553–564why, one step back
553 err.types = normalizeTypes(keys).map(function(o){ return o.value });
554 next(err);555 }556557 return this;558};559560/**561 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.562 *563 * @param {String} filename564 * @return {ServerResponse}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 565why, one step back
565 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 566–567why, one step back
566 */
567

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

lines 568–572why, one step back
568res.attachment = function attachment(filename) {
569 if (filename) {570 this.type(extname(filename));571 }572

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218

line 574why, one step back
574

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 575–578why, one step back
575 return this;
576};577578/**

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 579–589why, one step back
579 * Append additional header `field` with value `val`.
580 *581 * Example:582 *583 * res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);584 * res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');585 * res.append('Warning', '199 Miscellaneous warning');586 *587 * @param {String} field588 * @param {String|Array} val589 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 590why, one step back
590 * @public

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 591–600why, one step back
591 */
592593res.append = function append(field, val) {594 var prev = this.get(field);595 var value = val;596597 if (prev) {598 // concat the new and prev vals599 value = Array.isArray(prev) ? prev.concat(val)600 : Array.isArray(val) ? [prev].concat(val)

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 602–607why, one step back
602 }
603604 return this.set(field, value);605};606607/**

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 608–618why, one step back
608 * Set header `field` to `val`, or pass
609 * an object of header fields.610 *611 * Examples:612 *613 * res.set('Foo', ['bar', 'baz']);614 * res.set('Accept', 'application/json');615 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });616 *617 * Aliased as `res.header()`.618 *

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

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 625why, one step back
625 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 626–630why, one step back
626 */
627628res.set =629res.header = function header(field, val) {630 if (arguments.length === 2) {

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 631–635why, one step back
631 var value = Array.isArray(val)
632 ? val.map(String)633 : String(val);634635 // add charset to content-type

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

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

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

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 641why, one step back
641 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 642–643why, one step back
642
643 this.setHeader(field, value);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 644–656why, one step back
644 } else {
645 for (var key in field) {646 this.set(key, field[key]);647 }648 }649 return this;650};651652/**653 * Get value for header `field`.654 *655 * @param {String} field656 * @return {String}

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 657why, one step back
657 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 658–667why, one step back
658 */
659660res.get = function(field){661 return this.getHeader(field);662};663664/**665 * Clear cookie `name`.666 *667 * @param {String} name

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 670why, one step back
670 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 671–672why, one step back
671 */
672

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 677–679why, one step back
677};
678679/**

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 681–692why, one step back
681 *
682 * Options:683 *684 * - `maxAge` max-age in milliseconds, converted to `expires`685 * - `signed` sign the cookie686 * - `path` defaults to "/"687 *688 * Examples:689 *690 * // "Remember Me" for 15 minutes691 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });692 *

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 700why, one step back
700 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 710–711why, one step back
710 }
711

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 712–717why, one step back
712 var val = typeof value === 'object'
713 ? 'j:' + JSON.stringify(value)714 : String(value);715716 if (signed) {717 val = 's:' + sign(val, secret);

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 718why, one step back
718 }

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 719–730why, one step back
719
720 if ('maxAge' in opts) {721 opts.expires = new Date(Date.now() + opts.maxAge);722 opts.maxAge /= 1000;723 }724725 if (opts.path == null) {726 opts.path = '/';727 }728729 this.append('Set-Cookie', cookie.serialize(name, String(val), opts));730

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 731–747why, one step back
731 return this;
732};733734/**735 * Set the location header to `url`.736 *737 * The given `url` can also be "back", which redirects738 * to the _Referrer_ or _Referer_ headers or "/".739 *740 * Examples:741 *742 * res.location('/foo/bar').;743 * res.location('http://example.com');744 * res.location('../login');745 *746 * @param {String} url747 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 748why, one step back
748 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 749–750why, one step back
749 */
750

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 751–752why, one step back
751res.location = function location(url) {
752 var loc = url;

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 753–754why, one step back
753
754 // "back" is an alias for the referrer

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 758why, one step back
758

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 761–777why, one step back
761};
762763/**764 * Redirect to the given `url` with optional response `status`765 * defaulting to 302.766 *767 * The resulting `url` is determined by `res.location()`, so768 * it will play nicely with mounted apps, relative paths,769 * `"back"` etc.770 *771 * Examples:772 *773 * res.redirect('/foo/bar');774 * res.redirect('http://example.com');775 * res.redirect(301, 'http://example.com');776 * res.redirect('../login'); // /blog/post/1 -> /blog/login777 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 778why, one step back
778 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 779–787why, one step back
779 */
780781res.redirect = function redirect(url) {782 var address = url;783 var body;784 var status = 302;785786 // allow status / url787 if (arguments.length === 2) {

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

lines 788–789why, one step back
788 status = arguments[0]
789 address = arguments[1]

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 790–792why, one step back
790 }
791792 // Set location header

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 794–797why, one step back
794
795 // Support text/{plain,html} by default796 this.format({797 text: function(){

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 799–801why, one step back
799 },
800801 html: function(){

Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f

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

Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 804–816why, one step back
804 },
805806 default: function(){807 body = '';808 }809 });810811 // Respond812 this.statusCode = status;813 this.set('Content-Length', Buffer.byteLength(body));814815 if (this.req.method === 'HEAD') {816 this.end();

Merge tag '4.11.2'Douglas Christopher Wilsonbefore adoptiona9ef9e1

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 819–827why, one step back
819 }
820};821822/**823 * Add `field` to Vary. If already present in the Vary set, then824 * this call is simply ignored.825 *826 * @param {Array|String} field827 * @return {ServerResponse} for chaining

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 828why, one step back
828 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 829–846why, one step back
829 */
830831res.vary = function(field){832 vary(this, field);833834 return this;835};836837/**838 * Render `view` with the given `options` and optional callback `fn`.839 * When a callback function is given a response will _not_ be made840 * automatically, otherwise a response of _200_ and _text/html_ is given.841 *842 * Options:843 *844 * - `cache` boolean hinting to the engine it should cache845 * - `filename` filename of the view being rendered846 *

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

line 847why, one step back
847 * @public

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 848–849why, one step back
848 */
849

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 850–853why, one step back
850res.render = function render(view, options, callback) {
851 var app = this.req.app;852 var done = callback;853 var opts = options || {};

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

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

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 856–857why, one step back
856
857 // support callback function as second arg

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

lines 858–860why, one step back
858 if (typeof options === 'function') {
859 done = options;860 opts = {};

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 861–863why, one step back
861 }
862863 // merge res.locals

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 865–866why, one step back
865
866 // default callback to respond

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

lines 868–872why, one step back
868 if (err) return req.next(err);
869 self.send(str);870 };871872 // render

Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87

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

tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116

line 874why, one step back
874};

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 875–878why, one step back
875
876// pipe the send file stream877function sendfile(res, file, options, callback) {878 var done = false;

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

line 879why, one step back
879 var streaming;

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 880why, one step back
880

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 881–886why, one step back
881 // request aborted
882 function onaborted() {883 if (done) return;884 done = true;885886 var err = new Error('Request aborted');

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 888–890why, one step back
888 callback(err);
889 }890

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 891–914why, one step back
891 // directory
892 function ondirectory() {893 if (done) return;894 done = true;895896 var err = new Error('EISDIR, read');897 err.code = 'EISDIR';898 callback(err);899 }900901 // errors902 function onerror(err) {903 if (done) return;904 done = true;905 callback(err);906 }907908 // ended909 function onend() {910 if (done) return;911 done = true;912 callback();913 }914

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 915–919why, one step back
915 // file
916 function onfile() {917 streaming = false;918 }919

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 920–921why, one step back
920 // finished
921 function onfinish(err) {

Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 923–926why, one step back
923 if (err) return onerror(err);
924 if (done) return;925926 setImmediate(function () {

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 927–931why, one step back
927 if (streaming !== false && !done) {
928 onaborted();929 return;930 }931

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 932–933why, one step back
932 if (done) return;
933 done = true;

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 935–937why, one step back
935 });
936 }937

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

lines 938–943why, one step back
938 // streaming
939 function onstream() {940 streaming = true;941 }942943 file.on('directory', ondirectory);

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

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

Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3

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

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

lines 948–964why, one step back
948 onFinished(res, onfinish);
949950 if (options.headers) {951 // set headers on successful transfer952 file.on('headers', function headers(res) {953 var obj = options.headers;954 var keys = Object.keys(obj);955956 for (var i = 0; i < keys.length; i++) {957 var k = keys[i];958 res.setHeader(k, obj[k]);959 }960 });961 }962963 // pipe964 file.pipe(res);

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 965–967why, one step back
965}
966967/**

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 968–975why, one step back
968 * Stringify JSON, like JSON.stringify, but v8 optimized, with the
969 * ability to escape characters that can trigger HTML sniffing.970 *971 * @param {*} value972 * @param {function} replaces973 * @param {number} spaces974 * @param {boolean} escape975 * @returns {string}

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

lines 976–978why, one step back
976 * @private
977 */978

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

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

Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

line 985why, one step back
985

Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 987–994why, one step back
987 json = json.replace(/[<>&]/g, function (c) {
988 switch (c.charCodeAt(0)) {989 case 0x3c:990 return '\\u003c'991 case 0x3e:992 return '\\u003e'993 case 0x26:994 return '\\u0026'

Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99

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

Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe

lines 996–1002why, one step back
996 default:
997 return c998 }999 })1000 }10011002 return json

Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a

line 1003why, one step back
1003}