dart-lang / dart-lang/http

send is not a good middleware function

Open
#441 2 comments 18 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
1.1k
Forks
419
Avg merge
4d 14h
Merged PRs (30d)
8

Description

The documentation for package:http says that send is the place to override if you want to intercept and modify requests. It advertises it as a middleware solution, and gives an example at the bottom of the README.

The problem is that the example only intercepts the request, and not the response. To be able to use the response in middleware, say, to read the content-type and decode the body according to what you get, you have to read the stream, but also return a StreamedResponse. But, the StreamedResponse only allows one listener, and that listener is taken up by the _sendUnstreamed function which all the other functions call. So, to do this using the current interface, you would have to convert the stream to a broadcast stream in a middleware, but know that only one middleware is doing that since you can't create multiple broadcasters on a single stream. Quite ugly.

The whole interface doesn't really make sense, since everything is being first funneled to _sendUnstreamed, but we are required to override something that returns a StreamedResponse. If we are already funneling everthing through something that waits for the stream to complete, that is the place we should be overriding.

So, basically I am asking for _sendUnstreamed to be made public and overridable, and that function is really what middleware should be wrapping.

Contributor guide

Open the contributing guide

Research direction

Start with the package:http README middleware example and trace how send, _sendUnstreamed, and StreamedResponse interact. Compare the current request-only interception with the requested response-aware middleware design; done means the interface supports wrapping completed responses without requiring awkward stream broadcasting.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.