feat: guv

This commit is contained in:
cha0s 2020-12-06 20:14:45 -06:00
parent f3191d0e63
commit 33b1c94d2b
7 changed files with 6898 additions and 0 deletions

3
packages/governor/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/*.js
/*.js.map
!/webpack.config.js

View File

@ -0,0 +1,39 @@
{
"name": "@latus/governor",
"version": "1.0.0",
"main": "index.js",
"author": "cha0s",
"license": "MIT",
"scripts": {
"build": "NODE_PATH=./node_modules webpack --mode production",
"clean": "rm -f yarn.lock && yarn",
"dev": "NODE_PATH=./node_modules webpack --mode development",
"forcepub": "npm unpublish --force $(node -e 'process.stdout.write(require(`./package.json`).name)') && npm publish",
"lint": "NODE_PATH=./node_modules eslint --format codeframe --ext mjs,js .",
"test": "NODE_PATH=./node_modules mocha --config ../../config/.mocharc.js",
"watch": "NODE_PATH=./node_modules webpack --watch --mode development"
},
"files": [
"index.js",
"index.js.map"
],
"dependencies": {
"@latus/redis": "^1.0.0",
"@latus/socket": "^1.0.0",
"debug": "4.3.1",
"rate-limiter-flexible": "^2.1.13"
},
"devDependencies": {
"@neutrinojs/airbnb-base": "^9.4.0",
"@neutrinojs/copy": "9.4.0",
"@neutrinojs/library": "^9.4.0",
"@neutrinojs/mocha": "^9.4.0",
"chai": "4.2.0",
"eslint": "^7",
"eslint-import-resolver-webpack": "0.13.0",
"mocha": "^8",
"neutrino": "^9.4.0",
"webpack": "^4",
"webpack-cli": "^3"
}
}

View File

@ -0,0 +1,14 @@
import LimitedPacket from './limited-packet';
export default {
hooks: {
'@latus/socket/packets.decorate': (Packets, latus) => (
Object.fromEntries(
Object.entries(Packets).map(([keyPrefix, Packet]) => [
keyPrefix,
!Packet.limit ? Packet : LimitedPacket(latus, [keyPrefix, Packet]),
]),
)
),
},
};

View File

@ -0,0 +1,30 @@
import {ValidationError} from '@latus/socket';
import createLimiter from './limiter';
export default (latus, [keyPrefix, Packet]) => class LimitedPacket extends Packet {
constructor(...args) {
super(...args);
this.limit = createLimiter(latus, {keyPrefix, ...Packet.limit});
}
static async validate(packet, socket) {
try {
await packet.limit.consume(socket.id);
}
catch (error) {
if (error.msBeforeNext) {
throw new ValidationError({
code: 429,
ttr: Math.round(error.msBeforeNext / 1000) || 1,
});
}
throw error;
}
if (super.validate) {
super.validate(packet, socket);
}
}
};

View File

@ -0,0 +1,7 @@
import {createClient} from '@latus/redis';
import {RateLimiterRedis} from 'rate-limiter-flexible';
export default (latus, options) => new RateLimiterRedis({
...options,
storeClient: createClient(latus),
});

View File

@ -0,0 +1,8 @@
// Whilst the configuration object can be modified here, the recommended way of making
// changes is via the presets' options or Neutrino's API in `.neutrinorc.js` instead.
// Neutrino's inspect feature can be used to view/export the generated configuration.
const neutrino = require('neutrino');
const configOfConfigs = require(`${__dirname}/.neutrinorc`);
const configs = Array.isArray(configOfConfigs) ? configOfConfigs : [configOfConfigs];
module.exports = configs.map((config) => neutrino(config).webpack());

6797
packages/governor/yarn.lock Normal file

File diff suppressed because it is too large Load Diff