Skip to content

Commit da5597a

Browse files
author
Anna Gringauze
authored
Migrated more dwds directories to null safety (#1660)
* Removed dependecies on dwds.dart inside dwds code * Migrate loaders and debugging/metadata directories to null safety * Add format errors * Removed unnecessary 'late' from metadata fields * Format * Migrated more dwds directories to null safety: connection (some files) handlers (some files) readers servers services (some files) utilities * Fix test failures * Fix injector test failure
1 parent 5efb563 commit da5597a

File tree

10 files changed

+130
-160
lines changed

10 files changed

+130
-160
lines changed

dwds/lib/src/connections/app_connection.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:convert';
97

dwds/lib/src/handlers/injector.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:convert';
97
import 'dart:io';
@@ -32,7 +30,7 @@ const _clientScript = 'dwds/src/injected/client';
3230
/// information.
3331
class DwdsInjector {
3432
final LoadStrategy _loadStrategy;
35-
final Future<String> _extensionUri;
33+
final Future<String>? _extensionUri;
3634
final _devHandlerPaths = StreamController<String>();
3735
final _logger = Logger('DwdsInjector');
3836
final bool _enableDevtoolsLaunch;
@@ -41,14 +39,14 @@ class DwdsInjector {
4139

4240
DwdsInjector(
4341
this._loadStrategy, {
44-
Future<String> extensionUri,
45-
bool enableDevtoolsLaunch,
46-
bool useSseForInjectedClient,
47-
bool emitDebugEvents,
42+
Future<String>? extensionUri,
43+
bool enableDevtoolsLaunch = false,
44+
bool useSseForInjectedClient = true,
45+
bool emitDebugEvents = true,
4846
}) : _extensionUri = extensionUri,
4947
_enableDevtoolsLaunch = enableDevtoolsLaunch,
50-
_useSseForInjectedClient = useSseForInjectedClient ?? true,
51-
_emitDebugEvents = emitDebugEvents ?? true;
48+
_useSseForInjectedClient = useSseForInjectedClient,
49+
_emitDebugEvents = emitDebugEvents;
5250

5351
/// Returns the embedded dev handler paths.
5452
///
@@ -60,6 +58,9 @@ class DwdsInjector {
6058
if (request.url.path.endsWith('$_clientScript.js')) {
6159
final uri = await Isolate.resolvePackageUri(
6260
Uri.parse('package:$_clientScript.js'));
61+
if (uri == null) {
62+
throw StateError('Cannot resolve "package:$_clientScript.js"');
63+
}
6364
final result = await File(uri.toFilePath()).readAsString();
6465
return Response.ok(result, headers: {
6566
HttpHeaders.contentTypeHeader: 'application/javascript'
@@ -123,8 +124,7 @@ class DwdsInjector {
123124
return response.change(body: body, headers: newHeaders);
124125
} else {
125126
final loadResponse = await _loadStrategy.handler(request);
126-
if (loadResponse != null &&
127-
loadResponse.statusCode != HttpStatus.notFound) {
127+
if (loadResponse.statusCode != HttpStatus.notFound) {
128128
return loadResponse;
129129
}
130130
return innerHandler(request);
@@ -140,7 +140,7 @@ String _injectClientAndHoistMain(
140140
String appId,
141141
String devHandlerPath,
142142
String entrypointPath,
143-
String extensionUri,
143+
String? extensionUri,
144144
LoadStrategy loadStrategy,
145145
bool enableDevtoolsLaunch,
146146
bool emitDebugEvents,
@@ -194,7 +194,7 @@ String _injectedClientSnippet(
194194
String appId,
195195
String devHandlerPath,
196196
String entrypointPath,
197-
String extensionUri,
197+
String? extensionUri,
198198
LoadStrategy loadStrategy,
199199
bool enableDevtoolsLaunch,
200200
bool emitDebugEvents,

dwds/lib/src/readers/frontend_server_asset_reader.dart

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
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-
// @dart = 2.9
6-
75
import 'dart:convert';
86
import 'dart:io';
97

8+
import 'package:logging/logging.dart';
109
import 'package:package_config/package_config.dart';
1110
import 'package:path/path.dart' as p;
1211

@@ -15,6 +14,7 @@ import '../readers/asset_reader.dart';
1514
/// A reader for Dart sources and related source maps provided by the Frontend
1615
/// Server.
1716
class FrontendServerAssetReader implements AssetReader {
17+
final _logger = Logger('FrontendServerAssetReader');
1818
final File _mapOriginal;
1919
final File _mapIncremental;
2020
final File _jsonOriginal;
@@ -49,29 +49,38 @@ class FrontendServerAssetReader implements AssetReader {
4949
.absolute(p.join(_packageRoot, '.dart_tool/package_config.json'))));
5050

5151
@override
52-
Future<String> dartSourceContents(String serverPath) async {
53-
if (!serverPath.endsWith('.dart')) return null;
54-
final packageConfig = await _packageConfig;
55-
56-
Uri fileUri;
57-
if (serverPath.startsWith('packages/')) {
58-
final packagePath = serverPath.replaceFirst('packages/', 'package:');
59-
fileUri = packageConfig.resolve(Uri.parse(packagePath));
60-
} else {
61-
fileUri = p.toUri(p.join(_packageRoot, serverPath));
52+
Future<String?> dartSourceContents(String serverPath) async {
53+
if (serverPath.endsWith('.dart')) {
54+
final packageConfig = await _packageConfig;
55+
56+
Uri? fileUri;
57+
if (serverPath.startsWith('packages/')) {
58+
final packagePath = serverPath.replaceFirst('packages/', 'package:');
59+
fileUri = packageConfig.resolve(Uri.parse(packagePath));
60+
} else {
61+
fileUri = p.toUri(p.join(_packageRoot, serverPath));
62+
}
63+
if (fileUri != null) {
64+
final source = File(fileUri.toFilePath());
65+
if (source.existsSync()) return source.readAsString();
66+
}
6267
}
63-
64-
final source = File(fileUri.toFilePath());
65-
if (!await source.exists()) return null;
66-
return await source.readAsString();
68+
_logger.severe('Cannot find source contents for $serverPath');
69+
return null;
6770
}
6871

6972
@override
70-
Future<String> sourceMapContents(String serverPath) async {
71-
if (!serverPath.endsWith('lib.js.map')) return null;
72-
if (!serverPath.startsWith('/')) serverPath = '/$serverPath';
73-
// Strip the .map, sources are looked up by their js path.
74-
return _mapContents[p.withoutExtension(serverPath)];
73+
Future<String?> sourceMapContents(String serverPath) async {
74+
if (serverPath.endsWith('lib.js.map')) {
75+
if (!serverPath.startsWith('/')) serverPath = '/$serverPath';
76+
// Strip the .map, sources are looked up by their js path.
77+
serverPath = p.withoutExtension(serverPath);
78+
if (_mapContents.containsKey(serverPath)) {
79+
return _mapContents[serverPath];
80+
}
81+
}
82+
_logger.severe('Cannot find source map contents for $serverPath');
83+
return null;
7584
}
7685

7786
/// Updates the internal caches by reading the Frontend Server output files.

dwds/lib/src/readers/proxy_server_asset_reader.dart

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:convert';
97
import 'dart:io';
@@ -20,14 +18,11 @@ import 'asset_reader.dart';
2018
class ProxyServerAssetReader implements AssetReader {
2119
final _logger = Logger('ProxyServerAssetReader');
2220

23-
Handler _handler;
24-
http.Client _client;
21+
late final Handler _handler;
22+
late final http.Client _client;
2523

2624
ProxyServerAssetReader(int assetServerPort,
27-
{String root, String host, bool isHttps}) {
28-
host ??= 'localhost';
29-
root ??= '';
30-
isHttps ??= false;
25+
{String root = '', String host = 'localhost', bool isHttps = false}) {
3126
final scheme = isHttps ? 'https://' : 'http://';
3227
final inner = HttpClient()
3328
..maxConnectionsPerHost = 200
@@ -37,19 +32,19 @@ class ProxyServerAssetReader implements AssetReader {
3732
? IOClient(inner..badCertificateCallback = (cert, host, port) => true)
3833
: IOClient(inner);
3934
var url = '$scheme$host:$assetServerPort/';
40-
if (root?.isNotEmpty ?? false) url += '$root/';
35+
if (root.isNotEmpty) url += '$root/';
4136
_handler = proxyHandler(url, client: _client);
4237
}
4338

4439
@override
45-
Future<String> dartSourceContents(String serverPath) =>
40+
Future<String?> dartSourceContents(String serverPath) =>
4641
_readResource(serverPath);
4742

4843
@override
49-
Future<String> sourceMapContents(String serverPath) =>
44+
Future<String?> sourceMapContents(String serverPath) =>
5045
_readResource(serverPath);
5146

52-
Future<String> _readResource(String path) async {
47+
Future<String?> _readResource(String path) async {
5348
// Handlers expect a fully formed HTML URI. The actual hostname and port
5449
// does not matter.
5550
final response =
@@ -71,7 +66,7 @@ class ProxyServerAssetReader implements AssetReader {
7166
}
7267

7368
@override
74-
Future<String> metadataContents(String serverPath) =>
69+
Future<String?> metadataContents(String serverPath) =>
7570
_readResource(serverPath);
7671

7772
@override

dwds/lib/src/servers/devtools.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart = 2.9
6-
75
import 'dart:io';
86

97
typedef DevtoolsLauncher = Future<DevTools> Function(String hostname);
@@ -17,7 +15,7 @@ class DevTools {
1715
/// Null until [close] is called.
1816
///
1917
/// All subsequent calls to [close] will return this future.
20-
Future<void> _closed;
18+
Future<void>? _closed;
2119

2220
DevTools(this.hostname, this.port, this._server);
2321

dwds/lib/src/servers/extension_backend.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:io';
97

@@ -20,21 +18,20 @@ import 'extension_debugger.dart';
2018
const authenticationResponse = 'Dart Debug Authentication Success!\n\n'
2119
'You can close this tab and launch the Dart Debug Extension again.';
2220

23-
Logger _logger = Logger('ExtensionBackend');
24-
2521
/// A backend for the Dart Debug Extension.
2622
///
2723
/// Sets up an SSE handler to communicate with the extension background.
2824
/// Uses that SSE channel to create an [ExtensionDebugger].
2925
class ExtensionBackend {
26+
static final _logger = Logger('ExtensionBackend');
3027
final String hostname;
3128
final int port;
3229
final HttpServer _server;
3330

3431
/// Null until [close] is called.
3532
///
3633
/// All subsequent calls to [close] will return this future.
37-
Future<void> _closed;
34+
Future<void>? _closed;
3835

3936
ExtensionBackend._(
4037
SocketHandler socketHandler, this.hostname, this.port, this._server)
@@ -47,7 +44,8 @@ class ExtensionBackend {
4744
cascade = cascade.add((request) {
4845
if (request.url.path == authenticationPath) {
4946
return Response.ok(authenticationResponse, headers: {
50-
'Access-Control-Allow-Origin': request.headers['origin'],
47+
if (request.headers.containsKey('origin'))
48+
'Access-Control-Allow-Origin': request.headers['origin']!,
5149
'Access-Control-Allow-Credentials': 'true'
5250
});
5351
}

0 commit comments

Comments
 (0)