;; The Node flavour of the Helia block/data stores.
;;
;; lib/log is shared with the browser builds of the same room stack, so it may
;; not name a store package at all: `blockstore-fs`/`datastore-fs` drag node:fs
;; into anything that requires them, and the browser's IndexedDB-backed pair is a
;; different package entirely. lib/log's `open-helia` therefore takes stores that
;; are ALREADY OPEN, and this namespace is the Node side that builds them.
;;
;; `open-fs-helia` keeps the store-PATHS signature v1.0.0 published as
;; lib/log.ts's `openHelia`, and is what ardegazu.rooms.internals re-exports
;; under that name — dist/lib/log.js's `openHelia` is unchanged for consumers.
(ns ardegazu.rooms.node.stores
(:require ["blockstore-fs" :refer (FsBlockstore)]
["datastore-fs" :refer (FsDatastore)]
[ardegazu.rooms.lib.log :as log]
[shadow.cljs.modern :refer (js-await)]))
(defn open-fs-stores
"Construct and open the filesystem-backed pair from a {blocks, data} object of
PATHS. Two apps must not share a blockstore (see lib/log's open-helia), so
derive both from your own state directory. Resolves to #js [blockstore
datastore], both open."
[stores]
(let [blockstore (FsBlockstore. (unchecked-get stores "blocks"))
datastore (FsDatastore. (unchecked-get stores "data"))]
(js-await [_ (.open ^js blockstore)]
(js-await [_ (.open ^js datastore)]
#js [blockstore datastore]))))
(defn open-fs-helia
"lib/log's open-helia behind the historical store-PATHS signature: open the
filesystem stores named by `stores` {blocks, data}, then hand them to Helia."
[libp2p stores]
(js-await [pair (open-fs-stores stores)]
(log/open-helia libp2p (aget pair 0) (aget pair 1))))