Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate FrontendServerDdcStrategyProvider and FrontendServerRequireStrategyProvider under a shared parent class. - [#2517](https://gith… #2518

Merged
merged 6 commits into from
Nov 12, 2024
8 changes: 4 additions & 4 deletions dwds/lib/dwds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 0 additions & 34 deletions dwds/lib/src/loaders/frontend_server_ddc.dart

This file was deleted.

32 changes: 0 additions & 32 deletions dwds/lib/src/loaders/frontend_server_require.dart

This file was deleted.

124 changes: 92 additions & 32 deletions dwds/lib/src/loaders/frontend_server_strategy_provider.dart
Original file line number Diff line number Diff line change
@@ -1,88 +1,90 @@
// 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;

abstract class FrontendServerStrategyProvider<T> {
final ReloadConfiguration configuration;
final AssetReader assetReader;
final PackageUriMapper packageUriMapper;
final Future<Map<String, String>> Function() digestsProvider;
final String basePath;
final BuildSettings buildSettings;
abstract class FrontendServerStrategyProvider<T extends LoadStrategy> {
final ReloadConfiguration _configuration;
final AssetReader _assetReader;
final PackageUriMapper _packageUriMapper;
final Future<Map<String, String>> Function() _digestsProvider;
final String _basePath;
final BuildSettings _buildSettings;

FrontendServerStrategyProvider(
this.configuration,
this.assetReader,
this.packageUriMapper,
this.digestsProvider,
this.buildSettings,
) : basePath = assetReader.basePath;
this._configuration,
this._assetReader,
this._packageUriMapper,
this._digestsProvider,
this._buildSettings,
) : _basePath = _assetReader.basePath;

T get strategy;

String removeBasePath(String path) {
if (basePath.isEmpty) return path;
String _removeBasePath(String path) {
if (_basePath.isEmpty) return path;
final stripped = stripLeadingSlashes(path);
return stripLeadingSlashes(stripped.substring(basePath.length));
return stripLeadingSlashes(stripped.substring(_basePath.length));
}

String addBasePath(String serverPath) => basePath.isEmpty
String _addBasePath(String serverPath) => _basePath.isEmpty
? stripLeadingSlashes(serverPath)
: '$basePath/${stripLeadingSlashes(serverPath)}';
: '$_basePath/${stripLeadingSlashes(serverPath)}';

String removeJsExtension(String path) =>
String _removeJsExtension(String path) =>
path.endsWith('.js') ? p.withoutExtension(path) : path;

Future<Map<String, String>> moduleProvider(
Future<Map<String, String>> _moduleProvider(
MetadataProvider metadataProvider,
) async =>
(await metadataProvider.moduleToModulePath).map(
(key, value) =>
MapEntry(key, stripLeadingSlashes(removeJsExtension(value))),
MapEntry(key, stripLeadingSlashes(_removeJsExtension(value))),
);

Future<String?> moduleForServerPath(
Future<String?> _moduleForServerPath(
MetadataProvider metadataProvider,
String serverPath,
) async {
final modulePathToModule = await metadataProvider.modulePathToModule;
final relativeServerPath = removeBasePath(serverPath);
final relativeServerPath = _removeBasePath(serverPath);
return modulePathToModule[relativeServerPath];
}

Future<String> serverPathForModule(
Future<String> _serverPathForModule(
MetadataProvider metadataProvider,
String module,
) async =>
addBasePath((await metadataProvider.moduleToModulePath)[module] ?? '');
_addBasePath((await metadataProvider.moduleToModulePath)[module] ?? '');

Future<String> sourceMapPathForModule(
Future<String> _sourceMapPathForModule(
MetadataProvider metadataProvider,
String module,
) async =>
addBasePath((await metadataProvider.moduleToSourceMap)[module] ?? '');
_addBasePath((await metadataProvider.moduleToSourceMap)[module] ?? '');

String? serverPathForAppUri(String appUrl) {
String? _serverPathForAppUri(String appUrl) {
final appUri = Uri.parse(appUrl);
if (appUri.isScheme('org-dartlang-app')) {
return addBasePath(appUri.path);
return _addBasePath(appUri.path);
}
if (appUri.isScheme('package')) {
final resolved = packageUriMapper.packageUriToServerPath(appUri);
final resolved = _packageUriMapper.packageUriToServerPath(appUri);
if (resolved != null) {
return resolved;
}
}
return null;
}

Future<Map<String, ModuleInfo>> moduleInfoForProvider(
Future<Map<String, ModuleInfo>> _moduleInfoForProvider(
MetadataProvider metadataProvider,
) async {
final modules = await metadataProvider.moduleToModulePath;
Expand All @@ -99,3 +101,61 @@ abstract class FrontendServerStrategyProvider<T> {
return result;
}
}

/// Provides a [DdcStrategy] suitable for use with Frontend Server.
class FrontendServerDdcStrategyProvider
extends FrontendServerStrategyProvider<DdcStrategy> {
late final DdcStrategy _ddcStrategy = DdcStrategy(
srujzs marked this conversation as resolved.
Show resolved Hide resolved
_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<RequireStrategy> {
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;
}
22 changes: 13 additions & 9 deletions dwds/test/fixtures/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}.',
)
Expand Down