From 8b2d9bf7254cebd6e083061b3d999adc6976018e Mon Sep 17 00:00:00 2001 From: thomashii Date: Sun, 28 Nov 2021 12:15:56 +0800 Subject: [PATCH] Updated framework --- packages/auth_twitter/pubspec.yaml | 4 +- packages/framework/CHANGELOG.md | 4 ++ .../framework/lib/src/core/controller.dart | 16 +++++--- .../lib/src/core/hooked_service.dart | 40 ++++++++++--------- .../framework/lib/src/core/injection.dart | 6 +-- .../lib/src/core/response_context.dart | 2 +- packages/framework/lib/src/core/routable.dart | 13 +++--- packages/framework/lib/src/core/server.dart | 17 ++++---- packages/framework/lib/src/core/service.dart | 14 +++---- .../framework/lib/src/http/angel_http.dart | 8 ++-- .../lib/src/http/http_request_context.dart | 2 +- .../lib/src/http2/http2_request_context.dart | 4 +- packages/framework/pubspec.yaml | 2 +- packages/framework/test/controller_test.dart | 8 ++-- packages/framework/test/di_test.dart | 4 +- packages/production/CHANGELOG.md | 4 ++ packages/production/example/main.dart | 8 ++-- packages/production/lib/src/runner.dart | 6 +-- packages/production/pubspec.yaml | 2 +- packages/shelf/CHANGELOG.md | 4 ++ packages/shelf/lib/src/shelf_driver.dart | 2 +- packages/shelf/pubspec.yaml | 2 +- 22 files changed, 99 insertions(+), 73 deletions(-) diff --git a/packages/auth_twitter/pubspec.yaml b/packages/auth_twitter/pubspec.yaml index 2ba132bd..c2d96d5a 100644 --- a/packages/auth_twitter/pubspec.yaml +++ b/packages/auth_twitter/pubspec.yaml @@ -6,8 +6,8 @@ publish_to: none environment: sdk: ">=2.10.0 <3.0.0" dependencies: - angel_auth: - angel_framework: + angel_auth: ^2.0.0 + angel_framework: ^2.0.0 http: ^0.13.0 path: ^1.0.0 twitter: diff --git a/packages/framework/CHANGELOG.md b/packages/framework/CHANGELOG.md index 33cc0c4d..8b968b76 100644 --- a/packages/framework/CHANGELOG.md +++ b/packages/framework/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 4.3.0 + +* Updated `container` to non nullable + ## 4.2.2 * Added `Date` to response header diff --git a/packages/framework/lib/src/core/controller.dart b/packages/framework/lib/src/core/controller.dart index 62cdcc46..c72644bd 100644 --- a/packages/framework/lib/src/core/controller.dart +++ b/packages/framework/lib/src/core/controller.dart @@ -12,7 +12,13 @@ class Controller { Angel? _app; /// The [Angel] application powering this controller. - Angel? get app => _app; + 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; @@ -36,14 +42,14 @@ class Controller { _app = app; if (injectSingleton != false) { - if (!app.container!.has(runtimeType)) { - _app!.container!.registerSingleton(this, as: runtimeType); + if (!app.container.has(runtimeType)) { + _app!.container.registerSingleton(this, as: runtimeType); } } - var name = await applyRoutes(app, app.container!.reflector); + var name = await applyRoutes(app, app.container.reflector); app.controllers[name] = this; - return null; + //return null; } /// Applies the routes from this [Controller] to some [router]. diff --git a/packages/framework/lib/src/core/hooked_service.dart b/packages/framework/lib/src/core/hooked_service.dart index 9b70723a..ccf5c718 100644 --- a/packages/framework/lib/src/core/hooked_service.dart +++ b/packages/framework/lib/src/core/hooked_service.dart @@ -76,7 +76,9 @@ class HookedService> /// Closes any open [StreamController]s on this instance. **Internal use only**. @override Future close() { - _ctrl.forEach((c) => c.close()); + for (var c in _ctrl) { + c.close(); + } beforeIndexed._close(); beforeRead._close(); beforeCreated._close(); @@ -95,7 +97,7 @@ class HookedService> /// Adds hooks to this instance. void addHooks(Angel app) { - var hooks = getAnnotation(inner, app.container!.reflector); + var hooks = getAnnotation(inner, app.container.reflector); var before = >[]; var after = >[]; @@ -107,7 +109,7 @@ class HookedService> void applyListeners( Function fn, HookedServiceEventDispatcher dispatcher, [bool? isAfter]) { - var hooks = getAnnotation(fn, app.container!.reflector); + var hooks = getAnnotation(fn, app.container.reflector); final listeners = >[ ...isAfter == true ? after : before ]; @@ -144,8 +146,8 @@ class HookedService> }); @override - void addRoutes([Service? s]) { - super.addRoutes(s ?? inner); + void addRoutes([Service? service]) { + super.addRoutes(service ?? inner); } /// Runs the [listener] before every service method specified. @@ -274,26 +276,26 @@ class HookedService> } @override - Future> index([Map? _params]) { - var params = _stripReq(_params); + Future> index([Map? params]) { + var localParams = _stripReq(params); return beforeIndexed - ._emit(HookedServiceEvent(false, _getRequest(_params), - _getResponse(_params), inner, HookedServiceEvent.indexed, - params: params)) + ._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: params, result: before.result)) + ._emit(HookedServiceEvent(true, _getRequest(params), + _getResponse(params), inner, HookedServiceEvent.indexed, + params: localParams, result: before.result)) .then((after) => after.result as List); } - return inner.index(params).then((result) { + return inner.index(localParams).then((result) { return afterIndexed - ._emit(HookedServiceEvent(true, _getRequest(_params), - _getResponse(_params), inner, HookedServiceEvent.indexed, - params: params, result: result)) + ._emit(HookedServiceEvent(true, _getRequest(params), + _getResponse(params), inner, HookedServiceEvent.indexed, + params: localParams, result: result)) .then((after) => after.result as List); }); }); @@ -557,7 +559,9 @@ class HookedServiceEventDispatcher> { final List> listeners = []; void _close() { - _ctrl.forEach((c) => c.close()); + for (var c in _ctrl) { + c.close(); + } listeners.clear(); } diff --git a/packages/framework/lib/src/core/injection.dart b/packages/framework/lib/src/core/injection.dart index 8e65511a..70aee136 100644 --- a/packages/framework/lib/src/core/injection.dart +++ b/packages/framework/lib/src/core/injection.dart @@ -12,7 +12,7 @@ RequestHandler ioc(Function handler, {Iterable optional = const []}) { RequestHandler? contained; if (req.app?.container != null) { - var injection = preInject(handler, req.app!.container!.reflector); + var injection = preInject(handler, req.app!.container.reflector); //if (injection != null) { injection.optional.addAll(optional); contained = handleContained(handler, injection); @@ -70,7 +70,7 @@ Future resolveInjection(requirement, InjectionRequest injection, } } else if (requirement is Type && requirement != dynamic) { try { - var futureType = container!.reflector.reflectFutureOf(requirement); + var futureType = container.reflector.reflectFutureOf(requirement); if (container.has(futureType.reflectedType)) { return await container.make(futureType.reflectedType); } @@ -78,7 +78,7 @@ Future resolveInjection(requirement, InjectionRequest injection, // Ignore. } - return await container!.make(requirement); + return await container.make(requirement); } else if (throwOnUnresolved) { throw ArgumentError( '$requirement cannot be injected into a request handler.'); diff --git a/packages/framework/lib/src/core/response_context.dart b/packages/framework/lib/src/core/response_context.dart index de41610d..f29dbdbe 100644 --- a/packages/framework/lib/src/core/response_context.dart +++ b/packages/framework/lib/src/core/response_context.dart @@ -288,7 +288,7 @@ abstract class ResponseContext } final head = controller - .findExpose(app!.container!.reflector)! + .findExpose(app!.container.reflector)! .path .toString() .replaceAll(_straySlashes, ''); diff --git a/packages/framework/lib/src/core/routable.dart b/packages/framework/lib/src/core/routable.dart index 67e83925..09e8464e 100644 --- a/packages/framework/lib/src/core/routable.dart +++ b/packages/framework/lib/src/core/routable.dart @@ -48,14 +48,15 @@ class Routable extends Router { final Map _serviceLookups = {}; final Map configuration = {}; - final Container? _container; + final Container _container; Routable([Reflector? reflector]) - : _container = reflector == null ? null : Container(reflector), +// : _container = reflector == null ? null : Container(reflector), + : _container = Container(reflector ?? ThrowingReflector()), super(); /// A [Container] used to inject dependencies. - Container? get container => _container; + Container get container => _container; void close() { _services.clear(); @@ -99,10 +100,10 @@ class Routable extends Router { {Iterable middleware = const {}}) { final handlers = []; // Merge @Middleware declaration, if any - var reflector = _container?.reflector; - if (reflector != null && reflector is! ThrowingReflector) { + var reflector = _container.reflector; + if (reflector is! ThrowingReflector) { var middlewareDeclaration = - getAnnotation(handler, _container?.reflector); + getAnnotation(handler, _container.reflector); if (middlewareDeclaration != null) { handlers.addAll(middlewareDeclaration.handlers); } diff --git a/packages/framework/lib/src/core/server.dart b/packages/framework/lib/src/core/server.dart index e04cf2c8..e0975319 100644 --- a/packages/framework/lib/src/core/server.dart +++ b/packages/framework/lib/src/core/server.dart @@ -4,6 +4,7 @@ import 'dart:async'; import 'dart:collection' show HashMap; import 'dart:convert'; import 'package:angel3_container/angel3_container.dart'; +import 'package:angel3_container/mirrors.dart'; import 'package:angel3_http_exception/angel3_http_exception.dart'; import 'package:angel3_route/angel3_route.dart'; import 'package:belatuk_combinator/belatuk_combinator.dart'; @@ -184,12 +185,12 @@ class Angel extends Routable { /// Loads some base dependencies into the service container. void bootstrapContainer() { if (runtimeType != Angel) { - container?.registerSingleton(this); + container.registerSingleton(this); } - container?.registerSingleton(this); - container?.registerSingleton(this); - container?.registerSingleton(this); + container.registerSingleton(this); + container.registerSingleton(this); + container.registerSingleton(this); } /// Shuts down the server, and closes any open [StreamController]s. @@ -315,6 +316,7 @@ class Angel extends Routable { /// the execution will be faster, as the injection requirements were stored beforehand. Future runContained(Function handler, RequestContext req, ResponseContext res, [Container? container]) { + container ??= Container(MirrorsReflector()); return Future.sync(() { if (_preContained.containsKey(handler)) { return handleContained(handler, _preContained[handler]!, container)( @@ -328,10 +330,11 @@ class Angel extends Routable { /// 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 ??= + req.container ?? res.app?.container ?? Container(ThrowingReflector()); var h = handleContained( handler, - _preContained[handler] = preInject(handler, container!.reflector), + _preContained[handler] = preInject(handler, container.reflector), container); return Future.sync(() => h(req, res)); // return closureMirror.apply(args).reflectee; @@ -350,7 +353,7 @@ class Angel extends Routable { /// /// If you are on `Dart >=2.0.0`, simply call `mountController()`. Future mountController([Type? type]) { - var controller = container!.make(type); + var controller = container.make(type); return configure(controller.configureServer).then((_) => controller); } diff --git a/packages/framework/lib/src/core/service.dart b/packages/framework/lib/src/core/service.dart index a109e91c..71febd2b 100644 --- a/packages/framework/lib/src/core/service.dart +++ b/packages/framework/lib/src/core/service.dart @@ -223,12 +223,12 @@ class Service extends Routable { var handlers = List.from(handlerss); // Add global middleware if declared on the instance itself - var before = getAnnotation(service, app!.container!.reflector); + var before = getAnnotation(service, app!.container.reflector); if (before != null) handlers.addAll(before.handlers); var indexMiddleware = - getAnnotation(service.index, app!.container!.reflector); + getAnnotation(service.index, app!.container.reflector); get('/', (req, res) { return index(mergeMap([ {'query': req.queryParameters}, @@ -241,7 +241,7 @@ class Service extends Routable { ]); var createMiddleware = - getAnnotation(service.create, app!.container!.reflector); + getAnnotation(service.create, app!.container.reflector); post('/', (req, ResponseContext res) { return req.parseBody().then((_) async { return await create( @@ -261,7 +261,7 @@ class Service extends Routable { ]); var readMiddleware = - getAnnotation(service.read, app!.container!.reflector); + getAnnotation(service.read, app!.container.reflector); get('/:id', (req, res) { return read( @@ -277,7 +277,7 @@ class Service extends Routable { ]); var modifyMiddleware = - getAnnotation(service.modify, app!.container!.reflector); + getAnnotation(service.modify, app!.container.reflector); patch('/:id', (req, res) { return req.parseBody().then((_) async { @@ -296,7 +296,7 @@ class Service extends Routable { ]); var updateMiddleware = - getAnnotation(service.update, app!.container!.reflector); + getAnnotation(service.update, app!.container.reflector); post('/:id', (req, res) { return req.parseBody().then((_) async { return await update( @@ -330,7 +330,7 @@ class Service extends Routable { ]); var removeMiddleware = - getAnnotation(service.remove, app!.container!.reflector); + getAnnotation(service.remove, app!.container.reflector); delete('/', (req, res) { return remove( '' as Id, diff --git a/packages/framework/lib/src/http/angel_http.dart b/packages/framework/lib/src/http/angel_http.dart index a43c85fc..dd389634 100644 --- a/packages/framework/lib/src/http/angel_http.dart +++ b/packages/framework/lib/src/http/angel_http.dart @@ -15,6 +15,8 @@ import 'http_response_context.dart'; final RegExp _straySlashes = RegExp(r'(^/+)|(/+$)'); +typedef ServerGeneratorType = Future Function(dynamic, int); + /// Adapts `dart:io`'s [HttpServer] to serve Angel. class AngelHttp extends Driver { @@ -27,8 +29,7 @@ class AngelHttp extends Driver Function(dynamic, int) serverGenerator, bool useZone) + AngelHttp._(Angel app, ServerGeneratorType serverGenerator, bool useZone) : super(app, serverGenerator, useZone: useZone); factory AngelHttp(Angel app, {bool useZone = true}) { @@ -36,8 +37,7 @@ class AngelHttp extends Driver Function(dynamic, int) serverGenerator, + factory AngelHttp.custom(Angel app, ServerGeneratorType serverGenerator, {bool useZone = true}) { return AngelHttp._(app, serverGenerator, useZone); } diff --git a/packages/framework/lib/src/http/http_request_context.dart b/packages/framework/lib/src/http/http_request_context.dart index 63edaaeb..cafaa391 100644 --- a/packages/framework/lib/src/http/http_request_context.dart +++ b/packages/framework/lib/src/http/http_request_context.dart @@ -77,7 +77,7 @@ class HttpRequestContext extends RequestContext { /// Magically transforms an [HttpRequest] into a [RequestContext]. static Future from( HttpRequest request, Angel app, String path) { - var ctx = HttpRequestContext().._container = app.container!.createChild(); + var ctx = HttpRequestContext().._container = app.container.createChild(); var override = request.method; diff --git a/packages/framework/lib/src/http2/http2_request_context.dart b/packages/framework/lib/src/http2/http2_request_context.dart index 64fc8227..d5cdcfcf 100644 --- a/packages/framework/lib/src/http2/http2_request_context.dart +++ b/packages/framework/lib/src/http2/http2_request_context.dart @@ -1,7 +1,7 @@ import 'dart:async'; import 'dart:convert'; import 'dart:io'; -import 'package:angel3_container/src/container.dart'; +import 'package:angel3_container/angel3_container.dart'; import 'package:angel3_framework/angel3_framework.dart'; import 'package:collection/collection.dart' show IterableExtension; import 'package:http2/transport.dart'; @@ -35,7 +35,7 @@ class Http2RequestContext extends RequestContext { Map sessions, Uuid uuid) { var c = Completer(); - var req = Http2RequestContext._(app.container!.createChild()) + var req = Http2RequestContext._(app.container.createChild()) ..app = app .._socket = socket .._stream = stream; diff --git a/packages/framework/pubspec.yaml b/packages/framework/pubspec.yaml index bfd8739b..f9351e6e 100644 --- a/packages/framework/pubspec.yaml +++ b/packages/framework/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_framework -version: 4.2.2 +version: 4.3.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/dukefirehawk/angel/tree/master/packages/framework diff --git a/packages/framework/test/controller_test.dart b/packages/framework/test/controller_test.dart index db29bce3..59c45c9b 100644 --- a/packages/framework/test/controller_test.dart +++ b/packages/framework/test/controller_test.dart @@ -85,8 +85,8 @@ void main() { res.redirectToAction('TodoController@foo', {'foo': 'world'})); // Register as a singleton, just for the purpose of this test - if (!app.container!.has()) { - app.container!.registerSingleton(todoController = TodoController()); + if (!app.container.has()) { + app.container.registerSingleton(todoController = TodoController()); } // Using mountController(); @@ -98,7 +98,7 @@ void main() { // 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(); - await todoController.applyRoutes(subRouter, app.container!.reflector); + await todoController.applyRoutes(subRouter, app.container.reflector); app.mount('/ctrl_group', subRouter); print(app.controllers); @@ -165,7 +165,7 @@ void main() { test('named actions', () async { var response = await client.get(Uri.parse('$url/redirect')); print('Response: ${response.body}'); - expect(response.body, equals('Hello, \"world!\"')); + expect(response.body, equals('Hello, "world!"')); }); group('optional expose', () { diff --git a/packages/framework/test/di_test.dart b/packages/framework/test/di_test.dart index a8c511f4..b47ecf10 100644 --- a/packages/framework/test/di_test.dart +++ b/packages/framework/test/di_test.dart @@ -25,8 +25,8 @@ void main() { client = http.Client(); // Inject some todos - app.container!.registerSingleton(Todo(text: TEXT, over: OVER)); - app.container!.registerFactory>((container) async { + app.container.registerSingleton(Todo(text: TEXT, over: OVER)); + app.container.registerFactory>((container) async { var req = container.make(); var text = await utf8.decoder.bind(req.body!).join(); return Foo(text); diff --git a/packages/production/CHANGELOG.md b/packages/production/CHANGELOG.md index e0748c35..9c798f0e 100644 --- a/packages/production/CHANGELOG.md +++ b/packages/production/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 3.1.2 + +* Fixed null safety warnings + ## 3.1.1 * Fixed license link diff --git a/packages/production/example/main.dart b/packages/production/example/main.dart index 19871d48..0b2ebeb6 100644 --- a/packages/production/example/main.dart +++ b/packages/production/example/main.dart @@ -8,15 +8,15 @@ void main(List args) => Runner('example', configureServer).run(args); Future configureServer(Angel app) async { // Use the injected `pub_sub.Client` to send messages. - var client = app.container?.make(); + var client = app.container.make(); var greeting = 'Hello! This is the default greeting.'; // We can listen for an event to perform some behavior. // // Here, we use message passing to synchronize some common state. - var onGreetingChanged = await client?.subscribe('greeting_changed'); + var onGreetingChanged = await client.subscribe('greeting_changed'); onGreetingChanged - ?.cast() + .cast() .listen((newGreeting) => greeting = newGreeting); // Add some routes... @@ -33,7 +33,7 @@ Future configureServer(Angel app) async { // This route will push a new value for `greeting`. app.get('/change_greeting/:newGreeting', (req, res) { greeting = (req.params['newGreeting'] as String? ?? ''); - client?.publish('greeting_changed', greeting); + client.publish('greeting_changed', greeting); return 'Changed greeting -> $greeting'; }); diff --git a/packages/production/lib/src/runner.dart b/packages/production/lib/src/runner.dart index ad7154f7..8617a7e4 100644 --- a/packages/production/lib/src/runner.dart +++ b/packages/production/lib/src/runner.dart @@ -50,7 +50,7 @@ ____________ ________________________ ___ |__ | / /_ ____/__ ____/__ / __ /| |_ |/ /_ / __ __ __/ __ / _ ___ | /| / / /_/ / _ /___ _ /___ -/_/ |_/_/ |_/ \____/ /_____/ /_____/ +/_/ |_/_/ |_/ ____/ /_____/ /_____/ '''; @@ -233,8 +233,8 @@ _ ___ | /| / / /_/ / _ /___ _ /___ pub_sub.IsolateClient('client${argsWithId.id}', args.pubSubSendPort); var app = Angel(reflector: args.reflector) - ..container!.registerSingleton(client) - ..container!.registerSingleton(InstanceInfo(id: argsWithId.id)); + ..container.registerSingleton(client) + ..container.registerSingleton(InstanceInfo(id: argsWithId.id)); app.shutdownHooks.add((_) => client.close()); diff --git a/packages/production/pubspec.yaml b/packages/production/pubspec.yaml index 7248068b..10956a50 100644 --- a/packages/production/pubspec.yaml +++ b/packages/production/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_production -version: 3.1.1 +version: 3.1.2 description: Helpers for concurrency, message-passing, rotating loggers, and other production functionality in Angel3. homepage: https://angel3-framework.web.app repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/production diff --git a/packages/shelf/CHANGELOG.md b/packages/shelf/CHANGELOG.md index 2dbbe3fe..9793e29c 100644 --- a/packages/shelf/CHANGELOG.md +++ b/packages/shelf/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 3.0.0-dev.3 + +* Fixed null safety warnings + ## 3.0.0-dev.2 * Updated to use `package:belatuk_pretty_logging` diff --git a/packages/shelf/lib/src/shelf_driver.dart b/packages/shelf/lib/src/shelf_driver.dart index 2775bfb7..a889f458 100644 --- a/packages/shelf/lib/src/shelf_driver.dart +++ b/packages/shelf/lib/src/shelf_driver.dart @@ -106,7 +106,7 @@ class AngelShelf extends Driver