Skip to content

Commit 128c2c8

Browse files
Pi Songkunthamgrouma
authored andcommitted
Added an optional port of the Dart Debug Extension to be injected. (#489)
* Added an optional port of the Dart Debug Extension to be injected. * Added an optional hostname to be injected too.
1 parent 5abe9cb commit 128c2c8

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

dwds/lib/src/handlers/injected_handler.dart

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ const mainExtensionMarker = '/* MAIN_EXTENSION_MARKER */';
2424
const _clientScript = 'dwds/src/injected/client';
2525

2626
Handler Function(Handler) createInjectedHandler(
27-
ReloadConfiguration configuration,
28-
) =>
27+
ReloadConfiguration configuration,
28+
{String extensionHostname,
29+
int extensionPort}) =>
2930
(innerHandler) {
3031
return (Request request) async {
3132
if (request.url.path == '$_clientScript.js') {
@@ -66,7 +67,9 @@ Handler Function(Handler) createInjectedHandler(
6667
.replaceAll('(', '')
6768
.replaceAll(')', '')
6869
.trim();
69-
body += _injectedClientJs(configuration, appId, mainFuntion);
70+
body += _injectedClientJs(configuration, appId, mainFuntion,
71+
extensionHostname: extensionHostname,
72+
extensionPort: extensionPort);
7073
body += bodyLines.sublist(extensionIndex + 2).join('\n');
7174
// Change the hot restart handler to re-assign
7275
// `window.$dartRunMain` to the new main, instead of invoking it.
@@ -86,11 +89,20 @@ Handler Function(Handler) createInjectedHandler(
8689
};
8790

8891
String _injectedClientJs(
89-
ReloadConfiguration configuration, String appId, String mainFunction) =>
90-
'''\n
91-
// Injected by webdev for build results support.
92-
window.\$dartAppId = "$appId";
93-
window.\$dartRunMain = $mainFunction;
94-
window.\$dartReloadConfiguration = "$configuration";
95-
window.\$dartLoader.forceLoadModule('$_clientScript');
96-
''';
92+
ReloadConfiguration configuration, String appId, String mainFunction,
93+
{String extensionHostname, int extensionPort}) {
94+
var injectedBody = '''\n
95+
// Injected by webdev for build results support.
96+
window.\$dartAppId = "$appId";
97+
window.\$dartRunMain = $mainFunction;
98+
window.\$dartReloadConfiguration = "$configuration";
99+
window.\$dartLoader.forceLoadModule('$_clientScript');
100+
''';
101+
if (extensionPort != null && extensionHostname != null) {
102+
injectedBody += '''
103+
window.\$extensionHostname = "$extensionHostname";
104+
window.\$extensionPort = "$extensionPort";
105+
''';
106+
}
107+
return injectedBody;
108+
}

dwds/test/handlers/injected_handler_test.dart

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void main() {
1616
const entryEtag = 'entry etag';
1717
const nonEntryEtag = 'some etag';
1818

19-
group('InjectedMiddelware', () {
19+
group('InjectedHandlerWithoutExtension', () {
2020
setUp(() async {
2121
var pipeline = const Pipeline()
2222
.addMiddleware(createInjectedHandler(ReloadConfiguration.liveReload));
@@ -95,5 +95,41 @@ void main() {
9595
});
9696
expect(cachedResponse.statusCode, HttpStatus.notModified);
9797
});
98+
99+
test('Does not inject the extension backend port', () async {
100+
var result = await http.get(
101+
'http://localhost:${server.port}/entrypoint$bootstrapJsExtension');
102+
expect(result.body.contains('extensionHostname'), isFalse);
103+
expect(result.body.contains('extensionPort'), isFalse);
104+
});
105+
});
106+
107+
group('InjectedHandlerWithExtension', () {
108+
setUp(() async {
109+
var someExtensionHostname = 'localhost';
110+
var someExtensionPort = 4000;
111+
var pipeline = const Pipeline().addMiddleware(createInjectedHandler(
112+
ReloadConfiguration.liveReload,
113+
extensionHostname: someExtensionHostname,
114+
extensionPort: someExtensionPort));
115+
server = await shelf_io.serve(pipeline.addHandler((request) {
116+
return Response.ok(
117+
'$entrypointExtensionMarker\n'
118+
'$mainExtensionMarker\n'
119+
'app.main.main()',
120+
headers: {HttpHeaders.etagHeader: entryEtag});
121+
}), 'localhost', 0);
122+
});
123+
124+
tearDown(() async {
125+
await server.close();
126+
});
127+
128+
test('Injects the extension backend port', () async {
129+
var result = await http.get(
130+
'http://localhost:${server.port}/entrypoint$bootstrapJsExtension');
131+
expect(result.body.contains('extensionHostname'), isTrue);
132+
expect(result.body.contains('extensionPort'), isTrue);
133+
});
98134
});
99135
}

0 commit comments

Comments
 (0)