Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkgs/shelf_router/lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import 'dart:collection' show UnmodifiableMapView;
import 'dart:convert';

import 'package:http_methods/http_methods.dart';
import 'package:meta/meta.dart' show sealed;
import 'package:shelf/shelf.dart';

Expand Down Expand Up @@ -130,7 +129,7 @@ class Router {
/// `HEAD` is always wrong. To explicitely implement a `HEAD` handler it must
/// be registered before the `GET` handler.
void add(String verb, String route, Function handler) {
if (!isHttpMethod(verb)) {
if (!_isHttpMethod(verb)) {
throw ArgumentError.value(verb, 'verb', 'expected a valid HTTP method');
}
verb = verb.toUpperCase();
Expand Down Expand Up @@ -305,3 +304,8 @@ class _RouteNotFoundResponse extends Response {
);
}
}

bool _isHttpMethod(String method) {
return {'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'}
.contains(method.toUpperCase());
Comment on lines +309 to +310
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve performance, you can make the Set a compile-time constant by adding the const keyword. This prevents the set from being re-created on every call to _isHttpMethod.

  return const {'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'}
      .contains(method.toUpperCase());

}
1 change: 0 additions & 1 deletion pkgs/shelf_router/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ environment:
sdk: ^3.3.0

dependencies:
http_methods: ^1.1.0
meta: ^1.3.0
shelf: ^1.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import 'package:analyzer/dart/element/element.dart'
import 'package:analyzer/dart/element/type.dart' show ParameterizedType;
import 'package:build/build.dart' show BuildStep, log;
import 'package:code_builder/code_builder.dart' as code;
import 'package:http_methods/http_methods.dart' show isHttpMethod;
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf_router/shelf_router.dart' as shelf_router;
import 'package:shelf_router/src/router_entry.dart' // ignore: implementation_imports
Expand Down Expand Up @@ -185,7 +184,7 @@ void _typeCheckHandler(_Handler h) {
}

// Check the verb, note that $all is a special value for handling all verbs.
if (!isHttpMethod(h.verb) && h.verb != r'$all') {
if (!_isHttpMethod(h.verb) && h.verb != r'$all') {
throw g.InvalidGenerationSourceError(
'The verb "${h.verb}" used in shelf_router.Route annotation must be '
'a valid HTTP method',
Expand Down Expand Up @@ -337,3 +336,8 @@ void _typeCheckMount(_Handler h) {
);
}
}

bool _isHttpMethod(String method) {
return {'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'}
.contains(method.toUpperCase());
Comment on lines +341 to +342
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve performance, you can make the Set a compile-time constant by adding the const keyword. This prevents the set from being re-created on every call to _isHttpMethod.

Suggested change
return {'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'}
.contains(method.toUpperCase());
return const {'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'}
.contains(method.toUpperCase());

}
1 change: 0 additions & 1 deletion pkgs/shelf_router_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ dependencies:
build: ^4.0.0
build_config: ^1.2.0
code_builder: ^4.2.0
http_methods: ^1.1.0
shelf: ^1.1.0
shelf_router: ^1.1.0
source_gen: ^4.0.1
Expand Down