forked from ipfs-shipyard/ipfs-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (59 loc) · 2.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict'
const { Buffer } = require('buffer')
const { getIpfs, providers } = require('ipfs-provider')
const { httpClient, jsIpfs, windowIpfs } = providers
document.addEventListener('DOMContentLoaded', async () => {
const { ipfs, provider, apiAddress } = await getIpfs({
// HTTP client library can be defined globally to keep code minimal
// when httpClient provider is used multiple times
loadHttpClientModule: () => require('ipfs-http-client'),
// try window.ipfs (if present),
// then http apis (if up),
// and finally fallback to spawning embedded js-ipfs
providers: [
windowIpfs({
permissions: { commands: ['files.add', 'files.cat'] }
}),
httpClient({
// try multiaddr of a local node
apiAddress: '/ip4/127.0.0.1/tcp/5001'
}),
httpClient(), // try "/api/v0/" on the same Origin as the page
httpClient({
// try URL of a remote node
apiAddress: 'http://dev.local:8080'
}),
jsIpfs({
// js-ipfs package is used only once, here
loadJsIpfsModule: () => require('ipfs'), // note require instead of
options: { } // pass config: https://github.com/ipfs/js-ipfs/blob/master/packages/ipfs/docs/MODULE.md#ipfscreateoptions
})
]
})
console.log('IPFS API is provided by: ' + provider)
if (provider === 'httpClient') {
console.log('HTTP API address: ' + apiAddress)
}
async function store () {
const toStore = document.getElementById('source').value
for await (const file of ipfs.add(toStore)) {
if (file && file.cid) {
console.log('successfully stored', file)
await display(file.cid.toString())
} else {
console.error('unable to add', file)
}
}
}
async function display (cid) {
const chunks = []
for await (const chunk of ipfs.cat(cid)) {
chunks.push(chunk)
}
const data = Buffer.concat(chunks).toString()
document.getElementById('cid').innerText = cid
document.getElementById('content').innerText = data
document.getElementById('output').setAttribute('style', 'display: block')
}
document.getElementById('store').onclick = store
})