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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « lib/src/dir_handler.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
OLDNEW
« 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