Skip to content

Commit

Permalink
Modify engines field, add yarn lockfile, refactor names (#7)
Browse files Browse the repository at this point in the history
* refactor: names and indentation

* modified engine compatibility

* rebuild
  • Loading branch information
glebec authored Oct 14, 2016
1 parent e97243f commit 7d48c41
Show file tree
Hide file tree
Showing 4 changed files with 2,790 additions and 60 deletions.
66 changes: 36 additions & 30 deletions lib/volleyball.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,90 +8,96 @@ var makeId = require('./id');

var sp = ' ';

module.exports = new Volleyball();
module.exports = Volleyball(); // eslint-disable-line new-cap

function Volleyball() {
var config = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

var logger = getLogger(config.debug);

// items shared across multiple req-res cycles, for a given volleyball
var log = getLogger(config.debug);

function volleyball(req, res, next) {
var shared = {
logger: logger,
// items shared between the request and response of just one cycle
var cycle = {
log: log,
id: makeId(),
time: process.hrtime()
};

logReq(req, shared);

logReq(req, cycle);
res.on('finish', function () {
return logRes(res, shared);
return logRes(res, cycle);
});
res.on('close', function () {
return logClose(res, shared);
return logClose(res, cycle);
});

next();
}
volleyball.custom = volleyballFactory;

return volleyball;
}
// factory method which does not expose any of the library internals
volleyball.custom = function () {
return Volleyball.apply(undefined, arguments);
}; // eslint-disable-line new-cap

function volleyballFactory() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

return new (Function.prototype.bind.apply(Volleyball, [null].concat(args)))();
return volleyball;
}

function getLogger(debugChoice) {
if (!debugChoice) return defaultLogger;
if (debugChoice === true) return debug('http');
if (typeof debugChoice === 'string') return debug(debugChoice);
if (typeof debugChoice === 'function') return debugChoice;
throw Error('Invalid option for debug');
}

function defaultLogger(str) {
process.stdout.write(str + '\n');
}

function logReq(req, shared) {
function logReq(req, cycle) {
var bytes = +req.headers['content-length'];
var type = req.headers['content-type'];

var reqLine = shared.id + ' ' + chalk.dim('——>') + ' ';
var reqLine = cycle.id + ' ' + chalk.dim('——>') + ' ';
reqLine += chalk.bold.underline(req.method) + ' ' + req.url + ' ';
if (bytes) reqLine += chalk.blue(filesize(bytes)) + sp;
if (type) reqLine += chalk.blue.dim(type);

shared.logger(reqLine);
cycle.log(reqLine);
}

function logRes(res, shared) {
function logRes(res, cycle) {
var status = res.statusCode;
var meaning = http.STATUS_CODES[status];
var bytes = +res.getHeader('content-length');
var type = res.getHeader('content-type');

var statusColor = void 0;
if (status >= 500) statusColor = 'red';else if (status >= 400) statusColor = 'yellow';else if (status >= 300) statusColor = 'cyan';else if (status >= 200) statusColor = 'green';else statusColor = 'reset';
var statusColor = colorForStatus(status);

var resLine = shared.id + ' ' + chalk.dim('<——') + ' ';
var resLine = cycle.id + ' ' + chalk.dim('<——') + ' ';
resLine += chalk[statusColor](status + ' ' + meaning) + sp;
if (bytes) resLine += chalk.blue(filesize(bytes)) + sp;
if (type) resLine += chalk.blue.dim(type) + sp;
resLine += chalk.dim('(<> ' + msDiff(shared.time) + ' ms)');
resLine += chalk.dim('(<\u2014> ' + msDiff(cycle.time) + ' ms)');

shared.logger(resLine);
cycle.log(resLine);
}

function logClose(res, shared) {
var closeLine = shared.id + ' ' + chalk.dim('—X—') + ' ';
function logClose(res, cycle) {
var closeLine = cycle.id + ' ' + chalk.dim('—X—') + ' ';
closeLine += chalk.red('connection closed before res end/flush');

shared.logger(closeLine);
cycle.log(closeLine);
}

function colorForStatus(status) {
if (status >= 500) return 'red';
if (status >= 400) return 'yellow';
if (status >= 300) return 'cyan';
if (status >= 200) return 'green';
return 'reset';
}

function msDiff(time) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.3.0",
"description": "🏐 Tiny HTTP logger for Express showing asynchronous requests and responses",
"engines": {
"node": "^4.0.0"
"node": ">=4.0.0"
},
"homepage": "https://github.com/glebec/volleyball",
"main": "lib/volleyball.js",
Expand Down
62 changes: 33 additions & 29 deletions src/volleyball.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,84 +8,88 @@ const makeId = require('./id')

const sp = ' '

module.exports = new Volleyball()
module.exports = Volleyball() // eslint-disable-line new-cap

function Volleyball (config = {}) {
const logger = getLogger(config.debug)

// items shared across multiple req-res cycles, for a given volleyball
const log = getLogger(config.debug)

function volleyball (req, res, next) {
const shared = {
logger: logger,
// items shared between the request and response of just one cycle
const cycle = {
log: log,
id: makeId(),
time: process.hrtime()
}

logReq(req, shared)

res.on('finish', () => logRes(res, shared))
res.on('close', () => logClose(res, shared))
logReq(req, cycle)
res.on('finish', () => logRes(res, cycle))
res.on('close', () => logClose(res, cycle))

next()
}
volleyball.custom = volleyballFactory

return volleyball
}
// factory method which does not expose any of the library internals
volleyball.custom = (...args) => Volleyball(...args) // eslint-disable-line new-cap

function volleyballFactory (...args) {
return new Volleyball(...args)
return volleyball
}

function getLogger (debugChoice) {
if (!debugChoice) return defaultLogger
if (debugChoice === true) return debug('http')
if (typeof debugChoice === 'string') return debug(debugChoice)
if (typeof debugChoice === 'function') return debugChoice
throw Error('Invalid option for debug')
}

function defaultLogger (str) {
process.stdout.write(str + '\n')
}

function logReq (req, shared) {
function logReq (req, cycle) {
const bytes = +req.headers['content-length']
const type = req.headers['content-type']

let reqLine = `${shared.id} ${chalk.dim('——>')} `
let reqLine = `${cycle.id} ${chalk.dim('——>')} `
reqLine += `${chalk.bold.underline(req.method)} ${req.url} `
if (bytes) reqLine += chalk.blue(filesize(bytes)) + sp
if (type) reqLine += chalk.blue.dim(type)

shared.logger(reqLine)
cycle.log(reqLine)
}

function logRes (res, shared) {
function logRes (res, cycle) {
const status = res.statusCode
const meaning = http.STATUS_CODES[status]
const bytes = +res.getHeader('content-length')
const type = res.getHeader('content-type')

let statusColor
if (status >= 500) statusColor = 'red'
else if (status >= 400) statusColor = 'yellow'
else if (status >= 300) statusColor = 'cyan'
else if (status >= 200) statusColor = 'green'
else statusColor = 'reset'
const statusColor = colorForStatus(status)

let resLine = `${shared.id} ${chalk.dim('<——')} `
let resLine = `${cycle.id} ${chalk.dim('<——')} `
resLine += chalk[statusColor](`${status} ${meaning}`) + sp
if (bytes) resLine += chalk.blue(filesize(bytes)) + sp
if (type) resLine += chalk.blue.dim(type) + sp
resLine += chalk.dim(`(<—> ${msDiff(shared.time)} ms)`)
resLine += chalk.dim(`(<—> ${msDiff(cycle.time)} ms)`)

shared.logger(resLine)
cycle.log(resLine)
}

function logClose (res, shared) {
let closeLine = `${shared.id} ${chalk.dim('—X—')} `
function logClose (res, cycle) {
let closeLine = `${cycle.id} ${chalk.dim('—X—')} `
closeLine += chalk.red('connection closed before res end/flush')

shared.logger(closeLine)
cycle.log(closeLine)
}

function colorForStatus (status) {
if (status >= 500) return 'red'
if (status >= 400) return 'yellow'
if (status >= 300) return 'cyan'
if (status >= 200) return 'green'
return 'reset'
}

function msDiff (time) {
Expand Down
Loading

0 comments on commit 7d48c41

Please sign in to comment.