Skip to content

Commit 7d48c41

Browse files
authored
Modify engines field, add yarn lockfile, refactor names (#7)
* refactor: names and indentation * modified engine compatibility * rebuild
1 parent e97243f commit 7d48c41

File tree

4 files changed

+2790
-60
lines changed

4 files changed

+2790
-60
lines changed

lib/volleyball.js

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,90 +8,96 @@ var makeId = require('./id');
88

99
var sp = ' ';
1010

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

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

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

1820
function volleyball(req, res, next) {
19-
var shared = {
20-
logger: logger,
21+
// items shared between the request and response of just one cycle
22+
var cycle = {
23+
log: log,
2124
id: makeId(),
2225
time: process.hrtime()
2326
};
2427

25-
logReq(req, shared);
26-
28+
logReq(req, cycle);
2729
res.on('finish', function () {
28-
return logRes(res, shared);
30+
return logRes(res, cycle);
2931
});
3032
res.on('close', function () {
31-
return logClose(res, shared);
33+
return logClose(res, cycle);
3234
});
3335

3436
next();
3537
}
36-
volleyball.custom = volleyballFactory;
3738

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

41-
function volleyballFactory() {
42-
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
43-
args[_key] = arguments[_key];
44-
}
45-
46-
return new (Function.prototype.bind.apply(Volleyball, [null].concat(args)))();
44+
return volleyball;
4745
}
4846

4947
function getLogger(debugChoice) {
5048
if (!debugChoice) return defaultLogger;
5149
if (debugChoice === true) return debug('http');
5250
if (typeof debugChoice === 'string') return debug(debugChoice);
5351
if (typeof debugChoice === 'function') return debugChoice;
52+
throw Error('Invalid option for debug');
5453
}
5554

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

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

64-
var reqLine = shared.id + ' ' + chalk.dim('——>') + ' ';
63+
var reqLine = cycle.id + ' ' + chalk.dim('——>') + ' ';
6564
reqLine += chalk.bold.underline(req.method) + ' ' + req.url + ' ';
6665
if (bytes) reqLine += chalk.blue(filesize(bytes)) + sp;
6766
if (type) reqLine += chalk.blue.dim(type);
6867

69-
shared.logger(reqLine);
68+
cycle.log(reqLine);
7069
}
7170

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

78-
var statusColor = void 0;
79-
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';
77+
var statusColor = colorForStatus(status);
8078

81-
var resLine = shared.id + ' ' + chalk.dim('<——') + ' ';
79+
var resLine = cycle.id + ' ' + chalk.dim('<——') + ' ';
8280
resLine += chalk[statusColor](status + ' ' + meaning) + sp;
8381
if (bytes) resLine += chalk.blue(filesize(bytes)) + sp;
8482
if (type) resLine += chalk.blue.dim(type) + sp;
85-
resLine += chalk.dim('(<> ' + msDiff(shared.time) + ' ms)');
83+
resLine += chalk.dim('(<\u2014> ' + msDiff(cycle.time) + ' ms)');
8684

87-
shared.logger(resLine);
85+
cycle.log(resLine);
8886
}
8987

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

94-
shared.logger(closeLine);
92+
cycle.log(closeLine);
93+
}
94+
95+
function colorForStatus(status) {
96+
if (status >= 500) return 'red';
97+
if (status >= 400) return 'yellow';
98+
if (status >= 300) return 'cyan';
99+
if (status >= 200) return 'green';
100+
return 'reset';
95101
}
96102

97103
function msDiff(time) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.3.0",
44
"description": "🏐 Tiny HTTP logger for Express showing asynchronous requests and responses",
55
"engines": {
6-
"node": "^4.0.0"
6+
"node": ">=4.0.0"
77
},
88
"homepage": "https://github.com/glebec/volleyball",
99
"main": "lib/volleyball.js",

src/volleyball.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,84 +8,88 @@ const makeId = require('./id')
88

99
const sp = ' '
1010

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

1313
function Volleyball (config = {}) {
14-
const logger = getLogger(config.debug)
14+
15+
// items shared across multiple req-res cycles, for a given volleyball
16+
const log = getLogger(config.debug)
1517

1618
function volleyball (req, res, next) {
17-
const shared = {
18-
logger: logger,
19+
// items shared between the request and response of just one cycle
20+
const cycle = {
21+
log: log,
1922
id: makeId(),
2023
time: process.hrtime()
2124
}
2225

23-
logReq(req, shared)
24-
25-
res.on('finish', () => logRes(res, shared))
26-
res.on('close', () => logClose(res, shared))
26+
logReq(req, cycle)
27+
res.on('finish', () => logRes(res, cycle))
28+
res.on('close', () => logClose(res, cycle))
2729

2830
next()
2931
}
30-
volleyball.custom = volleyballFactory
3132

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

35-
function volleyballFactory (...args) {
36-
return new Volleyball(...args)
36+
return volleyball
3737
}
3838

3939
function getLogger (debugChoice) {
4040
if (!debugChoice) return defaultLogger
4141
if (debugChoice === true) return debug('http')
4242
if (typeof debugChoice === 'string') return debug(debugChoice)
4343
if (typeof debugChoice === 'function') return debugChoice
44+
throw Error('Invalid option for debug')
4445
}
4546

4647
function defaultLogger (str) {
4748
process.stdout.write(str + '\n')
4849
}
4950

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

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

59-
shared.logger(reqLine)
60+
cycle.log(reqLine)
6061
}
6162

62-
function logRes (res, shared) {
63+
function logRes (res, cycle) {
6364
const status = res.statusCode
6465
const meaning = http.STATUS_CODES[status]
6566
const bytes = +res.getHeader('content-length')
6667
const type = res.getHeader('content-type')
6768

68-
let statusColor
69-
if (status >= 500) statusColor = 'red'
70-
else if (status >= 400) statusColor = 'yellow'
71-
else if (status >= 300) statusColor = 'cyan'
72-
else if (status >= 200) statusColor = 'green'
73-
else statusColor = 'reset'
69+
const statusColor = colorForStatus(status)
7470

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

81-
shared.logger(resLine)
77+
cycle.log(resLine)
8278
}
8379

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

88-
shared.logger(closeLine)
84+
cycle.log(closeLine)
85+
}
86+
87+
function colorForStatus (status) {
88+
if (status >= 500) return 'red'
89+
if (status >= 400) return 'yellow'
90+
if (status >= 300) return 'cyan'
91+
if (status >= 200) return 'green'
92+
return 'reset'
8993
}
9094

9195
function msDiff (time) {

0 commit comments

Comments
 (0)