From 61fe9add1b239eb271fa2d6bef854869424904bf Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Mon, 1 Jul 2019 15:53:18 +0200 Subject: [PATCH 1/2] Add support to use IPFS gateway to read ipfs data This makes caching of IPFS documents easier. Browsers should cache those by default --- lib/utils/ipfs.js | 9 +++++++-- package.json | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/utils/ipfs.js b/lib/utils/ipfs.js index c4b1beb..6e306de 100644 --- a/lib/utils/ipfs.js +++ b/lib/utils/ipfs.js @@ -1,13 +1,14 @@ const ipfsClient = require('ipfs-http-client'); const multihashes = require('multihashes'); +const fetch = require('node-fetch'); class IPFS { constructor (config) { if (!config) { config = { host: 'localhost', port: '5001', protocol: 'http' }; } - this._ipfsAPI = ipfsClient(config); this._config = config; + this._ipfsAPI = ipfsClient(config); } catAndMerge (data, deserialize) { @@ -38,7 +39,11 @@ class IPFS { if (hashData.hasOwnProperty('hashSize')) { ipfsHash = this.encodeHash(hashData); } - return this._ipfsAPI.cat(ipfsHash); + if (this._config['gatewayUrl']) { + return fetch(`${this._config['gatewayUrl']}/${ipfsHash}`).then(r => r.text()); + } else { + return this._ipfsAPI.cat(ipfsHash); + } } pin (hashData) { diff --git a/package.json b/package.json index dbe1f3e..a931e0f 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "ethers": "^4.0.29", "ipfs-http-client": "^32.0.1", "kosmos-schemas": "^2.1.0", + "node-fetch": "^2.6.0", "tv4": "^1.3.0" }, "keywords": [ -- 2.25.1 From a45c8d14be97874ded8bfa90d2a6606c2155ef46 Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Mon, 1 Jul 2019 16:12:22 +0200 Subject: [PATCH 2/2] Downgrade IPFS package The new version uses generators which breaks the build on kredits-web. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a931e0f..47d73ad 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ }, "dependencies": { "ethers": "^4.0.29", - "ipfs-http-client": "^32.0.1", + "ipfs-http-client": "^30.1.3", "kosmos-schemas": "^2.1.0", "node-fetch": "^2.6.0", "tv4": "^1.3.0" -- 2.25.1