connect-preprocess
Advanced tools
Comparing version 0.1.3 to 1.0.0
130
index.js
@@ -1,76 +0,80 @@ | ||
module.exports = function( options ) { | ||
module.exports = function( options ){ | ||
'use strict'; | ||
var Q = require( 'q' ); | ||
var url = require( 'url' ); | ||
var mime = require( 'mime' ); | ||
var fs = require( 'fs-extra' ); | ||
var extend = require( 'extend' ); | ||
var resolvePath = require( 'resolve-path' ); | ||
function $Defaults() { | ||
return { | ||
var $preprocessor = (function(){ | ||
var defaults = { | ||
root: process.cwd(), | ||
index: 'index.html', | ||
accept: [ 'html' , 'css' , 'js' ], | ||
engine: function( string ) { return string } | ||
}; | ||
} | ||
var $preprocessor = extend( $Defaults() , options , { | ||
_accepts: function( req , res ) { | ||
var content_type = res._headers ? (res._headers['content-type'] || '').split( ';' )[0] : null; | ||
var ext = content_type ? mime.extension( content_type ) : req.url.match( /(\.\w+)\??.*$/ ); | ||
return res.statusCode == 200 && this.accept.indexOf( ext ) >= 0; | ||
} | ||
}); | ||
return extend( defaults , options , { | ||
_accepts: function( fpath ){ | ||
var match = fpath.match( /\.(\w+)$/ ); | ||
return match ? $preprocessor.accept.indexOf( match[1] ) >= 0 : false; | ||
} | ||
}); | ||
}()); | ||
return function( req , res , next ) { | ||
if (res.__preprocess) { | ||
return next(); | ||
} | ||
res.__preprocess = true; | ||
var writeHead = res.writeHead; | ||
var write = res.write; | ||
var end = res.end; | ||
function restore() { | ||
res.writeHead = writeHead; | ||
res.write = write; | ||
res.end = end; | ||
} | ||
res.push = function( chunk ) { | ||
res.data = (res.data || '') + chunk; | ||
}; | ||
res.writeHead = function(){}; | ||
res.write = function( string , encoding ) { | ||
encoding = encoding || 'utf-8'; | ||
if (string !== undefined) { | ||
var body = (Buffer.isBuffer( string ) ? string.toString( encoding ) : string); | ||
if (res._headers && $preprocessor._accepts( req , res )) { | ||
res.push( body ); | ||
} | ||
else { | ||
restore(); | ||
return write.call( res , string , encoding ); | ||
} | ||
return function middleware( req , res , next ){ | ||
var pathname = url.parse( req.url ).pathname; | ||
var fpath = resolvePath( $preprocessor.root , pathname.substr( 1 )); | ||
Q.fcall(function(){ | ||
if (req.method != 'GET') { | ||
return Q.reject(); | ||
} | ||
}; | ||
res.end = function( string , encoding ) { | ||
restore(); | ||
encoding = encoding || 'utf-8'; | ||
if (res.data && $preprocessor._accepts( req , res )) { | ||
string = $preprocessor.engine( res.data ); | ||
if (res.data !== undefined && !res._header) { | ||
res.setHeader( 'Content-Length' , Buffer.byteLength( string , encoding )); | ||
}) | ||
.then(function(){ | ||
return Q.fcall(function getStats(){ | ||
return Q.promise(function( resolve , reject ){ | ||
fs.stat( fpath , function( err , stats ){ | ||
return err ? reject() : resolve( stats ); | ||
}); | ||
}) | ||
.then(function( stats ){ | ||
if (stats.isDirectory() && $preprocessor.index) { | ||
fpath = resolvePath( fpath , $preprocessor.index ); | ||
return getStats(); | ||
} | ||
return stats; | ||
}); | ||
}) | ||
.then(function( stats ){ | ||
if (stats.isFile() && $preprocessor._accepts( fpath )) { | ||
return Q.promise(function( resolve , reject ){ | ||
fs.readFile( fpath , 'utf-8' , function( err , content ){ | ||
return err ? reject( err ) : resolve( content ); | ||
}); | ||
}); | ||
} | ||
res.data = ''; | ||
} | ||
res.end( string , encoding ); | ||
}; | ||
next(); | ||
return Q.reject(); | ||
}); | ||
}) | ||
.then(function( content ){ | ||
var info = { | ||
root: fpath.substr( 0 , fpath.indexOf( pathname )), | ||
pathname: fpath.substr(fpath.indexOf( pathname )), | ||
mime: mime.lookup( fpath ) | ||
}; | ||
return Q.resolve().then(function(){ | ||
return $preprocessor.engine( content , info ); | ||
}) | ||
.then(function( processed ){ | ||
res.setHeader( 'Content-Type' , info.mime ); | ||
res.setHeader( 'Content-Length' , Buffer.byteLength( processed )); | ||
return processed; | ||
}); | ||
}) | ||
.then(function( content ){ | ||
res.end( content ); | ||
}) | ||
.fail( next ); | ||
}; | ||
}; |
{ | ||
"name": "connect-preprocess", | ||
"description": "flexible preprocessor middleware for connect / express.", | ||
"version": "0.1.3", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
@@ -19,14 +19,12 @@ "homepage": "https://github.com/elnarddogg/connect-preprocess", | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/elnarddogg/connect-preprocess/blob/master/LICENSE.txt" | ||
} | ||
], | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
"node": ">= 0.10.40" | ||
}, | ||
"dependencies": { | ||
"mime": "~1.3.4", | ||
"extend": "~2.0.1" | ||
"extend": "~2.0.1", | ||
"fs-extra": "^0.26.2", | ||
"mime": "^1.3.4", | ||
"q": "^1.4.1", | ||
"resolve-path": "^1.3.0" | ||
}, | ||
@@ -33,0 +31,0 @@ "keywords": [ |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
6331
77
0
5
1
+ Addedfs-extra@^0.26.2
+ Addedq@^1.4.1
+ Addedresolve-path@^1.3.0
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addeddepd@1.1.2(transitive)
+ Addedfs-extra@0.26.7(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhttp-errors@1.6.3(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.3(transitive)
+ Addedjsonfile@2.4.0(transitive)
+ Addedklaw@1.3.1(transitive)
+ Addedmime@1.6.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedq@1.5.1(transitive)
+ Addedresolve-path@1.4.0(transitive)
+ Addedrimraf@2.7.1(transitive)
+ Addedsetprototypeof@1.1.0(transitive)
+ Addedstatuses@1.5.0(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedmime@1.3.6(transitive)
Updatedmime@^1.3.4