Unverified Commit 7148cc61 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Migrate flutter_tool net.dart to null safety (#78922)

parent d79b1668
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'dart:async'; import 'dart:async';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
...@@ -21,13 +19,14 @@ typedef HttpClientFactory = HttpClient Function(); ...@@ -21,13 +19,14 @@ typedef HttpClientFactory = HttpClient Function();
typedef UrlTunneller = Future<String> Function(String url); typedef UrlTunneller = Future<String> Function(String url);
/// If [httpClientFactory] is null, a default [HttpClient] is used.
class Net { class Net {
Net({ Net({
HttpClientFactory httpClientFactory, HttpClientFactory? httpClientFactory,
@required Logger logger, required Logger logger,
@required Platform platform, required Platform platform,
}) : }) :
_httpClientFactory = httpClientFactory, _httpClientFactory = httpClientFactory ?? (() => HttpClient()),
_logger = logger, _logger = logger,
_platform = platform; _platform = platform;
...@@ -45,16 +44,16 @@ class Net { ...@@ -45,16 +44,16 @@ class Net {
/// returns an empty list. /// returns an empty list.
/// ///
/// If [maxAttempts] is exceeded, returns null. /// If [maxAttempts] is exceeded, returns null.
Future<List<int>> fetchUrl(Uri url, { Future<List<int>?> fetchUrl(Uri url, {
int maxAttempts, int? maxAttempts,
File destFile, File? destFile,
@visibleForTesting Duration durationOverride, @visibleForTesting Duration? durationOverride,
}) async { }) async {
int attempts = 0; int attempts = 0;
int durationSeconds = 1; int durationSeconds = 1;
while (true) { while (true) {
attempts += 1; attempts += 1;
_MemoryIOSink memorySink; _MemoryIOSink? memorySink;
IOSink sink; IOSink sink;
if (destFile == null) { if (destFile == null) {
memorySink = _MemoryIOSink(); memorySink = _MemoryIOSink();
...@@ -68,7 +67,7 @@ class Net { ...@@ -68,7 +67,7 @@ class Net {
destSink: sink, destSink: sink,
); );
if (result) { if (result) {
return memorySink?.writes?.takeBytes() ?? <int>[]; return memorySink?.writes.takeBytes() ?? <int>[];
} }
if (maxAttempts != null && attempts >= maxAttempts) { if (maxAttempts != null && attempts >= maxAttempts) {
...@@ -91,19 +90,14 @@ class Net { ...@@ -91,19 +90,14 @@ class Net {
// Returns true on success and false on failure. // Returns true on success and false on failure.
Future<bool> _attempt(Uri url, { Future<bool> _attempt(Uri url, {
IOSink destSink, IOSink? destSink,
bool onlyHeaders = false, bool onlyHeaders = false,
}) async { }) async {
assert(onlyHeaders || destSink != null); assert(onlyHeaders || destSink != null);
_logger.printTrace('Downloading: $url'); _logger.printTrace('Downloading: $url');
HttpClient httpClient; final HttpClient httpClient = _httpClientFactory();
if (_httpClientFactory != null) {
httpClient = _httpClientFactory();
} else {
httpClient = HttpClient();
}
HttpClientRequest request; HttpClientRequest request;
HttpClientResponse response; HttpClientResponse? response;
try { try {
if (onlyHeaders) { if (onlyHeaders) {
request = await httpClient.headUrl(url); request = await httpClient.headUrl(url);
...@@ -112,7 +106,7 @@ class Net { ...@@ -112,7 +106,7 @@ class Net {
} }
response = await request.close(); response = await request.close();
} on ArgumentError catch (error) { } on ArgumentError catch (error) {
final String overrideUrl = _platform.environment['FLUTTER_STORAGE_BASE_URL']; final String? overrideUrl = _platform.environment['FLUTTER_STORAGE_BASE_URL'];
if (overrideUrl != null && url.toString().contains(overrideUrl)) { if (overrideUrl != null && url.toString().contains(overrideUrl)) {
_logger.printError(error.toString()); _logger.printError(error.toString());
throwToolExit( throwToolExit(
...@@ -144,9 +138,9 @@ class Net { ...@@ -144,9 +138,9 @@ class Net {
// If we're making a HEAD request, we're only checking to see if the URL is // If we're making a HEAD request, we're only checking to see if the URL is
// valid. // valid.
if (onlyHeaders) { if (onlyHeaders) {
return response.statusCode == HttpStatus.ok; return response!.statusCode == HttpStatus.ok;
} }
if (response.statusCode != HttpStatus.ok) { if (response!.statusCode != HttpStatus.ok) {
if (response.statusCode > 0 && response.statusCode < 500) { if (response.statusCode > 0 && response.statusCode < 500) {
throwToolExit( throwToolExit(
'Download failed.\n' 'Download failed.\n'
...@@ -162,7 +156,7 @@ class Net { ...@@ -162,7 +156,7 @@ class Net {
_logger.printTrace('Received response from server, collecting bytes...'); _logger.printTrace('Received response from server, collecting bytes...');
try { try {
assert(destSink != null); assert(destSink != null);
await response.forEach(destSink.add); await response.forEach(destSink!.add);
return true; return true;
} on IOException catch (error) { } on IOException catch (error) {
_logger.printTrace('Download error: $error'); _logger.printTrace('Download error: $error');
...@@ -199,12 +193,12 @@ class _MemoryIOSink implements IOSink { ...@@ -199,12 +193,12 @@ class _MemoryIOSink implements IOSink {
} }
@override @override
void write(Object obj) { void write(Object? obj) {
add(encoding.encode('$obj')); add(encoding.encode('$obj'));
} }
@override @override
void writeln([ Object obj = '' ]) { void writeln([ Object? obj = '' ]) {
add(encoding.encode('$obj\n')); add(encoding.encode('$obj\n'));
} }
...@@ -221,7 +215,7 @@ class _MemoryIOSink implements IOSink { ...@@ -221,7 +215,7 @@ class _MemoryIOSink implements IOSink {
} }
@override @override
void addError(dynamic error, [ StackTrace stackTrace ]) { void addError(dynamic error, [ StackTrace? stackTrace ]) {
throw UnimplementedError(); throw UnimplementedError();
} }
......
...@@ -8,7 +8,6 @@ import '../android/gradle_utils.dart' as gradle; ...@@ -8,7 +8,6 @@ import '../android/gradle_utils.dart' as gradle;
import '../base/common.dart'; import '../base/common.dart';
import '../base/context.dart'; import '../base/context.dart';
import '../base/file_system.dart'; import '../base/file_system.dart';
import '../base/io.dart';
import '../base/net.dart'; import '../base/net.dart';
import '../base/terminal.dart'; import '../base/terminal.dart';
import '../convert.dart'; import '../convert.dart';
...@@ -91,7 +90,7 @@ class CreateCommand extends CreateBase { ...@@ -91,7 +90,7 @@ class CreateCommand extends CreateBase {
// Lazy-initialize the net utilities with values from the context. // Lazy-initialize the net utilities with values from the context.
Net _cachedNet; Net _cachedNet;
Net get _net => _cachedNet ??= Net( Net get _net => _cachedNet ??= Net(
httpClientFactory: context.get<HttpClientFactory>() ?? () => HttpClient(), httpClientFactory: context.get<HttpClientFactory>(),
logger: globals.logger, logger: globals.logger,
platform: globals.platform, platform: globals.platform,
); );
......
...@@ -11,7 +11,6 @@ import 'package:meta/meta.dart'; ...@@ -11,7 +11,6 @@ import 'package:meta/meta.dart';
import '../base/common.dart'; import '../base/common.dart';
import '../base/context.dart'; import '../base/context.dart';
import '../base/file_system.dart'; import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart'; import '../base/logger.dart';
import '../base/net.dart'; import '../base/net.dart';
import '../cache.dart'; import '../cache.dart';
...@@ -145,7 +144,7 @@ class UpdatePackagesCommand extends FlutterCommand { ...@@ -145,7 +144,7 @@ class UpdatePackagesCommand extends FlutterCommand {
// Lazy-initialize the net utilities with values from the context. // Lazy-initialize the net utilities with values from the context.
Net _cachedNet; Net _cachedNet;
Net get _net => _cachedNet ??= Net( Net get _net => _cachedNet ??= Net(
httpClientFactory: context.get<HttpClientFactory>() ?? () => HttpClient(), httpClientFactory: context.get<HttpClientFactory>(),
logger: globals.logger, logger: globals.logger,
platform: globals.platform, platform: globals.platform,
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment