Skip to content
This repository has been archived by the owner on Sep 14, 2021. It is now read-only.

Commit

Permalink
Address a number of open issues.
Browse files Browse the repository at this point in the history
Fix example in README.md (#20) Add missing import in io_io.dart (#18);

Add error message when trying to load resource that cannot be resolved (#17),
instead of just failing when trying to use a `null` URI.

Make HttpClient be shared and reduce max number of connections to same server.
Hopefully this addresses #19. If not, the program really needs more
file descriptors than the OS provides.

R=floitsch@google.com

Review-Url: https://codereview.chromium.org//2666013004 .
  • Loading branch information
lrhn committed Feb 1, 2017
1 parent 39d3a2a commit 15728b5
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 2.1.1
- Reduce max concurrent connections to the same host to 6 when using `dart:io`.
That's the same limit that many browsers use.
- Trying to load a resource from a non-existing package now gives a better
error message.

## 2.1.0
- Make failing HTTP requests throw an `HttpException`.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'dart:convert' show UTF8;
main() async {
var resource = new Resource("package:foo/foo_data.txt");
var string = await resource.readAsString(UTF8);
var string = await resource.readAsString(encoding: UTF8);
print(string);
}
```
Expand Down
7 changes: 5 additions & 2 deletions lib/src/io_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "dart:io" show File,
HttpClient,
HttpClientResponse,
HttpClientRequest,
HttpException,
HttpHeaders;

import "package:typed_data/typed_buffers.dart" show Uint8Buffer;
Expand Down Expand Up @@ -90,9 +91,11 @@ Future<String> readAsString(Uri uri, Encoding encoding) async {
throw new UnsupportedError("Unsupported scheme: $uri");
}

HttpClient _sharedHttpClient = new HttpClient()..maxConnectionsPerHost = 6;

Future<HttpClientResponse> _httpGetBytes(Uri uri) async {
HttpClientRequest request = await new HttpClient().getUrl(uri);
request.headers.set(HttpHeaders.ACCEPT, "application/octet-stream, */*");
HttpClientRequest request = await _sharedHttpClient.getUrl(uri);
request.headers.set(HttpHeaders.ACCEPT, "application/octet-stream, */*");
return request.close();
}

Expand Down
11 changes: 8 additions & 3 deletions lib/src/resolve.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import "dart:async" show Future;
import "dart:isolate" show Isolate;

/// Helper function for resolving to a non-relative, non-package URI.
Future<Uri> resolveUri(Uri uri) async {
Future<Uri> resolveUri(Uri uri) {
if (uri.scheme == "package") {
return Isolate.resolvePackageUri(uri);
return Isolate.resolvePackageUri(uri).then((resolvedUri) {
if (resolvedUri == null) {
throw new ArgumentError.value(uri, "uri", "Unknown package");
}
return resolvedUri;
});
}
return Uri.base.resolveUri(uri);
return new Future<Uri>.value(Uri.base.resolveUri(uri));
}
2 changes: 1 addition & 1 deletion lib/src/resource_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import "dart:async" show Future, Stream;
import "dart:convert" show Encoding;
import "package_loader.dart";
import "io_none.dart"
if (dart.library.io) "io_io.dart"
if (dart.library.html) "io_html.dart"
if (dart.library.io) "io_io.dart"
as io;

/// Resource loading strategy.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: resource
version: 2.1.0
version: 2.1.1
description: Reading resource data from (package and other) files.
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/resource
Expand Down
4 changes: 2 additions & 2 deletions test/resource_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ main() {
}

test("load package: URIs", () async {
await testLoad(pkguri("foo/bar/baz"));
await testLoad(pkguri("bar/foo/baz"));
await testLoad(pkguri("resource/bar/baz"));
await testLoad(pkguri("test/foo/baz"));
});
test("load non-pkgUri", () async {
await testLoad(Uri.parse("file://localhost/something?x#y"));
Expand Down

0 comments on commit 15728b5

Please sign in to comment.