fix conditional livereload, warn about watching too many files

This commit is contained in:
AJ ONeal 2016-10-07 13:12:46 -06:00
parent cb898418f2
commit 56244d4d84
3 changed files with 40 additions and 29 deletions

50
app.js
View File

@ -8,6 +8,26 @@ module.exports = function (opts) {
var index = serveIndex(opts.public); var index = serveIndex(opts.public);
var content = opts.content; var content = opts.content;
function _reloadWrite(data, enc, cb) {
/*jshint validthis: true */
if (this.headersSent) {
this.__write(data, enc, cb);
return;
}
if (!/html/i.test(this.getHeader('Content-Type'))) {
this.__write(data, enc, cb);
return;
}
if (this.getHeader('Content-Length')) {
this.setHeader('Content-Length', this.getHeader('Content-Length') + this.__my_addLen);
}
this.__write(this.__my_livereload);
this.__write(data, enc, cb);
}
return function (req, res) { return function (req, res) {
if (content && '/' === req.url) { if (content && '/' === req.url) {
// res.setHeader('Content-Type', 'application/octet-stream'); // res.setHeader('Content-Type', 'application/octet-stream');
@ -15,36 +35,18 @@ module.exports = function (opts) {
return; return;
} }
var done = finalhandler(req, res); var done = finalhandler(req, res);
var livereload = '';
var addLen = 0;
if (opts.livereload) { if (opts.livereload) {
livereload = '<script src="//' res.__my_livereload = '<script src="//'
+ (res.getHeader('Host') || opts.servername).split(':')[0] + (res.getHeader('Host') || opts.servername).split(':')[0]
+ ':35729/livereload.js?snipver=1"></script>'; + ':35729/livereload.js?snipver=1"></script>';
addLen = livereload.length; res.__my_addLen = res.__my_livereload.length;
// TODO modify prototype instead of each instance?
res.__write = res.write;
res.write = _reloadWrite;
} }
res.__write = res.write;
res.write = function (data, enc, cb) {
if (this.headersSent) {
this.__write(data, enc, cb);
return;
}
if (!/html/i.test(this.getHeader('Content-Type'))) {
this.__write(data, enc, cb);
return;
}
if (this.getHeader('Content-Length')) {
this.setHeader('Content-Length', this.getHeader('Content-Length') + addLen);
}
this.__write(livereload);
this.__write(data, enc, cb);
};
function serveStatic() { function serveStatic() {
serve(req, res, function (err) { serve(req, res, function (err) {
if (err) { return done(err); } if (err) { return done(err); }

View File

@ -41,7 +41,7 @@
"finalhandler": "^0.4.0", "finalhandler": "^0.4.0",
"httpolyglot": "^0.1.1", "httpolyglot": "^0.1.1",
"ipify": "^1.1.0", "ipify": "^1.1.0",
"livereload": "^0.4.0", "livereload": "^0.5.0",
"localhost.daplie.com-certificates": "^1.2.0", "localhost.daplie.com-certificates": "^1.2.0",
"minimist": "^1.1.1", "minimist": "^1.1.1",
"redirect-https": "^1.1.0", "redirect-https": "^1.1.0",

View File

@ -9,6 +9,7 @@ var fs = require('fs');
var path = require('path'); var path = require('path');
var httpPort = 80; var httpPort = 80;
var httpsPort = 443; var httpsPort = 443;
var lrPort = 35729;
var portFallback = 8443; var portFallback = 8443;
var insecurePortFallback = 4080; var insecurePortFallback = 4080;
@ -77,11 +78,19 @@ function createServer(port, pubdir, content, opts) {
server.listen(port, function () { server.listen(port, function () {
opts.port = port; opts.port = port;
opts.lrPort = 35729; if (opts.livereload) {
var livereload = require('livereload'); opts.lrPort = opts.lrPort || lrPort;
var server2 = livereload.createServer({ https: opts.httpsOptions, port: opts.lrPort }); var livereload = require('livereload');
var server2 = livereload.createServer({
https: opts.httpsOptions
, port: opts.lrPort
, exclusions: [ '.hg', '.git', '.svn', 'node_modules' ]
});
server2.watch(pubdir); console.info("[livereload] watching " + pubdir);
console.warn("WARNING: If CPU usage spikes to 100% it's because too many files are being watched");
server2.watch(pubdir);
}
if ('false' !== opts.insecurePort && httpPort !== opts.insecurePort) { if ('false' !== opts.insecurePort && httpPort !== opts.insecurePort) {
return createInsecureServer(opts.insecurePort, pubdir, opts).then(resolve); return createInsecureServer(opts.insecurePort, pubdir, opts).then(resolve);