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> <main>
<div id="selection"> <div id="selection">
Load demo extension from 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('url@assets/extensions/demo')">HTTP</button>
<button onclick="load('gist@jeluard/1b41c8194c1658e1b6fc0bde4aa6db95')">Gist</button> <button onclick="load('gist@jeluard/1b41c8194c1658e1b6fc0bde4aa6db95')">Gist</button>
</div> </div>

View File

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

View File

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

View File

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