Skip to content

Commit

Permalink
Merge #2: v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
HeySreelal authored Sep 15, 2024
2 parents 7e11cf1 + 4da8598 commit d608564
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.0.2

- Added more documentations
- Fixes here and there

# 1.0.1

- Minor meta info changes
Expand Down
55 changes: 54 additions & 1 deletion lib/shelf_limiter.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
4 changes: 2 additions & 2 deletions lib/src/shelf_limiter_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit d608564

Please sign in to comment.