OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'package:package_resolver/package_resolver.dart'; |
| 6 import 'package:path/path.dart' as p; |
| 7 import 'package:shelf/shelf.dart'; |
| 8 import 'package:shelf_static/shelf_static.dart'; |
| 9 |
| 10 import 'async_handler.dart'; |
| 11 |
| 12 /// A shelf handler that serves a virtual packages directory based on a package |
| 13 /// config. |
| 14 class PackageConfigHandler { |
| 15 /// The static handlers for serving entries in the package config, indexed by |
| 16 /// name. |
| 17 final _packageHandlers = new Map<String, Handler>(); |
| 18 |
| 19 /// The information specifying how to do package resolution. |
| 20 PackageResolver _resolver; |
| 21 |
| 22 PackageConfigHandler(this._resolver); |
| 23 |
| 24 /// The callback for handling a single request. |
| 25 call(Request request) { |
| 26 var segments = request.url.pathSegments; |
| 27 return _handlerFor(segments.first)(request.change(path: segments.first)); |
| 28 } |
| 29 |
| 30 /// Creates a handler for [package] based on the package map in [resolver]. |
| 31 Handler _handlerFor(String package) { |
| 32 return _packageHandlers.putIfAbsent(package, () { |
| 33 return new AsyncHandler(_resolver.urlFor(package).then((url) { |
| 34 var handler = url == null |
| 35 ? (_) => new Response.notFound("Package $package not found.") |
| 36 : createStaticHandler(p.fromUri(url), serveFilesOutsidePath: true); |
| 37 |
| 38 return handler; |
| 39 })); |
| 40 }); |
| 41 } |
| 42 } |
OLD | NEW |