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
+ } ) ( ) ;
0 commit comments