Skip to content

Commit 12fa2fb

Browse files
authored
Use request uri to establish SSE connection (#736)
* Use request uri to establish SSE connection
1 parent f9a9398 commit 12fa2fb

File tree

4 files changed

+110
-42
lines changed

4 files changed

+110
-42
lines changed

dwds/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 0.7.5-dev
22

3+
- The injected client's connection is now based off the request URI.
34
- Fix an issue where resuming while paused at the start would cause an error.
45
- Expose the `ChromeDebugException` class for error handling purposes.
56

dwds/lib/src/handlers/injected_handler.dart

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Handler Function(Handler) createInjectedHandler(
3030
int extensionPort}) =>
3131
(innerHandler) {
3232
return (Request request) async {
33-
if (request.url.path == '$_clientScript.js') {
33+
if (request.url.path.endsWith('$_clientScript.js')) {
3434
var uri = await Isolate.resolvePackageUri(
3535
Uri.parse('package:$_clientScript.js'));
3636
var result = await File(uri.toFilePath()).readAsString();
@@ -67,9 +67,16 @@ Handler Function(Handler) createInjectedHandler(
6767
var mainFunction = bodyLines[extensionIndex + 1]
6868
.replaceAll('main()', 'main')
6969
.trim();
70-
body += _injectedClientJs(configuration, appId, mainFunction,
71-
extensionHostname: extensionHostname,
72-
extensionPort: extensionPort);
70+
var requestedUriBase = '${request.requestedUri.scheme}'
71+
'://${request.requestedUri.authority}';
72+
body += _injectedClientJs(
73+
configuration,
74+
appId,
75+
mainFunction,
76+
requestedUriBase,
77+
extensionHostname: extensionHostname,
78+
extensionPort: extensionPort,
79+
);
7380
body += bodyLines.sublist(extensionIndex + 2).join('\n');
7481
// Change the hot restart handler to re-assign
7582
// `window.$dartRunMain` to the new main, instead of invoking it.
@@ -89,14 +96,20 @@ Handler Function(Handler) createInjectedHandler(
8996
};
9097

9198
String _injectedClientJs(
92-
ReloadConfiguration configuration, String appId, String mainFunction,
93-
{String extensionHostname, int extensionPort}) {
99+
ReloadConfiguration configuration,
100+
String appId,
101+
String mainFunction,
102+
String requestedUriBase, {
103+
String extensionHostname,
104+
int extensionPort,
105+
}) {
94106
var injectedBody = '// Injected by webdev for build results support.\n'
95107
'window.\$dartAppId = "$appId";\n'
96108
'window.\$dartRunMain = $mainFunction;\n'
97109
'window.\$dartReloadConfiguration = "$configuration";\n'
98110
'window.\$dartLoader.forceLoadModule("$_clientScript");\n'
99111
'window.\$dartModuleStrategy = "$loadModule";\n'
112+
'window.\$dartUriBase = "$requestedUriBase";\n'
100113
'window.\$loadModuleConfig = $loadModule;\n';
101114
if (extensionPort != null && extensionHostname != null) {
102115
injectedBody += 'window.\$extensionHostname = "$extensionHostname";\n'

dwds/lib/src/injected/client.js

Lines changed: 70 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dwds/web/client.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Future<void> main() {
3333
// Test apps may already have this set.
3434
dartAppInstanceId ??= Uuid().v1();
3535

36-
var client = SseClient(r'/$sseHandler');
36+
var client = SseClient(_fixProtocol('$dartUriBase/\$sseHandler'));
3737
// Ensure the SSE connection is established before proceeding.
3838
// Note that `onOpen` is a broadcast stream so we must listen for this
3939
// immediately.
@@ -133,12 +133,31 @@ $stackTrace
133133
});
134134
}
135135

136+
/// Returns [url] modified if necessary so that, if the current page is served
137+
/// over `https`, then the URL is converted to `https`. Localhost is treated
138+
/// as a special case and not modified.
139+
String _fixProtocol(String url) {
140+
if (window.location.protocol == 'https:' && !url.startsWith('https://')) {
141+
// Chrome seems to allow mixed content from localhost.
142+
if (url.startsWith('http://localhost')) {
143+
return url;
144+
} else {
145+
return url.replaceFirst('http://', 'https://');
146+
}
147+
} else {
148+
return url;
149+
}
150+
}
151+
136152
@JS(r'$dartAppId')
137153
external String get dartAppId;
138154

139155
@JS(r'$dartAppInstanceId')
140156
external String get dartAppInstanceId;
141157

158+
@JS(r'$dartUriBase')
159+
external String get dartUriBase;
160+
142161
@JS(r'$dartAppInstanceId')
143162
external set dartAppInstanceId(String id);
144163

0 commit comments

Comments
 (0)