Skip to content

Commit a26a8d3

Browse files
committed
add source
1 parent f1976ba commit a26a8d3

File tree

8 files changed

+292
-1
lines changed

8 files changed

+292
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ build/Release
2929
# Dependency directories
3030
node_modules
3131
jspm_packages
32+
bower_components/
3233

3334
# Optional npm cache directory
3435
.npm
3536

3637
# Optional REPL history
3738
.node_repl_history
39+
40+
#ide
41+
.idea*
42+
.idea/

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2016 jershell
3+
Copyright (c) 2016 QuickResto
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# simple-jsonrpc-js
2+
3+
4+
## Install
5+
6+
You can install this package either with `bower`.
7+
8+

bower.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "simple-jsonrpc-js",
3+
"version": "0.0.1",
4+
"license": "MIT",
5+
"main": "./simple-jsonrpc-js.js",
6+
"ignore": [],
7+
"dependencies": {
8+
"lodash": "4.13.1"
9+
}
10+
}

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('./simple-jsonrpc-js');
2+
module.exports = simple_jsonrpc;

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "simple-jsonrpc-js",
3+
"version": "0.0.1",
4+
"description": "simple-jsonrpc client/server",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/jershell/simple-jsonrpc-js.git"
12+
},
13+
"keywords": [
14+
"jsonrpc",
15+
"websocket",
16+
"client-side"
17+
],
18+
"author": "QuickResto Frontend Team <[email protected]>",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/jershell/simple-jsonrpc-js/issues"
22+
},
23+
"homepage": "http://future_home_page.org/dev/simple-jsonrpc-js",
24+
}

simple-jsonrpc-js.js

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
(function(undefined) {
2+
'use strict';
3+
var simple_jsonrpc = function(){
4+
console.log('init simple_jsonrpc instance');
5+
var ERRORS = {
6+
"INVALID_REQUEST": {
7+
"code": -32600,
8+
"message": "Invalid Request The JSON sent is not a valid Request object."
9+
},
10+
"PARSE_ERROR": {
11+
"code": -32700,
12+
"message": "Invalid JSON was received by the server. An error occurred on the server while parsing the JSON text."
13+
},
14+
"METHOD_NOT_FOUND": {
15+
"code": -32601,
16+
"message": "Method not found The method does not exist / is not available."
17+
},
18+
"INVALID_PARAMS": {
19+
"code": -32602,
20+
"message": "Invalid params Invalid method parameter(s)."
21+
},
22+
"INTERNAL_ERROR": {
23+
"code": -32603,
24+
"message": "Internal error Internal JSON-RPC error."
25+
}
26+
};
27+
28+
var self = this,
29+
waitingframe = {},
30+
id = 0,
31+
dispatcher = {};
32+
33+
function isPromise(thing){
34+
return !!thing && 'function' === typeof thing.then;
35+
}
36+
37+
function resolveFunction(frame) {
38+
39+
}
40+
41+
function resolveFrame (frame){
42+
43+
//resolve called function
44+
if(frame.id && frame.hasOwnProperty('result')){
45+
waitingframe[frame.id].resolve(frame.result);
46+
delete waitingframe[frame.id];
47+
}
48+
//remote call
49+
else if(frame.method && !frame.result){
50+
51+
if(dispatcher.hasOwnProperty(frame.method)){
52+
try {
53+
var result;
54+
55+
if (frame.hasOwnProperty('params')) {
56+
result = _.isArray(frame.params) ?
57+
dispatcher[frame.method].apply(dispatcher, frame.params) :
58+
(function () {
59+
throw new Error('position params is not implemented');
60+
// var argsName = $injector.annotate(dispatcher[frame.method]),
61+
// argsValues = [];
62+
//
63+
// argsName.forEach(function (arg) {
64+
//
65+
// if (frame.params.hasOwnProperty(arg)) {
66+
// argsValues.push(frame.params[arg]);
67+
// }
68+
// else {
69+
// argsValues.push(undefined);
70+
// }
71+
// });
72+
// console.debug('argsValues', argsValues);
73+
// return dispatcher[frame.method].apply(dispatcher, argsValues);
74+
}());
75+
}
76+
else {
77+
result = dispatcher[frame.method]();
78+
}
79+
80+
if (isPromise(result)) {
81+
result
82+
.then(function (res) {
83+
self.toStream(JSON.stringify({
84+
"jsonrpc": "2.0",
85+
"id": frame.id,
86+
"result": res
87+
}));
88+
})
89+
.catch(function (E) {
90+
console.error(E);
91+
var error = ERRORS.INTERNAL_ERROR;
92+
error.data = E.message;
93+
94+
self.toStream(JSON.stringify({
95+
"jsonrpc": "2.0",
96+
"id": frame.id,
97+
"error": error
98+
}));
99+
});
100+
}
101+
else {
102+
self.toStream(JSON.stringify({
103+
"jsonrpc": "2.0",
104+
"id": frame.id,
105+
"result": result
106+
}));
107+
}
108+
}
109+
catch(Error){
110+
console.error(Error);
111+
var error = ERRORS.INTERNAL_ERROR;
112+
error.data = Error.message;
113+
114+
self.toStream(JSON.stringify({
115+
"jsonrpc": "2.0",
116+
"id": frame.id,
117+
"error": error
118+
}));
119+
}
120+
}
121+
else {
122+
self.toStream({
123+
"jsonrpc": "2.0",
124+
"id": frame.id,
125+
"error": ERRORS.METHOD_NOT_FOUND
126+
});
127+
}
128+
}
129+
else if(frame.error && frame.id){
130+
waitingframe[frame.id].reject(frame.error);
131+
delete waitingframe[frame.id];
132+
}
133+
else if(frame.error && !frame.id){
134+
console.debug('unknown error', frame.error);
135+
}
136+
return waitingframe[frame.id];
137+
}
138+
139+
//{"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}
140+
//{"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}
141+
self.toStream = function(a){
142+
console.debug(arguments);
143+
};
144+
145+
self.dispatch = function(functionName, callback) {
146+
dispatcher[functionName] = callback;
147+
};
148+
149+
self.call = function(method, params){
150+
return new Promise(function(resolve, reject){
151+
id += 1;
152+
var message = {
153+
"jsonrpc": "2.0",
154+
"method": method,
155+
"id": id
156+
};
157+
158+
if(_.isObject(params) && !_.isEmpty(params)){
159+
message.params = params;
160+
}
161+
162+
waitingframe[id.toString()] = {
163+
resolve: resolve,
164+
reject: reject
165+
};
166+
167+
if(_.isFunction(self.toStream)){
168+
self.toStream(JSON.stringify(message));
169+
}
170+
});
171+
};
172+
173+
self.callNotification = function(){
174+
var message = {
175+
"jsonrpc": "2.0",
176+
"method": method,
177+
"params": params,
178+
};
179+
180+
if(_.isObject(params) && !_.isEmpty(params)){
181+
message.params = params;
182+
}
183+
184+
self.toStream(JSON.stringify(message));
185+
};
186+
187+
self.messageHandler = function(rawMessage){
188+
try {
189+
var message = JSON.parse(rawMessage);
190+
191+
if(_.isArray(message)){
192+
// var r;
193+
// frames.forEach(function(frame){
194+
// r = resolveFrame(frame);
195+
// if(isPromise(r)){
196+
//
197+
// }
198+
// });
199+
console.error('not implement');
200+
self.toStream(JSON.stringify({
201+
"jsonrpc": "2.0",
202+
"error": ERRORS.INTERNAL_ERROR
203+
}));
204+
throw "not implement"
205+
}
206+
else if(_.isObject(message)){
207+
resolveFrame(message);
208+
}
209+
}
210+
catch (e) {
211+
self.toStream(JSON.stringify({
212+
"jsonrpc": "2.0",
213+
"error": ERRORS.PARSE_ERROR
214+
}));
215+
}
216+
};
217+
};
218+
219+
var result = new simple_jsonrpc();
220+
221+
if (typeof define == 'function' && define.amd) {
222+
define('simple_jsonrpc', [], function() {
223+
return result;
224+
});
225+
}
226+
else if(typeof module !== undefined && typeof module.exports !== undefined ){
227+
module.exports = result;
228+
}
229+
else {
230+
return result;
231+
}
232+
})();

test/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
var assert = require("assert")
3+
var simple_jsonrpc = require('../simple-jsonrpc-js');
4+
5+
describe('#1', function(){
6+
it('should return -1 when the value is not present', function(){
7+
assert.equal(-1, [1,2,3].indexOf(5));
8+
assert.equal(-1, [1,2,3].indexOf(0));
9+
});
10+
});

0 commit comments

Comments
 (0)