44c6312df8
Also, convert package:vm to use onDiagnostic. We're consolidating all dependencies on package:front_end in one file per tool. The idea is, if you want to depend on something in the package:front_end, you modify pkg/front_end/lib/src/api_unstable/vm.dart and consult with the front-end team. These consolidated files will help us when designing a public API in the future. Change-Id: I66bafdd0ae29605fd12f8d6c589dbc761e5c7a97 Reviewed-on: https://dart-review.googlesource.com/c/77581 Auto-Submit: Peter von der Ahé <ahe@google.com> Reviewed-by: Alexander Markov <alexmarkov@google.com> Commit-Queue: Peter von der Ahé <ahe@google.com>
68 lines
1.9 KiB
Dart
68 lines
1.9 KiB
Dart
import 'dart:async';
|
|
import 'dart:io' as io;
|
|
|
|
import 'package:front_end/src/api_unstable/vm.dart';
|
|
|
|
class HttpAwareFileSystem implements FileSystem {
|
|
FileSystem original;
|
|
|
|
HttpAwareFileSystem(this.original);
|
|
|
|
@override
|
|
FileSystemEntity entityForUri(Uri uri) {
|
|
if (uri.scheme == 'http' || uri.scheme == 'https') {
|
|
return new HttpFileSystemEntity(this, uri);
|
|
} else {
|
|
return original.entityForUri(uri);
|
|
}
|
|
}
|
|
}
|
|
|
|
class HttpFileSystemEntity implements FileSystemEntity {
|
|
HttpAwareFileSystem fileSystem;
|
|
Uri uri;
|
|
|
|
HttpFileSystemEntity(this.fileSystem, this.uri);
|
|
|
|
@override
|
|
Future<bool> exists() async {
|
|
return connectAndRun((io.HttpClient httpClient) async {
|
|
io.HttpClientRequest request = await httpClient.headUrl(uri);
|
|
io.HttpClientResponse response = await request.close();
|
|
return response.statusCode == io.HttpStatus.ok;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Future<List<int>> readAsBytes() async {
|
|
return connectAndRun((io.HttpClient httpClient) async {
|
|
io.HttpClientRequest request = await httpClient.getUrl(uri);
|
|
io.HttpClientResponse response = await request.close();
|
|
if (response.statusCode != io.HttpStatus.ok) {
|
|
throw new FileSystemException(uri, response.toString());
|
|
}
|
|
List<List<int>> list = await response.toList();
|
|
return list.expand((list) => list).toList();
|
|
});
|
|
}
|
|
|
|
@override
|
|
Future<String> readAsString() async {
|
|
return String.fromCharCodes(await readAsBytes());
|
|
}
|
|
|
|
T connectAndRun<T>(T body(io.HttpClient httpClient)) {
|
|
io.HttpClient httpClient;
|
|
try {
|
|
httpClient = new io.HttpClient();
|
|
// Set timeout to be shorter than anticipated OS default
|
|
httpClient.connectionTimeout = const Duration(seconds: 5);
|
|
return body(httpClient);
|
|
} on Exception catch (e) {
|
|
throw new FileSystemException(uri, e.toString());
|
|
} finally {
|
|
httpClient?.close(force: true);
|
|
}
|
|
}
|
|
}
|