luci-base: rpc.js: make base URL configurable
[oweals/luci.git] / modules / luci-base / htdocs / luci-static / resources / rpc.js
1 'use strict';
2
3 var rpcRequestRegistry = {},
4     rpcRequestBatch = null,
5     rpcRequestID = 1,
6     rpcSessionID = L.env.sessionid || '00000000000000000000000000000000',
7     rpcBaseURL = L.url('admin/ubus');
8
9 return L.Class.extend({
10         call: function(req, cbFn) {
11                 var cb = cbFn.bind(this, req),
12                     q = '';
13
14                 if (Array.isArray(req)) {
15                         if (req.length == 0)
16                                 return Promise.resolve([]);
17
18                         for (var i = 0; i < req.length; i++)
19                                 q += '%s%s.%s'.format(
20                                         q ? ';' : '/',
21                                         req[i].params[1],
22                                         req[i].params[2]
23                                 );
24                 }
25                 else {
26                         q += '/%s.%s'.format(req.params[1], req.params[2]);
27                 }
28
29                 return L.Request.post(rpcBaseURL + q, req, {
30                         timeout: (L.env.rpctimeout || 5) * 1000,
31                         credentials: true
32                 }).then(cb);
33         },
34
35         handleListReply: function(req, msg) {
36                 var list = msg.result;
37
38                 /* verify message frame */
39                 if (typeof(msg) != 'object' || msg.jsonrpc != '2.0' || !msg.id || !Array.isArray(list))
40                         list = [ ];
41
42                 req.resolve(list);
43         },
44
45         handleCallReply: function(reqs, res) {
46                 var type = Object.prototype.toString,
47                     data = [],
48                     msg = null;
49
50                 if (!res.ok)
51                         L.error('RPCError', 'RPC call failed with HTTP error %d: %s',
52                                 res.status, res.statusText || '?');
53
54                 msg = res.json();
55
56                 if (!Array.isArray(reqs)) {
57                         msg = [ msg ];
58                         reqs = [ reqs ];
59                 }
60
61                 for (var i = 0; i < msg.length; i++) {
62                         /* fetch related request info */
63                         var req = rpcRequestRegistry[reqs[i].id];
64                         if (typeof(req) != 'object')
65                                 throw 'No related request for JSON response';
66
67                         /* fetch response attribute and verify returned type */
68                         var ret = undefined;
69
70                         /* verify message frame */
71                         if (typeof(msg[i]) == 'object' && msg[i].jsonrpc == '2.0') {
72                                 if (typeof(msg[i].error) == 'object' && msg[i].error.code && msg[i].error.message)
73                                         req.reject(new Error('RPC call failed with error %d: %s'
74                                                 .format(msg[i].error.code, msg[i].error.message || '?')));
75                                 else if (Array.isArray(msg[i].result) && msg[i].result[0] == 0)
76                                         ret = (msg[i].result.length > 1) ? msg[i].result[1] : msg[i].result[0];
77                         }
78                         else {
79                                 req.reject(new Error('Invalid message frame received'));
80                         }
81
82                         if (req.expect) {
83                                 for (var key in req.expect) {
84                                         if (ret != null && key != '')
85                                                 ret = ret[key];
86
87                                         if (ret == null || type.call(ret) != type.call(req.expect[key]))
88                                                 ret = req.expect[key];
89
90                                         break;
91                                 }
92                         }
93
94                         /* apply filter */
95                         if (typeof(req.filter) == 'function') {
96                                 req.priv[0] = ret;
97                                 req.priv[1] = req.params;
98                                 ret = req.filter.apply(this, req.priv);
99                         }
100
101                         req.resolve(ret);
102
103                         /* store response data */
104                         if (typeof(req.index) == 'number')
105                                 data[req.index] = ret;
106                         else
107                                 data = ret;
108
109                         /* delete request object */
110                         delete rpcRequestRegistry[reqs[i].id];
111                 }
112
113                 return Promise.resolve(data);
114         },
115
116         list: function() {
117                 var msg = {
118                         jsonrpc: '2.0',
119                         id:      rpcRequestID++,
120                         method:  'list',
121                         params:  arguments.length ? this.varargs(arguments) : undefined
122                 };
123
124                 return this.call(msg, this.handleListReply);
125         },
126
127         batch: function() {
128                 if (!Array.isArray(rpcRequestBatch))
129                         rpcRequestBatch = [ ];
130         },
131
132         flush: function() {
133                 if (!Array.isArray(rpcRequestBatch))
134                         return Promise.resolve([]);
135
136                 var req = rpcRequestBatch;
137                 rpcRequestBatch = null;
138
139                 /* call rpc */
140                 return this.call(req, this.handleCallReply);
141         },
142
143         declare: function(options) {
144                 return Function.prototype.bind.call(function(rpc, options) {
145                         var args = this.varargs(arguments, 2);
146                         return new Promise(function(resolveFn, rejectFn) {
147                                 /* build parameter object */
148                                 var p_off = 0;
149                                 var params = { };
150                                 if (Array.isArray(options.params))
151                                         for (p_off = 0; p_off < options.params.length; p_off++)
152                                                 params[options.params[p_off]] = args[p_off];
153
154                                 /* all remaining arguments are private args */
155                                 var priv = [ undefined, undefined ];
156                                 for (; p_off < args.length; p_off++)
157                                         priv.push(args[p_off]);
158
159                                 /* store request info */
160                                 var req = rpcRequestRegistry[rpcRequestID] = {
161                                         expect:  options.expect,
162                                         filter:  options.filter,
163                                         resolve: resolveFn,
164                                         reject:  rejectFn,
165                                         params:  params,
166                                         priv:    priv
167                                 };
168
169                                 /* build message object */
170                                 var msg = {
171                                         jsonrpc: '2.0',
172                                         id:      rpcRequestID++,
173                                         method:  'call',
174                                         params:  [
175                                                 rpcSessionID,
176                                                 options.object,
177                                                 options.method,
178                                                 params
179                                         ]
180                                 };
181
182                                 /* when a batch is in progress then store index in request data
183                                  * and push message object onto the stack */
184                                 if (Array.isArray(rpcRequestBatch))
185                                         req.index = rpcRequestBatch.push(msg) - 1;
186
187                                 /* call rpc */
188                                 else
189                                         rpc.call(msg, rpc.handleCallReply);
190                         });
191                 }, this, this, options);
192         },
193
194         getSessionID: function() {
195                 return rpcSessionID;
196         },
197
198         setSessionID: function(sid) {
199                 rpcSessionID = sid;
200         },
201
202         getBaseURL: function() {
203                 return rpcBaseURL;
204         },
205
206         setBaseURL: function(url) {
207                 rpcBaseURL = url;
208         }
209 });