From c1a7de9b8681b6dddd71524c86671ddf866c3037 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Tue, 16 Jan 2024 16:10:05 +0000 Subject: [PATCH] refactor: use ms --- package.json | 2 +- src/authentication.js | 7 +++---- src/constant.js | 8 ++++---- src/index.js | 11 +++++------ src/send/cache.js | 8 +++++--- test/query-parameters.js | 6 +++--- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index f8bf8c7..c56c089 100644 --- a/package.json +++ b/package.json @@ -88,7 +88,6 @@ "@keyvhq/multi": "~2.1.0", "@keyvhq/redis": "~2.1.0", "@kikobeats/time-span": "~1.0.3", - "@lukeed/ms": "~2.0.2", "@microlink/mql": "~0.12.2", "@microlink/ping-url": "~1.4.11", "@microlink/ua": "~1.2.0", @@ -112,6 +111,7 @@ "is-email-like": "~1.0.0", "is-url-http": "~2.3.7", "lodash": "~4.17.21", + "ms": "~2.1.3", "on-finished": "~2.4.1", "p-any": "~3.0.0", "p-cancelable": "2.1.1", diff --git a/src/authentication.js b/src/authentication.js index 82ce126..25b3b3f 100644 --- a/src/authentication.js +++ b/src/authentication.js @@ -1,12 +1,12 @@ 'use strict' +const timeSpan = require('@kikobeats/time-span')({ format: require('ms') }) const debug = require('debug-logfmt')('unavatar:authentication') const { RateLimiterMemory } = require('rate-limiter-flexible') const FrequencyCounter = require('frequency-counter') const onFinished = require('on-finished') -const { format } = require('@lukeed/ms') -const START = Date.now() +const duration = timeSpan() const reqsMin = new FrequencyCounter(60) let reqs = 0 @@ -57,12 +57,11 @@ module.exports = async (req, res, next) => { res.setHeader('X-Rate-Limit-Remaining', quotaRemaining) res.setHeader('X-Rate-Limit-Reset', new Date(Date.now() + msBeforeNext)) - const uptime = format(Date.now() - START) const perMinute = reqsMin.freq() const perSecond = Number(perMinute / 60).toFixed(1) debug(req.ipAddress, { - uptime, + uptime: duration(), reqs, 'req/m': perMinute, 'req/s': perSecond, diff --git a/src/constant.js b/src/constant.js index 4f19df0..c09153a 100644 --- a/src/constant.js +++ b/src/constant.js @@ -1,7 +1,7 @@ 'use strict' -const { parse } = require('@lukeed/ms') const { existsSync } = require('fs') +const ms = require('ms') const TMP_FOLDER = existsSync('/dev/shm') ? '/dev/shm' : '/tmp' @@ -9,9 +9,9 @@ const { ALLOWED_REQ_HEADERS = ['accept-encoding', 'accept', 'user-agent'], AVATAR_SIZE = 400, AVATAR_TIMEOUT = 25000, - TTL_DEFAULT = parse('1y'), - TTL_MIN = parse('1h'), - TTL_MAX = parse('28d'), + TTL_DEFAULT = ms('1y'), + TTL_MIN = ms('1h'), + TTL_MAX = ms('28d'), NODE_ENV = 'development', PORT = 3000, RATE_LIMIT_WINDOW = 86400, diff --git a/src/index.js b/src/index.js index 9651c1b..610b910 100644 --- a/src/index.js +++ b/src/index.js @@ -1,17 +1,16 @@ 'use strict' +const timeSpan = require('@kikobeats/time-span')({ + format: n => ms(Math.round(n)) +}) const debug = require('debug-logfmt')('unavatar') const serveStatic = require('serve-static') const createRouter = require('router-http') const onFinished = require('on-finished') -const { format } = require('@lukeed/ms') const { forEach } = require('lodash') const send = require('send-http') const path = require('path') - -const timeSpan = require('@kikobeats/time-span')({ - format: n => format(Math.round(n)) -}) +const ms = require('ms') const { providers } = require('./providers') const ssrCache = require('./send/cache') @@ -73,7 +72,7 @@ router debug( `${req.ipAddress} ${new URL(req.url, API_URL).toString()} ${ res.statusCode - } ${req.timestamp()}` + } – ${req.timestamp()}` ) }) next() diff --git a/src/send/cache.js b/src/send/cache.js index c385df5..6f85e16 100644 --- a/src/send/cache.js +++ b/src/send/cache.js @@ -1,6 +1,6 @@ 'use strict' -const { parse } = require('@lukeed/ms') +const ms = require('ms') const memoize = require('../util/memoize') const send = require('.') @@ -9,8 +9,10 @@ const { TTL_DEFAULT, TTL_MIN, TTL_MAX } = require('../constant') const getTtl = memoize(ttl => { if (ttl === undefined || ttl === null) return TTL_DEFAULT - const value = typeof ttl === 'number' ? ttl : parse(ttl) - if (value === undefined || value < TTL_MIN || value > TTL_MAX) { return TTL_DEFAULT } + const value = typeof ttl === 'number' ? ttl : ms(ttl) + if (value === undefined || value < TTL_MIN || value > TTL_MAX) { + return TTL_DEFAULT + } return value }) diff --git a/test/query-parameters.js b/test/query-parameters.js index a01a659..fc9b75c 100644 --- a/test/query-parameters.js +++ b/test/query-parameters.js @@ -1,8 +1,8 @@ 'use strict' -const { parse } = require('@lukeed/ms') const test = require('ava') const got = require('got') +const ms = require('ms') const { TTL_DEFAULT } = require('../src/constant') @@ -60,6 +60,6 @@ test('ttl', t => { t.is(getTtl('foo'), TTL_DEFAULT) t.is(getTtl('29d'), TTL_DEFAULT) t.is(getTtl('29d'), TTL_DEFAULT) - t.is(getTtl(parse('2h')), parse('2h')) - t.is(getTtl('2h'), parse('2h')) + t.is(getTtl(ms('2h')), ms('2h')) + t.is(getTtl('2h'), ms('2h')) })