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 name. | |
kevmoo
2016/07/21 21:55:07
long line
nweiz
2016/07/21 23:07:24
Done.
| |
16 final _packageHandlers = new Map<String, Handler>(); | |
17 | |
18 /// The information specifying how to do package resolution. | |
19 PackageResolver _resolver; | |
20 | |
21 PackageConfigHandler(this._resolver); | |
22 | |
23 /// The callback for handling a single request. | |
24 call(Request request) { | |
25 var segments = request.url.pathSegments; | |
26 return _handlerFor(segments.first)(request.change(path: segments.first)); | |
27 } | |
28 | |
29 /// Creates a handler for [package] based on the package map in [resolver]. | |
30 Handler _handlerFor(String package) { | |
31 return _packageHandlers.putIfAbsent(package, () { | |
32 return new AsyncHandler(_resolver.urlFor(package).then((url) { | |
33 var handler = url == null | |
34 ? (_) => new Response.notFound("Package $package not found.") | |
35 : createStaticHandler(p.fromUri(url), serveFilesOutsidePath: true); | |
36 | |
37 return handler; | |
38 })); | |
39 }); | |
40 } | |
41 } | |
OLD | NEW |