6 rpcSessionID
= L
.env
.sessionid
|| '00000000000000000000000000000000',
7 rpcBaseURL
= L
.url('admin/ubus'),
8 rpcInterceptorFns
= [];
16 * The `LuCI.rpc` class provides high level ubus JSON-RPC abstractions
17 * and means for listing and invoking remove RPC methods.
19 return baseclass
.extend(/** @lends LuCI.rpc.prototype */ {
21 call: function(req
, cb
, nobatch
) {
24 if (Array
.isArray(req
)) {
26 return Promise
.resolve([]);
28 for (var i
= 0; i
< req
.length
; i
++)
30 q
+= '%s%s.%s'.format(
36 else if (req
.params
) {
37 q
+= '/%s.%s'.format(req
.params
[1], req
.params
[2]);
40 return request
.post(rpcBaseURL
+ q
, req
, {
41 timeout
: (L
.env
.rpctimeout
|| 20) * 1000,
47 parseCallReply: function(req
, res
) {
50 if (res
instanceof Error
)
51 return req
.reject(res
);
55 L
.raise('RPCError', 'RPC call to %s/%s failed with HTTP error %d: %s',
56 req
.object
, req
.method
, res
.status
, res
.statusText
|| '?');
65 * The interceptor args are intentionally swapped.
66 * Response is passed as first arg to align with Request class interceptors
68 Promise
.all(rpcInterceptorFns
.map(function(fn
) { return fn(msg
, req
) }))
69 .then(this.handleCallReply
.bind(this, req
, msg
))
73 handleCallReply: function(req
, msg
) {
74 var type
= Object
.prototype.toString
,
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
);
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
|| '?');
92 if (!req
.object
&& !req
.method
) {
95 else if (Array
.isArray(msg
.result
)) {
96 if (req
.raise
&& msg
.result
[0] !== 0)
97 L
.raise('RPCError', 'RPC call to %s/%s failed with ubus code %d: %s',
98 req
.object
, req
.method
, msg
.result
[0], this.getStatusText(msg
.result
[0]));
100 ret
= (msg
.result
.length
> 1) ? msg
.result
[1] : msg
.result
[0];
104 for (var key
in req
.expect
) {
105 if (ret
!= null && key
!= '')
108 if (ret
== null || type
.call(ret
) != type
.call(req
.expect
[key
]))
109 ret
= req
.expect
[key
];
116 if (typeof(req
.filter
) == 'function') {
118 req
.priv
[1] = req
.params
;
119 ret
= req
.filter
.apply(this, req
.priv
);
126 * Lists available remote ubus objects or the method signatures of
129 * This function has two signatures and is sensitive to the number of
130 * arguments passed to it:
132 * Returns an array containing the names of all remote `ubus` objects
133 * - `list("objname", ...)`
134 * Returns method signatures for each given `ubus` object name.
136 * @param {...string} [objectNames]
137 * If any object names are given, this function will return the method
138 * signatures of each given object.
140 * @returns {Promise<Array<string>|Object<string, Object<string, Object<string, string>>>>}
141 * When invoked without arguments, this function will return a promise
142 * resolving to an array of `ubus` object names. When invoked with one or
143 * more arguments, a promise resolving to an object describing the method
144 * signatures of each requested `ubus` object name will be returned.
151 params
: arguments
.length
? this.varargs(arguments
) : undefined
154 return new Promise(L
.bind(function(resolveFn
, rejectFn
) {
155 /* store request info */
162 this.call(msg
, this.parseCallReply
.bind(this, req
));
167 * @typedef {Object} DeclareOptions
170 * @property {string} object
171 * The name of the remote `ubus` object to invoke.
173 * @property {string} method
174 * The name of the remote `ubus` method to invoke.
176 * @property {string[]} [params]
177 * Lists the named parameters expected by the remote `ubus` RPC method.
178 * The arguments passed to the resulting generated method call function
179 * will be mapped to named parameters in the order they appear in this
182 * Extraneous parameters passed to the generated function will not be
183 * sent to the remote procedure but are passed to the
184 * {@link LuCI.rpc~filterFn filter function} if one is specified.
187 * - `params: [ "foo", "bar" ]` -
188 * When the resulting call function is invoked with `fn(true, false)`,
189 * the corresponding args object sent to the remote procedure will be
190 * `{ foo: true, bar: false }`.
191 * - `params: [ "test" ], filter: function(reply, args, extra) { ... }` -
192 * When the resultung generated function is invoked with
193 * `fn("foo", "bar", "baz")` then `{ "test": "foo" }` will be sent as
194 * argument to the remote procedure and the filter function will be
195 * invoked with `filterFn(reply, [ "foo" ], "bar", "baz")`
197 * @property {Object<string,*>} [expect]
198 * Describes the expected return data structure. The given object is
199 * supposed to contain a single key selecting the value to use from
200 * the returned `ubus` reply object. The value of the sole key within
201 * the `expect` object is used to infer the expected type of the received
204 * If the received data does not contain `expect`'s key, or if the
205 * type of the data differs from the type of the value in the expect
206 * object, the expect object's value is returned as default instead.
208 * The key in the `expect` object may be an empty string (`''`) in which
209 * case the entire reply object is selected instead of one of its subkeys.
211 * If the `expect` option is omitted, the received reply will be returned
212 * as-is, regardless of its format or type.
215 * - `expect: { '': { error: 'Invalid response' } }` -
216 * This requires the entire `ubus` reply to be a plain JavaScript
217 * object. If the reply isn't an object but e.g. an array or a numeric
218 * error code instead, it will get replaced with
219 * `{ error: 'Invalid response' }` instead.
220 * - `expect: { results: [] }` -
221 * This requires the received `ubus` reply to be an object containing
222 * a key `results` with an array as value. If the received reply does
223 * not contain such a key, or if `reply.results` points to a non-array
224 * value, the empty array (`[]`) will be used instead.
225 * - `expect: { success: false }` -
226 * This requires the received `ubus` reply to be an object containing
227 * a key `success` with a boolean value. If the reply does not contain
228 * `success` or if `reply.success` is not a boolean value, `false` will
229 * be returned as default instead.
231 * @property {LuCI.rpc~filterFn} [filter]
232 * Specfies an optional filter function which is invoked to transform the
233 * received reply data before it is returned to the caller.
235 * @property {boolean} [reject=false]
236 * If set to `true`, non-zero ubus call status codes are treated as fatal
237 * error and lead to the rejection of the call promise. The default
238 * behaviour is to resolve with the call return code value instead.
242 * The filter function is invoked to transform a received `ubus` RPC call
243 * reply before returning it to the caller.
245 * @callback LuCI.rpc~filterFn
248 * The received `ubus` reply data or a subset of it as described in the
249 * `expect` option of the RPC call declaration. In case of remote call
250 * errors, `data` is numeric `ubus` error code instead.
252 * @param {Array<*>} args
253 * The arguments the RPC method has been invoked with.
255 * @param {...*} extraArgs
256 * All extraneous arguments passed to the RPC method exceeding the number
257 * of arguments describes in the RPC call declaration.
260 * The return value of the filter function will be returned to the caller
261 * of the RPC method as-is.
265 * The generated invocation function is returned by
266 * {@link LuCI.rpc#declare rpc.declare()} and encapsulates a single
269 * Calling this function will execute a remote `ubus` HTTP call request
270 * using the arguments passed to it as arguments and return a promise
271 * resolving to the received reply values.
273 * @callback LuCI.rpc~invokeFn
275 * @param {...*} params
276 * The parameters to pass to the remote procedure call. The given
277 * positional arguments will be named to named RPC parameters according
278 * to the names specified in the `params` array of the method declaration.
280 * Any additional parameters exceeding the amount of arguments in the
281 * `params` declaration are passed as private extra arguments to the
282 * declared filter function.
284 * @return {Promise<*>}
285 * Returns a promise resolving to the result data of the remote `ubus`
286 * RPC method invocation, optionally substituted and filtered according
287 * to the `expect` and `filter` declarations.
291 * Describes a remote RPC call procedure and returns a function
294 * @param {LuCI.rpc.DeclareOptions} options
295 * If any object names are given, this function will return the method
296 * signatures of each given object.
298 * @returns {LuCI.rpc~invokeFn}
299 * Returns a new function implementing the method call described in
302 declare: function(options
) {
303 return Function
.prototype.bind
.call(function(rpc
, options
) {
304 var args
= this.varargs(arguments
, 2);
305 return new Promise(function(resolveFn
, rejectFn
) {
306 /* build parameter object */
309 if (Array
.isArray(options
.params
))
310 for (p_off
= 0; p_off
< options
.params
.length
; p_off
++)
311 params
[options
.params
[p_off
]] = args
[p_off
];
313 /* all remaining arguments are private args */
314 var priv
= [ undefined, undefined ];
315 for (; p_off
< args
.length
; p_off
++)
316 priv
.push(args
[p_off
]);
318 /* store request info */
320 expect
: options
.expect
,
321 filter
: options
.filter
,
326 object
: options
.object
,
327 method
: options
.method
,
328 raise
: options
.reject
331 /* build message object */
345 rpc
.call(msg
, rpc
.parseCallReply
.bind(rpc
, req
), options
.nobatch
);
347 }, this, this, options
);
351 * Returns the current RPC session id.
354 * Returns the 32 byte session ID string used for authenticating remote
357 getSessionID: function() {
362 * Set the RPC session id to use.
364 * @param {string} sid
365 * Sets the 32 byte session ID string used for authenticating remote
368 setSessionID: function(sid
) {
373 * Returns the current RPC base URL.
376 * Returns the RPC URL endpoint to issue requests against.
378 getBaseURL: function() {
383 * Set the RPC base URL to use.
385 * @param {string} sid
386 * Sets the RPC URL endpoint to issue requests against.
388 setBaseURL: function(url
) {
393 * Translates a numeric `ubus` error code into a human readable
396 * @param {number} statusCode
397 * The numeric status code.
400 * Returns the textual description of the code.
402 getStatusText: function(statusCode
) {
403 switch (statusCode
) {
404 case 0: return _('Command OK');
405 case 1: return _('Invalid command');
406 case 2: return _('Invalid argument');
407 case 3: return _('Method not found');
408 case 4: return _('Resource not found');
409 case 5: return _('No data received');
410 case 6: return _('Permission denied');
411 case 7: return _('Request timeout');
412 case 8: return _('Not supported');
413 case 9: return _('Unspecified error');
414 case 10: return _('Connection lost');
415 default: return _('Unknown error code');
420 * Registered interceptor functions are invoked before the standard reply
421 * parsing and handling logic.
423 * By returning rejected promises, interceptor functions can cause the
424 * invocation function to fail, regardless of the received reply.
426 * Interceptors may also modify their message argument in-place to
427 * rewrite received replies before they're processed by the standard
428 * response handling code.
430 * A common use case for such functions is to detect failing RPC replies
431 * due to expired authentication in order to trigger a new login.
433 * @callback LuCI.rpc~interceptorFn
436 * The unprocessed, JSON decoded remote RPC method call reply.
438 * Since interceptors run before the standard parsing logic, the reply
439 * data is not verified for correctness or filtered according to
440 * `expect` and `filter` specifications in the declarations.
442 * @param {Object} req
443 * The related request object which is an extended variant of the
444 * declaration object, allowing access to internals of the invocation
445 * function such as `filter`, `expect` or `params` values.
447 * @return {Promise<*>|*}
448 * Interceptor functions may return a promise to defer response
449 * processing until some delayed work completed. Any values the returned
450 * promise resolves to are ignored.
452 * When the returned promise rejects with an error, the invocation
453 * function will fail too, forwarding the error to the caller.
457 * Registers a new interceptor function.
459 * @param {LuCI.rpc~interceptorFn} interceptorFn
460 * The inteceptor function to register.
462 * @returns {LuCI.rpc~interceptorFn}
463 * Returns the given function value.
465 addInterceptor: function(interceptorFn
) {
466 if (typeof(interceptorFn
) == 'function')
467 rpcInterceptorFns
.push(interceptorFn
);
468 return interceptorFn
;
472 * Removes a registered interceptor function.
474 * @param {LuCI.rpc~interceptorFn} interceptorFn
475 * The inteceptor function to remove.
478 * Returns `true` if the given function has been removed or `false`
479 * if it has not been found.
481 removeInterceptor: function(interceptorFn
) {
482 var oldlen
= rpcInterceptorFns
.length
, i
= oldlen
;
484 if (rpcInterceptorFns
[i
] === interceptorFn
)
485 rpcInterceptorFns
.splice(i
, 1);
486 return (rpcInterceptorFns
.length
< oldlen
);