From af15b06033b1cc87e1c6cbe7916f635e89d9bb2f Mon Sep 17 00:00:00 2001
From: Moritz <mosum@google.com>
Date: Thu, 4 Apr 2024 09:02:22 -0700
Subject: [PATCH] Revert "Support WASM for `package:intl` (#802)" (#822)

This reverts commit aa45d57a14d0de1cfbd3933b722e13f8c910958e.
---
 .github/workflows/intl.yml                    |  4 --
 pkgs/intl/CHANGELOG.md                        |  3 --
 pkgs/intl/lib/find_locale.dart                |  2 +-
 pkgs/intl/lib/intl_browser.dart               |  2 +-
 .../lib/src/http_request_data_reader.dart     | 45 +++++++++-------
 .../lib/src/locale/locale_extensions.dart     |  2 +-
 .../lib/src/locale/locale_implementation.dart |  2 +-
 pkgs/intl/pubspec.yaml                        |  5 +-
 .../test/number_format_compact_web_test.dart  | 52 ++++++++-----------
 9 files changed, 54 insertions(+), 63 deletions(-)

diff --git a/.github/workflows/intl.yml b/.github/workflows/intl.yml
index c834fb0c..e18e4aa5 100644
--- a/.github/workflows/intl.yml
+++ b/.github/workflows/intl.yml
@@ -42,7 +42,3 @@ jobs:
 
       - run: dart test
         if: ${{matrix.run-tests}}
-
-      - name: Run Chrome tests - wasm
-        run: dart test --platform chrome --compiler dart2wasm
-        if: always() && matrix.sdk == 'dev'
diff --git a/pkgs/intl/CHANGELOG.md b/pkgs/intl/CHANGELOG.md
index 30f46ce1..bbb9208c 100644
--- a/pkgs/intl/CHANGELOG.md
+++ b/pkgs/intl/CHANGELOG.md
@@ -5,9 +5,6 @@
  * Add example for pub.dev.
  * Fix issues with AM/PM markers.
  * Update to CLDR v44.1.
- * Require Dart `^3.3`
- * Require `package:web` `^0.5.0`.
- * Support compiling to WASM
 
 ## 0.19.0
  * Update to CLDR v44.
diff --git a/pkgs/intl/lib/find_locale.dart b/pkgs/intl/lib/find_locale.dart
index 15b0e505..7e5688cc 100644
--- a/pkgs/intl/lib/find_locale.dart
+++ b/pkgs/intl/lib/find_locale.dart
@@ -4,6 +4,6 @@
 
 export 'src/intl_default.dart' // Stub implementation
     // Browser implementation
-    if (dart.library.js_interop) 'intl_browser.dart'
+    if (dart.library.html) 'intl_browser.dart'
     // Native implementation
     if (dart.library.io) 'intl_standalone.dart';
diff --git a/pkgs/intl/lib/intl_browser.dart b/pkgs/intl/lib/intl_browser.dart
index e14d7995..9a09ec57 100644
--- a/pkgs/intl/lib/intl_browser.dart
+++ b/pkgs/intl/lib/intl_browser.dart
@@ -9,7 +9,7 @@
 
 library intl_browser;
 
-import 'package:web/web.dart';
+import 'dart:html';
 import 'intl.dart';
 
 // TODO(alanknight): The need to do this by forcing the user to specially
diff --git a/pkgs/intl/lib/src/http_request_data_reader.dart b/pkgs/intl/lib/src/http_request_data_reader.dart
index edff5c39..66ea7099 100644
--- a/pkgs/intl/lib/src/http_request_data_reader.dart
+++ b/pkgs/intl/lib/src/http_request_data_reader.dart
@@ -8,36 +8,41 @@
 library http_request_data_reader;
 
 import 'dart:async';
-import 'package:http/http.dart';
+import 'dart:html';
 import 'intl_helpers.dart';
 
 class HttpRequestDataReader implements LocaleDataReader {
   /// The base url from which we read the data.
   String url;
-
   HttpRequestDataReader(this.url);
 
   @override
   Future<String> read(String locale) {
-    final Client client = Client();
-    return _getString('$url$locale.json', client).timeout(
-      Duration(seconds: 5),
-      onTimeout: () {
-        client.close();
-        throw TimeoutException('Timeout while reading $locale');
-      },
-    );
+    var request = HttpRequest();
+    request.timeout = 5000;
+    return _getString('$url$locale.json', request).then((r) => r.responseText!);
   }
 
-  Future<String> _getString(String url, Client client) async {
-    final response = await client.get(Uri.parse(url));
-
-    if ((response.statusCode >= 200 && response.statusCode < 300) ||
-        response.statusCode == 0 ||
-        response.statusCode == 304) {
-      return response.body;
-    } else {
-      throw Exception('Failed to load $url');
-    }
+  /// Read a string with the given request. This is a stripped down copy
+  /// of HttpRequest getString, but was the simplest way I could find to
+  /// issue a request with a timeout.
+  Future<HttpRequest> _getString(String url, HttpRequest xhr) {
+    var completer = Completer<HttpRequest>();
+    xhr.open('GET', url, async: true);
+    xhr.onLoad.listen((e) {
+      // Note: file:// URIs have status of 0.
+      if ((xhr.status! >= 200 && xhr.status! < 300) ||
+          xhr.status == 0 ||
+          xhr.status == 304) {
+        completer.complete(xhr);
+      } else {
+        completer.completeError(e);
+      }
+    });
+
+    xhr.onError.listen(completer.completeError);
+    xhr.send();
+
+    return completer.future;
   }
 }
diff --git a/pkgs/intl/lib/src/locale/locale_extensions.dart b/pkgs/intl/lib/src/locale/locale_extensions.dart
index a4fdd49e..c04937dc 100644
--- a/pkgs/intl/lib/src/locale/locale_extensions.dart
+++ b/pkgs/intl/lib/src/locale/locale_extensions.dart
@@ -70,7 +70,7 @@ class LocaleExtensions {
         'RegExp/${_otherExtensionsValidValuesRE.pattern}. '
         'Entries: ${otherExtensions.entries}.');
     assert(
-        _xExtensions == null || _validXExtensionsRE.hasMatch(_xExtensions),
+        _xExtensions == null || _validXExtensionsRE.hasMatch(_xExtensions!),
         '_xExtensions must match RegExp/${_validXExtensionsRE.pattern}/ '
         'but is "$_xExtensions".');
   }
diff --git a/pkgs/intl/lib/src/locale/locale_implementation.dart b/pkgs/intl/lib/src/locale/locale_implementation.dart
index c8d2972b..65cc282b 100644
--- a/pkgs/intl/lib/src/locale/locale_implementation.dart
+++ b/pkgs/intl/lib/src/locale/locale_implementation.dart
@@ -182,7 +182,7 @@ class LocaleImplementation extends Locale {
       if (scriptCode != null) out.add(scriptCode!);
       if (countryCode != null) out.add(countryCode!);
       out.addAll(variants);
-      if (_extensions != null) out.addAll(_extensions.subtags);
+      if (_extensions != null) out.addAll(_extensions!.subtags);
       _languageTag = out.join('-');
     }
     return _languageTag!;
diff --git a/pkgs/intl/pubspec.yaml b/pkgs/intl/pubspec.yaml
index 7756d22a..61cbef4f 100644
--- a/pkgs/intl/pubspec.yaml
+++ b/pkgs/intl/pubspec.yaml
@@ -7,18 +7,17 @@ description: >-
 repository: https://github.com/dart-lang/i18n/tree/main/pkgs/intl
 
 environment:
-  sdk: ^3.3.0
+  sdk: ^3.0.0
 
 dependencies:
   clock: ^1.1.0
-  http: ^1.0.0
   meta: ^1.0.2
   path: ^1.8.0
-  web: ^0.5.0
 
 dev_dependencies:
   benchmark_harness: ^2.2.0
   ffi: ^1.0.0
   fixnum: ^1.0.0
+  js: ^0.6.3
   lints: '>=1.0.0 <3.0.0'
   test: ^1.16.0
diff --git a/pkgs/intl/test/number_format_compact_web_test.dart b/pkgs/intl/test/number_format_compact_web_test.dart
index b18d50c1..ea245981 100644
--- a/pkgs/intl/test/number_format_compact_web_test.dart
+++ b/pkgs/intl/test/number_format_compact_web_test.dart
@@ -9,18 +9,13 @@
 // consistency when the bug is fixed. Also fix documentation and perhaps
 // merge tests: these tests currently also touch non-compact currency
 // formatting.
-import 'dart:js_interop';
-import 'dart:js_interop_unsafe';
 import 'package:intl/intl.dart' as intl;
+import 'package:js/js_util.dart' as js;
 import 'package:test/test.dart';
 
 import 'compact_number_test_data.dart' as testdata;
 import 'more_compact_number_test_data.dart' as more_testdata;
 
-extension on JSNumber {
-  external String toLocaleString(String locale, [JSObject? options]);
-}
-
 void main() {
   testdata.compactNumberTestData.forEach(_validate);
   more_testdata.cldr35CompactNumTests.forEach(_validateMore);
@@ -70,20 +65,19 @@ String _ecmaFormatNumber(String locale, num number,
     String? compactDisplay,
     int? maximumSignificantDigits,
     bool? useGrouping}) {
-  final options = JSObject();
-  if (notation != null) options['notation'] = notation.toJS;
+  var options = js.newObject();
+  if (notation != null) js.setProperty(options, 'notation', notation);
   if (compactDisplay != null) {
-    options['compactDisplay'] = compactDisplay.toJS;
+    js.setProperty(options, 'compactDisplay', compactDisplay);
   }
-  if (style != null) options['style'] = style.toJS;
-  if (currency != null) options['currency'] = currency.toJS;
+  if (style != null) js.setProperty(options, 'style', style);
+  if (currency != null) js.setProperty(options, 'currency', currency);
   if (maximumSignificantDigits != null) {
-    options['maximumSignificantDigits'] = maximumSignificantDigits.toJS;
+    js.setProperty(
+        options, 'maximumSignificantDigits', maximumSignificantDigits);
   }
-  if (useGrouping != null) {
-    options['useGrouping'] = useGrouping.toJS;
-  }
-  return number.toJS.toLocaleString(locale, options);
+  if (useGrouping != null) js.setProperty(options, 'useGrouping', useGrouping);
+  return js.callMethod(number, 'toLocaleString', [locale, options]);
 }
 
 var _unsupportedChromeLocales = [
@@ -158,40 +152,40 @@ void _validateLong(String locale, List<List<String>> expected) {
 }
 
 void _validateMore(more_testdata.CompactRoundingTestCase t) {
-  final options = JSObject();
-  options['notation'] = 'compact'.toJS;
+  var options = js.newObject();
+  js.setProperty(options, 'notation', 'compact');
   if (t.maximumIntegerDigits != null) {
-    options['maximumIntegerDigits'] = t.maximumIntegerDigits!.toJS;
+    js.setProperty(options, 'maximumIntegerDigits', t.maximumIntegerDigits);
   }
 
   if (t.minimumIntegerDigits != null) {
-    options['minimumIntegerDigits'] = t.minimumIntegerDigits!.toJS;
+    js.setProperty(options, 'minimumIntegerDigits', t.minimumIntegerDigits);
   }
 
   if (t.maximumFractionDigits != null) {
-    options['maximumFractionDigits'] = t.maximumFractionDigits!.toJS;
+    js.setProperty(options, 'maximumFractionDigits', t.maximumFractionDigits);
   }
 
   if (t.minimumFractionDigits != null) {
-    options['minimumFractionDigits'] = t.minimumFractionDigits!.toJS;
+    js.setProperty(options, 'minimumFractionDigits', t.minimumFractionDigits);
   }
 
   if (t.minimumExponentDigits != null) {
-    options['minimumExponentDigits'] = t.minimumExponentDigits!.toJS;
+    js.setProperty(options, 'minimumExponentDigits', t.minimumExponentDigits);
   }
 
   if (t.maximumSignificantDigits != null) {
-    options['maximumSignificantDigits'] = t.maximumSignificantDigits!.toJS;
+    js.setProperty(
+        options, 'maximumSignificantDigits', t.maximumSignificantDigits);
   }
 
   if (t.minimumSignificantDigits != null) {
-    options['minimumSignificantDigits'] = t.minimumSignificantDigits!.toJS;
+    js.setProperty(
+        options, 'minimumSignificantDigits', t.minimumSignificantDigits);
   }
 
   test(t.toString(), () {
-    expect(
-      t.number.toJS.toLocaleString('en-US', options),
-      t.expected,
-    );
+    expect(js.callMethod(t.number, 'toLocaleString', ['en-US', options]),
+        t.expected);
   });
 }