Skip to content

Commit b399e94

Browse files
authored
Pull out dart:io-dependent functions from/src/utilities/shared.dart into /src/utilities/server.dart (#1942)
1 parent 6255c85 commit b399e94

File tree

8 files changed

+81
-77
lines changed

8 files changed

+81
-77
lines changed

dwds/lib/src/servers/extension_backend.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import 'package:dwds/data/extension_request.dart';
1010
import 'package:dwds/src/events.dart';
1111
import 'package:dwds/src/handlers/socket_connections.dart';
1212
import 'package:dwds/src/servers/extension_debugger.dart';
13-
import 'package:dwds/src/utilities/shared.dart';
13+
import 'package:dwds/src/utilities/server.dart';
1414
import 'package:logging/logging.dart';
1515
import 'package:shelf/shelf.dart';
1616

dwds/lib/src/services/debug_service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import 'package:dwds/src/loaders/strategy.dart';
1717
import 'package:dwds/src/readers/asset_reader.dart';
1818
import 'package:dwds/src/services/chrome_proxy_service.dart';
1919
import 'package:dwds/src/services/expression_compiler.dart';
20-
import 'package:dwds/src/utilities/shared.dart';
20+
import 'package:dwds/src/utilities/server.dart';
2121
import 'package:logging/logging.dart';
2222
import 'package:shelf/shelf.dart' as shelf;
2323
import 'package:shelf/shelf.dart' hide Response;

dwds/lib/src/utilities/server.dart

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
import 'dart:io';
7+
8+
import 'package:http_multi_server/http_multi_server.dart';
9+
import 'package:shelf/shelf.dart';
10+
import 'package:shelf/shelf_io.dart';
11+
import 'package:stack_trace/stack_trace.dart';
12+
13+
/// Returns `true` if [hostname] is bound to an IPv6 address.
14+
Future<bool> useIPv6ForHost(String hostname) async {
15+
final addresses = await InternetAddress.lookup(hostname);
16+
if (addresses.isEmpty) return false;
17+
final address = addresses.firstWhere(
18+
(a) => a.type == InternetAddressType.IPv6,
19+
orElse: () => addresses.first,
20+
);
21+
return address.type == InternetAddressType.IPv6;
22+
}
23+
24+
/// Returns a port that is probably, but not definitely, not in use.
25+
///
26+
/// This has a built-in race condition: another process may bind this port at
27+
/// any time after this call has returned.
28+
Future<int> findUnusedPort() async {
29+
int port;
30+
ServerSocket socket;
31+
try {
32+
socket =
33+
await ServerSocket.bind(InternetAddress.loopbackIPv6, 0, v6Only: true);
34+
} on SocketException {
35+
socket = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
36+
}
37+
port = socket.port;
38+
await socket.close();
39+
return port;
40+
}
41+
42+
/// Finds unused port and binds a new http server to it.
43+
///
44+
/// Retries a few times to recover from errors due to
45+
/// another thread or process opening the same port.
46+
/// Starts by trying to bind to [port] if specified.
47+
Future<HttpServer> startHttpServer(String hostname, {int? port}) async {
48+
HttpServer? httpServer;
49+
final retries = 5;
50+
var i = 0;
51+
var foundPort = port ?? await findUnusedPort();
52+
while (i < retries) {
53+
i++;
54+
try {
55+
httpServer = await HttpMultiServer.bind(hostname, foundPort);
56+
} on SocketException {
57+
if (i == retries) rethrow;
58+
}
59+
if (httpServer != null || i == retries) return httpServer!;
60+
foundPort = await findUnusedPort();
61+
await Future<void>.delayed(const Duration(milliseconds: 100));
62+
}
63+
return httpServer!;
64+
}
65+
66+
/// Handles [requests] using [handler].
67+
///
68+
/// Captures all sync and async stack error traces and passes
69+
/// them to the [onError] handler.
70+
void serveHttpRequests(Stream<HttpRequest> requests, Handler handler,
71+
void Function(Object, StackTrace) onError) {
72+
return Chain.capture(() {
73+
serveRequests(requests, handler);
74+
}, onError: onError);
75+
}

dwds/lib/src/utilities/shared.dart

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:async';
6-
import 'dart:io';
7-
85
import 'package:dwds/src/services/chrome_debug_exception.dart';
9-
import 'package:http_multi_server/http_multi_server.dart';
10-
import 'package:shelf/shelf.dart';
11-
import 'package:shelf/shelf_io.dart';
12-
import 'package:stack_trace/stack_trace.dart';
136
import 'package:vm_service/vm_service.dart';
147
import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart'
158
as wip;
@@ -22,70 +15,6 @@ String createId() {
2215
return '$_nextId';
2316
}
2417

25-
/// Returns `true` if [hostname] is bound to an IPv6 address.
26-
Future<bool> useIPv6ForHost(String hostname) async {
27-
final addresses = await InternetAddress.lookup(hostname);
28-
if (addresses.isEmpty) return false;
29-
final address = addresses.firstWhere(
30-
(a) => a.type == InternetAddressType.IPv6,
31-
orElse: () => addresses.first,
32-
);
33-
return address.type == InternetAddressType.IPv6;
34-
}
35-
36-
/// Returns a port that is probably, but not definitely, not in use.
37-
///
38-
/// This has a built-in race condition: another process may bind this port at
39-
/// any time after this call has returned.
40-
Future<int> findUnusedPort() async {
41-
int port;
42-
ServerSocket socket;
43-
try {
44-
socket =
45-
await ServerSocket.bind(InternetAddress.loopbackIPv6, 0, v6Only: true);
46-
} on SocketException {
47-
socket = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
48-
}
49-
port = socket.port;
50-
await socket.close();
51-
return port;
52-
}
53-
54-
/// Finds unused port and binds a new http server to it.
55-
///
56-
/// Retries a few times to recover from errors due to
57-
/// another thread or process opening the same port.
58-
/// Starts by trying to bind to [port] if specified.
59-
Future<HttpServer> startHttpServer(String hostname, {int? port}) async {
60-
HttpServer? httpServer;
61-
final retries = 5;
62-
var i = 0;
63-
var foundPort = port ?? await findUnusedPort();
64-
while (i < retries) {
65-
i++;
66-
try {
67-
httpServer = await HttpMultiServer.bind(hostname, foundPort);
68-
} on SocketException {
69-
if (i == retries) rethrow;
70-
}
71-
if (httpServer != null || i == retries) return httpServer!;
72-
foundPort = await findUnusedPort();
73-
await Future<void>.delayed(const Duration(milliseconds: 100));
74-
}
75-
return httpServer!;
76-
}
77-
78-
/// Handles [requests] using [handler].
79-
///
80-
/// Captures all sync and async stack error traces and passes
81-
/// them to the [onError] handler.
82-
void serveHttpRequests(Stream<HttpRequest> requests, Handler handler,
83-
void Function(Object, StackTrace) onError) {
84-
return Chain.capture(() {
85-
serveRequests(requests, handler);
86-
}, onError: onError);
87-
}
88-
8918
/// Throws an [wip.ExceptionDetails] object if `exceptionDetails` is present on the
9019
/// result.
9120
void handleErrorIfPresent(wip.WipResponse? response, {String? evalContents}) {

dwds/test/events_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'dart:io';
99
import 'package:dwds/src/connections/debug_connection.dart';
1010
import 'package:dwds/src/events.dart';
1111
import 'package:dwds/src/services/chrome_proxy_service.dart';
12-
import 'package:dwds/src/utilities/shared.dart';
12+
import 'package:dwds/src/utilities/server.dart';
1313
import 'package:test/test.dart';
1414
import 'package:vm_service/vm_service.dart';
1515
import 'package:webdriver/async_core.dart';

dwds/test/expression_compiler_service_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'dart:io';
1111
import 'package:dwds/expression_compiler.dart';
1212
import 'package:dwds/src/services/expression_compiler_service.dart';
1313
import 'package:dwds/src/utilities/sdk_configuration.dart';
14-
import 'package:dwds/src/utilities/shared.dart';
14+
import 'package:dwds/src/utilities/server.dart';
1515
import 'package:logging/logging.dart';
1616
import 'package:path/path.dart' as p;
1717
import 'package:shelf/shelf.dart';

dwds/test/fixtures/context.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import 'package:dwds/src/loaders/strategy.dart';
2121
import 'package:dwds/src/readers/proxy_server_asset_reader.dart';
2222
import 'package:dwds/src/services/expression_compiler_service.dart';
2323
import 'package:dwds/src/utilities/dart_uri.dart';
24-
import 'package:dwds/src/utilities/shared.dart';
24+
import 'package:dwds/src/utilities/server.dart';
2525
import 'package:file/local.dart';
2626
import 'package:frontend_server_common/src/resident_runner.dart';
2727
import 'package:http/http.dart';

dwds/test/fixtures/server.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'package:dwds/expression_compiler.dart';
1313
import 'package:dwds/src/loaders/require.dart';
1414
import 'package:dwds/src/servers/devtools.dart';
1515
import 'package:dwds/src/services/expression_compiler_service.dart';
16-
import 'package:dwds/src/utilities/shared.dart';
16+
import 'package:dwds/src/utilities/server.dart';
1717
import 'package:logging/logging.dart';
1818
import 'package:shelf/shelf.dart';
1919
import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';

0 commit comments

Comments
 (0)