From e2e50db861d28f6e88382f208432448d4a17fd8d Mon Sep 17 00:00:00 2001 From: Sreelal TS Date: Sun, 15 Sep 2024 12:42:17 +0530 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=A7=91=F0=9F=8F=BB=E2=80=8D=E2=9C=88?= =?UTF-8?q?=EF=B8=8F=20Docs=20and=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/shelf_limiter.dart | 55 ++++++++++++++++++++++++++++++++- lib/src/shelf_limiter_base.dart | 4 +-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/lib/shelf_limiter.dart b/lib/shelf_limiter.dart index ec0d353..83bc8b2 100644 --- a/lib/shelf_limiter.dart +++ b/lib/shelf_limiter.dart @@ -1,8 +1,61 @@ +/// # Shelf Limiter +/// +/// A powerful and highly customizable rate limiter for shelf library, allowing you to easily manage and control request rates in your Dart server. +/// +/// `shelf_limiter` is a middleware package for the [Shelf](https://pub.dev/packages/shelf) +/// library in Dart that provides rate limiting capabilities. It allows you to restrict +/// the number of requests a client can make to your server within a specified time window, +/// making it useful for preventing abuse and ensuring fair usage of your API. +/// +/// This package offers several customization options to suit your needs, including custom +/// headers and responses. It integrates seamlessly into your existing Shelf pipeline. +/// +/// ## Example +/// +/// ```dart +/// import 'dart:convert'; +/// import 'package:shelf/shelf.dart'; +/// import 'package:shelf/shelf_io.dart' as io; +/// import 'package:shelf_limiter/shelf_limiter.dart'; +/// +/// void main() async { +/// final options = RateLimiterOptions( +/// headers: { +/// 'X-Custom-Header': 'Rate limited', +/// }, +/// onRateLimitExceeded: (request) async { +/// return Response( +/// 429, +/// body: jsonEncode({ +/// 'status': false, +/// 'message': "Uh, hm! Wait a minute, that's a lot of request.", +/// }), +/// headers: { +/// 'Content-Type': 'application/json', +/// }, +/// ); +/// }, +/// ); +/// +/// final limiter = shelfLimiter( +/// maxRequests: 5, +/// windowSize: const Duration(minutes: 1), +/// options: options, +/// ); +/// +/// final handler = +/// const Pipeline().addMiddleware(limiter).addHandler(_echoRequest); +/// +/// var server = await io.serve(handler, 'localhost', 8080); +/// print('Server listening on port ${server.port}'); +/// } +/// +/// Response _echoRequest(Request request) => Response.ok('Request received'); +/// ``` library; import 'dart:async'; import 'dart:collection'; -import 'dart:io'; import 'package:shelf/shelf.dart'; part 'src/shelf_limiter_base.dart'; diff --git a/lib/src/shelf_limiter_base.dart b/lib/src/shelf_limiter_base.dart index e3a15f4..74c3405 100644 --- a/lib/src/shelf_limiter_base.dart +++ b/lib/src/shelf_limiter_base.dart @@ -89,8 +89,8 @@ Middleware shelfLimiter({ // Extract client identifier (IP by default) final clientIdentifier = options?.clientIdentifierExtractor != null ? options!.clientIdentifierExtractor!(request) - : (request.context['shelf.io.connection_info'] as HttpConnectionInfo) - .remoteAddress + : ((request.context['shelf.io.connection_info']) as dynamic) + ?.remoteAddress .address; // Check if the client has exceeded the rate limit From 4da85987e188eea18ff471c8825ea6b32c47b787 Mon Sep 17 00:00:00 2001 From: Sreelal TS Date: Sun, 15 Sep 2024 12:42:26 +0530 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=9A=94=20Version=20update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +++++ pubspec.yaml | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 674a7f5..0a11568 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 1.0.2 + +- Added more documentations +- Fixes here and there + # 1.0.1 - Minor meta info changes diff --git a/pubspec.yaml b/pubspec.yaml index 1257654..c43433c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: shelf_limiter description: A powerful and highly customizable rate limiter for shelf library, allowing you to easily manage and control request rates in your Dart server. -version: 1.0.1 +version: 1.0.2 repository: https://github.com/xooniverse/shelf_limiter issue_tracker: https://github.com/xooniverse/shelf_limiter/issues documentation: https://pub.dev/documentation/shelf_limiter/latest