From 62336e0d53419f1e2868ca5d1c4e62e12c6c8fb7 Mon Sep 17 00:00:00 2001 From: thomashii Date: Sat, 10 Jul 2021 12:32:42 +0800 Subject: [PATCH] Updated websocket --- packages/client/lib/base_angel_client.dart | 4 +- packages/proxy/.idea/angel_proxy.iml | 16 ----- packages/proxy/.idea/jsLibraryMappings.xml | 7 --- packages/proxy/.idea/misc.xml | 28 --------- packages/proxy/.idea/modules.xml | 8 --- .../.idea/runConfigurations/All_Tests.xml | 8 --- .../.idea/runConfigurations/Basic_Tests.xml | 6 -- .../runConfigurations/Pub_Serve_Tests.xml | 6 -- .../.idea/runConfigurations/multiple_dart.xml | 7 --- packages/proxy/.idea/vcs.xml | 6 -- packages/proxy/.vscode/launch.json | 11 ---- packages/proxy/CHANGELOG.md | 35 +++++++---- packages/proxy/README.md | 12 ++-- packages/proxy/lib/src/proxy_layer.dart | 30 +++++---- packages/proxy/pubspec.yaml | 5 +- packages/proxy/test/basic_test.dart | 13 ++-- packages/static/pubspec.yaml | 2 +- packages/websocket/CHANGELOG.md | 63 +++++++++++++------ packages/websocket/README.md | 45 ++++++------- .../websocket/lib/base_websocket_client.dart | 2 +- packages/websocket/lib/browser.dart | 4 +- packages/websocket/lib/hooks.dart | 10 ++- packages/websocket/lib/server.dart | 38 ++++++----- packages/websocket/pubspec.yaml | 9 +-- packages/websocket/test/auth_test.dart | 6 +- 25 files changed, 158 insertions(+), 223 deletions(-) delete mode 100644 packages/proxy/.idea/angel_proxy.iml delete mode 100644 packages/proxy/.idea/jsLibraryMappings.xml delete mode 100644 packages/proxy/.idea/misc.xml delete mode 100644 packages/proxy/.idea/modules.xml delete mode 100644 packages/proxy/.idea/runConfigurations/All_Tests.xml delete mode 100644 packages/proxy/.idea/runConfigurations/Basic_Tests.xml delete mode 100644 packages/proxy/.idea/runConfigurations/Pub_Serve_Tests.xml delete mode 100644 packages/proxy/.idea/runConfigurations/multiple_dart.xml delete mode 100644 packages/proxy/.idea/vcs.xml delete mode 100644 packages/proxy/.vscode/launch.json diff --git a/packages/client/lib/base_angel_client.dart b/packages/client/lib/base_angel_client.dart index 2d2cf108..19858048 100644 --- a/packages/client/lib/base_angel_client.dart +++ b/packages/client/lib/base_angel_client.dart @@ -52,7 +52,7 @@ abstract class BaseAngelClient extends Angel { final List _services = []; final http.BaseClient client; - final _p = Context(style: Style.url); + final Context _p = Context(style: Style.url); @override Stream get onAuthenticated => _onAuthenticated.stream; @@ -212,7 +212,7 @@ class BaseAngelService extends Service { final http.BaseClient client; final AngelDeserializer? deserializer; - final _p = Context(style: Style.url); + final Context _p = Context(style: Style.url); final StreamController> _onIndexed = StreamController(); final StreamController _onRead = StreamController(), diff --git a/packages/proxy/.idea/angel_proxy.iml b/packages/proxy/.idea/angel_proxy.iml deleted file mode 100644 index eae13016..00000000 --- a/packages/proxy/.idea/angel_proxy.iml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/packages/proxy/.idea/jsLibraryMappings.xml b/packages/proxy/.idea/jsLibraryMappings.xml deleted file mode 100644 index f3e502d1..00000000 --- a/packages/proxy/.idea/jsLibraryMappings.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/packages/proxy/.idea/misc.xml b/packages/proxy/.idea/misc.xml deleted file mode 100644 index c65900a0..00000000 --- a/packages/proxy/.idea/misc.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - General - - - XPath - - - - - AngularJS - - - - - - \ No newline at end of file diff --git a/packages/proxy/.idea/modules.xml b/packages/proxy/.idea/modules.xml deleted file mode 100644 index a68a6387..00000000 --- a/packages/proxy/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/packages/proxy/.idea/runConfigurations/All_Tests.xml b/packages/proxy/.idea/runConfigurations/All_Tests.xml deleted file mode 100644 index 44742797..00000000 --- a/packages/proxy/.idea/runConfigurations/All_Tests.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - \ No newline at end of file diff --git a/packages/proxy/.idea/runConfigurations/Basic_Tests.xml b/packages/proxy/.idea/runConfigurations/Basic_Tests.xml deleted file mode 100644 index 87d3c79c..00000000 --- a/packages/proxy/.idea/runConfigurations/Basic_Tests.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/packages/proxy/.idea/runConfigurations/Pub_Serve_Tests.xml b/packages/proxy/.idea/runConfigurations/Pub_Serve_Tests.xml deleted file mode 100644 index 74e5219a..00000000 --- a/packages/proxy/.idea/runConfigurations/Pub_Serve_Tests.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/packages/proxy/.idea/runConfigurations/multiple_dart.xml b/packages/proxy/.idea/runConfigurations/multiple_dart.xml deleted file mode 100644 index 36b7b1d6..00000000 --- a/packages/proxy/.idea/runConfigurations/multiple_dart.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - \ No newline at end of file diff --git a/packages/proxy/.idea/vcs.xml b/packages/proxy/.idea/vcs.xml deleted file mode 100644 index 94a25f7f..00000000 --- a/packages/proxy/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/packages/proxy/.vscode/launch.json b/packages/proxy/.vscode/launch.json deleted file mode 100644 index b78e04ca..00000000 --- a/packages/proxy/.vscode/launch.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "name": "Multiple Proxies", - "type": "dart-cli", - "request": "launch", - "program": "${workspaceRoot}/example/multiple.dart" - } - ] -} \ No newline at end of file diff --git a/packages/proxy/CHANGELOG.md b/packages/proxy/CHANGELOG.md index d0d0b128..118f2a95 100644 --- a/packages/proxy/CHANGELOG.md +++ b/packages/proxy/CHANGELOG.md @@ -1,32 +1,43 @@ -# 4.0.0 +# Change Log + +## 4.0.1 + +* Updated README + +## 4.0.0 + * Migrated to support Dart SDK 2.12.x NNBD -# 3.0.0 +## 3.0.0 + * Migrated to work with Dart SDK 2.12.x Non NNBD -# 2.2.0 +## 2.2.0 + * Use `http.Client` instead of `http.BaseClient`, and make it an optional parameter. * Allow `baseUrl` to accept `Uri` or `String`. * Add `Proxy.pushState`. -# 2.1.2 +## 2.1.2 + * Apply lints. -# 2.1.1 +## 2.1.1 + * Update for framework@2.0.0-alpha.15 -# 2.1.0 +## 2.1.0 -- Use `Uri` instead of archaic `host`, `port`, and `mapTo`. Also cleaner + safer + easier. +* Use `Uri` instead of archaic `host`, `port`, and `mapTo`. Also cleaner + safer + easier. * Enable WebSocket proxying. -# 2.0.0 +## 2.0.0 -- Updates for Angel 2. Big thanks to @denkuy! -- Use `package:path` for better path resolution. +* Updates for Angel 2. Big thanks to @denkuy! +* Use `package:path` for better path resolution. -# 1.1.1 +## 1.1.1 -- Removed reference to `io`; now works with HTTP/2. Thanks to @daniel-v! +* Removed reference to `io`; now works with HTTP/2. Thanks to @daniel-v! diff --git a/packages/proxy/README.md b/packages/proxy/README.md index 8deb15b9..06ba093d 100644 --- a/packages/proxy/README.md +++ b/packages/proxy/README.md @@ -1,12 +1,12 @@ -# angel3_proxy -[![version](https://img.shields.io/badge/pub-v4.0.0-brightgreen)](https://pub.dartlang.org/packages/angel3_proxy) +# Angel3 Proxy + +[![version](https://img.shields.io/badge/pub-v4.0.1-brightgreen)](https://pub.dartlang.org/packages/angel3_proxy) [![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/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/proxy/LICENSE) -Angel middleware to forward requests to another server (i.e. `webdev serve`). -Also supports WebSockets. +Angel middleware to forward requests to another server (i.e. `webdev serve`). Also supports WebSockets. ```dart import 'package:angel3_proxy/angel3_proxy.dart'; @@ -23,15 +23,17 @@ void main() async { ``` You can also restrict the proxy to serving only from a specific root: + ```dart Proxy(baseUrl, publicPath: '/remote'); ``` Also, you can map requests to a root path on the remote server: + ```dart Proxy(baseUrl.replace(path: '/path')); ``` Request bodies will be forwarded as well, if they are not empty. This allows things like POST requests to function. -For a request body to be forwarded, the body must not have already been parsed. \ No newline at end of file +For a request body to be forwarded, the body must not have already been parsed. diff --git a/packages/proxy/lib/src/proxy_layer.dart b/packages/proxy/lib/src/proxy_layer.dart index bcad14e2..bd5055a2 100644 --- a/packages/proxy/lib/src/proxy_layer.dart +++ b/packages/proxy/lib/src/proxy_layer.dart @@ -5,7 +5,7 @@ import 'package:angel3_framework/angel3_framework.dart'; import 'package:angel3_framework/http.dart'; import 'package:http_parser/http_parser.dart'; import 'package:http/http.dart' as http; -import 'package:path/path.dart' as p; +import 'package:path/path.dart'; final RegExp _straySlashes = RegExp(r'(^/+)|(/+$)'); final MediaType _fallbackMediaType = MediaType('application', 'octet-stream'); @@ -16,6 +16,8 @@ final MediaType _fallbackMediaType = MediaType('application', 'octet-stream'); class Proxy { String? _prefix; + final Context _p = Context(style: Style.url); + /// The underlying [Client] to use. final http.Client httpClient; @@ -82,12 +84,12 @@ class Proxy { Future handleRequest(RequestContext req, ResponseContext res) { var path = req.path.replaceAll(_straySlashes, ''); - if (_prefix!.isNotEmpty) { - if (!p.isWithin(_prefix!, path) && !p.equals(_prefix!, path)) { + if (_prefix?.isNotEmpty == true) { + if (!_p.isWithin(_prefix!, path) && !_p.equals(_prefix!, path)) { return Future.value(true); } - path = p.relative(path, from: _prefix); + path = _p.relative(path, from: _prefix); } return servePath(path, req, res); @@ -98,7 +100,7 @@ class Proxy { String path, RequestContext req, ResponseContext res) async { http.StreamedResponse rs; - var uri = baseUrl.replace(path: p.join(baseUrl.path, path)); + var uri = baseUrl.replace(path: _p.join(baseUrl.path, path)); try { if (req is HttpRequestContext && @@ -123,13 +125,13 @@ class Proxy { var headers = { 'host': uri.authority, 'x-forwarded-for': req.remoteAddress.address, - 'x-forwarded-port': req.uri!.port.toString(), + 'x-forwarded-port': req.uri?.port.toString() ?? '', 'x-forwarded-host': - req.headers!.host ?? req.headers!.value('host') ?? 'none', + req.headers?.host ?? req.headers?.value('host') ?? 'none', 'x-forwarded-proto': uri.scheme, }; - req.headers!.forEach((name, values) { + req.headers?.forEach((name, values) { headers[name] = values.join(','); }); @@ -139,8 +141,8 @@ class Proxy { List? body; if (!req.hasParsedBody) { - body = await req.body! - .fold(BytesBuilder(), (bb, buf) => bb..add(buf)) + body = await req.body + ?.fold(BytesBuilder(), (bb, buf) => bb..add(buf)) .then((bb) => bb.takeBytes()); } @@ -149,13 +151,17 @@ class Proxy { rq.headers['host'] = rq.url.host; rq.encoding = Utf8Codec(allowMalformed: true); - if (body != null) rq.bodyBytes = body; + if (body != null) { + rq.bodyBytes = body; + } return httpClient.send(rq); } var future = accessRemote(); - if (timeout != null) future = future.timeout(timeout!); + if (timeout != null) { + future = future.timeout(timeout!); + } rs = await future; } on TimeoutException catch (e, st) { if (recoverFromDead) return true; diff --git a/packages/proxy/pubspec.yaml b/packages/proxy/pubspec.yaml index 230e8fc9..9e11b421 100644 --- a/packages/proxy/pubspec.yaml +++ b/packages/proxy/pubspec.yaml @@ -1,7 +1,8 @@ name: angel3_proxy -version: 4.0.0 +version: 4.0.1 description: Angel middleware to forward requests to another server (i.e. pub serve). -homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/proxy +homepage: https://angel3-framework.web.app/ +repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/proxy environment: sdk: '>=2.12.0 <3.0.0' dependencies: diff --git a/packages/proxy/test/basic_test.dart b/packages/proxy/test/basic_test.dart index 4d168149..4fe66449 100644 --- a/packages/proxy/test/basic_test.dart +++ b/packages/proxy/test/basic_test.dart @@ -9,7 +9,7 @@ import 'package:test/test.dart'; import 'common.dart'; void main() { - Angel? app; + late Angel app; var client = http.IOClient(); //late HttpServer server; late HttpServer testServer; @@ -17,7 +17,7 @@ void main() { setUp(() async { app = Angel(); - var appHttp = AngelHttp(app!); + var appHttp = AngelHttp(app); testServer = await startTestServer(); @@ -33,15 +33,15 @@ void main() { print('Proxy 1 on: ${proxy1.baseUrl}'); print('Proxy 2 on: ${proxy2.baseUrl}'); - app!.all('/proxy/*', proxy1.handleRequest); - app!.all('*', proxy2.handleRequest); + app.all('/proxy/*', proxy1.handleRequest); + app.all('*', proxy2.handleRequest); - app!.fallback((req, res) { + app.fallback((req, res) { print('Intercepting empty from ${req.uri}'); res.write('intercept empty'); }); - app!.logger = Logger('angel'); + app.logger = Logger('angel'); Logger.root.onRecord.listen((rec) { print(rec); @@ -56,7 +56,6 @@ void main() { tearDown(() async { await testServer.close(force: true); //await server.close(force: true); - app = null; url = null; }); diff --git a/packages/static/pubspec.yaml b/packages/static/pubspec.yaml index 9fc0a996..a9c25aa3 100644 --- a/packages/static/pubspec.yaml +++ b/packages/static/pubspec.yaml @@ -1,7 +1,7 @@ name: angel3_static description: This library provides a virtual directory to serve static files for Angel3 framework. version: 4.0.2 -homepage: https://github.com/dukefirehawk/angel +homepage: https://angel3-framework.web.app/ repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/static environment: sdk: '>=2.12.0 <3.0.0' diff --git a/packages/websocket/CHANGELOG.md b/packages/websocket/CHANGELOG.md index 7d5bb5d0..6d6ad94a 100644 --- a/packages/websocket/CHANGELOG.md +++ b/packages/websocket/CHANGELOG.md @@ -1,63 +1,88 @@ -# 4.0.0 +# Change Log + +## 4.0.1 + +* Updated README +* Fixed NNBD issues + +## 4.0.0 + * Migrated to support Dart SDK 2.12.x NNBD -# 3.0.0 +## 3.0.0 + * Migrated to work with Dart SDK 2.12.x Non NNBD -# 2.0.3 +## 2.0.3 + * Remove `WebSocketController.plugin`. * Remove any unawaited futures. -# 2.0.2 +## 2.0.2 + * Update `stream_channel` to `2.0.0`. * Use `angel_framework^@2.0.0-rc.0`. -# 2.0.1 +## 2.0.1 + * Add `reconnectOnClose` and `reconnectinterval` parameters in top-level `WebSockets` constructors. * Close `WebSocketExtraneousEventHandler`. * Add onAuthenticated to server-side. -# 2.0.0 +## 2.0.0 + * Update to work with `client@2.0.0`. -# 2.0.0-alpha.8 +## 2.0.0-alpha.8 + * Support for WebSockets over HTTP/2 (though in practice this doesn't often happen, if ever). -# 2.0.0-alpha.7 +## 2.0.0-alpha.7 + * Replace `WebSocketSynchronizer` with `StreamChannel`. -# 2.0.0-alpha.6 +## 2.0.0-alpha.6 + * Explicit import of `import 'package:http/io_client.dart' as http;` -# 2.0.0-alpha.5 +## 2.0.0-alpha.5 + * Update `http` dependency. -# 2.0.0-alpha.4 +## 2.0.0-alpha.4 + * Remove `package:json_god`. * Make `WebSocketContext` take any `StreamChannel`. * Strong typing updates. -# 2.0.0-alpha.3 +## 2.0.0-alpha.3 + * Directly import Angel HTTP. -# 2.0.0-alpha.2 +## 2.0.0-alpha.2 + * Updated for the next version of `angel_client`. -# 2.0.0-alpha.1 +## 2.0.0-alpha.1 + * Refactorings for updated Angel 2 versions. * Remove `package:dart2_constant`. -# 2.0.0-alpha +## 2.0.0-alpha + * Depend on Dart 2 and Angel 2. -# 1.1.2 +## 1.1.2 + * Dart 2 updates. * Added `handleClient`, which is nice for external implementations that plug into `AngelWebSocket`. -# 1.1.1 +## 1.1.1 + * Deprecated `unwrap`. * Service streams now pump out `e.data`, rather than the actual event. -# 1.1.0+1 -* Added `unwrap`. \ No newline at end of file +## 1.1.0+1 + +* Added `unwrap`. diff --git a/packages/websocket/README.md b/packages/websocket/README.md index 6f1a2fde..e87db382 100644 --- a/packages/websocket/README.md +++ b/packages/websocket/README.md @@ -1,29 +1,24 @@ -# angel3_websocket -[![version](https://img.shields.io/badge/pub-v4.0.0-brightgreen)](https://pub.dartlang.org/packages/angel3_websocket) +# Angel3 Websocket Library + +[![version](https://img.shields.io/badge/pub-v4.0.1-brightgreen)](https://pub.dartlang.org/packages/angel3_websocket) [![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/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/websocket/LICENSE) -WebSocket plugin for Angel. - -This plugin broadcasts events from hooked services via WebSockets. - -In addition, it adds itself to the app's IoC container as `AngelWebSocket`, so that it can be used -in controllers as well. +WebSocket plugin for Angel3. This plugin broadcasts events from hooked services via WebSockets. In addition, it adds itself to the app's IoC container as `AngelWebSocket`, so that it can be used in controllers as well. WebSocket contexts are add to `req.properties` as `'socket'`. +## Usage -# Usage - -**Server-side** +### Server-side ```dart import "package:angel3_framework/angel3_framework.dart"; import "package:angel3_websocket/server.dart"; -main() async { +void main() async { var app = Angel(); var ws = AngelWebSocket(); @@ -38,8 +33,7 @@ main() async { ``` -Filtering events is easy with hooked services. Just return a `bool`, whether -synchronously or asynchronously. +Filtering events is easy with hooked services. Just return a `bool`, whether synchronously or asynchronously. ```dart myService.properties['ws:filter'] = (HookedServiceEvent e, WebSocketContext socket) async { @@ -51,14 +45,14 @@ myService.index({ }); ``` -**Adding Handlers within a Controller** +#### Adding Handlers within a Controller `WebSocketController` extends a normal `Controller`, but also listens to WebSockets. ```dart import 'dart:async'; -import "package:angel_framework/angel_framework.dart"; -import "package:angel_websocket/server.dart"; +import "package:angel3_framework/angel3_framework.dart"; +import "package:angel3_websocket/server.dart"; @Expose("/") class MyController extends WebSocketController { @@ -86,25 +80,22 @@ class MyController extends WebSocketController { } ``` -**Client Use** +### Client Use -This repo also provides two client libraries `browser` and `io` that extend the base -`angel3_client` interface, and allow you to use a very similar API on the client to that of -the server. +This repo also provides two client libraries `browser` and `io` that extend the base `angel3_client` interface, and allow you to use a very similar API on the client to that of the server. -The provided clients also automatically try to reconnect their WebSockets when disconnected, -which means you can restart your development server without having to reload browser windows. +The provided clients also automatically try to reconnect their WebSockets when disconnected, which means you can restart your development server without having to reload browser windows. They also provide streams of data that pump out filtered data as it comes in from the server. Clients can even perform authentication over WebSockets. -**In the Browser** +#### In the Browser ```dart import "package:angel3_websocket/browser.dart"; -main() async { +void main() async { Angel app = WebSockets("/ws"); await app.connect(); @@ -127,7 +118,7 @@ main() async { } ``` -**CLI Client** +#### CLI Client ```dart import "package:angel3_framework/common.dart"; @@ -163,4 +154,4 @@ main() async { // Authenticate a WebSocket, if you were not already authenticated... app.authenticateViaJwt(''); -} \ No newline at end of file +} diff --git a/packages/websocket/lib/base_websocket_client.dart b/packages/websocket/lib/base_websocket_client.dart index 716e4b6e..6615b934 100644 --- a/packages/websocket/lib/base_websocket_client.dart +++ b/packages/websocket/lib/base_websocket_client.dart @@ -241,7 +241,7 @@ abstract class BaseWebSocketClient extends BaseAngelClient { if (_socket == null) { _queue.addLast(action); } else { - socket!.sink.add(serialize(action)); + socket?.sink.add(serialize(action)); } } diff --git a/packages/websocket/lib/browser.dart b/packages/websocket/lib/browser.dart index 1cdb08c2..c79a9561 100644 --- a/packages/websocket/lib/browser.dart +++ b/packages/websocket/lib/browser.dart @@ -42,7 +42,7 @@ class WebSockets extends BaseWebSocketClient { StreamSubscription? sub; t = Timer.periodic(Duration(milliseconds: 500), (timer) { if (!ctrl.isClosed) { - if (wnd.closed!) { + if (wnd.closed == true) { ctrl.addError(AngelHttpException.notAuthenticated( message: errorMessage ?? 'Authentication via popup window failed.')); @@ -60,7 +60,7 @@ class WebSockets extends BaseWebSocketClient { ctrl.add((e as CustomEvent).detail.toString()); t.cancel(); ctrl.close(); - sub!.cancel(); + sub?.cancel(); } }); diff --git a/packages/websocket/lib/hooks.dart b/packages/websocket/lib/hooks.dart index 668dd198..0a87f931 100644 --- a/packages/websocket/lib/hooks.dart +++ b/packages/websocket/lib/hooks.dart @@ -7,6 +7,7 @@ import 'package:angel3_framework/angel3_framework.dart'; HookedServiceEventListener doNotBroadcast([provider]) { return (HookedServiceEvent e) { if (e.params != null && e.params!.containsKey('provider')) { + var eParam = e.params!; var deny = false; var providers = provider is Iterable ? provider : [provider]; @@ -14,18 +15,15 @@ HookedServiceEventListener doNotBroadcast([provider]) { if (deny) break; if (p is Providers) { - deny = deny || - p == e.params!['provider'] || - e.params!['provider'] == p.via; + deny = deny || p == eParam['provider'] || eParam['provider'] == p.via; } else if (p == null) { deny = true; } else { - deny = - deny || (e.params!['provider'] as Providers).via == p.toString(); + deny = deny || (eParam['provider'] as Providers).via == p.toString(); } } - e.params!['broadcast'] = false; + eParam['broadcast'] = false; } }; } diff --git a/packages/websocket/lib/server.dart b/packages/websocket/lib/server.dart index e5d8de80..453e89c9 100644 --- a/packages/websocket/lib/server.dart +++ b/packages/websocket/lib/server.dart @@ -14,6 +14,7 @@ import 'package:stream_channel/stream_channel.dart'; import 'package:web_socket_channel/io.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:collection/collection.dart' show IterableExtension; +import 'package:logging/logging.dart'; import 'angel3_websocket.dart'; import 'constants.dart'; export 'angel3_websocket.dart'; @@ -26,6 +27,8 @@ typedef WebSocketResponseSerializer = String Function(dynamic data); /// Broadcasts events from [HookedService]s, and handles incoming [WebSocketAction]s. class AngelWebSocket { + final _log = Logger('AngelWebSocket'); + final List _clients = []; final List _servicesAlreadyWired = []; @@ -258,6 +261,7 @@ class AngelWebSocket { return null; } } catch (e, st) { + _log.severe('Unable to handle unknown action'); catchError(e, st, socket); } } @@ -266,15 +270,15 @@ class AngelWebSocket { Future handleAuth(WebSocketAction action, WebSocketContext socket) async { if (allowAuth != false && action.eventName == authenticateAction && - action.params!['query'] is Map && - action.params!['query']['jwt'] is String) { + action.params?['query'] is Map && + action.params?['query']['jwt'] is String) { try { var auth = socket.request.container!.make()!; var jwt = action.params!['query']['jwt'] as String; AuthToken token; token = AuthToken.validate(jwt, auth.hmac); - var user = await auth.deserializer!(token.userId as Object); + var user = await auth.deserializer(token.userId as Object); socket.request ..container!.registerSingleton(token) ..container!.registerSingleton(user, as: user.runtimeType); @@ -282,6 +286,7 @@ class AngelWebSocket { socket.send(authenticatedEvent, {'token': token.serialize(auth.hmac), 'data': user}); } catch (e, st) { + _log.severe('Authentication failed'); catchError(e, st, socket); } } else { @@ -345,6 +350,7 @@ class AngelWebSocket { } } } catch (e, st) { + _log.severe('Invalid data'); catchError(e, st, socket); } } @@ -353,16 +359,16 @@ class AngelWebSocket { // Send an error if (e is AngelHttpException) { socket.sendError(e); - app!.logger?.severe(e.message, e.error ?? e, e.stackTrace); + app?.logger?.severe(e.message, e.error ?? e, e.stackTrace); } else if (sendErrors) { var err = AngelHttpException(e, message: e.toString(), stackTrace: st, errors: [st.toString()]); socket.sendError(err); - app!.logger?.severe(err.message, e, st); + app?.logger?.severe(err.message, e, st); } else { var err = AngelHttpException(e); socket.sendError(err); - app!.logger?.severe(e.toString(), e, st); + app?.logger?.severe(e.toString(), e, st); } } @@ -383,10 +389,10 @@ class AngelWebSocket { /// Configures an [Angel] instance to listen for WebSocket connections. Future configureServer(Angel app) async { - app.container!.registerSingleton(this); + app.container?.registerSingleton(this); if (runtimeType != AngelWebSocket) { - app.container!.registerSingleton(this); + app.container?.registerSingleton(this); } // Set up services @@ -397,7 +403,7 @@ class AngelWebSocket { }); if (synchronizationChannel != null) { - synchronizationChannel!.stream + synchronizationChannel?.stream .listen((e) => batchEvent(e, notify: false)); } @@ -406,7 +412,7 @@ class AngelWebSocket { /// Handles an incoming [WebSocketContext]. Future handleClient(WebSocketContext socket) async { - var origin = socket.request.headers!.value('origin'); + var origin = socket.request.headers?.value('origin'); if (allowedOrigins != null && !allowedOrigins!.contains(origin)) { throw AngelHttpException.forbidden( message: @@ -418,7 +424,7 @@ class AngelWebSocket { _onConnection.add(socket); - socket.request.container!.registerSingleton(socket); + socket.request.container?.registerSingleton(socket); socket.channel.stream.listen( (data) { @@ -451,11 +457,11 @@ class AngelWebSocket { return false; } else if (req is Http2RequestContext && res is Http2ResponseContext) { var connection = - req.headers!['connection']?.map((s) => s.toLowerCase().trim()); - var upgrade = req.headers!.value('upgrade')?.toLowerCase(); - var version = req.headers!.value('sec-websocket-version'); - var key = req.headers!.value('sec-websocket-key'); - var protocol = req.headers!.value('sec-websocket-protocol'); + req.headers?['connection']?.map((s) => s.toLowerCase().trim()); + var upgrade = req.headers?.value('upgrade')?.toLowerCase(); + var version = req.headers?.value('sec-websocket-version'); + var key = req.headers?.value('sec-websocket-key'); + var protocol = req.headers?.value('sec-websocket-protocol'); if (connection == null) { throw AngelHttpException.badRequest( diff --git a/packages/websocket/pubspec.yaml b/packages/websocket/pubspec.yaml index d286fad1..d95a90aa 100644 --- a/packages/websocket/pubspec.yaml +++ b/packages/websocket/pubspec.yaml @@ -1,7 +1,8 @@ name: angel3_websocket -description: Support for using pkg:angel_client with WebSockets. Designed for Angel. -version: 4.0.0 -homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/websocket +description: This library provides WebSockets support for Angel3 framework. +version: 4.0.1 +homepage: https://angel3-framework.web.app/ +repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/websocket environment: sdk: '>=2.12.0 <3.0.0' dependencies: @@ -15,10 +16,10 @@ dependencies: stream_channel: ^2.1.0 web_socket_channel: ^2.1.0 collection: ^1.15.0 + logging: ^1.0.1 dev_dependencies: angel3_container: ^3.0.0 angel3_model: ^3.0.0 - logging: ^1.0.1 pedantic: ^1.11.0 test: ^1.17.4 \ No newline at end of file diff --git a/packages/websocket/test/auth_test.dart b/packages/websocket/test/auth_test.dart index 79e05dd7..cd001d9e 100644 --- a/packages/websocket/test/auth_test.dart +++ b/packages/websocket/test/auth_test.dart @@ -18,10 +18,8 @@ void main() { setUp(() async { app = Angel(); http = AngelHttp(app, useZone: false); - var auth = AngelAuth(); - - auth.serializer = (_) async => 'baz'; - auth.deserializer = (_) async => USER; + var auth = AngelAuth( + serializer: (_) async => 'baz', deserializer: (_) async => USER); auth.strategies['local'] = LocalAuthStrategy( (username, password) async {