Skip to content

Commit

Permalink
refactor: use ms
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jan 16, 2024
1 parent 6d28475 commit c1a7de9
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
7 changes: 3 additions & 4 deletions src/authentication.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/constant.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use strict'

const { parse } = require('@lukeed/ms')
const { existsSync } = require('fs')
const ms = require('ms')

const TMP_FOLDER = existsSync('/dev/shm') ? '/dev/shm' : '/tmp'

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,
Expand Down
11 changes: 5 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -73,7 +72,7 @@ router
debug(
`${req.ipAddress} ${new URL(req.url, API_URL).toString()} ${
res.statusCode
} ${req.timestamp()}`
} ${req.timestamp()}`
)
})
next()
Expand Down
8 changes: 5 additions & 3 deletions src/send/cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { parse } = require('@lukeed/ms')
const ms = require('ms')

const memoize = require('../util/memoize')
const send = require('.')
Expand All @@ -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
})

Expand Down
6 changes: 3 additions & 3 deletions test/query-parameters.js
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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'))
})

0 comments on commit c1a7de9

Please sign in to comment.