Use configurable imports to avoid having two versions of everything.
R=floitsch@google.com
Review URL: https://codereview.chromium.org//2353933002 .
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 674b8df..99b366e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,10 @@
# Changelog
+## 2.0.0
+- Use configuration dependent imports to avoid having separate implementations
+ for `dart:io` and `dart:html`.
+- Remove `browser_resource.dart`.
+
## 1.1.0
- Added browser-compatible version as `browser_resource.dart` library.
diff --git a/lib/browser_resource.dart b/lib/browser_resource.dart
deleted file mode 100644
index cf2db65..0000000
--- a/lib/browser_resource.dart
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
-// 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.
-
-/// A [Resource] is data that can be read into a Dart program.
-///
-/// A resource is identified by a URI. It can be loaded as bytes or data.
-/// The resource URI may be a `package:` URI.
-///
-/// Example:
-///
-/// var resource = new Resource("package:foo/foo_data.txt");
-/// var string = await resource.readAsString(UTF8);
-/// print(string);
-///
-/// Example:
-///
-/// var resource = new Resource("http://example.com/data.json");
-/// var obj = await resource.openRead() // Reads as stream of bytes.
-/// .transform(UTF8.fuse(JSON).decoder)
-/// .first;
-///
-///
-/// Notice: Currently this library requires `dart:Html` to do the reading,
-/// so it doesn't work outside of a browser.
-/// This library will eventually be mergeded into the `resource.dart` when
-/// features are available to make that possible.
-library browser_resource;
-
-export "src/browser/resource.dart" show Resource;
-export "src/browser/loader.dart" show ResourceLoader;
diff --git a/lib/resource.dart b/lib/resource.dart
index 91c74a0..4326d5f 100644
--- a/lib/resource.dart
+++ b/lib/resource.dart
@@ -19,12 +19,7 @@
/// var obj = await resource.openRead() // Reads as stream of bytes.
/// .transform(UTF8.fuse(JSON).decoder)
/// .first;
-///
-///
-/// Notice: Currently this library requires `dart:io` to do the reading,
-/// so it doesn't work in the browser. Use `browser_resource.dart` in the
-/// browser.
library resource;
-export "src/io/resource.dart" show Resource;
-export "src/io/loader.dart" show ResourceLoader;
+export "src/resource.dart" show Resource;
+export "src/resource_loader.dart" show ResourceLoader;
diff --git a/lib/src/browser/loader.dart b/lib/src/browser/loader.dart
deleted file mode 100644
index 0ecaf67..0000000
--- a/lib/src/browser/loader.dart
+++ /dev/null
@@ -1,55 +0,0 @@
-// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
-// 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.
-
-import "dart:async" show Future, Stream;
-import "dart:convert" show Encoding;
-
-import "../resource_loader.dart" as base;
-import "../package_loader.dart"as base;
-import "html_io.dart" as io;
-
-/// Resource loading strategy.
-///
-/// An abstraction of the functionality needed to load resources.
-///
-/// Implementations of this interface decide which URI schemes they support.
-abstract class ResourceLoader implements base.ResourceLoader {
- /// A resource loader that can load as many of the following URI
- /// schemes as are supported by the platform:
- /// * file
- /// * http
- /// * https
- /// * data
- /// * package
- ///
- /// (For example, file: URIs are not supported in the browser).
- /// Relative URI references are accepted - they are resolved against
- /// [Uri.base] before being loaded.
- static ResourceLoader get defaultLoader =>
- const PackageLoader(const DefaultLoader());
-}
-
-/// Default implementation of [ResourceLoader]..
-///
-/// Uses the system's available loading functionality to implement the
-/// loading functions.
-///
-/// Supports as many of `http:`, `https:`, `file:` and `data:` URIs as
-/// possible.
-class DefaultLoader implements ResourceLoader {
- const DefaultLoader();
-
- Stream<List<int>> openRead(Uri uri) => io.readAsStream(uri);
-
- Future<List<int>> readAsBytes(Uri uri) => io.readAsBytes(uri);
-
- Future<String> readAsString(Uri uri, {Encoding encoding}) =>
- io.readAsString(uri, encoding);
-}
-
-// A loader that implements base.PackageLoader *and* ResourceLoader from this
-// file.
-class PackageLoader extends base.PackageLoader implements ResourceLoader {
- const PackageLoader(ResourceLoader loader) : super(loader);
-}
diff --git a/lib/src/browser/resource.dart b/lib/src/browser/resource.dart
deleted file mode 100644
index 9368e4e..0000000
--- a/lib/src/browser/resource.dart
+++ /dev/null
@@ -1,35 +0,0 @@
-// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
-// 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.
-
-import "dart:core" hide Resource;
-
-import "loader.dart" show ResourceLoader, DefaultLoader;
-
-// TODO(lrn): Merge with implementation when configured imports removes
-// the need to share code.
-import "../resource.dart" as base show Resource;
-
-class Resource extends base.Resource {
- /// Creates a resource object with the given [uri] as location.
- ///
- /// The [uri] must be either a [Uri] or a string containing a valid URI.
- /// If the string is not a valid URI, using any of the functions on
- /// the resource object will fail.
- ///
- /// The URI may be relative, in which case it will be resolved
- /// against [Uri.base] before being used.
- ///
- /// The URI may use the `package` scheme, which is always supported.
- /// Other schemes may also be supported where possible.
- ///
- /// If [loader] is provided, it is used to load absolute non-package URIs.
- /// Package: URIs are resolved to a non-package URI before being loaded, so
- /// the loader doesn't have to support package: URIs, nor does it need to
- /// support relative URI references.
- /// If [loader] is omitted, a default implementation is used which supports
- /// as many of `http`, `https`, `file` and `data` as are available on the
- /// current platform.
- const Resource(uri, {ResourceLoader loader})
- : super(uri, (loader != null) ? loader : const DefaultLoader());
-}
diff --git a/lib/src/io/loader.dart b/lib/src/io/loader.dart
deleted file mode 100644
index d52cf44..0000000
--- a/lib/src/io/loader.dart
+++ /dev/null
@@ -1,55 +0,0 @@
-// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
-// 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.
-
-import "dart:async" show Future, Stream;
-import "dart:convert" show Encoding;
-
-import "../resource_loader.dart" as base;
-import "../package_loader.dart"as base;
-import "io.dart" as io;
-
-/// Resource loading strategy.
-///
-/// An abstraction of the functionality needed to load resources.
-///
-/// Implementations of this interface decide which URI schemes they support.
-abstract class ResourceLoader implements base.ResourceLoader {
- /// A resource loader that can load as many of the following URI
- /// schemes as are supported by the platform:
- /// * file
- /// * http
- /// * https
- /// * data
- /// * package
- ///
- /// (For example, file: URIs are not supported in the browser).
- /// Relative URI references are accepted - they are resolved against
- /// [Uri.base] before being loaded.
- static ResourceLoader get defaultLoader =>
- const PackageLoader(const DefaultLoader());
-}
-
-/// Default implementation of [ResourceLoader]..
-///
-/// Uses the system's available loading functionality to implement the
-/// loading functions.
-///
-/// Supports as many of `http:`, `https:`, `file:` and `data:` URIs as
-/// possible.
-class DefaultLoader implements ResourceLoader {
- const DefaultLoader();
-
- Stream<List<int>> openRead(Uri uri) => io.readAsStream(uri);
-
- Future<List<int>> readAsBytes(Uri uri) => io.readAsBytes(uri);
-
- Future<String> readAsString(Uri uri, {Encoding encoding}) =>
- io.readAsString(uri, encoding);
-}
-
-// A loader that implements base.PackageLoader *and* ResourceLoader from this
-// file.
-class PackageLoader extends base.PackageLoader implements ResourceLoader {
- const PackageLoader(ResourceLoader loader) : super(loader);
-}
diff --git a/lib/src/io/resource.dart b/lib/src/io/resource.dart
deleted file mode 100644
index 4ca3b8f..0000000
--- a/lib/src/io/resource.dart
+++ /dev/null
@@ -1,35 +0,0 @@
-// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
-// 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.
-
-import "dart:core" hide Resource;
-
-import "loader.dart" show ResourceLoader, DefaultLoader;
-
-// TODO(lrn): Merge with implementation when configured imports removes
-// the need to share code.
-import "../resource.dart" as base show Resource;
-
-class Resource extends base.Resource {
- /// Creates a resource object with the given [uri] as location.
- ///
- /// The [uri] must be either a [Uri] or a string containing a valid URI.
- /// If the string is not a valid URI, using any of the functions on
- /// the resource object will fail.
- ///
- /// The URI may be relative, in which case it will be resolved
- /// against [Uri.base] before being used.
- ///
- /// The URI may use the `package` scheme, which is always supported.
- /// Other schemes may also be supported where possible.
- ///
- /// If [loader] is provided, it is used to load absolute non-package URIs.
- /// Package: URIs are resolved to a non-package URI before being loaded, so
- /// the loader doesn't have to support package: URIs, nor does it need to
- /// support relative URI references.
- /// If [loader] is omitted, a default implementation is used which supports
- /// as many of `http`, `https`, `file` and `data` as are available on the
- /// current platform.
- const Resource(uri, {ResourceLoader loader})
- : super(uri, (loader != null) ? loader : const DefaultLoader());
-}
diff --git a/lib/src/browser/html_io.dart b/lib/src/io_html.dart
similarity index 100%
rename from lib/src/browser/html_io.dart
rename to lib/src/io_html.dart
diff --git a/lib/src/io/io.dart b/lib/src/io_io.dart
similarity index 100%
rename from lib/src/io/io.dart
rename to lib/src/io_io.dart
diff --git a/lib/src/io_none.dart b/lib/src/io_none.dart
new file mode 100644
index 0000000..77e90cc
--- /dev/null
+++ b/lib/src/io_none.dart
@@ -0,0 +1,30 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
+// 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.
+
+import "dart:async" show Future, Stream;
+
+/// Read the bytes of a URI as a stream of bytes.
+Stream<List<int>> readAsStream(Uri uri) async* {
+ if (uri.scheme == "data") {
+ yield uri.data.contentAsBytes();
+ return;
+ }
+ throw new UnsupportedError("Unsupported scheme: $uri");
+}
+
+/// Read the bytes of a URI as a list of bytes.
+Future<List<int>> readAsBytes(Uri uri) async {
+ if (uri.scheme == "data") {
+ return uri.data.contentAsBytes();
+ }
+ throw new UnsupportedError("Unsupported scheme: $uri");
+}
+
+/// Read the bytes of a URI as a string.
+Future<String> readAsString(Uri uri, Encoding encoding) async {
+ if (uri.scheme == "data") {
+ return uri.data.contentAsString(encoding: encoding);
+ }
+ throw new UnsupportedError("Unsupported scheme: $uri");
+}
diff --git a/lib/src/package_loader.dart b/lib/src/package_loader.dart
index b834c79..e8c153e 100644
--- a/lib/src/package_loader.dart
+++ b/lib/src/package_loader.dart
@@ -14,7 +14,7 @@
/// package URIs and relative URI references before loading them.
///
/// This class may be useful when you don't want to bother creating a resource
-/// object, and just want to load a resource directly.
+/// object, and just want to load a package resource directly.
class PackageLoader implements ResourceLoader {
final ResourceLoader _loader;
const PackageLoader(ResourceLoader loader) : _loader = loader;
diff --git a/lib/src/resource.dart b/lib/src/resource.dart
index 2b19d9e..c13c2ea 100644
--- a/lib/src/resource.dart
+++ b/lib/src/resource.dart
@@ -2,7 +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.
-import "dart:core" hide Resource;
import "dart:async" show Future, Stream;
import "dart:convert" show Encoding;
@@ -23,8 +22,28 @@
/// The URI of the resource.
final _uri;
- const Resource(uri, ResourceLoader loader)
- : _uri = uri, _loader = loader;
+ /// Creates a resource object with the given [uri] as location.
+ ///
+ /// The [uri] must be either a [Uri] or a [String] containing a valid URI.
+ /// If the string is not a valid URI, using any of the functions on
+ /// the resource object will fail.
+ ///
+ /// The URI may be relative, in which case it will be resolved
+ /// against [Uri.base] before being used.
+ ///
+ /// The URI may use the `package` scheme,
+ /// which is supported if the platform can load files at runtime at all.
+ /// Other schemes may also be supported where possible.
+ ///
+ /// If [loader] is provided, it is used to load absolute non-package URIs.
+ /// Package: URIs are resolved to a non-package URI before being loaded, so
+ /// the loader doesn't have to support package: URIs, nor does it need to
+ /// support relative URI references.
+ /// If [loader] is omitted, a default implementation is used which supports
+ /// as many of `http`, `https`, `file` and `data` as are available on the
+ /// current platform.
+ const Resource(uri, {ResourceLoader loader})
+ : _uri = uri, _loader = loader ?? const DefaultLoader();
/// The location URI of this resource.
///
diff --git a/lib/src/resource_loader.dart b/lib/src/resource_loader.dart
index 1a189e1..3e0c78c 100644
--- a/lib/src/resource_loader.dart
+++ b/lib/src/resource_loader.dart
@@ -4,6 +4,11 @@
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"
+ as io;
/// Resource loading strategy.
///
@@ -11,6 +16,23 @@
///
/// Implementations of this interface decide which URI schemes they support.
abstract class ResourceLoader {
+ /// A resource loader that can load as many of the following URI
+ /// schemes as are supported by the platform:
+ /// * file
+ /// * http
+ /// * https
+ /// * data
+ /// * package
+ ///
+ /// For example, `file:` URIs are not supported in the browser.
+ /// Relative URI references are accepted - they are resolved against
+ /// [Uri.base] before being loaded.
+ ///
+ /// This loader is automatically used by the [Resource] class
+ /// if no other loader is specified.
+ static ResourceLoader get defaultLoader =>
+ const PackageLoader(const DefaultLoader());
+
/// Reads the file located by [uri] as a stream of bytes.
Stream<List<int>> openRead(Uri uri);
@@ -29,3 +51,21 @@
Future<String> readAsString(Uri uri, {Encoding encoding});
}
+/// Default implementation of [ResourceLoader].
+///
+/// Uses the system's available loading functionality to implement the
+/// loading functions.
+///
+/// Supports as many of `http:`, `https:`, `file:` and `data:` URIs as
+/// possible.
+class DefaultLoader implements ResourceLoader {
+ const DefaultLoader();
+
+ Stream<List<int>> openRead(Uri uri) => io.readAsStream(uri);
+
+ Future<List<int>> readAsBytes(Uri uri) => io.readAsBytes(uri);
+
+ Future<String> readAsString(Uri uri, {Encoding encoding}) =>
+ io.readAsString(uri, encoding);
+}
+
diff --git a/pubspec.yaml b/pubspec.yaml
index e677345..8822f27 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,11 +1,11 @@
name: resource
-version: 1.1.1-dev
+version: 2.0.0
description: Reading resource data from (package and other) files.
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/resource
environment:
- sdk: '>=1.14.0 <2.0.0'
+ sdk: '>=1.20.0 <2.0.0'
dependencies:
typed_data: "^1.0.0"
diff --git a/test/browser_http_test.dart b/test/browser_http_test.dart
index ecec680..ff4edad 100644
--- a/test/browser_http_test.dart
+++ b/test/browser_http_test.dart
@@ -7,7 +7,7 @@
import "dart:async";
import "dart:convert";
-import "package:resource/browser_resource.dart";
+import "package:resource/resource.dart";
import "package:test/test.dart";
const content = "Rødgrød med fløde";