Example #1
0
    function assetPipeline(request, response, next)
    {
        var normalizedPath = normalize(request.url);
        var priorityList = terraform.helpers.buildPriorityList(normalizedPath);
        var source = terraform.helpers.findFirstFile(assets, priorityList);

        if (!source)
            return next();

        terraformer.render(source, function(err, body)
        {
            if (err)
            {
                if (request.app.logger)
                    request.app.logger.error({ error: err.message, path: normalizedPath }, 'while rendering assset pipeline');
                return next();
            }

            if (!body)
                return next();

            var outputtype = terraform.helpers.outputType(source);
            var mimetype = lookupMimeType(outputtype);
            var charset = mime.charsets.lookup(mimetype);

            response.statusCode = 200;
            response.setHeader('Content-Type', mimetype + (charset ? '; charset=' + charset : ''));
            response.setHeader('Content-Length', body.length);
            response.end(body);
        });
    }
Example #2
0
  skin(req, rsp, [poly], function(){
    var priorityList  = terraform.helpers.buildPriorityList("404.html")
    var sourceFile    = terraform.helpers.findFirstFile(req.setup.publicPath, priorityList)
    if(!sourceFile) return next()

    req.poly.render(sourceFile, function(error, body){
      if(error){
        // TODO: make this better
        rsp.statusCode = 404;
        rsp.end("There is an error in your " + sourceFile + " file")
      }else{
        if(!body) return next()
        var type    = helpers.mimeType("html")
        var charset = mime.charsets.lookup(type)
        rsp.setHeader('Content-Type', type + (charset ? '; charset=' + charset : ''));
        rsp.setHeader('Content-Length', body.length);
        rsp.statusCode = 404;
        rsp.end(body)
      }
    })
  })
Example #3
0
  return function(req, rsp, next){
    var normalizedPath  = helpers.normalizeUrl(req.url)
    var priorityList    = terraform.helpers.buildPriorityList(normalizedPath)
    var sourceFile      = terraform.helpers.findFirstFile(publicPath, priorityList)

    if(!sourceFile) return next()

    terra.render(sourceFile, function(error, body){
      if(error) return next(error)
      if(!body) return next() // 404

      var outputType = terraform.helpers.outputType(sourceFile)
      var mimeType   = helpers.mimeType(outputType)
      var charset    = mime.charsets.lookup(mimeType)
      rsp.statusCode = 200
      rsp.setHeader('Content-Type', mimeType + (charset ? '; charset=' + charset : ''))
      rsp.setHeader('Content-Length', body.length)
      rsp.end(body)
    })

  }
Example #4
0
exports.process = function(req, rsp, next){
  var normalizedPath  = helpers.normalizeUrl(req.url)
  var priorityList    = terraform.helpers.buildPriorityList(normalizedPath)
  var sourceFile      = terraform.helpers.findFirstFile(req.setup.publicPath, priorityList)

  /**
   * We GTFO if we don't have a source file.
   */

  if(!sourceFile) return next()


  /**
   * Now we let terraform handle the asset pipeline.
   */

  req.poly.render(sourceFile, function(error, body){
    if(error){
      error.stack = helpers.stacktrace(error.stack, { lineno: error.lineno })

      var locals = {
        project: req.headers.host,
        error: error,
        pkg: pkg
      }
      if(terraform.helpers.outputType(sourceFile) == 'css'){
        var outputType = terraform.helpers.outputType(sourceFile)
        var mimeType   = helpers.mimeType(outputType)
        var charset    = mime.charsets.lookup(mimeType)
        var body       = helpers.cssError(locals)
        rsp.statusCode = 200
        rsp.setHeader('Content-Type', mimeType + (charset ? '; charset=' + charset : ''))
        rsp.setHeader('Content-Length', Buffer.byteLength(body, charset))
        rsp.end(body)
      }else{

        // Make the paths relative but keep the root dir.
        // TODO: move to helper.
        //
        // var loc = req.projectPath.split(path.sep); loc.pop()
        // var loc = loc.join(path.sep) + path.sep
        // if(error.filename) error.filename = error.filename.replace(loc, "")

        terraform.root(__dirname + "/templates").render("error.jade", locals, function(err, body){
          var mimeType   = helpers.mimeType('html')
          var charset    = mime.charsets.lookup(mimeType)
          rsp.statusCode = 500
          rsp.setHeader('Content-Type', mimeType + (charset ? '; charset=' + charset : ''))
          rsp.setHeader('Content-Length', Buffer.byteLength(body, charset))
          rsp.end(body)
        })
      }
    }else{
      // 404
      if(!body) return next()

      var outputType = terraform.helpers.outputType(sourceFile)
      var mimeType   = helpers.mimeType(outputType)
      var charset    = mime.charsets.lookup(mimeType)
      rsp.statusCode = 200
      rsp.setHeader('Content-Type', mimeType + (charset ? '; charset=' + charset : ''))
      rsp.setHeader('Content-Length', Buffer.byteLength(body, charset));
      rsp.end(body);
    }
  })

}
Example #5
0
exports.process = function(req, rsp, next){
  var normalizedPath  = helpers.normalizeUrl(req.url)
  var priorityList    = terraform.helpers.buildPriorityList(normalizedPath)
  var sourceFile      = terraform.helpers.findFirstFile(req.setup.publicPath, priorityList)


  /**
   * We GTFO if we don't have a source file.
   */

  if(!sourceFile){
    if (path.basename(normalizedPath) === "index.html") {
      var pathAr = normalizedPath.split(path.sep); pathAr.pop() // Pop index.html off the list
      var prospectCleanPath       = pathAr.join("/")
      var prospectNormalizedPath  = helpers.normalizeUrl(prospectCleanPath)
      var prospectPriorityList    = terraform.helpers.buildPriorityList(prospectNormalizedPath)
      prospectPriorityList.push(path.basename(prospectNormalizedPath + ".html"))

      sourceFile = terraform.helpers.findFirstFile(req.setup.publicPath, prospectPriorityList)

      if (!sourceFile) {
        return next()
      } else {
        // 301 redirect
        rsp.statusCode = 301
        rsp.setHeader('Location', prospectCleanPath)
        rsp.end('Redirecting to ' + utilsEscape(prospectCleanPath))
      }

    } else if(path.extname(normalizedPath) === '.map') {
      normalizedPath = helpers.popMapExtension(normalizedPath);
      priorityList    = terraform.helpers.buildPriorityList(normalizedPath)
      sourceFile      = terraform.helpers.findFirstFile(req.setup.publicPath, priorityList)

      req.poly.render(sourceFile, function(error, body, sourcemap){
        var mimeType   = 'application/json'
        var charset    = mime.charsets.lookup(mimeType)
        rsp.statusCode = 200;
        rsp.setHeader('Content-Type', mimeType + (charset ? '; charset=' + charset : ''));
        rsp.setHeader('Content-Length', Buffer.byteLength(sourcemap, charset));
        rsp.end(sourcemap.toString());
      });

    } else {
      return next()
    }
  } else {

    /**
     * Now we let terraform handle the asset pipeline.
     */

    req.poly.render(sourceFile, function(error, body, sourcemap){
      if(error){
        error.stack = helpers.stacktrace(error.stack, { lineno: error.lineno })

        var locals = {
          project: req.headers.host,
          error: error,
          pkg: pkg
        }
        if(terraform.helpers.outputType(sourceFile) == 'css'){
          var outputType = terraform.helpers.outputType(sourceFile)
          var mimeType   = helpers.mimeType(outputType)
          var charset    = mime.charsets.lookup(mimeType)
          var body       = helpers.cssError(locals)
          rsp.statusCode = 200
          rsp.setHeader('Content-Type', mimeType + (charset ? '; charset=' + charset : ''))
          rsp.setHeader('Content-Length', Buffer.byteLength(body, charset));
          rsp.end(body)
        }else{

          // Make the paths relative but keep the root dir.
          // TODO: move to helper.
          //
          // var loc = req.projectPath.split(path.sep); loc.pop()
          // var loc = loc.join(path.sep) + path.sep
          // if(error.filename) error.filename = error.filename.replace(loc, "")

          terraform.root(__dirname + "/templates").render("error.jade", locals, function(err, body){
            var mimeType   = helpers.mimeType('html')
            var charset    = mime.charsets.lookup(mimeType)
            rsp.statusCode = 500
            rsp.setHeader('Content-Type', mimeType + (charset ? '; charset=' + charset : ''))
            rsp.setHeader('Content-Length', Buffer.byteLength(body, charset));
            rsp.end(body)
          })
        }
      }else{
        // 404
        if(!body) return next()

        var outputType = terraform.helpers.outputType(sourceFile)
        var mimeType   = helpers.mimeType(outputType)
        var charset    = mime.charsets.lookup(mimeType)
        if(sourcemap !== undefined) {
          body += '\n/*# sourceMappingURL=' + path.basename(normalizedPath) + '.map */'
        }
        rsp.statusCode = 200
        rsp.setHeader('Content-Type', mimeType + (charset ? '; charset=' + charset : ''))
        rsp.setHeader('Content-Length', Buffer.byteLength(body, charset));
        rsp.end(body);
      }
    })
  }










}