Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:io'; | 5 import 'dart:io'; |
| 6 import 'package:http_server/http_server.dart' as http_server; | 6 import 'package:http_server/http_server.dart' as http_server; |
| 7 import 'package:route/server.dart'; | 7 import 'package:route/server.dart'; |
| 8 import 'package:path/path.dart'; | 8 import 'package:path/path.dart'; |
| 9 import 'package:http_server/http_server.dart'; | 9 import 'package:http_server/http_server.dart'; |
| 10 | 10 |
| 11 /* | 11 /* |
| 12 * This program serves a visualization of a JavaScript source map file generated | 12 * This program serves a visualization of a JavaScript source map file generated |
| 13 * by dart2js or pub. | 13 * by dart2js or pub. |
| 14 * | 14 * |
| 15 * Usage: dart source_map_viewer.dart <path to map file>. | 15 * Usage: dart source_map_viewer.dart <path to map file>. |
| 16 * | 16 * |
| 17 * The default system browser is started and pointed to the viewer if | 17 * The default system browser is started and pointed to the viewer if |
| 18 * available. | 18 * available. |
| 19 */ | 19 */ |
| 20 | 20 |
| 21 Directory rootDir = null; | 21 Uri sourceMapFile; |
| 22 String sourceMapFile; | |
| 23 | 22 |
| 24 void main(List<String> args) { | 23 void main(List<String> args) { |
| 25 if (args.length != 1) { | 24 if (args.length != 1) { |
| 26 print('One argument expected; the source map file.'); | 25 print('One argument expected; the source map file.'); |
| 27 exit(-1); | 26 exit(-1); |
| 28 return; | 27 return; |
| 29 } | 28 } |
| 30 | 29 |
| 31 File mapFile = new File(args[0]); | 30 File mapFile = new File(args[0]); |
| 32 if (!mapFile.existsSync()) { | 31 if (!mapFile.existsSync()) { |
| 33 print('Map file not found at ${args[0]}'); | 32 print('Map file not found at ${args[0]}'); |
| 34 exit(-2); | 33 exit(-2); |
| 35 return; | 34 return; |
| 36 } | 35 } |
| 37 | 36 |
| 38 sourceMapFile = basename(mapFile.path); | 37 sourceMapFile = toUri(mapFile.path); |
| 39 rootDir = mapFile.parent; | 38 startServer(); |
| 40 startServer(rootDir); | |
| 41 } | 39 } |
| 42 | 40 |
| 43 // Sends the content of the file requested in the path parameter. | 41 // Sends the content of the file requested in the path parameter. |
| 44 void handleFile(HttpRequest request) { | 42 void handleFile(HttpRequest request) { |
| 45 String path = request.uri.queryParameters["path"]; | 43 String path = request.uri.queryParameters["path"]; |
| 46 if (path == null) { | 44 if (path == null) { |
| 47 request.response.close(); | 45 request.response.close(); |
| 48 return; | 46 return; |
| 49 } | 47 } |
| 50 | 48 |
| 51 path = rootDir.path + '/' + path; | 49 Uri uri = sourceMapFile.resolve(path); |
| 52 new File(Uri.parse(path).toFilePath()).openRead() | 50 new File.fromUri(uri).openRead().pipe(request.response).catchError((e) { |
| 53 .pipe(request.response).catchError((e) { | 51 print("Error: $e"); |
| 54 print("Error: $e"); | |
| 55 request.response.close(); | 52 request.response.close(); |
| 56 }); | 53 }); |
| 57 } | 54 } |
| 58 | 55 |
| 59 // Sends back the name of the source map file. | 56 // Sends back the name of the source map file. |
| 60 void handleSourceMapFile(HttpRequest request) { | 57 void handleSourceMapFile(HttpRequest request) { |
| 61 request.response.write(sourceMapFile); | 58 request.response.write(basename(sourceMapFile.path)); |
| 62 request.response.close(); | 59 request.response.close(); |
| 63 } | 60 } |
| 64 | 61 |
| 65 // Starts an HttpServer rooted in [dir] with two special routes, /file and /map. | 62 // Starts an HttpServer rooted in [dir] with two special routes, /file and /map. |
| 66 // | 63 // |
| 67 // /file takes a parameter [path] and serves the content of the specified file | 64 // /file takes a parameter [path] and serves the content of the specified file |
| 68 // in path relative to [dir] | 65 // in path relative to [dir] |
| 69 // | 66 // |
| 70 // /map serves the name of the map file such that its content can be requested | 67 // /map serves the name of the map file such that its content can be requested |
| 71 // with a /file request as above. | 68 // with a /file request as above. |
| 72 void startServer(Directory dir) { | 69 void startServer() { |
| 73 rootDir = dir; | |
| 74 // Use port 0 to get an ephemeral port. | 70 // Use port 0 to get an ephemeral port. |
| 75 int port = 0; | 71 int port = 0; |
| 76 HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then((server) { | 72 HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, port).then((server) { |
| 77 port = server.port; | 73 port = server.port; |
| 78 print("Source mapping server is running on " | 74 print("Source mapping server is running on " |
| 79 "'http://${server.address.address}:$port/'"); | 75 "'http://${server.address.address}:$port/'"); |
| 80 Router router = new Router(server) | 76 Router router = new Router(server) |
| 81 ..serve('/file').listen(handleFile) | 77 ..serve('/file').listen(handleFile) |
| 82 ..serve('/map').listen(handleSourceMapFile); | 78 ..serve('/map').listen(handleSourceMapFile); |
| 83 | 79 |
| 84 // Set up default handler. This will serve files from our 'build' | 80 // Set up default handler. This will serve files from our 'build' |
| 85 // directory. Disable jail root, as packages are local symlinks. | 81 // directory. Disable jail root, as packages are local symlinks. |
| 86 VirtualDirectory virDir = new http_server.VirtualDirectory(dir.path) | 82 |
| 83 String root = fromUri(Platform.script.resolve('../build/web/')); | |
|
sigurdm
2014/06/06 08:55:45
If I forget to "pub build" this fails silently. Wo
Johnni Winther
2014/06/10 09:23:26
Done.
| |
| 84 VirtualDirectory virDir = new http_server.VirtualDirectory(root) | |
| 87 ..jailRoot = false | 85 ..jailRoot = false |
| 88 ..allowDirectoryListing = true; | 86 ..allowDirectoryListing = true; |
| 89 | 87 |
| 90 virDir.directoryHandler = (dir, request) { | 88 virDir.directoryHandler = (dir, request) { |
| 91 // Redirect directory requests to index.html files. | 89 // Redirect directory requests to index.html files. |
| 92 Uri indexUri = new Uri.file(dir.path).resolve('display.html'); | 90 Uri indexUri = new Uri.file(dir.path).resolve('display.html'); |
| 93 virDir.serveFile(new File(indexUri.toFilePath()), request); | 91 virDir.serveFile(new File(indexUri.toFilePath()), request); |
| 94 }; | 92 }; |
| 95 | 93 |
| 96 // Add an error page handler. | 94 // Add an error page handler. |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 123 } | 121 } |
| 124 } | 122 } |
| 125 | 123 |
| 126 print('Starting browser: ${command} ${url}'); | 124 print('Starting browser: ${command} ${url}'); |
| 127 Process.run(command, ['$url']).then((ProcessResult result) { | 125 Process.run(command, ['$url']).then((ProcessResult result) { |
| 128 if (result.exitCode != 0) { | 126 if (result.exitCode != 0) { |
| 129 print(result.stderr); | 127 print(result.stderr); |
| 130 } | 128 } |
| 131 }); | 129 }); |
| 132 } | 130 } |
| OLD | NEW |