Updated websocket,test and static

This commit is contained in:
thomashii 2022-02-22 08:07:01 +08:00
parent 48a441cf77
commit 49da5f76b1
18 changed files with 152 additions and 126 deletions

View file

@ -1,5 +1,13 @@
# Change Log
## 6.0.0
* Updated to min SDK 2.15.x
## 5.0.0
* No release. Skipped
## 4.1.0
* Updated to use `belatuk_range_header` package

View file

@ -265,10 +265,11 @@ class VirtualDirectory {
if (!acceptable) {
_log.severe('Mime type [$value] is not supported');
throw AngelHttpException(
UnsupportedError(
'Client requested $value, but server wanted to send $mimeType.'),
statusCode: 406,
message: '406 Not Acceptable');
//UnsupportedError(
// 'Client requested $value, but server wanted to send $mimeType.'),
errors: [
'Client requested $value, but server wanted to send $mimeType.'
], statusCode: 406, message: '406 Not Acceptable');
}
}
@ -320,7 +321,8 @@ class VirtualDirectory {
if (invalid) {
throw AngelHttpException(
Exception('Semantically invalid, or unbounded range.'),
//Exception('Semantically invalid, or unbounded range.'),
errors: ['Semantically invalid, or unbounded range.'],
statusCode: 416,
message: 'Semantically invalid, or unbounded range.');
}
@ -328,14 +330,15 @@ class VirtualDirectory {
// Ensure it's within range.
if (item.start >= totalFileSize || item.end >= totalFileSize) {
throw AngelHttpException(
Exception('Given range $item is out of bounds.'),
//Exception('Given range $item is out of bounds.'),
errors: ['Given range $item is out of bounds.'],
statusCode: 416,
message: 'Given range $item is out of bounds.');
}
}
if (header.items.isEmpty) {
throw AngelHttpException(null,
throw AngelHttpException(
statusCode: 416, message: '`Range` header may not be empty.');
} else if (header.items.length == 1) {
var item = header.items[0];

View file

@ -6,7 +6,7 @@ repository: https://github.com/dukefirehawk/angel/tree/angel3/packages/static
environment:
sdk: '>=2.15.0 <3.0.0'
dependencies:
angel3_framework: ^4.1.0
angel3_framework: ^6.0.0
belatuk_range_header: ^4.0.0
convert: ^3.0.0
crypto: ^3.0.1
@ -15,9 +15,32 @@ dependencies:
path: ^1.8.0
logging: ^1.0.1
dev_dependencies:
angel3_test: ^4.0.0
angel3_test: ^6.0.0
http: ^0.13.2
matcher: ^0.12.10
lints: ^1.0.0
test: ^1.17.4
dependency_overrides:
angel3_container:
path: ../container/angel_container
angel3_framework:
path: ../framework
angel3_http_exception:
path: ../http_exception
angel3_model:
path: ../model
angel3_route:
path: ../route
angel3_mock_request:
path: ../mock_request
angel3_auth:
path: ../auth
angel3_client:
path: ../client
angel3_websocket:
path: ../websocket
angel3_validate:
path: ../validate
angel3_test:
path: ../test

View file

@ -1,5 +1,13 @@
# Change Log
## 6.0.0
* Updated to min SDK 2.15.x
## 5.0.0
* No release. Skipped
## 4.1.1
* Fixed NNBD issues

View file

@ -2,7 +2,6 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:angel3_client/base_angel_client.dart' as client;
import 'package:angel3_client/io.dart' as client;
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_framework/http.dart';
import 'package:angel3_websocket/io.dart' as client;
@ -57,9 +56,6 @@ class TestClient extends client.BaseAngelClient {
/// The server instance to mock.
final Angel server;
@override
String? authToken;
late AngelHttp _http;
TestClient(this.server, {this.autoDecodeGzip = true, bool useZone = false})
@ -149,7 +145,7 @@ class TestClient extends client.BaseAngelClient {
reasonPhrase: rs.reasonPhrase);
}
@override
//@override
late String basePath;
@override

View file

@ -1,7 +1,6 @@
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:matcher/matcher.dart';
import 'package:angel3_http_exception/angel3_http_exception.dart';
import 'package:angel3_validate/angel3_validate.dart';
@ -43,7 +42,7 @@ Matcher hasStatus(int status) => _HasStatus(status);
Matcher hasValidBody(Validator validator) => _HasValidBody(validator);
class _IsJson extends Matcher {
var value;
dynamic value;
_IsJson(this.value);
@ -59,7 +58,7 @@ class _IsJson extends Matcher {
}
class _HasBody extends Matcher {
final body;
final dynamic body;
_HasBody(this.body);
@ -83,7 +82,7 @@ class _HasBody extends Matcher {
}
class _HasContentType extends Matcher {
var contentType;
dynamic contentType;
_HasContentType(this.contentType);
@ -116,7 +115,7 @@ class _HasContentType extends Matcher {
class _HasHeader extends Matcher {
final String key;
final value;
final dynamic value;
_HasHeader(this.key, this.value);

View file

@ -6,17 +6,37 @@ repository: https://github.com/dukefirehawk/angel/tree/master/packages/test
environment:
sdk: '>=2.15.0 <3.0.0'
dependencies:
angel3_client: ^4.1.0
angel3_framework: ^4.2.0
angel3_http_exception: ^3.0.0
angel3_validate: ^4.0.0
angel3_websocket: ^4.1.0
angel3_mock_request: ^2.0.0
angel3_container: ^3.1.0
angel3_client: ^6.0.0
angel3_framework: ^6.0.0
angel3_http_exception: ^6.0.0
angel3_validate: ^6.0.0
angel3_websocket: ^6.0.0
angel3_mock_request: ^6.0.0
angel3_container: ^6.0.0
http: ^0.13.1
matcher: ^0.12.10
web_socket_channel: ^2.0.0
dev_dependencies:
test: ^1.17.5
lints: ^1.0.0
dependency_overrides:
angel3_container:
path: ../container/angel_container
angel3_framework:
path: ../framework
angel3_http_exception:
path: ../http_exception
angel3_model:
path: ../model
angel3_route:
path: ../route
angel3_mock_request:
path: ../mock_request
angel3_auth:
path: ../auth
angel3_client:
path: ../client
angel3_websocket:
path: ../websocket
angel3_validate:
path: ../validate

View file

@ -1,5 +1,13 @@
# Change Log
## 6.0.0
* Updated to min SDK 2.15.x
## 5.0.0
* No release. Skipped
## 4.1.2
* Updated `package:angel3_container`

View file

@ -38,7 +38,7 @@ void main(List<String> args) async {
try {
ctx.setAlpnProtocols(['h2'], true);
} catch (e, st) {
app.logger!.severe(
app.logger.severe(
'Cannot set ALPN protocol on server to `h2`. The server will only serve HTTP/1.x.',
e,
st,

View file

@ -28,16 +28,16 @@ class WebSocketEvent<Data> {
class WebSocketAction {
String? id;
String? eventName;
var data;
Map<String, dynamic>? params;
dynamic data;
Map<String, dynamic> params;
WebSocketAction({this.id, this.eventName, this.data, this.params});
WebSocketAction({this.id, this.eventName, this.data, this.params = const {}});
factory WebSocketAction.fromJson(Map data) => WebSocketAction(
id: data['id'].toString(),
eventName: data['eventName'].toString(),
data: data['data'],
params: data['params'] as Map<String, dynamic>?);
params: data['params'] as Map<String, dynamic>? ?? {});
Map<String, dynamic> toJson() {
return {'id': id, 'eventName': eventName, 'data': data, 'params': params};

View file

@ -3,7 +3,6 @@ import 'dart:collection';
import 'dart:convert';
import 'package:angel3_client/angel3_client.dart';
import 'package:angel3_client/base_angel_client.dart';
import 'package:angel3_http_exception/angel3_http_exception.dart';
import 'package:http/src/base_client.dart' as http;
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:web_socket_channel/status.dart' as status;
@ -219,7 +218,7 @@ abstract class BaseWebSocketClient extends BaseAngelClient {
_socket = null;
if (reconnectOnClose == true) {
Timer.periodic(reconnectInterval!, (Timer timer) async {
var result;
WebSocketChannel? result;
try {
result = await connect(timeout: reconnectInterval);
@ -434,10 +433,6 @@ class WebSocketsService<Id, Data> extends Service<Id, Data?> {
params: params ?? {}));
return null;
}
/// No longer necessary.
@deprecated
Service unwrap() => this;
}
/// Contains a dynamic Map of [WebSocketEvent] streams.
@ -461,6 +456,8 @@ class WebSocketExtraneousEventHandler {
}
void _close() {
_events.values.forEach((s) => s.close());
for (var s in _events.values) {
s.close();
}
}
}

View file

@ -4,7 +4,6 @@ library angel3_websocket.browser;
import 'dart:async';
import 'dart:html';
import 'package:angel3_client/angel3_client.dart';
import 'package:angel3_http_exception/angel3_http_exception.dart';
import 'package:http/browser_client.dart' as http;
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:web_socket_channel/html.dart';

View file

@ -6,27 +6,6 @@ const String modifyAction = 'modify';
const String updateAction = 'update';
const String removeAction = 'remove';
@deprecated
const String ACTION_AUTHENTICATE = authenticateAction;
@deprecated
const String ACTION_INDEX = indexAction;
@deprecated
const String ACTION_READ = readAction;
@deprecated
const String ACTION_CREATE = createAction;
@deprecated
const String ACTION_MODIFY = modifyAction;
@deprecated
const String ACTION_UPDATE = updateAction;
@deprecated
const String ACTION_REMOVE = removeAction;
const String authenticatedEvent = 'authenticated';
const String errorEvent = 'error';
const String indexedEvent = 'indexed';
@ -36,30 +15,6 @@ const String modifiedEvent = 'modified';
const String updatedEvent = 'updated';
const String removedEvent = 'removed';
@deprecated
const String EVENT_AUTHENTICATED = authenticatedEvent;
@deprecated
const String EVENT_ERROR = errorEvent;
@deprecated
const String EVENT_INDEXED = indexedEvent;
@deprecated
const String EVENT_READ = readEvent;
@deprecated
const String EVENT_CREATED = createdEvent;
@deprecated
const String EVENT_MODIFIED = modifiedEvent;
@deprecated
const String EVENT_UPDATED = updatedEvent;
@deprecated
const String EVENT_REMOVED = removedEvent;
/// The standard Angel service actions.
const List<String> actions = <String>[
indexAction,
@ -70,9 +25,6 @@ const List<String> actions = <String>[
removeAction
];
@deprecated
const List<String> ACTIONS = actions;
/// The standard Angel service events.
const List<String> events = <String>[
indexedEvent,
@ -82,6 +34,3 @@ const List<String> events = <String>[
updatedEvent,
removedEvent
];
@deprecated
const List<String> EVENTS = events;

View file

@ -6,8 +6,8 @@ import 'package:angel3_framework/angel3_framework.dart';
/// If [provider] is `null`, any provider will be blocked.
HookedServiceEventListener doNotBroadcast([provider]) {
return (HookedServiceEvent e) {
if (e.params != null && e.params!.containsKey('provider')) {
var eParam = e.params!;
if (e.params.containsKey('provider')) {
var eParam = e.params;
var deny = false;
var providers = provider is Iterable ? provider : [provider];

View file

@ -10,11 +10,11 @@ import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_framework/http.dart';
import 'package:angel3_framework/http2.dart';
import 'package:belatuk_merge_map/belatuk_merge_map.dart';
import 'package:logging/logging.dart';
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';
@ -27,8 +27,6 @@ typedef WebSocketResponseSerializer = String Function(dynamic data);
/// Broadcasts events from [HookedService]s, and handles incoming [WebSocketAction]s.
class AngelWebSocket {
final _log = Logger('AngelWebSocket');
final List<WebSocketContext> _clients = <WebSocketContext>[];
final List<String> _servicesAlreadyWired = [];
@ -65,6 +63,8 @@ class AngelWebSocket {
List<String> get servicesAlreadyWired =>
List.unmodifiable(_servicesAlreadyWired);
Logger get _log => app.logger;
/// Used to notify other nodes of an event's firing. Good for scaled applications.
final StreamChannel<WebSocketEvent>? synchronizationChannel;
@ -152,7 +152,7 @@ class AngelWebSocket {
FutureOr<dynamic> Function(HookedServiceEvent<dynamic, dynamic, Service> e)
serviceHook(String path) {
return (HookedServiceEvent e) async {
if (e.params != null && e.params!['broadcast'] == false) return;
if (e.params['broadcast'] == false) return;
var event = await transformEvent(e);
event.eventName = '$path::${event.eventName}';
@ -160,8 +160,8 @@ class AngelWebSocket {
dynamic _filter(WebSocketContext socket) {
if (e.service.configuration.containsKey('ws:filter')) {
return e.service.configuration['ws:filter'](e, socket);
} else if (e.params != null && e.params!.containsKey('ws:filter')) {
return e.params?['ws:filter'](e, socket);
} else if (e.params.containsKey('ws:filter')) {
return e.params['ws:filter'](e, socket);
} else {
return true;
}
@ -212,11 +212,11 @@ class AngelWebSocket {
var actionName = split[1];
if (action.params is! Map) action.params = <String, dynamic>{};
//if (action.params is! Map) action.params = <String, dynamic>{};
if (allowClientParams != true) {
if (action.params!['query'] is Map) {
action.params = {'query': action.params!['query']};
if (action.params['query'] is Map) {
action.params = {'query': action.params['query']};
} else {
action.params = {};
}
@ -272,11 +272,11 @@ 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<AngelAuth>();
var jwt = action.params!['query']['jwt'] as String;
var jwt = action.params['query']['jwt'] as String;
AuthToken token;
token = AuthToken.validate(jwt, auth.hmac);
@ -330,7 +330,7 @@ class AngelWebSocket {
throw AngelHttpException.badRequest();
}
if (fromJson is Map && fromJson.containsKey('eventName')) {
if (fromJson.containsKey('eventName')) {
socket._onAction.add(WebSocketAction.fromJson(fromJson));
socket.on
._getStreamForEvent(fromJson['eventName'].toString())!
@ -361,16 +361,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,
var err = AngelHttpException(
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);
var err = AngelHttpException();
socket.sendError(err);
app.logger?.severe(e.toString(), e, st);
app.logger.severe(e.toString(), e, st);
}
}
@ -391,10 +391,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<AngelWebSocket>(this);
app.container.registerSingleton<AngelWebSocket>(this);
}
// Set up services

View file

@ -20,7 +20,7 @@ class WebSocketContext {
final StreamController<void> _onAuthenticated = StreamController();
final StreamController<Null> _onClose = StreamController<Null>();
final StreamController<void> _onClose = StreamController<void>();
final StreamController _onData = StreamController();
@ -31,7 +31,7 @@ class WebSocketContext {
Stream<void> get onAuthenticated => _onAuthenticated.stream;
/// Fired once the underlying [WebSocket] closes.
Stream<Null> get onClose => _onClose.stream;
Stream<void> get onClose => _onClose.stream;
/// Fired when any data is sent through [channel].
Stream get onData => _onData.stream;
@ -45,7 +45,7 @@ class WebSocketContext {
await _onAction.close();
await _onAuthenticated.close();
await _onData.close();
_onClose.add(null);
//_onClose.add(null);
await _onClose.close();
});
}

View file

@ -38,7 +38,7 @@ class WebSocketController extends Controller {
@override
Future configureServer(Angel app) async {
if (findExpose(app.container!.reflector) != null) {
if (findExpose(app.container.reflector) != null) {
await super.configureServer(app);
}

View file

@ -1,15 +1,15 @@
name: angel3_websocket
version: 4.1.2
version: 6.0.0
description: This library provides WebSockets support for Angel3 framework.
homepage: https://angel3-framework.web.app/
repository: https://github.com/dukefirehawk/angel/tree/master/packages/websocket
environment:
sdk: '>=2.12.0 <3.0.0'
sdk: '>=2.15.0 <3.0.0'
dependencies:
angel3_auth: ^4.1.0
angel3_client: ^4.1.0
angel3_framework: ^4.2.0
angel3_http_exception: ^3.0.0
angel3_auth: ^6.0.0
angel3_client: ^6.0.0
angel3_framework: ^6.0.0
angel3_http_exception: ^6.0.0
belatuk_merge_map: ^3.0.0
http: ^0.13.1
meta: ^1.3.0
@ -18,8 +18,24 @@ dependencies:
collection: ^1.15.0
logging: ^1.0.1
dev_dependencies:
angel3_container: ^3.1.0
angel3_model: ^3.1.0
angel3_container: ^6.0.0
angel3_model: ^6.0.0
test: ^1.17.5
lints: ^1.0.0
dependency_overrides:
angel3_container:
path: ../container/angel_container
angel3_framework:
path: ../framework
angel3_http_exception:
path: ../http_exception
angel3_model:
path: ../model
angel3_route:
path: ../route
angel3_mock_request:
path: ../mock_request
angel3_auth:
path: ../auth
angel3_client:
path: ../client