Skip to content

modifying DWDS Injector to always inject client and introduce useDwdsWebSocketConnection flag #2629

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dwds/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 24.3.11-wip
## 24.3.11

- Changed DWDS to always inject the client and added `useDwdsWebSocketConnection` flag to control communication protocol: when true uses socket-based implementation, when false uses Chrome-based communication protocol.
- Added WebSocket-based hot reload support: `reloadSources` in `ChromeProxyService` and `DevHandler` now handle hot reload requests and responses over WebSockets.
- Refactored the injected client to use a reusable function for handling hot reload requests and responses over WebSockets.
- Added support for breakpoint registering on a hot restart with the DDC library bundle format using PausePostRequests.
Expand Down
7 changes: 6 additions & 1 deletion dwds/lib/dart_web_debug_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ class Dwds {
required Stream<BuildResult> buildResults,
required ConnectionProvider chromeConnection,
required ToolConfiguration toolConfiguration,
// ignore: avoid-unused-parameters
@Deprecated(
'This parameter is ignored and will be removed in a future version.',
)
bool injectDebuggingSupportCode = true,
bool useDwdsWebSocketConnection = false,
}) async {
globalToolConfiguration = toolConfiguration;
final debugSettings = toolConfiguration.debugSettings;
Expand Down Expand Up @@ -120,7 +125,7 @@ class Dwds {

final injected = DwdsInjector(
extensionUri: extensionUri,
injectDebuggingSupportCode: injectDebuggingSupportCode,
useDwdsWebSocketConnection: useDwdsWebSocketConnection,
);

final devHandler = DevHandler(
Expand Down
42 changes: 20 additions & 22 deletions dwds/lib/src/handlers/injector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,21 @@ const _clientScript = 'dwds/src/injected/client';
/// to include the injected DWDS client, enabling debugging capabilities
/// and source mapping when running in a browser environment.
///
/// The `_injectDebuggingSupportCode` flag determines whether debugging-related
/// functionality should be included:
/// - When `true`, the DWDS client is injected, enabling debugging features.
/// - When `false`, debugging support is disabled, meaning the application will
/// run without debugging.
///
/// This separation allows for scenarios where debugging is not needed or
/// should be explicitly avoided.
/// TODO(yjessy): Remove this when the DWDS WebSocket connection is implemented.
/// The `_useDwdsWebSocketConnection` flag determines the communication protocol:
/// - When `true`, uses a socket-based implementation.
/// - When `false`, uses Chrome-based communication protocol.
class DwdsInjector {
final Future<String>? _extensionUri;
final _devHandlerPaths = StreamController<String>();
final _logger = Logger('DwdsInjector');
final bool _injectDebuggingSupportCode;
final bool _useDwdsWebSocketConnection;

DwdsInjector({
Future<String>? extensionUri,
bool injectDebuggingSupportCode = true,
bool useDwdsWebSocketConnection = false,
}) : _extensionUri = extensionUri,
_injectDebuggingSupportCode = injectDebuggingSupportCode;
_useDwdsWebSocketConnection = useDwdsWebSocketConnection;

/// Returns the embedded dev handler paths.
///
Expand Down Expand Up @@ -108,17 +104,15 @@ class DwdsInjector {
await globalToolConfiguration.loadStrategy.trackEntrypoint(
entrypoint,
);
// If true, inject the debugging client and hoist the main function
// to enable debugging support.
if (_injectDebuggingSupportCode) {
body = await _injectClientAndHoistMain(
body,
appId,
devHandlerPath,
entrypoint,
await _extensionUri,
);
}
// Always inject the debugging client and hoist the main function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means we're injecting the client when doing AMD + web-server, which wasn't the case before unless start-paused was passed, is that right? I don't have an objection to that addition, but I just want to make sure that doesn't break anything and this is intentional.

body = await _injectClientAndHoistMain(
body,
appId,
devHandlerPath,
entrypoint,
await _extensionUri,
_useDwdsWebSocketConnection,
);
body += await globalToolConfiguration.loadStrategy.bootstrapFor(
entrypoint,
);
Expand Down Expand Up @@ -154,6 +148,7 @@ Future<String> _injectClientAndHoistMain(
String devHandlerPath,
String entrypointPath,
String? extensionUri,
bool useDwdsWebSocketConnection,
) async {
final bodyLines = body.split('\n');
final extensionIndex = bodyLines.indexWhere(
Expand All @@ -172,6 +167,7 @@ Future<String> _injectClientAndHoistMain(
devHandlerPath,
entrypointPath,
extensionUri,
useDwdsWebSocketConnection,
);
result += '''
// Injected by dwds for debugging support.
Expand Down Expand Up @@ -203,6 +199,7 @@ Future<String> _injectedClientSnippet(
String devHandlerPath,
String entrypointPath,
String? extensionUri,
bool useDwdsWebSocketConnection,
) async {
final loadStrategy = globalToolConfiguration.loadStrategy;
final buildSettings = loadStrategy.buildSettings;
Expand All @@ -221,6 +218,7 @@ Future<String> _injectedClientSnippet(
'window.\$dartEmitDebugEvents = ${debugSettings.emitDebugEvents};\n'
'window.\$isInternalBuild = ${appMetadata.isInternalBuild};\n'
'window.\$isFlutterApp = ${buildSettings.isFlutterApp};\n'
'window.\$useDwdsWebSocketConnection = $useDwdsWebSocketConnection;\n'
'${loadStrategy is DdcLibraryBundleStrategy ? 'window.\$hotReloadSourcesPath = "${loadStrategy.hotReloadSourcesUri.toString()}";\n' : ''}'
'${loadStrategy.loadClientSnippet(_clientScript)}';

Expand Down
Loading
Loading