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 library pub.barback.web_socket_api; | 5 library pub.barback.web_socket_api; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:http_parser/http_parser.dart'; | 10 import 'package:http_parser/http_parser.dart'; |
11 import 'package:path/path.dart' as path; | 11 import 'package:path/path.dart' as path; |
12 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; | 12 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; |
13 | 13 |
| 14 import '../exit_codes.dart' as exit_codes; |
| 15 import '../io.dart'; |
| 16 import '../log.dart' as log; |
14 import '../utils.dart'; | 17 import '../utils.dart'; |
15 import 'asset_environment.dart'; | 18 import 'asset_environment.dart'; |
16 | 19 |
17 /// Implements the [WebSocket] API for communicating with a running pub serve | 20 /// Implements the [WebSocket] API for communicating with a running pub serve |
18 /// process, mainly for use by the Editor. | 21 /// process, mainly for use by the Editor. |
19 /// | 22 /// |
20 /// This is a [JSON-RPC 2.0](http://www.jsonrpc.org/specification) server. Its | 23 /// This is a [JSON-RPC 2.0](http://www.jsonrpc.org/specification) server. Its |
21 /// methods are described in the method-level documentation below. | 24 /// methods are described in the method-level documentation below. |
22 class WebSocketApi { | 25 class WebSocketApi { |
23 final CompatibleWebSocket _socket; | 26 final CompatibleWebSocket _socket; |
24 final AssetEnvironment _environment; | 27 final AssetEnvironment _environment; |
25 final _server = new json_rpc.Server(); | 28 final _server = new json_rpc.Server(); |
26 | 29 |
| 30 /// Whether the application should exit when this connection closes. |
| 31 bool _exitOnClose = false; |
| 32 |
27 WebSocketApi(this._socket, this._environment) { | 33 WebSocketApi(this._socket, this._environment) { |
28 _server.registerMethod("urlToAssetId", _urlToAssetId); | 34 _server.registerMethod("urlToAssetId", _urlToAssetId); |
29 _server.registerMethod("pathToUrls", _pathToUrls); | 35 _server.registerMethod("pathToUrls", _pathToUrls); |
30 _server.registerMethod("serveDirectory", _serveDirectory); | 36 _server.registerMethod("serveDirectory", _serveDirectory); |
31 _server.registerMethod("unserveDirectory", _unserveDirectory); | 37 _server.registerMethod("unserveDirectory", _unserveDirectory); |
| 38 |
| 39 /// Tells the server to exit as soon as this WebSocket connection is closed. |
| 40 /// |
| 41 /// This takes no arguments and returns no results. It can safely be called |
| 42 /// as a JSON-RPC notification. |
| 43 _server.registerMethod("exitOnClose", () => _exitOnClose = true); |
32 } | 44 } |
33 | 45 |
34 /// Listens on the socket. | 46 /// Listens on the socket. |
35 /// | 47 /// |
36 /// Returns a future that completes when the socket has closed. It will | 48 /// Returns a future that completes when the socket has closed. It will |
37 /// complete with an error if the socket had an error, otherwise it will | 49 /// complete with an error if the socket had an error, otherwise it will |
38 /// complete to `null`. | 50 /// complete to `null`. |
39 Future listen() { | 51 Future listen() { |
40 return _socket.listen((request) { | 52 return _socket.listen((request) { |
41 _server.parseRequest(request).then((response) { | 53 _server.parseRequest(request).then((response) { |
42 if (response != null) _socket.add(response); | 54 if (response != null) _socket.add(response); |
43 }); | 55 }); |
44 }, cancelOnError: true).asFuture(); | 56 }, cancelOnError: true).asFuture().then((_) { |
| 57 if (!_exitOnClose) return; |
| 58 log.message("WebSocket connection closed, terminating."); |
| 59 flushThenExit(exit_codes.SUCCESS); |
| 60 }); |
45 } | 61 } |
46 | 62 |
47 /// Given a URL to an asset that is served by pub, returns the ID of the | 63 /// Given a URL to an asset that is served by pub, returns the ID of the |
48 /// asset that would be accessed by that URL. | 64 /// asset that would be accessed by that URL. |
49 /// | 65 /// |
50 /// The method name is "urlToAssetId" and it takes a "url" parameter for the | 66 /// The method name is "urlToAssetId" and it takes a "url" parameter for the |
51 /// URL being mapped: | 67 /// URL being mapped: |
52 /// | 68 /// |
53 /// "params": { | 69 /// "params": { |
54 /// "url": "http://localhost:8080/index.html" | 70 /// "url": "http://localhost:8080/index.html" |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 | 303 |
288 | 304 |
289 /// The pub-specific JSON RPC error codes. | 305 /// The pub-specific JSON RPC error codes. |
290 class _Error { | 306 class _Error { |
291 /// The specified directory is not being served. | 307 /// The specified directory is not being served. |
292 static const NOT_SERVED = 1; | 308 static const NOT_SERVED = 1; |
293 | 309 |
294 /// The specified directory overlaps one or more ones already being served. | 310 /// The specified directory overlaps one or more ones already being served. |
295 static const OVERLAPPING = 2; | 311 static const OVERLAPPING = 2; |
296 } | 312 } |
OLD | NEW |