Skip to content

Commit

Permalink
Adding initial tests and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nojvek committed Jul 21, 2016
1 parent 2377125 commit 1bb4b4e
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
service_name: travis-pro
repo_token: 93ms0A7jcSsudOFUEZ7DpVGpDEdR1isO2
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
typings
out
out
coverage
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
language: node_js

node_js:
- "node"
- node

install:
- npm install -g typescript@next
- npm install -g mocha
- npm install -g typings
- typings install
- npm install
Expand All @@ -15,4 +14,7 @@ before_script:

script:
- npm run build
- npm run test
- npm test

after_success:
- npm run coveralls
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Noice Json Rpc
[![build status](https://travis-ci.org/nojvek/noice-json-rpc.svg?branch=master)](https://travis-ci.org/nojvek/noice-json-rpc)
[![Build Status](https://travis-ci.org/nojvek/noice-json-rpc.svg?branch=master)](https://travis-ci.org/nojvek/noice-json-rpc)
[![Coverage Status](https://coveralls.io/repos/github/nojvek/noice-json-rpc/badge.svg?branch=master)](https://coveralls.io/github/nojvek/noice-json-rpc?branch=master)
[![npm version](https://badge.fury.io/js/noice-json-rpc.svg)](https://badge.fury.io/js/noice-json-rpc)


Client and Server helpers to implement a clean function based Api for Json Rpc.

Expand Down
92 changes: 92 additions & 0 deletions lib/json-rpc2.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* Interface according to spec from http://www.jsonrpc.org/specification
* JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol.
* Primarily this specification defines several data structures.
* It is transport agnostic in that the concepts can be used within the same process,
* over sockets, over http, or in many various message passing environments. I
* It uses JSON (RFC 4627) as data format.
*/
export declare namespace JsonRpc2 {
/**
* Request object representation of a rpc call.
* Server always replies with a Response object having the same id.
*/
interface Request extends Notification {
/** An identifier established by the Client */
id: number;
}
/**
* Client can send a request with no expectation of a response.
* Server can send a notification without an explicit request by a client.
*/
interface Notification {
/** Name of the method to be invoked. */
method: string;
/** Parameter values to be used during the invocation of the method. */
params?: any;
/** Version of the JSON-RPC protocol. MUST be exactly "2.0". */
jsonrpc?: '2.0';
}
/**
* Response object representation of a rpc call.
* Response will always contain a result property unless an error occured.
* In which case, an error property is present.
*/
interface Response {
/** An identifier established by the Client. */
id: number;
/** Result object from the Server if method invocation was successful. */
result?: any;
/** Error object from Server if method invocation resulted in an error. */
error?: Error;
/** Version of the JSON-RPC protocol. MUST be exactly "2.0". */
jsonrpc?: '2.0';
}
/**
* Error object representation when a method invocation fails.
*/
interface Error {
/** Indicates the error type that occurred. */
code: ErrorCode;
/** A short description of the error. */
message: string;
/** Additional information about the error */
data?: any;
}
const enum ErrorCode {
/** Parse error Invalid JSON was received by the Server. */
ParseError = -32700,
/** Invalid Request The JSON sent is not a valid Request object. */
InvalidRequest = -32600,
/** The method does not exist / is not available. */
MethodNotFound = -32601,
/** Invalid method parameter(s). */
InvalidParams = 32602,
/** Internal JSON-RPC error. */
InternalError = -32603,
}
type PromiseOrNot<T> = Promise<T> | T;
/** A JsonRPC Client that abstracts the transportation of messages to and from the Server. */
interface Client {
/** Creates a Request object and sends to the Server. Returns the Response from the Server as a Promise. */
call: (method: string, params: any) => Promise<any>;
/** Invokes the handler function when Server sends a notification. */
on: (method: string, handler: (params: any) => void) => void;
/** Sends a notification to the Server. */
notify: (method: string, params?: any) => void;
}
/** A JsonRPC Server that abstracts the transportation of messages to and from the Client */
interface Server {
/**
* Invokes the handler function when Client sends a Request and sends the Response back.
* If handler function returns a Promise, then it waits for the promise to be resolved or rejected before returning.
* It also wraps the handler in a trycatch so it can send an error response when an exception is thrown.
*/
expose: (method: string, handler: (params: any) => Promise<any>) => void;
/** Invokes the handler function when Client sends a notification. */
on: (method: string, handler: (params: any) => void) => void;
/** Sends a notification to the Client. */
notify: (method: string, params?: any) => void;
}
}
export default JsonRpc2;
1 change: 1 addition & 0 deletions lib/json-rpc2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";
17 changes: 9 additions & 8 deletions lib/noice-json-rpc.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/// <reference path="../typings/index.d.ts" />
import { JsonRpc2 } from './json-rpc2';
import { EventEmitter } from 'events';
export interface LikeSocket {
send(message: string): void;
on(event: string, cb: Function): this;
removeListener(event: string, cb: Function): this;
on(event: 'open', cb: (ws: LikeSocket) => void): this;
on(event: 'message', cb: (data: string) => void): this;
on(event: 'error', cb: (err: Error) => void): this;
on(event: string, cb: Function): any;
removeListener(event: string, cb: Function): any;
on(event: 'open', cb: (ws: LikeSocket) => void): any;
on(event: 'message', cb: (data: string) => void): any;
on(event: 'error', cb: (err: Error) => void): any;
}
export interface LikeSocketServer {
on(event: string, cb: Function): this;
on(event: 'connection', cb: (ws: LikeSocket) => void): this;
on(event: 'error', cb: (err: Error) => void): this;
on(event: string, cb: Function): any;
on(event: 'connection', cb: (ws: LikeSocket) => void): any;
on(event: 'error', cb: (err: Error) => void): any;
clients?: LikeSocket[];
}
export interface LogOpts {
Expand Down
7 changes: 7 additions & 0 deletions lib/noice-json-rpc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference path="../typings/index.d.ts" />
"use strict";
const events_1 = require('events');
/**
Expand All @@ -14,6 +15,9 @@ class Client extends events_1.EventEmitter {
this._emitLog = false;
this._consoleLog = false;
this.setLogging(opts);
if (!socket) {
throw new TypeError("socket cannot be undefined or null");
}
this._connectedPromise = new Promise((resolve, reject) => {
this._socket = socket;
socket.on('error', reject);
Expand Down Expand Up @@ -147,6 +151,9 @@ class Server extends events_1.EventEmitter {
this._emitLog = false;
this._consoleLog = false;
this.setLogging(opts);
if (!server) {
throw new TypeError("server cannot be undefined or null");
}
this._socketServer = server;
server.on('error', (e) => this.emit('error', e));
server.on('connection', socket => {
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
],
"scripts": {
"build": "tsc -p src",
"clean": "rm -rf lib",
"test": "echo 'test'"
"clean": "rm -rf lib && rm -rf test",
"pretest": "tsc -p tests",
"test": "istanbul cover node_modules/.bin/_mocha -- -R landing out/*.test.js",
"coveralls": "cat coverage/lcov.info | node_modules/.bin/coveralls"
},
"repository": {
"type": "git",
Expand All @@ -37,6 +39,8 @@
"devDependencies": {
"chai": "^3.5.0",
"chrome-remote-debug-protocol": "^1.0.1",
"coveralls": "^2.11.11",
"mocha": "^2.5.3",
"ws": "^1.1.1"
}
}
File renamed without changes.
28 changes: 20 additions & 8 deletions src/noice-json-rpc.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
/// <reference path="../typings/index.d.ts" />

import {JsonRpc2} from './json-rpc2'
import {EventEmitter} from 'events'

export interface LikeSocket {
send(message: string): void
on(event: string, cb: Function): this;
removeListener(event: string, cb: Function): this;
on(event: string, cb: Function): any;
removeListener(event: string, cb: Function): any;

on(event: 'open', cb: (ws: LikeSocket) => void ): this
on(event: 'message', cb: (data: string) => void): this;
on(event: 'error', cb: (err: Error) => void ): this
on(event: 'open', cb: (ws: LikeSocket) => void ): any
on(event: 'message', cb: (data: string) => void): any;
on(event: 'error', cb: (err: Error) => void ): any
}

export interface LikeSocketServer {
on(event: string, cb: Function): this;
on(event: 'connection', cb: (ws: LikeSocket) => void ): this
on(event: 'error', cb: (err: Error) => void ): this
on(event: string, cb: Function): any;
on(event: 'connection', cb: (ws: LikeSocket) => void ): any
on(event: 'error', cb: (err: Error) => void ): any
clients?: LikeSocket[]
}

Expand Down Expand Up @@ -52,6 +54,10 @@ export class Client extends EventEmitter implements JsonRpc2.Client{
super()
this.setLogging(opts)

if (!socket) {
throw new TypeError("socket cannot be undefined or null")
}

this._connectedPromise = new Promise((resolve, reject) => {
this._socket = socket

Expand Down Expand Up @@ -192,6 +198,12 @@ export class Server extends EventEmitter implements JsonRpc2.Server {
constructor (server: LikeSocketServer, opts?:ServerOpts) {
super()
this.setLogging(opts)

if (!server) {
throw new TypeError("server cannot be undefined or null")
}


this._socketServer = server
server.on('error', (e) => this.emit('error', e))

Expand Down
20 changes: 20 additions & 0 deletions tests/client-server.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {LikeSocket, LikeSocketServer} from '../lib/noice-json-rpc'

export class MockSocket implements LikeSocket {
send(message: string) {
}

on(event: string, cb: Function) {
}

removeListener(event: string, cb: Function) {
}
}

export class MockSocketServer implements LikeSocketServer {
on(event: string, cb: Function) {
}

removeListener(event: string, cb: Function) {
}
}
22 changes: 22 additions & 0 deletions tests/client-server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {Client, Server} from '../lib/noice-json-rpc'
import {assert} from 'chai'

describe('Client', () => {
let client: Client

it('fails on null socket', ()=> {
assert.throws(() => {
client = new Client(null)
}, TypeError)
})
})

describe('Server', () => {
let server: Server

it('fails on null server', ()=> {
assert.throws(() => {
server = new Server(null)
}, TypeError)
})
})
6 changes: 3 additions & 3 deletions src/example.ts → tests/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as WebSocket from 'ws'
import WebSocketServer = WebSocket.Server
import * as http from 'http'
import Crdp from 'chrome-remote-debug-protocol'
import * as rpc from './noice-json-rpc'
import * as rpc from '../lib/noice-json-rpc'

async function setupClient() {
try {
Expand Down Expand Up @@ -60,5 +60,5 @@ function setupServer() {

}

setupServer()
setupClient()
//setupServer()
//setupClient()
2 changes: 1 addition & 1 deletion test/tsconfig.json → tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"removeComments": false,
"sourceMap": false,
"target": "ES6",
"outDir": "../out",
"declaration": false,
"outDir": "../out",
"moduleResolution": "node"
}
}

0 comments on commit 1bb4b4e

Please sign in to comment.