luci-base: rpc.js: fix list requests, handle aborted http requests
authorJo-Philipp Wich <jo@mein.io>
Thu, 12 Sep 2019 12:02:06 +0000 (14:02 +0200)
committerJo-Philipp Wich <jo@mein.io>
Thu, 12 Sep 2019 12:02:06 +0000 (14:02 +0200)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-base/htdocs/luci-static/resources/rpc.js

index 9a0f0164aeb32a8ecd5f3c90c0ab0ead43958070..87850b8564521c120b9c9e385c815901dfdcfbbd 100644 (file)
@@ -14,35 +14,29 @@ return L.Class.extend({
                                return Promise.resolve([]);
 
                        for (var i = 0; i < req.length; i++)
-                               q += '%s%s.%s'.format(
-                                       q ? ';' : '/',
-                                       req[i].params[1],
-                                       req[i].params[2]
-                               );
+                               if (req[i].params)
+                                       q += '%s%s.%s'.format(
+                                               q ? ';' : '/',
+                                               req[i].params[1],
+                                               req[i].params[2]
+                                       );
                }
-               else {
+               else if (req.params) {
                        q += '/%s.%s'.format(req.params[1], req.params[2]);
                }
 
                return L.Request.post(rpcBaseURL + q, req, {
                        timeout: (L.env.rpctimeout || 5) * 1000,
                        credentials: true
-               }).then(cb);
-       },
-
-       handleListReply: function(req, msg) {
-               var list = msg.result;
-
-               /* verify message frame */
-               if (typeof(msg) != 'object' || msg.jsonrpc != '2.0' || !msg.id || !Array.isArray(list))
-                       list = [ ];
-
-               req.resolve(list);
+               }).then(cb, cb);
        },
 
        parseCallReply: function(req, res) {
                var msg = null;
 
+               if (res instanceof Error)
+                       return req.reject(res);
+
                try {
                        if (!res.ok)
                                L.raise('RPCError', 'RPC call to %s/%s failed with HTTP error %d: %s',
@@ -82,7 +76,10 @@ return L.Class.extend({
                        return req.reject(e);
                }
 
-               if (Array.isArray(msg.result)) {
+               if (!req.object && !req.method) {
+                       ret = msg.result;
+               }
+               else if (Array.isArray(msg.result)) {
                        ret = (msg.result.length > 1) ? msg.result[1] : msg.result[0];
                }
 
@@ -116,7 +113,16 @@ return L.Class.extend({
                        params:  arguments.length ? this.varargs(arguments) : undefined
                };
 
-               return this.call(msg, this.handleListReply);
+               return new Promise(L.bind(function(resolveFn, rejectFn) {
+                       /* store request info */
+                       var req = {
+                               resolve: resolveFn,
+                               reject:  rejectFn
+                       };
+
+                       /* call rpc */
+                       this.call(msg, this.parseCallReply.bind(this, req));
+               }, this));
        },
 
        declare: function(options) {