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 fileTake it
The walk stopped after 200 commits: lines older than ab8d116 point at it.
Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 1–7why, one step back1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */7Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 8–9why, one step back8'use strict';9tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 10–11why, one step back10/**11 * Module dependencies.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 12why, one step back12 * @privatetests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 13–14why, one step back13 */14Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 15why, one step back15var Buffer = require('safe-buffer').BufferUse content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 16why, one step back16var contentDisposition = require('content-disposition');Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 17why, one step back17var encodeUrl = require('encodeurl');tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 18–19why, one step back18var escapeHtml = require('escape-html');19var http = require('http');Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 20why, one step back20var onFinished = require('on-finished');Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 21why, one step back21var mime = require('mime-types')tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 22why, one step back22var path = require('path');Use path-is-absolute module for absolute path detectionJeremiah Senkpielbefore adoption694869d
line 23why, one step back23var pathIsAbsolute = require('path-is-absolute');Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 24why, one step back24var statuses = require('statuses')Merge tag '3.18.1'Douglas Christopher Wilsonbefore adoptionccc45a7
line 25why, one step back25var merge = require('utils-merge');tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 26–32why, one step back26var 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 back33var resolve = path.resolve;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 34–37why, one step back34var vary = require('vary');3536/**37 * Response prototype.Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 38why, one step back38 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 39–40why, one step back39 */40Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 41why, one step back41var res = Object.create(http.ServerResponse.prototype)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 42–43why, one step back4243/**Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 44–50why, one step back44 * Module exports.45 * @public46 */4748module.exports = res4950/**tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 51–54why, one step back51 * Set status `code`.52 *53 * @param {Number} code54 * @return {ServerResponse}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 55why, one step back55 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 56–57why, one step back56 */57Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 58why, one step back58res.status = function status(code) {tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 59–74why, one step back59 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 back75 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 76–90why, one step back76 */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 back91 * res.send(Buffer.from('wahoo'));tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 92–95why, one step back92 * res.send({ some: 'json' });93 * res.send('<p>some html</p>');94 *95 * @param {string|number|boolean|object|Buffer} bodyMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 96why, one step back96 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 97–140why, one step back97 */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 }140Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 141–144why, one step back141 // determine if ETag should be generated142 var etagFn = app.get('etag fn')143 var generateETag = !this.get('ETag') && typeof etagFn === 'function'144tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 145why, one step back145 // populate Content-LengthMerge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 146why, one step back146 var lentests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 147why, one step back147 if (chunk !== undefined) {Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 148–156why, one step back148 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 back157 encoding = undefined;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 158why, one step back158 len = chunk.lengthtests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 159–163why, one step back159 }160161 this.set('Content-Length', len);162 }163Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 164–165why, one step back164 // populate ETag165 var etag;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 166–167why, one step back166 if (generateETag && len !== undefined) {167 if ((etag = etagFn(chunk, encoding))) {Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 168why, one step back168 this.set('ETag', etag);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 169–175why, one step back169 }170 }171172 // freshness173 if (req.fresh) this.statusCode = 304;174175 // strip irrelevant headersMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 176why, one step back176 if (204 === this.statusCode || 304 === this.statusCode) {tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 177–182why, one step back177 this.removeHeader('Content-Type');178 this.removeHeader('Content-Length');179 this.removeHeader('Transfer-Encoding');180 chunk = '';181 }182Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 183why, one step back183 if (req.method === 'HEAD') {Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 184why, one step back184 // skip body for HEADtests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 185why, one step back185 this.end();Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 186–188why, one step back186 } else {187 // respond188 this.end(chunk, encoding);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 189–202why, one step back189 }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} objMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 203why, one step back203 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 204–208why, one step back204 */205206res.json = function json(obj) {207 // settings208 var app = this.app;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 209why, one step back209 var escape = app.get('json escape')tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 210–211why, one step back210 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 back212 var body = stringify(obj, replacer, spaces, escape)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 213–230why, one step back213214 // 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} objMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 231why, one step back231 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 232–236why, one step back232 */233234res.jsonp = function jsonp(obj) {235 // settings236 var app = this.app;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 237why, one step back237 var escape = app.get('json escape')tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 238–239why, one step back238 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 back240 var body = stringify(obj, replacer, spaces, escape)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 241–261why, one step back241 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, '');261Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
lines 262–270why, one step back262 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 back271272 // 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 back278};279280/**Add res.sendStatusSeth Samuelbefore adoption12f92a5
lines 281–291why, one step back281 * 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} statusCodeMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 292why, one step back292 * @publicAdd res.sendStatusSeth Samuelbefore adoption12f92a5
lines 293–295why, one step back293 */294295res.sendStatus = function sendStatus(statusCode) {Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 296why, one step back296 var body = statuses[statusCode] || String(statusCode)Add res.sendStatusSeth Samuelbefore adoption12f92a5
lines 297–304why, one step back297298 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 back305 * 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 back308 * The callback `callback(err)` is invoked when the transfer is completeMerge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 309why, one step back309 * or when an error occurs. Be sure to check `res.headersSent`Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 310–341why, one step back310 * if you wish to attempt responding, as the header and some data311 * 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 back342 * @publicAdd res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 343–344why, one step back343 */344Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 345–346why, one step back345res.sendFile = function sendFile(path, options, callback) {346 var done = callback;Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 347why, one step back347 var req = this.req;Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 348why, one step back348 var res = this;Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 349why, one step back349 var next = req.next;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 350why, one step back350 var opts = options || {};Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 351–355why, one step back351352 if (!path) {353 throw new TypeError('path argument is required to res.sendFile');354 }355Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 356–359why, one step back356 if (typeof path !== 'string') {357 throw new TypeError('path must be a string to res.sendFile')358 }359Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 360–361why, one step back360 // support function as second arg361 if (typeof options === 'function') {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 362–363why, one step back362 done = options;363 opts = {};Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 364–365why, one step back364 }365Use path-is-absolute module for absolute path detectionJeremiah Senkpielbefore adoption694869d
line 366why, one step back366 if (!opts.root && !pathIsAbsolute(path)) {Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
lines 367–369why, one step back367 throw new TypeError('path must be absolute or specify root to res.sendFile');368 }369Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 370why, one step back370 // create file streamAdd res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 371why, one step back371 var pathname = encodeURI(path);Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 372why, one step back372 var file = send(req, pathname, opts);Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 373why, one step back373Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 374why, one step back374 // transferMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 375–376why, one step back375 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 back377 if (err && err.code === 'EISDIR') return next();Add res.sendFileDouglas Christopher Wilsonbefore adoption2cb029f
line 378why, one step back378Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 379why, one step back379 // next() all but write errorsMerge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 380why, one step back380 if (err && err.code !== 'ECONNABORTED' && err.syscall !== 'write') {Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 381–383why, one step back381 next(err);382 }383 });tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 384–389why, one step back384};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 back390 * and optional callback `callback(err)`. The callback is invokedtests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 391–393why, one step back391 * when the data transfer is complete, or when an error has392 * 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 back394 * 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 back399 * This method uses `res.sendFile()`.tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 400why, one step back400 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 401why, one step back401 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 402–403why, one step back402 */403Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 404why, one step back404res.download = function download (path, filename, options, callback) {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 405–406why, one step back405 var done = callback;406 var name = filename;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 407why, one step back407 var opts = options || nullMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 408why, one step back408Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 409why, one step back409 // support function as second or third argFix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7
line 410why, one step back410 if (typeof filename === 'function') {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 411–412why, one step back411 done = filename;412 name = null;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 413–416why, one step back413 opts = null414 } else if (typeof options === 'function') {415 done = options416 opts = nulltests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 417–420why, one step back417 }418419 // set Content-Disposition when file is sent420 var headers = {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 421why, one step back421 'Content-Disposition': contentDisposition(name || path)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 422–423why, one step back422 };423Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 424–438why, one step back424 // merge user-provided headers425 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 = headers438Fix incorrect deprecation warnings on res.downloadDouglas Christopher Wilsonbefore adoption1150ca7
lines 439–441why, one step back439 // Resolve the full path for sendFile440 var fullPath = resolve(path);441Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 442–443why, one step back442 // send file443 return this.sendFile(fullPath, opts, done)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 444–446why, one step back444};445446/**Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 447why, one step back447 * Set _Content-Type_ response header with `type` through `mime.contentType()`tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 448why, one step back448 * 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 back449 * When no mapping is found though `mime.contentType()`, the type is set to450 * "application/octet-stream".tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 451–461why, one step back451 *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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 462why, one step back462 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 463–465why, one step back463 */464465res.contentType =Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 466–467why, one step back466res.type = function contentType(type) {467 var ct = type.indexOf('/') === -1Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 468why, one step back468 ? (mime.contentType(type) || 'application/octet-stream')Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 469–471why, one step back469 : type;470471 return this.set('Content-Type', ct);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 472–497why, one step back472};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 back498 * 'application/json': function () {tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 499–527why, one step back499 * 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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 528why, one step back528 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 529–538why, one step back529 */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);538Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 539–541why, one step back539 var key = keys.length > 0540 ? req.accepts(keys)541 : false;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 542–551why, one step back542543 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 back552 err.status = err.statusCode = 406;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 553–564why, one step back553 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 back565 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 566–567why, one step back566 */567Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
lines 568–572why, one step back568res.attachment = function attachment(filename) {569 if (filename) {570 this.type(extname(filename));571 }572tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 573why, one step back573 this.set('Content-Disposition', contentDisposition(filename));Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 574why, one step back574tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 575–578why, one step back575 return this;576};577578/**Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 579–589why, one step back579 * 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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 590why, one step back590 * @publicMerge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 591–600why, one step back591 */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 back601 : [prev, val]Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 602–607why, one step back602 }603604 return this.set(field, value);605};606607/**tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 608–618why, one step back608 * Set header `field` to `val`, or pass609 * 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 back619 * When the set header is "Content-Type", the type is expanded to include620 * the charset if not present using `mime.contentType()`.621 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 622–623why, one step back622 * @param {String|Object} field623 * @param {String|Array} valtests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 624why, one step back624 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 625why, one step back625 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 626–630why, one step back626 */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 back631 var value = Array.isArray(val)632 ? val.map(String)633 : String(val);634635 // add charset to content-typeMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
lines 636–639why, one step back636 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 back640 value = mime.contentType(value)tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 641why, one step back641 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 642–643why, one step back642643 this.setHeader(field, value);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 644–656why, one step back644 } 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 back657 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 658–667why, one step back658 */659660res.get = function(field){661 return this.getHeader(field);662};663664/**665 * Clear cookie `name`.666 *667 * @param {String} nameMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 668why, one step back668 * @param {Object} [options]tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 669why, one step back669 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 670why, one step back670 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 671–672why, one step back671 */672Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 673–676why, one step back673res.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 back677};678679/**Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 680why, one step back680 * Set cookie `name` to `value`, with the given `options`.tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 681–692why, one step back681 *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 back693 * // same as abovetests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 694–696why, one step back694 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })695 *696 * @param {String} nameMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 697why, one step back697 * @param {String|Object} valueMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 698why, one step back698 * @param {Object} [options]tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 699why, one step back699 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 700why, one step back700 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 701–702why, one step back701 */702Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 703–704why, one step back703res.cookie = function (name, value, options) {704 var opts = merge({}, options);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 705why, one step back705 var secret = this.req.secret;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 706–709why, one step back706 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 back710 }711Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 712–717why, one step back712 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 back718 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 719–730why, one step back719720 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));730tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 731–747why, one step back731 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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 748why, one step back748 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 749–750why, one step back749 */750Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 751–752why, one step back751res.location = function location(url) {752 var loc = url;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 753–754why, one step back753754 // "back" is an alias for the referrerMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 755–757why, one step back755 if (url === 'back') {756 loc = this.req.get('Referrer') || '/';757 }tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 758why, one step back758Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 759why, one step back759 // set locationMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 760why, one step back760 return this.set('Location', encodeUrl(loc));tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 761–777why, one step back761};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 back778 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 779–787why, one step back779 */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 back788 status = arguments[0]789 address = arguments[1]tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 790–792why, one step back790 }791792 // Set location headerMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 793why, one step back793 address = this.location(address).get('Location');tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 794–797why, one step back794795 // Support text/{plain,html} by default796 this.format({797 text: function(){Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 798why, one step back798 body = statuses[status] + '. Redirecting to ' + addresstests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 799–801why, one step back799 },800801 html: function(){Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f
line 802why, one step back802 var u = escapeHtml(address);Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 803why, one step back803 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 back804 },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 back817 } else {818 this.end(body);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 819–827why, one step back819 }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 chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 828why, one step back828 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 829–846why, one step back829 */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 back847 * @publictests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 848–849why, one step back848 */849Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 850–853why, one step back850res.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 back854 var req = this.req;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 855why, one step back855 var self = this;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 856–857why, one step back856857 // support callback function as second argMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 858–860why, one step back858 if (typeof options === 'function') {859 done = options;860 opts = {};tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 861–863why, one step back861 }862863 // merge res.localsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 864why, one step back864 opts._locals = self.locals;tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 865–866why, one step back865866 // default callback to respondMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 867why, one step back867 done = done || function (err, str) {tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
lines 868–872why, one step back868 if (err) return req.next(err);869 self.send(str);870 };871872 // renderMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 873why, one step back873 app.render(view, opts, done);tests: bail on failed test for developerDouglas Christopher Wilsonbefore adoptionab8d116
line 874why, one step back874};Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 875–878why, one step back875876// 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 back879 var streaming;Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 880why, one step back880Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 881–886why, one step back881 // request aborted882 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 back887 err.code = 'ECONNABORTED';Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 888–890why, one step back888 callback(err);889 }890Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 891–914why, one step back891 // directory892 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 }914Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 915–919why, one step back915 // file916 function onfile() {917 streaming = false;918 }919Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 920–921why, one step back920 // finished921 function onfinish(err) {Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 922why, one step back922 if (err && err.code === 'ECONNRESET') return onaborted();Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 923–926why, one step back923 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 back927 if (streaming !== false && !done) {928 onaborted();929 return;930 }931Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 932–933why, one step back932 if (done) return;933 done = true;Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 934why, one step back934 callback();Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 935–937why, one step back935 });936 }937Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 938–943why, one step back938 // streaming939 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 back944 file.on('end', onend);945 file.on('error', onerror);Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 946–947why, one step back946 file.on('file', onfile);947 file.on('stream', onstream);Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 948–964why, one step back948 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 back965}966967/**Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 968–975why, one step back968 * Stringify JSON, like JSON.stringify, but v8 optimized, with the969 * 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 back976 * @private977 */978Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 979why, one step back979function stringify (value, replacer, spaces, escape) {Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 980–981why, one step back980 // v8 checks arguments.length for optimizing simple call981 // https://bugs.chromium.org/p/v8/issues/detail?id=4730Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 982why, one step back982 var json = replacer || spacesMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 983–984why, one step back983 ? JSON.stringify(value, replacer, spaces)984 : JSON.stringify(value);Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 985why, one step back985Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 986why, one step back986 if (escape && typeof json === 'string') {Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 987–994why, one step back987 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 back995 /* istanbul ignore next: unreachable default */Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 996–1002why, one step back996 default:997 return c998 }999 })1000 }10011002 return jsonInvoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 1003why, one step back1003}