start static method

Future<Dwds> start({
  1. required AssetReader assetReader,
  2. required Stream<BuildResult> buildResults,
  3. required ConnectionProvider chromeConnection,
  4. required LoadStrategy loadStrategy,
  5. required bool enableDebugging,
  6. ExpressionCompiler? expressionCompiler,
  7. bool enableDebugExtension = false,
  8. String hostname = 'localhost',
  9. bool useSseForDebugProxy = true,
  10. bool useSseForDebugBackend = true,
  11. bool useSseForInjectedClient = true,
  12. UrlEncoder? urlEncoder,
  13. bool spawnDds = true,
  14. bool enableDevtoolsLaunch = true,
  15. DevtoolsLauncher? devtoolsLauncher,
  16. bool launchDevToolsInNewWindow = true,
  17. bool emitDebugEvents = true,
  18. bool isInternalBuild = false,
  19. Future<bool> isFlutterApp()?,
})

Implementation

static Future<Dwds> start({
  required AssetReader assetReader,
  required Stream<BuildResult> buildResults,
  required ConnectionProvider chromeConnection,
  required LoadStrategy loadStrategy,
  required bool enableDebugging,
  // TODO(annagrin): make expressionCompiler argument required
  // [issue 881](https://github.com/dart-lang/webdev/issues/881)
  ExpressionCompiler? expressionCompiler,
  bool enableDebugExtension = false,
  String hostname = 'localhost',
  bool useSseForDebugProxy = true,
  bool useSseForDebugBackend = true,
  bool useSseForInjectedClient = true,
  UrlEncoder? urlEncoder,
  bool spawnDds = true,
  // TODO(elliette): DevTools is inconsistently capitalized throughout this
  // file. Change all occurrences of devtools/Devtools to devTools/DevTools.
  bool enableDevtoolsLaunch = true,
  DevtoolsLauncher? devtoolsLauncher,
  bool launchDevToolsInNewWindow = true,
  bool emitDebugEvents = true,
  bool isInternalBuild = false,
  Future<bool> Function()? isFlutterApp,
}) async {
  globalLoadStrategy = loadStrategy;
  isFlutterApp ??= () => Future.value(true);

  DevTools? devTools;
  Future<String>? extensionUri;
  ExtensionBackend? extensionBackend;
  if (enableDebugExtension) {
    final handler = useSseForDebugBackend
        ? SseSocketHandler(
            SseHandler(
              Uri.parse('/\$debug'),
              // Proxy servers may actively kill long standing connections.
              // Allow for clients to reconnect in a short window. Making the
              // window too long may cause issues if the user closes a debug
              // session and initiates a new one during the keepAlive window.
              keepAlive: const Duration(seconds: 5),
            ),
          )
        : WebSocketSocketHandler();

    extensionBackend = await ExtensionBackend.start(handler, hostname);
    extensionUri = Future.value(
      Uri(
        scheme: useSseForDebugBackend ? 'http' : 'ws',
        host: extensionBackend.hostname,
        port: extensionBackend.port,
        path: r'$debug',
      ).toString(),
    );
    if (urlEncoder != null) extensionUri = urlEncoder(await extensionUri);
  }

  final serveDevTools = devtoolsLauncher != null;
  if (serveDevTools) {
    devTools = await devtoolsLauncher(hostname);
    final uri =
        Uri(scheme: 'http', host: devTools.hostname, port: devTools.port);
    _logger.info('Serving DevTools at $uri\n');
  }

  final injected = DwdsInjector(
    loadStrategy,
    useSseForInjectedClient: useSseForInjectedClient,
    extensionUri: extensionUri,
    enableDevtoolsLaunch: enableDevtoolsLaunch,
    emitDebugEvents: emitDebugEvents,
    isInternalBuild: isInternalBuild,
    isFlutterApp: isFlutterApp,
  );

  final devHandler = DevHandler(
    chromeConnection,
    buildResults,
    devTools,
    assetReader,
    hostname,
    extensionBackend,
    urlEncoder,
    useSseForDebugProxy,
    useSseForInjectedClient,
    expressionCompiler,
    injected,
    spawnDds,
    launchDevToolsInNewWindow,
  );

  return Dwds._(
    injected.middleware,
    devTools,
    devHandler,
    assetReader,
    enableDebugging,
  );
}