Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
enable Travis-CI (#23)
Browse files Browse the repository at this point in the history
* enable Travis-CI
* dartfmt
* Add analysis options
  • Loading branch information
kevmoo authored Nov 15, 2017
1 parent ee7c7df commit 684c748
Show file tree
Hide file tree
Showing 17 changed files with 567 additions and 516 deletions.
22 changes: 22 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
language: dart

dart:
- dev
- stable

dart_task:
- test
- dartanalyzer

matrix:
include:
- dart: dev
dart_task: dartfmt

# Only building master means that we don't run two builds for each pull request.
branches:
only: [master]

cache:
directories:
- $HOME/.pub-cache
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
analyzer:
strong-mode: true
18 changes: 12 additions & 6 deletions lib/error_code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ const SERVER_ERROR = -32000;
/// If [errorCode] isn't defined in the JSON-RPC 2.0 spec, returns null.
String name(int errorCode) {
switch (errorCode) {
case PARSE_ERROR: return "parse error";
case INVALID_REQUEST: return "invalid request";
case METHOD_NOT_FOUND: return "method not found";
case INVALID_PARAMS: return "invalid parameters";
case INTERNAL_ERROR: return "internal error";
default: return null;
case PARSE_ERROR:
return "parse error";
case INVALID_REQUEST:
return "invalid request";
case METHOD_NOT_FOUND:
return "method not found";
case INVALID_PARAMS:
return "invalid parameters";
case INTERNAL_ERROR:
return "internal error";
default:
return null;
}
}
15 changes: 6 additions & 9 deletions lib/src/channel_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,12 @@ class ChannelManager {
}
_listenCalled = true;

_channel.stream.listen(handleInput,
onError: (error, stackTrace) {
_doneCompleter.completeError(error, stackTrace);
_channel.sink.close();
},
onDone: () {
if (!_doneCompleter.isCompleted) _doneCompleter.complete();
},
cancelOnError: true);
_channel.stream.listen(handleInput, onError: (error, stackTrace) {
_doneCompleter.completeError(error, stackTrace);
_channel.sink.close();
}, onDone: () {
if (!_doneCompleter.isCompleted) _doneCompleter.complete();
}, cancelOnError: true);

return done;
}
Expand Down
13 changes: 5 additions & 8 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ class Client {
}
if (isClosed) throw new StateError("The client is closed.");

var message = <String, dynamic>{
"jsonrpc": "2.0",
"method": method
};
var message = <String, dynamic>{"jsonrpc": "2.0", "method": method};
if (id != null) message["id"] = id;
if (parameters != null) message["params"] = parameters;

Expand Down Expand Up @@ -190,10 +187,10 @@ class Client {
if (response.containsKey("result")) {
request.completer.complete(response["result"]);
} else {
request.completer.completeError(new RpcException(
response["error"]["code"],
response["error"]["message"],
data: response["error"]["data"]),
request.completer.completeError(
new RpcException(
response["error"]["code"], response["error"]["message"],
data: response["error"]["data"]),
request.chain);
}
}
Expand Down
13 changes: 7 additions & 6 deletions lib/src/peer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ class Peer implements Client, Server {
/// [Peer.listen] is called.
Peer.withoutJson(StreamChannel channel)
: _manager = new ChannelManager("Peer", channel) {
_server = new Server.withoutJson(new StreamChannel(
_serverIncomingForwarder.stream, channel.sink));
_client = new Client.withoutJson(new StreamChannel(
_clientIncomingForwarder.stream, channel.sink));
_server = new Server.withoutJson(
new StreamChannel(_serverIncomingForwarder.stream, channel.sink));
_client = new Client.withoutJson(
new StreamChannel(_clientIncomingForwarder.stream, channel.sink));
}

// Client methods.
Expand Down Expand Up @@ -92,8 +92,9 @@ class Peer implements Client, Server {
} else {
_serverIncomingForwarder.add(message);
}
} else if (message is List && message.isNotEmpty &&
message.first is Map) {
} else if (message is List &&
message.isNotEmpty &&
message.first is Map) {
if (message.first.containsKey('result') ||
message.first.containsKey('error')) {
_clientIncomingForwarder.add(message);
Expand Down
34 changes: 22 additions & 12 deletions lib/src/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,7 @@ class Server {
// response, even if one is generated on the server.
if (!request.containsKey('id')) return null;

return {
'jsonrpc': '2.0',
'result': result,
'id': request['id']
};
return {'jsonrpc': '2.0', 'result': result, 'id': request['id']};
} catch (error, stackTrace) {
if (error is RpcException) {
if (error.code == error_code.INVALID_REQUEST ||
Expand All @@ -196,40 +192,54 @@ class Server {
/// Validates that [request] matches the JSON-RPC spec.
void _validateRequest(request) {
if (request is! Map) {
throw new RpcException(error_code.INVALID_REQUEST, 'Request must be '
throw new RpcException(
error_code.INVALID_REQUEST,
'Request must be '
'an Array or an Object.');
}

if (!request.containsKey('jsonrpc')) {
throw new RpcException(error_code.INVALID_REQUEST, 'Request must '
throw new RpcException(
error_code.INVALID_REQUEST,
'Request must '
'contain a "jsonrpc" key.');
}

if (request['jsonrpc'] != '2.0') {
throw new RpcException(error_code.INVALID_REQUEST, 'Invalid JSON-RPC '
throw new RpcException(
error_code.INVALID_REQUEST,
'Invalid JSON-RPC '
'version ${JSON.encode(request['jsonrpc'])}, expected "2.0".');
}

if (!request.containsKey('method')) {
throw new RpcException(error_code.INVALID_REQUEST, 'Request must '
throw new RpcException(
error_code.INVALID_REQUEST,
'Request must '
'contain a "method" key.');
}

var method = request['method'];
if (request['method'] is! String) {
throw new RpcException(error_code.INVALID_REQUEST, 'Request method must '
throw new RpcException(
error_code.INVALID_REQUEST,
'Request method must '
'be a string, but was ${JSON.encode(method)}.');
}

var params = request['params'];
if (request.containsKey('params') && params is! List && params is! Map) {
throw new RpcException(error_code.INVALID_REQUEST, 'Request params must '
throw new RpcException(
error_code.INVALID_REQUEST,
'Request params must '
'be an Array or an Object, but was ${JSON.encode(params)}.');
}

var id = request['id'];
if (id != null && id is! String && id is! num) {
throw new RpcException(error_code.INVALID_REQUEST, 'Request id must be a '
throw new RpcException(
error_code.INVALID_REQUEST,
'Request id must be a '
'string, number, or null, but was ${JSON.encode(id)}.');
}
}
Expand Down
Loading

0 comments on commit 684c748

Please sign in to comment.