Unverified Commit c141a53f authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] support any as a special web-hostname (#54715)

parent 2f23476c
...@@ -136,7 +136,12 @@ class WebAssetServer implements AssetReader { ...@@ -136,7 +136,12 @@ class WebAssetServer implements AssetReader {
DwdsLauncher dwdsLauncher = Dwds.start, DwdsLauncher dwdsLauncher = Dwds.start,
}) async { }) async {
try { try {
final InternetAddress address = (await InternetAddress.lookup(hostname)).first; InternetAddress address;
if (hostname == 'any') {
address = InternetAddress.anyIPv4;
} else {
address = (await InternetAddress.lookup(hostname)).first;
}
final HttpServer httpServer = await HttpServer.bind(address, port); final HttpServer httpServer = await HttpServer.bind(address, port);
final PackageConfig packageConfig = await loadPackageConfigUri( final PackageConfig packageConfig = await loadPackageConfigUri(
globals.fs.file(PackageMap.globalPackagesPath).absolute.uri, globals.fs.file(PackageMap.globalPackagesPath).absolute.uri,
...@@ -211,6 +216,7 @@ class WebAssetServer implements AssetReader { ...@@ -211,6 +216,7 @@ class WebAssetServer implements AssetReader {
final Chrome chrome = await ChromeLauncher.connectedInstance; final Chrome chrome = await ChromeLauncher.connectedInstance;
return chrome.chromeConnection; return chrome.chromeConnection;
}, },
hostname: hostname,
urlEncoder: urlTunneller, urlEncoder: urlTunneller,
enableDebugging: true, enableDebugging: true,
useSseForDebugProxy: useSseForDebugProxy, useSseForDebugProxy: useSseForDebugProxy,
...@@ -634,7 +640,11 @@ class WebDevFS implements DevFS { ...@@ -634,7 +640,11 @@ class WebDevFS implements DevFS {
expressionCompiler, expressionCompiler,
testMode: testMode, testMode: testMode,
); );
_baseUri = Uri.parse('http://$hostname:$port'); if (hostname == 'any') {
_baseUri = Uri.http('localhost:$port', '');
} else {
_baseUri = Uri.http('$hostname:$port', '');
}
return _baseUri; return _baseUri;
} }
......
...@@ -159,7 +159,11 @@ abstract class FlutterCommand extends Command<void> { ...@@ -159,7 +159,11 @@ abstract class FlutterCommand extends Command<void> {
void usesWebOptions({ bool hide = true }) { void usesWebOptions({ bool hide = true }) {
argParser.addOption('web-hostname', argParser.addOption('web-hostname',
defaultsTo: 'localhost', defaultsTo: 'localhost',
help: 'The hostname to serve web application on.', help:
'The hostname that the web sever will use to resolve an IP to serve '
'from. The unresolved hostname is used to launch Chrome when using '
'the chrome Device. The name "any" may also be used to serve on any '
'IPV4 for either the Chrome or web-server device.',
hide: hide, hide: hide,
); );
argParser.addOption('web-port', argParser.addOption('web-port',
......
...@@ -380,7 +380,7 @@ void main() { ...@@ -380,7 +380,7 @@ void main() {
webDevFS.requireJS.createSync(recursive: true); webDevFS.requireJS.createSync(recursive: true);
webDevFS.stackTraceMapper.createSync(recursive: true); webDevFS.stackTraceMapper.createSync(recursive: true);
await webDevFS.create(); final Uri uri = await webDevFS.create();
webDevFS.webAssetServer.entrypointCacheDirectory = globals.fs.currentDirectory; webDevFS.webAssetServer.entrypointCacheDirectory = globals.fs.currentDirectory;
globals.fs.currentDirectory globals.fs.currentDirectory
.childDirectory('lib') .childDirectory('lib')
...@@ -432,13 +432,56 @@ void main() { ...@@ -432,13 +432,56 @@ void main() {
expect(await webDevFS.webAssetServer.dartSourceContents('web_entrypoint.dart'), expect(await webDevFS.webAssetServer.dartSourceContents('web_entrypoint.dart'),
contains('GENERATED')); contains('GENERATED'));
// served on localhost
expect(uri, Uri.http('localhost:0', ''));
await webDevFS.destroy();
}));
test('Can start web server with hostname any', () => testbed.run(() async {
globals.fs.file('.packages').writeAsStringSync('\n');
final File outputFile = globals.fs.file(globals.fs.path.join('lib', 'main.dart'))
..createSync(recursive: true);
outputFile.parent.childFile('a.sources').writeAsStringSync('');
outputFile.parent.childFile('a.json').writeAsStringSync('{}');
outputFile.parent.childFile('a.map').writeAsStringSync('{}');
outputFile.parent.childFile('.packages').writeAsStringSync('\n');
final ResidentCompiler residentCompiler = MockResidentCompiler();
when(residentCompiler.recompile(
any,
any,
outputPath: anyNamed('outputPath'),
packagesFilePath: anyNamed('packagesFilePath'),
)).thenAnswer((Invocation invocation) async {
return const CompilerOutput('a', 0, <Uri>[]);
});
final WebDevFS webDevFS = WebDevFS(
hostname: 'any',
port: 0,
packagesFilePath: '.packages',
urlTunneller: null,
useSseForDebugProxy: true,
buildMode: BuildMode.debug,
enableDwds: false,
entrypoint: Uri.base,
testMode: true,
expressionCompiler: null,
);
webDevFS.requireJS.createSync(recursive: true);
webDevFS.stackTraceMapper.createSync(recursive: true);
final Uri uri = await webDevFS.create();
expect(uri, Uri.http('localhost:0', ''));
await webDevFS.destroy(); await webDevFS.destroy();
})); }));
test('Launches DWDS with the correct arguments', () => testbed.run(() async { test('Launches DWDS with the correct arguments', () => testbed.run(() async {
globals.fs.file('.packages').writeAsStringSync('\n'); globals.fs.file('.packages').writeAsStringSync('\n');
final WebAssetServer server = await WebAssetServer.start( final WebAssetServer server = await WebAssetServer.start(
'localhost', 'any',
8123, 8123,
(String url) => null, (String url) => null,
true, true,
...@@ -466,6 +509,7 @@ void main() { ...@@ -466,6 +509,7 @@ void main() {
expect(enableDebugging, true); expect(enableDebugging, true);
expect(enableDebugExtension, true); expect(enableDebugExtension, true);
expect(useSseForDebugProxy, true); expect(useSseForDebugProxy, true);
expect(hostname, 'any');
return MockDwds(); return MockDwds();
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment