Skip to content

Commit

Permalink
add msg filter
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Sep 5, 2023
1 parent 48259f7 commit fd25c77
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions lib/servez.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function fileExists(filename) {
const noopLogger = {
log() {},
error() {},
filter() { return true; },
c: new Proxy({}, {
get(target, name) {
return s => s;
Expand All @@ -96,14 +97,32 @@ const noopLogger = {
};

class Servez extends EventEmitter {
static MsgType = {
Intro: 1,
Error: 2,
Info: 3,
};

constructor(settings) {
super();
const root = settings.root;
const local = settings.local;
const hostname = local ? '127.0.0.1' : undefined;
const logger = settings.logger || noopLogger;
const log = logger.log.bind(logger);
const error = logger.error.bind(logger);
const filterFn = msgType => logger.filter ? logger.filter(msgType) : true;
const logFiltered = (msgType, ...args) => {
if (filterFn(msgType)) {
logger.log(...args);
}
};
const error = (...args) => {
logger.error(...args);
};
const errorFiltered = (msgType, ...args) => {
if (filterFn(msgType)) {
error(...args);
}
};
const c = logger.c || noopLogger.c;

const app = express();
Expand Down Expand Up @@ -179,7 +198,7 @@ class Servez extends EventEmitter {
}

app.use((req, res, next) => {
log(`${req.method} ${c.cyan(req.originalUrl)}`);
logFiltered(Servez.MsgType.Info, `${req.method} ${c.cyan(req.originalUrl)}`);
next();
});

Expand Down Expand Up @@ -207,13 +226,13 @@ class Servez extends EventEmitter {

function localErrorHandler(err, req, res, next) {
debug(`ERROR: ${req.method} ${req.url} ${err}`);
error(`ERROR: ${req.method} ${c.cyan(req.url)} ${err}`);
errorFiltered(Servez.MsgType.Error, `ERROR: ${req.method} ${c.cyan(req.url)} ${err}`);
res.status(500).send(`<pre>${err}</pre>`);
}

function nonErrorLocalErrorHandler(req, res, next) {
debug(`ERROR: ${req.method} ${req.url} [404]`);
error(`ERROR: ${req.method} ${c.cyan(req.url)} [404: does not exist]`);
errorFiltered(Servez.MsgType.Info, `ERROR: ${req.method} ${c.cyan(req.url)} [404: does not exist]`);
res.status(404).send(`<pre>ERROR 404: No such path ${req.path}</pre>`);
}

Expand All @@ -224,7 +243,7 @@ class Servez extends EventEmitter {
const port = await getFreePort(settings.port);
if (!settings.scan && port !== settings.port) {
const msg = `ERROR: port $${port} in use`;
error(msg);
errorFiltered(Servez.MsgType.Error, msg);
this.emit('error', msg);
return;
}
Expand All @@ -245,21 +264,21 @@ class Servez extends EventEmitter {
server = http.createServer(app);
}
server.on('error', (e) => {
error('ERROR:', e.message);
errorFiltered(Servez.MsgType.Error, 'ERROR:', e.message);
this.emit('error', e);
});
server.on('listening', () => {
started = true;
log(c.yellow(`server started on ${hostname || '::'}${port} for path: ${c.cyan(root)}`));
log(c.yellow('available on:'));
logFiltered(Servez.MsgType.Intro, c.yellow(`server started on ${hostname || '::'}${port} for path: ${c.cyan(root)}`));
logFiltered(Servez.MsgType.Intro, c.yellow('available on:'));
const protocol = settings.ssl ? 'https://' : 'http://';
log(` ${protocol}localhost:${port}`);
logFiltered(Servez.MsgType.Intro, ` ${protocol}localhost:${port}`);
if (!hostname) {
const iFaces = os.networkInterfaces();
Object.keys(iFaces).forEach((dev) => {
iFaces[dev].forEach((details) => {
if (details.family === 'IPv4') {
log(` ${protocol}${details.address}:${port}`);
logFiltered(Servez.MsgType.Intro, ` ${protocol}${details.address}:${port}`);
this.emit('host', {
root: `${protocol}${details.address}:${port}/`,
});
Expand All @@ -280,7 +299,7 @@ class Servez extends EventEmitter {
server.listen(port, hostname);
} catch (e) {
debug('error starting server');
error('ERROR:', e, e.message, e.stack);
errorFiltered(Servez.MsgType.Error, 'ERROR:', e, e.message, e.stack);
}

this.close = function() {
Expand Down

0 comments on commit fd25c77

Please sign in to comment.