findBridgePortsForWorkspace function

Future<List<int>> findBridgePortsForWorkspace(
  1. String name, {
  2. String host = '127.0.0.1',
  3. int minPort = defaultVSCodeBridgePort,
  4. int maxPort = maxVSCodeBridgePort,
  5. BridgePortProbe probe = _defaultProbe,
  6. BridgeIdentityFetcher fetchIdentity = fetchBridgeWorkspaceName,
})

Scans the CLI bridge port range and returns every port whose window has the workspace name open, ascending; empty when none does.

One scanBridgePorts pass over minPort–maxPort, each identity matched with normalizeWorkspaceName exactly as findBridgePortForWorkspace matches it — so the plural and the singular can never disagree about which windows match, only about how many they report. The singular stops at the first match and is the right call when any matching window will do; this is the right call when the caller must be able to say that a name matched more than one window, which the singular cannot tell it.

Returns an empty list rather than throwing when nothing matches: "no window" and "one window" and "two windows" are three answers to the same question, and a caller asking it is already prepared to read a count.

Implementation

Future<List<int>> findBridgePortsForWorkspace(
  String name, {
  String host = '127.0.0.1',
  int minPort = defaultVSCodeBridgePort,
  int maxPort = maxVSCodeBridgePort,
  BridgePortProbe probe = _defaultProbe,
  BridgeIdentityFetcher fetchIdentity = fetchBridgeWorkspaceName,
}) async {
  final target = normalizeWorkspaceName(name);
  final table = await scanBridgePorts(
    host: host,
    minPort: minPort,
    maxPort: maxPort,
    probe: probe,
    fetchIdentity: fetchIdentity,
  );
  final ports = table.keys.toList()..sort();
  return [
    for (final port in ports)
      if (normalizeWorkspaceName(table[port]!) == target) port,
  ];
}