Skip to content

Commit

Permalink
Merge pull request #129 from donejs/revert-128-h2two
Browse files Browse the repository at this point in the history
Revert "Use http2 module for https/http2 support"
  • Loading branch information
matthewp authored Sep 7, 2018
2 parents 0765563 + 70cdee3 commit 4688d0b
Show file tree
Hide file tree
Showing 31 changed files with 468 additions and 1,063 deletions.
1 change: 0 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
},
"curly": true,
"eqeqeq": true,
"esversion": 6,
"freeze": true,
"indent": 2,
"latedef": false,
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ language: node_js
script: npm test
node_js:
- "8"
- "10"
- "9"
3 changes: 2 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
environment:
matrix:
# node.js
- nodejs_version: "6"
- nodejs_version: "7"
- nodejs_version: "8"
- nodejs_version: "10"
# Install scripts. (runs after repo cloning)
install:
# Get the latest stable version of Node.js or io.js
Expand Down
51 changes: 0 additions & 51 deletions lib/app.js

This file was deleted.

63 changes: 55 additions & 8 deletions lib/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,54 @@ program.version(pkg.version)
exports.program = program;

exports.run = function(){
const serve = require("../index");
let exec = require("child_process").exec;
var makeServer = require("../index");
var exec = require("child_process").exec;
var startServer = function(app){
var servers = [];
// If using TLS, set up an HTTP2 server with automatic
// http->https forwarding.
if(options.key && options.cert) {
var net = require("net");
var spdy = require("donejs-spdy");
port = Number(port);
var httpPort = port + 1;
var httpsPort = httpPort + 1;
var server = spdy.createServer({
key: fs.readFileSync(options.key),
cert: fs.readFileSync(options.cert),
spdy: {
protocols: ['h2', 'http/1.1']
}
}, app);
server.listen(httpsPort);
servers.push(server);

server = require("http").createServer(function(req, res){
var host = req.headers.host;
res.writeHead(301, { "Location": "https://" + host + req.url });
res.end();
});
server.listen(httpPort);

// This is a TCP server that forwards to the correct port for
// http or https
net.createServer(function(conn){
conn.once("data", function (buf) {
// A TLS handshake record starts with byte 22.
var address = (buf[0] === 22) ? httpsPort : httpPort;
var proxy = net.createConnection(address, function (){
proxy.write(buf);
conn.pipe(proxy).pipe(conn);
});
});
}).listen(port);

servers.push(server);
} else {
servers.push(app.listen(port));
}
return servers;
};

var options = {
path: program.args[0] ? path.join(process.cwd(), program.args[0]) : process.cwd(),
Expand Down Expand Up @@ -84,10 +130,11 @@ exports.run = function(){
}
}

let port = program.port || process.env.PORT || 3030;
let mainServer = serve(port, options);
var app = makeServer(options);
var port = program.port || process.env.PORT || 3030;
var servers = startServer(app);

mainServer.on('error', function(e) {
servers[0].on('error', function(e) {
if(e.code === 'EADDRINUSE') {
console.error('ERROR: Can not start done-serve on port ' + port +
'.\nAnother application is already using it.');
Expand All @@ -97,8 +144,8 @@ exports.run = function(){
}
});

mainServer.on('listening', function() {
var address = mainServer.address();
servers[0].on('listening', function() {
var address = servers[0].address();
var url = 'http://' + (address.address === '::' ?
'localhost' : address.address) + ':' + port;

Expand All @@ -109,5 +156,5 @@ exports.run = function(){
console.log('done-serve starting on ' + url);
});

return mainServer;
return servers[0];
};
15 changes: 0 additions & 15 deletions lib/http1.js

This file was deleted.

101 changes: 95 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,99 @@
var path = require('path');

module.exports = function(port, options) {
let createApp = require("./app");
let createServers = require("./servers").create;
var compression = require('compression');
var errorFormat = require('donejs-error-format');
var express = require('express');
var serveIndex = require('serve-index');
var debug = require('debug')('done-serve');
var middleware = require('done-ssr-middleware');
var fs = require('fs');

let app = createApp(options);
let [mainServer] = createServers(port, options, app);
var proxy = require('./proxy');

return mainServer;
function notFoundHandler(req, res, next) {
res.status(404);
next(new Error('File not found'));
}

module.exports = function (options) {
var app = express()
.use(compression());

debug('Initializing done-serve application', options);

if (options.configure) {
options.configure(app);
}

if (options.proxy) {
proxy(app, options);
}

app.use(express.static(path.join(options.path)));

if(!options.static) {
var steal = {
config: path.join(options.path, 'package.json') + '!npm',
liveReload: options.liveReload
};

if(options.main) {
steal.main = options.main;
}

var mw = middleware(steal, options);

debug('Registering done-ssr-middleware');
app.use(mw);

// Error handler
app.use(function(error, request, response, next) {
var parts = errorFormat.extract(error);
var errorOptions = {
liveReload: !!options.liveReload
};
var html = errorFormat.html(parts, errorOptions);

if(options.logErrors !== false) {
console.log('\033c');
console.error(error.stack);
}

response.status(500).type('html').end(html);
});
} else {
// Unobtrusively handle pushstate routing when SSR is turned off.
app.get('*', function (req, res, next) {
var urlParts = path.parse(req.url);
var isPushstateRoute = !urlParts.ext || urlParts.name.includes('?');
if (isPushstateRoute) {
var env = process.env.NODE_ENV || 'development';
var htmlPath = path.join(options.path, './' + env + '.html');
if (!fs.existsSync(htmlPath)) {
htmlPath = path.join(options.path, './production.html');
}
if (fs.existsSync(htmlPath)) {
return res.sendFile(htmlPath);
}
}
return next();
});

app.use(serveIndex(path.join(options.path), { icons: true }));
app.use(notFoundHandler);

if(options.errorPage) {
var filename = path.join(process.cwd(), options.errorPage);

debug('Registering pushState file', filename);

app.use(function(err, req, res, next) {
debug('Pushstate error handler', filename);
res.status(200);
res.sendFile(filename);
});
}
}

return app;
};
24 changes: 0 additions & 24 deletions lib/middleware/compression.js

This file was deleted.

23 changes: 0 additions & 23 deletions lib/middleware/error.js

This file was deleted.

7 changes: 0 additions & 7 deletions lib/middleware/not-found.js

This file was deleted.

Loading

0 comments on commit 4688d0b

Please sign in to comment.