Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 43 additions & 43 deletions bin/http-server
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

'use strict';

var chalk = require('chalk'),
var pc = require('picocolors'),
os = require('os'),
httpServer = require('../lib/http-server'),
portfinder = require('portfinder'),
Expand Down Expand Up @@ -112,10 +112,10 @@ if (nopanic){
process.on('error', (e)=> {
// Results in a string like "2021-12-27 14:56:31"
const etime = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
console.log(colors.green(etime));
console.log(`${colors.red('Fatal error: ')}${e.code}: ${e.message}`);
console.log(pc.green(etime));
console.log(`${pc.red('Fatal error: ')}${e.code}: ${e.message}`);
const filename = `httpserver-${etime.split(' ').join('_')}.log`;
console.log(colors.bold(`Check ${filename} file in this folder.`));
console.log(pc.bold(`Check ${filename} file in this folder.`));
fs.writeFileSync(filename, JSON.stringify(e));
process.exit(1);
});
Expand Down Expand Up @@ -145,25 +145,25 @@ if (!argv.s && !argv.silent) {
if (error) {
logger.info(
'[%s] %s "%s %s" Error (%s): "%s"',
date, ip, chalk.red(req.method), chalk.red(req.url),
chalk.red(error.status.toString()), chalk.red(error.message)
date, ip, pc.red(req.method), pc.red(req.url),
pc.red(error.status.toString()), pc.red(error.message)
);
}
else if (req.proxy) {
logger.info(
'[%s] %s "%s" (%s)-> "%s"',
date, ip, chalk.cyan(req.url), chalk.magenta('Proxy'), chalk.cyan(req.proxy.target)
date, ip, pc.cyan(req.url), pc.magenta('Proxy'), pc.cyan(req.proxy.target)
);
} else {
logger.info(
'[%s] %s "%s %s" "%s"',
date, ip, chalk.cyan(req.method), chalk.cyan(req.url),
date, ip, pc.cyan(req.method), pc.cyan(req.url),
req.headers['user-agent']
);
}
}
};
} else if (chalk) {
} else if (pc) {
logger = {
info: function () {},
request: function () {}
Expand Down Expand Up @@ -230,7 +230,7 @@ function listen(port) {

if (websocket) {
if (!proxy) {
logger.warning(colors.yellow('WebSocket proxy will not be enabled because proxy is not enabled'));
logger.info(pc.yellow('WebSocket proxy will not be enabled because proxy is not enabled'));
} else {
options.websocket = true;
}
Expand Down Expand Up @@ -264,7 +264,7 @@ function listen(port) {
try {
new url.URL(proxy);
} catch (err) {
logger.info(chalk.red('Error: Invalid proxy url'));
logger.info(pc.red('Error: Invalid proxy url'));
process.exit(1);
}
}
Expand All @@ -282,7 +282,7 @@ function listen(port) {
}
}
catch (err) {
logger.info(chalk.red('Error: Invalid proxy config or file'));
logger.info(pc.red('Error: Invalid proxy config or file'));
process.exit(1);
}
// Proxy file overrides cli config
Expand All @@ -291,18 +291,18 @@ function listen(port) {
}

if (proxyAll && proxyConfig) {
logger.info(chalk.red('Error: --proxy-all cannot be used with --proxy-config'));
logger.info(pc.red('Error: --proxy-all cannot be used with --proxy-config'));
logger.info(
'%s\n%s\n%s',
chalk.yellow('Hint: Use'),
chalk.cyan('"/**": {\n "target": "your-proxy"\n}'),
chalk.yellow('in the proxy config to achieve the same effect.')
pc.yellow('Hint: Use'),
pc.cyan('"/**": {\n "target": "your-proxy"\n}'),
pc.yellow('in the proxy config to achieve the same effect.')
);
process.exit(1);
}

if (proxyAll && !proxy) {
logger.info(chalk.red('Error: --proxy-all requires --proxy to be set'));
logger.info(pc.red('Error: --proxy-all requires --proxy to be set'));
process.exit(1);
}

Expand All @@ -315,13 +315,13 @@ function listen(port) {
try {
fs.lstatSync(options.https.cert);
} catch (err) {
logger.info(chalk.red('Error: Could not find certificate ' + options.https.cert));
logger.info(pc.red('Error: Could not find certificate ' + options.https.cert));
process.exit(1);
}
try {
fs.lstatSync(options.https.key);
} catch (err) {
logger.info(chalk.red('Error: Could not find private key ' + options.https.key));
logger.info(pc.red('Error: Could not find private key ' + options.https.key));
process.exit(1);
}
}
Expand All @@ -335,51 +335,51 @@ function listen(port) {
path = baseDir ? '/' + baseDir.replace(/^\//, '') : '';

logger.info([
chalk.yellow('Starting up http-server, serving '),
chalk.cyan(server.root),
tls ? (chalk.yellow(' through') + chalk.cyan(' https')) : ''
pc.yellow('Starting up http-server, serving '),
pc.cyan(server.root),
tls ? (pc.yellow(' through') + pc.cyan(' https')) : ''
].join(''));

logger.info([chalk.yellow('\nhttp-server version: '), chalk.cyan(require('../package.json').version)].join(''));
logger.info([pc.yellow('\nhttp-server version: '), pc.cyan(require('../package.json').version)].join(''));

logger.info([
chalk.yellow('\nhttp-server settings: '),
([chalk.yellow('COOP: '), argv.coop ? chalk.cyan(argv.coop) : chalk.red('disabled')].join('')),
([chalk.yellow('CORS: '), argv.cors ? chalk.cyan(argv.cors) : chalk.red('disabled')].join('')),
([chalk.yellow('Private Network Access: '), argv['private-network-access'] ? chalk.cyan(argv['private-network-access']) : chalk.red('disabled')].join('')),
([chalk.yellow('Cache: '), argv.c ? (argv.c === '-1' ? chalk.red('disabled') : chalk.cyan(argv.c + ' seconds')) : chalk.cyan('3600 seconds')].join('')),
([chalk.yellow('Connection Timeout: '), Math.max(0, argv.t) === 0 ? chalk.red('disabled') :
pc.yellow('\nhttp-server settings: '),
([pc.yellow('COOP: '), argv.coop ? pc.cyan(argv.coop) : pc.red('disabled')].join('')),
([pc.yellow('CORS: '), argv.cors ? pc.cyan(argv.cors) : pc.red('disabled')].join('')),
([pc.yellow('Private Network Access: '), argv['private-network-access'] ? pc.cyan(argv['private-network-access']) : pc.red('disabled')].join('')),
([pc.yellow('Cache: '), argv.c ? (argv.c === '-1' ? pc.red('disabled') : pc.cyan(argv.c + ' seconds')) : pc.cyan('3600 seconds')].join('')),
([pc.yellow('Connection Timeout: '), Math.max(0, argv.t) === 0 ? pc.red('disabled') :
((!isNaN(argv.t) && !isNaN(parseFloat(argv.t))) ?
chalk.cyan(Number(argv.t) + ' seconds') : chalk.cyan('120 seconds'))].join('')),
([chalk.yellow('Directory Listings: '), argv.d ? chalk.red('not visible') : chalk.cyan('visible')].join('')),
([chalk.yellow('AutoIndex: '), argv.i ? chalk.red('not visible') : chalk.cyan('visible')].join('')),
([chalk.yellow('Serve GZIP Files: '), argv.g || argv.gzip ? chalk.cyan('true') : chalk.red('false')].join('')),
([chalk.yellow('Serve Brotli Files: '), argv.b || argv.brotli ? chalk.cyan('true') : chalk.red('false')].join('')),
([chalk.yellow('Default File Extension: '), argv.e ? chalk.cyan(argv.e) : (argv.ext ? chalk.cyan(argv.ext) : chalk.red('none'))].join('')),
([chalk.yellow('Base directory: '), baseDir ? chalk.cyan(baseDir) : chalk.cyan('/')].join(''))
pc.cyan(Number(argv.t) + ' seconds') : pc.cyan('120 seconds'))].join('')),
([pc.yellow('Directory Listings: '), argv.d ? pc.red('not visible') : pc.cyan('visible')].join('')),
([pc.yellow('AutoIndex: '), argv.i ? pc.red('not visible') : pc.cyan('visible')].join('')),
([pc.yellow('Serve GZIP Files: '), argv.g || argv.gzip ? pc.cyan('true') : pc.red('false')].join('')),
([pc.yellow('Serve Brotli Files: '), argv.b || argv.brotli ? pc.cyan('true') : pc.red('false')].join('')),
([pc.yellow('Default File Extension: '), argv.e ? pc.cyan(argv.e) : (argv.ext ? pc.cyan(argv.ext) : pc.red('none'))].join('')),
([pc.yellow('Base directory: '), baseDir ? pc.cyan(baseDir) : pc.cyan('/')].join(''))
].join('\n'));

if (options.headers) {
logger.info(chalk.yellow('Additional Headers:'));
logger.info(pc.yellow('Additional Headers:'));
for (let k in options.headers) {
let v = options.headers[k];
logger.info(chalk.yellow(`\t${k}:`) + chalk.cyan(` ${v}`));
logger.info(pc.yellow(`\t${k}:`) + pc.cyan(` ${v}`));
}
}

logger.info(chalk.yellow('\nAvailable on:'));
logger.info(pc.yellow('\nAvailable on:'));


if (argv.a && (host !== '::' || host !== '0.0.0.0')) {
logger.info(` ${protocol}${host}:${chalk.green(port.toString())}${path}`);
logger.info(` ${protocol}${host}:${pc.green(port.toString())}${path}`);
} else {
Object.keys(ifaces).forEach(function (dev) {
ifaces[dev].forEach(function (details) {
if (details.family === 'IPv4' || details.family === 4) {
logger.info((' ' + protocol + details.address + ':' + chalk.green(port.toString()) + path));
logger.info((' ' + protocol + details.address + ':' + pc.green(port.toString()) + path));
}
if (details.family === 'IPv6' && !details.address.startsWith("fe80") ) { // Ignoring Ipv6-Link Local addresses
logger.info((' ' + protocol + details.address + ':' + chalk.green(port.toString())));
logger.info((' ' + protocol + details.address + ':' + pc.green(port.toString())));
}
});
});
Expand All @@ -396,7 +396,7 @@ function listen(port) {
// Set up "CTRL-C" hook, before printing out "Hit CTRL-C to stop the server"
function stopServer() {
server.close();
logger.info(chalk.red('http-server stopped.'));
logger.info(pc.red('http-server stopped.'));
process.exit();
}

Expand Down
Loading