response.js
This file
Path
lib/response.js
1,025 lines · 23.6 KiB · JavaScript
Last changed by
feat: Added check to support Uint8Array in response sending (#6285)Alexander Ceruttibefore adoption55869f4
History of this fileTake it
The walk stopped after 200 commits: lines older than 20635d0 point at it.
Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 1–7why, one step back1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */7Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 8–9why, one step back8'use strict';9Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 10–11why, one step back10/**11 * Module dependencies.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 12why, one step back12 * @privateMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 13–14why, one step back13 */14Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 15why, one step back15var contentDisposition = require('content-disposition');Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 16why, one step back16var createError = require('http-errors')Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 17why, one step back17var encodeUrl = require('encodeurl');Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 18why, one step back18var escapeHtml = require('escape-html');fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
line 19why, one step back19var http = require('node:http');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')fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
lines 22–23why, one step back22var path = require('node:path');23var pathIsAbsolute = require('node:path').isAbsolute;Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 24why, one step back24var statuses = require('statuses')Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 25–36why, one step back25var sign = require('cookie-signature').sign;26var normalizeType = require('./utils').normalizeType;27var normalizeTypes = require('./utils').normalizeTypes;28var setCharset = require('./utils').setCharset;29var cookie = require('cookie');30var send = require('send');31var extname = path.extname;32var resolve = path.resolve;33var vary = require('vary');3435/**36 * Response prototype.Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 37why, one step back37 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 38–39why, one step back38 */39Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
line 40why, one step back40var res = Object.create(http.ServerResponse.prototype)Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 41–42why, one step back4142/**Merge tag '4.15.0'Douglas Christopher Wilsonbefore adoption501e24e
lines 43–49why, one step back43 * Module exports.44 * @public45 */4647module.exports = res4849/**Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
line 50why, one step back50 * Set the HTTP status code for the response.Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 51why, one step back51 *Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
lines 52–58why, one step back52 * Expects an integer value between 100 and 999 inclusive.53 * Throws an error if the provided status code is not an integer or if it's outside the allowable range.54 *55 * @param {number} code - The HTTP status code to set.56 * @return {ServerResponse} - Returns itself for chaining methods.57 * @throws {TypeError} If `code` is not an integer.58 * @throws {RangeError} If `code` is outside the range 100 to 999.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 59why, one step back59 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 60–61why, one step back60 */61Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 62why, one step back62res.status = function status(code) {Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
lines 63–65why, one step back63 // Check if the status code is not an integer64 if (!Number.isInteger(code)) {65 throw new TypeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be an integer.`);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 66why, one step back66 }Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
lines 67–71why, one step back67 // Check if the status code is outside of Node's valid range68 if (code < 100 || code > 999) {69 throw new RangeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be greater than 99 and less than 1000.`);70 }71Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 72–87why, one step back72 this.statusCode = code;73 return this;74};7576/**77 * Set Link header field with the given `links`.78 *79 * Examples:80 *81 * res.links({82 * next: 'http://api.example.com/users?page=2',83 * last: 'http://api.example.com/users?page=5'84 * });85 *86 * @param {Object} links87 * @return {ServerResponse}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 88why, one step back88 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 89–103why, one step back89 */9091res.links = function(links){92 var link = this.get('Link') || '';93 if (link) link += ', ';94 return this.set('Link', link + Object.keys(links).map(function(rel){95 return '<' + links[rel] + '>; rel="' + rel + '"';96 }).join(', '));97};9899/**100 * Send a response.101 *102 * Examples:103 *Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 104why, one step back104 * res.send(Buffer.from('wahoo'));Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 105–108why, one step back105 * res.send({ some: 'json' });106 * res.send('<p>some html</p>');107 *108 * @param {string|number|boolean|object|Buffer} bodyMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 109why, one step back109 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 110–132why, one step back110 */111112res.send = function send(body) {113 var chunk = body;114 var encoding;115 var req = this.req;116 var type;117118 // settings119 var app = this.app;120121 switch (typeof chunk) {122 // string defaulting to html123 case 'string':124 if (!this.get('Content-Type')) {125 this.type('html');126 }127 break;128 case 'boolean':129 case 'number':130 case 'object':131 if (chunk === null) {132 chunk = '';feat: Added check to support Uint8Array in response sending (#6285)Alexander Ceruttibefore adoption55869f4
line 133why, one step back133 } else if (ArrayBuffer.isView(chunk)) {Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 134–153why, one step back134 if (!this.get('Content-Type')) {135 this.type('bin');136 }137 } else {138 return this.json(chunk);139 }140 break;141 }142143 // write strings in utf-8144 if (typeof chunk === 'string') {145 encoding = 'utf8';146 type = this.get('Content-Type');147148 // reflect this in content-type149 if (typeof type === 'string') {150 this.set('Content-Type', setCharset(type, 'utf-8'));151 }152 }153Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 154–157why, one step back154 // determine if ETag should be generated155 var etagFn = app.get('etag fn')156 var generateETag = !this.get('ETag') && typeof etagFn === 'function'157Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 158why, one step back158 // populate Content-LengthMerge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 159why, one step back159 var lenMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 160why, one step back160 if (chunk !== undefined) {Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 161–169why, one step back161 if (Buffer.isBuffer(chunk)) {162 // get length of Buffer163 len = chunk.length164 } else if (!generateETag && chunk.length < 1000) {165 // just calculate length when no ETag + small chunk166 len = Buffer.byteLength(chunk, encoding)167 } else {168 // convert chunk to Buffer and calculate169 chunk = Buffer.from(chunk, encoding)Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 170why, one step back170 encoding = undefined;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 171why, one step back171 len = chunk.lengthMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 172–176why, one step back172 }173174 this.set('Content-Length', len);175 }176Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
lines 177–178why, one step back177 // populate ETag178 var etag;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 179–180why, one step back179 if (generateETag && len !== undefined) {180 if ((etag = etagFn(chunk, encoding))) {Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 181why, one step back181 this.set('ETag', etag);Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 182–185why, one step back182 }183 }184185 // freshnessMerge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
line 186why, one step back186 if (req.fresh) this.status(304);Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 187–188why, one step back187188 // strip irrelevant headersMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 189why, one step back189 if (204 === this.statusCode || 304 === this.statusCode) {Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 190–195why, one step back190 this.removeHeader('Content-Type');191 this.removeHeader('Content-Length');192 this.removeHeader('Transfer-Encoding');193 chunk = '';194 }195Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 196–202why, one step back196 // alter headers for 205197 if (this.statusCode === 205) {198 this.set('Content-Length', '0')199 this.removeHeader('Transfer-Encoding')200 chunk = ''201 }202Merge tag '4.12.0'Douglas Christopher Wilsonbefore adoptionf6ec710
line 203why, one step back203 if (req.method === 'HEAD') {Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 204why, one step back204 // skip body for HEADMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 205why, one step back205 this.end();Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 206–208why, one step back206 } else {207 // respond208 this.end(chunk, encoding);Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 209–222why, one step back209 }210211 return this;212};213214/**215 * Send JSON response.216 *217 * Examples:218 *219 * res.json(null);220 * res.json({ user: 'tj' });221 *222 * @param {string|number|boolean|object} objMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 223why, one step back223 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 224–228why, one step back224 */225226res.json = function json(obj) {227 // settings228 var app = this.app;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 229why, one step back229 var escape = app.get('json escape')Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 230–231why, one step back230 var replacer = app.get('json replacer');231 var spaces = app.get('json spaces');Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 232why, one step back232 var body = stringify(obj, replacer, spaces, escape)Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 233–250why, one step back233234 // content-type235 if (!this.get('Content-Type')) {236 this.set('Content-Type', 'application/json');237 }238239 return this.send(body);240};241242/**243 * Send JSON response with JSONP callback support.244 *245 * Examples:246 *247 * res.jsonp(null);248 * res.jsonp({ user: 'tj' });249 *250 * @param {string|number|boolean|object} objMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 251why, one step back251 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 252–256why, one step back252 */253254res.jsonp = function jsonp(obj) {255 // settings256 var app = this.app;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 257why, one step back257 var escape = app.get('json escape')Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 258–259why, one step back258 var replacer = app.get('json replacer');259 var spaces = app.get('json spaces');Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 260why, one step back260 var body = stringify(obj, replacer, spaces, escape)Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 261–281why, one step back261 var callback = this.req.query[app.get('jsonp callback name')];262263 // content-type264 if (!this.get('Content-Type')) {265 this.set('X-Content-Type-Options', 'nosniff');266 this.set('Content-Type', 'application/json');267 }268269 // fixup callback270 if (Array.isArray(callback)) {271 callback = callback[0];272 }273274 // jsonp275 if (typeof callback === 'string' && callback.length !== 0) {276 this.set('X-Content-Type-Options', 'nosniff');277 this.set('Content-Type', 'text/javascript');278279 // restrict callback charset280 callback = callback.replace(/[^\[\]\w$.]/g, '');281Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
lines 282–290why, one step back282 if (body === undefined) {283 // empty argument284 body = ''285 } else if (typeof body === 'string') {286 // replace chars not allowed in JavaScript that are in JSON287 body = body288 .replace(/\u2028/g, '\\u2028')289 .replace(/\u2029/g, '\\u2029')290 }Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 291–300why, one step back291292 // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"293 // the typeof check is just to reduce client error noise294 body = '/**/ typeof ' + callback + ' === \'function\' && ' + callback + '(' + body + ');';295 }296297 return this.send(body);298};299300/**Add res.sendStatusSeth Samuelbefore adoption12f92a5
lines 301–311why, one step back301 * Send given HTTP status code.302 *303 * Sets the response status to `statusCode` and the body of the304 * response to the standard description from node's http.STATUS_CODES305 * or the statusCode number if no description.306 *307 * Examples:308 *309 * res.sendStatus(200);310 *311 * @param {number} statusCodeMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 312why, one step back312 * @publicAdd res.sendStatusSeth Samuelbefore adoption12f92a5
lines 313–315why, one step back313 */314315res.sendStatus = function sendStatus(statusCode) {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 316why, one step back316 var body = statuses.message[statusCode] || String(statusCode)Add res.sendStatusSeth Samuelbefore adoption12f92a5
line 317why, one step back317Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
line 318why, one step back318 this.status(statusCode);Add res.sendStatusSeth Samuelbefore adoption12f92a5
lines 319–324why, one step back319 this.type('txt');320321 return this.send(body);322};323324/**Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 325–327why, one step back325 * Transfer the file at the given `path`.326 *327 * Automatically sets the _Content-Type_ response header field.Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 328why, one step back328 * The callback `callback(err)` is invoked when the transfer is completeMerge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 329why, one step back329 * or when an error occurs. Be sure to check `res.headersSent`Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 330–361why, one step back330 * if you wish to attempt responding, as the header and some data331 * may have already been transferred.332 *333 * Options:334 *335 * - `maxAge` defaulting to 0 (can be string converted by `ms`)336 * - `root` root directory for relative filenames337 * - `headers` object of headers to serve with file338 * - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them339 *340 * Other options are passed along to `send`.341 *342 * Examples:343 *344 * The following example illustrates how `res.sendFile()` may345 * be used as an alternative for the `static()` middleware for346 * dynamic situations. The code backing `res.sendFile()` is actually347 * the same code, so HTTP cache support etc is identical.348 *349 * app.get('/user/:uid/photos/:file', function(req, res){350 * var uid = req.params.uid351 * , file = req.params.file;352 *353 * req.user.mayViewFilesFrom(uid, function(yes){354 * if (yes) {355 * res.sendFile('/uploads/' + uid + '/' + file);356 * } else {357 * res.send(403, 'Sorry! you cant see that.');358 * }359 * });360 * });361 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 362why, one step back362 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 363–364why, one step back363 */364Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 365–366why, one step back365res.sendFile = function sendFile(path, options, callback) {366 var done = callback;Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 367why, one step back367 var req = this.req;Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 368why, one step back368 var res = this;Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 369why, one step back369 var next = req.next;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 370why, one step back370 var opts = options || {};Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 371–375why, one step back371372 if (!path) {373 throw new TypeError('path argument is required to res.sendFile');374 }375Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
lines 376–379why, one step back376 if (typeof path !== 'string') {377 throw new TypeError('path must be a string to res.sendFile')378 }379Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 380–381why, one step back380 // support function as second arg381 if (typeof options === 'function') {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 382–383why, one step back382 done = options;383 opts = {};Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 384–385why, one step back384 }385Use path-is-absolute module for absolute path detectionJeremiah Senkpielbefore adoption694869d
line 386why, one step back386 if (!opts.root && !pathIsAbsolute(path)) {Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 387–389why, one step back387 throw new TypeError('path must be absolute or specify root to res.sendFile');388 }389Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 390why, one step back390 // create file streamMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 391why, one step back391 var pathname = encodeURI(path);Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 392why, one step back392 var file = send(req, pathname, opts);Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 393why, one step back393Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 394why, one step back394 // transferMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 395–396why, one step back395 sendfile(res, file, opts, function (err) {396 if (done) return done(err);Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 397why, one step back397 if (err && err.code === 'EISDIR') return next();Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 398why, one step back398Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 399why, one step back399 // next() all but write errorsMerge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 400why, one step back400 if (err && err.code !== 'ECONNABORTED' && err.syscall !== 'write') {Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 401–403why, one step back401 next(err);402 }403 });Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 404–409why, one step back404};405406/**407 * Transfer the file at the given `path` as an attachment.408 *409 * Optionally providing an alternate attachment `filename`,Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 410why, one step back410 * and optional callback `callback(err)`. The callback is invokedMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 411why, one step back411 * when the data transfer is complete, or when an error hasMerge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
line 412why, one step back412 * occurred. Be sure to check `res.headersSent` if you plan to respond.Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 413why, one step back413 *Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 414–418why, one step back414 * Optionally providing an `options` object to use with `res.sendFile()`.415 * This function will set the `Content-Disposition` header, overriding416 * any `Content-Disposition` header passed as header options in order417 * to set the attachment and filename.418 *Remove res.sendfileDouglas Christopher Wilsonbefore adoption0fc4f07
line 419why, one step back419 * This method uses `res.sendFile()`.Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 420why, one step back420 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 421why, one step back421 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 422–423why, one step back422 */423Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 424why, one step back424res.download = function download (path, filename, options, callback) {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 425–426why, one step back425 var done = callback;426 var name = filename;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 427why, one step back427 var opts = options || nullMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 428why, one step back428Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 429why, one step back429 // support function as second or third argMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 430why, one step back430 if (typeof filename === 'function') {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 431–432why, one step back431 done = filename;432 name = null;Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 433–436why, one step back433 opts = null434 } else if (typeof options === 'function') {435 done = options436 opts = nullMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 437–438why, one step back437 }438Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 439–445why, one step back439 // support optional filename, where options may be in it's place440 if (typeof filename === 'object' &&441 (typeof options === 'function' || options === undefined)) {442 name = null443 opts = filename444 }445Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 446–447why, one step back446 // set Content-Disposition when file is sent447 var headers = {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 448why, one step back448 'Content-Disposition': contentDisposition(name || path)Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 449–450why, one step back449 };450Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 451–465why, one step back451 // merge user-provided headers452 if (opts && opts.headers) {453 var keys = Object.keys(opts.headers)454 for (var i = 0; i < keys.length; i++) {455 var key = keys[i]456 if (key.toLowerCase() !== 'content-disposition') {457 headers[key] = opts.headers[key]458 }459 }460 }461462 // merge user-provided options463 opts = Object.create(opts)464 opts.headers = headers465Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 466why, one step back466 // Resolve the full path for sendFileMerge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 467–469why, one step back467 var fullPath = !opts.root468 ? resolve(path)469 : pathMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 470why, one step back470Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 471–472why, one step back471 // send file472 return this.sendFile(fullPath, opts, done)Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 473–475why, one step back473};474475/**Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 476why, one step back476 * Set _Content-Type_ response header with `type` through `mime.contentType()`Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 477why, one step back477 * when it does not contain "/", or set the Content-Type to `type` otherwise.Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
lines 478–479why, one step back478 * When no mapping is found though `mime.contentType()`, the type is set to479 * "application/octet-stream".Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 480–490why, one step back480 *481 * Examples:482 *483 * res.type('.html');484 * res.type('html');485 * res.type('json');486 * res.type('application/json');487 * res.type('png');488 *489 * @param {String} type490 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 491why, one step back491 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 492–494why, one step back492 */493494res.contentType =Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 495–496why, one step back495res.type = function contentType(type) {496 var ct = type.indexOf('/') === -1Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 497why, one step back497 ? (mime.contentType(type) || 'application/octet-stream')Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 498–500why, one step back498 : type;499500 return this.set('Content-Type', ct);Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 501–526why, one step back501};502503/**504 * Respond to the Acceptable formats using an `obj`505 * of mime-type callbacks.506 *507 * This method uses `req.accepted`, an array of508 * acceptable types ordered by their quality values.509 * When "Accept" is not present the _first_ callback510 * is invoked, otherwise the first match is used. When511 * no match is performed the server responds with512 * 406 "Not Acceptable".513 *514 * Content-Type is set for you, however if you choose515 * you may alter this within the callback using `res.type()`516 * or `res.set('Content-Type', ...)`.517 *518 * res.format({519 * 'text/plain': function(){520 * res.send('hey');521 * },522 *523 * 'text/html': function(){524 * res.send('<p>hey</p>');525 * },526 *Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 527why, one step back527 * 'application/json': function () {Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 528–556why, one step back528 * res.send({ message: 'hey' });529 * }530 * });531 *532 * In addition to canonicalized MIME types you may533 * also use extnames mapped to these types:534 *535 * res.format({536 * text: function(){537 * res.send('hey');538 * },539 *540 * html: function(){541 * res.send('<p>hey</p>');542 * },543 *544 * json: function(){545 * res.send({ message: 'hey' });546 * }547 * });548 *549 * By default Express passes an `Error`550 * with a `.status` of 406 to `next(err)`551 * if a match is not made. If you provide552 * a `.default` callback it will be invoked553 * instead.554 *555 * @param {Object} obj556 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 557why, one step back557 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 558–563why, one step back558 */559560res.format = function(obj){561 var req = this.req;562 var next = req.next;563Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 564–565why, one step back564 var keys = Object.keys(obj)565 .filter(function (v) { return v !== 'default' })Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 566why, one step back566Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 567–569why, one step back567 var key = keys.length > 0568 ? req.accepts(keys)569 : false;Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 570–575why, one step back570571 this.vary("Accept");572573 if (key) {574 this.set('Content-Type', normalizeType(key).value);575 obj[key](req, this, next);Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 576–577why, one step back576 } else if (obj.default) {577 obj.default(req, this, next)Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 578why, one step back578 } else {Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 579–581why, one step back579 next(createError(406, {580 types: normalizeTypes(keys).map(function (o) { return o.value })581 }))Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 582–591why, one step back582 }583584 return this;585};586587/**588 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.589 *590 * @param {String} filename591 * @return {ServerResponse}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 592why, one step back592 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 593–594why, one step back593 */594Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
lines 595–599why, one step back595res.attachment = function attachment(filename) {596 if (filename) {597 this.type(extname(filename));598 }599Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 600why, one step back600 this.set('Content-Disposition', contentDisposition(filename));Use content-disposition moduleDouglas Christopher Wilsonbefore adoption6f31218
line 601why, one step back601Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 602–605why, one step back602 return this;603};604605/**Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 606–616why, one step back606 * Append additional header `field` with value `val`.607 *608 * Example:609 *610 * res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);611 * res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');612 * res.append('Warning', '199 Miscellaneous warning');613 *614 * @param {String} field615 * @param {String|Array} val616 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 617why, one step back617 * @publicMerge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 618–627why, one step back618 */619620res.append = function append(field, val) {621 var prev = this.get(field);622 var value = val;623624 if (prev) {625 // concat the new and prev vals626 value = Array.isArray(prev) ? prev.concat(val)627 : Array.isArray(val) ? [prev].concat(val)Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 628why, one step back628 : [prev, val]Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 629–634why, one step back629 }630631 return this.set(field, value);632};633634/**Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 635–645why, one step back635 * Set header `field` to `val`, or pass636 * an object of header fields.637 *638 * Examples:639 *640 * res.set('Foo', ['bar', 'baz']);641 * res.set('Accept', 'application/json');642 * res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });643 *644 * Aliased as `res.header()`.645 *Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
lines 646–648why, one step back646 * When the set header is "Content-Type", the type is expanded to include647 * the charset if not present using `mime.contentType()`.648 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 649–650why, one step back649 * @param {String|Object} field650 * @param {String|Array} valMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 651why, one step back651 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 652why, one step back652 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 653–657why, one step back653 */654655res.set =656res.header = function header(field, val) {657 if (arguments.length === 2) {Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 658–662why, one step back658 var value = Array.isArray(val)659 ? val.map(String)660 : String(val);661662 // add charset to content-typeMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
lines 663–666why, one step back663 if (field.toLowerCase() === 'content-type') {664 if (Array.isArray(value)) {665 throw new TypeError('Content-Type cannot be set to an Array');666 }Use mime-types for file to content type mappingDouglas Christopher Wilsonbefore adoptiona0276c6
line 667why, one step back667 value = mime.contentType(value)Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 668why, one step back668 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 669–670why, one step back669670 this.setHeader(field, value);Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 671–683why, one step back671 } else {672 for (var key in field) {673 this.set(key, field[key]);674 }675 }676 return this;677};678679/**680 * Get value for header `field`.681 *682 * @param {String} field683 * @return {String}Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 684why, one step back684 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 685–694why, one step back685 */686687res.get = function(field){688 return this.getHeader(field);689};690691/**692 * Clear cookie `name`.693 *694 * @param {String} nameMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 695why, one step back695 * @param {Object} [options]Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 696why, one step back696 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 697why, one step back697 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 698–699why, one step back698 */699Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 700why, one step back700res.clearCookie = function clearCookie(name, options) {Merge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
lines 701–704why, one step back701 // Force cookie expiration by setting expires to the past702 const opts = { path: '/', ...options, expires: new Date(1)};703 // ensure maxAge is not passed704 delete opts.maxAgeMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 705–706why, one step back705706 return this.cookie(name, '', opts);Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 707–709why, one step back707};708709/**Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 710why, one step back710 * Set cookie `name` to `value`, with the given `options`.Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 711–722why, one step back711 *712 * Options:713 *714 * - `maxAge` max-age in milliseconds, converted to `expires`715 * - `signed` sign the cookie716 * - `path` defaults to "/"717 *718 * Examples:719 *720 * // "Remember Me" for 15 minutes721 * res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });722 *Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
line 723why, one step back723 * // same as aboveMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 724–726why, one step back724 * res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })725 *726 * @param {String} nameMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 727why, one step back727 * @param {String|Object} valueMerge tag '4.15.4'Douglas Christopher Wilsonbefore adoptionc319fe2
line 728why, one step back728 * @param {Object} [options]Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 729why, one step back729 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 730why, one step back730 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 731–732why, one step back731 */732Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 733why, one step back733res.cookie = function (name, value, options) {fix: Remove `utils-merge` dependency - use spread syntax instead (#6091)Phillip Bartabefore adoption246f6f5
line 734why, one step back734 var opts = { ...options };Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 735why, one step back735 var secret = this.req.secret;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 736–739why, one step back736 var signed = opts.signed;737738 if (signed && !secret) {739 throw new Error('cookieParser("secret") required for signed cookies');Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 740–741why, one step back740 }741Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 742–747why, one step back742 var val = typeof value === 'object'743 ? 'j:' + JSON.stringify(value)744 : String(value);745746 if (signed) {747 val = 's:' + sign(val, secret);Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 748why, one step back748 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 749why, one step back749Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
lines 750–756why, one step back750 if (opts.maxAge != null) {751 var maxAge = opts.maxAge - 0752753 if (!isNaN(maxAge)) {754 opts.expires = new Date(Date.now() + maxAge)755 opts.maxAge = Math.floor(maxAge / 1000)756 }Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 757–764why, one step back757 }758759 if (opts.path == null) {760 opts.path = '/';761 }762763 this.append('Set-Cookie', cookie.serialize(name, String(val), opts));764Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 765–781why, one step back765 return this;766};767768/**769 * Set the location header to `url`.770 *771 * The given `url` can also be "back", which redirects772 * to the _Referrer_ or _Referer_ headers or "/".773 *774 * Examples:775 *776 * res.location('/foo/bar').;777 * res.location('http://example.com');778 * res.location('../login');779 *780 * @param {String} url781 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 782why, one step back782 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 783–784why, one step back783 */784Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 785why, one step back785res.location = function location(url) {Delete `back` as a magic string (#5933)Blake Embreybefore adoptionbdd81f8
line 786why, one step back786 return this.set('Location', encodeUrl(url));Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 787–799why, one step back787};788789/**790 * Redirect to the given `url` with optional response `status`791 * defaulting to 302.792 *793 * Examples:794 *795 * res.redirect('/foo/bar');796 * res.redirect('http://example.com');797 * res.redirect(301, 'http://example.com');798 * res.redirect('../login'); // /blog/post/1 -> /blog/login799 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 800why, one step back800 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 801–809why, one step back801 */802803res.redirect = function redirect(url) {804 var address = url;805 var body;806 var status = 302;807808 // allow status / url809 if (arguments.length === 2) {Remove res.redirect(url, status) signatureMike Tunnicliffebefore adoption71395f5
lines 810–811why, one step back810 status = arguments[0]811 address = arguments[1]Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 812–814why, one step back812 }813814 // Set location headerMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
line 815why, one step back815 address = this.location(address).get('Location');Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 816–819why, one step back816817 // Support text/{plain,html} by default818 this.format({819 text: function(){Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 820why, one step back820 body = statuses.message[status] + '. Redirecting to ' + addressMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 821–823why, one step back821 },822823 html: function(){Fix res.redirect body when redirect status specifiedAlex Upadhyaybefore adoption6f0302f
line 824why, one step back824 var u = escapeHtml(address);Merge tag '4.20.0' into 5.0Wes Toddbefore adoptione5feb9f
line 825why, one step back825 body = '<p>' + statuses.message[status] + '. Redirecting to ' + u + '</p>'Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 826–833why, one step back826 },827828 default: function(){829 body = '';830 }831 });832833 // RespondMerge branch '5.0' into 5-mergectcpipbefore adoptiondde1f7d
line 834why, one step back834 this.status(status);Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 835–838why, one step back835 this.set('Content-Length', Buffer.byteLength(body));836837 if (this.req.method === 'HEAD') {838 this.end();Merge tag '4.11.2'Douglas Christopher Wilsonbefore adoptiona9ef9e1
lines 839–840why, one step back839 } else {840 this.end(body);Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 841–849why, one step back841 }842};843844/**845 * Add `field` to Vary. If already present in the Vary set, then846 * this call is simply ignored.847 *848 * @param {Array|String} field849 * @return {ServerResponse} for chainingMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 850why, one step back850 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 851–868why, one step back851 */852853res.vary = function(field){854 vary(this, field);855856 return this;857};858859/**860 * Render `view` with the given `options` and optional callback `fn`.861 * When a callback function is given a response will _not_ be made862 * automatically, otherwise a response of _200_ and _text/html_ is given.863 *864 * Options:865 *866 * - `cache` boolean hinting to the engine it should cache867 * - `filename` filename of the view being rendered868 *Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 869why, one step back869 * @publicMerge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 870–871why, one step back870 */871Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 872–875why, one step back872res.render = function render(view, options, callback) {873 var app = this.req.app;874 var done = callback;875 var opts = options || {};Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 876why, one step back876 var req = this.req;Merge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 877why, one step back877 var self = this;Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 878–879why, one step back878879 // support callback function as second argMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
lines 880–882why, one step back880 if (typeof options === 'function') {881 done = options;882 opts = {};Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 883–885why, one step back883 }884885 // merge res.localsMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 886why, one step back886 opts._locals = self.locals;Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 887–888why, one step back887888 // default callback to respondMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 889why, one step back889 done = done || function (err, str) {Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
lines 890–894why, one step back890 if (err) return req.next(err);891 self.send(str);892 };893894 // renderMerge tag '4.13.1'Douglas Christopher Wilsonbefore adoptionb309b87
line 895why, one step back895 app.render(view, opts, done);Merge tag '3.16.9'Douglas Christopher Wilsonbefore adoption20635d0
line 896why, one step back896};Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 897–900why, one step back897898// pipe the send file stream899function sendfile(res, file, options, callback) {900 var done = false;Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 901why, one step back901 var streaming;Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 902why, one step back902Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 903–908why, one step back903 // request aborted904 function onaborted() {905 if (done) return;906 done = true;907908 var err = new Error('Request aborted');Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 909why, one step back909 err.code = 'ECONNABORTED';Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 910–912why, one step back910 callback(err);911 }912Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 913–936why, one step back913 // directory914 function ondirectory() {915 if (done) return;916 done = true;917918 var err = new Error('EISDIR, read');919 err.code = 'EISDIR';920 callback(err);921 }922923 // errors924 function onerror(err) {925 if (done) return;926 done = true;927 callback(err);928 }929930 // ended931 function onend() {932 if (done) return;933 done = true;934 callback();935 }936Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 937–941why, one step back937 // file938 function onfile() {939 streaming = false;940 }941Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 942–943why, one step back942 // finished943 function onfinish(err) {Merge tag '4.12.4'Douglas Christopher Wilsonbefore adoptionf90e045
line 944why, one step back944 if (err && err.code === 'ECONNRESET') return onaborted();Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 945–948why, one step back945 if (err) return onerror(err);946 if (done) return;947948 setImmediate(function () {Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 949–953why, one step back949 if (streaming !== false && !done) {950 onaborted();951 return;952 }953Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 954–955why, one step back954 if (done) return;955 done = true;Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
line 956why, one step back956 callback();Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 957–959why, one step back957 });958 }959Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 960–965why, one step back960 // streaming961 function onstream() {962 streaming = true;963 }964965 file.on('directory', ondirectory);Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 966–967why, one step back966 file.on('end', onend);967 file.on('error', onerror);Merge tag '4.11.1'Douglas Christopher Wilsonbefore adoption8a5ecd3
lines 968–969why, one step back968 file.on('file', onfile);969 file.on('stream', onstream);Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 970–986why, one step back970 onFinished(res, onfinish);971972 if (options.headers) {973 // set headers on successful transfer974 file.on('headers', function headers(res) {975 var obj = options.headers;976 var keys = Object.keys(obj);977978 for (var i = 0; i < keys.length; i++) {979 var k = keys[i];980 res.setHeader(k, obj[k]);981 }982 });983 }984985 // pipe986 file.pipe(res);Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 987–989why, one step back987}988989/**Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 990–993why, one step back990 * Stringify JSON, like JSON.stringify, but v8 optimized, with the991 * ability to escape characters that can trigger HTML sniffing.992 *993 * @param {*} valueMerge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 994why, one step back994 * @param {function} replacerMerge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 995–997why, one step back995 * @param {number} spaces996 * @param {boolean} escape997 * @returns {string}Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 998–1000why, one step back998 * @private999 */1000Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 1001why, one step back1001function stringify (value, replacer, spaces, escape) {Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 1002–1003why, one step back1002 // v8 checks arguments.length for optimizing simple call1003 // https://bugs.chromium.org/p/v8/issues/detail?id=4730Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 1004why, one step back1004 var json = replacer || spacesMerge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 1005–1006why, one step back1005 ? JSON.stringify(value, replacer, spaces)1006 : JSON.stringify(value);Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
line 1007why, one step back1007Merge tag '4.17.2'Douglas Christopher Wilsonbefore adoptionc7d528c
line 1008why, one step back1008 if (escape && typeof json === 'string') {Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1009–1016why, one step back1009 json = json.replace(/[<>&]/g, function (c) {1010 switch (c.charCodeAt(0)) {1011 case 0x3c:1012 return '\\u003c'1013 case 0x3e:1014 return '\\u003e'1015 case 0x26:1016 return '\\u0026'Merge tag '4.17.1'Douglas Christopher Wilsonbefore adoption121fe99
line 1017why, one step back1017 /* istanbul ignore next: unreachable default */Merge tag '4.16.2'Douglas Christopher Wilsonbefore adoption62e12fe
lines 1018–1024why, one step back1018 default:1019 return c1020 }1021 })1022 }10231024 return jsonInvoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
line 1025why, one step back1025}