dox-foundation-demo node_modules/express/node_modules/send/lib/send.js

debug

declaration
debug

    Description

    Module dependencies.

    Source

    var debug = require('debug')('send')
      , parseRange = require('range-parser')
      , Stream = require('stream')
      , mime = require('mime')
      , fresh = require('fresh')
      , path = require('path')
      , http = require('http')
      , fs = require('fs')
      , basename = path.basename
      , normalize = path.normalize
      , join = path.join
      , utils = require('./utils');

    send

    function
    send()
    • @param: {Request}req
    • @param: {String}path
    • @param: {Object}options
    • @return: {SendStream}

    Description

    Return a SendStream for req and path.

    Source

    function send(req, path, options) {
      return new SendStream(req, path, options);
    }

    __proto__

    property
    SendStream.prototype.__proto__

      Description

      Inherits from Stream.prototype.

      Source

      SendStream.prototype.__proto__ = Stream.prototype;

      hidden

      method
      SendStream.prototype.hidden()
      • @param: {Boolean}path
      • @return: {SendStream}

      Description

      Enable or disable "hidden" (dot) files.

      Source

      SendStream.prototype.hidden = function(val){
        debug('hidden %s', val);
        this._hidden = val;
        return this;
      };

      index

      method
      SendStream.prototype.index()
      • @param: {String,Boolean}path
      • @return: {SendStream}

      Description

      Set index path, set to a falsy
      value to disable index support.

      Source

      SendStream.prototype.index = function(path){
        debug('index %s', path);
        this._index = path;
        return this;
      };

      root

      property
      SendStream.prototype.root
      • @param: {String}path
      • @return: {SendStream}

      Description

      Set root path.

      Source

      SendStream.prototype.root = 
      SendStream.prototype.from = function(path){
        this._root = normalize(path);
        return this;
      };

      maxage

      method
      SendStream.prototype.maxage()
      • @param: {Number}ms
      • @return: {SendStream}

      Description

      Set max-age to ms.

      Source

      SendStream.prototype.maxage = function(ms){
        if (Infinity == ms) ms = 60 * 60 * 24 * 365 * 1000;
        debug('max-age %d', ms);
        this._maxage = ms;
        return this;
      };

      pipe

      method
      SendStream.prototype.pipe()
      • @param: {Stream}res
      • @return: {Stream}res

      Description

      Pipe to `res.

      Source

      SendStream.prototype.pipe = function(res){
        var self = this
          , args = arguments
          , path = this.path
          , root = this._root;
      
        // references
        this.res = res;
      
        // invalid request uri
        path = utils.decode(path);
        if (-1 == path) return this.error(400);
      
        // null byte(s)
        if (~path.indexOf('\0')) return this.error(400);
      
        // join / normalize from optional root dir
        if (root) path = normalize(join(this._root, path));
      
        // ".." is malicious without "root"
        if (this.isMalicious()) return this.error(403);
      
        // malicious path
        if (root && 0 != path.indexOf(root)) return this.error(403);
      
        // hidden file support
        if (!this._hidden && this.hasLeadingDot()) return this.error(404);
      
        // index file support
        if (this._index && this.hasTrailingSlash()) path += this._index;
      
        debug('stat "%s"', path);
        fs.stat(path, function(err, stat){
          if (err) return self.onStatError(err);
          if (stat.isDirectory()) return self.redirect(self.path);
          self.emit('file', path, stat);
          self.send(path, stat);
        });
      
        return res;
      };

      send

      method
      SendStream.prototype.send()
      • @param: {String}path

      Description

      Transfer path.

      Source

      SendStream.prototype.send = function(path, stat){
        var options = this.options;
        var len = stat.size;
        var res = this.res;
        var req = this.req;
        var ranges = req.headers.range;
        var offset = options.start || 0;
      
        // set header fields
        this.setHeader(stat);
      
        // set content-type
        this.type(path);
      
        // conditional GET support
        if (this.isConditionalGET()
          && this.isCachable()
          && this.isFresh()) {
          return this.notModified();
        }
      
        // adjust len to start/end options
        len = Math.max(0, len - offset);
        if (options.end !== undefined) {
          var bytes = options.end - offset + 1;
          if (len > bytes) len = bytes;
        }
      
        // Range support
        if (ranges) {
          ranges = parseRange(len, ranges);
      
          // unsatisfiable
          if (-1 == ranges) {
            res.setHeader('Content-Range', 'bytes */' + stat.size);
            return this.error(416);
          }
      
          // valid (syntactically invalid ranges are treated as a regular response)
          if (-2 != ranges) {
            options.start = offset + ranges[0].start;
            options.end = offset + ranges[0].end;
      
            // Content-Range
            res.statusCode = 206;
            res.setHeader('Content-Range', 'bytes '
              + ranges[0].start
              + '-'
              + ranges[0].end
              + '/'
              + len);
            len = options.end - options.start + 1;
          }
        }
      
        // content-length
        res.setHeader('Content-Length', len);
      
        // HEAD support
        if ('HEAD' == req.method) return res.end();
      
        this.stream(path, options);
      };