diff --git a/CHANGELOG.md b/CHANGELOG.md index f1157295..cb9d4309 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,4 +31,6 @@ replaced with `parseBody`, `parseUploadedFiles`, and `parseRawRequestBuffer`, re as well as `query`. * Removed `Angel.injections` and `RequestContext.injections`. * Removed `Angel.inject`. -* Removed a dependency on `package:pool`, which also meant removing `AngelHttp.throttle`. \ No newline at end of file +* Removed a dependency on `package:pool`, which also meant removing `AngelHttp.throttle`. +* Remove the `RequestMiddleware` typedef; from now on, one should use `ResponseContext.end` +exclusively to close responses. \ No newline at end of file diff --git a/lib/src/core/routable.dart b/lib/src/core/routable.dart index 14fc90a2..2fa9fac2 100644 --- a/lib/src/core/routable.dart +++ b/lib/src/core/routable.dart @@ -1,7 +1,9 @@ library angel_framework.http.routable; import 'dart:async'; + import 'package:angel_route/angel_route.dart'; + import '../util.dart'; import 'hooked_service.dart'; import 'metadata.dart'; @@ -12,32 +14,29 @@ import 'service.dart'; final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)'); -/// A function that intercepts a request and determines whether handling of it should continue. -typedef Future RequestMiddleware(RequestContext req, ResponseContext res); - /// A function that receives an incoming [RequestContext] and responds to it. typedef FutureOr RequestHandler(RequestContext req, ResponseContext res); /// Sequentially runs a list of [handlers] of middleware, and returns early if any does not /// return `true`. Works well with [Router].chain. -RequestMiddleware waterfall(List handlers) { +RequestHandler waterfall(Iterable handlers) { return (req, res) { - Future Function() runPipeline; + Future Function() runPipeline; for (var handler in handlers) { if (handler == null) break; if (runPipeline == null) - runPipeline = () => req.app.executeHandler(handler, req, res); + runPipeline = () => Future.sync(() => handler(req, res)); else { var current = runPipeline; - runPipeline = () => current().then((result) => !result + runPipeline = () => current().then((result) => !res.isOpen ? new Future.value(result) : req.app.executeHandler(handler, req, res)); } } - runPipeline ??= () => new Future.value(true); + runPipeline ??= () => new Future.value(); return runPipeline(); }; } @@ -58,7 +57,8 @@ class Routable extends Router { /// Additional filters to be run on designated requests. @override - final Map requestMiddleware = {}; + final Map requestMiddleware = + {}; /// A set of [Service] objects that have been mapped into routes. Map get services => _services;