Switched to hyper express

This commit is contained in:
SamTv12345 2024-08-19 15:22:11 +02:00
parent 3c66e8c5d6
commit fd9b5d0ff3
8 changed files with 84 additions and 67 deletions

View file

@ -18,7 +18,7 @@ const settings = require('../utils/Settings');
const stats = require('../stats')
import util from 'util';
const webaccess = require('./express/webaccess');
import nanoexpress from 'nanoexpress';
import HyperExpress from 'hyper-express';
import SecretRotator from '../security/SecretRotator';
@ -101,7 +101,7 @@ exports.createServer = async () => {
exports.restartServer = async () => {
await closeServer();
const app = nanoexpress(); // New syntax for express v3
const app = new HyperExpress.Server(); // New syntax for express v3
if (settings.ssl) {
console.log('SSL -- enabled');
@ -252,7 +252,10 @@ exports.restartServer = async () => {
socketsEvents.emit('updated');
});
});
await util.promisify(exports.server.listen).bind(exports.server)(settings.port, settings.ip);
app.listen(settings.port, settings.ip, ()=>{
console.log("running")
})
//await util.promisify(exports.server.listen).bind(exports.server)(settings.port, settings.ip);
startTime.setValue(Date.now());
logger.info('HTTP server listening for connections');
};

View file

@ -9,14 +9,14 @@ exports.expressCreateServer = (hook_name:string, args: ArgsExpressType, cb:Funct
exports.app = args.app;
// Handle errors
args.app.use((err:ErrorCaused, req:any, res:any, next:Function) => {
/*args.app.use((req:any, res:any, next:Function) => {
// if an error occurs Connect will pass it down
// through these "error-handling" middleware
// allowing you to respond however you like
res.status(500).send({error: 'Sorry, something bad happened!'});
console.error(err.stack ? err.stack : err.toString());
stats.meter('http500').mark();
});
});*/
return cb();
};

View file

@ -25,7 +25,7 @@ exports.expressCreateServer = (hookName:string, args:ArgsExpressType, cb:Functio
});
// handle export requests
args.app.use('/p/:pad/:rev?/export/:type', limiter);
args.app.use('/p/pad/rev/export/type', limiter);
args.app.get('/p/:pad/:rev?/export/:type', (req:any, res:any, next:Function) => {
(async () => {
const types = ['pdf', 'doc', 'txt', 'html', 'odt', 'etherpad'];
@ -71,7 +71,7 @@ exports.expressCreateServer = (hookName:string, args:ArgsExpressType, cb:Functio
});
// handle import requests
args.app.use('/p/:pad/import', limiter);
args.app.use('/p/pad/import', limiter);
args.app.post('/p/:pad/import', (req:any, res:any, next:Function) => {
(async () => {
// @ts-ignore

View file

@ -121,14 +121,10 @@ const handleLiveReload = async (args: any, padString: string, timeSliderString:
};
args.app.use((req: any, res: any, next: Function) => {
if (req.path.startsWith('/p/') && req.path.split('/').length == 3) {
req.params = {
pad: req.path.split('/')[2]
}
req.padId = req.path.split('/')[2]
routeHandlers['/p/:pad'](req, res);
} else if (req.path.startsWith('/p/') && req.path.split('/').length == 4) {
req.params = {
pad: req.path.split('/')[2]
}
req.padId = req.path.split('/')[2]
routeHandlers['/p/:pad/timeslider'](req, res);
} else if (req.path == "/"){
routeHandlers['/'](req, res);
@ -163,7 +159,7 @@ const handleLiveReload = async (args: any, padString: string, timeSliderString:
setRouteHandler("/p/:pad", (req: any, res: any, next: Function) => {
// The below might break for pads being rewritten
const isReadOnly = !webaccess.userCanModify(req.params.pad, req);
const isReadOnly = !webaccess.userCanModify(req.padId, req);
hooks.callAll('padInitToolbar', {
toolbar,
@ -192,7 +188,7 @@ const handleLiveReload = async (args: any, padString: string, timeSliderString:
setRouteHandler("/p/:pad/timeslider", (req: any, res: any, next: Function) => {
console.log("Reloading pad")
// The below might break for pads being rewritten
const isReadOnly = !webaccess.userCanModify(req.params.pad, req);
const isReadOnly = !webaccess.userCanModify(req.padId, req);
hooks.callAll('padInitToolbar', {
toolbar,
@ -331,7 +327,7 @@ exports.expressCreateServer = async (hookName: string, args: any, cb: Function)
// serve pad.html under /p
args.app.get('/p/:pad', (req: any, res: any, next: Function) => {
// The below might break for pads being rewritten
const isReadOnly = !webaccess.userCanModify(req.params.pad, req);
const isReadOnly = !webaccess.userCanModify(req.padId, req);
hooks.callAll('padInitToolbar', {
toolbar,

View file

@ -34,7 +34,7 @@ exports.expressPreSession = async (hookName:string, {app}:any) => {
// Minify will serve static files compressed (minify enabled). It also has
// file-specific hacks for ace/require-kernel/etc.
app.get('/static/:filename(*)', minify);
app.use('/static', minify);
// serve plugin definitions
// not very static, but served here so that client can do

View file

@ -146,7 +146,8 @@ const compatPaths = {
* @param res the Express response
*/
const _minify = async (req:any, res:any) => {
let filename = req.params.filename;
let filename = req.path.split('/static/')[1]
try {
filename = sanitizePathname(filename);
} catch (err) {
@ -211,23 +212,26 @@ const _minify = async (req:any, res:any) => {
}
if (!exists) {
res.writeHead(404, {});
res.status(404)
//res.writeHead(404, {});
res.end();
} else if (new Date(req.headers['if-modified-since']) >= date) {
res.writeHead(304, {});
res.status(304)
res.end();
} else if (req.method === 'HEAD') {
res.header('Content-Type', contentType);
res.writeHead(200, {});
res.status(200)
res.end();
} else if (req.method === 'GET') {
const content = await getFileCompressed(filename, contentType as string);
res.header('Content-Type', contentType);
res.writeHead(200, {});
res.status(200)
//res.writeHead(200, {});
res.write(content);
res.end();
} else {
res.writeHead(405, {allow: 'HEAD, GET'});
//res.writeHead(405, {allow: 'HEAD, GET'});
res.status(405)
res.end();
}
};

View file

@ -56,7 +56,7 @@
"lru-cache": "^11.0.0",
"measured-core": "^2.0.0",
"mime-types": "^2.1.35",
"nanoexpress": "^6.4.4",
"hyper-express": "^6.16.4",
"oidc-provider": "^8.5.1",
"openapi-backend": "^5.10.6",
"proxy-addr": "^2.0.7",