Browse Source

perf(dynamite_runtime,nextcloud): BytesStreamExtension add stream based xml decoding

Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
pull/1162/head
Nikolas Rimikis 1 year ago
parent
commit
e5b04f44d2
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 20
      packages/dynamite/dynamite_runtime/lib/src/http_extensions.dart
  2. 2
      packages/dynamite/dynamite_runtime/pubspec.yaml
  3. 3
      packages/nextcloud/lib/src/webdav/client.dart

20
packages/dynamite/dynamite_runtime/lib/src/http_extensions.dart

@ -3,13 +3,18 @@ import 'dart:convert';
import 'dart:typed_data';
import 'package:universal_io/io.dart';
import 'package:xml/xml.dart';
import 'package:xml/xml_events.dart';
/// A stream of bytes.
///
/// Usually a `Stream<Uint8List>`.
typedef BytesStream = Stream<List<int>>;
final _utf8JsonDecoder = utf8.decoder.fuse(json.decoder);
final _jsonBytesConverter = utf8.decoder.fuse(json.decoder);
final _xmlBytesConverter =
utf8.decoder.fuse(XmlEventDecoder()).fuse(const XmlNormalizeEvents()).fuse(const XmlNodeDecoder());
/// Extension on byte streams that enable efficient transformations.
extension BytesStreamExtension on BytesStream {
@ -26,7 +31,18 @@ extension BytesStreamExtension on BytesStream {
Future<String> get string => transform(utf8.decoder).join();
/// Converts the stream into a JSON using the [utf8] encoding.
Future<Object?> get json => transform(_utf8JsonDecoder).first;
Future<Object?> get json => transform(_jsonBytesConverter).first;
/// Converts the stream into XML using the [utf8] encoding.
///
/// Returns the root element of this stream.
Future<XmlElement> get xml async {
final element = await transform(_xmlBytesConverter)
.expand((final events) => events)
.firstWhere((final element) => element is XmlElement);
return element as XmlElement;
}
}
/// Extension on a http responses.

2
packages/dynamite/dynamite_runtime/pubspec.yaml

@ -8,9 +8,11 @@ environment:
dependencies:
built_collection: ^5.0.0
built_value: ^8.0.1
collection: ^1.0.0
cookie_jar: ^4.0.7
meta: ^1.0.0
universal_io: ^2.0.0
xml: ^6.0.0
dev_dependencies:
build_runner: ^2.4.6

3
packages/nextcloud/lib/src/webdav/client.dart

@ -8,7 +8,6 @@ import 'package:nextcloud/src/webdav/path_uri.dart';
import 'package:nextcloud/src/webdav/props.dart';
import 'package:nextcloud/src/webdav/webdav.dart';
import 'package:universal_io/io.dart';
import 'package:xml/xml.dart' as xml;
/// Base path used on the server
final webdavBase = PathUri.parse('/remote.php/webdav');
@ -73,7 +72,7 @@ class WebDavClient {
}
Future<WebDavMultistatus> _parseResponse(final HttpClientResponse response) async =>
WebDavMultistatus.fromXmlElement(xml.XmlDocument.parse(await response.string).rootElement);
WebDavMultistatus.fromXmlElement(await response.xml);
Map<String, String> _getUploadHeaders({
required final DateTime? lastModified,

Loading…
Cancel
Save