Skip to content

Commit

Permalink
Change parameters to Dwds.start
Browse files Browse the repository at this point in the history
  • Loading branch information
elliette committed Sep 13, 2023
1 parent 6b21ecf commit 793bcfb
Show file tree
Hide file tree
Showing 22 changed files with 197 additions and 175 deletions.
2 changes: 1 addition & 1 deletion dwds/lib/asset_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// BSD-style license that can be found in the LICENSE file.

export 'src/readers/asset_reader.dart'
show AssetReader, UrlEncoder, PackageUriMapper, stripLeadingSlashes;
show AssetReader, PackageUriMapper, stripLeadingSlashes;
7 changes: 7 additions & 0 deletions dwds/lib/config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

export 'src/config/app_metadata.dart' show AppMetadata;
export 'src/config/debug_settings.dart'
show UrlEncoder, DevToolsLauncher, DebugSettings;
70 changes: 26 additions & 44 deletions dwds/lib/dart_web_debug_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import 'dart:async';

import 'package:dwds/data/build_result.dart';
import 'package:dwds/src/config/app_metadata.dart';
import 'package:dwds/src/config/debug_settings.dart';
import 'package:dwds/src/connections/app_connection.dart';
import 'package:dwds/src/connections/debug_connection.dart';
import 'package:dwds/src/events.dart';
Expand All @@ -15,7 +17,6 @@ import 'package:dwds/src/loaders/strategy.dart';
import 'package:dwds/src/readers/asset_reader.dart';
import 'package:dwds/src/servers/devtools.dart';
import 'package:dwds/src/servers/extension_backend.dart';
import 'package:dwds/src/services/expression_compiler.dart';
import 'package:dwds/src/utilities/globals.dart';
import 'package:logging/logging.dart';
import 'package:shelf/shelf.dart';
Expand Down Expand Up @@ -67,35 +68,18 @@ class Dwds {
required Stream<BuildResult> buildResults,
required ConnectionProvider chromeConnection,
required LoadStrategy loadStrategy,
required bool enableDebugging,
// TODO(annagrin): make expressionCompiler argument required
// [issue 881](https://github.com/dart-lang/webdev/issues/881)
ExpressionCompiler? expressionCompiler,
bool enableDebugExtension = false,
String hostname = 'localhost',
bool useSseForDebugProxy = true,
bool useSseForDebugBackend = true,
bool useSseForInjectedClient = true,
UrlEncoder? urlEncoder,
bool spawnDds = true,
// TODO(elliette): DevTools is inconsistently capitalized throughout this
// file. Change all occurrences of devtools/Devtools to devTools/DevTools.
bool enableDevtoolsLaunch = true,
DevtoolsLauncher? devtoolsLauncher,
bool launchDevToolsInNewWindow = true,
bool emitDebugEvents = true,
bool isInternalBuild = false,
Future<bool> Function()? isFlutterApp,
required DebugSettings debugSettings,
required AppMetadata appMetadata,
}) async {
globalLoadStrategy = loadStrategy;
globalIsInternalBuild = isInternalBuild;
isFlutterApp ??= () => Future.value(true);
globalAppMetadata = appMetadata;
globalDebugSettings = debugSettings;

DevTools? devTools;
Future<String>? extensionUri;
ExtensionBackend? extensionBackend;
if (enableDebugExtension) {
final handler = useSseForDebugBackend
if (debugSettings.enableDebugExtension) {
final handler = debugSettings.useSseForDebugBackend
? SseSocketHandler(
SseHandler(
Uri.parse('/\$debug'),
Expand All @@ -108,58 +92,56 @@ class Dwds {
)
: WebSocketSocketHandler();

extensionBackend = await ExtensionBackend.start(handler, hostname);
extensionBackend =
await ExtensionBackend.start(handler, appMetadata.hostname);
extensionUri = Future.value(
Uri(
scheme: useSseForDebugBackend ? 'http' : 'ws',
scheme: debugSettings.useSseForDebugBackend ? 'http' : 'ws',
host: extensionBackend.hostname,
port: extensionBackend.port,
path: r'$debug',
).toString(),
);
if (urlEncoder != null) extensionUri = urlEncoder(await extensionUri);
final urlEncoder = debugSettings.urlEncoder;
if (urlEncoder != null) {
extensionUri = urlEncoder(await extensionUri);
}
}

final serveDevTools = devtoolsLauncher != null;
if (serveDevTools) {
devTools = await devtoolsLauncher(hostname);
final devToolsLauncher = debugSettings.devToolsLauncher;
if (devToolsLauncher != null) {
devTools = await devToolsLauncher(appMetadata.hostname);
final uri =
Uri(scheme: 'http', host: devTools.hostname, port: devTools.port);
_logger.info('Serving DevTools at $uri\n');
}

final injected = DwdsInjector(
loadStrategy,
useSseForInjectedClient: useSseForInjectedClient,
extensionUri: extensionUri,
enableDevtoolsLaunch: enableDevtoolsLaunch,
emitDebugEvents: emitDebugEvents,
isInternalBuild: isInternalBuild,
isFlutterApp: isFlutterApp,
);

final devHandler = DevHandler(
chromeConnection,
buildResults,
devTools,
assetReader,
hostname,
appMetadata.hostname,
extensionBackend,
urlEncoder,
useSseForDebugProxy,
useSseForInjectedClient,
expressionCompiler,
debugSettings.urlEncoder,
debugSettings.useSseForDebugProxy,
debugSettings.useSseForInjectedClient,
debugSettings.expressionCompiler,
injected,
spawnDds,
launchDevToolsInNewWindow,
debugSettings.spawnDds,
debugSettings.launchDevToolsInNewWindow,
);

return Dwds._(
injected.middleware,
devTools,
devHandler,
assetReader,
enableDebugging,
debugSettings.enableDebugging,
);
}
}
6 changes: 4 additions & 2 deletions dwds/lib/dwds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// BSD-style license that can be found in the LICENSE file.

export 'dart_web_debug_service.dart' show Dwds, ConnectionProvider;
export 'src/config/app_metadata.dart' show AppMetadata;
export 'src/config/debug_settings.dart'
show UrlEncoder, DevToolsLauncher, DebugSettings;
export 'src/connections/app_connection.dart' show AppConnection;
export 'src/connections/debug_connection.dart' show DebugConnection;
export 'src/debugging/metadata/provider.dart'
Expand All @@ -17,8 +20,7 @@ export 'src/loaders/frontend_server_require.dart'
export 'src/loaders/legacy.dart' show LegacyStrategy;
export 'src/loaders/require.dart' show RequireStrategy;
export 'src/loaders/strategy.dart' show LoadStrategy, ReloadConfiguration;
export 'src/readers/asset_reader.dart'
show AssetReader, UrlEncoder, PackageUriMapper;
export 'src/readers/asset_reader.dart' show AssetReader, PackageUriMapper;
export 'src/readers/frontend_server_asset_reader.dart'
show FrontendServerAssetReader;
export 'src/readers/proxy_server_asset_reader.dart' show ProxyServerAssetReader;
Expand Down
18 changes: 18 additions & 0 deletions dwds/lib/src/config/app_metadata.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Metadata for the connected app.
///
/// These are set by the code runner and passed to DWDS on start up.
class AppMetadata {
final String hostname;
final bool isInternalBuild;
Future<bool> Function() isFlutterApp;

AppMetadata({
this.hostname = 'localhost',
this.isInternalBuild = false,
Future<bool> Function()? isFlutterApp,
}) : isFlutterApp = isFlutterApp ?? (() => Future.value(true));
}
45 changes: 45 additions & 0 deletions dwds/lib/src/config/debug_settings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. 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/servers/devtools.dart';
import 'package:dwds/src/services/expression_compiler.dart';

typedef UrlEncoder = Future<String> Function(String url);

typedef DevToolsLauncher = Future<DevTools> Function(String hostname);

/// Debug settings for the connected app.
///
/// These are set by the code runner and passed to DWDS on start up.
class DebugSettings {
final bool enableDebugging;
final bool enableDebugExtension;
final bool useSseForDebugProxy;
final bool useSseForDebugBackend;
final bool useSseForInjectedClient;
final bool spawnDds;
final bool enableDevToolsLaunch;
final bool launchDevToolsInNewWindow;
final bool emitDebugEvents;
final DevToolsLauncher? devToolsLauncher;
// TODO(annagrin): make expressionCompiler argument required
// [issue 881](https://github.com/dart-lang/webdev/issues/881)
final ExpressionCompiler? expressionCompiler;
final UrlEncoder? urlEncoder;

DebugSettings({
required this.enableDebugging,
this.enableDebugExtension = false,
this.useSseForDebugProxy = true,
this.useSseForDebugBackend = true,
this.useSseForInjectedClient = true,
this.spawnDds = true,
this.enableDevToolsLaunch = true,
this.launchDevToolsInNewWindow = true,
this.emitDebugEvents = true,
this.devToolsLauncher,
this.expressionCompiler,
this.urlEncoder,
});
}
1 change: 1 addition & 0 deletions dwds/lib/src/handlers/dev_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:dwds/data/error_response.dart';
import 'package:dwds/data/isolate_events.dart';
import 'package:dwds/data/register_event.dart';
import 'package:dwds/data/serializers.dart';
import 'package:dwds/src/config/debug_settings.dart';
import 'package:dwds/src/connections/app_connection.dart';
import 'package:dwds/src/connections/debug_connection.dart';
import 'package:dwds/src/debugging/execution_context.dart';
Expand Down
Loading

0 comments on commit 793bcfb

Please sign in to comment.