add(angel3): adding re-branded angel3 framework package
This commit is contained in:
parent
78e6f36d2c
commit
9148934aa6
96 changed files with 10296 additions and 0 deletions
12
packages/framework/AUTHORS.md
Normal file
12
packages/framework/AUTHORS.md
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
Primary Authors
|
||||||
|
===============
|
||||||
|
|
||||||
|
* __[Thomas Hii](dukefirehawk.apps@gmail.com)__
|
||||||
|
|
||||||
|
Thomas is the current maintainer of the code base. He has refactored and migrated the
|
||||||
|
code base to support NNBD.
|
||||||
|
|
||||||
|
* __[Tobe O](thosakwe@gmail.com)__
|
||||||
|
|
||||||
|
Tobe has written much of the original code prior to NNBD migration. He has moved on and
|
||||||
|
is no longer involved with the project.
|
452
packages/framework/CHANGELOG.md
Normal file
452
packages/framework/CHANGELOG.md
Normal file
|
@ -0,0 +1,452 @@
|
||||||
|
# Change Log
|
||||||
|
|
||||||
|
## 8.4.0
|
||||||
|
|
||||||
|
* Require Dart >= 3.3
|
||||||
|
* Updated `lints` to 4.0.0
|
||||||
|
|
||||||
|
## 8.3.2
|
||||||
|
|
||||||
|
* Updated README
|
||||||
|
|
||||||
|
## 8.3.1
|
||||||
|
|
||||||
|
* Updated repository link
|
||||||
|
|
||||||
|
## 8.3.0
|
||||||
|
|
||||||
|
* Updated `lints` to 3.0.0
|
||||||
|
* Fixed linter warnings
|
||||||
|
|
||||||
|
## 8.2.0
|
||||||
|
|
||||||
|
* Add `addResponseHeader` to `AngelHttp` to add headers to HTTP default response
|
||||||
|
* Add `removeResponseHeader` to `AngelHttp` to remove headers from HTTP default response
|
||||||
|
|
||||||
|
## 8.1.1
|
||||||
|
|
||||||
|
* Updated broken image on README
|
||||||
|
|
||||||
|
## 8.1.0
|
||||||
|
|
||||||
|
* Updated `uuid` to 4.0.0
|
||||||
|
|
||||||
|
## 8.0.0
|
||||||
|
|
||||||
|
* Require Dart >= 3.0
|
||||||
|
* Updated `http` to 1.0.0
|
||||||
|
|
||||||
|
## 7.0.4
|
||||||
|
|
||||||
|
* Updated `Expose` fields to non-nullable
|
||||||
|
* Updated `Controller` to use non-nullable field
|
||||||
|
|
||||||
|
## 7.0.3
|
||||||
|
|
||||||
|
* Fixed issue #83. Allow Http request to return null headers instead of throwing an exception.
|
||||||
|
|
||||||
|
## 7.0.2
|
||||||
|
|
||||||
|
* Added performance benchmark to README
|
||||||
|
|
||||||
|
## 7.0.1
|
||||||
|
|
||||||
|
* Fixed `BytesBuilder` warnings
|
||||||
|
|
||||||
|
## 7.0.0
|
||||||
|
|
||||||
|
* Require Dart >= 2.17
|
||||||
|
|
||||||
|
## 6.0.0
|
||||||
|
|
||||||
|
* Require Dart >= 2.16
|
||||||
|
* Updated `container` to non nullable
|
||||||
|
* Updated `angel` to non nullable
|
||||||
|
* Updated `logger` to non nullable
|
||||||
|
* Refactored error handler
|
||||||
|
|
||||||
|
## 5.0.0
|
||||||
|
|
||||||
|
* Skipped release
|
||||||
|
|
||||||
|
## 4.2.4
|
||||||
|
|
||||||
|
* Fixed issue 48. Log not working in development
|
||||||
|
|
||||||
|
## 4.2.3
|
||||||
|
|
||||||
|
* Fixed `res.json()` throwing bad state exception
|
||||||
|
|
||||||
|
## 4.2.2
|
||||||
|
|
||||||
|
* Added `Date` to response header
|
||||||
|
* Updated `Server: Angel3` response header
|
||||||
|
|
||||||
|
## 4.2.1
|
||||||
|
|
||||||
|
* Updated `package:angel3_container`
|
||||||
|
|
||||||
|
## 4.2.0
|
||||||
|
|
||||||
|
* Updated to `package:belatuk_combinator`
|
||||||
|
* Updated to `package:belatuk_merge_map`
|
||||||
|
* Updated linter to `package:lints`
|
||||||
|
|
||||||
|
## 4.1.3
|
||||||
|
|
||||||
|
* Updated README
|
||||||
|
|
||||||
|
## 4.1.2
|
||||||
|
|
||||||
|
* Updated README
|
||||||
|
* Fixed NNBD issues
|
||||||
|
|
||||||
|
## 4.1.1
|
||||||
|
|
||||||
|
* Updated link to `Angel3` home page
|
||||||
|
* Fixed pedantic warnings
|
||||||
|
|
||||||
|
## 4.1.0
|
||||||
|
|
||||||
|
* Replaced `http_server` with `belatuk_http_server`
|
||||||
|
|
||||||
|
## 4.0.4
|
||||||
|
|
||||||
|
* Fixed response returning incorrect status code
|
||||||
|
|
||||||
|
## 4.0.3
|
||||||
|
|
||||||
|
* Fixed "Primitive after parsed param injection" test case
|
||||||
|
* Fixed "Cannot remove all unless explicitly set" test case
|
||||||
|
* Fixed "null" test case
|
||||||
|
|
||||||
|
## 4.0.2
|
||||||
|
|
||||||
|
* Updated README
|
||||||
|
|
||||||
|
## 4.0.1
|
||||||
|
|
||||||
|
* Updated README
|
||||||
|
|
||||||
|
## 4.0.0
|
||||||
|
|
||||||
|
* Migrated to support Dart >= 2.12 NNBD
|
||||||
|
|
||||||
|
## 3.0.0
|
||||||
|
|
||||||
|
* Migrated to work with Dart >= 2.12 Non NNBD
|
||||||
|
|
||||||
|
## 2.1.1
|
||||||
|
|
||||||
|
* `AngelHttp.uri` now returns an empty `Uri` if the server is not listening.
|
||||||
|
|
||||||
|
## 2.1.0
|
||||||
|
|
||||||
|
* This release was originally planned to be `2.0.5`, but it adds several features, and has
|
||||||
|
therefore been bumped to `2.1.0`.
|
||||||
|
* Fix a new (did not appear before 2.6/2.7) type error causing compilation to fail.
|
||||||
|
<https://github.com/angel-dart/framework/issues/249>
|
||||||
|
|
||||||
|
## 2.0.5-beta
|
||||||
|
|
||||||
|
* Make `@Expose()` in `Controller` optional. <https://github.com/angel-dart/angel/issues/107>
|
||||||
|
* Add `allowHttp1` to `AngelHttp2` constructors. <https://github.com/angel-dart/angel/issues/108>
|
||||||
|
* Add `deserializeBody` and `decodeBody` to `RequestContext`. <https://github.com/angel-dart/angel/issues/109>
|
||||||
|
* Add `HostnameRouter`, which allows for routing based on hostname. <https://github.com/angel-dart/angel/issues/110>
|
||||||
|
* Default to using `ThrowingReflector`, instead of `EmptyReflector`. This will give a more descriptive
|
||||||
|
error when trying to use controllers, etc. without reflection enabled.
|
||||||
|
* `mountController` returns the mounted controller.
|
||||||
|
|
||||||
|
## 2.0.4+1
|
||||||
|
|
||||||
|
* Run `Controller.configureRoutes` before mounting `@Expose` routes.
|
||||||
|
* Make `Controller.configureServer` always return a `Future`.
|
||||||
|
|
||||||
|
## 2.0.4
|
||||||
|
|
||||||
|
* Prepare for Dart SDK change to `Stream<List<int>>` that are now
|
||||||
|
`Stream<Uint8List>`.
|
||||||
|
* Accept any content type if accept header is missing. See
|
||||||
|
[this PR](https://github.com/angel-dart/framework/pull/239).
|
||||||
|
|
||||||
|
## 2.0.3
|
||||||
|
|
||||||
|
* Patch up a bug caused by an upstream change to Dart's stream semantics.
|
||||||
|
See more: <https://github.com/angel-dart/angel/issues/106#issuecomment-499564485>
|
||||||
|
|
||||||
|
## 2.0.2+1
|
||||||
|
|
||||||
|
* Fix a bug in the implementation of `Controller.applyRoutes`.
|
||||||
|
|
||||||
|
## 2.0.2
|
||||||
|
|
||||||
|
* Make `ResponseContext` *explicitly* implement `StreamConsumer` (though technically it already did???)
|
||||||
|
* Split `Controller.configureServer` to create `Controller.applyRoutes`.
|
||||||
|
|
||||||
|
## 2.0.1
|
||||||
|
|
||||||
|
* Tracked down a bug in `Driver.runPipeline` that allowed fallback
|
||||||
|
handlers to run, even after the response was closed.
|
||||||
|
* Add `RequestContext.shutdownHooks`.
|
||||||
|
* Call `RequestContext.close` in `Driver.sendResponse`.
|
||||||
|
* AngelConfigurer is now `FutureOr<void>`, instead of just `FutureOr`.
|
||||||
|
* Use a `Container.has<Stopwatch>` check in `Driver.sendResponse`.
|
||||||
|
* Remove unnecessary `new` and `const`.
|
||||||
|
|
||||||
|
## 2.0.0
|
||||||
|
|
||||||
|
* Angel 2! :angel: :rocket:
|
||||||
|
|
||||||
|
## 2.0.0-rc.10
|
||||||
|
|
||||||
|
* Fix an error that prevented `AngelHttp2.custom` from working properly.
|
||||||
|
* Add `startSharedHttp2`.
|
||||||
|
|
||||||
|
## 2.0.0-rc.9
|
||||||
|
|
||||||
|
* Fix some bugs in the `HookedService` implementation that skipped
|
||||||
|
the outputs of `before` events.
|
||||||
|
|
||||||
|
## 2.0.0-rc.8
|
||||||
|
|
||||||
|
* Fix `MapService` flaw where clients could remove all records, even if `allowRemoveAll` were `false`.
|
||||||
|
|
||||||
|
## 2.0.0-rc.7
|
||||||
|
|
||||||
|
* `AnonymousService` can override `readData`.
|
||||||
|
* `Service.map` now overrides `readData`.
|
||||||
|
* `HookedService.readData` forwards to `inner`.
|
||||||
|
|
||||||
|
## 2.0.0-rc.6
|
||||||
|
|
||||||
|
* Make `redirect` and `download` methods asynchronous.
|
||||||
|
|
||||||
|
## 2.0.0-rc.5
|
||||||
|
|
||||||
|
* Make `serializer` `FutureOr<String> Function(Object)`.
|
||||||
|
* Make `ResponseContext.serialize` return `Future<bool>`.
|
||||||
|
|
||||||
|
## 2.0.0-rc.4
|
||||||
|
|
||||||
|
* Support resolution of asynchronous injections in controllers and `ioc`.
|
||||||
|
* Inject `RequestContext` and `ResponseContext` into requests.
|
||||||
|
|
||||||
|
## 2.0.0-rc.3
|
||||||
|
|
||||||
|
* `MapService.modify` was not actually modifying items.
|
||||||
|
|
||||||
|
## 2.0.0-rc.2
|
||||||
|
|
||||||
|
* Fixes Pub analyzer lints (see `angel_route@3.0.6`)
|
||||||
|
|
||||||
|
## 2.0.0-rc.1
|
||||||
|
|
||||||
|
* Fix logic error that allowed content to be written to streaming responses after `close` was closed.
|
||||||
|
|
||||||
|
## 2.0.0-rc.0
|
||||||
|
|
||||||
|
* Log a warning when no `reflector` is provided.
|
||||||
|
* Add `AngelEnvironment` class.
|
||||||
|
* Add `Angel.environment`.
|
||||||
|
* Deprecated `app.isProduction` in favor of `app.environment.isProduction`.
|
||||||
|
* Allow setting of `bodyAsObject`, `bodyAsMap`, or `bodyAsList` **exactly once**.
|
||||||
|
* Resolve named singletons in `resolveInjection`.
|
||||||
|
* Fix a bug where `Service.parseId<double>` would attempt to parse an `int`.
|
||||||
|
* Replace as Data cast in Service.dart with a method that throws a 400 on error.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.24
|
||||||
|
|
||||||
|
* Add `AngelEnv` class to `core`.
|
||||||
|
* Deprecate `Angel.isProduction`, in favor of `AngelEnv`.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.23
|
||||||
|
|
||||||
|
* `ResponseContext.render` sets `charset` to `utf8` in `contentType`.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.22
|
||||||
|
|
||||||
|
* Update pipeline handling mechanism, and inject a `MiddlewarePipelineIterator`.
|
||||||
|
* This allows routes to know where in the resolution process they exist, at runtime.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.21
|
||||||
|
|
||||||
|
* Update for `angel_route@3.0.4` compatibility.
|
||||||
|
* Add `readAsBytes` and `readAsString` to `UploadedFile`.
|
||||||
|
* URI-decode path components in HTTP2.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.20
|
||||||
|
|
||||||
|
* Inject the `MiddlewarePipeline` into requests.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.19
|
||||||
|
|
||||||
|
* `parseBody` checks for null content type, and throws a `400` if none was given.
|
||||||
|
* Add `ResponseContext.contentLength`.
|
||||||
|
* Update `streamFile` to set content length, and also to work on `HEAD` requests.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.18
|
||||||
|
|
||||||
|
* Upgrade `http2` dependency.
|
||||||
|
* Upgrade `uuid` dependency.
|
||||||
|
* Fixed a bug that prevented body parsing from ever completing with `http2`.
|
||||||
|
* Add `Providers.hashCode`.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.17
|
||||||
|
|
||||||
|
* Revert the migration to `lumberjack` for now. In the future, when it's more
|
||||||
|
stable, there'll be a conversion, perhaps.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.16
|
||||||
|
|
||||||
|
* Use `package:lumberjack` for logging.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.15
|
||||||
|
|
||||||
|
* Remove dependency on `body_parser`.
|
||||||
|
* `RequestContext` now exposes a `Stream<List<int>> get body` getter.
|
||||||
|
* Calling `RequestContext.parseBody()` parses its contents.
|
||||||
|
* Added `bodyAsMap`, `bodyAsList`, `bodyAsObject`, and `uploadedFiles` to `RequestContext`.
|
||||||
|
* Removed `Angel.keepRawRequestBuffers` and anything that had to do with buffering request bodies.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.14
|
||||||
|
|
||||||
|
* Patch `HttpResponseContext._openStream` to send content-length.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.13
|
||||||
|
|
||||||
|
* Fixed a logic error in `HttpResponseContext` that prevented status codes from being sent.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.12
|
||||||
|
|
||||||
|
* Remove `ResponseContext.sendFile`.
|
||||||
|
* Add `Angel.mimeTypeResolver`.
|
||||||
|
* Fix a bug where an unknown MIME type on `streamFile` would return a 500.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.11
|
||||||
|
|
||||||
|
* Add `readMany` to `Service`.
|
||||||
|
* Allow `ResponseContext.redirect` to take a `Uri`.
|
||||||
|
* Add `Angel.mountController`.
|
||||||
|
* Add `Angel.findServiceOf`.
|
||||||
|
* Roll in HTTP/2. See `pkg:angel_framework/http2.dart`.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.10
|
||||||
|
|
||||||
|
* All calls to `Service.parseId` are now affixed with the `<Id>` argument.
|
||||||
|
* Added `uri` getter to `AngelHttp`.
|
||||||
|
* The default for `parseQuery` now wraps query parameters in `Map<String, dynamic>.from`.
|
||||||
|
This resolves a bug in `package:angel_validate`.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.9
|
||||||
|
|
||||||
|
* Add `Service.map`.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.8
|
||||||
|
|
||||||
|
* No longer export HTTP-specific code from `angel_framework.dart`.
|
||||||
|
An import of `import 'package:angel_framework/http.dart';` will be necessary in most cases now.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.7
|
||||||
|
|
||||||
|
* Force a tigher contract on services. They now must return `Data` on all
|
||||||
|
methods except for `index`, which returns a `List<Data>`.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.6
|
||||||
|
|
||||||
|
* Allow passing a custom `Container` to `handleContained` and co.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.5
|
||||||
|
|
||||||
|
* `MapService` methods now explicitly return `Map<String, dynamic>`.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.4
|
||||||
|
|
||||||
|
* Renamed `waterfall` to `chain`.
|
||||||
|
* Renamed `Routable.service` to `Routable.findService`.
|
||||||
|
* Also `Routable.findHookedService`.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.3
|
||||||
|
|
||||||
|
* Added `<Id, Data>` type parameters to `Service`.
|
||||||
|
* `HookedService` now follows suit, and takes a third parameter, pointing to the inner service.
|
||||||
|
* `Routable.use` now uses the generic parameters added to `Service`.
|
||||||
|
* Added generic usage to `HookedServiceListener`, etc.
|
||||||
|
* All service methods take `Map<String, dynamic>` as `params` now.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.2
|
||||||
|
|
||||||
|
* Added `ResponseContext.detach`.
|
||||||
|
|
||||||
|
## 2.0.0-alpha.1
|
||||||
|
|
||||||
|
* Removed `Angel.injectEncoders`.
|
||||||
|
* Added `Providers.toJson`.
|
||||||
|
* Moved `Providers.graphql` to `Providers.graphQL`.
|
||||||
|
* `Angel.optimizeForProduction` no longer calls `preInject`,
|
||||||
|
as it does not need to.
|
||||||
|
* Rename `ResponseContext.enableBuffer` to `ResponseContext.useBuffer`.
|
||||||
|
|
||||||
|
## 2.0.0-alpha
|
||||||
|
|
||||||
|
* Removed `random_string` dependency.
|
||||||
|
* Moved reflection to `package:angel_container`.
|
||||||
|
* Upgraded `package:file` to `5.0.0`.
|
||||||
|
* `ResponseContext.sendFile` now uses `package:file`.
|
||||||
|
* Abandon `ContentType` in favor of `MediaType`.
|
||||||
|
* Changed view engine to use `Map<String, dynamic>`.
|
||||||
|
* Remove dependency on `package:json_god` by default.
|
||||||
|
* Remove dependency on `package:dart2_constant`.
|
||||||
|
* Moved `lib/hooks.dart` into `package:angel_hooks`.
|
||||||
|
* Moved `TypedService` into `package:angel_typed_service`.
|
||||||
|
* Completely removed the `AngelBase` class.
|
||||||
|
* Removed all `@deprecated` symbols.
|
||||||
|
* `Service.toId` was renamed to `Service.parseId`; it also now uses its
|
||||||
|
single type argument to determine how to parse a value. \* In addition, this method was also made `static`.
|
||||||
|
* `RequestContext` and `ResponseContext` are now generic, and take a
|
||||||
|
single type argument pointing to the underlying request/response type,
|
||||||
|
respectively.
|
||||||
|
* `RequestContext.io` and `ResponseContext.io` are now permanently
|
||||||
|
gone.
|
||||||
|
* `HttpRequestContextImpl` and `HttpResponseContextImpl` were renamed to
|
||||||
|
`HttpRequestContext` and `HttpResponseContext`.
|
||||||
|
* Lazy-parsing request bodies is now the default; `Angel.lazyParseBodies` was replaced
|
||||||
|
with `Angel.eagerParseRequestBodies`.
|
||||||
|
* `Angel.storeOriginalBuffer` -> `Angel.storeRawRequestBuffers`.
|
||||||
|
* The methods `lazyBody`, `lazyFiles`, and `lazyOriginalBuffer` on `ResponseContext` were all
|
||||||
|
replaced with `parseBody`, `parseUploadedFiles`, and `parseRawRequestBuffer`, respectively.
|
||||||
|
* Removed the synchronous equivalents of the above methods (`body`, `files`, and `originalBuffer`),
|
||||||
|
as well as `query`.
|
||||||
|
* Removed `Angel.injections` and `RequestContext.injections`.
|
||||||
|
* Removed `Angel.inject` and `RequestContext.inject`.
|
||||||
|
* 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.
|
||||||
|
* `waterfall` will now only accept `RequestHandler`.
|
||||||
|
* `Routable`, and all of its subclasses, now extend `Router<RequestHandler>`, and therefore only
|
||||||
|
take routes in the form of `FutureOr myFunc(RequestContext, ResponseContext res)`.
|
||||||
|
* `@Middleware` now takes an `Iterable` of `RequestHandler`s.
|
||||||
|
* `@Expose.path` now *must* be a `String`, not just any `Pattern`.
|
||||||
|
* `@Expose.middleware` now takes `Iterable<RequestHandler>`, instead of just `List`.
|
||||||
|
* `createDynamicHandler` was renamed to `ioc`, and is now used to run IoC-aware handlers in a
|
||||||
|
type-safe manner.
|
||||||
|
* `RequestContext.params` is now a `Map<String, dynamic>`, rather than just a `Map`.
|
||||||
|
* Removed `RequestContext.grab`.
|
||||||
|
* Removed `RequestContext.properties`.
|
||||||
|
* Removed the defunct `debug` property where it still existed.
|
||||||
|
* `Routable.use` now only accepts a `Service`.
|
||||||
|
* Removed `Angel.createZoneForRequest`.
|
||||||
|
* Removed `Angel.defaultZoneCreator`.
|
||||||
|
* Added all flags to the `Angel` constructor, ex. `Angel.eagerParseBodies`.
|
||||||
|
* Fix a bug where synchronous errors in `handleRequest` would not be caught.
|
||||||
|
* `AngelHttp.useZone` now defaults to `false`.
|
||||||
|
* `ResponseContext` now starts in streaming mode by default; the response buffer is opt-in,
|
||||||
|
as in many cases it is unnecessary and slows down response time.
|
||||||
|
* `ResponseContext.streaming` was replaced by `ResponseContext.isBuffered`.
|
||||||
|
* Made `LockableBytesBuilder` public.
|
||||||
|
* Removed the now-obsolete `ResponseContext.willCloseItself`.
|
||||||
|
* Removed `ResponseContext.dispose`.
|
||||||
|
* Removed the now-obsolete `ResponseContext.end`.
|
||||||
|
* Removed the now-obsolete `ResponseContext.releaseCorrespondingRequest`.
|
||||||
|
* `preInject` now takes a `Reflector` as its second argument.
|
||||||
|
* `Angel.reflector` defaults to `const EmptyReflector()`, disabling
|
||||||
|
reflection out-of-the-box.
|
29
packages/framework/LICENSE
Normal file
29
packages/framework/LICENSE
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
BSD 3-Clause License
|
||||||
|
|
||||||
|
Copyright (c) 2021, dukefirehawk.com
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
3. Neither the name of the copyright holder nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
86
packages/framework/README.md
Normal file
86
packages/framework/README.md
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
# Angel3 Framework
|
||||||
|
|
||||||
|
[![Angel3 Framework](../../angel3_logo.png)](https://github.com/dart-backend/angel)
|
||||||
|
|
||||||
|
![Pub Version (including pre-releases)](https://img.shields.io/pub/v/angel3_framework?include_prereleases)
|
||||||
|
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
|
||||||
|
[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion)
|
||||||
|
[![License](https://img.shields.io/github/license/dart-backend/angel)](https://github.com/dart-backend/angel/tree/master/packages/framework/LICENSE)
|
||||||
|
[![melos](https://img.shields.io/badge/maintained%20with-melos-f700ff.svg?style=flat-square)](https://github.com/invertase/melos)
|
||||||
|
|
||||||
|
Angel3 framework is a high-powered HTTP server with support for dependency injection, sophisticated routing, authentication, ORM, graphql etc. It is designed to keep the core minimal but extensible through a series of plugin packages. It won't dictate which features, databases or web templating engine to use. This flexibility enable Angel3 framework to grow with your application as new features can be added to handle the new use cases.
|
||||||
|
|
||||||
|
This package is the core package of [Angel3](https://github.com/dart-backend/angel). For more information, visit us at [Angel3 Website](https://angel3-framework.web.app).
|
||||||
|
|
||||||
|
## Installation and Setup
|
||||||
|
|
||||||
|
### (Option 1) Create a new project by cloning from boilerplate templates
|
||||||
|
|
||||||
|
1. Download and install [Dart](https://dart.dev/get-dart)
|
||||||
|
|
||||||
|
2. Clone one of the following starter projects:
|
||||||
|
* [Angel3 Basic Template](https://github.com/dukefirehawk/boilerplates/tree/v7/angel3-basic)
|
||||||
|
* [Angel3 ORM Template](https://github.com/dukefirehawk/boilerplates/tree/v7/angel3-orm)
|
||||||
|
* [Angel3 ORM MySQL Template](https://github.com/dukefirehawk/boilerplates/tree/v7/angel3-orm-mysql)
|
||||||
|
* [Angel3 Graphql Template](https://github.com/dukefirehawk/boilerplates/tree/v7/angel3-graphql)
|
||||||
|
|
||||||
|
3. Run the project in development mode (*hot-reloaded* is enabled on file changes).
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dart --observe bin/dev.dart
|
||||||
|
```
|
||||||
|
|
||||||
|
4. Run the project in production mode (*hot-reloaded* is disabled).
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dart bin/prod.dart
|
||||||
|
```
|
||||||
|
|
||||||
|
5. Run as docker. Edit and build the image with the provided `Dockerfile` file.
|
||||||
|
|
||||||
|
### (Option 2) Create a new project with Angel3 CLI
|
||||||
|
|
||||||
|
1. Download and install [Dart](https://dart.dev/get-dart)
|
||||||
|
|
||||||
|
2. Install the [Angel3 CLI](https://pub.dev/packages/angel3_cli):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dart pub global activate angel3_cli
|
||||||
|
```
|
||||||
|
|
||||||
|
3. On terminal, create a new project:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
angel3 init hello
|
||||||
|
```
|
||||||
|
|
||||||
|
4. Run the project in development mode (*hot-reloaded* is enabled on file changes).
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dart --observe bin/dev.dart
|
||||||
|
```
|
||||||
|
|
||||||
|
5. Run the project in production mode (*hot-reloaded* is disabled).
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dart bin/prod.dart
|
||||||
|
```
|
||||||
|
|
||||||
|
6. Run as docker. Edit and build the image with the provided `Dockerfile` file.
|
||||||
|
|
||||||
|
## Performance Benchmark
|
||||||
|
|
||||||
|
The performance benchmark can be found at
|
||||||
|
|
||||||
|
[TechEmpower Framework Benchmarks Round 21](https://www.techempower.com/benchmarks/#section=data-r21&test=composite)
|
||||||
|
|
||||||
|
### Migrating from Angel to Angel3
|
||||||
|
|
||||||
|
Check out [Migrating to Angel3](https://angel3-docs.dukefirehawk.com/migration/angel-2.x.x-to-angel3/migration-guide-3)
|
||||||
|
|
||||||
|
## Donation & Support
|
||||||
|
|
||||||
|
If you like this project and interested in supporting its development, you can make a donation using the following services:
|
||||||
|
|
||||||
|
* [![GitHub](https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86)](https://github.com/sponsors/dukefirehawk)
|
||||||
|
* [paypal](https://paypal.me/dukefirehawk?country.x=MY&locale.x=en_US) service
|
1
packages/framework/analysis_options.yaml
Normal file
1
packages/framework/analysis_options.yaml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
include: package:lints/recommended.yaml
|
29
packages/framework/dev.key
Normal file
29
packages/framework/dev.key
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
-----BEGIN ENCRYPTED PRIVATE KEY-----
|
||||||
|
MIIE5DAcBgoqhkiG9w0BDAEBMA4ECL7L6rj6uEHGAgIIAASCBMLbucyfqAkgCbhP
|
||||||
|
xNSHYllPMAv/dsIjtnsBwepCXPGkCBCuOAw/2FaCHjN9hBqL5V7fkrKeaemhm2YE
|
||||||
|
ycPtlHJYPDf3kEkyMjdZ9rIY6kePGfQizs2uJPcXj4YPyQ4HsfVXpOicKfQrouf5
|
||||||
|
Mze9bGzeMN065q3iP4dYUMwHAyZYteXCsanQNHlqvsWli0W+H8St8fdsXefZhnv1
|
||||||
|
qVatKWdNdWQ9t5MuljgNU2Vv56sHKEYXI0yLxk2QUMk8KlJfnmt8foYUsnPUXHmc
|
||||||
|
gIjLKwwVkpdololnEHSNu0cEOUPowjgJru+uMpn7vdNl7TPEQ9jbEgdNg4JwoYzU
|
||||||
|
0nao8WzjaSp7kzvZz0VFwKnk5AjstGvvuAWckADdq23QElbn/mF7AG1m/TBpYxzF
|
||||||
|
gTt37UdndS/AcvVznWVVrRP5iTSIawdIwvqI4s7rqsoE0GCcak+RhchgAz2gWKkS
|
||||||
|
oODUo0JL6pPVbJ3l4ebbaO6c99nDVc8dViPtc1EkStJEJ2O4kI4xgLSCr4Y9ahKn
|
||||||
|
oAaoSkX7Xxq3aQm+BzqSpLjdGL8atsqR/YVOIHYIl3gThvP0NfZGx1xHyvO5mCdZ
|
||||||
|
kHxSA7tKWxauZ3eQ2clbnzeRsl4El0WMHy/5K1ovene4v7sunmoXVtghBC8hK6eh
|
||||||
|
zMO9orex2PNQ/VQC7HCvtytunOVx1lkSBoNo7hR70igg6rW9H7UyoAoBOwMpT1xa
|
||||||
|
J6V62nqruTKOqFNfur7aHJGpHGtDb5/ickHeYCyPTvmGp67u4wChzKReeg02oECe
|
||||||
|
d1E5FKAcIa8s9TVOB6Z+HvTRNQZu2PsI6TJnjQRowvY9DAHiWTlJZBBY/pko3hxX
|
||||||
|
TsIeybpvRdEHpDWv86/iqtw1hv9CUxS/8ZTWUgBo+osShHW79FeDASr9FC4/Zn76
|
||||||
|
ZDERTgV4YWlW/klVWcG2lFo7jix+OPXAB+ZQavLhlN1xdWBcIz1AUWjAM4hdPylW
|
||||||
|
HCX4PB9CQIPl2E7F+Y2p6nMcMWSJVBi5UIH7E9LfaBguXSzMmTk2Fw5p1aOQ6wfN
|
||||||
|
goVAMVwi8ppAVs741PfHdZ295xMmK/1LCxz5DeAdD/tsA/SYfT753GotioDuC7im
|
||||||
|
EyJ5JyvTr5I6RFFBuqt3NlUb3Hp16wP3B2x9DZiB6jxr0l341/NHgsyeBXkuIy9j
|
||||||
|
ON2mvpBPCJhS8kgWo3G0UyyKnx64tcgpGuSvZhGwPz843B6AbYyE6pMRfSWRMkMS
|
||||||
|
YZYa+VNKhR4ixdj07ocFZEWLVjCH7kxkE8JZXKt8jKYmkWd0lS1QVjgaKlO6lRa3
|
||||||
|
q6SPJkhW6pvqobvcqVNXwi1XuzpZeEbuh0B7OTekFTTxx5g9XeDl56M8SVQ1KEhT
|
||||||
|
Q1t7H2Nba18WCB7cf+6PN0F0K0Jz1Kq7ZWaqEI/grX1m4RQuvNF5807sB/QKMO/Z
|
||||||
|
Gz3NXvHg5xTJRd/567lxPGkor0cE7qD1EZfmJ2HrBYXQ91bhgA7LToBuMZo6ZRXH
|
||||||
|
QfsanjbP4FPLMiGdQigLjj3A35L/f4sQOOVac/sRaFnm7pzcxsMvyVU/YtvGcjYE
|
||||||
|
xaOOVnamg661Wo0wksXoDjeSz/JIyyKO3Gwp1FSm2wGLjjy/Ehmqcqy8rvHuf07w
|
||||||
|
AUukhVtTNn4=
|
||||||
|
-----END ENCRYPTED PRIVATE KEY-----
|
57
packages/framework/dev.pem
Normal file
57
packages/framework/dev.pem
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
MIIDKTCCAhGgAwIBAgIJAOWmjTS+OnTEMA0GCSqGSIb3DQEBCwUAMBcxFTATBgNV
|
||||||
|
BAMMDGludGVybWVkaWF0ZTAeFw0xNTA1MTgwOTAwNDBaFw0yMzA4MDQwOTAwNDBa
|
||||||
|
MBQxEjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
|
||||||
|
AQoCggEBALlcwQJuzd+xH8QFgfJSn5tRlvhkldSX98cE7NiA602NBbnAVyUrkRXq
|
||||||
|
Ni75lgt0kwjYfA9z674m8WSVbgpLPintPCla9CYky1TH0keIs8Rz6cGWHryWEHiu
|
||||||
|
EDuljQynu2b3sAFuHu9nfWurbJwZnFakBKpdQ9m4EyOZCHC/jHYY7HacKSXg1Cki
|
||||||
|
we2ca0BWDrcqy8kLy0dZ5oC6IZG8O8drAK8f3f44CRYw59D3sOKBrKXaabpvyEcb
|
||||||
|
N7Wk2HDBVwHpUJo1reVwtbM8dhqQayYSD8oXnGpP3RQNu/e2rzlXRyq/BfcDY1JI
|
||||||
|
7TbC4t/7/N4EcPSpGsTcSOC9A7FpzvECAwEAAaN7MHkwCQYDVR0TBAIwADAsBglg
|
||||||
|
hkgBhvhCAQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYDVR0O
|
||||||
|
BBYEFCnwiEMMFZh7NhCr+qA8K0w4Q+AOMB8GA1UdIwQYMBaAFB0h1Evsaw2vfrmS
|
||||||
|
YuoCTmC4EE6ZMA0GCSqGSIb3DQEBCwUAA4IBAQAcFmHMaXRxyoNaeOowQ6iQWoZd
|
||||||
|
AUbvG7SHr7I6Pi2aqdqofsKWts7Ytm5WsS0M2nN+sW504houu0iCPeJJX8RQw2q4
|
||||||
|
CCcNOs9IXk+2uMzlpocHpv+yYoUiD5DxgWh7eghQMLyMpf8FX3Gy4VazeuXznHOM
|
||||||
|
4gE4L417xkDzYOzqVTp0FTyAPUv6G2euhNCD6TMru9REcRhYul+K9kocjA5tt2KG
|
||||||
|
MH6y28LXbLyq4YJUxSUU9gY/xlnbbZS48KDqEcdYC9zjW9nQ0qS+XQuQuFIcwjJ5
|
||||||
|
V4kAUYxDu6FoTpyQjgsrmBbZlKNxH7Nj4NDlcdJhp/zeSKHqWa5hSWjjKIxp
|
||||||
|
-----END CERTIFICATE-----
|
||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
MIIDAjCCAeqgAwIBAgIJAOWmjTS+OnTDMA0GCSqGSIb3DQEBCwUAMBgxFjAUBgNV
|
||||||
|
BAMMDXJvb3RhdXRob3JpdHkwHhcNMTUwNTE4MDkwMDQwWhcNMjMwODA0MDkwMDQw
|
||||||
|
WjAXMRUwEwYDVQQDDAxpbnRlcm1lZGlhdGUwggEiMA0GCSqGSIb3DQEBAQUAA4IB
|
||||||
|
DwAwggEKAoIBAQDSrAO1CoPvUllgLOzDm5nG0skDF7vh1DUgAIDVGz0ecD0JFbQx
|
||||||
|
EF79pju/6MbtpTW2FYvRp11t/G7rGtX923ybOHY/1MNFQrdIvPlO1VV7IGKjoMwP
|
||||||
|
DNeb0fIGjHoE9QxaDxR8NX8xQbItpsw+TUtRfc9SLkR+jaYJfVRoM21BOncZbSHE
|
||||||
|
YKiZlEbpecB/+EtwVpgvl+8mPD5U07Fi4fp/lza3WXInXQPyiTVllIEJCt4PKmlu
|
||||||
|
MocNaJOW38bysL7i0PzDpVZtOxLHOTaW68yF3FckIHNCaA7k1ABEEEegjFMmIao7
|
||||||
|
B9w7A0jvr4jZVvNmui5Djjn+oJxwEVVgyf8LAgMBAAGjUDBOMB0GA1UdDgQWBBQd
|
||||||
|
IdRL7GsNr365kmLqAk5guBBOmTAfBgNVHSMEGDAWgBRk81s9d0ZbiZhh44KckwPb
|
||||||
|
oTc0XzAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQBZQTK0plfdB5PC
|
||||||
|
cC5icut4EmrByJa1RbU7ayuEE70e7hla6KVmVjVdCBGltI4jBYwfhKbRItHiAJ/8
|
||||||
|
x+XZKBG8DLPFuDb7lAa1ObhAYF7YThUFPQYaBhfzKcWrdmWDBFpvNv6E0Mm364dZ
|
||||||
|
e7Yxmbe5S4agkYPoxEzgEYmcUk9jbjdR6eTbs8laG169ljrECXfEU9RiAcqz5iSX
|
||||||
|
NLSewqB47hn3B9qgKcQn+PsgO2j7M+rfklhNgeGJeWmy7j6clSOuCsIjWHU0RLQ4
|
||||||
|
0W3SB/rpEAJ7fgQbYUPTIUNALSOWi/o1tDX2mXPRjBoxqAv7I+vYk1lZPmSzkyRh
|
||||||
|
FKvRDxsW
|
||||||
|
-----END CERTIFICATE-----
|
||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
MIIDAzCCAeugAwIBAgIJAJ0MomS4Ck+8MA0GCSqGSIb3DQEBCwUAMBgxFjAUBgNV
|
||||||
|
BAMMDXJvb3RhdXRob3JpdHkwHhcNMTUwNTE4MDkwMDQwWhcNMjMwODA0MDkwMDQw
|
||||||
|
WjAYMRYwFAYDVQQDDA1yb290YXV0aG9yaXR5MIIBIjANBgkqhkiG9w0BAQEFAAOC
|
||||||
|
AQ8AMIIBCgKCAQEAts1ijtBV92S2cOvpUMOSTp9c6A34nIGr0T5Nhz6XiqRVT+gv
|
||||||
|
dQgmkdKJQjbvR60y6jzltYFsI2MpGVXY8h/oAL81D/k7PDB2aREgyBfTPAhBHyGw
|
||||||
|
siR+2xYt5b/Zs99q5RdRqQNzNpLPJriIKvUsRyQWy1UiG2s7pRXQeA8qB0XtJdCj
|
||||||
|
kFIi+G2bDsaffspGeDOCqt7t+yqvRXfSES0c/l7DIHaiMbbp4//ZNML3RNgAjPz2
|
||||||
|
hCezZ+wOYajOIyoSPK8IgICrhYFYxvgWxwbLDBEfC5B3jOQsySe10GoRAKZz1gBV
|
||||||
|
DmgReu81tYJmdgkc9zknnQtIFdA0ex+GvZlfWQIDAQABo1AwTjAdBgNVHQ4EFgQU
|
||||||
|
ZPNbPXdGW4mYYeOCnJMD26E3NF8wHwYDVR0jBBgwFoAUZPNbPXdGW4mYYeOCnJMD
|
||||||
|
26E3NF8wDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEATzkZ97K777uZ
|
||||||
|
lQcduNX3ey4IbCiEzFA2zO5Blj+ilfIwNbZXNOgm/lqNvVGDYs6J1apJJe30vL3X
|
||||||
|
J+t2zsZWzzQzb9uIU37zYemt6m0fHrSrx/iy5lGNqt3HMfqEcOqSCOIK3PCTMz2/
|
||||||
|
uyGe1iw33PVeWsm1JUybQ9IrU/huJjbgOHU4wab+8SJCM49ipArp68Fr6j4lcEaE
|
||||||
|
4rfRg1ZsvxiOyUB3qPn6wyL/JB8kOJ+QCBe498376eaem8AEFk0kQRh6hDaWtq/k
|
||||||
|
t6IIXQLjx+EBDVP/veK0UnVhKRP8YTOoV8ZiG1NcdlJmX/Uk7iAfevP7CkBfSN8W
|
||||||
|
r6AL284qtw==
|
||||||
|
-----END CERTIFICATE-----
|
59
packages/framework/example/controller.dart
Normal file
59
packages/framework/example/controller.dart
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
// Logging set up/boilerplate
|
||||||
|
Logger.root.onRecord.listen(print);
|
||||||
|
|
||||||
|
// Create our server.
|
||||||
|
var app = Angel(logger: Logger('angel'), reflector: MirrorsReflector());
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
|
||||||
|
await app.mountController<ArtistsController>();
|
||||||
|
|
||||||
|
// Simple fallback to throw a 404 on unknown paths.
|
||||||
|
app.fallback((req, res) {
|
||||||
|
throw HttpException.notFound(
|
||||||
|
message: 'Unknown path: "${req.uri!.path}"',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.errorHandler = (e, req, res) => e.toJson();
|
||||||
|
|
||||||
|
await http.startServer('127.0.0.1', 3000);
|
||||||
|
print('Listening at ${http.uri}');
|
||||||
|
app.dumpTree();
|
||||||
|
}
|
||||||
|
|
||||||
|
class ArtistsController extends Controller {
|
||||||
|
List index() {
|
||||||
|
return ['Elvis', 'Stevie', 'Van Gogh'];
|
||||||
|
}
|
||||||
|
|
||||||
|
String getById(int id, RequestContext req) {
|
||||||
|
return 'You fetched ID: $id from IP: ${req.ip}';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Expose.post
|
||||||
|
Future<Artist> form(RequestContext req) async {
|
||||||
|
// Deserialize the body into an artist.
|
||||||
|
var artist = await req.deserializeBody((m) {
|
||||||
|
return Artist(name: m!['name'] as String? ?? '(unknown name)');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Return it (it will be serialized to JSON).
|
||||||
|
return artist;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Artist {
|
||||||
|
final String? name;
|
||||||
|
|
||||||
|
Artist({this.name});
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {'name': name};
|
||||||
|
}
|
||||||
|
}
|
25
packages/framework/example/handle_error.dart
Normal file
25
packages/framework/example/handle_error.dart
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
var app = Angel(reflector: MirrorsReflector())
|
||||||
|
..logger = (Logger('angel')
|
||||||
|
..onRecord.listen((rec) {
|
||||||
|
print(rec);
|
||||||
|
if (rec.error != null) print(rec.error);
|
||||||
|
if (rec.stackTrace != null) print(rec.stackTrace);
|
||||||
|
}))
|
||||||
|
..encoders.addAll({'gzip': gzip.encoder});
|
||||||
|
|
||||||
|
app.fallback(
|
||||||
|
(req, res) => Future.error('Throwing just because I feel like!'));
|
||||||
|
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
HttpServer? server = await http.startServer('127.0.0.1', 3000);
|
||||||
|
var url = 'http://${server.address.address}:${server.port}';
|
||||||
|
print('Listening at $url');
|
||||||
|
}
|
46
packages/framework/example/hostname.dart
Normal file
46
packages/framework/example/hostname.dart
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
|
Future<void> apiConfigurer(Angel app) async {
|
||||||
|
app.get('/', (req, res) => 'Hello, API!');
|
||||||
|
app.fallback((req, res) {
|
||||||
|
return 'fallback on ${req.uri} (within the API)';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> frontendConfigurer(Angel app) async {
|
||||||
|
app.fallback((req, res) => '(usually an index page would be shown here.)');
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
// Logging set up/boilerplate
|
||||||
|
hierarchicalLoggingEnabled = true;
|
||||||
|
//Logger.root.onRecord.listen(prettyLog);
|
||||||
|
|
||||||
|
var app = Angel(logger: Logger('angel'));
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
var multiHost = HostnameRouter.configure({
|
||||||
|
'api.localhost:3000': apiConfigurer,
|
||||||
|
'localhost:3000': frontendConfigurer,
|
||||||
|
});
|
||||||
|
|
||||||
|
app
|
||||||
|
..fallback(multiHost.handleRequest)
|
||||||
|
..fallback((req, res) {
|
||||||
|
res.write('Uncaught hostname: ${req.hostname}');
|
||||||
|
});
|
||||||
|
|
||||||
|
app.errorHandler = (e, req, res) {
|
||||||
|
print(e.message);
|
||||||
|
print(e.stackTrace);
|
||||||
|
return e.toJson();
|
||||||
|
};
|
||||||
|
|
||||||
|
await http.startServer('127.0.0.1', 3000);
|
||||||
|
print('Listening at ${http.uri}');
|
||||||
|
print('See what happens when you visit http://localhost:3000 instead '
|
||||||
|
'of http://127.0.0.1:3000. Then, try '
|
||||||
|
'http://api.localhost:3000.');
|
||||||
|
}
|
46
packages/framework/example/http2/body_parsing.dart
Normal file
46
packages/framework/example/http2/body_parsing.dart
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_framework/http2.dart';
|
||||||
|
import 'package:file/local.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
var app = Angel();
|
||||||
|
app.logger = Logger('angel')
|
||||||
|
..onRecord.listen((rec) {
|
||||||
|
print(rec);
|
||||||
|
if (rec.error != null) print(rec.error);
|
||||||
|
if (rec.stackTrace != null) print(rec.stackTrace);
|
||||||
|
});
|
||||||
|
|
||||||
|
var publicDir = Directory('example/public');
|
||||||
|
var indexHtml =
|
||||||
|
const LocalFileSystem().file(publicDir.uri.resolve('body_parsing.html'));
|
||||||
|
|
||||||
|
app.get('/', (req, res) => res.streamFile(indexHtml));
|
||||||
|
|
||||||
|
app.post('/', (req, res) => req.parseBody().then((_) => req.bodyAsMap));
|
||||||
|
|
||||||
|
var ctx = SecurityContext()
|
||||||
|
..useCertificateChain('dev.pem')
|
||||||
|
..usePrivateKey('dev.key', password: 'dartdart');
|
||||||
|
|
||||||
|
try {
|
||||||
|
ctx.setAlpnProtocols(['h2'], true);
|
||||||
|
} catch (e, st) {
|
||||||
|
app.logger.severe(
|
||||||
|
'Cannot set ALPN protocol on server to `h2`. The server will only serve HTTP/1.x.',
|
||||||
|
e,
|
||||||
|
st);
|
||||||
|
}
|
||||||
|
|
||||||
|
var http1 = AngelHttp(app);
|
||||||
|
var http2 = AngelHttp2(app, ctx);
|
||||||
|
|
||||||
|
// HTTP/1.x requests will fallback to `AngelHttp`
|
||||||
|
http2.onHttp1.listen(http1.handleRequest);
|
||||||
|
|
||||||
|
var server = await http2.startServer('127.0.0.1', 3000);
|
||||||
|
print('Listening at https://${server.address.address}:${server.port}');
|
||||||
|
}
|
7
packages/framework/example/http2/common.dart
Normal file
7
packages/framework/example/http2/common.dart
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
|
void dumpError(LogRecord rec) {
|
||||||
|
print(rec);
|
||||||
|
if (rec.error != null) print(rec.error);
|
||||||
|
if (rec.stackTrace != null) print(rec.stackTrace);
|
||||||
|
}
|
29
packages/framework/example/http2/dev.key
Normal file
29
packages/framework/example/http2/dev.key
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
-----BEGIN ENCRYPTED PRIVATE KEY-----
|
||||||
|
MIIE5DAcBgoqhkiG9w0BDAEBMA4ECL7L6rj6uEHGAgIIAASCBMLbucyfqAkgCbhP
|
||||||
|
xNSHYllPMAv/dsIjtnsBwepCXPGkCBCuOAw/2FaCHjN9hBqL5V7fkrKeaemhm2YE
|
||||||
|
ycPtlHJYPDf3kEkyMjdZ9rIY6kePGfQizs2uJPcXj4YPyQ4HsfVXpOicKfQrouf5
|
||||||
|
Mze9bGzeMN065q3iP4dYUMwHAyZYteXCsanQNHlqvsWli0W+H8St8fdsXefZhnv1
|
||||||
|
qVatKWdNdWQ9t5MuljgNU2Vv56sHKEYXI0yLxk2QUMk8KlJfnmt8foYUsnPUXHmc
|
||||||
|
gIjLKwwVkpdololnEHSNu0cEOUPowjgJru+uMpn7vdNl7TPEQ9jbEgdNg4JwoYzU
|
||||||
|
0nao8WzjaSp7kzvZz0VFwKnk5AjstGvvuAWckADdq23QElbn/mF7AG1m/TBpYxzF
|
||||||
|
gTt37UdndS/AcvVznWVVrRP5iTSIawdIwvqI4s7rqsoE0GCcak+RhchgAz2gWKkS
|
||||||
|
oODUo0JL6pPVbJ3l4ebbaO6c99nDVc8dViPtc1EkStJEJ2O4kI4xgLSCr4Y9ahKn
|
||||||
|
oAaoSkX7Xxq3aQm+BzqSpLjdGL8atsqR/YVOIHYIl3gThvP0NfZGx1xHyvO5mCdZ
|
||||||
|
kHxSA7tKWxauZ3eQ2clbnzeRsl4El0WMHy/5K1ovene4v7sunmoXVtghBC8hK6eh
|
||||||
|
zMO9orex2PNQ/VQC7HCvtytunOVx1lkSBoNo7hR70igg6rW9H7UyoAoBOwMpT1xa
|
||||||
|
J6V62nqruTKOqFNfur7aHJGpHGtDb5/ickHeYCyPTvmGp67u4wChzKReeg02oECe
|
||||||
|
d1E5FKAcIa8s9TVOB6Z+HvTRNQZu2PsI6TJnjQRowvY9DAHiWTlJZBBY/pko3hxX
|
||||||
|
TsIeybpvRdEHpDWv86/iqtw1hv9CUxS/8ZTWUgBo+osShHW79FeDASr9FC4/Zn76
|
||||||
|
ZDERTgV4YWlW/klVWcG2lFo7jix+OPXAB+ZQavLhlN1xdWBcIz1AUWjAM4hdPylW
|
||||||
|
HCX4PB9CQIPl2E7F+Y2p6nMcMWSJVBi5UIH7E9LfaBguXSzMmTk2Fw5p1aOQ6wfN
|
||||||
|
goVAMVwi8ppAVs741PfHdZ295xMmK/1LCxz5DeAdD/tsA/SYfT753GotioDuC7im
|
||||||
|
EyJ5JyvTr5I6RFFBuqt3NlUb3Hp16wP3B2x9DZiB6jxr0l341/NHgsyeBXkuIy9j
|
||||||
|
ON2mvpBPCJhS8kgWo3G0UyyKnx64tcgpGuSvZhGwPz843B6AbYyE6pMRfSWRMkMS
|
||||||
|
YZYa+VNKhR4ixdj07ocFZEWLVjCH7kxkE8JZXKt8jKYmkWd0lS1QVjgaKlO6lRa3
|
||||||
|
q6SPJkhW6pvqobvcqVNXwi1XuzpZeEbuh0B7OTekFTTxx5g9XeDl56M8SVQ1KEhT
|
||||||
|
Q1t7H2Nba18WCB7cf+6PN0F0K0Jz1Kq7ZWaqEI/grX1m4RQuvNF5807sB/QKMO/Z
|
||||||
|
Gz3NXvHg5xTJRd/567lxPGkor0cE7qD1EZfmJ2HrBYXQ91bhgA7LToBuMZo6ZRXH
|
||||||
|
QfsanjbP4FPLMiGdQigLjj3A35L/f4sQOOVac/sRaFnm7pzcxsMvyVU/YtvGcjYE
|
||||||
|
xaOOVnamg661Wo0wksXoDjeSz/JIyyKO3Gwp1FSm2wGLjjy/Ehmqcqy8rvHuf07w
|
||||||
|
AUukhVtTNn4=
|
||||||
|
-----END ENCRYPTED PRIVATE KEY-----
|
57
packages/framework/example/http2/dev.pem
Normal file
57
packages/framework/example/http2/dev.pem
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
MIIDKTCCAhGgAwIBAgIJAOWmjTS+OnTEMA0GCSqGSIb3DQEBCwUAMBcxFTATBgNV
|
||||||
|
BAMMDGludGVybWVkaWF0ZTAeFw0xNTA1MTgwOTAwNDBaFw0yMzA4MDQwOTAwNDBa
|
||||||
|
MBQxEjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
|
||||||
|
AQoCggEBALlcwQJuzd+xH8QFgfJSn5tRlvhkldSX98cE7NiA602NBbnAVyUrkRXq
|
||||||
|
Ni75lgt0kwjYfA9z674m8WSVbgpLPintPCla9CYky1TH0keIs8Rz6cGWHryWEHiu
|
||||||
|
EDuljQynu2b3sAFuHu9nfWurbJwZnFakBKpdQ9m4EyOZCHC/jHYY7HacKSXg1Cki
|
||||||
|
we2ca0BWDrcqy8kLy0dZ5oC6IZG8O8drAK8f3f44CRYw59D3sOKBrKXaabpvyEcb
|
||||||
|
N7Wk2HDBVwHpUJo1reVwtbM8dhqQayYSD8oXnGpP3RQNu/e2rzlXRyq/BfcDY1JI
|
||||||
|
7TbC4t/7/N4EcPSpGsTcSOC9A7FpzvECAwEAAaN7MHkwCQYDVR0TBAIwADAsBglg
|
||||||
|
hkgBhvhCAQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYDVR0O
|
||||||
|
BBYEFCnwiEMMFZh7NhCr+qA8K0w4Q+AOMB8GA1UdIwQYMBaAFB0h1Evsaw2vfrmS
|
||||||
|
YuoCTmC4EE6ZMA0GCSqGSIb3DQEBCwUAA4IBAQAcFmHMaXRxyoNaeOowQ6iQWoZd
|
||||||
|
AUbvG7SHr7I6Pi2aqdqofsKWts7Ytm5WsS0M2nN+sW504houu0iCPeJJX8RQw2q4
|
||||||
|
CCcNOs9IXk+2uMzlpocHpv+yYoUiD5DxgWh7eghQMLyMpf8FX3Gy4VazeuXznHOM
|
||||||
|
4gE4L417xkDzYOzqVTp0FTyAPUv6G2euhNCD6TMru9REcRhYul+K9kocjA5tt2KG
|
||||||
|
MH6y28LXbLyq4YJUxSUU9gY/xlnbbZS48KDqEcdYC9zjW9nQ0qS+XQuQuFIcwjJ5
|
||||||
|
V4kAUYxDu6FoTpyQjgsrmBbZlKNxH7Nj4NDlcdJhp/zeSKHqWa5hSWjjKIxp
|
||||||
|
-----END CERTIFICATE-----
|
||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
MIIDAjCCAeqgAwIBAgIJAOWmjTS+OnTDMA0GCSqGSIb3DQEBCwUAMBgxFjAUBgNV
|
||||||
|
BAMMDXJvb3RhdXRob3JpdHkwHhcNMTUwNTE4MDkwMDQwWhcNMjMwODA0MDkwMDQw
|
||||||
|
WjAXMRUwEwYDVQQDDAxpbnRlcm1lZGlhdGUwggEiMA0GCSqGSIb3DQEBAQUAA4IB
|
||||||
|
DwAwggEKAoIBAQDSrAO1CoPvUllgLOzDm5nG0skDF7vh1DUgAIDVGz0ecD0JFbQx
|
||||||
|
EF79pju/6MbtpTW2FYvRp11t/G7rGtX923ybOHY/1MNFQrdIvPlO1VV7IGKjoMwP
|
||||||
|
DNeb0fIGjHoE9QxaDxR8NX8xQbItpsw+TUtRfc9SLkR+jaYJfVRoM21BOncZbSHE
|
||||||
|
YKiZlEbpecB/+EtwVpgvl+8mPD5U07Fi4fp/lza3WXInXQPyiTVllIEJCt4PKmlu
|
||||||
|
MocNaJOW38bysL7i0PzDpVZtOxLHOTaW68yF3FckIHNCaA7k1ABEEEegjFMmIao7
|
||||||
|
B9w7A0jvr4jZVvNmui5Djjn+oJxwEVVgyf8LAgMBAAGjUDBOMB0GA1UdDgQWBBQd
|
||||||
|
IdRL7GsNr365kmLqAk5guBBOmTAfBgNVHSMEGDAWgBRk81s9d0ZbiZhh44KckwPb
|
||||||
|
oTc0XzAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQBZQTK0plfdB5PC
|
||||||
|
cC5icut4EmrByJa1RbU7ayuEE70e7hla6KVmVjVdCBGltI4jBYwfhKbRItHiAJ/8
|
||||||
|
x+XZKBG8DLPFuDb7lAa1ObhAYF7YThUFPQYaBhfzKcWrdmWDBFpvNv6E0Mm364dZ
|
||||||
|
e7Yxmbe5S4agkYPoxEzgEYmcUk9jbjdR6eTbs8laG169ljrECXfEU9RiAcqz5iSX
|
||||||
|
NLSewqB47hn3B9qgKcQn+PsgO2j7M+rfklhNgeGJeWmy7j6clSOuCsIjWHU0RLQ4
|
||||||
|
0W3SB/rpEAJ7fgQbYUPTIUNALSOWi/o1tDX2mXPRjBoxqAv7I+vYk1lZPmSzkyRh
|
||||||
|
FKvRDxsW
|
||||||
|
-----END CERTIFICATE-----
|
||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
MIIDAzCCAeugAwIBAgIJAJ0MomS4Ck+8MA0GCSqGSIb3DQEBCwUAMBgxFjAUBgNV
|
||||||
|
BAMMDXJvb3RhdXRob3JpdHkwHhcNMTUwNTE4MDkwMDQwWhcNMjMwODA0MDkwMDQw
|
||||||
|
WjAYMRYwFAYDVQQDDA1yb290YXV0aG9yaXR5MIIBIjANBgkqhkiG9w0BAQEFAAOC
|
||||||
|
AQ8AMIIBCgKCAQEAts1ijtBV92S2cOvpUMOSTp9c6A34nIGr0T5Nhz6XiqRVT+gv
|
||||||
|
dQgmkdKJQjbvR60y6jzltYFsI2MpGVXY8h/oAL81D/k7PDB2aREgyBfTPAhBHyGw
|
||||||
|
siR+2xYt5b/Zs99q5RdRqQNzNpLPJriIKvUsRyQWy1UiG2s7pRXQeA8qB0XtJdCj
|
||||||
|
kFIi+G2bDsaffspGeDOCqt7t+yqvRXfSES0c/l7DIHaiMbbp4//ZNML3RNgAjPz2
|
||||||
|
hCezZ+wOYajOIyoSPK8IgICrhYFYxvgWxwbLDBEfC5B3jOQsySe10GoRAKZz1gBV
|
||||||
|
DmgReu81tYJmdgkc9zknnQtIFdA0ex+GvZlfWQIDAQABo1AwTjAdBgNVHQ4EFgQU
|
||||||
|
ZPNbPXdGW4mYYeOCnJMD26E3NF8wHwYDVR0jBBgwFoAUZPNbPXdGW4mYYeOCnJMD
|
||||||
|
26E3NF8wDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEATzkZ97K777uZ
|
||||||
|
lQcduNX3ey4IbCiEzFA2zO5Blj+ilfIwNbZXNOgm/lqNvVGDYs6J1apJJe30vL3X
|
||||||
|
J+t2zsZWzzQzb9uIU37zYemt6m0fHrSrx/iy5lGNqt3HMfqEcOqSCOIK3PCTMz2/
|
||||||
|
uyGe1iw33PVeWsm1JUybQ9IrU/huJjbgOHU4wab+8SJCM49ipArp68Fr6j4lcEaE
|
||||||
|
4rfRg1ZsvxiOyUB3qPn6wyL/JB8kOJ+QCBe498376eaem8AEFk0kQRh6hDaWtq/k
|
||||||
|
t6IIXQLjx+EBDVP/veK0UnVhKRP8YTOoV8ZiG1NcdlJmX/Uk7iAfevP7CkBfSN8W
|
||||||
|
r6AL284qtw==
|
||||||
|
-----END CERTIFICATE-----
|
43
packages/framework/example/http2/main.dart
Normal file
43
packages/framework/example/http2/main.dart
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_framework/http2.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'common.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
var app = Angel()
|
||||||
|
..encoders.addAll({
|
||||||
|
'gzip': gzip.encoder,
|
||||||
|
'deflate': zlib.encoder,
|
||||||
|
});
|
||||||
|
app.logger = Logger('angel')..onRecord.listen(dumpError);
|
||||||
|
|
||||||
|
app.get('/', (req, res) => 'Hello HTTP/2!!!');
|
||||||
|
|
||||||
|
app.fallback((req, res) =>
|
||||||
|
throw HttpException.notFound(message: 'No file exists at ${req.uri}'));
|
||||||
|
|
||||||
|
var ctx = SecurityContext()
|
||||||
|
..useCertificateChain('dev.pem')
|
||||||
|
..usePrivateKey('dev.key', password: 'dartdart');
|
||||||
|
|
||||||
|
try {
|
||||||
|
ctx.setAlpnProtocols(['h2'], true);
|
||||||
|
} catch (e, st) {
|
||||||
|
app.logger.severe(
|
||||||
|
'Cannot set ALPN protocol on server to `h2`. The server will only serve HTTP/1.x.',
|
||||||
|
e,
|
||||||
|
st,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
var http1 = AngelHttp(app);
|
||||||
|
var http2 = AngelHttp2(app, ctx);
|
||||||
|
|
||||||
|
// HTTP/1.x requests will fallback to `AngelHttp`
|
||||||
|
http2.onHttp1.listen(http1.handleRequest);
|
||||||
|
|
||||||
|
await http2.startServer('127.0.0.1', 3000);
|
||||||
|
print('Listening at ${http2.uri}');
|
||||||
|
}
|
27
packages/framework/example/http2/public/app.js
Normal file
27
packages/framework/example/http2/public/app.js
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
window.onload = function() {
|
||||||
|
var $app = document.getElementById('app');
|
||||||
|
var $loading = document.getElementById('loading');
|
||||||
|
$app.removeChild($loading);
|
||||||
|
var $button = document.createElement('button');
|
||||||
|
var $h1 = document.createElement('h1');
|
||||||
|
$app.appendChild($h1);
|
||||||
|
$app.appendChild($button);
|
||||||
|
|
||||||
|
$h1.textContent = '~Angel HTTP/2 server push~';
|
||||||
|
|
||||||
|
$button.textContent = 'Change color';
|
||||||
|
$button.onclick = function() {
|
||||||
|
var color = Math.floor(Math.random() * 0xffffff);
|
||||||
|
$h1.style.color = '#' + color.toString(16);
|
||||||
|
};
|
||||||
|
|
||||||
|
$button.onclick();
|
||||||
|
|
||||||
|
window.setInterval($button.onclick, 2000);
|
||||||
|
|
||||||
|
var rotation = 0;
|
||||||
|
window.setInterval(function() {
|
||||||
|
rotation += .6;
|
||||||
|
$button.style.transform = 'rotate(' + rotation + 'deg)';
|
||||||
|
}, 10);
|
||||||
|
};
|
21
packages/framework/example/http2/public/body_parsing.html
Normal file
21
packages/framework/example/http2/public/body_parsing.html
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Angel HTTP/2</title>
|
||||||
|
<style>
|
||||||
|
input:not([type="submit"]) {
|
||||||
|
margin-bottom: 2em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="/" method="post">
|
||||||
|
<input name="name" placeholder="Your Name" type="text">
|
||||||
|
<input name="password" placeholder="Secret Field" type="password">
|
||||||
|
<input name="age" placeholder="Your Age" type="number">
|
||||||
|
<input name="birthday" placeholder="Your Birthday" type="datetime-local">
|
||||||
|
<input type="submit" value="Submit">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
12
packages/framework/example/http2/public/index.html
Normal file
12
packages/framework/example/http2/public/index.html
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Angel HTTP/2</title>
|
||||||
|
<link rel="stylesheet" href="style.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="app"><span id="loading">Loading...</span></div>
|
||||||
|
<script src="app.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
20
packages/framework/example/http2/public/style.css
Normal file
20
packages/framework/example/http2/public/style.css
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
button {
|
||||||
|
margin-top: 2em;
|
||||||
|
}
|
||||||
|
|
||||||
|
html, body {
|
||||||
|
background-color: #000;
|
||||||
|
}
|
||||||
|
|
||||||
|
#app {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
#app h1 {
|
||||||
|
font-style: italic;
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
#loading {
|
||||||
|
color: red;
|
||||||
|
}
|
62
packages/framework/example/http2/server_push.dart
Normal file
62
packages/framework/example/http2/server_push.dart
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_framework/http2.dart';
|
||||||
|
import 'package:file/local.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
var app = Angel();
|
||||||
|
app.logger = Logger('angel')
|
||||||
|
..onRecord.listen((rec) {
|
||||||
|
print(rec);
|
||||||
|
if (rec.error != null) print(rec.error);
|
||||||
|
if (rec.stackTrace != null) print(rec.stackTrace);
|
||||||
|
});
|
||||||
|
|
||||||
|
var publicDir = Directory('example/http2/public');
|
||||||
|
var indexHtml =
|
||||||
|
const LocalFileSystem().file(publicDir.uri.resolve('index.html'));
|
||||||
|
var styleCss =
|
||||||
|
const LocalFileSystem().file(publicDir.uri.resolve('style.css'));
|
||||||
|
var appJs = const LocalFileSystem().file(publicDir.uri.resolve('app.js'));
|
||||||
|
|
||||||
|
// Send files when requested
|
||||||
|
app
|
||||||
|
..get('/style.css', (req, res) => res.streamFile(styleCss))
|
||||||
|
..get('/app.js', (req, res) => res.streamFile(appJs));
|
||||||
|
|
||||||
|
app.get('/', (req, res) async {
|
||||||
|
// Regardless of whether we pushed other resources, let's still send /index.html.
|
||||||
|
await res.streamFile(indexHtml);
|
||||||
|
|
||||||
|
// If the client is HTTP/2 and supports server push, let's
|
||||||
|
// send down /style.css and /app.js as well, to improve initial load time.
|
||||||
|
if (res is Http2ResponseContext && res.canPush) {
|
||||||
|
await res.push('/style.css').streamFile(styleCss);
|
||||||
|
await res.push('/app.js').streamFile(appJs);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var ctx = SecurityContext()
|
||||||
|
..useCertificateChain('dev.pem')
|
||||||
|
..usePrivateKey('dev.key', password: 'dartdart');
|
||||||
|
|
||||||
|
try {
|
||||||
|
ctx.setAlpnProtocols(['h2'], true);
|
||||||
|
} catch (e, st) {
|
||||||
|
app.logger.severe(
|
||||||
|
'Cannot set ALPN protocol on server to `h2`. The server will only serve HTTP/1.x.',
|
||||||
|
e,
|
||||||
|
st);
|
||||||
|
}
|
||||||
|
|
||||||
|
var http1 = AngelHttp(app);
|
||||||
|
var http2 = AngelHttp2(app, ctx);
|
||||||
|
|
||||||
|
// HTTP/1.x requests will fallback to `AngelHttp`
|
||||||
|
http2.onHttp1.listen(http1.handleRequest);
|
||||||
|
|
||||||
|
var server = await http2.startServer('127.0.0.1', 3000);
|
||||||
|
print('Listening at https://${server.address.address}:${server.port}');
|
||||||
|
}
|
53
packages/framework/example/json.dart
Normal file
53
packages/framework/example/json.dart
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'dart:isolate';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
var x = 0;
|
||||||
|
var c = Completer();
|
||||||
|
var exit = ReceivePort();
|
||||||
|
var isolates = <Isolate>[];
|
||||||
|
|
||||||
|
exit.listen((_) {
|
||||||
|
if (++x >= 50) {
|
||||||
|
c.complete();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
for (var i = 1; i < Platform.numberOfProcessors; i++) {
|
||||||
|
var isolate = await Isolate.spawn(serverMain, null);
|
||||||
|
isolates.add(isolate);
|
||||||
|
print('Spawned isolate #${i + 1}...');
|
||||||
|
|
||||||
|
isolate.addOnExitListener(exit.sendPort);
|
||||||
|
}
|
||||||
|
|
||||||
|
serverMain(null);
|
||||||
|
|
||||||
|
print('Angel listening at http://localhost:3000');
|
||||||
|
await c.future;
|
||||||
|
}
|
||||||
|
|
||||||
|
void serverMain(_) async {
|
||||||
|
var app = Angel();
|
||||||
|
var http =
|
||||||
|
AngelHttp.custom(app, startShared, useZone: false); // Run a cluster
|
||||||
|
|
||||||
|
app.get('/', (req, res) {
|
||||||
|
return res.serialize({
|
||||||
|
'foo': 'bar',
|
||||||
|
'one': [2, 'three'],
|
||||||
|
'bar': {'baz': 'quux'}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.errorHandler = (e, req, res) {
|
||||||
|
print(e.message);
|
||||||
|
print(e.stackTrace);
|
||||||
|
};
|
||||||
|
|
||||||
|
var server = await http.startServer('127.0.0.1', 3000);
|
||||||
|
print('Listening at http://${server.address.address}:${server.port}');
|
||||||
|
}
|
58
packages/framework/example/main.dart
Normal file
58
packages/framework/example/main.dart
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
// Logging set up/boilerplate
|
||||||
|
//Logger.root.onRecord.listen(prettyLog);
|
||||||
|
|
||||||
|
// Create our server.
|
||||||
|
var app = Angel(
|
||||||
|
logger: Logger('angel'),
|
||||||
|
reflector: MirrorsReflector(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Index route. Returns JSON.
|
||||||
|
app.get('/', (req, res) => 'Welcome to Angel!');
|
||||||
|
|
||||||
|
// Accepts a URL like /greet/foo or /greet/bob.
|
||||||
|
app.get(
|
||||||
|
'/greet/:name',
|
||||||
|
(req, res) {
|
||||||
|
var name = req.params['name'];
|
||||||
|
res
|
||||||
|
..write('Hello, $name!')
|
||||||
|
..close();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Pattern matching - only call this handler if the query value of `name` equals 'emoji'.
|
||||||
|
app.get(
|
||||||
|
'/greet',
|
||||||
|
ioc((@Query('name', match: 'emoji') String name) => '😇🔥🔥🔥'),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Handle any other query value of `name`.
|
||||||
|
app.get(
|
||||||
|
'/greet',
|
||||||
|
ioc((@Query('name') String name) => 'Hello, $name!'),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Simple fallback to throw a 404 on unknown paths.
|
||||||
|
app.fallback((req, res) {
|
||||||
|
throw HttpException.notFound(
|
||||||
|
message: 'Unknown path: "${req.uri!.path}"',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
var server = await http.startServer('127.0.0.1', 3000);
|
||||||
|
var url = 'http://${server.address.address}:${server.port}';
|
||||||
|
print('Listening at $url');
|
||||||
|
print('Visit these pages to see Angel in action:');
|
||||||
|
print('* $url/greet/bob');
|
||||||
|
print('* $url/greet/?name=emoji');
|
||||||
|
print('* $url/greet/?name=jack');
|
||||||
|
print('* $url/nonexistent_page');
|
||||||
|
}
|
22
packages/framework/example/map_service.dart
Normal file
22
packages/framework/example/map_service.dart
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
// Logging set up/boilerplate
|
||||||
|
Logger.root.onRecord.listen(print);
|
||||||
|
|
||||||
|
// Create our server.
|
||||||
|
var app = Angel(
|
||||||
|
logger: Logger('angel'),
|
||||||
|
reflector: MirrorsReflector(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Create a RESTful service that manages an in-memory collection.
|
||||||
|
app.use('/api/todos', MapService());
|
||||||
|
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
await http.startServer('127.0.0.1', 0);
|
||||||
|
print('Listening at ${http.uri}');
|
||||||
|
}
|
14
packages/framework/example/status.dart
Normal file
14
packages/framework/example/status.dart
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
var app = Angel();
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
|
||||||
|
app.fallback((req, res) {
|
||||||
|
res.statusCode = 304;
|
||||||
|
});
|
||||||
|
|
||||||
|
await http.startServer('127.0.0.1', 3000);
|
||||||
|
print('Listening at ${http.uri}');
|
||||||
|
}
|
18
packages/framework/example/view.dart
Normal file
18
packages/framework/example/view.dart
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
var app = Angel(reflector: MirrorsReflector());
|
||||||
|
|
||||||
|
app.viewGenerator = (name, [data]) async =>
|
||||||
|
'View generator invoked with name $name and data: $data';
|
||||||
|
|
||||||
|
// Index route. Returns JSON.
|
||||||
|
app.get('/', (req, res) => res.render('index', {'foo': 'bar'}));
|
||||||
|
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
var server = await http.startServer('127.0.0.1', 3000);
|
||||||
|
var url = 'http://${server.address.address}:${server.port}';
|
||||||
|
print('Listening at $url');
|
||||||
|
}
|
9
packages/framework/example/views/index.jl
Normal file
9
packages/framework/example/views/index.jl
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Title</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Hello!</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
1
packages/framework/lib/http.dart
Normal file
1
packages/framework/lib/http.dart
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export 'src/http/http.dart';
|
3
packages/framework/lib/http2.dart
Normal file
3
packages/framework/lib/http2.dart
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
export 'src/http2/angel_http2.dart';
|
||||||
|
export 'src/http2/http2_request_context.dart';
|
||||||
|
export 'src/http2/http2_response_context.dart';
|
7
packages/framework/lib/platform_framework.dart
Normal file
7
packages/framework/lib/platform_framework.dart
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
/// An easily-extensible web server framework in Dart.
|
||||||
|
library angel3_framework;
|
||||||
|
|
||||||
|
export 'package:platform_exceptions/http_exception.dart';
|
||||||
|
export 'package:platform_model/platform_model.dart';
|
||||||
|
export 'package:platform_route/platform_route.dart';
|
||||||
|
export 'src/core/core.dart';
|
61
packages/framework/lib/src/core/anonymous_service.dart
Normal file
61
packages/framework/lib/src/core/anonymous_service.dart
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'service.dart';
|
||||||
|
|
||||||
|
/// An easy helper class to create one-off services without having to create an entire class.
|
||||||
|
///
|
||||||
|
/// Well-suited for testing.
|
||||||
|
class AnonymousService<Id, Data> extends Service<Id, Data> {
|
||||||
|
FutureOr<List<Data>> Function([Map<String, dynamic>?])? _index;
|
||||||
|
FutureOr<Data> Function(Id, [Map<String, dynamic>?])? _read, _remove;
|
||||||
|
FutureOr<Data> Function(Data, [Map<String, dynamic>?])? _create;
|
||||||
|
FutureOr<Data> Function(Id, Data, [Map<String, dynamic>?])? _modify, _update;
|
||||||
|
|
||||||
|
AnonymousService(
|
||||||
|
{FutureOr<List<Data>> Function([Map<String, dynamic>? params])? index,
|
||||||
|
FutureOr<Data> Function(Id id, [Map<String, dynamic>? params])? read,
|
||||||
|
FutureOr<Data> Function(Data data, [Map<String, dynamic>? params])?
|
||||||
|
create,
|
||||||
|
FutureOr<Data> Function(Id id, Data data, [Map<String, dynamic>? params])?
|
||||||
|
modify,
|
||||||
|
FutureOr<Data> Function(Id id, Data data, [Map<String, dynamic>? params])?
|
||||||
|
update,
|
||||||
|
FutureOr<Data> Function(Id id, [Map<String, dynamic>? params])? remove,
|
||||||
|
super.readData}) {
|
||||||
|
_index = index;
|
||||||
|
_read = read;
|
||||||
|
_create = create;
|
||||||
|
_modify = modify;
|
||||||
|
_update = update;
|
||||||
|
_remove = remove;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<List<Data>> index([Map<String, dynamic>? params]) =>
|
||||||
|
Future.sync(() => _index != null ? _index!(params) : super.index(params));
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Data> read(Id id, [Map<String, dynamic>? params]) => Future.sync(
|
||||||
|
() => _read != null ? _read!(id, params) : super.read(id, params));
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Data> create(Data data, [Map<String, dynamic>? params]) =>
|
||||||
|
Future.sync(() => _create != null
|
||||||
|
? _create!(data, params)
|
||||||
|
: super.create(data, params));
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Data> modify(Id id, Data data, [Map<String, dynamic>? params]) =>
|
||||||
|
Future.sync(() => _modify != null
|
||||||
|
? _modify!(id, data, params)
|
||||||
|
: super.modify(id, data, params));
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Data> update(Id id, Data data, [Map<String, dynamic>? params]) =>
|
||||||
|
Future.sync(() => _update != null
|
||||||
|
? _update!(id, data, params)
|
||||||
|
: super.update(id, data, params));
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Data> remove(Id id, [Map<String, dynamic>? params]) => Future.sync(
|
||||||
|
() => _remove != null ? _remove!(id, params) : super.remove(id, params));
|
||||||
|
}
|
242
packages/framework/lib/src/core/controller.dart
Normal file
242
packages/framework/lib/src/core/controller.dart
Normal file
|
@ -0,0 +1,242 @@
|
||||||
|
library platform_framework.http.controller;
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:platform_container/platform_container.dart';
|
||||||
|
import 'package:platform_route/platform_route.dart';
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:recase/recase.dart';
|
||||||
|
import '../core/core.dart';
|
||||||
|
|
||||||
|
/// Supports grouping routes with shared functionality.
|
||||||
|
class Controller {
|
||||||
|
Angel? _app;
|
||||||
|
|
||||||
|
/// The [Angel] application powering this controller.
|
||||||
|
Angel get app {
|
||||||
|
if (_app == null) {
|
||||||
|
throw ArgumentError("Angel is not instantiated.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return _app!;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// If `true` (default), this class will inject itself as a singleton into the [app]'s container when bootstrapped.
|
||||||
|
final bool injectSingleton;
|
||||||
|
|
||||||
|
/// Middleware to run before all handlers in this class.
|
||||||
|
List<RequestHandler> middleware = [];
|
||||||
|
|
||||||
|
/// A mapping of route paths to routes, produced from the [Expose] annotations on this class.
|
||||||
|
Map<String, Route> routeMappings = {};
|
||||||
|
|
||||||
|
SymlinkRoute<RequestHandler>? _mountPoint;
|
||||||
|
|
||||||
|
/// The route at which this controller is mounted on the server.
|
||||||
|
SymlinkRoute<RequestHandler>? get mountPoint => _mountPoint;
|
||||||
|
|
||||||
|
Controller({this.injectSingleton = true});
|
||||||
|
|
||||||
|
/// Applies routes, DI, and other configuration to an [app].
|
||||||
|
@mustCallSuper
|
||||||
|
Future<void> configureServer(Angel app) async {
|
||||||
|
_app = app;
|
||||||
|
|
||||||
|
if (injectSingleton != false) {
|
||||||
|
if (!app.container.has(runtimeType)) {
|
||||||
|
_app!.container.registerSingleton(this, as: runtimeType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var name = await applyRoutes(app, app.container.reflector);
|
||||||
|
app.controllers[name] = this;
|
||||||
|
//return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Applies the routes from this [Controller] to some [router].
|
||||||
|
Future<String> applyRoutes(
|
||||||
|
Router<RequestHandler> router, Reflector reflector) async {
|
||||||
|
// Load global expose decl
|
||||||
|
var classMirror = reflector.reflectClass(runtimeType)!;
|
||||||
|
var exposeDecl = findExpose(reflector);
|
||||||
|
|
||||||
|
if (exposeDecl == null) {
|
||||||
|
throw Exception('All controllers must carry an @Expose() declaration.');
|
||||||
|
}
|
||||||
|
|
||||||
|
var routable = Routable();
|
||||||
|
_mountPoint = router.mount(exposeDecl.path, routable);
|
||||||
|
//_mountPoint = m;
|
||||||
|
var typeMirror = reflector.reflectType(runtimeType);
|
||||||
|
|
||||||
|
// Pre-reflect methods
|
||||||
|
var instanceMirror = reflector.reflectInstance(this);
|
||||||
|
final handlers = <RequestHandler>[...exposeDecl.middleware, ...middleware];
|
||||||
|
final routeBuilder =
|
||||||
|
_routeBuilder(reflector, instanceMirror, routable, handlers);
|
||||||
|
await configureRoutes(routable);
|
||||||
|
classMirror.declarations.forEach(routeBuilder);
|
||||||
|
|
||||||
|
// Return the name.
|
||||||
|
var result =
|
||||||
|
exposeDecl.as?.isNotEmpty == true ? exposeDecl.as : typeMirror!.name;
|
||||||
|
|
||||||
|
return Future.value(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Function(ReflectedDeclaration) _routeBuilder(
|
||||||
|
Reflector reflector,
|
||||||
|
ReflectedInstance? instanceMirror,
|
||||||
|
Routable routable,
|
||||||
|
Iterable<RequestHandler> handlers) {
|
||||||
|
return (ReflectedDeclaration decl) {
|
||||||
|
var methodName = decl.name;
|
||||||
|
|
||||||
|
// Ignore built-in methods.
|
||||||
|
if (methodName != 'toString' &&
|
||||||
|
methodName != 'noSuchMethod' &&
|
||||||
|
methodName != 'call' &&
|
||||||
|
methodName != 'equals' &&
|
||||||
|
methodName != '==') {
|
||||||
|
var exposeDecl = decl.function!.annotations
|
||||||
|
.map((m) => m.reflectee)
|
||||||
|
.firstWhere((r) => r is Expose, orElse: () => null) as Expose?;
|
||||||
|
|
||||||
|
if (exposeDecl == null) {
|
||||||
|
// If this has a @noExpose, return null.
|
||||||
|
if (decl.function!.annotations.any((m) => m.reflectee is NoExpose)) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
// Otherwise, create an @Expose.
|
||||||
|
exposeDecl = Expose('');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var reflectedMethod =
|
||||||
|
instanceMirror!.getField(methodName).reflectee as Function?;
|
||||||
|
var middleware = <RequestHandler>[
|
||||||
|
...handlers,
|
||||||
|
...exposeDecl.middleware
|
||||||
|
];
|
||||||
|
var name =
|
||||||
|
exposeDecl.as?.isNotEmpty == true ? exposeDecl.as : methodName;
|
||||||
|
|
||||||
|
// Check if normal
|
||||||
|
var method = decl.function!;
|
||||||
|
if (method.parameters.length == 2 &&
|
||||||
|
method.parameters[0].type.reflectedType == RequestContext &&
|
||||||
|
method.parameters[1].type.reflectedType == ResponseContext) {
|
||||||
|
// Create a regular route
|
||||||
|
routeMappings[name ?? ''] = routable
|
||||||
|
.addRoute(exposeDecl.method, exposeDecl.path,
|
||||||
|
(RequestContext req, ResponseContext res) {
|
||||||
|
var result = reflectedMethod!(req, res);
|
||||||
|
return result is RequestHandler ? result(req, res) : result;
|
||||||
|
}, middleware: middleware);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var injection = preInject(reflectedMethod!, reflector);
|
||||||
|
|
||||||
|
if (exposeDecl.allowNull.isNotEmpty == true) {
|
||||||
|
injection.optional.addAll(exposeDecl.allowNull);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is no path, reverse-engineer one.
|
||||||
|
var path = exposeDecl.path;
|
||||||
|
var httpMethod = exposeDecl.method;
|
||||||
|
if (path == '') {
|
||||||
|
// Try to build a route path by finding all potential
|
||||||
|
// path segments, and then joining them.
|
||||||
|
var parts = <String>[];
|
||||||
|
|
||||||
|
// If the name starts with get/post/patch, etc., then that
|
||||||
|
// should be the path.
|
||||||
|
var methodMatch = _methods.firstMatch(method.name);
|
||||||
|
if (methodMatch != null) {
|
||||||
|
var rest = method.name.replaceAll(_methods, '');
|
||||||
|
var restPath = ReCase(rest.isEmpty ? 'index' : rest)
|
||||||
|
.snakeCase
|
||||||
|
.replaceAll(_rgxMultipleUnderscores, '_');
|
||||||
|
httpMethod = methodMatch[1]!.toUpperCase();
|
||||||
|
|
||||||
|
if (['index', 'by_id'].contains(restPath)) {
|
||||||
|
parts.add('/');
|
||||||
|
} else {
|
||||||
|
parts.add(restPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If the name does NOT start with get/post/patch, etc. then
|
||||||
|
// snake_case-ify the name, and add it to the list of segments.
|
||||||
|
// If the name is index, though, add "/".
|
||||||
|
else {
|
||||||
|
if (method.name == 'index') {
|
||||||
|
parts.add('/');
|
||||||
|
} else {
|
||||||
|
parts.add(ReCase(method.name)
|
||||||
|
.snakeCase
|
||||||
|
.replaceAll(_rgxMultipleUnderscores, '_'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to infer String, int, or double. We called
|
||||||
|
// preInject() earlier, so we can figure out the types
|
||||||
|
// of required parameters, and add those to the path.
|
||||||
|
for (var p in injection.required) {
|
||||||
|
if (p is List && p.length == 2 && p[0] is String && p[1] is Type) {
|
||||||
|
var name = p[0] as String;
|
||||||
|
var type = p[1] as Type;
|
||||||
|
if (type == String) {
|
||||||
|
parts.add(':$name');
|
||||||
|
} else if (type == int) {
|
||||||
|
parts.add('int:$name');
|
||||||
|
} else if (type == double) {
|
||||||
|
parts.add('double:$name');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
path = parts.join('/');
|
||||||
|
if (!path.startsWith('/')) path = '/$path';
|
||||||
|
}
|
||||||
|
|
||||||
|
routeMappings[name ?? ''] = routable.addRoute(
|
||||||
|
httpMethod, path, handleContained(reflectedMethod, injection),
|
||||||
|
middleware: middleware);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Used to add additional routes or middlewares to the router from within
|
||||||
|
/// a [Controller].
|
||||||
|
///
|
||||||
|
/// ```dart
|
||||||
|
/// @override
|
||||||
|
/// FutureOr<void> configureRoutes(Routable routable) {
|
||||||
|
/// routable.all('*', myMiddleware);
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
FutureOr<void> configureRoutes(Routable routable) {}
|
||||||
|
|
||||||
|
static final RegExp _methods = RegExp(r'^(get|post|patch|delete)');
|
||||||
|
static final RegExp _rgxMultipleUnderscores = RegExp(r'__+');
|
||||||
|
|
||||||
|
/// Finds the [Expose] declaration for this class.
|
||||||
|
///
|
||||||
|
/// If [concreteOnly] is `false`, then if there is no actual
|
||||||
|
/// [Expose], one will be automatically created.
|
||||||
|
Expose? findExpose(Reflector reflector, {bool concreteOnly = false}) {
|
||||||
|
var existing = reflector
|
||||||
|
.reflectClass(runtimeType)!
|
||||||
|
.annotations
|
||||||
|
.map((m) => m.reflectee)
|
||||||
|
.firstWhere((r) => r is Expose, orElse: () => null) as Expose?;
|
||||||
|
return existing ??
|
||||||
|
(concreteOnly
|
||||||
|
? null
|
||||||
|
: Expose(ReCase(runtimeType.toString())
|
||||||
|
.snakeCase
|
||||||
|
.replaceAll('_controller', '')
|
||||||
|
.replaceAll('_ctrl', '')
|
||||||
|
.replaceAll(_rgxMultipleUnderscores, '_')));
|
||||||
|
}
|
||||||
|
}
|
14
packages/framework/lib/src/core/core.dart
Normal file
14
packages/framework/lib/src/core/core.dart
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
export 'anonymous_service.dart';
|
||||||
|
export 'controller.dart';
|
||||||
|
export 'driver.dart';
|
||||||
|
export 'env.dart';
|
||||||
|
export 'hooked_service.dart';
|
||||||
|
export 'hostname_parser.dart';
|
||||||
|
export 'hostname_router.dart';
|
||||||
|
export 'map_service.dart';
|
||||||
|
export 'metadata.dart';
|
||||||
|
export 'request_context.dart';
|
||||||
|
export 'response_context.dart';
|
||||||
|
export 'routable.dart';
|
||||||
|
export 'server.dart';
|
||||||
|
export 'service.dart';
|
400
packages/framework/lib/src/core/driver.dart
Normal file
400
packages/framework/lib/src/core/driver.dart
Normal file
|
@ -0,0 +1,400 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io' show Cookie;
|
||||||
|
import 'package:platform_exceptions/http_exception.dart';
|
||||||
|
import 'package:platform_route/platform_route.dart';
|
||||||
|
import 'package:belatuk_combinator/belatuk_combinator.dart';
|
||||||
|
import 'package:stack_trace/stack_trace.dart';
|
||||||
|
import 'package:tuple/tuple.dart';
|
||||||
|
import 'core.dart';
|
||||||
|
|
||||||
|
/// Base driver class for Angel implementations.
|
||||||
|
///
|
||||||
|
/// Powers both AngelHttp and AngelHttp2.
|
||||||
|
abstract class Driver<
|
||||||
|
Request,
|
||||||
|
Response,
|
||||||
|
Server extends Stream<Request>,
|
||||||
|
RequestContextType extends RequestContext,
|
||||||
|
ResponseContextType extends ResponseContext> {
|
||||||
|
final Angel app;
|
||||||
|
final bool useZone;
|
||||||
|
bool _closed = false;
|
||||||
|
|
||||||
|
StreamSubscription<Request>? _sub;
|
||||||
|
//final log = Logger('Driver');
|
||||||
|
|
||||||
|
/// The function used to bind this instance to a server..
|
||||||
|
final Future<Server> Function(dynamic, int) serverGenerator;
|
||||||
|
|
||||||
|
Driver(this.app, this.serverGenerator, {this.useZone = true});
|
||||||
|
|
||||||
|
/// The path at which this server is listening for requests.
|
||||||
|
Uri get uri;
|
||||||
|
|
||||||
|
/// The native server running this instance.
|
||||||
|
Server? server;
|
||||||
|
|
||||||
|
Future<Server> generateServer(address, int port) =>
|
||||||
|
serverGenerator(address, port);
|
||||||
|
|
||||||
|
/// Starts, and returns the server.
|
||||||
|
Future<Server> startServer([address, int port = 0]) {
|
||||||
|
var host = address ?? '127.0.0.1';
|
||||||
|
return generateServer(host, port).then((server) {
|
||||||
|
this.server = server;
|
||||||
|
|
||||||
|
return Future.wait(app.startupHooks.map(app.configure)).then((_) {
|
||||||
|
app.optimizeForProduction();
|
||||||
|
_sub = this.server?.listen((request) {
|
||||||
|
var stream = createResponseStreamFromRawRequest(request);
|
||||||
|
stream.listen((response) {
|
||||||
|
// TODO: To be revisited
|
||||||
|
handleRawRequest(request, response);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return Future.value(this.server!);
|
||||||
|
});
|
||||||
|
}).catchError((error) {
|
||||||
|
app.logger.severe('Failed to create server', error);
|
||||||
|
throw ArgumentError('[Driver]Failed to create server');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shuts down the underlying server.
|
||||||
|
Future<void> close() {
|
||||||
|
if (_closed) {
|
||||||
|
//return Future.value(_server);
|
||||||
|
return Future.value();
|
||||||
|
}
|
||||||
|
_closed = true;
|
||||||
|
|
||||||
|
_sub?.cancel();
|
||||||
|
|
||||||
|
return app.close().then((_) =>
|
||||||
|
Future.wait(app.shutdownHooks.map(app.configure))
|
||||||
|
.then((_) => Future.value()));
|
||||||
|
/*
|
||||||
|
return app.close().then((_) =>
|
||||||
|
Future.wait(app.shutdownHooks.map(app.configure))
|
||||||
|
.then((_) => Future.value(_server)));
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<RequestContextType> createRequestContext(
|
||||||
|
Request request, Response response);
|
||||||
|
|
||||||
|
Future<ResponseContextType> createResponseContext(
|
||||||
|
Request request, Response response,
|
||||||
|
[RequestContextType? correspondingRequest]);
|
||||||
|
|
||||||
|
void setHeader(Response response, String key, String value);
|
||||||
|
|
||||||
|
void setContentLength(Response response, int length);
|
||||||
|
|
||||||
|
void setChunkedEncoding(Response response, bool value);
|
||||||
|
|
||||||
|
void setStatusCode(Response response, int value);
|
||||||
|
|
||||||
|
void addCookies(Response response, Iterable<Cookie> cookies);
|
||||||
|
|
||||||
|
void writeStringToResponse(Response response, String value);
|
||||||
|
|
||||||
|
void writeToResponse(Response response, List<int> data);
|
||||||
|
|
||||||
|
Future closeResponse(Response response);
|
||||||
|
|
||||||
|
Stream<Response> createResponseStreamFromRawRequest(Request request);
|
||||||
|
|
||||||
|
/// Handles a single request.
|
||||||
|
Future handleRawRequest(Request request, Response response) {
|
||||||
|
app.logger.info('[Server] Called handleRawRequest');
|
||||||
|
|
||||||
|
return createRequestContext(request, response).then((req) {
|
||||||
|
return createResponseContext(request, response, req).then((res) {
|
||||||
|
Future handle() {
|
||||||
|
var path = req.path;
|
||||||
|
if (path == '/') path = '';
|
||||||
|
|
||||||
|
Tuple4<List, Map<String, dynamic>, ParseResult<RouteResult>,
|
||||||
|
MiddlewarePipeline> resolveTuple() {
|
||||||
|
var r = app.optimizedRouter;
|
||||||
|
var resolved =
|
||||||
|
r.resolveAbsolute(path, method: req.method, strip: false);
|
||||||
|
var pipeline = MiddlewarePipeline<RequestHandler>(resolved);
|
||||||
|
return Tuple4(
|
||||||
|
pipeline.handlers,
|
||||||
|
resolved.fold<Map<String, dynamic>>(
|
||||||
|
<String, dynamic>{}, (out, r) => out..addAll(r.allParams)),
|
||||||
|
//(resolved.isEmpty ? null : resolved.first.parseResult),
|
||||||
|
resolved.first.parseResult,
|
||||||
|
pipeline,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
var cacheKey = req.method + path;
|
||||||
|
var tuple = app.environment.isProduction
|
||||||
|
? app.handlerCache.putIfAbsent(cacheKey, resolveTuple)
|
||||||
|
: resolveTuple();
|
||||||
|
var line = tuple.item4 as MiddlewarePipeline<RequestHandler>;
|
||||||
|
var it = MiddlewarePipelineIterator<RequestHandler>(line);
|
||||||
|
|
||||||
|
req.params.addAll(tuple.item2);
|
||||||
|
|
||||||
|
req.container
|
||||||
|
?..registerSingleton<RequestContext>(req)
|
||||||
|
..registerSingleton<ResponseContext>(res)
|
||||||
|
..registerSingleton<MiddlewarePipeline>(tuple.item4)
|
||||||
|
..registerSingleton<MiddlewarePipeline<RequestHandler>>(line)
|
||||||
|
..registerSingleton<MiddlewarePipelineIterator>(it)
|
||||||
|
..registerSingleton<MiddlewarePipelineIterator<RequestHandler>>(it)
|
||||||
|
..registerSingleton<ParseResult<RouteResult>?>(tuple.item3)
|
||||||
|
..registerSingleton<ParseResult?>(tuple.item3);
|
||||||
|
|
||||||
|
if (!app.environment.isProduction) {
|
||||||
|
req.container?.registerSingleton<Stopwatch>(Stopwatch()..start());
|
||||||
|
}
|
||||||
|
|
||||||
|
return runPipeline(it, req, res, app)
|
||||||
|
.then((_) => sendResponse(request, response, req, res));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (useZone == false) {
|
||||||
|
Future f;
|
||||||
|
|
||||||
|
try {
|
||||||
|
f = handle();
|
||||||
|
} catch (e, st) {
|
||||||
|
f = Future.error(e, st);
|
||||||
|
}
|
||||||
|
|
||||||
|
return f.catchError((e, StackTrace st) {
|
||||||
|
if (e is FormatException) {
|
||||||
|
throw HttpException.badRequest(message: e.message)
|
||||||
|
..stackTrace = st;
|
||||||
|
}
|
||||||
|
throw HttpException(
|
||||||
|
stackTrace: st,
|
||||||
|
statusCode: (e is HttpException) ? e.statusCode : 500,
|
||||||
|
message: e?.toString() ?? '500 Internal Server Error');
|
||||||
|
}, test: (e) => e is HttpException).catchError((ee, StackTrace st) {
|
||||||
|
//print(">>>> Framework error: $ee");
|
||||||
|
//var t = (st).runtimeType;
|
||||||
|
//print(">>>> StackTrace: $t");
|
||||||
|
HttpException e;
|
||||||
|
if (ee is HttpException) {
|
||||||
|
e = ee;
|
||||||
|
} else {
|
||||||
|
e = HttpException(
|
||||||
|
stackTrace: st,
|
||||||
|
statusCode: 500,
|
||||||
|
message: ee?.toString() ?? '500 Internal Server Error');
|
||||||
|
}
|
||||||
|
|
||||||
|
var error = e.error ?? e;
|
||||||
|
var trace = Trace.from(StackTrace.current).terse;
|
||||||
|
app.logger.severe(e.message, error, trace);
|
||||||
|
|
||||||
|
return handleHttpException(e, st, req, res, request, response);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
var zoneSpec = ZoneSpecification(
|
||||||
|
print: (self, parent, zone, line) {
|
||||||
|
app.logger.info(line);
|
||||||
|
},
|
||||||
|
handleUncaughtError: (self, parent, zone, error, stackTrace) {
|
||||||
|
var trace = Trace.from(stackTrace).terse;
|
||||||
|
|
||||||
|
// TODO: To be revisited
|
||||||
|
Future(() {
|
||||||
|
HttpException e;
|
||||||
|
|
||||||
|
if (error is FormatException) {
|
||||||
|
e = HttpException.badRequest(message: error.message);
|
||||||
|
} else if (error is HttpException) {
|
||||||
|
e = error;
|
||||||
|
} else {
|
||||||
|
e = HttpException(
|
||||||
|
stackTrace: stackTrace, message: error.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
app.logger.severe(e.message, error, trace);
|
||||||
|
|
||||||
|
return handleHttpException(
|
||||||
|
e, trace, req, res, request, response);
|
||||||
|
}).catchError((e, StackTrace st) {
|
||||||
|
var trace = Trace.from(st).terse;
|
||||||
|
closeResponse(response);
|
||||||
|
// Ideally, we won't be in a position where an absolutely fatal error occurs,
|
||||||
|
// but if so, we'll need to log it.
|
||||||
|
app.logger.severe(
|
||||||
|
'Fatal error occurred when processing $uri.', e, trace);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
var zone = Zone.current.fork(specification: zoneSpec);
|
||||||
|
req.container?.registerSingleton<Zone>(zone);
|
||||||
|
req.container?.registerSingleton<ZoneSpecification>(zoneSpec);
|
||||||
|
|
||||||
|
// If a synchronous error is thrown, it's not caught by `zone.run`,
|
||||||
|
// so use a try/catch, and recover when need be.
|
||||||
|
|
||||||
|
try {
|
||||||
|
return zone.run(handle);
|
||||||
|
} catch (e, st) {
|
||||||
|
zone.handleUncaughtError(e, st);
|
||||||
|
return Future.value();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handles an [HttpException].
|
||||||
|
Future handleHttpException(
|
||||||
|
HttpException e,
|
||||||
|
StackTrace st,
|
||||||
|
RequestContext? req,
|
||||||
|
ResponseContext? res,
|
||||||
|
Request request,
|
||||||
|
Response response,
|
||||||
|
{bool ignoreFinalizers = false}) {
|
||||||
|
if (req == null || res == null) {
|
||||||
|
try {
|
||||||
|
app.logger.severe('500 Internal Server Error', e, st);
|
||||||
|
setStatusCode(response, 500);
|
||||||
|
writeStringToResponse(response, '500 Internal Server Error');
|
||||||
|
closeResponse(response);
|
||||||
|
} catch (e) {
|
||||||
|
app.logger.severe('500 Internal Server Error', e);
|
||||||
|
}
|
||||||
|
return Future.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future handleError;
|
||||||
|
|
||||||
|
if (!res.isOpen) {
|
||||||
|
handleError = Future.value();
|
||||||
|
} else {
|
||||||
|
res.statusCode = e.statusCode;
|
||||||
|
handleError =
|
||||||
|
Future.sync(() => app.errorHandler(e, req, res)).then((result) {
|
||||||
|
return app.executeHandler(result, req, res).then((_) => res.close());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return handleError.then((_) => sendResponse(request, response, req, res,
|
||||||
|
ignoreFinalizers: ignoreFinalizers == true));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends a response.
|
||||||
|
Future sendResponse(Request request, Response response, RequestContext req,
|
||||||
|
ResponseContext res,
|
||||||
|
{bool ignoreFinalizers = false}) {
|
||||||
|
//app.logger.fine("Calling SendResponse");
|
||||||
|
Future<void> cleanup(_) {
|
||||||
|
if (!app.environment.isProduction && req.container!.has<Stopwatch>()) {
|
||||||
|
var sw = req.container!.make<Stopwatch>();
|
||||||
|
app.logger.fine(
|
||||||
|
"${res.statusCode} ${req.method} ${req.uri} (${sw.elapsedMilliseconds} ms)");
|
||||||
|
}
|
||||||
|
return req.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Debugging header
|
||||||
|
/*
|
||||||
|
for (var key in res.headers.keys) {
|
||||||
|
app.logger.fine("Response header key: $key");
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!res.isBuffered) {
|
||||||
|
//if (res.isOpen) {
|
||||||
|
return res.close().then(cleanup);
|
||||||
|
//}
|
||||||
|
//return Future.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
//app.logger.fine("Calling finalizers");
|
||||||
|
|
||||||
|
var finalizers = ignoreFinalizers == true
|
||||||
|
? Future.value()
|
||||||
|
: Future.forEach(app.responseFinalizers, (dynamic f) => f(req, res));
|
||||||
|
|
||||||
|
return finalizers.then((_) {
|
||||||
|
//if (res.isOpen) res.close();
|
||||||
|
|
||||||
|
for (var key in res.headers.keys) {
|
||||||
|
app.logger.fine("Response header key: $key");
|
||||||
|
setHeader(response, key, res.headers[key] ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
|
setContentLength(response, res.buffer?.length ?? 0);
|
||||||
|
setChunkedEncoding(response, res.chunked ?? true);
|
||||||
|
|
||||||
|
var outputBuffer = res.buffer?.toBytes() ?? <int>[];
|
||||||
|
|
||||||
|
if (res.encoders.isNotEmpty) {
|
||||||
|
var allowedEncodings = req.headers
|
||||||
|
?.value('accept-encoding')
|
||||||
|
?.split(',')
|
||||||
|
.map((s) => s.trim())
|
||||||
|
.where((s) => s.isNotEmpty)
|
||||||
|
.map((str) {
|
||||||
|
// Ignore quality specifications in accept-encoding
|
||||||
|
// ex. gzip;q=0.8
|
||||||
|
if (!str.contains(';')) return str;
|
||||||
|
return str.split(';')[0];
|
||||||
|
});
|
||||||
|
|
||||||
|
if (allowedEncodings != null) {
|
||||||
|
for (var encodingName in allowedEncodings) {
|
||||||
|
var key = encodingName;
|
||||||
|
|
||||||
|
Converter<List<int>, List<int>>? encoder;
|
||||||
|
if (res.encoders.containsKey(encodingName)) {
|
||||||
|
encoder = res.encoders[encodingName];
|
||||||
|
} else if (encodingName == '*') {
|
||||||
|
encoder = res.encoders[key = res.encoders.keys.first];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (encoder != null) {
|
||||||
|
setHeader(response, 'content-encoding', key);
|
||||||
|
outputBuffer =
|
||||||
|
res.encoders[key]?.convert(outputBuffer) ?? <int>[];
|
||||||
|
setContentLength(response, outputBuffer.length);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setStatusCode(response, res.statusCode);
|
||||||
|
addCookies(response, res.cookies);
|
||||||
|
writeToResponse(response, outputBuffer);
|
||||||
|
return closeResponse(response).then(cleanup);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs a [MiddlewarePipeline].
|
||||||
|
static Future<void> runPipeline<RequestContextType extends RequestContext,
|
||||||
|
ResponseContextType extends ResponseContext>(
|
||||||
|
MiddlewarePipelineIterator<RequestHandler> it,
|
||||||
|
RequestContextType req,
|
||||||
|
ResponseContextType res,
|
||||||
|
Angel app) async {
|
||||||
|
var broken = false;
|
||||||
|
while (it.moveNext()) {
|
||||||
|
var current = it.current.handlers.iterator;
|
||||||
|
|
||||||
|
while (!broken && current.moveNext()) {
|
||||||
|
var result = await app.executeHandler(current.current, req, res);
|
||||||
|
if (result != true) {
|
||||||
|
broken = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
27
packages/framework/lib/src/core/env.dart
Normal file
27
packages/framework/lib/src/core/env.dart
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
/// A constant instance of [AngelEnv].
|
||||||
|
const AngelEnvironment angelEnv = AngelEnvironment();
|
||||||
|
|
||||||
|
/// Queries the environment's `ANGEL_ENV` value.
|
||||||
|
class AngelEnvironment {
|
||||||
|
final String? _customValue;
|
||||||
|
|
||||||
|
/// You can optionally provide a custom value, in order to override the system's
|
||||||
|
/// value.
|
||||||
|
const AngelEnvironment([this._customValue]);
|
||||||
|
|
||||||
|
/// Returns the value of the `ANGEL_ENV` variable; defaults to `'development'`.
|
||||||
|
String get value =>
|
||||||
|
(_customValue ?? Platform.environment['ANGEL_ENV'] ?? 'development')
|
||||||
|
.toLowerCase();
|
||||||
|
|
||||||
|
/// Returns whether the [value] is `'development'`.
|
||||||
|
bool get isDevelopment => value == 'development';
|
||||||
|
|
||||||
|
/// Returns whether the [value] is `'production'`.
|
||||||
|
bool get isProduction => value == 'production';
|
||||||
|
|
||||||
|
/// Returns whether the [value] is `'staging'`.
|
||||||
|
bool get isStaging => value == 'staging';
|
||||||
|
}
|
605
packages/framework/lib/src/core/hooked_service.dart
Normal file
605
packages/framework/lib/src/core/hooked_service.dart
Normal file
|
@ -0,0 +1,605 @@
|
||||||
|
library platform_framework.core.hooked_service;
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import '../util.dart';
|
||||||
|
import 'metadata.dart';
|
||||||
|
import 'request_context.dart';
|
||||||
|
import 'response_context.dart';
|
||||||
|
import 'routable.dart';
|
||||||
|
import 'server.dart';
|
||||||
|
import 'service.dart';
|
||||||
|
|
||||||
|
/// Wraps another service in a service that broadcasts events on actions.
|
||||||
|
class HookedService<Id, Data, T extends Service<Id, Data>>
|
||||||
|
extends Service<Id, Data> {
|
||||||
|
final List<StreamController<HookedServiceEvent>> _ctrl = [];
|
||||||
|
|
||||||
|
/// Tbe service that is proxied by this hooked one.
|
||||||
|
final T inner;
|
||||||
|
|
||||||
|
final HookedServiceEventDispatcher<Id, Data, T> beforeIndexed =
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T>();
|
||||||
|
final HookedServiceEventDispatcher<Id, Data, T> beforeRead =
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T>();
|
||||||
|
final HookedServiceEventDispatcher<Id, Data, T> beforeCreated =
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T>();
|
||||||
|
final HookedServiceEventDispatcher<Id, Data, T> beforeModified =
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T>();
|
||||||
|
final HookedServiceEventDispatcher<Id, Data, T> beforeUpdated =
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T>();
|
||||||
|
final HookedServiceEventDispatcher<Id, Data, T> beforeRemoved =
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T>();
|
||||||
|
final HookedServiceEventDispatcher<Id, Data, T> afterIndexed =
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T>();
|
||||||
|
final HookedServiceEventDispatcher<Id, Data, T> afterRead =
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T>();
|
||||||
|
final HookedServiceEventDispatcher<Id, Data, T> afterCreated =
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T>();
|
||||||
|
final HookedServiceEventDispatcher<Id, Data, T> afterModified =
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T>();
|
||||||
|
final HookedServiceEventDispatcher<Id, Data, T> afterUpdated =
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T>();
|
||||||
|
final HookedServiceEventDispatcher<Id, Data, T> afterRemoved =
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T>();
|
||||||
|
|
||||||
|
HookedService(this.inner) {
|
||||||
|
// Clone app instance
|
||||||
|
if (inner.isAppActive) {
|
||||||
|
app = inner.app;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
FutureOr<Data> Function(RequestContext, ResponseContext)? get readData =>
|
||||||
|
inner.readData;
|
||||||
|
|
||||||
|
RequestContext? _getRequest(Map? params) {
|
||||||
|
if (params == null) return null;
|
||||||
|
return params['__requestctx'] as RequestContext?;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResponseContext? _getResponse(Map? params) {
|
||||||
|
if (params == null) return null;
|
||||||
|
return params['__responsectx'] as ResponseContext?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> _stripReq(Map<String, dynamic>? params) {
|
||||||
|
if (params == null) {
|
||||||
|
return {};
|
||||||
|
} else {
|
||||||
|
return params.keys
|
||||||
|
.where((key) => key != '__requestctx' && key != '__responsectx')
|
||||||
|
.fold<Map<String, dynamic>>(
|
||||||
|
{}, (map, key) => map..[key] = params[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Closes any open [StreamController]s on this instance. **Internal use only**.
|
||||||
|
@override
|
||||||
|
Future close() {
|
||||||
|
for (var c in _ctrl) {
|
||||||
|
c.close();
|
||||||
|
}
|
||||||
|
beforeIndexed._close();
|
||||||
|
beforeRead._close();
|
||||||
|
beforeCreated._close();
|
||||||
|
beforeModified._close();
|
||||||
|
beforeUpdated._close();
|
||||||
|
beforeRemoved._close();
|
||||||
|
afterIndexed._close();
|
||||||
|
afterRead._close();
|
||||||
|
afterCreated._close();
|
||||||
|
afterModified._close();
|
||||||
|
afterUpdated._close();
|
||||||
|
afterRemoved._close();
|
||||||
|
inner.close();
|
||||||
|
return Future.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds hooks to this instance.
|
||||||
|
void addHooks(Angel app) {
|
||||||
|
var hooks = getAnnotation<Hooks>(inner, app.container.reflector);
|
||||||
|
var before = <HookedServiceEventListener<Id, Data, T>>[];
|
||||||
|
var after = <HookedServiceEventListener<Id, Data, T>>[];
|
||||||
|
|
||||||
|
if (hooks != null) {
|
||||||
|
before.addAll(hooks.before.cast());
|
||||||
|
after.addAll(hooks.after.cast());
|
||||||
|
}
|
||||||
|
|
||||||
|
void applyListeners(
|
||||||
|
Function fn, HookedServiceEventDispatcher<Id, Data, T> dispatcher,
|
||||||
|
[bool? isAfter]) {
|
||||||
|
var hooks = getAnnotation<Hooks>(fn, app.container.reflector);
|
||||||
|
final listeners = <HookedServiceEventListener<Id, Data, T>>[
|
||||||
|
...isAfter == true ? after : before
|
||||||
|
];
|
||||||
|
|
||||||
|
if (hooks != null) {
|
||||||
|
listeners.addAll((isAfter == true ? hooks.after : hooks.before).cast());
|
||||||
|
}
|
||||||
|
|
||||||
|
listeners.forEach(dispatcher.listen);
|
||||||
|
}
|
||||||
|
|
||||||
|
applyListeners(inner.index, beforeIndexed);
|
||||||
|
applyListeners(inner.read, beforeRead);
|
||||||
|
applyListeners(inner.create, beforeCreated);
|
||||||
|
applyListeners(inner.modify, beforeModified);
|
||||||
|
applyListeners(inner.update, beforeUpdated);
|
||||||
|
applyListeners(inner.remove, beforeRemoved);
|
||||||
|
applyListeners(inner.index, afterIndexed, true);
|
||||||
|
applyListeners(inner.read, afterRead, true);
|
||||||
|
applyListeners(inner.create, afterCreated, true);
|
||||||
|
applyListeners(inner.modify, afterModified, true);
|
||||||
|
applyListeners(inner.update, afterUpdated, true);
|
||||||
|
applyListeners(inner.remove, afterRemoved, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<RequestHandler> get bootstrappers =>
|
||||||
|
List<RequestHandler>.from(super.bootstrappers)
|
||||||
|
..add((RequestContext req, ResponseContext res) {
|
||||||
|
req.serviceParams
|
||||||
|
..['__requestctx'] = req
|
||||||
|
..['__responsectx'] = res;
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
void addRoutes([Service? service]) {
|
||||||
|
super.addRoutes(service ?? inner);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the [listener] before every service method specified.
|
||||||
|
void before(Iterable<String> eventNames,
|
||||||
|
HookedServiceEventListener<Id, Data, T> listener) {
|
||||||
|
eventNames.map((name) {
|
||||||
|
switch (name) {
|
||||||
|
case HookedServiceEvent.indexed:
|
||||||
|
return beforeIndexed;
|
||||||
|
case HookedServiceEvent.read:
|
||||||
|
return beforeRead;
|
||||||
|
case HookedServiceEvent.created:
|
||||||
|
return beforeCreated;
|
||||||
|
case HookedServiceEvent.modified:
|
||||||
|
return beforeModified;
|
||||||
|
case HookedServiceEvent.updated:
|
||||||
|
return beforeUpdated;
|
||||||
|
case HookedServiceEvent.removed:
|
||||||
|
return beforeRemoved;
|
||||||
|
default:
|
||||||
|
throw ArgumentError('Invalid service method: $name');
|
||||||
|
}
|
||||||
|
}).forEach((HookedServiceEventDispatcher<Id, Data, T> dispatcher) =>
|
||||||
|
dispatcher.listen(listener));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the [listener] after every service method specified.
|
||||||
|
void after(Iterable<String> eventNames,
|
||||||
|
HookedServiceEventListener<Id, Data, T> listener) {
|
||||||
|
eventNames.map((name) {
|
||||||
|
switch (name) {
|
||||||
|
case HookedServiceEvent.indexed:
|
||||||
|
return afterIndexed;
|
||||||
|
case HookedServiceEvent.read:
|
||||||
|
return afterRead;
|
||||||
|
case HookedServiceEvent.created:
|
||||||
|
return afterCreated;
|
||||||
|
case HookedServiceEvent.modified:
|
||||||
|
return afterModified;
|
||||||
|
case HookedServiceEvent.updated:
|
||||||
|
return afterUpdated;
|
||||||
|
case HookedServiceEvent.removed:
|
||||||
|
return afterRemoved;
|
||||||
|
default:
|
||||||
|
throw ArgumentError('Invalid service method: $name');
|
||||||
|
}
|
||||||
|
}).forEach((HookedServiceEventDispatcher<Id, Data, T> dispatcher) =>
|
||||||
|
dispatcher.listen(listener));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the [listener] before every service method.
|
||||||
|
void beforeAll(HookedServiceEventListener<Id, Data, T> listener) {
|
||||||
|
beforeIndexed.listen(listener);
|
||||||
|
beforeRead.listen(listener);
|
||||||
|
beforeCreated.listen(listener);
|
||||||
|
beforeModified.listen(listener);
|
||||||
|
beforeUpdated.listen(listener);
|
||||||
|
beforeRemoved.listen(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the [listener] after every service method.
|
||||||
|
void afterAll(HookedServiceEventListener<Id, Data, T> listener) {
|
||||||
|
afterIndexed.listen(listener);
|
||||||
|
afterRead.listen(listener);
|
||||||
|
afterCreated.listen(listener);
|
||||||
|
afterModified.listen(listener);
|
||||||
|
afterUpdated.listen(listener);
|
||||||
|
afterRemoved.listen(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a [Stream] of all events fired before every service method.
|
||||||
|
///
|
||||||
|
/// *NOTE*: Only use this if you do not plan to modify events. There is no guarantee
|
||||||
|
/// that events coming out of this [Stream] will see changes you make within the [Stream]
|
||||||
|
/// callback.
|
||||||
|
Stream<HookedServiceEvent<Id, Data, T>> beforeAllStream() {
|
||||||
|
var ctrl = StreamController<HookedServiceEvent<Id, Data, T>>();
|
||||||
|
_ctrl.add(ctrl);
|
||||||
|
before(HookedServiceEvent.all, ctrl.add);
|
||||||
|
return ctrl.stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a [Stream] of all events fired after every service method.
|
||||||
|
///
|
||||||
|
/// *NOTE*: Only use this if you do not plan to modify events. There is no guarantee
|
||||||
|
/// that events coming out of this [Stream] will see changes you make within the [Stream]
|
||||||
|
/// callback.
|
||||||
|
Stream<HookedServiceEvent<Id, Data, T>> afterAllStream() {
|
||||||
|
var ctrl = StreamController<HookedServiceEvent<Id, Data, T>>();
|
||||||
|
_ctrl.add(ctrl);
|
||||||
|
before(HookedServiceEvent.all, ctrl.add);
|
||||||
|
return ctrl.stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a [Stream] of all events fired before every service method specified.
|
||||||
|
///
|
||||||
|
/// *NOTE*: Only use this if you do not plan to modify events. There is no guarantee
|
||||||
|
/// that events coming out of this [Stream] will see changes you make within the [Stream]
|
||||||
|
/// callback.
|
||||||
|
Stream<HookedServiceEvent<Id, Data, T>> beforeStream(
|
||||||
|
Iterable<String> eventNames) {
|
||||||
|
var ctrl = StreamController<HookedServiceEvent<Id, Data, T>>();
|
||||||
|
_ctrl.add(ctrl);
|
||||||
|
before(eventNames, ctrl.add);
|
||||||
|
return ctrl.stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a [Stream] of all events fired AFTER every service method specified.
|
||||||
|
///
|
||||||
|
/// *NOTE*: Only use this if you do not plan to modify events. There is no guarantee
|
||||||
|
/// that events coming out of this [Stream] will see changes you make within the [Stream]
|
||||||
|
/// callback.
|
||||||
|
Stream<HookedServiceEvent<Id, Data, T>> afterStream(
|
||||||
|
Iterable<String> eventNames) {
|
||||||
|
var ctrl = StreamController<HookedServiceEvent<Id, Data, T>>();
|
||||||
|
_ctrl.add(ctrl);
|
||||||
|
after(eventNames, ctrl.add);
|
||||||
|
return ctrl.stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the [listener] before [create], [modify] and [update].
|
||||||
|
void beforeModify(HookedServiceEventListener<Id, Data, T> listener) {
|
||||||
|
beforeCreated.listen(listener);
|
||||||
|
beforeModified.listen(listener);
|
||||||
|
beforeUpdated.listen(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<List<Data>> index([Map<String, dynamic>? params]) {
|
||||||
|
var localParams = _stripReq(params);
|
||||||
|
return beforeIndexed
|
||||||
|
._emit(HookedServiceEvent(false, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.indexed,
|
||||||
|
params: localParams))
|
||||||
|
.then((before) {
|
||||||
|
if (before._canceled) {
|
||||||
|
return afterIndexed
|
||||||
|
._emit(HookedServiceEvent(true, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.indexed,
|
||||||
|
params: localParams, result: before.result))
|
||||||
|
.then((after) => after.result as List<Data>);
|
||||||
|
}
|
||||||
|
|
||||||
|
return inner.index(localParams).then((result) {
|
||||||
|
return afterIndexed
|
||||||
|
._emit(HookedServiceEvent(true, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.indexed,
|
||||||
|
params: localParams, result: result))
|
||||||
|
.then((after) => after.result as List<Data>);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Data> read(Id id, [Map<String, dynamic>? params]) {
|
||||||
|
var localParams = _stripReq(params);
|
||||||
|
return beforeRead
|
||||||
|
._emit(HookedServiceEvent(false, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.read,
|
||||||
|
id: id, params: localParams))
|
||||||
|
.then((before) {
|
||||||
|
if (before._canceled) {
|
||||||
|
return afterRead
|
||||||
|
._emit(HookedServiceEvent(true, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.read,
|
||||||
|
id: id, params: localParams, result: before.result))
|
||||||
|
.then((after) => after.result as Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return inner.read(id, localParams).then((result) {
|
||||||
|
return afterRead
|
||||||
|
._emit(HookedServiceEvent(true, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.read,
|
||||||
|
id: id, params: localParams, result: result))
|
||||||
|
.then((after) => after.result as Data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Data> create(Data data, [Map<String, dynamic>? params]) {
|
||||||
|
var localParams = _stripReq(params);
|
||||||
|
return beforeCreated
|
||||||
|
._emit(HookedServiceEvent(false, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.created,
|
||||||
|
data: data, params: localParams))
|
||||||
|
.then((before) {
|
||||||
|
if (before._canceled) {
|
||||||
|
return afterCreated
|
||||||
|
._emit(HookedServiceEvent(true, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.created,
|
||||||
|
data: before.data, params: localParams, result: before.result))
|
||||||
|
.then((after) => after.result as Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return inner.create(before.data as Data, localParams).then((result) {
|
||||||
|
return afterCreated
|
||||||
|
._emit(HookedServiceEvent(true, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.created,
|
||||||
|
data: before.data, params: localParams, result: result))
|
||||||
|
.then((after) => after.result as Data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Data> modify(Id id, Data data, [Map<String, dynamic>? params]) {
|
||||||
|
var localParams = _stripReq(params);
|
||||||
|
return beforeModified
|
||||||
|
._emit(HookedServiceEvent(false, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.modified,
|
||||||
|
id: id, data: data, params: localParams))
|
||||||
|
.then((before) {
|
||||||
|
if (before._canceled) {
|
||||||
|
return afterModified
|
||||||
|
._emit(HookedServiceEvent(true, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.modified,
|
||||||
|
id: id,
|
||||||
|
data: before.data,
|
||||||
|
params: localParams,
|
||||||
|
result: before.result))
|
||||||
|
.then((after) => after.result as Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return inner.modify(id, before.data as Data, localParams).then((result) {
|
||||||
|
return afterModified
|
||||||
|
._emit(HookedServiceEvent(true, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.created,
|
||||||
|
id: id, data: before.data, params: localParams, result: result))
|
||||||
|
.then((after) => after.result as Data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Data> update(Id id, Data data, [Map<String, dynamic>? params]) {
|
||||||
|
var localParams = _stripReq(params);
|
||||||
|
return beforeUpdated
|
||||||
|
._emit(HookedServiceEvent(false, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.updated,
|
||||||
|
id: id, data: data, params: localParams))
|
||||||
|
.then((before) {
|
||||||
|
if (before._canceled) {
|
||||||
|
return afterUpdated
|
||||||
|
._emit(HookedServiceEvent(true, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.updated,
|
||||||
|
id: id,
|
||||||
|
data: before.data,
|
||||||
|
params: localParams,
|
||||||
|
result: before.result))
|
||||||
|
.then((after) => after.result as Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return inner.update(id, before.data as Data, localParams).then((result) {
|
||||||
|
return afterUpdated
|
||||||
|
._emit(HookedServiceEvent(true, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.updated,
|
||||||
|
id: id, data: before.data, params: localParams, result: result))
|
||||||
|
.then((after) => after.result as Data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Data> remove(Id id, [Map<String, dynamic>? params]) {
|
||||||
|
var localParams = _stripReq(params);
|
||||||
|
return beforeRemoved
|
||||||
|
._emit(HookedServiceEvent(false, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.removed,
|
||||||
|
id: id, params: localParams))
|
||||||
|
.then((before) {
|
||||||
|
if (before._canceled) {
|
||||||
|
return afterRemoved
|
||||||
|
._emit(HookedServiceEvent(true, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.removed,
|
||||||
|
id: id, params: localParams, result: before.result))
|
||||||
|
.then((after) => after.result) as Data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return inner.remove(id, localParams).then((result) {
|
||||||
|
return afterRemoved
|
||||||
|
._emit(HookedServiceEvent(true, _getRequest(params),
|
||||||
|
_getResponse(params), inner, HookedServiceEvent.removed,
|
||||||
|
id: id, params: localParams, result: result))
|
||||||
|
.then((after) => after.result as Data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fires an `after` event. This will not be propagated to clients,
|
||||||
|
/// but will be broadcasted to WebSockets, etc.
|
||||||
|
Future<HookedServiceEvent<Id, Data, T>> fire(String eventName, result,
|
||||||
|
[HookedServiceEventListener<Id, Data, T>? callback]) {
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T> dispatcher;
|
||||||
|
|
||||||
|
switch (eventName) {
|
||||||
|
case HookedServiceEvent.indexed:
|
||||||
|
dispatcher = afterIndexed;
|
||||||
|
break;
|
||||||
|
case HookedServiceEvent.read:
|
||||||
|
dispatcher = afterRead;
|
||||||
|
break;
|
||||||
|
case HookedServiceEvent.created:
|
||||||
|
dispatcher = afterCreated;
|
||||||
|
break;
|
||||||
|
case HookedServiceEvent.modified:
|
||||||
|
dispatcher = afterModified;
|
||||||
|
break;
|
||||||
|
case HookedServiceEvent.updated:
|
||||||
|
dispatcher = afterUpdated;
|
||||||
|
break;
|
||||||
|
case HookedServiceEvent.removed:
|
||||||
|
dispatcher = afterRemoved;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw ArgumentError("Invalid service event name: '$eventName'");
|
||||||
|
}
|
||||||
|
|
||||||
|
var ev =
|
||||||
|
HookedServiceEvent<Id, Data, T>(true, null, null, inner, eventName);
|
||||||
|
return fireEvent(dispatcher, ev, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sends an arbitrary event down the hook chain.
|
||||||
|
Future<HookedServiceEvent<Id, Data, T>> fireEvent(
|
||||||
|
HookedServiceEventDispatcher<Id, Data, T> dispatcher,
|
||||||
|
HookedServiceEvent<Id, Data, T> event,
|
||||||
|
[HookedServiceEventListener<Id, Data, T>? callback]) {
|
||||||
|
Future? f;
|
||||||
|
if (callback != null && event._canceled != true) {
|
||||||
|
f = Future.sync(() => callback(event));
|
||||||
|
}
|
||||||
|
f ??= Future.value();
|
||||||
|
return f.then((_) => dispatcher._emit(event));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fired when a hooked service is invoked.
|
||||||
|
class HookedServiceEvent<Id, Data, T extends Service<Id, Data>> {
|
||||||
|
static const String indexed = 'indexed';
|
||||||
|
static const String read = 'read';
|
||||||
|
static const String created = 'created';
|
||||||
|
static const String modified = 'modified';
|
||||||
|
static const String updated = 'updated';
|
||||||
|
static const String removed = 'removed';
|
||||||
|
|
||||||
|
static const List<String> all = [
|
||||||
|
indexed,
|
||||||
|
read,
|
||||||
|
created,
|
||||||
|
modified,
|
||||||
|
updated,
|
||||||
|
removed
|
||||||
|
];
|
||||||
|
|
||||||
|
/// Use this to end processing of an event.
|
||||||
|
void cancel([result]) {
|
||||||
|
_canceled = true;
|
||||||
|
this.result = result ?? this.result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resolves a service from the application.
|
||||||
|
///
|
||||||
|
/// Shorthand for `e.service.app.service(...)`.
|
||||||
|
Service? getService(Pattern path) => service.app.findService(path);
|
||||||
|
|
||||||
|
bool _canceled = false;
|
||||||
|
final String _eventName;
|
||||||
|
Id? _id;
|
||||||
|
final bool _isAfter;
|
||||||
|
Data? data;
|
||||||
|
Map<String, dynamic>? _params;
|
||||||
|
final RequestContext? _request;
|
||||||
|
final ResponseContext? _response;
|
||||||
|
dynamic result;
|
||||||
|
|
||||||
|
String get eventName => _eventName;
|
||||||
|
|
||||||
|
Id? get id => _id;
|
||||||
|
|
||||||
|
bool get isAfter => _isAfter == true;
|
||||||
|
|
||||||
|
bool get isBefore => !isAfter;
|
||||||
|
|
||||||
|
Map get params => _params ?? {};
|
||||||
|
|
||||||
|
RequestContext? get request => _request;
|
||||||
|
|
||||||
|
ResponseContext? get response => _response;
|
||||||
|
|
||||||
|
/// The inner service whose method was hooked.
|
||||||
|
T service;
|
||||||
|
|
||||||
|
HookedServiceEvent(this._isAfter, this._request, this._response, this.service,
|
||||||
|
this._eventName,
|
||||||
|
{Id? id, this.data, Map<String, dynamic>? params, this.result}) {
|
||||||
|
//_data = data;
|
||||||
|
_id = id;
|
||||||
|
_params = params ?? {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Triggered on a hooked service event.
|
||||||
|
typedef HookedServiceEventListener<Id, Data, T extends Service<Id, Data>>
|
||||||
|
= FutureOr<dynamic> Function(HookedServiceEvent<Id, Data, T> event);
|
||||||
|
|
||||||
|
/// Can be listened to, but events may be canceled.
|
||||||
|
class HookedServiceEventDispatcher<Id, Data, T extends Service<Id, Data>> {
|
||||||
|
final List<StreamController<HookedServiceEvent<Id, Data, T>>> _ctrl = [];
|
||||||
|
final List<HookedServiceEventListener<Id, Data, T>> listeners = [];
|
||||||
|
|
||||||
|
void _close() {
|
||||||
|
for (var c in _ctrl) {
|
||||||
|
c.close();
|
||||||
|
}
|
||||||
|
listeners.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fires an event, and returns it once it is either canceled, or all listeners have run.
|
||||||
|
Future<HookedServiceEvent<Id, Data, T>> _emit(
|
||||||
|
HookedServiceEvent<Id, Data, T> event) {
|
||||||
|
if (event._canceled == true || listeners.isEmpty) {
|
||||||
|
return Future.value(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
var f = Future<HookedServiceEvent<Id, Data, T>>.value(event);
|
||||||
|
|
||||||
|
for (var listener in listeners) {
|
||||||
|
f = f.then((event) {
|
||||||
|
if (event._canceled) return event;
|
||||||
|
return Future.sync(() => listener(event)).then((_) => event);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a [Stream] containing all events fired by this dispatcher.
|
||||||
|
///
|
||||||
|
/// *NOTE*: Callbacks on the returned [Stream] cannot be guaranteed to run before other [listeners].
|
||||||
|
/// Use this only if you need a read-only stream of events.
|
||||||
|
Stream<HookedServiceEvent<Id, Data, T>> asStream() {
|
||||||
|
var ctrl = StreamController<HookedServiceEvent<Id, Data, T>>();
|
||||||
|
_ctrl.add(ctrl);
|
||||||
|
listen(ctrl.add);
|
||||||
|
return ctrl.stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Registers the listener to be called whenever an event is triggered.
|
||||||
|
void listen(HookedServiceEventListener<Id, Data, T> listener) {
|
||||||
|
listeners.add(listener);
|
||||||
|
}
|
||||||
|
}
|
81
packages/framework/lib/src/core/hostname_parser.dart
Normal file
81
packages/framework/lib/src/core/hostname_parser.dart
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
import 'dart:collection';
|
||||||
|
import 'package:string_scanner/string_scanner.dart';
|
||||||
|
|
||||||
|
/// Parses a string into a [RegExp] that is matched against hostnames.
|
||||||
|
class HostnameSyntaxParser {
|
||||||
|
final SpanScanner _scanner;
|
||||||
|
final _safe = RegExp(r'[0-9a-zA-Z-_:]+');
|
||||||
|
|
||||||
|
HostnameSyntaxParser(String hostname)
|
||||||
|
: _scanner = SpanScanner(hostname, sourceUrl: hostname);
|
||||||
|
|
||||||
|
FormatException _formatExc(String message) {
|
||||||
|
var span = _scanner.lastSpan ?? _scanner.emptySpan;
|
||||||
|
return FormatException(
|
||||||
|
'${span.start.toolString}: $message\n${span.highlight(color: true)}');
|
||||||
|
}
|
||||||
|
|
||||||
|
RegExp parse() {
|
||||||
|
var b = StringBuffer();
|
||||||
|
var parts = Queue<String>();
|
||||||
|
|
||||||
|
while (!_scanner.isDone) {
|
||||||
|
if (_scanner.scan('|')) {
|
||||||
|
if (parts.isEmpty) {
|
||||||
|
throw _formatExc('No hostname parts found before "|".');
|
||||||
|
} else {
|
||||||
|
var next = _parseHostnamePart();
|
||||||
|
if (next.isEmpty) {
|
||||||
|
throw _formatExc('No hostname parts found after "|".');
|
||||||
|
} else {
|
||||||
|
var prev = parts.removeLast();
|
||||||
|
parts.addLast('(($prev)|($next))');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var part = _parseHostnamePart();
|
||||||
|
if (part.isNotEmpty) {
|
||||||
|
if (_scanner.scan('.')) {
|
||||||
|
var subPart = _parseHostnamePart(shouldThrow: false);
|
||||||
|
while (subPart.isNotEmpty) {
|
||||||
|
part += '\\.$subPart';
|
||||||
|
if (_scanner.scan('.')) {
|
||||||
|
subPart = _parseHostnamePart(shouldThrow: false);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parts.add(part);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (parts.isNotEmpty) {
|
||||||
|
b.write(parts.removeFirst());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (b.isEmpty) {
|
||||||
|
throw _formatExc('Invalid or empty hostname.');
|
||||||
|
} else {
|
||||||
|
return RegExp('^$b\$', caseSensitive: false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String _parseHostnamePart({bool shouldThrow = true}) {
|
||||||
|
if (_scanner.scan('*.')) {
|
||||||
|
return r'([^$.]+\.)?';
|
||||||
|
} else if (_scanner.scan('*')) {
|
||||||
|
return r'[^$]*';
|
||||||
|
} else if (_scanner.scan('+')) {
|
||||||
|
return r'[^$]+';
|
||||||
|
} else if (_scanner.scan(_safe)) {
|
||||||
|
return _scanner.lastMatch?[0] ?? '';
|
||||||
|
} else if (!_scanner.isDone && shouldThrow) {
|
||||||
|
var s = String.fromCharCode(_scanner.peekChar()!);
|
||||||
|
throw _formatExc('Unexpected character "$s".');
|
||||||
|
} else {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
117
packages/framework/lib/src/core/hostname_router.dart
Normal file
117
packages/framework/lib/src/core/hostname_router.dart
Normal file
|
@ -0,0 +1,117 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:platform_container/platform_container.dart';
|
||||||
|
import 'package:platform_route/platform_route.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'env.dart';
|
||||||
|
import 'hostname_parser.dart';
|
||||||
|
import 'request_context.dart';
|
||||||
|
import 'response_context.dart';
|
||||||
|
import 'routable.dart';
|
||||||
|
import 'server.dart';
|
||||||
|
|
||||||
|
/// A utility that allows requests to be handled based on their
|
||||||
|
/// origin's hostname.
|
||||||
|
///
|
||||||
|
/// For example, an application could handle example.com and api.example.com
|
||||||
|
/// separately.
|
||||||
|
///
|
||||||
|
/// The provided patterns can be any `Pattern`. If a `String` is provided, a simple
|
||||||
|
/// grammar (see [HostnameSyntaxParser]) is used to create [RegExp].
|
||||||
|
///
|
||||||
|
/// For example:
|
||||||
|
/// * `example.com` -> `/example\.com/`
|
||||||
|
/// * `*.example.com` -> `/([^$.]\.)?example\.com/`
|
||||||
|
/// * `example.*` -> `/example\./[^$]*`
|
||||||
|
/// * `example.+` -> `/example\./[^$]+`
|
||||||
|
class HostnameRouter {
|
||||||
|
final Map<Pattern, Angel> _apps = {};
|
||||||
|
final Map<Pattern, FutureOr<Angel> Function()> _creators = {};
|
||||||
|
final List<Pattern> _patterns = [];
|
||||||
|
|
||||||
|
HostnameRouter(
|
||||||
|
{Map<Pattern, Angel> apps = const {},
|
||||||
|
Map<Pattern, FutureOr<Angel> Function()> creators = const {}}) {
|
||||||
|
Map<Pattern, V> parseMap<V>(Map<Pattern, V> map) {
|
||||||
|
return map.map((p, c) {
|
||||||
|
Pattern pp;
|
||||||
|
|
||||||
|
if (p is String) {
|
||||||
|
pp = HostnameSyntaxParser(p).parse();
|
||||||
|
} else {
|
||||||
|
pp = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
return MapEntry(pp, c);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
apps = parseMap(apps);
|
||||||
|
creators = parseMap(creators);
|
||||||
|
var patterns = apps.keys.followedBy(creators.keys).toSet().toList();
|
||||||
|
_apps.addAll(apps);
|
||||||
|
_creators.addAll(creators);
|
||||||
|
_patterns.addAll(patterns);
|
||||||
|
// print(_creators);
|
||||||
|
}
|
||||||
|
|
||||||
|
factory HostnameRouter.configure(
|
||||||
|
Map<Pattern, FutureOr<void> Function(Angel)> configurers,
|
||||||
|
{Reflector reflector = const EmptyReflector(),
|
||||||
|
AngelEnvironment environment = angelEnv,
|
||||||
|
Logger? logger,
|
||||||
|
bool allowMethodOverrides = true,
|
||||||
|
FutureOr<String> Function(dynamic)? serializer,
|
||||||
|
ViewGenerator? viewGenerator}) {
|
||||||
|
var creators = configurers.map((p, c) {
|
||||||
|
return MapEntry(p, () async {
|
||||||
|
var app = Angel(
|
||||||
|
reflector: reflector,
|
||||||
|
environment: environment,
|
||||||
|
logger: logger,
|
||||||
|
allowMethodOverrides: allowMethodOverrides,
|
||||||
|
serializer: serializer,
|
||||||
|
viewGenerator: viewGenerator);
|
||||||
|
await app.configure(c);
|
||||||
|
return app;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return HostnameRouter(creators: creators);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempts to handle a request, according to its hostname.
|
||||||
|
///
|
||||||
|
/// If none is matched, then `true` is returned.
|
||||||
|
/// Also returns `true` if all of the sub-app's handlers returned
|
||||||
|
/// `true`.
|
||||||
|
Future<bool> handleRequest(RequestContext req, ResponseContext res) async {
|
||||||
|
for (var pattern in _patterns) {
|
||||||
|
// print('${req.hostname} vs $_creators');
|
||||||
|
if (pattern.allMatches(req.hostname).isNotEmpty) {
|
||||||
|
// Resolve the entire pipeline within the context of the selected app.
|
||||||
|
var app = _apps[pattern] ??= (await _creators[pattern]!());
|
||||||
|
// print('App for ${req.hostname} = $app from $pattern');
|
||||||
|
// app.dumpTree();
|
||||||
|
|
||||||
|
var r = app.optimizedRouter;
|
||||||
|
var resolved = r.resolveAbsolute(req.path, method: req.method);
|
||||||
|
var pipeline = MiddlewarePipeline<RequestHandler>(resolved);
|
||||||
|
// print('Pipeline: $pipeline');
|
||||||
|
for (var handler in pipeline.handlers) {
|
||||||
|
// print(handler);
|
||||||
|
// Avoid stack overflow.
|
||||||
|
if (handler == handleRequest) {
|
||||||
|
continue;
|
||||||
|
} else if (!await app.executeHandler(handler, req, res)) {
|
||||||
|
// print('$handler TERMINATED');
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
// print('$handler CONTINUED');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, return true.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
208
packages/framework/lib/src/core/injection.dart
Normal file
208
packages/framework/lib/src/core/injection.dart
Normal file
|
@ -0,0 +1,208 @@
|
||||||
|
part of 'request_context.dart';
|
||||||
|
|
||||||
|
const List<Type> _primitiveTypes = [String, int, num, double, Null];
|
||||||
|
|
||||||
|
/// Shortcut for calling [preInject], and then [handleContained].
|
||||||
|
///
|
||||||
|
/// Use this to instantly create a request handler for a DI-enabled method.
|
||||||
|
///
|
||||||
|
/// Calling [ioc] also auto-serializes the result of a [handler].
|
||||||
|
RequestHandler ioc(Function handler, {Iterable<String> optional = const []}) {
|
||||||
|
return (req, res) {
|
||||||
|
RequestHandler? contained;
|
||||||
|
|
||||||
|
if (req.app?.container != null) {
|
||||||
|
var injection = preInject(handler, req.app!.container.reflector);
|
||||||
|
//if (injection != null) {
|
||||||
|
injection.optional.addAll(optional);
|
||||||
|
contained = handleContained(handler, injection);
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
return req.app!.executeHandler(contained, req, res);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Future resolveInjection(requirement, InjectionRequest injection,
|
||||||
|
RequestContext req, ResponseContext res, bool throwOnUnresolved,
|
||||||
|
[Container? container]) async {
|
||||||
|
dynamic propFromApp;
|
||||||
|
container ??= req.container ?? res.app!.container;
|
||||||
|
|
||||||
|
if (requirement == RequestContext) {
|
||||||
|
return req;
|
||||||
|
} else if (requirement == ResponseContext) {
|
||||||
|
return res;
|
||||||
|
} else if (requirement is String &&
|
||||||
|
injection.parameters.containsKey(requirement)) {
|
||||||
|
var param = injection.parameters[requirement]!;
|
||||||
|
var value = param.getValue(req);
|
||||||
|
if (value == null && param.required != false) throw param.error as Object;
|
||||||
|
return value;
|
||||||
|
} else if (requirement is String) {
|
||||||
|
if (req.container!.hasNamed(requirement)) {
|
||||||
|
return req.container!.findByName(requirement);
|
||||||
|
}
|
||||||
|
if (req.params.containsKey(requirement)) {
|
||||||
|
return req.params[requirement];
|
||||||
|
} else if ((propFromApp = req.app!.findProperty(requirement)) != null) {
|
||||||
|
return propFromApp;
|
||||||
|
} else if (injection.optional.contains(requirement)) {
|
||||||
|
return null;
|
||||||
|
} else if (throwOnUnresolved) {
|
||||||
|
throw ArgumentError(
|
||||||
|
"Cannot resolve parameter '$requirement' within handler.");
|
||||||
|
}
|
||||||
|
} else if (requirement is List &&
|
||||||
|
requirement.length == 2 &&
|
||||||
|
requirement.first is String &&
|
||||||
|
requirement.last is Type) {
|
||||||
|
var key = requirement.first;
|
||||||
|
var type = requirement.last;
|
||||||
|
if (req.params.containsKey(key) ||
|
||||||
|
req.app!.configuration.containsKey(key) ||
|
||||||
|
_primitiveTypes.contains(type)) {
|
||||||
|
return await resolveInjection(
|
||||||
|
key, injection, req, res, throwOnUnresolved, container);
|
||||||
|
} else {
|
||||||
|
return await resolveInjection(
|
||||||
|
type, injection, req, res, throwOnUnresolved, container);
|
||||||
|
}
|
||||||
|
} else if (requirement is Type && requirement != dynamic) {
|
||||||
|
try {
|
||||||
|
var futureType = container.reflector.reflectFutureOf(requirement);
|
||||||
|
if (container.has(futureType.reflectedType)) {
|
||||||
|
return await container.make(futureType.reflectedType);
|
||||||
|
}
|
||||||
|
} on UnsupportedError {
|
||||||
|
// Ignore.
|
||||||
|
}
|
||||||
|
|
||||||
|
return await container.make(requirement);
|
||||||
|
} else if (throwOnUnresolved) {
|
||||||
|
throw ArgumentError(
|
||||||
|
'$requirement cannot be injected into a request handler.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if an [InjectionRequest] can be sufficiently executed within the current request/response context.
|
||||||
|
bool suitableForInjection(
|
||||||
|
RequestContext req, ResponseContext res, InjectionRequest injection) {
|
||||||
|
return injection.parameters.values.any((p) {
|
||||||
|
if (p.match == null) return false;
|
||||||
|
var value = p.getValue(req);
|
||||||
|
return value == p.match;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handles a request with a DI-enabled handler.
|
||||||
|
RequestHandler handleContained(Function handler, InjectionRequest injection,
|
||||||
|
[Container? container]) {
|
||||||
|
return (RequestContext req, ResponseContext res) async {
|
||||||
|
if (injection.parameters.isNotEmpty &&
|
||||||
|
injection.parameters.values.any((p) => p.match != null) &&
|
||||||
|
!suitableForInjection(req, res, injection)) {
|
||||||
|
return Future.value(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
var args = [];
|
||||||
|
|
||||||
|
var named = <Symbol, dynamic>{};
|
||||||
|
|
||||||
|
for (var r in injection.required) {
|
||||||
|
args.add(await resolveInjection(r, injection, req, res, true, container));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var entry in injection.named.entries) {
|
||||||
|
var name = Symbol(entry.key);
|
||||||
|
named[name] = await resolveInjection(
|
||||||
|
[entry.key, entry.value], injection, req, res, false, container);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Function.apply(handler, args, named);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Contains a list of the data required for a DI-enabled method to run.
|
||||||
|
///
|
||||||
|
/// This improves performance by removing the necessity to reflect a method
|
||||||
|
/// every time it is requested.
|
||||||
|
///
|
||||||
|
/// Regular request handlers can also skip DI entirely, lowering response time
|
||||||
|
/// and memory use.
|
||||||
|
class InjectionRequest {
|
||||||
|
/// Optional, typed data that can be passed to a DI-enabled method.
|
||||||
|
final Map<String, Type> named;
|
||||||
|
|
||||||
|
/// A list of the arguments required for a DI-enabled method to run.
|
||||||
|
final List required;
|
||||||
|
|
||||||
|
/// A list of the arguments that can be null in a DI-enabled method.
|
||||||
|
final List<String> optional;
|
||||||
|
|
||||||
|
/// Extended parameter definitions.
|
||||||
|
final Map<String, Parameter> parameters;
|
||||||
|
|
||||||
|
const InjectionRequest.constant(
|
||||||
|
{this.named = const {},
|
||||||
|
this.required = const [],
|
||||||
|
this.optional = const [],
|
||||||
|
this.parameters = const {}});
|
||||||
|
|
||||||
|
InjectionRequest()
|
||||||
|
: named = {},
|
||||||
|
required = [],
|
||||||
|
optional = [],
|
||||||
|
parameters = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Predetermines what needs to be injected for a handler to run.
|
||||||
|
InjectionRequest preInject(Function handler, Reflector reflector) {
|
||||||
|
var injection = InjectionRequest();
|
||||||
|
|
||||||
|
var closureMirror = reflector.reflectFunction(handler)!;
|
||||||
|
|
||||||
|
if (closureMirror.parameters.isEmpty) return injection;
|
||||||
|
|
||||||
|
// Load parameters
|
||||||
|
for (var parameter in closureMirror.parameters) {
|
||||||
|
var name = parameter.name;
|
||||||
|
var type = parameter.type.reflectedType;
|
||||||
|
|
||||||
|
var p = parameter.annotations
|
||||||
|
.firstWhereOrNull(
|
||||||
|
(m) => m.type.isAssignableTo(reflector.reflectType(Parameter)))
|
||||||
|
?.reflectee as Parameter?;
|
||||||
|
//print(p);
|
||||||
|
if (p != null) {
|
||||||
|
injection.parameters[name] = Parameter(
|
||||||
|
cookie: p.cookie,
|
||||||
|
header: p.header,
|
||||||
|
query: p.query,
|
||||||
|
session: p.session,
|
||||||
|
match: p.match,
|
||||||
|
defaultValue: p.defaultValue,
|
||||||
|
required: parameter.isNamed ? false : p.required != false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!parameter.isNamed) {
|
||||||
|
if (!parameter.isRequired) injection.optional.add(name);
|
||||||
|
|
||||||
|
if (type == RequestContext || type == ResponseContext) {
|
||||||
|
injection.required.add(type);
|
||||||
|
} else if (name == 'req') {
|
||||||
|
injection.required.add(RequestContext);
|
||||||
|
} else if (name == 'res') {
|
||||||
|
injection.required.add(ResponseContext);
|
||||||
|
} else if (type == dynamic) {
|
||||||
|
injection.required.add(name);
|
||||||
|
} else {
|
||||||
|
injection.required.add([name, type]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
injection.named[name] = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return injection;
|
||||||
|
}
|
170
packages/framework/lib/src/core/map_service.dart
Normal file
170
packages/framework/lib/src/core/map_service.dart
Normal file
|
@ -0,0 +1,170 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:platform_exceptions/http_exception.dart';
|
||||||
|
|
||||||
|
import 'service.dart';
|
||||||
|
|
||||||
|
/// A basic service that manages an in-memory list of maps.
|
||||||
|
class MapService extends Service<String?, Map<String, dynamic>> {
|
||||||
|
/// If set to `true`, clients can remove all items by passing a `null` `id` to `remove`.
|
||||||
|
///
|
||||||
|
/// `false` by default.
|
||||||
|
final bool allowRemoveAll;
|
||||||
|
|
||||||
|
/// If set to `true`, parameters in `req.query` are applied to the database query.
|
||||||
|
final bool allowQuery;
|
||||||
|
|
||||||
|
/// If set to `true` (default), then the service will manage an `id` string and `createdAt` and `updatedAt` fields.
|
||||||
|
final bool autoIdAndDateFields;
|
||||||
|
|
||||||
|
/// If set to `true` (default), then the keys `created_at` and `updated_at` will automatically be snake_cased.
|
||||||
|
final bool autoSnakeCaseNames;
|
||||||
|
|
||||||
|
final List<Map<String, dynamic>> items = [];
|
||||||
|
|
||||||
|
MapService(
|
||||||
|
{this.allowRemoveAll = false,
|
||||||
|
this.allowQuery = true,
|
||||||
|
this.autoIdAndDateFields = true,
|
||||||
|
this.autoSnakeCaseNames = true})
|
||||||
|
: super();
|
||||||
|
|
||||||
|
String get createdAtKey =>
|
||||||
|
autoSnakeCaseNames == false ? 'createdAt' : 'created_at';
|
||||||
|
|
||||||
|
String get updatedAtKey =>
|
||||||
|
autoSnakeCaseNames == false ? 'updatedAt' : 'updated_at';
|
||||||
|
|
||||||
|
bool Function(Map<String, dynamic>) _matchesId(id) {
|
||||||
|
return (Map<String, dynamic> item) {
|
||||||
|
if (item['id'] == null) {
|
||||||
|
return false;
|
||||||
|
} else if (autoIdAndDateFields != false) {
|
||||||
|
return item['id'] == id?.toString();
|
||||||
|
} else {
|
||||||
|
return item['id'] == id;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<List<Map<String, dynamic>>> index([Map<String, dynamic>? params]) {
|
||||||
|
if (allowQuery == false || params == null || params['query'] is! Map) {
|
||||||
|
return Future.value(items);
|
||||||
|
} else {
|
||||||
|
var query = params['query'] as Map?;
|
||||||
|
|
||||||
|
return Future.value(items.where((item) {
|
||||||
|
for (var key in query!.keys) {
|
||||||
|
if (!item.containsKey(key)) {
|
||||||
|
return false;
|
||||||
|
} else if (item[key] != query[key]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}).toList());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Map<String, dynamic>> read(String? id,
|
||||||
|
[Map<String, dynamic>? params]) {
|
||||||
|
return Future.value(items.firstWhere(_matchesId(id),
|
||||||
|
orElse: (() => throw HttpException.notFound(
|
||||||
|
message: 'No record found for ID $id'))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Map<String, dynamic>> create(Map<String, dynamic> data,
|
||||||
|
[Map<String, dynamic>? params]) {
|
||||||
|
var now = DateTime.now().toIso8601String();
|
||||||
|
var result = Map<String, dynamic>.from(data);
|
||||||
|
|
||||||
|
if (autoIdAndDateFields == true) {
|
||||||
|
result
|
||||||
|
..['id'] = items.length.toString()
|
||||||
|
..[autoSnakeCaseNames == false ? 'createdAt' : 'created_at'] = now
|
||||||
|
..[autoSnakeCaseNames == false ? 'updatedAt' : 'updated_at'] = now;
|
||||||
|
}
|
||||||
|
items.add(result);
|
||||||
|
return Future.value(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Map<String, dynamic>> modify(String? id, Map<String, dynamic> data,
|
||||||
|
[Map<String, dynamic>? params]) {
|
||||||
|
//if (data is! Map) {
|
||||||
|
// throw HttpException.badRequest(
|
||||||
|
// message:
|
||||||
|
// 'MapService does not support `modify` with ${data.runtimeType}.');
|
||||||
|
//}
|
||||||
|
if (!items.any(_matchesId(id))) return create(data, params);
|
||||||
|
|
||||||
|
return read(id).then((item) {
|
||||||
|
var idx = items.indexOf(item);
|
||||||
|
if (idx < 0) return create(data, params);
|
||||||
|
var result = Map<String, dynamic>.from(item)..addAll(data);
|
||||||
|
|
||||||
|
if (autoIdAndDateFields == true) {
|
||||||
|
result[autoSnakeCaseNames == false ? 'updatedAt' : 'updated_at'] =
|
||||||
|
DateTime.now().toIso8601String();
|
||||||
|
}
|
||||||
|
return Future.value(items[idx] = result);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Map<String, dynamic>> update(String? id, Map<String, dynamic> data,
|
||||||
|
[Map<String, dynamic>? params]) {
|
||||||
|
//if (data is! Map) {
|
||||||
|
// throw HttpException.badRequest(
|
||||||
|
// message:
|
||||||
|
// 'MapService does not support `update` with ${data.runtimeType}.');
|
||||||
|
//}
|
||||||
|
if (!items.any(_matchesId(id))) return create(data, params);
|
||||||
|
|
||||||
|
return read(id).then((old) {
|
||||||
|
if (!items.remove(old)) {
|
||||||
|
throw HttpException.notFound(message: 'No record found for ID $id');
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = Map<String, dynamic>.from(data);
|
||||||
|
if (autoIdAndDateFields == true) {
|
||||||
|
result
|
||||||
|
..['id'] = id?.toString()
|
||||||
|
..[autoSnakeCaseNames == false ? 'createdAt' : 'created_at'] =
|
||||||
|
old[autoSnakeCaseNames == false ? 'createdAt' : 'created_at']
|
||||||
|
..[autoSnakeCaseNames == false ? 'updatedAt' : 'updated_at'] =
|
||||||
|
DateTime.now().toIso8601String();
|
||||||
|
}
|
||||||
|
items.add(result);
|
||||||
|
return Future.value(result);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Map<String, dynamic>> remove(String? id,
|
||||||
|
[Map<String, dynamic>? params]) {
|
||||||
|
if (id == null || id == 'null') {
|
||||||
|
// Remove everything...
|
||||||
|
if (!(allowRemoveAll == true ||
|
||||||
|
params?.containsKey('provider') != true)) {
|
||||||
|
throw HttpException.forbidden(
|
||||||
|
message: 'Clients are not allowed to delete all items.');
|
||||||
|
} else {
|
||||||
|
items.clear();
|
||||||
|
return Future.value({});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return read(id, params).then((result) {
|
||||||
|
if (items.remove(result)) {
|
||||||
|
return result;
|
||||||
|
} else {
|
||||||
|
throw HttpException.notFound(message: 'No record found for ID $id');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
166
packages/framework/lib/src/core/metadata.dart
Normal file
166
packages/framework/lib/src/core/metadata.dart
Normal file
|
@ -0,0 +1,166 @@
|
||||||
|
library angel_framework.http.metadata;
|
||||||
|
|
||||||
|
import 'package:platform_exceptions/http_exception.dart';
|
||||||
|
|
||||||
|
import 'hooked_service.dart' show HookedServiceEventListener;
|
||||||
|
import 'request_context.dart';
|
||||||
|
import 'routable.dart';
|
||||||
|
|
||||||
|
/// Annotation to map middleware onto a handler.
|
||||||
|
class Middleware {
|
||||||
|
final Iterable<RequestHandler> handlers;
|
||||||
|
|
||||||
|
const Middleware(this.handlers);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attaches hooks to a [HookedService].
|
||||||
|
class Hooks {
|
||||||
|
final List<HookedServiceEventListener> before;
|
||||||
|
final List<HookedServiceEventListener> after;
|
||||||
|
|
||||||
|
const Hooks({this.before = const [], this.after = const []});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Specifies to NOT expose a method to the Internet.
|
||||||
|
class NoExpose {
|
||||||
|
const NoExpose();
|
||||||
|
}
|
||||||
|
|
||||||
|
const NoExpose noExpose = NoExpose();
|
||||||
|
|
||||||
|
/// Exposes a [Controller] or a [Controller] method to the Internet.
|
||||||
|
/// Example:
|
||||||
|
///
|
||||||
|
/// ```dart
|
||||||
|
/// @Expose('/elements')
|
||||||
|
/// class ElementController extends Controller {
|
||||||
|
///
|
||||||
|
/// @Expose('/')
|
||||||
|
/// List<Element> getList() => someComputationHere();
|
||||||
|
///
|
||||||
|
/// @Expose('/int:elementId')
|
||||||
|
/// getElement(int elementId) => someOtherComputation();
|
||||||
|
///
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
class Expose {
|
||||||
|
final String method;
|
||||||
|
final String path;
|
||||||
|
final Iterable<RequestHandler> middleware;
|
||||||
|
final String? as;
|
||||||
|
final List<String> allowNull;
|
||||||
|
|
||||||
|
static const Expose get = Expose('', method: 'GET'),
|
||||||
|
post = Expose('', method: 'POST'),
|
||||||
|
patch = Expose('', method: 'PATCH'),
|
||||||
|
put = Expose('', method: 'PUT'),
|
||||||
|
delete = Expose('', method: 'DELETE'),
|
||||||
|
head = Expose('', method: 'HEAD');
|
||||||
|
|
||||||
|
const Expose(this.path,
|
||||||
|
{this.method = 'GET',
|
||||||
|
this.middleware = const [],
|
||||||
|
this.as,
|
||||||
|
this.allowNull = const []});
|
||||||
|
|
||||||
|
const Expose.method(this.method,
|
||||||
|
{this.middleware = const [], this.as, this.allowNull = const []})
|
||||||
|
: path = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Used to apply special dependency injections or functionality to a function parameter.
|
||||||
|
class Parameter {
|
||||||
|
/// Inject the value of a request cookie.
|
||||||
|
final String? cookie;
|
||||||
|
|
||||||
|
/// Inject the value of a request header.
|
||||||
|
final String? header;
|
||||||
|
|
||||||
|
/// Inject the value of a key from the session.
|
||||||
|
final String? session;
|
||||||
|
|
||||||
|
/// Inject the value of a key from the query.
|
||||||
|
final String? query;
|
||||||
|
|
||||||
|
/// Only execute the handler if the value of this parameter matches the given value.
|
||||||
|
final dynamic match;
|
||||||
|
|
||||||
|
/// Specify a default value.
|
||||||
|
final dynamic defaultValue;
|
||||||
|
|
||||||
|
/// If `true` (default), then an error will be thrown if this parameter is not present.
|
||||||
|
final bool required;
|
||||||
|
|
||||||
|
const Parameter(
|
||||||
|
{this.cookie,
|
||||||
|
this.query,
|
||||||
|
this.header,
|
||||||
|
this.session,
|
||||||
|
this.match,
|
||||||
|
this.defaultValue,
|
||||||
|
this.required = true});
|
||||||
|
|
||||||
|
/// Returns an error that can be thrown when the parameter is not present.
|
||||||
|
Object? get error {
|
||||||
|
if (cookie?.isNotEmpty == true) {
|
||||||
|
return HttpException.badRequest(
|
||||||
|
message: 'Missing required cookie "$cookie".');
|
||||||
|
}
|
||||||
|
if (header?.isNotEmpty == true) {
|
||||||
|
return HttpException.badRequest(
|
||||||
|
message: 'Missing required header "$header".');
|
||||||
|
}
|
||||||
|
if (query?.isNotEmpty == true) {
|
||||||
|
return HttpException.badRequest(
|
||||||
|
message: 'Missing required query parameter "$query".');
|
||||||
|
}
|
||||||
|
if (session?.isNotEmpty == true) {
|
||||||
|
return StateError('Session does not contain required key "$session".');
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Obtains a value for this parameter from a [RequestContext].
|
||||||
|
dynamic getValue(RequestContext req) {
|
||||||
|
if (cookie?.isNotEmpty == true) {
|
||||||
|
return req.cookies.firstWhere((c) => c.name == cookie).value;
|
||||||
|
}
|
||||||
|
if (header?.isNotEmpty == true) {
|
||||||
|
return req.headers?.value(header ?? '') ?? defaultValue;
|
||||||
|
}
|
||||||
|
if (session?.isNotEmpty == true) {
|
||||||
|
return req.session?[session] ?? defaultValue;
|
||||||
|
}
|
||||||
|
if (query?.isNotEmpty == true) {
|
||||||
|
return req.uri?.queryParameters[query] ?? defaultValue;
|
||||||
|
}
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shortcut for declaring a request header [Parameter].
|
||||||
|
class Header extends Parameter {
|
||||||
|
const Header(String header, {super.match, super.defaultValue, super.required})
|
||||||
|
: super(header: header);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shortcut for declaring a request session [Parameter].
|
||||||
|
class Session extends Parameter {
|
||||||
|
const Session(String session,
|
||||||
|
{super.match, super.defaultValue, super.required})
|
||||||
|
: super(session: session);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shortcut for declaring a request query [Parameter].
|
||||||
|
class Query extends Parameter {
|
||||||
|
const Query(String query, {super.match, super.defaultValue, super.required})
|
||||||
|
: super(query: query);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shortcut for declaring a request cookie [Parameter].
|
||||||
|
class CookieValue extends Parameter {
|
||||||
|
const CookieValue(String cookie,
|
||||||
|
{super.match, super.defaultValue, super.required})
|
||||||
|
: super(cookie: cookie);
|
||||||
|
}
|
382
packages/framework/lib/src/core/request_context.dart
Normal file
382
packages/framework/lib/src/core/request_context.dart
Normal file
|
@ -0,0 +1,382 @@
|
||||||
|
library angel_framework.http.request_context;
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:typed_data' show BytesBuilder;
|
||||||
|
import 'dart:io'
|
||||||
|
show Cookie, HeaderValue, HttpHeaders, HttpSession, InternetAddress;
|
||||||
|
|
||||||
|
import 'package:platform_container/platform_container.dart';
|
||||||
|
import 'package:http_parser/http_parser.dart';
|
||||||
|
import 'package:belatuk_http_server/belatuk_http_server.dart';
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:mime/mime.dart';
|
||||||
|
import 'package:path/path.dart' as p;
|
||||||
|
import 'package:collection/collection.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
|
import 'metadata.dart';
|
||||||
|
import 'response_context.dart';
|
||||||
|
import 'routable.dart';
|
||||||
|
import 'server.dart' show Angel;
|
||||||
|
|
||||||
|
part 'injection.dart';
|
||||||
|
|
||||||
|
/// A convenience wrapper around an incoming [RawRequest].
|
||||||
|
abstract class RequestContext<RawRequest> {
|
||||||
|
/// Similar to [Angel.shutdownHooks], allows for logic to be executed
|
||||||
|
/// when a [RequestContext] is done being processed.
|
||||||
|
final _log = Logger('RequestContext');
|
||||||
|
|
||||||
|
final List<FutureOr<void> Function()> shutdownHooks = [];
|
||||||
|
|
||||||
|
String? _acceptHeaderCache, _extensionCache;
|
||||||
|
bool? _acceptsAllCache;
|
||||||
|
Map<String, dynamic>? _queryParameters;
|
||||||
|
Object? _bodyObject;
|
||||||
|
bool _hasParsedBody = false, _closed = false;
|
||||||
|
Map<String, dynamic> _bodyFields = {};
|
||||||
|
List _bodyList = [];
|
||||||
|
List<UploadedFile> _uploadedFiles = <UploadedFile>[];
|
||||||
|
MediaType _contentType = MediaType('text', 'plain');
|
||||||
|
|
||||||
|
/// The underlying [RawRequest] provided by the driver.
|
||||||
|
RawRequest get rawRequest;
|
||||||
|
|
||||||
|
/// Additional params to be passed to services.
|
||||||
|
final Map<String, dynamic> serviceParams = {};
|
||||||
|
|
||||||
|
/// The [Angel] instance that is responding to this request.
|
||||||
|
Angel? app;
|
||||||
|
|
||||||
|
/// Any cookies sent with this request.
|
||||||
|
List<Cookie> get cookies => <Cookie>[];
|
||||||
|
|
||||||
|
/// All HTTP headers sent with this request.
|
||||||
|
HttpHeaders? get headers;
|
||||||
|
|
||||||
|
/// The requested hostname.
|
||||||
|
String get hostname => 'localhost';
|
||||||
|
|
||||||
|
/// The IoC container that can be used to provide functionality to produce
|
||||||
|
/// objects of a given type.
|
||||||
|
///
|
||||||
|
/// This is a *child* of the container found in `app`.
|
||||||
|
Container? get container;
|
||||||
|
|
||||||
|
/// The user's IP.
|
||||||
|
String get ip => remoteAddress.address;
|
||||||
|
|
||||||
|
/// This request's HTTP method.
|
||||||
|
///
|
||||||
|
/// This may have been processed by an override. See [originalMethod] to get the real method.
|
||||||
|
String get method => 'GET';
|
||||||
|
|
||||||
|
/// The original HTTP verb sent to the server.
|
||||||
|
String get originalMethod => 'GET';
|
||||||
|
|
||||||
|
/// The content type of an incoming request.
|
||||||
|
MediaType get contentType {
|
||||||
|
if (headers?.contentType != null) {
|
||||||
|
try {
|
||||||
|
_contentType = MediaType.parse(headers!.contentType.toString());
|
||||||
|
} catch (e) {
|
||||||
|
_log.warning(
|
||||||
|
'Invalid media type [${headers!.contentType.toString()}]', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return _contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The URL parameters extracted from the request URI.
|
||||||
|
Map<String, dynamic> params = <String, dynamic>{};
|
||||||
|
|
||||||
|
/// The requested path.
|
||||||
|
String get path => '';
|
||||||
|
|
||||||
|
/// Is this an **XMLHttpRequest**?
|
||||||
|
bool get isXhr {
|
||||||
|
return headers?.value('X-Requested-With')?.trim().toLowerCase() ==
|
||||||
|
'xmlhttprequest';
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The remote address requesting this resource.
|
||||||
|
InternetAddress get remoteAddress;
|
||||||
|
|
||||||
|
/// The user's HTTP session.
|
||||||
|
HttpSession? get session;
|
||||||
|
|
||||||
|
/// The [Uri] instance representing the path this request is responding to.
|
||||||
|
Uri? get uri;
|
||||||
|
|
||||||
|
/// The [Stream] of incoming binary data sent from the client.
|
||||||
|
Stream<List<int>>? get body;
|
||||||
|
|
||||||
|
/// Returns `true` if [parseBody] has been called so far.
|
||||||
|
bool get hasParsedBody => _hasParsedBody;
|
||||||
|
|
||||||
|
/// Returns a *mutable* [Map] of the fields parsed from the request [body].
|
||||||
|
///
|
||||||
|
/// Note that [parseBody] must be called first.
|
||||||
|
Map<String, dynamic> get bodyAsMap {
|
||||||
|
if (!hasParsedBody) {
|
||||||
|
throw StateError('The request body has not been parsed yet.');
|
||||||
|
}
|
||||||
|
// else if (_bodyFields == null) {
|
||||||
|
// throw StateError('The request body, $_bodyObject, is not a Map.');
|
||||||
|
//}
|
||||||
|
|
||||||
|
return _bodyFields;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This setter allows you to explicitly set the request body **exactly once**.
|
||||||
|
///
|
||||||
|
/// Use this if the format of the body is not natively parsed by Angel.
|
||||||
|
set bodyAsMap(Map<String, dynamic>? value) => bodyAsObject = value;
|
||||||
|
|
||||||
|
/// Returns a *mutable* [List] parsed from the request [body].
|
||||||
|
///
|
||||||
|
/// Note that [parseBody] must be called first.
|
||||||
|
List? get bodyAsList {
|
||||||
|
if (!hasParsedBody) {
|
||||||
|
throw StateError('The request body has not been parsed yet.');
|
||||||
|
// TODO: Relook at this
|
||||||
|
//} else if (_bodyList == null) {
|
||||||
|
} else if (_bodyList.isEmpty) {
|
||||||
|
throw StateError('The request body, $_bodyObject, is not a List.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return _bodyList;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This setter allows you to explicitly set the request body **exactly once**.
|
||||||
|
///
|
||||||
|
/// Use this if the format of the body is not natively parsed by Angel.
|
||||||
|
set bodyAsList(List? value) => bodyAsObject = value;
|
||||||
|
|
||||||
|
/// Returns the parsed request body, whatever it may be (typically a [Map] or [List]).
|
||||||
|
///
|
||||||
|
/// Note that [parseBody] must be called first.
|
||||||
|
Object? get bodyAsObject {
|
||||||
|
if (!hasParsedBody) {
|
||||||
|
throw StateError('The request body has not been parsed yet.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return _bodyObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This setter allows you to explicitly set the request body **exactly once**.
|
||||||
|
///
|
||||||
|
/// Use this if the format of the body is not natively parsed by Angel.
|
||||||
|
set bodyAsObject(value) {
|
||||||
|
if (_bodyObject != null) {
|
||||||
|
throw StateError(
|
||||||
|
'The request body has already been parsed/set, and cannot be overwritten.');
|
||||||
|
} else {
|
||||||
|
if (value is List) _bodyList = value;
|
||||||
|
if (value is Map<String, dynamic>) _bodyFields = value;
|
||||||
|
_bodyObject = value;
|
||||||
|
_hasParsedBody = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a *mutable* map of the files parsed from the request [body].
|
||||||
|
///
|
||||||
|
/// Note that [parseBody] must be called first.
|
||||||
|
List<UploadedFile>? get uploadedFiles {
|
||||||
|
if (!hasParsedBody) {
|
||||||
|
throw StateError('The request body has not been parsed yet.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return _uploadedFiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a *mutable* map of the fields contained in the query.
|
||||||
|
Map<String, dynamic> get queryParameters => _queryParameters ??=
|
||||||
|
Map<String, dynamic>.from(uri?.queryParameters ?? {});
|
||||||
|
|
||||||
|
/// Returns the file extension of the requested path, if any.
|
||||||
|
///
|
||||||
|
/// Includes the leading `.`, if there is one.
|
||||||
|
String get extension => _extensionCache ??= p.extension(uri?.path ?? '');
|
||||||
|
|
||||||
|
/// Returns `true` if the client's `Accept` header indicates that the given [contentType] is considered a valid response.
|
||||||
|
///
|
||||||
|
/// You cannot provide a `null` [contentType].
|
||||||
|
/// If the `Accept` header's value is `*/*`, this method will always return `true`.
|
||||||
|
/// To ignore the wildcard (`*/*`), pass [strict] as `true`.
|
||||||
|
///
|
||||||
|
/// [contentType] can be either of the following:
|
||||||
|
/// * A [ContentType], in which case the `Accept` header will be compared against its `mimeType` property.
|
||||||
|
/// * Any other Dart value, in which case the `Accept` header will be compared against the result of a `toString()` call.
|
||||||
|
bool accepts(contentType, {bool strict = false}) {
|
||||||
|
var contentTypeString = contentType is MediaType
|
||||||
|
? contentType.mimeType
|
||||||
|
: contentType?.toString();
|
||||||
|
|
||||||
|
// Change to assert
|
||||||
|
if (contentTypeString == null) {
|
||||||
|
_log.severe('RequestContext.accepts is null');
|
||||||
|
throw ArgumentError(
|
||||||
|
'RequestContext.accepts expects the `contentType` parameter to NOT be null.');
|
||||||
|
}
|
||||||
|
|
||||||
|
_acceptHeaderCache ??= headers?.value('accept');
|
||||||
|
|
||||||
|
if (_acceptHeaderCache == null) {
|
||||||
|
return true;
|
||||||
|
} else if (strict != true && _acceptHeaderCache!.contains('*/*')) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return _acceptHeaderCache!.contains(contentTypeString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns as `true` if the client's `Accept` header indicates that it will accept any response content type.
|
||||||
|
bool get acceptsAll => _acceptsAllCache ??= accepts('*/*');
|
||||||
|
|
||||||
|
/// Shorthand for deserializing [bodyAsMap], using some transformer function [f].
|
||||||
|
Future<T> deserializeBody<T>(FutureOr<T> Function(Map?) f,
|
||||||
|
{Encoding encoding = utf8}) async {
|
||||||
|
await parseBody(encoding: encoding);
|
||||||
|
return await f(bodyAsMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shorthand for decoding [bodyAsMap], using some [codec].
|
||||||
|
Future<T> decodeBody<T>(Codec<T, Map?> codec, {Encoding encoding = utf8}) =>
|
||||||
|
deserializeBody(codec.decode, encoding: encoding);
|
||||||
|
|
||||||
|
/// Manually parses the request body, if it has not already been parsed.
|
||||||
|
Future<void> parseBody({Encoding encoding = utf8}) async {
|
||||||
|
//if (contentType == null) {
|
||||||
|
// throw FormatException('Missing "content-type" header.');
|
||||||
|
//}
|
||||||
|
|
||||||
|
if (!_hasParsedBody) {
|
||||||
|
_hasParsedBody = true;
|
||||||
|
|
||||||
|
var contentBody = body ?? Stream.empty();
|
||||||
|
|
||||||
|
if (contentType.type == 'application' && contentType.subtype == 'json') {
|
||||||
|
_uploadedFiles = [];
|
||||||
|
|
||||||
|
var parsed = (_bodyObject =
|
||||||
|
await encoding.decoder.bind(contentBody).join().then(json.decode));
|
||||||
|
|
||||||
|
if (parsed is Map) {
|
||||||
|
_bodyFields = Map<String, dynamic>.from(parsed);
|
||||||
|
} else if (parsed is List) {
|
||||||
|
_bodyList = parsed;
|
||||||
|
}
|
||||||
|
} else if (contentType.type == 'application' &&
|
||||||
|
contentType.subtype == 'x-www-form-urlencoded') {
|
||||||
|
_uploadedFiles = [];
|
||||||
|
var parsed = await encoding.decoder
|
||||||
|
.bind(contentBody)
|
||||||
|
.join()
|
||||||
|
.then((s) => Uri.splitQueryString(s, encoding: encoding));
|
||||||
|
_bodyFields = Map<String, dynamic>.from(parsed);
|
||||||
|
} else if (contentType.type == 'multipart' &&
|
||||||
|
contentType.subtype == 'form-data' &&
|
||||||
|
contentType.parameters.containsKey('boundary')) {
|
||||||
|
var boundary = contentType.parameters['boundary'] ?? '';
|
||||||
|
var transformer = MimeMultipartTransformer(boundary);
|
||||||
|
var parts = transformer.bind(contentBody).map((part) =>
|
||||||
|
HttpMultipartFormData.parse(part, defaultEncoding: encoding));
|
||||||
|
_bodyFields = {};
|
||||||
|
_uploadedFiles = [];
|
||||||
|
|
||||||
|
await for (var part in parts) {
|
||||||
|
if (part.isBinary) {
|
||||||
|
_uploadedFiles.add(UploadedFile(part));
|
||||||
|
} else if (part.isText &&
|
||||||
|
part.contentDisposition.parameters.containsKey('name')) {
|
||||||
|
// If there is no name, then don't parse it.
|
||||||
|
var key = part.contentDisposition.parameters['name'];
|
||||||
|
if (key != null) {
|
||||||
|
var value = await part.join();
|
||||||
|
_bodyFields[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
_bodyFields = {};
|
||||||
|
_uploadedFiles = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Disposes of all resources.
|
||||||
|
@mustCallSuper
|
||||||
|
Future<void> close() async {
|
||||||
|
if (!_closed) {
|
||||||
|
_closed = true;
|
||||||
|
_acceptsAllCache = null;
|
||||||
|
_acceptHeaderCache = null;
|
||||||
|
serviceParams.clear();
|
||||||
|
params.clear();
|
||||||
|
await Future.forEach(shutdownHooks, (dynamic hook) => hook());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads information about a binary chunk uploaded to the server.
|
||||||
|
class UploadedFile {
|
||||||
|
/// The underlying `form-data` item.
|
||||||
|
final HttpMultipartFormData formData;
|
||||||
|
final log = Logger('UploadedFile');
|
||||||
|
|
||||||
|
MediaType _contentType = MediaType('multipart', 'form-data');
|
||||||
|
|
||||||
|
UploadedFile(this.formData);
|
||||||
|
|
||||||
|
/// Returns the binary stream from [formData].
|
||||||
|
Stream<List<int>> get data => formData.cast<List<int>>();
|
||||||
|
|
||||||
|
/// The filename associated with the data on the user's system.
|
||||||
|
/// Returns [:null:] if not present.
|
||||||
|
String? get filename => formData.contentDisposition.parameters['filename'];
|
||||||
|
|
||||||
|
/// The name of the field associated with this data.
|
||||||
|
/// Returns [:null:] if not present.
|
||||||
|
String? get name => formData.contentDisposition.parameters['name'];
|
||||||
|
|
||||||
|
/// The parsed [:Content-Type:] header of the [:HttpMultipartFormData:].
|
||||||
|
/// Returns [:null:] if not present.
|
||||||
|
//MediaType get contentType => _contentType ??= (formData.contentType == null
|
||||||
|
// ? null
|
||||||
|
// : MediaType.parse(formData.contentType.toString()));
|
||||||
|
|
||||||
|
MediaType get contentType {
|
||||||
|
if (formData.contentType != null) {
|
||||||
|
try {
|
||||||
|
_contentType = MediaType.parse(formData.contentType.toString());
|
||||||
|
} catch (e) {
|
||||||
|
log.warning(
|
||||||
|
'Invalue media type [${formData.contentType.toString()}]', e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return _contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The parsed [:Content-Transfer-Encoding:] header of the
|
||||||
|
/// [:HttpMultipartFormData:]. This field is used to determine how to decode
|
||||||
|
/// the data. Returns [:null:] if not present.
|
||||||
|
HeaderValue? get contentTransferEncoding => formData.contentTransferEncoding;
|
||||||
|
|
||||||
|
/// Reads the contents of the file into a single linear buffer.
|
||||||
|
///
|
||||||
|
/// Note that this leads to holding the whole file in memory, which might
|
||||||
|
/// not be ideal for large files.w
|
||||||
|
Future<List<int>> readAsBytes() {
|
||||||
|
return data
|
||||||
|
.fold<BytesBuilder>(BytesBuilder(), (bb, out) => bb..add(out))
|
||||||
|
.then((bb) => bb.takeBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads the contents of the file as [String], using the given [encoding].
|
||||||
|
Future<String> readAsString({Encoding encoding = utf8}) {
|
||||||
|
return encoding.decoder.bind(data).join();
|
||||||
|
}
|
||||||
|
}
|
456
packages/framework/lib/src/core/response_context.dart
Normal file
456
packages/framework/lib/src/core/response_context.dart
Normal file
|
@ -0,0 +1,456 @@
|
||||||
|
library platform_framework.http.response_context;
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:convert' as c show json;
|
||||||
|
import 'dart:io' show BytesBuilder, Cookie;
|
||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:platform_route/platform_route.dart';
|
||||||
|
import 'package:file/file.dart';
|
||||||
|
import 'package:http_parser/http_parser.dart';
|
||||||
|
import 'package:mime/mime.dart';
|
||||||
|
|
||||||
|
import 'controller.dart';
|
||||||
|
import 'request_context.dart';
|
||||||
|
import 'server.dart' show Angel;
|
||||||
|
|
||||||
|
final RegExp _straySlashes = RegExp(r'(^/+)|(/+$)');
|
||||||
|
|
||||||
|
/// A convenience wrapper around an outgoing HTTP request.
|
||||||
|
abstract class ResponseContext<RawResponse>
|
||||||
|
implements StreamConsumer<List<int>>, StreamSink<List<int>>, StringSink {
|
||||||
|
final Map properties = {};
|
||||||
|
final CaseInsensitiveMap<String> _headers = CaseInsensitiveMap<String>.from(
|
||||||
|
{'content-type': 'text/plain', 'server': 'Angel3'});
|
||||||
|
|
||||||
|
//final log = Logger('ResponseContext');
|
||||||
|
|
||||||
|
Completer? _done;
|
||||||
|
int _statusCode = 200;
|
||||||
|
|
||||||
|
/// The [Angel] instance that is sending a response.
|
||||||
|
Angel? app;
|
||||||
|
|
||||||
|
/// Is `Transfer-Encoding` chunked?
|
||||||
|
bool? chunked;
|
||||||
|
|
||||||
|
/// Any and all cookies to be sent to the user.
|
||||||
|
final List<Cookie> cookies = [];
|
||||||
|
|
||||||
|
/// A set of [Converter] objects that can be used to encode response data.
|
||||||
|
///
|
||||||
|
/// At most one encoder will ever be used to convert data.
|
||||||
|
final Map<String, Converter<List<int>, List<int>>> encoders = {};
|
||||||
|
|
||||||
|
/// A [Map] of data to inject when `res.render` is called.
|
||||||
|
///
|
||||||
|
/// This can be used to reduce boilerplate when using templating engines.
|
||||||
|
final Map<String, dynamic> renderParams = {};
|
||||||
|
|
||||||
|
/// Points to the [RequestContext] corresponding to this response.
|
||||||
|
RequestContext? get correspondingRequest;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future get done => (_done ?? Completer()).future;
|
||||||
|
|
||||||
|
/// Headers that will be sent to the user.
|
||||||
|
///
|
||||||
|
/// Note that if you have already started writing to the underlying stream, headers will not persist.
|
||||||
|
CaseInsensitiveMap<String> get headers => _headers;
|
||||||
|
|
||||||
|
/// Serializes response data into a String.
|
||||||
|
///
|
||||||
|
/// The default is conversion into JSON via `json.encode`.
|
||||||
|
///
|
||||||
|
/// If you are 100% sure that your response handlers will only
|
||||||
|
/// be JSON-encodable objects (i.e. primitives, `List`s and `Map`s),
|
||||||
|
/// then consider setting [serializer] to `JSON.encode`.
|
||||||
|
///
|
||||||
|
/// To set it globally for the whole [app], use the following helper:
|
||||||
|
/// ```dart
|
||||||
|
/// app.injectSerializer(JSON.encode);
|
||||||
|
/// ```
|
||||||
|
FutureOr<String> Function(dynamic) serializer = c.json.encode;
|
||||||
|
|
||||||
|
/// This response's status code.
|
||||||
|
int get statusCode => _statusCode;
|
||||||
|
|
||||||
|
set statusCode(int value) {
|
||||||
|
if (!isOpen) {
|
||||||
|
throw closed();
|
||||||
|
} else {
|
||||||
|
_statusCode = value; // ?? 200;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if the response is still available for processing by Angel.
|
||||||
|
///
|
||||||
|
/// If it is `false`, then Angel will stop executing handlers, and will only run
|
||||||
|
/// response finalizers if the response [isBuffered].
|
||||||
|
bool get isOpen;
|
||||||
|
|
||||||
|
/// Returns `true` if response data is being written to a buffer, rather than to the underlying stream.
|
||||||
|
bool get isBuffered;
|
||||||
|
|
||||||
|
/// A set of UTF-8 encoded bytes that will be written to the response.
|
||||||
|
BytesBuilder? get buffer;
|
||||||
|
|
||||||
|
/// The underlying [RawResponse] under this instance.
|
||||||
|
RawResponse get rawResponse;
|
||||||
|
|
||||||
|
/// Signals Angel that the response is being held alive deliberately, and that the framework should not automatically close it.
|
||||||
|
///
|
||||||
|
/// This is mostly used in situations like WebSocket handlers, where the connection should remain
|
||||||
|
/// open indefinitely.
|
||||||
|
FutureOr<RawResponse> detach();
|
||||||
|
|
||||||
|
/// Gets or sets the content length to send back to a client.
|
||||||
|
///
|
||||||
|
/// Returns `null` if the header is invalidly formatted.
|
||||||
|
int? get contentLength {
|
||||||
|
return int.tryParse(headers['content-length'] ?? '-1');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets or sets the content length to send back to a client.
|
||||||
|
///
|
||||||
|
/// If [value] is `null`, then the header will be removed.
|
||||||
|
set contentLength(int? value) {
|
||||||
|
if (value == null || value == -1) {
|
||||||
|
headers.remove('content-length');
|
||||||
|
} else {
|
||||||
|
headers['content-length'] = value.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets or sets the content type to send back to a client.
|
||||||
|
MediaType get contentType {
|
||||||
|
try {
|
||||||
|
return MediaType.parse(headers['content-type']!);
|
||||||
|
} catch (_) {
|
||||||
|
return MediaType('text', 'plain');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets or sets the content type to send back to a client.
|
||||||
|
set contentType(MediaType value) {
|
||||||
|
headers['content-type'] = value.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
static StateError closed() => StateError('Cannot modify a closed response.');
|
||||||
|
|
||||||
|
/// Sends a download as a response.
|
||||||
|
Future<void> download(File file, {String? filename}) async {
|
||||||
|
if (!isOpen) throw closed();
|
||||||
|
|
||||||
|
headers['Content-Disposition'] =
|
||||||
|
'attachment; filename="${filename ?? file.path}"';
|
||||||
|
contentType = MediaType.parse(lookupMimeType(file.path)!);
|
||||||
|
headers['content-length'] = file.lengthSync().toString();
|
||||||
|
|
||||||
|
if (!isBuffered) {
|
||||||
|
await file.openRead().cast<List<int>>().pipe(this);
|
||||||
|
} else {
|
||||||
|
buffer!.add(file.readAsBytesSync());
|
||||||
|
await close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Prevents more data from being written to the response, and locks it entire from further editing.
|
||||||
|
@override
|
||||||
|
Future<void> close() {
|
||||||
|
if (buffer is LockableBytesBuilder) {
|
||||||
|
(buffer as LockableBytesBuilder).lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_done?.isCompleted == false) _done!.complete();
|
||||||
|
return Future.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Serializes JSON to the response.
|
||||||
|
Future<bool> json(value) =>
|
||||||
|
serialize(value, contentType: MediaType('application', 'json'));
|
||||||
|
|
||||||
|
/// Returns a JSONP response.
|
||||||
|
///
|
||||||
|
/// You can override the [contentType] sent; by default it is `application/javascript`.
|
||||||
|
Future<void> jsonp(value,
|
||||||
|
{String callbackName = 'callback', MediaType? contentType}) {
|
||||||
|
if (!isOpen) throw closed();
|
||||||
|
this.contentType = contentType ?? MediaType('application', 'javascript');
|
||||||
|
write('$callbackName(${serializer(value)})');
|
||||||
|
return close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Renders a view to the response stream, and closes the response.
|
||||||
|
Future<void> render(String view, [Map<String, dynamic>? data]) {
|
||||||
|
if (!isOpen) throw closed();
|
||||||
|
contentType = MediaType('text', 'html', {'charset': 'utf-8'});
|
||||||
|
return Future<String>.sync(() => app!.viewGenerator!(
|
||||||
|
view,
|
||||||
|
Map<String, dynamic>.from(renderParams)
|
||||||
|
..addAll(data ?? <String, dynamic>{}))).then((content) {
|
||||||
|
write(content);
|
||||||
|
return close();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Redirects to user to the given URL.
|
||||||
|
///
|
||||||
|
/// [url] can be a `String`, or a `List`.
|
||||||
|
/// If it is a `List`, a URI will be constructed
|
||||||
|
/// based on the provided params.
|
||||||
|
///
|
||||||
|
/// See [Router]#navigate for more. :)
|
||||||
|
Future<void> redirect(url, {bool absolute = true, int? code}) {
|
||||||
|
if (!isOpen) throw closed();
|
||||||
|
headers
|
||||||
|
..['content-type'] = 'text/html'
|
||||||
|
..['location'] = (url is String || url is Uri)
|
||||||
|
? url.toString()
|
||||||
|
: app!.navigate(url as Iterable, absolute: absolute);
|
||||||
|
statusCode = code ?? 302;
|
||||||
|
write('''
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Redirecting...</title>
|
||||||
|
<meta http-equiv="refresh" content="0; url=$url">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Currently redirecting you...</h1>
|
||||||
|
<br />
|
||||||
|
Click <a href="$url">here</a> if you are not automatically redirected...
|
||||||
|
<script>
|
||||||
|
window.location = "$url";
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
''');
|
||||||
|
return close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Redirects to the given named [Route].
|
||||||
|
Future<void> redirectTo(String name, [Map? params, int? code]) async {
|
||||||
|
if (!isOpen) throw closed();
|
||||||
|
Route? findRoute(Router r) {
|
||||||
|
for (var route in r.routes) {
|
||||||
|
if (route is SymlinkRoute) {
|
||||||
|
final m = findRoute(route.router);
|
||||||
|
|
||||||
|
if (m != null) return m;
|
||||||
|
} else if (route.name == name) {
|
||||||
|
return route;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var matched = findRoute(app!);
|
||||||
|
|
||||||
|
if (matched != null) {
|
||||||
|
await redirect(
|
||||||
|
matched.makeUri(params!.keys.fold<Map<String, dynamic>>({}, (out, k) {
|
||||||
|
return out..[k.toString()] = params[k];
|
||||||
|
})),
|
||||||
|
code: code);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw ArgumentError.notNull('Route to redirect to ($name)');
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Redirects to the given [Controller] action.
|
||||||
|
Future<void> redirectToAction(String action, [Map? params, int? code]) {
|
||||||
|
if (!isOpen) throw closed();
|
||||||
|
// UserController@show
|
||||||
|
var split = action.split('@');
|
||||||
|
|
||||||
|
if (split.length < 2) {
|
||||||
|
throw Exception(
|
||||||
|
"Controller redirects must take the form of 'Controller@action'. You gave: $action");
|
||||||
|
}
|
||||||
|
|
||||||
|
var controller = app!.controllers[split[0].replaceAll(_straySlashes, '')];
|
||||||
|
|
||||||
|
if (controller == null) {
|
||||||
|
throw Exception("Could not find a controller named '${split[0]}'");
|
||||||
|
}
|
||||||
|
|
||||||
|
var matched = controller.routeMappings[split[1]];
|
||||||
|
|
||||||
|
if (matched == null) {
|
||||||
|
throw Exception(
|
||||||
|
"Controller '${split[0]}' does not contain any action named '${split[1]}'");
|
||||||
|
}
|
||||||
|
|
||||||
|
final head = controller
|
||||||
|
.findExpose(app!.container.reflector)!
|
||||||
|
.path
|
||||||
|
.toString()
|
||||||
|
.replaceAll(_straySlashes, '');
|
||||||
|
var tail = '';
|
||||||
|
if (params != null) {
|
||||||
|
tail = matched
|
||||||
|
.makeUri(params.keys.fold<Map<String, dynamic>>({}, (out, k) {
|
||||||
|
return out..[k.toString()] = params[k];
|
||||||
|
}))
|
||||||
|
.replaceAll(_straySlashes, '');
|
||||||
|
}
|
||||||
|
return redirect('$head/$tail'.replaceAll(_straySlashes, ''), code: code);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Serializes data to the response.
|
||||||
|
Future<bool> serialize(value, {MediaType? contentType}) async {
|
||||||
|
if (!isOpen) {
|
||||||
|
throw closed();
|
||||||
|
}
|
||||||
|
this.contentType = contentType ?? MediaType('application', 'json');
|
||||||
|
var text = await serializer(value);
|
||||||
|
if (text.isEmpty) return true;
|
||||||
|
write(text);
|
||||||
|
await close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Streams a file to this response.
|
||||||
|
///
|
||||||
|
/// `HEAD` responses will not actually write data.
|
||||||
|
Future streamFile(File file) async {
|
||||||
|
if (!isOpen) {
|
||||||
|
throw closed();
|
||||||
|
}
|
||||||
|
var mimeType = app!.mimeTypeResolver.lookup(file.path);
|
||||||
|
contentLength = await file.length();
|
||||||
|
contentType = mimeType == null
|
||||||
|
? MediaType('application', 'octet-stream')
|
||||||
|
: MediaType.parse(mimeType);
|
||||||
|
|
||||||
|
if (correspondingRequest!.method != 'HEAD') {
|
||||||
|
return addStream(file.openRead().cast<List<int>>()).then((_) => close());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Configure the response to write to an intermediate response buffer, rather than to the stream directly.
|
||||||
|
void useBuffer();
|
||||||
|
|
||||||
|
/// Adds a stream directly the underlying response.
|
||||||
|
///
|
||||||
|
/// If this instance has access to a [correspondingRequest], then it will attempt to transform
|
||||||
|
/// the content using at most one of the response [encoders].
|
||||||
|
@override
|
||||||
|
Future addStream(Stream<List<int>> stream);
|
||||||
|
|
||||||
|
@override
|
||||||
|
void addError(Object error, [StackTrace? stackTrace]) {
|
||||||
|
if (_done?.isCompleted == false) {
|
||||||
|
_done!.completeError(error, stackTrace);
|
||||||
|
} else if (_done == null) {
|
||||||
|
if (stackTrace != null) {
|
||||||
|
Zone.current.handleUncaughtError(error, stackTrace);
|
||||||
|
} else {
|
||||||
|
app?.logger.warning('[ResponseContext] stackTrace is null');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Writes data to the response.
|
||||||
|
@override
|
||||||
|
void write(value, {Encoding? encoding}) {
|
||||||
|
encoding ??= utf8;
|
||||||
|
|
||||||
|
if (!isOpen && isBuffered) {
|
||||||
|
throw closed();
|
||||||
|
} else if (!isBuffered) {
|
||||||
|
add(encoding.encode(value.toString()));
|
||||||
|
} else {
|
||||||
|
buffer!.add(encoding.encode(value.toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void writeCharCode(int charCode) {
|
||||||
|
if (!isOpen && isBuffered) {
|
||||||
|
throw closed();
|
||||||
|
} else if (!isBuffered) {
|
||||||
|
add([charCode]);
|
||||||
|
} else {
|
||||||
|
buffer!.addByte(charCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void writeln([Object? obj = '']) {
|
||||||
|
write(obj.toString());
|
||||||
|
write('\r\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void writeAll(Iterable objects, [String separator = '']) {
|
||||||
|
write(objects.join(separator));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract class LockableBytesBuilder implements BytesBuilder {
|
||||||
|
factory LockableBytesBuilder() {
|
||||||
|
return _LockableBytesBuilderImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
void lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _LockableBytesBuilderImpl implements LockableBytesBuilder {
|
||||||
|
final BytesBuilder _buf = BytesBuilder(copy: false);
|
||||||
|
bool _closed = false;
|
||||||
|
|
||||||
|
StateError _deny() =>
|
||||||
|
StateError('Cannot modified a closed response\'s buffer.');
|
||||||
|
|
||||||
|
@override
|
||||||
|
void lock() {
|
||||||
|
_closed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void add(List<int> bytes) {
|
||||||
|
if (_closed) {
|
||||||
|
throw _deny();
|
||||||
|
} else {
|
||||||
|
_buf.add(bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void addByte(int byte) {
|
||||||
|
if (_closed) {
|
||||||
|
throw _deny();
|
||||||
|
} else {
|
||||||
|
_buf.addByte(byte);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void clear() {
|
||||||
|
_buf.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool get isEmpty => _buf.isEmpty;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool get isNotEmpty => _buf.isNotEmpty;
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get length => _buf.length;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Uint8List takeBytes() {
|
||||||
|
return _buf.takeBytes();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Uint8List toBytes() {
|
||||||
|
return _buf.toBytes();
|
||||||
|
}
|
||||||
|
}
|
140
packages/framework/lib/src/core/routable.dart
Normal file
140
packages/framework/lib/src/core/routable.dart
Normal file
|
@ -0,0 +1,140 @@
|
||||||
|
library angel_framework.http.routable;
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:platform_container/platform_container.dart';
|
||||||
|
import 'package:platform_route/platform_route.dart';
|
||||||
|
|
||||||
|
import '../util.dart';
|
||||||
|
import 'hooked_service.dart';
|
||||||
|
import 'metadata.dart';
|
||||||
|
import 'request_context.dart';
|
||||||
|
import 'response_context.dart';
|
||||||
|
import 'service.dart';
|
||||||
|
|
||||||
|
final RegExp _straySlashes = RegExp(r'(^/+)|(/+$)');
|
||||||
|
|
||||||
|
/// A function that receives an incoming [RequestContext] and responds to it.
|
||||||
|
typedef RequestHandler = FutureOr<dynamic> Function(
|
||||||
|
RequestContext<dynamic> req, ResponseContext<dynamic> res);
|
||||||
|
|
||||||
|
/// Sequentially runs a list of [handlers] of middleware, and returns early if any does not
|
||||||
|
/// return `true`. Works well with [Router].chain.
|
||||||
|
RequestHandler chain(Iterable<RequestHandler> handlers) {
|
||||||
|
return (req, res) {
|
||||||
|
Future Function()? runPipeline;
|
||||||
|
|
||||||
|
for (var handler in handlers) {
|
||||||
|
//if (handler == null) break;
|
||||||
|
|
||||||
|
if (runPipeline == null) {
|
||||||
|
runPipeline = () => Future.sync(() => handler(req, res));
|
||||||
|
} else {
|
||||||
|
var current = runPipeline;
|
||||||
|
runPipeline = () => current().then((result) => !res.isOpen
|
||||||
|
? Future.value(result)
|
||||||
|
: req.app!.executeHandler(handler, req, res));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
runPipeline ??= () => Future.value();
|
||||||
|
return runPipeline();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A routable server that can handle dynamic requests.
|
||||||
|
class Routable extends Router<RequestHandler> {
|
||||||
|
final Map<Pattern, Service> _services = {};
|
||||||
|
final Map<Pattern, Service?> _serviceLookups = {};
|
||||||
|
|
||||||
|
/// A [Map] of application-specific data that can be accessed.
|
||||||
|
///
|
||||||
|
/// Packages like `package:angel3_configuration` populate this map
|
||||||
|
/// for you.
|
||||||
|
final Map configuration = {};
|
||||||
|
|
||||||
|
final Container _container;
|
||||||
|
|
||||||
|
Routable([Reflector? reflector])
|
||||||
|
// : _container = reflector == null ? null : Container(reflector),
|
||||||
|
: _container = Container(reflector ?? ThrowingReflector()),
|
||||||
|
super();
|
||||||
|
|
||||||
|
/// A [Container] used to inject dependencies.
|
||||||
|
Container get container => _container;
|
||||||
|
|
||||||
|
void close() {
|
||||||
|
_services.clear();
|
||||||
|
configuration.clear();
|
||||||
|
_onService.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A set of [Service] objects that have been mapped into routes.
|
||||||
|
Map<Pattern, Service> get services => _services;
|
||||||
|
|
||||||
|
final StreamController<Service> _onService =
|
||||||
|
StreamController<Service>.broadcast();
|
||||||
|
|
||||||
|
/// Fired whenever a service is added to this instance.
|
||||||
|
///
|
||||||
|
/// **NOTE**: This is a broadcast stream.
|
||||||
|
Stream<Service> get onService => _onService.stream;
|
||||||
|
|
||||||
|
/// Retrieves the service assigned to the given path.
|
||||||
|
T? findService<T extends Service>(Pattern path) {
|
||||||
|
return _serviceLookups.putIfAbsent(path, () {
|
||||||
|
return _services[path] ??
|
||||||
|
_services[path.toString().replaceAll(_straySlashes, '')];
|
||||||
|
}) as T?;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shorthand for finding a [Service] in a statically-typed manner.
|
||||||
|
Service<Id, Data>? findServiceOf<Id, Data>(Pattern path) {
|
||||||
|
return findService<Service<Id, Data>>(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shorthand for finding a [HookedService] in a statically-typed manner.
|
||||||
|
HookedService<dynamic, dynamic, T>? findHookedService<T extends Service>(
|
||||||
|
Pattern path) {
|
||||||
|
return findService(path) as HookedService<dynamic, dynamic, T>?;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Route<RequestHandler> addRoute(
|
||||||
|
String method, String path, RequestHandler handler,
|
||||||
|
{Iterable<RequestHandler> middleware = const {}}) {
|
||||||
|
final handlers = <RequestHandler>[];
|
||||||
|
// Merge @Middleware declaration, if any
|
||||||
|
var reflector = _container.reflector;
|
||||||
|
if (reflector is! ThrowingReflector) {
|
||||||
|
var middlewareDeclaration =
|
||||||
|
getAnnotation<Middleware>(handler, _container.reflector);
|
||||||
|
if (middlewareDeclaration != null) {
|
||||||
|
handlers.addAll(middlewareDeclaration.handlers);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final handlerSequence = <RequestHandler>[];
|
||||||
|
handlerSequence.addAll(middleware);
|
||||||
|
handlerSequence.addAll(handlers);
|
||||||
|
|
||||||
|
return super.addRoute(method, path.toString(), handler,
|
||||||
|
middleware: handlerSequence);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Mounts a [service] at the given [path].
|
||||||
|
///
|
||||||
|
/// Returns a [HookedService] that can be used to hook into
|
||||||
|
/// events dispatched by this service.
|
||||||
|
HookedService<Id, Data, T> use<Id, Data, T extends Service<Id, Data>>(
|
||||||
|
String path, T service) {
|
||||||
|
var hooked = HookedService<Id, Data, T>(service);
|
||||||
|
_services[path.toString().trim().replaceAll(RegExp(r'(^/+)|(/+$)'), '')] =
|
||||||
|
hooked;
|
||||||
|
hooked.addRoutes();
|
||||||
|
mount(path.toString(), hooked);
|
||||||
|
service.onHooked(hooked);
|
||||||
|
_onService.add(hooked);
|
||||||
|
return hooked;
|
||||||
|
}
|
||||||
|
}
|
423
packages/framework/lib/src/core/server.dart
Normal file
423
packages/framework/lib/src/core/server.dart
Normal file
|
@ -0,0 +1,423 @@
|
||||||
|
library platform_framework.http.server;
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:collection' show HashMap;
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'package:platform_container/platform_container.dart';
|
||||||
|
import 'package:platform_exceptions/http_exception.dart';
|
||||||
|
import 'package:platform_route/platform_route.dart';
|
||||||
|
import 'package:belatuk_combinator/belatuk_combinator.dart';
|
||||||
|
import 'package:http_parser/http_parser.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:mime/mime.dart';
|
||||||
|
import 'package:tuple/tuple.dart';
|
||||||
|
import 'controller.dart';
|
||||||
|
import 'env.dart';
|
||||||
|
import 'hooked_service.dart';
|
||||||
|
import 'request_context.dart';
|
||||||
|
import 'response_context.dart';
|
||||||
|
import 'routable.dart';
|
||||||
|
import 'service.dart';
|
||||||
|
|
||||||
|
//final RegExp _straySlashes = RegExp(r'(^/+)|(/+$)');
|
||||||
|
|
||||||
|
/// A function that configures an [Angel] server.
|
||||||
|
typedef AngelConfigurer = FutureOr<void> Function(Angel app);
|
||||||
|
|
||||||
|
/// A function that asynchronously generates a view from the given path and data.
|
||||||
|
typedef ViewGenerator = FutureOr<String> Function(String path,
|
||||||
|
[Map<String, dynamic>? data]);
|
||||||
|
|
||||||
|
/// A function that handles error
|
||||||
|
typedef AngelErrorHandler = dynamic Function(
|
||||||
|
HttpException e, RequestContext req, ResponseContext res);
|
||||||
|
|
||||||
|
/// The default error handler for [Angel] server
|
||||||
|
Future<bool> _defaultErrorHandler(
|
||||||
|
HttpException e, RequestContext req, ResponseContext res) async {
|
||||||
|
if (!req.accepts('text/html', strict: true) &&
|
||||||
|
(req.accepts('application/json') ||
|
||||||
|
req.accepts('application/javascript'))) {
|
||||||
|
await res.json(e.toJson());
|
||||||
|
return Future.value(false);
|
||||||
|
} else {
|
||||||
|
res.contentType = MediaType('text', 'html', {'charset': 'utf8'});
|
||||||
|
res.statusCode = e.statusCode;
|
||||||
|
res.write('<!DOCTYPE html><html><head><title>${e.message}</title>');
|
||||||
|
res.write('</head><body><h1>${e.message}</h1><ul>');
|
||||||
|
|
||||||
|
for (var error in e.errors) {
|
||||||
|
res.write('<li>$error</li>');
|
||||||
|
}
|
||||||
|
|
||||||
|
res.write('</ul></body></html>');
|
||||||
|
return Future.value(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Default ROOT level logger
|
||||||
|
Logger _defaultLogger() {
|
||||||
|
Logger logger = Logger('ROOT')
|
||||||
|
..onRecord.listen((rec) {
|
||||||
|
if (rec.error == null) {
|
||||||
|
print(rec.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rec.error != null) {
|
||||||
|
var err = rec.error;
|
||||||
|
if (err is HttpException && err.statusCode != 500) return;
|
||||||
|
print('${rec.message} \n');
|
||||||
|
print(rec.error);
|
||||||
|
if (rec.stackTrace != null) {
|
||||||
|
print(rec.stackTrace);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A powerful real-time/REST/MVC server class.
|
||||||
|
class Angel extends Routable {
|
||||||
|
static Future<String> _noViewEngineConfigured(String view, [Map? data]) =>
|
||||||
|
Future.value('No view engine has been configured yet.');
|
||||||
|
|
||||||
|
final List<Angel> _children = [];
|
||||||
|
final Map<
|
||||||
|
String,
|
||||||
|
Tuple4<List, Map<String, dynamic>, ParseResult<RouteResult>,
|
||||||
|
MiddlewarePipeline>> handlerCache = HashMap();
|
||||||
|
|
||||||
|
Router<RequestHandler>? _flattened;
|
||||||
|
Angel? _parent;
|
||||||
|
|
||||||
|
/// A global Map of converters that can transform responses bodies.
|
||||||
|
final Map<String, Converter<List<int>, List<int>>> encoders = {};
|
||||||
|
|
||||||
|
final Map<dynamic, InjectionRequest> _preContained = {};
|
||||||
|
|
||||||
|
/// A [MimeTypeResolver] that can be used to specify the MIME types of files not known by `package:mime`.
|
||||||
|
final MimeTypeResolver mimeTypeResolver = MimeTypeResolver();
|
||||||
|
|
||||||
|
/// A middleware to inject a serialize on every request.
|
||||||
|
FutureOr<String> Function(dynamic)? serializer;
|
||||||
|
|
||||||
|
/// A [Map] of dependency data obtained via reflection.
|
||||||
|
///
|
||||||
|
/// You may modify this [Map] yourself if you intend to avoid reflection entirely.
|
||||||
|
Map<dynamic, InjectionRequest> get preContained => _preContained;
|
||||||
|
|
||||||
|
/// Returns the [flatten]ed version of this router in production.
|
||||||
|
Router<RequestHandler> get optimizedRouter => _flattened ?? this;
|
||||||
|
|
||||||
|
/// Determines whether to allow HTTP request method overrides.
|
||||||
|
bool allowMethodOverrides = true;
|
||||||
|
|
||||||
|
/// All child application mounted on this instance.
|
||||||
|
List<Angel> get children => List<Angel>.unmodifiable(_children);
|
||||||
|
|
||||||
|
final Map<Pattern, Controller> _controllers = {};
|
||||||
|
|
||||||
|
/// A set of [Controller] objects that have been loaded into the application.
|
||||||
|
Map<Pattern, Controller> get controllers => _controllers;
|
||||||
|
|
||||||
|
/// The [AngelEnvironment] in which the application is running.
|
||||||
|
///
|
||||||
|
/// By default, it is automatically inferred.
|
||||||
|
final AngelEnvironment environment;
|
||||||
|
|
||||||
|
/// Returns the parent instance of this application, if any.
|
||||||
|
Angel? get parent => _parent;
|
||||||
|
|
||||||
|
/// Outputs diagnostics and debug messages.
|
||||||
|
Logger _logger = _defaultLogger();
|
||||||
|
|
||||||
|
Logger get logger => _logger;
|
||||||
|
|
||||||
|
/// Assign a custom logger.
|
||||||
|
/// Passing null will reset to default logger
|
||||||
|
set logger(Logger? log) {
|
||||||
|
_logger.clearListeners();
|
||||||
|
|
||||||
|
_logger = log ?? _defaultLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Plug-ins to be called right before server startup.
|
||||||
|
///
|
||||||
|
/// If the server is never started, they will never be called.
|
||||||
|
final List<AngelConfigurer> startupHooks = [];
|
||||||
|
|
||||||
|
/// Plug-ins to be called right before server shutdown.
|
||||||
|
///
|
||||||
|
/// If the server is never [close]d, they will never be called.
|
||||||
|
final List<AngelConfigurer> shutdownHooks = [];
|
||||||
|
|
||||||
|
/// Always run before responses are sent.
|
||||||
|
///
|
||||||
|
/// These will only not run if a response's `willCloseItself` is set to `true`.
|
||||||
|
final List<RequestHandler> responseFinalizers = [];
|
||||||
|
|
||||||
|
/// A function that renders views.
|
||||||
|
///
|
||||||
|
/// Called by [ResponseContext]@`render`.
|
||||||
|
ViewGenerator? viewGenerator = _noViewEngineConfigured;
|
||||||
|
|
||||||
|
/// The handler currently configured to run on [HttpException]s.
|
||||||
|
AngelErrorHandler errorHandler = _defaultErrorHandler;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Route<RequestHandler> addRoute(
|
||||||
|
String method, String path, RequestHandler handler,
|
||||||
|
{Iterable<RequestHandler> middleware = const []}) {
|
||||||
|
if (_flattened != null) {
|
||||||
|
logger.warning(
|
||||||
|
'WARNING: You added a route ($method $path) to the router, after it had been optimized.');
|
||||||
|
logger.warning(
|
||||||
|
'This route will be ignored, and no requests will ever reach it.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.addRoute(method, path, handler, middleware: middleware);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
SymlinkRoute<RequestHandler> mount(
|
||||||
|
String path, Router<RequestHandler> router) {
|
||||||
|
if (_flattened != null) {
|
||||||
|
logger.warning(
|
||||||
|
'WARNING: You added mounted a child router ($path) on the router, after it had been optimized.');
|
||||||
|
logger.warning(
|
||||||
|
'This route will be ignored, and no requests will ever reach it.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (router is Angel) {
|
||||||
|
router._parent = this;
|
||||||
|
_children.add(router);
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.mount(path.toString(), router);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Loads some base dependencies into the service container.
|
||||||
|
void bootstrapContainer() {
|
||||||
|
if (runtimeType != Angel) {
|
||||||
|
container.registerSingleton(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
container.registerSingleton<Angel>(this);
|
||||||
|
container.registerSingleton<Routable>(this);
|
||||||
|
container.registerSingleton<Router>(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shuts down the server, and closes any open [StreamController]s.
|
||||||
|
///
|
||||||
|
/// The server will be **COMPLETELY DEFUNCT** after this operation!
|
||||||
|
@override
|
||||||
|
Future<void> close() {
|
||||||
|
Future.forEach(services.values, (Service service) {
|
||||||
|
service.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
super.close();
|
||||||
|
viewGenerator = _noViewEngineConfigured;
|
||||||
|
_preContained.clear();
|
||||||
|
handlerCache.clear();
|
||||||
|
encoders.clear();
|
||||||
|
_children.clear();
|
||||||
|
//_parent = null;
|
||||||
|
//logger = null;
|
||||||
|
//_flattened = null;
|
||||||
|
startupHooks.clear();
|
||||||
|
shutdownHooks.clear();
|
||||||
|
responseFinalizers.clear();
|
||||||
|
return Future.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dumpTree(
|
||||||
|
{Function(String tree)? callback,
|
||||||
|
String header = 'Dumping route tree:',
|
||||||
|
String tab = ' ',
|
||||||
|
bool showMatchers = false}) {
|
||||||
|
if (environment.isProduction) {
|
||||||
|
_flattened ??= flatten(this);
|
||||||
|
|
||||||
|
_flattened!.dumpTree(
|
||||||
|
callback: callback,
|
||||||
|
header: header.isNotEmpty == true
|
||||||
|
? header
|
||||||
|
: (environment.isProduction
|
||||||
|
? 'Dumping flattened route tree:'
|
||||||
|
: 'Dumping route tree:'),
|
||||||
|
tab: tab);
|
||||||
|
} else {
|
||||||
|
super.dumpTree(
|
||||||
|
callback: callback,
|
||||||
|
header: header.isNotEmpty == true
|
||||||
|
? header
|
||||||
|
: (environment.isProduction
|
||||||
|
? 'Dumping flattened route tree:'
|
||||||
|
: 'Dumping route tree:'),
|
||||||
|
tab: tab);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future getHandlerResult(handler, RequestContext req, ResponseContext res) {
|
||||||
|
if (handler is RequestHandler) {
|
||||||
|
var result = handler(req, res);
|
||||||
|
return getHandlerResult(result, req, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (handler is Future) {
|
||||||
|
return handler.then((result) => getHandlerResult(result, req, res));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (handler is Function) {
|
||||||
|
var result = runContained(handler, req, res);
|
||||||
|
return getHandlerResult(result, req, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (handler is Stream) {
|
||||||
|
return getHandlerResult(handler.toList(), req, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Future.value(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs some [handler]. Returns `true` if request execution should continue.
|
||||||
|
Future<bool> executeHandler(
|
||||||
|
handler, RequestContext req, ResponseContext res) {
|
||||||
|
return getHandlerResult(handler, req, res).then((result) {
|
||||||
|
if (result == null) {
|
||||||
|
return false;
|
||||||
|
} else if (result is bool) {
|
||||||
|
return result;
|
||||||
|
} else if (result != null) {
|
||||||
|
return res.serialize(result);
|
||||||
|
} else {
|
||||||
|
return res.isOpen;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Attempts to find a property by the given name within this application.
|
||||||
|
dynamic findProperty(key) {
|
||||||
|
if (configuration.containsKey(key)) return configuration[key];
|
||||||
|
|
||||||
|
//return parent != null ? parent?.findProperty(key) : null;
|
||||||
|
if (parent != null) {
|
||||||
|
return parent?.findProperty(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs several optimizations, *if* [angelEnv.isProduction] is `true`.
|
||||||
|
///
|
||||||
|
/// * Preprocesses all dependency injection, and eliminates the burden of reflecting handlers
|
||||||
|
/// at run-time.
|
||||||
|
/// * [flatten]s the route tree into a linear one.
|
||||||
|
///
|
||||||
|
/// You may [force] the optimization to run, if you are not running in production.
|
||||||
|
void optimizeForProduction({bool force = false}) {
|
||||||
|
if (environment.isProduction || force == true) {
|
||||||
|
_flattened ??= flatten(this);
|
||||||
|
logger.info('Angel is running in production mode.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run a function after injecting from service container.
|
||||||
|
/// If this function has been reflected before, then
|
||||||
|
/// the execution will be faster, as the injection requirements were stored beforehand.
|
||||||
|
Future runContained(Function handler, RequestContext req, ResponseContext res,
|
||||||
|
[Container? container]) {
|
||||||
|
container ??= Container(EmptyReflector());
|
||||||
|
return Future.sync(() {
|
||||||
|
if (_preContained.containsKey(handler)) {
|
||||||
|
return handleContained(handler, _preContained[handler]!, container)(
|
||||||
|
req, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
return runReflected(handler, req, res, container);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs with DI, and *always* reflects. Prefer [runContained].
|
||||||
|
Future runReflected(Function handler, RequestContext req, ResponseContext res,
|
||||||
|
[Container? container]) {
|
||||||
|
container ??=
|
||||||
|
req.container ?? res.app?.container ?? Container(EmptyReflector());
|
||||||
|
|
||||||
|
if (container.reflector is EmptyReflector) {
|
||||||
|
throw ArgumentError("No `reflector` passed");
|
||||||
|
}
|
||||||
|
var h = handleContained(
|
||||||
|
handler,
|
||||||
|
_preContained[handler] = preInject(handler, container.reflector),
|
||||||
|
container);
|
||||||
|
return Future.sync(() => h(req, res));
|
||||||
|
// return closureMirror.apply(args).reflectee;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Applies an [AngelConfigurer] to this instance.
|
||||||
|
Future configure(AngelConfigurer configurer) {
|
||||||
|
return Future.sync(() => configurer(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shorthand for using the [container] to instantiate, and then mount a [Controller].
|
||||||
|
/// Returns the created controller.
|
||||||
|
///
|
||||||
|
/// Just like [Container].make, in contexts without properly-reified generics (dev releases of Dart 2),
|
||||||
|
/// provide a [type] argument as well.
|
||||||
|
///
|
||||||
|
/// If you are on `Dart >=2.0.0`, simply call `mountController<T>()`.
|
||||||
|
Future<T> mountController<T extends Controller>([Type? type]) {
|
||||||
|
var controller = container.make<T>(type);
|
||||||
|
return configure(controller.configureServer).then((_) => controller);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Shorthand for calling `all('*', handler)`.
|
||||||
|
Route<RequestHandler?> fallback(RequestHandler handler) {
|
||||||
|
return all('*', handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
HookedService<Id, Data, T> use<Id, Data, T extends Service<Id, Data>>(
|
||||||
|
String path, T service) {
|
||||||
|
service.app = this;
|
||||||
|
return super.use(path, service)..app = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const String _reflectionErrorMessage =
|
||||||
|
'${ContainerConst.defaultErrorMessage} $_reflectionInfo';
|
||||||
|
|
||||||
|
static const String _reflectionInfo =
|
||||||
|
'Features like controllers, constructor dependency injection, and `ioc` require reflection, '
|
||||||
|
'and will not work without it.\n\n'
|
||||||
|
'For more, see the documentation:\n'
|
||||||
|
'https://docs.angel-dart.dev/guides/dependency-injection#enabling-dart-mirrors-or-other-reflection';
|
||||||
|
|
||||||
|
Angel(
|
||||||
|
{Reflector reflector =
|
||||||
|
const ThrowingReflector(errorMessage: _reflectionErrorMessage),
|
||||||
|
this.environment = angelEnv,
|
||||||
|
Logger? logger,
|
||||||
|
this.allowMethodOverrides = true,
|
||||||
|
this.serializer,
|
||||||
|
this.viewGenerator})
|
||||||
|
: super(reflector) {
|
||||||
|
// Override default logger
|
||||||
|
if (logger != null) {
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (reflector is EmptyReflector || reflector is ThrowingReflector) {
|
||||||
|
var msg =
|
||||||
|
'No `reflector` was passed to the Angel constructor, so reflection will not be available.\n$_reflectionInfo';
|
||||||
|
this.logger.warning(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
bootstrapContainer();
|
||||||
|
viewGenerator ??= _noViewEngineConfigured;
|
||||||
|
serializer ??= json.encode;
|
||||||
|
}
|
||||||
|
}
|
380
packages/framework/lib/src/core/service.dart
Normal file
380
packages/framework/lib/src/core/service.dart
Normal file
|
@ -0,0 +1,380 @@
|
||||||
|
library platform_framework.http.service;
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:platform_exceptions/http_exception.dart';
|
||||||
|
import 'package:belatuk_merge_map/belatuk_merge_map.dart';
|
||||||
|
import 'package:quiver/core.dart';
|
||||||
|
import '../util.dart';
|
||||||
|
import 'anonymous_service.dart';
|
||||||
|
import 'hooked_service.dart' show HookedService;
|
||||||
|
import 'metadata.dart';
|
||||||
|
import 'request_context.dart';
|
||||||
|
import 'response_context.dart';
|
||||||
|
import 'routable.dart';
|
||||||
|
import 'server.dart';
|
||||||
|
|
||||||
|
/// Indicates how the service was accessed.
|
||||||
|
///
|
||||||
|
/// This will be passed to the `params` object in a service method.
|
||||||
|
/// When requested on the server side, this will be null.
|
||||||
|
class Providers {
|
||||||
|
/// The transport through which the client is accessing this service.
|
||||||
|
final String via;
|
||||||
|
|
||||||
|
const Providers(this.via);
|
||||||
|
|
||||||
|
static const String viaRest = 'rest';
|
||||||
|
static const String viaWebsocket = 'websocket';
|
||||||
|
static const String viaGraphQL = 'graphql';
|
||||||
|
|
||||||
|
/// Represents a request via REST.
|
||||||
|
static const Providers rest = Providers(viaRest);
|
||||||
|
|
||||||
|
/// Represents a request over WebSockets.
|
||||||
|
static const Providers websocket = Providers(viaWebsocket);
|
||||||
|
|
||||||
|
/// Represents a request parsed from GraphQL.
|
||||||
|
static const Providers graphQL = Providers(viaGraphQL);
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => hashObjects([via]);
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(other) => other is Providers && other.via == via;
|
||||||
|
|
||||||
|
Map<String, String> toJson() {
|
||||||
|
return {'via': via};
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
return 'via:$via';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A front-facing interface that can present data to and operate on data on behalf of the user.
|
||||||
|
///
|
||||||
|
/// Heavily inspired by FeathersJS. <3
|
||||||
|
class Service<Id, Data> extends Routable {
|
||||||
|
/// A [List] of keys that services should ignore, should they see them in the query.
|
||||||
|
static const List<String> specialQueryKeys = <String>[
|
||||||
|
r'$limit',
|
||||||
|
r'$sort',
|
||||||
|
'page',
|
||||||
|
'token'
|
||||||
|
];
|
||||||
|
|
||||||
|
/// Handlers that must run to ensure this service's functionality.
|
||||||
|
List<RequestHandler> get bootstrappers => [];
|
||||||
|
|
||||||
|
/// The [Angel] app powering this service.
|
||||||
|
Angel? _app;
|
||||||
|
|
||||||
|
Angel get app {
|
||||||
|
if (_app == null) {
|
||||||
|
throw ArgumentError("Angel is not initialized");
|
||||||
|
}
|
||||||
|
return _app!;
|
||||||
|
}
|
||||||
|
|
||||||
|
set app(Angel angel) {
|
||||||
|
_app = angel;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get isAppActive => _app != null;
|
||||||
|
|
||||||
|
/// Closes this service, including any database connections or stream controllers.
|
||||||
|
@override
|
||||||
|
void close() {}
|
||||||
|
|
||||||
|
/// An optional [readData] function can be passed to handle non-map/non-json bodies.
|
||||||
|
Service(
|
||||||
|
{FutureOr<Data> Function(RequestContext, ResponseContext)? readData}) {
|
||||||
|
_readData = readData;
|
||||||
|
|
||||||
|
_readData ??= (req, res) {
|
||||||
|
if (req.bodyAsObject is! Data) {
|
||||||
|
throw HttpException.badRequest(
|
||||||
|
message:
|
||||||
|
'Invalid request body. Expected $Data; found ${req.bodyAsObject} instead.');
|
||||||
|
} else {
|
||||||
|
return req.bodyAsObject as Data;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
FutureOr<Data> Function(RequestContext, ResponseContext)? _readData;
|
||||||
|
|
||||||
|
/// A [Function] that reads the request body and converts it into [Data].
|
||||||
|
FutureOr<Data> Function(RequestContext, ResponseContext)? get readData =>
|
||||||
|
_readData;
|
||||||
|
|
||||||
|
/// Retrieves the first object from the result of calling [index] with the given [params].
|
||||||
|
///
|
||||||
|
/// If the result of [index] is `null`, OR an empty [Iterable], a 404 `HttpException` will be thrown.
|
||||||
|
///
|
||||||
|
/// If the result is both non-null and NOT an [Iterable], it will be returned as-is.
|
||||||
|
///
|
||||||
|
/// If the result is a non-empty [Iterable], [findOne] will return `it.first`, where `it` is the aforementioned [Iterable].
|
||||||
|
///
|
||||||
|
/// A custom [errorMessage] may be provided.
|
||||||
|
Future<Data> findOne(
|
||||||
|
[Map<String, dynamic>? params,
|
||||||
|
String errorMessage = 'No record was found matching the given query.']) {
|
||||||
|
return index(params).then((result) {
|
||||||
|
if (result.isEmpty) {
|
||||||
|
throw HttpException.notFound(message: errorMessage);
|
||||||
|
} else {
|
||||||
|
return result.first;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieves all resources.
|
||||||
|
Future<List<Data>> index([Map<String, dynamic>? params]) {
|
||||||
|
throw HttpException.methodNotAllowed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieves the desired resource.
|
||||||
|
Future<Data> read(Id id, [Map<String, dynamic>? params]) {
|
||||||
|
throw HttpException.methodNotAllowed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads multiple resources at once.
|
||||||
|
///
|
||||||
|
/// Service implementations should override this to ensure data is fetched within a
|
||||||
|
/// single round trip.
|
||||||
|
Future<List<Data>> readMany(List<Id> ids, [Map<String, dynamic>? params]) {
|
||||||
|
return Future.wait(ids.map((id) => read(id, params)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a resource.
|
||||||
|
Future<Data> create(Data data, [Map<String, dynamic>? params]) {
|
||||||
|
throw HttpException.methodNotAllowed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Modifies a resource.
|
||||||
|
Future<Data> modify(Id id, Data data, [Map<String, dynamic>? params]) {
|
||||||
|
throw HttpException.methodNotAllowed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Overwrites a resource.
|
||||||
|
Future<Data> update(Id id, Data data, [Map<String, dynamic>? params]) {
|
||||||
|
throw HttpException.methodNotAllowed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Removes the given resource.
|
||||||
|
Future<Data> remove(Id id, [Map<String, dynamic>? params]) {
|
||||||
|
throw HttpException.methodNotAllowed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates an [AnonymousService] that wraps over this one, and maps input and output
|
||||||
|
/// using two converter functions.
|
||||||
|
///
|
||||||
|
/// Handy utility for handling data in a type-safe manner.
|
||||||
|
Service<Id, U> map<U>(U Function(Data) encoder, Data Function(U) decoder,
|
||||||
|
{FutureOr<U> Function(RequestContext, ResponseContext)? readData}) {
|
||||||
|
readData ??= (req, res) async {
|
||||||
|
var inner = await this.readData!(req, res)!;
|
||||||
|
return encoder(inner);
|
||||||
|
};
|
||||||
|
|
||||||
|
return AnonymousService<Id, U>(
|
||||||
|
readData: readData,
|
||||||
|
index: ([params]) {
|
||||||
|
return index(params).then((it) => it.map(encoder).toList());
|
||||||
|
},
|
||||||
|
read: (id, [params]) {
|
||||||
|
return read(id, params).then(encoder);
|
||||||
|
},
|
||||||
|
create: (data, [params]) {
|
||||||
|
return create(decoder(data), params).then(encoder);
|
||||||
|
},
|
||||||
|
modify: (id, data, [params]) {
|
||||||
|
return modify(id, decoder(data), params).then(encoder);
|
||||||
|
},
|
||||||
|
update: (id, data, [params]) {
|
||||||
|
return update(id, decoder(data), params).then(encoder);
|
||||||
|
},
|
||||||
|
remove: (id, [params]) {
|
||||||
|
return remove(id, params).then(encoder);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transforms an [id] (whether it is a String, num, etc.) into one acceptable by a service.
|
||||||
|
///
|
||||||
|
/// The single type argument, [T], is used to determine how to parse the [id].
|
||||||
|
///
|
||||||
|
/// For example, `parseId<bool>` attempts to parse the value as a [bool].
|
||||||
|
static T parseId<T>(id) {
|
||||||
|
if (id == null || id == 'null') {
|
||||||
|
return 'null' as T;
|
||||||
|
//throw ArgumentError("[Service] Null is not supported");
|
||||||
|
} else if (T == String) {
|
||||||
|
return id.toString() as T;
|
||||||
|
} else if (T == int) {
|
||||||
|
return int.parse(id.toString()) as T;
|
||||||
|
} else if (T == bool) {
|
||||||
|
return (id == true || id.toString() == 'true') as T;
|
||||||
|
} else if (T == double) {
|
||||||
|
return double.parse(id.toString()) as T;
|
||||||
|
} else if (T == num) {
|
||||||
|
return num.parse(id.toString()) as T;
|
||||||
|
} else {
|
||||||
|
return id as T;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates RESTful routes pointing to this class's methods.
|
||||||
|
void addRoutes([Service? service]) {
|
||||||
|
_addRoutesInner(service ?? this, bootstrappers);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _addRoutesInner(Service service, Iterable<RequestHandler> handlerss) {
|
||||||
|
var restProvider = {'provider': Providers.rest};
|
||||||
|
var handlers = List<RequestHandler>.from(handlerss);
|
||||||
|
|
||||||
|
// Add global middleware if declared on the instance itself
|
||||||
|
var before = getAnnotation<Middleware>(service, app.container.reflector);
|
||||||
|
|
||||||
|
if (before != null) handlers.addAll(before.handlers);
|
||||||
|
|
||||||
|
var indexMiddleware =
|
||||||
|
getAnnotation<Middleware>(service.index, app.container.reflector);
|
||||||
|
get('/', (req, res) {
|
||||||
|
return index(mergeMap([
|
||||||
|
{'query': req.queryParameters},
|
||||||
|
restProvider,
|
||||||
|
req.serviceParams
|
||||||
|
]));
|
||||||
|
}, middleware: [
|
||||||
|
...handlers,
|
||||||
|
...(indexMiddleware == null) ? [] : indexMiddleware.handlers.toList()
|
||||||
|
]);
|
||||||
|
|
||||||
|
var createMiddleware =
|
||||||
|
getAnnotation<Middleware>(service.create, app.container.reflector);
|
||||||
|
post('/', (req, ResponseContext res) {
|
||||||
|
return req.parseBody().then((_) async {
|
||||||
|
return await create(
|
||||||
|
(await readData!(req, res))!,
|
||||||
|
mergeMap([
|
||||||
|
{'query': req.queryParameters},
|
||||||
|
restProvider,
|
||||||
|
req.serviceParams
|
||||||
|
])).then((r) {
|
||||||
|
res.statusCode = 201;
|
||||||
|
return r;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, middleware: [
|
||||||
|
...handlers,
|
||||||
|
...(createMiddleware == null) ? [] : createMiddleware.handlers.toList()
|
||||||
|
]);
|
||||||
|
|
||||||
|
var readMiddleware =
|
||||||
|
getAnnotation<Middleware>(service.read, app.container.reflector);
|
||||||
|
|
||||||
|
get('/:id', (req, res) {
|
||||||
|
return read(
|
||||||
|
parseId<Id>(req.params['id']),
|
||||||
|
mergeMap([
|
||||||
|
{'query': req.queryParameters},
|
||||||
|
restProvider,
|
||||||
|
req.serviceParams
|
||||||
|
]));
|
||||||
|
}, middleware: [
|
||||||
|
...handlers,
|
||||||
|
...(readMiddleware == null) ? [] : readMiddleware.handlers.toList()
|
||||||
|
]);
|
||||||
|
|
||||||
|
var modifyMiddleware =
|
||||||
|
getAnnotation<Middleware>(service.modify, app.container.reflector);
|
||||||
|
|
||||||
|
patch('/:id', (req, res) {
|
||||||
|
return req.parseBody().then((_) async {
|
||||||
|
return await modify(
|
||||||
|
parseId<Id>(req.params['id']),
|
||||||
|
(await readData!(req, res))!,
|
||||||
|
mergeMap([
|
||||||
|
{'query': req.queryParameters},
|
||||||
|
restProvider,
|
||||||
|
req.serviceParams
|
||||||
|
]));
|
||||||
|
});
|
||||||
|
}, middleware: [
|
||||||
|
...handlers,
|
||||||
|
...(modifyMiddleware == null) ? [] : modifyMiddleware.handlers.toList()
|
||||||
|
]);
|
||||||
|
|
||||||
|
var updateMiddleware =
|
||||||
|
getAnnotation<Middleware>(service.update, app.container.reflector);
|
||||||
|
post('/:id', (req, res) {
|
||||||
|
return req.parseBody().then((_) async {
|
||||||
|
return await update(
|
||||||
|
parseId<Id>(req.params['id']),
|
||||||
|
(await readData!(req, res))!,
|
||||||
|
mergeMap([
|
||||||
|
{'query': req.queryParameters},
|
||||||
|
restProvider,
|
||||||
|
req.serviceParams
|
||||||
|
]));
|
||||||
|
});
|
||||||
|
}, middleware: [
|
||||||
|
...handlers,
|
||||||
|
...(updateMiddleware == null) ? [] : updateMiddleware.handlers.toList()
|
||||||
|
]);
|
||||||
|
|
||||||
|
put('/:id', (req, res) {
|
||||||
|
return req.parseBody().then((_) async {
|
||||||
|
return await update(
|
||||||
|
parseId<Id>(req.params['id']),
|
||||||
|
(await readData!(req, res))!,
|
||||||
|
mergeMap([
|
||||||
|
{'query': req.queryParameters},
|
||||||
|
restProvider,
|
||||||
|
req.serviceParams
|
||||||
|
]));
|
||||||
|
});
|
||||||
|
}, middleware: [
|
||||||
|
...handlers,
|
||||||
|
...(updateMiddleware == null) ? [] : updateMiddleware.handlers.toList()
|
||||||
|
]);
|
||||||
|
|
||||||
|
var removeMiddleware =
|
||||||
|
getAnnotation<Middleware>(service.remove, app.container.reflector);
|
||||||
|
delete('/', (req, res) {
|
||||||
|
return remove(
|
||||||
|
'' as Id,
|
||||||
|
mergeMap([
|
||||||
|
{'query': req.queryParameters},
|
||||||
|
restProvider,
|
||||||
|
req.serviceParams
|
||||||
|
]));
|
||||||
|
}, middleware: [
|
||||||
|
...handlers,
|
||||||
|
...(removeMiddleware == null) ? [] : removeMiddleware.handlers.toList()
|
||||||
|
]);
|
||||||
|
|
||||||
|
delete('/:id', (req, res) {
|
||||||
|
return remove(
|
||||||
|
parseId<Id>(req.params['id']),
|
||||||
|
mergeMap([
|
||||||
|
{'query': req.queryParameters},
|
||||||
|
restProvider,
|
||||||
|
req.serviceParams
|
||||||
|
]));
|
||||||
|
}, middleware: [
|
||||||
|
...handlers,
|
||||||
|
...(removeMiddleware == null) ? [] : removeMiddleware.handlers.toList()
|
||||||
|
]);
|
||||||
|
|
||||||
|
// REST compliance
|
||||||
|
put('/', (req, res) => throw HttpException.notFound());
|
||||||
|
patch('/', (req, res) => throw HttpException.notFound());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Invoked when this service is wrapped within a [HookedService].
|
||||||
|
void onHooked(HookedService hookedService) {}
|
||||||
|
}
|
10
packages/framework/lib/src/fast_name_from_symbol.dart
Normal file
10
packages/framework/lib/src/fast_name_from_symbol.dart
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
final Map<Symbol, String> _cache = {};
|
||||||
|
|
||||||
|
String fastNameFromSymbol(Symbol s) {
|
||||||
|
return _cache.putIfAbsent(s, () {
|
||||||
|
var str = s.toString();
|
||||||
|
var open = str.indexOf('"');
|
||||||
|
var close = str.lastIndexOf('"');
|
||||||
|
return str.substring(open + 1, close);
|
||||||
|
});
|
||||||
|
}
|
142
packages/framework/lib/src/http/angel_http.dart
Normal file
142
packages/framework/lib/src/http/angel_http.dart
Normal file
|
@ -0,0 +1,142 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io'
|
||||||
|
show
|
||||||
|
Cookie,
|
||||||
|
HttpRequest,
|
||||||
|
HttpResponse,
|
||||||
|
HttpServer,
|
||||||
|
Platform,
|
||||||
|
SecurityContext;
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import '../core/core.dart';
|
||||||
|
import 'http_request_context.dart';
|
||||||
|
import 'http_response_context.dart';
|
||||||
|
|
||||||
|
final RegExp _straySlashes = RegExp(r'(^/+)|(/+$)');
|
||||||
|
|
||||||
|
typedef ServerGeneratorType = Future<HttpServer> Function(dynamic, int);
|
||||||
|
|
||||||
|
/// Adapts `dart:io`'s [HttpServer] to serve Angel.
|
||||||
|
class AngelHttp extends Driver<HttpRequest, HttpResponse, HttpServer,
|
||||||
|
HttpRequestContext, HttpResponseContext> {
|
||||||
|
@override
|
||||||
|
Uri get uri {
|
||||||
|
return Uri(
|
||||||
|
scheme: 'http', host: server?.address.address, port: server?.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
AngelHttp._(super.app, super.serverGenerator, bool useZone)
|
||||||
|
: super(useZone: useZone);
|
||||||
|
|
||||||
|
factory AngelHttp(Angel app, {bool useZone = true}) {
|
||||||
|
return AngelHttp._(app, HttpServer.bind, useZone);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An instance mounted on a server started by the [serverGenerator].
|
||||||
|
factory AngelHttp.custom(Angel app, ServerGeneratorType serverGenerator,
|
||||||
|
{bool useZone = true, Map<String, String> headers = const {}}) {
|
||||||
|
return AngelHttp._(app, serverGenerator, useZone);
|
||||||
|
}
|
||||||
|
|
||||||
|
factory AngelHttp.fromSecurityContext(Angel app, SecurityContext context,
|
||||||
|
{bool useZone = true}) {
|
||||||
|
return AngelHttp._(app, (address, int port) {
|
||||||
|
return HttpServer.bindSecure(address, port, context);
|
||||||
|
}, useZone);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates an HTTPS server.
|
||||||
|
///
|
||||||
|
/// Provide paths to a certificate chain and server key (both .pem).
|
||||||
|
/// If no password is provided, a random one will be generated upon running
|
||||||
|
/// the server.
|
||||||
|
factory AngelHttp.secure(
|
||||||
|
Angel app, String certificateChainPath, String serverKeyPath,
|
||||||
|
{String? password, bool useZone = true}) {
|
||||||
|
var certificateChain =
|
||||||
|
Platform.script.resolve(certificateChainPath).toFilePath();
|
||||||
|
var serverKey = Platform.script.resolve(serverKeyPath).toFilePath();
|
||||||
|
var serverContext = SecurityContext();
|
||||||
|
serverContext.useCertificateChain(certificateChain, password: password);
|
||||||
|
serverContext.usePrivateKey(serverKey, password: password);
|
||||||
|
|
||||||
|
return AngelHttp.fromSecurityContext(app, serverContext, useZone: useZone);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future handleRequest(HttpRequest request) =>
|
||||||
|
handleRawRequest(request, request.response);
|
||||||
|
|
||||||
|
@override
|
||||||
|
void addCookies(HttpResponse response, Iterable<Cookie> cookies) =>
|
||||||
|
response.cookies.addAll(cookies);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> close() async {
|
||||||
|
return await super.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove headers from HTTP Response
|
||||||
|
void removeResponseHeader(Map<String, Object> headers) {
|
||||||
|
headers.forEach((key, value) {
|
||||||
|
server?.defaultResponseHeaders.remove(key, value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add headers to HTTP Response
|
||||||
|
void addResponseHeader(Map<String, Object> headers) {
|
||||||
|
headers.forEach((key, value) {
|
||||||
|
server?.defaultResponseHeaders.add(key, value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future closeResponse(HttpResponse response) => response.close();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<HttpRequestContext> createRequestContext(
|
||||||
|
HttpRequest request, HttpResponse response) {
|
||||||
|
var path = request.uri.path.replaceAll(_straySlashes, '');
|
||||||
|
if (path.isEmpty) path = '/';
|
||||||
|
return HttpRequestContext.from(request, app, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<HttpResponseContext> createResponseContext(
|
||||||
|
HttpRequest request, HttpResponse response,
|
||||||
|
[HttpRequestContext? correspondingRequest]) {
|
||||||
|
var context = HttpResponseContext(response, app, correspondingRequest);
|
||||||
|
context.serializer = (app.serializer ?? json.encode);
|
||||||
|
context.encoders.addAll(app.encoders);
|
||||||
|
return Future<HttpResponseContext>.value(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Stream<HttpResponse> createResponseStreamFromRawRequest(
|
||||||
|
HttpRequest request) =>
|
||||||
|
Stream.fromIterable([request.response]);
|
||||||
|
|
||||||
|
@override
|
||||||
|
void setChunkedEncoding(HttpResponse response, bool value) =>
|
||||||
|
response.headers.chunkedTransferEncoding = value;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void setContentLength(HttpResponse response, int length) =>
|
||||||
|
response.headers.contentLength = length;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void setHeader(HttpResponse response, String key, String value) =>
|
||||||
|
response.headers.set(key, value);
|
||||||
|
|
||||||
|
@override
|
||||||
|
void setStatusCode(HttpResponse response, int value) =>
|
||||||
|
response.statusCode = value;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void writeStringToResponse(HttpResponse response, String value) =>
|
||||||
|
response.write(value);
|
||||||
|
|
||||||
|
@override
|
||||||
|
void writeToResponse(HttpResponse response, List<int> data) =>
|
||||||
|
response.add(data);
|
||||||
|
}
|
19
packages/framework/lib/src/http/http.dart
Normal file
19
packages/framework/lib/src/http/http.dart
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
/// Various libraries useful for creating highly-extensible servers.
|
||||||
|
library angel_framework.http;
|
||||||
|
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
export 'angel_http.dart';
|
||||||
|
export 'http_request_context.dart';
|
||||||
|
export 'http_response_context.dart';
|
||||||
|
|
||||||
|
/// Boots a shared server instance. Use this if launching multiple isolates.
|
||||||
|
Future<HttpServer> startShared(address, int port) =>
|
||||||
|
HttpServer.bind(address ?? '127.0.0.1', port, shared: true);
|
||||||
|
|
||||||
|
Future<HttpServer> Function(dynamic, int) startSharedSecure(
|
||||||
|
SecurityContext securityContext) {
|
||||||
|
return (address, int port) => HttpServer.bindSecure(
|
||||||
|
address ?? '127.0.0.1', port, securityContext,
|
||||||
|
shared: true);
|
||||||
|
}
|
110
packages/framework/lib/src/http/http_request_context.dart
Normal file
110
packages/framework/lib/src/http/http_request_context.dart
Normal file
|
@ -0,0 +1,110 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:platform_container/platform_container.dart';
|
||||||
|
import 'package:http_parser/http_parser.dart';
|
||||||
|
|
||||||
|
import '../core/core.dart';
|
||||||
|
|
||||||
|
/// An implementation of [RequestContext] that wraps a [HttpRequest].
|
||||||
|
class HttpRequestContext extends RequestContext<HttpRequest?> {
|
||||||
|
Container? _container;
|
||||||
|
MediaType _contentType = MediaType('text', 'plain');
|
||||||
|
HttpRequest? _io;
|
||||||
|
String? _override;
|
||||||
|
String _path = '';
|
||||||
|
|
||||||
|
@override
|
||||||
|
Container? get container => _container;
|
||||||
|
|
||||||
|
@override
|
||||||
|
MediaType get contentType {
|
||||||
|
return _contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Cookie> get cookies {
|
||||||
|
return rawRequest?.cookies ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
HttpHeaders? get headers {
|
||||||
|
return rawRequest?.headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get hostname {
|
||||||
|
return rawRequest?.headers.value('host') ?? 'localhost';
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The underlying [HttpRequest] instance underneath this context.
|
||||||
|
@override
|
||||||
|
HttpRequest? get rawRequest => _io;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Stream<List<int>>? get body => _io;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get method {
|
||||||
|
return _override ?? originalMethod;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get originalMethod {
|
||||||
|
return rawRequest?.method ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get path {
|
||||||
|
return _path;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
InternetAddress get remoteAddress {
|
||||||
|
return rawRequest?.connectionInfo?.remoteAddress ??
|
||||||
|
InternetAddress("127.0.0.1");
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
HttpSession? get session {
|
||||||
|
return rawRequest?.session;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Uri get uri {
|
||||||
|
return rawRequest?.uri ?? Uri();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Magically transforms an [HttpRequest] into a [RequestContext].
|
||||||
|
static Future<HttpRequestContext> from(
|
||||||
|
HttpRequest request, Angel app, String path) {
|
||||||
|
var ctx = HttpRequestContext().._container = app.container.createChild();
|
||||||
|
|
||||||
|
var override = request.method;
|
||||||
|
|
||||||
|
if (app.allowMethodOverrides == true) {
|
||||||
|
override =
|
||||||
|
request.headers.value('x-http-method-override')?.toUpperCase() ??
|
||||||
|
request.method;
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.app = app;
|
||||||
|
ctx._contentType = request.headers.contentType == null
|
||||||
|
? MediaType('text', 'plain')
|
||||||
|
: MediaType.parse(request.headers.contentType.toString());
|
||||||
|
ctx._override = override;
|
||||||
|
ctx._path = path;
|
||||||
|
ctx._io = request;
|
||||||
|
|
||||||
|
return Future.value(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future close() {
|
||||||
|
//_contentType = null;
|
||||||
|
_io = null;
|
||||||
|
_override = null;
|
||||||
|
//_path = null;
|
||||||
|
return super.close();
|
||||||
|
}
|
||||||
|
}
|
220
packages/framework/lib/src/http/http_response_context.dart
Normal file
220
packages/framework/lib/src/http/http_response_context.dart
Normal file
|
@ -0,0 +1,220 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io' hide BytesBuilder;
|
||||||
|
import 'dart:typed_data' show BytesBuilder;
|
||||||
|
import 'package:http_parser/http_parser.dart';
|
||||||
|
|
||||||
|
import '../core/core.dart';
|
||||||
|
import 'http_request_context.dart';
|
||||||
|
|
||||||
|
/// An implementation of [ResponseContext] that abstracts over an [HttpResponse].
|
||||||
|
class HttpResponseContext extends ResponseContext<HttpResponse> {
|
||||||
|
/// The underlying [HttpResponse] under this instance.
|
||||||
|
@override
|
||||||
|
final HttpResponse rawResponse;
|
||||||
|
|
||||||
|
LockableBytesBuilder? _buffer;
|
||||||
|
|
||||||
|
final HttpRequestContext? _correspondingRequest;
|
||||||
|
bool _isDetached = false, _isClosed = false, _streamInitialized = false;
|
||||||
|
|
||||||
|
HttpResponseContext(this.rawResponse, Angel? app,
|
||||||
|
[this._correspondingRequest]) {
|
||||||
|
this.app = app;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
HttpResponse detach() {
|
||||||
|
_isDetached = true;
|
||||||
|
return rawResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
RequestContext? get correspondingRequest {
|
||||||
|
return _correspondingRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool get isOpen {
|
||||||
|
return !_isClosed && !_isDetached;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool get isBuffered => _buffer != null;
|
||||||
|
|
||||||
|
@override
|
||||||
|
BytesBuilder? get buffer => _buffer;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void addError(Object error, [StackTrace? stackTrace]) {
|
||||||
|
rawResponse.addError(error, stackTrace);
|
||||||
|
super.addError(error, stackTrace);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void useBuffer() {
|
||||||
|
_buffer = LockableBytesBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterable<String>? __allowedEncodings;
|
||||||
|
|
||||||
|
Iterable<String>? get _allowedEncodings {
|
||||||
|
return __allowedEncodings ??= correspondingRequest?.headers
|
||||||
|
?.value('accept-encoding')
|
||||||
|
?.split(',')
|
||||||
|
.map((s) => s.trim())
|
||||||
|
.where((s) => s.isNotEmpty)
|
||||||
|
.map((str) {
|
||||||
|
// Ignore quality specifications in accept-encoding
|
||||||
|
// ex. gzip;q=0.8
|
||||||
|
if (!str.contains(';')) return str;
|
||||||
|
return str.split(';')[0];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
set contentType(MediaType value) {
|
||||||
|
super.contentType = value;
|
||||||
|
if (!_streamInitialized) {
|
||||||
|
rawResponse.headers.contentType =
|
||||||
|
ContentType(value.type, value.subtype, parameters: value.parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _openStream() {
|
||||||
|
if (!_streamInitialized) {
|
||||||
|
// If this is the first stream added to this response,
|
||||||
|
// then add headers, status code, etc.
|
||||||
|
rawResponse
|
||||||
|
..statusCode = statusCode
|
||||||
|
..cookies.addAll(cookies);
|
||||||
|
headers.forEach(rawResponse.headers.set);
|
||||||
|
|
||||||
|
rawResponse.headers.date = DateTime.now();
|
||||||
|
|
||||||
|
if (headers.containsKey('content-length')) {
|
||||||
|
rawResponse.contentLength = int.tryParse(headers['content-length']!) ??
|
||||||
|
rawResponse.contentLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
rawResponse.headers.contentType = ContentType(
|
||||||
|
contentType.type, contentType.subtype,
|
||||||
|
charset: contentType.parameters['charset'],
|
||||||
|
parameters: contentType.parameters);
|
||||||
|
|
||||||
|
if (encoders.isNotEmpty && correspondingRequest != null) {
|
||||||
|
if (_allowedEncodings != null) {
|
||||||
|
for (var encodingName in _allowedEncodings!) {
|
||||||
|
Converter<List<int>, List<int>>? encoder;
|
||||||
|
var key = encodingName;
|
||||||
|
|
||||||
|
if (encoders.containsKey(encodingName)) {
|
||||||
|
encoder = encoders[encodingName];
|
||||||
|
} else if (encodingName == '*') {
|
||||||
|
encoder = encoders[key = encoders.keys.first];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (encoder != null) {
|
||||||
|
rawResponse.headers.set('content-encoding', key);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//_isClosed = true;
|
||||||
|
return _streamInitialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future addStream(Stream<List<int>> stream) {
|
||||||
|
if (_isClosed && isBuffered) throw ResponseContext.closed();
|
||||||
|
_openStream();
|
||||||
|
|
||||||
|
var output = stream;
|
||||||
|
|
||||||
|
if (encoders.isNotEmpty && correspondingRequest != null) {
|
||||||
|
if (_allowedEncodings != null) {
|
||||||
|
for (var encodingName in _allowedEncodings!) {
|
||||||
|
Converter<List<int>, List<int>>? encoder;
|
||||||
|
var key = encodingName;
|
||||||
|
|
||||||
|
if (encoders.containsKey(encodingName)) {
|
||||||
|
encoder = encoders[encodingName];
|
||||||
|
} else if (encodingName == '*') {
|
||||||
|
encoder = encoders[key = encoders.keys.first];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (encoder != null) {
|
||||||
|
output = encoders[key]!.bind(output);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return rawResponse.addStream(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void add(List<int> data) {
|
||||||
|
if (_isClosed && isBuffered) {
|
||||||
|
throw ResponseContext.closed();
|
||||||
|
} else if (!isBuffered) {
|
||||||
|
if (!_isClosed) {
|
||||||
|
_openStream();
|
||||||
|
|
||||||
|
if (encoders.isNotEmpty && correspondingRequest != null) {
|
||||||
|
if (_allowedEncodings != null) {
|
||||||
|
for (var encodingName in _allowedEncodings!) {
|
||||||
|
Converter<List<int>, List<int>>? encoder;
|
||||||
|
var key = encodingName;
|
||||||
|
|
||||||
|
if (encoders.containsKey(encodingName)) {
|
||||||
|
encoder = encoders[encodingName];
|
||||||
|
} else if (encodingName == '*') {
|
||||||
|
encoder = encoders[key = encoders.keys.first];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (encoder != null) {
|
||||||
|
data = encoders[key]!.convert(data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rawResponse.add(data);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
buffer!.add(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future close() {
|
||||||
|
if (!_isDetached) {
|
||||||
|
if (!_isClosed) {
|
||||||
|
if (!isBuffered) {
|
||||||
|
try {
|
||||||
|
_openStream();
|
||||||
|
rawResponse.close();
|
||||||
|
} catch (_) {
|
||||||
|
// This only seems to occur on `MockHttpRequest`, but
|
||||||
|
// this try/catch prevents a crash.
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
_buffer!.lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
_isClosed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
super.close();
|
||||||
|
}
|
||||||
|
return Future.value();
|
||||||
|
}
|
||||||
|
}
|
243
packages/framework/lib/src/http2/angel_http2.dart
Normal file
243
packages/framework/lib/src/http2/angel_http2.dart
Normal file
|
@ -0,0 +1,243 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:platform_framework/platform_framework.dart' hide Header;
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:http2/transport.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
import 'http2_request_context.dart';
|
||||||
|
import 'http2_response_context.dart';
|
||||||
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
|
/// Boots a shared server instance. Use this if launching multiple isolates.
|
||||||
|
Future<SecureServerSocket> startSharedHttp2(
|
||||||
|
address, int port, SecurityContext ctx) {
|
||||||
|
return SecureServerSocket.bind(address, port, ctx, shared: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adapts `package:http2`'s [ServerTransportConnection] to serve Angel.
|
||||||
|
class AngelHttp2 extends Driver<Socket, ServerTransportStream,
|
||||||
|
SecureServerSocket, Http2RequestContext, Http2ResponseContext> {
|
||||||
|
final ServerSettings? settings;
|
||||||
|
late AngelHttp _http;
|
||||||
|
final StreamController<HttpRequest> _onHttp1 = StreamController();
|
||||||
|
final Map<String, MockHttpSession> _sessions = {};
|
||||||
|
final Uuid _uuid = Uuid();
|
||||||
|
_AngelHttp2ServerSocket? _artificial;
|
||||||
|
|
||||||
|
SecureServerSocket? get socket => _artificial;
|
||||||
|
|
||||||
|
AngelHttp2._(
|
||||||
|
Angel app,
|
||||||
|
Future<SecureServerSocket> Function(dynamic, int) serverGenerator,
|
||||||
|
bool useZone,
|
||||||
|
bool allowHttp1,
|
||||||
|
this.settings)
|
||||||
|
: super(
|
||||||
|
app,
|
||||||
|
serverGenerator,
|
||||||
|
useZone: useZone,
|
||||||
|
) {
|
||||||
|
if (allowHttp1) {
|
||||||
|
_http = AngelHttp(app, useZone: useZone);
|
||||||
|
onHttp1.listen(_http.handleRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
factory AngelHttp2(Angel app, SecurityContext securityContext,
|
||||||
|
{bool useZone = true,
|
||||||
|
bool allowHttp1 = false,
|
||||||
|
ServerSettings? settings}) {
|
||||||
|
return AngelHttp2.custom(app, securityContext, SecureServerSocket.bind,
|
||||||
|
allowHttp1: allowHttp1, settings: settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
factory AngelHttp2.custom(
|
||||||
|
Angel app,
|
||||||
|
SecurityContext ctx,
|
||||||
|
Future<SecureServerSocket> Function(
|
||||||
|
InternetAddress? address, int port, SecurityContext ctx)
|
||||||
|
serverGenerator,
|
||||||
|
{bool useZone = true,
|
||||||
|
bool allowHttp1 = false,
|
||||||
|
ServerSettings? settings}) {
|
||||||
|
return AngelHttp2._(app, (address, port) {
|
||||||
|
var addr = address is InternetAddress
|
||||||
|
? address
|
||||||
|
: InternetAddress(address.toString());
|
||||||
|
return Future.sync(() => serverGenerator(addr, port, ctx));
|
||||||
|
}, useZone, allowHttp1, settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Fires when an HTTP/1.x request is received.
|
||||||
|
Stream<HttpRequest> get onHttp1 => _onHttp1.stream;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<SecureServerSocket> generateServer([address, int? port]) async {
|
||||||
|
var s = await serverGenerator(address ?? '127.0.0.1', port ?? 0);
|
||||||
|
return _artificial = _AngelHttp2ServerSocket(s, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> close() async {
|
||||||
|
await _artificial?.close();
|
||||||
|
await _http.close();
|
||||||
|
return await super.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void addCookies(ServerTransportStream response, Iterable<Cookie> cookies) {
|
||||||
|
var headers =
|
||||||
|
cookies.map((cookie) => Header.ascii('set-cookie', cookie.toString()));
|
||||||
|
response.sendHeaders(headers.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future closeResponse(ServerTransportStream response) {
|
||||||
|
response.terminate();
|
||||||
|
return Future.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Http2RequestContext> createRequestContext(
|
||||||
|
Socket request, ServerTransportStream response) {
|
||||||
|
return Http2RequestContext.from(response, request, app, _sessions, _uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Http2ResponseContext> createResponseContext(
|
||||||
|
Socket request, ServerTransportStream response,
|
||||||
|
[Http2RequestContext? correspondingRequest]) async {
|
||||||
|
return Http2ResponseContext(app, response, correspondingRequest)
|
||||||
|
..encoders.addAll(app.encoders);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Stream<ServerTransportStream> createResponseStreamFromRawRequest(
|
||||||
|
Socket request) {
|
||||||
|
var connection =
|
||||||
|
ServerTransportConnection.viaSocket(request, settings: settings);
|
||||||
|
return connection.incomingStreams;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void setChunkedEncoding(ServerTransportStream response, bool value) {
|
||||||
|
// Do nothing in HTTP/2
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void setContentLength(ServerTransportStream response, int length) {
|
||||||
|
setHeader(response, 'content-length', length.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void setHeader(ServerTransportStream response, String key, String? value) {
|
||||||
|
response.sendHeaders([Header.ascii(key, value!)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void setStatusCode(ServerTransportStream response, int value) {
|
||||||
|
response.sendHeaders([Header.ascii(':status', value.toString())]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Uri get uri => Uri(
|
||||||
|
scheme: 'https',
|
||||||
|
host: server?.address.address,
|
||||||
|
port: server?.port != 443 ? server?.port : null);
|
||||||
|
|
||||||
|
@override
|
||||||
|
void writeStringToResponse(ServerTransportStream response, String value) {
|
||||||
|
writeToResponse(response, utf8.encode(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void writeToResponse(ServerTransportStream response, List<int> data) {
|
||||||
|
response.sendData(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _FakeServerSocket extends Stream<Socket> implements ServerSocket {
|
||||||
|
final _AngelHttp2ServerSocket angel;
|
||||||
|
final _ctrl = StreamController<Socket>();
|
||||||
|
|
||||||
|
_FakeServerSocket(this.angel);
|
||||||
|
|
||||||
|
@override
|
||||||
|
InternetAddress get address => angel.address;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<ServerSocket> close() async {
|
||||||
|
await (_ctrl.close());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get port => angel.port;
|
||||||
|
|
||||||
|
@override
|
||||||
|
StreamSubscription<Socket> listen(void Function(Socket event)? onData,
|
||||||
|
{Function? onError, void Function()? onDone, bool? cancelOnError}) {
|
||||||
|
return _ctrl.stream.listen(onData,
|
||||||
|
cancelOnError: cancelOnError, onError: onError, onDone: onDone);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _AngelHttp2ServerSocket extends Stream<SecureSocket>
|
||||||
|
implements SecureServerSocket {
|
||||||
|
final SecureServerSocket socket;
|
||||||
|
final AngelHttp2 driver;
|
||||||
|
final _ctrl = StreamController<SecureSocket>();
|
||||||
|
late _FakeServerSocket _fake;
|
||||||
|
StreamSubscription? _sub;
|
||||||
|
|
||||||
|
_AngelHttp2ServerSocket(this.socket, this.driver) {
|
||||||
|
_fake = _FakeServerSocket(this);
|
||||||
|
HttpServer.listenOn(_fake).pipe(driver._onHttp1);
|
||||||
|
_sub = socket.listen(
|
||||||
|
(socket) {
|
||||||
|
if (socket.selectedProtocol == null ||
|
||||||
|
socket.selectedProtocol == 'http/1.0' ||
|
||||||
|
socket.selectedProtocol == 'http/1.1') {
|
||||||
|
_fake._ctrl.add(socket);
|
||||||
|
} else if (socket.selectedProtocol == 'h2' ||
|
||||||
|
socket.selectedProtocol == 'h2-14') {
|
||||||
|
_ctrl.add(socket);
|
||||||
|
} else {
|
||||||
|
socket.destroy();
|
||||||
|
throw Exception(
|
||||||
|
'AngelHttp2 does not support ${socket.selectedProtocol} as an ALPN protocol.');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onDone: _ctrl.close,
|
||||||
|
onError: (e, st) {
|
||||||
|
driver.app.logger.warning(
|
||||||
|
'HTTP/2 incoming connection failure: ', e, st as StackTrace);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
InternetAddress get address => socket.address;
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get port => socket.port;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<SecureServerSocket> close() {
|
||||||
|
_sub?.cancel();
|
||||||
|
_fake.close();
|
||||||
|
_ctrl.close();
|
||||||
|
return socket.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
StreamSubscription<SecureSocket> listen(
|
||||||
|
void Function(SecureSocket event)? onData,
|
||||||
|
{Function? onError,
|
||||||
|
void Function()? onDone,
|
||||||
|
bool? cancelOnError}) {
|
||||||
|
return _ctrl.stream.listen(onData,
|
||||||
|
cancelOnError: cancelOnError, onError: onError, onDone: onDone);
|
||||||
|
}
|
||||||
|
}
|
186
packages/framework/lib/src/http2/http2_request_context.dart
Normal file
186
packages/framework/lib/src/http2/http2_request_context.dart
Normal file
|
@ -0,0 +1,186 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:platform_container/platform_container.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:collection/collection.dart' show IterableExtension;
|
||||||
|
import 'package:http2/transport.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
|
final RegExp _comma = RegExp(r',\s*');
|
||||||
|
final RegExp _straySlashes = RegExp(r'(^/+)|(/+$)');
|
||||||
|
|
||||||
|
class Http2RequestContext extends RequestContext<ServerTransportStream?> {
|
||||||
|
final StreamController<List<int>> _body = StreamController();
|
||||||
|
@override
|
||||||
|
final Container container;
|
||||||
|
List<Cookie> _cookies = <Cookie>[];
|
||||||
|
HttpHeaders? _headers;
|
||||||
|
String? _method, _override, _path;
|
||||||
|
late Socket _socket;
|
||||||
|
ServerTransportStream? _stream;
|
||||||
|
Uri? _uri;
|
||||||
|
HttpSession? _session;
|
||||||
|
|
||||||
|
Http2RequestContext._(this.container);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Stream<List<int>> get body => _body.stream;
|
||||||
|
|
||||||
|
static Future<Http2RequestContext> from(
|
||||||
|
ServerTransportStream stream,
|
||||||
|
Socket socket,
|
||||||
|
Angel app,
|
||||||
|
Map<String, MockHttpSession> sessions,
|
||||||
|
Uuid uuid) {
|
||||||
|
var c = Completer<Http2RequestContext>();
|
||||||
|
var req = Http2RequestContext._(app.container.createChild())
|
||||||
|
..app = app
|
||||||
|
.._socket = socket
|
||||||
|
.._stream = stream;
|
||||||
|
|
||||||
|
var headers = req._headers = MockHttpHeaders();
|
||||||
|
// String scheme = 'https', host = socket.address.address, path = '';
|
||||||
|
var uri =
|
||||||
|
Uri(scheme: 'https', host: socket.address.address, port: socket.port);
|
||||||
|
var cookies = <Cookie>[];
|
||||||
|
|
||||||
|
void finalize() {
|
||||||
|
req
|
||||||
|
.._cookies = List.unmodifiable(cookies)
|
||||||
|
.._uri = uri;
|
||||||
|
if (!c.isCompleted) c.complete(req);
|
||||||
|
}
|
||||||
|
|
||||||
|
void parseHost(String value) {
|
||||||
|
var inUri = Uri.tryParse(value);
|
||||||
|
if (inUri == null) return;
|
||||||
|
// if (uri == null || uri.scheme == 'localhost') return;
|
||||||
|
|
||||||
|
if (inUri.hasScheme) uri = uri.replace(scheme: inUri.scheme);
|
||||||
|
|
||||||
|
if (inUri.hasAuthority) {
|
||||||
|
uri = uri.replace(host: inUri.host, userInfo: inUri.userInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inUri.hasPort) uri = uri.replace(port: inUri.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.incomingMessages.listen((msg) {
|
||||||
|
if (msg is DataStreamMessage) {
|
||||||
|
finalize();
|
||||||
|
req._body.add(msg.bytes);
|
||||||
|
} else if (msg is HeadersStreamMessage) {
|
||||||
|
for (var header in msg.headers) {
|
||||||
|
var name = ascii.decode(header.name).toLowerCase();
|
||||||
|
var value = Uri.decodeComponent(ascii.decode(header.value));
|
||||||
|
|
||||||
|
switch (name) {
|
||||||
|
case ':method':
|
||||||
|
req._method = value;
|
||||||
|
break;
|
||||||
|
case ':path':
|
||||||
|
var inUri = Uri.parse(value);
|
||||||
|
uri = uri.replace(path: inUri.path);
|
||||||
|
if (inUri.hasQuery) uri = uri.replace(query: inUri.query);
|
||||||
|
var path = uri.path.replaceAll(_straySlashes, '');
|
||||||
|
req._path = path;
|
||||||
|
if (path.isEmpty) req._path = '/';
|
||||||
|
break;
|
||||||
|
case ':scheme':
|
||||||
|
uri = uri.replace(scheme: value);
|
||||||
|
break;
|
||||||
|
case ':authority':
|
||||||
|
parseHost(value);
|
||||||
|
break;
|
||||||
|
case 'cookie':
|
||||||
|
var cookieStrings = value.split(';').map((s) => s.trim());
|
||||||
|
|
||||||
|
for (var cookieString in cookieStrings) {
|
||||||
|
try {
|
||||||
|
cookies.add(Cookie.fromSetCookieValue(cookieString));
|
||||||
|
} catch (_) {
|
||||||
|
// Ignore malformed cookies, and just don't add them to the container.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
var name = ascii.decode(header.name).toLowerCase();
|
||||||
|
|
||||||
|
if (name == 'host') {
|
||||||
|
parseHost(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
headers.add(name, value.split(_comma));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.endStream) finalize();
|
||||||
|
}
|
||||||
|
}, onDone: () {
|
||||||
|
finalize();
|
||||||
|
req._body.close();
|
||||||
|
}, cancelOnError: true, onError: c.completeError);
|
||||||
|
|
||||||
|
// Apply session
|
||||||
|
var dartSessId = cookies.firstWhereOrNull((c) => c.name == 'DARTSESSID');
|
||||||
|
|
||||||
|
dartSessId ??= Cookie('DARTSESSID', uuid.v4());
|
||||||
|
|
||||||
|
req._session = sessions.putIfAbsent(
|
||||||
|
dartSessId.value,
|
||||||
|
() => MockHttpSession(id: dartSessId!.value),
|
||||||
|
);
|
||||||
|
|
||||||
|
return c.future;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Cookie> get cookies => _cookies;
|
||||||
|
|
||||||
|
/// The underlying HTTP/2 [ServerTransportStream].
|
||||||
|
ServerTransportStream? get stream => _stream;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Uri? get uri => _uri;
|
||||||
|
|
||||||
|
@override
|
||||||
|
HttpSession? get session {
|
||||||
|
return _session;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
InternetAddress get remoteAddress => _socket.remoteAddress;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get path {
|
||||||
|
return _path ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get originalMethod {
|
||||||
|
return _method ?? 'GET';
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get method {
|
||||||
|
return _override ?? _method ?? 'GET';
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get hostname => _headers?.value('host') ?? 'localhost';
|
||||||
|
|
||||||
|
@override
|
||||||
|
HttpHeaders? get headers => _headers;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future close() {
|
||||||
|
_body.close();
|
||||||
|
return super.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
ServerTransportStream? get rawRequest => _stream;
|
||||||
|
}
|
233
packages/framework/lib/src/http2/http2_response_context.dart
Normal file
233
packages/framework/lib/src/http2/http2_response_context.dart
Normal file
|
@ -0,0 +1,233 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io' hide BytesBuilder;
|
||||||
|
import 'dart:typed_data';
|
||||||
|
import 'package:platform_framework/platform_framework.dart' hide Header;
|
||||||
|
import 'package:http2/transport.dart';
|
||||||
|
import 'http2_request_context.dart';
|
||||||
|
|
||||||
|
class Http2ResponseContext extends ResponseContext<ServerTransportStream> {
|
||||||
|
final ServerTransportStream stream;
|
||||||
|
|
||||||
|
@override
|
||||||
|
ServerTransportStream get rawResponse => stream;
|
||||||
|
|
||||||
|
LockableBytesBuilder? _buffer;
|
||||||
|
|
||||||
|
final Http2RequestContext? _req;
|
||||||
|
|
||||||
|
bool _isDetached = false,
|
||||||
|
_isClosed = false,
|
||||||
|
_streamInitialized = false,
|
||||||
|
_isPush = false;
|
||||||
|
|
||||||
|
Uri? _targetUri;
|
||||||
|
|
||||||
|
Http2ResponseContext(Angel? app, this.stream, this._req) {
|
||||||
|
this.app = app;
|
||||||
|
_targetUri = _req?.uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
final List<Http2ResponseContext> _pushes = [];
|
||||||
|
|
||||||
|
/// Returns `true` if an attempt to [push] a resource will succeed.
|
||||||
|
///
|
||||||
|
/// See [ServerTransportStream].`push`.
|
||||||
|
bool get canPush => stream.canPush;
|
||||||
|
|
||||||
|
/// Returns a [List] of all resources that have [push]ed to the client.
|
||||||
|
List<Http2ResponseContext> get pushes => List.unmodifiable(_pushes);
|
||||||
|
|
||||||
|
@override
|
||||||
|
ServerTransportStream detach() {
|
||||||
|
_isDetached = true;
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
RequestContext? get correspondingRequest => _req;
|
||||||
|
|
||||||
|
Uri? get targetUri => _targetUri;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool get isOpen {
|
||||||
|
return !_isClosed && !_isDetached;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool get isBuffered => _buffer != null;
|
||||||
|
|
||||||
|
@override
|
||||||
|
BytesBuilder? get buffer => _buffer;
|
||||||
|
|
||||||
|
// @override
|
||||||
|
// void addError(Object error, [StackTrace? stackTrace]) {
|
||||||
|
// super.addError(error, stackTrace);
|
||||||
|
// }
|
||||||
|
|
||||||
|
@override
|
||||||
|
void useBuffer() {
|
||||||
|
_buffer = LockableBytesBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write headers, status, etc. to the underlying [stream].
|
||||||
|
bool _openStream() {
|
||||||
|
if (_isPush || _streamInitialized) return false;
|
||||||
|
|
||||||
|
var headers = <Header>[
|
||||||
|
Header.ascii(':status', statusCode.toString()),
|
||||||
|
];
|
||||||
|
|
||||||
|
if (encoders.isNotEmpty && correspondingRequest != null) {
|
||||||
|
if (_allowedEncodings != null) {
|
||||||
|
for (var encodingName in _allowedEncodings!) {
|
||||||
|
Converter<List<int>, List<int>>? encoder;
|
||||||
|
var key = encodingName;
|
||||||
|
|
||||||
|
if (encoders.containsKey(encodingName)) {
|
||||||
|
encoder = encoders[encodingName];
|
||||||
|
} else if (encodingName == '*') {
|
||||||
|
encoder = encoders[key = encoders.keys.first];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (encoder != null) {
|
||||||
|
this.headers['content-encoding'] = key;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add all normal headers
|
||||||
|
for (var key in this.headers.keys) {
|
||||||
|
headers.add(Header.ascii(key.toLowerCase(), this.headers[key]!));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Persist session ID
|
||||||
|
cookies.add(Cookie('DARTSESSID', _req!.session!.id));
|
||||||
|
|
||||||
|
// Send all cookies
|
||||||
|
for (var cookie in cookies) {
|
||||||
|
headers.add(Header.ascii('set-cookie', cookie.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.sendHeaders(headers);
|
||||||
|
return _streamInitialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Iterable<String>? __allowedEncodings;
|
||||||
|
|
||||||
|
Iterable<String>? get _allowedEncodings {
|
||||||
|
return __allowedEncodings ??= correspondingRequest?.headers
|
||||||
|
?.value('accept-encoding')
|
||||||
|
?.split(',')
|
||||||
|
.map((s) => s.trim())
|
||||||
|
.where((s) => s.isNotEmpty)
|
||||||
|
.map((str) {
|
||||||
|
// Ignore quality specifications in accept-encoding
|
||||||
|
// ex. gzip;q=0.8
|
||||||
|
if (!str.contains(';')) return str;
|
||||||
|
return str.split(';')[0];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future addStream(Stream<List<int>> stream) {
|
||||||
|
if (!isOpen && isBuffered) throw ResponseContext.closed();
|
||||||
|
_openStream();
|
||||||
|
|
||||||
|
var output = stream;
|
||||||
|
|
||||||
|
if (encoders.isNotEmpty && correspondingRequest != null) {
|
||||||
|
if (_allowedEncodings != null) {
|
||||||
|
for (var encodingName in _allowedEncodings!) {
|
||||||
|
Converter<List<int>, List<int>>? encoder;
|
||||||
|
var key = encodingName;
|
||||||
|
|
||||||
|
if (encoders.containsKey(encodingName)) {
|
||||||
|
encoder = encoders[encodingName];
|
||||||
|
} else if (encodingName == '*') {
|
||||||
|
encoder = encoders[key = encoders.keys.first];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (encoder != null) {
|
||||||
|
output = encoders[key]!.bind(output);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return output.forEach(this.stream.sendData);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void add(List<int> data) {
|
||||||
|
if (!isOpen && isBuffered) {
|
||||||
|
throw ResponseContext.closed();
|
||||||
|
} else if (!isBuffered) {
|
||||||
|
_openStream();
|
||||||
|
|
||||||
|
if (!_isClosed) {
|
||||||
|
if (encoders.isNotEmpty && correspondingRequest != null) {
|
||||||
|
if (_allowedEncodings != null) {
|
||||||
|
for (var encodingName in _allowedEncodings!) {
|
||||||
|
Converter<List<int>, List<int>>? encoder;
|
||||||
|
var key = encodingName;
|
||||||
|
|
||||||
|
if (encoders.containsKey(encodingName)) {
|
||||||
|
encoder = encoders[encodingName];
|
||||||
|
} else if (encodingName == '*') {
|
||||||
|
encoder = encoders[key = encoders.keys.first];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (encoder != null) {
|
||||||
|
data = encoders[key]!.convert(data);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.sendData(data);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
buffer!.add(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future close() async {
|
||||||
|
if (!_isDetached && !_isClosed && !isBuffered) {
|
||||||
|
_openStream();
|
||||||
|
await stream.outgoingMessages.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
_isClosed = true;
|
||||||
|
await super.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Pushes a resource to the client.
|
||||||
|
Http2ResponseContext push(String path,
|
||||||
|
{Map<String, String> headers = const {}, String method = 'GET'}) {
|
||||||
|
var targetUri = _req!.uri!.replace(path: path);
|
||||||
|
|
||||||
|
var h = <Header>[
|
||||||
|
Header.ascii(':authority', targetUri.authority),
|
||||||
|
Header.ascii(':method', method),
|
||||||
|
Header.ascii(':path', targetUri.path),
|
||||||
|
Header.ascii(':scheme', targetUri.scheme),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (var key in headers.keys) {
|
||||||
|
h.add(Header.ascii(key, headers[key]!));
|
||||||
|
}
|
||||||
|
|
||||||
|
var s = stream.push(h);
|
||||||
|
var r = Http2ResponseContext(app, s, _req)
|
||||||
|
.._isPush = true
|
||||||
|
.._targetUri = targetUri;
|
||||||
|
_pushes.add(r);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
}
|
123
packages/framework/lib/src/safe_stream_controller.dart
Normal file
123
packages/framework/lib/src/safe_stream_controller.dart
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
typedef _InitCallback = void Function();
|
||||||
|
|
||||||
|
/// A [StreamController] boilerplate that prevents memory leaks.
|
||||||
|
abstract class SafeCtrl<T> {
|
||||||
|
factory SafeCtrl() => _SingleSafeCtrl();
|
||||||
|
|
||||||
|
factory SafeCtrl.broadcast() => _BroadcastSafeCtrl();
|
||||||
|
|
||||||
|
Stream<T> get stream;
|
||||||
|
|
||||||
|
void add(T event);
|
||||||
|
|
||||||
|
void addError(error, [StackTrace? stackTrace]);
|
||||||
|
|
||||||
|
Future close();
|
||||||
|
|
||||||
|
void whenInitialized(void Function() callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
class _SingleSafeCtrl<T> implements SafeCtrl<T> {
|
||||||
|
late StreamController<T> _stream;
|
||||||
|
bool _hasListener = false, _initialized = false;
|
||||||
|
_InitCallback? _initializer;
|
||||||
|
|
||||||
|
_SingleSafeCtrl() {
|
||||||
|
_stream = StreamController<T>(onListen: () {
|
||||||
|
_hasListener = true;
|
||||||
|
|
||||||
|
if (!_initialized && _initializer != null) {
|
||||||
|
_initializer!();
|
||||||
|
_initialized = true;
|
||||||
|
}
|
||||||
|
}, onPause: () {
|
||||||
|
_hasListener = false;
|
||||||
|
}, onResume: () {
|
||||||
|
_hasListener = true;
|
||||||
|
}, onCancel: () {
|
||||||
|
_hasListener = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Stream<T> get stream => _stream.stream;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void add(T event) {
|
||||||
|
if (_hasListener) _stream.add(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void addError(error, [StackTrace? stackTrace]) {
|
||||||
|
if (_hasListener) _stream.addError(error as Object, stackTrace);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future close() {
|
||||||
|
return _stream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void whenInitialized(void Function() callback) {
|
||||||
|
if (!_initialized) {
|
||||||
|
if (!_hasListener) {
|
||||||
|
_initializer = callback;
|
||||||
|
} else {
|
||||||
|
_initializer!();
|
||||||
|
_initialized = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _BroadcastSafeCtrl<T> implements SafeCtrl<T> {
|
||||||
|
late StreamController<T> _stream;
|
||||||
|
int _listeners = 0;
|
||||||
|
bool _initialized = false;
|
||||||
|
_InitCallback? _initializer;
|
||||||
|
|
||||||
|
_BroadcastSafeCtrl() {
|
||||||
|
_stream = StreamController<T>.broadcast(onListen: () {
|
||||||
|
_listeners++;
|
||||||
|
|
||||||
|
if (!_initialized && _initializer != null) {
|
||||||
|
_initializer!();
|
||||||
|
_initialized = true;
|
||||||
|
}
|
||||||
|
}, onCancel: () {
|
||||||
|
_listeners--;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Stream<T> get stream => _stream.stream;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void add(T event) {
|
||||||
|
if (_listeners > 0) _stream.add(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void addError(error, [StackTrace? stackTrace]) {
|
||||||
|
if (_listeners > 0) _stream.addError(error as Object, stackTrace);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future close() {
|
||||||
|
return _stream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void whenInitialized(void Function() callback) {
|
||||||
|
if (!_initialized) {
|
||||||
|
if (_listeners <= 0) {
|
||||||
|
_initializer = callback;
|
||||||
|
} else {
|
||||||
|
_initializer!();
|
||||||
|
_initialized = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
27
packages/framework/lib/src/util.dart
Normal file
27
packages/framework/lib/src/util.dart
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import 'package:platform_container/platform_container.dart';
|
||||||
|
|
||||||
|
final RegExp straySlashes = RegExp(r'(^/+)|(/+$)');
|
||||||
|
|
||||||
|
T? matchingAnnotation<T>(List<ReflectedInstance> metadata) {
|
||||||
|
for (var metaDatum in metadata) {
|
||||||
|
if (metaDatum.type.reflectedType == T) {
|
||||||
|
return metaDatum.reflectee as T?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
T? getAnnotation<T>(obj, Reflector? reflector) {
|
||||||
|
if (reflector == null) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
if (obj is Function) {
|
||||||
|
var methodMirror = reflector.reflectFunction(obj)!;
|
||||||
|
return matchingAnnotation<T>(methodMirror.annotations);
|
||||||
|
} else {
|
||||||
|
var classMirror = reflector.reflectClass(obj.runtimeType)!;
|
||||||
|
return matchingAnnotation<T>(classMirror.annotations);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
65
packages/framework/performance/hello/angel.md
Normal file
65
packages/framework/performance/hello/angel.md
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
# Angel Performance Results
|
||||||
|
|
||||||
|
5 consecutive trials run on a Windows 10 box with 4GB RAM, and several programs open in the background.
|
||||||
|
|
||||||
|
Setup:
|
||||||
|
|
||||||
|
* Angel framework `1.0.8`
|
||||||
|
* Running `wrk` 4.0.2.2
|
||||||
|
* 2 threads
|
||||||
|
* 256 connections
|
||||||
|
* 30 seconds
|
||||||
|
|
||||||
|
Average:
|
||||||
|
|
||||||
|
* `11070.18` req/sec
|
||||||
|
* `11.86` ms latency
|
||||||
|
|
||||||
|
```bash
|
||||||
|
tobe@LAPTOP-VBHCSVRH:/mnt/c/Users/thosa$ wrk -c 256 -d 30 -t 2 http://localhost:3000
|
||||||
|
Running 30s test @ http://localhost:3000
|
||||||
|
2 threads and 256 connections
|
||||||
|
Thread Stats Avg Stdev Max +/- Stdev
|
||||||
|
Latency 12.23ms 7.56ms 206.05ms 93.09%
|
||||||
|
Req/Sec 5.48k 761.94 7.18k 87.50%
|
||||||
|
324822 requests in 30.06s, 62.88MB read
|
||||||
|
Requests/sec: 10806.24
|
||||||
|
Transfer/sec: 2.09MB
|
||||||
|
tobe@LAPTOP-VBHCSVRH:/mnt/c/Users/thosa$ wrk -c 256 -d 30 -t 2 http://localhost:3000
|
||||||
|
Running 30s test @ http://localhost:3000
|
||||||
|
2 threads and 256 connections
|
||||||
|
Thread Stats Avg Stdev Max +/- Stdev
|
||||||
|
Latency 11.06ms 4.88ms 134.86ms 78.68%
|
||||||
|
Req/Sec 5.98k 539.40 7.50k 91.40%
|
||||||
|
356355 requests in 30.11s, 68.99MB read
|
||||||
|
Requests/sec: 11836.11
|
||||||
|
Transfer/sec: 2.29MB
|
||||||
|
tobe@LAPTOP-VBHCSVRH:/mnt/c/Users/thosa$ wrk -c 256 -d 30 -t 2 http://localhost:3000
|
||||||
|
Running 30s test @ http://localhost:3000
|
||||||
|
2 threads and 256 connections
|
||||||
|
Thread Stats Avg Stdev Max +/- Stdev
|
||||||
|
Latency 12.03ms 6.18ms 159.93ms 87.89%
|
||||||
|
Req/Sec 5.52k 0.88k 7.32k 90.31%
|
||||||
|
327749 requests in 30.06s, 63.45MB read
|
||||||
|
Requests/sec: 10901.35
|
||||||
|
Transfer/sec: 2.11MB
|
||||||
|
tobe@LAPTOP-VBHCSVRH:/mnt/c/Users/thosa$ wrk -c 256 -d 30 -t 2 http://localhost:3000
|
||||||
|
Running 30s test @ http://localhost:3000
|
||||||
|
2 threads and 256 connections
|
||||||
|
Thread Stats Avg Stdev Max +/- Stdev
|
||||||
|
Latency 12.92ms 7.06ms 189.00ms 82.48%
|
||||||
|
Req/Sec 5.12k 1.00k 6.42k 75.59%
|
||||||
|
302273 requests in 30.05s, 58.52MB read
|
||||||
|
Requests/sec: 10059.96
|
||||||
|
Transfer/sec: 1.95MB
|
||||||
|
tobe@LAPTOP-VBHCSVRH:/mnt/c/Users/thosa$ wrk -c 256 -d 30 -t 2 http://localhost:3000
|
||||||
|
Running 30s test @ http://localhost:3000
|
||||||
|
2 threads and 256 connections
|
||||||
|
Thread Stats Avg Stdev Max +/- Stdev
|
||||||
|
Latency 11.05ms 4.92ms 104.90ms 69.57%
|
||||||
|
Req/Sec 5.95k 0.87k 7.65k 76.80%
|
||||||
|
352798 requests in 30.03s, 68.30MB read
|
||||||
|
Requests/sec: 11747.23
|
||||||
|
Transfer/sec: 2.27MB
|
||||||
|
tobe@LAPTOP-VBHCSVRH:/mnt/c/Users/thosa$
|
||||||
|
```
|
23
packages/framework/performance/hello/main.dart
Normal file
23
packages/framework/performance/hello/main.dart
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
/// A basic server that prints "Hello, world!"
|
||||||
|
library performance.hello;
|
||||||
|
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
var app = Angel();
|
||||||
|
var http = AngelHttp.custom(app, startShared, useZone: false);
|
||||||
|
|
||||||
|
app.get('/', (req, res) => res.write('Hello, world!'));
|
||||||
|
app.optimizeForProduction(force: true);
|
||||||
|
|
||||||
|
var oldHandler = app.errorHandler;
|
||||||
|
app.errorHandler = (e, req, res) {
|
||||||
|
print('Oops: ${e.error ?? e}');
|
||||||
|
print(e.stackTrace);
|
||||||
|
return oldHandler(e, req, res);
|
||||||
|
};
|
||||||
|
|
||||||
|
await http.startServer('127.0.0.1', 3000);
|
||||||
|
print('Listening at ${http.uri}');
|
||||||
|
}
|
18
packages/framework/performance/hello/raw.dart
Normal file
18
packages/framework/performance/hello/raw.dart
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
/// A basic server that prints "Hello, world!"
|
||||||
|
library performance.hello;
|
||||||
|
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
Future<void> main() {
|
||||||
|
return HttpServer.bind('127.0.0.1', 3000, shared: true).then((server) {
|
||||||
|
print('Listening at http://${server.address.address}:${server.port}');
|
||||||
|
|
||||||
|
server.listen((request) {
|
||||||
|
if (request.uri.path == '/') {
|
||||||
|
request.response.write('Hello, world!');
|
||||||
|
}
|
||||||
|
|
||||||
|
request.response.close();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
63
packages/framework/performance/hello/raw.md
Normal file
63
packages/framework/performance/hello/raw.md
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
# `dart:io` Results
|
||||||
|
|
||||||
|
5 consecutive trials run on a Windows 10 box with 4GB RAM, and several programs open in the background.
|
||||||
|
|
||||||
|
Setup:
|
||||||
|
|
||||||
|
* Running `wrk` 4.0.2.2
|
||||||
|
* 2 threads
|
||||||
|
* 256 connections
|
||||||
|
* 30 seconds
|
||||||
|
|
||||||
|
Average:
|
||||||
|
|
||||||
|
* `14598.16` req/sec
|
||||||
|
* `8.88` ms latency
|
||||||
|
|
||||||
|
```bash
|
||||||
|
tobe@LAPTOP-VBHCSVRH:/mnt/c/Users/thosa$ wrk -c 256 -d 30 -t 2 http://localhost:3000
|
||||||
|
Running 30s test @ http://localhost:3000
|
||||||
|
2 threads and 256 connections
|
||||||
|
Thread Stats Avg Stdev Max +/- Stdev
|
||||||
|
Latency 9.67ms 8.19ms 202.28ms 96.17%
|
||||||
|
Req/Sec 7.15k 1.47k 9.97k 73.76%
|
||||||
|
417716 requests in 30.07s, 82.06MB read
|
||||||
|
Requests/sec: 13892.50
|
||||||
|
Transfer/sec: 2.73MB
|
||||||
|
tobe@LAPTOP-VBHCSVRH:/mnt/c/Users/thosa$ wrk -c 256 -d 30 -t 2 http://localhost:3000
|
||||||
|
Running 30s test @ http://localhost:3000
|
||||||
|
2 threads and 256 connections
|
||||||
|
Thread Stats Avg Stdev Max +/- Stdev
|
||||||
|
Latency 8.47ms 3.14ms 100.77ms 65.40%
|
||||||
|
Req/Sec 7.61k 670.47 8.85k 73.88%
|
||||||
|
453301 requests in 30.07s, 89.05MB read
|
||||||
|
Requests/sec: 15077.15
|
||||||
|
Transfer/sec: 2.96MB
|
||||||
|
tobe@LAPTOP-VBHCSVRH:/mnt/c/Users/thosa$ wrk -c 256 -d 30 -t 2 http://localhost:3000
|
||||||
|
Running 30s test @ http://localhost:3000
|
||||||
|
2 threads and 256 connections
|
||||||
|
Thread Stats Avg Stdev Max +/- Stdev
|
||||||
|
Latency 8.62ms 3.51ms 73.34ms 63.74%
|
||||||
|
Req/Sec 7.52k 650.22 8.91k 79.17%
|
||||||
|
448445 requests in 30.07s, 88.10MB read
|
||||||
|
Requests/sec: 14911.53
|
||||||
|
Transfer/sec: 2.93MB
|
||||||
|
tobe@LAPTOP-VBHCSVRH:/mnt/c/Users/thosa$ wrk -c 256 -d 30 -t 2 http://localhost:3000
|
||||||
|
Running 30s test @ http://localhost:3000
|
||||||
|
2 threads and 256 connections
|
||||||
|
Thread Stats Avg Stdev Max +/- Stdev
|
||||||
|
Latency 8.75ms 3.51ms 70.50ms 64.53%
|
||||||
|
Req/Sec 7.41k 825.50 10.23k 72.24%
|
||||||
|
441338 requests in 30.09s, 86.70MB read
|
||||||
|
Requests/sec: 14665.62
|
||||||
|
Transfer/sec: 2.88MB
|
||||||
|
tobe@LAPTOP-VBHCSVRH:/mnt/c/Users/thosa$ wrk -c 256 -d 30 -t 2 http://localhost:3000
|
||||||
|
Running 30s test @ http://localhost:3000
|
||||||
|
2 threads and 256 connections
|
||||||
|
Thread Stats Avg Stdev Max +/- Stdev
|
||||||
|
Latency 8.90ms 3.62ms 78.36ms 66.71%
|
||||||
|
Req/Sec 7.31k 742.11 10.79k 77.84%
|
||||||
|
434674 requests in 30.09s, 85.39MB read
|
||||||
|
Requests/sec: 14443.98
|
||||||
|
Transfer/sec: 2.84MB
|
||||||
|
```
|
541
packages/framework/pubspec.lock
Normal file
541
packages/framework/pubspec.lock
Normal file
|
@ -0,0 +1,541 @@
|
||||||
|
# Generated by pub
|
||||||
|
# See https://dart.dev/tools/pub/glossary#lockfile
|
||||||
|
packages:
|
||||||
|
_fe_analyzer_shared:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: _fe_analyzer_shared
|
||||||
|
sha256: "5aaf60d96c4cd00fe7f21594b5ad6a1b699c80a27420f8a837f4d68473ef09e3"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "68.0.0"
|
||||||
|
_macros:
|
||||||
|
dependency: transitive
|
||||||
|
description: dart
|
||||||
|
source: sdk
|
||||||
|
version: "0.1.0"
|
||||||
|
analyzer:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: analyzer
|
||||||
|
sha256: "21f1d3720fd1c70316399d5e2bccaebb415c434592d778cce8acb967b8578808"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "6.5.0"
|
||||||
|
args:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: args
|
||||||
|
sha256: "7cf60b9f0cc88203c5a190b4cd62a99feea42759a7fa695010eb5de1c0b2252a"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.5.0"
|
||||||
|
async:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: async
|
||||||
|
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.11.0"
|
||||||
|
belatuk_code_buffer:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: belatuk_code_buffer
|
||||||
|
sha256: "147d68b24f099ccc6230f354cc05c4f8df1be2a95c26daebb859cbe2096d83f4"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "5.2.0"
|
||||||
|
belatuk_combinator:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: belatuk_combinator
|
||||||
|
sha256: "04bfb2c6f667cdb668529ecf2060a2fa2c79c22c6d85793a76d1cd6d73acf68e"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "5.2.0"
|
||||||
|
belatuk_http_server:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: belatuk_http_server
|
||||||
|
sha256: "3a4cfea80835e7feec47c1cf3f0ce17b3b80433b65f2b48f940d3f95f8ffe9d9"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "4.4.0"
|
||||||
|
belatuk_merge_map:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: belatuk_merge_map
|
||||||
|
sha256: a05dc05da77e7be4a34adeb863c62b44bcd846ff6efa00cf60d17ae91e809a21
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "5.2.0"
|
||||||
|
boolean_selector:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: boolean_selector
|
||||||
|
sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.1"
|
||||||
|
charcode:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: charcode
|
||||||
|
sha256: fb98c0f6d12c920a02ee2d998da788bca066ca5f148492b7085ee23372b12306
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.3.1"
|
||||||
|
collection:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: collection
|
||||||
|
sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.19.0"
|
||||||
|
convert:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: convert
|
||||||
|
sha256: "0f08b14755d163f6e2134cb58222dd25ea2a2ee8a195e53983d57c075324d592"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "3.1.1"
|
||||||
|
coverage:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: coverage
|
||||||
|
sha256: c1fb2dce3c0085f39dc72668e85f8e0210ec7de05345821ff58530567df345a5
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.9.2"
|
||||||
|
crypto:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: crypto
|
||||||
|
sha256: ec30d999af904f33454ba22ed9a86162b35e52b44ac4807d1d93c288041d7d27
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "3.0.5"
|
||||||
|
file:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: file
|
||||||
|
sha256: "5fc22d7c25582e38ad9a8515372cd9a93834027aacf1801cf01164dac0ffa08c"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "7.0.0"
|
||||||
|
fixnum:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: fixnum
|
||||||
|
sha256: "25517a4deb0c03aa0f32fd12db525856438902d9c16536311e76cdc57b31d7d1"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.1.0"
|
||||||
|
frontend_server_client:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: frontend_server_client
|
||||||
|
sha256: f64a0333a82f30b0cca061bc3d143813a486dc086b574bfb233b7c1372427694
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "4.0.0"
|
||||||
|
glob:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: glob
|
||||||
|
sha256: "0e7014b3b7d4dac1ca4d6114f82bf1782ee86745b9b42a92c9289c23d8a0ab63"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.2"
|
||||||
|
http:
|
||||||
|
dependency: "direct dev"
|
||||||
|
description:
|
||||||
|
name: http
|
||||||
|
sha256: b9c29a161230ee03d3ccf545097fccd9b87a5264228c5d348202e0f0c28f9010
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.2.2"
|
||||||
|
http2:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: http2
|
||||||
|
sha256: "9ced024a160b77aba8fb8674e38f70875e321d319e6f303ec18e87bd5a4b0c1d"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.3.0"
|
||||||
|
http_multi_server:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: http_multi_server
|
||||||
|
sha256: "97486f20f9c2f7be8f514851703d0119c3596d14ea63227af6f7a481ef2b2f8b"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "3.2.1"
|
||||||
|
http_parser:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: http_parser
|
||||||
|
sha256: "40f592dd352890c3b60fec1b68e786cefb9603e05ff303dbc4dda49b304ecdf4"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "4.1.0"
|
||||||
|
io:
|
||||||
|
dependency: "direct dev"
|
||||||
|
description:
|
||||||
|
name: io
|
||||||
|
sha256: "2ec25704aba361659e10e3e5f5d672068d332fc8ac516421d483a11e5cbd061e"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.4"
|
||||||
|
js:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: js
|
||||||
|
sha256: c1b2e9b5ea78c45e1a0788d29606ba27dc5f71f019f32ca5140f61ef071838cf
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.7.1"
|
||||||
|
lints:
|
||||||
|
dependency: "direct dev"
|
||||||
|
description:
|
||||||
|
name: lints
|
||||||
|
sha256: "976c774dd944a42e83e2467f4cc670daef7eed6295b10b36ae8c85bcbf828235"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "4.0.0"
|
||||||
|
logging:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: logging
|
||||||
|
sha256: "623a88c9594aa774443aa3eb2d41807a48486b5613e67599fb4c41c0ad47c340"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.2.0"
|
||||||
|
macros:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: macros
|
||||||
|
sha256: "12e8a9842b5a7390de7a781ec63d793527582398d16ea26c60fed58833c9ae79"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.1.0-main.0"
|
||||||
|
matcher:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: matcher
|
||||||
|
sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.12.16+1"
|
||||||
|
meta:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: meta
|
||||||
|
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.16.0"
|
||||||
|
mime:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: mime
|
||||||
|
sha256: "801fd0b26f14a4a58ccb09d5892c3fbdeff209594300a542492cf13fba9d247a"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.6"
|
||||||
|
node_preamble:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: node_preamble
|
||||||
|
sha256: "6e7eac89047ab8a8d26cf16127b5ed26de65209847630400f9aefd7cd5c730db"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.2"
|
||||||
|
package_config:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: package_config
|
||||||
|
sha256: "1c5b77ccc91e4823a5af61ee74e6b972db1ef98c2ff5a18d3161c982a55448bd"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.0"
|
||||||
|
path:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: path
|
||||||
|
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.9.0"
|
||||||
|
platform_container:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
path: "../container/container"
|
||||||
|
relative: true
|
||||||
|
source: path
|
||||||
|
version: "9.0.0"
|
||||||
|
platform_exceptions:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
path: "../exceptions"
|
||||||
|
relative: true
|
||||||
|
source: path
|
||||||
|
version: "9.0.0"
|
||||||
|
platform_mock_request:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
path: "../mock_request"
|
||||||
|
relative: true
|
||||||
|
source: path
|
||||||
|
version: "9.0.0"
|
||||||
|
platform_model:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
path: "../model"
|
||||||
|
relative: true
|
||||||
|
source: path
|
||||||
|
version: "9.0.0"
|
||||||
|
platform_route:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
path: "../route"
|
||||||
|
relative: true
|
||||||
|
source: path
|
||||||
|
version: "9.0.0"
|
||||||
|
pool:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: pool
|
||||||
|
sha256: "20fe868b6314b322ea036ba325e6fc0711a22948856475e2c2b6306e8ab39c2a"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.5.1"
|
||||||
|
pub_semver:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: pub_semver
|
||||||
|
sha256: "40d3ab1bbd474c4c2328c91e3a7df8c6dd629b79ece4c4bd04bee496a224fb0c"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.4"
|
||||||
|
quiver:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: quiver
|
||||||
|
sha256: ea0b925899e64ecdfbf9c7becb60d5b50e706ade44a85b2363be2a22d88117d2
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "3.2.2"
|
||||||
|
recase:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: recase
|
||||||
|
sha256: e4eb4ec2dcdee52dcf99cb4ceabaffc631d7424ee55e56f280bc039737f89213
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "4.1.0"
|
||||||
|
shelf:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: shelf
|
||||||
|
sha256: e7dd780a7ffb623c57850b33f43309312fc863fb6aa3d276a754bb299839ef12
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.4.2"
|
||||||
|
shelf_packages_handler:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: shelf_packages_handler
|
||||||
|
sha256: "89f967eca29607c933ba9571d838be31d67f53f6e4ee15147d5dc2934fee1b1e"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "3.0.2"
|
||||||
|
shelf_static:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: shelf_static
|
||||||
|
sha256: c87c3875f91262785dade62d135760c2c69cb217ac759485334c5857ad89f6e3
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.1.3"
|
||||||
|
shelf_web_socket:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: shelf_web_socket
|
||||||
|
sha256: "073c147238594ecd0d193f3456a5fe91c4b0abbcc68bf5cd95b36c4e194ac611"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.0"
|
||||||
|
source_map_stack_trace:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: source_map_stack_trace
|
||||||
|
sha256: c0713a43e323c3302c2abe2a1cc89aa057a387101ebd280371d6a6c9fa68516b
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.2"
|
||||||
|
source_maps:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: source_maps
|
||||||
|
sha256: "708b3f6b97248e5781f493b765c3337db11c5d2c81c3094f10904bfa8004c703"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.10.12"
|
||||||
|
source_span:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: source_span
|
||||||
|
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.10.0"
|
||||||
|
sprintf:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: sprintf
|
||||||
|
sha256: "1fc9ffe69d4df602376b52949af107d8f5703b77cda567c4d7d86a0693120f23"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "7.0.0"
|
||||||
|
stack_trace:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: stack_trace
|
||||||
|
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.11.1"
|
||||||
|
stream_channel:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: stream_channel
|
||||||
|
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.2"
|
||||||
|
string_scanner:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: string_scanner
|
||||||
|
sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.3.0"
|
||||||
|
term_glyph:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: term_glyph
|
||||||
|
sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.2.1"
|
||||||
|
test:
|
||||||
|
dependency: "direct dev"
|
||||||
|
description:
|
||||||
|
name: test
|
||||||
|
sha256: "713a8789d62f3233c46b4a90b174737b2c04cb6ae4500f2aa8b1be8f03f5e67f"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.25.8"
|
||||||
|
test_api:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: test_api
|
||||||
|
sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.7.3"
|
||||||
|
test_core:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: test_core
|
||||||
|
sha256: "12391302411737c176b0b5d6491f466b0dd56d4763e347b6714efbaa74d7953d"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.6.5"
|
||||||
|
tuple:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: tuple
|
||||||
|
sha256: a97ce2013f240b2f3807bcbaf218765b6f301c3eff91092bcfa23a039e7dd151
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.2"
|
||||||
|
typed_data:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: typed_data
|
||||||
|
sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.3.2"
|
||||||
|
uuid:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: uuid
|
||||||
|
sha256: f33d6bb662f0e4f79dcd7ada2e6170f3b3a2530c28fc41f49a411ddedd576a77
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "4.5.0"
|
||||||
|
vm_service:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: vm_service
|
||||||
|
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "14.2.5"
|
||||||
|
watcher:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: watcher
|
||||||
|
sha256: "3d2ad6751b3c16cf07c7fca317a1413b3f26530319181b37e3b9039b84fc01d8"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.1.0"
|
||||||
|
web:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: web
|
||||||
|
sha256: d43c1d6b787bf0afad444700ae7f4db8827f701bc61c255ac8d328c6f4d52062
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.0"
|
||||||
|
web_socket:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: web_socket
|
||||||
|
sha256: "3c12d96c0c9a4eec095246debcea7b86c0324f22df69893d538fcc6f1b8cce83"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.1.6"
|
||||||
|
web_socket_channel:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: web_socket_channel
|
||||||
|
sha256: "9f187088ed104edd8662ca07af4b124465893caf063ba29758f97af57e61da8f"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "3.0.1"
|
||||||
|
webkit_inspection_protocol:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: webkit_inspection_protocol
|
||||||
|
sha256: "87d3f2333bb240704cd3f1c6b5b7acd8a10e7f0bc28c28dcf14e782014f4a572"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.2.1"
|
||||||
|
yaml:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: yaml
|
||||||
|
sha256: "75769501ea3489fca56601ff33454fe45507ea3bfb014161abc3b43ae25989d5"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "3.1.2"
|
||||||
|
sdks:
|
||||||
|
dart: ">=3.4.0 <4.0.0"
|
49
packages/framework/pubspec.yaml
Normal file
49
packages/framework/pubspec.yaml
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
name: platform_framework
|
||||||
|
version: 9.0.0
|
||||||
|
description: A high-powered HTTP server extensible framework with dependency injection, routing and much more.
|
||||||
|
homepage: https://angel3-framework.web.app/
|
||||||
|
repository: https://github.com/dart-backend/angel/tree/master/packages/framework
|
||||||
|
environment:
|
||||||
|
sdk: '>=3.3.0 <4.0.0'
|
||||||
|
dependencies:
|
||||||
|
platform_container: ^9.0.0
|
||||||
|
platform_exceptions: ^9.0.0
|
||||||
|
platform_model: ^9.0.0
|
||||||
|
platform_route: ^9.0.0
|
||||||
|
platform_mock_request: ^9.0.0
|
||||||
|
belatuk_merge_map: ^5.1.0
|
||||||
|
belatuk_combinator: ^5.1.0
|
||||||
|
belatuk_http_server: ^4.1.0
|
||||||
|
charcode: ^1.3.0
|
||||||
|
file: ^7.0.0
|
||||||
|
http_parser: ^4.0.0
|
||||||
|
http2: ^2.0.0
|
||||||
|
logging: ^1.1.0
|
||||||
|
matcher: ^0.12.16
|
||||||
|
meta: ^1.9.0
|
||||||
|
mime: ^1.0.0
|
||||||
|
path: ^1.8.0
|
||||||
|
quiver: ^3.2.0
|
||||||
|
recase: ^4.1.0
|
||||||
|
stack_trace: ^1.11.0
|
||||||
|
string_scanner: ^1.2.0
|
||||||
|
tuple: ^2.0.0
|
||||||
|
uuid: ^4.0.0
|
||||||
|
collection: ^1.17.0
|
||||||
|
dev_dependencies:
|
||||||
|
http: ^1.0.0
|
||||||
|
io: ^1.0.0
|
||||||
|
test: ^1.24.0
|
||||||
|
lints: ^4.0.0
|
||||||
|
# dependency_overrides:
|
||||||
|
# platform_container:
|
||||||
|
# path: ../container/angel_container
|
||||||
|
# platform_http_exception:
|
||||||
|
# path: ../http_exception
|
||||||
|
# platform_model:
|
||||||
|
# path: ../model
|
||||||
|
# platform_route:
|
||||||
|
# path: ../route
|
||||||
|
# platform_mock_request:
|
||||||
|
# path: ../mock_request
|
||||||
|
|
70
packages/framework/test/accepts_test.dart
Normal file
70
packages/framework/test/accepts_test.dart
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
final Uri endpoint = Uri.parse('http://example.com/accept');
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('no content type', () async {
|
||||||
|
var req = await acceptContentTypes();
|
||||||
|
expect(req.acceptsAll, isFalse);
|
||||||
|
//expect(req.accepts(ContentType.JSON), isFalse);
|
||||||
|
expect(req.accepts('application/json'), isFalse);
|
||||||
|
//expect(req.accepts(ContentType.HTML), isFalse);
|
||||||
|
expect(req.accepts('text/html'), isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('wildcard', () async {
|
||||||
|
var req = await acceptContentTypes(['*/*']);
|
||||||
|
expect(req.acceptsAll, isTrue);
|
||||||
|
//expect(req.accepts(ContentType.JSON), isTrue);
|
||||||
|
expect(req.accepts('application/json'), isTrue);
|
||||||
|
//expect(req.accepts(ContentType.HTML), isTrue);
|
||||||
|
expect(req.accepts('text/html'), isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('specific type', () async {
|
||||||
|
var req = await acceptContentTypes(['text/html']);
|
||||||
|
expect(req.acceptsAll, isFalse);
|
||||||
|
//expect(req.accepts(ContentType.JSON), isFalse);
|
||||||
|
expect(req.accepts('application/json'), isFalse);
|
||||||
|
//expect(req.accepts(ContentType.HTML), isTrue);
|
||||||
|
expect(req.accepts('text/html'), isTrue);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('strict', () async {
|
||||||
|
var req = await acceptContentTypes(['text/html', '*/*']);
|
||||||
|
expect(req.accepts('text/html'), isTrue);
|
||||||
|
//expect(req.accepts(ContentType.HTML), isTrue);
|
||||||
|
//expect(req.accepts(ContentType.JSON, strict: true), isFalse);
|
||||||
|
expect(req.accepts('application/json', strict: true), isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('disallow null', () {
|
||||||
|
late RequestContext req;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
req = await acceptContentTypes();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('throws error', () {
|
||||||
|
expect(() => req.accepts(null), throwsArgumentError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<RequestContext> acceptContentTypes(
|
||||||
|
[Iterable<String> contentTypes = const []]) {
|
||||||
|
var headerString =
|
||||||
|
contentTypes.isEmpty ? ContentType.text : contentTypes.join(',');
|
||||||
|
var rq = MockHttpRequest('GET', endpoint, persistentConnection: false);
|
||||||
|
rq.headers.set('accept', headerString);
|
||||||
|
rq.close();
|
||||||
|
var app = Angel(reflector: MirrorsReflector());
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
return http.createRequestContext(rq, rq.response);
|
||||||
|
}
|
64
packages/framework/test/all.dart
Normal file
64
packages/framework/test/all.dart
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:io/ansi.dart';
|
||||||
|
import 'http_404_hole_test.dart' as hole404;
|
||||||
|
import 'accepts_test.dart' as accepts;
|
||||||
|
import 'anonymous_service_test.dart' as anonymous_service;
|
||||||
|
import 'body_test.dart' as body;
|
||||||
|
import 'controller_test.dart' as controller;
|
||||||
|
import 'detach_test.dart' as detach;
|
||||||
|
import 'di_test.dart' as di;
|
||||||
|
import 'encoders_buffer_test.dart' as encoders_buffer;
|
||||||
|
import 'env_test.dart' as env;
|
||||||
|
import 'exception_test.dart' as exception;
|
||||||
|
import 'extension_test.dart' as extension_test;
|
||||||
|
import 'find_one_test.dart' as find_one;
|
||||||
|
import 'general_test.dart' as general;
|
||||||
|
import 'hooked_test.dart' as hooked;
|
||||||
|
import 'parameter_meta_test.dart' as parameter_meta;
|
||||||
|
import 'parse_id_test.dart' as parse_id;
|
||||||
|
import 'precontained_test.dart' as precontained;
|
||||||
|
import 'primitives_test.dart' as primitives;
|
||||||
|
import 'repeat_request_test.dart' as repeat_request;
|
||||||
|
import 'req_shutdown_test.dart' as req_shutdown;
|
||||||
|
import 'routing_test.dart' as routing;
|
||||||
|
import 'serialize_test.dart' as serialize;
|
||||||
|
import 'server_test.dart' as server;
|
||||||
|
import 'service_map_test.dart' as service_map;
|
||||||
|
import 'services_test.dart' as services;
|
||||||
|
import 'streaming_test.dart' as streaming;
|
||||||
|
import 'view_generator_test.dart' as view_generator;
|
||||||
|
//import 'response_header_test.dart' as response_header;
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
/// For running with coverage
|
||||||
|
void main() {
|
||||||
|
print(cyan.wrap('Running tests on ${Platform.version}'));
|
||||||
|
group('http_404_hole', hole404.main);
|
||||||
|
group('accepts', accepts.main);
|
||||||
|
group('anonymous service', anonymous_service.main);
|
||||||
|
group('body', body.main);
|
||||||
|
//group('response_header', response_header.main);
|
||||||
|
group('controller', controller.main);
|
||||||
|
group('detach', detach.main);
|
||||||
|
group('di', di.main);
|
||||||
|
group('encoders_buffer', encoders_buffer.main);
|
||||||
|
group('env', env.main);
|
||||||
|
group('exception', exception.main);
|
||||||
|
group('extension', extension_test.main);
|
||||||
|
group('find_one', find_one.main);
|
||||||
|
group('general', general.main);
|
||||||
|
group('hooked', hooked.main);
|
||||||
|
group('parameter_meta', parameter_meta.main);
|
||||||
|
group('parse_id', parse_id.main);
|
||||||
|
group('precontained', precontained.main);
|
||||||
|
group('primitives', primitives.main);
|
||||||
|
group('repeat_request', repeat_request.main);
|
||||||
|
group('req_shutdown', req_shutdown.main);
|
||||||
|
group('routing', routing.main);
|
||||||
|
group('serialize', serialize.main);
|
||||||
|
group('server', server.main);
|
||||||
|
group('service_map', service_map.main);
|
||||||
|
group('services', services.main);
|
||||||
|
group('streaming', streaming.main);
|
||||||
|
group('view generator', view_generator.main);
|
||||||
|
}
|
44
packages/framework/test/anonymous_service_test.dart
Normal file
44
packages/framework/test/anonymous_service_test.dart
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('custom methods', () async {
|
||||||
|
var svc = AnonymousService<String?, String?>(
|
||||||
|
index: ([p]) async => ['index'],
|
||||||
|
read: (id, [p]) async => 'read',
|
||||||
|
create: (data, [p]) async => 'create',
|
||||||
|
modify: (id, data, [p]) async => 'modify',
|
||||||
|
update: (id, data, [p]) async => 'update',
|
||||||
|
remove: (id, [p]) async => 'remove');
|
||||||
|
expect(await svc.index(), ['index']);
|
||||||
|
expect(await svc.read(null), 'read');
|
||||||
|
expect(await svc.create(null), 'create');
|
||||||
|
expect(await svc.modify(null, null), 'modify');
|
||||||
|
expect(await svc.update(null, null), 'update');
|
||||||
|
expect(await svc.remove(null), 'remove');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('defaults to throwing', () async {
|
||||||
|
try {
|
||||||
|
var svc = AnonymousService();
|
||||||
|
await svc.read(1);
|
||||||
|
throw 'Should have thrown 405!';
|
||||||
|
} on HttpException {
|
||||||
|
// print('Ok!');
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
var svc = AnonymousService();
|
||||||
|
await svc.modify(2, null);
|
||||||
|
throw 'Should have thrown 405!';
|
||||||
|
} on HttpException {
|
||||||
|
// print('Ok!');
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
var svc = AnonymousService();
|
||||||
|
await svc.update(3, null);
|
||||||
|
throw 'Should have thrown 405!';
|
||||||
|
} on HttpException {
|
||||||
|
// print('Ok!');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
139
packages/framework/test/body_test.dart
Normal file
139
packages/framework/test/body_test.dart
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
var app = Angel();
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
|
||||||
|
Future<RequestContext> request(
|
||||||
|
{bool asJson = true,
|
||||||
|
bool parse = true,
|
||||||
|
Map<String, dynamic>? bodyFields,
|
||||||
|
List? bodyList}) async {
|
||||||
|
var rq =
|
||||||
|
MockHttpRequest('POST', Uri(path: '/'), persistentConnection: false);
|
||||||
|
|
||||||
|
if (bodyFields != null) {
|
||||||
|
if (asJson) {
|
||||||
|
rq
|
||||||
|
..headers.contentType = ContentType('application', 'json')
|
||||||
|
..write(json.encode(bodyFields));
|
||||||
|
} else {
|
||||||
|
var b = StringBuffer();
|
||||||
|
var i = 0;
|
||||||
|
for (var entry in bodyFields.entries) {
|
||||||
|
if (i++ > 0) b.write('&');
|
||||||
|
b.write(entry.key);
|
||||||
|
b.write('=');
|
||||||
|
b.write(Uri.encodeComponent(entry.value.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
rq
|
||||||
|
..headers.contentType =
|
||||||
|
ContentType('application', 'x-www-form-urlencoded')
|
||||||
|
..write(json.encode(b.toString()));
|
||||||
|
}
|
||||||
|
} else if (bodyList != null) {
|
||||||
|
rq
|
||||||
|
..headers.contentType = ContentType('application', 'json')
|
||||||
|
..write(json.encode(bodyList));
|
||||||
|
}
|
||||||
|
|
||||||
|
await rq.close();
|
||||||
|
var req = await http.createRequestContext(rq, rq.response);
|
||||||
|
if (parse) await req.parseBody();
|
||||||
|
return req;
|
||||||
|
}
|
||||||
|
|
||||||
|
test('parses json maps', () async {
|
||||||
|
var req = await request(bodyFields: {'hello': 'world'});
|
||||||
|
expect(req.bodyAsObject, TypeMatcher<Map<String, dynamic>>());
|
||||||
|
expect(req.bodyAsMap, {'hello': 'world'});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('parses json lists', () async {
|
||||||
|
var req = await request(bodyList: ['foo', 'bar']);
|
||||||
|
expect(req.bodyAsObject, TypeMatcher<List>());
|
||||||
|
expect(req.bodyAsList, ['foo', 'bar']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('deserializeBody', () async {
|
||||||
|
var req = await request(
|
||||||
|
asJson: true, bodyFields: {'text': 'Hey', 'complete': false});
|
||||||
|
var todo = await req.deserializeBody(Todo.fromMap);
|
||||||
|
expect(todo.text, 'Hey');
|
||||||
|
expect(todo.completed, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('decodeBody', () async {
|
||||||
|
var req = await request(
|
||||||
|
asJson: true, bodyFields: {'text': 'Hey', 'complete': false});
|
||||||
|
var todo = await req.decodeBody(TodoCodec());
|
||||||
|
expect(todo.text, 'Hey');
|
||||||
|
expect(todo.completed, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('throws when body has not been parsed', () async {
|
||||||
|
var req = await request(parse: false);
|
||||||
|
expect(() => req.bodyAsObject, throwsStateError);
|
||||||
|
expect(() => req.bodyAsMap, throwsStateError);
|
||||||
|
expect(() => req.bodyAsList, throwsStateError);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can set body object exactly once', () async {
|
||||||
|
var req = await request(parse: false);
|
||||||
|
req.bodyAsObject = 23;
|
||||||
|
expect(req.bodyAsObject, 23);
|
||||||
|
expect(() => req.bodyAsObject = {45.6: '34'}, throwsStateError);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can set body map exactly once', () async {
|
||||||
|
var req = await request(parse: false);
|
||||||
|
req.bodyAsMap = {'hey': 'yes'};
|
||||||
|
expect(req.bodyAsMap, {'hey': 'yes'});
|
||||||
|
expect(() => req.bodyAsMap = {'hm': 'ok'}, throwsStateError);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can set body list exactly once', () async {
|
||||||
|
var req = await request(parse: false);
|
||||||
|
req.bodyAsList = [
|
||||||
|
{'hey': 'yes'}
|
||||||
|
];
|
||||||
|
expect(req.bodyAsList, [
|
||||||
|
{'hey': 'yes'}
|
||||||
|
]);
|
||||||
|
expect(
|
||||||
|
() => req.bodyAsList = [
|
||||||
|
{'hm': 'ok'}
|
||||||
|
],
|
||||||
|
throwsStateError);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
class Todo {
|
||||||
|
String? text;
|
||||||
|
bool? completed;
|
||||||
|
|
||||||
|
Todo({this.text, this.completed});
|
||||||
|
|
||||||
|
static Todo fromMap(Map? m) =>
|
||||||
|
Todo(text: m!['text'] as String?, completed: m['complete'] as bool?);
|
||||||
|
}
|
||||||
|
|
||||||
|
class TodoCodec extends Codec<Todo, Map?> {
|
||||||
|
@override
|
||||||
|
Converter<Map, Todo> get decoder => TodoDecoder();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Converter<Todo, Map> get encoder => throw UnsupportedError('no encoder');
|
||||||
|
}
|
||||||
|
|
||||||
|
class TodoDecoder extends Converter<Map, Todo> {
|
||||||
|
@override
|
||||||
|
Todo convert(Map input) => Todo.fromMap(input);
|
||||||
|
}
|
60
packages/framework/test/common.dart
Normal file
60
packages/framework/test/common.dart
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
library angel_framework.test.common;
|
||||||
|
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:matcher/matcher.dart';
|
||||||
|
|
||||||
|
class Todo extends Model {
|
||||||
|
String? text;
|
||||||
|
String? over;
|
||||||
|
|
||||||
|
Todo({this.text, this.over});
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
'text': text,
|
||||||
|
'over': over,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class BookService extends Service {
|
||||||
|
@override
|
||||||
|
Future<List> index([params]) async {
|
||||||
|
print('Book params: $params');
|
||||||
|
|
||||||
|
return [
|
||||||
|
{'foo': 'bar'}
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void incrementTodoTimes(e) {
|
||||||
|
IncrementService.times++;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Hooks(before: [incrementTodoTimes])
|
||||||
|
class IncrementService extends Service {
|
||||||
|
static int times = 0;
|
||||||
|
|
||||||
|
@override
|
||||||
|
@Hooks(after: [incrementTodoTimes])
|
||||||
|
Future<List> index([params]) async => [];
|
||||||
|
}
|
||||||
|
|
||||||
|
class IsInstanceOf<T> implements Matcher {
|
||||||
|
const IsInstanceOf();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Description describeMismatch(
|
||||||
|
item, Description mismatchDescription, Map matchState, bool verbose) {
|
||||||
|
return mismatchDescription.add('$item is not an instance of $T');
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Description describe(Description description) {
|
||||||
|
return description.add('is an instance of $T');
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool matches(item, Map matchState) => item is T;
|
||||||
|
}
|
206
packages/framework/test/controller_test.dart
Normal file
206
packages/framework/test/controller_test.dart
Normal file
|
@ -0,0 +1,206 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
import 'common.dart';
|
||||||
|
|
||||||
|
@Expose('/todos', middleware: [foo])
|
||||||
|
class TodoController extends Controller {
|
||||||
|
List<Todo> todos = [Todo(text: 'Hello', over: 'world')];
|
||||||
|
|
||||||
|
@Expose('/:id', middleware: [bar])
|
||||||
|
Future<Todo> fetchTodo(
|
||||||
|
String id, RequestContext req, ResponseContext res) async {
|
||||||
|
expect(req, isNotNull);
|
||||||
|
expect(res, isNotNull);
|
||||||
|
return todos[int.parse(id)];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Expose('/namedRoute/:foo', as: 'foo')
|
||||||
|
Future<String> someRandomRoute(
|
||||||
|
RequestContext req, ResponseContext res) async {
|
||||||
|
return "${req.params['foo']}!";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class NoExposeController extends Controller {
|
||||||
|
String getIndex() => 'Hey!';
|
||||||
|
|
||||||
|
int timesTwo(int n) => n * 2;
|
||||||
|
|
||||||
|
String repeatName(String name, int times) {
|
||||||
|
var b = StringBuffer();
|
||||||
|
for (var i = 0; i < times; i++) {
|
||||||
|
b.writeln(name);
|
||||||
|
}
|
||||||
|
return b.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Expose('/yellow', method: 'POST')
|
||||||
|
String someColor() => 'yellow';
|
||||||
|
|
||||||
|
@Expose.patch
|
||||||
|
int three() => 333;
|
||||||
|
|
||||||
|
@noExpose
|
||||||
|
String hideThis() => 'Should not be exposed';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Expose('/named', as: 'foo')
|
||||||
|
class NamedController extends Controller {
|
||||||
|
@Expose('/optional/:arg?', allowNull: ['arg'])
|
||||||
|
int optional() => 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool foo(RequestContext req, ResponseContext res) {
|
||||||
|
res.write('Hello, ');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bar(RequestContext req, ResponseContext res) {
|
||||||
|
res.write('world!');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late Angel app;
|
||||||
|
late TodoController todoController;
|
||||||
|
late NoExposeController noExposeCtrl;
|
||||||
|
late HttpServer server;
|
||||||
|
var client = http.Client();
|
||||||
|
String? url;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
app = Angel(reflector: MirrorsReflector());
|
||||||
|
app.get(
|
||||||
|
'/redirect',
|
||||||
|
(req, res) async =>
|
||||||
|
res.redirectToAction('TodoController@foo', {'foo': 'world'}));
|
||||||
|
|
||||||
|
// Register as a singleton, just for the purpose of this test
|
||||||
|
if (!app.container.has<TodoController>()) {
|
||||||
|
app.container.registerSingleton(todoController = TodoController());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Using mountController<T>();
|
||||||
|
await app.mountController<TodoController>();
|
||||||
|
|
||||||
|
noExposeCtrl = await app.mountController<NoExposeController>();
|
||||||
|
|
||||||
|
// Place controller in group. The applyRoutes() call, however, is async.
|
||||||
|
// Until https://github.com/angel-dart/route/issues/28 is closed,
|
||||||
|
// this will need to be done by manually mounting the router.
|
||||||
|
var subRouter = Router<RequestHandler>();
|
||||||
|
await todoController.applyRoutes(subRouter, app.container.reflector);
|
||||||
|
app.mount('/ctrl_group', subRouter);
|
||||||
|
|
||||||
|
print(app.controllers);
|
||||||
|
app.dumpTree();
|
||||||
|
|
||||||
|
server = await AngelHttp(app).startServer();
|
||||||
|
url = 'http://${server.address.address}:${server.port}';
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() async {
|
||||||
|
await server.close(force: true);
|
||||||
|
url = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
test('basic', () {
|
||||||
|
expect(todoController.app, app);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('create dynamic handler', () async {
|
||||||
|
var app = Angel(reflector: MirrorsReflector());
|
||||||
|
app.get(
|
||||||
|
'/foo',
|
||||||
|
ioc(({String? bar}) {
|
||||||
|
return 2;
|
||||||
|
}, optional: ['bar']));
|
||||||
|
var rq = MockHttpRequest('GET', Uri(path: 'foo'));
|
||||||
|
await AngelHttp(app).handleRequest(rq);
|
||||||
|
var body = await utf8.decoder.bind(rq.response).join();
|
||||||
|
expect(json.decode(body), 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('optional name', () async {
|
||||||
|
var app = Angel(reflector: MirrorsReflector());
|
||||||
|
await app.configure(NamedController().configureServer);
|
||||||
|
expect(app.controllers['foo'], const IsInstanceOf<NamedController>());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('middleware', () async {
|
||||||
|
var rgx = RegExp('^Hello, world!');
|
||||||
|
var response = await client.get(Uri.parse('$url/todos/0'));
|
||||||
|
print('Response: ${response.body}');
|
||||||
|
|
||||||
|
expect(rgx.firstMatch(response.body)?.start, equals(0));
|
||||||
|
|
||||||
|
var todo = json.decode(response.body.replaceAll(rgx, '')) as Map;
|
||||||
|
print('Todo: $todo');
|
||||||
|
expect(todo['text'], equals('Hello'));
|
||||||
|
expect(todo['over'], equals('world'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('controller in group', () async {
|
||||||
|
var rgx = RegExp('^Hello, world!');
|
||||||
|
var response = await client.get(Uri.parse('$url/ctrl_group/todos/0'));
|
||||||
|
print('Response: ${response.body}');
|
||||||
|
|
||||||
|
expect(rgx.firstMatch(response.body)?.start, equals(0));
|
||||||
|
|
||||||
|
var todo = json.decode(response.body.replaceAll(rgx, '')) as Map;
|
||||||
|
print('Todo: $todo');
|
||||||
|
expect(todo['text'], equals('Hello'));
|
||||||
|
expect(todo['over'], equals('world'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('named actions', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/redirect'));
|
||||||
|
print('Response: ${response.body}');
|
||||||
|
expect(response.body, equals('Hello, "world!"'));
|
||||||
|
});
|
||||||
|
|
||||||
|
group('optional expose', () {
|
||||||
|
test('removes suffixes from controller names', () {
|
||||||
|
expect(noExposeCtrl.mountPoint!.path, 'no_expose');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('mounts correct routes', () {
|
||||||
|
print(noExposeCtrl.routeMappings.keys);
|
||||||
|
expect(noExposeCtrl.routeMappings.keys.toList(),
|
||||||
|
['getIndex', 'timesTwo', 'repeatName', 'someColor', 'three']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('mounts correct methods', () {
|
||||||
|
void expectMethod(String name, String method) {
|
||||||
|
expect(noExposeCtrl.routeMappings[name]!.method, method);
|
||||||
|
}
|
||||||
|
|
||||||
|
expectMethod('getIndex', 'GET');
|
||||||
|
expectMethod('timesTwo', 'GET');
|
||||||
|
expectMethod('repeatName', 'GET');
|
||||||
|
expectMethod('someColor', 'POST');
|
||||||
|
expectMethod('three', 'PATCH');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('mounts correct paths', () {
|
||||||
|
void expectPath(String name, String path) {
|
||||||
|
expect(noExposeCtrl.routeMappings[name]!.path, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
expectPath('getIndex', '/');
|
||||||
|
expectPath('timesTwo', '/times_two/int:n');
|
||||||
|
expectPath('repeatName', '/repeat_name/:name/int:times');
|
||||||
|
expectPath('someColor', '/yellow');
|
||||||
|
expectPath('three', '/three');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
35
packages/framework/test/detach_test.dart
Normal file
35
packages/framework/test/detach_test.dart
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late AngelHttp http;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
var app = Angel();
|
||||||
|
http = AngelHttp(app);
|
||||||
|
|
||||||
|
app.get('/detach', (req, res) async {
|
||||||
|
if (res is HttpResponseContext) {
|
||||||
|
var io = res.detach();
|
||||||
|
io.write('Hey!');
|
||||||
|
await io.close();
|
||||||
|
} else {
|
||||||
|
throw StateError('This endpoint only supports HTTP/1.1.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() => http.close());
|
||||||
|
|
||||||
|
test('detach response', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/detach'));
|
||||||
|
await rq.close();
|
||||||
|
var rs = rq.response;
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
var body = await rs.transform(utf8.decoder).join();
|
||||||
|
expect(body, 'Hey!');
|
||||||
|
});
|
||||||
|
}
|
141
packages/framework/test/di_test.dart
Normal file
141
packages/framework/test/di_test.dart
Normal file
|
@ -0,0 +1,141 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:platform_container/platform_container.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
import 'common.dart';
|
||||||
|
|
||||||
|
final String sampleText = 'make your bed';
|
||||||
|
final String sampleOver = 'never';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late Angel app;
|
||||||
|
late http.Client client;
|
||||||
|
late HttpServer server;
|
||||||
|
String? url;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
app = Angel(reflector: MirrorsReflector());
|
||||||
|
client = http.Client();
|
||||||
|
|
||||||
|
// Inject some todos
|
||||||
|
app.container.registerSingleton(Todo(text: sampleText, over: sampleOver));
|
||||||
|
app.container.registerFactory<Future<Foo>>((container) async {
|
||||||
|
var req = container.make<RequestContext>();
|
||||||
|
var text = await utf8.decoder.bind(req.body!).join();
|
||||||
|
return Foo(text);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/errands', ioc((Todo singleton) => singleton));
|
||||||
|
app.get(
|
||||||
|
'/errands3',
|
||||||
|
ioc(({required Errand singleton, Todo? foo, RequestContext? req}) =>
|
||||||
|
singleton.text));
|
||||||
|
app.post('/async', ioc((Foo foo) => {'baz': foo.bar}));
|
||||||
|
await app.configure(SingletonController().configureServer);
|
||||||
|
await app.configure(ErrandController().configureServer);
|
||||||
|
|
||||||
|
server = await AngelHttp(app).startServer();
|
||||||
|
url = 'http://${server.address.host}:${server.port}';
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() async {
|
||||||
|
url = null;
|
||||||
|
client.close();
|
||||||
|
await server.close(force: true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('runContained with custom container', () async {
|
||||||
|
var app = Angel();
|
||||||
|
var c = Container(const MirrorsReflector());
|
||||||
|
c.registerSingleton(Todo(text: 'Hey!'));
|
||||||
|
|
||||||
|
app.get('/', (req, res) async {
|
||||||
|
return app.runContained((Todo t) => t.text, req, res, c);
|
||||||
|
});
|
||||||
|
|
||||||
|
var rq = MockHttpRequest('GET', Uri(path: '/'));
|
||||||
|
await rq.close();
|
||||||
|
var rs = rq.response;
|
||||||
|
await AngelHttp(app).handleRequest(rq);
|
||||||
|
var text = await rs.transform(utf8.decoder).join();
|
||||||
|
expect(text, json.encode('Hey!'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('singleton in route', () async {
|
||||||
|
validateTodoSingleton(await client.get(Uri.parse('$url/errands')));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('singleton in controller', () async {
|
||||||
|
validateTodoSingleton(await client.get(Uri.parse('$url/errands2')));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('make in route', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/errands3'));
|
||||||
|
var text = await json.decode(response.body) as String?;
|
||||||
|
expect(text, equals(sampleText));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('make in controller', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/errands4'));
|
||||||
|
var text = await json.decode(response.body) as String?;
|
||||||
|
expect(text, equals(sampleText));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('resolve from future in controller', () async {
|
||||||
|
var response =
|
||||||
|
await client.post(Uri.parse('$url/errands4/async'), body: 'hey');
|
||||||
|
expect(response.body, json.encode({'bar': 'hey'}));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('resolve from future in route', () async {
|
||||||
|
var response = await client.post(Uri.parse('$url/async'), body: 'yes');
|
||||||
|
expect(response.body, json.encode({'baz': 'yes'}));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void validateTodoSingleton(response) {
|
||||||
|
var todo = json.decode(response.body.toString()) as Map;
|
||||||
|
expect(todo['id'], equals(null));
|
||||||
|
expect(todo['text'], equals(sampleText));
|
||||||
|
expect(todo['over'], equals(sampleOver));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Expose('/errands2')
|
||||||
|
class SingletonController extends Controller {
|
||||||
|
@Expose('/')
|
||||||
|
Todo todo(Todo singleton) => singleton;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Expose('/errands4')
|
||||||
|
class ErrandController extends Controller {
|
||||||
|
@Expose('/')
|
||||||
|
String? errand(Errand errand) {
|
||||||
|
return errand.text;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Expose('/async', method: 'POST')
|
||||||
|
Map<String, String> asyncResolve(Foo foo) {
|
||||||
|
return {'bar': foo.bar};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
final String bar;
|
||||||
|
|
||||||
|
Foo(this.bar);
|
||||||
|
}
|
||||||
|
|
||||||
|
class Errand {
|
||||||
|
Todo todo;
|
||||||
|
|
||||||
|
String? get text => todo.text;
|
||||||
|
|
||||||
|
Errand(this.todo);
|
||||||
|
}
|
112
packages/framework/test/encoders_buffer_test.dart
Normal file
112
packages/framework/test/encoders_buffer_test.dart
Normal file
|
@ -0,0 +1,112 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io' hide BytesBuilder;
|
||||||
|
import 'dart:typed_data' show BytesBuilder;
|
||||||
|
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
Future<List<int>> getBody(MockHttpResponse rs) async {
|
||||||
|
var list = await rs.toList();
|
||||||
|
var bb = BytesBuilder();
|
||||||
|
list.forEach(bb.add);
|
||||||
|
return bb.takeBytes();
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late Angel app;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
app = Angel(reflector: MirrorsReflector());
|
||||||
|
app.encoders.addAll(
|
||||||
|
{
|
||||||
|
'deflate': zlib.encoder,
|
||||||
|
'gzip': gzip.encoder,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
app.get('/hello', (req, res) {
|
||||||
|
res
|
||||||
|
..useBuffer()
|
||||||
|
..write('Hello, world!');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() => app.close());
|
||||||
|
|
||||||
|
encodingTests(() => app);
|
||||||
|
}
|
||||||
|
|
||||||
|
void encodingTests(Angel Function() getApp) {
|
||||||
|
group('encoding', () {
|
||||||
|
Angel app;
|
||||||
|
late AngelHttp http;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
app = getApp();
|
||||||
|
http = AngelHttp(app);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('sends plaintext if no accept-encoding', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/hello'));
|
||||||
|
await rq.close();
|
||||||
|
var rs = rq.response;
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
|
||||||
|
var body = await rs.transform(utf8.decoder).join();
|
||||||
|
expect(body, 'Hello, world!');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('encodes if wildcard', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/hello'))
|
||||||
|
..headers.set('accept-encoding', '*');
|
||||||
|
await rq.close();
|
||||||
|
var rs = rq.response;
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
|
||||||
|
var body = await getBody(rs);
|
||||||
|
//print(rs.headers);
|
||||||
|
expect(rs.headers.value('content-encoding'), 'deflate');
|
||||||
|
expect(body, zlib.encode(utf8.encode('Hello, world!')));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('encodes if wildcard + multiple', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/hello'))
|
||||||
|
..headers.set('accept-encoding', ['foo', 'bar', '*']);
|
||||||
|
await rq.close();
|
||||||
|
var rs = rq.response;
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
|
||||||
|
var body = await getBody(rs);
|
||||||
|
expect(rs.headers.value('content-encoding'), 'deflate');
|
||||||
|
expect(body, zlib.encode(utf8.encode('Hello, world!')));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('encodes if explicit', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/hello'))
|
||||||
|
..headers.set('accept-encoding', 'gzip');
|
||||||
|
await rq.close();
|
||||||
|
var rs = rq.response;
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
|
||||||
|
var body = await getBody(rs);
|
||||||
|
expect(rs.headers.value('content-encoding'), 'gzip');
|
||||||
|
expect(body, gzip.encode(utf8.encode('Hello, world!')));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('only uses one encoder', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/hello'));
|
||||||
|
rq.headers.set('accept-encoding', ['gzip', 'deflate']);
|
||||||
|
await rq.close();
|
||||||
|
var rs = rq.response;
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
|
||||||
|
var body = await getBody(rs);
|
||||||
|
expect(rs.headers.value('content-encoding'), 'gzip');
|
||||||
|
expect(body, gzip.encode(utf8.encode('Hello, world!')));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
18
packages/framework/test/env_test.dart
Normal file
18
packages/framework/test/env_test.dart
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('custom value', () => expect(AngelEnvironment('hey').value, 'hey'));
|
||||||
|
|
||||||
|
test('lowercases', () => expect(AngelEnvironment('HeY').value, 'hey'));
|
||||||
|
test(
|
||||||
|
'default to env or development',
|
||||||
|
() => expect(AngelEnvironment().value,
|
||||||
|
(Platform.environment['ANGEL_ENV'] ?? 'development').toLowerCase()));
|
||||||
|
test('isDevelopment',
|
||||||
|
() => expect(AngelEnvironment('development').isDevelopment, true));
|
||||||
|
test('isStaging', () => expect(AngelEnvironment('staging').isStaging, true));
|
||||||
|
test('isDevelopment',
|
||||||
|
() => expect(AngelEnvironment('production').isProduction, true));
|
||||||
|
}
|
65
packages/framework/test/exception_test.dart
Normal file
65
packages/framework/test/exception_test.dart
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('named constructors', () {
|
||||||
|
expect(HttpException.badRequest(), isException(400, '400 Bad Request'));
|
||||||
|
expect(HttpException.notAuthenticated(),
|
||||||
|
isException(401, '401 Not Authenticated'));
|
||||||
|
expect(HttpException.paymentRequired(),
|
||||||
|
isException(402, '402 Payment Required'));
|
||||||
|
expect(HttpException.forbidden(), isException(403, '403 Forbidden'));
|
||||||
|
expect(HttpException.notFound(), isException(404, '404 Not Found'));
|
||||||
|
expect(HttpException.methodNotAllowed(),
|
||||||
|
isException(405, '405 Method Not Allowed'));
|
||||||
|
expect(
|
||||||
|
HttpException.notAcceptable(), isException(406, '406 Not Acceptable'));
|
||||||
|
expect(HttpException.methodTimeout(), isException(408, '408 Timeout'));
|
||||||
|
expect(HttpException.conflict(), isException(409, '409 Conflict'));
|
||||||
|
expect(HttpException.notProcessable(),
|
||||||
|
isException(422, '422 Not Processable'));
|
||||||
|
expect(HttpException.notImplemented(),
|
||||||
|
isException(501, '501 Not Implemented'));
|
||||||
|
expect(HttpException.unavailable(), isException(503, '503 Unavailable'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('fromMap', () {
|
||||||
|
expect(HttpException.fromMap({'status_code': -1, 'message': 'ok'}),
|
||||||
|
isException(-1, 'ok'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('toMap = toJson', () {
|
||||||
|
var exc = HttpException.badRequest();
|
||||||
|
expect(exc.toMap(), exc.toJson());
|
||||||
|
var json_ = json.encode(exc.toJson());
|
||||||
|
var exc2 = HttpException.fromJson(json_);
|
||||||
|
expect(exc2.toJson(), exc.toJson());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('toString', () {
|
||||||
|
expect(HttpException(statusCode: 420, message: 'Blaze It').toString(),
|
||||||
|
'420: Blaze It');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Matcher isException(int statusCode, String message) =>
|
||||||
|
_IsException(statusCode, message);
|
||||||
|
|
||||||
|
class _IsException extends Matcher {
|
||||||
|
final int statusCode;
|
||||||
|
final String message;
|
||||||
|
|
||||||
|
_IsException(this.statusCode, this.message);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Description describe(Description description) =>
|
||||||
|
description.add('has status code $statusCode and message "$message"');
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool matches(item, Map matchState) {
|
||||||
|
return item is HttpException &&
|
||||||
|
item.statusCode == statusCode &&
|
||||||
|
item.message == message;
|
||||||
|
}
|
||||||
|
}
|
32
packages/framework/test/extension_test.dart
Normal file
32
packages/framework/test/extension_test.dart
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
final Uri endpoint = Uri.parse('http://example.com');
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('single extension', () async {
|
||||||
|
var req = await makeRequest('foo.js');
|
||||||
|
expect(req.extension, '.js');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('multiple extensions', () async {
|
||||||
|
var req = await makeRequest('foo.min.js');
|
||||||
|
expect(req.extension, '.js');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('no extension', () async {
|
||||||
|
var req = await makeRequest('foo');
|
||||||
|
expect(req.extension, '');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<RequestContext> makeRequest(String path) {
|
||||||
|
var rq = MockHttpRequest('GET', endpoint.replace(path: path))..close();
|
||||||
|
var app = Angel(reflector: MirrorsReflector());
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
return http.createRequestContext(rq, rq.response);
|
||||||
|
}
|
24
packages/framework/test/find_one_test.dart
Normal file
24
packages/framework/test/find_one_test.dart
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import 'common.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
var throwsAnAngelHttpException = throwsA(const IsInstanceOf<HttpException>());
|
||||||
|
|
||||||
|
/*
|
||||||
|
test('throw 404 on null', () {
|
||||||
|
var service = AnonymousService(index: ([p]) => null);
|
||||||
|
expect(() => service.findOne(), throwsAnAngelHttpException);
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
|
test('throw 404 on empty iterable', () {
|
||||||
|
var service = AnonymousService(index: ([p]) => []);
|
||||||
|
expect(() => service.findOne(), throwsAnAngelHttpException);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('return first element of iterable', () async {
|
||||||
|
var service = AnonymousService(index: ([p]) => [2]);
|
||||||
|
expect(await service.findOne(), 2);
|
||||||
|
});
|
||||||
|
}
|
36
packages/framework/test/general_test.dart
Normal file
36
packages/framework/test/general_test.dart
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late Angel app;
|
||||||
|
late http.Client client;
|
||||||
|
late HttpServer server;
|
||||||
|
late String url;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
app = Angel(reflector: MirrorsReflector())
|
||||||
|
..post('/foo', (req, res) => res.serialize({'hello': 'world'}))
|
||||||
|
..all('*', (req, res) => throw HttpException.notFound());
|
||||||
|
client = http.Client();
|
||||||
|
|
||||||
|
server = await AngelHttp(app).startServer();
|
||||||
|
url = 'http://${server.address.host}:${server.port}';
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() async {
|
||||||
|
client.close();
|
||||||
|
await server.close(force: true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('allow override of method', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/foo'),
|
||||||
|
headers: {'X-HTTP-Method-Override': 'POST'});
|
||||||
|
print('Response: ${response.body}');
|
||||||
|
expect(json.decode(response.body), equals({'hello': 'world'}));
|
||||||
|
});
|
||||||
|
}
|
14
packages/framework/test/hm.dart
Normal file
14
packages/framework/test/hm.dart
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:io/ansi.dart';
|
||||||
|
import 'all.dart' as hm;
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
var zone = Zone.current.fork(
|
||||||
|
specification: ZoneSpecification(print: (self, parent, zone, line) {
|
||||||
|
if (line == 'null') {
|
||||||
|
parent.print(zone, cyan.wrap(StackTrace.current.toString())!);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return await zone.run(hm.main);
|
||||||
|
}
|
148
packages/framework/test/hooked_test.dart
Normal file
148
packages/framework/test/hooked_test.dart
Normal file
|
@ -0,0 +1,148 @@
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import 'common.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
Map headers = <String, String>{
|
||||||
|
'Accept': 'application/json',
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
};
|
||||||
|
|
||||||
|
late Angel app;
|
||||||
|
late HttpServer server;
|
||||||
|
late String url;
|
||||||
|
late http.Client client;
|
||||||
|
late HookedService todoService;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
app = Angel(reflector: MirrorsReflector());
|
||||||
|
client = http.Client();
|
||||||
|
app.use('/todos', MapService());
|
||||||
|
app.use('/books', BookService());
|
||||||
|
|
||||||
|
todoService = app.findHookedService<MapService>('todos')
|
||||||
|
as HookedService<dynamic, dynamic, Service>;
|
||||||
|
|
||||||
|
todoService.beforeAllStream().listen((e) {
|
||||||
|
print('Fired ${e.eventName}! Data: ${e.data}; Params: ${e.params}');
|
||||||
|
});
|
||||||
|
|
||||||
|
app.errorHandler = (e, req, res) {
|
||||||
|
throw e.error as Object;
|
||||||
|
};
|
||||||
|
|
||||||
|
server = await AngelHttp(app).startServer();
|
||||||
|
url = 'http://${server.address.host}:${server.port}';
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() async {
|
||||||
|
await server.close(force: true);
|
||||||
|
client.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('listen before and after', () async {
|
||||||
|
var count = 0;
|
||||||
|
|
||||||
|
todoService
|
||||||
|
..beforeIndexed.listen((_) {
|
||||||
|
count++;
|
||||||
|
})
|
||||||
|
..afterIndexed.listen((_) {
|
||||||
|
count++;
|
||||||
|
});
|
||||||
|
|
||||||
|
var response = await client.get(Uri.parse('$url/todos'));
|
||||||
|
print(response.body);
|
||||||
|
expect(count, equals(2));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cancel before', () async {
|
||||||
|
todoService.beforeCreated
|
||||||
|
..listen((HookedServiceEvent event) {
|
||||||
|
event.cancel({'hello': 'hooked world'});
|
||||||
|
})
|
||||||
|
..listen((HookedServiceEvent event) {
|
||||||
|
event.cancel({'this_hook': 'should never run'});
|
||||||
|
});
|
||||||
|
|
||||||
|
var response = await client.post(Uri.parse('$url/todos'),
|
||||||
|
body: json.encode({'arbitrary': 'data'}),
|
||||||
|
headers: headers as Map<String, String>);
|
||||||
|
print(response.body);
|
||||||
|
var result = json.decode(response.body) as Map;
|
||||||
|
expect(result['hello'], equals('hooked world'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cancel after', () async {
|
||||||
|
todoService.afterIndexed
|
||||||
|
..listen((HookedServiceEvent event) async {
|
||||||
|
// Hooks can be Futures ;)
|
||||||
|
event.cancel([
|
||||||
|
{'angel': 'framework'}
|
||||||
|
]);
|
||||||
|
})
|
||||||
|
..listen((HookedServiceEvent event) {
|
||||||
|
event.cancel({'this_hook': 'should never run either'});
|
||||||
|
});
|
||||||
|
|
||||||
|
var response = await client.get(Uri.parse('$url/todos'));
|
||||||
|
print(response.body);
|
||||||
|
var result = json.decode(response.body) as List;
|
||||||
|
expect(result[0]['angel'], equals('framework'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('asStream() fires', () async {
|
||||||
|
var stream = todoService.afterCreated.asStream();
|
||||||
|
await todoService.create({'angel': 'framework'});
|
||||||
|
expect(await stream.first.then((e) => e.result['angel']), 'framework');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('metadata', () async {
|
||||||
|
final service = HookedService(IncrementService())..addHooks(app);
|
||||||
|
expect(service.inner, isNot(const IsInstanceOf<MapService>()));
|
||||||
|
IncrementService.times = 0;
|
||||||
|
await service.index();
|
||||||
|
expect(IncrementService.times, equals(2));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('inject request + response', () async {
|
||||||
|
var books = app.findService('books')
|
||||||
|
as HookedService<dynamic, dynamic, Service<dynamic, dynamic>>;
|
||||||
|
|
||||||
|
books.beforeIndexed.listen((e) {
|
||||||
|
expect([e.request, e.response], everyElement(isNotNull));
|
||||||
|
print('Indexing books at path: ${e.request?.path}');
|
||||||
|
});
|
||||||
|
|
||||||
|
var response = await client.get(Uri.parse('$url/books'));
|
||||||
|
print(response.body);
|
||||||
|
|
||||||
|
var result = json.decode(response.body);
|
||||||
|
expect(result, isList);
|
||||||
|
expect(result, isNotEmpty);
|
||||||
|
expect(result[0], equals({'foo': 'bar'}));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('contains provider in before and after', () async {
|
||||||
|
var svc = HookedService(AnonymousService(index: ([p]) async => []));
|
||||||
|
|
||||||
|
void ensureProviderIsPresent(HookedServiceEvent e) {
|
||||||
|
var type = e.isBefore ? 'before' : 'after';
|
||||||
|
print('Params to $type ${e.eventName}: ${e.params}');
|
||||||
|
expect(e.params, isMap);
|
||||||
|
expect(e.params.keys, contains('provider'));
|
||||||
|
expect(e.params['provider'], const IsInstanceOf<Providers>());
|
||||||
|
}
|
||||||
|
|
||||||
|
svc
|
||||||
|
..beforeAll(ensureProviderIsPresent)
|
||||||
|
..afterAll(ensureProviderIsPresent);
|
||||||
|
|
||||||
|
await svc.index({'provider': const Providers('testing')});
|
||||||
|
});
|
||||||
|
}
|
304
packages/framework/test/http2/adapter_test.dart
Normal file
304
packages/framework/test/http2/adapter_test.dart
Normal file
|
@ -0,0 +1,304 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io' hide BytesBuilder;
|
||||||
|
import 'dart:typed_data';
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart' hide Header;
|
||||||
|
import 'package:platform_framework/http2.dart';
|
||||||
|
import 'package:collection/collection.dart' show IterableExtension;
|
||||||
|
import 'package:http/src/multipart_file.dart' as http;
|
||||||
|
import 'package:http/src/multipart_request.dart' as http;
|
||||||
|
import 'package:http/io_client.dart';
|
||||||
|
import 'package:http2/transport.dart';
|
||||||
|
import 'package:http_parser/http_parser.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import 'http2_client.dart';
|
||||||
|
|
||||||
|
const String jfk =
|
||||||
|
'Ask not what your country can do for you, but what you can do for your country.';
|
||||||
|
|
||||||
|
Stream<List<int>> jfkStream() {
|
||||||
|
return Stream.fromIterable([utf8.encode(jfk)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
var client = Http2Client();
|
||||||
|
late IOClient h1c;
|
||||||
|
Angel app;
|
||||||
|
late AngelHttp2 http2;
|
||||||
|
late Uri serverRoot;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
app = Angel(reflector: MirrorsReflector())..encoders['gzip'] = gzip.encoder;
|
||||||
|
hierarchicalLoggingEnabled = true;
|
||||||
|
app.logger = Logger.detached('angel.http2')
|
||||||
|
..onRecord.listen((rec) {
|
||||||
|
print(rec);
|
||||||
|
if (rec.error == null) return;
|
||||||
|
print(rec.error);
|
||||||
|
if (rec.stackTrace != null) print(rec.stackTrace);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/', (req, res) async {
|
||||||
|
res.write('Hello world');
|
||||||
|
await res.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
app.all('/method', (req, res) => req.method);
|
||||||
|
|
||||||
|
app.get('/json', (_, __) => {'foo': 'bar'});
|
||||||
|
|
||||||
|
app.get('/stream', (req, res) => jfkStream().pipe(res));
|
||||||
|
|
||||||
|
app.get('/headers', (req, res) async {
|
||||||
|
res.headers.addAll({'foo': 'bar', 'x-angel': 'http2'});
|
||||||
|
await res.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/status', (req, res) async {
|
||||||
|
res.statusCode = 1337;
|
||||||
|
await res.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post('/body', (req, res) => req.parseBody().then((_) => req.bodyAsMap));
|
||||||
|
|
||||||
|
app.post('/upload', (req, res) async {
|
||||||
|
await req.parseBody();
|
||||||
|
var body = req.bodyAsMap;
|
||||||
|
var files = req.uploadedFiles ?? [];
|
||||||
|
|
||||||
|
var file = files.firstWhereOrNull((f) => f.name == 'file')!;
|
||||||
|
return [
|
||||||
|
await file.data.map((l) => l.length).reduce((a, b) => a + b),
|
||||||
|
file.contentType.mimeType,
|
||||||
|
body
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/push', (req, res) async {
|
||||||
|
res.write('ok');
|
||||||
|
|
||||||
|
if (res is Http2ResponseContext && res.canPush) {
|
||||||
|
var a = res.push('a')..write('a');
|
||||||
|
await a.close();
|
||||||
|
|
||||||
|
var b = res.push('b')..write('b');
|
||||||
|
await b.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
await res.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/param/:name', (req, res) => req.params);
|
||||||
|
|
||||||
|
app.get('/query', (req, res) {
|
||||||
|
print('incoming URI: ${req.uri}');
|
||||||
|
return req.queryParameters;
|
||||||
|
});
|
||||||
|
|
||||||
|
var ctx = SecurityContext()
|
||||||
|
..useCertificateChain('dev.pem')
|
||||||
|
..usePrivateKey('dev.key', password: 'dartdart')
|
||||||
|
..setAlpnProtocols(['h2'], true);
|
||||||
|
|
||||||
|
// Create an HTTP client that trusts our server.
|
||||||
|
h1c = IOClient(HttpClient()..badCertificateCallback = (_, __, ___) => true);
|
||||||
|
|
||||||
|
http2 = AngelHttp2(app, ctx, allowHttp1: true);
|
||||||
|
|
||||||
|
var server = await http2.startServer();
|
||||||
|
serverRoot = Uri.parse('https://127.0.0.1:${server.port}');
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() async {
|
||||||
|
await http2.close();
|
||||||
|
h1c.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('buffered response', () async {
|
||||||
|
var response = await client.get(serverRoot);
|
||||||
|
expect(response.body, 'Hello world');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('allowHttp1', () async {
|
||||||
|
var response = await h1c.get(serverRoot);
|
||||||
|
expect(response.body, 'Hello world');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('streamed response', () async {
|
||||||
|
var response = await client.get(serverRoot.replace(path: '/stream'));
|
||||||
|
expect(response.body, jfk);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('gzip', () {
|
||||||
|
test('buffered response', () async {
|
||||||
|
var response = await client
|
||||||
|
.get(serverRoot, headers: {'accept-encoding': 'gzip, deflate, br'});
|
||||||
|
expect(response.headers['content-encoding'], 'gzip');
|
||||||
|
var decoded = gzip.decode(response.bodyBytes);
|
||||||
|
expect(utf8.decode(decoded), 'Hello world');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('streamed response', () async {
|
||||||
|
var response = await client.get(serverRoot.replace(path: '/stream'),
|
||||||
|
headers: {'accept-encoding': 'gzip'});
|
||||||
|
expect(response.headers['content-encoding'], 'gzip');
|
||||||
|
//print(response.body);
|
||||||
|
var decoded = gzip.decode(response.bodyBytes);
|
||||||
|
expect(utf8.decode(decoded), jfk);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('query uri decoded', () async {
|
||||||
|
var uri =
|
||||||
|
serverRoot.replace(path: '/query', queryParameters: {'foo!': 'bar?'});
|
||||||
|
var response = await client.get(uri);
|
||||||
|
print('Sent $uri');
|
||||||
|
expect(response.body, json.encode({'foo!': 'bar?'}));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('params uri decoded', () async {
|
||||||
|
var response = await client.get(serverRoot.replace(path: '/param/foo!'));
|
||||||
|
expect(response.body, json.encode({'name': 'foo!'}));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('method parsed', () async {
|
||||||
|
var response = await client.delete(serverRoot.replace(path: '/method'));
|
||||||
|
expect(response.body, json.encode('DELETE'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('json response', () async {
|
||||||
|
var response = await client.get(serverRoot.replace(path: '/json'));
|
||||||
|
expect(response.body, json.encode({'foo': 'bar'}));
|
||||||
|
expect(ContentType.parse(response.headers['content-type']!).mimeType,
|
||||||
|
ContentType.json.mimeType);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('status sent', () async {
|
||||||
|
var response = await client.get(serverRoot.replace(path: '/status'));
|
||||||
|
expect(response.statusCode, 1337);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('headers sent', () async {
|
||||||
|
var response = await client.get(serverRoot.replace(path: '/headers'));
|
||||||
|
expect(response.headers['foo'], 'bar');
|
||||||
|
expect(response.headers['x-angel'], 'http2');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('server push', () async {
|
||||||
|
var socket = await SecureSocket.connect(
|
||||||
|
serverRoot.host,
|
||||||
|
serverRoot.port,
|
||||||
|
onBadCertificate: (_) => true,
|
||||||
|
supportedProtocols: ['h2'],
|
||||||
|
);
|
||||||
|
|
||||||
|
var connection = ClientTransportConnection.viaSocket(
|
||||||
|
socket,
|
||||||
|
settings: ClientSettings(allowServerPushes: true),
|
||||||
|
);
|
||||||
|
|
||||||
|
var headers = <Header>[
|
||||||
|
Header.ascii(':authority', serverRoot.authority),
|
||||||
|
Header.ascii(':method', 'GET'),
|
||||||
|
Header.ascii(':path', serverRoot.replace(path: '/push').path),
|
||||||
|
Header.ascii(':scheme', serverRoot.scheme),
|
||||||
|
];
|
||||||
|
|
||||||
|
var stream = connection.makeRequest(headers, endStream: true);
|
||||||
|
|
||||||
|
var bb = await stream.incomingMessages
|
||||||
|
.where((s) => s is DataStreamMessage)
|
||||||
|
.cast<DataStreamMessage>()
|
||||||
|
.fold<BytesBuilder>(BytesBuilder(), (out, msg) => out..add(msg.bytes));
|
||||||
|
|
||||||
|
// Check that main body was sent
|
||||||
|
expect(utf8.decode(bb.takeBytes()), 'ok');
|
||||||
|
|
||||||
|
var pushes = await stream.peerPushes.toList();
|
||||||
|
expect(pushes, hasLength(2));
|
||||||
|
|
||||||
|
var pushA = pushes[0], pushB = pushes[1];
|
||||||
|
|
||||||
|
String getPath(TransportStreamPush p) => ascii.decode(p.requestHeaders
|
||||||
|
.firstWhere((h) => ascii.decode(h.name) == ':path')
|
||||||
|
.value);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Future<String> getBody(ClientTransportStream stream) async {
|
||||||
|
await stream.outgoingMessages.close();
|
||||||
|
var bb = await stream.incomingMessages
|
||||||
|
.map((s) {
|
||||||
|
if (s is HeadersStreamMessage) {
|
||||||
|
for (var h in s.headers) {
|
||||||
|
print('${ASCII.decode(h.name)}: ${ASCII.decode(h.value)}');
|
||||||
|
}
|
||||||
|
} else if (s is DataStreamMessage) {
|
||||||
|
print(UTF8.decode(s.bytes));
|
||||||
|
}
|
||||||
|
|
||||||
|
return s;
|
||||||
|
})
|
||||||
|
.where((s) => s is DataStreamMessage)
|
||||||
|
.cast<DataStreamMessage>()
|
||||||
|
.fold<BytesBuilder>(
|
||||||
|
BytesBuilder(), (out, msg) => out..add(msg.bytes));
|
||||||
|
return UTF8.decode(bb.takeBytes());
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
expect(getPath(pushA), '/a');
|
||||||
|
expect(getPath(pushB), '/b');
|
||||||
|
|
||||||
|
// However, Chrome, Firefox, Edge all can
|
||||||
|
//expect(await getBody(pushA.stream), 'a');
|
||||||
|
//expect(await getBody(pushB.stream), 'b');
|
||||||
|
});
|
||||||
|
|
||||||
|
group('body parsing', () {
|
||||||
|
test('urlencoded body parsed', () async {
|
||||||
|
var response = await client.post(
|
||||||
|
serverRoot.replace(path: '/body'),
|
||||||
|
headers: {
|
||||||
|
'accept': 'application/json',
|
||||||
|
'content-type': 'application/x-www-form-urlencoded'
|
||||||
|
},
|
||||||
|
body: 'foo=bar',
|
||||||
|
);
|
||||||
|
expect(response.body, json.encode({'foo': 'bar'}));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('json body parsed', () async {
|
||||||
|
var response = await client.post(serverRoot.replace(path: '/body'),
|
||||||
|
headers: {
|
||||||
|
'accept': 'application/json',
|
||||||
|
'content-type': 'application/json'
|
||||||
|
},
|
||||||
|
body: json.encode({'foo': 'bar'}));
|
||||||
|
expect(response.body, json.encode({'foo': 'bar'}));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('multipart body parsed', () async {
|
||||||
|
var rq =
|
||||||
|
http.MultipartRequest('POST', serverRoot.replace(path: '/upload'));
|
||||||
|
rq.headers.addAll({'accept': 'application/json'});
|
||||||
|
|
||||||
|
rq.fields['foo'] = 'bar';
|
||||||
|
rq.files.add(http.MultipartFile(
|
||||||
|
'file', Stream.fromIterable([utf8.encode('hello world')]), 11,
|
||||||
|
contentType: MediaType('angel', 'framework')));
|
||||||
|
|
||||||
|
var response = await client.send(rq);
|
||||||
|
var responseBody = await response.stream.transform(utf8.decoder).join();
|
||||||
|
|
||||||
|
expect(
|
||||||
|
responseBody,
|
||||||
|
json.encode([
|
||||||
|
11,
|
||||||
|
'angel/framework',
|
||||||
|
{'foo': 'bar'}
|
||||||
|
]));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
105
packages/framework/test/http2/http2_client.dart
Normal file
105
packages/framework/test/http2/http2_client.dart
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io' hide BytesBuilder;
|
||||||
|
import 'dart:typed_data';
|
||||||
|
import 'package:http/http.dart';
|
||||||
|
import 'package:http2/transport.dart';
|
||||||
|
|
||||||
|
/// Simple HTTP/2 client
|
||||||
|
class Http2Client extends BaseClient {
|
||||||
|
static Future<ClientTransportStream> convertRequestToStream(
|
||||||
|
BaseRequest request) async {
|
||||||
|
// Connect a socket
|
||||||
|
var socket = await SecureSocket.connect(
|
||||||
|
request.url.host,
|
||||||
|
request.url.port,
|
||||||
|
onBadCertificate: (_) => true,
|
||||||
|
supportedProtocols: ['h2'],
|
||||||
|
);
|
||||||
|
|
||||||
|
var connection = ClientTransportConnection.viaSocket(socket);
|
||||||
|
|
||||||
|
var headers = <Header>[
|
||||||
|
Header.ascii(':authority', request.url.authority),
|
||||||
|
Header.ascii(':method', request.method),
|
||||||
|
Header.ascii(
|
||||||
|
':path',
|
||||||
|
request.url.path +
|
||||||
|
(request.url.hasQuery ? ('?${request.url.query}') : '')),
|
||||||
|
Header.ascii(':scheme', request.url.scheme),
|
||||||
|
];
|
||||||
|
|
||||||
|
var bb = await request
|
||||||
|
.finalize()
|
||||||
|
.fold<BytesBuilder>(BytesBuilder(), (out, list) => out..add(list));
|
||||||
|
var body = bb.takeBytes();
|
||||||
|
|
||||||
|
if (body.isNotEmpty) {
|
||||||
|
headers.add(Header.ascii('content-length', body.length.toString()));
|
||||||
|
}
|
||||||
|
|
||||||
|
request.headers.forEach((k, v) {
|
||||||
|
headers.add(Header.ascii(k, v));
|
||||||
|
});
|
||||||
|
|
||||||
|
var stream = connection.makeRequest(headers, endStream: body.isEmpty);
|
||||||
|
|
||||||
|
if (body.isNotEmpty) {
|
||||||
|
stream.sendData(body, endStream: true);
|
||||||
|
} else {
|
||||||
|
await (stream.outgoingMessages.close());
|
||||||
|
}
|
||||||
|
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if the response stream was closed.
|
||||||
|
static Future<bool> readResponse(ClientTransportStream stream,
|
||||||
|
Map<String, String> headers, BytesBuilder body) {
|
||||||
|
var c = Completer<bool>();
|
||||||
|
var closed = false;
|
||||||
|
|
||||||
|
stream.incomingMessages.listen(
|
||||||
|
(msg) {
|
||||||
|
if (msg is HeadersStreamMessage) {
|
||||||
|
for (var header in msg.headers) {
|
||||||
|
var name = ascii.decode(header.name).toLowerCase(),
|
||||||
|
value = ascii.decode(header.value);
|
||||||
|
headers[name] = value;
|
||||||
|
//print('$name: $value');
|
||||||
|
}
|
||||||
|
} else if (msg is DataStreamMessage) {
|
||||||
|
body.add(msg.bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!closed && msg.endStream) closed = true;
|
||||||
|
},
|
||||||
|
cancelOnError: true,
|
||||||
|
onError: c.completeError,
|
||||||
|
onDone: () => c.complete(closed),
|
||||||
|
);
|
||||||
|
|
||||||
|
return c.future;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<StreamedResponse> send(BaseRequest request) async {
|
||||||
|
var stream = await convertRequestToStream(request);
|
||||||
|
var headers = <String, String>{};
|
||||||
|
var body = BytesBuilder();
|
||||||
|
var closed = await readResponse(stream, headers, body);
|
||||||
|
return StreamedResponse(
|
||||||
|
Stream.fromIterable([body.takeBytes()]),
|
||||||
|
int.parse(headers[':status']!),
|
||||||
|
headers: headers,
|
||||||
|
isRedirect: headers.containsKey('location'),
|
||||||
|
contentLength: headers.containsKey('content-length')
|
||||||
|
? int.parse(headers['content-length']!)
|
||||||
|
: null,
|
||||||
|
request: request,
|
||||||
|
reasonPhrase: null,
|
||||||
|
// doesn't exist in HTTP/2
|
||||||
|
persistentConnection: !closed,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
80
packages/framework/test/http_404_hole_test.dart
Normal file
80
packages/framework/test/http_404_hole_test.dart
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:charcode/ascii.dart';
|
||||||
|
import 'package:http/io_client.dart' as http;
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import 'pretty_log.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late http.IOClient client;
|
||||||
|
late AngelHttp driver;
|
||||||
|
late Logger logger;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
client = http.IOClient();
|
||||||
|
hierarchicalLoggingEnabled = true;
|
||||||
|
|
||||||
|
logger = Logger.detached('http_404_hole')
|
||||||
|
..level = Level.ALL
|
||||||
|
..onRecord.listen(prettyLog);
|
||||||
|
|
||||||
|
var app = Angel(logger: logger);
|
||||||
|
|
||||||
|
app.fallback(hello);
|
||||||
|
app.fallback(throw404);
|
||||||
|
|
||||||
|
// The error handler in the boilerplate.
|
||||||
|
var oldErrorHandler = app.errorHandler;
|
||||||
|
app.errorHandler = (e, req, res) async {
|
||||||
|
if (req.accepts('text/html', strict: true)) {
|
||||||
|
if (e.statusCode == 404 && req.accepts('text/html', strict: true)) {
|
||||||
|
await res
|
||||||
|
.render('error', {'message': 'No file exists at ${req.uri}.'});
|
||||||
|
} else {
|
||||||
|
await res.render('error', {'message': e.message});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return await oldErrorHandler(e, req, res);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
driver = AngelHttp(app);
|
||||||
|
await driver.startServer();
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() {
|
||||||
|
logger.clearListeners();
|
||||||
|
client.close();
|
||||||
|
scheduleMicrotask(driver.close);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('does not continue processing after streaming', () async {
|
||||||
|
var url = driver.uri.replace(path: '/hey');
|
||||||
|
for (var i = 0; i < 100; i++) {
|
||||||
|
var r = await client.get(url);
|
||||||
|
print('#$i: ${r.statusCode}: ${r.body}');
|
||||||
|
expect(r.statusCode, 200);
|
||||||
|
expect(r.body, 'Hello!');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Simulate angel_static
|
||||||
|
Future<void> hello(RequestContext req, ResponseContext res) {
|
||||||
|
if (req.path == 'hey') {
|
||||||
|
var bytes = [$H, $e, $l, $l, $o, $exclamation];
|
||||||
|
var s = Stream<List<int>>.fromIterable([bytes]);
|
||||||
|
return s.pipe(res);
|
||||||
|
} else {
|
||||||
|
return Future.value();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 404
|
||||||
|
void throw404(RequestContext req, ResponseContext res) {
|
||||||
|
Zone.current
|
||||||
|
.handleUncaughtError('This 404 should not occur.', StackTrace.current);
|
||||||
|
throw HttpException.notFound();
|
||||||
|
}
|
57
packages/framework/test/jsonp_test.dart
Normal file
57
packages/framework/test/jsonp_test.dart
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:http_parser/http_parser.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
var app = Angel();
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
|
||||||
|
app.get('/default', (req, res) => res.jsonp({'foo': 'bar'}));
|
||||||
|
|
||||||
|
app.get('/callback',
|
||||||
|
(req, res) => res.jsonp({'foo': 'bar'}, callbackName: 'doIt'));
|
||||||
|
|
||||||
|
app.get(
|
||||||
|
'/contentType',
|
||||||
|
(req, res) =>
|
||||||
|
res.jsonp({'foo': 'bar'}, contentType: MediaType('foo', 'bar')));
|
||||||
|
|
||||||
|
Future<MediaType> getContentType(String path) async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri(path: '/$path'));
|
||||||
|
await rq.close();
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
return MediaType.parse(rq.response.headers.contentType.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String> getText(String path) async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri(path: '/$path'));
|
||||||
|
await rq.close();
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
return await rq.response.transform(utf8.decoder).join();
|
||||||
|
}
|
||||||
|
|
||||||
|
test('default', () async {
|
||||||
|
var response = await getText('default');
|
||||||
|
var contentType = await getContentType('default');
|
||||||
|
expect(response, r'callback({"foo":"bar"})');
|
||||||
|
expect(contentType.mimeType, 'application/javascript');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('callback', () async {
|
||||||
|
var response = await getText('callback');
|
||||||
|
var contentType = await getContentType('callback');
|
||||||
|
expect(response, r'doIt({"foo":"bar"})');
|
||||||
|
expect(contentType.mimeType, 'application/javascript');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('content type', () async {
|
||||||
|
var response = await getText('contentType');
|
||||||
|
var contentType = await getContentType('contentType');
|
||||||
|
expect(response, r'callback({"foo":"bar"})');
|
||||||
|
expect(contentType.mimeType, 'foo/bar');
|
||||||
|
});
|
||||||
|
}
|
150
packages/framework/test/parameter_meta_test.dart
Normal file
150
packages/framework/test/parameter_meta_test.dart
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
Future<String> readResponse(MockHttpResponse rs) {
|
||||||
|
return rs.transform(utf8.decoder).join();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future printResponse(MockHttpResponse rs) {
|
||||||
|
return readResponse(rs).then((text) {
|
||||||
|
print(text.isEmpty ? '<empty response>' : text);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('parameter_meta', parameterMetaTests);
|
||||||
|
}
|
||||||
|
|
||||||
|
void parameterMetaTests() {
|
||||||
|
Angel app;
|
||||||
|
late AngelHttp http;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
app = Angel(reflector: MirrorsReflector());
|
||||||
|
http = AngelHttp(app);
|
||||||
|
|
||||||
|
app.get('/cookie', ioc((@CookieValue('token') String jwt) {
|
||||||
|
return jwt;
|
||||||
|
}));
|
||||||
|
|
||||||
|
app.get('/header', ioc((@Header('x-foo') String header) {
|
||||||
|
return header;
|
||||||
|
}));
|
||||||
|
|
||||||
|
app.get('/query', ioc((@Query('q') String query) {
|
||||||
|
return query;
|
||||||
|
}));
|
||||||
|
|
||||||
|
app.get('/session', ioc((@Session('foo') String foo) {
|
||||||
|
return foo;
|
||||||
|
}));
|
||||||
|
|
||||||
|
app.get('/match', ioc((@Query('mode', match: 'pos') String mode) {
|
||||||
|
return 'YES $mode';
|
||||||
|
}));
|
||||||
|
|
||||||
|
app.get('/match', ioc((@Query('mode', match: 'neg') String mode) {
|
||||||
|
return 'NO $mode';
|
||||||
|
}));
|
||||||
|
|
||||||
|
app.get('/match', ioc((@Query('mode') String mode) {
|
||||||
|
return 'DEFAULT $mode';
|
||||||
|
}));
|
||||||
|
|
||||||
|
app.logger = Logger('parameter_meta_test')
|
||||||
|
..onRecord.listen((rec) {
|
||||||
|
print(rec);
|
||||||
|
if (rec.error != null) print(rec.error);
|
||||||
|
if (rec.stackTrace != null) print(rec.stackTrace);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('injects header or throws', () async {
|
||||||
|
// Invalid request
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/header'));
|
||||||
|
await rq.close();
|
||||||
|
var rs = rq.response;
|
||||||
|
//TODO: Using await will hang. To be resolved.
|
||||||
|
http.handleRequest(rq);
|
||||||
|
|
||||||
|
await printResponse(rs);
|
||||||
|
expect(rs.statusCode, 400);
|
||||||
|
|
||||||
|
// Valid request
|
||||||
|
rq = MockHttpRequest('GET', Uri.parse('/header'))
|
||||||
|
..headers.add('x-foo', 'bar');
|
||||||
|
await rq.close();
|
||||||
|
rs = rq.response;
|
||||||
|
http.handleRequest(rq);
|
||||||
|
|
||||||
|
var body = await readResponse(rs);
|
||||||
|
print('Body: $body');
|
||||||
|
expect(rs.statusCode, 200);
|
||||||
|
expect(body, json.encode('bar'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('injects session or throws', () async {
|
||||||
|
// Invalid request
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/session'));
|
||||||
|
await rq.close();
|
||||||
|
var rs = rq.response;
|
||||||
|
http
|
||||||
|
.handleRequest(rq)
|
||||||
|
.timeout(const Duration(seconds: 5))
|
||||||
|
.catchError((_) => null);
|
||||||
|
|
||||||
|
await printResponse(rs);
|
||||||
|
expect(rs.statusCode, 500);
|
||||||
|
|
||||||
|
rq = MockHttpRequest('GET', Uri.parse('/session'));
|
||||||
|
rq.session['foo'] = 'bar';
|
||||||
|
await rq.close();
|
||||||
|
rs = rq.response;
|
||||||
|
http.handleRequest(rq);
|
||||||
|
|
||||||
|
await printResponse(rs);
|
||||||
|
expect(rs.statusCode, 200);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Originally, the plan was to test cookie, session, header, etc.,
|
||||||
|
// but that behavior has been consolidated into `getValue`. Thus,
|
||||||
|
// they will all function the same way.
|
||||||
|
|
||||||
|
test('pattern matching', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/match?mode=pos'));
|
||||||
|
await rq.close();
|
||||||
|
var rs = rq.response;
|
||||||
|
http.handleRequest(rq);
|
||||||
|
var body = await readResponse(rs);
|
||||||
|
print('Body: $body');
|
||||||
|
expect(rs.statusCode, 200);
|
||||||
|
expect(body, json.encode('YES pos'));
|
||||||
|
|
||||||
|
rq = MockHttpRequest('GET', Uri.parse('/match?mode=neg'));
|
||||||
|
await rq.close();
|
||||||
|
rs = rq.response;
|
||||||
|
http.handleRequest(rq);
|
||||||
|
body = await readResponse(rs);
|
||||||
|
print('Body: $body');
|
||||||
|
expect(rs.statusCode, 200);
|
||||||
|
expect(body, json.encode('NO neg'));
|
||||||
|
|
||||||
|
// Fallback
|
||||||
|
rq = MockHttpRequest('GET', Uri.parse('/match?mode=ambi'));
|
||||||
|
await rq.close();
|
||||||
|
rs = rq.response;
|
||||||
|
http.handleRequest(rq);
|
||||||
|
body = await readResponse(rs);
|
||||||
|
print('Body: $body');
|
||||||
|
expect(rs.statusCode, 200);
|
||||||
|
expect(body, json.encode('DEFAULT ambi'));
|
||||||
|
});
|
||||||
|
}
|
33
packages/framework/test/parse_id_test.dart
Normal file
33
packages/framework/test/parse_id_test.dart
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('null', () {
|
||||||
|
expect(Service.parseId('null'), 'null');
|
||||||
|
expect(Service.parseId(null), 'null');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('String', () {
|
||||||
|
expect(Service.parseId('23'), '23');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('int', () {
|
||||||
|
expect(Service.parseId<int>('23'), 23);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('double', () {
|
||||||
|
expect(Service.parseId<double>('23.4'), 23.4);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('num', () {
|
||||||
|
expect(Service.parseId<num>('23.4'), 23.4);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('bool', () {
|
||||||
|
expect(Service.parseId<bool>('true'), true);
|
||||||
|
expect(Service.parseId<bool>(true), true);
|
||||||
|
expect(Service.parseId<bool>('false'), false);
|
||||||
|
expect(Service.parseId<bool>(false), false);
|
||||||
|
expect(Service.parseId<bool>('hmm'), false);
|
||||||
|
});
|
||||||
|
}
|
28
packages/framework/test/precontained_test.dart
Normal file
28
packages/framework/test/precontained_test.dart
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('preinjects functions', () async {
|
||||||
|
var app = Angel(reflector: MirrorsReflector())
|
||||||
|
..configuration['foo'] = 'bar'
|
||||||
|
..get('/foo', ioc(echoAppFoo));
|
||||||
|
app.optimizeForProduction(force: true);
|
||||||
|
print(app.preContained);
|
||||||
|
expect(app.preContained.keys, contains(echoAppFoo));
|
||||||
|
|
||||||
|
var rq = MockHttpRequest('GET', Uri(path: '/foo'));
|
||||||
|
await rq.close();
|
||||||
|
await AngelHttp(app).handleRequest(rq);
|
||||||
|
var rs = rq.response;
|
||||||
|
var body = await rs.transform(utf8.decoder).join();
|
||||||
|
expect(body, json.encode('bar'));
|
||||||
|
}, skip: 'Angel no longer has to preinject functions');
|
||||||
|
}
|
||||||
|
|
||||||
|
String echoAppFoo(String foo) => foo;
|
38
packages/framework/test/pretty_log.dart
Normal file
38
packages/framework/test/pretty_log.dart
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:io/ansi.dart';
|
||||||
|
|
||||||
|
/// Prints the contents of a [LogRecord] with pretty colors.
|
||||||
|
void prettyLog(LogRecord record) {
|
||||||
|
var code = chooseLogColor(record.level);
|
||||||
|
|
||||||
|
if (record.error == null) print(code.wrap(record.toString()));
|
||||||
|
|
||||||
|
if (record.error != null) {
|
||||||
|
var err = record.error;
|
||||||
|
print(code.wrap('$record\n'));
|
||||||
|
print(code.wrap(err.toString()));
|
||||||
|
|
||||||
|
if (record.stackTrace != null) {
|
||||||
|
print(code.wrap(record.stackTrace.toString()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Chooses a color based on the logger [level].
|
||||||
|
AnsiCode chooseLogColor(Level level) {
|
||||||
|
if (level == Level.SHOUT) {
|
||||||
|
return backgroundRed;
|
||||||
|
} else if (level == Level.SEVERE) {
|
||||||
|
return red;
|
||||||
|
} else if (level == Level.WARNING) {
|
||||||
|
return yellow;
|
||||||
|
} else if (level == Level.INFO) {
|
||||||
|
return cyan;
|
||||||
|
} else if (level == Level.CONFIG ||
|
||||||
|
level == Level.FINE ||
|
||||||
|
level == Level.FINER ||
|
||||||
|
level == Level.FINEST) {
|
||||||
|
return lightGray;
|
||||||
|
}
|
||||||
|
return resetAll;
|
||||||
|
}
|
75
packages/framework/test/primitives_test.dart
Normal file
75
packages/framework/test/primitives_test.dart
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io' show stderr;
|
||||||
|
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late Angel app;
|
||||||
|
late AngelHttp http;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
app = Angel(reflector: MirrorsReflector())
|
||||||
|
..configuration['global'] = 305; // Pitbull!
|
||||||
|
http = AngelHttp(app);
|
||||||
|
|
||||||
|
app.get('/string/:string', ioc((String string) => string));
|
||||||
|
|
||||||
|
app.get(
|
||||||
|
'/num/parsed/:num',
|
||||||
|
chain([
|
||||||
|
(req, res) {
|
||||||
|
req.params['n'] = num.parse(req.params['num'].toString());
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
ioc((num n) => n),
|
||||||
|
]));
|
||||||
|
|
||||||
|
app.get('/num/global', ioc((num global) => global));
|
||||||
|
|
||||||
|
app.errorHandler = (e, req, res) {
|
||||||
|
stderr
|
||||||
|
..writeln(e.error)
|
||||||
|
..writeln(e.stackTrace);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() => app.close());
|
||||||
|
|
||||||
|
test('String type annotation', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/string/hello'));
|
||||||
|
await rq.close();
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
var rs = await rq.response.transform(utf8.decoder).join();
|
||||||
|
expect(rs, json.encode('hello'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Primitive after parsed param injection', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/num/parsed/24'));
|
||||||
|
await rq.close();
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
var rs = await rq.response.transform(utf8.decoder).join();
|
||||||
|
expect(rs, json.encode(24));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('globally-injected primitive', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/num/global'));
|
||||||
|
await rq.close();
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
var rs = await rq.response.transform(utf8.decoder).join();
|
||||||
|
expect(rs, json.encode(305));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('unparsed primitive throws error', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/num/unparsed/32'));
|
||||||
|
await rq.close();
|
||||||
|
var req = await http.createRequestContext(rq, rq.response);
|
||||||
|
var res = await http.createResponseContext(rq, rq.response, req);
|
||||||
|
expect(() => app.runContained((num unparsed) => unparsed, req, res),
|
||||||
|
throwsA(isA<ArgumentError>()));
|
||||||
|
});
|
||||||
|
}
|
33
packages/framework/test/repeat_request_test.dart
Normal file
33
packages/framework/test/repeat_request_test.dart
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
MockHttpRequest mk(int id) {
|
||||||
|
return MockHttpRequest('GET', Uri.parse('/test/$id'))..close();
|
||||||
|
}
|
||||||
|
|
||||||
|
test('can request the same url twice', () async {
|
||||||
|
var app = Angel(reflector: MirrorsReflector())
|
||||||
|
..get('/test/:id', ioc((id) => 'Hello $id'));
|
||||||
|
var rq1 = mk(1), rq2 = mk(2), rq3 = mk(1);
|
||||||
|
await Future.wait([rq1, rq2, rq3].map(AngelHttp(app).handleRequest));
|
||||||
|
var body1 = await rq1.response.transform(utf8.decoder).join(),
|
||||||
|
body2 = await rq2.response.transform(utf8.decoder).join(),
|
||||||
|
body3 = await rq3.response.transform(utf8.decoder).join();
|
||||||
|
print('Response #1: $body1');
|
||||||
|
print('Response #2: $body2');
|
||||||
|
print('Response #3: $body3');
|
||||||
|
expect(
|
||||||
|
body1,
|
||||||
|
allOf(
|
||||||
|
isNot(body2),
|
||||||
|
equals(body3),
|
||||||
|
));
|
||||||
|
});
|
||||||
|
}
|
46
packages/framework/test/req_shutdown_test.dart
Normal file
46
packages/framework/test/req_shutdown_test.dart
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:http/io_client.dart' as http;
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import 'pretty_log.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late http.IOClient client;
|
||||||
|
late AngelHttp driver;
|
||||||
|
late Logger logger;
|
||||||
|
late StringBuffer buf;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
buf = StringBuffer();
|
||||||
|
client = http.IOClient();
|
||||||
|
hierarchicalLoggingEnabled = true;
|
||||||
|
|
||||||
|
logger = Logger.detached('req_shutdown')
|
||||||
|
..level = Level.ALL
|
||||||
|
..onRecord.listen(prettyLog);
|
||||||
|
|
||||||
|
var app = Angel(logger: logger);
|
||||||
|
|
||||||
|
app.fallback((req, res) {
|
||||||
|
req.shutdownHooks.add(() => buf.write('Hello, '));
|
||||||
|
req.shutdownHooks.add(() => buf.write('world!'));
|
||||||
|
});
|
||||||
|
|
||||||
|
driver = AngelHttp(app);
|
||||||
|
await driver.startServer();
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() {
|
||||||
|
logger.clearListeners();
|
||||||
|
client.close();
|
||||||
|
scheduleMicrotask(driver.close);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('does not continue processing after streaming', () async {
|
||||||
|
await client.get(driver.uri);
|
||||||
|
await Future.delayed(Duration(milliseconds: 100));
|
||||||
|
expect(buf.toString(), 'Hello, world!');
|
||||||
|
});
|
||||||
|
}
|
58
packages/framework/test/response_header_test.dart
Normal file
58
packages/framework/test/response_header_test.dart
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/src/http/angel_http.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late Angel app;
|
||||||
|
late AngelHttp http;
|
||||||
|
late HttpClient client;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
app = Angel(reflector: MirrorsReflector());
|
||||||
|
http = AngelHttp(app);
|
||||||
|
|
||||||
|
await http.startServer();
|
||||||
|
|
||||||
|
var formData = {'id': 100, 'name': 'William'};
|
||||||
|
app.get('/api/v1/user/list', (RequestContext req, res) async {
|
||||||
|
//await req.parseBody();
|
||||||
|
//res.write('Hello, World!');
|
||||||
|
res.json(formData);
|
||||||
|
});
|
||||||
|
|
||||||
|
client = HttpClient();
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() async {
|
||||||
|
client.close();
|
||||||
|
await http.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Remove Response Header', () async {
|
||||||
|
http.removeResponseHeader({'x-frame-options': 'SAMEORIGIN'});
|
||||||
|
|
||||||
|
var request = await client.get('localhost', 3000, '/api/v1/user/list');
|
||||||
|
HttpClientResponse response = await request.close();
|
||||||
|
//print(response.headers);
|
||||||
|
expect(response.headers['x-frame-options'], isNull);
|
||||||
|
}, skip: true);
|
||||||
|
|
||||||
|
test('Add Response Header', () async {
|
||||||
|
http.addResponseHeader({
|
||||||
|
'X-XSRF_TOKEN':
|
||||||
|
'a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e'
|
||||||
|
});
|
||||||
|
|
||||||
|
var request = await client.get('localhost', 3000, '/api/v1/user/list');
|
||||||
|
HttpClientResponse response = await request.close();
|
||||||
|
//print(response.headers);
|
||||||
|
expect(
|
||||||
|
response.headers['X-XSRF_TOKEN'],
|
||||||
|
equals([
|
||||||
|
'a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e'
|
||||||
|
]));
|
||||||
|
}, skip: true);
|
||||||
|
}
|
223
packages/framework/test/routing_test.dart
Normal file
223
packages/framework/test/routing_test.dart
Normal file
|
@ -0,0 +1,223 @@
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:io/ansi.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
import 'common.dart';
|
||||||
|
|
||||||
|
@Middleware([interceptor])
|
||||||
|
Future<String> testMiddlewareMetadata(
|
||||||
|
RequestContext req, ResponseContext res) async {
|
||||||
|
return 'This should not be shown.';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Middleware([interceptService])
|
||||||
|
class QueryService extends Service {
|
||||||
|
@override
|
||||||
|
@Middleware([interceptor])
|
||||||
|
Future<Map?> read(id, [Map? params]) async => params;
|
||||||
|
}
|
||||||
|
|
||||||
|
void interceptor(RequestContext req, ResponseContext res) {
|
||||||
|
res
|
||||||
|
..write('Middleware')
|
||||||
|
..close();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool interceptService(RequestContext req, ResponseContext res) {
|
||||||
|
res.write('Service with ');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late Angel app;
|
||||||
|
late Angel nested;
|
||||||
|
late Angel todos;
|
||||||
|
late String url;
|
||||||
|
late http.Client client;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
app = Angel(reflector: MirrorsReflector());
|
||||||
|
nested = Angel(reflector: MirrorsReflector());
|
||||||
|
todos = Angel(reflector: MirrorsReflector());
|
||||||
|
|
||||||
|
for (var app in [app, nested, todos]) {
|
||||||
|
app.logger = Logger('routing_test')
|
||||||
|
..onRecord.listen((rec) {
|
||||||
|
if (rec.error != null) {
|
||||||
|
stdout
|
||||||
|
..writeln(cyan.wrap(rec.toString()))
|
||||||
|
..writeln(cyan.wrap(rec.error.toString()))
|
||||||
|
..writeln(cyan.wrap(rec.stackTrace.toString()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
todos.get('/action/:action', (req, res) => res.json(req.params));
|
||||||
|
|
||||||
|
late Route ted;
|
||||||
|
|
||||||
|
ted = nested.post('/ted/:route', (RequestContext req, res) {
|
||||||
|
print('Params: ${req.params}');
|
||||||
|
print('Path: ${ted.path}, uri: ${req.path}');
|
||||||
|
print('matcher: ${ted.parser}');
|
||||||
|
return req.params;
|
||||||
|
});
|
||||||
|
|
||||||
|
app.mount('/nes', nested);
|
||||||
|
app.get('/meta', testMiddlewareMetadata);
|
||||||
|
app.get('/intercepted', (req, res) => 'This should not be shown',
|
||||||
|
middleware: [interceptor]);
|
||||||
|
app.get('/hello', (req, res) => 'world');
|
||||||
|
app.get('/name/:first/last/:last', (req, res) => req.params);
|
||||||
|
app.post(
|
||||||
|
'/lambda',
|
||||||
|
(RequestContext req, res) =>
|
||||||
|
req.parseBody().then((_) => req.bodyAsMap));
|
||||||
|
app.mount('/todos/:id', todos);
|
||||||
|
app
|
||||||
|
.get('/greet/:name',
|
||||||
|
(RequestContext req, res) async => "Hello ${req.params['name']}")
|
||||||
|
.name = 'Named routes';
|
||||||
|
app.get('/named', (req, ResponseContext res) async {
|
||||||
|
await res.redirectTo('Named routes', {'name': 'tests'});
|
||||||
|
});
|
||||||
|
app.get('/log', (RequestContext req, res) async {
|
||||||
|
print('Query: ${req.queryParameters}');
|
||||||
|
return 'Logged';
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/method', (req, res) => 'Only GET');
|
||||||
|
app.post('/method', (req, res) => 'Only POST');
|
||||||
|
|
||||||
|
app.use('/query', QueryService());
|
||||||
|
|
||||||
|
RequestHandler write(String message) {
|
||||||
|
return (req, res) {
|
||||||
|
res.write(message);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
app.chain([write('a')]).chain([write('b'), write('c')]).get(
|
||||||
|
'/chained', (req, res) => res.close());
|
||||||
|
|
||||||
|
app.fallback((req, res) => 'MJ');
|
||||||
|
|
||||||
|
//app.dumpTree(header: "DUMPING ROUTES:", showMatchers: true);
|
||||||
|
|
||||||
|
client = http.Client();
|
||||||
|
var server = await AngelHttp(app).startServer('127.0.0.1', 0);
|
||||||
|
url = 'http://${server.address.host}:${server.port}';
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() async {
|
||||||
|
await app.close();
|
||||||
|
client.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Can match basic url', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/hello'));
|
||||||
|
expect(response.body, equals('"world"'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Can match url with multiple parameters', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/name/HELLO/last/WORLD'));
|
||||||
|
print('Response: ${response.body}');
|
||||||
|
var json_ = json.decode(response.body);
|
||||||
|
expect(json_, const IsInstanceOf<Map>());
|
||||||
|
expect(json_['first'], equals('HELLO'));
|
||||||
|
expect(json_['last'], equals('WORLD'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Chained routes', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/chained'));
|
||||||
|
expect(response.body, equals('abc'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Can nest another Angel instance', () async {
|
||||||
|
var response = await client.post(Uri.parse('$url/nes/ted/foo'));
|
||||||
|
var json_ = json.decode(response.body);
|
||||||
|
expect(json_['route'], equals('foo'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Can parse parameters from a nested Angel instance', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/todos/1337/action/test'));
|
||||||
|
var json_ = json.decode(response.body);
|
||||||
|
print('JSON: $json_');
|
||||||
|
expect(json_['id'], equals('1337'));
|
||||||
|
expect(json_['action'], equals('test'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Can add and use named middleware', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/intercepted'));
|
||||||
|
expect(response.body, equals('Middleware'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Middleware via metadata', () async {
|
||||||
|
// Metadata
|
||||||
|
var response = await client.get(Uri.parse('$url/meta'));
|
||||||
|
expect(response.body, equals('Middleware'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Can serialize function result as JSON', () async {
|
||||||
|
Map headers = <String, String>{'Content-Type': 'application/json'};
|
||||||
|
var postData = json.encode({'it': 'works'});
|
||||||
|
var response = await client.post(Uri.parse('$url/lambda'),
|
||||||
|
headers: headers as Map<String, String>, body: postData);
|
||||||
|
print('Response: ${response.body}');
|
||||||
|
expect(json.decode(response.body)['it'], equals('works'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Fallback routes', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/my_favorite_artist'));
|
||||||
|
expect(response.body, equals('"MJ"'));
|
||||||
|
});
|
||||||
|
|
||||||
|
/* TODO: Revisit this later
|
||||||
|
test('Can name routes', () {
|
||||||
|
Route foo = app.get('/framework/:id', null)..name = 'frm';
|
||||||
|
print('Foo: $foo');
|
||||||
|
String uri = foo.makeUri({'id': 'angel'});
|
||||||
|
print(uri);
|
||||||
|
expect(uri, equals('framework/angel'));
|
||||||
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
|
test('Redirect to named routes', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/named'));
|
||||||
|
print(response.body);
|
||||||
|
expect(json.decode(response.body), equals('Hello tests'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Match routes, even with query params', () async {
|
||||||
|
var response = await client
|
||||||
|
.get(Uri.parse('$url/log?foo=bar&bar=baz&baz.foo=bar&baz.bar=foo'));
|
||||||
|
print(response.body);
|
||||||
|
expect(json.decode(response.body), equals('Logged'));
|
||||||
|
|
||||||
|
response = await client.get(Uri.parse('$url/query/foo?bar=baz'));
|
||||||
|
print(response.body);
|
||||||
|
expect(response.body, equals('Service with Middleware'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('only match route with matching method', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/method'));
|
||||||
|
print(response.body);
|
||||||
|
expect(response.body, '"Only GET"');
|
||||||
|
|
||||||
|
response = await client.post(Uri.parse('$url/method'));
|
||||||
|
print(response.body);
|
||||||
|
expect(response.body, '"Only POST"');
|
||||||
|
|
||||||
|
response = await client.patch(Uri.parse('$url/method'));
|
||||||
|
print(response.body);
|
||||||
|
expect(response.body, '"MJ"');
|
||||||
|
});
|
||||||
|
}
|
43
packages/framework/test/serialize_test.dart
Normal file
43
packages/framework/test/serialize_test.dart
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:http_parser/http_parser.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late Angel app;
|
||||||
|
late http.Client client;
|
||||||
|
late HttpServer server;
|
||||||
|
late String url;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
app = Angel(reflector: MirrorsReflector())
|
||||||
|
..get('/foo', ioc(() => {'hello': 'world'}))
|
||||||
|
..get('/bar', (req, res) async {
|
||||||
|
await res.serialize({'hello': 'world'},
|
||||||
|
contentType: MediaType('text', 'html'));
|
||||||
|
});
|
||||||
|
client = http.Client();
|
||||||
|
|
||||||
|
server = await AngelHttp(app).startServer();
|
||||||
|
url = 'http://${server.address.host}:${server.port}';
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() async {
|
||||||
|
client.close();
|
||||||
|
await server.close(force: true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('correct content-type', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/foo'));
|
||||||
|
print('Response: ${response.body}');
|
||||||
|
expect(response.headers['content-type'], contains('application/json'));
|
||||||
|
|
||||||
|
response = await client.get(Uri.parse('$url/bar'));
|
||||||
|
print('Response: ${response.body}');
|
||||||
|
expect(response.headers['content-type'], contains('text/html'));
|
||||||
|
});
|
||||||
|
}
|
222
packages/framework/test/server_test.dart
Normal file
222
packages/framework/test/server_test.dart
Normal file
|
@ -0,0 +1,222 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
final Uri $foo = Uri.parse('http://localhost:3000/foo');
|
||||||
|
|
||||||
|
/// Additional tests to improve coverage of server.dart
|
||||||
|
void main() {
|
||||||
|
group('scoping', () {
|
||||||
|
var parent = Angel(reflector: MirrorsReflector())..configuration['two'] = 2;
|
||||||
|
var child = Angel(reflector: MirrorsReflector());
|
||||||
|
parent.mount('/child', child);
|
||||||
|
|
||||||
|
test('sets children', () {
|
||||||
|
expect(parent.children, contains(child));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('sets parent', () {
|
||||||
|
expect(child.parent, parent);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('properties can climb up hierarchy', () {
|
||||||
|
expect(child.findProperty('two'), 2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('custom server generator', () {
|
||||||
|
var app = Angel(reflector: MirrorsReflector());
|
||||||
|
var http = AngelHttp.custom(app, HttpServer.bind);
|
||||||
|
expect(http.serverGenerator, HttpServer.bind);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('default error handler', () async {
|
||||||
|
var app = Angel(reflector: MirrorsReflector());
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
var rq = MockHttpRequest('GET', $foo);
|
||||||
|
await (rq.close());
|
||||||
|
var rs = rq.response;
|
||||||
|
var req = await http.createRequestContext(rq, rs);
|
||||||
|
var res = await http.createResponseContext(rq, rs);
|
||||||
|
var e = HttpException(
|
||||||
|
statusCode: 321, message: 'Hello', errors: ['foo', 'bar']);
|
||||||
|
await app.errorHandler(e, req, res);
|
||||||
|
await http.sendResponse(rq, rs, req, res);
|
||||||
|
expect(
|
||||||
|
ContentType.parse(rs.headers.value('content-type')!).mimeType,
|
||||||
|
'text/html',
|
||||||
|
);
|
||||||
|
expect(rs.statusCode, e.statusCode);
|
||||||
|
var body = await rs.transform(utf8.decoder).join();
|
||||||
|
expect(body, contains('<title>${e.message}</title>'));
|
||||||
|
expect(body, contains('<li>foo</li>'));
|
||||||
|
expect(body, contains('<li>bar</li>'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('plug-ins run on startup', () async {
|
||||||
|
var app = Angel(reflector: MirrorsReflector());
|
||||||
|
app.startupHooks.add((app) => app.configuration['two'] = 2);
|
||||||
|
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
await http.startServer();
|
||||||
|
expect(app.configuration['two'], 2);
|
||||||
|
await app.close();
|
||||||
|
await http.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('warning when adding routes to flattened router', () {
|
||||||
|
var app = Angel(reflector: MirrorsReflector())
|
||||||
|
..optimizeForProduction(force: true);
|
||||||
|
app.dumpTree();
|
||||||
|
app.get('/', (req, res) => 2);
|
||||||
|
app.mount('/foo', Router()..get('/', (req, res) => 3));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('services close on close call', () async {
|
||||||
|
var app = Angel(reflector: MirrorsReflector());
|
||||||
|
var svc = CustomCloseService();
|
||||||
|
expect(svc.value, 2);
|
||||||
|
app.use('/', svc);
|
||||||
|
await app.close();
|
||||||
|
expect(svc.value, 3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('global injection added to injection map', () async {
|
||||||
|
var app = Angel(reflector: MirrorsReflector())..configuration['a'] = 'b';
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
app.get('/', ioc((String a) => a));
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/'));
|
||||||
|
await (rq.close());
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
var body = await rq.response.transform(utf8.decoder).join();
|
||||||
|
expect(body, json.encode('b'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('global injected serializer', () async {
|
||||||
|
var app = Angel(reflector: MirrorsReflector())..serializer = (_) => 'x';
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
app.get($foo.path, (req, ResponseContext res) => res.serialize(null));
|
||||||
|
var rq = MockHttpRequest('GET', $foo);
|
||||||
|
await (rq.close());
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
var body = await rq.response.transform(utf8.decoder).join();
|
||||||
|
expect(body, 'x');
|
||||||
|
});
|
||||||
|
|
||||||
|
group('handler results', () {
|
||||||
|
var app = Angel(reflector: MirrorsReflector());
|
||||||
|
var http = AngelHttp(app);
|
||||||
|
app.responseFinalizers.add((req, res) => throw HttpException.forbidden());
|
||||||
|
late RequestContext req;
|
||||||
|
late ResponseContext res;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
var rq = MockHttpRequest('GET', $foo);
|
||||||
|
await (rq.close());
|
||||||
|
req = await http.createRequestContext(rq, rq.response);
|
||||||
|
res = await http.createResponseContext(rq, rq.response);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('getHandlerResult', () {
|
||||||
|
test('return request handler', () async {
|
||||||
|
handler(req, res) => (req, res) async {
|
||||||
|
return 2;
|
||||||
|
};
|
||||||
|
var r = await app.getHandlerResult(handler, req, res);
|
||||||
|
expect(r, 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('return future', () async {
|
||||||
|
var handler = Future.value(2);
|
||||||
|
expect(await app.getHandlerResult(handler, req, res), 2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('executeHandler', () {
|
||||||
|
test('return Stream', () async {
|
||||||
|
handler(req, res) => Stream.fromIterable([2, 3]);
|
||||||
|
expect(await app.executeHandler(handler, req, res), isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('end response', () async {
|
||||||
|
handler(req, ResponseContext res) => res.close();
|
||||||
|
expect(await app.executeHandler(handler, req, res), isFalse);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group('handleHttpException', () {
|
||||||
|
late Angel app;
|
||||||
|
late AngelHttp http;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
app = Angel(reflector: MirrorsReflector());
|
||||||
|
app.get('/wtf', (req, res) => throw HttpException.forbidden());
|
||||||
|
app.get('/wtf2', (req, res) => throw HttpException.forbidden());
|
||||||
|
http = AngelHttp(app);
|
||||||
|
await http.startServer('127.0.0.1', 0);
|
||||||
|
|
||||||
|
var oldHandler = app.errorHandler;
|
||||||
|
app.errorHandler = (e, req, res) {
|
||||||
|
print('FATAL: ${e.error ?? e}');
|
||||||
|
print(e.stackTrace);
|
||||||
|
return oldHandler(e, req, res);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() => app.close());
|
||||||
|
|
||||||
|
test('can send json', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri(path: 'wtf'))
|
||||||
|
..headers.set('accept', 'application/json');
|
||||||
|
await rq.close();
|
||||||
|
http.handleRequest(rq);
|
||||||
|
await rq.response.toList();
|
||||||
|
expect(rq.response.statusCode, 403);
|
||||||
|
expect(rq.response.headers.contentType!.mimeType, 'application/json');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can throw in finalizer', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri(path: 'wtf'))
|
||||||
|
..headers.set('accept', 'application/json');
|
||||||
|
await rq.close();
|
||||||
|
http.handleRequest(rq);
|
||||||
|
await rq.response.toList();
|
||||||
|
expect(rq.response.statusCode, 403);
|
||||||
|
expect(rq.response.headers.contentType!.mimeType, 'application/json');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can send html', () async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri(path: 'wtf2'));
|
||||||
|
//rq.headers.set('accept', 'text/html');
|
||||||
|
await rq.close();
|
||||||
|
http.handleRequest(rq);
|
||||||
|
await rq.response.toList();
|
||||||
|
expect(rq.response.statusCode, 403);
|
||||||
|
expect(rq.response.headers.contentType?.mimeType, 'text/html');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
class CustomCloseService extends Service {
|
||||||
|
int value = 2;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void close() {
|
||||||
|
value = 3;
|
||||||
|
super.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Expose('/foo')
|
||||||
|
class FooController extends Controller {
|
||||||
|
@Expose('/bar')
|
||||||
|
Future<String> bar() async => 'baz';
|
||||||
|
}
|
84
packages/framework/test/service_map_test.dart
Normal file
84
packages/framework/test/service_map_test.dart
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
import 'package:quiver/core.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
MapService inner;
|
||||||
|
late Service<String?, Todo> mapped;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
inner = MapService();
|
||||||
|
mapped = inner.map<Todo>(Todo.fromMap, Todo.toMap);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('create', () async {
|
||||||
|
var result = await mapped.create(
|
||||||
|
Todo(text: 'hello', complete: false),
|
||||||
|
);
|
||||||
|
print(result);
|
||||||
|
expect(
|
||||||
|
result,
|
||||||
|
Todo(text: 'hello', complete: false),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
group('after create', () {
|
||||||
|
late Todo result;
|
||||||
|
String? id;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
result = await mapped.create(Todo(text: 'hello', complete: false));
|
||||||
|
id = result.id;
|
||||||
|
});
|
||||||
|
|
||||||
|
test('index', () async {
|
||||||
|
expect(await mapped.index(), [result]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('modify', () async {
|
||||||
|
var newTodo = Todo(text: 'yes', complete: true);
|
||||||
|
expect(await mapped.update(id, newTodo), newTodo);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('update', () async {
|
||||||
|
var newTodo = Todo(id: 'hmmm', text: 'yes', complete: true);
|
||||||
|
expect(await mapped.update(id, newTodo), newTodo);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('read', () async {
|
||||||
|
expect(await mapped.read(id), result);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('remove', () async {
|
||||||
|
expect(await mapped.remove(id), result);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
class Todo {
|
||||||
|
final String? id, text;
|
||||||
|
final bool? complete;
|
||||||
|
|
||||||
|
Todo({this.id, this.text, this.complete});
|
||||||
|
|
||||||
|
static Todo fromMap(Map<String, dynamic> json) {
|
||||||
|
return Todo(
|
||||||
|
id: json['id'] as String?,
|
||||||
|
text: json['text'] as String?,
|
||||||
|
complete: json['complete'] as bool?);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Map<String, dynamic> toMap(Todo model) {
|
||||||
|
return {'id': model.id, 'text': model.text, 'complete': model.complete};
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(other) =>
|
||||||
|
other is Todo && other.text == text && other.complete == complete;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() => '$id:$text($complete)';
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => hash2(text.hashCode, complete.hashCode);
|
||||||
|
}
|
131
packages/framework/test/services_test.dart
Normal file
131
packages/framework/test/services_test.dart
Normal file
|
@ -0,0 +1,131 @@
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
import 'package:stack_trace/stack_trace.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
class Todo extends Model {
|
||||||
|
String? text;
|
||||||
|
String? over;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
Map headers = <String, String>{
|
||||||
|
'Accept': 'application/json',
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
};
|
||||||
|
late Angel app;
|
||||||
|
late MapService service;
|
||||||
|
late String url;
|
||||||
|
late http.Client client;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
app = Angel(reflector: MirrorsReflector())
|
||||||
|
..use('/todos', service = MapService())
|
||||||
|
..errorHandler = (e, req, res) {
|
||||||
|
if (e.error != null) print('Whoops: ${e.error}');
|
||||||
|
if (e.stackTrace != null) print(Chain.forTrace(e.stackTrace!).terse);
|
||||||
|
};
|
||||||
|
|
||||||
|
var server = await AngelHttp(app).startServer();
|
||||||
|
client = http.Client();
|
||||||
|
url = 'http://${server.address.host}:${server.port}';
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() async {
|
||||||
|
await app.close();
|
||||||
|
client.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
group('memory', () {
|
||||||
|
test('can index an empty service', () async {
|
||||||
|
var response = await client.get(Uri.parse('$url/todos/'));
|
||||||
|
print(response.body);
|
||||||
|
expect(response.body, equals('[]'));
|
||||||
|
print(response.body);
|
||||||
|
expect(json.decode(response.body).length, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can create data', () async {
|
||||||
|
var postData = json.encode({'text': 'Hello, world!'});
|
||||||
|
var response = await client.post(Uri.parse('$url/todos'),
|
||||||
|
headers: headers as Map<String, String>, body: postData);
|
||||||
|
expect(response.statusCode, 201);
|
||||||
|
var jsons = json.decode(response.body);
|
||||||
|
print(jsons);
|
||||||
|
expect(jsons['text'], equals('Hello, world!'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can fetch data', () async {
|
||||||
|
var postData = json.encode({'text': 'Hello, world!'});
|
||||||
|
await client.post(Uri.parse('$url/todos'),
|
||||||
|
headers: headers as Map<String, String>, body: postData);
|
||||||
|
var response = await client.get(Uri.parse('$url/todos/0'));
|
||||||
|
expect(response.statusCode, 200);
|
||||||
|
var jsons = json.decode(response.body);
|
||||||
|
print(jsons);
|
||||||
|
expect(jsons['text'], equals('Hello, world!'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can modify data', () async {
|
||||||
|
var postData = json.encode({'text': 'Hello, world!'});
|
||||||
|
await client.post(Uri.parse('$url/todos'),
|
||||||
|
headers: headers as Map<String, String>, body: postData);
|
||||||
|
postData = json.encode({'text': 'modified'});
|
||||||
|
|
||||||
|
var response = await client.patch(Uri.parse('$url/todos/0'),
|
||||||
|
headers: headers, body: postData);
|
||||||
|
expect(response.statusCode, 200);
|
||||||
|
var jsons = json.decode(response.body);
|
||||||
|
print(jsons);
|
||||||
|
expect(jsons['text'], equals('modified'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can overwrite data', () async {
|
||||||
|
var postData = json.encode({'text': 'Hello, world!'});
|
||||||
|
await client.post(Uri.parse('$url/todos'),
|
||||||
|
headers: headers as Map<String, String>, body: postData);
|
||||||
|
postData = json.encode({'over': 'write'});
|
||||||
|
|
||||||
|
var response = await client.post(Uri.parse('$url/todos/0'),
|
||||||
|
headers: headers, body: postData);
|
||||||
|
expect(response.statusCode, 200);
|
||||||
|
var jsons = json.decode(response.body);
|
||||||
|
print(jsons);
|
||||||
|
expect(jsons['text'], equals(null));
|
||||||
|
expect(jsons['over'], equals('write'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('readMany', () async {
|
||||||
|
var items = <Map>[
|
||||||
|
await service.create({'foo': 'bar'}),
|
||||||
|
await service.create({'bar': 'baz'}),
|
||||||
|
await service.create({'baz': 'quux'})
|
||||||
|
];
|
||||||
|
|
||||||
|
var ids = items.map((m) => m['id'] as String?).toList();
|
||||||
|
expect(await service.readMany(ids), items);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('can delete data', () async {
|
||||||
|
var postData = json.encode({'text': 'Hello, world!'});
|
||||||
|
var created = await client
|
||||||
|
.post(Uri.parse('$url/todos'),
|
||||||
|
headers: headers as Map<String, String>, body: postData)
|
||||||
|
.then((r) => json.decode(r.body));
|
||||||
|
var response =
|
||||||
|
await client.delete(Uri.parse("$url/todos/${created['id']}"));
|
||||||
|
expect(response.statusCode, 200);
|
||||||
|
var json_ = json.decode(response.body);
|
||||||
|
print(json_);
|
||||||
|
expect(json_['text'], equals('Hello, world!'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cannot remove all unless explicitly set', () async {
|
||||||
|
var response = await client.delete(Uri.parse('$url/todos/null'));
|
||||||
|
expect(response.statusCode, 403);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
119
packages/framework/test/streaming_test.dart
Normal file
119
packages/framework/test/streaming_test.dart
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:platform_container/mirrors.dart';
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:platform_framework/http.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:platform_mock_request/platform_mock_request.dart';
|
||||||
|
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
import 'encoders_buffer_test.dart' show encodingTests;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
late Angel app;
|
||||||
|
late AngelHttp http;
|
||||||
|
|
||||||
|
setUp(() {
|
||||||
|
app = Angel(reflector: MirrorsReflector());
|
||||||
|
http = AngelHttp(app, useZone: true);
|
||||||
|
|
||||||
|
app.logger = Logger('streaming_test')
|
||||||
|
..onRecord.listen((rec) {
|
||||||
|
print(rec);
|
||||||
|
if (rec.stackTrace != null) print(rec.stackTrace);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.encoders.addAll(
|
||||||
|
{
|
||||||
|
'deflate': zlib.encoder,
|
||||||
|
'gzip': gzip.encoder,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
app.get('/hello', (req, res) {
|
||||||
|
return Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits])
|
||||||
|
.pipe(res);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/write', (req, res) async {
|
||||||
|
await res.addStream(
|
||||||
|
Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits]));
|
||||||
|
res.write('bye');
|
||||||
|
await res.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/multiple', (req, res) async {
|
||||||
|
await res.addStream(
|
||||||
|
Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits]));
|
||||||
|
await res.addStream(Stream<List<int>>.fromIterable(['bye'.codeUnits]));
|
||||||
|
await res.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/overwrite', (req, res) async {
|
||||||
|
res.statusCode = 32;
|
||||||
|
await Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits])
|
||||||
|
.pipe(res);
|
||||||
|
|
||||||
|
var f = Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits])
|
||||||
|
.pipe(res)
|
||||||
|
.then((_) => false)
|
||||||
|
.catchError((_) => true);
|
||||||
|
|
||||||
|
expect(f, completion(true));
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/error', (req, res) => res.addError(StateError('wtf')));
|
||||||
|
|
||||||
|
app.errorHandler = (e, req, res) async {
|
||||||
|
stderr
|
||||||
|
..writeln(e.error)
|
||||||
|
..writeln(e.stackTrace);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() => http.close());
|
||||||
|
|
||||||
|
void expectHelloBye(String path) async {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse(path));
|
||||||
|
await (rq.close());
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
var body = await rq.response.transform(utf8.decoder).join();
|
||||||
|
expect(body, 'Hello, world!bye');
|
||||||
|
}
|
||||||
|
|
||||||
|
test('write after addStream', () => expectHelloBye('/write'));
|
||||||
|
|
||||||
|
test('multiple addStream', () => expectHelloBye('/multiple'));
|
||||||
|
|
||||||
|
test('cannot write after close', () async {
|
||||||
|
try {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/overwrite'));
|
||||||
|
await rq.close();
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
var body = await rq.response.transform(utf8.decoder).join();
|
||||||
|
|
||||||
|
if (rq.response.statusCode != 32) {
|
||||||
|
throw 'overwrite should throw error; response: $body';
|
||||||
|
}
|
||||||
|
} on StateError {
|
||||||
|
// Success
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('res => addError', () async {
|
||||||
|
try {
|
||||||
|
var rq = MockHttpRequest('GET', Uri.parse('/error'));
|
||||||
|
await (rq.close());
|
||||||
|
await http.handleRequest(rq);
|
||||||
|
var body = await rq.response.transform(utf8.decoder).join();
|
||||||
|
throw 'addError should throw error; response: $body';
|
||||||
|
} on StateError {
|
||||||
|
// Should throw error...
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
encodingTests(() => app);
|
||||||
|
}
|
10
packages/framework/test/view_generator_test.dart
Normal file
10
packages/framework/test/view_generator_test.dart
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import 'package:platform_framework/platform_framework.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('default view generator', () async {
|
||||||
|
var app = Angel();
|
||||||
|
var view = await app.viewGenerator!('foo', {'bar': 'baz'});
|
||||||
|
expect(view, contains('No view engine'));
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in a new issue