Added rate limiter to all XIV API requests
This commit is contained in:
parent
3ebbb867b2
commit
1bbcae6119
3 changed files with 18 additions and 4 deletions
16
index.js
16
index.js
|
@ -2,6 +2,7 @@ const cacheManager = require('cache-manager');
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
const fsStore = require('cache-manager-fs-binary');
|
const fsStore = require('cache-manager-fs-binary');
|
||||||
|
const rateLimit = require("express-rate-limit");
|
||||||
|
|
||||||
const { CardCreator } = require('./create-card');
|
const { CardCreator } = require('./create-card');
|
||||||
|
|
||||||
|
@ -25,6 +26,13 @@ const diskCache = cacheManager.caching({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Rate limit all requests that result in XIV API calls
|
||||||
|
const limiter = rateLimit({
|
||||||
|
windowMs: 1000, // ms = 1s
|
||||||
|
max: 20, // default XIV API request limit
|
||||||
|
keyGenerator: () => 'global',
|
||||||
|
});
|
||||||
|
|
||||||
async function getCharacterIdByName(world, name, retries = 1) {
|
async function getCharacterIdByName(world, name, retries = 1) {
|
||||||
if (retries === -1) return undefined;
|
if (retries === -1) return undefined;
|
||||||
|
|
||||||
|
@ -61,7 +69,7 @@ function getOriginalQueryString(req) {
|
||||||
return url.search;
|
return url.search;
|
||||||
}
|
}
|
||||||
|
|
||||||
app.get('/prepare/id/:characterId', (req, res, next) => {
|
app.get('/prepare/id/:characterId', limiter, (req, res, next) => {
|
||||||
const language = typeof req.query.lang === 'string' && supportedLanguages.includes(req.query.lang) ? req.query.lang : supportedLanguages[0];
|
const language = typeof req.query.lang === 'string' && supportedLanguages.includes(req.query.lang) ? req.query.lang : supportedLanguages[0];
|
||||||
|
|
||||||
cacheCreateCard(req.params.characterId, null, language)
|
cacheCreateCard(req.params.characterId, null, language)
|
||||||
|
@ -74,7 +82,7 @@ app.get('/prepare/id/:characterId', (req, res, next) => {
|
||||||
.catch(next);
|
.catch(next);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/prepare/name/:world/:characterName', (req, res, next) => {
|
app.get('/prepare/name/:world/:characterName', limiter, (req, res, next) => {
|
||||||
getCharacterIdByName(req.params.world, req.params.characterName)
|
getCharacterIdByName(req.params.world, req.params.characterName)
|
||||||
.then(characterId => {
|
.then(characterId => {
|
||||||
if (characterId == null) {
|
if (characterId == null) {
|
||||||
|
@ -86,7 +94,7 @@ app.get('/prepare/name/:world/:characterName', (req, res, next) => {
|
||||||
.catch(next);
|
.catch(next);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/characters/id/:characterId.png', (req, res, next) => {
|
app.get('/characters/id/:characterId.png', limiter, (req, res, next) => {
|
||||||
const language = typeof req.query.lang === 'string' && supportedLanguages.includes(req.query.lang) ? req.query.lang : supportedLanguages[0];
|
const language = typeof req.query.lang === 'string' && supportedLanguages.includes(req.query.lang) ? req.query.lang : supportedLanguages[0];
|
||||||
|
|
||||||
cacheCreateCard(req.params.characterId, null, language)
|
cacheCreateCard(req.params.characterId, null, language)
|
||||||
|
@ -108,7 +116,7 @@ app.get('/characters/id/:characterId', (req, res) => {
|
||||||
res.redirect(`/characters/id/${req.params.characterId}.png${getOriginalQueryString(req)}`);
|
res.redirect(`/characters/id/${req.params.characterId}.png${getOriginalQueryString(req)}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/characters/name/:world/:characterName.png', (req, res, next) => {
|
app.get('/characters/name/:world/:characterName.png', limiter, (req, res, next) => {
|
||||||
getCharacterIdByName(req.params.world, req.params.characterName)
|
getCharacterIdByName(req.params.world, req.params.characterName)
|
||||||
.then(characterId => {
|
.then(characterId => {
|
||||||
if (characterId == null) {
|
if (characterId == null) {
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
"cache-manager-fs-binary": "^1.0.4",
|
"cache-manager-fs-binary": "^1.0.4",
|
||||||
"canvas": "^2.8.0",
|
"canvas": "^2.8.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
|
"express-rate-limit": "^5.5.0",
|
||||||
"node-fetch": "^2.6.5"
|
"node-fetch": "^2.6.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
@ -534,6 +534,11 @@ etag@~1.8.1:
|
||||||
resolved "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz"
|
resolved "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz"
|
||||||
integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=
|
integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=
|
||||||
|
|
||||||
|
express-rate-limit@^5.5.0:
|
||||||
|
version "5.5.0"
|
||||||
|
resolved "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-5.5.0.tgz"
|
||||||
|
integrity sha512-/1mrKggjXMxd1/ghPub5N3d36u5VlK8KjbQFQLxYub09BWSSgSXMQbXgFiIW0BYxjM49YCj8bkihONZR2U4+mQ==
|
||||||
|
|
||||||
express@^4.17.1:
|
express@^4.17.1:
|
||||||
version "4.17.1"
|
version "4.17.1"
|
||||||
resolved "https://registry.npmjs.org/express/-/express-4.17.1.tgz"
|
resolved "https://registry.npmjs.org/express/-/express-4.17.1.tgz"
|
||||||
|
|
Loading…
Reference in a new issue