diff --git a/dwds/CHANGELOG.md b/dwds/CHANGELOG.md index 422927cc4..2172c4ea9 100644 --- a/dwds/CHANGELOG.md +++ b/dwds/CHANGELOG.md @@ -1,5 +1,6 @@ ## 24.2.0-wip +- Consolidate `FrontendServerDdcStrategyProvider` and `FrontendServerRequireStrategyProvider` under a shared parent class. - [#2517](https://github.com/dart-lang/webdev/issues/2517) - Remove `build_daemon_ddc_and_canary_evaluate_test`. - [2512](https://github.com/dart-lang/webdev/issues/2512) - Replace deprecated JS code `this.__proto__` with `Object.getPrototypeOf(this)`. - [#2500](https://github.com/dart-lang/webdev/pull/2500) - Migrate injected client code to `package:web`. - [#2491](https://github.com/dart-lang/webdev/pull/2491) diff --git a/dwds/lib/dwds.dart b/dwds/lib/dwds.dart index 9ab8282d8..25d0e3273 100644 --- a/dwds/lib/dwds.dart +++ b/dwds/lib/dwds.dart @@ -20,10 +20,10 @@ export 'src/handlers/socket_connections.dart'; export 'src/loaders/build_runner_require.dart' show BuildRunnerRequireStrategyProvider; export 'src/loaders/ddc.dart' show DdcStrategy; -export 'src/loaders/frontend_server_ddc.dart' - show FrontendServerDdcStrategyProvider; -export 'src/loaders/frontend_server_require.dart' - show FrontendServerRequireStrategyProvider; +export 'src/loaders/frontend_server_strategy_provider.dart' + show + FrontendServerDdcStrategyProvider, + FrontendServerRequireStrategyProvider; export 'src/loaders/require.dart' show RequireStrategy; export 'src/loaders/strategy.dart' show LoadStrategy, ReloadConfiguration, BuildSettings; diff --git a/dwds/lib/src/loaders/frontend_server_require.dart b/dwds/lib/src/loaders/frontend_server_require.dart deleted file mode 100644 index c3c8ff1b9..000000000 --- a/dwds/lib/src/loaders/frontend_server_require.dart +++ /dev/null @@ -1,114 +0,0 @@ -// Copyright 2020 The Dart Authors. 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:dwds/src/debugging/metadata/provider.dart'; -import 'package:dwds/src/loaders/require.dart'; -import 'package:dwds/src/loaders/strategy.dart'; -import 'package:dwds/src/readers/asset_reader.dart'; -import 'package:dwds/src/services/expression_compiler.dart'; -import 'package:path/path.dart' as p; - -/// Provides a [RequireStrategy] suitable for use with Frontend Server. -class FrontendServerRequireStrategyProvider { - final ReloadConfiguration _configuration; - final AssetReader _assetReader; - final PackageUriMapper _packageUriMapper; - final Future> Function() _digestsProvider; - final String _basePath; - final BuildSettings _buildSettings; - - late final RequireStrategy _requireStrategy = RequireStrategy( - _configuration, - _moduleProvider, - (_) => _digestsProvider(), - _moduleForServerPath, - _serverPathForModule, - _sourceMapPathForModule, - _serverPathForAppUri, - _moduleInfoForProvider, - _assetReader, - _buildSettings, - ); - - FrontendServerRequireStrategyProvider( - this._configuration, - this._assetReader, - this._packageUriMapper, - this._digestsProvider, - this._buildSettings, - ) : _basePath = _assetReader.basePath; - - RequireStrategy get strategy => _requireStrategy; - - String _removeBasePath(String path) { - if (_basePath.isEmpty) return path; - - final stripped = stripLeadingSlashes(path); - return stripLeadingSlashes(stripped.substring(_basePath.length)); - } - - String _addBasePath(String serverPath) => _basePath.isEmpty - ? stripLeadingSlashes(serverPath) - : '$_basePath/${stripLeadingSlashes(serverPath)}'; - - Future> _moduleProvider( - MetadataProvider metadataProvider, - ) async => - (await metadataProvider.moduleToModulePath).map( - (key, value) => - MapEntry(key, stripLeadingSlashes(removeJsExtension(value))), - ); - - Future _moduleForServerPath( - MetadataProvider metadataProvider, - String serverPath, - ) async { - final modulePathToModule = await metadataProvider.modulePathToModule; - final relativeServerPath = _removeBasePath(serverPath); - return modulePathToModule[relativeServerPath]; - } - - Future _serverPathForModule( - MetadataProvider metadataProvider, - String module, - ) async => - _addBasePath((await metadataProvider.moduleToModulePath)[module] ?? ''); - - Future _sourceMapPathForModule( - MetadataProvider metadataProvider, - String module, - ) async => - _addBasePath((await metadataProvider.moduleToSourceMap)[module] ?? ''); - - String? _serverPathForAppUri(String appUrl) { - final appUri = Uri.parse(appUrl); - if (appUri.isScheme('org-dartlang-app')) { - return _addBasePath(appUri.path); - } - if (appUri.isScheme('package')) { - final resolved = _packageUriMapper.packageUriToServerPath(appUri); - if (resolved != null) { - return resolved; - } - } - return null; - } - - Future> _moduleInfoForProvider( - MetadataProvider metadataProvider, - ) async { - final modules = await metadataProvider.moduleToModulePath; - final result = {}; - for (final module in modules.keys) { - final modulePath = modules[module]!; - result[module] = ModuleInfo( - // TODO: Save locations of full kernel files in ddc metadata. - // Issue: https://github.com/dart-lang/sdk/issues/43684 - p.setExtension(modulePath, '.full.dill'), - p.setExtension(modulePath, '.dill'), - ); - } - return result; - } -} diff --git a/dwds/lib/src/loaders/frontend_server_ddc.dart b/dwds/lib/src/loaders/frontend_server_strategy_provider.dart similarity index 71% rename from dwds/lib/src/loaders/frontend_server_ddc.dart rename to dwds/lib/src/loaders/frontend_server_strategy_provider.dart index cfc60594e..1dbc8a069 100644 --- a/dwds/lib/src/loaders/frontend_server_ddc.dart +++ b/dwds/lib/src/loaders/frontend_server_strategy_provider.dart @@ -1,16 +1,16 @@ -// Copyright 2023 The Dart Authors. All rights reserved. +// Copyright 2024 The Dart Authors. 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:dwds/src/debugging/metadata/provider.dart'; import 'package:dwds/src/loaders/ddc.dart'; +import 'package:dwds/src/loaders/require.dart'; import 'package:dwds/src/loaders/strategy.dart'; import 'package:dwds/src/readers/asset_reader.dart'; import 'package:dwds/src/services/expression_compiler.dart'; import 'package:path/path.dart' as p; -/// Provides a [DdcStrategy] suitable for use with Frontend Server. -class FrontendServerDdcStrategyProvider { +abstract class FrontendServerStrategyProvider { final ReloadConfiguration _configuration; final AssetReader _assetReader; final PackageUriMapper _packageUriMapper; @@ -18,22 +18,7 @@ class FrontendServerDdcStrategyProvider { final String _basePath; final BuildSettings _buildSettings; - late final DdcStrategy _ddcStrategy = DdcStrategy( - _configuration, - _moduleProvider, - (_) => _digestsProvider(), - _moduleForServerPath, - _serverPathForModule, - _sourceMapPathForModule, - _serverPathForAppUri, - _moduleInfoForProvider, - _assetReader, - _buildSettings, - (String _) => null, - null, - ); - - FrontendServerDdcStrategyProvider( + FrontendServerStrategyProvider( this._configuration, this._assetReader, this._packageUriMapper, @@ -41,11 +26,10 @@ class FrontendServerDdcStrategyProvider { this._buildSettings, ) : _basePath = _assetReader.basePath; - DdcStrategy get strategy => _ddcStrategy; + T get strategy; String _removeBasePath(String path) { if (_basePath.isEmpty) return path; - final stripped = stripLeadingSlashes(path); return stripLeadingSlashes(stripped.substring(_basePath.length)); } @@ -54,12 +38,15 @@ class FrontendServerDdcStrategyProvider { ? stripLeadingSlashes(serverPath) : '$_basePath/${stripLeadingSlashes(serverPath)}'; + String _removeJsExtension(String path) => + path.endsWith('.js') ? p.withoutExtension(path) : path; + Future> _moduleProvider( MetadataProvider metadataProvider, ) async => (await metadataProvider.moduleToModulePath).map( (key, value) => - MapEntry(key, stripLeadingSlashes(removeJsExtension(value))), + MapEntry(key, stripLeadingSlashes(_removeJsExtension(value))), ); Future _moduleForServerPath( @@ -114,3 +101,61 @@ class FrontendServerDdcStrategyProvider { return result; } } + +/// Provides a [DdcStrategy] suitable for use with Frontend Server. +class FrontendServerDdcStrategyProvider + extends FrontendServerStrategyProvider { + late final DdcStrategy _ddcStrategy = DdcStrategy( + _configuration, + _moduleProvider, + (_) => _digestsProvider(), + _moduleForServerPath, + _serverPathForModule, + _sourceMapPathForModule, + _serverPathForAppUri, + _moduleInfoForProvider, + _assetReader, + _buildSettings, + (String _) => null, + null, + ); + + FrontendServerDdcStrategyProvider( + super._configuration, + super._assetReader, + super._packageUriMapper, + super._digestsProvider, + super._buildSettings, + ); + + @override + DdcStrategy get strategy => _ddcStrategy; +} + +/// Provides a [RequireStrategy] suitable for use with Frontend Server. +class FrontendServerRequireStrategyProvider + extends FrontendServerStrategyProvider { + late final RequireStrategy _requireStrategy = RequireStrategy( + _configuration, + _moduleProvider, + (_) => _digestsProvider(), + _moduleForServerPath, + _serverPathForModule, + _sourceMapPathForModule, + _serverPathForAppUri, + _moduleInfoForProvider, + _assetReader, + _buildSettings, + ); + + FrontendServerRequireStrategyProvider( + super._configuration, + super._assetReader, + super._packageUriMapper, + super._digestsProvider, + super._buildSettings, + ); + + @override + RequireStrategy get strategy => _requireStrategy; +} diff --git a/dwds/test/fixtures/context.dart b/dwds/test/fixtures/context.dart index 4de952f66..5443000f2 100644 --- a/dwds/test/fixtures/context.dart +++ b/dwds/test/fixtures/context.dart @@ -15,8 +15,7 @@ import 'package:dwds/src/connections/app_connection.dart'; import 'package:dwds/src/connections/debug_connection.dart'; import 'package:dwds/src/debugging/webkit_debugger.dart'; import 'package:dwds/src/loaders/build_runner_require.dart'; -import 'package:dwds/src/loaders/frontend_server_ddc.dart'; -import 'package:dwds/src/loaders/frontend_server_require.dart'; +import 'package:dwds/src/loaders/frontend_server_strategy_provider.dart'; import 'package:dwds/src/loaders/strategy.dart'; import 'package:dwds/src/readers/proxy_server_asset_reader.dart'; import 'package:dwds/src/services/chrome_proxy_service.dart'; @@ -348,13 +347,18 @@ class TestContext { () async => {}, buildSettings, ).strategy, - ModuleFormat.ddc => FrontendServerDdcStrategyProvider( - testSettings.reloadConfiguration, - assetReader, - packageUriMapper, - () async => {}, - buildSettings, - ).strategy, + ModuleFormat.ddc => buildSettings.canaryFeatures + ? throw Exception( + '''Unsupported DDC module format ${testSettings.moduleFormat.name} + with canaryFeatures set to ${buildSettings.canaryFeatures}.''', + ) + : FrontendServerDdcStrategyProvider( + testSettings.reloadConfiguration, + assetReader, + packageUriMapper, + () async => {}, + buildSettings, + ).strategy, _ => throw Exception( 'Unsupported DDC module format ${testSettings.moduleFormat.name}.', )