Skip to content
2 changes: 0 additions & 2 deletions dwds/lib/src/connections/app_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

import 'dart:async';
import 'dart:convert';

Expand Down
26 changes: 13 additions & 13 deletions dwds/lib/src/handlers/injector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

import 'dart:async';
import 'dart:convert';
import 'dart:io';
Expand Down Expand Up @@ -32,7 +30,7 @@ const _clientScript = 'dwds/src/injected/client';
/// information.
class DwdsInjector {
final LoadStrategy _loadStrategy;
final Future<String> _extensionUri;
final Future<String>? _extensionUri;
final _devHandlerPaths = StreamController<String>();
final _logger = Logger('DwdsInjector');
final bool _enableDevtoolsLaunch;
Expand All @@ -41,14 +39,14 @@ class DwdsInjector {

DwdsInjector(
this._loadStrategy, {
Future<String> extensionUri,
bool enableDevtoolsLaunch,
bool useSseForInjectedClient,
bool emitDebugEvents,
Future<String>? extensionUri,
bool enableDevtoolsLaunch = false,
bool useSseForInjectedClient = true,
bool emitDebugEvents = true,
}) : _extensionUri = extensionUri,
_enableDevtoolsLaunch = enableDevtoolsLaunch,
_useSseForInjectedClient = useSseForInjectedClient ?? true,
_emitDebugEvents = emitDebugEvents ?? true;
_useSseForInjectedClient = useSseForInjectedClient,
_emitDebugEvents = emitDebugEvents;

/// Returns the embedded dev handler paths.
///
Expand All @@ -60,6 +58,9 @@ class DwdsInjector {
if (request.url.path.endsWith('$_clientScript.js')) {
final uri = await Isolate.resolvePackageUri(
Uri.parse('package:$_clientScript.js'));
if (uri == null) {
throw StateError('Cannot resolve "package:$_clientScript.js"');
}
final result = await File(uri.toFilePath()).readAsString();
return Response.ok(result, headers: {
HttpHeaders.contentTypeHeader: 'application/javascript'
Expand Down Expand Up @@ -123,8 +124,7 @@ class DwdsInjector {
return response.change(body: body, headers: newHeaders);
} else {
final loadResponse = await _loadStrategy.handler(request);
if (loadResponse != null &&
loadResponse.statusCode != HttpStatus.notFound) {
if (loadResponse.statusCode != HttpStatus.notFound) {
return loadResponse;
}
return innerHandler(request);
Expand All @@ -140,7 +140,7 @@ String _injectClientAndHoistMain(
String appId,
String devHandlerPath,
String entrypointPath,
String extensionUri,
String? extensionUri,
LoadStrategy loadStrategy,
bool enableDevtoolsLaunch,
bool emitDebugEvents,
Expand Down Expand Up @@ -194,7 +194,7 @@ String _injectedClientSnippet(
String appId,
String devHandlerPath,
String entrypointPath,
String extensionUri,
String? extensionUri,
LoadStrategy loadStrategy,
bool enableDevtoolsLaunch,
bool emitDebugEvents,
Expand Down
51 changes: 30 additions & 21 deletions dwds/lib/src/readers/frontend_server_asset_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

import 'dart:convert';
import 'dart:io';

import 'package:logging/logging.dart';
import 'package:package_config/package_config.dart';
import 'package:path/path.dart' as p;

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

@override
Future<String> dartSourceContents(String serverPath) async {
if (!serverPath.endsWith('.dart')) return null;
final packageConfig = await _packageConfig;

Uri fileUri;
if (serverPath.startsWith('packages/')) {
final packagePath = serverPath.replaceFirst('packages/', 'package:');
fileUri = packageConfig.resolve(Uri.parse(packagePath));
} else {
fileUri = p.toUri(p.join(_packageRoot, serverPath));
Future<String?> dartSourceContents(String serverPath) async {
if (serverPath.endsWith('.dart')) {
final packageConfig = await _packageConfig;

Uri? fileUri;
if (serverPath.startsWith('packages/')) {
final packagePath = serverPath.replaceFirst('packages/', 'package:');
fileUri = packageConfig.resolve(Uri.parse(packagePath));
} else {
fileUri = p.toUri(p.join(_packageRoot, serverPath));
}
if (fileUri != null) {
final source = File(fileUri.toFilePath());
if (source.existsSync()) return source.readAsString();
}
}

final source = File(fileUri.toFilePath());
if (!await source.exists()) return null;
return await source.readAsString();
_logger.severe('Cannot find source contents for $serverPath');
return null;
}

@override
Future<String> sourceMapContents(String serverPath) async {
if (!serverPath.endsWith('lib.js.map')) return null;
if (!serverPath.startsWith('/')) serverPath = '/$serverPath';
// Strip the .map, sources are looked up by their js path.
return _mapContents[p.withoutExtension(serverPath)];
Future<String?> sourceMapContents(String serverPath) async {
if (serverPath.endsWith('lib.js.map')) {
if (!serverPath.startsWith('/')) serverPath = '/$serverPath';
// Strip the .map, sources are looked up by their js path.
serverPath = p.withoutExtension(serverPath);
if (_mapContents.containsKey(serverPath)) {
return _mapContents[serverPath];
}
}
_logger.severe('Cannot find source map contents for $serverPath');
return null;
}

/// Updates the internal caches by reading the Frontend Server output files.
Expand Down
21 changes: 8 additions & 13 deletions dwds/lib/src/readers/proxy_server_asset_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

import 'dart:async';
import 'dart:convert';
import 'dart:io';
Expand All @@ -20,14 +18,11 @@ import 'asset_reader.dart';
class ProxyServerAssetReader implements AssetReader {
final _logger = Logger('ProxyServerAssetReader');

Handler _handler;
http.Client _client;
late final Handler _handler;
late final http.Client _client;

ProxyServerAssetReader(int assetServerPort,
{String root, String host, bool isHttps}) {
host ??= 'localhost';
root ??= '';
isHttps ??= false;
{String root = '', String host = 'localhost', bool isHttps = false}) {
final scheme = isHttps ? 'https://' : 'http://';
final inner = HttpClient()
..maxConnectionsPerHost = 200
Expand All @@ -37,19 +32,19 @@ class ProxyServerAssetReader implements AssetReader {
? IOClient(inner..badCertificateCallback = (cert, host, port) => true)
: IOClient(inner);
var url = '$scheme$host:$assetServerPort/';
if (root?.isNotEmpty ?? false) url += '$root/';
if (root.isNotEmpty) url += '$root/';
_handler = proxyHandler(url, client: _client);
}

@override
Future<String> dartSourceContents(String serverPath) =>
Future<String?> dartSourceContents(String serverPath) =>
_readResource(serverPath);

@override
Future<String> sourceMapContents(String serverPath) =>
Future<String?> sourceMapContents(String serverPath) =>
_readResource(serverPath);

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

@override
Future<String> metadataContents(String serverPath) =>
Future<String?> metadataContents(String serverPath) =>
_readResource(serverPath);

@override
Expand Down
4 changes: 1 addition & 3 deletions dwds/lib/src/servers/devtools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

import 'dart:io';

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

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

Expand Down
10 changes: 4 additions & 6 deletions dwds/lib/src/servers/extension_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// @dart = 2.9

import 'dart:async';
import 'dart:io';

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

Logger _logger = Logger('ExtensionBackend');

/// A backend for the Dart Debug Extension.
///
/// Sets up an SSE handler to communicate with the extension background.
/// Uses that SSE channel to create an [ExtensionDebugger].
class ExtensionBackend {
static final _logger = Logger('ExtensionBackend');
final String hostname;
final int port;
final HttpServer _server;

/// Null until [close] is called.
///
/// All subsequent calls to [close] will return this future.
Future<void> _closed;
Future<void>? _closed;

ExtensionBackend._(
SocketHandler socketHandler, this.hostname, this.port, this._server)
Expand All @@ -47,7 +44,8 @@ class ExtensionBackend {
cascade = cascade.add((request) {
if (request.url.path == authenticationPath) {
return Response.ok(authenticationResponse, headers: {
'Access-Control-Allow-Origin': request.headers['origin'],
if (request.headers.containsKey('origin'))
'Access-Control-Allow-Origin': request.headers['origin']!,
'Access-Control-Allow-Credentials': 'true'
});
}
Expand Down
Loading