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

Unified Diff: lib/src/package_config_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 | « lib/src/dir_handler.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/package_config_handler.dart
diff --git a/lib/src/package_config_handler.dart b/lib/src/package_config_handler.dart
new file mode 100644
index 0000000000000000000000000000000000000000..fd792901fca3599e58fc62843f34b0d20fd75413
--- /dev/null
+++ b/lib/src/package_config_handler.dart
@@ -0,0 +1,42 @@
+// 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 'package:package_resolver/package_resolver.dart';
+import 'package:path/path.dart' as p;
+import 'package:shelf/shelf.dart';
+import 'package:shelf_static/shelf_static.dart';
+
+import 'async_handler.dart';
+
+/// A shelf handler that serves a virtual packages directory based on a package
+/// config.
+class PackageConfigHandler {
+ /// The static handlers for serving entries in the package config, indexed by
+ /// name.
+ final _packageHandlers = new Map<String, Handler>();
+
+ /// The information specifying how to do package resolution.
+ PackageResolver _resolver;
+
+ PackageConfigHandler(this._resolver);
+
+ /// The callback for handling a single request.
+ call(Request request) {
+ var segments = request.url.pathSegments;
+ return _handlerFor(segments.first)(request.change(path: segments.first));
+ }
+
+ /// Creates a handler for [package] based on the package map in [resolver].
+ Handler _handlerFor(String package) {
+ return _packageHandlers.putIfAbsent(package, () {
+ return new AsyncHandler(_resolver.urlFor(package).then((url) {
+ var handler = url == null
+ ? (_) => new Response.notFound("Package $package not found.")
+ : createStaticHandler(p.fromUri(url), serveFilesOutsidePath: true);
+
+ return handler;
+ }));
+ });
+ }
+}
« no previous file with comments | « lib/src/dir_handler.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698