Skip to content

Commit

Permalink
⚡️ Check Uint8List before doing byte conversions (#2045)
Browse files Browse the repository at this point in the history
Prevent `Uint8List.fromList` if it already is `Uint8List`.

### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [ ] I have added the required tests to prove the fix/feature I'm
adding
- [ ] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [x] I have updated the `CHANGELOG.md` in the corresponding package

---------

Signed-off-by: Alex Li <github@alexv525.com>
Co-authored-by: Peter Leibiger <kuhnroyal@gmail.com>
  • Loading branch information
AlexV525 and kuhnroyal authored Nov 26, 2023
1 parent d589d08 commit a7a7878
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ See the [Migration Guide][] for the complete breaking changes list.**
- Allow case-sensitive header keys with the `preserveHeaderCase` flag through options.
- Fix `receiveTimeout` for the `IOHttpClientAdapter`.
- Fix `receiveTimeout` for the `download` method of `DioForNative`.
- Improve the stream byte conversion.

## 5.3.4

Expand Down
4 changes: 3 additions & 1 deletion dio/lib/src/adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ class ResponseBody {
this.statusMessage,
this.isRedirect = false,
Map<String, List<String>>? headers,
}) : stream = Stream.value(Uint8List.fromList(bytes)),
}) : stream = Stream.value(
bytes is Uint8List ? bytes : Uint8List.fromList(bytes),
),
headers = headers ?? {};

/// Whether this response is a redirect.
Expand Down
4 changes: 3 additions & 1 deletion dio/lib/src/adapters/browser_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ class BrowserHttpClientAdapter implements HttpClientAdapter {
}
final completer = Completer<Uint8List>();
final sink = ByteConversionSink.withCallback(
(bytes) => completer.complete(Uint8List.fromList(bytes)),
(bytes) => completer.complete(
bytes is Uint8List ? bytes : Uint8List.fromList(bytes),
),
);
requestStream.listen(
sink.add,
Expand Down
1 change: 1 addition & 0 deletions plugins/native_dio_adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Adds `createCronetEngine` and `createCupertinoConfiguration`
to deprecate `cronetEngine` and `cupertinoConfiguration`
for the `NativeAdapter`, to avoid platform exceptions.
- Improve the request stream byte conversion.

## 1.1.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ class ConversionLayerAdapter implements HttpClientAdapter {
if (requestStream != null) {
final completer = Completer<Uint8List>();
final sink = ByteConversionSink.withCallback(
(bytes) => completer.complete(Uint8List.fromList(bytes)),
(bytes) => completer.complete(
bytes is Uint8List ? bytes : Uint8List.fromList(bytes),
),
);
requestStream.listen(
sink.add,
Expand Down

0 comments on commit a7a7878

Please sign in to comment.