Skip to content

Commit

Permalink
Add a flag to allow the default Client to be tree shaken away. (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan authored Mar 9, 2023
1 parent 2039fb3 commit ee03604
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 5 deletions.
76 changes: 73 additions & 3 deletions .github/workflows/dart.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkgs/http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 0.13.6-dev

* `BrowserClient` throws an exception if `send` is called after `close`.
* If `no_default_http_client=true` is set in the environment then disk usage
is reduced in some circumstances.
* Require Dart 2.19

## 0.13.5
Expand Down
8 changes: 7 additions & 1 deletion pkgs/http/lib/src/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import 'streamed_response.dart';
/// Create a [BrowserClient].
///
/// Used from conditional imports, matches the definition in `client_stub.dart`.
BaseClient createClient() => BrowserClient();
BaseClient createClient() {
if (const bool.fromEnvironment('no_default_http_client')) {
throw StateError('no_default_http_client was defined but runWithClient '
'was not used to configure a Client implementation.');
}
return BrowserClient();
}

/// A `dart:html`-based HTTP client that runs in the browser and is backed by
/// XMLHttpRequests.
Expand Down
11 changes: 11 additions & 0 deletions pkgs/http/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ Client? get zoneClient {
/// runWithClient(() => runApp(const MyApp()), clientFactory);
/// }
/// ```
///
/// If [runWithClient] is used and the environment defines
/// `no_default_http_client=true` then generated binaries may be smaller e.g.
/// ```shell
/// $ flutter build appbundle --dart-define=no_default_http_client=true ...
/// $ dart compile exe --define=no_default_http_client=true ...
/// ```
///
/// If `no_default_http_client=true` is set then any call to the [Client]
/// factory (i.e. `Client()`) outside of the [Zone] created by [runWithClient]
/// will throw [StateError].
R runWithClient<R>(R Function() body, Client Function() clientFactory,
{ZoneSpecification? zoneSpecification}) =>
runZoned(body,
Expand Down
8 changes: 7 additions & 1 deletion pkgs/http/lib/src/io_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import 'io_streamed_response.dart';
/// Create an [IOClient].
///
/// Used from conditional imports, matches the definition in `client_stub.dart`.
BaseClient createClient() => IOClient();
BaseClient createClient() {
if (const bool.fromEnvironment('no_default_http_client')) {
throw StateError('no_default_http_client was defined but runWithClient '
'was not used to configure a Client implementation.');
}
return IOClient();
}

/// Exception thrown when the underlying [HttpClient] throws a
/// [SocketException].
Expand Down
3 changes: 3 additions & 0 deletions pkgs/http/mono_pkg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ stages:
- test: --platform chrome
os:
- linux
- command: dart run --define=no_default_http_client=true test/no_default_http_client_test.dart
os:
- linux
18 changes: 18 additions & 0 deletions pkgs/http/test/no_default_http_client_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:http/http.dart' as http;
import 'package:test/test.dart';

/// Tests that no [http.Client] is provided by default when run with
/// `--define=no_default_http_client=true`.
void main() {
test('Client()', () {
if (const bool.fromEnvironment('no_default_http_client')) {
expect(http.Client.new, throwsA(isA<StateError>()));
} else {
expect(http.Client(), isA<http.Client>());
}
});
}
4 changes: 4 additions & 0 deletions tool/ci.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ee03604

Please sign in to comment.