Browse Source

fix: lint

Tienson Qin 1 month ago
parent
commit
94c6f698e4

+ 0 - 11
deps/common/src/logseq/common/async.clj

@@ -1,11 +0,0 @@
-(ns ^:no-doc logseq.publish.async
-  (:require [shadow.cljs.modern]))
-
-(defmacro js-await
-  "Like `let` but for async values, executed sequentially.
-  Non-async values are wrapped in `js/Promise.resolve`."
-  [[a b & bindings] & body]
-  (let [b `(~'js/Promise.resolve ~b)]
-    (if (seq bindings)
-      `(shadow.cljs.modern/js-await ~[a b] (js-await ~bindings ~@body))
-      `(shadow.cljs.modern/js-await ~[a b] ~@body))))

+ 36 - 36
deps/publish/src/logseq/publish/assets.cljs

@@ -2,8 +2,8 @@
   "Handles publishing assets"
   (:require [clojure.string :as string]
             [logseq.common.authorization :as authorization]
-            [logseq.publish.common :as publish-common])
-  (:require-macros [logseq.common.async :refer [js-await]]))
+            [logseq.publish.common :as publish-common]
+            [promesa.core :as p]))
 
 (defn asset-content-type [ext]
   (case (string/lower-case (or ext ""))
@@ -32,37 +32,37 @@
           nil)))))
 
 (defn handle-post-asset [request env]
-  (js-await [auth-header (.get (.-headers request) "authorization")
-             token (when (and auth-header (string/starts-with? auth-header "Bearer "))
-                     (subs auth-header 7))
-             claims (cond
-                      (nil? token) nil
-                      :else (authorization/verify-jwt token env))]
-            (if (nil? claims)
-              (publish-common/unauthorized)
-              (let [meta (parse-asset-meta-header request)
-                    graph-uuid (get meta :graph)
-                    asset-uuid (get meta :asset_uuid)
-                    asset-type (get meta :asset_type)
-                    checksum (get meta :checksum)]
-                (if (or (nil? meta) (string/blank? graph-uuid) (string/blank? asset-uuid) (string/blank? asset-type))
-                  (publish-common/bad-request "missing asset metadata")
-                  (js-await [body (.arrayBuffer request)
-                             r2 (aget env "PUBLISH_R2")
-                             r2-key (str "publish/assets/" graph-uuid "/" asset-uuid "." asset-type)
-                             ^js existing (.head r2 r2-key)
-                             existing-checksum (when existing
-                                                 (when-let [meta (.-customMetadata existing)]
-                                                   (aget meta "checksum")))
-                             content-type (or (get meta :content_type)
-                                              (asset-content-type asset-type))
-                             put? (not (and existing-checksum checksum (= existing-checksum checksum)))
-                             _ (when put?
-                                 (.put r2 r2-key body
-                                       #js {:httpMetadata #js {:contentType content-type}
-                                            :customMetadata #js {:checksum (or checksum "")
-                                                                 :owner_sub (aget claims "sub")}}))]
-                            (publish-common/json-response {:asset_uuid asset-uuid
-                                                           :graph_uuid graph-uuid
-                                                           :asset_type asset-type
-                                                           :asset_url (str "/asset/" graph-uuid "/" asset-uuid "." asset-type)})))))))
+  (p/let [auth-header (.get (.-headers request) "authorization")
+          token (when (and auth-header (string/starts-with? auth-header "Bearer "))
+                  (subs auth-header 7))
+          claims (cond
+                   (nil? token) nil
+                   :else (authorization/verify-jwt token env))]
+    (if (nil? claims)
+      (publish-common/unauthorized)
+      (let [meta (parse-asset-meta-header request)
+            graph-uuid (get meta :graph)
+            asset-uuid (get meta :asset_uuid)
+            asset-type (get meta :asset_type)
+            checksum (get meta :checksum)]
+        (if (or (nil? meta) (string/blank? graph-uuid) (string/blank? asset-uuid) (string/blank? asset-type))
+          (publish-common/bad-request "missing asset metadata")
+          (p/let [body (.arrayBuffer request)
+                  r2 (aget env "PUBLISH_R2")
+                  r2-key (str "publish/assets/" graph-uuid "/" asset-uuid "." asset-type)
+                  ^js existing (.head r2 r2-key)
+                  existing-checksum (when existing
+                                      (when-let [meta (.-customMetadata existing)]
+                                        (aget meta "checksum")))
+                  content-type (or (get meta :content_type)
+                                   (asset-content-type asset-type))
+                  put? (not (and existing-checksum checksum (= existing-checksum checksum)))
+                  _ (when put?
+                      (.put r2 r2-key body
+                            #js {:httpMetadata #js {:contentType content-type}
+                                 :customMetadata #js {:checksum (or checksum "")
+                                                      :owner_sub (aget claims "sub")}}))]
+            (publish-common/json-response {:asset_uuid asset-uuid
+                                           :graph_uuid graph-uuid
+                                           :asset_type asset-type
+                                           :asset_url (str "/asset/" graph-uuid "/" asset-uuid "." asset-type)})))))))

+ 120 - 120
deps/publish/src/logseq/publish/common.cljs

@@ -3,8 +3,8 @@
   (:require [clojure.string :as string]
             [cognitect.transit :as transit]
             [datascript.transit :as dt]
-            [logseq.db :as ldb])
-  (:require-macros [logseq.common.async :refer [js-await]]))
+            [logseq.db :as ldb]
+            [promesa.core :as p]))
 
 (def text-decoder (js/TextDecoder.))
 (def text-encoder (js/TextEncoder.))
@@ -117,9 +117,9 @@
        (apply str)))
 
 (defn sha256-hex [message]
-  (js-await [data (.encode text-encoder message)
-             digest (.digest js/crypto.subtle "SHA-256" data)]
-            (to-hex digest)))
+  (p/let [data (.encode text-encoder message)
+          digest (.digest js/crypto.subtle "SHA-256" data)]
+    (to-hex digest)))
 
 (def password-kdf-max-iterations 90000)
 (def password-kdf-iterations 90000)
@@ -133,31 +133,31 @@
         (string/replace #"=+$" ""))))
 
 (defn hash-password [password]
-  (js-await [salt (doto (js/Uint8Array. 16)
-                    (js/crypto.getRandomValues))
-             crypto-key (.importKey js/crypto.subtle
-                                    "raw"
-                                    (.encode text-encoder password)
-                                    #js {:name "PBKDF2"}
-                                    false
-                                    #js ["deriveBits"])
-             iterations (min password-kdf-iterations password-kdf-max-iterations)
-             derived (.deriveBits js/crypto.subtle
-                                  #js {:name "PBKDF2"
-                                       :hash "SHA-256"
-                                       :salt salt
-                                       :iterations iterations}
-                                  crypto-key
-                                  256)
-             derived-bytes (js/Uint8Array. derived)
-             salt-encoded (bytes->base64url salt)
-             hash-encoded (bytes->base64url derived-bytes)]
-            (str "pbkdf2$sha256$"
-                 iterations
-                 "$"
-                 salt-encoded
-                 "$"
-                 hash-encoded)))
+  (p/let [salt (doto (js/Uint8Array. 16)
+                 (js/crypto.getRandomValues))
+          crypto-key (.importKey js/crypto.subtle
+                                 "raw"
+                                 (.encode text-encoder password)
+                                 #js {:name "PBKDF2"}
+                                 false
+                                 #js ["deriveBits"])
+          iterations (min password-kdf-iterations password-kdf-max-iterations)
+          derived (.deriveBits js/crypto.subtle
+                               #js {:name "PBKDF2"
+                                    :hash "SHA-256"
+                                    :salt salt
+                                    :iterations iterations}
+                               crypto-key
+                               256)
+          derived-bytes (js/Uint8Array. derived)
+          salt-encoded (bytes->base64url salt)
+          hash-encoded (bytes->base64url derived-bytes)]
+    (str "pbkdf2$sha256$"
+         iterations
+         "$"
+         salt-encoded
+         "$"
+         hash-encoded)))
 
 (defn base64url->uint8array [input]
   (let [pad (if (pos? (mod (count input) 4))
@@ -179,43 +179,43 @@
                  (= "pbkdf2" (nth parts 0))
                  (= "sha256" (nth parts 1)))
       false
-      (js-await [iterations (js/parseInt (nth parts 2))]
-                (if (> iterations password-kdf-max-iterations)
-                  false
-                  (js-await [salt (base64url->uint8array (nth parts 3))
-                             expected (base64url->uint8array (nth parts 4))
-                             crypto-key (.importKey js/crypto.subtle
-                                                    "raw"
-                                                    (.encode text-encoder password)
-                                                    #js {:name "PBKDF2"}
-                                                    false
-                                                    #js ["deriveBits"])
-                             derived (.deriveBits js/crypto.subtle
-                                                  #js {:name "PBKDF2"
-                                                       :hash "SHA-256"
-                                                       :salt salt
-                                                       :iterations iterations}
-                                                  crypto-key
-                                                  (* 8 (.-length expected)))
-                             derived-bytes (js/Uint8Array. derived)]
-                            (if (not= (.-length derived-bytes) (.-length expected))
-                              false
-                              (let [mismatch (reduce (fn [acc idx]
-                                                       (bit-or acc
-                                                               (bit-xor (aget derived-bytes idx)
-                                                                        (aget expected idx))))
-                                                     0
-                                                     (range (.-length expected)))]
-                                (zero? mismatch)))))))))
+      (p/let [iterations (js/parseInt (nth parts 2))]
+        (if (> iterations password-kdf-max-iterations)
+          false
+          (p/let [salt (base64url->uint8array (nth parts 3))
+                  expected (base64url->uint8array (nth parts 4))
+                  crypto-key (.importKey js/crypto.subtle
+                                         "raw"
+                                         (.encode text-encoder password)
+                                         #js {:name "PBKDF2"}
+                                         false
+                                         #js ["deriveBits"])
+                  derived (.deriveBits js/crypto.subtle
+                                       #js {:name "PBKDF2"
+                                            :hash "SHA-256"
+                                            :salt salt
+                                            :iterations iterations}
+                                       crypto-key
+                                       (* 8 (.-length expected)))
+                  derived-bytes (js/Uint8Array. derived)]
+            (if (not= (.-length derived-bytes) (.-length expected))
+              false
+              (let [mismatch (reduce (fn [acc idx]
+                                       (bit-or acc
+                                               (bit-xor (aget derived-bytes idx)
+                                                        (aget expected idx))))
+                                     0
+                                     (range (.-length expected)))]
+                (zero? mismatch)))))))))
 
 (defn hmac-sha256 [key message]
-  (js-await [crypto-key (.importKey js/crypto.subtle
-                                    "raw"
-                                    key
-                                    #js {:name "HMAC" :hash "SHA-256"}
-                                    false
-                                    #js ["sign"])]
-            (.sign js/crypto.subtle "HMAC" crypto-key message)))
+  (p/let [crypto-key (.importKey js/crypto.subtle
+                                 "raw"
+                                 key
+                                 #js {:name "HMAC" :hash "SHA-256"}
+                                 false
+                                 #js ["sign"])]
+    (.sign js/crypto.subtle "HMAC" crypto-key message)))
 
 (defn encode-rfc3986 [value]
   (-> (js/encodeURIComponent value)
@@ -229,66 +229,66 @@
        (string/join "/")))
 
 (defn get-signature-key [secret date-stamp region service]
-  (js-await [k-date (hmac-sha256
-                     (.encode text-encoder (str "AWS4" secret))
-                     (.encode text-encoder date-stamp))
-             k-region (hmac-sha256 k-date (.encode text-encoder region))
-             k-service (hmac-sha256 k-region (.encode text-encoder service))]
-            (hmac-sha256 k-service (.encode text-encoder "aws4_request"))))
+  (p/let [k-date (hmac-sha256
+                  (.encode text-encoder (str "AWS4" secret))
+                  (.encode text-encoder date-stamp))
+          k-region (hmac-sha256 k-date (.encode text-encoder region))
+          k-service (hmac-sha256 k-region (.encode text-encoder service))]
+    (hmac-sha256 k-service (.encode text-encoder "aws4_request"))))
 
 (defn presign-r2-url [r2-key env]
-  (js-await [region "auto"
-             service "s3"
-             host (str (aget env "R2_ACCOUNT_ID") ".r2.cloudflarestorage.com")
-             bucket (aget env "R2_BUCKET")
-             method "GET"
-             now (js/Date.)
-             amz-date (.replace (.toISOString now) #"[ :-]|\.\d{3}" "")
-             date-stamp (.slice amz-date 0 8)
-             credential-scope (str date-stamp "/" region "/" service "/aws4_request")
-             params (->> [["X-Amz-Algorithm" "AWS4-HMAC-SHA256"]
-                          ["X-Amz-Credential" (str (aget env "R2_ACCESS_KEY_ID") "/" credential-scope)]
-                          ["X-Amz-Date" amz-date]
-                          ["X-Amz-Expires" "300"]
-                          ["X-Amz-SignedHeaders" "host"]]
-                         (sort-by first))
-             canonical-query (->> params
-                                  (map (fn [[k v]]
-                                         (str (encode-rfc3986 k) "=" (encode-rfc3986 v))))
-                                  (string/join "&"))
-             canonical-uri (str "/" bucket "/" (encode-path r2-key))
-             canonical-headers (str "host:" host "\n")
-             signed-headers "host"
-             payload-hash "UNSIGNED-PAYLOAD"
-             canonical-request (string/join "\n"
-                                            [method
-                                             canonical-uri
-                                             canonical-query
-                                             canonical-headers
-                                             signed-headers
-                                             payload-hash])
-             canonical-hash (sha256-hex canonical-request)
-             string-to-sign (string/join "\n"
-                                         ["AWS4-HMAC-SHA256"
-                                          amz-date
-                                          credential-scope
-                                          canonical-hash])
-             signing-key (get-signature-key (aget env "R2_SECRET_ACCESS_KEY")
-                                            date-stamp
-                                            region
-                                            service)
-             raw-signature (hmac-sha256 signing-key (.encode text-encoder string-to-sign))
-             signature (to-hex raw-signature)
-             signed-query (str canonical-query "&X-Amz-Signature=" signature)]
-            (str "https://" host canonical-uri "?" signed-query)))
+  (p/let [region "auto"
+          service "s3"
+          host (str (aget env "R2_ACCOUNT_ID") ".r2.cloudflarestorage.com")
+          bucket (aget env "R2_BUCKET")
+          method "GET"
+          now (js/Date.)
+          amz-date (.replace (.toISOString now) #"[ :-]|\.\d{3}" "")
+          date-stamp (.slice amz-date 0 8)
+          credential-scope (str date-stamp "/" region "/" service "/aws4_request")
+          params (->> [["X-Amz-Algorithm" "AWS4-HMAC-SHA256"]
+                       ["X-Amz-Credential" (str (aget env "R2_ACCESS_KEY_ID") "/" credential-scope)]
+                       ["X-Amz-Date" amz-date]
+                       ["X-Amz-Expires" "300"]
+                       ["X-Amz-SignedHeaders" "host"]]
+                      (sort-by first))
+          canonical-query (->> params
+                               (map (fn [[k v]]
+                                      (str (encode-rfc3986 k) "=" (encode-rfc3986 v))))
+                               (string/join "&"))
+          canonical-uri (str "/" bucket "/" (encode-path r2-key))
+          canonical-headers (str "host:" host "\n")
+          signed-headers "host"
+          payload-hash "UNSIGNED-PAYLOAD"
+          canonical-request (string/join "\n"
+                                         [method
+                                          canonical-uri
+                                          canonical-query
+                                          canonical-headers
+                                          signed-headers
+                                          payload-hash])
+          canonical-hash (sha256-hex canonical-request)
+          string-to-sign (string/join "\n"
+                                      ["AWS4-HMAC-SHA256"
+                                       amz-date
+                                       credential-scope
+                                       canonical-hash])
+          signing-key (get-signature-key (aget env "R2_SECRET_ACCESS_KEY")
+                                         date-stamp
+                                         region
+                                         service)
+          raw-signature (hmac-sha256 signing-key (.encode text-encoder string-to-sign))
+          signature (to-hex raw-signature)
+          signed-query (str canonical-query "&X-Amz-Signature=" signature)]
+    (str "https://" host canonical-uri "?" signed-query)))
 
 (defn normalize-etag [etag]
   (when etag
     (string/replace etag #"\"" "")))
 
 (defn short-id-for-page [graph-uuid page-uuid]
-  (js-await [payload (.encode text-encoder (str graph-uuid ":" page-uuid))
-             digest (.digest js/crypto.subtle "SHA-256" payload)]
-            (let [data (js/Uint8Array. digest)
-                  encoded (bytes->base64url data)]
-              (subs encoded 0 10))))
+  (p/let [payload (.encode text-encoder (str graph-uuid ":" page-uuid))
+          digest (.digest js/crypto.subtle "SHA-256" payload)]
+    (let [data (js/Uint8Array. digest)
+          encoded (bytes->base64url data)]
+      (subs encoded 0 10))))

+ 116 - 116
deps/publish/src/logseq/publish/meta_store.cljs

@@ -1,8 +1,8 @@
 (ns logseq.publish.meta-store
   "Handles storing Durable Object in SQLite"
   (:require [clojure.string :as string]
-            [logseq.publish.common :as publish-common])
-  (:require-macros [logseq.common.async :refer [js-await]]))
+            [logseq.publish.common :as publish-common]
+            [promesa.core :as p]))
 
 (defn init-schema! [sql]
   (let [cols (publish-common/get-sql-rows (publish-common/sql-exec sql "PRAGMA table_info(pages);"))
@@ -110,121 +110,121 @@
     (init-schema! sql)
     (cond
       (= "POST" (.-method request))
-      (js-await [body (.json request)]
-                (let [page-uuid (aget body "page_uuid")
-                      graph-uuid (aget body "graph")]
-                  (if (and (string? page-uuid) (string? graph-uuid))
-                    (publish-common/sql-exec sql
-                                             (str "INSERT INTO pages ("
-                                                  "page_uuid,"
-                                                  "page_title,"
-                                                  "page_tags,"
-                                                  "graph_uuid,"
-                                                  "schema_version,"
-                                                  "block_count,"
-                                                  "content_hash,"
-                                                  "content_length,"
-                                                  "r2_key,"
-                                                  "owner_sub,"
-                                                  "owner_username,"
-                                                  "created_at,"
-                                                  "updated_at,"
-                                                  "short_id,"
-                                                  "password_hash"
-                                                  ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
-                                                  " ON CONFLICT(graph_uuid, page_uuid) DO UPDATE SET"
-                                                  " page_uuid=excluded.page_uuid,"
-                                                  " page_title=excluded.page_title,"
-                                                  " page_tags=excluded.page_tags,"
-                                                  " schema_version=excluded.schema_version,"
-                                                  " block_count=excluded.block_count,"
-                                                  " content_hash=excluded.content_hash,"
-                                                  " content_length=excluded.content_length,"
-                                                  " r2_key=excluded.r2_key,"
-                                                  " owner_sub=excluded.owner_sub,"
-                                                  " owner_username=excluded.owner_username,"
-                                                  " updated_at=excluded.updated_at,"
-                                                  " short_id=excluded.short_id,"
-                                                  " password_hash=excluded.password_hash;")
-                                             page-uuid
-                                             (aget body "page_title")
-                                             (aget body "page_tags")
-                                             graph-uuid
-                                             (aget body "schema_version")
-                                             (aget body "block_count")
-                                             (aget body "content_hash")
-                                             (aget body "content_length")
-                                             (aget body "r2_key")
-                                             (aget body "owner_sub")
-                                             (aget body "owner_username")
-                                             (aget body "created_at")
-                                             (aget body "updated_at")
-                                             (aget body "short_id")
-                                             (aget body "password_hash"))
-                    (throw (js/Error. "publish: missing page_uuid or graph")))
-                  (let [refs (aget body "refs")
-                        tagged-nodes (aget body "tagged_nodes")
-                        blocks (aget body "blocks")
-                        graph-uuid (aget body "graph")
-                        page-uuid (aget body "page_uuid")]
-                    (when (and graph-uuid page-uuid)
-                      (publish-common/sql-exec sql
-                                               "DELETE FROM page_refs WHERE graph_uuid = ? AND source_page_uuid = ?;"
-                                               graph-uuid
-                                               page-uuid)
-                      (publish-common/sql-exec sql
-                                               "DELETE FROM page_tags WHERE graph_uuid = ? AND source_page_uuid = ?;"
-                                               graph-uuid
-                                               page-uuid)
-                      (doseq [ref refs]
-                        (publish-common/sql-exec sql
-                                                 (str "INSERT OR REPLACE INTO page_refs ("
-                                                      "graph_uuid, target_page_uuid, target_page_title, target_page_name, source_page_uuid, "
-                                                      "source_page_title, source_block_uuid, source_block_content, "
-                                                      "source_block_format, updated_at"
-                                                      ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")
-                                                 (aget ref "graph_uuid")
-                                                 (aget ref "target_page_uuid")
-                                                 (aget ref "target_page_title")
-                                                 (aget ref "target_page_name")
-                                                 (aget ref "source_page_uuid")
-                                                 (aget ref "source_page_title")
-                                                 (aget ref "source_block_uuid")
-                                                 (aget ref "source_block_content")
-                                                 (aget ref "source_block_format")
-                                                 (aget ref "updated_at")))
+      (p/let [body (.json request)]
+        (let [page-uuid (aget body "page_uuid")
+              graph-uuid (aget body "graph")]
+          (if (and (string? page-uuid) (string? graph-uuid))
+            (publish-common/sql-exec sql
+                                     (str "INSERT INTO pages ("
+                                          "page_uuid,"
+                                          "page_title,"
+                                          "page_tags,"
+                                          "graph_uuid,"
+                                          "schema_version,"
+                                          "block_count,"
+                                          "content_hash,"
+                                          "content_length,"
+                                          "r2_key,"
+                                          "owner_sub,"
+                                          "owner_username,"
+                                          "created_at,"
+                                          "updated_at,"
+                                          "short_id,"
+                                          "password_hash"
+                                          ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
+                                          " ON CONFLICT(graph_uuid, page_uuid) DO UPDATE SET"
+                                          " page_uuid=excluded.page_uuid,"
+                                          " page_title=excluded.page_title,"
+                                          " page_tags=excluded.page_tags,"
+                                          " schema_version=excluded.schema_version,"
+                                          " block_count=excluded.block_count,"
+                                          " content_hash=excluded.content_hash,"
+                                          " content_length=excluded.content_length,"
+                                          " r2_key=excluded.r2_key,"
+                                          " owner_sub=excluded.owner_sub,"
+                                          " owner_username=excluded.owner_username,"
+                                          " updated_at=excluded.updated_at,"
+                                          " short_id=excluded.short_id,"
+                                          " password_hash=excluded.password_hash;")
+                                     page-uuid
+                                     (aget body "page_title")
+                                     (aget body "page_tags")
+                                     graph-uuid
+                                     (aget body "schema_version")
+                                     (aget body "block_count")
+                                     (aget body "content_hash")
+                                     (aget body "content_length")
+                                     (aget body "r2_key")
+                                     (aget body "owner_sub")
+                                     (aget body "owner_username")
+                                     (aget body "created_at")
+                                     (aget body "updated_at")
+                                     (aget body "short_id")
+                                     (aget body "password_hash"))
+            (throw (js/Error. "publish: missing page_uuid or graph")))
+          (let [refs (aget body "refs")
+                tagged-nodes (aget body "tagged_nodes")
+                blocks (aget body "blocks")
+                graph-uuid (aget body "graph")
+                page-uuid (aget body "page_uuid")]
+            (when (and graph-uuid page-uuid)
+              (publish-common/sql-exec sql
+                                       "DELETE FROM page_refs WHERE graph_uuid = ? AND source_page_uuid = ?;"
+                                       graph-uuid
+                                       page-uuid)
+              (publish-common/sql-exec sql
+                                       "DELETE FROM page_tags WHERE graph_uuid = ? AND source_page_uuid = ?;"
+                                       graph-uuid
+                                       page-uuid)
+              (doseq [ref refs]
+                (publish-common/sql-exec sql
+                                         (str "INSERT OR REPLACE INTO page_refs ("
+                                              "graph_uuid, target_page_uuid, target_page_title, target_page_name, source_page_uuid, "
+                                              "source_page_title, source_block_uuid, source_block_content, "
+                                              "source_block_format, updated_at"
+                                              ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);")
+                                         (aget ref "graph_uuid")
+                                         (aget ref "target_page_uuid")
+                                         (aget ref "target_page_title")
+                                         (aget ref "target_page_name")
+                                         (aget ref "source_page_uuid")
+                                         (aget ref "source_page_title")
+                                         (aget ref "source_block_uuid")
+                                         (aget ref "source_block_content")
+                                         (aget ref "source_block_format")
+                                         (aget ref "updated_at")))
 
-                      (doseq [tag tagged-nodes]
-                        (publish-common/sql-exec sql
-                                                 (str "INSERT OR REPLACE INTO page_tags ("
-                                                      "graph_uuid, tag_page_uuid, tag_title, source_page_uuid, "
-                                                      "source_page_title, source_block_uuid, source_block_content, "
-                                                      "source_block_format, updated_at"
-                                                      ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);")
-                                                 (aget tag "graph_uuid")
-                                                 (aget tag "tag_page_uuid")
-                                                 (aget tag "tag_title")
-                                                 (aget tag "source_page_uuid")
-                                                 (aget tag "source_page_title")
-                                                 (aget tag "source_block_uuid")
-                                                 (aget tag "source_block_content")
-                                                 (aget tag "source_block_format")
-                                                 (aget tag "updated_at"))))
-                    (publish-common/sql-exec sql
-                                             "DELETE FROM page_blocks WHERE graph_uuid = ? AND page_uuid = ?;"
-                                             graph-uuid
-                                             page-uuid)
-                    (doseq [block blocks]
-                      (publish-common/sql-exec sql
-                                               (str "INSERT OR REPLACE INTO page_blocks ("
-                                                    "graph_uuid, page_uuid, block_uuid, block_content, updated_at"
-                                                    ") VALUES (?, ?, ?, ?, ?);")
-                                               (aget body "graph")
-                                               (aget block "page_uuid")
-                                               (aget block "block_uuid")
-                                               (aget block "block_content")
-                                               (aget block "updated_at"))))
-                  (publish-common/json-response {:ok true})))
+              (doseq [tag tagged-nodes]
+                (publish-common/sql-exec sql
+                                         (str "INSERT OR REPLACE INTO page_tags ("
+                                              "graph_uuid, tag_page_uuid, tag_title, source_page_uuid, "
+                                              "source_page_title, source_block_uuid, source_block_content, "
+                                              "source_block_format, updated_at"
+                                              ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);")
+                                         (aget tag "graph_uuid")
+                                         (aget tag "tag_page_uuid")
+                                         (aget tag "tag_title")
+                                         (aget tag "source_page_uuid")
+                                         (aget tag "source_page_title")
+                                         (aget tag "source_block_uuid")
+                                         (aget tag "source_block_content")
+                                         (aget tag "source_block_format")
+                                         (aget tag "updated_at"))))
+            (publish-common/sql-exec sql
+                                     "DELETE FROM page_blocks WHERE graph_uuid = ? AND page_uuid = ?;"
+                                     graph-uuid
+                                     page-uuid)
+            (doseq [block blocks]
+              (publish-common/sql-exec sql
+                                       (str "INSERT OR REPLACE INTO page_blocks ("
+                                            "graph_uuid, page_uuid, block_uuid, block_content, updated_at"
+                                            ") VALUES (?, ?, ?, ?, ?);")
+                                       (aget body "graph")
+                                       (aget block "page_uuid")
+                                       (aget block "block_uuid")
+                                       (aget block "block_content")
+                                       (aget block "updated_at"))))
+          (publish-common/json-response {:ok true})))
 
       (= "GET" (.-method request))
       (let [url (js/URL. (.-url request))

File diff suppressed because it is too large
+ 468 - 468
deps/publish/src/logseq/publish/routes.cljs


+ 0 - 1
src/main/frontend/components/header.cljs

@@ -35,7 +35,6 @@
             [logseq.shui.ui :as shui]
             [logseq.shui.util :as shui-util]
             [missionary.core :as m]
-            [promesa.core :as p]
             [reitit.frontend.easy :as rfe]
             [rum.core :as rum]))
 

+ 2 - 24
src/main/frontend/handler/db_based/db_sync.cljs

@@ -39,20 +39,8 @@
     (string? data) (.encode (js/TextEncoder.) data)
     :else (js/Uint8Array. data)))
 
-(defn- decode-snapshot-rows [bytes]
-  (sqlite-util/read-transit-str (.decode snapshot-text-decoder (->uint8 bytes))))
-
-(defn- snapshot-rows-e2ee?
-  [rows]
-  (boolean
-   (some (fn [[_ content _]]
-           (try
-             (let [data (sqlite-util/read-transit-str content)]
-               (and (map? data)
-                    (= :logseq.kv/graph-rtc-e2ee? (:db/ident data))))
-             (catch :default _
-               false)))
-         rows)))
+(defn- decode-snapshot-rows [payload]
+  (sqlite-util/read-transit-str (.decode snapshot-text-decoder (->uint8 payload))))
 
 (defn- frame-len [^js data offset]
   (let [view (js/DataView. (.-buffer data) offset 4)]
@@ -108,16 +96,6 @@
 
 (declare fetch-json)
 
-(defn- fetch-graph-e2ee?
-  [base graph-uuid]
-  (if-not (and (string? base) (string? graph-uuid))
-    false
-    (p/let [resp (fetch-json (str base "/e2ee/graphs/" graph-uuid "/aes-key")
-                             {:method "GET"}
-                             {:response-schema :e2ee/graph-aes-key})
-            encrypted-aes-key (:encrypted-aes-key resp)]
-      (boolean (string? encrypted-aes-key)))))
-
 (declare coerce-http-response)
 
 (defn- fetch-json

+ 0 - 3
src/main/frontend/handler/db_based/sync.cljs

@@ -35,9 +35,6 @@
     (db-sync-handler/<rtc-update-presence! editing-block-uuid)
     (rtc-handler/<rtc-update-presence! editing-block-uuid)))
 
-(defn <rtc-branch-graph! [repo]
-  (rtc-handler/<rtc-branch-graph! repo))
-
 (defn notification-download-higher-schema-graph! [graph-name graph-uuid schema-version]
   (rtc-handler/notification-download-higher-schema-graph! graph-name graph-uuid schema-version))
 

+ 0 - 26
src/main/frontend/worker/db_sync.cljs

@@ -583,14 +583,6 @@
     (p/let [items (p/all (mapv (fn [item] (decrypt-tx-item aes-key item)) tx-data))]
       (vec items))))
 
-(defn- <encrypt-keys-attrs
-  [aes-key keys]
-  (p/all (mapv (fn [[e a v t]]
-                 (if (contains? rtc-const/encrypt-attr-set a)
-                   (p/let [v' (<encrypt-text-value aes-key v)]
-                     [e a v' t])
-                   [e a v t])) keys)))
-
 (defn- <decrypt-keys-attrs
   [aes-key keys]
   (p/all (mapv (fn [[e a v t]]
@@ -599,24 +591,6 @@
                      [e a v' t])
                    (p/resolved [e a v t]))) keys)))
 
-(defn- <encrypt-snapshot-rows
-  [aes-key rows]
-  (if-not (seq rows)
-    (p/resolved [])
-    (p/let [items (p/all
-                   (mapv (fn [[addr content addresses]]
-                           (let [data (ldb/read-transit-str content)]
-                             (p/let [keys' (if (map? data) ; node
-                                             (<encrypt-keys-attrs aes-key (:keys data))
-                                             ;; leaf
-                                             (p/let [result (p/all (map #(<encrypt-keys-attrs aes-key %) data))]
-                                               (vec result)))
-                                     data' (if (map? data) (assoc data :keys keys') keys')
-                                     content' (ldb/write-transit-str data')]
-                               [addr content' addresses])))
-                         rows))]
-      (vec items))))
-
 (defn- <decrypt-snapshot-rows
   [aes-key rows]
   (if-not (seq rows)

+ 2 - 2
src/main/frontend/worker/pipeline.cljs

@@ -455,7 +455,7 @@
 (defn transact-pipeline
   "Compute extra tx-data and block/refs, should ensure it's a pure function and
   doesn't call `d/transact!` or `ldb/transact!`."
-  [repo {:keys [db-after tx-meta _tx-data] :as tx-report}]
+  [{:keys [db-after tx-meta _tx-data] :as tx-report}]
   (when-not (:temp-conn? tx-meta)
     (let [extra-tx-data (compute-extra-tx-data tx-report)
           tx-report* (if (seq extra-tx-data)
@@ -469,7 +469,7 @@
           deleted-block-ids (set (map :db/id deleted-blocks))
           blocks' (remove (fn [b] (deleted-block-ids (:db/id b))) blocks)
           block-refs (when (seq blocks')
-                       (rebuild-block-refs repo tx-report* blocks'))
+                       (rebuild-block-refs tx-report* blocks'))
           tx-id-data (let [db-after (:db-after tx-report*)
                            updated-blocks (remove (fn [b] (contains? deleted-block-ids (:db/id b)))
                                                   (concat pages blocks))

+ 18 - 16
src/main/frontend/worker/rtc/client_op.cljs

@@ -129,18 +129,19 @@
               [:db/add "e" :local-tx t]))]
       (ldb/transact! conn [tx-data]))))
 
-(defn update-local-checksum
-  [repo checksum]
-  {:pre [(some? checksum)]}
-  (let [conn (worker-state/get-client-ops-conn repo)]
-    (assert (some? conn) repo)
-    (let [tx-data
-          (if-let [datom (first (d/datoms @conn :avet :db-sync/checksum))]
-            [:db/add (:e datom) :db-sync/checksum checksum]
-            (if-let [datom (first (d/datoms @conn :avet :local-tx))]
+(comment
+  (defn update-local-checksum
+    [repo checksum]
+    {:pre [(some? checksum)]}
+    (let [conn (worker-state/get-client-ops-conn repo)]
+      (assert (some? conn) repo)
+      (let [tx-data
+            (if-let [datom (first (d/datoms @conn :avet :db-sync/checksum))]
               [:db/add (:e datom) :db-sync/checksum checksum]
-              [:db/add "e" :db-sync/checksum checksum]))]
-      (ldb/transact! conn [tx-data]))))
+              (if-let [datom (first (d/datoms @conn :avet :local-tx))]
+                [:db/add (:e datom) :db-sync/checksum checksum]
+                [:db/add "e" :db-sync/checksum checksum]))]
+        (ldb/transact! conn [tx-data])))))
 
 (defn remove-local-tx
   [repo]
@@ -157,11 +158,12 @@
       ;; (assert (some? r))
       r)))
 
-(defn get-local-checksum
-  [repo]
-  (let [conn (worker-state/get-client-ops-conn repo)]
-    (assert (some? conn) repo)
-    (:v (first (d/datoms @conn :avet :db-sync/checksum)))))
+(comment
+  (defn get-local-checksum
+    [repo]
+    (let [conn (worker-state/get-client-ops-conn repo)]
+      (assert (some? conn) repo)
+      (:v (first (d/datoms @conn :avet :db-sync/checksum))))))
 
 (defn- merge-update-ops
   [op1 op2]

+ 0 - 22
src/test/frontend/handler/db_based/db_sync_test.cljs

@@ -4,28 +4,6 @@
             [frontend.handler.user :as user-handler]
             [promesa.core :as p]))
 
-(deftest download-graph-e2ee-detection-test
-  (async done
-         (-> (p/with-redefs [db-sync/fetch-json (fn [_ _ _]
-                                                  (p/resolved {:encrypted-aes-key "k"}))]
-               (p/let [enabled? (#'db-sync/fetch-graph-e2ee? "http://base" "graph-1")]
-                 (is (true? enabled?))
-                 (done)))
-             (p/catch (fn [e]
-                        (is false (str e))
-                        (done))))))
-
-(deftest download-graph-e2ee-missing-key-test
-  (async done
-         (-> (p/with-redefs [db-sync/fetch-json (fn [_ _ _]
-                                                  (p/resolved {}))]
-               (p/let [enabled? (#'db-sync/fetch-graph-e2ee? "http://base" "graph-1")]
-                 (is (false? enabled?))
-                 (done)))
-             (p/catch (fn [e]
-                        (is false (str e))
-                        (done))))))
-
 (deftest remove-member-request-test
   (async done
          (let [called (atom nil)]

+ 1 - 1
src/test/frontend/worker/db_sync_sim_test.cljs

@@ -360,7 +360,7 @@
             op
             (recur (- remaining weight) rest-ops)))))))
 
-(defn- run-ops! [rng {:keys [conn base-uuid state client]} steps history & {:keys [pick-op-opts]}]
+(defn- run-ops! [rng {:keys [conn base-uuid state]} steps history & {:keys [pick-op-opts]}]
   (dotimes [_ steps]
     (let [{:keys [f name]} (pick-op rng pick-op-opts)
           ;; _ (prn :debug :client (:repo client) :name name)

+ 21 - 18
src/test/frontend/worker/db_sync_test.cljs

@@ -1,5 +1,5 @@
 (ns frontend.worker.db-sync-test
-  (:require [cljs.test :refer [deftest is testing run-test async]]
+  (:require [cljs.test :refer [deftest is testing async]]
             [datascript.core :as d]
             [frontend.common.crypt :as crypt]
             [frontend.worker.db-sync :as db-sync]
@@ -88,23 +88,26 @@
 (deftest encrypt-decrypt-snapshot-rows-test
   (async done
          (-> (p/let [aes-key (crypt/<generate-aes-key)
-                     content (sqlite-util/write-transit-str {:db/id 1
-                                                             :block/title "hello"
-                                                             :block/name "page"})
-                     rows [[1 content nil]]
-                     encrypted (#'db-sync/<encrypt-snapshot-rows aes-key rows)]
-               (is (not= rows encrypted))
-               (let [[_ content* _] (first encrypted)]
-                 (is (string? content*))
-                 (is (not= content content*)))
-               (p/let [decrypted (#'db-sync/<decrypt-snapshot-rows aes-key encrypted)
-                       normalize (fn [content']
-                                   (let [data (ldb/read-transit-str content')]
-                                     (if (map? data)
-                                       (update data :keys (fnil vec []))
-                                       data)))
-                       decoded-original (normalize content)
-                       decoded-decrypted (normalize (nth (first decrypted) 1))]
+                     keys [[1 :block/title "hello" 0]
+                           [1 :block/name "page" 0]
+                           [1 :block/uuid (random-uuid) 0]]
+                     content (sqlite-util/write-transit-str {:keys keys})
+                     encrypted-title (#'db-sync/<encrypt-text-value aes-key "hello")
+                     encrypted-name (#'db-sync/<encrypt-text-value aes-key "page")
+                     encrypted-content (sqlite-util/write-transit-str
+                                        {:keys [[1 :block/title encrypted-title 0]
+                                                [1 :block/name encrypted-name 0]
+                                                [1 :block/uuid (nth (nth keys 2) 2) 0]]})
+                     rows [[1 encrypted-content nil]]]
+               (is (not= content encrypted-content))
+               (p/let [decrypted (#'db-sync/<decrypt-snapshot-rows aes-key rows)
+                       decode (fn [content']
+                                (let [data (ldb/read-transit-str content')]
+                                  (if (map? data)
+                                    (update data :keys (fnil vec []))
+                                    data)))
+                       decoded-original (decode content)
+                       decoded-decrypted (decode (nth (first decrypted) 1))]
                  (is (= decoded-original decoded-decrypted))
                  (done)))
              (p/catch (fn [e]

Some files were not shown because too many files changed in this diff