Skip to content

Commit

Permalink
chore: optimize Request.read
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed Jun 22, 2024
1 parent aa6b818 commit 45f32ab
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 4 additions & 0 deletions packages/dart_frog/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.1.1-wip

- chore: optimize `Request.read()`.

# 1.1.0

- feat: support reuse of nested router ([#393](https://github.com/VeryGoodOpenSource/dart_frog/pull/393))
Expand Down
14 changes: 6 additions & 8 deletions packages/dart_frog/lib/src/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Request {

Request._(this._request);

shelf.Request _request;
final shelf.Request _request;

/// Connection information for the associated HTTP request.
HttpConnectionInfo get connectionInfo {
Expand All @@ -120,16 +120,12 @@ class Request {

/// Returns a [Future] containing the body as a [String].
Future<String> body() async {
const requestBodyKey = 'dart_frog.request.body';
final bodyFromContext =
_request.context[requestBodyKey] as Completer<String>?;
if (bodyFromContext != null) return bodyFromContext.future;
final bodyFromContext = _bodyFutureExpando[_request];
if (bodyFromContext != null) return bodyFromContext;

final completer = Completer<String>();
try {
_request = _request.change(
context: {..._request.context, requestBodyKey: completer},
);
_bodyFutureExpando[_request] = completer.future;
completer.complete(await _request.readAsString());
} catch (error, stackTrace) {
completer.completeError(error, stackTrace);
Expand Down Expand Up @@ -158,3 +154,5 @@ class Request {
return Request._(_request.change(headers: headers, path: path, body: body));
}
}

final _bodyFutureExpando = Expando<Future<String>>('dart_frog.request.body');
2 changes: 1 addition & 1 deletion packages/dart_frog/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dart_frog
description: A fast, minimalistic backend framework for Dart built by Very Good Ventures.
version: 1.1.0
version: 1.1.1-wip
homepage: https://dartfrog.vgv.dev
repository: https://github.com/VeryGoodOpenSource/dart_frog
issue_tracker: https://github.com/VeryGoodOpenSource/dart_frog/issues
Expand Down

0 comments on commit 45f32ab

Please sign in to comment.