New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

connect-preprocess

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

connect-preprocess - npm Package Compare versions

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": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc