fluttercommunity / plus_plugins

Flutter Community Plus Plugins
BSD 3-Clause "New" or "Revised" License
1.62k stars 983 forks source link

fix(package_info_plus): add support for file scheme on web #3345

Closed alvarobcprado closed 2 weeks ago

alvarobcprado commented 2 weeks ago

Description

Web apps using file scheme cannot retrieve version.json, PackageInfoPlusWebPlugin.versionJsonUrl calls Uri.origin which throws StateError if scheme is not http or https

Checklist

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

miquelbeltran commented 2 weeks ago

Hey! thanks for the contribution.

Do you mean when calling fromPlatform with a file:// scheme, right? e.g. PackageInfo.fromPlatform(baseUrl: 'file://....')

alvarobcprado commented 2 weeks ago

Yes @miquelbeltran , when calling fromPlatform with a file:// scheme, a Bad state: Origin is only applicable to schemes http and https error is throwed from baseUri._origin inside the PackageInfoPlusWebPlugin.versionJsonUrl method.

   /// Get version.json full url.
  Uri versionJsonUrl(String baseUrl, int cacheBuster) {
    final baseUri = Uri.parse(baseUrl);
    final regExp = RegExp(r'[^/]+\.html.*');
    final String originPath =
        '${baseUri._origin}${baseUri.path.replaceAll(regExp, '')}';

...

extension _UriOrigin on Uri {
  /// Get origin.
  ///
  /// This is different from [Uri.origin] because that has checks to prevent
  /// non-http/https use-cases.
  String get _origin {
    if (isScheme('chrome-extension')) {
      return '$scheme://$host';
    }
    return origin;
  }
}
miquelbeltran commented 2 weeks ago

Got it! thanks for the clarification