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:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 import 'package:path/path.dart' as path; | 8 import 'package:path/path.dart' as path; |
9 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; | 9 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; |
10 import 'package:web_socket_channel/web_socket_channel.dart'; | 10 import 'package:web_socket_channel/web_socket_channel.dart'; |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 /// } | 214 /// } |
215 /// | 215 /// |
216 /// If successful, it returns a map containing the URL that can be used to | 216 /// If successful, it returns a map containing the URL that can be used to |
217 /// access the directory. | 217 /// access the directory. |
218 /// | 218 /// |
219 /// "result": { | 219 /// "result": { |
220 /// "url": "http://localhost:8083" | 220 /// "url": "http://localhost:8083" |
221 /// } | 221 /// } |
222 /// | 222 /// |
223 /// If the directory is already being served, returns the previous URL. | 223 /// If the directory is already being served, returns the previous URL. |
224 Future<Map> _serveDirectory(json_rpc.Parameters params) { | 224 Future<Map> _serveDirectory(json_rpc.Parameters params) async { |
225 var rootDirectory = _validateRelativePath(params, "path"); | 225 var rootDirectory = _validateRelativePath(params, "path"); |
226 return _environment.serveDirectory(rootDirectory).then((server) { | 226 try { |
| 227 var server = await _environment.serveDirectory(rootDirectory); |
227 return { | 228 return { |
228 "url": server.url.toString() | 229 "url": server.url.toString() |
229 }; | 230 }; |
230 }).catchError((error) { | 231 } on OverlappingSourceDirectoryException catch (error) { |
231 if (error is! OverlappingSourceDirectoryException) throw error; | |
232 | |
233 var dir = pluralize("directory", error.overlappingDirectories.length, | 232 var dir = pluralize("directory", error.overlappingDirectories.length, |
234 plural: "directories"); | 233 plural: "directories"); |
235 var overlapping = toSentence(error.overlappingDirectories.map( | 234 var overlapping = toSentence(error.overlappingDirectories.map( |
236 (dir) => '"$dir"')); | 235 (dir) => '"$dir"')); |
237 print("data: ${error.overlappingDirectories}"); | 236 print("data: ${error.overlappingDirectories}"); |
238 throw new json_rpc.RpcException(_Error.OVERLAPPING, | 237 throw new json_rpc.RpcException(_Error.OVERLAPPING, |
239 'Path "$rootDirectory" overlaps already served $dir $overlapping.', | 238 'Path "$rootDirectory" overlaps already served $dir $overlapping.', |
240 data: { | 239 data: { |
241 "directories": error.overlappingDirectories | 240 "directories": error.overlappingDirectories |
242 }); | 241 }); |
243 }); | 242 } |
244 } | 243 } |
245 | 244 |
246 /// Given a relative directory path within the entrypoint package, unbinds | 245 /// Given a relative directory path within the entrypoint package, unbinds |
247 /// the server previously bound to that directory and returns its (now | 246 /// the server previously bound to that directory and returns its (now |
248 /// unreachable) URL. | 247 /// unreachable) URL. |
249 /// | 248 /// |
250 /// The method name is "unserveDirectory" and it takes a "path" key (a | 249 /// The method name is "unserveDirectory" and it takes a "path" key (a |
251 /// native OS path relative to the root of the entrypoint package) for the | 250 /// native OS path relative to the root of the entrypoint package) for the |
252 /// directory being unserved: | 251 /// directory being unserved: |
253 /// | 252 /// |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 } | 299 } |
301 | 300 |
302 /// The pub-specific JSON RPC error codes. | 301 /// The pub-specific JSON RPC error codes. |
303 class _Error { | 302 class _Error { |
304 /// The specified directory is not being served. | 303 /// The specified directory is not being served. |
305 static const NOT_SERVED = 1; | 304 static const NOT_SERVED = 1; |
306 | 305 |
307 /// The specified directory overlaps one or more ones already being served. | 306 /// The specified directory overlaps one or more ones already being served. |
308 static const OVERLAPPING = 2; | 307 static const OVERLAPPING = 2; |
309 } | 308 } |
OLD | NEW |