Skip to content

Commit

Permalink
Consolidate and under a shared parent class. - [dart-lang#2517](dart-…
Browse files Browse the repository at this point in the history
  • Loading branch information
jyameo committed Nov 4, 2024
1 parent 472511d commit dad87d8
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 208 deletions.
1 change: 1 addition & 0 deletions dwds/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
128 changes: 23 additions & 105 deletions dwds/lib/src/loaders/frontend_server_ddc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,115 +2,33 @@
// 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/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;
import 'package:dwds/src/loaders/frontend_server_strategy_provider.dart';

/// Provides a [DdcStrategy] suitable for use with Frontend Server.
class FrontendServerDdcStrategyProvider {
final ReloadConfiguration _configuration;
final AssetReader _assetReader;
final PackageUriMapper _packageUriMapper;
final Future<Map<String, String>> Function() _digestsProvider;
final String _basePath;
final BuildSettings _buildSettings;

late final DdcStrategy _ddcStrategy = DdcStrategy(
_configuration,
_moduleProvider,
(_) => _digestsProvider(),
_moduleForServerPath,
_serverPathForModule,
_sourceMapPathForModule,
_serverPathForAppUri,
_moduleInfoForProvider,
_assetReader,
_buildSettings,
(String _) => null,
null,
);

class FrontendServerDdcStrategyProvider
extends FrontendServerStrategyProvider<DdcStrategy> {
FrontendServerDdcStrategyProvider(
this._configuration,
this._assetReader,
this._packageUriMapper,
this._digestsProvider,
this._buildSettings,
) : _basePath = _assetReader.basePath;

DdcStrategy get strategy => _ddcStrategy;

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<Map<String, String>> _moduleProvider(
MetadataProvider metadataProvider,
) async =>
(await metadataProvider.moduleToModulePath).map(
(key, value) =>
MapEntry(key, stripLeadingSlashes(removeJsExtension(value))),
);

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

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

Future<String> _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;
}
super.configuration,
super.assetReader,
super.packageUriMapper,
super.digestsProvider,
super.buildSettings,
);

Future<Map<String, ModuleInfo>> _moduleInfoForProvider(
MetadataProvider metadataProvider,
) async {
final modules = await metadataProvider.moduleToModulePath;
final result = <String, ModuleInfo>{};
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'),
@override
DdcStrategy get strategy => DdcStrategy(
configuration,
moduleProvider,
(_) => digestsProvider(),
moduleForServerPath,
serverPathForModule,
sourceMapPathForModule,
serverPathForAppUri,
moduleInfoForProvider,
assetReader,
buildSettings,
(String _) => null,
null,
);
}
return result;
}
}
124 changes: 21 additions & 103 deletions dwds/lib/src/loaders/frontend_server_require.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,113 +2,31 @@
// 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/frontend_server_strategy_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<Map<String, String>> Function() _digestsProvider;
final String _basePath;
final BuildSettings _buildSettings;

late final RequireStrategy _requireStrategy = RequireStrategy(
_configuration,
_moduleProvider,
(_) => _digestsProvider(),
_moduleForServerPath,
_serverPathForModule,
_sourceMapPathForModule,
_serverPathForAppUri,
_moduleInfoForProvider,
_assetReader,
_buildSettings,
);

class FrontendServerRequireStrategyProvider
extends FrontendServerStrategyProvider<RequireStrategy> {
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<Map<String, String>> _moduleProvider(
MetadataProvider metadataProvider,
) async =>
(await metadataProvider.moduleToModulePath).map(
(key, value) =>
MapEntry(key, stripLeadingSlashes(removeJsExtension(value))),
);

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

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

Future<String> _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;
}
super.configuration,
super.assetReader,
super.packageUriMapper,
super.digestsProvider,
super.buildSettings,
);

Future<Map<String, ModuleInfo>> _moduleInfoForProvider(
MetadataProvider metadataProvider,
) async {
final modules = await metadataProvider.moduleToModulePath;
final result = <String, ModuleInfo>{};
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'),
@override
RequireStrategy get strategy => RequireStrategy(
configuration,
moduleProvider,
(_) => digestsProvider(),
moduleForServerPath,
serverPathForModule,
sourceMapPathForModule,
serverPathForAppUri,
moduleInfoForProvider,
assetReader,
buildSettings,
);
}
return result;
}
}
105 changes: 105 additions & 0 deletions dwds/lib/src/loaders/frontend_server_strategy_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2023 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.

// frontend_server_strategy_provider.dart

// ignore_for_file: unused_element

import 'package:dwds/src/debugging/metadata/provider.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;

FrontendServerStrategyProvider(
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;
final stripped = stripLeadingSlashes(path);
return stripLeadingSlashes(stripped.substring(basePath.length));
}

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

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

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

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

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

Future<String> 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<Map<String, ModuleInfo>> moduleInfoForProvider(
MetadataProvider metadataProvider,
) async {
final modules = await metadataProvider.moduleToModulePath;
final result = <String, ModuleInfo>{};
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;
}
}

0 comments on commit dad87d8

Please sign in to comment.