Skip to content

Commit

Permalink
Prettier update
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-r committed Apr 6, 2020
1 parent ce673e3 commit 2c85230
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 41 deletions.
4 changes: 2 additions & 2 deletions examples/express-example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const { statsMiddleware, getStats } = initStats({
endpointStats: true,
complexEndpoints: ['/user/:id'],
customStats: true,
addHeader: true
addHeader: true,
});

app.use(statsMiddleware);
app.get('/', (req, res) => res.end('Hello'));
app.get('/user/:id', (req, res) => res.end(`Hello ${req.params.id}`));
app.get('/long', async (req, res) => {
const measurement = req.startMeasurement('long');
await new Promise(resolve => {
await new Promise((resolve) => {
setTimeout(resolve, 2000);
});
req.finishMeasurement(measurement);
Expand Down
4 changes: 2 additions & 2 deletions examples/fastify-example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { statsMiddleware, getStats } = initStats({
endpointStats: true,
complexEndpoints: ['/user/:id'],
customStats: true,
addHeader: true
addHeader: true,
});

/*
Expand All @@ -20,7 +20,7 @@ fastify.get('/', (req, res) => res.send('Hello'));
fastify.get('/user/:id', (req, res) => res.send(`Hello ${req.params.id}`));
fastify.get('/long', async (req, res) => {
const measurement = req.startMeasurement('long');
await new Promise(resolve => {
await new Promise((resolve) => {
setTimeout(resolve, 2000);
});
req.finishMeasurement(measurement);
Expand Down
17 changes: 7 additions & 10 deletions examples/koa-example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,27 @@ const { statsMiddleware, getStats } = initStats({
endpointStats: true,
complexEndpoints: ['/user/:id'],
customStats: true,
addHeader: true
addHeader: true,
});

// Small modification is required to work with koa
const koaStatsMiddleware = (ctx, next) =>
statsMiddleware(ctx.req, ctx.res, next);
koaStatsMiddleware._name = 'statsMiddleware';

router.get('/', ctx => (ctx.body = 'Hello'));
router.get('/user/:id', ctx => (ctx.body = `Hello ${ctx.params.id}`));
router.get('/long', async ctx => {
router.get('/', (ctx) => (ctx.body = 'Hello'));
router.get('/user/:id', (ctx) => (ctx.body = `Hello ${ctx.params.id}`));
router.get('/long', async (ctx) => {
const measurement = ctx.req.startMeasurement('long');
await new Promise(resolve => {
await new Promise((resolve) => {
setTimeout(resolve, 2000);
});
ctx.req.finishMeasurement(measurement);
ctx.body = 'Long job finished'; // eslint-disable-line require-atomic-updates
});
router.get('/stats', ctx => (ctx.body = getStats()));
router.get('/stats', (ctx) => (ctx.body = getStats()));

app
.use(koaStatsMiddleware)
.use(router.routes())
.use(router.allowedMethods());
app.use(koaStatsMiddleware).use(router.routes()).use(router.allowedMethods());

app.listen(8080);
console.log('Server listens at http://localhost:8080'); // eslint-disable-line no-console
4 changes: 2 additions & 2 deletions examples/node-example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { statsMiddleware, getStats } = initStats({
endpointStats: true,
complexEndpoints: ['/user/:id'],
customStats: true,
addHeader: true
addHeader: true,
});

http
Expand All @@ -20,7 +20,7 @@ http
return res.end(`Hello ${req.url.split('/user/')[1]}`);
}
const measurement = req.startMeasurement('long');
await new Promise(resolve => {
await new Promise((resolve) => {
setTimeout(resolve, 2000);
});
req.finishMeasurement(measurement);
Expand Down
4 changes: 2 additions & 2 deletions examples/polka-example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { statsMiddleware, getStats } = initStats({
endpointStats: true,
complexEndpoints: ['/user/:id'],
customStats: true,
addHeader: true
addHeader: true,
});

polka()
Expand All @@ -15,7 +15,7 @@ polka()
.get('/user/:id', (req, res) => send(res, 200, `Hello ${req.params.id}`))
.get('/long', async (req, res) => {
const measurement = req.startMeasurement('long');
await new Promise(resolve => {
await new Promise((resolve) => {
setTimeout(resolve, 2000);
});
req.finishMeasurement(measurement);
Expand Down
4 changes: 2 additions & 2 deletions examples/rayo-example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { statsMiddleware, getStats } = initStats({
endpointStats: true,
complexEndpoints: ['/user/:id'],
customStats: true,
addHeader: true
addHeader: true,
});

rayo({ port: 8080 })
Expand All @@ -14,7 +14,7 @@ rayo({ port: 8080 })
.get('/user/:id', (req, res) => res.end(`Hello ${req.params.id}`))
.get('/long', async (req, res) => {
const measurement = req.startMeasurement('long');
await new Promise(resolve => {
await new Promise((resolve) => {
setTimeout(resolve, 2000);
});
req.finishMeasurement(measurement);
Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ function initMiddleware(opts = {}) {
pid: process.pid,
totalTime: 0,
averageTime: 0,
count: 0
count: 0,
};

const statusCodes = {};
const endpointStats = {};
const complexEndpoints =
(opts.complexEndpoints &&
opts.complexEndpoints.map(path => ({ ...regexparam(path), path }))) ||
opts.complexEndpoints.map((path) => ({ ...regexparam(path), path }))) ||
[];
const customStats = {};

Expand All @@ -28,7 +28,7 @@ function initMiddleware(opts = {}) {
uptime: process.uptime() * 1e3, // convert to ms
uptimeHumanReadable: prettyTime(Math.floor(process.uptime() * 1e9)),
statusCodes,
...stats
...stats,
};
if (opts.endpointStats) {
result.endpointStats = endpointStats;
Expand All @@ -45,7 +45,7 @@ function initMiddleware(opts = {}) {
totalTime: 0,
averageTime: 0,
started: 0,
count: 0
count: 0,
};
}
customStats[name].started++;
Expand Down Expand Up @@ -88,7 +88,7 @@ function initMiddleware(opts = {}) {
// prefer using `req.originalUrl` as some frameworks replace `req.url`
const url = req.originalUrl || req.url;
let path = urlParse(url).pathname;
const complexPath = complexEndpoints.find(endpoint =>
const complexPath = complexEndpoints.find((endpoint) =>
endpoint.pattern.test(path)
);
path = complexPath ? complexPath.path : path;
Expand All @@ -99,7 +99,7 @@ function initMiddleware(opts = {}) {
totalTime: 0,
averageTime: 0,
count: 0,
statusCodes: {}
statusCodes: {},
};
}

Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"eslint-plugin-prettier": "^3.1.2",
"husky": "^4.2.3",
"nyc": "^15.0.0",
"prettier": "^1.19.1",
"prettier": "^2.0.4",
"pretty-quick": "^2.0.1",
"supertest": "^4.0.2"
}
Expand Down
18 changes: 9 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const http = require('http');
const initStats = require('.');
const { hrTimeToMs } = require('./utils');

test('initMiddleware returns getStats and statsMiddleware', t => {
test('initMiddleware returns getStats and statsMiddleware', (t) => {
const { getStats, statsMiddleware } = initStats();
t.is(typeof getStats, 'function');
t.is(typeof statsMiddleware, 'function');
});

test('getStats returns a correct object', t => {
test('getStats returns a correct object', (t) => {
const { getStats } = initStats();
const stats = getStats();
t.is(typeof stats.uptime, 'number');
Expand All @@ -29,7 +29,7 @@ test('getStats returns a correct object', t => {
t.is(typeof stats.customStats, 'undefined');
});

test('statsMiddleware works', async t => {
test('statsMiddleware works', async (t) => {
const { getStats, statsMiddleware } = initStats();
const server = createServer(statsMiddleware);

Expand All @@ -52,7 +52,7 @@ test('statsMiddleware works', async t => {
t.deepEqual(stats.statusCodes, { 200: 2 });
});

test('endpointStats option works', async t => {
test('endpointStats option works', async (t) => {
const { getStats, statsMiddleware } = initStats({ endpointStats: true });
const server = createServer(statsMiddleware);

Expand All @@ -78,10 +78,10 @@ test('endpointStats option works', async t => {
t.deepEqual(endpointStat.statusCodes, { 200: 1 });
});

test('complexEndpoints option works', async t => {
test('complexEndpoints option works', async (t) => {
const { getStats, statsMiddleware } = initStats({
endpointStats: true,
complexEndpoints: ['/user/:id']
complexEndpoints: ['/user/:id'],
});
const server = createServer(statsMiddleware);

Expand All @@ -99,7 +99,7 @@ test('complexEndpoints option works', async t => {
t.deepEqual(endpointStat.statusCodes, { 200: 2 });
});

test('customStats option works', t => {
test('customStats option works', (t) => {
const { getStats, statsMiddleware } = initStats({ customStats: true });
const req = {};
statsMiddleware(req, {}, () => {});
Expand Down Expand Up @@ -130,7 +130,7 @@ test('customStats option works', t => {
t.is(customStat.count, 2);
});

test('addHeader option works', async t => {
test('addHeader option works', async (t) => {
const { statsMiddleware } = initStats({ addHeader: true });
const server = createServer(statsMiddleware);

Expand All @@ -139,7 +139,7 @@ test('addHeader option works', async t => {
t.assert(res.headers['x-response-time'].includes('ms'));
});

test('utils.hrTimeToMs works correctly', t => {
test('utils.hrTimeToMs works correctly', (t) => {
t.is(hrTimeToMs([0, 0]), 0);
t.is(hrTimeToMs([1, 0]), 1000); // 1st argument is a second 1s = 1000ms
t.is(hrTimeToMs([0, 1]), 1e-6); // 2nd argument is a nanosecond 1ns = 1e-6ms
Expand Down

0 comments on commit 2c85230

Please sign in to comment.