Просмотр исходного кода

use promesa instead of js-await

Tienson Qin 1 неделя назад
Родитель
Сommit
cdd380bdc7

+ 27 - 28
deps/common/src/logseq/common/authorization.cljs

@@ -1,6 +1,6 @@
 (ns logseq.common.authorization
-  (:require [clojure.string :as string])
-  (:require-macros [logseq.common.async :refer [js-await]]))
+  (:require [clojure.string :as string]
+            [promesa.core :as p]))
 
 (def text-decoder (js/TextDecoder.))
 (def text-encoder (js/TextEncoder.))
@@ -31,34 +31,33 @@
               #js ["verify"]))
 
 (defn verify-jwt [token env]
-  (prn :debug :token token)
   (let [parts (string/split token #"\.")
         _ (when (not= 3 (count parts)) (throw (ex-info "invalid" {})))
         header-part (nth parts 0)
         payload-part (nth parts 1)
         signature-part (nth parts 2)]
-    (js-await [header (decode-jwt-part header-part)
-               payload (decode-jwt-part payload-part)
-               issuer (aget env "COGNITO_ISSUER")
-               client-id (aget env "COGNITO_CLIENT_ID")
-               _ (when (not= (aget payload "iss") issuer) (throw (ex-info "iss not found" {})))
-               _ (when (not= (aget payload "aud") client-id) (throw (ex-info "aud not found" {})))
-               now (js/Math.floor (/ (.now js/Date) 1000))
-               _ (when (and (aget payload "exp") (< (aget payload "exp") now))
-                   (throw (ex-info "exp" {})))
-               jwks-resp (js/fetch (aget env "COGNITO_JWKS_URL"))
-               _ (when-not (.-ok jwks-resp) (throw (ex-info "jwks" {})))
-               jwks (.json jwks-resp)
-               keys (or (aget jwks "keys") #js [])
-               key (.find keys (fn [k] (= (aget k "kid") (aget header "kid"))))
-               _ (when-not key (throw (ex-info "kid" {})))
-               crypto-key (import-rsa-key key)
-               data (.encode text-encoder (str header-part "." payload-part))
-               signature (base64url->uint8array signature-part)
-               ok (.verify js/crypto.subtle
-                           "RSASSA-PKCS1-v1_5"
-                           crypto-key
-                           signature
-                           data)]
-              (when ok
-                payload))))
+    (p/let [header (decode-jwt-part header-part)
+            payload (decode-jwt-part payload-part)
+            issuer (aget env "COGNITO_ISSUER")
+            client-id (aget env "COGNITO_CLIENT_ID")
+            _ (when (not= (aget payload "iss") issuer) (throw (ex-info "iss not found" {})))
+            _ (when (not= (aget payload "aud") client-id) (throw (ex-info "aud not found" {})))
+            now (js/Math.floor (/ (.now js/Date) 1000))
+            _ (when (and (aget payload "exp") (< (aget payload "exp") now))
+                (throw (ex-info "exp" {})))
+            jwks-resp (js/fetch (aget env "COGNITO_JWKS_URL"))
+            _ (when-not (.-ok jwks-resp) (throw (ex-info "jwks" {})))
+            jwks (.json jwks-resp)
+            keys (or (aget jwks "keys") #js [])
+            key (.find keys (fn [k] (= (aget k "kid") (aget header "kid"))))
+            _ (when-not key (throw (ex-info "kid" {})))
+            crypto-key (import-rsa-key key)
+            data (.encode text-encoder (str header-part "." payload-part))
+            signature (base64url->uint8array signature-part)
+            ok (.verify js/crypto.subtle
+                        "RSASSA-PKCS1-v1_5"
+                        crypto-key
+                        signature
+                        data)]
+      (when ok
+        payload))))

+ 6 - 6
deps/worker-sync/src/logseq/worker_sync/common.cljs

@@ -1,11 +1,11 @@
 (ns logseq.worker-sync.common
   (:require [clojure.string :as string]
-            [logseq.db.sqlite.util :as sqlite-util])
-  (:require-macros [logseq.common.async :refer [js-await]]))
+            [logseq.db.sqlite.util :as sqlite-util]
+            [promesa.core :as p]))
 
 (def text-decoder (js/TextDecoder.))
 
-(defn- cors-headers []
+(defn cors-headers []
   #js {"Access-Control-Allow-Origin" "*"
        "Access-Control-Allow-Headers" "content-type,authorization,x-amz-meta-checksum,x-amz-meta-type"
        "Access-Control-Allow-Methods" "GET,POST,PUT,DELETE,OPTIONS,HEAD"})
@@ -57,9 +57,9 @@
   (.apply (.-exec sql) sql (to-array (cons sql-str args))))
 
 (defn read-json [request]
-  (js-await [body (.text request)]
-            (when (seq body)
-              (js/JSON.parse body))))
+  (p/let [body (.text request)]
+    (when (seq body)
+      (js/JSON.parse body))))
 
 (defn read-transit [value]
   (when (string? value)

+ 89 - 89
deps/worker-sync/src/logseq/worker_sync/worker.cljs

@@ -12,8 +12,8 @@
             [logseq.worker-sync.protocol :as protocol]
             [logseq.worker-sync.storage :as storage]
             [logseq.worker-sync.worker-core :as worker-core]
-            [shadow.cljs.modern :refer (defclass)])
-  (:require-macros [logseq.common.async :refer [js-await]]))
+            [promesa.core :as p]
+            [shadow.cljs.modern :refer (defclass)]))
 
 (glogi-console/install!)
 
@@ -81,10 +81,10 @@
       (if (= method "OPTIONS")
         (handle-assets request env)
         (if-let [{:keys [graph-id]} (parse-asset-path path)]
-          (js-await [access-resp (graph-access-response request env graph-id)]
-                    (if (.-ok access-resp)
-                      (handle-assets request env)
-                      access-resp))
+          (p/let [access-resp (graph-access-response request env graph-id)]
+            (if (.-ok access-resp)
+              (handle-assets request env)
+              access-resp))
           (common/bad-request "invalid asset path")))
 
       (= method "OPTIONS")
@@ -105,16 +105,16 @@
         (if (seq graph-id)
           (if (= method "OPTIONS")
             (common/options-response)
-            (js-await [access-resp (graph-access-response request env graph-id)]
-                      (if (.-ok access-resp)
-                        (let [^js namespace (.-LOGSEQ_SYNC_DO env)
-                              do-id (.idFromName namespace graph-id)
-                              stub (.get namespace do-id)]
-                          (if (common/upgrade-request? request)
-                            (.fetch stub request)
-                            (let [rewritten (js/Request. new-url request)]
-                              (.fetch stub rewritten))))
-                        access-resp)))
+            (p/let [access-resp (graph-access-response request env graph-id)]
+              (if (.-ok access-resp)
+                (let [^js namespace (.-LOGSEQ_SYNC_DO env)
+                      do-id (.idFromName namespace graph-id)
+                      stub (.get namespace do-id)]
+                  (if (common/upgrade-request? request)
+                    (.fetch stub request)
+                    (let [rewritten (js/Request. new-url request)]
+                      (.fetch stub rewritten))))
+                access-resp)))
           (common/bad-request "missing graph id")))
 
       :else
@@ -521,79 +521,79 @@
         (common/options-response)
         (do
           (index-init! sql)
-          (js-await [claims (auth-claims request env)]
-                    (cond
-                      (nil? claims)
-                      (common/unauthorized)
-
-                      (and (= method "GET") (= ["graphs"] parts))
-                      (let [owner-sub (aget claims "sub")]
-                        (if (string? owner-sub)
-                          (common/json-response {:graphs (index-list sql owner-sub)})
-                          (common/unauthorized)))
-
-                      (and (= method "POST") (= ["graphs"] parts))
-                      (.then (common/read-json request)
-                             (fn [result]
-                               (let [graph-id (str (random-uuid))
-                                     graph-name (aget result "graph_name")
-                                     owner-sub (aget claims "sub")
-                                     schema-version (aget result "schema_version")]
-                                 (cond
-                                   (not (string? owner-sub))
-                                   (common/unauthorized)
-
-                                   (not (string? graph-name))
-                                   (common/bad-request "missing graph_name")
-
-                                   :else
-                                   (do
-                                     (index-upsert! sql graph-id graph-name owner-sub schema-version)
-                                     (common/json-response {:graph_id graph-id}))))))
-
-                      (and (= method "GET")
-                           (= 3 (count parts))
-                           (= "graphs" (first parts))
-                           (= "access" (nth parts 2)))
-                      (let [graph-id (nth parts 1)
-                            owner-sub (aget claims "sub")]
-                        (cond
-                          (not (string? owner-sub))
-                          (common/unauthorized)
-
-                          (index-owns-graph? sql graph-id owner-sub)
-                          (common/json-response {:ok true})
-
-                          :else
-                          (common/forbidden)))
-
-                      (and (= method "DELETE")
-                           (= 2 (count parts))
-                           (= "graphs" (first parts)))
-                      (let [graph-id (nth parts 1)
-                            owner-sub (aget claims "sub")]
-                        (cond
-                          (not (seq graph-id))
-                          (common/bad-request "missing graph id")
-
-                          (not (string? owner-sub))
-                          (common/unauthorized)
-
-                          (not (index-owns-graph? sql graph-id owner-sub))
-                          (common/forbidden)
-
-                          :else
-                          (do
-                            (index-delete! sql graph-id)
-                            (let [^js namespace (.-LOGSEQ_SYNC_DO (.-env self))
-                                  do-id (.idFromName namespace graph-id)
-                                  stub (.get namespace do-id)
-                                  reset-url (str (.-origin url) "/admin/reset")]
-                              (.fetch stub (js/Request. reset-url #js {:method "DELETE"})))
-                            (common/json-response {:graph_id graph-id :deleted true}))))
-
-                      :else
-                      (common/not-found)))))
+          (p/let [claims (auth-claims request env)]
+            (cond
+              (nil? claims)
+              (common/unauthorized)
+
+              (and (= method "GET") (= ["graphs"] parts))
+              (let [owner-sub (aget claims "sub")]
+                (if (string? owner-sub)
+                  (common/json-response {:graphs (index-list sql owner-sub)})
+                  (common/unauthorized)))
+
+              (and (= method "POST") (= ["graphs"] parts))
+              (.then (common/read-json request)
+                     (fn [result]
+                       (let [graph-id (str (random-uuid))
+                             graph-name (aget result "graph_name")
+                             owner-sub (aget claims "sub")
+                             schema-version (aget result "schema_version")]
+                         (cond
+                           (not (string? owner-sub))
+                           (common/unauthorized)
+
+                           (not (string? graph-name))
+                           (common/bad-request "missing graph_name")
+
+                           :else
+                           (do
+                             (index-upsert! sql graph-id graph-name owner-sub schema-version)
+                             (common/json-response {:graph_id graph-id}))))))
+
+              (and (= method "GET")
+                   (= 3 (count parts))
+                   (= "graphs" (first parts))
+                   (= "access" (nth parts 2)))
+              (let [graph-id (nth parts 1)
+                    owner-sub (aget claims "sub")]
+                (cond
+                  (not (string? owner-sub))
+                  (common/unauthorized)
+
+                  (index-owns-graph? sql graph-id owner-sub)
+                  (common/json-response {:ok true})
+
+                  :else
+                  (common/forbidden)))
+
+              (and (= method "DELETE")
+                   (= 2 (count parts))
+                   (= "graphs" (first parts)))
+              (let [graph-id (nth parts 1)
+                    owner-sub (aget claims "sub")]
+                (cond
+                  (not (seq graph-id))
+                  (common/bad-request "missing graph id")
+
+                  (not (string? owner-sub))
+                  (common/unauthorized)
+
+                  (not (index-owns-graph? sql graph-id owner-sub))
+                  (common/forbidden)
+
+                  :else
+                  (do
+                    (index-delete! sql graph-id)
+                    (let [^js namespace (.-LOGSEQ_SYNC_DO (.-env self))
+                          do-id (.idFromName namespace graph-id)
+                          stub (.get namespace do-id)
+                          reset-url (str (.-origin url) "/admin/reset")]
+                      (.fetch stub (js/Request. reset-url #js {:method "DELETE"})))
+                    (common/json-response {:graph_id graph-id :deleted true}))))
+
+              :else
+              (common/not-found)))))
       (catch :default error
         (log/error :worker-sync/index-error error)
         (common/json-response {:error "server error"} 500)))))