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 {
DwdsLauncher dwdsLauncher = Dwds.start,
}) async {
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 PackageConfig packageConfig = await loadPackageConfigUri(
globals.fs.file(PackageMap.globalPackagesPath).absolute.uri,
......@@ -211,6 +216,7 @@ class WebAssetServer implements AssetReader {
final Chrome chrome = await ChromeLauncher.connectedInstance;
return chrome.chromeConnection;
},
hostname: hostname,
urlEncoder: urlTunneller,
enableDebugging: true,
useSseForDebugProxy: useSseForDebugProxy,
......@@ -634,7 +640,11 @@ class WebDevFS implements DevFS {
expressionCompiler,
testMode: testMode,
);
_baseUri = Uri.parse('http://$hostname:$port');
if (hostname == 'any') {
_baseUri = Uri.http('localhost:$port', '');
} else {
_baseUri = Uri.http('$hostname:$port', '');
}
return _baseUri;
}
......
......@@ -159,7 +159,11 @@ abstract class FlutterCommand extends Command<void> {
void usesWebOptions({ bool hide = true }) {
argParser.addOption('web-hostname',
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,
);
argParser.addOption('web-port',
......
......@@ -380,7 +380,7 @@ void main() {
webDevFS.requireJS.createSync(recursive: true);
webDevFS.stackTraceMapper.createSync(recursive: true);
await webDevFS.create();
final Uri uri = await webDevFS.create();
webDevFS.webAssetServer.entrypointCacheDirectory = globals.fs.currentDirectory;
globals.fs.currentDirectory
.childDirectory('lib')
......@@ -432,13 +432,56 @@ void main() {
expect(await webDevFS.webAssetServer.dartSourceContents('web_entrypoint.dart'),
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();
}));
test('Launches DWDS with the correct arguments', () => testbed.run(() async {
globals.fs.file('.packages').writeAsStringSync('\n');
final WebAssetServer server = await WebAssetServer.start(
'localhost',
'any',
8123,
(String url) => null,
true,
......@@ -466,6 +509,7 @@ void main() {
expect(enableDebugging, true);
expect(enableDebugExtension, true);
expect(useSseForDebugProxy, true);
expect(hostname, 'any');
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