Permalink
Browse files

jslint OK on lib file

modified:   lib/connect-stricten.js
  • Loading branch information...
1 parent 82aa54b commit 737e48c923a860841c83c102f4922b7b793d2294 @yukinying committed Nov 15, 2012
Showing with 120 additions and 103 deletions.
  1. +3 −2 examples/proxy-express.js
  2. +117 −101 lib/connect-stricten.js
@@ -16,14 +16,15 @@ var proxyware = function( req, res ) {
req.headers["Accept-Encoding"] = '';
proxy.proxyRequest(req, res, {
host: uri.hostname,
- port: uri.port || 80
+ port: (uri.port || 80),
});
};
-// app.use(express.logger());
+app.use(express.logger());
app.use(connect.responseTime());
app.use(strict.stricten() );
app.use(strict.beautifier());
app.all('/*',proxyware);
app.listen(8080);
+
View
@@ -1,203 +1,219 @@
-exports.filterJS = function( req, res ) {
- return (
- req.accepts('json') == 'json'
- && res.statusCode == 200
- ) ;
-}
-
-exports.filterHTML = function( req, res ) {
- return (
- req.accepts('html') == 'html'
- && res.statusCode == 200
- ) ;
-}
-
-exports.beautify = function( body, p1, p2, p3 ) {
- if( p3 ) body = p2;
- try {
- var U = require("uglify-js2");
- var ostream = U.OutputStream({beautify: true, source_map: null} );
- var toplevel = U.parse(body);
+/*jslint nomen: true, regexp: true, white: true, node: true, indent: 2, maxerr: 1000 */
+/*jshint indent: 2 */
+
+"use strict";
+
+exports.filterJS = function (req, res) {
+ return ((req.accepts('json') === 'json' || /\.js/.test(req.url)) && res.statusCode === 200);
+ };
+
+exports.filterHTML = function (req, res) {
+ return ((req.accepts('html') === 'html' || /\.html/.test(req.url)) && res.statusCode === 200);
+ };
+
+exports.beautify = function (body, p1, p2, p3) {
+ if(p3) {
+ if(/<script>/.test(p1) || /text.javascript/.test(p1)) {
+ body = p2;
+ }
+ return p1 + p2 + p3;
+ }
+ try {
+ var U = require("uglify-js2")
+ , ostream = U.OutputStream({beautify: true, source_map: null})
+ , toplevel = U.parse(body)
+ ;
toplevel.print(ostream);
- if( p3 )
- return p1 + "\n" + ostream.toString() + "\n" + p3 ;
- else
- return ostream.toString();
+ if(p3) {
+ return p1 + "\n" + ostream.toString() + "\n" + p3 ;
+ }
+ return ostream.toString();
} catch (e) {
- console.log(body);
- console.log(e)
- }
+ console.log(body);
+ console.log(e);
+ }
+ if(p3) { return p1 + p2 + p3; }
return body;
-}
+};
-exports.strictening = function( body, p1, p2, p3 ) {
- if( p3 )
- return p1 + "\n" + '"use strict";' + "\n" + p2 + "\n" + p3 ;
- else
- return '"use strict";' + "\n" + body;
-}
+exports.strictening = function (body, p1, p2, p3) {
+ if(p3) {
+ if(/<script>/.test(p1) || /text.javascript/.test(p1)) {
+ return p1 + "\n" + '"use strict";' + "\n" + p2 + "\n" + p3 ;
+ }
+ return p1+p2+p3; /* type is not javascript */
+ }
+ return '"use strict";' + "\n" + body;
+};
-exports.beautifier = function(options) {
+exports.beautifier = function (opt) {
- var options = options || {} ;
+ var options = opt || {} ;
return function (req, res, next) {
- var
- write = res.write
+ var
+ write = res.write
, writeHead = res.writeHead
, end = res.end
- , url = req.originalUrl
+ , url = req.originalUrl
, body = ''
, isJS = exports.filterJS(req,res)
, isHTML = exports.filterHTML(req,res)
;
- res.on('header', function( ) {
- if( isJS || isHTML ) {
- console.log('Url is ' + req.url );
- console.log('Content Length is ' + res.get('Content-Length'));
- console.log('Content Type is ' + res.get('Content-Type'));
-
+ res.on('header', function () {
+ if(isJS || isHTML) {
res.removeHeader('Content-Length');
res.removeHeader('last-modified');
}
- if( res.get('Content-Type') ) {
+ if(res.get && res.get('Content-Type')) {
isJS = isJS && /javascript|json/i.test(res.get('Content-Type'));
isHTML = isHTML && /html/i.test(res.get('Content-Type'));
}
});
- // From https://github.com/nateps/connect/blob/4025c2fbd2f53d4a8fe5608055514eee0d696ca1/lib/patch.js
+ // From https://github.com/nateps/connect/blob/4025c2fbd2f53d4a8fe5608055514eee0d696ca1/lib/patch.js
// Until connect has taken the patch, we could only patch it here.
- res.writeHead = function(statusCode){
- var reasonPhrase, headers, headerIndex;
- if (typeof arguments[1] == 'string') {
- reasonPhrase = arguments[1];
+ res.writeHead = function (statusCode, oreason){
+ var reasonPhrase, headers, headerIndex,
+ name;
+ if (typeof oreason === 'string') {
+ reasonPhrase = oreason;
headerIndex = 2;
} else {
headerIndex = 1;
}
headers = arguments[headerIndex];
if (headers) {
- for (var name in headers) {
- this.setHeader(name, headers[name]);
+ for (name in headers) {
+ if (headers.hasOwnProperty(name)) {
+ this.setHeader(name, headers[name]);
+ }
}
}
- if (!this._emittedHeader) this.emit('header');
+ if (!this._emittedHeader) { this.emit('header'); }
this._emittedHeader = true;
return writeHead.call(this, statusCode, reasonPhrase);
};
- res.write = function( chunk, encoding ) {
- if( isJS || isHTML ) {
- body += chunk; }
- else {
- write.apply(res,arguments);
+ res.write = function (chunk, encoding) {
+ if(isJS || isHTML) {
+ body += chunk;
+ // console.log('appending chunk for beautifier');
+ } else {
+ write.apply(res,arguments);
}
- }
+ };
- res.end = function(chunk,encoding) {
- if( isJS && !isHTML ) {
- if (chunk) body += chunk;
+ res.end = function (chunk,encoding) {
+ if(isJS && !isHTML) {
+ if (chunk) { body += chunk; }
console.log('beautifying (js) ' + url);
- var ubody = exports.beautify( body ) ;
+ var ubody = exports.beautify(body) ;
write.call(res,ubody,encoding);
end.call(res);
- } else if ( isHTML ) {
- if (chunk) body += chunk;
+ } else if (isHTML) {
+ if (chunk) { body += chunk; }
console.log('beautifying (html) ' + url);
- body = body.replace(/(<script[^>]*>)((?:.|\s)*?)(<\/script>)/mig, exports.beautify ) ;
+ body = body.replace(/(<script[^>]*>)((?:.|\s)*?)(<\/script>)/mig, exports.beautify) ;
write.call(res,body,encoding);
end.call(res);
-
} else {
- end.apply(res,arguments)
+ end.apply(res,arguments);
}
- }
+ };
next();
};
-}
+};
-exports.stricten = function(options) {
+exports.stricten = function (opt) {
- var options = options || {} ;
+ var options = opt || {} ;
return function (req, res, next) {
var
- called = false
+ called = false
, write = res.write
, writeHead = res.writeHead
, body = ''
, end = res.end
- , url = req.originalUrl
- , isJS = exports.filterJS( req, res)
- , isHTML = exports.filterHTML( req, res)
+ , url = req.originalUrl
+ , isJS = exports.filterJS(req, res)
+ , isHTML = exports.filterHTML(req, res)
;
// Normal case
- res.on('header', function( ) {
- if( isJS || isHTML ) {
+ res.on('header', function () {
+ if(isJS || isHTML) {
res.removeHeader('Content-Length');
res.removeHeader('last-modified'); // do not cache modification on-the-fly
}
- if( res.get('Content-Type') ) {
+ if(res.get && res.get('Content-Type')) {
isJS = isJS && /javascript|json/i.test(res.get('Content-Type'));
isHTML = isHTML && /html/i.test(res.get('Content-Type'));
}
});
- // From https://github.com/nateps/connect/blob/4025c2fbd2f53d4a8fe5608055514eee0d696ca1/lib/patch.js
+ // From https://github.com/nateps/connect/blob/4025c2fbd2f53d4a8fe5608055514eee0d696ca1/lib/patch.js
// Until connect has taken the patch, we could only patch it here.
- res.writeHead = function(statusCode){
- var reasonPhrase, headers, headerIndex;
- if (typeof arguments[1] == 'string') {
- reasonPhrase = arguments[1];
+ res.writeHead = function (statusCode, oreason){
+ var reasonPhrase, headers, headerIndex,
+ name;
+ if (typeof oreason === 'string') {
+ reasonPhrase = oreason;
headerIndex = 2;
} else {
headerIndex = 1;
}
headers = arguments[headerIndex];
if (headers) {
- for (var name in headers) {
- this.setHeader(name, headers[name]);
+ for (name in headers) {
+ if (headers.hasOwnProperty(name)) {
+ this.setHeader(name, headers[name]);
+ }
}
}
- if (!this._emittedHeader) this.emit('header');
+ if (!this._emittedHeader) { this.emit('header'); }
this._emittedHeader = true;
return writeHead.call(this, statusCode, reasonPhrase);
};
- res.write = function( chunk, encoding ) {
+ res.write = function (chunk, encoding) {
- if( isJS && !isHTML ) {
+ if(!called && isJS && !isHTML) {
called = true;
console.log('strictening (js) ' + url);
write.call(res, exports.strictening(chunk), encoding);
- } else if ( isHTML ) {
- if (chunk) body += chunk;
+ } else if (isHTML) {
+ // console.log('appending chunk for stricten (html) ');
+ if (chunk) { body += chunk; }
} else {
write.apply(res,arguments);
}
- }
-
+ };
- res.end = function(chunk,encoding) {
- if ( isHTML ) {
- if (chunk) body += chunk;
+ res.end = function (chunk,encoding) {
+ if(!called && isJS && !isHTML) {
+ console.log('strictening (js) ' + url);
+ write.call(res, exports.strictening(chunk), encoding);
+ end.call(res);
+ } else if (isHTML) {
+ if (chunk) { body += chunk; }
console.log('strictening (html) ' + url);
- body = body.replace(/(<script[^>]*>)((?:.|\s)*?)(<\/script>)/mig, exports.strictening ) ;
+ // TODO relax this so we don't need to capture the body.
+ body = body.replace(/(<script[^>]*>)((?:.|\s)*?)(<\/script>)/mig, exports.strictening) ;
write.call(res,body,encoding);
end.call(res);
} else {
- end.apply(res,arguments)
+ end.apply(res,arguments);
}
- }
+ };
next();
};
-}
+};

0 comments on commit 737e48c

Please sign in to comment.