Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Unified Diff: lib/shelf_packages_handler.dart

Issue 2171743003: Add the package implementation. (Closed) Base URL: git@github.com:dart-lang/shelf_packages_handler.git@master
Patch Set: Code review changes Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « codereview.settings ('k') | lib/src/async_handler.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/shelf_packages_handler.dart
diff --git a/lib/shelf_packages_handler.dart b/lib/shelf_packages_handler.dart
new file mode 100644
index 0000000000000000000000000000000000000000..eda62032d7831f62bd9294c9aaefeade91e48605
--- /dev/null
+++ b/lib/shelf_packages_handler.dart
@@ -0,0 +1,44 @@
+// 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.
+
+library shelf_packages_handler;
+
+import 'package:shelf/shelf.dart';
+import 'package:shelf_static/shelf_static.dart';
+import 'package:package_resolver/package_resolver.dart';
+import 'package:path/path.dart' as p;
+
+import 'src/async_handler.dart';
+import 'src/dir_handler.dart';
+import 'src/package_config_handler.dart';
+
+/// A handler that serves the contents of a virtual packages directory.
+///
+/// This effectively serves `package:${request.url}`. It locates packages using
+/// the package resolution logic defined by [resolver]. If [resolver] isn't
+/// passed, it defaults to the current isolate's package resolution logic.
+///
+/// This can only serve assets from `file:` URIs.
+Handler packagesHandler({PackageResolver resolver}) {
+ resolver ??= PackageResolver.current;
+ return new AsyncHandler(resolver.packageRoot.then((packageRoot) {
+ if (packageRoot != null) {
+ return createStaticHandler(p.fromUri(packageRoot),
+ serveFilesOutsidePath: true);
+ } else {
+ return new PackageConfigHandler(resolver);
+ }
+ }));
+}
+
+/// A handler that serves virtual `packages/` directories wherever they're
+/// requested.
+///
+/// This serves the same assets as [packagesHandler] for every URL that contains
+/// `/packages/`. Otherwise, it returns 404s for all requests.
+///
+/// This is useful for ensuring that `package:` imports work for all entrypoints
+/// in Dartium.
+Handler packagesDirHandler({PackageResolver resolver}) =>
+ new DirHandler("packages", packagesHandler(resolver: resolver));
« no previous file with comments | « codereview.settings ('k') | lib/src/async_handler.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698