1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

prototype publishing to remote node (ipfs.infura.io)

This commit is contained in:
Matthias Kretschmann 2019-09-05 16:02:47 +02:00
parent 8127174fbd
commit a2f075171c
Signed by: m
GPG Key ID: 606EEEF3C479A91F
6 changed files with 388 additions and 18 deletions

317
client/package-lock.json generated
View File

@ -8533,6 +8533,11 @@
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-2.0.1.tgz",
"integrity": "sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk="
},
"fast-fifo": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fast-fifo/-/fast-fifo-1.0.0.tgz",
"integrity": "sha512-4VEXmjxLj7sbs8J//cn2qhRap50dGzF5n8fjay8mau+Jn4hxSeR3xPFwxMaQq/pDaq7+KQk0PAbC2+nWDkJrmQ=="
},
"fast-glob": {
"version": "2.2.7",
"resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-2.2.7.tgz",
@ -11650,6 +11655,206 @@
}
}
},
"ipfs-http-client": {
"version": "35.1.0",
"resolved": "https://registry.npmjs.org/ipfs-http-client/-/ipfs-http-client-35.1.0.tgz",
"integrity": "sha512-C2726d79vydHRc1GdwIWsWnm29zhV568pmQdNru63StXdaDWeijexL4BwcLQi18/JPudT4s9iLTYU1ixIJIHCQ==",
"requires": {
"abort-controller": "^3.0.0",
"async": "^2.6.1",
"async-iterator-all": "^1.0.0",
"async-iterator-to-pull-stream": "^1.3.0",
"bignumber.js": "^9.0.0",
"bl": "^3.0.0",
"bs58": "^4.0.1",
"buffer": "^5.4.2",
"cids": "~0.7.1",
"concat-stream": "github:hugomrdias/concat-stream#feat/smaller",
"debug": "^4.1.0",
"detect-node": "^2.0.4",
"end-of-stream": "^1.4.1",
"err-code": "^2.0.0",
"explain-error": "^1.0.4",
"flatmap": "0.0.3",
"fs-extra": "^8.1.0",
"glob": "^7.1.3",
"ipfs-block": "~0.8.1",
"ipfs-utils": "^0.1.0",
"ipld-dag-cbor": "~0.15.0",
"ipld-dag-pb": "~0.17.3",
"ipld-raw": "^4.0.0",
"is-ipfs": "~0.6.1",
"is-pull-stream": "0.0.0",
"is-stream": "^2.0.0",
"iso-stream-http": "~0.1.2",
"iso-url": "~0.4.6",
"it-glob": "0.0.4",
"it-to-stream": "^0.1.1",
"iterable-ndjson": "^1.1.0",
"just-kebab-case": "^1.1.0",
"just-map-keys": "^1.1.0",
"kind-of": "^6.0.2",
"ky": "^0.13.0",
"ky-universal": "^0.3.0",
"lru-cache": "^5.1.1",
"multiaddr": "^6.0.6",
"multibase": "~0.6.0",
"multicodec": "~0.5.1",
"multihashes": "~0.4.14",
"ndjson": "github:hugomrdias/ndjson#feat/readable-stream3",
"once": "^1.4.0",
"peer-id": "~0.12.3",
"peer-info": "~0.15.1",
"promise-nodeify": "^3.0.1",
"promisify-es6": "^1.0.3",
"pull-defer": "~0.2.3",
"pull-stream": "^3.6.9",
"pull-stream-to-async-iterator": "^1.0.2",
"pull-to-stream": "~0.1.1",
"pump": "^3.0.0",
"qs": "^6.5.2",
"readable-stream": "^3.1.1",
"stream-to-pull-stream": "^1.7.2",
"tar-stream": "^2.0.1",
"through2": "^3.0.1"
},
"dependencies": {
"async": {
"version": "2.6.3",
"resolved": "https://registry.npmjs.org/async/-/async-2.6.3.tgz",
"integrity": "sha512-zflvls11DCy+dQWzTW2dzuilv8Z5X/pjfmZOWba6TNIVDm+2UDaJmXSOXlasHKfNBs8oo3M0aT50fDEWfKZjXg==",
"requires": {
"lodash": "^4.17.14"
}
},
"bl": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/bl/-/bl-3.0.0.tgz",
"integrity": "sha512-EUAyP5UHU5hxF8BPT0LKW8gjYLhq1DQIcneOX/pL/m2Alo+OYDQAJlHq+yseMP50Os2nHXOSic6Ss3vSQeyf4A==",
"requires": {
"readable-stream": "^3.0.1"
}
},
"buffer": {
"version": "5.4.2",
"resolved": "https://registry.npmjs.org/buffer/-/buffer-5.4.2.tgz",
"integrity": "sha512-iy9koArjAFCzGnx3ZvNA6Z0clIbbFgbdWQ0mKD3hO0krOrZh8UgA6qMKcZvwLJxS+D6iVR76+5/pV56yMNYTag==",
"requires": {
"base64-js": "^1.0.2",
"ieee754": "^1.1.4"
}
},
"concat-stream": {
"version": "github:hugomrdias/concat-stream#057bc7b5d6d8df26c8cf00a3f151b6721a0a8034",
"from": "github:hugomrdias/concat-stream#feat/smaller",
"requires": {
"inherits": "^2.0.3",
"readable-stream": "^3.0.2"
}
},
"debug": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz",
"integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==",
"requires": {
"ms": "^2.1.1"
}
},
"fs-extra": {
"version": "8.1.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz",
"integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==",
"requires": {
"graceful-fs": "^4.2.0",
"jsonfile": "^4.0.0",
"universalify": "^0.1.0"
}
},
"ipfs-utils": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/ipfs-utils/-/ipfs-utils-0.1.0.tgz",
"integrity": "sha512-ukL46N8nxURmTAWC2fUKPLFaHQEvCuMn6mrM/6izEbthB6TCkFTqmUl/dhx5cRJFkYOUTvBzKNo9dnvo+8oAwQ==",
"requires": {
"buffer": "^5.2.1",
"err-code": "^2.0.0",
"fs-extra": "^8.1.0",
"is-buffer": "^2.0.3",
"is-electron": "^2.2.0",
"is-pull-stream": "0.0.0",
"is-stream": "^2.0.0",
"it-glob": "0.0.4",
"kind-of": "^6.0.2",
"pull-stream-to-async-iterator": "^1.0.2",
"readable-stream": "^3.4.0"
}
},
"is-stream": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.0.tgz",
"integrity": "sha512-XCoy+WlUr7d1+Z8GgSuXmpuUFC9fOhRXglJMx+dwLKTkL44Cjd4W1Z5P+BQZpr+cR93aGP4S/s7Ftw6Nd/kiEw=="
},
"jsonfile": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
"integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=",
"requires": {
"graceful-fs": "^4.1.6"
}
},
"lru-cache": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz",
"integrity": "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==",
"requires": {
"yallist": "^3.0.2"
}
},
"ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"pump": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz",
"integrity": "sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==",
"requires": {
"end-of-stream": "^1.1.0",
"once": "^1.3.1"
}
},
"readable-stream": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.4.0.tgz",
"integrity": "sha512-jItXPLmrSR8jmTRmRWJXCnGJsfy85mB3Wd/uINMXA65yrnFo0cPClFIUWzo2najVNSl+mx7/4W8ttlLWJe99pQ==",
"requires": {
"inherits": "^2.0.3",
"string_decoder": "^1.1.1",
"util-deprecate": "^1.0.1"
}
},
"tar-stream": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.1.0.tgz",
"integrity": "sha512-+DAn4Nb4+gz6WZigRzKEZl1QuJVOLtAwwF+WUxy1fJ6X63CaGaUAxJRD2KEn1OMfcbCjySTYpNC6WmfQoIEOdw==",
"requires": {
"bl": "^3.0.0",
"end-of-stream": "^1.4.1",
"fs-constants": "^1.0.0",
"inherits": "^2.0.3",
"readable-stream": "^3.1.1"
}
},
"through2": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/through2/-/through2-3.0.1.tgz",
"integrity": "sha512-M96dvTalPT3YbYLaKaCuwu+j06D/8Jfib0o/PxbVt6Amhv3dUAtW6rTV1jPgJSBG83I/e04Y6xkVdVhSRhi0ww==",
"requires": {
"readable-stream": "2 || 3"
}
}
}
},
"ipfs-http-response": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/ipfs-http-response/-/ipfs-http-response-0.3.1.tgz",
@ -12937,6 +13142,83 @@
"is-object": "^1.0.1"
}
},
"it-glob": {
"version": "0.0.4",
"resolved": "https://registry.npmjs.org/it-glob/-/it-glob-0.0.4.tgz",
"integrity": "sha512-sTMM62VQWRqlMpgbd+x1uTviQY7a8vMLXYmw+KPiV9vmAYuyIr9Sp1QRQ5B/faybf4O9RzMGyQb7eFpqLwsBhQ==",
"requires": {
"fs-extra": "^8.1.0",
"minimatch": "^3.0.4"
},
"dependencies": {
"fs-extra": {
"version": "8.1.0",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-8.1.0.tgz",
"integrity": "sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==",
"requires": {
"graceful-fs": "^4.2.0",
"jsonfile": "^4.0.0",
"universalify": "^0.1.0"
}
},
"jsonfile": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
"integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=",
"requires": {
"graceful-fs": "^4.1.6"
}
}
}
},
"it-to-stream": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/it-to-stream/-/it-to-stream-0.1.1.tgz",
"integrity": "sha512-QQx/58JBvT189imr6fD234F8aVf8EdyQHJR0MxXAOShEWK1NWyahPYIQt/tQG7PId0ZG/6/3tUiVCfw2cq+e1w==",
"requires": {
"buffer": "^5.2.1",
"fast-fifo": "^1.0.0",
"get-iterator": "^1.0.2",
"p-defer": "^3.0.0",
"p-fifo": "^1.0.0",
"readable-stream": "^3.4.0"
},
"dependencies": {
"p-defer": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/p-defer/-/p-defer-3.0.0.tgz",
"integrity": "sha512-ugZxsxmtTln604yeYd29EGrNhazN2lywetzpKhfmQjW/VJmhpDmWbiX+h0zL8V91R0UXkhb3KtPmyq9PZw3aYw=="
},
"readable-stream": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.4.0.tgz",
"integrity": "sha512-jItXPLmrSR8jmTRmRWJXCnGJsfy85mB3Wd/uINMXA65yrnFo0cPClFIUWzo2najVNSl+mx7/4W8ttlLWJe99pQ==",
"requires": {
"inherits": "^2.0.3",
"string_decoder": "^1.1.1",
"util-deprecate": "^1.0.1"
}
}
}
},
"iterable-ndjson": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/iterable-ndjson/-/iterable-ndjson-1.1.0.tgz",
"integrity": "sha512-OOp1Lb0o3k5MkXHx1YaIY5Z0ELosZfTnBaas9f8opJVcZGBIONA2zY/6CYE+LKkqrSDooIneZbrBGgOZnHPkrg==",
"requires": {
"string_decoder": "^1.2.0"
},
"dependencies": {
"string_decoder": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz",
"integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==",
"requires": {
"safe-buffer": "~5.2.0"
}
}
}
},
"jest": {
"version": "24.7.1",
"resolved": "https://registry.npmjs.org/jest/-/jest-24.7.1.tgz",
@ -14714,6 +14996,20 @@
"integrity": "sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==",
"dev": true
},
"ky": {
"version": "0.13.0",
"resolved": "https://registry.npmjs.org/ky/-/ky-0.13.0.tgz",
"integrity": "sha512-qmyUE2IvwLveZEHiLmFzUIrW4EsSX18ItZgEfgx8JXFxSEPKrnXvWrjQDP6zi4mwcie3MWBEHyUg0aNF8OS9oA=="
},
"ky-universal": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/ky-universal/-/ky-universal-0.3.0.tgz",
"integrity": "sha512-CM4Bgb2zZZpsprcjI6DNYTaH3oGHXL2u7BU4DK+lfCuC4snkt9/WRpMYeKbBbXscvKkeqBwzzjFX2WwmKY5K/A==",
"requires": {
"abort-controller": "^3.0.0",
"node-fetch": "^2.6.0"
}
},
"last-call-webpack-plugin": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/last-call-webpack-plugin/-/last-call-webpack-plugin-3.0.0.tgz",
@ -18121,6 +18417,22 @@
"p-reduce": "^1.0.0"
}
},
"p-fifo": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/p-fifo/-/p-fifo-1.0.0.tgz",
"integrity": "sha512-IjoCxXW48tqdtDFz6fqo5q1UfFVjjVZe8TC1QRflvNUJtNfCUhxOUw6MOVZhDPjqhSzc26xKdugsO17gmzd5+A==",
"requires": {
"fast-fifo": "^1.0.0",
"p-defer": "^3.0.0"
},
"dependencies": {
"p-defer": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/p-defer/-/p-defer-3.0.0.tgz",
"integrity": "sha512-ugZxsxmtTln604yeYd29EGrNhazN2lywetzpKhfmQjW/VJmhpDmWbiX+h0zL8V91R0UXkhb3KtPmyq9PZw3aYw=="
}
}
},
"p-finally": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz",
@ -19818,6 +20130,11 @@
"integrity": "sha1-mEcocL8igTL8vdhoEputEsPAKeM=",
"dev": true
},
"promise-nodeify": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/promise-nodeify/-/promise-nodeify-3.0.1.tgz",
"integrity": "sha512-ghsSuzZXJX8iO7WVec2z7GI+Xk/EyiD+JZK7AZKhUqYfpLa/Zs4ylUD+CwwnKlG6G3HnkUPMAi6PO7zeqGKssg=="
},
"promise-timeout": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/promise-timeout/-/promise-timeout-1.3.0.tgz",

View File

@ -24,6 +24,7 @@
"filesize": "^4.1.2",
"history": "^4.9.0",
"ipfs": "^0.37.1",
"ipfs-http-client": "^35.1.0",
"is-url-superb": "^3.0.0",
"moment": "^2.24.0",
"query-string": "^6.8.2",

View File

@ -0,0 +1 @@
declare module 'ipfs-http-client'

View File

@ -0,0 +1,48 @@
import { useEffect, useState } from 'react'
import ipfsClient from 'ipfs-http-client'
let ipfs: any = null
let ipfsMessage = ''
let ipfsVersion = ''
export default function useIpfsApi(config: {
host: string
port: string
protocol: string
}) {
const [isIpfsReady, setIpfsReady] = useState(Boolean(ipfs))
const [ipfsError, setIpfsError] = useState(null)
async function initIpfs() {
ipfsMessage = 'Checking IPFS gateway...'
if (ipfs) return
try {
ipfs = await ipfsClient(config)
const version = await ipfs.version()
ipfsVersion = version.version
ipfsMessage = 'Connected to IPFS gateway'
} catch (error) {
setIpfsError(error.message)
}
setIpfsReady(Boolean(ipfs))
}
useEffect(() => {
initIpfs()
// just like componentWillUnmount()
return function cleanup() {
if (ipfs) {
setIpfsReady(false)
ipfs = null
ipfsMessage = ''
ipfsVersion = ''
setIpfsError(null)
}
}
}, [])
return { ipfs, ipfsVersion, isIpfsReady, ipfsError, ipfsMessage }
}

View File

@ -11,7 +11,7 @@ let ipfsVersion = ''
export default function useIpfs() {
const [isIpfsReady, setIpfsReady] = useState(Boolean(ipfs))
const [ipfsInitError, setIpfsInitError] = useState(null)
const [ipfsError, setIpfsError] = useState(null)
async function startIpfs() {
ipfsMessage = 'Starting IPFS...'
@ -46,7 +46,7 @@ export default function useIpfs() {
ipfsMessage = message
console.error(message)
ipfs = null
setIpfsInitError(error.message)
setIpfsError(error.message)
}
}
setIpfsReady(Boolean(ipfs))
@ -64,11 +64,11 @@ export default function useIpfs() {
ipfs = null
ipfsMessage = ''
ipfsVersion = ''
setIpfsInitError(null)
setIpfsError(null)
console.timeEnd('IPFS stopped')
}
}
}, [])
return { ipfs, ipfsVersion, isIpfsReady, ipfsInitError, ipfsMessage }
return { ipfs, ipfsVersion, isIpfsReady, ipfsError, ipfsMessage }
}

View File

@ -2,7 +2,7 @@
import React, { useState } from 'react'
import axios from 'axios'
import useIpfs from '../../../hooks/use-ipfs'
import useIpfsApi from '../../../hooks/use-ipfs-api'
import Label from '../../../components/atoms/Form/Label'
import Spinner from '../../../components/atoms/Spinner'
import styles from './Ipfs.module.scss'
@ -30,39 +30,44 @@ function formatBytes(a: number, b: number) {
}
export default function Ipfs({ addFile }: { addFile(url: string): void }) {
const config = {
host: 'ipfs.infura.io',
port: '5001',
protocol: 'https'
}
const {
ipfs,
ipfsVersion,
isIpfsReady,
ipfsInitError,
ipfsError,
ipfsMessage
} = useIpfs()
} = useIpfsApi(config)
const [loading, setLoading] = useState(false)
const [message, setMessage] = useState('')
async function saveToIpfs(buffer: Buffer) {
setLoading(true)
setMessage('Adding to local IPFS node<br />')
setMessage('Adding to remote IPFS node<br />')
try {
const response = await ipfs.add(buffer, {
progress: (length: number) => {
progress: (length: number) =>
setMessage(
`Adding to local IPFS node<br />
`Adding to remote IPFS node<br />
${formatBytes(length, 0)}`
)
}
})
const cid = response[0].hash
console.log(`File added: ${cid}`)
// Ping gateway url to make it globally available.
// Using gateway.ipfs.io is faster for initial ping,
// but we store ipfs.io url in DDO.
// Ping gateway url to make it globally available,
// but store ipfs.io url in DDO.
// https://ipfs.github.io/public-gateway-checker/
const url = `https://ipfs.io/ipfs/${cid}`
const urlGateway = `https://gateway.ipfs.io/ipfs/${cid}`
const urlGateway = `https://ipfs.infura.io/ipfs/${cid}`
setMessage('Checking IPFS gateway URL')
await pingUrl(urlGateway)
@ -107,9 +112,7 @@ export default function Ipfs({ addFile }: { addFile(url: string): void }) {
{ipfsMessage}
</div>
)}
{ipfsInitError && (
<div className={styles.error}>{ipfsInitError}</div>
)}
{ipfsError && <div className={styles.error}>{ipfsError}</div>}
</div>
)
}