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 ret
= (msg
.result
.length
> 1) ? msg
.result
[1] : msg
.result
[0];
100 for (var key
in req
.expect
) {
101 if (ret
!= null && key
!= '')
104 if (ret
== null || type
.call(ret
) != type
.call(req
.expect
[key
]))
105 ret
= req
.expect
[key
];
112 if (typeof(req
.filter
) == 'function') {
114 req
.priv
[1] = req
.params
;
115 ret
= req
.filter
.apply(this, req
.priv
);
122 * Lists available remote ubus objects or the method signatures of
125 * This function has two signatures and is sensitive to the number of
126 * arguments passed to it:
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.
132 * @param {...string} [objectNames]
133 * If any object names are given, this function will return the method
134 * signatures of each given object.
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.
147 params
: arguments
.length
? this.varargs(arguments
) : undefined
150 return new Promise(L
.bind(function(resolveFn
, rejectFn
) {
151 /* store request info */
158 this.call(msg
, this.parseCallReply
.bind(this, req
));
163 * @typedef {Object} DeclareOptions
166 * @property {string} object
167 * The name of the remote `ubus` object to invoke.
169 * @property {string} method
170 * The name of the remote `ubus` method to invoke.
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
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.
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")`
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
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.
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.
207 * If the `expect` option is omitted, the received reply will be returned
208 * as-is, regardless of its format or type.
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.
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.
234 * The filter function is invoked to transform a received `ubus` RPC call
235 * reply before returning it to the caller.
237 * @callback LuCI.rpc~filterFn
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.
244 * @param {Array<*>} args
245 * The arguments the RPC method has been invoked with.
247 * @param {...*} extraArgs
248 * All extraneous arguments passed to the RPC method exceeding the number
249 * of arguments describes in the RPC call declaration.
252 * The return value of the filter function will be returned to the caller
253 * of the RPC method as-is.
257 * The generated invocation function is returned by
258 * {@link LuCI.rpc#declare rpc.declare()} and encapsulates a single
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.
265 * @callback LuCI.rpc~invokeFn
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.
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.
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.
283 * Describes a remote RPC call procedure and returns a function
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.
290 * @returns {LuCI.rpc~invokeFn}
291 * Returns a new function implementing the method call described in
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 */
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
];
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
]);
310 /* store request info */
312 expect
: options
.expect
,
313 filter
: options
.filter
,
318 object
: options
.object
,
319 method
: options
.method
322 /* build message object */
336 rpc
.call(msg
, rpc
.parseCallReply
.bind(rpc
, req
), options
.nobatch
);
338 }, this, this, options
);
342 * Returns the current RPC session id.
345 * Returns the 32 byte session ID string used for authenticating remote
348 getSessionID: function() {
353 * Set the RPC session id to use.
355 * @param {string} sid
356 * Sets the 32 byte session ID string used for authenticating remote
359 setSessionID: function(sid
) {
364 * Returns the current RPC base URL.
367 * Returns the RPC URL endpoint to issue requests against.
369 getBaseURL: function() {
374 * Set the RPC base URL to use.
376 * @param {string} sid
377 * Sets the RPC URL endpoint to issue requests against.
379 setBaseURL: function(url
) {
384 * Translates a numeric `ubus` error code into a human readable
387 * @param {number} statusCode
388 * The numeric status code.
391 * Returns the textual description of the code.
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');
411 * Registered interceptor functions are invoked before the standard reply
412 * parsing and handling logic.
414 * By returning rejected promises, interceptor functions can cause the
415 * invocation function to fail, regardless of the received reply.
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.
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.
424 * @callback LuCI.rpc~interceptorFn
427 * The unprocessed, JSON decoded remote RPC method call reply.
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.
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.
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.
443 * When the returned promise rejects with an error, the invocation
444 * function will fail too, forwarding the error to the caller.
448 * Registers a new interceptor function.
450 * @param {LuCI.rpc~interceptorFn} interceptorFn
451 * The inteceptor function to register.
453 * @returns {LuCI.rpc~interceptorFn}
454 * Returns the given function value.
456 addInterceptor: function(interceptorFn
) {
457 if (typeof(interceptorFn
) == 'function')
458 rpcInterceptorFns
.push(interceptorFn
);
459 return interceptorFn
;
463 * Removes a registered interceptor function.
465 * @param {LuCI.rpc~interceptorFn} interceptorFn
466 * The inteceptor function to remove.
469 * Returns `true` if the given function has been removed or `false`
470 * if it has not been found.
472 removeInterceptor: function(interceptorFn
) {
473 var oldlen
= rpcInterceptorFns
.length
, i
= oldlen
;
475 if (rpcInterceptorFns
[i
] === interceptorFn
)
476 rpcInterceptorFns
.splice(i
, 1);
477 return (rpcInterceptorFns
.length
< oldlen
);