Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
skin(req, rsp, [poly], function(){
var priorityList = terraform.helpers.buildPriorityList("200.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', Buffer.byteLength(body, charset));
rsp.statusCode = 200;
rsp.end(body)
}
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', Buffer.byteLength(body, charset));
rsp.end(body)
})
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', Buffer.byteLength(body, charset));
rsp.end(body)
})
exports.mwl = function(req, rsp, next){
var ext = path.extname(req.url).replace(/^\./, '')
req.originalExt = ext
// This prevents the source files from being served, but also
// has to factor in that in this brave new world, sometimes
// `.html` (Handlebars, others), `.css` (PostCSS), and
// `.js` (Browserify) are actually being used to specify
// source files
//if (['js'].indexOf(ext) === -1) {
if (terraform.helpers.processors["html"].indexOf(ext) !== -1 || terraform.helpers.processors["css"].indexOf(ext) !== -1 || terraform.helpers.processors["js"].indexOf(ext) !== -1) {
notFound(req, rsp, next)
} else {
next()
}
//} else {
//next()
//}
}
// not sure what this does anymore.
if(!contents || contents.replace(/^\s\s*/, '').replace(/\s\s*$/, '') == ''){
contents = '{}'
}
// attempt to parse the file
try{
var cfg = JSON.parse(contents)
}catch(e){
e.source = "JSON"
e.dest = "CONFIG"
e.message = e.message
e.filename = configPath
e.stack = contents
e.lineno = -1
throw new terraform.helpers.TerraformError(e)
}
if(!cfg.hasOwnProperty('globals')) cfg['globals'] = {}
cfg.globals.environment = process.env.NODE_ENV || env
// replace values that look like environment variables
// e.g. '$foo' -> process.env.foo
cfg = envy(cfg)
return {
projectPath : projectPath,
publicPath : publicPath,
config : cfg
}
skin(req, rsp, [poly], function(){
var priorityList = terraform.helpers.buildPriorityList("200.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', Buffer.byteLength(body, charset));
rsp.statusCode = 200;
rsp.end(body)
terra.render(file, function(error, body){
if(error){
done(error)
}else{
if(body){
var dest = path.resolve(outputPath, terraform.helpers.outputPath(file))
fs.mkdirp(path.dirname(dest), function(err){
fs.writeFile(dest, body, done)
})
}else{
if (file === "bundle.cjs"){
var b = browserify({
entries: [path.resolve(setup.publicPath, "bundle.cjs")],
cache: {},
packageCache: {},
plugin: []
})
var dest = path.resolve(outputPath, "bundle.js")
var chunks = []
var stream = b.bundle()