Merge pull request #3769 from dibdot/logread-fix
[project/luci.git] / modules / luci-base / htdocs / luci-static / resources / rpc.js
1 'use strict';
2 'require baseclass';
3 'require request';
4
5 var rpcRequestID = 1,
6 rpcSessionID = L.env.sessionid || '00000000000000000000000000000000',
7 rpcBaseURL = L.url('admin/ubus'),
8 rpcInterceptorFns = [];
9
10 /**
11 * @class rpc
12 * @memberof LuCI
13 * @hideconstructor
14 * @classdesc
15 *
16 * The `LuCI.rpc` class provides high level ubus JSON-RPC abstractions
17 * and means for listing and invoking remove RPC methods.
18 */
19 return baseclass.extend(/** @lends LuCI.rpc.prototype */ {
20 /* privates */
21 call: function(req, cb, nobatch) {
22 var q = '';
23
24 if (Array.isArray(req)) {
25 if (req.length == 0)
26 return Promise.resolve([]);
27
28 for (var i = 0; i < req.length; i++)
29 if (req[i].params)
30 q += '%s%s.%s'.format(
31 q ? ';' : '/',
32 req[i].params[1],
33 req[i].params[2]
34 );
35 }
36 else if (req.params) {
37 q += '/%s.%s'.format(req.params[1], req.params[2]);
38 }
39
40 return request.post(rpcBaseURL + q, req, {
41 timeout: (L.env.rpctimeout || 20) * 1000,
42 nobatch: nobatch,
43 credentials: true
44 }).then(cb, cb);
45 },
46
47 parseCallReply: function(req, res) {
48 var msg = null;
49
50 if (res instanceof Error)
51 return req.reject(res);
52
53 try {
54 if (!res.ok)
55 L.raise('RPCError', 'RPC call to %s/%s failed with HTTP error %d: %s',
56 req.object, req.method, res.status, res.statusText || '?');
57
58 msg = res.json();
59 }
60 catch (e) {
61 return req.reject(e);
62 }
63
64 /*
65 * The interceptor args are intentionally swapped.
66 * Response is passed as first arg to align with Request class interceptors
67 */
68 Promise.all(rpcInterceptorFns.map(function(fn) { return fn(msg, req) }))
69 .then(this.handleCallReply.bind(this, req, msg))
70 .catch(req.reject);
71 },
72
73 handleCallReply: function(req, msg) {
74 var type = Object.prototype.toString,
75 ret = null;
76
77 try {
78 /* verify message frame */
79 if (!L.isObject(msg) || msg.jsonrpc != '2.0')
80 L.raise('RPCError', 'RPC call to %s/%s returned invalid message frame',
81 req.object, req.method);
82
83 /* check error condition */
84 if (L.isObject(msg.error) && msg.error.code && msg.error.message)
85 L.raise('RPCError', 'RPC call to %s/%s failed with error %d: %s',
86 req.object, req.method, msg.error.code, msg.error.message || '?');
87 }
88 catch (e) {
89 return req.reject(e);
90 }
91
92 if (!req.object && !req.method) {
93 ret = msg.result;
94 }
95 else if (Array.isArray(msg.result)) {
96 ret = (msg.result.length > 1) ? msg.result[1] : msg.result[0];
97 }
98
99 if (req.expect) {
100 for (var key in req.expect) {
101 if (ret != null && key != '')
102 ret = ret[key];
103
104 if (ret == null || type.call(ret) != type.call(req.expect[key]))
105 ret = req.expect[key];
106
107 break;
108 }
109 }
110
111 /* apply filter */
112 if (typeof(req.filter) == 'function') {
113 req.priv[0] = ret;
114 req.priv[1] = req.params;
115 ret = req.filter.apply(this, req.priv);
116 }
117
118 req.resolve(ret);
119 },
120
121 /**
122 * Lists available remote ubus objects or the method signatures of
123 * specific objects.
124 *
125 * This function has two signatures and is sensitive to the number of
126 * arguments passed to it:
127 * - `list()` -
128 * Returns an array containing the names of all remote `ubus` objects
129 * - `list("objname", ...)`
130 * Returns method signatures for each given `ubus` object name.
131 *
132 * @param {...string} [objectNames]
133 * If any object names are given, this function will return the method
134 * signatures of each given object.
135 *
136 * @returns {Promise<Array<string>|Object<string, Object<string, Object<string, string>>>>}
137 * When invoked without arguments, this function will return a promise
138 * resolving to an array of `ubus` object names. When invoked with one or
139 * more arguments, a promise resolving to an object describing the method
140 * signatures of each requested `ubus` object name will be returned.
141 */
142 list: function() {
143 var msg = {
144 jsonrpc: '2.0',
145 id: rpcRequestID++,
146 method: 'list',
147 params: arguments.length ? this.varargs(arguments) : undefined
148 };
149
150 return new Promise(L.bind(function(resolveFn, rejectFn) {
151 /* store request info */
152 var req = {
153 resolve: resolveFn,
154 reject: rejectFn
155 };
156
157 /* call rpc */
158 this.call(msg, this.parseCallReply.bind(this, req));
159 }, this));
160 },
161
162 /**
163 * @typedef {Object} DeclareOptions
164 * @memberof LuCI.rpc
165 *
166 * @property {string} object
167 * The name of the remote `ubus` object to invoke.
168 *
169 * @property {string} method
170 * The name of the remote `ubus` method to invoke.
171 *
172 * @property {string[]} [params]
173 * Lists the named parameters expected by the remote `ubus` RPC method.
174 * The arguments passed to the resulting generated method call function
175 * will be mapped to named parameters in the order they appear in this
176 * array.
177 *
178 * Extraneous parameters passed to the generated function will not be
179 * sent to the remote procedure but are passed to the
180 * {@link LuCI.rpc~filterFn filter function} if one is specified.
181 *
182 * Examples:
183 * - `params: [ "foo", "bar" ]` -
184 * When the resulting call function is invoked with `fn(true, false)`,
185 * the corresponding args object sent to the remote procedure will be
186 * `{ foo: true, bar: false }`.
187 * - `params: [ "test" ], filter: function(reply, args, extra) { ... }` -
188 * When the resultung generated function is invoked with
189 * `fn("foo", "bar", "baz")` then `{ "test": "foo" }` will be sent as
190 * argument to the remote procedure and the filter function will be
191 * invoked with `filterFn(reply, [ "foo" ], "bar", "baz")`
192 *
193 * @property {Object<string,*>} [expect]
194 * Describes the expected return data structure. The given object is
195 * supposed to contain a single key selecting the value to use from
196 * the returned `ubus` reply object. The value of the sole key within
197 * the `expect` object is used to infer the expected type of the received
198 * `ubus` reply data.
199 *
200 * If the received data does not contain `expect`'s key, or if the
201 * type of the data differs from the type of the value in the expect
202 * object, the expect object's value is returned as default instead.
203 *
204 * The key in the `expect` object may be an empty string (`''`) in which
205 * case the entire reply object is selected instead of one of its subkeys.
206 *
207 * If the `expect` option is omitted, the received reply will be returned
208 * as-is, regardless of its format or type.
209 *
210 * Examples:
211 * - `expect: { '': { error: 'Invalid response' } }` -
212 * This requires the entire `ubus` reply to be a plain JavaScript
213 * object. If the reply isn't an object but e.g. an array or a numeric
214 * error code instead, it will get replaced with
215 * `{ error: 'Invalid response' }` instead.
216 * - `expect: { results: [] }` -
217 * This requires the received `ubus` reply to be an object containing
218 * a key `results` with an array as value. If the received reply does
219 * not contain such a key, or if `reply.results` points to a non-array
220 * value, the empty array (`[]`) will be used instead.
221 * - `expect: { success: false }` -
222 * This requires the received `ubus` reply to be an object containing
223 * a key `success` with a boolean value. If the reply does not contain
224 * `success` or if `reply.success` is not a boolean value, `false` will
225 * be returned as default instead.
226 *
227 * @property {LuCI.rpc~filterFn} [filter]
228 * Specfies an optional filter function which is invoked to transform the
229 * received reply data before it is returned to the caller.
230 *
231 */
232
233 /**
234 * The filter function is invoked to transform a received `ubus` RPC call
235 * reply before returning it to the caller.
236 *
237 * @callback LuCI.rpc~filterFn
238 *
239 * @param {*} data
240 * The received `ubus` reply data or a subset of it as described in the
241 * `expect` option of the RPC call declaration. In case of remote call
242 * errors, `data` is numeric `ubus` error code instead.
243 *
244 * @param {Array<*>} args
245 * The arguments the RPC method has been invoked with.
246 *
247 * @param {...*} extraArgs
248 * All extraneous arguments passed to the RPC method exceeding the number
249 * of arguments describes in the RPC call declaration.
250 *
251 * @return {*}
252 * The return value of the filter function will be returned to the caller
253 * of the RPC method as-is.
254 */
255
256 /**
257 * The generated invocation function is returned by
258 * {@link LuCI.rpc#declare rpc.declare()} and encapsulates a single
259 * RPC method call.
260 *
261 * Calling this function will execute a remote `ubus` HTTP call request
262 * using the arguments passed to it as arguments and return a promise
263 * resolving to the received reply values.
264 *
265 * @callback LuCI.rpc~invokeFn
266 *
267 * @param {...*} params
268 * The parameters to pass to the remote procedure call. The given
269 * positional arguments will be named to named RPC parameters according
270 * to the names specified in the `params` array of the method declaration.
271 *
272 * Any additional parameters exceeding the amount of arguments in the
273 * `params` declaration are passed as private extra arguments to the
274 * declared filter function.
275 *
276 * @return {Promise<*>}
277 * Returns a promise resolving to the result data of the remote `ubus`
278 * RPC method invocation, optionally substituted and filtered according
279 * to the `expect` and `filter` declarations.
280 */
281
282 /**
283 * Describes a remote RPC call procedure and returns a function
284 * implementing it.
285 *
286 * @param {LuCI.rpc.DeclareOptions} options
287 * If any object names are given, this function will return the method
288 * signatures of each given object.
289 *
290 * @returns {LuCI.rpc~invokeFn}
291 * Returns a new function implementing the method call described in
292 * `options`.
293 */
294 declare: function(options) {
295 return Function.prototype.bind.call(function(rpc, options) {
296 var args = this.varargs(arguments, 2);
297 return new Promise(function(resolveFn, rejectFn) {
298 /* build parameter object */
299 var p_off = 0;
300 var params = { };
301 if (Array.isArray(options.params))
302 for (p_off = 0; p_off < options.params.length; p_off++)
303 params[options.params[p_off]] = args[p_off];
304
305 /* all remaining arguments are private args */
306 var priv = [ undefined, undefined ];
307 for (; p_off < args.length; p_off++)
308 priv.push(args[p_off]);
309
310 /* store request info */
311 var req = {
312 expect: options.expect,
313 filter: options.filter,
314 resolve: resolveFn,
315 reject: rejectFn,
316 params: params,
317 priv: priv,
318 object: options.object,
319 method: options.method
320 };
321
322 /* build message object */
323 var msg = {
324 jsonrpc: '2.0',
325 id: rpcRequestID++,
326 method: 'call',
327 params: [
328 rpcSessionID,
329 options.object,
330 options.method,
331 params
332 ]
333 };
334
335 /* call rpc */
336 rpc.call(msg, rpc.parseCallReply.bind(rpc, req), options.nobatch);
337 });
338 }, this, this, options);
339 },
340
341 /**
342 * Returns the current RPC session id.
343 *
344 * @returns {string}
345 * Returns the 32 byte session ID string used for authenticating remote
346 * requests.
347 */
348 getSessionID: function() {
349 return rpcSessionID;
350 },
351
352 /**
353 * Set the RPC session id to use.
354 *
355 * @param {string} sid
356 * Sets the 32 byte session ID string used for authenticating remote
357 * requests.
358 */
359 setSessionID: function(sid) {
360 rpcSessionID = sid;
361 },
362
363 /**
364 * Returns the current RPC base URL.
365 *
366 * @returns {string}
367 * Returns the RPC URL endpoint to issue requests against.
368 */
369 getBaseURL: function() {
370 return rpcBaseURL;
371 },
372
373 /**
374 * Set the RPC base URL to use.
375 *
376 * @param {string} sid
377 * Sets the RPC URL endpoint to issue requests against.
378 */
379 setBaseURL: function(url) {
380 rpcBaseURL = url;
381 },
382
383 /**
384 * Translates a numeric `ubus` error code into a human readable
385 * description.
386 *
387 * @param {number} statusCode
388 * The numeric status code.
389 *
390 * @returns {string}
391 * Returns the textual description of the code.
392 */
393 getStatusText: function(statusCode) {
394 switch (statusCode) {
395 case 0: return _('Command OK');
396 case 1: return _('Invalid command');
397 case 2: return _('Invalid argument');
398 case 3: return _('Method not found');
399 case 4: return _('Resource not found');
400 case 5: return _('No data received');
401 case 6: return _('Permission denied');
402 case 7: return _('Request timeout');
403 case 8: return _('Not supported');
404 case 9: return _('Unspecified error');
405 case 10: return _('Connection lost');
406 default: return _('Unknown error code');
407 }
408 },
409
410 /**
411 * Registered interceptor functions are invoked before the standard reply
412 * parsing and handling logic.
413 *
414 * By returning rejected promises, interceptor functions can cause the
415 * invocation function to fail, regardless of the received reply.
416 *
417 * Interceptors may also modify their message argument in-place to
418 * rewrite received replies before they're processed by the standard
419 * response handling code.
420 *
421 * A common use case for such functions is to detect failing RPC replies
422 * due to expired authentication in order to trigger a new login.
423 *
424 * @callback LuCI.rpc~interceptorFn
425 *
426 * @param {*} msg
427 * The unprocessed, JSON decoded remote RPC method call reply.
428 *
429 * Since interceptors run before the standard parsing logic, the reply
430 * data is not verified for correctness or filtered according to
431 * `expect` and `filter` specifications in the declarations.
432 *
433 * @param {Object} req
434 * The related request object which is an extended variant of the
435 * declaration object, allowing access to internals of the invocation
436 * function such as `filter`, `expect` or `params` values.
437 *
438 * @return {Promise<*>|*}
439 * Interceptor functions may return a promise to defer response
440 * processing until some delayed work completed. Any values the returned
441 * promise resolves to are ignored.
442 *
443 * When the returned promise rejects with an error, the invocation
444 * function will fail too, forwarding the error to the caller.
445 */
446
447 /**
448 * Registers a new interceptor function.
449 *
450 * @param {LuCI.rpc~interceptorFn} interceptorFn
451 * The inteceptor function to register.
452 *
453 * @returns {LuCI.rpc~interceptorFn}
454 * Returns the given function value.
455 */
456 addInterceptor: function(interceptorFn) {
457 if (typeof(interceptorFn) == 'function')
458 rpcInterceptorFns.push(interceptorFn);
459 return interceptorFn;
460 },
461
462 /**
463 * Removes a registered interceptor function.
464 *
465 * @param {LuCI.rpc~interceptorFn} interceptorFn
466 * The inteceptor function to remove.
467 *
468 * @returns {boolean}
469 * Returns `true` if the given function has been removed or `false`
470 * if it has not been found.
471 */
472 removeInterceptor: function(interceptorFn) {
473 var oldlen = rpcInterceptorFns.length, i = oldlen;
474 while (i--)
475 if (rpcInterceptorFns[i] === interceptorFn)
476 rpcInterceptorFns.splice(i, 1);
477 return (rpcInterceptorFns.length < oldlen);
478 }
479 });