Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add @microlink/ua integration #296

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"@kikobeats/time-span": "~1.0.2",
"@microlink/mql": "~0.12.1",
"@microlink/ping-url": "~1.4.10",
"@microlink/ua": "~1.0.0",
"async-ratelimiter": "~1.3.11",
"browserless": "~10.2.0",
"cacheable-lookup": "~6.1.0",
Expand Down
28 changes: 14 additions & 14 deletions src/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ const rateLimitError = (() => {

module.exports = rateLimiter
? async (req, res, next) => {
if (req.headers['x-api-key'] === API_KEY) return next()
const { total, reset, remaining } = await rateLimiter.get({
id: req.ipAddress
})

if (!res.writableEnded) {
const _remaining = Math.max(0, remaining - 1)
res.setHeader('X-Rate-Limit-Limit', total)
res.setHeader('X-Rate-Limit-Remaining', _remaining)
res.setHeader('X-Rate-Limit-Reset', reset)
debug(req.ipAddress, { total, remaining: _remaining })
}

return remaining ? next() : next(rateLimitError)
if (req.headers['x-api-key'] === API_KEY) return next()
const { total, reset, remaining } = await rateLimiter.get({
id: req.ipAddress
})

if (!res.writableEnded) {
const _remaining = Math.max(0, remaining - 1)
res.setHeader('X-Rate-Limit-Limit', total)
res.setHeader('X-Rate-Limit-Remaining', _remaining)
res.setHeader('X-Rate-Limit-Reset', reset)
debug(req.ipAddress, { total, remaining: _remaining })
}

return remaining ? next() : next(rateLimitError)
}
: false
1 change: 1 addition & 0 deletions src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const API_URL =

module.exports = {
...process.env,
isProduction: NODE_ENV === 'production',
API_URL,
ALLOWED_REQ_HEADERS,
AVATAR_SIZE,
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { providers } = require('./providers')
const ssrCache = require('./send/cache')
const avatar = require('./avatar')

const { API_URL } = require('./constant')
const { isProduction, API_URL } = require('./constant')

const router = createRouter((error, req, res) => {
const hasError = error !== undefined
Expand Down Expand Up @@ -41,6 +41,7 @@ router
next()
},
require('./authentication'),
isProduction && require('./ua'),
(req, res, next) => {
req.timestamp = timeSpan()
req.query = Array.from(new URLSearchParams(req.query)).reduce(
Expand Down
9 changes: 9 additions & 0 deletions src/ua.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

const redis = require('./util/redis').ua
const ua = require('@microlink/ua')(redis)

module.exports = async (req, _, next) => {
await ua.incr(req.headers['user-agent'])
next()
}
2 changes: 1 addition & 1 deletion src/util/keyv.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const KeyvMulti = require('@keyvhq/multi')
const Keyv = require('@keyvhq/core')
const assert = require('assert')

const redis = require('./redis')
const redis = require('./redis').cache

const { CACHE_TTL } = require('../constant')

Expand Down
12 changes: 6 additions & 6 deletions src/util/rate-limiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const RateLimiter = require('async-ratelimiter')

const { RATE_LIMIT_WINDOW, RATE_LIMIT } = require('../constant')

const db = require('./redis')
const db = require('./redis').cache

module.exports = db
? new RateLimiter({
db,
namespace: 'rate',
duration: RATE_LIMIT_WINDOW,
max: RATE_LIMIT
})
db,
namespace: 'rate',
duration: RATE_LIMIT_WINDOW,
max: RATE_LIMIT
})
: undefined
14 changes: 10 additions & 4 deletions src/util/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

const Redis = require('ioredis')

const { REDIS_URI } = require('../constant')
const { REDIS_URI, REDIS_UA_URI } = require('../constant')

module.exports = REDIS_URI
? new Redis(REDIS_URI, {
const createClient = uri =>
uri
? new Redis(uri, {
lazyConnect: true,
enableAutoPipelining: true,
maxRetriesPerRequest: 2
})
: undefined
: undefined

module.exports = {
cache: createClient(REDIS_URI),
ua: createClient(REDIS_UA_URI)
}
Loading