import 'dart:async'; import 'dart:convert'; import 'dart:typed_data'; import 'package:universal_io/io.dart'; /// A stream of bytes. /// /// Usually a `Stream`. typedef BytesStream = Stream>; final _utf8JsonDecoder = utf8.decoder.fuse(json.decoder); /// Extension on byte streams that enable efficient transformations. extension BytesStreamExtension on BytesStream { /// Returns the all bytes of the stream. Future get bytes async { final buffer = BytesBuilder(); await forEach(buffer.add); return buffer.toBytes(); } /// Converts the stream into a `String` using the [utf8] encoding. Future get string => transform(utf8.decoder).join(); /// Converts the stream into a JSON using the [utf8] encoding. Future get json => transform(_utf8JsonDecoder).first; } /// Extension on a http responses. extension HttpClientResponseExtension on HttpClientResponse { /// Returns a map of headers. Map get responseHeaders { final responseHeaders = {}; headers.forEach((final name, final values) { responseHeaders[name] = values.last; }); return responseHeaders; } }