From 33df1ffef467314274f09ab36570ccb4cbe8d133 Mon Sep 17 00:00:00 2001 From: Martin Torp Date: Fri, 6 Mar 2020 09:29:49 +0100 Subject: [PATCH] update express to version 4 --- lib/subservers.js | 10 +++++----- life_star.js | 42 ++++++++++++++++++++++-------------------- package.json | 7 +++++-- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/lib/subservers.js b/lib/subservers.js index ebcfec9..8d96fc0 100644 --- a/lib/subservers.js +++ b/lib/subservers.js @@ -46,13 +46,13 @@ function getAdditionalSubservers(subserverHandler) { function runFuncAndRecordNewExpressRoutes(app, func, context) { var method, oldRoutes = {}; - for (method in app.routes) { oldRoutes[method] = [].concat(app.routes[method]); } + for (method in ['post', 'get', 'delete', 'put'].map(rt => [rt, app._router.stack.map(r => r.route).filter(r => r && r.methods[rt])]).reduce((acc, elem) => {acc[elem[0]] = elem[1]; return acc}, {})) { oldRoutes[method] = [].concat(['post', 'get', 'delete', 'put'].map(rt => [rt, app._router.stack.map(r => r.route).filter(r => r && r.methods[rt])]).reduce((acc, elem) => {acc[elem[0]] = elem[1]; return acc}, {})[method]); } // 2) run the function func.call(context); // 3) find new routes and remember them has belonging to this subserver var newRoutes = []; - for (method in app.routes) { - app.routes[method].forEach(function(route) { + for (method in ['post', 'get', 'delete', 'put'].map(rt => [rt, app._router.stack.map(r => r.route).filter(r => r && r.methods[rt])]).reduce((acc, elem) => {acc[elem[0]] = elem[1]; return acc}, {})) { + ['post', 'get', 'delete', 'put'].map(rt => [rt, app._router.stack.map(r => r.route).filter(r => r && r.methods[rt])]).reduce((acc, elem) => {acc[elem[0]] = elem[1]; return acc}, {})[method].forEach(function(route) { if (oldRoutes[method].indexOf(route) === -1) newRoutes.push(route); }); } @@ -61,7 +61,7 @@ function runFuncAndRecordNewExpressRoutes(app, func, context) { function removeRouteFromExpressApp(app, route) { if (!app) return false; - var routes = app.routes[route.method], + var routes = ['post', 'get', 'delete', 'put'].map(rt => [rt, app._router.stack.map(r => r.route).filter(r => r && r.methods[rt])]).reduce((acc, elem) => {acc[elem[0]] = elem[1]; return acc}, {})[route.method], idx = routes.indexOf(route); if (idx === -1) return false; routes.splice(idx, 1); @@ -102,7 +102,7 @@ Subserver.prototype.start = function(app) { self.myRegisteredRoutes = newRoutes; // push subserver routes at the start of the route list so they can match newRoutes.reverse().forEach(function(route) { - var routes = app.routes[route.method]; + var routes = ['post', 'get', 'delete', 'put'].map(rt => [rt, app._router.stack.map(r => r.route).filter(r => r && r.methods[rt])]).reduce((acc, elem) => {acc[elem[0]] = elem[1]; return acc}, {})[route.method]; routes.splice(routes.indexOf(route), 1); routes.unshift(route); }); diff --git a/life_star.js b/life_star.js index 8b20c16..a7e7462 100644 --- a/life_star.js +++ b/life_star.js @@ -1,3 +1,6 @@ +const bodyParser = require('body-parser'); +const cookieParser = require('cookie-parser'); +const cookieSession = require('cookie-session'); /*global require, module*/ var lang = require('lively.lang'), express = require('express'), @@ -96,23 +99,22 @@ var serverSetup = module.exports = function(config, thenDo) { function createServer(next) { if (config.enableSSL) { - var https = require('https'), - options = { - // Specify the key and certificate file - key: fs.readFileSync(config.sslServerKey), - cert: fs.readFileSync(config.sslServerCert), - // Specify the Certificate Authority certificate - ca: fs.readFileSync(config.sslCACert), - - // This is where the magic happens in Node. All previous steps simply - // setup SSL (except the CA). By requesting the client provide a - // certificate, we are essentially authenticating the user. - requestCert: config.enableSSLClientAuth, - - // If specified as "true", no unauthenticated traffic will make it to - // the route specified. - rejectUnauthorized: config.enableSSLClientAuth - }; + var options = { + // Specify the key and certificate file + key: fs.readFileSync(config.sslServerKey), + cert: fs.readFileSync(config.sslServerCert), + // Specify the Certificate Authority certificate + ca: fs.readFileSync(config.sslCACert), + + // This is where the magic happens in Node. All previous steps simply + // setup SSL (except the CA). By requesting the client provide a + // certificate, we are essentially authenticating the user. + requestCert: config.enableSSLClientAuth, + + // If specified as "true", no unauthenticated traffic will make it to + // the route specified. + rejectUnauthorized: config.enableSSLClientAuth + }; server = require('https').createServer(options, app); } else { server = require('http').createServer(app); @@ -209,17 +211,17 @@ var serverSetup = module.exports = function(config, thenDo) { } function setupBodyParser(next) { - app.use(express.bodyParser({limit: '150mb'})); + app.use(bodyParser({limit: '150mb'})); next(); } function setupCookies(next) { - app.use(express.cookieParser()); + app.use(cookieParser()); // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- // store auth information into a cookie // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- - app.use(express.cookieSession({ + app.use(cookieSession({ key: 'livelykernel-sign-on', secret: 'foo', proxy: config.behindProxy, diff --git a/package.json b/package.json index ba5ad64..22c4057 100644 --- a/package.json +++ b/package.json @@ -9,12 +9,15 @@ "description": "Another web server for Lively", "dependencies": { "async": "~0.9", - "express": "~3", + "express": "^4.0.0", "lively-davfs": "^0.4.4", "log4js": "0.4.1", "morgan": ">=1.2.0", "request": "~2.2", - "lively.lang": "^0.7" + "lively.lang": "^0.7", + "body-parser": "^1.19.0", + "cookie-parser": "^1.4.4", + "cookie-session": "^1.4.0" }, "devDependencies": { "nodeunit": ""