         res-ch    (chan)]
     (if (.existsSync fs file-name)
       (let [rs      (.createReadStream fs file-name)]
-        (.on rs "data" (fn [chunk] (put! res-ch  (cljs.reader/read-string (.toString chunk)))))
+        (.on rs "data" (fn [chunk] (put! res-ch  (fress/read (. chunk -buffer)))))
         (.on rs "close" #(close! res-ch))
-        (.on rs "error" (fn [err] (close! res-ch) (println "error: " err))))
+        (.on rs "error" (fn [err]
+                          (put! res-ch (ex-info "Could not read edn."
+                                                {:type      :read-edn-error
+                                                 :key       key
+                                                 :exception err}))
+                          (close! res-ch))))
       (do (put! res-ch "File not exist")
           (close! res-ch)))
     res-ch))
+
+(defn read-edn-key [path key]
+  (let [file-name (str path "/meta/" key)
+        res-ch    (chan)]
+    (if (.existsSync fs file-name)
+      (let [rs      (.createReadStream fs file-name)]
+        (.on rs "data" (fn [chunk] (put! res-ch  (fress/read (. chunk -buffer)))))
+        (.on rs "close" #(close! res-ch))
+        (.on rs "error" (fn [err]
+                          (put! res-ch (ex-info "Could not read edn key."
+                                                {:type      :read-edn-key-error
+                                                 :key       key
+                                                 :exception err}))
+                          (close! res-ch))))
+      (do (put! res-ch "File not exist")
+          (close! res-ch)))
+    res-ch))
+
+(defn read-binary
+  "return read stream"
+  [folder key locked-cb]
+  (let [res       (chan)
+        file-id   (uuid key)
+        file-name (str folder "/data/" file-id)]
+    (if (.existsSync fs file-name)
+      (let [rs (.createReadStream fs file-name)]
+        (.on rs "close" #(close! res))
+        (.on rs "error" (fn [err]
+                          (put! res (ex-info "Could not read binary."
+                                                {:type      :read-binary-error
+                                                 :key       key
+                                                 :exception err}))
+                          (close! res)))
+        (go
+          (<! (locked-cb {:read-stream rs}))
+          (put! res "File not exist")
+          (close! res))))
+    res))
+
+(defn list-keys [{:keys [folder]}]
+  (doseq [id (filter #(re-matches #"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" %) (js->clj (.readdirSync fs "/tmp/mystore/meta")))]
+    (go (println (<! (read-edn-key folder id))))))
 
 (defn delete-entry [folder file-name res-ch]
   (let [key-file (str folder "/meta/" file-name)

                                     :locks          (atom {})
                                     :config         config}))))

(comment
  ;create store
  (go (def store (<! (new-fs-store "/tmp/mystore"))))

  (delete-store "mystore")
  ;exists?
  
  (go (println (<! (-assoc-in store [:new] 43))))
  (go (println (<! (-exists? store :new)))) 
  (go (println (<! (-update-in store [:new] inc))))
  (go (println (<! (-get-in store [:new]))))
  (go (println (<! (-dissoc store :new))))


;;Input, Outputstream

(def my-obj (clj->js {:key "123" :format :edn}))

(defn- read-key
  [filename key]
  (when (.existsSync fs path)
    (let [rs (.createReadStream fs path)]
      (.on rs "data" (fn [chunk] (set! scope (.toString chunk))))
      (.on rs "close" #(println "Reading done"))
      (.on rs "error" (fn [err] (println "error: " err))))))










(write-edn-key "/tmp" {:key "123" :format :edn})






(def rc (chan))

(go (println (<! (read-edn "/tmp" "123"))))

(defn read-my-key []
  (let [value (js-obj)]
    (go (set! (.-var value) (<! (read-edn "/tmp" "123"))))
    value))

(read-my-key)

(write-edn "myfile.txt" "Hello World")

(take! (read-edn "/tmp" "123") #(println %))

(def myatom (atom {}))

(poll! (read-edn "/tmp" "123"))

(async/reduce (<! (read-edn "/tmp" "123")))

;read back use -> cljs.reader/read-string
(cljs.reader/read-string (prn-str {:x "123" :y "123"}))


        (println err)))))

(defn write-edn-key [folder {:keys [key] :as meta}]
  (let [key       (uuid (first key))
        temp-file (str folder "/meta/" key ".new")
        new-file  (str folder "/meta/" key)
        fd        (.openSync fs new-file "w+")
        _         (.closeSync fs fd)
        ws        (.createWriteStream fs temp-file)
        res-ch (chan)]
    (.on ws "close" #(try (.renameSync fs temp-file new-file)
                          (catch js/Object err
                            (put! res-ch (ex-info "Could not write edn key."
                                                  {:type      :write-edn-key-error
                                                   :key       key
                                                   :exception err}))
                            (close! res-ch))
                          (finally (close! res-ch))))
    (.on ws "error"
         (fn [err]
           (put! res-ch (ex-info "Could not write edn key."
                                 {:type      :write-edn-key-error
                                  :key       key
                                  :exception err}))
           (close! res-ch)))
    (.write ws (js/Uint8Array. (fress/write meta)))
    (.end ws)
    res-ch))

(defn write-edn [folder key up-fn]
  (let [key       (uuid (first key))
        temp-file (str folder "/data/" key ".new")
        new-file  (str folder "/data/" key)
        res-ch    (chan)]
    (if (.existsSync fs new-file)
      (let [rs (.createReadStream fs new-file)]
        (.on rs "data" (fn [chunk]
                         (let [old (fress/read (. chunk -buffer))
                               ws  (.createWriteStream fs temp-file)
                               value     (up-fn old)]
                           (.on ws "finish" #(put! res-ch [old value]))
                           (.on ws "error"
                                (fn [err]
                                  (put! res-ch (ex-info "Could not write edn."
                                                        {:type      :write-edn-error
                                                         :key       key
                                                         :exception err}))
                                  (close! res-ch)))
                           (.write ws (js/Uint8Array. (fress/write value)))
                           (.close ws))))
        (.on rs "close" (fn [] (.renameSync fs temp-file new-file) (close! res-ch)))
        (.on rs "error" (fn [err]
                          (put! res-ch (ex-info "Could not write edn."
                                                {:type      :write-edn-error
                                                 :key       key
                                                 :exception err}))
                          (close! res-ch))))
      (let [fd        (.openSync fs new-file "w+")
            _         (.closeSync fs fd)
            ws        (.createWriteStream fs temp-file)
            value     (up-fn nil)]
        (.on ws "finish" #(put! res-ch [nil value]))
        (.on ws "close" #(try
                           (.renameSync fs temp-file new-file)
                           (catch js/Object err
                             (put! res-ch (ex-info "Could not write edn."
                                                   {:type      :write-edn-error
                                                    :key       key
                                                    :exception err}))
                             (close! res-ch))))
        (.write ws (js/Uint8Array. (fress/write value)))
        (.end ws)))
    res-ch))

(defn write-binary [folder key input]
  (let [res-ch (chan)]
    (try
      (let [fn        (uuid key)
            temp-file (str folder "/data/" fn ".new")
            new-file  (str folder "/data/" fn)
            ws        (.createWriteStream fs temp-file)
            input     (if (js/Buffer.isBuffer input)
                        (let [stream (stream.PassThrough)
                              _      (.end stream input)]
                          stream)
                        (if (instance? stream input)
                          input
                          (throw (js/Error. "Invalid input type"))))]
        (.on ws "finish" #(println "tempfile writing done"))
        (.on ws "close" #(try
                           (.renameSync fs temp-file new-file)
                           (catch js/Object err
                             (put! res-ch (ex-info "Could not write binary."
                                                   {:type      :write-binary-error
                                                    :key       key
                                                    :exception err})))
                           (finally
                             (.unpipe input ws)
                             (put! res-ch true)
                             (close! res-ch))))
        (.pipe input ws))
      (catch js/Object err
        (put! res-ch (ex-info "Could not write binary."
                              {:type      :write-binary-error
                               :key       key
                               :exception err}))
        (close! res-ch)))
    res-ch))

(defn read-edn [path key]
