Aktueller Stand

This commit is contained in:
2026-01-22 22:22:48 +01:00
parent 33e2bc61e2
commit fa5f3808bb
169 changed files with 58567 additions and 25460 deletions

View File

@@ -0,0 +1,116 @@
'use strict'
import Redis from 'ioredis'
import Fastify from 'fastify'
const redis = new Redis({
enableAutoPipelining: true,
connectionName: 'my-connection-name',
host: 'localhost',
port: 6379,
connectTimeout: 500,
maxRetriesPerRequest: 1
})
const fastify = Fastify()
await fastify.register(import('../index.js'),
{
global: false,
max: 3000, // default max rate limit
// timeWindow: 1000*60,
// cache: 10000,
allowList: ['127.0.0.2'], // global allowList access ( ACL based on the key from the keyGenerator)
redis, // connection to redis
skipOnError: false // default false
// keyGenerator: function(req) { /* ... */ }, // default (req) => req.raw.ip
})
fastify.get('/', {
config: {
rateLimit: {
max: 3,
timeWindow: '1 minute'
}
}
}, (_req, reply) => {
reply.send({ hello: 'from ... root' })
})
fastify.get('/private', {
config: {
rateLimit: {
max: 3,
allowList: ['127.0.2.1', '127.0.3.1'],
timeWindow: '1 minute'
}
}
}, (_req, reply) => {
reply.send({ hello: 'from ... private' })
})
fastify.get('/public', (_req, reply) => {
reply.send({ hello: 'from ... public' })
})
fastify.get('/public/sub-rated-1', {
config: {
rateLimit: {
timeWindow: '1 minute',
allowList: ['127.0.2.1'],
onExceeding: function () {
console.log('callback on exceededing ... executed before response to client. req is give as argument')
},
onExceeded: function () {
console.log('callback on exceeded ... to black ip in security group for example, req is give as argument')
}
}
}
}, (_req, reply) => {
reply.send({ hello: 'from sub-rated-1 ... using default max value ... ' })
})
fastify.get('/public/sub-rated-2', {
config: {
rateLimit: {
max: 3,
timeWindow: '1 minute',
onExceeding: function () {
console.log('callback on exceededing ... executed before response to client. req is give as argument')
},
onExceeded: function () {
console.log('callback on exceeded ... to black ip in security group for example, req is give as argument')
}
}
}
}, (_req, reply) => {
reply.send({ hello: 'from ... sub-rated-2' })
})
fastify.get('/home', {
config: {
rateLimit: {
max: 200,
timeWindow: '1 minute'
}
}
}, (_req, reply) => {
reply.send({ hello: 'toto' })
})
fastify.get('/customerrormessage', {
config: {
rateLimit: {
max: 2,
timeWindow: '1 minute',
errorResponseBuilder: (_req, context) => ({ code: 429, timeWindow: context.after, limit: context.max })
}
}
}, (_req, reply) => {
reply.send({ hello: 'toto' })
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
console.log('Server listening at http://localhost:3000')
})

View File

@@ -0,0 +1,124 @@
'use strict'
/* eslint-disable no-undef */
// Example of a custom store using Knex.js and MySQL.
//
// Assumes you have access to a configured knex object.
//
// Note that the rate check should place a read lock on the row.
// For MySQL see:
// https://dev.mysql.com/doc/refman/8.0/en/innodb-locking-reads.html
// https://blog.nodeswat.com/concurrency-mysql-and-node-js-a-journey-of-discovery-31281e53572e
//
// Below is an example table to store rate limits that must be created
// in the database first.
//
// exports.up = async knex => {
// await knex.schema.createTable('rate_limits', table => {
// table.string('source').notNullable()
// table.string('route').notNullable()
// table.integer('count').unsigned()
// table.bigInteger ('ttl')
// table.primary(['route', 'source'])
// })
// }
//
// exports.down = async knex => {
// await knex.schema.dropTable('rate_limits')
// }
//
// CREATE TABLE `rate_limits` (
// `source` varchar(255) NOT NULL,
// `route` varchar(255) NOT NULL,
// `count` int unsigned DEFAULT NULL,
// `ttl` int unsigned DEFAULT NULL,
// PRIMARY KEY (`route`,`source`)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
function KnexStore (options) {
this.options = options
this.route = ''
}
KnexStore.prototype.routeKey = function (route) {
if (route) this.route = route
return route
}
KnexStore.prototype.incr = async function (key, cb) {
const now = (new Date()).getTime()
const ttl = now + this.options.timeWindow
const max = this.options.max
const cond = { route: this.route, source: key }
const trx = await knex.transaction()
try {
// NOTE: MySQL syntax FOR UPDATE for read lock on counter stats in row
const row = await trx('rate_limits')
.whereRaw('route = ? AND source = ? FOR UPDATE', [cond.route || '', cond.source]) // Create read lock
const d = row[0]
if (d && d.ttl > now) {
// Optimization - no need to UPDATE if max has been reached.
if (d.count < max) {
await trx
.raw('UPDATE rate_limits SET count = ? WHERE route = ? AND source = ?', [d.count + 1, cond.route, key])
}
// If we were already at max no need to UPDATE but we must still send d.count + 1 to trigger rate limit.
process.nextTick(cb, null, { current: d.count + 1, ttl: d.ttl })
} else {
await trx
.raw('INSERT INTO rate_limits(route, source, count, ttl) VALUES(?,?,1,?) ON DUPLICATE KEY UPDATE count = 1, ttl = ?', [cond.route, key, d?.ttl || ttl, ttl])
process.nextTick(cb, null, { current: 1, ttl: d?.ttl || ttl })
}
await trx.commit()
} catch (err) {
await trx.rollback()
// TODO: Handle as desired
fastify.log.error(err)
process.nextTick(cb, err, { current: 0 })
}
}
KnexStore.prototype.child = function (routeOptions = {}) {
// NOTE: Optionally override and set global: false here for route specific
// options, which then allows you to use `global: true` should you
// wish to during initial registration below.
const options = { ...this.options, ...routeOptions, global: false }
const store = new KnexStore(options)
store.routeKey(routeOptions.routeInfo.method + routeOptions.routeInfo.url)
return store
}
fastify.register(require('../../fastify-rate-limit'),
{
global: false,
max: 10,
store: KnexStore,
skipOnError: false
}
)
fastify.get('/', {
config: {
rateLimit: {
max: 10,
timeWindow: '1 minute'
}
}
}, (_req, reply) => {
reply.send({ hello: 'from ... root' })
})
fastify.get('/private', {
config: {
rateLimit: {
max: 3,
timeWindow: '1 minute'
}
}
}, (_req, reply) => {
reply.send({ hello: 'from ... private' })
})
fastify.get('/public', (_req, reply) => {
reply.send({ hello: 'from ... public' })
})

View File

@@ -0,0 +1,118 @@
'use strict'
// Example of a Custom Store using Knex.js ORM for SQLite database
// Below is an example table to store rate limits that must be created
// in the database first
//
// CREATE TABLE "RateLimits" (
// "Route" TEXT,
// "Source" TEXT,
// "Count" INTEGER,
// "TTL" NUMERIC,
// PRIMARY KEY("Source")
// );
//
// CREATE UNIQUE INDEX "idx_uniq_route_source" ON "RateLimits" (Route, Source);
//
const Knex = require('knex')
const fastify = require('fastify')()
const knex = Knex({
client: 'sqlite3',
connection: {
filename: './db.sqlite'
}
})
function KnexStore (options) {
this.options = options
this.route = ''
}
KnexStore.prototype.routeKey = function (route) {
if (route) {
this.route = route
} else {
return route
}
}
KnexStore.prototype.incr = function (key, cb) {
const now = (new Date()).getTime()
const ttl = now + this.options.timeWindow
knex.transaction(function (trx) {
trx
.where({ Route: this.route, Source: key })
.then(d => {
if (d.TTL > now) {
trx
.raw(`UPDATE RateLimits SET Count = 1 WHERE Route='${this.route}' AND Source='${key}'`)
.then(() => {
cb(null, { current: 1, ttl: d.TTL })
})
.catch(err => {
cb(err, { current: 0 })
})
} else {
trx
.raw(`INSERT INTO RateLimits(Route, Source, Count, TTL) VALUES('${this.route}', '${key}',1,${d.TTL || ttl}) ON CONFLICT(Route, Source) DO UPDATE SET Count=Count+1,TTL=${ttl}`)
.then(() => {
cb(null, { current: d.Count ? d.Count + 1 : 1, ttl: d.TTL || ttl })
})
.catch(err => {
cb(err, { current: 0 })
})
}
})
.catch(err => {
cb(err, { current: 0 })
})
})
}
KnexStore.prototype.child = function (routeOptions) {
const options = Object.assign(this.options, routeOptions)
const store = new KnexStore(options)
store.routeKey(routeOptions.routeInfo.method + routeOptions.routeInfo.url)
return store
}
fastify.register(require('../../fastify-rate-limit'),
{
global: false,
max: 10,
store: KnexStore,
skipOnError: false
}
)
fastify.get('/', {
config: {
rateLimit: {
max: 10,
timeWindow: '1 minute'
}
}
}, (_req, reply) => {
reply.send({ hello: 'from ... root' })
})
fastify.get('/private', {
config: {
rateLimit: {
max: 3,
timeWindow: '1 minute'
}
}
}, (_req, reply) => {
reply.send({ hello: 'from ... private' })
})
fastify.get('/public', (_req, reply) => {
reply.send({ hello: 'from ... public' })
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
console.log('Server listening at http://localhost:3000')
})

View File

@@ -0,0 +1,185 @@
'use strict'
// Example of a Custom Store using Sequelize ORM for PostgreSQL database
// Sequelize Migration for "RateLimits" table
//
// module.exports = {
// up: (queryInterface, { TEXT, INTEGER, BIGINT }) => {
// return queryInterface.createTable(
// 'RateLimits',
// {
// Route: {
// type: TEXT,
// allowNull: false
// },
// Source: {
// type: TEXT,
// allowNull: false,
// primaryKey: true
// },
// Count: {
// type: INTEGER,
// allowNull: false
// },
// TTL: {
// type: BIGINT,
// allowNull: false
// }
// },
// {
// freezeTableName: true,
// timestamps: false,
// uniqueKeys: {
// unique_tag: {
// customIndex: true,
// fields: ['Route', 'Source']
// }
// }
// }
// )
// },
// down: queryInterface => {
// return queryInterface.dropTable('RateLimits')
// }
// }
const fastify = require('fastify')()
const Sequelize = require('sequelize')
const databaseUri = 'postgres://username:password@localhost:5432/fastify-rate-limit-example'
const sequelize = new Sequelize(databaseUri)
// OR
// const sequelize = new Sequelize('database', 'username', 'password');
// Sequelize Model for "RateLimits" table
//
const RateLimits = sequelize.define(
'RateLimits',
{
Route: {
type: Sequelize.TEXT,
allowNull: false
},
Source: {
type: Sequelize.TEXT,
allowNull: false,
primaryKey: true
},
Count: {
type: Sequelize.INTEGER,
allowNull: false
},
TTL: {
type: Sequelize.BIGINT,
allowNull: false
}
},
{
freezeTableName: true,
timestamps: false,
indexes: [
{
unique: true,
fields: ['Route', 'Source']
}
]
}
)
function RateLimiterStore (options) {
this.options = options
this.route = ''
}
RateLimiterStore.prototype.routeKey = function routeKey (route) {
if (route) this.route = route
return route
}
RateLimiterStore.prototype.incr = async function incr (key, cb) {
const now = new Date().getTime()
const ttl = now + this.options.timeWindow
const cond = { Route: this.route, Source: key }
const RateLimit = await RateLimits.findOne({ where: cond })
if (RateLimit && parseInt(RateLimit.TTL, 10) > now) {
try {
await RateLimit.update({ Count: RateLimit.Count + 1 }, cond)
cb(null, {
current: RateLimit.Count + 1,
ttl: RateLimit.TTL
})
} catch (err) {
cb(err, {
current: 0
})
}
} else {
sequelize.query(
`INSERT INTO "RateLimits"("Route", "Source", "Count", "TTL")
VALUES('${this.route}', '${key}', 1,
${RateLimit?.TTL || ttl})
ON CONFLICT("Route", "Source") DO UPDATE SET "Count"=1, "TTL"=${ttl}`
)
.then(() => {
cb(null, {
current: 1,
ttl: RateLimit?.TTL || ttl
})
})
.catch(err => {
cb(err, {
current: 0
})
})
}
}
RateLimiterStore.prototype.child = function child (routeOptions = {}) {
const options = Object.assign(this.options, routeOptions)
const store = new RateLimiterStore(options)
store.routeKey(routeOptions.routeInfo.method + routeOptions.routeInfo.url)
return store
}
fastify.register(require('../../fastify-rate-limit'),
{
global: false,
max: 10,
store: RateLimiterStore,
skipOnError: false
}
)
fastify.get('/', {
config: {
rateLimit: {
max: 10,
timeWindow: '1 minute'
}
}
}, (_req, reply) => {
reply.send({ hello: 'from ... root' })
})
fastify.get('/private', {
config: {
rateLimit: {
max: 3,
timeWindow: '1 minute'
}
}
}, (_req, reply) => {
reply.send({ hello: 'from ... private' })
})
fastify.get('/public', (_req, reply) => {
reply.send({ hello: 'from ... public' })
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
console.log('Server listening at http://localhost:3000')
})

View File

@@ -0,0 +1,25 @@
import fastify from 'fastify'
import fastifyRateLimit from '../index.js'
const server = fastify()
await server.register(fastifyRateLimit, {
global: true,
max: 10000,
timeWindow: '1 minute'
})
server.get('/', (_request, reply) => {
reply.send('Hello, world!')
})
const start = async () => {
try {
await server.listen({ port: 3000 })
console.log('Server is running on port 3000')
} catch (error) {
console.error('Error starting server:', error)
}
}
start()

View File

@@ -0,0 +1,113 @@
'use strict'
const Redis = require('ioredis')
const redis = new Redis({
connectionName: 'my-connection-name',
host: 'localhost',
port: 6379,
connectTimeout: 500,
maxRetriesPerRequest: 1
})
const fastify = require('fastify')()
fastify.register(require('../../fastify-rate-limit'),
{
global: false,
max: 3000, // default max rate limit
// timeWindow: 1000*60,
// cache: 10000,
allowList: ['127.0.0.2'], // global allowList access ( ACL based on the key from the keyGenerator)
redis, // connection to redis
skipOnError: false // default false
// keyGenerator: function(req) { /* ... */ }, // default (req) => req.raw.ip
})
fastify.get('/', {
config: {
rateLimit: {
max: 3,
timeWindow: '1 minute'
}
}
}, (_req, reply) => {
reply.send({ hello: 'from ... root' })
})
fastify.get('/private', {
config: {
rateLimit: {
max: 3,
allowList: ['127.0.2.1', '127.0.3.1'],
timeWindow: '1 minute'
}
}
}, (_req, reply) => {
reply.send({ hello: 'from ... private' })
})
fastify.get('/public', (_req, reply) => {
reply.send({ hello: 'from ... public' })
})
fastify.get('/public/sub-rated-1', {
config: {
rateLimit: {
timeWindow: '1 minute',
allowList: ['127.0.2.1'],
onExceeding: function () {
console.log('callback on exceededing ... executed before response to client. req is give as argument')
},
onExceeded: function () {
console.log('callback on exceeded ... to black ip in security group for example, req is give as argument')
}
}
}
}, (_req, reply) => {
reply.send({ hello: 'from sub-rated-1 ... using default max value ... ' })
})
fastify.get('/public/sub-rated-2', {
config: {
rateLimit: {
max: 3,
timeWindow: '1 minute',
onExceeding: function () {
console.log('callback on exceededing ... executed before response to client. req is give as argument')
},
onExceeded: function () {
console.log('callback on exceeded ... to black ip in security group for example, req is give as argument')
}
}
}
}, (_req, reply) => {
reply.send({ hello: 'from ... sub-rated-2' })
})
fastify.get('/home', {
config: {
rateLimit: {
max: 200,
timeWindow: '1 minute'
}
}
}, (_req, reply) => {
reply.send({ hello: 'toto' })
})
fastify.get('/customerrormessage', {
config: {
rateLimit: {
max: 2,
timeWindow: '1 minute',
errorResponseBuilder: (_req, context) => ({ code: 429, timeWindow: context.after, limit: context.max })
}
}
}, (_req, reply) => {
reply.send({ hello: 'toto' })
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
console.log('Server listening at http://localhost:3000')
})