[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [meek/webextension] Make roundtrip async.



commit 52ff3d08b09d96fca4c6ef247fa9c173b3b867a9
Author: David Fifield <david@xxxxxxxxxxxxxxx>
Date:   Fri Feb 15 12:08:01 2019 -0700

    Make roundtrip async.
    
    roundtrip doesn't need to know the ID; tack that on in the onMessage
    listener.
---
 webextension/background.js | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)

diff --git a/webextension/background.js b/webextension/background.js
index fd39273..8092352 100644
--- a/webextension/background.js
+++ b/webextension/background.js
@@ -79,7 +79,7 @@ function base64_encode(dec_buf) {
     return btoa(dec_str);
 }
 
-function roundtrip(id, request) {
+async function roundtrip(request) {
     // Process the incoming request spec and convert it into parameters to the
     // fetch API. Also enforce some restrictions on what kinds of requests we
     // are willing to make.
@@ -119,28 +119,26 @@ function roundtrip(id, request) {
         // TODO: strip Origin header?
         // TODO: proxy
     } catch (error) {
-        port.postMessage({id, response: {error: `request spec failed valiation: ${error.message}`}});
-        return;
+        return {error: `request spec failed valiation: ${error.message}`};
     }
 
-    // Now actually do the request and send the result back to the native
-    // process.
-    fetch(url, init)
-        .then(resp => resp.arrayBuffer().then(body => ({
-            status: resp.status,
-            body: base64_encode(body),
-        })))
+    // Now actually do the request and build a response object.
+    try {
+        let resp = await fetch(url, init);
+        let body = await resp.arrayBuffer();
+        return {status: resp.status, body: base64_encode(body)};
+    } catch (error) {
         // Convert any errors into an error response.
-        .catch(error => ({error: error.message}))
-        // Send the response (success or failure) back to the requester, tagged
-        // with its ID.
-        .then(response => port.postMessage({id, response}));
+        return {error: error.message};
+    }
 }
 
 port.onMessage.addListener((message) => {
     switch (message.command) {
         case "roundtrip":
-            roundtrip(message.id, message.request);
+            // Do a roundtrip and send the result back to the native process.
+            roundtrip(message.request)
+                .then(response => port.postMessage({id: message.id, response}));
             break;
         case "report-address":
             // Tell meek-client where our subprocess (the one that actually



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits