From c3be877dbf5254fca7de3e1efa9d0090c9b08b1d Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Sun, 8 Oct 2023 11:00:41 +0000 Subject: [PATCH] refactor: use @kikobeats/time-span dependency --- package.json | 1 + src/index.js | 10 +++------- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index c699e70..3b26bdb 100644 --- a/package.json +++ b/package.json @@ -87,6 +87,7 @@ "@keyvhq/core": "~2.1.0", "@keyvhq/multi": "~2.1.0", "@keyvhq/redis": "~2.1.0", + "@kikobeats/time-span": "~1.0.2", "@microlink/mql": "~0.11.4", "@microlink/ping-url": "~1.4.10", "async-ratelimiter": "~1.3.11", diff --git a/src/index.js b/src/index.js index 0db08dd..bc9bb79 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ 'use strict' +const timeSpan = require('@kikobeats/time-span')() const debug = require('debug-logfmt')('unavatar') const serveStatic = require('serve-static') const createRouter = require('router-http') @@ -14,11 +15,6 @@ const avatar = require('./avatar') const { API_URL } = require('./constant') -const timestamp = - (start = process.hrtime.bigint()) => - () => - Math.round(Number(process.hrtime.bigint() - start) / 1e6) - const router = createRouter((error, req, res) => { const hasError = error !== undefined let statusCode = 404 @@ -46,7 +42,7 @@ router }, require('./authentication'), (req, res, next) => { - req.timestamp = timestamp() + req.timestamp = timeSpan() req.query = Array.from(new URLSearchParams(req.query)).reduce( (acc, [key, value]) => { try { @@ -62,7 +58,7 @@ router debug( `${req.ipAddress} ${new URL(req.url, API_URL).toString()} ${ res.statusCode - } ${req.timestamp()}ms` + } ${Math.round(req.timestamp())}ms` ) }) next()