From cb898418f2e370de533cb83b839eefb25af8cf1f Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 7 Oct 2016 12:00:21 -0600 Subject: [PATCH 1/2] cache secureContext --- serve.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/serve.js b/serve.js index 37834b0..f0c88bf 100755 --- a/serve.js +++ b/serve.js @@ -132,6 +132,7 @@ function run() { // letsencrypt var cert = require('localhost.daplie.com-certificates').merge({}); + var secureContext; var opts = { agreeTos: argv.agreeTos || argv['agree-tos'] @@ -147,7 +148,10 @@ function run() { var peerCa; opts.httpsOptions.SNICallback = function (servername, cb) { - cb(null, tls.createSecureContext(opts.httpsOptions)); + if (!secureContext) { + secureContext = tls.createSecureContext(opts.httpsOptions); + } + cb(null, secureContext); return; }; From 56244d4d84a06e8b9cc2d990d71b4ff91b8ae7b6 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 7 Oct 2016 13:12:46 -0600 Subject: [PATCH 2/2] fix conditional livereload, warn about watching too many files --- app.js | 50 ++++++++++++++++++++++++++------------------------ package.json | 2 +- serve.js | 17 +++++++++++++---- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/app.js b/app.js index bd05672..db54571 100644 --- a/app.js +++ b/app.js @@ -8,6 +8,26 @@ module.exports = function (opts) { var index = serveIndex(opts.public); 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) { if (content && '/' === req.url) { // res.setHeader('Content-Type', 'application/octet-stream'); @@ -15,36 +35,18 @@ module.exports = function (opts) { return; } var done = finalhandler(req, res); - var livereload = ''; - var addLen = 0; if (opts.livereload) { - livereload = ''; - 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() { serve(req, res, function (err) { if (err) { return done(err); } diff --git a/package.json b/package.json index d14126f..18cfef8 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "finalhandler": "^0.4.0", "httpolyglot": "^0.1.1", "ipify": "^1.1.0", - "livereload": "^0.4.0", + "livereload": "^0.5.0", "localhost.daplie.com-certificates": "^1.2.0", "minimist": "^1.1.1", "redirect-https": "^1.1.0", diff --git a/serve.js b/serve.js index f0c88bf..eb83136 100755 --- a/serve.js +++ b/serve.js @@ -9,6 +9,7 @@ var fs = require('fs'); var path = require('path'); var httpPort = 80; var httpsPort = 443; +var lrPort = 35729; var portFallback = 8443; var insecurePortFallback = 4080; @@ -77,11 +78,19 @@ function createServer(port, pubdir, content, opts) { server.listen(port, function () { opts.port = port; - opts.lrPort = 35729; - var livereload = require('livereload'); - var server2 = livereload.createServer({ https: opts.httpsOptions, port: opts.lrPort }); + if (opts.livereload) { + opts.lrPort = opts.lrPort || 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) { return createInsecureServer(opts.insecurePort, pubdir, opts).then(resolve);