Skip to content

Commit

Permalink
build(deps-dev): replace standard with neostandard (#397)
Browse files Browse the repository at this point in the history
* build(deps-dev): replace standard with neostandard

* chore: add eslint.config.js

* fix lint

---------

Co-authored-by: Aras Abbasi <[email protected]>
  • Loading branch information
Fdawgs and Uzlopak authored Dec 8, 2024
1 parent 3c48b42 commit d7741f3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![CI](https://github.com/fastify/fastify-rate-limit/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-rate-limit/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/@fastify/rate-limit.svg?style=flat)](https://www.npmjs.com/package/@fastify/rate-limit)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

A low overhead rate limiter for your routes.

Expand Down
6 changes: 6 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

module.exports = require('neostandard')({
ignores: require('neostandard').resolveIgnoresFromGitignore(),
ts: true
})
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"type": "commonjs",
"types": "types/index.d.ts",
"scripts": {
"lint": "standard",
"lint:fix": "standard --fix",
"lint": "eslint",
"lint:fix": "eslint --fix",
"redis": "docker run -p 6379:6379 --name rate-limit-redis -d --rm redis",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "c8 --100 node --test",
Expand Down Expand Up @@ -36,8 +36,8 @@
"fastify": "^5.0.0",
"ioredis": "^5.4.1",
"knex": "^3.1.0",
"neostandard": "^0.11.9",
"sqlite3": "^5.1.7",
"standard": "^17.1.0",
"tsd": "^0.31.1"
},
"dependencies": {
Expand Down
8 changes: 4 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {
preHandlerAsyncHookHandler,
RouteGenericInterface,
RouteOptions
} from 'fastify';
} from 'fastify'

declare module 'fastify' {
interface FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider> {
rateLimit<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault,
SchemaCompiler extends FastifySchema = FastifySchema,
SchemaCompiler extends FastifySchema = FastifySchema
>(options?: fastifyRateLimit.RateLimitOptions): preHandlerAsyncHookHandler<
RawServer,
RawRequest,
Expand All @@ -32,7 +32,7 @@ declare module 'fastify' {
}
}

type FastifyRateLimit = FastifyPluginCallback<fastifyRateLimit.RateLimitPluginOptions>;
type FastifyRateLimit = FastifyPluginCallback<fastifyRateLimit.RateLimitPluginOptions>

declare namespace fastifyRateLimit {

Expand Down Expand Up @@ -144,5 +144,5 @@ declare namespace fastifyRateLimit {
export { fastifyRateLimit as default }
}

declare function fastifyRateLimit(...params: Parameters<FastifyRateLimit>): ReturnType<FastifyRateLimit>
declare function fastifyRateLimit (...params: Parameters<FastifyRateLimit>): ReturnType<FastifyRateLimit>
export = fastifyRateLimit
50 changes: 31 additions & 19 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,32 @@ import fastify, {
RouteOptions
} from 'fastify'
import * as http2 from 'http2'
import { default as ioredis } from 'ioredis'
import pino from 'pino';
import { default as IORedis } from 'ioredis'
import pino from 'pino'
import fastifyRateLimit, {
errorResponseBuilderContext,
FastifyRateLimitOptions,
FastifyRateLimitStore,
RateLimitPluginOptions
} from '..'
import { expectAssignable, expectType } from 'tsd'

class CustomStore implements FastifyRateLimitStore {
constructor(options: FastifyRateLimitOptions) {}
incr(
options: FastifyRateLimitOptions

constructor (options: FastifyRateLimitOptions) {
this.options = options
}

incr (
key: string,
callback: (
error: Error | null,
result?: { current: number; ttl: number }
) => void
) {}
child(routeOptions: RouteOptions & { path: string; prefix: string }) {

child (routeOptions: RouteOptions & { path: string; prefix: string }) {
return <CustomStore>(<FastifyRateLimitOptions>{})
}
}
Expand All @@ -36,7 +43,7 @@ const options1: RateLimitPluginOptions = {
timeWindow: 5000,
cache: 10000,
allowList: ['127.0.0.1'],
redis: new ioredis({ host: '127.0.0.1' }),
redis: new IORedis({ host: '127.0.0.1' }),
skipOnError: true,
ban: 10,
continueExceeding: false,
Expand All @@ -49,13 +56,13 @@ const options1: RateLimitPluginOptions = {
if (context.ban) {
return {
statusCode: 403,
error: "Forbidden",
error: 'Forbidden',
message: `You can not access this service as you have sent too many requests that exceed your rate limit. Your IP: ${req.ip} and Limit: ${context.max}`,
}
} else {
return {
statusCode: 429,
error: "Too Many Requests",
error: 'Too Many Requests',
message: `You hit the rate limit, please slow down! You can retry in ${context.after}`,
}
}
Expand Down Expand Up @@ -103,7 +110,7 @@ const options5: RateLimitPluginOptions = {
max: 3,
timeWindow: 5000,
cache: 10000,
redis: new ioredis({ host: '127.0.0.1' }),
redis: new IORedis({ host: '127.0.0.1' }),
nameSpace: 'my-namespace'
}

Expand Down Expand Up @@ -144,14 +151,19 @@ const options9: RateLimitPluginOptions = {
appWithImplicitHttp.register(fastifyRateLimit, options1)
appWithImplicitHttp.register(fastifyRateLimit, options2)
appWithImplicitHttp.register(fastifyRateLimit, options5)
appWithImplicitHttp.register(fastifyRateLimit,options9)
appWithImplicitHttp.register(fastifyRateLimit, options9)

appWithImplicitHttp.register(fastifyRateLimit, options3).then(() => {
const preHandler1: preHandlerAsyncHookHandler = appWithImplicitHttp.rateLimit()
const preHandler2: preHandlerAsyncHookHandler = appWithImplicitHttp.rateLimit(options1)
const preHandler3: preHandlerAsyncHookHandler = appWithImplicitHttp.rateLimit(options2)
const preHandler4: preHandlerAsyncHookHandler = appWithImplicitHttp.rateLimit(options3)
const preHandler5: preHandlerAsyncHookHandler = appWithImplicitHttp.rateLimit(options4)
expectType<preHandlerAsyncHookHandler>(appWithImplicitHttp.rateLimit())
expectType<preHandlerAsyncHookHandler>(appWithImplicitHttp.rateLimit(options1))
expectType<preHandlerAsyncHookHandler>(appWithImplicitHttp.rateLimit(options2))
expectType<preHandlerAsyncHookHandler>(appWithImplicitHttp.rateLimit(options3))
expectType<preHandlerAsyncHookHandler>(appWithImplicitHttp.rateLimit(options4))
expectType<preHandlerAsyncHookHandler>(appWithImplicitHttp.rateLimit(options5))
expectType<preHandlerAsyncHookHandler>(appWithImplicitHttp.rateLimit(options6))
expectType<preHandlerAsyncHookHandler>(appWithImplicitHttp.rateLimit(options7))
expectType<preHandlerAsyncHookHandler>(appWithImplicitHttp.rateLimit(options8))
expectType<preHandlerAsyncHookHandler>(appWithImplicitHttp.rateLimit(options9))
// The following test is dependent on https://github.com/fastify/fastify/pull/2929
// appWithImplicitHttp.setNotFoundHandler({
// preHandler: appWithImplicitHttp.rateLimit()
Expand All @@ -160,7 +172,7 @@ appWithImplicitHttp.register(fastifyRateLimit, options3).then(() => {
// })
})

appWithImplicitHttp.get('/', { config: { rateLimit: { max: 10, timeWindow: "60s" } } }, () => { return "limited" })
appWithImplicitHttp.get('/', { config: { rateLimit: { max: 10, timeWindow: '60s' } } }, () => { return 'limited' })

const appWithHttp2: FastifyInstance<
http2.Http2Server,
Expand All @@ -172,11 +184,11 @@ appWithHttp2.register(fastifyRateLimit, options1)
appWithHttp2.register(fastifyRateLimit, options2)
appWithHttp2.register(fastifyRateLimit, options3)
appWithHttp2.register(fastifyRateLimit, options5)
appWithHttp2.register(fastifyRateLimit, options6)
appWithHttp2.register(fastifyRateLimit, options7)
appWithHttp2.register(fastifyRateLimit, options8)
appWithHttp2.register(fastifyRateLimit, options9)


appWithHttp2.get('/public', {
config: {
rateLimit: false
Expand All @@ -185,13 +197,13 @@ appWithHttp2.get('/public', {
reply.send({ hello: 'from ... public' })
})

const errorResponseContext: errorResponseBuilderContext = {
expectAssignable<errorResponseBuilderContext>({
statusCode: 429,
ban: true,
after: '123',
max: 1000,
ttl: 123
}
})

const appWithCustomLogger = fastify({
loggerInstance: pino(),
Expand Down

0 comments on commit d7741f3

Please sign in to comment.