This commit is contained in:
AJ ONeal 2016-10-07 13:17:04 -06:00
commit b3baa626aa
3 changed files with 46 additions and 31 deletions

46
app.js
View File

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

View File

@ -45,7 +45,7 @@
"le-challenge-dns": "^2.0.1", "le-challenge-dns": "^2.0.1",
"le-challenge-fs": "^2.0.5", "le-challenge-fs": "^2.0.5",
"letsencrypt-express": "^2.0.2", "letsencrypt-express": "^2.0.2",
"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

@ -10,6 +10,7 @@ var path = require('path');
var DDNS = require('ddns-cli'); var DDNS = require('ddns-cli');
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;
@ -139,11 +140,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) {
opts.lrPort = opts.lrPort || lrPort;
var livereload = require('livereload'); var livereload = require('livereload');
var server2 = livereload.createServer({ https: opts.httpsOptions, port: opts.lrPort }); var server2 = livereload.createServer({
https: opts.httpsOptions
, port: opts.lrPort
, exclusions: [ '.hg', '.git', '.svn', 'node_modules' ]
});
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); 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);
@ -195,6 +204,7 @@ function run() {
// letsencrypt // letsencrypt
var cert = require('localhost.daplie.com-certificates').merge({}); var cert = require('localhost.daplie.com-certificates').merge({});
var secureContext;
var opts = { var opts = {
agreeTos: argv.agreeTos || argv['agree-tos'] agreeTos: argv.agreeTos || argv['agree-tos']
@ -211,7 +221,10 @@ function run() {
var p; var p;
opts.httpsOptions.SNICallback = function (servername, cb) { opts.httpsOptions.SNICallback = function (servername, cb) {
cb(null, tls.createSecureContext(opts.httpsOptions)); if (!secureContext) {
secureContext = tls.createSecureContext(opts.httpsOptions);
}
cb(null, secureContext);
return; return;
}; };