Skip to content
Merged
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
34 changes: 23 additions & 11 deletions dwds/lib/src/handlers/injected_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ const mainExtensionMarker = '/* MAIN_EXTENSION_MARKER */';
const _clientScript = 'dwds/src/injected/client';

Handler Function(Handler) createInjectedHandler(
ReloadConfiguration configuration,
) =>
ReloadConfiguration configuration,
{String extensionHostname,
int extensionPort}) =>
(innerHandler) {
return (Request request) async {
if (request.url.path == '$_clientScript.js') {
Expand Down Expand Up @@ -66,7 +67,9 @@ Handler Function(Handler) createInjectedHandler(
.replaceAll('(', '')
.replaceAll(')', '')
.trim();
body += _injectedClientJs(configuration, appId, mainFuntion);
body += _injectedClientJs(configuration, appId, mainFuntion,
extensionHostname: extensionHostname,
extensionPort: extensionPort);
body += bodyLines.sublist(extensionIndex + 2).join('\n');
// Change the hot restart handler to re-assign
// `window.$dartRunMain` to the new main, instead of invoking it.
Expand All @@ -86,11 +89,20 @@ Handler Function(Handler) createInjectedHandler(
};

String _injectedClientJs(
ReloadConfiguration configuration, String appId, String mainFunction) =>
'''\n
// Injected by webdev for build results support.
window.\$dartAppId = "$appId";
window.\$dartRunMain = $mainFunction;
window.\$dartReloadConfiguration = "$configuration";
window.\$dartLoader.forceLoadModule('$_clientScript');
''';
ReloadConfiguration configuration, String appId, String mainFunction,
{String extensionHostname, int extensionPort}) {
var injectedBody = '''\n
// Injected by webdev for build results support.
window.\$dartAppId = "$appId";
window.\$dartRunMain = $mainFunction;
window.\$dartReloadConfiguration = "$configuration";
window.\$dartLoader.forceLoadModule('$_clientScript');
''';
if (extensionPort != null && extensionHostname != null) {
injectedBody += '''
window.\$extensionHostname = "$extensionHostname";
window.\$extensionPort = "$extensionPort";
''';
}
return injectedBody;
}
38 changes: 37 additions & 1 deletion dwds/test/handlers/injected_handler_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void main() {
const entryEtag = 'entry etag';
const nonEntryEtag = 'some etag';

group('InjectedMiddelware', () {
group('InjectedHandlerWithoutExtension', () {
setUp(() async {
var pipeline = const Pipeline()
.addMiddleware(createInjectedHandler(ReloadConfiguration.liveReload));
Expand Down Expand Up @@ -95,5 +95,41 @@ void main() {
});
expect(cachedResponse.statusCode, HttpStatus.notModified);
});

test('Does not inject the extension backend port', () async {
var result = await http.get(
'http://localhost:${server.port}/entrypoint$bootstrapJsExtension');
expect(result.body.contains('extensionHostname'), isFalse);
expect(result.body.contains('extensionPort'), isFalse);
});
});

group('InjectedHandlerWithExtension', () {
setUp(() async {
var someExtensionHostname = 'localhost';
var someExtensionPort = 4000;
var pipeline = const Pipeline().addMiddleware(createInjectedHandler(
ReloadConfiguration.liveReload,
extensionHostname: someExtensionHostname,
extensionPort: someExtensionPort));
server = await shelf_io.serve(pipeline.addHandler((request) {
return Response.ok(
'$entrypointExtensionMarker\n'
'$mainExtensionMarker\n'
'app.main.main()',
headers: {HttpHeaders.etagHeader: entryEtag});
}), 'localhost', 0);
});

tearDown(() async {
await server.close();
});

test('Injects the extension backend port', () async {
var result = await http.get(
'http://localhost:${server.port}/entrypoint$bootstrapJsExtension');
expect(result.body.contains('extensionHostname'), isTrue);
expect(result.body.contains('extensionPort'), isTrue);
});
});
}