From c6e7892c247fb008a8b3bded8679ea50c742f49b Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Tue, 21 Nov 2023 12:17:41 -0800 Subject: [PATCH] wip - pkg:web migration --- .github/workflows/dart.yml | 154 ++++++++++++++++++-------- pkgs/http/CHANGELOG.md | 4 +- pkgs/http/lib/src/browser_client.dart | 63 ++++++++--- pkgs/http/lib/src/client.dart | 2 +- pkgs/http/lib/src/client_stub.dart | 2 +- pkgs/http/pubspec.yaml | 3 +- pkgs/http/test/html/utils.dart | 2 +- 7 files changed, 164 insertions(+), 66 deletions(-) diff --git a/.github/workflows/dart.yml b/.github/workflows/dart.yml index da4811ab32..baebff25d7 100644 --- a/.github/workflows/dart.yml +++ b/.github/workflows/dart.yml @@ -40,16 +40,16 @@ jobs: - name: mono_repo self validate run: dart pub global run mono_repo generate --validate job_002: - name: "analyze_and_format; linux; Dart 3.0.0; PKGS: pkgs/http, pkgs/http_client_conformance_tests, pkgs/http_profile; `dart analyze --fatal-infos`" + name: "analyze_and_format; linux; Dart 3.0.0; PKGS: pkgs/http_client_conformance_tests, pkgs/http_profile; `dart analyze --fatal-infos`" runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 with: path: "~/.pub-cache/hosted" - key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0;packages:pkgs/http-pkgs/http_client_conformance_tests-pkgs/http_profile;commands:analyze_1" + key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0;packages:pkgs/http_client_conformance_tests-pkgs/http_profile;commands:analyze_1" restore-keys: | - os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0;packages:pkgs/http-pkgs/http_client_conformance_tests-pkgs/http_profile + os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0;packages:pkgs/http_client_conformance_tests-pkgs/http_profile os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0 os:ubuntu-latest;pub-cache-hosted os:ubuntu-latest @@ -60,15 +60,6 @@ jobs: - id: checkout name: Checkout repository uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - - id: pkgs_http_pub_upgrade - name: pkgs/http; dart pub upgrade - run: dart pub upgrade - if: "always() && steps.checkout.conclusion == 'success'" - working-directory: pkgs/http - - name: "pkgs/http; dart analyze --fatal-infos" - run: dart analyze --fatal-infos - if: "always() && steps.pkgs_http_pub_upgrade.conclusion == 'success'" - working-directory: pkgs/http - id: pkgs_http_client_conformance_tests_pub_upgrade name: pkgs/http_client_conformance_tests; dart pub upgrade run: dart pub upgrade @@ -88,6 +79,36 @@ jobs: if: "always() && steps.pkgs_http_profile_pub_upgrade.conclusion == 'success'" working-directory: pkgs/http_profile job_003: + name: "analyze_and_format; linux; Dart 3.2.0; PKG: pkgs/http; `dart analyze --fatal-infos`" + runs-on: ubuntu-latest + steps: + - name: Cache Pub hosted dependencies + uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 + with: + path: "~/.pub-cache/hosted" + key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.2.0;packages:pkgs/http;commands:analyze_1" + restore-keys: | + os:ubuntu-latest;pub-cache-hosted;sdk:3.2.0;packages:pkgs/http + os:ubuntu-latest;pub-cache-hosted;sdk:3.2.0 + os:ubuntu-latest;pub-cache-hosted + os:ubuntu-latest + - name: Setup Dart SDK + uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d + with: + sdk: "3.2.0" + - id: checkout + name: Checkout repository + uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 + - id: pkgs_http_pub_upgrade + name: pkgs/http; dart pub upgrade + run: dart pub upgrade + if: "always() && steps.checkout.conclusion == 'success'" + working-directory: pkgs/http + - name: "pkgs/http; dart analyze --fatal-infos" + run: dart analyze --fatal-infos + if: "always() && steps.pkgs_http_pub_upgrade.conclusion == 'success'" + working-directory: pkgs/http + job_004: name: "analyze_and_format; linux; Dart dev; PKGS: pkgs/http, pkgs/http_client_conformance_tests, pkgs/http_profile; `dart analyze --fatal-infos`" runs-on: ubuntu-latest steps: @@ -135,7 +156,7 @@ jobs: run: dart analyze --fatal-infos if: "always() && steps.pkgs_http_profile_pub_upgrade.conclusion == 'success'" working-directory: pkgs/http_profile - job_004: + job_005: name: "analyze_and_format; linux; Dart dev; PKGS: pkgs/http, pkgs/http_client_conformance_tests, pkgs/http_profile; `dart format --output=none --set-exit-if-changed .`" runs-on: ubuntu-latest steps: @@ -183,7 +204,7 @@ jobs: run: "dart format --output=none --set-exit-if-changed ." if: "always() && steps.pkgs_http_profile_pub_upgrade.conclusion == 'success'" working-directory: pkgs/http_profile - job_005: + job_006: name: "analyze_and_format; linux; Flutter stable; PKG: pkgs/flutter_http_example; `dart format --output=none --set-exit-if-changed .`" runs-on: ubuntu-latest steps: @@ -213,7 +234,7 @@ jobs: run: "dart format --output=none --set-exit-if-changed ." if: "always() && steps.pkgs_flutter_http_example_pub_upgrade.conclusion == 'success'" working-directory: pkgs/flutter_http_example - job_006: + job_007: name: "analyze_and_format; linux; Flutter stable; PKG: pkgs/flutter_http_example; `flutter analyze --fatal-infos`" runs-on: ubuntu-latest steps: @@ -243,17 +264,17 @@ jobs: run: flutter analyze --fatal-infos if: "always() && steps.pkgs_flutter_http_example_pub_upgrade.conclusion == 'success'" working-directory: pkgs/flutter_http_example - job_007: - name: "unit_test; linux; Dart 3.0.0; PKG: pkgs/http; `dart run --define=no_default_http_client=true test/no_default_http_client_test.dart`" + job_008: + name: "unit_test; linux; Dart 3.0.0; PKG: pkgs/http_profile; `dart test --platform vm`" runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 with: path: "~/.pub-cache/hosted" - key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0;packages:pkgs/http;commands:command_1" + key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0;packages:pkgs/http_profile;commands:test_2" restore-keys: | - os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0;packages:pkgs/http + os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0;packages:pkgs/http_profile os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0 os:ubuntu-latest;pub-cache-hosted os:ubuntu-latest @@ -264,6 +285,44 @@ jobs: - id: checkout name: Checkout repository uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 + - id: pkgs_http_profile_pub_upgrade + name: pkgs/http_profile; dart pub upgrade + run: dart pub upgrade + if: "always() && steps.checkout.conclusion == 'success'" + working-directory: pkgs/http_profile + - name: "pkgs/http_profile; dart test --platform vm" + run: dart test --platform vm + if: "always() && steps.pkgs_http_profile_pub_upgrade.conclusion == 'success'" + working-directory: pkgs/http_profile + needs: + - job_001 + - job_002 + - job_003 + - job_004 + - job_005 + - job_006 + - job_007 + job_009: + name: "unit_test; linux; Dart 3.2.0; PKG: pkgs/http; `dart run --define=no_default_http_client=true test/no_default_http_client_test.dart`" + runs-on: ubuntu-latest + steps: + - name: Cache Pub hosted dependencies + uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 + with: + path: "~/.pub-cache/hosted" + key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.2.0;packages:pkgs/http;commands:command_1" + restore-keys: | + os:ubuntu-latest;pub-cache-hosted;sdk:3.2.0;packages:pkgs/http + os:ubuntu-latest;pub-cache-hosted;sdk:3.2.0 + os:ubuntu-latest;pub-cache-hosted + os:ubuntu-latest + - name: Setup Dart SDK + uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d + with: + sdk: "3.2.0" + - id: checkout + name: Checkout repository + uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 - id: pkgs_http_pub_upgrade name: pkgs/http; dart pub upgrade run: dart pub upgrade @@ -280,24 +339,25 @@ jobs: - job_004 - job_005 - job_006 - job_008: - name: "unit_test; linux; Dart 3.0.0; PKG: pkgs/http; `dart test --platform chrome`" + - job_007 + job_010: + name: "unit_test; linux; Dart 3.2.0; PKG: pkgs/http; `dart test --platform chrome`" runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 with: path: "~/.pub-cache/hosted" - key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0;packages:pkgs/http;commands:test_3" + key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.2.0;packages:pkgs/http;commands:test_3" restore-keys: | - os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0;packages:pkgs/http - os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0 + os:ubuntu-latest;pub-cache-hosted;sdk:3.2.0;packages:pkgs/http + os:ubuntu-latest;pub-cache-hosted;sdk:3.2.0 os:ubuntu-latest;pub-cache-hosted os:ubuntu-latest - name: Setup Dart SDK uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d with: - sdk: "3.0.0" + sdk: "3.2.0" - id: checkout name: Checkout repository uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 @@ -317,24 +377,25 @@ jobs: - job_004 - job_005 - job_006 - job_009: - name: "unit_test; linux; Dart 3.0.0; PKGS: pkgs/http, pkgs/http_profile; `dart test --platform vm`" + - job_007 + job_011: + name: "unit_test; linux; Dart 3.2.0; PKG: pkgs/http; `dart test --platform vm`" runs-on: ubuntu-latest steps: - name: Cache Pub hosted dependencies uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 with: path: "~/.pub-cache/hosted" - key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0;packages:pkgs/http-pkgs/http_profile;commands:test_2" + key: "os:ubuntu-latest;pub-cache-hosted;sdk:3.2.0;packages:pkgs/http;commands:test_2" restore-keys: | - os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0;packages:pkgs/http-pkgs/http_profile - os:ubuntu-latest;pub-cache-hosted;sdk:3.0.0 + os:ubuntu-latest;pub-cache-hosted;sdk:3.2.0;packages:pkgs/http + os:ubuntu-latest;pub-cache-hosted;sdk:3.2.0 os:ubuntu-latest;pub-cache-hosted os:ubuntu-latest - name: Setup Dart SDK uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d with: - sdk: "3.0.0" + sdk: "3.2.0" - id: checkout name: Checkout repository uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 @@ -347,15 +408,6 @@ jobs: run: dart test --platform vm if: "always() && steps.pkgs_http_pub_upgrade.conclusion == 'success'" working-directory: pkgs/http - - id: pkgs_http_profile_pub_upgrade - name: pkgs/http_profile; dart pub upgrade - run: dart pub upgrade - if: "always() && steps.checkout.conclusion == 'success'" - working-directory: pkgs/http_profile - - name: "pkgs/http_profile; dart test --platform vm" - run: dart test --platform vm - if: "always() && steps.pkgs_http_profile_pub_upgrade.conclusion == 'success'" - working-directory: pkgs/http_profile needs: - job_001 - job_002 @@ -363,7 +415,8 @@ jobs: - job_004 - job_005 - job_006 - job_010: + - job_007 + job_012: name: "unit_test; linux; Dart dev; PKG: pkgs/http; `dart run --define=no_default_http_client=true test/no_default_http_client_test.dart`" runs-on: ubuntu-latest steps: @@ -400,7 +453,8 @@ jobs: - job_004 - job_005 - job_006 - job_011: + - job_007 + job_013: name: "unit_test; linux; Dart dev; PKG: pkgs/http; `dart test --platform chrome`" runs-on: ubuntu-latest steps: @@ -437,7 +491,8 @@ jobs: - job_004 - job_005 - job_006 - job_012: + - job_007 + job_014: name: "unit_test; linux; Dart dev; PKGS: pkgs/http, pkgs/http_profile; `dart test --platform vm`" runs-on: ubuntu-latest steps: @@ -483,7 +538,8 @@ jobs: - job_004 - job_005 - job_006 - job_013: + - job_007 + job_015: name: "unit_test; linux; Flutter stable; PKG: pkgs/flutter_http_example; `flutter test --platform chrome`" runs-on: ubuntu-latest steps: @@ -520,7 +576,8 @@ jobs: - job_004 - job_005 - job_006 - job_014: + - job_007 + job_016: name: "unit_test; linux; Flutter stable; PKG: pkgs/flutter_http_example; `flutter test`" runs-on: ubuntu-latest steps: @@ -557,7 +614,8 @@ jobs: - job_004 - job_005 - job_006 - job_015: + - job_007 + job_017: name: "unit_test; macos; Flutter stable; PKG: pkgs/flutter_http_example; `flutter test`" runs-on: macos-latest steps: @@ -594,7 +652,8 @@ jobs: - job_004 - job_005 - job_006 - job_016: + - job_007 + job_018: name: "unit_test; windows; Flutter stable; PKG: pkgs/flutter_http_example; `flutter test`" runs-on: windows-latest steps: @@ -621,3 +680,4 @@ jobs: - job_004 - job_005 - job_006 + - job_007 diff --git a/pkgs/http/CHANGELOG.md b/pkgs/http/CHANGELOG.md index 6d39b104df..84316ff8a2 100644 --- a/pkgs/http/CHANGELOG.md +++ b/pkgs/http/CHANGELOG.md @@ -1,8 +1,10 @@ -## 1.1.1 +## 1.1.1-wip * `BrowserClient` throws `ClientException` when the `'Content-Length'` header is invalid. * `IOClient` trims trailing whitespace on header values. +* Require Dart 3.2 +* Browser: support Wasm by using package:web. ## 1.1.0 diff --git a/pkgs/http/lib/src/browser_client.dart b/pkgs/http/lib/src/browser_client.dart index 9345be0ce1..c685b0dc70 100644 --- a/pkgs/http/lib/src/browser_client.dart +++ b/pkgs/http/lib/src/browser_client.dart @@ -3,9 +3,11 @@ // BSD-style license that can be found in the LICENSE file. import 'dart:async'; -import 'dart:html'; +import 'dart:js_interop'; import 'dart:typed_data'; +import 'package:web/helpers.dart'; + import 'base_client.dart'; import 'base_request.dart'; import 'byte_stream.dart'; @@ -37,7 +39,7 @@ class BrowserClient extends BaseClient { /// The currently active XHRs. /// /// These are aborted if the client is closed. - final _xhrs = {}; + final _xhrs = {}; /// Whether to send credentials such as cookies or authorization headers for /// cross-site requests. @@ -55,18 +57,21 @@ class BrowserClient extends BaseClient { 'HTTP request failed. Client is already closed.', request.url); } var bytes = await request.finalize().toBytes(); - var xhr = HttpRequest(); + + var xhr = await HttpRequest.request( + '${request.url}', + method: request.method, + responseType: 'arraybuffer', + requestHeaders: request.headers, + sendData: bytes.toJS, + ); _xhrs.add(xhr); - xhr - ..open(request.method, '${request.url}', async: true) - ..responseType = 'arraybuffer' - ..withCredentials = withCredentials; - request.headers.forEach(xhr.setRequestHeader); var completer = Completer(); unawaited(xhr.onLoad.first.then((_) { - if (xhr.responseHeaders['content-length'] case final contentLengthHeader? + if (xhr.getResponseHeader('content-length') + case final contentLengthHeader? when !_digitRegex.hasMatch(contentLengthHeader)) { completer.completeError(ClientException( 'Invalid content-length header [$contentLengthHeader].', @@ -75,12 +80,17 @@ class BrowserClient extends BaseClient { return; } var body = (xhr.response as ByteBuffer).asUint8List(); - completer.complete(StreamedResponse( - ByteStream.fromBytes(body), xhr.status!, + + completer.complete( + StreamedResponse( + ByteStream.fromBytes(body), + xhr.status, contentLength: body.length, request: request, headers: xhr.responseHeaders, - reasonPhrase: xhr.statusText)); + reasonPhrase: xhr.statusText, + ), + ); })); unawaited(xhr.onError.first.then((_) { @@ -91,8 +101,6 @@ class BrowserClient extends BaseClient { StackTrace.current); })); - xhr.send(bytes); - try { return await completer.future; } finally { @@ -112,3 +120,30 @@ class BrowserClient extends BaseClient { _xhrs.clear(); } } + +extension on XMLHttpRequest { + Map get responseHeaders { + // from Closure's goog.net.Xhrio.getResponseHeaders. + var headers = {}; + var headersString = getAllResponseHeaders(); + var headersList = headersString.split('\r\n'); + for (var header in headersList) { + if (header.isEmpty) { + continue; + } + + var splitIdx = header.indexOf(': '); + if (splitIdx == -1) { + continue; + } + var key = header.substring(0, splitIdx).toLowerCase(); + var value = header.substring(splitIdx + 2); + if (headers.containsKey(key)) { + headers[key] = '${headers[key]}, $value'; + } else { + headers[key] = value; + } + } + return headers; + } +} diff --git a/pkgs/http/lib/src/client.dart b/pkgs/http/lib/src/client.dart index 9bceb887f4..85d933a85a 100644 --- a/pkgs/http/lib/src/client.dart +++ b/pkgs/http/lib/src/client.dart @@ -12,7 +12,7 @@ import '../http.dart' as http; import 'base_client.dart'; import 'base_request.dart'; import 'client_stub.dart' - if (dart.library.html) 'browser_client.dart' + if (dart.library.js_interop) 'browser_client.dart' if (dart.library.io) 'io_client.dart'; import 'exception.dart'; import 'response.dart'; diff --git a/pkgs/http/lib/src/client_stub.dart b/pkgs/http/lib/src/client_stub.dart index 1a34d50d7e..6384fd0a3f 100644 --- a/pkgs/http/lib/src/client_stub.dart +++ b/pkgs/http/lib/src/client_stub.dart @@ -6,4 +6,4 @@ import 'base_client.dart'; /// Implemented in `browser_client.dart` and `io_client.dart`. BaseClient createClient() => throw UnsupportedError( - 'Cannot create a client without dart:html or dart:io.'); + 'Cannot create a client without dart:js_interop or dart:io.'); diff --git a/pkgs/http/pubspec.yaml b/pkgs/http/pubspec.yaml index 37d397ea7d..b65b129764 100644 --- a/pkgs/http/pubspec.yaml +++ b/pkgs/http/pubspec.yaml @@ -4,12 +4,13 @@ description: A composable, multi-platform, Future-based API for HTTP requests. repository: https://github.com/dart-lang/http/tree/master/pkgs/http environment: - sdk: ^3.0.0 + sdk: ^3.2.0 dependencies: async: ^2.5.0 http_parser: ^4.0.0 meta: ^1.3.0 + web: ^0.4.0 dev_dependencies: dart_flutter_team_lints: ^2.0.0 diff --git a/pkgs/http/test/html/utils.dart b/pkgs/http/test/html/utils.dart index abe5808a99..501c621256 100644 --- a/pkgs/http/test/html/utils.dart +++ b/pkgs/http/test/html/utils.dart @@ -2,7 +2,7 @@ // 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 'dart:html'; +import 'package:web/helpers.dart'; export '../utils.dart';