Skip to content

Commit c6ac84c

Browse files
committed
fix: can't export db on browser
1 parent 9145dd4 commit c6ac84c

7 files changed

Lines changed: 45 additions & 28 deletions

File tree

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@
4646
- New properties should be added to `logseq.db.frontend.property/built-in-properties`.
4747
- Avoid creating new class or property unless you have to.
4848
- Avoid shadow var, e.g. `bytes` should be named as `payload`.
49+
- Avoid using `js/Buffer` in browser related code.

src/main/frontend/persist_db/browser.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226

227227
(<import-db [_this repo data]
228228
(->
229-
(p/let [base64 (util/unit8array-to-base64string data)]
229+
(p/let [base64 (util/uint8array-to-base64string data)]
230230
(state/<invoke-db-worker :thread-api/import-db-base64 repo base64))
231231
(p/catch (fn [error]
232232
(log/error :import-db-error repo error "SQLiteDB import error")

src/main/frontend/persist_db/remote.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178

179179
(<import-db [_this repo data]
180180
(->
181-
(p/let [base64 (util/unit8array-to-base64string data)]
181+
(p/let [base64 (util/uint8array-to-base64string data)]
182182
(invoke! client "thread-api/import-db-base64" [repo base64]))
183183
(p/catch (fn [error]
184184
(log/error :import-db-error repo error "SQLiteDB import error")

src/main/frontend/util.cljc

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,7 @@
105105
#?(:cljs (defonce convert-to-letters utils/convertToLetters))
106106
#?(:cljs (defonce hsl2hex utils/hsl2hex))
107107
#?(:cljs (defonce base64string-to-unit8array utils/base64ToUint8Array))
108-
#?(:cljs
109-
(defn unit8array-to-base64string
110-
[payload]
111-
(when payload
112-
(let [buffer (cond
113-
(instance? js/Buffer payload)
114-
payload
115-
116-
(instance? js/Uint8Array payload)
117-
(js/Buffer.from payload)
118-
119-
(instance? js/ArrayBuffer payload)
120-
(js/Buffer.from payload)
121-
122-
:else
123-
(js/Buffer.from payload))]
124-
(.toString buffer "base64")))))
108+
#?(:cljs (defonce uint8array-to-base64string utils/uint8ArrayToBase64))
125109

126110
#?(:cljs (def string-join-path common-util/string-join-path))
127111

src/main/frontend/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,17 @@ export function base64ToUint8Array (base64String) {
514514
return null
515515
}
516516
}
517+
518+
export function uint8ArrayToBase64 (uint8Array) {
519+
try {
520+
let binary = ''
521+
const len = uint8Array.byteLength
522+
for (let i = 0; i < len; i++) {
523+
binary += String.fromCharCode(uint8Array[i])
524+
}
525+
return btoa(binary)
526+
} catch (e) {
527+
console.error('Error converting Uint8Array to base64:', e)
528+
return null
529+
}
530+
}

src/main/frontend/worker/db_core.cljs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -891,10 +891,7 @@
891891
(checkpoint-db! repo db))
892892
(p/let [data (<export-db-file repo)]
893893
(when data
894-
(let [buffer (if (instance? js/Buffer data)
895-
data
896-
(js/Buffer.from data))]
897-
(.toString buffer "base64")))))
894+
(worker-util/uint8array-to-base64string data))))
898895

899896
(def-thread-api :thread-api/export-client-ops-db-base64
900897
[repo]
@@ -914,10 +911,7 @@
914911
(str "client-ops-" repo-path)]]
915912
(p/let [payload (<export-db-file-with-paths repo export-paths)]
916913
(when payload
917-
(let [buffer (if (instance? js/Buffer payload)
918-
payload
919-
(js/Buffer.from payload))]
920-
(.toString buffer "base64"))))))
914+
(worker-util/uint8array-to-base64string payload)))))
921915

922916
(def-thread-api :thread-api/backup-db-sqlite
923917
[repo dst-path]
@@ -939,7 +933,7 @@
939933
[repo base64]
940934
(when-not (string/blank? repo)
941935
(p/let [pool (<get-opfs-pool repo)
942-
data (require-sqlite-payload repo (js/Buffer.from base64 "base64"))
936+
data (require-sqlite-payload repo (worker-util/base64string-to-unit8array base64))
943937
_ (close-db! repo)
944938
_ (<import-db pool data)
945939
_ (start-db! repo {:import-type :sqlite-db})]

src/main/frontend/worker_common/util.cljc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@
3131

3232
#?(:cljs
3333
(do
34+
(defn uint8array-to-base64string
35+
[payload]
36+
(when payload
37+
(try
38+
(let [binary (apply str (map #(char %) payload))]
39+
(base64/encodeString binary false))
40+
(catch :default e
41+
(js/console.error "Error converting Uint8Array to base64:" e)
42+
nil))))
43+
44+
(defn base64string-to-unit8array
45+
[base64-string]
46+
(when base64-string
47+
(try
48+
(let [binary (base64/decodeString base64-string true)
49+
len (.-length binary)
50+
arr (new js/Uint8Array len)]
51+
(doseq [i (range len)]
52+
(aset arr i (.charCodeAt binary i)))
53+
arr)
54+
(catch :default e
55+
(js/console.error "Error converting base64 to Uint8Array:" e)
56+
nil))))
57+
3458
(defn post-message
3559
[type data & {:keys [port]}]
3660
(when-let [worker (or port (when (exists? js/self) js/self))]

0 commit comments

Comments
 (0)