Skip to content
Myr

response.js

This file

Path

lib/response.js

1,142 lines · 26.5 KiB · JavaScript

Last changed by

Revert "Improve error message for null/undefined to res.status"Douglas Christopher Wilsonbefore adoption0a48e18

History of this file

Take it

1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2014-2015 Douglas Christopher Wilson5 * MIT Licensed6 */78'use strict';910/**11 * Module dependencies.12 * @private13 */1415var Buffer = require('safe-buffer').Buffer16var contentDisposition = require('content-disposition');17var deprecate = require('depd')('express');18var encodeUrl = require('encodeurl');19var escapeHtml = require('escape-html');20var http = require('http');21var isAbsolute = require('./utils').isAbsolute;22var onFinished = require('on-finished');23var path = require('path');24var statuses = require('statuses')25var merge = require('utils-merge');26var sign = require('cookie-signature').sign;27var normalizeType = require('./utils').normalizeType;28var normalizeTypes = require('./utils').normalizeTypes;29var setCharset = require('./utils').setCharset;30var cookie = require('cookie');31var send = require('send');32var extname = path.extname;33var mime = send.mime;34var resolve = path.resolve;35var vary = require('vary');3637/**38 * Response prototype.39 * @public40 */4142var res = Object.create(http.ServerResponse.prototype)4344/**45 * Module exports.46 * @public47 */4849module.exports = res5051/**52 * Module variables.53 * @private54 */5556var charsetRegExp = /;\s*charset\s*=/;5758/**59 * Set status `code`.60 *61 * @param {Number} code62 * @return {ServerResponse}63 * @public64 */6566res.status = function status(code) {67  this.statusCode = code;68  return this;69};7071/**72 * Set Link header field with the given `links`.73 *74 * Examples:75 *76 *    res.links({77 *      next: 'http://api.example.com/users?page=2',78 *      last: 'http://api.example.com/users?page=5'79 *    });80 *81 * @param {Object} links82 * @return {ServerResponse}83 * @public84 */8586res.links = function(links){87  var link = this.get('Link') || '';88  if (link) link += ', ';89  return this.set('Link', link + Object.keys(links).map(function(rel){90    return '<' + links[rel] + '>; rel="' + rel + '"';91  }).join(', '));92};9394/**95 * Send a response.96 *97 * Examples:98 *99 *     res.send(Buffer.from('wahoo'));100 *     res.send({ some: 'json' });101 *     res.send('<p>some html</p>');102 *103 * @param {string|number|boolean|object|Buffer} body104 * @public105 */106107res.send = function send(body) {108  var chunk = body;109  var encoding;110  var req = this.req;111  var type;112113  // settings114  var app = this.app;115116  // allow status / body117  if (arguments.length === 2) {118    // res.send(body, status) backwards compat119    if (typeof arguments[0] !== 'number' && typeof arguments[1] === 'number') {120      deprecate('res.send(body, status): Use res.status(status).send(body) instead');121      this.statusCode = arguments[1];122    } else {123      deprecate('res.send(status, body): Use res.status(status).send(body) instead');124      this.statusCode = arguments[0];125      chunk = arguments[1];126    }127  }128129  // disambiguate res.send(status) and res.send(status, num)130  if (typeof chunk === 'number' && arguments.length === 1) {131    // res.send(status) will set status message as text string132    if (!this.get('Content-Type')) {133      this.type('txt');134    }135136    deprecate('res.send(status): Use res.sendStatus(status) instead');137    this.statusCode = chunk;138    chunk = statuses[chunk]139  }140141  switch (typeof chunk) {142    // string defaulting to html143    case 'string':144      if (!this.get('Content-Type')) {145        this.type('html');146      }147      break;148    case 'boolean':149    case 'number':150    case 'object':151      if (chunk === null) {152        chunk = '';153      } else if (Buffer.isBuffer(chunk)) {154        if (!this.get('Content-Type')) {155          this.type('bin');156        }157      } else {158        return this.json(chunk);159      }160      break;161  }162163  // write strings in utf-8164  if (typeof chunk === 'string') {165    encoding = 'utf8';166    type = this.get('Content-Type');167168    // reflect this in content-type169    if (typeof type === 'string') {170      this.set('Content-Type', setCharset(type, 'utf-8'));171    }172  }173174  // determine if ETag should be generated175  var etagFn = app.get('etag fn')176  var generateETag = !this.get('ETag') && typeof etagFn === 'function'177178  // populate Content-Length179  var len180  if (chunk !== undefined) {181    if (Buffer.isBuffer(chunk)) {182      // get length of Buffer183      len = chunk.length184    } else if (!generateETag && chunk.length < 1000) {185      // just calculate length when no ETag + small chunk186      len = Buffer.byteLength(chunk, encoding)187    } else {188      // convert chunk to Buffer and calculate189      chunk = Buffer.from(chunk, encoding)190      encoding = undefined;191      len = chunk.length192    }193194    this.set('Content-Length', len);195  }196197  // populate ETag198  var etag;199  if (generateETag && len !== undefined) {200    if ((etag = etagFn(chunk, encoding))) {201      this.set('ETag', etag);202    }203  }204205  // freshness206  if (req.fresh) this.statusCode = 304;207208  // strip irrelevant headers209  if (204 === this.statusCode || 304 === this.statusCode) {210    this.removeHeader('Content-Type');211    this.removeHeader('Content-Length');212    this.removeHeader('Transfer-Encoding');213    chunk = '';214  }215216  if (req.method === 'HEAD') {217    // skip body for HEAD218    this.end();219  } else {220    // respond221    this.end(chunk, encoding);222  }223224  return this;225};226227/**228 * Send JSON response.229 *230 * Examples:231 *232 *     res.json(null);233 *     res.json({ user: 'tj' });234 *235 * @param {string|number|boolean|object} obj236 * @public237 */238239res.json = function json(obj) {240  var val = obj;241242  // allow status / body243  if (arguments.length === 2) {244    // res.json(body, status) backwards compat245    if (typeof arguments[1] === 'number') {246      deprecate('res.json(obj, status): Use res.status(status).json(obj) instead');247      this.statusCode = arguments[1];248    } else {249      deprecate('res.json(status, obj): Use res.status(status).json(obj) instead');250      this.statusCode = arguments[0];251      val = arguments[1];252    }253  }254255  // settings256  var app = this.app;257  var escape = app.get('json escape')258  var replacer = app.get('json replacer');259  var spaces = app.get('json spaces');260  var body = stringify(val, replacer, spaces, escape)261262  // content-type263  if (!this.get('Content-Type')) {264    this.set('Content-Type', 'application/json');265  }266267  return this.send(body);268};269270/**271 * Send JSON response with JSONP callback support.272 *273 * Examples:274 *275 *     res.jsonp(null);276 *     res.jsonp({ user: 'tj' });277 *278 * @param {string|number|boolean|object} obj279 * @public280 */281282res.jsonp = function jsonp(obj) {283  var val = obj;284285  // allow status / body286  if (arguments.length === 2) {287    // res.json(body, status) backwards compat288    if (typeof arguments[1] === 'number') {289      deprecate('res.jsonp(obj, status): Use res.status(status).json(obj) instead');290      this.statusCode = arguments[1];291    } else {292      deprecate('res.jsonp(status, obj): Use res.status(status).jsonp(obj) instead');293      this.statusCode = arguments[0];294      val = arguments[1];295    }296  }297298  // settings299  var app = this.app;300  var escape = app.get('json escape')301  var replacer = app.get('json replacer');302  var spaces = app.get('json spaces');303  var body = stringify(val, replacer, spaces, escape)304  var callback = this.req.query[app.get('jsonp callback name')];305306  // content-type307  if (!this.get('Content-Type')) {308    this.set('X-Content-Type-Options', 'nosniff');309    this.set('Content-Type', 'application/json');310  }311312  // fixup callback313  if (Array.isArray(callback)) {314    callback = callback[0];315  }316317  // jsonp318  if (typeof callback === 'string' && callback.length !== 0) {319    this.set('X-Content-Type-Options', 'nosniff');320    this.set('Content-Type', 'text/javascript');321322    // restrict callback charset323    callback = callback.replace(/[^\[\]\w$.]/g, '');324325    // replace chars not allowed in JavaScript that are in JSON326    body = body327      .replace(/\u2028/g, '\\u2028')328      .replace(/\u2029/g, '\\u2029');329330    // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse"331    // the typeof check is just to reduce client error noise332    body = '/**/ typeof ' + callback + ' === \'function\' && ' + callback + '(' + body + ');';333  }334335  return this.send(body);336};337338/**339 * Send given HTTP status code.340 *341 * Sets the response status to `statusCode` and the body of the342 * response to the standard description from node's http.STATUS_CODES343 * or the statusCode number if no description.344 *345 * Examples:346 *347 *     res.sendStatus(200);348 *349 * @param {number} statusCode350 * @public351 */352353res.sendStatus = function sendStatus(statusCode) {354  var body = statuses[statusCode] || String(statusCode)355356  this.statusCode = statusCode;357  this.type('txt');358359  return this.send(body);360};361362/**363 * Transfer the file at the given `path`.364 *365 * Automatically sets the _Content-Type_ response header field.366 * The callback `callback(err)` is invoked when the transfer is complete367 * or when an error occurs. Be sure to check `res.sentHeader`368 * if you wish to attempt responding, as the header and some data369 * may have already been transferred.370 *371 * Options:372 *373 *   - `maxAge`   defaulting to 0 (can be string converted by `ms`)374 *   - `root`     root directory for relative filenames375 *   - `headers`  object of headers to serve with file376 *   - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them377 *378 * Other options are passed along to `send`.379 *380 * Examples:381 *382 *  The following example illustrates how `res.sendFile()` may383 *  be used as an alternative for the `static()` middleware for384 *  dynamic situations. The code backing `res.sendFile()` is actually385 *  the same code, so HTTP cache support etc is identical.386 *387 *     app.get('/user/:uid/photos/:file', function(req, res){388 *       var uid = req.params.uid389 *         , file = req.params.file;390 *391 *       req.user.mayViewFilesFrom(uid, function(yes){392 *         if (yes) {393 *           res.sendFile('/uploads/' + uid + '/' + file);394 *         } else {395 *           res.send(403, 'Sorry! you cant see that.');396 *         }397 *       });398 *     });399 *400 * @public401 */402403res.sendFile = function sendFile(path, options, callback) {404  var done = callback;405  var req = this.req;406  var res = this;407  var next = req.next;408  var opts = options || {};409410  if (!path) {411    throw new TypeError('path argument is required to res.sendFile');412  }413414  if (typeof path !== 'string') {415    throw new TypeError('path must be a string to res.sendFile')416  }417418  // support function as second arg419  if (typeof options === 'function') {420    done = options;421    opts = {};422  }423424  if (!opts.root && !isAbsolute(path)) {425    throw new TypeError('path must be absolute or specify root to res.sendFile');426  }427428  // create file stream429  var pathname = encodeURI(path);430  var file = send(req, pathname, opts);431432  // transfer433  sendfile(res, file, opts, function (err) {434    if (done) return done(err);435    if (err && err.code === 'EISDIR') return next();436437    // next() all but write errors438    if (err && err.code !== 'ECONNABORTED' && err.syscall !== 'write') {439      next(err);440    }441  });442};443444/**445 * Transfer the file at the given `path`.446 *447 * Automatically sets the _Content-Type_ response header field.448 * The callback `callback(err)` is invoked when the transfer is complete449 * or when an error occurs. Be sure to check `res.sentHeader`450 * if you wish to attempt responding, as the header and some data451 * may have already been transferred.452 *453 * Options:454 *455 *   - `maxAge`   defaulting to 0 (can be string converted by `ms`)456 *   - `root`     root directory for relative filenames457 *   - `headers`  object of headers to serve with file458 *   - `dotfiles` serve dotfiles, defaulting to false; can be `"allow"` to send them459 *460 * Other options are passed along to `send`.461 *462 * Examples:463 *464 *  The following example illustrates how `res.sendfile()` may465 *  be used as an alternative for the `static()` middleware for466 *  dynamic situations. The code backing `res.sendfile()` is actually467 *  the same code, so HTTP cache support etc is identical.468 *469 *     app.get('/user/:uid/photos/:file', function(req, res){470 *       var uid = req.params.uid471 *         , file = req.params.file;472 *473 *       req.user.mayViewFilesFrom(uid, function(yes){474 *         if (yes) {475 *           res.sendfile('/uploads/' + uid + '/' + file);476 *         } else {477 *           res.send(403, 'Sorry! you cant see that.');478 *         }479 *       });480 *     });481 *482 * @public483 */484485res.sendfile = function (path, options, callback) {486  var done = callback;487  var req = this.req;488  var res = this;489  var next = req.next;490  var opts = options || {};491492  // support function as second arg493  if (typeof options === 'function') {494    done = options;495    opts = {};496  }497498  // create file stream499  var file = send(req, path, opts);500501  // transfer502  sendfile(res, file, opts, function (err) {503    if (done) return done(err);504    if (err && err.code === 'EISDIR') return next();505506    // next() all but write errors507    if (err && err.code !== 'ECONNABORTED' && err.syscall !== 'write') {508      next(err);509    }510  });511};512513res.sendfile = deprecate.function(res.sendfile,514  'res.sendfile: Use res.sendFile instead');515516/**517 * Transfer the file at the given `path` as an attachment.518 *519 * Optionally providing an alternate attachment `filename`,520 * and optional callback `callback(err)`. The callback is invoked521 * when the data transfer is complete, or when an error has522 * ocurred. Be sure to check `res.headersSent` if you plan to respond.523 *524 * Optionally providing an `options` object to use with `res.sendFile()`.525 * This function will set the `Content-Disposition` header, overriding526 * any `Content-Disposition` header passed as header options in order527 * to set the attachment and filename.528 *529 * This method uses `res.sendFile()`.530 *531 * @public532 */533534res.download = function download (path, filename, options, callback) {535  var done = callback;536  var name = filename;537  var opts = options || null538539  // support function as second or third arg540  if (typeof filename === 'function') {541    done = filename;542    name = null;543    opts = null544  } else if (typeof options === 'function') {545    done = options546    opts = null547  }548549  // set Content-Disposition when file is sent550  var headers = {551    'Content-Disposition': contentDisposition(name || path)552  };553554  // merge user-provided headers555  if (opts && opts.headers) {556    var keys = Object.keys(opts.headers)557    for (var i = 0; i < keys.length; i++) {558      var key = keys[i]559      if (key.toLowerCase() !== 'content-disposition') {560        headers[key] = opts.headers[key]561      }562    }563  }564565  // merge user-provided options566  opts = Object.create(opts)567  opts.headers = headers568569  // Resolve the full path for sendFile570  var fullPath = resolve(path);571572  // send file573  return this.sendFile(fullPath, opts, done)574};575576/**577 * Set _Content-Type_ response header with `type` through `mime.lookup()`578 * when it does not contain "/", or set the Content-Type to `type` otherwise.579 *580 * Examples:581 *582 *     res.type('.html');583 *     res.type('html');584 *     res.type('json');585 *     res.type('application/json');586 *     res.type('png');587 *588 * @param {String} type589 * @return {ServerResponse} for chaining590 * @public591 */592593res.contentType =594res.type = function contentType(type) {595  var ct = type.indexOf('/') === -1596    ? mime.lookup(type)597    : type;598599  return this.set('Content-Type', ct);600};601602/**603 * Respond to the Acceptable formats using an `obj`604 * of mime-type callbacks.605 *606 * This method uses `req.accepted`, an array of607 * acceptable types ordered by their quality values.608 * When "Accept" is not present the _first_ callback609 * is invoked, otherwise the first match is used. When610 * no match is performed the server responds with611 * 406 "Not Acceptable".612 *613 * Content-Type is set for you, however if you choose614 * you may alter this within the callback using `res.type()`615 * or `res.set('Content-Type', ...)`.616 *617 *    res.format({618 *      'text/plain': function(){619 *        res.send('hey');620 *      },621 *622 *      'text/html': function(){623 *        res.send('<p>hey</p>');624 *      },625 *626 *      'appliation/json': function(){627 *        res.send({ message: 'hey' });628 *      }629 *    });630 *631 * In addition to canonicalized MIME types you may632 * also use extnames mapped to these types:633 *634 *    res.format({635 *      text: function(){636 *        res.send('hey');637 *      },638 *639 *      html: function(){640 *        res.send('<p>hey</p>');641 *      },642 *643 *      json: function(){644 *        res.send({ message: 'hey' });645 *      }646 *    });647 *648 * By default Express passes an `Error`649 * with a `.status` of 406 to `next(err)`650 * if a match is not made. If you provide651 * a `.default` callback it will be invoked652 * instead.653 *654 * @param {Object} obj655 * @return {ServerResponse} for chaining656 * @public657 */658659res.format = function(obj){660  var req = this.req;661  var next = req.next;662663  var fn = obj.default;664  if (fn) delete obj.default;665  var keys = Object.keys(obj);666667  var key = keys.length > 0668    ? req.accepts(keys)669    : false;670671  this.vary("Accept");672673  if (key) {674    this.set('Content-Type', normalizeType(key).value);675    obj[key](req, this, next);676  } else if (fn) {677    fn();678  } else {679    var err = new Error('Not Acceptable');680    err.status = err.statusCode = 406;681    err.types = normalizeTypes(keys).map(function(o){ return o.value });682    next(err);683  }684685  return this;686};687688/**689 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.690 *691 * @param {String} filename692 * @return {ServerResponse}693 * @public694 */695696res.attachment = function attachment(filename) {697  if (filename) {698    this.type(extname(filename));699  }700701  this.set('Content-Disposition', contentDisposition(filename));702703  return this;704};705706/**707 * Append additional header `field` with value `val`.708 *709 * Example:710 *711 *    res.append('Link', ['<http://localhost/>', '<http://localhost:3000/>']);712 *    res.append('Set-Cookie', 'foo=bar; Path=/; HttpOnly');713 *    res.append('Warning', '199 Miscellaneous warning');714 *715 * @param {String} field716 * @param {String|Array} val717 * @return {ServerResponse} for chaining718 * @public719 */720721res.append = function append(field, val) {722  var prev = this.get(field);723  var value = val;724725  if (prev) {726    // concat the new and prev vals727    value = Array.isArray(prev) ? prev.concat(val)728      : Array.isArray(val) ? [prev].concat(val)729      : [prev, val];730  }731732  return this.set(field, value);733};734735/**736 * Set header `field` to `val`, or pass737 * an object of header fields.738 *739 * Examples:740 *741 *    res.set('Foo', ['bar', 'baz']);742 *    res.set('Accept', 'application/json');743 *    res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });744 *745 * Aliased as `res.header()`.746 *747 * @param {String|Object} field748 * @param {String|Array} val749 * @return {ServerResponse} for chaining750 * @public751 */752753res.set =754res.header = function header(field, val) {755  if (arguments.length === 2) {756    var value = Array.isArray(val)757      ? val.map(String)758      : String(val);759760    // add charset to content-type761    if (field.toLowerCase() === 'content-type') {762      if (Array.isArray(value)) {763        throw new TypeError('Content-Type cannot be set to an Array');764      }765      if (!charsetRegExp.test(value)) {766        var charset = mime.charsets.lookup(value.split(';')[0]);767        if (charset) value += '; charset=' + charset.toLowerCase();768      }769    }770771    this.setHeader(field, value);772  } else {773    for (var key in field) {774      this.set(key, field[key]);775    }776  }777  return this;778};779780/**781 * Get value for header `field`.782 *783 * @param {String} field784 * @return {String}785 * @public786 */787788res.get = function(field){789  return this.getHeader(field);790};791792/**793 * Clear cookie `name`.794 *795 * @param {String} name796 * @param {Object} [options]797 * @return {ServerResponse} for chaining798 * @public799 */800801res.clearCookie = function clearCookie(name, options) {802  var opts = merge({ expires: new Date(1), path: '/' }, options);803804  return this.cookie(name, '', opts);805};806807/**808 * Set cookie `name` to `value`, with the given `options`.809 *810 * Options:811 *812 *    - `maxAge`   max-age in milliseconds, converted to `expires`813 *    - `signed`   sign the cookie814 *    - `path`     defaults to "/"815 *816 * Examples:817 *818 *    // "Remember Me" for 15 minutes819 *    res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });820 *821 *    // same as above822 *    res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })823 *824 * @param {String} name825 * @param {String|Object} value826 * @param {Object} [options]827 * @return {ServerResponse} for chaining828 * @public829 */830831res.cookie = function (name, value, options) {832  var opts = merge({}, options);833  var secret = this.req.secret;834  var signed = opts.signed;835836  if (signed && !secret) {837    throw new Error('cookieParser("secret") required for signed cookies');838  }839840  var val = typeof value === 'object'841    ? 'j:' + JSON.stringify(value)842    : String(value);843844  if (signed) {845    val = 's:' + sign(val, secret);846  }847848  if ('maxAge' in opts) {849    opts.expires = new Date(Date.now() + opts.maxAge);850    opts.maxAge /= 1000;851  }852853  if (opts.path == null) {854    opts.path = '/';855  }856857  this.append('Set-Cookie', cookie.serialize(name, String(val), opts));858859  return this;860};861862/**863 * Set the location header to `url`.864 *865 * The given `url` can also be "back", which redirects866 * to the _Referrer_ or _Referer_ headers or "/".867 *868 * Examples:869 *870 *    res.location('/foo/bar').;871 *    res.location('http://example.com');872 *    res.location('../login');873 *874 * @param {String} url875 * @return {ServerResponse} for chaining876 * @public877 */878879res.location = function location(url) {880  var loc = url;881882  // "back" is an alias for the referrer883  if (url === 'back') {884    loc = this.req.get('Referrer') || '/';885  }886887  // set location888  return this.set('Location', encodeUrl(loc));889};890891/**892 * Redirect to the given `url` with optional response `status`893 * defaulting to 302.894 *895 * The resulting `url` is determined by `res.location()`, so896 * it will play nicely with mounted apps, relative paths,897 * `"back"` etc.898 *899 * Examples:900 *901 *    res.redirect('/foo/bar');902 *    res.redirect('http://example.com');903 *    res.redirect(301, 'http://example.com');904 *    res.redirect('../login'); // /blog/post/1 -> /blog/login905 *906 * @public907 */908909res.redirect = function redirect(url) {910  var address = url;911  var body;912  var status = 302;913914  // allow status / url915  if (arguments.length === 2) {916    if (typeof arguments[0] === 'number') {917      status = arguments[0];918      address = arguments[1];919    } else {920      deprecate('res.redirect(url, status): Use res.redirect(status, url) instead');921      status = arguments[1];922    }923  }924925  // Set location header926  address = this.location(address).get('Location');927928  // Support text/{plain,html} by default929  this.format({930    text: function(){931      body = statuses[status] + '. Redirecting to ' + address932    },933934    html: function(){935      var u = escapeHtml(address);936      body = '<p>' + statuses[status] + '. Redirecting to <a href="' + u + '">' + u + '</a></p>'937    },938939    default: function(){940      body = '';941    }942  });943944  // Respond945  this.statusCode = status;946  this.set('Content-Length', Buffer.byteLength(body));947948  if (this.req.method === 'HEAD') {949    this.end();950  } else {951    this.end(body);952  }953};954955/**956 * Add `field` to Vary. If already present in the Vary set, then957 * this call is simply ignored.958 *959 * @param {Array|String} field960 * @return {ServerResponse} for chaining961 * @public962 */963964res.vary = function(field){965  // checks for back-compat966  if (!field || (Array.isArray(field) && !field.length)) {967    deprecate('res.vary(): Provide a field name');968    return this;969  }970971  vary(this, field);972973  return this;974};975976/**977 * Render `view` with the given `options` and optional callback `fn`.978 * When a callback function is given a response will _not_ be made979 * automatically, otherwise a response of _200_ and _text/html_ is given.980 *981 * Options:982 *983 *  - `cache`     boolean hinting to the engine it should cache984 *  - `filename`  filename of the view being rendered985 *986 * @public987 */988989res.render = function render(view, options, callback) {990  var app = this.req.app;991  var done = callback;992  var opts = options || {};993  var req = this.req;994  var self = this;995996  // support callback function as second arg997  if (typeof options === 'function') {998    done = options;999    opts = {};1000  }10011002  // merge res.locals1003  opts._locals = self.locals;10041005  // default callback to respond1006  done = done || function (err, str) {1007    if (err) return req.next(err);1008    self.send(str);1009  };10101011  // render1012  app.render(view, opts, done);1013};10141015// pipe the send file stream1016function sendfile(res, file, options, callback) {1017  var done = false;1018  var streaming;10191020  // request aborted1021  function onaborted() {1022    if (done) return;1023    done = true;10241025    var err = new Error('Request aborted');1026    err.code = 'ECONNABORTED';1027    callback(err);1028  }10291030  // directory1031  function ondirectory() {1032    if (done) return;1033    done = true;10341035    var err = new Error('EISDIR, read');1036    err.code = 'EISDIR';1037    callback(err);1038  }10391040  // errors1041  function onerror(err) {1042    if (done) return;1043    done = true;1044    callback(err);1045  }10461047  // ended1048  function onend() {1049    if (done) return;1050    done = true;1051    callback();1052  }10531054  // file1055  function onfile() {1056    streaming = false;1057  }10581059  // finished1060  function onfinish(err) {1061    if (err && err.code === 'ECONNRESET') return onaborted();1062    if (err) return onerror(err);1063    if (done) return;10641065    setImmediate(function () {1066      if (streaming !== false && !done) {1067        onaborted();1068        return;1069      }10701071      if (done) return;1072      done = true;1073      callback();1074    });1075  }10761077  // streaming1078  function onstream() {1079    streaming = true;1080  }10811082  file.on('directory', ondirectory);1083  file.on('end', onend);1084  file.on('error', onerror);1085  file.on('file', onfile);1086  file.on('stream', onstream);1087  onFinished(res, onfinish);10881089  if (options.headers) {1090    // set headers on successful transfer1091    file.on('headers', function headers(res) {1092      var obj = options.headers;1093      var keys = Object.keys(obj);10941095      for (var i = 0; i < keys.length; i++) {1096        var k = keys[i];1097        res.setHeader(k, obj[k]);1098      }1099    });1100  }11011102  // pipe1103  file.pipe(res);1104}11051106/**1107 * Stringify JSON, like JSON.stringify, but v8 optimized, with the1108 * ability to escape characters that can trigger HTML sniffing.1109 *1110 * @param {*} value1111 * @param {function} replaces1112 * @param {number} spaces1113 * @param {boolean} escape1114 * @returns {string}1115 * @private1116 */11171118function stringify (value, replacer, spaces, escape) {1119  // v8 checks arguments.length for optimizing simple call1120  // https://bugs.chromium.org/p/v8/issues/detail?id=47301121  var json = replacer || spaces1122    ? JSON.stringify(value, replacer, spaces)1123    : JSON.stringify(value);11241125  if (escape) {1126    json = json.replace(/[<>&]/g, function (c) {1127      switch (c.charCodeAt(0)) {1128        case 0x3c:1129          return '\\u003c'1130        case 0x3e:1131          return '\\u003e'1132        case 0x26:1133          return '\\u0026'1134        /* istanbul ignore next: unreachable default */1135        default:1136          return c1137      }1138    })1139  }11401141  return json1142}