This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.js
81 lines (64 loc) · 1.87 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* eslint-disable no-console */
'use strict'
const ipfsClient = require('ipfs-client')
let ipfs
const COLORS = {
active: 'blue',
success: 'green',
error: 'red'
}
const showStatus = (text, bg) => {
console.info(text)
const log = document.getElementById('output')
if (!log) {
return
}
const line = document.createElement('p')
line.innerText = text
line.style.color = bg
log.appendChild(line)
}
async function * streamFiles () {
for (let i = 0; i < 100; i++) {
await new Promise((resolve) => {
setTimeout(() => resolve(), 100)
})
showStatus(`Sending /file-${i}.txt`, COLORS.active)
yield {
path: `/file-${i}.txt`,
content: `file ${i}`
}
}
}
async function main (grpcApi, httpApi) {
showStatus(`Connecting to ${grpcApi} using ${httpApi} as fallback`, COLORS.active)
ipfs = ipfsClient({
grpc: grpcApi,
http: httpApi
})
const id = await ipfs.id()
showStatus(`Daemon active\nID: ${id.id}`, COLORS.success)
for await (const file of ipfs.addAll(streamFiles(), {
wrapWithDirectory: true,
// this is just to show the interleaving of uploads and progress events
// otherwise we'd have to upload 50 files before we see any response from
// the server. do not specify this so low in production as you'll have
// greatly degraded import performance
fileImportConcurrency: 1,
progress: (bytes, file) => {
showStatus(`File progress ${file} ${bytes}`, COLORS.active)
}
})) {
showStatus(`Added file: ${file.path} ${file.cid}`, COLORS.success)
}
showStatus('Finished!', COLORS.success)
}
// Event listeners
document.getElementById('connect-submit').onclick = (e) => {
e.preventDefault()
main(document.getElementById('grpc-input').value, document.getElementById('http-input').value)
.catch(err => {
showStatus(err.message, COLORS.error)
console.error(err)
})
}