Simplified IPFS access

This commit is contained in:
Julien Eluard 2018-09-25 16:13:20 +02:00
parent 83c9755d32
commit 9628be3fe2
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
4 changed files with 21 additions and 48 deletions

View File

@ -39,7 +39,7 @@
<main>
<div id="selection">
Load demo extension from
<button onclick="load('ipfs@QmSKP6f2uUsFq4mk1Afe4ZktxwQifrLb4xRQYNE1LxidKz')">IPFS</button>
<button onclick="load('ipfs@QmUCnqKmH4vvd5oXyJUkBV1rRnkLMX29iVugoREsaNTWr5')">IPFS</button>
<button onclick="load('url@assets/extensions/demo')">HTTP</button>
<button onclick="load('gist@jeluard/1b41c8194c1658e1b6fc0bde4aa6db95')">Gist</button>
</div>

View File

@ -2,50 +2,23 @@
(:require [clojure.string :as string]
[pluto.storage :as storage]))
(defn- ipfs->extension [ipfs-extension]
{:extension-id (:Hash ipfs-extension)
:name (:Name ipfs-extension)})
(defn result [xhr]
(let [status (.-status xhr)]
(if (= 404 status)
{:type :error :value status}
{:type :success :value [{:content (.-responseText xhr)}]})))
(defn parse-directory [response]
(when-not (string/blank? response)
(->> (js->clj (js/JSON.parse response) :keywordize-keys true)
:Objects
first
:Links
(map ipfs->extension))))
;; TODO Handle all edn files types, not only extension.edn
(defn fetch-promise [url]
(new js/Promise (fn [resolve reject]
(let [xhr (js/XMLHttpRequest.)]
(.open xhr "GET" url true)
(set! (.-timeout xhr) 2000)
(set! (.-ontimeout xhr) #(reject :timeout))
(.send xhr nil)
(set! (.-onload xhr)
#(resolve (.-responseText xhr)))))))
(defn infura-url [hash]
(str "https://ipfs.infura.io/ipfs/" hash))
(defn list-all [gateway-url directory]
(fetch-promise (str gateway-url "/api/v0/ls?arg=" directory)))
(defn fetch
[gateway-url extension]
(..
(fetch-promise (str gateway-url "/api/v0/cat?arg=" (:extension-id extension)))
(then (fn [content]
(assoc extension :content content)))))
(defn fetch-all [gateway-url extensions]
(let [promises (js/Promise.all (clj->js (mapv #(fetch gateway-url %) extensions)))]
(.then promises
#(js->clj % :keywordize-keys true))))
(defrecord IPFSStorage [gateway-url]
(deftype IPFSStorage []
storage/Storage
(fetch [_ extension callback]
(..
(list-all gateway-url (:value extension))
(then parse-directory)
(then (partial fetch-all gateway-url))
(then #(callback {:type :success :value %})
#(callback {:type :error :value %})))))
(fetch [_ {:keys [value]} callback]
(let [xhr (js/XMLHttpRequest.)]
(.open xhr "GET" (infura-url value) true)
(.send xhr nil)
(set! (.-onreadystatechange xhr)
#(when (= (.-readyState xhr) 4)
(callback (result xhr)))))))

View File

@ -8,12 +8,12 @@
(def all
{"url" (http/HTTPStorage.)
"gist" (gist/GistStorage.)
"ipfs" (ipfs/IPFSStorage. "https://gateway.ipfs.io")})
"ipfs" (ipfs/IPFSStorage.)})
(defn fetch [uri cb]
(when (and uri cb))
(when (and uri cb)
(let [[type id] (string/split uri "@")]
(when-let [s (get all type)]
(storage/fetch
s
{:value id} cb))))
{:value id} cb)))))

View File

@ -39,7 +39,7 @@
<main>
<div id="selection">
Load demo extension from
<button onclick="load('ipfs@QmSKP6f2uUsFq4mk1Afe4ZktxwQifrLb4xRQYNE1LxidKz')">IPFS</button>
<button onclick="load('ipfs@QmUCnqKmH4vvd5oXyJUkBV1rRnkLMX29iVugoREsaNTWr5')">IPFS</button>
<button onclick="load('url@assets/extensions/demo')">HTTP</button>
<button onclick="load('gist@jeluard/1b41c8194c1658e1b6fc0bde4aa6db95')">Gist</button>
</div>